Tools/Run/RNAMotif.pm untested

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