-
Notifications
You must be signed in to change notification settings - Fork 4
/
MetaPrism_data.gene.pl
340 lines (324 loc) · 12 KB
/
MetaPrism_data.gene.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env perl
# Author: Jiwoong Kim ([email protected])
use strict;
use warnings;
local $SIG{__WARN__} = sub { die $_[0] };
use Cwd 'abs_path';
use Bio::DB::Taxonomy;
use IPC::Open2;
use Time::HiRes;
use URI::Escape;
use XML::LibXML;
use Getopt::Long qw(:config no_ignore_case);
(my $codePath = abs_path($0)) =~ s/\/[^\/]*$//;
my $dataPath = "$codePath/data";
system("mkdir -p $dataPath");
my @dataURLList = (
'https://cdc.biohpc.swmed.edu/FMAP/FMAP_data',
'https://qbrc.swmed.edu/FMAP/FMAP_data',
);
# NCBI E-utility
my $baseURL = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils';
my $apiKey = $ENV{'NCBI_API_KEY'};
GetOptions(
'h' => \(my $help = ''),
'p=i' => \(my $threads = 1),
'b' => \(my $databaseOrthology = ''),
'a' => \(my $databaseAMR = ''),
'r' => \(my $redownload = ''),
't=s' => \(my $taxonIds = '2,2157,4751'),
'i=s' => \(my $unirefIdentity = '90'),
);
if($help) {
die <<EOF;
Usage: perl MetaPrism_data.gene.pl [options]
Options: -h display this help message
-p INT number of threads [$threads]
-r redownload data
-b build orthology database
-a build AMR gene database
-t STR NCBI taxonomy ID [$taxonIds]
-i INT UniRef identity [$unirefIdentity]
EOF
}
my $database = '';
if($databaseOrthology) {
my @taxonIdList = ();
if($taxonIds ne '') {
@taxonIdList = sort {$a <=> $b} eval($taxonIds);
@taxonIdList = @taxonIdList[0, grep {$taxonIdList[$_ - 1] != $taxonIdList[$_]} 1 .. $#taxonIdList];
}
my %taxonIdHash = ();
if(@taxonIdList) {
if(not -r "$dataPath/nodes.dmp" or not -r "$dataPath/names.dmp" or $redownload) {
my $URL = "ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz";
my $file = "$dataPath/taxdump.tar.gz";
system("wget --no-verbose --no-check-certificate -O $file $URL") if(not -r $file or $redownload);
system("cd $dataPath; tar -zxf taxdump.tar.gz nodes.dmp");
system("cd $dataPath; tar -zxf taxdump.tar.gz names.dmp");
system("rm -f $dataPath/$_") foreach('nodes', 'parents', 'names2id', 'id2names');
}
my $db = Bio::DB::Taxonomy->new(-source => 'flatfile', -directory => $dataPath, -nodesfile => "$dataPath/nodes.dmp", -namesfile => "$dataPath/names.dmp");
my @taxonList = map {$db->get_taxon(-taxonid => $_)} @taxonIdList;
setTaxonIdHash($_) foreach(@taxonList);
sub setTaxonIdHash {
my ($taxon) = @_;
return if(defined($taxonIdHash{$taxon->id}));
$taxonIdHash{$taxon->id} = $taxon;
my @taxonList = $db->each_Descendent($taxon);
setTaxonIdHash($_) foreach(@taxonList);
}
}
$database = join('_', 'orthology', "uniref$unirefIdentity", @taxonIdList);
$database = join('.', $database, getTimeString());
my %unirefOrthologyCountHash = ();
my $pid = open2(my $reader, my $writer, "sort -t '\t' -k1,1 -k2 | uniq | cut -f2-");
open(my $writerLog, "> $dataPath/$database.log");
{
my $URL = 'https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/idmapping/idmapping.dat.gz';
my $file = "$dataPath/idmapping.dat.gz";
system("wget --no-verbose --no-check-certificate -O $file $URL") if(not -r $file or $redownload);
my %tokenHash = ('UniProtKB-AC' => '');
open(my $reader, "gzip -dc $file | sort -t '\t' -k1,1 |");
while(my $line = <$reader>) {
chomp($line);
my @tokenList = split(/\t/, $line, -1);
if($tokenList[0] ne $tokenHash{'UniProtKB-AC'}) {
printGeneUniref() if($tokenHash{'UniProtKB-AC'} ne '');
%tokenHash = ('UniProtKB-AC' => $tokenList[0]);
}
push(@{$tokenHash{$tokenList[1]}}, $tokenList[2]);
}
printGeneUniref() if($tokenHash{'UniProtKB-AC'} ne '');
close($reader);
sub printGeneUniref {
my ($taxonIdList, $geneList, $unirefList) = @tokenHash{'NCBI_TaxID', 'KEGG', "UniRef$unirefIdentity"};
if(scalar(keys %taxonIdHash) == 0 || (defined($taxonIdList) && grep {defined($taxonIdHash{$_})} @$taxonIdList)) {
if(defined($unirefList) && defined($geneList)) {
print $writer join("\t", $_->[1], $_->[0], join(',', @$unirefList)), "\n" foreach(map {[$_, split(/:/, $_)]} @$geneList);
}
}
}
}
close($writer);
{
my $orgCode = '';
my %geneUnirefHash = ();
while(my $line = <$reader>) {
chomp($line);
my ($gene, $uniref) = split(/\t/, $line, -1);
if($gene !~ /^$orgCode:/) {
addUnirefOrthology() if($orgCode ne '');
($orgCode = $gene) =~ s/:.*$//;
%geneUnirefHash = ();
}
$geneUnirefHash{$gene}->{$uniref} = 1;
}
addUnirefOrthology() if($orgCode ne '');
close($reader);
sub addUnirefOrthology {
my %geneOrthologyHash = ();
open(my $reader, "wget --no-verbose --no-check-certificate -O - http://rest.kegg.jp/link/ko/$orgCode |");
while(my $line = <$reader>) {
chomp($line);
my ($gene, $orthology) = split(/\t/, $line, -1);
$orthology =~ s/^ko://;
$geneOrthologyHash{$gene}->{$orthology} = 1;
}
close($reader);
foreach my $gene (keys %geneUnirefHash) {
my @unirefList = sort keys %{$geneUnirefHash{$gene}};
my @orthologyList = ();
@orthologyList = sort keys %$_ if(defined($_ = $geneOrthologyHash{$gene}));
if(scalar(@unirefList) == 1 && scalar(@orthologyList) == 1) {
$unirefOrthologyCountHash{$unirefList[0]}->{$orthologyList[0]} += 1;
} else {
print $writerLog join("\t", $gene, join(',', @unirefList), join(',', @orthologyList)), "\n";
}
}
}
}
{
my $URL = "https://ftp.uniprot.org/pub/databases/uniprot/uniref/uniref$unirefIdentity/uniref$unirefIdentity.fasta.gz";
my $file = "$dataPath/uniref$unirefIdentity.fasta.gz";
system("wget --no-verbose --no-check-certificate -O $file $URL") if(not -r $file or $redownload);
open(my $reader, "gzip -dc $file |");
open(my $writer, "> $dataPath/$database.fasta");
my $printSequence = '';
while(my $line = <$reader>) {
chomp($line);
if($line =~ /^>(\S*)/) {
my $uniref = $1;
$printSequence = '';
if(defined(my $orthologyCountHash = $unirefOrthologyCountHash{$uniref})) {
if(scalar(my @orthologyList = sort keys %$orthologyCountHash) == 1) {
print $writer ">$orthologyList[0]_$uniref\n";
$printSequence = 1;
} else {
print $writerLog join("\t", $uniref, $_, $orthologyCountHash->{$_}), "\n" foreach(@orthologyList);
}
}
} elsif($printSequence) {
print $writer "$line\n";
}
}
close($reader);
close($writer);
}
close($reader);
waitpid($pid, 0);
close($writerLog);
{
open(my $reader, 'wget --no-verbose --no-check-certificate -O - http://rest.kegg.jp/list/ko |');
open(my $writer, "> $dataPath/$database.definition.txt");
while(my $line = <$reader>) {
chomp($line);
my ($orthology, $definition) = split(/\t/, $line, -1);
$orthology =~ s/^ko://;
print $writer join("\t", $orthology, $definition), "\n";
}
close($reader);
close($writer);
}
{
open(my $writer, "> $dataPath/database");
print $writer "$database\n";
close($writer);
}
} elsif($databaseAMR) {
$database = 'AMR';
my %geneDefinitionCountHash = ();
{
open(my $reader, "wget --no-verbose --no-check-certificate -O - ftp://ftp.ncbi.nlm.nih.gov/pathogen/Antimicrobial_resistance/Data/latest/ReferenceGeneCatalog.txt |");
open(my $writer, "| sort -t '\t' -k1,1 -k2,2 | uniq > $dataPath/$database.txt");
chomp(my $line = <$reader>);
my @columnList = split(/\t/, $line, -1);
while(my $line = <$reader>) {
chomp($line);
my %tokenHash = ();
@tokenHash{@columnList} = split(/\t/, $line, -1);
if($tokenHash{'type'} eq 'AMR' && ((my $protein = $tokenHash{'refseq_protein_accession'}) ne '') && ((my $gene = $tokenHash{'gene_family'}) ne '')) {
print $writer join("\t", $protein, $gene), "\n";
if((my $definition = $tokenHash{'product_name'}) ne '') {
$geneDefinitionCountHash{$gene}->{$definition} += 1;
}
}
}
close($reader);
close($writer);
}
{
open(my $reader, "$dataPath/$database.txt");
open(my $writer, "> $dataPath/$database.fasta");
while(my $line = <$reader>) {
chomp($line);
my ($protein, $gene) = split(/\t/, $line, -1);
my @idList = esearch('protein', sprintf('%s[Accession]', $protein));
print $writer efetch('protein', $_, 'fasta', 'text') foreach(@idList);
}
close($reader);
close($writer);
}
{
open(my $writer, "> $dataPath/$database.definition.txt");
foreach my $gene (sort keys %geneDefinitionCountHash) {
my %definitionCountHash = %{$geneDefinitionCountHash{$gene}};
my @definitionList = sort {$definitionCountHash{$b} <=> $definitionCountHash{$a} || $a cmp $b} keys %definitionCountHash;
print $writer join("\t", $gene, $definitionList[0]), "\n";
}
close($writer);
}
{
open(my $writer, "> $dataPath/database");
print $writer "$database\n";
close($writer);
}
} else {
downloadDataFile('database');
open(my $reader, "$dataPath/database") or die "Can't open '$dataPath/database': $!";
chomp($database = <$reader>);
close($reader);
downloadDataFile("$database.fasta.gz");
system("gzip -df $dataPath/$database.fasta.gz");
downloadDataFile("$database.definition.txt");
}
system("diamond makedb --threads $threads --in $dataPath/$database.fasta --db $dataPath/$database.dmnd");
sub getTimeString {
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
return sprintf('%04d%02d%02d%02d%02d%02d', $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
}
sub downloadDataFile {
foreach my $file (@_) {
foreach my $dataURL (@dataURLList) {
system("wget --no-verbose --no-check-certificate -O $dataPath/$file $dataURL/$file") if(not -r "$dataPath/$file" or $redownload);
system("rm $dataPath/$file") if(-z "$dataPath/$file");
}
}
}
sub efetch {
my ($db, $id, $rettype, $retmode) = @_;
my $output = '';
while($output eq '') {
if(defined($apiKey)) {
$output = `wget --no-verbose --no-check-certificate -O - '$baseURL/efetch.fcgi?db=$db&id=$id&rettype=$rettype&retmode=$retmode&api_key=$apiKey'`;
Time::HiRes::usleep(105);
} else {
$output = `wget --no-verbose --no-check-certificate -O - '$baseURL/efetch.fcgi?db=$db&id=$id&rettype=$rettype&retmode=$retmode'`;
Time::HiRes::usleep(350);
}
}
return $output;
}
sub esearch {
my ($db, $term, $mindate, $maxdate) = @_;
my $encodedTerm = uri_escape($term);
$mindate = '' unless(defined($mindate));
$maxdate = '' unless(defined($maxdate));
my @idList = ();
my $retmax = 500;
for(my $retstart = 0, my ($count, $web, $key) = ($retmax); $retstart < $count;) {
my $xmlString = '';
unless(defined($web) && defined($key)) {
if(defined($apiKey)) {
$xmlString = `wget --no-verbose --no-check-certificate -O - '$baseURL/esearch.fcgi?db=$db&term=$encodedTerm&usehistory=y&retmax=$retmax&retstart=$retstart&mindate=$mindate&maxdate=$maxdate&api_key=$apiKey'`;
} else {
$xmlString = `wget --no-verbose --no-check-certificate -O - '$baseURL/esearch.fcgi?db=$db&term=$encodedTerm&usehistory=y&retmax=$retmax&retstart=$retstart&mindate=$mindate&maxdate=$maxdate'`;
}
} else {
if(defined($apiKey)) {
$xmlString = `wget --no-verbose --no-check-certificate -O - '$baseURL/esearch.fcgi?WebEnv=$web&query_key=$key&retmax=$retmax&retstart=$retstart&api_key=$apiKey'`;
} else {
$xmlString = `wget --no-verbose --no-check-certificate -O - '$baseURL/esearch.fcgi?WebEnv=$web&query_key=$key&retmax=$retmax&retstart=$retstart'`;
}
}
if($xmlString =~ /<\/eSearchResult>\n?$/) {
my $dom = XML::LibXML->load_xml(string => $xmlString);
my $root = $dom->documentElement();
($count) = map {$_->textContent} getChildNodeList($root, 'Count');
unless(defined($web) && defined($key)) {
($web) = map {$_->textContent} getChildNodeList($root, 'WebEnv');
($key) = map {$_->textContent} getChildNodeList($root, 'QueryKey');
}
push(@idList, map {$_->textContent} getChildNodeList($root, 'IdList', 'Id'));
$retstart += $retmax;
}
if(defined($apiKey)) {
Time::HiRes::usleep(105);
} else {
Time::HiRes::usleep(350);
}
}
return @idList;
}
sub getChildNodeList {
my ($node, @childNodeTagNameList) = @_;
my @childNodeList = ();
if(@childNodeTagNameList) {
foreach my $childNode ($node->getChildrenByTagName(shift @childNodeTagNameList)) {
push(@childNodeList, getChildNodeList($childNode, @childNodeTagNameList));
}
} else {
push(@childNodeList, $node);
}
return @childNodeList;
}