Tools/Run/Phylo/Phylip/DrawTree.pm

filter: Statementstmt 36.0% 48 / 133 Branchbran 23.3% 14 / 60 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 10 / 15 Podpod 100% 5 / 5 totaltotal 34.8% 77 / 221 CC 36 SCAR 37.5
CC
36
Cov
31%
_setparams
110.0
_run
90.0
_setinput
20.0
CRAP 42.6
« Tools/Run/Phylo/Phylip/SeqBoot.pm ↑ Summary Tools/Run/Alignment/Sim4.pm »
1 # $Id$
2 #
3 # BioPerl module for Bio::Tools::Run::Phylo::Phylip::DrawTree
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Jason Stajich <jason-AT-bioperl_DOT_org>
8 #
9 # Copyright Jason Stajich
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::Phylip::DrawTree - use Phylip DrawTree
18 program to draw trees
19  
20 =head1 SYNOPSIS
21  
22 use Bio::Tools::Run::Phylo::Phylip::DrawTree;
23  
24 my $treedraw = Bio::Tools::Run::Phylo::Phylip::DrawTree->new();
25 my $treeimagefile = $drawfact->run($tree);
26  
27 =head1 DESCRIPTION
28  
29 This is a module for automating drawing of trees through Joe
30 Felsenstein's Phylip suite.
31  
32 To set parameters with option you need to pass in an array reference or
33 a string, depending on the parameter. For example:
34  
35 $treedraw->HORIZMARGINS(['2.00','2.5']);
36 $treedraw->ANCESTRALNODES('C');
37 $treedraw->TREESTYLE('PHEN');
38 $treedraw->USEBRANCHLENS('N');
39  
40 This can be a brittle module as the menus change in PHYLIP. It should
41 support phylip 3.6 but no guarantees.
42  
43 =head1 FEEDBACK
44  
45 =head2 Mailing Lists
46  
47 User feedback is an integral part of the evolution of this and other
48 Bioperl modules. Send your comments and suggestions preferably to the
49 Bioperl mailing list. Your participation is much appreciated.
50  
51 bioperl-l@bioperl.org - General discussion
52 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53  
54 =head2 Support
55  
56 Please direct usage questions or support issues to the mailing list:
57  
58 I<bioperl-l@bioperl.org>
59  
60 rather than to the module maintainer directly. Many experienced and
61 reponsive experts will be able look at the problem and quickly
62 address it. Please include a thorough description of the problem
63 with code and data examples if at all possible.
64  
65 =head2 Reporting Bugs
66  
67 Report bugs to the Bioperl bug tracking system to help us keep track
68 of the bugs and their resolution. Bug reports can be submitted via the
69 web:
70  
71 http://redmine.open-bio.org/projects/bioperl/
72  
73 =head1 AUTHOR - Jason Stajich
74  
75 Email jason-at-bioperl.org
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the object methods.
80 Internal methods are usually preceded with a _
81  
82 =cut
83  
84  
85 # Let the code begin...
86  
87  
88 package Bio::Tools::Run::Phylo::Phylip::DrawTree;
89 1 ▶use vars qw($AUTOLOAD @ISA $PROGRAM $PROGRAMDIR $PROGRAMNAME
Subroutine BEGIN: called CC=1 SCAR=0.0
90 $FONTFILE @DRAW_PARAMS @OTHER_SWITCHES
91 1 %OK_FIELD %DEFAULT);
92 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 # inherit from Phylip::Base which has some methods for dealing with
94 # Phylip specifics
95 1 ▶use Bio::Tools::Run::Phylo::Phylip::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 1 @ISA = qw( Bio::Tools::Run::Phylo::Phylip::Base );
99  
100 1 ▶use Bio::Tools::Run::Phylo::Phylip::PhylipConf qw(%Menu);
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 # You will need to enable the neighbor program. This
103 # can be done in (at least) 3 ways:
104 #
105 # 1. define an environmental variable PHYLIPDIR:
106 # export PHYLIPDIR=/home/shawnh/PHYLIP/bin
107 #
108 # 2. include a definition of an environmental variable PHYLIPDIR in
109 # every script that will use DrawTree.pm.
110 # $ENV{PHYLIPDIR} = '/home/shawnh/PHYLIP/bin';
111 #
112 # 3. You can set the path to the program through doing:
113 # my @params('program'=>'/usr/local/bin/drawgram');
114 # my $neighbor_factory = Bio::Tools::Run::Phylo::Phylip::DrawTree->new(@params)
115  
116 1 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=22.3
117 1 %DEFAULT = ('PLOTTER' => 'P',
118 'SCREEN' => 'N');
119 1 ▶ $DEFAULT{'FONTFILE'} = Bio::Root::IO->catfile($ENV{'PHYLIPDIR'},"font1") if $ENV{'PHYLIPDIR'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PHYLIPDIR"} 0 1 1
120  
121 1 $PROGRAMNAME="drawtree";
122 1 ▶ if (defined $ENV{'PHYLIPDIR'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{'PHYLIPDIR'}) { } 0 1 1
123 0 ▶ $PROGRAMDIR = $ENV{'PHYLIPDIR'} || '';
Condition0%0/2
$ENV{'PHYLIPDIR'} || ''
l!l
00
MC/DC0%0/1
$ENV{'PHYLIPDIR'} || ''
$ENV{'PHYLIPDIR'}
Truth table0%0/2
A
A= $ENV{'PHYLIPDIR'}
Aresult
11
00
124 0 ▶ $PROGRAM = Bio::Root::IO->catfile($PROGRAMDIR,
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin/i ? : 0 0 0
125 $PROGRAMNAME.($^O =~ /mswin/i ?'.exe':''));
126 0 $DEFAULT{'FONTFILE'} = Bio::Root::IO->catfile($ENV{'PHYLIPDIR'},"font1");
127 }
128 else {
129 1 $PROGRAM = $PROGRAMNAME;
130 }
131  
132 1 @DRAW_PARAMS = qw(PLOTTER SCREEN LABEL_ANGLE ROTATION TREEARC
133 ITERATE SCALE HORIZMARGINS VERTICALMARGINS FONT
134 );
135 1 @OTHER_SWITCHES = qw(QUIET);
136 1 foreach my $attr(@DRAW_PARAMS,@OTHER_SWITCHES) {
137 11 $OK_FIELD{$attr}++;
138 }
139 }
140  
141 =head2 program_name
142  
143 Title : program_name
144 Usage : $obj->program_name()
145 Function: holds the program name
146 Returns: string
147 Args : None
148  
149 =cut
150  
151 6 ▶sub program_name {
Subroutine program_name: called CC=1 SCAR=0.0
Pod documented
152 6 return $PROGRAMNAME;
153 }
154  
155 =head2 program_dir
156  
157 Title : program_dir
158 Usage : $drawfact->program_dir()
159 Function: returns the program directory, obtained from ENV variable.
160 Returns : string
161 Args :
162  
163 =cut
164  
165 3 ▶sub program_dir {
Subroutine program_dir: called CC=2 SCAR=7.6
Pod documented
166 3 ▶ return Bio::Root::IO->catfile($ENV{PHYLIPDIR}) if $ENV{PHYLIPDIR};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PHYLIPDIR"} 0 3 3
167 }
168  
169 =head2 new
170  
171 Title : new
172 Usage : my $obj = Bio::Tools::Run::Phylo::Phylip::DrawTree->new();
173 Function: Builds a new Bio::Tools::Run::Phylo::Phylip::DrawTree object
174 Returns : an instance of Bio::Tools::Run::Phylo::Phylip::DrawTree
175 Args : The available DrawGram parameters
176  
177  
178 =cut
179  
180 1 ▶sub new {
Subroutine new: called CC=7 SCAR=20.2
Pod documented
181 1 my($class,@args) = @_;
182  
183 1 my $self = $class->SUPER::new(@args);
184
185 1 my ($attr, $value);
186
187 1 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 2 1 3
188 2 $attr = shift @args;
189 2 $value = shift @args;
190 2 ▶ next if( $attr =~ /^-/ ); # don't want named parameters
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^-/ 1 1 2
191 1 ▶ if ($attr =~/PROGRAM/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /PROGRAM/i) 0 1 1
192 0 $self->executable($value);
193 0 next;
194 }
195 1 $self->$attr($value);
196 }
197 1 ▶ $self->plotter($DEFAULT{'PLOTTER'}) unless $self->plotter;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->plotter 1 0 1
198 1 ▶ $self->screen($DEFAULT{'SCREEN'}) unless $self->screen;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->screen 1 0 1
199 1 ▶ $self->fontfile($DEFAULT{'FONTFILE'}) unless $self->fontfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fontfile 1 0 1
200 1 return $self;
201 }
202  
203  
204 4 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
205 4 my $self = shift;
206 4 my $attr = $AUTOLOAD;
207 4 $attr =~ s/.*:://;
208 4 $attr = uc $attr;
209 4 ▶ $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $Bio::Tools::Run::Phylo::Phylip::DrawTree::OK_FIELD{$attr} 0 4 4
210 4 ▶ $self->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 2 4
211 4 return $self->{$attr};
212 }
213  
214 =head2 run
215  
216 Title : run
217 Usage : my $file = $app->run($treefile);
218 Function: Draw a tree
219 Returns : File containing the rendered tree
220 Args : either a Bio::Tree::TreeI
221 OR
222 filename of a tree in newick format
223  
224 =cut
225  
226 0 ▶sub run{
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
227 0 my ($self,$input) = @_;
228
229 # Create input file pointer
230 0 my ($infilename) = $self->_setinput($input);
231 0 ▶ if (!$infilename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($infilename) 0 0 0
232 0 $self->throw("Problems setting up for drawgram. Probably bad input data in $input !");
233 }
234  
235 # Create parameter string to pass to neighbor program
236 0 my $param_string = $self->_setparams();
237  
238 # run drawgram
239 0 my $plotfile = $self->_run($infilename,$param_string);
240 0 return $plotfile;
241 }
242  
243 =head2 draw_tree
244  
245 Title : draw_tree
246 Usage : my $file = $app->draw_tree($treefile);
247 Function: This method is deprecated. Please use run method.
248 Returns : File containing the rendered tree
249 Args : either a Bio::Tree::TreeI
250 OR
251 filename of a tree in newick format
252  
253 =cut
254  
255 0 ▶sub draw_tree{
Subroutine draw_tree: not called CC=1 SCAR=6.9
Pod documented
256 0 return shift->run(@_);
257 }
258  
259 =head2 _run
260  
261 Title : _run
262 Usage : Internal function, not to be called directly
263 Function: makes actual system call to drawgram program
264 Example :
265 Returns : Bio::Tree object
266 Args : Name of a file the tree to draw in newick format
267 and a parameter string to be passed to drawgram
268  
269  
270 =cut
271  
272 0 ▶sub _run {
Subroutine _run: not called CC=9 SCAR=45.0
273 0 my ($self,$infile,$param_string) = @_;
274 0 my $instring;
275 0 my $curpath = cwd;
276
277 0 ▶ unless( File::Spec->file_name_is_absolute($infile) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($infile)) 0 0 0
278 0 $infile = $self->io->catfile($curpath,$infile);
279 }
280 0 $instring = $infile . "\n";
281 0 ▶ if( ! defined $self->fontfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fontfile) 0 0 0
282 0 $self->throw("You must have defined a fontfile");
283 }
284  
285 0 ▶ if( -e $self->io->catfile($curpath,'fontfile') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $self->io->catfile($curpath, 'fontfile')) { } 0 0 0
elsif ('File::Spec'->file_name_is_absolute($self->fontfile)) { } 0 0 0
286 0 $instring .= $self->io->catfile($curpath,'fontfile')."\n";
287 } elsif( File::Spec->file_name_is_absolute($self->fontfile) ) {
288 #$instring .= $self->io->catfile($self->tempdir,$self->fontfile)."\n";
289 0 $instring .= $self->io->catfile($self->fontfile)."\n";
290 } else {
291 0 $instring .= $self->io->catfile($curpath,$self->fontfile)."\n";
292 }
293 0 chdir($self->tempdir);
294 0 $instring .= $param_string;
295 0 $self->debug( "Program ".$self->executable." $param_string\n");
296 # open a pipe to run drawgram to bypass interactive menus
297 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
298 0 ▶ my $null = ($^O =~ m/mswin/i) ? 'NUL' : '/dev/null';
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin/i ? : 0 0 0
299 0 open(DRAW,"|".$self->executable.">$null");
300 }
301 else {
302 0 open(DRAW,"|".$self->executable);
303 }
304 0 print DRAW $instring;
305 0 close(DRAW);
306 0 chdir($curpath);
307 #get the results
308 0 my $plotfile = $self->io->catfile($self->tempdir,$self->plotfile);
309  
310 0 ▶ $self->throw("drawgram did not create plotfile correctly ($plotfile)")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $plotfile 0 0 0
311 unless (-e $plotfile);
312 0 return $plotfile;
313 }
314  
315 =head2 _setinput()
316  
317 Title : _setinput
318 Usage : Internal function, not to be called directly
319 Function: Create input file for drawing program
320 Example :
321 Returns : filename containing tree in newick format
322 Args : Bio::Tree::TreeI object
323  
324 =cut
325  
326 0 ▶sub _setinput {
Subroutine _setinput: not called CC=4 SCAR=30.0
327 0 my ($self, $input) = @_;
328 0 my $treefile;
329 0 ▶ unless (ref $input) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input) { } 0 0 0
if (do { $input->isa("Bio::Tree::TreeI") }) 0 0 0
330 # check that file exists or throw
331 0 $treefile = $input;
332 0 ▶ unless (-e $input) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $input) 0 0 0
333
334 0 } elsif ($input->isa("Bio::Tree::TreeI")) {
335 # Open temporary file for both reading & writing of BioSeq array
336 0 my $tfh;
337 0 ($tfh,$treefile) = $self->io->tempfile(-dir=>$self->tempdir);
338 0 my $treeIO = Bio::TreeIO->new(-fh => $tfh,
339 -format=>'newick');
340 0 $treeIO->write_tree($input);
341 0 $treeIO->close();
342 0 close($tfh);
343 0 undef $tfh;
344 }
345 0 return $treefile;
346 }
347  
348 =head2 _setparams()
349  
350 Title : _setparams
351 Usage : Internal function, not to be called directly
352 Function: Create parameter inputs for drawgram program
353 Example :
354 Returns : parameter string to be passed to drawgram
355 Args : name of calling object
356  
357 =cut
358  
359  
360  
361 0 ▶sub _setparams {
Subroutine _setparams: not called CC=10 SCAR=47.0
362 0 my ($attr, $value, $self);
363  
364 #do nothing for now
365 0 $self = shift;
366 0 my $param_string = "";
367 0 my $cat = 0;
368 0 my ($hmargin,$vmargin);
369 0 my %menu = %{$Menu{$self->version}->{'DRAWTREE'}};
370 0 foreach my $attr ( @DRAW_PARAMS) {
371 0 $value = $self->$attr();
372 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
373 0 my @vals;
374 0 ▶ if( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 0 0
375 0 ($value,@vals) = @$value;
376 }
377 0 $attr = uc($attr);
378 0 ▶ if( ! exists $menu{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $menu{$attr}) 0 0 0
379 0 $self->warn("unknown parameter $attr, known params are ".
380 join(",",keys %menu). "\n");
381 }
382 0 ▶ if( ref ($menu{$attr}) !~ /HASH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($menu{$attr}) =~ /HASH/i) 0 0 0
383 0 ▶ unless( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) { } 0 0 0
384 0 $param_string .= $menu{$attr};
385 } else {
386 0 $param_string .= sprintf($menu{$attr},$value,@vals);
387 }
388 0 next;
389 }
390 0 my $seen = 0;
391 0 for my $stype ( keys %{$menu{$attr}} ) {
392 0 ▶ if( $value =~ /$stype/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$stype/i) 0 0 0
393 0 $param_string .= sprintf($menu{$attr}->{$stype},@vals);
394 0 $seen = 1;
395 0 last;
396 }
397 }
398 0 ▶ unless( $seen ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 0 0 0
399 0 $self->warn("Unknown requested attribute $attr, $value is not known\n");
400 }
401 }
402 0 $param_string .="Y\n";
403 0 return $param_string;
404 }
405  
406  
407 =head1 Bio::Tools::Run::Wrapper methods
408  
409 =cut
410  
411 =head2 no_param_checks
412  
413 Title : no_param_checks
414 Usage : $obj->no_param_checks($newval)
415 Function: Boolean flag as to whether or not we should
416 trust the sanity checks for parameter values
417 Returns : value of no_param_checks
418 Args : newvalue (optional)
419  
420  
421 =cut
422  
423 =head2 save_tempfiles
424  
425 Title : save_tempfiles
426 Usage : $obj->save_tempfiles($newval)
427 Function:
428 Returns : value of save_tempfiles
429 Args : newvalue (optional)
430  
431  
432 =cut
433  
434 =head2 outfile_name
435  
436 Title : outfile_name
437 Usage : my $outfile = $dragram->outfile_name();
438 Function: Get/Set the name of the output file for this run
439 (if you wanted to do something special)
440 Returns : string
441 Args : [optional] string to set value to
442  
443  
444 =cut
445  
446  
447 =head2 tempdir
448  
449 Title : tempdir
450 Usage : my $tmpdir = $self->tempdir();
451 Function: Retrieve a temporary directory name (which is created)
452 Returns : string which is the name of the temporary directory
453 Args : none
454  
455  
456 =cut
457  
458 =head2 cleanup
459  
460 Title : cleanup
461 Usage : $codeml->cleanup();
462 Function: Will cleanup the tempdir directory after a DrawTree run
463 Returns : none
464 Args : none
465  
466  
467 =cut
468  
469 =head2 io
470  
471 Title : io
472 Usage : $obj->io($newval)
473 Function: Gets a Bio::Root::IO object
474 Returns : Bio::Root::IO object
475 Args : none
476  
477 See L<Bio::Root::IO>
478  
479 =cut
480  
481 1 1; # Needed to keep compiler happy
« Tools/Run/Phylo/Phylip/SeqBoot.pm ↑ Summary Tools/Run/Alignment/Sim4.pm »