File Coverage

blib/lib/Bio/Tools/Run/Phylo/Phyml.pm
Criterion Covered Total %
statement 90 283 31.8
branch 53 230 23.0
condition 19 79 24.0
subroutine 16 36 44.4
pod 26 26 100.0
total 204 654 31.1


line stmt bran cond sub pod time code
1             #
2             # BioPerl module for Bio::Tools::Run::Phylo::Phyml
3             #
4             # Please direct questions and support issues to
5             #
6             # Cared for by Heikki Lehvaslaiho
7             #
8             # Copyright Heikki Lehvaslaiho
9             #
10             # You may distribute this module under the same terms as perl itself
11              
12             # POD documentation - main docs before the code
13              
14             =head1 NAME
15              
16             Bio::Tools::Run::Phylo::Phyml - Wrapper for rapid reconstruction of phylogenies using Phyml
17              
18             =head1 SYNOPSIS
19              
20             use Bio::Tools::Run::Phylo::Phyml;
21              
22             # Make a Phyml factory
23             $factory = Bio::Tools::Run::Phylo::Phyml->new(-verbose => 2);
24             # it defaults to protein alignment
25             # change parameters
26             $factory->model('Dayhoff');
27             # Pass the factory an alignment and run
28             $inputfilename = 't/data/protpars.phy';
29             $tree = $factory->run($inputfilename); # $tree is a Bio::Tree::Tree object.
30              
31              
32             # or set parameters at object creation
33             my %args = (
34             -data_type => 'dna',
35             -model => 'HKY',
36             -kappa => 4,
37             -invar => 'e',
38             -category_number => 4,
39             -alpha => 'e',
40             -tree => 'BIONJ',
41             -opt_topology => '0',
42             -opt_lengths => '1',
43             );
44             $factory = Bio::Tools::Run::Phylo::Phyml->new(%args);
45             # if you need the output files do
46             $factory->save_tempfiles(1);
47             $factory->tempdir($workdir);
48              
49             # and get a Bio::Align::AlignI (SimpleAlign) object from somewhere
50             $tree = $factory->run($aln);
51              
52             =head1 DESCRIPTION
53              
54             This is a wrapper for running the phyml application by Stephane
55             Guindon and Olivier Gascuel. You can download it from:
56             http://atgc.lirmm.fr/phyml/
57              
58             =head2 Installing
59              
60             After downloading, you need to rename a the copy of the program that
61             runs under your operating system. I.e. C into C.
62              
63             You will need to help this Phyml wrapper to find the C program.
64             This can be done in (at least) three ways:
65              
66             =over
67              
68             =item 1.
69              
70             Make sure the Phyml executable is in your path. Copy it to, or create
71             a symbolic link from a directory that is in your path.
72              
73             =item 2.
74              
75             Define an environmental variable PHYMLDIR which is a
76             directory which contains the 'phyml' application: In bash:
77              
78             export PHYMLDIR=/home/username/phyml_v2.4.4/exe
79              
80             In csh/tcsh:
81              
82             setenv PHYMLDIR /home/username/phyml_v2.4.4/exe
83              
84             =item 3.
85              
86             Include a definition of an environmental variable PHYMLDIR in
87             every script that will use this Phyml wrapper module, e.g.:
88              
89             BEGIN { $ENV{PHYMLDIR} = '/home/username/phyml_v2.4.4/exe' }
90             use Bio::Tools::Run::Phylo::Phyml;
91              
92             =back
93              
94             =head2 Running
95              
96             This wrapper has been tested with PHYML v2.4.4 and v.3.0. It may work with
97             recent Phyml releases using a date format for the format, but the wrapper
98             hasn't been extensively tested in these cases, so for the moment only the
99             simpler numbered versions are supported.
100              
101             In its current state, the wrapper supports only input of one MSA and
102             output of one tree. It can easily be extended to support more advanced
103             capabilities of C.
104              
105             Two convienience methods have been added on top of the standard
106             BioPerl WrapperBase ones: stats() and tree_string(). You can call them
107             to after running the phyml program to retrieve into a string the statistics
108             and the tree in Newick format.
109              
110             =head1 FEEDBACK
111              
112             =head2 Mailing Lists
113              
114             User feedback is an integral part of the evolution of this and other
115             Bioperl modules. Send your comments and suggestions preferably to
116             the Bioperl mailing list. Your participation is much appreciated.
117              
118             bioperl-l@bioperl.org - General discussion
119             http://bioperl.org/wiki/Mailing_lists - About the mailing lists
120              
121             =head2 Support
122              
123             Please direct usage questions or support issues to the mailing list:
124              
125             I
126              
127             rather than to the module maintainer directly. Many experienced and
128             reponsive experts will be able look at the problem and quickly
129             address it. Please include a thorough description of the problem
130             with code and data examples if at all possible.
131              
132             =head2 Reporting Bugs
133              
134             Report bugs to the Bioperl bug tracking system to help us keep track
135             of the bugs and their resolution. Bug reports can be submitted via
136             the web:
137              
138             http://bugzilla.open-bio.org/
139              
140             =head1 AUTHOR - Heikki Lehvaslaiho
141              
142             heikki at bioperl dot org
143              
144             =head1 APPENDIX
145              
146             The rest of the documentation details each of the object methods.
147             Internal methods are usually preceded with a _
148              
149             =cut
150              
151             package Bio::Tools::Run::Phylo::Phyml;
152 1     1   104086 use strict;
  1         1  
  1         27  
