Tools/Run/Phylo/PAML/Baseml.pm

filter: Statementstmt 22.1% 33 / 149 Branchbran 2.8% 2 / 70 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 47.6% 10 / 21 Podpod 100% 11 / 11 totaltotal 19.3% 56 / 289 SCAR 72.2
CC
46
Cov
14%
run
812.0
set_default_parameters
42.0
set_parameter
30.0
CRAP 1373.0
↑ Summary Tools/Run/Phylo/PAML/Evolver.pm »
1 package Bio::Tools::Run::Phylo::PAML::Baseml;
2 1 $Bio::Tools::Run::Phylo::PAML::Baseml::VERSION = '1.7.2';
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use vars qw(@ISA %VALIDVALUES $MINNAMELEN $PROGRAMNAME $PROGRAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Bio::TreeIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Bio::Tools::Phylo::PAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use base qw(Bio::Tools::Run::Phylo::PhyloBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ABSTRACT: Wrapper aroud the PAML program baseml
16 # AUTHOR: Jason Stajich <jason@bioperl.org>
17 # OWNER: Jason Stajich <jason@bioperl.org>
18 # LICENSE: Perl_5
19  
20 # AUTHOR: Sendu Bala <bix@sendu.me.uk>
21  
22  
23 BEGIN {
24 1 ▶ $MINNAMELEN = 25;
Subroutine BEGIN: called CC=3 SCAR=12.0
25 1 ▶ $PROGRAMNAME = 'baseml' . ($^O =~ /mswin/i ?'.exe':'');
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin/i ? : 0 1 1
26 1 ▶ if( defined $ENV{'PAMLDIR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PAMLDIR"}) 0 1 1
27 0 $PROGRAM = Bio::Root::IO->catfile($ENV{'PAMLDIR'},$PROGRAMNAME);
28 }
29 # valid values for parameters, the default one is always
30 # the first one in the array
31 # much of the documentation here is lifted directly from the baseml.ctl
32 # example file provided with the package
33 %VALIDVALUES = (
34 1 'noisy' => [ 0..3,9],
35 'verbose' => [ 0,1,2], # 0:concise, 1:detailed, 2:too much
36 'runmode' => [0..5],
37 # for runmode
38 # 0: use the provided tree structure(s) in treefile
39 # 1,2: mean heuristic search by star-decomposition alg
40 # 2: starts from star tree while 1 reads a multifurcating
41 # tree from treefile and ties to estimate the best
42 # bifurcating tree
43 # 3: stepwise addition
44 # 4: NNI perturbation with the starting tree
45 # Tree search DOES NOT WORK WELL so estimate a tree
46 # using other programs first
47 'model' => [5, 0..8],
48 # for model
49 # 0: JC69 (uncorrected)
50 # 1: K80 (transitions/transversion weighted differently)
51 # 2: F81
52 # 3: F84
53 # 4: HKY85
54 # 5: T92 (Tamura 92)
55 # 6: TN93 (Tajima-Nei) correct for multiple substitutions
56 # 7: REV (aka GTR)
57 # 8: UNREST
58 # See Yang 1994 JME 39:105-111
59  
60 # model 8 special case of the REV model
61 # model 9 is special case of unrestricted model
62 # can also supply special rate parameters
63 # so for example (from pamlDOC.pdf
64 # $model = '8 [2 (CT) (AG)]'; # TN93
65 # $model = '8 [2 (TA AT TG CA CG) (AG)]'; # TN93
66 # $model = '9 [1 (TC CT AG GA)]; # K80
67 # $model = '9 [0]'; # JC69
68 # $model = '9 [11 (TA) (TG) (CT) (CA) (CG) (AT) (AC) (AG) (GT) (GC) (GA)],
69  
70 'outfile' => 'mlb',
71 'fix_kappa'=> [0,1], # 0:estimate kappa, 1:fix kappa
72 'kappa' => '2.5', # initial or fixed kappa
73 'fix_alpha'=> [1,0], # 0: estimate gamma shape param
74 # 1: fix it at alpha
75 'alpha' => '0', # initial of fixed alpha
76 # 0: infinity (constant rate)
77 'Malpha' => [0,1], # different alphas for genes
78  
79 'fix_rho'=> [1,0], # 0: estimate gamma shape param
80 # 1: fix it at alpha
81 'rho' => '0', # initial of fixed alpha
82 # 0: infinity (constant rate)
83  
84 'ncatG' => '5', # number of categories in the dD,AdG, or nparkK models of rates
85 'nparK' => [0..4], # rate-class models
86 # 1:rk 2:rk&fK
87 # 3:rK&MK(1/K) 4:rK&MK
88 'nhomo' => [0..4], # 0 & 1: homogeneous,
89 # 2: kappa for brances
90 # 3:N1 4:N2
91 'getSE' => [0,1],
92 'RateAncestor' => [0,1,2], # rates (alpha > 0) or
93 # ancestral states
94 'cleandata' => [1,0], # remove sites with
95 # ambiguity data (1:yes or 0:no)
96  
97 'fix_blength' => [0,-1,1,2], # 0: ignore, -1: random,
98 # 1: initial, 2: fixed
99  
100 'icode' => [ 0..10], # (with RateAncestor=1.
101 #try "GC" in data,model=4,Mgene=4)
102 'ndata' => [1..10],
103 'clock' => [0..3], # 0: no clock, 1: clock, 2: local clock, 3: CombinedAnalysis
104 'Small_Diff' => '1e-6', #underflow issues?
105 'Mgene' => [0..4], # 0:rates, 1:separate; 2:diff pi, 3:diff kapa, 4:all diff
106 );
107 }
108  
109  
110  
111 sub program_name {
112 0 ▶ return $PROGRAMNAME;
Subroutine program_name: not called CC=1 SCAR=6.9
Pod documented
113 }
114  
115  
116 sub program_dir {
117 0 ▶ return Bio::Root::IO->catfile($ENV{PAMLDIR}) if $ENV{PAMLDIR};
Subroutine program_dir: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PAMLDIR"} 0 0 0
118 }
119  
120  
121 sub new {
122 0 ▶ my($class,@args) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
123  
124 0 my $self = $class->SUPER::new(@args);
125 0 my ($aln,$tree,$st) = $self->_rearrange([qw(ALIGNMENT TREE SAVE_TEMPFILES)],
126 @args);
127 0 ▶ defined $aln && $self->alignment($aln);
Branch: 1 branch
BranchTrueFalseTotal
defined $aln and $self->alignment($aln) 0 0 0
128 0 ▶ defined $tree && $self->tree($tree);
Branch: 1 branch
BranchTrueFalseTotal
defined $tree and $self->tree($tree) 0 0 0
129 0 ▶ defined $st && $self->save_tempfiles($st);
Branch: 1 branch
BranchTrueFalseTotal
defined $st and $self->save_tempfiles($st) 0 0 0
130  
131 0 return $self;
132 }
133  
134  
135 sub run {
136 0 ▶ my ($self, $aln, $tree) = @_;
Subroutine run: not called CC=28 SCAR=67.0
Pod documented
137 0 ▶ $aln = $self->alignment($aln) if $aln;
Branch: 1 branch
BranchTrueFalseTotal
if $aln 0 0 0
138 0 ▶ $tree = $self->tree($tree) if $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $tree 0 0 0
139 0 ▶ $aln ||= $self->alignment();
Condition0%0/3
$aln ||= $self->alignment
l!l&&r!l&&!r
000
MC/DC0%0/2
$aln ||= $self->alignment
$aln $self->alignment
Truth table0%0/3
A ||= B
A= $alnB= $self->alignment
ABresult
1X1
011
000
140 0 ▶ $tree ||= $self->tree();
Condition0%0/3
$tree ||= $self->tree
l!l&&r!l&&!r
000
MC/DC0%0/2
$tree ||= $self->tree
$tree $self->tree
Truth table0%0/3
A ||= B
A= $treeB= $self->tree
ABresult
1X1
011
000
141  
142 0 my %params = $self->get_parameters;
143 0 ▶ if( ! $aln ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aln) 0 0 0
144 0 $self->warn("must have supplied a valid aligment file in order to run baseml");
145 0 return 0;
146 }
147 0 ▶ if ((defined $params{runmode} && ($params{runmode} == 0 || $params{runmode} == 1)) && ! $tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"runmode"} and $params{"runmode"} == 0 || $params{"runmode"} == 1 and not $tree) 0 0 0
Condition0%0/9
defined $params{"runmode"} and $params{"runmode"} == 0 || $params{"runmode"} == 1 and not $tree
!ll&&!rl&&r
000
defined $params{"runmode"} and $params{"runmode"} == 0 || $params{"runmode"} == 1
!ll&&!rl&&r
000
$params{"runmode"} == 0 or $params{"runmode"} == 1
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $params{"runmode"} and $params{"runmode"} == 0 || $params{"runmode"} == 1 and not $tree
defined $params{"runmode"} $params{"runmode"} == 0 $params{"runmode"} == 1 not $tree
Truth table0%0/6
A and B or C and not(D)
A= defined $params{"runmode"}B= $params{"runmode"} == 0C= $params{"runmode"} == 1D= not $tree
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
148 0 $self->warn("must have supplied a tree in order to run baseml in runmode 0 or 1");
149 0 return 0;
150 }
151  
152 # replace spaces with underscores in ids, since baseml really doesn't like
153 # spaces (actually, the resulting double quotes) in tree ids
154 0 ▶ if ($tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree) 0 0 0
155 0 my $changed = 0;
156 0 ▶ foreach my $thing ($aln->each_seq, $tree ? $tree->get_leaf_nodes : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$tree ? : 0 0 0
157 0 my $id = $thing->id;
158 0 ▶ if ($id =~ / /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ / /) 0 0 0
159 0 $id =~ s/\s+/_/g;
160 0 $thing->id($id);
161 0 $changed = 1;
162 }
163 }
164 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 0 0
165 0 my $new_aln = $aln->new;
166 0 foreach my $seq ($aln->each_seq) {
167 0 $new_aln->add_seq($seq);
168 }
169 0 $aln = $new_aln;
170 0 $aln = $self->alignment($aln);
171 0 $tree = $self->tree($tree);
172 }
173  
174 # check node and seq names match
175 0 $self->_check_names;
176 }
177  
178 # output the alignment and tree to tempfiles
179 0 ▶ my $tempseqfile = $self->_write_alignment('phylip',
Branch: 1 branch
BranchTrueFalseTotal
$Bio::Tools::Run::Phylo::PAML::Baseml::MINNAMELEN > $aln->maxdisplayname_length ? : 0 0 0
180 -interleaved => 0,
181 -idlinebreak => 1,
182 -line_length => 60,
183 -wrap_sequential => 1,
184 -idlength => $MINNAMELEN > $aln->maxdisplayname_length() ? $MINNAMELEN : $aln->maxdisplayname_length() +1);
185 0 ▶ $tree = $self->_write_tree() if $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $tree 0 0 0
186  
187 # now let's print the baseml.ctl file.
188 # many of the these programs are finicky about what the filename is
189 # and won't even run without the properly named file. Ack
190 0 my $tmpdir = $self->tempdir();
191 0 my $baseml_ctl = "$tmpdir/baseml.ctl";
192 0 ▶ open(my $mlfh, ">$baseml_ctl") or $self->throw("cannot open $baseml_ctl for writing");
Branch: 1 branch
BranchTrueFalseTotal
open my $mlfh, ">$baseml_ctl" or $self->throw("cannot open $baseml_ctl for writing") 0 0 0
193 0 print $mlfh "seqfile = $tempseqfile\n";
194 0 ▶ print $mlfh "treefile = $tree\n" if $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $tree 0 0 0
195  
196 0 my $outfile = $self->outfile_name;
197  
198 0 print $mlfh "outfile = $outfile\n";
199 0 ▶ while( my ($param,$val) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 0 0 0
200 0 ▶ next if $param eq 'outfile';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "outfile" 0 0 0
201 0 print $mlfh "$param = $val\n";
202 }
203 0 close($mlfh);
204  
205 0 my ($rc,$parser) = (1);
206 {
207 0 my $cwd = cwd();
208 0 my $exit_status;
209 0 chdir($tmpdir);
210 0 my $ynexe = $self->executable();
211 0 ▶ $self->throw("unable to find executable for 'baseml'") unless $ynexe;
Branch: 1 branch
BranchTrueFalseTotal
unless $ynexe 0 0 0
212 0 open(my $run, "$ynexe |");
213 0 my @output = <$run>;
214 0 $exit_status = close($run);
215 0 $self->error_string(join('', grep { /\berr(or)?: /io } @output));
216 0 ▶ if ($self->error_string || !$exit_status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error_string or not $exit_status) 0 0 0
Condition0%0/3
$self->error_string or not $exit_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->error_string or not $exit_status
$self->error_string not $exit_status
Truth table0%0/3
A or not(B)
A= $self->error_stringB= not $exit_status
ABresult
1X1
011
000
217 0 $self->warn("There was an error - see error_string for the program output");
218 0 $rc = 0;
219 }
220 0 eval {
221 0 $parser = Bio::Tools::Phylo::PAML->new(-file => "$tmpdir/mlb",
222 -dir => "$tmpdir");
223  
224 };
225 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
226 0 $self->warn($self->error_string);
227 }
228  
229 0 chdir($cwd);
230 }
231 0 ▶ if( $self->verbose > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 0) 0 0 0
232 0 open(my $in, "$tmpdir/mlb");
233 0 ▶ while(<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 0 0 0
234 0 $self->debug($_);
235 }
236 0 close($in);
237 }
238  
239 0 return ($rc,$parser);
240 }
241  
242  
243 sub error_string {
244 0 ▶ my ($self,$value) = @_;
Subroutine error_string: not called CC=2 SCAR=17.9
Pod documented
245 0 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
246 0 chomp($value);
247 0 $self->{'error_string'} = $value;
248 }
249 0 return $self->{'error_string'};
250  
251 }
252  
253  
254 sub alignment{
255 0 ▶ my $self = shift;
Subroutine alignment: not called CC=1 SCAR=6.9
Pod documented
256 0 return $self->_alignment(@_);
257 }
258  
259  
260 sub tree {
261 0 ▶ my $self = shift;
Subroutine tree: not called CC=1 SCAR=6.9
Pod documented
262 0 return $self->_tree(@_);
263 }
264  
265  
266 sub get_parameters{
267 0 ▶ my ($self) = @_;
Subroutine get_parameters: not called CC=1 SCAR=6.9
Pod documented
268 # we're returning a copy of this
269 0 return %{ $self->{'_basemlparams'} };
270 }
271  
272  
273  
274 sub set_parameter{
275 0 ▶ my ($self,$param,$value) = @_;
Subroutine set_parameter: not called CC=5 SCAR=34.0
Pod documented
276  
277 0 ▶ if( ! defined $VALIDVALUES{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param}) 0 0 0
278 0 $self->warn("unknown parameter $param will not set unless you force by setting no_param_checks to true");
279 0 return 0;
280 }
281 0 ▶ if( ref( $VALIDVALUES{$param}) =~ /ARRAY/i &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref($Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param}) =~ /ARRAY/i and scalar @{$Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param};} > 0) 0 0 0
Condition0%0/3
ref($Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param}) =~ /ARRAY/i and scalar @{$Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref($Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param}) =~ /ARRAY/i and scalar @{$Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param};} > 0
ref($Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param}) =~ /ARRAY/i scalar @{$Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param};} > 0
Truth table0%0/3
A and B
A= ref($Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param}) =~ /ARRAY/iB= scalar @{$Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES{$param};} > 0
ABresult
0X0
100
111
282 0 scalar @{$VALIDVALUES{$param}} > 0 ) {
283  
284 0 my %allowed = map { $_ => 1 } @{ $VALIDVALUES{$param} };
285 0 ▶ unless ( exists $allowed{$value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $allowed{$value}) 0 0 0
286 0 $self->warn("parameter $param specified value $value is not recognized, please see the documentation and the code for this module or set the no_param_checks to a true value");
287 0 return 0;
288 }
289 }
290 0 $self->{'_basemlparams'}->{$param} = $value;
291 0 return 1;
292 }
293  
294  
295 sub set_default_parameters{
296 0 ▶ my ($self,$keepold) = @_;
Subroutine set_default_parameters: not called CC=6 SCAR=37.4
Pod documented
297 0 ▶ $keepold = 0 unless defined $keepold;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keepold 0 0 0
298  
299 0 ▶ while( my ($param,$val) = each %VALIDVALUES ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Bio::Tools::Run::Phylo::PAML::Baseml::VALIDVALUES) 0 0 0
300 # skip if we want to keep old values and it is already set
301 0 ▶ next if( defined $self->{'_basemlparams'}->{$param} && $keepold);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_basemlparams"}{$param} and $keepold 0 0 0
Condition0%0/3
defined $self->{"_basemlparams"}{$param} and $keepold
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_basemlparams"}{$param} and $keepold
defined $self->{"_basemlparams"}{$param} $keepold
Truth table0%0/3
A and B
A= defined $self->{"_basemlparams"}{$param}B= $keepold
ABresult
0X0
100
111
302 0 ▶ if(ref($val)=~/ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($val) =~ /ARRAY/i) { } 0 0 0
303 0 $self->{'_basemlparams'}->{$param} = $val->[0];
304 } else {
305 0 $self->{'_basemlparams'}->{$param} = $val;
306 }
307 }
308 }
309  
310  
311  
312  
313  
314 sub outfile_name {
315 0 ▶ my $self = shift;
Subroutine outfile_name: not called CC=3 SCAR=24.8
Pod documented
316 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
317 0 return $self->{'_basemlparams'}->{'outfile'} = shift @_;
318 }
319 0 ▶ unless (defined $self->{'_basemlparams'}->{'outfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_basemlparams"}{"outfile"}) 0 0 0
320 0 $self->{'_basemlparams'}->{'outfile'} = 'mlb';
321 }
322 0 return $self->{'_basemlparams'}->{'outfile'};
323 }
324  
325  
326  
327  
328 1 1;
329  
330 __END__
↑ Summary Tools/Run/Phylo/PAML/Evolver.pm »