Mercurial > repos > ucsb-phylogenetics > osiris_phylogenetics
annotate getdata/get_gb_sp.pl @ 0:5b9a38ec4a39 draft default tip
First commit of old repositories
author | osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu> |
---|---|
date | Tue, 11 Mar 2014 12:19:13 -0700 |
parents | |
children |
rev | line source |
---|---|
0
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
2 use strict; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
3 no warnings; #genbank produces annoying warning if no sequence is found |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
4 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
5 #use FindBin; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
6 #use lib "$FindBin::Bin/lib"; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
7 use Bio::DB::GenBank; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
8 use Bio::SeqIO; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
9 use Bio::Root::Exception; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
10 use Error qw(:try); |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
11 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
12 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
13 my $datafile = $ARGV[0]; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
14 my $datatype = $ARGV[1]; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
15 my $outtype = $ARGV[2]; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
16 my $outfile = $ARGV[3]; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
17 my $nodata = $ARGV[4]; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
18 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
19 my $accessions; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
20 my @accnums; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
21 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
22 open (FILE,"<$datafile") or die "Cannot open file containing accession numbers\n"; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
23 open (OUT,">$outfile") or die "Cannot open outfile\n"; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
24 close OUT; #This overwrites old file if it exists |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
25 open (ND,">$nodata") or die "Cannot open file\n"; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
26 my $fh = Bio::SeqIO->newFh(-format=>$outtype, -file=>">>$outfile"); |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
27 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
28 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
29 while (<FILE>) |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
30 { |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
31 chomp; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
32 next unless ($_); |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
33 push(@accnums, $_); |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
34 } |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
35 close FILE; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
36 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
37 my $countnames = 0; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
38 foreach (@accnums){ |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
39 #Should check input for one word per line and throw error if not, which is not done |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
40 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
41 $accessions = $_; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
42 chomp; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
43 if($accessions eq ""){ |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
44 die "Put spaces between accession numbers. No Empty Lines allowed.\n"; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
45 } |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
46 my $qry_string .= $accessions."[organism]"." "; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
47 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
48 # my $GBseq; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
49 my $gb = new Bio::DB::GenBank; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
50 my $query = Bio::DB::Query::GenBank->new |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
51 (-query =>$qry_string, |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
52 -db =>$datatype); |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
53 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
54 my $seqio; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
55 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
56 if (eval {$gb->get_Stream_by_query($query)}){ |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
57 $seqio = $gb->get_Stream_by_query($query); |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
58 while( my $GBseq = $seqio->next_seq ) { |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
59 my $sequence = $GBseq; # read a sequence object |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
60 print $fh $sequence; # write a sequence object |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
61 } |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
62 }else{ |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
63 print ND "$accessions\n"; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
64 } |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
65 } |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
66 exit; |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
67 |
5b9a38ec4a39
First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff
changeset
|
68 |