rework tempfile handling to explicitly close a filehanle - assigning it to undef...
[bioperl-run.git] / Bio / Tools / Run / Tmhmm.pm
blob63e8a7f3e851887518d92ed53ff112f71a9db354
3 # Copyright Balamurugan Kumarasamy
5 # You may distribute this module under the same terms as perl itself
6 # POD documentation - main docs before the code
9 =head1 NAME
11 Bio::Tools::Run::Tmhmm - Object for identifying transmembrane helixes
12 in a given protein seequence.
14 =head1 SYNOPSIS
16 # Build a Tmhmm factory
18 # $paramfile is the full path to the seg binary file
20 my @params = ('PROGRAM',$paramfile);
21 my $factory = Bio::Tools::Run::Tmhmm->new($param);
23 # Pass the factory a Bio::Seq object
24 # @feats is an array of Bio::SeqFeature::Generic objects
26 my @feats = $factory->run($seq);
28 =head1 DESCRIPTION
30 Tmhmm is a program for identifying transmembrane helices in proteins
32 =head1 FEEDBACK
34 =head2 Mailing Lists
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to one
38 of the Bioperl mailing lists. Your participation is much appreciated.
40 bioperl-l@bioperl.org - General discussion
41 http://bio.perl.org/MailList.html - About the mailing lists
43 =head2 Reporting Bugs
45 Report bugs to the Bioperl bug tracking system to help us keep track
46 the bugs and their resolution. Bug reports can be submitted via
47 email or the web:
49 bioperl-bugs@bio.perl.org
50 http://bio.perl.org/bioperl-bugs/
52 =head1 AUTHOR - Bala
54 Email savikalpa@fugu-sg.org
57 =head1 APPENDIX
59 The rest of the documentation details each of the object
60 methods. Internal methods are usually preceded with a _
62 =cut
64 package Bio::Tools::Run::Tmhmm;
66 use vars qw($AUTOLOAD @ISA $PROGRAM $PROGRAMDIR
67 $PROGRAMNAME @TMHMM_PARAMS %OK_FIELD);
68 use strict;
69 use Bio::SeqIO;
70 use Bio::Root::Root;
71 use Bio::Root::IO;
72 use Bio::Tools::Tmhmm;
73 use Bio::Tools::Run::WrapperBase;
75 @ISA = qw(Bio::Root::Root Bio::Tools::Run::WrapperBase);
77 BEGIN {
78 $PROGRAMNAME = 'tmhmm' . ($^O =~ /mswin/i ?'.exe':'');
80 if (defined $ENV{TMHMMDIR}) {
81 $PROGRAMDIR = $ENV{TMHMMDIR} || '';
82 $PROGRAM = Bio::Root::IO->catfile($PROGRAMDIR,
83 "tmhmm".($^O =~ /mswin/i ?'.exe':''));
85 else {
86 $PROGRAM = 'tmhmm';
88 @TMHMM_PARAMS=qw(PROGRAM VERBOSE);
89 foreach my $attr ( @TMHMM_PARAMS)
90 { $OK_FIELD{$attr}++; }
93 =head2 program_name
95 Title : program_name
96 Usage : $factory>program_name()
97 Function: holds the program name
98 Returns: string
99 Args : None
101 =cut
103 sub program_name {
104 return 'tmhmm';
107 =head2 program_dir
109 Title : program_dir
110 Usage : $factory->program_dir(@params)
111 Function: returns the program directory, obtiained from ENV variable.
112 Returns: string
113 Args :
115 =cut
117 sub program_dir {
118 return Bio::Root::IO->catfile($ENV{TMHMMDIR});
121 sub AUTOLOAD {
122 my $self = shift;
123 my $attr = $AUTOLOAD;
124 $attr =~ s/.*:://;
125 $attr = uc $attr;
126 $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
127 $self->{$attr} = shift if @_;
128 return $self->{$attr};
131 =head2 new
133 Title : new
134 Usage : $rm->new(@params)
135 Function: creates a new Tmhmm factory
136 Returns: Bio::Tools::Run::Tmhmm
137 Args :
139 =cut
141 sub new {
142 my ($class,@args) = @_;
143 my $self = $class->SUPER::new(@args);
145 my ($attr, $value);
146 while (@args) {
147 $attr = shift @args;
148 $value = shift @args;
149 next if( $attr =~ /^-/ ); # don't want named parameters
150 $self->$attr($value);
152 return $self;
155 =head2 predict_protein_features
157 Title : predict_protein_features()
158 Usage : DEPRECATED Use $obj->run($seq) instead
159 Function: Runs Tmhmm and creates an array of featrues
160 Returns : An array of Bio::SeqFeature::Generic objects
161 Args : A Bio::PrimarySeqI
163 =cut
165 sub predict_protein_features{
166 return shift->run(@_);
169 =head2 run
171 Title : run()
172 Usage : $obj->run($seq)
173 Function: Runs Tmhmm and creates an array of featrues
174 Returns : An array of Bio::SeqFeature::Generic objects
175 Args : A Bio::PrimarySeqI
177 =cut
179 sub run{
180 my ($self,$seq) = @_;
181 my @feats;
183 if (ref($seq) ) {# it is an object
185 if (ref($seq) =~ /GLOB/) {
186 $self->throw("cannot use filehandle");
189 my $infile1 = $self->_writeSeqFile($seq);
191 $self->_input($infile1);
193 @feats = $self->_run();
194 unlink $infile1;
197 else {
198 #The clone object is not a seq object but a file. Perhaps
199 #should check here or before if this file is fasta format...if
200 #not die Here the file does not need to be created or
201 #deleted. Its already written and may be used by other
202 #runnables.
204 $self->_input($seq);
206 @feats = $self->_run();
210 return @feats;
215 =head2 _input
217 Title : _input
218 Usage : obj->_input($seqFile)
219 Function: Internal(not to be used directly)
220 Returns :
221 Args :
223 =cut
225 sub _input() {
226 my ($self,$infile1) = @_;
227 if (defined $infile1){
229 $self->{'input'}=$infile1;
231 return $self->{'input'};
234 =head2 _run
236 Title : _run
237 Usage : $obj->_run()
238 Function: Internal(not to be used directly)
239 Returns : An array of Bio::SeqFeature::Generic objects
240 Args :
242 =cut
244 sub _run {
245 my ($self)= @_;
247 my ($tfh1,$outfile) = $self->io->tempfile(-dir=>$self->tempdir());
248 my $str =$self->executable." ".$self->_input." > ".$outfile;
249 my $status = system($str);
250 $self->throw( "Tmhmm call ($str) crashed: $? \n") unless $status==0;
252 my $filehandle;
253 if (ref ($outfile) !~ /GLOB/) {
254 open (TMHMM, "<".$outfile) or $self->throw ("Couldn't open file ".$outfile.": $!\n");
255 $filehandle = \*TMHMM;
257 else {
258 $filehandle = $outfile;
261 my $tmhmm_parser = Bio::Tools::Tmhmm->new(-fh=>$filehandle);
263 my @tmhmm_feat;
265 while(my $tmhmm_feat = $tmhmm_parser->next_result){
267 push @tmhmm_feat, $tmhmm_feat;
269 # free resources
270 $self->cleanup();
271 unlink $outfile;
272 close($tfh1);
273 undef $tfh1;
274 return @tmhmm_feat;
278 =head2 _writeSeqFile
280 Title : _writeSeqFile
281 Usage : obj->_writeSeqFile($seq)
282 Function: Internal(not to be used directly)
283 Returns :
284 Args :
286 =cut
288 sub _writeSeqFile{
289 my ($self,$seq) = @_;
290 my ($tfh,$inputfile) = $self->io->tempfile(-dir=>$self->tempdir());
291 my $in = Bio::SeqIO->new(-fh => $tfh , '-format' => 'Fasta');
292 $in->write_seq($seq);
293 close($tfh);
294 undef $tfh;
295 return $inputfile;