File Coverage

blib/lib/Bio/Tools/Run/Glimmer.pm
Criterion Covered Total %
statement 43 106 40.5
branch 6 34 17.6
condition n/a
subroutine 13 17 76.4
pod 5 5 100.0
total 67 162 41.3


line stmt bran cond sub pod time code
1             # BioPerl module for Bio::Tools::Run::Glimmer
2             #
3             # Please direct questions and support issues to
4             #
5             # Cared for by Bioperl
6             #
7             # Copyright Bioperl, Mark Johnson
8             #
9             # Special thanks to Chris Fields, 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::Glimmer - Wrapper for local execution of Glimmer,
18             GlimmerM and GlimmerHMM.
19              
20             =head1 SYNOPSIS
21              
22             # glimmer2
23             my $factory =
24             Bio::Tools::Run::Glimmer->new('-program' => 'glimmer3',
25             '-model' => 'model.icm');
26             # glimmer3
27             my $factory =
28             Bio::Tools::Run::Glimmer->new('-program' => 'glimmer2',
29             '-model' => 'model.icm');
30             # glimmerm
31             my $factory =
32             Bio::Tools::Run::Glimmer->new('-program' => 'glimmerm');
33              
34             # glimmerHMM
35             my $factory =
36             Bio::Tools::Run::Glimmer->new('-program' => 'glimmerHMM');
37              
38             # Pass the factory Bio::Seq objects
39             # returns a Bio::Tools::Glimmer object
40             my $glimmer = $factory->run($seq);
41             or
42             my $glimmer = $factor->run(@seq);
43              
44             =head1 DESCRIPTION
45              
46             Wrapper module for the Glimmer family of programs. Should work with
47             all currently available flavors: Glimmer, GlimmerM and GlimmerHMM.
48             However, only Glimmer 2.X and 3.X have been tested.
49              
50             Glimmer is open source and available at
51             L.
52              
53             GlimmerM is open source and available at
54             L.
55              
56             GlimmerHMM is open source and available at
57             L.
58              
59             Note that Glimmer 2.X will only process the first
60             sequence in a fasta file (if you run() more than one
61             sequence at a time, only the first will be processed).
62              
63             Note that Glimmer 3.X produces two output files. This
64             module only passes the .predict file to Bio::Tools::Glimmer.
65              
66             =head1 FEEDBACK
67              
68             =head2 Mailing Lists
69              
70             User feedback is an integral part of the evolution of this and other
71             Bioperl modules. Send your comments and suggestions preferably to one
72             of the Bioperl mailing lists. Your participation is much appreciated.
73              
74             bioperl-l@bioperl.org - General discussion
75             http://bioperl.org/wiki/Mailing_lists - About the mailing lists
76              
77             =head2 Support
78              
79             Please direct usage questions or support issues to the mailing list:
80              
81             I
82              
83             rather than to the module maintainer directly. Many experienced and
84             reponsive experts will be able look at the problem and quickly
85             address it. Please include a thorough description of the problem
86             with code and data examples if at all possible.
87              
88             =head2 Reporting Bugs
89              
90             Report bugs to the Bioperl bug tracking system to help us keep track
91             the bugs and their resolution. Bug reports can be submitted via the
92             web:
93              
94             http://redmine.open-bio.org/projects/bioperl/
95              
96             =head1 AUTHOR - Mark Johnson
97              
98             Email: johnsonm-at-gmail-dot-com
99              
100             =head1 APPENDIX
101              
102             The rest of the documentation details each of the object
103             methods. Internal methods are usually preceded with a _
104              
105             =cut
106              
107             package Bio::Tools::Run::Glimmer;
108              
109 2     2   248356 use strict;
  2         3  
  2         46  
110 2     2   7 use warnings;
  2         2  
  2         42  
111              
112 2     2   921 use Bio::SeqIO;
  2         76293  
  2         20  
113 2     2   52 use Bio::Root::Root;
  2         3  
  2         4  
114 2     2   877 use Bio::Tools::Run::WrapperBase;
  2         4  
  2         39  
115 2     2   933 use Bio::Tools::Glimmer;
  2         97557  
  2         21  
116 2     2   991 use English;
  2         2534  
  2         8  
117 2     2   723 use IPC::Run; # Should be okay on WIN32 (See IPC::Run Docs)
  2         2  
  2         72  
118              
119 2     2   7 use base qw(Bio::Root::Root Bio::Tools::Run::WrapperBase);
  2         2  
  2         1866  
