-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubmlst.pl
executable file
·288 lines (256 loc) · 7.19 KB
/
pubmlst.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
#!/usr/bin/perl -w
# PubMLST2 analysis, now with generation of a PHYLIP file
unless (@ARGV) {
print STDERR "\tPubMLST2 will analyze all .fasta files in the current directory.\n";
print STDERR "\tFirst argument is name of analysis, used in output file names.\n";
print STDERR "\tSecond argument is the path to the directory containing PubMLST allele and type files\n";
exit;
}
open (OUTPUT,"> $ARGV[0].mlst");
# gather PubMLST data
chomp(@files=<$ARGV[1]/*>);
foreach $file (@files) {
open(INPUT,"< $file");
chomp($line=<INPUT>); # grab first line of file
close (INPUT);
if ($line =~ /^ST/) { # MLST profiles
@header = split(/\t/,$line);
shift(@header); # remove "ST" at start
pop(@header); # remove "clonal_complex" at end
print OUTPUT "File";
foreach $allele (@header) { # alleles as an array
print OUTPUT "\t$allele";
}
print OUTPUT "\tSequence Type\tClonal Complex\n";
open (INPUT,"< $file");
while (defined($line=<INPUT>)) {
chomp($line);
if ($line =~ /^ST/) { next; } # header
@temp = split(/\t/,$line);
$typeID = shift(@temp); # extract ST
undef $typealleles;
$i = 0;
foreach $allele (@temp) { # alleles
if (defined($header[$i])) {
$typealleles .= "$header[$i]_$allele";
} else {
$MLSTcomplex{$typeID}=$allele;
}
$i++;
}
$MLSTtype{$typealleles}=$typeID; # key is e.g. acs_1aro_13gua_12mut_7nuo_72pps_4trp_18, value is ST code
}
close (INPUT);
} elsif ($line =~ /^>/) { # allele data
undef $defline;
undef $sequence;
open (INPUT,"< $file");
while (defined($line=<INPUT>)) {
chomp($line);
if ($line =~ /^>/) {
if (defined($defline)) {
$allele{$defline}=uc($sequence);
}
($defline) = $line =~ /^>(.*)/;
undef $sequence;
} else {
$sequence .= $line;
}
}
$allele{$defline}=uc($sequence);
close (INPUT);
} else {
print STDERR "\t$file UNEXPECTED\n";
exit;
}
}
# revcomp alleles
foreach $seq (keys %allele) {
$revallele{$seq} = reversecomplement($allele{$seq});
}
# analyze files
$novelmlstcount = 0;
chomp(@files1=<*.fasta>);
chomp(@files2=<*.fa>);
chomp(@files3=<*.fna>);
undef @files;
foreach $entry (@files1) {
push(@files,$entry);
}
foreach $entry (@files2) {
push(@files,$entry);
}
foreach $entry (@files3) {
push(@files,$entry);
}
foreach $file (@files) {
print OUTPUT "$file";
print STDERR "$file\n";
undef $defline;
undef $sequence;
undef %mlsthash;
undef $typealleles;
open (INPUT,"< $file");
while (defined($line=<INPUT>)) {
chomp($line);
if ($line =~ /^>/) {
if (defined($defline)) {
mlstcheck($sequence);
}
($defline) = $line =~ /^>(.*)/;
undef $sequence;
} else {
$sequence .= uc($line);
}
}
mlstcheck($sequence);
close (INPUT);
undef $unresolved; # tracking type of lack of resolution
foreach $seq1 (@header) { # output alleles, allowing for alleles not found & multiple alleles
undef $status;
foreach $seq (keys %mlsthash) {
if ($seq =~ /${seq1}_/) {
if (defined($status)) { # check for multiple alleles
$status .= ",$seq";
$unresolved = "unresolved allele(s)";
} else {
$status = $seq;
}
}
}
if (defined($status)) { # allele found
print OUTPUT "\t$status";
$typealleles .= $status; # determine genotype to match to ST
} else {
print OUTPUT "\t"; # allele not found
$unresolved = "unresolved allele(s)";
}
}
if (defined($typealleles)) { # allele(s) found
if (defined($MLSTtype{$typealleles})) { # type exists
$finalMLST{$file}=$MLSTtype{$typealleles};
print OUTPUT "\t$MLSTtype{$typealleles}";
$typecount{$MLSTtype{$typealleles}}++;
if (defined($MLSTcomplex{$MLSTtype{$typealleles}})) { # complex exists
print OUTPUT "\t$MLSTcomplex{$MLSTtype{$typealleles}}\n";
} else { # complex does not exist
print OUTPUT "\t\n";
}
} else { # type does not exist
if (defined($unresolved)) { # does not have all alleles
print OUTPUT "\t\t\n";
$typecount{$unresolved}++;
$finalMLST{$file}="unresolved";
} else { # has all alleles, but not seen before
$novelmlstcount++;
$MLSTtype{$typealleles}= "Novel_${novelmlstcount}";
$typecount{$MLSTtype{$typealleles}}++;
$finalMLST{$file}=$MLSTtype{$typealleles};
print OUTPUT "\t$MLSTtype{$typealleles}\t\n";
}
}
} else { # allele(s) not found
print OUTPUT "\t\t\n";
$unresolved = "incorrect pathogen";
$typecount{$unresolved}++;
$finalMLST{$file}="incorrect_pathogen";
}
foreach $entry (sort keys %mlsthash) { # generate phylip data
if (defined($allele{$entry})) {
$phylip{$file} .= $allele{$entry};
} else {
$badphylip{$file}++;
}
}
}
close (OUTPUT);
# relabel file
open (OUTPUT2,"> $ARGV[0].mlst.relabel");
foreach $file (keys %finalMLST) {
print OUTPUT2 "$file\tMLST${finalMLST{$file}}_$file\n";
}
close (OUTPUT2);
# stats
open (OUTPUT,"> $ARGV[0].mlst.log");
print OUTPUT "MLST\tFreq\n";
foreach $typing (sort {$typecount{$b} <=> $typecount{$a}} keys %typecount) {
print OUTPUT "$typing\t$typecount{$typing}\n";
print STDERR "$typing\t$typecount{$typing}\n";
}
close (OUTPUT);
# relaxed phylip
foreach $entry (keys %phylip) {
if (defined($badphylip{$entry})) {
print STDERR "$file excluded from PHYLIP file due to missing allele sequence(s)\n";
next;
}
$count2 = length($phylip{$entry});
push(@lengtharray,$count2);
}
@num_sorted = sort {$b <=> $a} @lengtharray;
foreach $entry (sort keys %phylip) {
if (defined($badphylip{$entry})) {
next;
}
$plength = length($phylip{$entry});
unless ($plength == $num_sorted[0]) {
print STDERR "$entry excluded from PHYLIP file due to unexpected allele summed sequence length: $plength\n";
next;
}
$phycount++;
@name = split(/\./,$entry);
push(@phyarray,"${name[0]}_MLST_${finalMLST{$entry}} $phylip{$entry}");
}
open (OUTPUT,"> $ARGV[0].mlst.phy");
print OUTPUT "$phycount $num_sorted[0]\n";
foreach $entry (@phyarray) {
print OUTPUT "$entry\n";
}
close (OUTPUT);
# done
exit;
# subroutines
sub mlstcheck {
foreach $seq (keys %allele) {
if ($sequence =~ /$allele{$seq}/) {
$mlsthash{$seq}++;
}
}
foreach $seq (keys %revallele) {
if ($sequence =~ /$revallele{$seq}/) {
$mlsthash{$seq}++;
}
}
}
sub reversecomplement {
my($insequence, @sequence, $length, $i, $newsequence, %reverse, $newbase);
$reverse{"A"}="T";
$reverse{"C"}="G";
$reverse{"G"}="C";
$reverse{"T"}="A";
$reverse{"R"}="Y"; # R = A or G
$reverse{"Y"}="R"; # Y = T or C
$reverse{"M"}="K"; # M = A or C
$reverse{"K"}="M"; # K = T or G
$reverse{"S"}="S"; # S = C or G
$reverse{"W"}="W"; # W = A or T
$reverse{"H"}="D"; # H = A or C or T
$reverse{"D"}="H"; # D = T or G or A
$reverse{"B"}="V"; # B = C or G or T
$reverse{"V"}="B"; # V = G or C or A
$reverse{"X"}="N";
$reverse{"N"}="N";
$insequence = uc($_[0]);
@sequence=split(//,$insequence);
$length=@sequence;
$length=$length-1;
for ($i=$length; $i >= 0; $i--) {
if (defined($reverse{$sequence[$i]})) {
$newsequence .= $reverse{$sequence[$i]};
} else {
print STDERR "\tREVCOMP ERROR\tUnrecognized nucleotide \"$sequence[$i]\", using \"N\"\n";
$newsequence .= "N";
}
}
return $newsequence;
}