File Coverage

blib/lib/Bio/Tools/Run/Phylo/PAML/Yn00.pm
Criterion Covered Total %
statement 34 131 25.9
branch 2 48 4.1
condition 0 18 0.0
subroutine 11 20 55.0
pod 9 9 100.0
total 56 226 24.7


line stmt bran cond sub pod time code
1             package Bio::Tools::Run::Phylo::PAML::Yn00;
2             $Bio::Tools::Run::Phylo::PAML::Yn00::VERSION = '1.7.2';
3 1     1   365 use utf8;
  1         2  
  1         5  
4 1     1   27 use strict;
  1         1  
  1         17  
5 1     1   4 use warnings;
  1         2  
  1         28  
6              
7 1     1   5 use vars qw(@ISA %VALIDVALUES $MINNAMELEN $PROGRAMNAME $PROGRAM);
  1         2  
  1         58  
8 1     1   5 use Cwd;
  1         1  
  1         49  
9 1     1   8 use Bio::Root::Root;
  1         3  
  1         39  
10 1     1   6 use Bio::AlignIO;
  1         2  
  1         16  
11 1     1   4 use Bio::TreeIO;
  1         2  
  1         20  
12 1     1   5 use Bio::Tools::Run::WrapperBase;
  1         1  
  1         19  
13 1     1   4 use Bio::Tools::Phylo::PAML;
  1         4  
  1         130  
