1#
2# Please direct questions and support issues to <bioperl-l@bioperl.org>
3#
4# Cared for by Shawn Hoon
5#
6# Copyright Shawn Hoon
7#
8# You may distribute this module under the same terms as perl itself
9#
10# POD documentation - main docs before the code
11
12=head1 NAME
13
14Bio::Tools::Run::Alignment::Exonerate
15
16=head1 SYNOPSIS
17
18  use Bio::Tools::Run::Alignment::Exonerate;
19  use Bio::SeqIO;
20
21  my $qio = Bio::SeqIO->new(-file=>$ARGV[0],-format=>'fasta');
22  my $query = $qio->next_seq();
23  my $tio = Bio::SeqIO->new(-file=>$ARGV[1],-format=>'fasta');
24  my $target = $sio->next_seq();
25
26  #exonerate parameters can all be passed via arguments parameter.
27  #parameters passed are not checked for validity
28
29  my $run = Bio::Tools::Run::Alignment::Exonerate->
30      new(arguments=>'--model est2genome --bestn 10');
31  my $searchio_obj = $run->run($query,$target);
32
33  while(my $result = $searchio->next_result){
34    while( my $hit = $result->next_hit ) {
35      while( my $hsp = $hit->next_hsp ) {
36        print $hsp->start."\t".$hsp->end."\n";
37      }
38    }
39  }
40
41=head1 DESCRIPTION
42
43Wrapper for Exonerate alignment program. You can get exonerate at
44http://www.ebi.ac.uk/~guy/exonerate/.  This wrapper is written without
45parameter checking. All parameters are passed via the arugment
46parameter that is passed in the constructor. See SYNOPSIS.  For
47exonerate parameters, run exonerate --help for more details.
48
49=head1 PROGRAM VERSIONS
50
51The tests have been shown to pass with exonorate versions 2.0 - 2.2.
52
53=head1 FEEDBACK
54
55=head2 Mailing Lists
56
57User feedback is an integral part of the evolution of this and other
58Bioperl modules. Send your comments and suggestions preferably to one
59of the Bioperl mailing lists.  Your participation is much appreciated.
60
61  bioperl-l@bioperl.org                  - General discussion
62  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
63
64=head2 Support
65
66Please direct usage questions or support issues to the mailing list:
67
68I<bioperl-l@bioperl.org>
69
70rather than to the module maintainer directly. Many experienced and
71reponsive experts will be able look at the problem and quickly
72address it. Please include a thorough description of the problem
73with code and data examples if at all possible.
74
75=head2 Reporting Bugs
76
77Report bugs to the Bioperl bug tracking system to help us keep track
78the bugs and their resolution.  Bug reports can be submitted via the
79web:
80
81  http://redmine.open-bio.org/projects/bioperl/
82
83=head1 AUTHOR - Shawn Hoon
84
85  Email shawnh-at-stanford.edu
86
87=head1 APPENDIX
88
89The rest of the documentation details each of the object
90methods. Internal methods are usually preceded with a _
91
92=cut
93
94package Bio::Tools::Run::Alignment::Exonerate;
95
96use vars qw($AUTOLOAD @ISA $PROGRAM  $PROGRAMDIR
97            $PROGRAMNAME @EXONERATE_PARAMS %OK_FIELD);
98use strict;
99use Bio::Root::Root;
100use Bio::Root::IO;
101use Bio::Factory::ApplicationFactoryI;
102use Bio::Tools::Run::WrapperBase;
103use Bio::SearchIO;
104
105@ISA = qw(Bio::Root::Root Bio::Tools::Run::WrapperBase
106	  Bio::Factory::ApplicationFactoryI);
107
108=head2 program_name
109
110 Title   : program_name
111 Usage   : $factory>program_name()
112 Function: holds the program name
113 Returns:  string
114 Args    : None
115
116=cut
117
118sub program_name {
119    return 'exonerate';
120}
121
122=head2 program_dir
123
124 Title   : program_dir
125 Usage   : $factory->program_dir(@params)
126 Function: returns the program directory, obtained from ENV variable.
127 Returns:  string
128 Args    :
129
130=cut
131
132sub program_dir {
133    return Bio::Root::IO->catfile($ENV{EXONERATEDIR}) if $ENV{EXONERATEDIR};
134}
135sub AUTOLOAD {
136       my $self = shift;
137       my $attr = $AUTOLOAD;
138       $attr =~ s/.*:://;
139       $attr = uc $attr;
140       $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
141       $self->{$attr} = shift if @_;
142       return $self->{$attr};
143}
144
145=head2 new
146
147 Title   : new
148 Usage   : my $factory= Bio::Tools::Run::Phrap->new();
149 Function: creates a new Phrap factory
150 Returns:  Bio::Tools::Run::Phrap
151 Args    :
152
153=cut
154
155sub new {
156       my ($class,@args) = @_;
157       my $self = $class->SUPER::new(@args);
158
159       my ($attr, $value);
160       while (@args)  {
161           $attr =   shift @args;
162           $value =  shift @args;
163           next if( $attr =~ /^-/ ); # don't want named parameters
164           if ($attr =~/PROGRAM/i) {
165              $self->executable($value);
166              next;
167           }
168           $self->$attr($value);
169       }
170       return $self;
171}
172
173=head2  version
174
175 Title   : version
176 Usage   : exit if $prog->version() < 1.8
177 Function: Determine the version number of the program
178 Example :
179 Returns : float or undef
180 Args    : none
181
182=cut
183
184sub version {
185    my ($self) = @_;
186    my $exe;
187    return undef unless $exe = $self->executable;
188    my $string = `$exe -v` ;
189    #exonerate from exonerate version 2.0.0\n...
190    my ($version) = $string =~ /exonerate version ([\d+\.]+)/m;
191    $version =~ s/\.(\d+)$/$1/;
192    return $version || undef;
193}
194
195
196=head2 run
197
198 Title   :   run()
199 Usage   :   my $feats = $factory->run($seq)
200 Function:   Runs Phrap
201 Returns :   An array of Bio::SeqFeature::Generic objects
202 Args    :   A Bio::PrimarySeqI
203
204=cut
205
206sub run {
207    my ($self,$query,$target) = @_;
208    my @feats;
209    my ($file1) = $self->_writeInput($query);
210    my ($file2) = $self->_writeInput($target);
211    my $assembly = $self->_run($file1,$file2);
212    return $assembly;
213}
214
215=head2 _input
216
217 Title   :   _input
218 Usage   :   $factory->_input($seqFile)
219 Function:   get/set for input file
220 Returns :
221 Args    :
222
223=cut
224
225sub _input() {
226     my ($self,$infile1) = @_;
227     $self->{'input'} = $infile1 if(defined $infile1);
228     return $self->{'input'};
229 }
230
231=head2 _run
232
233 Title   :   _run
234 Usage   :   $factory->_run()
235 Function:   Makes a system call and runs Phrap
236 Returns :   An array of Bio::SeqFeature::Generic objects
237 Args    :
238
239=cut
240
241sub _run {
242     my ($self,$query,$target)= @_;
243
244     my ($tfh,$outfile) = $self->io->tempfile(-dir=>$self->tempdir);
245     my $param_str = $self->_setparams." ".$self->arguments;
246     my $str = $self->executable." $param_str $query $target "." > $outfile";
247     $self->debug( "$str\n");
248     my $status = system($str);
249     $self->throw( "Exonerate call ($str) crashed: $? \n") unless $status==0;
250     my $filehandle;
251     my $exonerate_obj = Bio::SearchIO->new(-file=>"$outfile",-format=>'exonerate');
252
253     close($tfh);
254     undef $tfh;
255     unlink $outfile;
256
257     return $exonerate_obj;
258}
259
260
261=head2 _writeInput
262
263 Title   :   _writeInput
264 Usage   :   $factory->_writeInput($query,$target)
265 Function:   Creates a file from the given seq object
266 Returns :   A string(filename)
267 Args    :   Bio::PrimarySeqI
268
269=cut
270
271sub _writeInput{
272    my ($self,$query) = @_;
273    my ($fh,$infile1);
274    if (ref($query) =~ /ARRAY/i) {
275      my @infilearr;
276      ($fh, $infile1) = $self->io->tempfile();
277      my $temp = Bio::SeqIO->new( -file => ">$infile1",
278                                  -format => 'Fasta' );
279      foreach my $seq1 (@$query) {
280        unless ($seq1->isa("Bio::PrimarySeqI")) {
281          return 0;
282        }
283        $temp->write_seq($seq1);
284        push @infilearr, $infile1;
285      }
286    }
287    elsif($query->isa("Bio::PrimarySeqI")) {
288      ($fh, $infile1) = $self->io->tempfile();
289      my $temp = Bio::SeqIO->new( -file => ">$infile1",
290                                  -format => 'Fasta' );
291      $temp->write_seq($query);
292    }
293    else {
294      $infile1 = $query;
295    }
296    return $infile1;
297}
298
299=head2 _setparams
300
301 Title   :  _setparams
302 Usage   :  Internal function, not to be called directly
303 Function:  creates a string of params to be used in the command string
304 Example :
305 Returns :  string of params
306 Args    :
307
308=cut
309
310sub _setparams {
311    my ($self) = @_;
312    my $param_string = '';
313    foreach my $attr(@EXONERATE_PARAMS){
314        next if($attr=~/PROGRAM/);
315        my $value = $self->$attr();
316        next unless (defined $value);
317        my $attr_key = ' -'.(lc $attr);
318        $param_string .= $attr_key.' '.$value;
319    }
320    return $param_string;
321}
322
3231;
324