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

filter: Statementstmt 36.3% 48 / 132 Branchbran 24.1% 14 / 58 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 10 / 15 Podpod 100% 5 / 5 totaltotal 35.3% 77 / 218 CC 35 SCAR 36.6
CC
35
Cov
32%
_setparams
110.0
_run
72.0
_setinput
20.0
CRAP 38.7
« blib/lib/Bio/Tools/Run/Alignment/Sim4.pm ↑ Summary blib/lib/Bio/Tools/Run/Phylo/Semphy.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
90 $FONTFILE @DRAW_PARAMS @OTHER_SWITCHES
91 1 ▶ %OK_FIELD %DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 BEGIN {
117 1 ▶ %DEFAULT = ('PLOTTER' => 'P',
Subroutine BEGIN: called CC=6 SCAR=22.3
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 sub program_name {
152 6 ▶ return $PROGRAMNAME;
Subroutine program_name: called CC=1 SCAR=0.0
Pod documented
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 sub program_dir {
166 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
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 sub new {
181 1 ▶ my($class,@args) = @_;
Subroutine new: called CC=7 SCAR=20.2
Pod documented
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 sub AUTOLOAD {
205 4 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
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 sub run{
227 0 ▶ my ($self,$input) = @_;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
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 sub draw_tree{
256 0 ▶ return shift->run(@_);
Subroutine draw_tree: not called CC=1 SCAR=6.9
Pod documented
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 sub _run {
273 0 ▶ my ($self,$infile,$param_string) = @_;
Subroutine _run: not called CC=8 SCAR=42.8
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 open(DRAW,"|".$self->executable.">/dev/null");
299 }
300 else {
301 0 open(DRAW,"|".$self->executable);
302 }
303 0 print DRAW $instring;
304 0 close(DRAW);
305 0 chdir($curpath);
306 #get the results
307 0 my $plotfile = $self->io->catfile($self->tempdir,$self->plotfile);
308  
309 0 ▶ $self->throw("drawgram did not create plotfile correctly ($plotfile)")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $plotfile 0 0 0
310 unless (-e $plotfile);
311 0 return $plotfile;
312 }
313  
314 =head2 _setinput()
315  
316 Title : _setinput
317 Usage : Internal function, not to be called directly
318 Function: Create input file for drawing program
319 Example :
320 Returns : filename containing tree in newick format
321 Args : Bio::Tree::TreeI object
322  
323 =cut
324  
325 sub _setinput {
326 0 ▶ my ($self, $input) = @_;
Subroutine _setinput: not called CC=4 SCAR=30.0
327 0 my $treefile;
328 0 ▶ unless (ref $input) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input) { } 0 0 0
(do { $input->isa("Bio::Tree::TreeI") }) 0 0 0
329 # check that file exists or throw
330 0 $treefile = $input;
331 0 ▶ unless (-e $input) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $input) 0 0 0
332
333 0 } elsif ($input->isa("Bio::Tree::TreeI")) {
334 # Open temporary file for both reading & writing of BioSeq array
335 0 my $tfh;
336 0 ($tfh,$treefile) = $self->io->tempfile(-dir=>$self->tempdir);
337 0 my $treeIO = Bio::TreeIO->new(-fh => $tfh,
338 -format=>'newick');
339 0 $treeIO->write_tree($input);
340 0 $treeIO->close();
341 0 close($tfh);
342 0 undef $tfh;
343 }
344 0 return $treefile;
345 }
346  
347 =head2 _setparams()
348  
349 Title : _setparams
350 Usage : Internal function, not to be called directly
351 Function: Create parameter inputs for drawgram program
352 Example :
353 Returns : parameter string to be passed to drawgram
354 Args : name of calling object
355  
356 =cut
357  
358  
359  
360 sub _setparams {
361 0 ▶ my ($attr, $value, $self);
Subroutine _setparams: not called CC=10 SCAR=47.0
362  
363 #do nothing for now
364 0 $self = shift;
365 0 my $param_string = "";
366 0 my $cat = 0;
367 0 my ($hmargin,$vmargin);
368 0 my %menu = %{$Menu{$self->version}->{'DRAWTREE'}};
369 0 foreach my $attr ( @DRAW_PARAMS) {
370 0 $value = $self->$attr();
371 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
372 0 my @vals;
373 0 ▶ if( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 0 0
374 0 ($value,@vals) = @$value;
375 }
376 0 $attr = uc($attr);
377 0 ▶ if( ! exists $menu{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $menu{$attr}) 0 0 0
378 0 $self->warn("unknown parameter $attr, known params are ".
379 join(",",keys %menu). "\n");
380 }
381 0 ▶ if( ref ($menu{$attr}) !~ /HASH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($menu{$attr}) =~ /HASH/i) 0 0 0
382 0 ▶ unless( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) { } 0 0 0
383 0 $param_string .= $menu{$attr};
384 } else {
385 0 $param_string .= sprintf($menu{$attr},$value,@vals);
386 }
387 0 next;
388 }
389 0 my $seen = 0;
390 0 for my $stype ( keys %{$menu{$attr}} ) {
391 0 ▶ if( $value =~ /$stype/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$stype/i) 0 0 0
392 0 $param_string .= sprintf($menu{$attr}->{$stype},@vals);
393 0 $seen = 1;
394 0 last;
395 }
396 }
397 0 ▶ unless( $seen ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 0 0 0
398 0 $self->warn("Unknown requested attribute $attr, $value is not known\n");
399 }
400 }
401 0 $param_string .="Y\n";
402 0 return $param_string;
403 }
404  
405  
406 =head1 Bio::Tools::Run::Wrapper methods
407  
408 =cut
409  
410 =head2 no_param_checks
411  
412 Title : no_param_checks
413 Usage : $obj->no_param_checks($newval)
414 Function: Boolean flag as to whether or not we should
415 trust the sanity checks for parameter values
416 Returns : value of no_param_checks
417 Args : newvalue (optional)
418  
419  
420 =cut
421  
422 =head2 save_tempfiles
423  
424 Title : save_tempfiles
425 Usage : $obj->save_tempfiles($newval)
426 Function:
427 Returns : value of save_tempfiles
428 Args : newvalue (optional)
429  
430  
431 =cut
432  
433 =head2 outfile_name
434  
435 Title : outfile_name
436 Usage : my $outfile = $dragram->outfile_name();
437 Function: Get/Set the name of the output file for this run
438 (if you wanted to do something special)
439 Returns : string
440 Args : [optional] string to set value to
441  
442  
443 =cut
444  
445  
446 =head2 tempdir
447  
448 Title : tempdir
449 Usage : my $tmpdir = $self->tempdir();
450 Function: Retrieve a temporary directory name (which is created)
451 Returns : string which is the name of the temporary directory
452 Args : none
453  
454  
455 =cut
456  
457 =head2 cleanup
458  
459 Title : cleanup
460 Usage : $codeml->cleanup();
461 Function: Will cleanup the tempdir directory after a DrawTree run
462 Returns : none
463 Args : none
464  
465  
466 =cut
467  
468 =head2 io
469  
470 Title : io
471 Usage : $obj->io($newval)
472 Function: Gets a Bio::Root::IO object
473 Returns : Bio::Root::IO object
474 Args : none
475  
476 See L<Bio::Root::IO>
477  
478 =cut
479  
480 1 1; # Needed to keep compiler happy
« blib/lib/Bio/Tools/Run/Alignment/Sim4.pm ↑ Summary blib/lib/Bio/Tools/Run/Phylo/Semphy.pm »