blib/lib/Bio/Tools/Run/Phylo/Hyphy/Modeltest.pm

filter: Statementstmt 50.5% 51 / 101 Branchbran 28.8% 15 / 52 Conditioncond 16.6% 2 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 84.6% 11 / 13 Podpod 100% 4 / 4 totaltotal 44.4% 84 / 189 CC 33 SCAR 39.8
CC
33
Cov
41%
run
156.0
set_default_parameters
35.4
new
10.7
CRAP 53.5
« blib/lib/Bio/Tools/Run/RNAMotif.pm ↑ Summary blib/lib/Bio/Tools/Run/Phylo/Phyml.pm »
1 # $Id$
2 #
3 # BioPerl module for Bio::Tools::Run::Phylo::Hyphy::Modeltest
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Albert Vilella <avilella-at-gmail-dot-com>
8 #
9 # Copyright Albert Vilella
10 #
11 # You may distribute this module under the same terms as perl itself
12  
13 # POD documentation - main docs before the code
14  
15 =head1 NAME
16  
17 Bio::Tools::Run::Phylo::Hyphy::Modeltest - Wrapper around the Hyphy Modeltest analysis
18  
19 =head1 SYNOPSIS
20  
21 use Bio::Tools::Run::Phylo::Hyphy::Modeltest;
22 use Bio::AlignIO;
23 use Bio::TreeIO;
24  
25 my $alignio = Bio::AlignIO->new(-format => 'fasta',
26 -file => 't/data/hyphy1.fasta');
27  
28 my $aln = $alignio->next_aln;
29 my $treeio = Bio::TreeIO->new(
30 -format => 'newick', -file => 't/data/hyphy1.tree');
31  
32 my $modeltest = Bio::Tools::Run::Phylo::Hyphy::Modeltest->new();
33 $modeltest->alignment($aln);
34 $modeltest->tree($tree);
35 my ($rc,$results) = $modeltest->run();
36  
37 =head1 DESCRIPTION
38  
39 This is a wrapper around the Modeltest analysis of HyPhy ([Hy]pothesis
40 Testing Using [Phy]logenies) package of Sergei Kosakowsky Pond,
41 Spencer V. Muse, Simon D.W. Frost and Art Poon. See
42 http://www.hyphy.org for more information.
43  
44 This module will generate the correct list of options for interfacing
45 with TemplateBatchFiles/Ghostrides/Modeltestwrapper.bf.
46  
47 =head1 FEEDBACK
48  
49 =head2 Mailing Lists
50  
51 User feedback is an integral part of the evolution of this and other
52 Bioperl modules. Send your comments and suggestions preferably to
53 the Bioperl mailing list. Your participation is much appreciated.
54  
55 bioperl-l@bioperl.org - General discussion
56 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
57  
58 =head2 Support
59  
60 Please direct usage questions or support issues to the mailing list:
61  
62 I<bioperl-l@bioperl.org>
63  
64 rather than to the module maintainer directly. Many experienced and
65 reponsive experts will be able look at the problem and quickly
66 address it. Please include a thorough description of the problem
67 with code and data examples if at all possible.
68  
69 =head2 Reporting Bugs
70  
71 Report bugs to the Bioperl bug tracking system to help us keep track
72 of the bugs and their resolution. Bug reports can be submitted via the
73 web:
74  
75 http://redmine.open-bio.org/projects/bioperl/
76  
77 =head1 AUTHOR - Albert Vilella
78  
79 Email avilella-at-gmail-dot-com
80  
81 =head1 CONTRIBUTORS
82  
83 Additional contributors names and emails here
84  
85 =head1 APPENDIX
86  
87 The rest of the documentation details each of the object methods.
88 Internal methods are usually preceded with a _
89  
90 =cut
91  
92  
93 # Let the code begin...
94  
95  
96 package Bio::Tools::Run::Phylo::Hyphy::Modeltest;
97 1 ▶use vars qw(@ISA @VALIDVALUES $PROGRAMNAME $PROGRAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use Bio::Root::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use Bio::TreeIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use Bio::Tools::Run::Phylo::Hyphy::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use Bio::Tools::Run::WrapperBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 1 @ISA = qw(Bio::Root::Root Bio::Tools::Run::Phylo::Hyphy::Base);
106  
107 =head2 Default Values
108  
109 Valid and default values for Modeltest are listed below. The default
110 values are always the first one listed. These descriptions are
111 essentially lifted from the python wrapper or provided by the author.
112  
113 INCOMPLETE DOCUMENTATION OF ALL METHODS
114  
115 =cut
116  
117 BEGIN {
118 1 ▶ $PROGRAMNAME = 'HYPHYMP' . ($^O =~ /mswin/i ?'.exe':'');
Subroutine BEGIN: called CC=4 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin/i ? : 0 1 1
119 1 ▶ if( defined $ENV{'HYPHYDIR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HYPHYDIR"}) 0 1 1
120 0 ▶ $PROGRAM = Bio::Root::IO->catfile($ENV{'HYPHYDIR'},$PROGRAMNAME). ($^O =~ /mswin/i ?'.exe':'');;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin/i ? : 0 0 0
121 }
122  
123 @VALIDVALUES =
124 (
125 1 {'tempalnfile' => undef }, # aln file goes here
126 {'temptreefile' => undef }, # tree file goes here
127 {'Number of Rate Classes' => [ '4' ] },
128 {'Model Selection Method' => [ 'Both',
129 'Hierarchical Test',
130 'AIC Test'] },
131 {'Model rejection level' => '0.05' },
132 );
133 }
134  
135  
136 =head2 new
137  
138 Title : new
139 Usage : my $obj = Bio::Tools::Run::Phylo::Hyphy::Modeltest->new();
140 Function: Builds a new Bio::Tools::Run::Phylo::Hyphy::Modeltest object
141 Returns : Bio::Tools::Run::Phylo::Hyphy::Modeltest
142 Args : -alignment => the Bio::Align::AlignI object
143 -save_tempfiles => boolean to save the generated tempfiles and
144 NOT cleanup after onesself (default FALSE)
145 -tree => the Bio::Tree::TreeI object
146 -params => a hashref of parameters (all passed to set_parameter)
147 -executable => where the hyphy executable resides
148  
149 See also: L<Bio::Tree::TreeI>, L<Bio::Align::AlignI>
150  
151 =cut
152  
153 sub new {
154 1 ▶ my($class,@args) = @_;
Subroutine new: called CC=7 SCAR=23.7
Pod documented
155  
156 1 my $self = $class->SUPER::new(@args);
157 1 my ($aln, $tree, $st, $params, $exe,
158 $ubl) = $self->_rearrange([qw(ALIGNMENT TREE SAVE_TEMPFILES
159 PARAMS EXECUTABLE)],
160 @args);
161 1 ▶ defined $aln && $self->alignment($aln);
Branch: 1 branch
BranchTrueFalseTotal
defined $aln and $self->alignment($aln) 0 1 1
162 1 ▶ defined $tree && $self->tree($tree);
Branch: 1 branch
BranchTrueFalseTotal
defined $tree and $self->tree($tree) 0 1 1
163 1 ▶ defined $st && $self->save_tempfiles($st);
Branch: 1 branch
BranchTrueFalseTotal
defined $st and $self->save_tempfiles($st) 0 1 1
164 1 ▶ defined $exe && $self->executable($exe);
Branch: 1 branch
BranchTrueFalseTotal
defined $exe and $self->executable($exe) 0 1 1
165  
166 1 $self->set_default_parameters();
167 1 ▶ if( defined $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 0 1 1
168 0 ▶ if( ref($params) !~ /HASH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref($params) =~ /HASH/i) { } 0 0 0
169 0 $self->warn("Must provide a valid hash ref for parameter -FLAGS");
170 } else {
171 0 map { $self->set_parameter($_, $$params{$_}) } keys %$params;
172 }
173 }
174 1 return $self;
175 }
176  
177  
178 =head2 run
179  
180 Title : run
181 Usage : my ($rc,$results) = $modeltest->run($aln);
182 Function: run the modeltest analysis using the default or updated parameters
183 the alignment parameter must have been set
184 Returns : Return code, Hash
185 Args : L<Bio::Align::AlignI> object,
186 L<Bio::Tree::TreeI> object [optional]
187  
188  
189 =cut
190  
191 sub run {
192 0 ▶ my ($self,$aln,$tree) = @_;
Subroutine run: not called CC=12 SCAR=50.5
Pod documented
193  
194 0 ▶ $self->prepare($aln,$tree) unless (defined($self->{'_prepared'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_prepared"} 0 0 0
195 0 my ($rc,$results) = (1);
196 {
197 0 my $commandstring;
198 my $exit_status;
199 0 my $tempdir = $self->tempdir;
200 0 my $modeltestexe = $self->executable();
201 0 ▶ $self->throw("unable to find or run executable for 'HYPHY'") unless $modeltestexe && -e $modeltestexe && -x _;
Branch: 1 branch
BranchTrueFalseTotal
unless $modeltestexe and -e $modeltestexe and -x _ 0 0 0
Condition0%0/6
$modeltestexe and -e $modeltestexe and -x _
!ll&&!rl&&r
000
$modeltestexe and -e $modeltestexe
!ll&&!rl&&r
000
MC/DC0%0/3
$modeltestexe and -e $modeltestexe and -x _
$modeltestexe -e $modeltestexe -x _
Truth table0%0/4
A and B and C
A= $modeltestexeB= -e $modeltestexeC= -x _
ABCresult
0XX0
10X0
1100
1111
202 0 $commandstring = $modeltestexe . " BASEPATH=" . $self->program_dir . " " . $self->{'_wrapper'};
203 0 ▶ open(RUN, "$commandstring |") or $self->throw("Cannot open exe $modeltestexe");
Branch: 1 branch
BranchTrueFalseTotal
open RUN, "$commandstring |" or $self->throw("Cannot open exe $modeltestexe") 0 0 0
204 0 my @output = <RUN>;
205 0 $exit_status = close(RUN);
206 0 $self->error_string(join('',@output));
207 0 ▶ if( (grep { /\berr(or)?: /io } @output) || !$exit_status) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\berr(or)?: /io;} @output or not $exit_status) 0 0 0
Condition0%0/3
grep {/\berr(or)?: /io;} @output or not $exit_status
l!l&&r!l&&!r
000
MC/DC0%0/2
grep {/\berr(or)?: /io;} @output or not $exit_status
grep {/\berr(or)?: /io;} @output not $exit_status
Truth table0%0/3
A or not(B)
A= grep {/\berr(or)?: /io;} @outputB= not $exit_status
ABresult
1X1
011
000
208 0 $self->warn("There was an error - see error_string for the program output");
209 0 $rc = 0;
210 }
211 0 my $outfile = $self->outfile_name;
212 0 eval {
213 0 ▶ open(OUTFILE, ">$outfile") or $self->throw("cannot open $outfile for writing");
Branch: 1 branch
BranchTrueFalseTotal
open OUTFILE, ">$outfile" or $self->throw("cannot open $outfile for writing") 0 0 0
214 # FIXME -- needs output parsing -- ask hyphy to clean that up into a tsv?
215 0 foreach my $output (@output) {
216 0 print OUTFILE $output;
217 0 $results .= sprintf($output);
218 }
219 0 close(OUTFILE);
220 };
221 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
222 0 $self->warn($self->error_string);
223 }
224 }
225 0 ▶ unless ( $self->save_tempfiles ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->save_tempfiles) 0 0 0
226 0 unlink($self->{'_wrapper'});
227 0 $self->cleanup();
228 }
229 0 return ($rc,$results);
230 }
231  
232  
233 =head2 create_wrapper
234  
235 Title : create_wrapper
236 Usage : $self->create_wrapper
237 Function: It will create the wrapper file that interfaces with the analysis bf file
238 Example :
239 Returns :
240 Args :
241  
242  
243 =cut
244  
245 sub create_wrapper {
246 0 ▶ my $self = shift;
Subroutine create_wrapper: not called CC=1 SCAR=6.9
Pod documented
247  
248 0 my $batchfile = 'ModelTest.bf';
249 0 $self->SUPER::create_wrapper($batchfile);
250 }
251  
252  
253 =head2 set_default_parameters
254  
255 Title : set_default_parameters
256 Usage : $modeltest->set_default_parameters(0);
257 Function: (Re)set the default parameters from the defaults
258 (the first value in each array in the
259 %VALIDVALUES class variable)
260 Returns : none
261 Args : boolean: keep existing parameter values
262  
263  
264 =cut
265  
266 sub set_default_parameters {
267 1 ▶ my ($self,$keepold) = @_;
Subroutine set_default_parameters: called CC=12 SCAR=35.7
Pod documented
268 1 ▶ $keepold = 0 unless defined $keepold;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keepold 1 0 1
269 1 foreach my $elem (@VALIDVALUES) {
270 5 my ($param,$val) = each %$elem;
271 # skip if we want to keep old values and it is already set
272 5 ▶ if (ref($val)=~/ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($val) =~ /ARRAY/i) 2 3 5
273 2 ▶ unless (ref($val->[0])=~/HASH/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($val->[0]) =~ /HASH/i) { } 0 2 2
274 2 push @{ $self->{'_orderedparams'} }, {$param, $val->[0]};
275 } else {
276 0 $val = $val->[0];
277 }
278 }
279 5 ▶ if ( ref($val) =~ /HASH/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($val) =~ /HASH/i) { } 0 5 5
elsif (not ref($val) =~ /HASH/i || ref($val) =~ /ARRAY/i) { } 3 2 5
280 0 my $prevparam;
281 0 ▶ while (defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
282 0 ▶ last unless (ref($val) =~ /HASH/i);
Branch: 1 branch
BranchTrueFalseTotal
unless ref($val) =~ /HASH/i 0 0 0
283 0 ▶ last unless (defined($param));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 0 0
284 0 $prevparam = $param;
285 0 ($param,$val) = each %{$val};
286 0 push @{ $self->{'_orderedparams'} }, {$prevparam, $param};
287 0 ▶ push @{ $self->{'_orderedparams'} }, {$param, $val} if (defined($val));
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
288 }
289 ▶ } elsif (ref($val) !~ /HASH/i && ref($val) !~ /ARRAY/i) {
Condition66%2/3
ref($val) =~ /HASH/i || ref($val) =~ /ARRAY/i
l!l&&r!l&&!r
023
MC/DC50%1/2
ref($val) =~ /HASH/i || ref($val) =~ /ARRAY/i
ref($val) =~ /HASH/i ref($val) =~ /ARRAY/i
Truth table66%2/3
A || B
A= ref($val) =~ /HASH/iB= ref($val) =~ /ARRAY/i
ABresult
1X1
011
000
290 3 push @{ $self->{'_orderedparams'} }, {$param, $val};
291 }
292 }
293 }
294  
295  
296 =head1 Bio::Tools::Run::WrapperBase methods
297  
298 =cut
299  
300 =head2 save_tempfiles
301  
302 Title : save_tempfiles
303 Usage : $obj->save_tempfiles($newval)
304 Function:
305 Returns : value of save_tempfiles
306 Args : newvalue (optional)
307  
308  
309 =cut
310  
311  
312 =head2 tempdir
313  
314 Title : tempdir
315 Usage : my $tmpdir = $self->tempdir();
316 Function: Retrieve a temporary directory name (which is created)
317 Returns : string which is the name of the temporary directory
318 Args : none
319  
320  
321 =cut
322  
323 =head2 cleanup
324  
325 Title : cleanup
326 Usage : $modeltest->cleanup();
327 Function: Will cleanup the tempdir directory after a run
328 Returns : none
329 Args : none
330  
331  
332 =cut
333  
334 =head2 io
335  
336 Title : io
337 Usage : $obj->io($newval)
338 Function: Gets a L<Bio::Root::IO> object
339 Returns : L<Bio::Root::IO>
340 Args : none
341  
342  
343 =cut
344  
345 sub DESTROY {
346 1 ▶ my $self= shift;
Subroutine DESTROY: called CC=2 SCAR=7.0
347 1 ▶ unless ( $self->save_tempfiles ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->save_tempfiles) 1 0 1
348 1 $self->cleanup();
349 }
350 1 $self->SUPER::DESTROY();
351 }
352  
353 1 1;
« blib/lib/Bio/Tools/Run/RNAMotif.pm ↑ Summary blib/lib/Bio/Tools/Run/Phylo/Phyml.pm »