script/export-itol.pl untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 120 CC 4 SCAR 15.1
CC
4
Cov
0%
message
12.0
read_tree_ids
6.0
BEGIN
2.0
CRAP 4.5
1 #!/usr/bin/env perl
2 # PODNAME: export-itol.pl
3 # ABSTRACT: Download formatted trees from iTOL
4 # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be>
5  
6 use Modern::Perl '2011';
7 use autodie;
8  
9 use Getopt::Euclid qw(:vars);
10 use Smart::Comments '###';
11  
12 use Config::Any;
13 use HTTP::Request::Common;
14 use LWP::UserAgent;
15  
16 use Bio::MUST::Core;
17 use Bio::MUST::Core::Constants qw(:files);
18 use Bio::MUST::Core::Utils qw(change_suffix insert_suffix);
19  
20  
21 my $download_url = "https://itol.embl.de/batch_downloader.cgi";
22  
23 my $suffix_like = qr{ \bsvg\b | \bpdf\b | \beps\b | \bps\b | \bpng\b
24 | \bnewick\b | \bnexus\b | \bphyloxml\b }xms;
25  
26 FILE:
27 for my $infile (@ARGV_infiles) {
28  
29 ### Processing: $infile
30 my $id_for = read_tree_ids($infile);
31  
32 # determine config file (global or infile-dependent)
33 $infile =~ s/$_//xms for @ARGV_in_strip;
34 my $cfgfile = $ARGV_config ? $ARGV_config : change_suffix($infile, '.ini');
35  
36 ### config file: $cfgfile
37 my $config = Config::Any->load_files( {
38 files => [ $cfgfile ],
39 flatten_to_hash => 1,
40 use_ext => 1,
41 } );
42  
43 my %param_for = %{ $config->{$cfgfile} };
44  
45 if ($param_for{'format'} !~ $suffix_like) {
46 warn <<"EOT";
47 Warning: unspecified or invalid output format: $param_for{format}; skipping!
48 EOT
49 next FILE;
50 }
51  
52 TREE:
53 while ( my ($outfile, $tree) = each %{$id_for} ) {
54  
55 unless ($outfile && $tree) {
56 warn <<'EOT';
57 Warning: missing tree filename or id; skipping!
58 EOT
59 next TREE;
60 }
61  
62 $outfile .= q{.} . $param_for{'format'};
63 $outfile = insert_suffix($outfile, $ARGV_out_suffix)
64 if $ARGV_out_suffix;
65 $param_for{'outFile'} = $outfile;
66  
67 $param_for{'tree' } = $tree;
68  
69 # submit the data
70 my $ua = LWP::UserAgent->new();
71 $ua->agent("iTOLbatchDownloader5.0");
72 my $request = POST $download_url,
73 Content_Type => 'form-data', Content => [ %param_for ];
74 my $response = $ua->request($request);
75  
76 message( $response, $param_for{'outFile'} )
77 }
78 }
79  
80  
81 sub read_tree_ids {
82 my $infile = shift;
83  
84 open my $in, '<', $infile;
85  
86 my %id_for;
87  
88 LINE:
89 while (my $line = <$in>){
90 chomp $line;
91  
92 # skip empty lines and process comments
93 next LINE if $line =~ $EMPTY_LINE
94 || $line =~ $COMMENT_LINE;
95  
96 # Note: var names after import-itol.pl
97 my ($file, $tree_id) = split /\t/xms, $line;
98 $id_for{$file} = $tree_id;
99 }
100  
101 return \%id_for;
102 }
103  
104 # TODO: use standard BMC error message scheme
105 sub message {
106 my $response = shift;
107 my $outfile = shift;
108  
109 if ( $response->is_success() ) {
110 my @res = split /\n/xms, $response->content;
111  
112 # check for export error
113 if ($response->header('Content-type') =~ /text\/html/xms) {
114 warn <<"EOT";
115 Warning: export failed. iTOL returned the following error message:
116 $res[0]
117 EOT
118 }
119  
120 # if no warnings, export tree to outfile
121 else {
122 ### Exporting tree to: $outfile
123 open my $out, '>', $outfile;
124 say {$out} join "\n", @res;
125 }
126 }
127  
128 else {
129 warn <<"EOT";
130 Warning: iTOL returned a web server error; full message follows:
131 EOT
132 print $response->as_string;
133 }
134  
135 return;
136 }
137  
138 __END__