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

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 49 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 246 CC 24 SCAR 38.0
CC
24
Cov
0%
_run
90.0
_setparams
90.0
version
12.0
CRAP 44.8
1 #
2 # BioPerl module for Bio::Tools::Run::ERPIN
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::ERPIN - Wrapper for local execution of the ERPIN suite of
17 programs.
18  
19 =head1 SYNOPSIS
20  
21 #run
22  
23 my @params = (
24 trset => 'BL.erpin',
25 region => [1, 10],
26 # Set up search strategy this way...
27 strategy => [ 'umask' => [1, 2],
28 'umask' => [1, 2, 3, 4],
29 'umask' => [1, 2, 3, 4, 5, 6],
30 'nomask',
31 'cutoff' => [0, 10, 15, 20]
32 ]
33 # or use a simple string...
34 #strategy => 'Ðumask 4 Ðadd 5 -nomask -cutoff 0 10 15',
35 pcw => 100
36 );
37  
38 my $factory = Bio::Tools::Run::ERPIN->new(-program =>'erpin',
39 @params);
40  
41 # Pass the factory a Bio::Seq object or a file name
42 # Returns a Bio::SearchIO object
43  
44 #my $search = $factory->run("B_sub.fas");
45 my $search = $factory->run($seq);
46 my @feat;
47 while (my $result = $searchio->next_result){
48 while(my $hit = $result->next_hit){
49 while (my $hsp = $hit->next_hsp){
50 print join("\t", ( $r->query_name,
51 $hit->name,
52 $hsp->hit->start,
53 $hsp->hit->end,
54 $hsp->meta,
55 $hsp->score,
56 )), "\n";
57 }
58 }
59 }
60  
61 =head1 DESCRIPTION
62  
63 =cut
64  
65 =head1 FEEDBACK
66  
67 =head2 Mailing Lists
68  
69 User feedback is an integral part of the evolution of this and other
70 Bioperl modules. Send your comments and suggestions preferably to one
71 of the Bioperl mailing lists. Your participation is much appreciated.
72  
73 bioperl-l@bioperl.org - General discussion
74 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
75  
76 =head2 Support
77  
78 Please direct usage questions or support issues to the mailing list:
79  
80 I<bioperl-l@bioperl.org>
81  
82 rather than to the module maintainer directly. Many experienced and
83 reponsive experts will be able look at the problem and quickly
84 address it. Please include a thorough description of the problem
85 with code and data examples if at all possible.
86  
87 =head2 Reporting Bugs
88  
89 Report bugs to the Bioperl bug tracking system to help us keep track
90 the bugs and their resolution. Bug reports can be submitted via the
91 web:
92  
93 http://redmine.open-bio.org/projects/bioperl/
94  
95 =head1 AUTHOR - Chris Fields
96  
97 Email: cjfields-at-uiuc-dot-edu
98  
99 =head1 CONTRIBUTORS
100  
101 cjfields-at-uiuc-dot-edu
102  
103 =head1 APPENDIX
104  
105 The rest of the documentation details each of the object
106 methods. Internal methods are usually preceded with a _
107  
108 =cut
109  
110 package Bio::Tools::Run::ERPIN;
111  
112 use strict;
113 use Bio::SeqIO;
114 use Bio::Root::Root;
115 use Bio::SearchIO;
116 use Bio::AlignIO;
117 use Bio::Tools::Run::WrapperBase;
118  
119 use base qw(Bio::Root::Root Bio::Tools::Run::WrapperBase);
120  
121 # will move parameters to each program, use this for _set_params
122 my %ERPIN_PROGS = (
123 cfgs => 1,
124 erpin => 1,
125 frandseq => 1,
126 mstat => 1,
127 sview => 1,
128 tstrip => 1,
129 epnstat => 1,
130 ev => 1,
131 mhistview => 1,
132 pview => 1,
133 tstat => 1,
134 tview => 1,
135 );
136  
137 my %ERPIN_SWITCHES = map {$_ => 1} qw(dmp smp fwd rev fwd+rev long short mute
138 warnings globstat locstat unifstat Eon Eoff hist chrono);
139  
140 # order is important here
141 my @ERPIN_PARAMS=qw(program model file strategy dmp smp fwd rev fwd+rev long
142 short mute warnings globstat locstat unifstat Eon Eoff hist seq1 nseq bgn
143 len logzero tablen chrono pcw hpcw spcw sumf tset);
144  
145 =head2 new
146  
147 Title : new
148 Usage : my $wrapper = Bio::Tools::Run::RNAMotif->new(@params)
149 Function: creates a new RNAMotif factory
150 Returns: Bio::Tools::Run::RNAMotif
151 Args : list of parameters
152 -tempfile => set tempfile flag (default 0)
153 -outfile_name => set file to send output to (default none)
154  
155 =cut
156  
157 sub new {
158 my ($class,@args) = @_;
159 my $self = $class->SUPER::new(@args);
160 my ($out, $tf) = $self->_rearrange([qw(OUTFILE_NAME TEMPFILE)], @args);
161 $self->io->_initialize_io();
162 if ($tf && !$out) {
163 my ($tfh, $outfile) = $self->io->tempfile(-dir=>$self->tempdir());
164 close($tfh);
165 undef $tfh;
166 $self->outfile_name($outfile);
167 } else {
168 $out ||= '';
169 $self->outfile_name($out);
170 }
171 $tf && $self->tempfile($tf);
172 $self->_set_from_args(\@args,
173 -methods => [@ERPIN_PARAMS],
174 -create => 1
175 );
176 return $self;
177 }
178  
179 =head2 program_name
180  
181 Title : program_name
182 Usage : $factory>program_name()
183 Function: holds the program name
184 Returns: string
185 Args : None
186  
187 =cut
188  
189 sub program_name {
190 my ($self) = shift;
191 return $self->program(@_);
192 }
193  
194 =head2 program_dir
195  
196 Title : program_dir
197 Usage : $factory->program_dir(@params)
198 Function: returns the program directory, obtained from ENV variable.
199 Returns: string
200 Args :
201  
202 =cut
203  
204 sub program_dir {
205 return Bio::Root::IO->catfile($ENV{ERPINDIR}) if $ENV{ERPINDIR};
206 }
207  
208 =head2 version
209  
210 Title : version
211 Usage : $v = $prog->version();
212 Function: Determine the version number of the program
213 Example :
214 Returns : float or undef
215 Args : none
216  
217 =cut
218  
219 sub version {
220 my ($self) = @_;
221 return undef unless $self->executable;
222 my $string = `erpin -h 2>&1`;
223 my $v;
224 if ($string =~ m{Version\s([\d.]+)}) {
225 $v = $1;
226 }
227 return $self->{'_progversion'} = $v || $string;
228 }
229  
230 =head2 run
231  
232 Title : run
233 Usage : $obj->run($seqFile)
234 Function: Runs ERPIN programs and returns Bio::SearchIO
235 Returns :
236 Args : Must pass Bio::PrimarySeqI's or file names
237  
238 =cut
239  
240 sub run {
241 my ($self,@seq) = @_;
242 $self->throw ("Must define 'db', pass a file name, or a list of Bio::PrimarySeqI objects")
243 if (!@seq);
244 if (ref $seq[0] && $seq[0]->isa("Bio::PrimarySeqI") ){# it is an object
245 my $infile1 = $self->_writeSeqFile(@seq);
246 return $self->_run($infile1);
247 } else {
248 return $self->_run(@seq);
249 }
250 }
251  
252 =head2 tempfile
253  
254 Title : tempfile
255 Usage : $obj->tempfile(1)
256 Function: Set tempfile flag. When set, writes output to a tempfile; this
257 is overridden by outfile_name() if set
258 Returns : Boolean setting (or undef if not set)
259 Args : [OPTIONAL] Boolean
260  
261 =cut
262  
263 sub tempfile {
264 my $self = shift;
265 return $self->{'_tempfile'} = shift if @_;
266 return $self->{'_tempfile'};
267 }
268  
269 =head2 _run
270  
271 Title : _run
272 Usage : $obj->_run()
273 Function: Internal(not to be used directly)
274 Returns :
275 Args :
276  
277 =cut
278  
279 sub _run {
280 my ($self,$file,$prog) = @_;
281 return unless $self->executable;
282 $self->io->_io_cleanup();
283 my ($str, $progname, $outfile) =
284 ($prog || $self->executable, $self->program_name, $self->outfile_name);
285 my $param_str = $self->_setparams($file);
286 $str .= " $param_str";
287 $self->debug("ERPIN command: $str\n");
288
289 # rnamotif => SearchIO object
290 # rmfmt -a => AlignIO object
291 # all others sent to outfile, tempfile, or STDERR (upon verbose = 1)
292
293 my $obj = ($progname eq 'erpin') ?
294 Bio::SearchIO->new(-verbose => $self->verbose,
295 -format => "erpin",
296 -version => $self->version,
297 -database => $file
298 ) :
299 undef;
300
301 my @args;
302 # file-based
303 if ($outfile) {
304 local $SIG{CHLD} = 'DEFAULT';
305 my $status = system($str);
306 if($status || !-e $outfile || -z $outfile ) {
307 my $error = ($!) ? "$! Status: $status" : "Status: $status";
308 $self->throw( "ERPIN call crashed: $error \n[command $str]\n");
309 return undef;
310 }
311 if ($obj && ref($obj)) {
312 $obj->file($outfile);
313 @args = (-file => $outfile);
314 }
315 # fh-based
316 } else {
317 open(my $fh,"$str |") || $self->throw("ERPIN call ($str) crashed: $?\n");
318 if ($obj && ref($obj)) {
319 $obj->fh($fh);
320 @args = (-fh => $fh);
321 } else {
322 # dump to debugging
323 my $io;
324 while(<$fh>) {$io .= $_;}
325 close($fh);
326 $self->debug($io);
327 return 1;
328 }
329 }
330 # initialize SearchIO/AlignIO...um...IO
331 # (since file/fh set post obj construction)
332 $obj->_initialize_io(@args) if $obj && ref($obj);
333 return $obj || 1;
334 }
335  
336 =head2 _setparams
337  
338 Title : _setparams
339 Usage : Internal function, not to be called directly
340 Function: creates a string of params to be used in the command string
341 Example :
342 Returns : string of params
343 Args :
344  
345 =cut
346  
347 sub _setparams {
348 my ($self, $file) = @_;
349 my $progname = $self->program_name;
350 # small sanity check
351 $self->throw("Unknown program: $progname") if
352 (!exists $ERPIN_PROGS{$progname} );
353 my $param_string;
354
355 my $outfile = ($self->outfile_name) ? ' > '.$self->outfile_name : '';
356 my ($tset, $st) = ($self->tset, $self->strategy);
357
358 $param_string = join " ", ($tset, $file, $st);
359 $self->debug("String : $param_string\n");
360
361 $self->throw("Must have both a training set and search strategy defined!")
362 if (!defined($tset) || !defined ($st));
363
364 my @params;
365 foreach my $attr (@ERPIN_PARAMS) {
366 next if $attr eq 'program' || $attr eq 'tset' || $attr eq 'strategy';
367 my $value = $self->$attr();
368 next unless ($attr eq 'file' || defined $value);
369 my $attr_key = '-'.$attr;
370 if (exists $ERPIN_SWITCHES{$attr}) {
371 push @params, $attr_key;
372 } else {
373 if ($attr eq 'file') {
374 push @params, $file;
375 } else {
376 push @params, $attr_key.' '.$value;
377 }
378 }
379 }
380
381 $param_string .= ' '.join ' ', @params;
382 $param_string .= $outfile if $outfile;
383
384 return $param_string;
385 }
386  
387 =head2 _writeSeqFile
388  
389 Title : _writeSeqFile
390 Usage : obj->_writeSeqFile($seq)
391 Function: Internal(not to be used directly)
392 Returns : writes passed Seq objects to tempfile, to be used as input
393 for program
394 Args :
395  
396 =cut
397  
398 sub _writeSeqFile {
399 my ($self,@seq) = @_;
400 my ($tfh,$inputfile) = $self->io->tempfile(-dir=>$self->tempdir);
401 my $in = Bio::SeqIO->new(-fh => $tfh , '-format' => 'fasta');
402 foreach my $s(@seq){
403 $in->write_seq($s);
404 }
405 $in->close();
406 $in = undef;
407 close($tfh);
408 undef $tfh;
409 return $inputfile;
410 }
411  
412 1;