~ubuntu-branches/ubuntu/trusty/bioperl/trusty

« back to all changes in this revision

Viewing changes to scripts/utilities/bp_download_query_genbank.pl

  • Committer: Package Import Robot
  • Author(s): Charles Plessy
  • Date: 2013-09-22 13:39:48 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20130922133948-c6z62zegjyp7ztou
Tags: 1.6.922-1
* New upstream release.
* Replaces and Breaks grinder (<< 0.5.3-3~) because of overlaping contents.
  Closes: #722910
* Stop Replacing and Breaking bioperl ( << 1.6.9 ): not needed anymore. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 ) || die $!;
 
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
                                                                                                                  -ids => \@mini_ids,
 
105
                                                                                                                 );
 
106
                my $stream = $dbh->get_Stream_by_query($query);
 
107
                while( my $seq = $stream->next_seq ) {
 
108
                        $out->write_seq($seq);
 
109
                }
 
110
        }
 
111
        exit;
 
112
} elsif( $options{'-query'}) {
 
113
        $query = Bio::DB::Query::GenBank->new(%options);
 
114
} elsif( $queryfile ) {
 
115
        open(my $fh => $queryfile) || die $!;
 
116
        while(<$queryfile>) {
 
117
                chomp;
 
118
                $options{'-query'} .= $_;
 
119
        }
 
120
        $query = Bio::DB::Query::GenBank->new(%options);
 
121
        close($fh);
 
122
} else {
 
123
        die("no query string or gifile\n");
 
124
}
 
125
my $stream = $dbh->get_Stream_by_query($query);
 
126
while( my $seq = $stream->next_seq ) {
 
127
        $out->write_seq($seq);
 
128
}