blib/lib/Bio/Tools/Run/RNAMotif.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 52 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 285 CC 31 SCAR 40.0
CC
31
Cov
0%
_run
156.0
_setparams
72.0
version
20.0
CRAP 54.7
1 # $Id$
2 #
3 # BioPerl module for Bio::Tools::Run::RNAMotif
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Chris Fields
8 #
9 # Copyright Chris Fields
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::RNAMotif - Wrapper for local execution of rnamotif, rm2ct,
18 rmfmt, rmprune
19  
20 =head1 SYNOPSIS
21  
22 #run rnamotif|rmfmt|rm2ct
23  
24 my @params = (
25 descr => 'pyrR.descr',
26 fmt => 'gb',
27 setvar => 'ctx_maxlen=20',
28 context => 1,
29 sh => 1,
30 );
31  
32 my $factory = Bio::Tools::Run::RNAMotif->new(-program =>'rnamotif',
33 -prune => 1,
34 @params);
35  
36 # Pass the factory a Bio::Seq object or a file name
37 # Returns a Bio::SearchIO object
38  
39 #my $searchio = $factory->run("B_sub.gb");
40 my $searchio = $factory->run($seq);
41 while (my $result = $searchio->next_result){
42 while(my $hit = $result->next_hit){
43 while (my $hsp = $hit->next_hsp){
44 print join("\t", ( $r->query_name,
45 $hit->name,
46 $hsp->hit->start,
47 $hsp->hit->end,
48 $hsp->meta,
49 $hsp->score,
50 )), "\n";
51 }
52 }
53 }
54  
55 # Pass a finished report through rmfmt (-a format only)
56 # Returns Bio::AlignIO object
57  
58 my $aio = Bio::AlignIO->new(-file=>"rna.msf",-format=>'msf');
59 my $factory = Bio::Tools::Run::RNAMotif->new('program'=>'rmfmt',
60 'a' => 1);
61 my $alnin = $factory->run('trna.rnamotif');
62  
63 my $aln = $alnin->next_aln;
64  
65 $aio->write_aln($aln);
66  
67 =head1 DESCRIPTION
68  
69 Wrapper module for Tom Macke and David Cases's RNAMotif suite of programs. This
70 allows running of rnamotif, rmprune, rm2ct, and rmfmt. Binaries are available at
71 http://www.scripps.edu/mb/case/casegr-sh-3.5.html.
72  
73 This wrapper allows for one to save output to an optional named file or tempfile
74 using the '-outfile_name' or '-tempfile' parameters; this is primarily for
75 saving output from the rm2ct program, which currently does not have a parser
76 available. If both a named output file and tempfile flag are set, the output
77 file name is used. The default setting is piping output into a filehandle for
78 parsing (or output to STDERR, for rm2ct which requires '-verbose' set to 1).
79  
80 WARNING: At this time, there is very little checking of parameter settings, so
81 one could have an error if setting the worng parameter for a program. Future
82 versions will likely add some error checking.
83  
84 =head1 NOTES ON PROGRAM PARAMETERS
85  
86 All program parameters are currently supported. Of note, the 'D' parameter, used
87 for setting the value of a variable to a value, is changed to 'set_var' to avoid
88 name collisions with 'd' (used for dumping internal data structures).
89  
90 =head1 FEEDBACK
91  
92 =head2 Mailing Lists
93  
94 User feedback is an integral part of the evolution of this and other
95 Bioperl modules. Send your comments and suggestions preferably to one
96 of the Bioperl mailing lists. Your participation is much appreciated.
97  
98 bioperl-l@bioperl.org - General discussion
99 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
100  
101 =head2 Support
102  
103 Please direct usage questions or support issues to the mailing list:
104  
105 I<bioperl-l@bioperl.org>
106  
107 rather than to the module maintainer directly. Many experienced and
108 reponsive experts will be able look at the problem and quickly
109 address it. Please include a thorough description of the problem
110 with code and data examples if at all possible.
111  
112 =head2 Reporting Bugs
113  
114 Report bugs to the Bioperl bug tracking system to help us keep track
115 the bugs and their resolution. Bug reports can be submitted via the
116 web:
117  
118 http://redmine.open-bio.org/projects/bioperl/
119  
120 =head1 AUTHOR - Chris Fields
121  
122 Email: cjfields-at-uiuc-dot-edu
123  
124 =head1 CONTRIBUTORS
125  
126 cjfields-at-uiuc-dot-edu
127  
128 =head1 APPENDIX
129  
130 The rest of the documentation details each of the object
131 methods. Internal methods are usually preceded with a _
132  
133 =cut
134  
135 package Bio::Tools::Run::RNAMotif;
136  
137 use strict;
138 use Bio::SeqIO;
139 use Bio::Root::Root;
140 use Bio::SearchIO;
141 use Bio::AlignIO;
142 use Bio::Tools::Run::WrapperBase;
143  
144 use base qw(Bio::Root::Root Bio::Tools::Run::WrapperBase);
145  
146 # will move parameters to each program, use this for _set_params
147 my %RNAMOTIF_PROGS =(
148 rnamotif => [qw(c d h N O p s v context sh setvar I xdfname pre post
149 descr xdescr fmt fmap )],
150 rm2ct => [qw(t)],
151 rmfmt => [qw(a l la smax td)],
152 rmprune => [] # no params
153 );
154  
155 my %RNAMOTIF_SWITCHES = map {$_ => 1} qw(c d h p s v l a la context sh);
156  
157 # order is important here
158 my @RNAMOTIF_PARAMS=qw(program prune c sh N d h p s v context setvar O I
159 xdfname pre post descr xdescr fmt fmap l a la t);
160  
161 =head2 new
162  
163 Title : new
164 Usage : my $wrapper = Bio::Tools::Run::RNAMotif->new(@params)
165 Function: creates a new RNAMotif factory
166 Returns: Bio::Tools::Run::RNAMotif
167 Args : list of parameters
168 -tempfile => set tempfile flag (default 0)
169 -outfile_name => set file to send output to (default none)
170 -prune => set rmprune postprocess flag (default 0)
171  
172 =cut
173  
174 sub new {
175 my ($class,@args) = @_;
176 my $self = $class->SUPER::new(@args);
177 my ($out, $tf) = $self->_rearrange([qw(OUTFILE_NAME TEMPFILE)], @args);
178 $self->io->_initialize_io();
179 if ($tf && !$out) {
180 my ($tfh, $outfile) = $self->io->tempfile(-dir=>$self->tempdir());
181 close($tfh);
182 undef $tfh;
183 $self->outfile_name($outfile);
184 } else {
185 $out ||= '';
186 $self->outfile_name($out);
187 }
188 $tf && $self->tempfile($tf);
189 $self->_set_from_args(\@args,
190 -methods => [@RNAMOTIF_PARAMS],
191 -create => 1
192 );
193 return $self;
194 }
195  
196 =head2 program_name
197  
198 Title : program_name
199 Usage : $factory>program_name()
200 Function: holds the program name
201 Returns: string
202 Args : None
203  
204 =cut
205  
206 sub program_name {
207 my ($self) = shift;
208 return $self->program(@_);
209 }
210  
211 =head2 program_dir
212  
213 Title : program_dir
214 Usage : $factory->program_dir(@params)
215 Function: returns the program directory, obtained from ENV variable.
216 Returns: string
217 Args :
218  
219 =cut
220  
221 sub program_dir {
222 return Bio::Root::IO->catfile($ENV{RNAMOTIFDIR}) if $ENV{RNAMOTIFDIR};
223 }
224  
225 =head2 version
226  
227 Title : version
228 Usage : $v = $prog->version();
229 Function: Determine the version number of the program
230 Example :
231 Returns : float or undef
232 Args : none
233  
234 =cut
235  
236 sub version {
237 my ($self) = @_;
238 return undef unless $self->executable;
239 return $self->{'_progversion'} if $self->{'_progversion'};
240 my $string = `rnamotif -v 2>&1`;
241 my $v;
242 if ($string =~ m{([\d.]+)}) {
243 $v = $1;
244 }
245 return $self->{'_progversion'} = $v || $string;
246 }
247  
248 =head2 run
249  
250 Title : run
251 Usage : $obj->run($seqFile)
252 Function: Runs RNAMotif programs, returns Bio::SearchIO/Bio::AlignIO
253 Returns : Depends on program:
254 'rnamotif' - returns Bio::SearchIO
255 'rmfmt -a' - returns Bio::AlignIO
256 all others - sends output to outfile, tempfile, STDERR
257  
258 Use search() (for Bio::SearchIO stream) or get_AlignIO() (for
259 Bio::AlignIO stream) for a uniform Bioperl object interface.
260  
261 Args : A Bio::PrimarySeqI or file name
262 Note : This runs any RNAMotif program set via program()
263  
264 =cut
265  
266 sub run {
267 my ($self,@seq) = @_;
268 $self->throw ("Must pass a file name or a list of Bio::PrimarySeqI objects")
269 if (!@seq);
270 if (ref $seq[0] && $seq[0]->isa("Bio::PrimarySeqI") ){# it is an object
271 my $infile1 = $self->_writeSeqFile(@seq);
272 return $self->_run($infile1);
273 } else {
274 return $self->_run(@seq);
275 }
276 }
277  
278 =head2 search
279  
280 Title : search
281 Usage : $searchio = $obj->search($seqFile)
282 Function: Runs 'rnamotif' on seqs, returns Bio::SearchIO
283 Returns : A Bio::SearchIO
284 Args : A Bio::PrimarySeqI or file name
285 Note : Runs 'rnamotif' only, regardless of program setting; all other
286 parameters loaded
287  
288 =cut
289  
290 sub search {
291 my ($self,@seq) = @_;
292 $self->throw ("Must pass a file name or a list of Bio::PrimarySeqI objects")
293 if (!@seq);
294 if (ref $seq[0] && $seq[0]->isa("Bio::PrimarySeqI") ){# it is an object
295 my $infile1 = $self->_writeSeqFile(@seq);
296 return $self->_run($infile1);
297 } else {
298 return $self->_run(@seq);
299 }
300 }
301  
302 =head2 get_AlignIO
303  
304 Title : get_AlignIO
305 Usage : $aln = $obj->get_AlignIO($seqFile)
306 Function: Runs 'rmfmt -a' on file, returns Bio::AlignIO
307 Returns : A Bio::AlignIO
308 Args : File name
309 Note : Runs 'rmfmt -a' only, regardless of program setting; only file
310 name and outfile (if any) are set
311  
312 =cut
313  
314 sub get_AlignIO {
315 my ($self,@seq) = @_;
316 $self->throw ("Must pass a file name")
317 if (!@seq && ref($seq[0]));
318 return $self->_run(@seq);
319 }
320  
321 =head2 tempfile
322  
323 Title : tempfile
324 Usage : $obj->tempfile(1)
325 Function: Set tempfile flag. When set, writes output to a tempfile; this
326 is overridden by outfile_name() if set
327 Returns : Boolean setting (or undef if not set)
328 Args : [OPTIONAL] Boolean
329  
330 =cut
331  
332 sub tempfile {
333 my $self = shift;
334 return $self->{'_tempfile'} = shift if @_;
335 return $self->{'_tempfile'};
336 }
337  
338 =head2 prune
339  
340 Title : prune
341 Usage : $obj->prune(1)
342 Function: Set rmprune flag. When set, follows any searches with a call to
343 rmprune (this deletes some redundant sequence hits)
344 Returns : Boolean setting (or undef if not set)
345 Args : [OPTIONAL] Boolean
346  
347 =cut
348  
349 sub prune {
350 my $self = shift;
351 return $self->{'_prune'} = shift if @_;
352 return $self->{'_prune'};
353 }
354  
355 =head2 _run
356  
357 Title : _run
358 Usage : $obj->_run()
359 Function: Internal(not to be used directly)
360 Returns :
361 Args :
362  
363 =cut
364  
365 sub _run {
366 my ($self,$file,$prog)= @_;
367 return unless $self->executable;
368 $self->io->_io_cleanup();
369 my ($str, $progname, $outfile) =
370 ($prog || $self->executable, $self->program_name, $self->outfile_name);
371 my $param_str = $self->_setparams($file);
372 my $descr = ($self->can('descr')) ? $self->descr :
373 ($self->can('xdescr')) ? $self->xdescr :
374 $self->throw("Must have a descriptor present!");
375 $str .= " $param_str";
376 $self->debug("RNAMotif command: $str\n");
377
378 # rnamotif => SearchIO object
379 # rmfmt -a => AlignIO object
380 # all others sent to outfile, tempfile, or STDERR (upon verbose = 1)
381
382 my $obj = ($progname eq 'rnamotif' || $progname eq 'rmprune' ) ?
383 Bio::SearchIO->new(-verbose => $self->verbose,
384 -format => "rnamotif",
385 -version => $self->version,
386 -database => $file,
387 -model => $descr) :
388 ($progname eq 'rmfmt' && $self->can('a') && $self->a) ?
389 Bio::AlignIO->new(-verbose => $self->verbose, -format =>'fasta') :
390 undef;
391
392 my @args;
393 # file-based
394 if ($outfile) {
395 local $SIG{CHLD} = 'DEFAULT';
396 my $status = system($str);
397 if($status || !-e $outfile ) {
398 my $error = ($!) ? "$! Status: $status" : "Status: $status";
399 $self->throw( "RNAMotif call crashed: $error \n[command $str]\n");
400 return undef;
401 }
402 if ($obj && ref($obj)) {
403 $obj->file($outfile);
404 @args = (-file => $outfile);
405 }
406 # fh-based
407 } else {
408 open(my $fh,"$str |") || $self->throw("RNAMotif call ($str) crashed: $?\n");
409 if ($obj && ref($obj)) {
410 $obj->fh($fh);
411 @args = (-fh => $fh);
412 } else {
413 # dump to debugging
414 my $io;
415 while(<$fh>) {$io .= $_;}
416 close($fh);
417 $self->debug($io);
418 return 1;
419 }
420 }
421 # initialize SearchIO/AlignIO...um...IO
422 # (since file/fh set post obj construction)
423 $obj->_initialize_io(@args) if $obj && ref($obj);
424 return $obj || 1;
425 }
426  
427 =head2 _setparams
428  
429 Title : _setparams
430 Usage : Internal function, not to be called directly
431 Function: creates a string of params to be used in the command string
432 Example :
433 Returns : string of params
434 Args :
435  
436 =cut
437  
438 sub _setparams {
439 my ($self, $file) = @_;
440 my $progname = $self->program_name;
441 # small sanity check
442 $self->throw("Unknown program: $progname") if
443 (!exists $RNAMOTIF_PROGS{$progname} );
444  
445 my $param_string;
446 my $outfile = ($self->outfile_name) ? ' > '.$self->outfile_name : '';
447
448 my @params;
449 foreach my $attr (@RNAMOTIF_PARAMS) {
450 next if ($attr =~/PROGRAM|DB|PRUNE/i);
451 my $value = $self->$attr();
452 next unless (defined $value);
453 my $attr_key = '-'.$attr;
454 if (exists $RNAMOTIF_SWITCHES{$attr}) {
455 push @params, $attr_key;
456 } else {
457 if ($attr eq 'setvar') {
458 push @params, '-D'.$value;
459 } else {
460 push @params, $attr_key.' '.$value;
461 }
462 }
463 }
464
465 $param_string = join ' ', @params;
466 $param_string .= ' '.$file;
467  
468 if ($self->prune && $self->program_name eq 'rnamotif') {
469 $param_string .= ' | rmprune';
470 }
471  
472 $param_string .= $outfile;
473
474 return $param_string;
475 }
476  
477 =head2 _writeSeqFile
478  
479 Title : _writeSeqFile
480 Usage : obj->_writeSeqFile($seq)
481 Function: Internal(not to be used directly)
482 Returns : writes passed Seq objects to tempfile, to be used as input
483 for program
484 Args :
485  
486 =cut
487  
488 sub _writeSeqFile {
489 my ($self,@seq) = @_;
490 my ($tfh,$inputfile) = $self->io->tempfile(-dir=>$self->tempdir);
491 my $in = Bio::SeqIO->new(-fh => $tfh , '-format' => 'fasta');
492 foreach my $s(@seq){
493 $in->write_seq($s);
494 }
495 $in->close();
496 $in = undef;
497 close($tfh);
498 undef $tfh;
499 return $inputfile;
500 }
501  
502 1;