| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
|
| 4 |
=head1 NAME |
| 5 |
|
| 6 |
bp_download_query_genbank - script to query Genbank and retrieve records |
| 7 |
|
| 8 |
=head1 USAGE |
| 9 |
|
| 10 |
bp_download_query_genbank --query "Neurospora[ORGN]" --db nucest -o Ncrassa_ESTs.fa --format fasta |
| 11 |
|
| 12 |
bp_download_query_genbank --queryfile 'filewithquery' --db nucest -o Ncrassa_ESTs.fa --format fasta |
| 13 |
|
| 14 |
=head2 Other options |
| 15 |
|
| 16 |
Provide ONE of: |
| 17 |
|
| 18 |
-q --query query string OR |
| 19 |
--queryfile profile file with query OR |
| 20 |
--gi --gis --gifile file with list of GIs to download |
| 21 |
|
| 22 |
Database type: |
| 23 |
|
| 24 |
-d --db database (nucleotide [default], nucest, protein, ) |
| 25 |
|
| 26 |
-o --out --outfile output file (results are displayed on screen otherwise) |
| 27 |
-f --format sequence file output format (fasta by default) |
| 28 |
-v --verbose debugging output |
| 29 |
|
| 30 |
=head2 Query options |
| 31 |
|
| 32 |
--maxids maximum number of IDs to retrieve in a set (100 at a time by default) |
| 33 |
--reldate |
| 34 |
--maxdate maxdate for a record |
| 35 |
--mindate minimum date for record |
| 36 |
--datetype edat or mdat (entered or modified) |
| 37 |
|
| 38 |
=head1 AUTHOR Jason Stajich |
| 39 |
|
| 40 |
Jason Stajich, jason-AT-bioperl.org |
| 41 |
|
| 42 |
=cut |
| 43 |
|
| 44 |
use strict; |
| 45 |
use warnings; |
| 46 |
use Bio::DB::GenBank; |
| 47 |
use Bio::DB::GenPept; |
| 48 |
use Bio::DB::Query::GenBank; |
| 49 |
use Bio::SeqIO; |
| 50 |
use Getopt::Long; |
| 51 |
|
| 52 |
my ($queryfile,$outfile,$format,$debug,%options); |
| 53 |
|
| 54 |
$format = 'fasta'; |
| 55 |
|
| 56 |
$options{'-maxids'} = '100'; |
| 57 |
$options{'-db'} = 'nucleotide'; # can be nucleotide, nucest, protein |
| 58 |
my $gifile; |
| 59 |
GetOptions( |
| 60 |
'h|help' => sub { exec('perldoc', $0); |
| 61 |
exit(0); |
| 62 |
}, |
| 63 |
'v|verbose' => \$debug, |
| 64 |
'f|format:s' => \$format, |
| 65 |
'queryfile:s' => \$queryfile, |
| 66 |
'o|out|outfile:s' => \$outfile, |
| 67 |
'gi|gifile|gis:s' => \$gifile, |
| 68 |
# DB::Query options |
| 69 |
'd|db:s' => \$options{'-db'}, |
| 70 |
'mindate:s' => \$options{'-mindate'}, |
| 71 |
'maxdate:s' => \$options{'-maxdate'}, |
| 72 |
'reldate:s' => \$options{'-reldate'}, |
| 73 |
'datetype:s' => \$options{'-datetype'}, # edat or mdat |
| 74 |
'maxids:i' => \$options{'-maxids'}, |
| 75 |
'q|query:s' => \$options{'-query'}, |
| 76 |
); |
| 77 |
|
| 78 |
my $out; |
| 79 |
|
| 80 |
if( $outfile ) { |
| 81 |
$out = Bio::SeqIO->new(-format => $format, |
| 82 |
-file => ">$outfile"); |
| 83 |
} else { |
| 84 |
$out = Bio::SeqIO->new(-format => $format); # write to STDOUT |
| 85 |
} |
| 86 |
|
| 87 |
my $dbh; |
| 88 |
if( $options{'-db'} eq 'protein' ) { |
| 89 |
$dbh = Bio::DB::GenPept->new(-verbose => $debug); |
| 90 |
} else { |
| 91 |
$dbh = Bio::DB::GenBank->new(-verbose => $debug); |
| 92 |
} |
| 93 |
my $query; |
| 94 |
if( $gifile ) { |
| 95 |
my @ids; |
| 96 |
open my $fh, '<', $gifile or die "Could not read file '$gifile': $!\n"; |
| 97 |
while(<$fh>) { |
| 98 |
push @ids, split; |
| 99 |
} |
| 100 |
close $fh; |
| 101 |
while( @ids ) { |
| 102 |
my @mini_ids = splice(@ids, 0, $options{'-maxids'}); |
| 103 |
$query = Bio::DB::Query::GenBank->new(%options, |
| 104 |
-verbose =>$debug, |
| 105 |
-ids => \@mini_ids, |
| 106 |
); |
| 107 |
my $stream = $dbh->get_Stream_by_query($query); |
| 108 |
while( my $seq = $stream->next_seq ) { |
| 109 |
$out->write_seq($seq); |
| 110 |
} |
| 111 |
} |
| 112 |
exit; |
| 113 |
} elsif( $options{'-query'}) { |
| 114 |
$query = Bio::DB::Query::GenBank->new(%options,-verbose => $debug); |
| 115 |
} elsif( $queryfile ) { |
| 116 |
open my $fh, '<', $queryfile or die "Could not read file '$queryfile': $!\n"; |
| 117 |
while(<$fh>) { |
| 118 |
chomp; |
| 119 |
$options{'-query'} .= $_; |
| 120 |
} |
| 121 |
$query = Bio::DB::Query::GenBank->new(%options,-verbose => $debug); |
| 122 |
close $fh; |
| 123 |
} else { |
| 124 |
die("no query string or gifile\n"); |
| 125 |
} |
| 126 |
my $stream = $dbh->get_Stream_by_query($query); |
| 127 |
while( my $seq = $stream->next_seq ) { |
| 128 |
$out->write_seq($seq); |
| 129 |
} |