-
Notifications
You must be signed in to change notification settings - Fork 1
/
getFastas.pl
263 lines (218 loc) · 6.99 KB
/
getFastas.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Retrieves all protein sequences for the genomes specified in $taxaFile
# by querying the NCBI nuccore database and its links to the protein database.
# The sequences are saved as FASTA files, one file per taxon, in $projectDir
# (Actually, the accessions in $genomeFile need not be genomes -
# all that is required is for the Nucleotide GI to be a valid record, and to
# have links to the Protein database.)
# the format for $taxaFile:
# <taxon id> <nucleotide GI 1> <nucleotide GI 2> [...] [<nucleotide GI n>]
# <taxon id> na
# If the genome for a particular taxon is
# incomplete, you can type "na" next to the taxID, and the program will retrieve
# protein sequences directly from the nr protein database. The disadvantage
# of retrieval directly from nr is that there may be organellar sequences.
# Using the links to nr from RefSeq
# chromosome nucleotide sequences is the preferred method of retrieval,
# if the genome is complete.
use lib '/nv/hp10/jstern7/perl5reinstall/lib';
use lib '/nv/hp10/jstern7/perl5reinstall/lib/perl5';
use Data::Dumper;
use HTTP::CookieJar::LWP ();
use LWP::UserAgent ();
$projectDir = "/home/ec2-user/STORI/universal20150110";
#get taxa list
$taxaFile = "/home/ec2-user/STORI/taxids_GIs.txt"; #a list of taxids and Genome GIs
$userAgent = "www.github.com/jgstern"; #this is how you will be represented to NIH's server
if (!(-d $projectDir)) {
my $cmd = "mkdir $projectDir";
system($cmd);
}
$blastDirPath = $projectDir . "/blast";
if (!(-d $blastDirPath)) {
my $cmd = "mkdir $blastDirPath";
system($cmd);
}
$fastaDirPath = $projectDir . "/fasta";
if (!(-d $fastaDirPath)) {
my $cmd = "mkdir $fastaDirPath";
system($cmd);
}
my %accessionHash=();
open (TAXA, $taxaFile);
@fileArr=<TAXA>;
close TAXA;
$count=0;
foreach my $line (@fileArr) {
#print $line;
my @lineArr=split / /, $line;
my $taxID = shift(@lineArr);
my @temp = ();
SETLOOP: foreach my $elt (@lineArr) {
if ($elt =~ m/^na$/) {
$accessionHash{$taxID} = "na";
last SETLOOP;
}
elsif ($elt =~ m/^(.+)$/) {
push @temp, $1;
}
}
$accessionHash{$taxID} = [@temp] if ($#temp >= 0);
}
#print Dumper \%accessionHash;
my %retrievalReport=();
foreach my $taxon (keys %accessionHash) {
my $filename = $fastaDirPath . "/" . $taxon . ".fasta";
my $outcome;
if ($accessionHash{$taxon} eq "na") {
$outcome = RetrieveFasta_nr_nofilter($taxon, $filename);
}
else {
$outcome = RetrieveFasta_linked($taxon, $filename);
}
$retrievalReport{$taxon} = $outcome;
}
print Dumper \%retrievalReport;
sub MyUrlGetter {
my $jar = HTTP::CookieJar::LWP->new;
my $ua = LWP::UserAgent->new(
cookie_jar => $jar,
protocols_allowed => ['http', 'https'],
timeout => 10,
);
$ua->env_proxy;
$ua->agent($userAgent);
my $url = shift(@_);
my $response = $ua->get($url);
if ($response->is_success) {
return $response->decoded_content;
}
else {
return $response->status_line;
}
}
sub RetrieveFasta_linked {
my $taxon = shift(@_);
my $filename = shift(@_);
my @giArr = @{$accessionHash{$taxon}};
my $total=0;
my $successful=0;
my $efetch_out="";
foreach my $id (@giArr) {
# Download protein records linked to nucleotide records.
my $db1 = 'nuccore'; # &dbfrom
my $db2 = 'protein'; # &db
my $linkname = 'nuccore_protein'; # desired link &linkname
#input UIDs in $db1 (protein GIs)
#assemble the elink URL
$base = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/';
$url = $base . "elink.fcgi?dbfrom=$db1&db=$db2&id=$id";
$url .= "&linkname=$linkname&cmd=neighbor_history";
#post the elink URL
print "$url\n";
$output = MyUrlGetter($url);
#parse WebEnv and QueryKey
$web = $1 if ($output =~ /<WebEnv>(\S+)<\/WebEnv>/);
$key = $1 if ($output =~ /<QueryKey>(\d+)<\/QueryKey>/);
### include this code for ELink-EFetch
#assemble the efetch URL
$url = $base . "efetch.fcgi?db=$db2&query_key=$key&WebEnv=$web";
$url .= "&rettype=fasta&retmode=text";
my $fetched = MyUrlGetter($url);
if (!($fetched =~ m/\<ERROR\>/)) {
#open output file for writing
$efetch_out .= $fetched;
$efetch_out .= "\n";
print "$url\n";
$successful++;
}
$total++;
sleep 5;
}
open(OUT, ">$filename") || die "cant open file\n";
print OUT "$efetch_out";
close OUT;
$ans = $successful . "/" . $total;
return $ans;
}
sub RetrieveFasta_nr {
my $taxon = shift(@_);
my $filename = shift(@_);
my $successes = 0;
my $total = 0;
$query = "(txid" . $taxon . "[orgn])+NOT+((chloroplast*[Protein+Name])+OR+(mitochondrial*[Protein+Name]))";
#assemble the esearch url
$base = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/';
$url = $base . "esearch.fcgi?db=protein&term=$query&usehistory=y";
#post the esearch url
$output = MyUrlGetter($url);
print "$url\n";
#parse WebEnv, QueryKey and Count (# records retreived)
$web = $1 if ($output =~ /<WebEnv>(\S+)<\/WebEnv>/);
$key = $1 if ($output =~ /<QueryKey>(\d+)<\/QueryKey>/);
$count = $1 if ($output =~ /<Count>(\d+)<\/Count>/);
#open output file for writing
open(OUT, ">$filename") || die "cant open file\n";
#retrieve data in batches of 500
$retmax = 500;
for ($retstart = 0; $retstart < $count; $retstart += $retmax) {
$efetch_url = $base . "efetch.fcgi?db=protein&WebEnv=$web";
$efetch_url .= "&query_key=$key&retstart=$retstart";
$efetch_url .= "&retmax=$retmax&rettype=fasta&retmode=text";
$efetch_out = get($efetch_url);
if (!($efetch_out =~ m/.*ERROR.*/)) {
print OUT "$efetch_out";
#print "done\n";
$successes++;
}
else {
#print "error retrieving $url\n $efetch_url\n file not written\n\n";
}
$total++;
sleep 15;
}
#print "done\n";
close OUT;
my $ans = $successes . "/" . $total;
return $ans;
}
sub RetrieveFasta_nr_nofilter {
my $taxon = shift(@_);
my $filename = shift(@_);
my $successes = 0;
my $total = 0;
$query = "(txid" . $taxon . "[orgn])";
#assemble the esearch url
$base = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/';
$url = $base . "esearch.fcgi?db=protein&term=$query&usehistory=y";
#post the esearch url
$output = MyUrlGetter($url);
print "$url\n";
#parse WebEnv, QueryKey and Count (# records retreived)
$web = $1 if ($output =~ /<WebEnv>(\S+)<\/WebEnv>/);
$key = $1 if ($output =~ /<QueryKey>(\d+)<\/QueryKey>/);
$count = $1 if ($output =~ /<Count>(\d+)<\/Count>/);
#open output file for writing
open(OUT, ">$filename") || die "cant open file\n";
#retrieve data in batches of 1000
$retmax = 1000;
for ($retstart = 0; $retstart < $count; $retstart += $retmax) {
$efetch_url = $base . "efetch.fcgi?db=protein&WebEnv=$web";
$efetch_url .= "&query_key=$key&retstart=$retstart";
$efetch_url .= "&retmax=$retmax&rettype=fasta&retmode=text";
$efetch_out = MyUrlGetter($efetch_url);
if (!($efetch_out =~ m/.*ERROR.*/)) {
print OUT "$efetch_out";
#print "done\n";
$successes++;
}
else {
#print "error retrieving $url\n $efetch_url\n file not written\n\n";
}
$total++;
sleep 10;
}
#print "done\n";
close OUT;
my $ans = $successes . "/" . $total;
return $ans;
}