comparison fa-extract-few.pl @ 0:bcb73a63ffee draft

Uploaded
author simon-gladman
date Tue, 25 Jun 2013 01:48:39 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bcb73a63ffee
1 #!/usr/bin/perl -w
2 use strict;
3 use Bio::SeqIO;
4
5 my(@Options, $verbose, $inverse, $file);
6 setOptions();
7
8 my $in = Bio::SeqIO->new(-file=>$file, -format=>'Fasta');
9 my $out = Bio::SeqIO->new(-fh=>\*STDOUT, -format=>'Fasta');
10 my $nread=0;
11 my $nwrote=0;
12
13 my $pattern = join('|', @ARGV);
14
15 while (my $seq = $in->next_seq) {
16 $nread++;
17 my $match = ($seq->description =~ m/($pattern)/ or $seq->display_id =~ m/($pattern)/);
18 #print STDERR "Found match: ",$seq->display_id, " ", $seq->description, "\n" if $verbose;
19 if ($match ^ $inverse) { # rare use for XOR !
20 $out->write_seq($seq);
21 $nwrote++;
22 }
23 }
24
25 #print STDERR "Read $nread sequences, wrote $nwrote, with pattern: $pattern\n";
26 exit(0);
27 #----------------------------------------------------------------------
28 # Option setting routines
29
30 sub setOptions {
31 use Getopt::Long;
32
33 @Options = (
34 {OPT=>"h|help", VAR=>\&usage, DESC=>"This help"},
35 {OPT=>"verbose!", VAR=>\$verbose, DEFAULT=>0, DESC=>"Verbose"},
36 {OPT=>"v|inverse!", VAR=>\$inverse, DEFAULT=>0, DESC=>"Output NON-matching sequences instead"},
37 {OPT=>"f|file=s", VAR=>\$file, DEFAULT=>"", DESC=>"The fasta file to extract sequences from"},
38 );
39
40 (!@ARGV) && (usage());
41
42 &GetOptions(map {$_->{OPT}, $_->{VAR}} @Options) || usage();
43
44 # Now setup default values.
45 foreach (@Options) {
46 if (defined($_->{DEFAULT}) && !defined(${$_->{VAR}})) {
47 ${$_->{VAR}} = $_->{DEFAULT};
48 }
49 }
50 }
51
52 sub usage {
53 print "Usage: $0 [options] id1 [id2 ...] < input.fasta > output.fasta\n";
54 foreach (@Options) {
55 printf " --%-13s %s%s.\n",$_->{OPT},$_->{DESC},
56 defined($_->{DEFAULT}) ? " (default '$_->{DEFAULT}')" : "";
57 }
58 exit(1);
59 }
60
61 #----------------------------------------------------------------------