-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTOL_downloader.pl
executable file
·392 lines (263 loc) · 12.9 KB
/
iTOL_downloader.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use Pod::Usage;
use Getopt::Regex;
use Config::File::Simple;
=pod
=head1 NAME
iTOL_downloader - batch download trees from iTOL in different formats
=head1 SYNOPSIS
B<iTOL_downloader.pl> I<options>
=head1 DESCRIPTION
Use B<iTOL_downloader.pl> to download your trees from iTOL. You can specify the format and other export parameters using the options described below.
=head1 OPTIONS
Separate options and their values with a space.
=over 4
=item B<--help>
display this message
=item B<--config>
read upload options from a file (format described below); example file available at http://itol.embl.de/help/export.cfg
=item B<--tree>
iTOL tree ID
=item B<--outputFile>
save the resulting tree into the specified file
=item B<--format>
export format
=over 6
=item B<'png'>
Portable Network Graphics; bitmap format
=item B<'svg'>
Scalable Vector Graphics; vector format
=item B<'eps'>
Encapsulated Postscript; vector format
=item B<'ps'>
Postscript; vector format
=item B<'pdf'>
Portable Document Format; vector format
=item B<'nexus'>
Nexus tree file; plain text
=item B<'newick'>
Newick tree file; plain text
=back
=item B<--reRoot>
Reroot the tree on the specified node. If your tree does not have internal node IDs, you can use two leaf IDs separated with a vertical line. Last common ancestor of these nodes will be used as the new root (for example --reRoot 'Homo_sapiens|Gallus_gallus')
=back
=head2 OPTIONS FOR GRAPHICAL FORMATS
=over 4
=item B<--displayMode>
tree display mode; normal or circular
=item B<--alignLabels>
should the leaf labels be aligned? (0 or 1)
=item B<--ignoreBRL>
branch lengths will be ignored if set to 1
=item B<--showBRL>
branch lenght values will be displayed on the tree. Overrides --showBS if both are specified
=item B<--showBS>
bootstraps will be displayed on the tree
=item B<--BSdisplayValue>
specify which bootstrap will be displayed. You must specify the numeric value and choose the direction using the letters 'M' (for more than) or 'L' (for less than). For example:
display bootstraps above 60: --BSdisplayValue M60
display bootstraps below 0.3: --BSdisplayValue L0.3
=item B<--BSdisplayType>
'text' or 'symbol'
=item B<--BSsymbolMaxSize>
if 'symbol' is used to display bootstraps, use this option to specify the maximum circle size (in pixels)
=item B<--hideRanges>
if the tree has colored ranges defined, you can omit them from the exported tree by setting this to 1
=item B<--rangesCover>
'clades' or 'leaves'
if the tree has colored ranges defined, specify their extent using this option
=item B<--colorBranches>
if the tree has colored branches, use this option to use the colors in the exported tree
=item B<--rotation>
for circular display mode, specify the rotation angle in degrees
=item B<--arc>
for circular display mode, specify the arc in which the tree should be displayed (in degrees, default is 350)
=item B<--inverted>
if set to 1, the circular tree will be inverted
=item B<--resolution>
image resolution in DPI (dots per inch), used only when exporting to PNG
=item B<--hideLabels>
do not display leaf labels
=item B<--fontSize>
font size to be used for leaf labels
=item B<--lineWidth>
line width in pixels (default is 3)
=item B<--scaleFactor>
the default horizontal tree scale will be multiplied with the specified value
=item B<--showInternalLabels>
internal node IDs will be displayed if this option is set to 1
=item B<--pruneList>
comma separated list of leaf IDs which should be included in the tree. If not specified, complete tree will be exported.
=item B<--collapseList>
comma separated list of internal node IDs which will be collapsed. If the tree does not have internal nodes defined, you can specify them using two leafs IDs instead. Separate leaf IDs with a vertical line ('|'). The last common ancestor of each pair will be collapsed. For example:
--collapseList Homo_sapiens|Gallus_gallus,Escherichia_coli|Mycoplasma_pneumoniae
=item B<--datasetList>
comma separated list of dataset IDs to include in the exported tree. For example:
--datasetList dataset1,dataset3,dataset4
=item B<--omitDashedLines>
used only when datasets are included in the exported tree. When set, dataset values will not be connected to tree's leaves using dashed lines.
=back
=head2 OPTIONS FOR PLAIN TEXT FORMATS
=over 4
=item B<--newickExportFormat>
output options for Newick format
=over 6
=item B<'withIDs'>
Include internal node IDs in the tree. Example output: ((A:0.1, B:0.1)INTid1:0.2[95]);
=item B<'withoutIDs'>
Only bootstraps and branch lengths will be included. Example output: ((A:0.1, B:0.1)95:0.2);
=back
=back
=head1 USING A CONFIGURATION FILE
We provided an option of using a separate plain text config file to specify the export parameters. To use a configuration file, simply use the B<--config> option to specify its location.
The configuration file should contain one parameter per line. Use the B<=> sign to assign values to the parameters. Here is an example configuration file:
outputFile = example.pdf
format = pdf
tree = 123456789
displayMode = circular
inverted = 1
arc = 360
rotate = 70
=head1 SEE ALSO
iTOL Home page : http://itol.embl.de
iTOL Help : http://itol.embl.de/help/help.shtml
=head1 AUTHOR
Ivica Letunic <[email protected]>
Contact me if you have any questions or comments.
=cut
my $download_url = "http://itol.embl.de/batch_downloader.cgi";
my ($configFile, $outFile, $showHelp, $tree, $format,$pruneList,$collapseList,$ignoreBRL,$newickExportFormat, $colorBranches, $displayMode, $alignLabels, $showBS, $showBRL, $reRoot, $hideRanges, $rangesCover, $BSdisplayValue, $BSdisplayType, $BSsymbolMaxSize, $rotation, $arc, $inverted, $resolution, $hideLabels, $fontSize, $lineWidth, $scaleFactor, $showInternalLabels, $datasetList, $omitDashedLines);
Getopt::Regex::GetOptions(\@ARGV,
['--?h(elp)?',\$showHelp, 0],
['--?config=?',\$configFile, 1],
['--?tree=?',\$tree, 1],
['--?outputFile=?',\$outFile, 1],
['--?format=?',\$format, 1],
['--?displayMode=?',\$displayMode, 1],
['--?alignLabels=?',\$alignLabels, 1],
['--?pruneList=?',\$pruneList, 1],
['--?collapseList=?',\$collapseList, 1],
['--?ignoreBRL=?',\$ignoreBRL, 1],
['--?showBS=?',\$showBS, 1],
['--?showBRL=?',\$showBRL, 1],
['--?reRoot=?',\$reRoot, 1],
['--?hideRanges=?',\$hideRanges, 1],
['--?rangesCover=?',\$rangesCover, 1],
['--?colorBranches=?',\$colorBranches, 1],
['--?BSdisplayValue=?',\$BSdisplayValue, 1],
['--?BSdisplayType=?',\$BSdisplayType, 1],
['--?BSsymbolMaxSize=?',\$BSsymbolMaxSize, 1],
['--?rotation=?',\$rotation, 1],
['--?arc=?',\$arc, 1],
['--?inverted=?',\$inverted, 1],
['--?resolution=?',\$resolution, 1],
['--?hideLabels=?',\$hideLabels, 1],
['--?fontSize=?',\$fontSize, 1],
['--?lineWidth=?',\$lineWidth, 1],
['--?scaleFactor=?',\$scaleFactor, 1],
['--?showInternalLabels=?',\$showInternalLabels, 1],
['--?newickExportFormat=?',\$newickExportFormat, 1],
['--?datasetList=?',\$datasetList, 1],
['--?omitDashedLines=?',\$omitDashedLines, 1]
);
pod2usage(VERBOSE => 2) if ( $showHelp );
if (-e $configFile) {
my $cfg = new Config::File::Simple($configFile);
$tree = $cfg->read('tree') if ($cfg->read('tree'));
$outFile = $cfg->read('outputFile') if ($cfg->read('outputFile') );
$format = $cfg->read('format') if ($cfg->read('format') );
$ignoreBRL = $cfg->read('ignoreBRL') if ($cfg->read('ignoreBRL') );
$pruneList = $cfg->read('pruneList') if ($cfg->read('pruneList') );
$collapseList = $cfg->read('collapseList') if ($cfg->read('collapseList') );
$newickExportFormat = $cfg->read('newickExportFormat') if ($cfg->read('newickExportFormat') );
$scaleFactor = $cfg->read('scaleFactor') if ($cfg->read('scaleFactor') );
$colorBranches = $cfg->read('colorBranches') if ($cfg->read('colorBranches') );
$displayMode = $cfg->read('displayMode') if ($cfg->read('displayMode') );
$hideLabels = $cfg->read('hideLabels') if ($cfg->read('hideLabels') );
$fontSize = $cfg->read('fontSize') if ($cfg->read('fontSize') );
$rotation = $cfg->read('rotation') if ($cfg->read('rotation') );
$arc = $cfg->read('arc') if ($cfg->read('arc') );
$inverted = $cfg->read('inverted') if ($cfg->read('inverted') );
$lineWidth = $cfg->read('lineWidth') if ($cfg->read('lineWidth') );
$showInternalLabels = $cfg->read('showInternalLabels') if ($cfg->read('showInternalLabels') );
$alignLabels = $cfg->read('alignLabels') if ($cfg->read('alignLabels') );
$showBS = $cfg->read('showBS') if ($cfg->read('showBS') );
$showBRL = $cfg->read('showBRL') if ($cfg->read('showBRL') );
$hideRanges = $cfg->read('hideRanges') if ($cfg->read('hideRanges') );
$reRoot = $cfg->read('reRoot') if ($cfg->read('reRoot') );
$rangesCover = $cfg->read('rangesCover') if ($cfg->read('rangesCover') );
$BSdisplayValue = $cfg->read('BSdisplayValue') if ($cfg->read('BSdisplayValue') );
$BSdisplayType = $cfg->read('BSdisplayType') if ($cfg->read('BSdisplayType') );
$BSsymbolMaxSize = $cfg->read('BSsymbolMaxSize') if ($cfg->read('BSsymbolMaxSize') );
$resolution = $cfg->read('resolution') if ($cfg->read('resolution') );
$datasetList = $cfg->read('datasetList') if ($cfg->read('datasetList'));
$omitDashedLines = $cfg->read('omitDashedLines') if ($cfg->read('omitDashedLines'));
}
print "\niTOL batch downloader\n=====================\n";
#need a tree ID, format and outfile
unless ( length($tree) and length($outFile) and length($format) ) {
print STDERR "Missing requried parameters. At least 'tree', 'format' and 'outputFile' must be specified. Use the --help option to display full help.\n";
exit;
}
unless ($format eq 'svg' or $format eq 'eps' or $format eq 'ps' or $format eq 'pdf' or $format eq 'png' or $format eq 'newick' or $format eq 'nexus') {
print STDERR "ERROR: Invalid output format. Supported formats: 'eps', 'svg', 'ps', 'pdf', 'png', 'newick' or 'nexus'\n";
exit;
}
#prepare the basic POST data
my %post_content;
$post_content{'format'} = $format;
$post_content{'tree'} = $tree;
if (defined $displayMode) { $post_content{'displayMode'} = $displayMode; }
if (defined $hideLabels) { $post_content{'hideLabels'} = $hideLabels; }
if (defined $fontSize) { $post_content{'fontSize'} = $fontSize; }
if (defined $ignoreBRL) { $post_content{'ignoreBRL'} = $ignoreBRL; }
if (defined $pruneList) { $post_content{'pruneList'} = $pruneList; }
if (defined $collapseList) { $post_content{'collapseList'} = $collapseList; }
if (defined $scaleFactor) { $post_content{'scaleFactor'} = $scaleFactor; }
if (defined $colorBranches) { $post_content{'colorBranches'} = $colorBranches; }
if (defined $newickExportFormat) { $post_content{'newickExportFormat'} = $newickExportFormat; }
if (defined $displayMode) { $post_content{'displayMode'} = $displayMode; }
if (defined $fontSize) { $post_content{'fontSize'} = $fontSize; }
if (defined $rotation) { $post_content{'rotation'} = $rotation; }
if (defined $arc) { $post_content{'arc'} = $arc; }
if (defined $inverted) { $post_content{'inverted'} = $inverted; }
if (defined $lineWidth) { $post_content{'lineWidth'} = $lineWidth; }
if (defined $showInternalLabels) { $post_content{'showInternalLabels'} = $showInternalLabels; }
if (defined $alignLabels) { $post_content{'alignLabels'} = $alignLabels; }
if (defined $showBS) { $post_content{'showBS'} = $showBS; }
if (defined $showBRL) { $post_content{'showBRL'} = $showBRL; }
if (defined $reRoot) { $post_content{'reRoot'} = $reRoot; }
if (defined $hideRanges) { $post_content{'hideRanges'} = $hideRanges; }
if (defined $rangesCover) { $post_content{'rangesCover'} = $rangesCover; }
if (defined $BSdisplayType) { $post_content{'BSdisplayType'} = $BSdisplayType; }
if (defined $BSsymbolMaxSize) { $post_content{'BSsymbolMaxSize'} = $BSsymbolMaxSize; }
if (defined $resolution) { $post_content{'resolution'} = $resolution; }
if (defined $datasetList) { $post_content{'datasetList'} = $datasetList; }
if (defined $omitDashedLines) { $post_content{'omitDashedLines'} = $omitDashedLines; }
#submit the data
my $ua = LWP::UserAgent->new();
$ua->agent("iTOLbatchDownloader1.0");
my $req = POST $download_url, Content_Type => 'form-data', Content => [ %post_content ];
my $response = $ua->request($req);
if ($response->is_success()) {
#check for the content type
#if text/html, there was an error
#otherwise dump the results into the outfile
if ($response->header("Content-type") =~ /text\/html/) {
my @res = split(/\n/, $response->content);
print "Export failed. iTOL returned the following error message:\n\n$res[0]\n\n";
exit;
} else {
open (OUT, ">$outFile") or die "Cannot write to $outFile";
binmode OUT;
print OUT $response->content;
print "Exported tree saved to $outFile\n";
# print join("\n", @res);
}
} else {
print "iTOL returned a web server error. Full message follows:\n\n";
print $response->as_string;
}