14              
15             @ISA = qw(Bio::Root::Root Bio::Tools::Run::WrapperBase);
16              
17             # ABSTRACT: Wrapper aroud the PAML program yn00
18             # AUTHOR: Jason Stajich
19             # OWNER: Jason Stajich
20             # LICENSE: Perl_5
21              
22              
23              
24             BEGIN {
25              
26 1     1   3 $MINNAMELEN = 25;
27 1 50       5 $PROGRAMNAME = 'yn00' . ($^O =~ /mswin/i ?'.exe':'');
28 1 50       4 if( defined $ENV{'PAMLDIR'} ) {
29 0         0 $PROGRAM = Bio::Root::IO->catfile($ENV{'PAMLDIR'},$PROGRAMNAME);
30             }
31             # valid values for parameters, the default one is always
32             # the first one in the array
33             # much of the documentation here is lifted directly from the codeml.ctl
34             # example file provided with the package
35             %VALIDVALUES = (
36 1         1013 'noisy' => [ 0..3,9],
37             'verbose' => [ 0,1,2], # 0:concise, 1:detailed, 2:too much
38              
39             'weighting' => [0,1], # weighting pathways between codons
40             'commonf3x4' => [0,1], # use same f3x4 for all sites
41              
42             # (icode) genetic code
43             # 0:universal code
44             # 1:mamalian mt
45             # 2:yeast mt
46             # 3:mold mt,
47             # 4:invertebrate mt
48             # 5:ciliate nuclear
49             # 6:echinoderm mt
50             # 7:euplotid mt
51             # 8:alternative yeast nu.
52             # 9:ascidian mt
53             #10:blepharisma nu
54             # these correspond to 1-11 in the genbank transl table
55              
56             'icode' => [ 0..10],
57             'ndata' => [1..10],
58             );
59             }
60              
61              
62              
63             sub program_name {
64 0     0 1   return $PROGRAMNAME;
65             }
66              
67              
68             sub program_dir {
69 0 0   0 1   return Bio::Root::IO->catfile($ENV{PAMLDIR}) if $ENV{PAMLDIR};
70             }
71              
72              
73             sub new {
74 0     0 1   my($class,@args) = @_;
75              
76 0           my $self = $class->SUPER::new(@args);
77 0           my ($aln,$st) = $self->_rearrange([qw(ALIGNMENT SAVE_TEMPFILES)],
78             @args);
79 0 0         defined $aln && $self->alignment($aln);
80 0 0         defined $st && $self->save_tempfiles($st);
81              
82 0           $self->set_default_parameters();
83 0           return $self;
84             }
85              
86              
87             sub run{
88 0     0 1   my ($self,$aln) = @_;
89 0   0       ($aln) ||= $self->alignment();
90 0 0         if( ! $aln ) {
91 0           $self->warn("must have supplied a valid alignment file in order to run yn00");
92 0           return 0;
93             }
94 0           my ($tmpdir) = $self->tempdir();
95 0           my ($tempseqFH,$tempseqfile);
96 0 0 0       if( ! ref($aln) && -e $aln ) {
97 0           $tempseqfile = $aln;
98             } else {
99 0 0         ($tempseqFH,$tempseqfile) = $self->io->tempfile
100             ('-dir' => $tmpdir,
101             UNLINK => ($self->save_tempfiles ? 0 : 1));
102 0 0         my $alnout = Bio::AlignIO->new('-format' => 'phylip',
103             '-fh' => $tempseqFH,
104             '-interleaved' => 0,
105             '-idlength' => $MINNAMELEN > $aln->maxdisplayname_length() ? $MINNAMELEN : $aln->maxdisplayname_length() +1);
106              
107 0           $alnout->write_aln($aln);
108 0           $alnout->close();
109 0           undef $alnout;
110 0           close($tempseqFH);
111 0           undef $tempseqFH;
112             }
113             # now let's print the yn.ctl file.
114             # many of the these programs are finicky about what the filename is
115             # and won't even run without the properly named file. Ack
116              
117 0           my $yn_ctl = "$tmpdir/yn00.ctl";
118 0 0         open(YN, ">$yn_ctl") or $self->throw("cannot open $yn_ctl for writing");
119 0           print YN "seqfile = $tempseqfile\n";
120              
121 0           my $outfile = $self->outfile_name;
122              
123 0           print YN "outfile = $outfile\n";
124 0           my %params = $self->get_parameters;
125 0           while( my ($param,$val) = each %params ) {
126 0           print YN "$param = $val\n";
127             }
128 0           close(YN);
129 0           my ($rc,$parser) = (1);
130             {
131 0           my $cwd = cwd();
  0            
132 0           my $exit_status;
133 0           chdir($tmpdir);
134 0           my $ynexe = $self->executable();
135 0 0         $self->throw("unable to find executable for 'yn'") unless $ynexe;
136 0           open(RUN, "$ynexe |");
137 0           my @output = ;
138 0           $exit_status = close(RUN);
139 0           $self->error_string(join('',@output));
140 0 0 0       if( (grep { /\berr(or)?: /io } @output) || !$exit_status ) {
  0            
141 0           $self->warn("There was an error - see error_string for the program output");
142 0           $rc = 0;
143             }
144 0           eval {
145 0           $parser = Bio::Tools::Phylo::PAML->new(-file => "$tmpdir/mlc",
146             -dir => "$tmpdir");
147              
148             };
149 0 0         if( $@ ) {
150 0           $self->warn($self->error_string);
151             }
152 0           chdir($cwd);
153             }
154 0 0         if( $self->verbose > 0 ) {
155 0           open(IN, "$tmpdir/mlc");
156 0           while() {
157 0           $self->debug($_);
158             }
159             }
160              
161 0 0         unless ( $self->save_tempfiles ) {
162 0           unlink("$yn_ctl");
163 0           $self->cleanup();
164             }
165 0           return ($rc,$parser);
166             }
167              
168              
169             sub error_string{
170 0     0 1   my ($self,$value) = @_;
171 0 0         if( defined $value) {
172 0           $self->{'error_string'} = $value;
173             }
174 0           return $self->{'error_string'};
175              
176             }
177              
178              
179             sub alignment{
180 0     0 1   my ($self,$aln) = @_;
181 0 0         if( defined $aln ) {
182 0 0 0       if( !ref($aln) || ! $aln->isa('Bio::Align::AlignI') ) {
183 0           $self->warn("Must specify a valid Bio::Align::AlignI object to the alignment function");
184 0           return undef;
185             }
186 0           $self->{'_alignment'} = $aln;
187             }
188 0           return $self->{'_alignment'};
189             }
190              
191              
192             sub get_parameters{
193 0     0 1   my ($self) = @_;
194             # we're returning a copy of this
195 0           return %{ $self->{'_codemlparams'} };
  0            
196             }
197              
198              
199              
200             sub set_parameter{
201 0     0 1   my ($self,$param,$value) = @_;
202 0 0         if( ! defined $VALIDVALUES{$param} ) {
203 0           $self->warn("unknown parameter $param will not set unless you force by setting no_param_checks to true");
204 0           return 0;
205             }
206 0 0 0       if( ref( $VALIDVALUES{$param}) =~ /ARRAY/i &&
207 0           scalar @{$VALIDVALUES{$param}} > 0 ) {
208              
209 0 0         unless ( grep {$value} @{ $VALIDVALUES{$param} } ) {
  0            
  0            
210 0           $self->warn("parameter $param specified value $value is not recognized, please see the documentation and the code for this module or set the no_param_checks to a true value");
211 0           return 0;
212             }
213             }
214 0           $self->{'_codemlparams'}->{$param} = $value;
215 0           return 1;
216             }
217              
218              
219             sub set_default_parameters{
220 0     0 1   my ($self,$keepold) = @_;
221 0 0         $keepold = 0 unless defined $keepold;
222              
223 0           while( my ($param,$val) = each %VALIDVALUES ) {
224             # skip if we want to keep old values and it is already set
225 0 0 0       next if( defined $self->{'_codemlparams'}->{$param} && $keepold);
226 0 0         if(ref($val)=~/ARRAY/i ) {
227 0           $self->{'_codemlparams'}->{$param} = $val->[0];
228             } else {
229 0           $self->{'_codemlparams'}->{$param} = $val;
230             }
231             }
232             }
233              
234              
235              
236              
237              
238              
239              
240              
241              
242              
243             1;
244              
245             __END__