-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgconvtree.pl
executable file
·268 lines (249 loc) · 6.51 KB
/
pgconvtree.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
my $buildno = '2.0.x';
#
# pgconvtree
#
# 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;
my $outputfile = $ARGV[-1];
if ($outputfile !~ /^stdout$/i) {
print(<<"_END");
pgconvtree $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();
}
# initialize variables
if ($outputfile !~ /^stdout$/i && -e $outputfile) {
&errorMessage(__LINE__, "\"$outputfile\" already exists.");
}
my $inputfile = $ARGV[-2];
unless (-e $inputfile) {
&errorMessage(__LINE__, "\"$inputfile\" does not exist.");
}
my $tablefile;
my $format;
my $outformat;
my @trees;
# check options
for (my $i = 0; $i < scalar(@ARGV) - 2; $i ++) {
if ($ARGV[$i] =~ /^-+(?:o|output)=(.+)$/i) {
my $outoption = $1;
if ($outoption =~ /^NEXUS$/i) {
unless ($outformat) {
$outformat = 'NEXUS';
}
else {
&errorMessage(__LINE__, 'Output option is doubly specified.');
}
}
elsif ($outoption =~ /^Newick$/i) {
unless ($outformat) {
$outformat = 'Newick';
}
else {
&errorMessage(__LINE__, 'Output option is doubly specified.');
}
}
else {
&errorMessage(__LINE__, "\"$ARGV[$i]\" is unknown option.");
}
}
elsif ($ARGV[$i] =~ /^-+(?:t|table)=(.+)$/i && -f $1 && !-z $1) {
$tablefile = $1;
}
else {
&errorMessage(__LINE__, "\"$ARGV[$i]\" is unknown option.");
}
}
unless ($outformat) {
&errorMessage(__LINE__, 'Output option is not specified.');
}
# file format recognition
unless (open(INFILE, "< $inputfile")) {
&errorMessage(__LINE__, "Cannot open \"$inputfile\".");
}
while (<INFILE>) {
if (/^#NEXUS/i) {
$format = 'NEXUS';
}
elsif (/^\{/) {
$format = 'TF';
}
else {
$format = 'Newick'
}
last;
}
close(INFILE);
# read input files
unless (open(INFILE, "< $inputfile")) {
&errorMessage(__LINE__, "Cannot open \"$inputfile\".");
}
if ($format eq 'NEXUS') {
my $treesblock = 0;
my $translate = 0;
my $tnt;
my %table;
while (<INFILE>) {
if ($treesblock != 1 && /^\s*Begin\s+Trees\s*;/i) {
$treesblock = 1;
}
elsif ($treesblock == 1 && /^\s*End\s*;/i) {
$treesblock = 0;
}
elsif ($treesblock == 1 && $translate == 1) {
if (/^\s*(\S+)\s+([^,;\s]+)\s*([,;])\s*\r?\n?$/) {
$table{$1} = $2;
if ($3 eq ';') {
$translate = 0;
}
}
}
elsif ($treesblock == 1 && $translate == 0 && !%table && /^\s*Translate/i) {
$translate = 1;
}
elsif ($treesblock == 1 && $translate == 0 && /\s*Tree\s*\S+\s*=\s*(?:\[[^\(\):]+\])?\s*(\(.+\))(?:\:0|\:0\.0)?;/i) {
my $tree = $1;
foreach my $from (keys(%table)) {
$tree =~ s/([\(,])$from([:,\)])/$1$table{$from}$2/;
}
push(@trees, $tree);
}
elsif ($treesblock == 1 && $translate == 0 && /\s*Tree\s*\S+\s*=\s*(?:\[[^\(\):]+\])?\s*/i) {
$tnt = 1;
}
elsif ($tnt == 1 && $treesblock == 1 && $translate == 0 && /(\(.+\))(?:\:0|\:0\.0)?;/i) {
my $tree = $1;
foreach my $from (keys(%table)) {
$tree =~ s/([\(,])$from([:,\)])/$1$table{$from}$2/;
}
push(@trees, $tree);
$tnt = 0;
}
}
}
elsif ($format eq 'Newick') {
local $/ = ';';
while (<INFILE>) {
if (/(\(.+\))(?:\:0|\:0\.0)?(?:\s*\[([\d\/\.]+)\])?;/s) {
my $tree = $1;
$tree =~ s/\s//sg;
push(@trees, $tree);
}
}
}
elsif ($format eq 'TF') {
while (<INFILE>) {
if (/Phylogeny->(\{.+?\}),/) {
my $tree = $1;
$tree =~ tr/\{\}"/\(\)/d;
push(@trees, $tree);
}
}
}
close(INFILE);
# convert sequence names
if ($tablefile) {
my %replace;
unless (open(INFILE, "< $tablefile")) {
&errorMessage(__LINE__, "Cannot open \"$tablefile\".");
}
while (<INFILE>) {
if (/^(\S+) +\"(.+)\"\r?\n?$/) {
my $taxon = $1;
my $temp = $2;
$taxon =~ s/\s//g;
$replace{$taxon} = $temp;
}
}
close(INFILE);
for (my $i = 0; $i < scalar(@trees); $i ++) {
foreach my $taxon (sort({$b cmp $a} keys(%replace))) {
$trees[$i] =~ s/$taxon/$replace{$taxon}/g;
}
}
}
# output file
my $filehandle;
if ($outputfile =~ /^stdout$/i) {
unless (open($filehandle, '>-')) {
&errorMessage(__LINE__, "Cannot write STDOUT.");
}
}
else {
unless (open($filehandle, "> $outputfile")) {
&errorMessage(__LINE__, "Cannot make \"$outputfile\".");
}
}
if ($outformat eq 'NEXUS') {
print($filehandle "#NEXUS\n\nBegin Trees;\n");
}
for (my $i = 0; $i < scalar(@trees); $i ++) {
if ($outformat eq 'NEXUS') {
print($filehandle "\t" . 'Tree tree_' . ($i + 1) . ' = ');
}
print($filehandle $trees[$i] . ";\n");
}
if ($outformat eq 'NEXUS') {
print($filehandle "End;\n");
}
close($filehandle);
sub errorMessage {
my $lineno = shift(@_);
my $message = shift(@_);
print("ERROR!: line $lineno\n$message\n");
print("If you want to read help message, run this script without options.\n");
exit(1);
}
sub helpMessage {
print <<"_END";
Usage
=====
pgconvtree options inputfile outputfile
Command line options
====================
-o, --output=Newick|NEXUS
Specify output file format.
Acceptable input file formats
=============================
Newick
NEXUS
PHYLIP
TL Report
_END
exit;
}