blib/lib/Bio/Tools/Run/Alignment/Pal2Nal.pm

filter: Statementstmt 30.7% 24 / 78 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 70.0% 7 / 10 Podpod 100% 4 / 4 totaltotal 27.1% 35 / 129 CC 18 SCAR 44.3
CC
18
Cov
22%
run
156.0
_run
56.0
_setparams
2.0
CRAP 84.2
« blib/lib/Bio/Tools/Run/Hmmer.pm ↑ Summary blib/lib/Bio/Tools/Run/Alignment/Amap.pm »
1 # $Id: Pal2Nal.pm,v 1.3 2007/05/25 10:14:55 sendu Exp $
2 #
3 # BioPerl module for Bio::Tools::Run::Alignment::Pal2Nal
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Sendu Bala <bix@sendu.me.uk>
8 #
9 # Copyright Sendu Bala
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::Alignment::Pal2Nal - Wrapper for Pal2Nal
18  
19 =head1 SYNOPSIS
20  
21 use Bio::Tools::Run::Alignment::Pal2Nal;
22  
23 # Make a Pal2Nal factory
24 $factory = Bio::Tools::Run::Alignment::Pal2Nal->new();
25  
26 # Run Pal2Nal with a protein alignment file and a multi-fasta nucleotide
27 # file
28 my $aln = $factory->run($protein_alignfilename, $nucleotide_filename);
29  
30 # or with Bioperl objects
31 $aln = $factory->run($protein_bio_simplalign, [$nucleotide_bio_seq1,
32 $nucleotide_bio_seq2]);
33  
34 # combinations of files/ objects are possible
35  
36 # $aln isa Bio::SimpleAlign of the nucleotide sequences aligned according to
37 # the protein alignment
38  
39 =head1 DESCRIPTION
40  
41 This is a wrapper for running the Pal2Nal perl script by Mikita Suyama. You
42 can get details here: http://coot.embl.de/pal2nal/. Pal2Nal is used for aligning
43 a set of nucleotide sequences based on an alignment of their translations.
44  
45 You can try supplying normal pal2nal command-line arguments to new(), eg.
46 new() or calling arg-named methods (excluding the initial hyphen, eg.
47 $factory->(1) to set the - arg).
48  
49  
50 You will need to enable this Pal2Nal wrapper to find the pal2nal.pl script.
51 This can be done in (at least) three ways:
52  
53 1. Make sure the script is in your path.
54 2. Define an environmental variable PAL2NALDIR which is a
55 directory which contains the script:
56 In bash:
57  
58 export PAL2NALDIR=/home/username/pal2nal/
59  
60 In csh/tcsh:
61  
62 setenv PAL2NALDIR /home/username/pal2nal
63  
64 3. Include a definition of an environmental variable PAL2NALDIR in
65 every script that will use this Pal2Nal wrapper module, e.g.:
66  
67 BEGIN { $ENV{PAL2NALDIR} = '/home/username/pal2nal/' }
68 use Bio::Tools::Run::Alignment::Pal2Nal;
69  
70 =head1 FEEDBACK
71  
72 =head2 Mailing Lists
73  
74 User feedback is an integral part of the evolution of this and other
75 Bioperl modules. Send your comments and suggestions preferably to
76 the Bioperl mailing list. Your participation is much appreciated.
77  
78 bioperl-l@bioperl.org - General discussion
79 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
80  
81 =head2 Support
82  
83 Please direct usage questions or support issues to the mailing list:
84  
85 I<bioperl-l@bioperl.org>
86  
87 rather than to the module maintainer directly. Many experienced and
88 reponsive experts will be able look at the problem and quickly
89 address it. Please include a thorough description of the problem
90 with code and data examples if at all possible.
91  
92 =head2 Reporting Bugs
93  
94 Report bugs to the Bioperl bug tracking system to help us keep track
95 of the bugs and their resolution. Bug reports can be submitted via
96 the web:
97  
98 http://redmine.open-bio.org/projects/bioperl/
99  
100 =head1 AUTHOR - Sendu Bala
101  
102 Email bix@sendu.me.uk
103  
104 =head1 APPENDIX
105  
106 The rest of the documentation details each of the object methods.
107 Internal methods are usually preceded with a _
108  
109 =cut
110  
111 package Bio::Tools::Run::Alignment::Pal2Nal;
112 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 1 ▶use base qw(Bio::Tools::Run::Phylo::PhyloBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 1 our $PROGRAM_NAME = 'pal2nal.pl';
120 1 our $PROGRAM_DIR = $ENV{'PAL2NALDIR'};
121  
122 # methods for the pal2nal args we support
123 1 our @PARAMS = qw(codontable);
124 1 our @SWITCHES = qw(blockonly nogap nomismatch);
125  
126 # just to be explicit, args we don't support (yet) or we handle ourselves
127 1 our @UNSUPPORTED = qw(output html h nostderr);
128  
129 =head2 program_name
130  
131 Title : program_name
132 Usage : $factory>program_name()
133 Function: holds the program name
134 Returns : string
135 Args : None
136  
137 =cut
138  
139 sub program_name {
140 7 ▶ return $PROGRAM_NAME;
Subroutine program_name: called CC=1 SCAR=0.0
Pod documented
141 }
142  
143 =head2 program_dir
144  
145 Title : program_dir
146 Usage : $factory->program_dir(@params)
147 Function: returns the program directory, obtained from ENV variable.
148 Returns : string
149 Args : None
150  
151 =cut
152  
153 sub program_dir {
154 4 ▶ return $PROGRAM_DIR;
Subroutine program_dir: called CC=1 SCAR=0.0
Pod documented
155 }
156  
157 =head2 new
158  
159 Title : new
160 Usage : $factory = Bio::Tools::Run::Alignment::Pal2Nal->new()
161 Function: creates a new Pal2Nal factory.
162 Returns : Bio::Tools::Run::Alignment::Pal2Nal
163 Args : Most options understood by pal2nal.pl can be supplied as key =>
164 value pairs.
165  
166 These options can NOT be used with this wrapper:
167 -output
168 -html
169 -h
170 -nostderr
171  
172 =cut
173  
174 sub new {
175 1 ▶ my ($class, @args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
176 1 my $self = $class->SUPER::new(@args);
177
178 1 $self->_set_from_args(\@args, -methods => [@PARAMS, @SWITCHES, 'quiet'],
179 -create => 1);
180
181 1 return $self;
182 }
183  
184 =head2 run
185  
186 Title : run
187 Usage : $result = $factory->run($protein_align_file, $multi_fasta_nucleotide);
188 -or-
189 $result = $factory->run($prot_align_object, [$bioseq_object1, ...]);
190 Function: Runs pal2nal on a protein alignment and set of nucleotide sequences.
191 Returns : Bio::SimpleAlign;
192 Args : The first argument represents a protein alignment, the second
193 argument a set of nucleotide sequences.
194 The alignment can be provided as an alignment file readable by
195 Bio::AlignIO, or a Bio::Align::AlignI complient object (eg. a
196 Bio::SimpleAlign).
197 The nucleotide sequences can be provided as a single filename of a
198 fasta file containing multiple nucleotide sequences, or an array ref
199 of filenames, each file containing one sequence. Alternatively, an
200 array ref of Bio::PrimarySeqI compliant objects can be supplied.
201
202 In all cases, the protein alignment sequence names must correspond to
203 the names of the supplied nucleotide sequences.
204  
205 =cut
206  
207 sub run {
208 0 ▶ my ($self, $aln, $nucs) = @_;
Subroutine run: not called CC=12 SCAR=50.5
Pod documented
209
210 0 ▶ ($aln && $nucs) || $self->throw("alignment and nucleotides must be supplied");
Branch: 1 branch
BranchTrueFalseTotal
$aln and $nucs or $self->throw("alignment and nucleotides must be supplied") 0 0 0
Condition0%0/3
$aln and $nucs
!ll&&!rl&&r
000
MC/DC0%0/2
$aln and $nucs
$aln $nucs
Truth table0%0/3
A and B
A= $alnB= $nucs
ABresult
0X0
100
111
211 0 $aln = $self->_alignment($aln);
212
213 # gaps must be -, not .
214 0 my $fixed_aln = Bio::SimpleAlign->new();
215 0 foreach my $seq ($aln->each_seq) {
216 0 my $str = $seq->seq;
217 0 $str =~ s/\./-/g;
218 0 $fixed_aln->add_seq(Bio::LocatableSeq->new(-id => $seq->id, -seq => $str));
219 }
220 0 $self->_alignment($fixed_aln);
221
222 0 my $nucs_file;
223 0 ▶ if (-e $nucs) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $nucs) { } 0 0 0
elsif (ref $nucs eq 'ARRAY') { } 0 0 0
224 0 $nucs_file = $nucs;
225 }
226 elsif (ref($nucs) eq 'ARRAY') {
227 0 ▶ (my $tempfh, $nucs_file) = $self->io->tempfile('-dir' => $self->tempdir(), UNLINK => ($self->save_tempfiles ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$self->save_tempfiles ? : 0 0 0
228 0 close($tempfh);
229 0 my $sout = Bio::SeqIO->new(-file => ">".$nucs_file, -format => 'fasta');
230
231 0 foreach my $nuc (@{$nucs}) {
232 0 ▶ if (-e $nuc) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $nuc) { } 0 0 0
elsif (ref $nuc and $nuc->isa('Bio::PrimarySeqI')) { } 0 0 0
233 0 my $sin = Bio::SeqIO->new(-file => $nuc);
234 0 ▶ while (my $nuc_seq = $sin->next_seq) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nuc_seq = $sin->next_seq) 0 0 0
235 0 $sout->write_seq($nuc_seq);
236 }
237 }
238 ▶ elsif (ref($nuc) && $nuc->isa('Bio::PrimarySeqI')) {
Condition0%0/3
ref $nuc and $nuc->isa("Bio::PrimarySeqI")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nuc and $nuc->isa("Bio::PrimarySeqI")
ref $nuc $nuc->isa("Bio::PrimarySeqI")
Truth table0%0/3
A and B
A= ref $nucB= $nuc->isa("Bio::PrimarySeqI")
ABresult
0X0
100
111
239 0 $sout->write_seq($nuc);
240 }
241 else {
242 0 $self->throw("Don't understand nucleotide argument '$nuc'");
243 }
244 }
245 }
246 else {
247 0 $self->throw("Don't understand nucleotide argument '$nucs'");
248 }
249
250 0 return $self->_run($nucs_file);
251 }
252  
253 sub _run {
254 0 ▶ my ($self, $nucs_file) = @_;
Subroutine _run: not called CC=7 SCAR=40.3
255
256 0 ▶ my $exe = $self->executable || return;
Condition0%0/2
$self->executable || (return)
l!l
00
MC/DC0%0/1
$self->executable || (return)
$self->executable
Truth table0%0/2
A
A= $self->executable
Aresult
11
00
257
258 0 my $aln_file = $self->_write_alignment;
259
260 0 ▶ my ($rfh, $result_file) = $self->io->tempfile('-dir' => $self->tempdir(), UNLINK => ($self->save_tempfiles ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$self->save_tempfiles ? : 0 0 0
261 0 ▶ my ($efh, $error_file) = $self->io->tempfile('-dir' => $self->tempdir(), UNLINK => ($self->save_tempfiles ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$self->save_tempfiles ? : 0 0 0
262 0 close($rfh);
263 0 undef $rfh;
264 0 close($efh);
265 0 undef $efh;
266 0 my $command = $exe.$self->_setparams($aln_file, $nucs_file, $result_file, $error_file);
267 0 $self->debug("pal2nal command = $command\n");
268
269 0 ▶ system($command) && $self->throw("pal2nal call ($command) failed: $! | $?");
Branch: 1 branch
BranchTrueFalseTotal
system $command and $self->throw("pal2nal call ($command) failed: $! | $?") 0 0 0
270 0 open(my $errfh, '<', $error_file);
271 0 my $errors;
272 0 ▶ while (<$errfh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $errfh)) 0 0 0
273 0 $errors .= $_;
274 }
275 0 close($errfh);
276 0 ▶ $self->throw("pal2nal call ($command) had errors:\n$errors") if $errors;
Branch: 1 branch
BranchTrueFalseTotal
if $errors 0 0 0
277
278 0 my $ain = Bio::AlignIO->new(-file => $result_file, -format => 'fasta');
279 0 my $aln = $ain->next_aln;
280 0 $ain->close;
281
282 0 return $aln;
283 }
284  
285 =head2 _setparams
286  
287 Title : _setparams
288 Usage : Internal function, not to be called directly
289 Function: Creates a string of params to be used in the command string
290 Returns : string of params
291 Args : alignment and tree file names
292  
293 =cut
294  
295 sub _setparams {
296 0 ▶ my ($self, $aln_file, $nucs_file, $result_file, $error_file) = @_;
Subroutine _setparams: not called CC=1 SCAR=6.9
297
298 0 my $param_string = ' '.$aln_file;
299 0 $param_string .= ' '.$nucs_file;
300 0 $param_string .= $self->SUPER::_setparams(-params => \@PARAMS,
301 -switches => \@SWITCHES,
302 -dash => 1);
303 0 $param_string .= ' -output fasta';
304
305 0 $param_string .= " > $result_file 2> $error_file";
306
307 0 return $param_string;
308 }
309  
310 1 1;
« blib/lib/Bio/Tools/Run/Hmmer.pm ↑ Summary blib/lib/Bio/Tools/Run/Alignment/Amap.pm »