File Coverage

blib/lib/Bio/Tools/Run/Phylo/Phylip/DrawGram.pm
Criterion Covered Total %
statement 44 125 35.2
branch 11 54 20.3
condition 0 3 0.0
subroutine 10 15 66.6
pod 5 5 100.0
total 70 202 34.6


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