153              
154 1     1   275 use Bio::AlignIO;
  1         86914  
  1         41  
155 1     1   12 use File::Copy;
  1         4  
  1         62  
156 1     1   6 use File::Spec;
  1         3  
  1         18  
157 1     1   271 use Bio::TreeIO;
  1         14109  
  1         30  
158              
159 1     1   7 use base qw(Bio::Tools::Run::WrapperBase);
  1         2  
  1         312  
160              
161             our $PROGRAM_NAME = 'phyml';
162             our $PROGRAM_DIR = $ENV{'PHYMLDIR'};
163              
164             # valid substitution model names
165             our $models;
166              
167             # DNA
168             map { $models->{0}->{$_} = 1 } qw(JC69 K2P F81 HKY F84 TN93 GTR);
169              
170             # protein
171             map { $models->{1}->{$_} = 1 } qw(JTT MtREV Dayhoff WAG);
172              
173             our $models3;
174              
175             # DNA
176             map { $models3->{'nt'}->{$_} = 1 } qw(HKY85 JC69 K80 F81 F84 TN93 GTR );
177              
178             # protein
179             map { $models3->{'aa'}->{$_} = 1 }
180             qw(LG WAG JTT MtREV Dayhoff DCMut RtREV CpREV VT Blosum62 MtMam MtArt HIVw HIVb );
181              
182             =head2 new
183              
184             Title : new
185             Usage : $factory = Bio::Tools::Run::Phylo::Phyml->new(@params)
186             Function: creates a new Phyml factory
187             Returns : Bio::Tools::Run::Phylo::Phyml
188             Args : Optionally, provide any of the following (default in []):
189             -data_type => 'dna' or 'protein', [protein]
190             -dataset_count => integer, [1]
191             -model => 'HKY'... , [HKY|JTT]
192             -kappa => 'e' or float, [e]
193             -invar => 'e' or float, [e]
194             -category_number => integer, [1]
195             -alpha => 'e' or float (int v3),[e]
196             -tree => 'BIONJ' or your own, [BION]
197             -bootstrap => integer [123]
198             -opt_topology => boolean [1]
199             -opt_lengths => boolean [1]
200             -no_memory_check => boolean [1]
201             -program_name => string
202              
203             =cut
204              
205             sub new {
206 1     1 1 514 my ( $class, @args ) = @_;
207 1         11 my $self = $class->SUPER::new(@args);
208              
209             # for consistency with other run modules, allow params to be dashless
210 1         50 my %args = @args;
211 1         6 while ( my ( $key, $val ) = each %args ) {
212 1 50       7 if ( $key !~ /^-/ ) {
213 0         0 delete $args{$key};
214 0         0 $args{ '-' . $key } = $val;
215             }
216             }
217              
218             my (
219 1         11 $data_type, $data_format, $dataset_count, $model,
220             $freq, $kappa, $invar, $category_number,
221             $alpha, $tree, $opt_topology, $opt_lengths,
222             $opt, $search, $rand_start, $rand_starts,
223             $rand_seed, $no_memory_check, $bootstrap, $program_name
224             )
225             = $self->_rearrange(
226             [
227             qw( DATA_TYPE
228             DATA_FORMAT
229             DATASET_COUNT
230             MODEL
231             FREQ
232             KAPPA
233             INVAR
234             CATEGORY_NUMBER
235             ALPHA
236             TREE
237             OPT_TOPOLOGY
238             OPT_LENGTHS
239             OPT
240             SEARCH
241             RAND_START
242             RAND_STARTS
243             RAND_SEED
244             NO_MEMORY_CHECK
245             BOOTSTRAP
246             PROGRAM_NAME
247             )
248             ],
249             %args
250             );
251              
252 1 50       40 $self->data_type($data_type) if $data_type;
253 1 50       5 $self->data_format($data_format) if $data_format;
254 1 50       7 $self->dataset_count($dataset_count) if $dataset_count;
255 1 50       4 $self->model($model) if $model;
256 1 50       4 $self->freq($freq) if $freq;
257 1 50       4 $self->kappa($kappa) if $kappa;
258 1 50       5 $self->invar($invar) if $invar;
259 1 50       4 $self->category_number($category_number) if $category_number;
260 1 50       4 $self->alpha($alpha) if $alpha;
261 1 50       4 $self->tree($tree) if $tree;
262 1 50       16 $self->opt_topology($opt_topology) if $opt_topology;
263 1 50       4 $self->opt_lengths($opt_lengths) if $opt_lengths;
264 1 50       6 $self->opt($opt) if $opt;
265 1 50       3 $self->search($search) if $search;
266 1 50       3 $self->rand_start($rand_start) if $rand_start;
267 1 50       2 $self->rand_starts($rand_starts) if $rand_starts;
268 1 50       2 $self->rand_seed($rand_seed) if $rand_seed;
269 1 50       2 $self->no_memory_check($no_memory_check) if $no_memory_check;
270 1 50       4 $self->bootstrap($bootstrap) if $bootstrap;
271 1 50       2 $self->program_name($program_name) if $program_name;
272              
273 1         4 return $self;
274             }
275              
276             =head2 program_name
277              
278             Title : program_name
279             Usage : $factory>program_name()
280             Function: holds the program name
281             Returns : string
282             Args : None
283              
284             =cut
285              
286             sub program_name {
287 7     7 1 12 my ( $self, $value ) = @_;
288              
289 7 50       14 if ( defined($value) ) {
290 0 0       0 if ( $value =~ /^$PROGRAM_NAME[-a-z]*$/ ) {
291 0         0 $PROGRAM_NAME = $value;
292             } else {
293 0         0 $self->throw("$value is not a valid program name");
294             }
295             }
296              
297 7         33 $PROGRAM_NAME;
298             }
299              
300             =head2 program_dir
301              
302             Title : program_dir
303             Usage : $factory->program_dir(@params)
304             Function: returns the program directory, obtained from ENV variable.
305             Returns : string
306             Args : None
307              
308             =cut
309              
310             sub program_dir {
311 4     4 1 336 return $PROGRAM_DIR;
312             }
313              
314             =head2 version
315              
316             Title : version
317             Usage : exit if $prog->version < 1.8
318             Function: Determine the version number of the program
319             Example :
320             Returns : float or undef
321             Args : none
322              
323             Phyml before 3.0 did not display the version. Assume 2.44 when can not
324             determine it.
325              
326             Some releases do not state version number, only date, so the
327             version might have to be inferred from this date.
328              
329             =cut
330              
331             sub version {
332 0     0 1 0 my $self = shift;
333              
334 0 0       0 return $self->{'_version'} if defined $self->{'_version'};
335 0   0     0 my $exe = $self->executable || return;
336 0         0 my $string = substr `$exe -h`, 0, 40;
337 0         0 my ($version) = $string =~ /PhyML v([\d+\.]+)/;
338 0 0       0 if ( !$version ) {
339 0         0 $string =~ /PhyML\s+(\d{8})/;
340              
341             # 3 was released August 2008
342 0 0 0     0 $version = 3 if ( $1 && $1 >= 20080801 );
343             }
344 0         0 $self->{'_version'} = $version;
345 0 0       0 $version ? ( return $version ) : return '2.44';
346             }
347              
348             =head2 run
349              
350             Title : run
351             Usage : $factory->run($aln_file);
352             $factory->run($align_object);
353             Function: Runs Phyml to generate a tree
354             Returns : Bio::Tree::Tree object
355             Args : file name for your input alignment in a format
356             recognised by AlignIO, OR Bio::Align::AlignI
357             compliant object (eg. Bio::SimpleAlign).
358              
359             =cut
360              
361             sub run {
362 0     0 1 0 my ( $self, $in ) = @_;
363              
364 0 0 0     0 if ( ref $in && $in->isa("Bio::Align::AlignI") ) {
    0          
    0          
365 0         0 $in = $self->_write_phylip_align_file($in);
366             }
367             elsif ( !-e $in ) {
368 0         0 $self->throw( "When not supplying a Bio::Align::AlignI object, "
369             . "you must supply a readable filename" );
370             }
371             elsif ( -e $in ) {
372 0         0 copy( $in, $self->tempdir );
373 0         0 my $name =
374             File::Spec->splitpath($in); # name is the last item in the array
375 0         0 $in = File::Spec->catfile( $self->tempdir, $name );
376             }
377              
378 0         0 return $self->_run($in);
379             }
380              
381             =head2 stats
382              
383             Title : stats
384             Usage : $factory->stats;
385             Function: Returns the contents of the phyml '_phyml_stat.txt' output file
386             Returns : string with statistics about the run, undef before run()
387             Args : none
388              
389             =cut
390              
391             sub stats {
392 0     0 1 0 my $self = shift;
393 0         0 return $self->{_stats};
394             }
395              
396             =head2 tree_string
397              
398             Title : tree_string
399             Usage : $factory->tree_string;
400             $factory->run($align_object);
401             Function: Returns the contents of the phyml '_phyml_tree.txt' output file
402             Returns : string with tree in Newick format, undef before run()
403             Args : none
404              
405             =cut
406              
407             sub tree_string {
408 0     0 1 0 my $self = shift;
409 0         0 return $self->{_tree};
410             }
411              
412             =head2 Getsetters
413              
414             These methods are used to set and get program parameters before running.
415              
416             =head2 data_type
417              
418             Title : data_type
419             Usage : $phyml->data_type('nt');
420             Function: Sets sequence alphabet to 'dna' (nt in v3) or 'aa'
421             If leaved unset, will be set automatically
422             Returns : set value, defaults to 'protein'
423             Args : None to get, 'dna' ('nt') or 'aa' to set.
424              
425             =cut
426              
427             sub data_type {
428 0     0 1 0 my ( $self, $value ) = @_;
429 0 0 0     0 if ( $self->version && $self->version >= 3 ) {
430 0 0       0 if ( defined $value ) {
431 0 0       0 if ( $value eq 'nt' ) {
432 0         0 $self->{_data_type} = 'nt';
433             }
434             else {
435 0         0 $self->{_data_type} = 'aa';
436             }
437             }
438 0 0       0 return 'aa' unless defined $self->{_data_type};
439             }
440             else {
441 0 0       0 if ( defined $value ) {
442 0 0       0 if ( $value eq 'dna' ) {
443 0         0 $self->{_data_type} = '0';
444             }
445             else {
446 0         0 $self->{_data_type} = '1';
447             }
448             }
449 0 0       0 return '1' unless defined $self->{_data_type};
450             }
451 0         0 return $self->{_data_type};
452             }
453              
454             =head2 data_format
455              
456             Title : data_format
457             Usage : $phyml->data_format('s');
458             Function: Sets PHYLIP format to 'i' interleaved or
459             's' sequential
460             Returns : set value, defaults to 'i'
461             Args : None to get, 'i' or 's' to set.
462              
463             =cut
464              
465             sub data_format {
466 3     3 1 8 my ( $self, $value ) = @_;
467 3 100       8 if ( defined $value ) {
468 2 50 66     7 $self->throw("PHYLIP format must be 'i' or 's'")
469             unless $value eq 'i'
470             or $value eq 's';
471 2         5 $self->{_data_format} = $value;
472             }
473 3   100     15 return $self->{_data_format} || 'i';
474             }
475              
476             =head2 dataset_count
477              
478             Title : dataset_count
479             Usage : $phyml->dataset_count(3);
480             Function: Sets dataset number to deal with
481             Returns : set value, defaults to 1
482             Args : None to get, positive integer to set.
483              
484             =cut
485              
486             sub dataset_count {
487 2     2 1 5 my ( $self, $value ) = @_;
488 2 100       7 if ( defined $value ) {
489 1 50 33     9 die "Invalid positive integer [$value]"
490             unless $value =~ /^[-+]?\d*$/ and $value > 0;
491 1         3 $self->{_dataset_count} = $value;
492             }
493 2   100     12 return $self->{_dataset_count} || 1;
494             }
495              
496             =head2 model
497              
498             Title : model
499             Usage : $phyml->model('HKY');
500             Function: Choose the substitution model to use. One of
501              
502             JC69 | K2P | F81 | HKY | F84 | TN93 | GTR (DNA)
503             JTT | MtREV | Dayhoff | WAG (amino acids)
504              
505             v3.0:
506             HKY85 (default) | JC69 | K80 | F81 | F84 |
507             TN93 | GTR (DNA)
508             LG (default) | WAG | JTT | MtREV | Dayhoff | DCMut |
509             RtREV | CpREV | VT | Blosum62 | MtMam | MtArt |
510             HIVw | HIVb (amino acids)
511              
512             Returns : Name of the model, v2.4.4 defaults to {HKY|JTT}
513             Args : None to get, string to set.
514              
515             =cut
516              
517             sub model {
518 0     0 1 0 my ( $self, $value ) = @_;
519 0 0       0 if ( defined($value) ) {
520 0 0 0     0 if ( $self->version && $self->version >= 3 ) {
521 0 0       0 unless ( $value =~ /\d{6}/ ) {
522             $self->throw(
523             "Not a valid model name [$value] for current data type (alphabet)"
524 0 0       0 ) unless $models3->{ $self->data_type }->{$value};
525             }
526             }
527             else {
528             $self->throw(
529             "Not a valid model name [$value] for current data type (alphabet)"
530 0 0       0 ) unless $models->{ $self->data_type }->{$value};
531             }
532 0         0 $self->{_model} = $value;
533             }
534              
535 0 0       0 if ( $self->{_model} ) {
536 0         0 return $self->{_model};
537             }
538              
539 0 0 0     0 if ( $self->version && $self->version >= 3 ) {
540 0 0       0 if ( $self->data_type eq 'aa' ) {
541 0         0 return 'LG'; # protein
542             }
543             else {
544 0         0 return 'HKY85'; # DNA
545             }
546             }
547             else {
548 0 0       0 if ( $self->data_type ) {
549 0         0 return 'JTT'; # protein
550             }
551             else {
552 0         0 return 'HKY'; # DNA
553             }
554             }
555             }
556              
557             =head2 kappa
558              
559             Title : kappa
560             Usage : $phyml->kappa(4);
561             Function: Sets transition/transversion ratio, leave unset to estimate
562             Returns : set value, defaults to 'e'
563             Args : None to get, float or integer to set.
564              
565             =cut
566              
567             sub kappa {
568 2     2 1 5 my ( $self, $value ) = @_;
569 2 100       6 if ( defined $value ) {
570 1 50 33     9 die "Invalid number [$value]"
571             unless $value =~ /^[-+]?\d*\.?\d*$/
572             or $value eq 'e';
573 1         3 $self->{_kappa} = $value;
574             }
575 2 100       7 return 'e' unless defined $self->{_kappa};
576 1 50       3 return 'e' if $self->{_kappa} eq 'e';
577 1         12 return sprintf( "%.1f", $self->{_kappa} );
578             }
579              
580             =head2 invar
581              
582             Title : invar
583             Usage : $phyml->invar(.3);
584             Function: Sets proportion of invariable sites, leave unset to estimate
585             Returns : set value, defaults to 'e'
586             Args : None to get, float or integer to set.
587              
588             =cut
589              
590             sub invar {
591 2     2 1 5 my ( $self, $value ) = @_;
592 2 100       5 if ( defined $value ) {
593 1 50 33     13 die "Invalid number [$value]"
594             unless $value =~ /^[-+]?\d*\.\d*$/
595             or $value eq 'e';
596 1         4 $self->{_invar} = $value;
597             }
598 2 100       8 return 'e' unless defined $self->{_invar};
599 1 50       4 return 'e' if $self->{_invar} eq 'e';
600 1         8 return sprintf( "%.1f", $self->{_invar} );
601             }
602              
603             =head2 category_number
604              
605             Title : category_number
606             Usage : $phyml->category_number(4);
607             Function: Sets number of relative substitution rate categories
608             Returns : set value, defaults to 1
609             Args : None to get, integer to set.
610              
611             =cut
612              
613             sub category_number {
614 2     2 1 5 my ( $self, $value ) = @_;
615 2 100       7 if ( defined $value ) {
616 1 50 33     10 die "Invalid postive integer [$value]"
617             unless $value =~ /^[+]?\d*$/ and $value > 0;
618 1         3 $self->{_category_number} = $value;
619             }
620 2   100     11 return $self->{_category_number} || 1;
621             }
622              
623             =head2 alpha
624              
625             Title : alpha
626             Usage : $phyml->alpha(1.0);
627             Function: Sets gamma distribution parameter, leave unset to estimate
628             Returns : set value, defaults to 'e'
629             Args : None to get, float or integer to set.
630              
631             =cut
632              
633             sub alpha {
634 3     3 1 7 my ( $self, $value ) = @_;
635 3 100       8 if ( defined $value ) {
636 2 50 66     20 die "Invalid number [$value]"
637             unless $value =~ /^[-+]?\d*\.?\d*$/
638             or $value eq 'e';
639 2         5 $self->{_alpha} = $value;
640             }
641 3 100       8 return 'e' unless defined $self->{_alpha};
642 2 100       9 return 'e' if $self->{_alpha} eq 'e';
643 1   50     9 return sprintf( "%.1f", $self->{_alpha} ) || 'e';
644             }
645              
646             =head2 tree
647              
648             Title : tree
649             Usage : $phyml->tree('/tmp/tree.nwk');
650             Function: Sets starting tree, leave unset to estimate a distance tree
651             Returns : set value, defaults to 'BIONJ'
652             Args : None to get, newick tree file name to set.
653              
654             =cut
655              
656             sub tree {
657 3     3 1 23 my ( $self, $value ) = @_;
658 3 100       8 if ( defined $value ) {
659 2 50 66     63 die "Invalid number [$value]"
660             unless -e $value or $value eq 'BIONJ';
661 2         6 $self->{_tree} = $value;
662             }
663 3   100     21 return $self->{_tree} || 'BIONJ';
664             }
665              
666             =head2 v2 options
667              
668             These methods can be used with PhyML v2* only.
669              
670             =head2 opt_topology
671              
672             Title : opt_topology
673             Usage : $factory->opt_topology(1);
674             Function: Choose to optimise the tree topology
675             Returns : 1 or 0. Default is 1.
676             Args : None to get, boolean to set.
677              
678             v2.* only
679              
680             =cut
681              
682             sub opt_topology {
683 0     0 1   my ( $self, $value ) = @_;
684 0 0 0       $self->throw("Not a valid parameter [opt_topology] for to PhyML v3")
685             if $self->version && $self->version >= 3;
686 0 0         if ( defined($value) ) {
687 0 0         if ($value) {
688 0           $self->{_opt_topology} = 1;
689             }
690             else {
691 0           $self->{_opt_topology} = 0;
692             }
693             }
694 0   0       return $self->{_opt_topology} || 1;
695             }
696              
697             =head2 opt_lengths
698              
699             Title : opt_lengths
700             Usage : $factory->opt_lengths(0);
701             Function: Choose to optimise branch lengths and rate parameters
702             Returns : 1 or 0. Default is 1.
703             Args : None to get, boolean to set.
704              
705              
706             v2.* only
707              
708             =cut
709              
710             sub opt_lengths {
711 0     0 1   my ( $self, $value ) = @_;
712 0 0 0       $self->throw("Not a valid parameter [opt_lengths] for PhyML v3")
713             if $self->version && $self->version >= 3;
714 0 0         if ( defined($value) ) {
715 0 0         if ($value) {
716 0           $self->{_opt_lengths} = 1;
717             }
718             else {
719 0           $self->{_opt_lengths} = 0;
720             }
721             }
722 0   0       return $self->{_opt_lengths} || 1;
723             }
724              
725             =head2 v3 options
726              
727             These methods can be used with PhyML v3* only.
728              
729             =head2 freq
730              
731             Title : freq
732             Usage : $phyml->freq(e); $phyml->freq("0.2, 0.6, 0.6, 0.2");
733             Function: Sets nucleotide frequences or asks residue to be estimated
734             according to two models: e or d
735             Returns : set value,
736             Args : None to get, string to set.
737              
738             v3 only.
739              
740             =cut
741              
742             sub freq {
743 0     0 1   my ( $self, $value ) = @_;
744 0 0         $self->throw("Not a valid parameter [freq] prior to PhyML v3")
745             if $self->version < 3;
746 0 0         if ( defined $value ) {
747 0 0 0       die "Invalid value [$value]"
      0        
748             unless $value =~ /^[\d\. ]$/
749             or $value eq 'e'
750             or $value eq 'd';
751 0           $self->{_freq} = $value;
752             }
753 0           return $self->{_freq};
754             }
755              
756             =head2 opt
757              
758             Title : opt
759             Usage : $factory->opt(1);
760             Function: Optimise tree parameters: tlr|tl|tr|l|n
761             Returns : {value|n} (default n)
762             Args : None to get, string to set.
763              
764             v3.* only
765              
766             =cut
767              
768             sub opt {
769 0     0 1   my ( $self, $value ) = @_;
770 0 0         $self->throw("Not a valid parameter [opt] prior to PhyML v3")
771             if $self->version < 3;
772 0 0         if ( defined($value) ) {
773 0 0         $self->{_opt} = $value if $value =~ /tlr|tl|tr|l|n/;
774             }
775 0   0       return $self->{_opt} || 'n';
776             }
777              
778             =head2 search
779              
780             Title : search
781             Usage : $factory->search(SPR);
782             Function: Tree topology search operation algorithm: NNI|SPR|BEST
783             Returns : string (defaults to NNI)
784             Args : None to get, string to set.
785              
786             v3.* only
787              
788             =cut
789              
790             sub search {
791 0     0 1   my ( $self, $value ) = @_;
792 0 0         $self->throw("Not a valid parameter [search] prior to PhyML v3")
793             if $self->version < 3;
794 0 0         if ( defined($value) ) {
795 0 0         $self->{_search} = $value if $value =~ /NNI|SPR|BEST/;
796             }
797 0   0       return $self->{_search} || 'NNI';
798             }
799              
800             =head2 rand_start
801              
802             Title : rand_start
803             Usage : $factory->rand_start(1);
804             Function: Sets the initial SPR tree to random.
805             Returns : boolean (defaults to false)
806             Args : None to get, boolean to set.
807              
808             v3.* only; only meaningful if $prog-Esearch is 'SPR'
809              
810             =cut
811              
812             sub rand_start {
813 0     0 1   my ( $self, $value ) = @_;
814 0 0         $self->throw("Not a valid parameter [rand_start] prior to PhyML v3")
815             if $self->version < 3;
816 0 0         if ( defined($value) ) {
817 0 0         if ($value) {
818 0           $self->{_rand_start} = 1;
819             }
820             else {
821 0           $self->{_rand_start} = 0;
822             }
823             }
824 0           return $self->{_rand_start};
825             }
826              
827             =head2 rand_starts
828              
829             Title : rand_starts
830             Usage : $factory->rand_starts(10);
831             Function: Sets the number of initial random SPR trees
832             Returns : integer (defaults to 1)
833             Args : None to get, integer to set.
834              
835             v3.* only; only valid if $prog-Esearch is 'SPR'
836              
837             =cut
838              
839             sub rand_starts {
840 0     0 1   my ( $self, $value ) = @_;
841 0 0         $self->throw("Not a valid parameter [rand_starts] prior to PhyML v3")
842             if $self->version < 3;
843 0 0         if ( defined $value ) {
844 0 0         die "Invalid number [$value]"
845             unless $value =~ /^[-+]?\d+$/;
846 0           $self->{_rand_starts} = $value;
847             }
848 0   0       return $self->{_rand_starts} || 1;
849             }
850              
851             =head2 rand_seed
852              
853             Title : rand_seed
854             Usage : $factory->rand_seed(1769876);
855             Function: Seeds the random number generator
856             Returns : random integer
857             Args : None to get, integer to set.
858              
859             v3.* only; only valid if $prog-Esearch is 'SPR'
860              
861             Uses perl rand() to initialize if not explicitely set.
862              
863             =cut
864              
865             sub rand_seed {
866 0     0 1   my ( $self, $value ) = @_;
867 0 0         $self->throw("Not a valid parameter [rand_seed] prior to PhyML v3")
868             if $self->version < 3;
869 0 0         if ( defined $value ) {
870 0 0         die "Invalid number [$value]"
871             unless $value =~ /^[-+]?\d+$/;
872 0           $self->{_rand_seed} = $value;
873             }
874 0   0       return $self->{_rand_seed} || int rand 1000000;
875             }
876              
877             =head2 no_memory_check
878              
879             Title : no_memory_check
880             Usage : $factory->no_memory_check(1);
881             Function:
882             Returns : boolean (defaults to false)
883             Args : None to get, integer to set.
884              
885             =cut
886              
887             sub no_memory_check {
888 0     0 1   my ( $self, $value ) = @_;
889 0 0         $self->throw("Not a valid parameter [no_memory_check] prior to PhyML v3")
890             if $self->version < 3;
891 0 0         if ( defined($value) ) {
892 0 0         if ($value) {
893 0           $self->{_no_memory_check} = 1;
894             }
895             else {
896 0           $self->{_no_memory_check} = 0;
897             }
898             }
899 0   0       return $self->{_no_memory_check} || 0;
900             }
901              
902             =head2 bootstrap
903              
904             Title : bootstrap
905             Usage : $factory->bootstrap(100);
906             Function: Set number of bootstraps
907             Returns :
908             Args : None to get, integer to set.
909              
910             =cut
911              
912             sub bootstrap {
913 0     0     my ( $self, $value ) = @_;
914 0 0         $self->throw("Not a valid parameter [bootstrap] prior to PhyML v3")
915             if $self->version < 3;
916 0 0         if ( defined $value ) {
917 0 0         die "Invalid number [$value]" unless $value =~ /^\d+$/;
918 0           $self->{_bootstrap} = $value;
919             }
920 0           return $self->{_bootstrap};
921             }
922              
923             =head2 command
924              
925             Title : command
926             Usage : $factory->command(...);
927             Function:
928             Returns : string
929             Args : None to get, integer to set.
930              
931             =cut
932              
933             sub command {
934 0     0 1   my ( $self, $value ) = @_;
935 0 0         if ( defined($value) ) {
936 0 0         if ($value =~ /$PROGRAM_NAME/ ) {
937 0           $self->{_command} = $value;
938             }
939             else {
940 0           $self->throw("$value is not a $PROGRAM_NAME command");
941             }
942             }
943 0   0       return $self->{_command} || '';
944             }
945              
946             =head2 Internal methods
947              
948             These methods are private and should not be called outside this class.
949              
950             =cut
951              
952             sub _run {
953 0     0     my ( $self, $file ) = @_;
954              
955 0   0       my $exe = $self->executable || return;
956              
957 0           my $command;
958             my $output_stat_file;
959 0 0         if ( $self->version >= 3 ) {
960 0           $command = $exe . " -i $file" . $self->_setparams;
961 0           $output_stat_file = '_phyml_stats.txt';
962             }
963             else {
964 0           $command = $exe . " $file " . $self->arguments . $self->_setparams;
965 0           $output_stat_file = '_phyml_stat.txt';
966             }
967              
968 0           $self->command($command);
969 0           $self->debug("Phyml command = $command\n");
970 0           `$command`;
971              
972             # stats
973             {
974 0           my $stat_file = $file . $output_stat_file;
  0            
975 0 0         open( my $FH_STAT, "<", $stat_file )
976             || $self->throw(
977             "Phyml call ($command) did not give an output [$stat_file]: $?");
978 0           local $/;
979 0           $self->{_stats} .= <$FH_STAT>;
980             }
981              
982             #print $self->{stats};
983              
984             # tree
985 0           my $tree_file = $file . '_phyml_tree.txt';
986             {
987 0 0         open( my $FH_TREE, "<", $tree_file )
  0            
988             || $self->throw("Phyml call ($command) did not give an output: $?");
989 0           local $/;
990 0           $self->{_tree} .= <$FH_TREE>;
991             }
992              
993 0 0         open( my $FH_TREE, "<", $tree_file )
994             || $self->throw("Phyml call ($command) did not give an output: $?");
995              
996 0           my $treeio = Bio::TreeIO->new( -format => 'nhx', -fh => $FH_TREE );
997 0           my $tree = $treeio->next_tree;
998              
999             # could be faster to parse the tree only if needed?
1000              
1001 0           return $tree;
1002             }
1003              
1004             =head2 _setparams
1005              
1006             Title : _setparams
1007             Usage : Internal function, not to be called directly
1008             Function: Creates a string of params to be used in the command string
1009             Returns : string of params
1010             Args : none
1011              
1012             =cut
1013              
1014             sub _setparams {
1015 0     0     my $self = shift;
1016 0           my $param_string;
1017              
1018 0 0         if ( $self->version >= 3 ) {
1019              
1020             # version 3 or higher
1021 0           $param_string = ' -d ' . $self->data_type;
1022              
1023 0 0         $param_string .= ' -q ' if $self->data_format eq 's';
1024 0 0         $param_string .= ' -n ' . $self->dataset_count
1025             if $self->dataset_count > 1;
1026 0 0         $param_string .= ' -b ' . $self->bootstrap if $self->bootstrap;
1027              
1028             # $param_string .= ' 0'; # no bootstrap sets
1029 0           $param_string .= ' -m ' . $self->model;
1030 0 0         $param_string .= ' -f ' . $self->freq if $self->freq;
1031              
1032 0 0         if ( $self->data_type eq 'dna' ) {
1033 0           $param_string .= ' -t ' . $self->kappa;
1034             }
1035              
1036 0           $param_string .= ' -v ' . $self->invar;
1037 0           $param_string .= ' -c ' . $self->category_number;
1038 0           $param_string .= ' -a ' . $self->alpha;
1039 0 0         $param_string .= ' -u ' . $self->tree if $self->tree ne 'BIONJ';
1040 0 0         $param_string .= ' -o ' . $self->opt if $self->opt;
1041 0           $param_string .= ' -s ' . $self->search;
1042              
1043 0 0         if ( $self->search eq 'SPR' ) {
1044 0 0         $param_string .= ' --rand_start ' if $self->rand_start;
1045 0 0         $param_string .= ' --n_rand_starts ' . $self->rand_starts
1046             if $self->rand_starts;
1047 0           $param_string .= ' --r_seed ' . $self->rand_seed;
1048             }
1049              
1050 0 0         $param_string .= ' --no_memory_check '
1051             if $self->no_memory_check;
1052              
1053             }
1054             else {
1055              
1056             # version 2
1057 0           $param_string = ' ' . $self->data_type;
1058 0           $param_string .= ' ' . $self->data_format;
1059 0           $param_string .= ' ' . $self->dataset_count;
1060 0           $param_string .= ' 0'; # no bootstrap sets
1061 0           $param_string .= ' ' . $self->model;
1062              
1063 0 0         unless ( $self->data_type ) { # only for DNA
1064 0           $param_string .= ' ' . $self->kappa;
1065             }
1066              
1067 0           $param_string .= ' ' . $self->invar;
1068 0           $param_string .= ' ' . $self->category_number;
1069 0           $param_string .= ' ' . $self->alpha;
1070 0           $param_string .= ' ' . $self->tree;
1071 0           $param_string .= ' ' . $self->opt_topology;
1072 0           $param_string .= ' ' . $self->opt_lengths;
1073              
1074             }
1075 0           return $param_string;
1076             }
1077              
1078             =head2 _write_phylip_align_file
1079              
1080             Title : _write_phylip_align_file
1081             Usage : obj->__write_phylip_align_file($aln)
1082             Function: Internal (not to be used directly)
1083              
1084             Writes the alignment into the tmp directory
1085             in PHYLIP interlieved format
1086              
1087             Returns : filename
1088             Args : Bio::Align::AlignI
1089              
1090             =cut
1091              
1092             sub _write_phylip_align_file {
1093 0     0     my ( $self, $align ) = @_;
1094              
1095 0           my $tempfile = File::Spec->catfile( $self->tempdir, "aln$$.phylip" );
1096 0           $self->data_format('i');
1097 0           my $out = Bio::AlignIO->new(
1098             -file => ">$tempfile",
1099             -format => 'phylip',
1100             -interleaved => 1,
1101             -longid => 1
1102             );
1103 0           $out->write_aln($align);
1104 0           $out->close();
1105 0           $out = undef;
1106 0           return $tempfile;
1107             }
1108              
1109             1;