-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgtranseq.pl
executable file
·284 lines (261 loc) · 7.9 KB
/
pgtranseq.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
my $buildno = '2.0.x';
#
# pgtranseq
#
# Official web site of this script is
# https://www.fifthdimension.jp/products/phylogears/ .
# To know script details, see above URL.
#
# Copyright (C) 2008-2020 Akifumi S. Tanabe
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
use File::Spec;
use Digest::MD5;
# options
my $table = 0;
# input/output
my $inputfile;
my $outputprefix;
# global variables
my $devnull = File::Spec->devnull();
my @taxa;
my %bestframe;
my %minstop;
# file handles
my $filehandleinput1;
my $filehandleoutput1;
my $filehandleoutput2;
my $pipehandleinput1;
&main();
sub main {
# print startup messages
&printStartupMessage();
# get command line arguments
&getOptions();
# check variable consistency
&checkVariables();
# explore best frame for translation
&exploreBestFrame();
# save nucleotide and amino-acid sequences
&joinSequences();
}
sub printStartupMessage {
print(<<"_END");
pgtranseq $buildno
=======================================================================
Official web site of this script is
https://www.fifthdimension.jp/products/phylogears/ .
To know script details, see above URL.
Copyright (C) 2008-2020 Akifumi S. Tanabe
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
_END
# display usage if command line options were not specified
unless (@ARGV) {
&helpMessage();
}
}
sub getOptions {
# get input file name
$inputfile = $ARGV[-2];
# get output file prefix
$outputprefix = $ARGV[-1];
# read command line options
for (my $i = 0; $i < scalar(@ARGV) - 2; $i ++) {
if ($ARGV[$i] =~ /^-+(?:table|t)=(\d+)$/i) {
$table = $1;
}
else {
&errorMessage(__LINE__, "\"$ARGV[$i]\" is unknown option.");
}
}
}
sub checkVariables {
if (!-e $inputfile) {
&errorMessage(__LINE__, "The input file does not exist.");
}
if (-e "$outputprefix\_nuc.fasta" || -e "$outputprefix\_aa.fasta") {
&errorMessage(__LINE__, "The output file already exists.");
}
if (-e "$outputprefix.temp1") {
&errorMessage(__LINE__, "The temporary file already exists.");
}
if ($table !~ /^(?:0|1|2|3|4|5|6|9|10|11|12|13|14|15|16|21|22|23)$/) {
&errorMessage(__LINE__, "The specified genetic code is invalid.");
}
if (system("transeq -h 1> $devnull 2> $devnull")) {
&errorMessage(__LINE__, "Cannot find \"transeq\" of EMBOSS.")
}
}
sub exploreBestFrame {
unless (open($filehandleinput1, "< $inputfile")) {
&errorMessage(__LINE__, "Cannot read \"$inputfile\".");
}
local $/ = "\n>";
while (<$filehandleinput1>) {
if (/^>?\s*(\S[^\r\n]*)\r?\n(.+)/s) {
my $taxon = $1;
my $seq = uc($2);
my $md5 = Digest::MD5->new->add($taxon)->hexdigest;
if ($bestframe{$taxon}) {
&errorMessage(__LINE__, "\"$taxon\" is duplicated.");
}
else {
push(@taxa, $taxon);
}
$seq =~ s/\-//sg;
$seq =~ s/\?/N/sg;
$seq =~ s/[^A-Z]//sg;
unless (open($filehandleoutput1, "> $outputprefix.temp1")) {
&errorMessage(__LINE__, "Cannot write \"$outputprefix.temp1\".");
}
print($filehandleoutput1 ">$taxon\n$seq\n");
close($filehandleoutput1);
foreach my $i (1, 2, 3, -1, -2, -3) {
unless (open($pipehandleinput1, "transeq -table $table -frame $i $outputprefix.temp1 stdout |")) {
&errorMessage(__LINE__, "Cannot run \"transeq -table $table -frame $i $outputprefix.temp1 stdout\".");
}
my $tempaaseq;
while (<$pipehandleinput1>) {
if (/^>?\s*\S[^\r\n]*\r?\n(.+)/s) {
$tempaaseq = $1;
$tempaaseq =~ s/\r?\n?$//;
$tempaaseq =~ s/\*$//;
}
}
close($pipehandleinput1);
my @stop = $tempaaseq =~ /\*/g;
my $nstop = scalar(@stop);
if (!exists($minstop{$taxon}) || $nstop < $minstop{$taxon}) {
$minstop{$taxon} = $nstop;
$bestframe{$taxon} = $i;
my $tempnucseq = $seq;
if ($i < 0) {
$tempnucseq = &reversecomplement($tempnucseq);
}
if ($i == 2 || $i == -2) {
$tempnucseq = 'NN' . $tempnucseq;
$tempaaseq = 'X' . $tempaaseq;
}
elsif ($i == 3 || $i == -3) {
$tempnucseq = 'N' . $tempnucseq;
$tempaaseq = 'X' . $tempaaseq;
}
unless (open($filehandleoutput1, "> $outputprefix\_nuc_$md5.fasta")) {
&errorMessage(__LINE__, "Cannot write \"$outputprefix\_nuc_$md5.fasta\".");
}
print($filehandleoutput1 ">$taxon\n$tempnucseq\n");
close($filehandleoutput1);
unless (open($filehandleoutput1, "> $outputprefix\_aa_$md5.fasta")) {
&errorMessage(__LINE__, "Cannot write \"$outputprefix\_aa_$md5.fasta\".");
}
print($filehandleoutput1 ">$taxon\n$tempaaseq\n");
close($filehandleoutput1);
if ($nstop == 0) {
last;
}
}
}
unlink("$outputprefix.temp1");
}
}
close($filehandleinput1);
}
sub joinSequences {
unless (open($filehandleoutput1, "> $outputprefix\_nuc.fasta")) {
&errorMessage(__LINE__, "Cannot write \"$outputprefix\_nuc.fasta\".");
}
unless (open($filehandleoutput2, "> $outputprefix\_aa.fasta")) {
&errorMessage(__LINE__, "Cannot write \"$outputprefix\_aa.fasta\".");
}
foreach my $taxon (@taxa) {
my $md5 = Digest::MD5->new->add($taxon)->hexdigest;
if ($minstop{$taxon} > 0) {
print(STDERR "\"$taxon\" contains stop codon.\n");
}
# read nucleotide sequence
unless (open($filehandleinput1, "< $outputprefix\_nuc_$md5.fasta")) {
&errorMessage(__LINE__, "Cannot read \"$outputprefix\_nuc_$md5.fasta\".");
}
my $tempnucseq;
while (<$filehandleinput1>) {
s/\r?\n?$//;
unless (/^>/) {
$tempnucseq .= $_;
}
}
close($filehandleinput1);
unlink("$outputprefix\_nuc_$md5.fasta");
# save nucleotide sequence
print($filehandleoutput1 ">$taxon\n$tempnucseq\n");
# read amino-acid sequence
unless (open($filehandleinput1, "< $outputprefix\_aa_$md5.fasta")) {
&errorMessage(__LINE__, "Cannot read \"$outputprefix\_aa_$md5.fasta\".");
}
my $tempaaseq;
while (<$filehandleinput1>) {
s/\r?\n?$//;
unless (/^>/) {
$tempaaseq .= $_;
}
}
close($filehandleinput1);
unlink("$outputprefix\_aa_$md5.fasta");
# save amino-acid sequence
$tempaaseq =~ s/\*/X/g;
print($filehandleoutput2 ">$taxon\n$tempaaseq\n");
}
close($filehandleoutput1);
close($filehandleoutput2);
}
sub reversecomplement {
my @temp = split('', $_[0]);
my @seq;
foreach my $seq (reverse(@temp)) {
$seq =~ tr/ACGTMRYKVHDBacgtmrykvhdb/TGCAKYRMBDHVtgcakyrmbdhv/;
push(@seq, $seq);
}
return(join('', @seq));
}
sub errorMessage {
my $lineno = shift(@_);
my $message = shift(@_);
print(STDERR "ERROR!: line $lineno\n$message\n");
print(STDERR "If you want to read help message, run this script without options.\n");
exit(1);
}
sub helpMessage {
print(STDERR <<"_END");
Usage
=====
pgtranseq options inputfile outputprefix
Command line options
====================
-t,--table=INTEGER
Specify genetic code number. (default: 0)
Acceptable input file formats
=============================
FASTA
_END
exit;
}