blib/lib/Bio/Tools/Run/Phylo/Phylip/SeqBoot.pm

filter: Statementstmt 43.8% 60 / 137 Branchbran 20.3% 11 / 54 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.9% 15 / 19 Podpod 100% 5 / 5 totaltotal 41.3% 91 / 220 CC 32 SCAR 36.6
CC
32
Cov
37%
_run
90.0
_setparams
90.0
_setinput
42.0
CRAP 38.8
« blib/lib/Bio/Tools/Run/Phylo/Semphy.pm ↑ Summary blib/lib/Bio/Tools/Run/Simprot.pm »
1 # BioPerl module for Bio::Tools::Run::Phylo::Phylip::SeqBoot
2 #
3 # Created by
4 #
5 # Shawn Hoon
6 #
7 # You may distribute this module under the same terms as perl itself
8  
9 # POD documentation - main docs before the code
10  
11 =head1 NAME
12  
13 Bio::Tools::Run::Phylo::Phylip::SeqBoot - Wrapper for the phylip
14 program SeqBoot
15  
16 =head1 SYNOPSIS
17  
18 #Create a SimpleAlign object
19 @params = ('ktuple' => 2, 'matrix' => 'BLOSUM');
20 $factory = Bio::Tools::Run::Alignment::Clustalw->new(@params);
21 $inputfilename = 't/data/cysprot.fa';
22 $aln = $factory->align($inputfilename); # $aln is a SimpleAlign object.
23  
24 # Use seqboot to generate bootstap alignments
25 my @params = ('datatype'=>'SEQUENCE','replicates'=>100);
26 my $seq = Bio::Tools::Run::Phylo::Phylip::SeqBoot->new(@params);
27  
28 my $aln_ref = $seq->run($aln);
29  
30 my $aio = Bio::AlignIO->new(-file=>">alignment.bootstrap",-format=>"phylip");
31 foreach my $ai(@{$aln_ref}){
32 $aio->write_aln($ai);
33 }
34  
35 # To prevent PHYLIP from truncating sequence names:
36 # Step 1. Shelf the original names:
37 my ($aln_safe, $ref_name)= # $aln_safe has serial names
38 $aln->set_displayname_safe(); # $ref_name holds orginal names
39 # Step 2. Run PHYLIP programs:
40 $aln_ref = $seq->run($aln_safe); # Use $aln_safe instead of $aln
41 # Step 3. Retrieve orgininal names
42 $aio = Bio::AlignIO->new(
43 -file=>">alignment.bootstrap",
44 -format=>"fasta"); # FASTA output to view full names
45 foreach my $ai(@{$aln_ref}){
46 my $new_aln=$ai->restore_displayname($ref_name); # Restore names
47 $aio->write_aln($new_aln);
48 }
49  
50 =head1 DESCRIPTION
51  
52 Wrapper for seqboot from the phylip package by Joseph Felsentein.
53  
54 Taken from phylip doc...
55  
56 "SEQBOOT is a general boostrapping tool. It is intended to allow you to
57 generate multiple data sets that are resampled versions of the input data set.
58 SEQBOOT can handle molecular sequences, binary characters,
59 restriction sites, or gene frequencies."
60  
61 More documentation on using seqboot and setting parameters may be found
62 in the phylip package.
63  
64 VERSION Support
65 This wrapper currently supports v3.5 of phylip. There is also support for v3.6 although
66 this is still experimental as v3.6 is still under alpha release and not all functionalities maybe supported.
67  
68 =head1 PARAMETERS FOR SEQBOOT
69  
70 =head2 MODEL
71  
72 Title : DATATYPE
73 Description : (optional)
74  
75 This program supports 3 different datatypes
76 SEQUENCE: Molecular Sequences
77 MORPH : Discrete Morphological Characters
78 REST : Restriction Sites
79 GENEFREQ: Gene Frequencies
80  
81 Defaults to SEQUENCE
82  
83 =head2 PERMUTE
84  
85 Title: PERMUTE
86 Description: (optional)
87  
88 3 different resampling methods are available:
89  
90 BOOTSTRAP : creating a new data set by sampling N
91 characters randomly with replacement The
92 resulting data set has the same size as the
93 original, but some characters have been left
94 out and others are duplicated
95  
96 JACKKNIFE : Delete-half-jackknifing. It involves sampling
97 a random half of the characters, and
98 including them in the data but dropping the
99 others The resulting data sets are half the
100 size of the original, and no characters are
101 duplicated.
102  
103 PERMUTE : Permuting species within characters. It
104 involves permuting the columns of the data
105 matrix separately. This produces data matrices
106 that have the same number and kinds of
107 characters but no taxonomic structure.
108  
109 Defaults to BOOTSTRAP
110  
111 =head2 REPLICATES
112  
113 Title : REPLICATES
114 Description : (optional)
115  
116 This options allows the user to set the number of
117 replicate data sets. Most statisticians would be
118 happiest with 1000 to 10,000 replicates in a
119 bootstrap, but 100 gives a good rough picture
120  
121 Defaults to 100
122  
123 =head2 ALLELES
124  
125 Title : ALLELES
126 Description : (optional)
127  
128 This option is to be used with gene frequencies datatype
129 option to specify that all alleles at each locus are in
130 the input file.
131  
132 Defaults to NULL
133  
134 =head1 FEEDBACK
135  
136 =head2 Mailing Lists
137  
138 User feedback is an integral part of the evolution of this and other
139 Bioperl modules. Send your comments and suggestions preferably to one
140 of the Bioperl mailing lists. Your participation is much appreciated.
141  
142 bioperl-l@bioperl.org - General discussion
143 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
144  
145 =head2 Support
146  
147 Please direct usage questions or support issues to the mailing list:
148  
149 I<bioperl-l@bioperl.org>
150  
151 rather than to the module maintainer directly. Many experienced and
152 reponsive experts will be able look at the problem and quickly
153 address it. Please include a thorough description of the problem
154 with code and data examples if at all possible.
155  
156 =head2 Reporting Bugs
157  
158 Report bugs to the Bioperl bug tracking system to help us keep track
159 the bugs and their resolution. Bug reports can be submitted via the
160 web:
161  
162 http://redmine.open-bio.org/projects/bioperl/
163  
164 =head1 AUTHOR - Shawn Hoon
165  
166 Email shawnh@fugu-sg.org
167  
168 =head1 APPENDIX
169  
170 The rest of the documentation details each of the object
171 methods. Internal methods are usually preceded with a _
172  
173 =cut
174  
175 #'
176  
177
178 package Bio::Tools::Run::Phylo::Phylip::SeqBoot;
179  
180 1 use vars qw($AUTOLOAD @ISA $PROGRAM $PROGRAMDIR $PROGRAMNAME
181 @SEQBOOT_PARAMS @OTHER_SWITCHES
182 1 ▶ %OK_FIELD);
Subroutine BEGIN: called CC=1 SCAR=0.0
183 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 1 ▶use Bio::SimpleAlign;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 1 ▶use Bio::TreeIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 1 ▶use Bio::Tools::Run::Phylo::Phylip::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 1 ▶use Bio::Tools::Run::Phylo::Phylip::PhylipConf qw(%Menu);
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1 ▶use Bio::Matrix::PhylipDist;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192  
193 # inherit from Phylip::Base which has some methods for dealing with
194 # Phylip specifics
195 1 @ISA = qw(Bio::Tools::Run::Phylo::Phylip::Base);
196  
197 # You will need to enable the SeqBoot program. This
198 # can be done in (at least) 3 ways:
199 #
200 # 1. define an environmental variable PHYLIPDIR:
201 # export PHYLIPDIR=/home/shawnh/PHYLIP/bin
202 #
203 # 2. include a definition of an environmental variable CLUSTALDIR in
204 # every script that will use Clustal.pm.
205 # $ENV{PHYLIPDIR} = '/home/shawnh/PHYLIP/bin';
206 #
207 # 3. You can set the path to the program through doing:
208 # my @params('executable'=>'/usr/local/bin/seqboot');
209 # my $SeqBoot_factory = Bio::Tools::Run::Phylo::Phylip::SeqBoot->new(@params);
210 #
211  
212  
213 BEGIN {
214 1 ▶ @SEQBOOT_PARAMS = qw(DATATYPE PERMUTE BLOCKSIZE REPLICATES READWEIGHTS READCAT);
Subroutine BEGIN: called CC=2 SCAR=6.9
215 1 @OTHER_SWITCHES = qw(QUIET);
216 1 foreach my $attr(@SEQBOOT_PARAMS,@OTHER_SWITCHES) {
217 7 $OK_FIELD{$attr}++;
218 }
219 }
220  
221 =head2 program_name
222  
223 Title : program_name
224 Usage : >program_name()
225 Function: holds the program name
226 Returns: string
227 Args : None
228  
229 =cut
230  
231 sub program_name {
232 6 ▶ return 'seqboot';
Subroutine program_name: called CC=1 SCAR=0.0
Pod documented
233 }
234  
235 =head2 program_dir
236  
237 Title : program_dir
238 Usage : ->program_dir()
239 Function: returns the program directory, obtained from ENV variable.
240 Returns: string
241 Args :
242  
243 =cut
244  
245 sub program_dir {
246 3 ▶ return Bio::Root::IO->catfile($ENV{PHYLIPDIR}) if $ENV{PHYLIPDIR};
Subroutine program_dir: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PHYLIPDIR"} 0 3 3
247 }
248  
249 sub new {
250 1 ▶ my ($class,@args) = @_;
Subroutine new: called CC=5 SCAR=16.2
Pod documented
251 1 my $self = $class->SUPER::new(@args);
252
253 1 my ($attr, $value);
254 1 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 4 1 5
255 4 $attr = shift @args;
256 4 $value = shift @args;
257 4 ▶ next if( $attr =~ /^-/ ); # don't want named parameters
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^-/ 1 3 4
258 3 ▶ if ($attr =~/PROGRAM/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /PROGRAM/i) 0 3 3
259 0 $self->executable($value);
260 0 next;
261 }
262 3 ▶ if ($attr =~ /IDLENGTH/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /IDLENGTH/i) 1 2 3
263 1 $self->idlength($value);
264 1 next;
265 }
266 2 $self->$attr($value);
267 }
268 1 return $self;
269 }
270  
271 sub AUTOLOAD {
272 2 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
273 2 my $attr = $AUTOLOAD;
274 2 $attr =~ s/.*:://;
275 2 $attr = uc $attr;
276 2 ▶ $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $Bio::Tools::Run::Phylo::Phylip::SeqBoot::OK_FIELD{$attr} 0 2 2
277 2 ▶ $self->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
278 2 return $self->{$attr};
279 }
280  
281 =head2 idlength
282  
283 Title : idlength
284 Usage : $obj->idlength ($newval)
285 Function:
286 Returns : value of idlength
287 Args : newvalue (optional)
288  
289  
290 =cut
291  
292 sub idlength{
293 1 ▶ my $self = shift;
Subroutine idlength: called CC=2 SCAR=7.0
Pod documented
294 1 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
295 1 my $value = shift;
296 1 $self->{'idlength'} = $value;
297 }
298 1 return $self->{'idlength'};
299  
300 }
301  
302  
303 =head2 run
304  
305 Title : run
306 Usage :
307 $inputfilename = 't/data/prot.phy';
308 $matrix= $seqboot_factory->run($inputfilename);
309 or
310 $seq_array_ref = \@seq_array; @seq_array is array of Seq objs
311 $aln = $clustalw_factory->align($seq_array_ref);
312 $aln_ref = $SeqBootfactory->run($aln);
313  
314 Function: Create bootstrap sets of alignments
315 Example :
316 Returns : an array ref of L<Bio::SimpleAlign>
317 Args : Name of a file containing a multiple alignment in Phylip format
318 or an SimpleAlign object
319  
320 Throws an exception if argument is not either a string (eg a
321 filename) or a Bio::SimpleAlign object. If
322 argument is string, throws exception if file corresponding to string
323 name can not be found.
324  
325 =cut
326  
327 sub run{
328  
329 0 ▶ my ($self,$input) = @_;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
330 0 my ($infilename);
331  
332 # Create input file pointer
333 0 $infilename = $self->_setinput($input);
334 0 ▶ if (!$infilename) {$self->throw("Problems setting up for seqboot. Probably bad input data in $input !");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($infilename) 0 0 0
335  
336 # Create parameter string to pass to SeqBoot program
337 0 my $param_string = $self->_setparams();
338 # run SeqBoot
339 0 my $aln = $self->_run($infilename,$param_string);
340 0 return $aln;
341 }
342  
343 #################################################
344  
345 =head2 _run
346  
347 Title : _run
348 Usage : Internal function, not to be called directly
349 Function: makes actual system call to SeqBoot program
350 Example :
351 Returns : an array ref of <Bio::SimpleAlign>
352 Args : Name of a file containing a set of multiple alignments in Phylip format
353 and a parameter string to be passed to SeqBoot
354  
355  
356 =cut
357  
358 sub _run {
359 0 ▶ my ($self,$infile,$param_string) = @_;
Subroutine _run: not called CC=9 SCAR=45.0
360 0 my $instring;
361 0 my $curpath = cwd;
362 0 ▶ unless( File::Spec->file_name_is_absolute($infile) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($infile)) 0 0 0
363 0 $infile = $self->io->catfile($curpath,$infile);
364 }
365 #odd random seed
366 0 my $rand = (2 * int(rand(10000)) + 1);
367 0 ▶ if ($self->version == 3.5){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version == 3.5) { } 0 0 0
368 0 $instring = $infile."\n$rand\n$param_string";
369 }
370 else {
371 0 $instring = $infile."\n$param_string$rand\n";
372 }
373 0 $self->debug( "Program ".$self->executable." $instring\n");
374
375 0 chdir($self->tempdir);
376 #open a pipe to run SeqBoot to bypass interactive menus
377 0 ▶ if ($self->quiet() || $self->verbose() < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->quiet or $self->verbose < 0) { } 0 0 0
Condition0%0/3
$self->quiet or $self->verbose < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->quiet or $self->verbose < 0
$self->quiet $self->verbose < 0
Truth table0%0/3
A or B
A= $self->quietB= $self->verbose < 0
ABresult
1X1
011
000
378 0 open(SeqBoot,"|".$self->executable .">/dev/null");
379 }
380 else {
381 0 open(SeqBoot,"|".$self->executable);
382 }
383 0 print SeqBoot $instring;
384 0 close(SeqBoot);
385
386 # get the results
387 0 my $outfile = $self->io->catfile($self->tempdir,$self->outfile);
388 0 chdir($curpath);
389 0 ▶ $self->throw("SeqBoot did not create files correctly ($outfile)")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $outfile 0 0 0
390 unless (-e $outfile);
391
392 #parse the alignments
393 0 my @aln;
394 my @parse_params;
395  
396 0 ▶ push @parse_params, ('-interleaved' => 1) if $self->version == 3.6;
Branch: 1 branch
BranchTrueFalseTotal
if $self->version == "3.6" 0 0 0
397 0 my $aio = Bio::AlignIO->new(-file=>$outfile,-format=>"phylip",
398 @parse_params);
399 0 ▶ while (my $aln = $aio->next_aln){
Branch: 1 branch
BranchTrueFalseTotal
if (my $aln = $aio->next_aln) 0 0 0
400 0 push @aln, $aln;
401 }
402  
403 # Clean up the temporary files created along the way...
404 0 ▶ unlink $outfile unless $self->save_tempfiles;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->save_tempfiles 0 0 0
405
406 0 return \@aln;
407 }
408  
409  
410 =head2 _setinput()
411  
412 Title : _setinput
413 Usage : Internal function, not to be called directly
414 Function: Create input file for SeqBoot program
415 Example :
416 Returns : name of file containing a multiple alignment in Phylip format
417 Args : SimpleAlign object reference or input file name
418  
419  
420 =cut
421  
422 sub _setinput {
423 0 ▶ my ($self, $input) = @_;
Subroutine _setinput: not called CC=6 SCAR=37.4
424 0 my ($alnfilename,$tfh);
425
426 # a phy formatted alignment file
427 0 ▶ unless (ref $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $input) 0 0 0
428 # check that file exists or throw
429 0 $alnfilename= $input;
430 0 ▶ unless (-e $input) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $input) 0 0 0
431 0 return $alnfilename;
432 }
433 0 ▶ my @input = ref($input) eq 'ARRAY' ? @{$input}: ($input);
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq 'ARRAY' ? : 0 0 0
434  
435 0 ($tfh,$alnfilename) = $self->io->tempfile(-dir=>$self->tempdir);
436 0 my $alnIO = Bio::AlignIO->new(-fh => $tfh,
437 -format=>'phylip',
438 -idlength=>$self->idlength());
439 0 foreach my $input(@input){
440 # $input should be a Bio::Align::AlignI
441 0 ▶ $input->isa("Bio::Align::AlignI") || $self->throw("Expecting a Bio::Align::AlignI object");
Branch: 1 branch
BranchTrueFalseTotal
$input->isa("Bio::Align::AlignI") or $self->throw("Expecting a Bio::Align::AlignI object") 0 0 0
442 # Open temporary file for both reading & writing of BioSeq array
443 0 $alnIO->write_aln($input);
444 }
445 0 $alnIO->close();
446 0 close($tfh);
447 0 return $alnfilename;
448 }
449  
450 =head2 _setparams()
451  
452 Title : _setparams
453 Usage : Internal function, not to be called directly
454 Function: Create parameter inputs for SeqBoot program
455 Example :
456 Returns : parameter string to be passed to SeqBoot
457 Args : name of calling object
458  
459 =cut
460  
461 sub _setparams {
462 0 ▶ my ($attr, $value, $self);
Subroutine _setparams: not called CC=9 SCAR=45.0
463  
464 #do nothing for now
465 0 $self = shift;
466 0 my $param_string = "";
467 0 my $cat = 0;
468 0 my $gene_freq = 0;
469 0 my %menu = %{$Menu{$self->version}->{'SEQBOOT'}};
470  
471 0 foreach my $attr ( @SEQBOOT_PARAMS) {
472 0 $value = $self->$attr();
473 0 ▶ next unless (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
474 0 ▶ if ($attr =~/REPLICATES/i){
Branch: 2 branches
BranchTrueFalseTotal
if ($attr =~ /REPLICATES/i) { } 0 0 0
elsif ($attr =~ /DATATYPE/i) { } 0 0 0
475 0 ▶ if( $value !~ /(\d+(\.\d+)?)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /(\d+(\.\d+)?)/) 0 0 0
476 0 $self->warn("Expected a number in $attr\n");
477 0 next;
478 }
479 0 $param_string .= $menu{'REPLICATES'}."$value\n";
480 }
481 elsif($attr=~/DATATYPE/i){
482 0 ▶ $gene_freq = 1 if $value =~/GENEFREQ/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /GENEFREQ/i 0 0 0
483 0 $param_string .= $menu{'DATATYPE'}{uc $value};
484 }
485 else {
486 0 ▶ if($attr =~/ALLELES/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /ALLELES/i) 0 0 0
487 0 ▶ if(!$gene_freq){
Branch: 1 branch
BranchTrueFalseTotal
unless ($gene_freq) 0 0 0
488 0 $self->warn("Alleles options only be used with alleles option");
489 0 return;
490 }
491 0 $param_string .=$menu{uc $attr};
492 }
493 }
494 }
495 0 $param_string .= $menu{'SUBMIT'};
496  
497 0 return $param_string;
498 }
499  
500  
501  
502 =head1 Bio::Tools::Run::Wrapper methods
503  
504 =cut
505  
506 =head2 no_param_checks
507  
508 Title : no_param_checks
509 Usage : $obj->no_param_checks($newval)
510 Function: Boolean flag as to whether or not we should
511 trust the sanity checks for parameter values
512 Returns : value of no_param_checks
513 Args : newvalue (optional)
514  
515  
516 =cut
517  
518 =head2 save_tempfiles
519  
520 Title : save_tempfiles
521 Usage : $obj->save_tempfiles($newval)
522 Function:
523 Returns : value of save_tempfiles
524 Args : newvalue (optional)
525  
526  
527 =cut
528  
529 =head2 outfile_name
530  
531 Title : outfile_name
532 Usage : my $outfile = $SeqBoot->outfile_name();
533 Function: Get/Set the name of the output file for this run
534 (if you wanted to do something special)
535 Returns : string
536 Args : [optional] string to set value to
537  
538  
539 =cut
540  
541  
542 =head2 tempdir
543  
544 Title : tempdir
545 Usage : my $tmpdir = $self->tempdir();
546 Function: Retrieve a temporary directory name (which is created)
547 Returns : string which is the name of the temporary directory
548 Args : none
549  
550  
551 =cut
552  
553 =head2 cleanup
554  
555 Title : cleanup
556 Usage : $codeml->cleanup();
557 Function: Will cleanup the tempdir directory after a SeqBoot run
558 Returns : none
559 Args : none
560  
561  
562 =cut
563  
564 =head2 io
565  
566 Title : io
567 Usage : $obj->io($newval)
568 Function: Gets a L<Bio::Root::IO> object
569 Returns : L<Bio::Root::IO>
570 Args : none
571  
572  
573 =cut
574  
575 1 1; # Needed to keep compiler happy
« blib/lib/Bio/Tools/Run/Phylo/Semphy.pm ↑ Summary blib/lib/Bio/Tools/Run/Simprot.pm »