1 | #!/usr/bin/perl |
---|
2 | # =============================================================== # |
---|
3 | # # |
---|
4 | # File : raxml2arb.pl # |
---|
5 | # Purpose : Import XX best of YY calculated raxml trees into # |
---|
6 | # ARB and generate comment containing likelyhood # |
---|
7 | # and content of info file # |
---|
8 | # # |
---|
9 | # Coded by Ralf Westram (coder@reallysoft.de) in March 2008 # |
---|
10 | # Institute of Microbiology (Technical University Munich) # |
---|
11 | # http://www.arb-home.de/ # |
---|
12 | # # |
---|
13 | # =============================================================== # |
---|
14 | |
---|
15 | use strict; |
---|
16 | use warnings; |
---|
17 | |
---|
18 | sub arb_message($) { |
---|
19 | my ($msg) = @_; |
---|
20 | print "Message: $msg\n"; |
---|
21 | system("arb_message \"$msg\""); |
---|
22 | } |
---|
23 | |
---|
24 | sub error($) { |
---|
25 | my ($msg) = @_; |
---|
26 | if ($msg =~ /at.*\.pl/o) { |
---|
27 | my ($pe,$loc) = ($`,$&.$'); |
---|
28 | chomp $loc; |
---|
29 | $msg = $pe."\n(".$loc.")\nPlease check console for additional error reasons"; |
---|
30 | } |
---|
31 | arb_message($msg); |
---|
32 | die $msg; |
---|
33 | } |
---|
34 | |
---|
35 | sub raxml_filename($$$) { |
---|
36 | my ($type,$name,$run) = @_; |
---|
37 | my $fname = 'RAxML_'.$type.'.'.$name; |
---|
38 | if (defined $run) { $fname .= '.RUN.'.$run; } |
---|
39 | return $fname; |
---|
40 | } |
---|
41 | |
---|
42 | sub someWhat($$) { |
---|
43 | my ($count,$what) = @_; |
---|
44 | if ($count==1) { return '1 '.$what; } |
---|
45 | return $count.' '.$what.'s'; |
---|
46 | } |
---|
47 | |
---|
48 | sub treeInfo($$) { |
---|
49 | my ($name,$run) = @_; |
---|
50 | |
---|
51 | my $result = raxml_filename('result',$name,$run); |
---|
52 | my $bipartitions = raxml_filename('bipartitions',$name,$run); |
---|
53 | my $parsimony = raxml_filename('parsimonyTree',$name,$run); |
---|
54 | my $log = raxml_filename('log',$name,$run); |
---|
55 | |
---|
56 | if (not -f $result) { # no result tree, try bipartitions or parsimonyTree |
---|
57 | if (-f $bipartitions) { return ($bipartitions,'unknown'); } |
---|
58 | if (-f $parsimony) { return ($parsimony,'unknown'); } |
---|
59 | } |
---|
60 | |
---|
61 | open(LOG,'<'.$log) || die "can't open '$log' (Reason: $!)"; |
---|
62 | my $line = undef; |
---|
63 | foreach (<LOG>) { if ($_ ne '') { $line = $_; } } |
---|
64 | close(LOG); |
---|
65 | |
---|
66 | chomp($line); |
---|
67 | |
---|
68 | if (not $line =~ / (.*)$/o) { |
---|
69 | die "can't parse likelyhood from '$log'"; |
---|
70 | } |
---|
71 | |
---|
72 | my $likelyhood = $1; |
---|
73 | return ($result,$likelyhood); |
---|
74 | } |
---|
75 | |
---|
76 | sub findTrees($$\%) { |
---|
77 | my ($name,$runs,$likelyhood_r) = @_; |
---|
78 | |
---|
79 | %$likelyhood_r = (); |
---|
80 | |
---|
81 | if ($runs==1) { |
---|
82 | my ($tree,$likely) = treeInfo($name,undef); |
---|
83 | $$likelyhood_r{$tree} = $likely; |
---|
84 | } |
---|
85 | else { |
---|
86 | for (my $r = 0; $r<$runs; $r++) { |
---|
87 | my ($tree,$likely) = treeInfo($name,$r); |
---|
88 | $$likelyhood_r{$tree} = $likely; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | sub main() { |
---|
94 | eval { |
---|
95 | my $args = scalar(@ARGV); |
---|
96 | |
---|
97 | if ($args != 4) { |
---|
98 | die "Usage: raxml2arb.pl RUNNAME NUMBEROFRUNS TAKETREES [import|consense]\n". |
---|
99 | " import: Import the best TAKETREES trees of NUMBEROFRUNS generated trees into ARB\n". |
---|
100 | " consense: Create and import consense tree of best TAKETREES trees of NUMBEROFRUNS generated trees\n"; |
---|
101 | } |
---|
102 | |
---|
103 | my ($RUNNAME,$NUMBEROFRUNS,$TAKETREES,$CONSENSE) = @ARGV; |
---|
104 | |
---|
105 | if ($NUMBEROFRUNS<1) { die "NUMBEROFRUNS has to be 1 or more ($NUMBEROFRUNS)"; } |
---|
106 | |
---|
107 | my %likelyhood = (); |
---|
108 | findTrees($RUNNAME,$NUMBEROFRUNS,%likelyhood); |
---|
109 | |
---|
110 | my $createdTrees = scalar(keys %likelyhood); |
---|
111 | print "Found ".someWhat($createdTrees,'tree').":\n"; |
---|
112 | |
---|
113 | if ($TAKETREES > $createdTrees) { |
---|
114 | arb_message("Cant take more trees ($TAKETREES) than calculated ($NUMBEROFRUNS) .. using all"); |
---|
115 | $TAKETREES = $createdTrees; |
---|
116 | } |
---|
117 | |
---|
118 | my $calc_consense = 0; |
---|
119 | if ($CONSENSE eq 'consense') { |
---|
120 | if ($TAKETREES<2) { |
---|
121 | arb_message("Need to take at least 2 trees to create a consense tree - importing.."); |
---|
122 | $CONSENSE = 'import'; |
---|
123 | } |
---|
124 | else { |
---|
125 | $calc_consense = 1; |
---|
126 | } |
---|
127 | } |
---|
128 | elsif ($CONSENSE ne 'import') { |
---|
129 | die "Unknown value '$CONSENSE' (expected 'import' or 'consense')"; |
---|
130 | } |
---|
131 | |
---|
132 | my @sortedTrees = sort { $likelyhood{$b} <=> $likelyhood{$a}; } keys %likelyhood; |
---|
133 | foreach (@sortedTrees) { print " $_ = ".$likelyhood{$_}."\n"; } |
---|
134 | |
---|
135 | my @treesToTake = splice(@sortedTrees,0,$TAKETREES); |
---|
136 | if (scalar(@treesToTake)<1) { die "No trees to $CONSENSE"; } |
---|
137 | |
---|
138 | my $treesToTake = scalar(@treesToTake); |
---|
139 | my $treename = 'tree_RAxML_'.$$; |
---|
140 | my $infofile = 'RAxML_info.'.$RUNNAME; |
---|
141 | |
---|
142 | if ($calc_consense==0) { |
---|
143 | print 'Importing '.someWhat($treesToTake,'tree').":\n"; |
---|
144 | my $count = undef; |
---|
145 | if ($treesToTake>1) { $count = 1; } |
---|
146 | |
---|
147 | foreach (@treesToTake) { |
---|
148 | print " $_ = ".$likelyhood{$_}."\n"; |
---|
149 | my $currTreename = $treename; |
---|
150 | if (defined $count) { |
---|
151 | $currTreename .= '_'.$count; |
---|
152 | $count++; |
---|
153 | } |
---|
154 | my $command = 'arb_read_tree '.$currTreename.' '.$_.' "likelyhood='.$likelyhood{$_}.'"'; |
---|
155 | if (-f $infofile) { $command .= ' -commentFromFile '.$infofile; } |
---|
156 | |
---|
157 | system($command)==0 || die "can't execute '$command' (Reason: $!)"; |
---|
158 | } |
---|
159 | } |
---|
160 | else { # calc consense tree |
---|
161 | my $minLH = undef; |
---|
162 | my $maxLH = undef; |
---|
163 | my $meanLH = 0; |
---|
164 | |
---|
165 | print 'Consensing '.someWhat($treesToTake,'tree').":\n"; |
---|
166 | open(INTREE,'>intree') || die "can't write 'intree' (Reason: $!)"; |
---|
167 | foreach (@treesToTake) { |
---|
168 | my $LH = $likelyhood{$_}; |
---|
169 | print " $_ = $LH\n"; |
---|
170 | if (not defined $minLH) { |
---|
171 | $minLH = $maxLH = $LH; |
---|
172 | } |
---|
173 | else { |
---|
174 | if ($LH < $minLH) { $minLH = $LH; } |
---|
175 | if ($LH > $maxLH) { $maxLH = $LH; } |
---|
176 | } |
---|
177 | $meanLH += $LH; |
---|
178 | |
---|
179 | open(RAXMLTREE,'<'.$_) || die "can't read '$_' (Reason: $!)"; |
---|
180 | foreach (<RAXMLTREE>) { |
---|
181 | print INTREE $_; |
---|
182 | } |
---|
183 | close(RAXMLTREE); |
---|
184 | print INTREE "\n"; |
---|
185 | } |
---|
186 | close(INTREE); |
---|
187 | |
---|
188 | $meanLH /= $treesToTake; |
---|
189 | |
---|
190 | my $command = 'arb_echo y | consense'; |
---|
191 | system($command)==0 || die "can't execute '$command' (Reason: $!)"; |
---|
192 | |
---|
193 | if (not -f 'outtree') { |
---|
194 | die "Consense failed (no 'outtree' generated)"; |
---|
195 | } |
---|
196 | |
---|
197 | my $comment = "Consense tree of $treesToTake trees\nLikelyhood: min=$minLH mean=$meanLH max=$maxLH"; |
---|
198 | $command = 'arb_read_tree -consense '.$treesToTake.' '.$treename.' outtree "'.$comment.'"'; |
---|
199 | if (-f $infofile) { $command .= ' -commentFromFile '.$infofile; } |
---|
200 | system($command)==0 || die "can't execute '$command' (Reason: $!)"; |
---|
201 | } |
---|
202 | }; |
---|
203 | if ($@) { error($@); } |
---|
204 | } |
---|
205 | main(); |
---|