3 # BioPerl module for Bio::Tools::Run::Phylo::Phylip::DrawTree
5 # Cared for by Jason Stajich <jason@bioperl.org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::Tools::Run::Phylo::Phylip::DrawTree - use Phylip DrawTree program to draw trees
19 use Bio::Tools::Run::Phylo::Phylip::DrawTree;
21 my $drawfact = new Bio::Tools::Run::Phylo::Phylip::DrawTree();
22 my $treeimagefile = $drawfact->run($tree);
26 This is a module for automating drawing of trees through Joe
27 Felsenstein's Phylip suite.
33 User feedback is an integral part of the evolution of this and other
34 Bioperl modules. Send your comments and suggestions preferably to
35 the Bioperl mailing list. Your participation is much appreciated.
37 bioperl-l@bioperl.org - General discussion
38 http://bioperl.org/MailList.shtml - About the mailing lists
42 Report bugs to the Bioperl bug tracking system to help us keep track
43 of the bugs and their resolution. Bug reports can be submitted via
46 bioperl-bugs@bioperl.org
47 http://bugzilla.bioperl.org/
49 =head1 AUTHOR - Jason Stajich
51 Email jason@bioperl.org
53 Describe contact details here
57 Additional contributors names and emails here
61 The rest of the documentation details each of the object methods.
62 Internal methods are usually preceded with a _
67 # Let the code begin...
70 package Bio
::Tools
::Run
::Phylo
::Phylip
::DrawTree
;
71 use vars
qw($AUTOLOAD @ISA $PROGRAM $PROGRAMDIR $PROGRAMNAME
72 $FONTFILE @DRAW_PARAMS @OTHER_SWITCHES
76 use Bio::Tools::Run::Phylo::Phylip::Base;
78 @ISA = qw( Bio::Tools::Run::Phylo::Phylip::Base );
80 # inherit from Phylip::Base which has some methods for dealing with
82 @ISA = qw(Bio::Tools::Run::Phylo::Phylip::Base);
84 # You will need to enable the neighbor program. This
85 # can be done in (at least) 3 ways:
87 # 1. define an environmental variable PHYLIPDIR:
88 # export PHYLIPDIR=/home/shawnh/PHYLIP/bin
90 # 2. include a definition of an environmental variable PHYLIPDIR in
91 # every script that will use DrawTree.pm.
92 # $ENV{PHYLIPDIR} = '/home/shawnh/PHYLIP/bin';
94 # 3. You can set the path to the program through doing:
95 # my @params('program'=>'/usr/local/bin/drawgram');
96 # my $neighbor_factory = Bio::Tools::Run::Phylo::Phylip::DrawTree->new(@params)
99 %DEFAULT = ('PLOTTER' => 'L',
102 $PROGRAMNAME="drawtree";
103 if (defined $ENV{'PHYLIPDIR'}) {
104 $PROGRAMDIR = $ENV{'PHYLIPDIR'} || '';
105 $PROGRAM = Bio
::Root
::IO
->catfile($PROGRAMDIR,
106 $PROGRAMNAME.($^O
=~ /mswin/i ?
'.exe':''));
107 $DEFAULT{'FONTFILE'} = Bio
::Root
::IO
->catfile($ENV{'PHYLIPDIR'},"font1");
110 $PROGRAM = $PROGRAMNAME;
113 @DRAW_PARAMS = qw(PLOTTER SCREEN LABEL_ANGLE ROTATION TREEARC
115 HORIZMARGINS VERTICALMARGINS
120 @OTHER_SWITCHES = qw(QUIET);
121 foreach my $attr(@DRAW_PARAMS,@OTHER_SWITCHES) {
129 Usage : >program_name()
130 Function: holds the program name
143 Usage : ->program_dir()
144 Function: returns the program directory, obtiained from ENV variable.
151 return Bio
::Root
::IO
->catfile($ENV{PHYLIPDIR
}) if $ENV{PHYLIPDIR
};
157 Usage : my $obj = new Bio::Tools::Run::Phylo::Phylip::DrawTree();
158 Function: Builds a new Bio::Tools::Run::Phylo::Phylip::DrawTree object
159 Returns : an instance of Bio::Tools::Run::Phylo::Phylip::DrawTree
160 Args : The available DrawGram parameters
166 my($class,@args) = @_;
168 my $self = $class->SUPER::new
(@args);
174 $value = shift @args;
175 next if( $attr =~ /^-/ ); # don't want named parameters
176 if ($attr =~/PROGRAM/i) {
177 $self->executable($value);
180 $self->$attr($value);
182 $self->plotter($DEFAULT{'PLOTTER'}) unless $self->plotter;
183 $self->screen($DEFAULT{'SCREEN'}) unless $self->screen;
184 $self->fontfile($DEFAULT{'FONTFILE'}) unless $self->fontfile;
191 my $attr = $AUTOLOAD;
194 $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
195 $self->{$attr} = shift if @_;
196 return $self->{$attr};
202 Usage : my $file = $app->run($treefile);
203 Function: Draw a tree
204 Returns : File containing the rendered tree
205 Args : either a Bio::Tree::TreeI
207 filename of a tree in newick format
212 my ($self,$input) = @_;
214 # Create input file pointer
215 my ($infilename) = $self->_setinput($input);
217 $self->throw("Problems setting up for drawgram. Probably bad input data in $input !");
220 # Create parameter string to pass to neighbor program
221 my $param_string = $self->_setparams();
224 my $plotfile = $self->_run($infilename,$param_string);
231 Usage : my $file = $app->draw_tree($treefile);
232 Function: This method is deprecated. Please use run method.
233 Returns : File containing the rendered tree
234 Args : either a Bio::Tree::TreeI
236 filename of a tree in newick format
241 return shift->run(@_);
247 Usage : Internal function, not to be called directly
248 Function: makes actual system call to drawgram program
250 Returns : Bio::Tree object
251 Args : Name of a file the tree to draw in newick format
252 and a parameter string to be passed to drawgram
258 my ($self,$infile,$param_string) = @_;
262 unless( File
::Spec
->file_name_is_absolute($infile) ) {
263 $infile = $self->io->catfile($curpath,$infile);
265 $instring = $infile . "\n";
266 if( ! defined $self->fontfile ) {
267 $self->throw("You must have defined a fontfile");
270 if( -e
$self->io->catfile($curpath,'fontfile') ) {
271 $instring .= $self->io->catfile($curpath,'fontfile')."\n";
272 } elsif( File
::Spec
->file_name_is_absolute($self->fontfile) ) {
273 #$instring .= $self->io->catfile($self->tempdir,$self->fontfile)."\n";
274 $instring .= $self->io->catfile($self->fontfile)."\n";
276 $instring .= $self->io->catfile($curpath,$self->fontfile)."\n";
278 chdir($self->tempdir);
279 $instring .= $param_string;
280 $self->debug( "Program ".$self->executable." $param_string\n");
281 # open a pipe to run drawgram to bypass interactive menus
282 if ($self->quiet() || $self->verbose() < 0) {
283 open(DRAW
,"|".$self->executable.">/dev/null");
286 open(DRAW
,"|".$self->executable);
288 print DRAW
$instring;
292 my $plotfile = $self->io->catfile($self->tempdir,$self->plotfile);
294 $self->throw("drawgram did not create plotfile correctly ($plotfile)")
295 unless (-e
$plotfile);
302 Usage : Internal function, not to be called directly
303 Function: Create input file for drawing program
305 Returns : filename containing tree in newick format
306 Args : Bio::Tree::TreeI object
312 my ($self, $input) = @_;
314 unless (ref $input) {
315 # check that file exists or throw
317 unless (-e
$input) {return 0;}
319 } elsif ($input->isa("Bio::Tree::TreeI")) {
320 # Open temporary file for both reading & writing of BioSeq array
322 ($tfh,$treefile) = $self->io->tempfile(-dir
=>$self->tempdir);
323 my $treeIO = Bio
::TreeIO
->new(-fh
=> $tfh,
325 $treeIO->write_tree($input);
336 Usage : Internal function, not to be called directly
337 Function: Create parameter inputs for drawgram program
339 Returns : parameter string to be passed to drawgram
340 Args : name of calling object
345 my ($attr, $value, $self);
349 my $param_string = "";
351 my ($hmargin,$vmargin);
352 foreach my $attr ( @DRAW_PARAMS) {
353 $value = $self->$attr();
356 next unless (defined $value);
357 if ($attr eq 'PLOTTER' ||
358 $attr eq 'SCREEN' ) {
359 # take first char of the input
360 $param_string .= uc(substr($value,0,1))."\n";
362 } elsif( $attr eq 'USEBRANCHLENS' ) {
363 if( uc(substr($value,0,1)) eq 'Y' ||
364 uc(substr($value,0,1)) eq '1'
366 $self->warn("Expected a number in $attr\n");
369 $param_string .= "1\n$1";
370 } elsif( $attr eq 'LABEL_ANGLE' ) {
371 if( $value !~ /([FRA])/i ) {
372 $self->warn("($attr)Expected value of one of F,R,A");
376 $param_string .= "2\n$a\n";
379 if( $value =~ /(\-?\d+(\.\d+)?)/ ) {
381 if( $angle >= 90 || $angle < -90 ) {
382 $self->warn("provided an angle which is too large ($angle) expected -90 <= $angle <= 90, setting it to 0");
386 $param_string .= "$angle\n";
388 } elsif( $attr eq 'ROTATION' ) {
389 if( $value !~ /(\-?\d+(\.\d+)?)/ ||
390 $1 < -360 || $1 > 360 ) {
391 $self->warn("($attr)Expected a number between -360 and 360 $attr\n");
394 $param_string = "3\n$1\n";
395 } elsif( $attr eq 'TREEARC' ) {
396 if( $value !~ /(\-?\d+(\.\d+)?)/ ||
397 $1 <= 0 || $1 > 360 ) {
398 $self->warn("($attr)Expected a number between -360 and 360 $attr\n");
401 $param_string = "4\n$1\n";
402 } elsif( $attr eq 'ITERATE' ) {
403 if( uc(substr($value,0,1)) eq 'N' ||
404 substr($value,0,1) eq '0' ) {
405 $param_string .= "5\n";
407 } elsif( $attr eq 'SCALE' ) {
408 if( $value !~ /(\d+(\.\d+)?)/ ) {
409 $self->warn("($attr)Expected a number in $attr\n");
412 $param_string .= "6\n$1\n";
413 } elsif( $attr eq 'HORIZMARGINS' ) {
414 if( $value !~ /(\d+(\.\d+)?)/ ) {
415 $self->warn("($attr)Expected a number in $attr\n");
419 } elsif( $attr eq 'VERTICALMARGINS' ) {
420 if( $value !~ /(\d+(\.\d+)?)/ ) {
421 $self->warn("Expected a number in $attr\n");
425 } elsif( $attr eq 'CHARHEIGHT' ) {
426 if( $value !~ /(\d+(\.\d+)?)/ ) {
427 $self->warn("Expected a number in $attr\n");
430 $param_string .= "8\n$1";
431 } elsif( $attr eq 'ENTHUSIASM' ) {
432 if( $value !~ /(\d+(\.\d+)?)/ ) {
433 $self->warn("Expected a number from in $attr\n");
436 $param_string .= "9\n$1\n";
438 } elsif( $attr eq 'FONT' ) {
439 $value =~ s/([\w\d]+)\s+/$1/g;
440 $param_string .= "10\n$value\n";
443 if( $hmargin || $vmargin ) {
446 $param_string .= "5\n$hmargin\n$vmargin\n";
449 $param_string .="Y\n";
450 return $param_string;
455 =head1 Bio::Tools::Run::Wrapper methods
459 =head2 no_param_checks
461 Title : no_param_checks
462 Usage : $obj->no_param_checks($newval)
463 Function: Boolean flag as to whether or not we should
464 trust the sanity checks for parameter values
465 Returns : value of no_param_checks
466 Args : newvalue (optional)
471 =head2 save_tempfiles
473 Title : save_tempfiles
474 Usage : $obj->save_tempfiles($newval)
476 Returns : value of save_tempfiles
477 Args : newvalue (optional)
485 Usage : my $outfile = $dragram->outfile_name();
486 Function: Get/Set the name of the output file for this run
487 (if you wanted to do something special)
489 Args : [optional] string to set value to
498 Usage : my $tmpdir = $self->tempdir();
499 Function: Retrieve a temporary directory name (which is created)
500 Returns : string which is the name of the temporary directory
509 Usage : $codeml->cleanup();
510 Function: Will cleanup the tempdir directory after a DrawTree run
520 Usage : $obj->io($newval)
521 Function: Gets a L<Bio::Root::IO> object
522 Returns : L<Bio::Root::IO>
528 1; # Needed to keep compiler happy