120              
121             our @params = (qw(program model));
122              
123             our @glimmer2_params = (qw(C L g i o p q s t w));
124             our @glimmer2_switches = (qw(M X f l r));
125              
126             our @glimmer3_params = (qw(A C E L M P Z b g i t z));
127             our @glimmer3_switches = (qw(X f l o q r));
128              
129             our @glimmerM_params = (qw(d g t));
130             our @glimmerM_switches = (qw(5 3 f r s));
131              
132             our @glimmerHMM_params = (qw(d n p));
133             our @glimmerHMM_switches = (qw(f h v));
134              
135             =head2 program_name
136              
137             Title : program_name
138             Usage : $factory>program_name()
139             Function: gets/sets the program name
140             Returns: string
141             Args : string
142              
143             =cut
144              
145             sub program_name {
146            
147 12     12 1 39 my ($self, $val) = @_;
148            
149 12 50       15 $self->program($val) if $val;
150            
151 12         232 return $self->program();
152              
153             }
154              
155             =head2 program_dir
156              
157             Title : program_dir
158             Usage : $factory->program_dir()
159             Function: gets/sets the program dir
160             Returns: string
161             Args : string
162              
163             =cut
164              
165             sub program_dir {
166            
167 6     6 1 5 my ($self, $val) = @_;
168            
169 6 50       10 $self->{'_program_dir'} = $val if $val;
170            
171 6         17 return $self->{'_program_dir'};
172            
173             }
174              
175             =head2 model
176              
177             Title : model
178             Usage : $factory>model()
179             Function: gets/sets the name of the model (icm) file
180             Returns: string
181             Args : string
182              
183             =cut
184              
185             sub model {
186            
187 4     4 1 4265 my ($self, $val) = @_;
188            
189 4 100       42 $self->{'_model'} = $val if $val;
190            
191 4         12 return $self->{'_model'};
192              
193             }
194              
195             =head2 new
196              
197             Title : new
198             Usage : $glimmer->new(@params)
199             Function: creates a new Glimmer factory
200             Returns: Bio::Tools::Run::Glimmer
201             Args :
202              
203             =cut
204              
205             sub new {
206            
207 2     2 1 209 my ($class,@args) = @_;
208 2         21 my $self = $class->SUPER::new(@args);
209            
210 2         80 $self->io->_initialize_io();
211              
212 2         69 $self->_set_from_args(
213             \@args,
214             -methods => [
215             @params,
216             @glimmer2_params,
217             @glimmer2_switches,
218             @glimmer3_params,
219             @glimmer3_switches,
220             @glimmerM_params,
221             @glimmerM_switches,
222             @glimmerHMM_params,
223             @glimmerHMM_switches
224             ],
225             -create => 1,
226             );
227              
228 2 50       93 unless (defined($self->program())) {
229 0         0 $self->throw('Must specify program');
230             }
231              
232 2 50       19 unless (defined($self->model())) {
233 0         0 $self->throw('Must specify model');
234             }
235            
236 2         5 return $self;
237            
238             }
239              
240             =head2 run
241              
242             Title : run
243             Usage : $obj->run($seq_file)
244             Function: Runs Glimmer/GlimmerM/GlimmerHMM
245             Returns : A Bio::Tools::Glimmer object
246             Args : An array of Bio::PrimarySeqI objects
247              
248             =cut
249              
250             sub run{
251            
252 0     0 1   my ($self, @seq) = @_;
253              
254 0 0         unless (@seq) {
255 0           $self->throw("Must supply at least one Bio::PrimarySeqI");
256             }
257            
258 0           foreach my $seq (@seq) {
259            
260 0 0         unless ($seq->isa('Bio::PrimarySeqI')) {
261 0           $self->throw("Object does not implement Bio::PrimarySeqI");
262             }
263            
264             }
265              
266 0           my $program_name = $self->program_name();
267 0           my $file_name = $self->_write_seq_file(@seq);
268              
269 0           my @run_args = ( $file_name );
270              
271             # Glimmer 2.X ignores sequences after the first in a fasta file
272             # Glimmer 3.X will process multiple sequences at once
273 0 0         if ($program_name eq 'glimmer2') {
274 0 0         if (@seq > 1) {
275 0           $self->warn("Program $program_name processes one sequence at a time");
276             }
277 0           push @run_args, $seq[0]->display_id();
278 0           push @run_args, $seq[0]->length();
279             }
280            
281 0           return $self->_run(@run_args);
282            
283             }
284              
285             =head2 _run
286              
287             Title : _run
288             Usage : $obj->_run()
289             Function: Internal(not to be used directly)
290             Returns : An instance of Bio::Tools::Glimmer
291             Args : file name, sequence identifier (optional)
292              
293             =cut
294              
295             sub _run {
296            
297 0     0     my ($self, $seq_file_name, $seq_id, $seq_length) = @_;
298            
299 0           my @cmd = (
300             $self->executable(),
301             $seq_file_name,
302             $self->model(),
303             split(/\s+/, $self->_setparams()),
304             );
305              
306 0           my $cmd = join(' ', @cmd);
307 0           $self->debug("Glimmer Command = $cmd");
308            
309 0           my $program_name = $self->program_name();
310 0           my ($output_fh, $output_file_name, $detail_file_name);
311 0           my ($program_stdout, $program_stderr);
312            
313 0           my @ipc_args = (\@cmd, \undef);
314            
315             # No STDOUT option for glimmer3, it takes a
316             # 'tag' argument, and outputs tag.predict and
317             # tag.detail. It seems that tag can be a path,
318             # which is handy.
319 0 0         if ($program_name eq 'glimmer3') {
320            
321 0           my $temp_dir = $self->tempdir();
322 0           my $glimmer3_tag = "$temp_dir/glimmer3";
323              
324 0           push @cmd, $glimmer3_tag;
325 0           $output_file_name = "$glimmer3_tag.predict";
326 0           $detail_file_name = "$glimmer3_tag.detail";
327 0           push @ipc_args, \$program_stdout, \$program_stderr;
328            
329             }
330             else {
331            
332 0           ($output_fh, $output_file_name) = $self->io->tempfile(-dir=>$self->tempdir());
333 0           close($output_fh);
334 0           push @ipc_args, '>', $output_file_name;
335 0           push @ipc_args, '2>', \$program_stderr;
336            
337             }
338              
339             # Run the program via IPC::Run so:
340             # 1) The console doesn't get cluttered up with the program's STDERR/STDOUT
341             # 2) We don't have to embed STDERR/STDOUT redirection in $cmd
342             # 3) We don't have to deal with signal handling (IPC::Run should take care
343             # of everything automagically.
344              
345 0           eval {
346 0 0         IPC::Run::run(@ipc_args) || die $CHILD_ERROR;;
347             };
348              
349 0 0         if ($EVAL_ERROR) {
350 0           $self->throw("Glimmer call crashed: $EVAL_ERROR");
351             }
352              
353 0 0         $self->debug(join("\n", 'Glimmer STDOUT:', $program_stdout)) if $program_stdout;
354 0 0         $self->debug(join("\n", 'Glimmer STDERR:', $program_stderr)) if $program_stderr;
355            
356 0           my %parser_args = (-file => $output_file_name);
357              
358             # Pass along $seq_id and $seq_length if they were provided
359             # (only should be for glimmer2).
360 0 0         if (defined($seq_id)) { $parser_args{-seqname } = $seq_id; }
  0            
361 0 0         if (defined($seq_length)) { $parser_args{-seqlength} = $seq_length; }
  0            
362              
363             # Pass along the name of extra output file, with handy information about
364             # sequence lengths (only produced by glimmer3)
365 0 0         if (defined($detail_file_name)) { $parser_args{-detail} = $detail_file_name; }
  0            
366            
367 0           return Bio::Tools::Glimmer->new(%parser_args);
368            
369             }
370              
371             sub _setparams {
372              
373 0     0     my ($self) = @_;
374              
375 0           my $param_string = $self->SUPER::_setparams(
376             -params => [
377             @glimmer2_params,
378             @glimmer3_params,
379             @glimmerM_params,
380             @glimmerHMM_params,
381             ],
382             -switches => [
383             @glimmer2_switches,
384             @glimmer2_switches,
385             @glimmerM_switches,
386             @glimmerHMM_switches,
387             ],
388             -dash => 1
389              
390             );
391            
392             # Kill leading and trailing whitespace
393 0           $param_string =~ s/^\s+//g;
394 0           $param_string =~ s/\s+$//g;
395              
396 0           return $param_string;
397              
398             }
399              
400             =head2 _write_seq_file
401              
402             Title : _write_seq_file
403             Usage : obj->_write_seq_file($seq) or obj->_write_seq_file(@seq)
404             Function: Internal(not to be used directly)
405             Returns : Name of a temp file containing program output
406             Args : One or more Bio::PrimarySeqI objects
407              
408             =cut
409              
410             sub _write_seq_file {
411              
412 0     0     my ($self, @seq) = @_;
413            
414 0           my ($fh, $file_name) = $self->io->tempfile(-dir=>$self->tempdir());
415 0           my $out = Bio::SeqIO->new(-fh => $fh , '-format' => 'Fasta');
416              
417 0           foreach my $seq (@seq){
418 0           $out->write_seq($seq);
419             }
420              
421 0           close($fh);
422 0           $out->close();
423            
424 0           return $file_name;
425              
426             }
427              
428             1;