-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgetTaxonID.pl
More file actions
executable file
·180 lines (169 loc) · 4.33 KB
/
getTaxonID.pl
File metadata and controls
executable file
·180 lines (169 loc) · 4.33 KB
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
#!/usr/bin/perl
use strict;
use File::Basename;
use Cwd 'abs_path';
my $rootdir=dirname(abs_path(__FILE__));
my $bindir ="$rootdir/bin";
my $dbdir ="$rootdir/database";
my $db1tsv ="$dbdir/rnacentral.tsv";
my $db2 ="$dbdir/nt";
my $max_split_seqs=5000;
my $docstring=<<EOF
getTaxonID.pl seq.afa seq.afa.tsv
for rMSA format alignment seq.afa, map each hit to taxonID and output the
result to seq.afa.tsv
getTaxonID.pl seq.afa seq.afa.tsv names.dmp
in addition to taxonID, also map each hit to scientific name
"names.dmp" can be downloaded by:
\$ wget https://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdmp.zip
\$ unzip taxdmp.zip names.dmp
EOF
;
if (@ARGV<2)
{
print $docstring;
exit(1);
}
my $infile =$ARGV[0];
my $outfile=$ARGV[1];
my $tmpfile=$outfile.".tmp";
my $namedmp="";
$namedmp =$ARGV[2] if (@ARGV>2);
my @header_list=();
my @accession_list=();
my $accession="";
my @rc_list=();
my @nt_list=();
my $header;
my $cmd="grep -E '";
foreach $header(`grep '^>' $infile|sed 's/>//g'|cut -f1`)
{
chomp($header);
$accession="";
if ($header=~/^(URS[A-Z0-9]+)/) # RNAcentral
{
$accession="$1";
push(@rc_list,($accession)) if (! grep( /^$accession$/, @rc_list));
$cmd.="$accession|";
}
elsif ($header=~/^([_A-Z0-9]+)[.]/)
{
$accession="$1";
push(@nt_list,($accession)) if (! grep( /^$accession$/, @nt_list));
}
if (length $accession)
{
push(@header_list,($header));
push(@accession_list,($accession));
}
else
{
print "skip unmappable entry >".$header."\n";
}
}
printf "mapping %d RNAcentral accession(s)\n", scalar @rc_list;
my %taxon_dict;
%taxon_dict = map { $_ => "" } @accession_list;
my $taxonIDs;
if (scalar @rc_list)
{
$cmd=substr($cmd,0,(length $cmd)-1)."' $db1tsv";
my $success=0;
#print "$cmd\n";
foreach my $line(`$cmd`)
{
if ($line=~/^(URS[A-Z0-9]+)\s\d+\s([,\d]+)$/)
{
$accession="$1";
$taxonIDs="$2";
$taxon_dict{$accession}=$taxonIDs;
$success++;
}
}
if ($success==0)
{
printf "0 RNAcentral accession mapped. re-search full database.\n";
foreach my $line(`cat $db1tsv`)
{
if ($line=~/^(URS[A-Z0-9]+)\s\d+\s([,\d]+)$/)
{
$accession="$1";
$taxonIDs="$2";
$taxon_dict{$accession}=$taxonIDs;
}
}
}
}
printf "mapping %d NCBI nucleotide (NT) accession(s)\n", scalar @nt_list;
for (my $j=0;$j<scalar @nt_list;$j+=$max_split_seqs)
{
#print "mapping entry $j to ".($j+$max_split_seqs)."\n";
my $txt="";
foreach (my $i=$j;$i<=$j+$max_split_seqs;$i++)
{
$txt.="$nt_list[$i]\n";
}
open(FP,">$tmpfile");
print FP $txt;
close(FP);
foreach my $line(`$bindir/blastdbcmd -db $db2 -entry_batch $tmpfile -outfmt '%a %T'`)
{
if ($line=~/(\S+)\s(\S+)/)
{
$accession="$1";
$taxonIDs="$2";
$accession="$1" if ($accession=~/(\S+)[.]\d+/);
$taxon_dict{$accession}=$taxonIDs;
}
}
}
system("rm $tmpfile");
my %name_dict;
my $name;
my $taxonID;
if (length $namedmp)
{
printf "mapping scientific names\n";
foreach my $line(`grep -P "\tscientific name\t" $namedmp`)
{
if ($line=~/^(\d+)\t\|\t([\S\s]+?)\t\|\t/)
{
$taxonID="$1";
$name="$2";
$name_dict{$taxonID}=$name;
}
}
printf "read %d scientific names\n",scalar %name_dict;
}
printf "writing mapping file for %d hits\n", scalar @header_list;
my $txt="#accession\thit\ttaxonID\n";
$txt="#accession\thit\ttaxonID\tname\n" if (length $namedmp);
my $names;
for (my $i=0;$i<scalar @header_list;$i++)
{
$header=$header_list[$i];
$accession=$accession_list[$i];
$taxonIDs=$taxon_dict{$accession};
if (length $taxonIDs==0)
{
print "failed to map >$header\n";
}
if (length $namedmp)
{
$names="";
foreach $taxonID(split(/,/,$taxonIDs))
{
$names.=",$name_dict{$taxonID}" if (exists($name_dict{$taxonID}));
}
$names=substr($names,1);
$txt.="$accession\t$header\t$taxonIDs\t$names\n";
}
else
{
$txt.="$accession\t$header\t$taxonIDs\n";
}
}
open(FP,">$outfile");
print FP $txt;
close(FP);
exit(0);