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

filter: Statementstmt 22.6% 22 / 97 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 43.7% 7 / 16 Podpod 100% 9 / 9 totaltotal 23.1% 38 / 164 CC 22 SCAR 30.5
CC
22
Cov
16%
new
56.0
update_ordered_parameters
30.0
create_wrapper
20.0
CRAP 21.2
« blib/lib/Bio/Tools/Run/Pseudowise.pm ↑ Summary blib/lib/Bio/Tools/Run/Phylo/QuickTree.pm »
1 =head1 NAME
2  
3 Bio::Tools::Run::Phylo::Hyphy::BatchFile - Wrapper for custom execution of Hyphy batch files
4  
5 =head1 SYNOPSIS
6  
7 my $aln = Bio::Align::AlignI->new();
8 my $treeio = Bio::TreeIO->new(-format => "nexus", -file => "$tree_file");
9 my $tree = $treeio->next_tree();
10 my $bf_exec = Bio::Tools::Run::Phylo::Hyphy::BatchFile->new(-params => {'bf' => "hyphybatchfile.bf", 'order' => ["Universal", "Custom", $aln, "001001", $tree]});
11 $bf_exec->set_parameter('3', "012012");
12 my ($rc,$parser) = $bf_exec->run();
13  
14 =head1 DESCRIPTION
15  
16 This module creates a generic interface to processing of HBL files in HyPhy ([Hy]pothesis
17 Testing Using [Phy]logenies), a package by Sergei Kosakowsky Pond,
18 Spencer V. Muse, Simon D.W. Frost and Art Poon. See
19 http://www.hyphy.org for more information.
20  
21 Instances of this module require only a link to the batch file and an ordered list of
22 parameters, as described in the HyPhy documentation "SelectionAnalyses.pdf."
23  
24  
25 =head1 FEEDBACK
26  
27 =head2 Mailing Lists
28  
29 User feedback is an integral part of the evolution of this and other
30 Bioperl modules. Send your comments and suggestions preferably to
31 the Bioperl mailing list. Your participation is much appreciated.
32  
33 bioperl-l@bioperl.org - General discussion
34 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
35  
36 =head2 Support
37  
38 Please direct usage questions or support issues to the mailing list:
39  
40 I<bioperl-l@bioperl.org>
41  
42 rather than to the module maintainer directly. Many experienced and
43 reponsive experts will be able look at the problem and quickly
44 address it. Please include a thorough description of the problem
45 with code and data examples if at all possible.
46  
47 =head2 Reporting Bugs
48  
49 Report bugs to the Bioperl bug tracking system to help us keep track
50 of the bugs and their resolution. Bug reports can be submitted via the
51 web:
52  
53 http://redmine.open-bio.org/projects/bioperl/
54  
55 =head1 AUTHOR - Daisie Huang
56  
57 Email daisieh@zoology.ubc.ca
58  
59 =head1 CONTRIBUTORS
60  
61 Additional contributors names and emails here
62  
63 =cut
64  
65 package Bio::Tools::Run::Phylo::Hyphy::BatchFile;
66 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Bio::Root::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Bio::TreeIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Bio::Tools::Run::Phylo::Hyphy::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Bio::Tools::Run::WrapperBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 ▶use base qw(Bio::Root::Root Bio::Tools::Run::Phylo::Hyphy::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 =head2 valid_values
76  
77 Title : valid_values
78 Usage : $factory->valid_values()
79 Function: returns the possible parameters
80 Returns: an array holding all possible parameters. The default
81 values are always the first one listed. These descriptions are
82 essentially lifted from the python wrapper or provided by the author.
83 Args : None
84  
85 =cut
86  
87  
88 sub valid_values {
89 return (
90 0 ▶ {'geneticCode' => [ "Universal","VertebratemtDNA","YeastmtDNA","Mold/ProtozoanmtDNA",
Subroutine valid_values: not called CC=1 SCAR=6.9
Pod documented
91 "InvertebratemtDNA","CiliateNuclear","EchinodermmtDNA","EuplotidNuclear",
92 "Alt.YeastNuclear","AscidianmtDNA","FlatwormmtDNA","BlepharismaNuclear"]},
93 {'tempalnfile' => undef }, # aln file goes here
94 {'temptreefile' => undef }, # tree file goes here
95 );
96 }
97  
98 =head2 new
99  
100 Title : new
101 Usage : my $obj = Bio::Tools::Run::Phylo::Hyphy::BatchFile->new();
102 Function: Builds a new Bio::Tools::Run::Phylo::Hyphy::BatchFile object
103 Returns : Bio::Tools::Run::Phylo::Hyphy::BatchFile
104 Args : -alignment => the Bio::Align::AlignI object
105 -save_tempfiles => boolean to save the generated tempfiles and
106 NOT cleanup after onesself (default FALSE)
107 -tree => the Bio::Tree::TreeI object
108 -params => a hashref of parameters (all passed to set_parameter)
109 this hashref should include 'bf' => custombatchfile.bf
110 'order' => [array of ordered parameters]
111 -executable => where the hyphy executable resides
112  
113 See also: L<Bio::Tree::TreeI>, L<Bio::Align::AlignI>
114  
115 =cut
116  
117 sub new {
118 0 ▶ my($class,@args) = @_;
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
119  
120 0 my $self = $class->SUPER::new(@args);
121 0 my ($aln, $tree, $st, $params, $exe) = $self->_rearrange([qw(ALIGNMENT TREE SAVE_TEMPFILES PARAMS EXECUTABLE)], @args);
122 0 ▶ defined $aln && $self->alignment($aln);
Branch: 1 branch
BranchTrueFalseTotal
defined $aln and $self->alignment($aln) 0 0 0
123 0 ▶ defined $tree && $self->tree($tree);
Branch: 1 branch
BranchTrueFalseTotal
defined $tree and $self->tree($tree) 0 0 0
124 0 ▶ defined $st && $self->save_tempfiles($st);
Branch: 1 branch
BranchTrueFalseTotal
defined $st and $self->save_tempfiles($st) 0 0 0
125 0 ▶ defined $exe && $self->executable($exe);
Branch: 1 branch
BranchTrueFalseTotal
defined $exe and $self->executable($exe) 0 0 0
126  
127 0 $self->set_default_parameters();
128 0 ▶ if( defined $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 0 0 0
129 0 ▶ if( ref($params) !~ /HASH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref($params) =~ /HASH/i) { } 0 0 0
130 0 $self->warn("Must provide a valid hash ref for parameter -FLAGS");
131 } else {
132 0 map { $self->set_parameter($_, $$params{$_}) } keys %$params;
133 }
134 }
135 0 return $self;
136 }
137  
138 =head2 update_ordered_parameters
139  
140 Title : update_ordered_parameters
141 Usage : $BatchFile->update_ordered_parameters();
142 Function: updates all of the parameters needed for the ordered input redirect in HBL.
143 Returns : nothing
144 Args : none
145  
146 =cut
147  
148 sub update_ordered_parameters {
149 0 ▶ my ($self) = @_;
Subroutine update_ordered_parameters: not called CC=5 SCAR=34.0
Pod documented
150 0 ▶ unless (defined ($self->{'_params'}{'order'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_params"}{"order"}) 0 0 0
151 0 $self->throw("No ordered parameters for HYPHY were defined.");
152 }
153 0 ▶ for (my $i=0; $i< scalar @{$self->{'_params'}{'order'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"_params"}{"order"};}) 0 0 0
154 0 my $item = @{$self->{'_params'}{'order'}}[$i];
155 #FIXME: update_ordered_parameters should be more flexible. It should be able to tell what type of object $item is and, if necessary, create a temp file for it.
156 0 ▶ if (ref ($item) =~ m/Bio::SimpleAlign/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($item) =~ /Bio::SimpleAlign/) { } 0 0 0
elsif (ref($item) =~ /Bio::Tree::Tree/) { } 0 0 0
157 0 $item = $self->{'_params'}{'tempalnfile'};
158 } elsif (ref ($item) =~ m/Bio::Tree::Tree/) {
159 0 $item = $self->{'_params'}{'temptreefile'};
160 }
161 0 $self->{'_orderedparams'}[$i] = {$i, $item};
162 }
163 0 $self->SUPER::update_ordered_parameters();
164 }
165  
166 =head2 run
167  
168 Title : run
169 Usage : my ($rc,$results) = $BatchFile->run();
170 Function: run the Hyphy analysis using the specified batchfile and its ordered parameters
171 Returns : Return code, Hash
172 Args : none
173  
174  
175 =cut
176  
177 sub run {
178 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
179 0 my ($rc, $results) = $self->SUPER::run();
180 0 my $outfile = $self->outfile_name();
181 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
182 0 print OUTFILE $results;
183 0 close(OUTFILE);
184 0 return ($rc,$results);
185 }
186  
187  
188 =head2 create_wrapper
189  
190 Title : create_wrapper
191 Usage : $self->create_wrapper
192 Function: Creates the wrapper file for the batchfile specified in the hash, saves it to the hash as '_wrapper'.
193 Returns : nothing
194 Args : none
195  
196  
197 =cut
198  
199 sub create_wrapper {
200 0 ▶ my $self = shift;
Subroutine create_wrapper: not called CC=4 SCAR=30.0
Pod documented
201 0 my $batchfile = $self->batchfile;
202 0 ▶ unless (defined($batchfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $batchfile) 0 0 0
203 0 $self->throw("No batchfile specified, couldn't create wrapper.");
204 }
205  
206 0 ▶ unless (-f $batchfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $batchfile) 0 0 0
207 # check to see if maybe this batchfile is a template batchfile
208 0 my $new_bf = $self->io->catfile($self->hyphy_lib_dir,"TemplateBatchFiles",$batchfile);
209 0 $new_bf =~ s/\"//g;
210 0 ▶ if (-f $new_bf) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $new_bf) { } 0 0 0
211 0 $self->batchfile($new_bf);
212 } else {
213 0 $self->throw ("Specified batchfile $batchfile not found.");
214 0 return;
215 }
216 }
217 0 $self->SUPER::create_wrapper('"' . $self->batchfile . '"');
218 }
219  
220 =head2 set_parameter
221  
222 Title : set_parameter
223 Usage : $hyphy->set_parameter($param,$val);
224 Function: Sets the named parameter $param to $val if it is a non-numeric parameter
225 If $param is a number, sets the corresponding value of the ordered redirect array (starts from 1).
226 Returns : boolean if set was successful
227 Args : $param => name of the parameter
228 $value => value to set the parameter to
229  
230 =cut
231  
232  
233 sub set_parameter {
234 0 ▶ my ($self,$param,$value) = @_;
Subroutine set_parameter: not called CC=2 SCAR=17.9
Pod documented
235 0 ▶ if ($param =~ /\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /\d+/) { } 0 0 0
236 0 $self->{'_params'}{'order'}[$param-1] = $value;
237 } else {
238 0 $self->{'_params'}{$param} = $value;
239 }
240 0 return 1;
241 }
242  
243 =head2 batchfile
244  
245 Title : batchfile
246 Usage : $hyphy->batchfile($bf_name);
247 Function: Gets/sets the batchfile that is run by $hyphy.
248 Returns : The batchfile path.
249 Args : $bf_name => path of new batchfile
250  
251 =cut
252  
253 sub batchfile {
254 0 ▶ my ($self,$bf) = @_;
Subroutine batchfile: not called CC=3 SCAR=24.8
Pod documented
255 0 ▶ if (defined $bf) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bf) 0 0 0
256 0 $self->set_parameter('bf', $bf);
257 }
258  
259 0 ▶ if ($self->{'_params'}{'bf'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_params'}{'bf'}) { } 0 0 0
260 0 return $self->{'_params'}{'bf'};
261 } else {
262 0 $self->warn ("Batchfile was requested but no batchfile was found.");
263 }
264 0 return;
265 }
266  
267 =head2 make_batchfile_with_contents
268  
269 Title : make_batchfile_with_contents
270 Usage : $hyphy->make_batchfile_with_contents($bf_string);
271 Function: Creates a temporary file with the specified string of contents for the batchfile.
272 Returns : The batchfile path.
273 Args : $bf_string => contents for the batchfile
274  
275 =cut
276  
277 sub make_batchfile_with_contents {
278 0 ▶ my ($self,$bf_string) = @_;
Subroutine make_batchfile_with_contents: not called CC=2 SCAR=17.9
Pod documented
279 0 my $temp_bf = $self->io->catfile($self->tempdir,"temp.bf");
280 0 ▶ open (BF, ">", $temp_bf) or $self->throw("cannot open $temp_bf for writing");
Branch: 1 branch
BranchTrueFalseTotal
open BF, ">", $temp_bf or $self->throw("cannot open $temp_bf for writing") 0 0 0
281 0 print BF "$bf_string\n";
282 0 close BF;
283 0 return $self->batchfile($temp_bf);
284 }
285  
286  
287 =head2 set_default_parameters
288  
289 Title : set_default_parameters
290 Usage : $BatchFile->set_default_parameters(0);
291 Function: (Re)set the default parameters from the defaults
292 (the first value in each array in the
293 valid_values)
294 Returns : none
295 Args : boolean: keep existing parameter values
296  
297  
298 =cut
299  
300 sub set_default_parameters {
301 0 ▶ my ($self,$keepold) = @_;
Subroutine set_default_parameters: not called CC=4 SCAR=30.0
Pod documented
302 0 ▶ unless (defined $keepold) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $keepold) 0 0 0
303 0 $keepold = 0;
304 }
305 0 my @validvals = $self->valid_values();
306 0 ▶ for (my $i=0; $i< scalar (@validvals); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @validvals) 0 0 0
307 0 my $elem = $validvals[$i];
308 0 keys %$elem; #reset hash iterator
309 0 my ($param,$val) = each %$elem;
310 # skip if we want to keep old values and it is already set
311 0 ▶ if (ref($val)=~/ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($val) =~ /ARRAY/i) { } 0 0 0
312 0 $self->{'_orderedparams'}[$i] = {$param, $val->[0]};
313 } else {
314 0 $self->{'_orderedparams'}[$i] = {$param, $val};
315 }
316 #FIXME: for alignment and treefile, this should default to the ones in params.
317 }
318 }
319  
320 1 1;
« blib/lib/Bio/Tools/Run/Pseudowise.pm ↑ Summary blib/lib/Bio/Tools/Run/Phylo/QuickTree.pm »