Tools/Run/Alignment/Proda.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 268 CC 26 SCAR 36.4
CC
26
Cov
0%
_setinput
110.0
_setparams
72.0
AUTOLOAD
12.0
CRAP 38.0
1 # BioPerl module for Bio::Tools::Run::Alignment::Proda
2 #
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
4 #
5 # Cared for by Albert Vilella
6 #
7 #
8 #
9 # You may distribute this module under the same terms as perl itself
10 #
11 # POD documentation - main docs before the code
12  
13 =head1 NAME
14  
15 Bio::Tools::Run::Alignment::Proda - Object for the calculation of sets
16 of multiple sequence alignments from a set of unaligned sequences or
17 alignments using the Proda program.
18  
19 =head1 SYNOPSIS
20  
21 # Build a Proda alignment factory
22 $factory = Bio::Tools::Run::Alignment::Proda->new(@params);
23  
24 # Pass the factory a list of sequences to be aligned.
25 $inputfilename = 't/cysprot.fa';
26 # @alns is an array of SimpleAlign objects.
27 @alns = $factory->align($inputfilename);
28  
29 # or where @seq_array is an array of Bio::Seq objects
30 $seq_array_ref = \@seq_array;
31 @alns = $factory->align($seq_array_ref);
32  
33 # Or one can pass the factory a pair of (sub)alignments
34 #to be aligned against each other, e.g.:
35  
36 #There are various additional options and input formats available.
37 #See the DESCRIPTION section that follows for additional details.
38  
39 $factory = Bio::Tools::Run::Alignment::Proda->new();
40 @alns = $factory->align($seq_array_ref);
41  
42 =head1 DESCRIPTION
43  
44 You can get it and see information about it at this URL
45 http://proda.stanford.edu
46  
47 This program will return one or more local alignments for the
48 different repeated or rearranged regions in the sequences. If a
49 sequences contains more than one of those patterns, it will be present
50 more than once in the alignment. The difference will be in that the id
51 contain the start and end, like myseqid(123-456) and myseqid(567-890),
52 instead of simply myseqid as in the original input file. This is true
53 for all the output ids, even if they are present only once.
54  
55 =head2 Helping the module find your executable
56  
57 You will need to enable Proda to find the proda program. This can be
58 done in (at least) three ways:
59  
60 1. Make sure the proda executable is in your path (i.e.
61 'which proda' returns a valid program
62 2. define an environmental variable PRODADIR which points to a
63 directory containing the 'proda' app:
64 In bash
65 export PRODADIR=/home/progs/proda or
66 In csh/tcsh
67 setenv PRODADIR /home/progs/proda
68  
69 3. include a definition of an environmental variable PRODADIR
70 in every script that will
71 BEGIN {$ENV{PRODADIR} = '/home/progs/proda'; }
72 use Bio::Tools::Run::Alignment::Proda;
73  
74  
75 =head1 FEEDBACK
76  
77 =head2 Mailing Lists
78  
79 User feedback is an integral part of the evolution of this and other
80 Bioperl modules. Send your comments and suggestions preferably to one
81 of the Bioperl mailing lists. Your participation is much appreciated.
82  
83 bioperl-l@bioperl.org - General discussion
84 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
85  
86 =head2 Support
87  
88 Please direct usage questions or support issues to the mailing list:
89  
90 I<bioperl-l@bioperl.org>
91  
92 rather than to the module maintainer directly. Many experienced and
93 reponsive experts will be able look at the problem and quickly
94 address it. Please include a thorough description of the problem
95 with code and data examples if at all possible.
96  
97 =head2 Reporting Bugs
98  
99 Report bugs to the Bioperl bug tracking system to help us keep track
100 the bugs and their resolution. Bug reports can be submitted via the web:
101  
102 http://redmine.open-bio.org/projects/bioperl/
103  
104 =head1 AUTHOR - Albert Vilella
105  
106 Email avilella-at-gmail-dot-com
107  
108 =head1 APPENDIX
109  
110 The rest of the documentation details each of the object
111 methods. Internal methods are usually preceded with a _
112  
113 =cut
114  
115 package Bio::Tools::Run::Alignment::Proda;
116  
117 use vars qw($AUTOLOAD @ISA $PROGRAMNAME $PROGRAM %DEFAULTS
118 @PRODA_PARAMS @PRODA_SWITCHES @OTHER_SWITCHES
119 %OK_FIELD
120 );
121 use strict;
122 use Bio::Seq;
123 use Bio::SeqIO;
124 use Bio::SimpleAlign;
125 use Bio::AlignIO;
126 use Bio::Root::Root;
127 use Bio::Root::IO;
128 use Bio::Factory::ApplicationFactoryI;
129 use Bio::Tools::Run::WrapperBase;
130 @ISA = qw(Bio::Root::Root Bio::Tools::Run::WrapperBase
131 Bio::Factory::ApplicationFactoryI);
132  
133  
134 BEGIN {
135 %DEFAULTS = ( 'AFORMAT' => 'proda' );
136 @PRODA_PARAMS = qw (L);
137 @PRODA_SWITCHES = qw(POSTERIOR TRAN SILENT);
138 @OTHER_SWITCHES = qw();
139  
140 # Authorize attribute fields
141 foreach my $attr ( @PRODA_PARAMS, @PRODA_SWITCHES, @OTHER_SWITCHES ) {
142 $OK_FIELD{$attr}++; }
143 }
144  
145 =head2 program_name
146  
147 Title : program_name
148 Usage : $factory->program_name()
149 Function: holds the program name
150 Returns: string
151 Args : None
152  
153 =cut
154  
155 sub program_name {
156 return 'proda';
157 }
158  
159 =head2 program_dir
160  
161 Title : program_dir
162 Usage : $factory->program_dir(@params)
163 Function: returns the program directory, obtained from ENV variable.
164 Returns: string
165 Args :
166  
167 =cut
168  
169 sub program_dir {
170 return Bio::Root::IO->catfile($ENV{PRODADIR}) if $ENV{PRODADIR};
171 }
172  
173 =head2 new
174  
175 Title : new
176 Usage : my $proda = Bio::Tools::Run::Alignment::Proda->new();
177 Function: Constructor
178 Returns : Bio::Tools::Run::Alignment::Proda
179 Args : -outfile_name => $outname
180  
181  
182 =cut
183  
184 sub new{
185 my ($class,@args) = @_;
186 my $self = $class->SUPER::new(@args);
187 my ($on) = $self->SUPER::_rearrange([qw(OUTFILE_NAME)], @args);
188 $self->outfile_name($on || '');
189 my ($attr, $value);
190 $self->aformat($DEFAULTS{'AFORMAT'});
191  
192 while ( @args) {
193 $attr = shift @args;
194 $value = shift @args;
195 next if( $attr =~ /^-/); # don't want named parameters
196 $self->$attr($value);
197 }
198 return $self;
199 }
200  
201 sub AUTOLOAD {
202 my $self = shift;
203 my $attr = $AUTOLOAD;
204 $attr =~ s/.*:://;
205 $attr = uc $attr;
206 # aliasing
207 $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
208  
209 $self->{$attr} = shift if @_;
210 return $self->{$attr};
211 }
212  
213 =head2 error_string
214  
215 Title : error_string
216 Usage : $obj->error_string($newval)
217 Function: Where the output from the last analysus run is stored.
218 Returns : value of error_string
219 Args : newvalue (optional)
220  
221  
222 =cut
223  
224 sub error_string{
225 my ($self,$value) = @_;
226 if( defined $value) {
227 $self->{'error_string'} = $value;
228 }
229 return $self->{'error_string'};
230  
231 }
232  
233 =head2 version
234  
235 Title : version
236 Usage : exit if $prog->version() < 1.8
237 Function: Determine the version number of the program
238 Example :
239 Returns : float or undef
240 Args : none
241  
242 =cut
243  
244 sub version {
245 my ($self) = @_;
246 my $exe;
247 return undef unless $exe = $self->executable;
248 my $string = `$exe 2>&1` ;
249 #PRODA version 1.09 - align multiple protein sequences and print to standard output
250 $string =~ /ProDA\s+version\s+(\d+\.\d+)/m;
251 return $1 || undef;
252 }
253  
254 =head2 run
255  
256 Title : run
257 Usage : my $output = $application->run(\@seqs);
258 Function: Generic run of an application
259 Returns : Bio::SimpleAlign object
260 Args : Arrayref of Bio::PrimarySeqI objects or
261 a filename to run on
262  
263 =cut
264  
265 sub run {
266 my $self = shift;
267 return $self->align(shift);
268 }
269  
270 =head2 align
271  
272 Title : align
273 Usage :
274 $inputfilename = 't/data/cysprot.fa';
275 $aln = $factory->align($inputfilename);
276 or
277 $seq_array_ref = \@seq_array;
278 # @seq_array is array of Seq objs
279 $aln = $factory->align($seq_array_ref);
280 Function: Perform a multiple sequence alignment
281 Returns : Reference to a SimpleAlign object containing the
282 sequence alignment.
283 Args : Name of a file containing a set of unaligned fasta sequences
284 or else an array of references to Bio::Seq objects.
285  
286 Throws an exception if argument is not either a string (eg a
287 filename) or a reference to an array of Bio::Seq objects. If
288 argument is string, throws exception if file corresponding to string
289 name can not be found. If argument is Bio::Seq array, throws
290 exception if less than two sequence objects are in array.
291  
292 =cut
293  
294 sub align {
295 my ($self,$input) = @_;
296 # Create input file pointer
297 $self->io->_io_cleanup();
298 my ($infilename) = $self->_setinput($input);
299 if (! $infilename) {
300 $self->throw("Bad input data or less than 2 sequences in $input !");
301 }
302  
303 my $param_string = $self->_setparams();
304  
305 # run proda
306 return &_run($self, $infilename, $param_string);
307 }
308  
309 =head2 _run
310  
311 Title : _run
312 Usage : Internal function, not to be called directly
313 Function: makes actual system call to proda program
314 Example :
315 Returns : nothing; proda output is written to a
316 temporary file OR specified output file
317 Args : Name of a file containing a set of unaligned fasta sequences
318 and hash of parameters to be passed to proda
319  
320  
321 =cut
322  
323 sub _run {
324 my ($self,$infilename,$params) = @_;
325 my $commandstring = $self->executable." $infilename $params";
326  
327 $self->debug( "proda command = $commandstring \n");
328  
329 my $status = system($commandstring);
330 my $outfile = $self->outfile_name();
331  
332 if( !-e $outfile || -z $outfile ) {
333 $self->warn( "Proda call crashed: $? [command $commandstring]\n");
334 return undef;
335 }
336  
337 my $in = Bio::AlignIO->new('-file' => $outfile,
338 '-format' => $self->aformat,
339 );
340 my @alns;
341 while(my $aln = $in->next_aln) {
342 push @alns, $aln;
343 }
344 return @alns;
345 }
346  
347  
348 =head2 _setinput
349  
350 Title : _setinput
351 Usage : Internal function, not to be called directly
352 Function: Create input file for proda program
353 Example :
354 Returns : name of file containing proda data input AND
355 Args : Arrayref of Seqs or input file name
356  
357  
358 =cut
359  
360 sub _setinput {
361 my ($self,$input) = @_;
362 my ($infilename, $seq, $temp, $tfh);
363 if (! ref $input) {
364 # check that file exists or throw
365 $infilename = $input;
366 unless (-e $input) {return 0;}
367 # let's peek and guess
368 open(IN,$infilename) || $self->throw("Cannot open $infilename");
369 my $header;
370 while( defined ($header = <IN>) ) {
371 last if $header !~ /^\s+$/;
372 }
373 close(IN);
374 if ( $header !~ /^>\s*\S+/ ){
375 $self->throw("Need to provide a FASTA format file to proda!");
376 }
377 return ($infilename);
378 } elsif (ref($input) =~ /ARRAY/i ) { # $input may be an
379 # array of BioSeq objects...
380 # Open temporary file for both reading & writing of array
381 ($tfh,$infilename) = $self->io->tempfile();
382 if( ! ref($input->[0]) ) {
383 $self->warn("passed an array ref which did not contain objects to _setinput");
384 return undef;
385 } elsif( $input->[0]->isa('Bio::PrimarySeqI') ) {
386 $temp = Bio::SeqIO->new('-fh' => $tfh,
387 '-format' => 'fasta');
388 my $ct = 1;
389 foreach $seq (@$input) {
390 return 0 unless ( ref($seq) &&
391 $seq->isa("Bio::PrimarySeqI") );
392 if( ! defined $seq->display_id ||
393 $seq->display_id =~ /^\s+$/) {
394 $seq->display_id( "Seq".$ct++);
395 }
396 $temp->write_seq($seq);
397 }
398 $temp->close();
399 undef $temp;
400 close($tfh);
401 $tfh = undef;
402 } else {
403 $self->warn( "got an array ref with 1st entry ".
404 $input->[0].
405 " and don't know what to do with it\n");
406 }
407 return ($infilename);
408 } else {
409 $self->warn("Got $input and don't know what to do with it\n");
410 }
411 return 0;
412 }
413  
414  
415 =head2 _setparams
416  
417 Title : _setparams
418 Usage : Internal function, not to be called directly
419 Function: Create parameter inputs for proda program
420 Example :
421 Returns : parameter string to be passed to proda
422 during align or profile_align
423 Args : name of calling object
424  
425 =cut
426  
427 sub _setparams {
428 my ($self) = @_;
429 my ($attr, $value,$param_string);
430 $param_string = '';
431 my $laststr;
432 for $attr ( @PRODA_PARAMS ) {
433 $value = $self->$attr();
434 next unless (defined $value);
435 my $attr_key = lc $attr;
436 $attr_key = ' --'.$attr_key unless ($attr eq 'ANNOT');
437 $attr_key = ' -'.$attr_key if ($attr eq 'ANNOT');
438 $param_string .= $attr_key .' '.$value;
439  
440 }
441 for $attr ( @PRODA_SWITCHES) {
442 $value = $self->$attr();
443 next unless ($value);
444 my $attr_key = lc $attr; #put switches in format expected by tcoffee
445 $attr_key = ' -'.$attr_key;
446 $param_string .= $attr_key ;
447 }
448 # Set default output file if no explicit output file selected
449 unless ($self->outfile_name ) {
450 my ($tfh, $outfile) = $self->io->tempfile(-dir=>$self->tempdir());
451 close($tfh);
452 undef $tfh;
453 $self->outfile_name($outfile);
454 }
455 #FIXME: This may be only for *nixes. Double check in other OSes
456 $param_string .= " > ".$self->outfile_name;
457  
458 if ($self->verbose < 0) {
459 my $null = ($^O =~ m/mswin/i) ? 'NUL' : '/dev/null';
460 $param_string .= " 2> $null";
461 }
462 return $param_string;
463 }
464  
465 =head2 aformat
466  
467 Title : aformat
468 Usage : my $alignmentformat = $self->aformat();
469 Function: Get/Set alignment format
470 Returns : string
471 Args : string
472  
473  
474 =cut
475  
476 sub aformat{
477 my $self = shift;
478 $self->{'_aformat'} = shift if @_;
479 return $self->{'_aformat'};
480 }
481  
482 =head1 Bio::Tools::Run::BaseWrapper methods
483  
484 =cut
485  
486 =head2 no_param_checks
487  
488 Title : no_param_checks
489 Usage : $obj->no_param_checks($newval)
490 Function: Boolean flag as to whether or not we should
491 trust the sanity checks for parameter values
492 Returns : value of no_param_checks
493 Args : newvalue (optional)
494  
495  
496 =cut
497  
498 =head2 save_tempfiles
499  
500 Title : save_tempfiles
501 Usage : $obj->save_tempfiles($newval)
502 Function:
503 Returns : value of save_tempfiles
504 Args : newvalue (optional)
505  
506  
507 =cut
508  
509 =head2 outfile_name
510  
511 Title : outfile_name
512 Usage : my $outfile = $proda->outfile_name();
513 Function: Get/Set the name of the output file for this run
514 (if you wanted to do something special)
515 Returns : string
516 Args : [optional] string to set value to
517  
518  
519 =cut
520  
521  
522 =head2 tempdir
523  
524 Title : tempdir
525 Usage : my $tmpdir = $self->tempdir();
526 Function: Retrieve a temporary directory name (which is created)
527 Returns : string which is the name of the temporary directory
528 Args : none
529  
530  
531 =cut
532  
533 =head2 cleanup
534  
535 Title : cleanup
536 Usage : $proda->cleanup();
537 Function: Will cleanup the tempdir directory
538 Returns : none
539 Args : none
540  
541  
542 =cut
543  
544 =head2 io
545  
546 Title : io
547 Usage : $obj->io($newval)
548 Function: Gets a L<Bio::Root::IO> object
549 Returns : L<Bio::Root::IO>
550 Args : none
551  
552  
553 =cut
554  
555 1; # Needed to keep compiler happy