lib/Bio/ORA.pm untested

filter: Statementstmt 0.0% 0 / 519 Branchbran 0.0% 0 / 202 Conditioncond 0.0% 0 / 121 MC/DCmcdc 0.0% 0 / 72 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 945 CC 102 SCAR 62.9
CC
102
Cov
0%
show
1122.0
_find_orf
870.0
_what_or
132.0
CRAP 538.5
1 #!/usr/bin/perl
2 # $Revision: 2.0 $
3 # $Date: 2016/06/15 $
4 # $Id: or.pl $
5 # $Author: Michael Bekaert $
6 #
7 # Olfactory Receptor family Assigner (ORA) [bioperl module]
8 # Copyright 2007-2016 Bekaert M <michael.bekaert@stir.ac.uk>
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #
23 # POD documentation - main docs before the code
24  
25 =head1 NAME
26  
27 Bio::ORA - Olfactory Receptor family Assigner (ORA) [bioperl module]
28  
29 =head1 SYNOPSIS
30  
31 Take a sequence object from, say, an inputstream, and find an Olfactory
32 Receptor gene. HMM profiles are used in order to identify location, frame
33 and orientation of such gene.
34  
35 Creating the ORA object, eg:
36  
37 my $inputstream = Bio::SeqIO->new( -file => 'seqfile', -format => 'fasta' );
38 my $seqobj = $inputstream->next_seq();
39 my $ORA_obj = Bio::ORA->new( $seqobj );
40  
41 Obtain an array holding the start point, the stop point, the DNA sequence
42 and amino-acid sequence, eg:
43  
44 my $array_ref = $ORA_obj->{'_result'} if ( $ORA_obj->find() );
45  
46 Display result in genbank format, eg:
47  
48 $ORA_obj->show( 'genbank' );
49  
50 =head1 DESCRIPTION
51  
52 Bio::ORA is a featherweight object for identifying mammalian
53 olfactory receptor genes. The sequences should not be longer than 40kb. The
54 returned array include location, sequence and statistic for the putative
55 olfactory receptor gene. Fully functional with DNA and EST
56 sequence, no intron supported.
57  
58 See Synopsis above for the object creation code.
59  
60 =head1 DRIVER SCRIPT
61  
62 #!/usr/bin/perl
63 use strict;
64 use warnings;
65 use Bio::Seq;
66 use Bio::ORA;
67  
68 my $inseq = Bio::SeqIO->new( '-file' => q{<} . $ARGV[0], -format => 'fasta' );
69 while (my $seq = $inseq->next_seq) {
70 my $ORA_obj = Bio::ORA->new( $seq );
71 if ( $ORA_obj->find() ) {
72 $ORA_obj->show( 'genbank' );
73 } else {
74 print {*STDOUT} " no hit!\n";
75 }
76 }
77  
78 =head1 REQUIREMENTS
79  
80 To use this module you may need:
81 * Bioperl (L<http://bioperl.org/>) modules,
82 * HMMER v3+ distribution (L<http://hmmer.org/>) and
83 * FASTA 36+ distribution (L<ftp://ftp.ebi.ac.uk/pub/software/unix/fasta/>).
84  
85 =head1 LOCAL ADAPTATION
86  
87 This module uses three softwares. If HMMER or FASTA are updated make sure that
88 HMMER's hmmscan and FASTA's tfastx36 and fastx36 still exists under these names.
89 You change the call my editing the "Default softwares" section.
90  
91 # Default softwares
92 my $hmmscan = 'hmmscan';
93 my $tfastx = 'tfastx36';
94 my $fastx = 'fastx36';
95  
96 =head1 FEEDBACK
97  
98 If you have any problems with or questions about the scripts, please contact us
99 through a GitHub issue (L<https://github.com/pseudogene/ora/issues>). You are
100 invited to contribute new features, fixes, or updates, large or small; we are
101 always thrilled to receive pull requests, and do our best to process them as
102 fast as we can.
103  
104 =head1 AUTHOR
105  
106 B<Michael Bekaert> (michael.bekaert@stir.ac.uk)
107  
108 Address:
109 Institute of Aquaculture
110 University of Stirling
111 Stirling
112 Scotland, FK9 4LA
113 UK
114  
115 =head1 SEE ALSO
116  
117 perl(1), bioperl web site
118  
119 =head1 LICENSE
120  
121 Copyright 2007-2016 - Michael Bekaert
122  
123 This program is free software: you can redistribute it and/or modify
124 it under the terms of the GNU General Public License as published by
125 the Free Software Foundation, either version 3 of the License, or
126 (at your option) any later version.
127  
128 This program is distributed in the hope that it will be useful,
129 but WITHOUT ANY WARRANTY; without even the implied warranty of
130 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131 GNU General Public License for more details.
132  
133 You should have received a copy of the GNU General Public License
134 along with this program. If not, see L<http://www.gnu.org/licenses/>.
135  
136 =head1 APPENDIX
137  
138 The rest of the documentation details each of the object methods. Internal
139 methods are usually preceded with a _
140  
141 =cut
142  
143 package Bio::ORA;
144 use strict;
145 use warnings;
146 use vars qw($VERSION);
147 use File::Temp qw/tempfile/;
148 use File::Basename qw/ dirname /;
149 use Cwd qw/ abs_path /;
150 use Bio::SeqIO;
151 use Bio::Seq;
152 use Bio::SearchIO;
153 use Bio::PrimarySeq;
154 use Bio::PrimarySeqI;
155 use Bio::Tools::CodonTable;
156 use base qw/Bio::Root::Root Bio::Root::IO/;
157 our $VERSION = '2.0';
158  
159 # Default softwares
160 my $hmmscan = 'hmmscan';
161 my $tfastx = 'tfastx36';
162 my $fastx = 'fastx36';
163  
164 # Default path
165 my $PATH_REF = abs_path(dirname(abs_path($0)) . '/or.fasta');
166 my $PATH_HMM = abs_path(dirname(abs_path($0)) . '/or.hmm');
167 my $PATH_TMP = '/tmp';
168  
169 =head2 _findexec
170  
171 Title : _findexec
172 Usage : my $path = $self->_findexec( $exec );
173 Function: Find an executable file in the $PATH.
174 Returns : The full path to the executable.
175 Args : $exec (mandatory) executable to be find.
176  
177 =cut
178  
179 sub _findexec
180 {
181 my ($self, @args) = @_;
182 my $exec = shift @args;
183 foreach my $p (split /:/, $ENV{'PATH'}) {
184 return "$p/$exec"
185 if (-x "$p/$exec");
186 }
187 return $ENV{'HOME'} . "/bin/$exec" if (-x $ENV{'HOME'} . "/bin/$exec");
188 return;
189 }
190  
191 =head2 new
192  
193 Title : new
194 Usage : my $nb = Bio::ORA->new( $seqobj, $table, $aug, $hmm );
195 Function: Initialize ORA object.
196 Returns : An ORA object.
197 Args : $seqobj (mandatory) PrimarySeqI object (dna or rna),
198 $table (optional) translation table/genetic code number,
199 the default value is 1,
200 $aug (optional) use other start codon than AUG (default 0),
201 $hmm (optional) path to hmm profiles by default ORA looks at
202 ./or.hmm.
203  
204 =cut
205  
206 sub new
207 {
208 my ($class, @args) = @_;
209 my ($seqobj, $table, $aug, $hmm) = @args;
210 my $self = bless {}, $class;
211 $self->{'_aug'} = ((defined $aug && int($aug) == 1) ? 0 : 1);
212 $self->{'_hmm'} = ((defined $hmm) ? $hmm : $PATH_HMM);
213 $seqobj->throw(
214 "die in _initialize, ORA.pm works only on PrimarySeqI objects\n")
215 unless ($seqobj->isa('Bio::PrimarySeqI'));
216 $seqobj->throw("die in _initialize, ORA.pm works only on DNA sequences\n")
217 if ($seqobj->alphabet eq 'protein');
218 $seqobj->throw(
219 "die in _initialize, ORA.pm works only on DNA sequences < 40kb\n")
220 if (length($seqobj->seq()) > 40_000);
221 $seqobj->throw('die in _initialize, hmm profile not found at '
222 . $self->{'_hmm'} . "\n")
223 unless (-f $self->{'_hmm'});
224 my $chrom =
225 ((defined $seqobj->display_id) ? $seqobj->display_id : 'noname');
226 $seqobj = uc $seqobj->seq();
227 $seqobj =~ tr/U/T/;
228 $chrom =~ tr/,/\_/;
229 $self->{'_seqref'} =
230 Bio::Seq->new(-seq => $seqobj, -alphabet => 'dna', -id => $chrom);
231 $self->{'_table'} = ((defined $table) ? $table : 1);
232 $self->{'_verbose'} = q{};
233 return $self;
234 }
235  
236 =head2 find
237  
238 Title : find
239 Usage : my $bool = $ORI_obj->find( $evalue, $strand, $start, $end );
240 Function: Identify an olfactory receptor protein.
241 Returns : boolean.
242 Args : $evalue (optional) set the E-value (expected) threshold.
243 Default is 1e-30,
244 $strand(optional) strand where search should be done (1 direct,
245 -1 reverse or 0 both). Default is 0,
246 $start (optional) coordinate of the first nucleotide. Useful
247 for coordinate calculation when first is not 1. Default is 1,
248 $end (optional) coordinate of the last nucleotide. Default is
249 the sequence length.
250  
251 =cut
252  
253 sub find
254 {
255 my ($self, @args) = @_;
256 my ($evalue, $strand, $start, $end) = @args;
257 $self->{'_evalue'} = ((defined $evalue) && ($evalue > 0)) ? $evalue : 1e-30;
258 $strand = 0 unless ((defined $strand) && ($strand == 1 || $strand == -1));
259 $start = 1 unless ((defined $start) && $start > 1);
260 $end = $self->{'_seqref'}->length
261 unless ((defined $end) && $end > 1 && $end > $start);
262 my $status = $self->_what_or($strand);
263 if (($status) && ($self->{'_hmmor'}[0] < $self->{'_evalue'}))
264 {
265 $self->{'_verbose'} = '> '
266 . $self->{'_hmmor'}[1]
267 . ' found ('
268 . $self->{'_hmmor'}[0]
269 . ') for '
270 . $self->{'_seqref'}->display_id
271 . ' in frame '
272 . ($self->{'_hmmor'}[6] > 0 ? q{+} : q{-})
273 . $self->{'_hmmor'}[2] . "\n";
274 return $self->_find_orf($self->{'_hmmor'}[6], $start, $end);
275 }
276 else
277 {
278 $self->{'_verbose'} =
279 '> no hit for ' . $self->{'_seqref'}->display_id . "\n";
280 }
281 return 0;
282 }
283  
284 =head2 _what_or
285  
286 Title : _what_or
287 Usage : my $bool = $self->_what_or( $strand );
288 Function: Use HMM profiles to identify an olfactory receptor gene.
289 Returns : boolean.
290 Args : $strand (optional) strand where search should be done
291 (1 direct, -1 reverse or 0 both). Default is 0.
292  
293 =cut
294  
295 sub _what_or
296 {
297 my ($self, @args) = @_;
298 my $strand = shift @args;
299 my ($best, $second);
300 my $seq = $self->{'_seqref'};
301 $seq = $seq->revcom if ($strand < 0);
302 my ($TMP, $filename) = tempfile(DIR => $PATH_TMP, UNLINK => 1);
303 for (my $i = 0 ; $i < 3 ; $i++)
304 {
305 print {$TMP} ">$i\n",
306 $seq->translate(undef, undef, $i, $self->{'_table'})->seq(), "\n";
307 }
308 if ($strand == 0)
309 {
310 $seq = $seq->revcom;
311 for (my $i = 0 ; $i < 3 ; $i++)
312 {
313 print {$TMP} ">$i-\n",
314 $seq->translate(undef, undef, $i, $self->{'_table'})->seq(), "\n";
315 }
316 }
317 close $TMP;
318 my $pfam = $self->_findexec($hmmscan);
319 if ((defined $seq) && (-x $pfam))
320 {
321 system $pfam . q{ }
322 . $self->{'_hmm'} . q{ }
323 . $filename . q{>}
324 . $filename
325 . '.report';
326 eval
327 {
328 my $hmmer =
329 Bio::SearchIO->new(-file => $filename . '.report',
330 -format => 'hmmer');
331 while (my $result = $hmmer->next_result)
332 {
333 while (my $hit = $result->next_hit)
334 {
335 while (my $hsp = $hit->next_hsp)
336 {
337 $self->{'_hmmor'} = (
338 [
339 $hsp->evalue(),
340 $hit->name,
341 substr($result->query_name(), 0, 1),
342 $hsp->score(),
343 $hsp->start('query'),
344 $hsp->end('query'),
345 (
346 ($strand != 0)
347 ? $strand
348 : ((length($result->query_name()) > 1) ? -1 : 1)
349 )
350 ]
351 )
352 if (!defined $self->{'_hmmor'}[0]
353 || $hsp->evalue() < $self->{'_hmmor'}[0]);
354 $best = $hsp->evalue()
355 if ((!defined $best) || $hsp->evalue() < $best);
356 $second = $hsp->evalue()
357 if ($hsp->evalue() > $best
358 && (!defined $second || $hsp->evalue() < $second));
359 }
360 }
361 }
362 $self->{'_hmmor'}[7] = $second
363 if (defined $best && defined $self->{'_hmmor'}[0]);
364 };
365 unlink $filename . '.report';
366 }
367 unlink $filename;
368 return (defined $self->{'_hmmor'}[0]) ? 1 : 0;
369 }
370  
371 =head2 _find_orf
372  
373 Title : _find_orf
374 Usage : my $bool = $self->_find_or( $strand, $start, $end );
375 Function: Retrieve the olfactory receptor ORF.
376 Returns : boolean.
377 Args : $strand (mandatory) strand where ORA have been found
378 (1 direct or -1 reverse),
379 $start (mandatory) coordinate of the first nucleotide,
380 $end (mandatory) coordinate of the last nucleotide.
381  
382 =cut
383  
384 sub _find_orf
385 {
386 my ($self, @args) = @_;
387 my ($strand, $start, $end) = @args;
388 my ($position1, $position2);
389 if ((defined $self->{'_hmmor'}) && (defined $self->{'_hmmor'}[0]))
390 {
391 my ($begin, $stop) = $self->_translation();
392 my $seq = $self->{'_seqref'};
393 $seq = $seq->revcom if ($strand < 0);
394 $seq = $seq->seq();
395 my ($i, $j) = 0;
396 $self->{'_no5'} = 1 if ($self->{'_hmmor'}[4] < 9);
397 my $mydna = substr $seq, 0,
398 ($self->{'_hmmor'}[4] * 3 + abs($self->{'_hmmor'}[2]) + 9);
399 if ($mydna =~ m/(($begin)((?!($stop|$begin))(.{3}))*?)$/o)
400 {
401 $position1 = length $`;
402 }
403 else
404 {
405 if ($mydna =~ m/(($stop)((?!($stop))(.{3}))*)$/o)
406 {
407 $position1 = 3 + length $`;
408 $self->{'_noaug'} = 1;
409 }
410 elsif ($mydna =~ m/^(.{0,2})((.{3})*)$/o)
411 {
412 $position1 = length $1;
413 $self->{'_noaug'} = 2;
414 }
415 }
416 if (defined $position1)
417 {
418 $mydna = substr $seq, $position1;
419 my ($coord, $dna, $stopcodon);
420 if ($mydna =~ m/^(.{3})(((?!($stop))(.{3})){194,})($stop)/o)
421 {
422 $dna = $1 . $2;
423 $stopcodon = $6;
424 }
425 elsif ($mydna =~ m/^(.{3})(((?!($stop))(.{3})){194,})(.{0,2})$/o)
426 {
427 $self->{'_no3'} = 1;
428 $dna = $2;
429 $stopcodon = q{};
430 }
431 elsif ($mydna =~ m/($stop)(((?!($stop))(.{3})){194,})($stop)/o)
432 {
433 $self->{'_noaug'} = 1;
434 $position1 = 3 + length $`;
435 $dna = $2;
436 $stopcodon = $6;
437 }
438 else
439 {
440 $dna = substr $seq, ($self->{'_hmmor'}[4] * 3) - 3,
441 (($self->{'_hmmor'}[5] - $self->{'_hmmor'}[4] + 1) * 3);
442 $dna = $1 if ($dna =~ m/^((.{3})*).{0,2}$/o);
443 $position1 = $self->{'_hmmor'}[4] * 3;
444 $self->{'_pseudo'} = 1;
445 $stopcodon = q{};
446 }
447 if (defined $dna)
448 {
449 if ($self->{'_hmmor'}[6] > 0)
450 {
451 $position2 =
452 $start + $position1 + length($dna . $stopcodon) - 1;
453 $position1 = $start + $position1;
454 $coord =
455 ((defined $self->{'_no5'}) ? q{<} : q{})
456 . $position1 . '..'
457 . $position2
458 . ((defined $self->{'_no3'}) ? q{>} : q{});
459 }
460 else
461 {
462 $position2 = $end - $position1;
463 $position1 =
464 $end - $position1 - length($dna . $stopcodon) + 1;
465 $coord =
466 'complement('
467 . ((defined $self->{'_no3'}) ? q{<} : q{})
468 . $position1 . '..'
469 . $position2
470 . ((defined $self->{'_no5'}) ? q{>} : q{}) . q{)};
471 }
472 if (defined $self->{'_frag'})
473 {
474 $self->{'_pseudo'} = 0
475 if (($position2 - $position1 + 1) < $self->{'_frag'});
476 }
477 else
478 {
479 $self->{'_pseudo'} = 0
480 if (($position2 - $position1 + 1) < 600);
481 $self->{'_pseudo'} = 0
482 if ( (($position2 - $position1 + 1) < 900)
483 && ($self->{'_hmmor'}[1] ne 'OR7')
484 && !(defined $self->{'_no5'} || defined $self->{'_no3'})
485 );
486 $self->{'_pseudo'} = 0
487 if ( (($position2 - $position1 + 1) < 800)
488 && ($self->{'_hmmor'}[1] eq 'OR7')
489 && !(defined $self->{'_no5'} || defined $self->{'_no3'})
490 );
491 }
492 $self->{'_result'} = (
493 [
494 $coord,
495 $position1,
496 $position2,
497 $self->{'_hmmor'}[6],
498 'Olfactory Receptor, family '
499 . substr($self->{'_hmmor'}[1], 2),
500 $self->{'_hmmor'}[1],
501 (
502 defined $self->{'_pseudo'}
503 ? 'Pseudogene (' . $self->{'_pseudo'} . '); '
504 : q{}
505 )
506 . (
507 (defined $self->{'_no5'})
508 ? 'The sequence seems incomplete, 5\' of the CDS is missing; '
509 : q{}
510 )
511 . (
512 (defined $self->{'_no3'})
513 ? 'The sequence seems incomplete, 3\' of the CDS is missing; '
514 : q{}
515 )
516 . (
517 !(defined $self->{'_pseudo'})
518 && (defined $self->{'_noaug'})
519 ? 'The position of the initiation codon is not identified; '
520 : q{}
521 )
522 . 'HMM for family '
523 . $self->{'_hmmor'}[1] . ': '
524 . $self->{'_hmmor'}[0] . ' ('
525 . ($self->{'_hmmor'}[7] - $self->{'_hmmor'}[0]) . q{)},
526 $dna . $stopcodon,
527 Bio::Seq->new(-seq => $dna, alphabet => 'dna')
528 ->translate(undef, undef, undef, $self->{'_table'})
529 ->seq()
530 ]
531 ) if (defined $coord);
532 }
533 }
534 }
535 return (defined $position2) ? 1 : 0;
536 }
537  
538 =head2 getHits
539  
540 Title : getHits
541 Usage : my @hits = Bio::ORA->getHits( $seq, $evalue, $ref );
542 Function: Quick localization of ORs (use FASTA).
543 Returns : Array of hits start/stop positions.
544 Args : $seq (mandatory) primarySeqI object (dna or rna),
545 $evalue (mandatory) det the E-value threshold,
546 $ref (optional) path to fasta reference file, by default ORA
547 look at ./or.fasta.
548  
549 =cut
550  
551 sub getHits
552 {
553 my ($self, @args) = @_;
554 my ($seq, $evalue, $ref) = @args;
555 $ref = ((defined $ref) ? $ref : $PATH_REF);
556 $evalue = ((defined $evalue) && ($evalue > 0)) ? $evalue : 1;
557 my ($TMP, $filename) = tempfile(DIR => $PATH_TMP, UNLINK => 1);
558 my $fasta = $self->_findexec($tfastx);
559 my @hits;
560 if ((defined $seq) && (-x $fasta))
561 {
562 print {$TMP} ">query\n", $seq->seq(), "\n\n";
563 close $TMP;
564 system( $fasta
565 . ' -Q -b 1 -d 1 -H '
566 . $ref . q{ }
567 . $filename . q{>}
568 . $filename
569 . '.report') == 0
570 or return;
571 eval
572 {
573 my $in = Bio::SearchIO->new(-format => 'fasta',
574 -file => $filename . '.report');
575 while (my $result = $in->next_result)
576 {
577 while (my $hit = $result->next_hit)
578 {
579 while (my $hsp = $hit->next_hsp)
580 {
581 push(
582 @hits,
583 (
584 $hit->strand('query') . q{|}
585 . sprintf("%09d", $hsp->start('hit')) . q{|}
586 . sprintf("%09d", $hsp->end('hit'))
587 )
588 ) if ($hsp->evalue <= $evalue);
589 }
590 }
591 }
592 };
593 unlink $filename . '.report';
594 }
595 unlink $filename;
596 if ($#hits >= 0)
597 {
598 @hits = sort @hits;
599 my @hits2 = ();
600 for (my $i = 0 ; $i < $#hits ; $i++)
601 {
602 my ($hitstrand, $hitstart, $hitend) = split /\|/, $hits[$i];
603 my $loop = 0;
604 do
605 {
606 $loop = 0;
607 my ($hitstrand_n, $hitstart_n, $hitend_n);
608 ($hitstrand_n, $hitstart_n, $hitend_n) =
609 split(/\|/, $hits[$i + 1])
610 if (defined $hits[$i + 1]);
611 if (
612 (defined $hits[$i + 1])
613 && ( (abs($hitstart_n - $hitend) < 500)
614 || (abs($hitstart - $hitend_n) < 500)
615 || ( ($hitstart_n < $hitend)
616 && ($hitstart_n > $hitstart))
617 || (($hitend_n < $hitend) && ($hitend_n > $hitstart))
618 || ( ($hitstart < $hitend_n)
619 && ($hitstart > $hitstart_n))
620 || (($hitend < $hitend_n) && ($hitend > $hitstart_n)))
621 && ($hitstrand == $hitstrand_n)
622 )
623 {
624 $i++;
625 $hitend = $hitend_n if ($hitend < $hitend_n);
626 $hitstart = $hitstart_n if ($hitstart > $hitstart_n);
627 $loop = 1;
628 }
629 } until ($loop == 0);
630 push @hits2,
631 ( $hitstrand . q{|}
632 . sprintf("%09d", $hitstart) . q{|}
633 . sprintf("%09d", $hitend));
634 }
635 @hits = @hits2;
636 }
637 return @hits;
638 }
639  
640 =head2 fastScan
641  
642 Title : fastScan
643 Usage : my @hits = Bio::ORA->fastScan( $seq, $ref );
644 Function: Quick localization of ORs (use FASTA).
645 Returns : Array of hits start/stop positions.
646 Args : $seq (mandatory) primarySeqI object (dna or rna),
647 $ref (optional) path to fasta reference file, by default ORA
648 look at ./or.fasta.
649  
650 =cut
651  
652 sub fastScan
653 {
654 my ($self, @args) = @_;
655 my ($seqfile, $ref) = @args;
656 $ref = ((defined $ref) ? $ref : $PATH_REF);
657 my ($TMP, $filename) = tempfile(DIR => $PATH_TMP, UNLINK => 1);
658 close $TMP;
659 my $fasta = $self->_findexec($fastx);
660 my @hits;
661 if ((defined $seqfile) && (-x $fasta))
662 {
663 system( $fasta
664 . ' -b 1 -d 1 -E 1 -H -Q '
665 . $seqfile . q{ }
666 . $ref . q{>}
667 . $filename) == 0
668 or return;
669 eval {
670 my $in = Bio::SearchIO->new(-format => 'fasta', -file => $filename);
671 my $last;
672 while (my $result = $in->next_result)
673 {
674 push(@hits, $result->query_name())
675 if (!defined $last || $last ne $result->query_name());
676 $last = $result->query_name();
677 }
678 };
679 }
680 unlink($filename);
681 return @hits;
682 }
683  
684 =head2 show
685  
686 Title : show
687 Usage : $ORA_obj->show( $outstyle );
688 Function: Print result in various style.
689 Returns : none.
690 Args : $outstyle (mandatory) 'fasta', 'genbank', 'cvs', 'xml' or 'tsv' style.
691  
692  
693 =cut
694  
695 sub show
696 {
697 my ($self, @args) = @_;
698 my $out = shift @args;
699 $out = ((defined $out) ? $out : 'fasta');
700 if ($out eq 'xml-begin')
701 {
702 print {*STDOUT} "<orml version=\"0.9\">\n";
703 print {*STDOUT} " <analysis>\n";
704 print {*STDOUT} " <program>\n";
705 print {*STDOUT} " <prog-name>ORA.pm</prog-name>\n";
706 print {*STDOUT} " <prog-version>$VERSION</prog-version>\n";
707 print {*STDOUT} " </program>\n";
708 print {*STDOUT} " <date>\n";
709 print {*STDOUT} ' <day>', (gmtime)[3], "</day>\n";
710 print {*STDOUT} ' <month>', (gmtime)[4] + 1, "</month>\n";
711 print {*STDOUT} ' <year>', (gmtime)[5] + 1900, "</year>\n";
712 print {*STDOUT} " </date>\n";
713 print {*STDOUT} " <parameter>\n";
714 print {*STDOUT} ' <evalue>', shift(@args), "</evalue>\n";
715 print {*STDOUT} ' <table>', shift(@args), "</table>\n";
716 print {*STDOUT} " </parameter>\n";
717 print {*STDOUT} " </analysis>\n";
718 }
719 elsif ($out eq 'xml-end') { print {*STDOUT} "</orml>\n"; }
720 elsif ((defined $self->{'_result'}) && (defined $self->{'_result'}[8]))
721 {
722 my $detail = shift @args;
723 if ($out eq 'genbank')
724 {
725 my @dated = localtime time;
726 my %month = (
727 0 => 'JAN',
728 1 => 'FEB',
729 2 => 'MAR',
730 3 => 'APR',
731 4 => 'MAY',
732 5 => 'JUN',
733 6 => 'JUL',
734 7 => 'AUG',
735 8 => 'SEP',
736 9 => 'OCT',
737 10 => 'NOV',
738 11 => 'DEC'
739 );
740 printf {*STDOUT}
741 "\nLOCUS %-20s %7d bp linear UNA %02d-%3s-%04d\n",
742 (
743 $self->{'_seqref'}->display_id,
744 $self->{'_seqref'}->length(),
745 $dated[3], $month{$dated[4]}, $dated[5] + 1900
746 );
747 print {*STDOUT} 'ACCESSION ', $self->{'_seqref'}->display_id,
748 "\n";
749 print {*STDOUT} 'DEFINITION ', $self->{'_result'}[5], ".\n";
750 print {*STDOUT}
751 "KEYWORDS .\nSOURCE Unknown.\n ORGANISM Unknown\n Unclassified.\n";
752 print {*STDOUT} 'COMMENT Method: ORA v', $VERSION, ".\n";
753 print {*STDOUT} "FEATURES Location/Qualifiers\n";
754 print {*STDOUT} ' source 1..',
755 $self->{'_seqref'}->length(), "\n";
756 print {*STDOUT} ' gene ',
757 (
758 ($self->{'_result'}[3] < 0)
759 ? ('complement(<'
760 . $self->{'_result'}[1] . '..'
761 . $self->{'_result'}[2] . '>)')
762 : ( q{<}
763 . $self->{'_result'}[1] . '..'
764 . $self->{'_result'}[2] . q{>})
765 ),
766 "\n";
767 print {*STDOUT} ' /locus_tag="',
768 $self->{'_seqref'}->display_id, "\"\n";
769 print {*STDOUT} ' /gene="',
770 $self->{'_result'}[5], "\"\n";
771 print " /pseudo\n"
772 if (defined $self->{'_pseudo'});
773 print {*STDOUT} ' /inference="',
774 $self->{'_hmmor'}[1], ' family: ', $self->{'_hmmor'}[0], ' (',
775 ($self->{'_hmmor'}[7] - $self->{'_hmmor'}[0]), ")\"\n";
776  
777 if ($detail)
778 {
779 my $dna = $self->{'_result'}[7] . q{"};
780 print {*STDOUT} ' /dna="',
781 substr($dna, 0, 52), "\n";
782 my $i = 0;
783 while (length($dna) > (($i * 58) + 52))
784 {
785 print {*STDOUT} q{ } x 21,
786 substr($dna, (($i++ * 58) + 52), 58), "\n";
787 }
788 }
789 print {*STDOUT} ' CDS ', $self->{'_result'}[0],
790 "\n";
791 print {*STDOUT} ' /locus_tag="',
792 $self->{'_seqref'}->display_id, "\"\n";
793 print {*STDOUT} ' /gene="',
794 $self->{'_result'}[5], "\"\n";
795 my $note = $self->{'_result'}[6] . q{"};
796 print {*STDOUT} ' /note="',
797 substr($note, 0, 51), "\n";
798 my $i = 0;
799 while (length($note) > (($i * 58) + 51))
800 {
801 print {*STDOUT} q{ } x 21,
802 substr($note, (($i++ * 58) + 51), 58), "\n";
803 }
804 print {*STDOUT} " /pseudo\n"
805 if (defined $self->{'_pseudo'});
806 print {*STDOUT} " /codon_start=1\n";
807 print {*STDOUT} ' /transl_table=',
808 $self->{'_table'}, "\n";
809 print {*STDOUT} ' /product="',
810 $self->{'_result'}[4],
811 ((defined $self->{'_pseudo'}) ? ', pseudogene' : q{}), "\"\n";
812 my $translation_issue = $self->{'_result'}[8] . q{"};
813 print {*STDOUT} ' /translation="',
814 substr($translation_issue, 0, 44), "\n";
815 $i = 0;
816  
817 while (length($translation_issue) > (($i * 58) + 44))
818 {
819 print {*STDOUT} q{ } x 21,
820 substr($translation_issue, (($i++ * 58) + 44), 58), "\n";
821 }
822 print {*STDOUT} "ORIGIN\n";
823 my $dna = $self->{'_seqref'}->seq();
824 my $j = 0;
825 while (length($dna) > ($j * 60))
826 {
827 $i = 0;
828 printf {*STDOUT} ' %7d ', ($j * 60 + 1);
829 while (length($dna) > ($j * 60 + $i * 10) && $i < 6)
830 {
831 print {*STDOUT} substr($dna, ($j * 60 + $i++ * 10), 10),
832 q{ };
833 }
834 print {*STDOUT} "\n";
835 $j++;
836 }
837 print {*STDOUT} "//\n";
838 }
839 elsif ($out eq 'tbl')
840 { # 'Feature Table' output (for tbl2asn, NCBI)
841 my $organism = shift @args;
842 print {*STDOUT} '>Feature ', $self->{'_seqref'}->display_id, "\n";
843 print {*STDOUT} "<1\t>", $self->{'_seqref'}->length(), "\tgene\n";
844 print {*STDOUT} "\t\t\tgene\t", $self->{'_result'}[5], "\n";
845 print {*STDOUT} "\t\t\tlocus_tag\t", $self->{'_seqref'}->display_id,
846 "\n";
847 print {*STDOUT} "\t\t\tpseudo\n" if (defined $self->{'_pseudo'});
848 print {*STDOUT} "\t\t\tnote\t", $self->{'_hmmor'}[1], ' family: ',
849 $self->{'_hmmor'}[0], "\n";
850 unless (defined $self->{'_pseudo'})
851 {
852 print {*STDOUT} "<1\t>", $self->{'_seqref'}->length(),
853 "\tCDS\n";
854 print {*STDOUT} "\t\t\tcodon_start\t",
855 ((($self->{'_result'}[1]) % 3 == 0)
856 ? 3
857 : ($self->{'_result'}[1]) % 3), "\n";
858 print {*STDOUT} "\t\t\ttransl_table\t", $self->{'_table'}, "\n";
859 print {*STDOUT} "\t\t\tproduct\t", $self->{'_result'}[4], "\n";
860 }
861 print {*STDOUT} "\n";
862 print {*STDERR} "\n>", $self->{'_seqref'}->display_id,
863 ' [organism=', (defined $organism ? $organism : 'Unknown'), '] ',
864 $self->{'_result'}[4], " gene member, partial cds.\n";
865 my $i = 0;
866 my $dna = $self->{'_seqref'}->seq();
867 while (length($dna) > ($i * 80))
868 {
869 print {*STDERR} substr($dna, ($i++ * 80), 80), "\n";
870 }
871 }
872 elsif ($out eq 'csv')
873 { # CSV output
874 print {*STDOUT} $self->{'_seqref'}->display_id, q{,},
875 ((defined $self->{'_pseudo'}) ? q{N} : q{Y}), q{,},
876 ($self->{'_result'}[2] - $self->{'_result'}[1] + 1), q{,},
877 substr($self->{'_hmmor'}[1], 2), q{,}, $self->{'_hmmor'}[0], q{,},
878 $self->{'_hmmor'}[7], q{,}, $self->{'_result'}[7], q{,},
879 $self->{'_result'}[8], "\n";
880 }
881 elsif ($out eq 'tsv')
882 { # numeric TSV aka R output
883 print {*STDOUT} $self->{'_seqref'}->display_id, "\t",
884 ((defined $self->{'_pseudo'}) ? q{0} : q{1}), "\t",
885 ($self->{'_result'}[2] - $self->{'_result'}[1] + 1), "\tOR",
886 sprintf("%02d", substr($self->{'_hmmor'}[1], 2)), "\t",
887 $self->{'_hmmor'}[0], "\t",
888 ((defined $self->{'_pseudo'}) ? $self->{'_pseudo'} : q{0}), "\n";
889 }
890 elsif ($out eq 'xml')
891 {
892 print {*STDOUT} ' <sequence id="', $self->{'_seqref'}->display_id,
893 ".seq\">\n";
894 print {*STDOUT} " <input>\n";
895 print {*STDOUT} ' <seq type="dna" length="',
896 $self->{'_seqref'}->length, '">', $self->{'_seqref'}->seq,
897 "</seq>\n";
898 print {*STDOUT} " </input>\n";
899 print {*STDOUT} ' <output id="', $self->{'_seqref'}->display_id,
900 "\">\n";
901 print {*STDOUT} ' <gene id="', $self->{'_seqref'}->display_id,
902 ".1\">\n";
903 print {*STDOUT} ' <coord',
904 (defined $self->{'_no5'}) ? ' 5prime="missing"' : q{},
905 (defined $self->{'_no3'}) ? ' 3prime="missing"' : q{}, q{>},
906 (
907 ($self->{'_result'}[3] < 0)
908 ? ('complement('
909 . ((defined $self->{'_no3'}) ? q{<} : q{})
910 . $self->{'_result'}[1] . '..'
911 . $self->{'_result'}[2]
912 . ((defined $self->{'_no5'}) ? q{>} : q{}) . q{)})
913 : ( ((defined $self->{'_no5'}) ? q{<} : q{})
914 . $self->{'_result'}[1] . '..'
915 . $self->{'_result'}[2]
916 . ((defined $self->{'_no3'}) ? q{>} : q{}))
917 ),
918 "</coord>\n";
919 print {*STDOUT} ' <name>', $self->{'_result'}[5], "</name>\n";
920 print {*STDOUT} ' <seq type="dna" length="',
921 length($self->{'_result'}[7]), '">', $self->{'_result'}[7],
922 "</seq>\n";
923 print {*STDOUT} " </gene>\n";
924 print {*STDOUT} ' <cds id="', $self->{'_seqref'}->display_id,
925 ".2\">\n";
926 print {*STDOUT} ' <coord',
927 (defined $self->{'_noaug'}) ? ' start="unknown"' : q{},
928 (defined $self->{'_no5'}) ? ' 5prime="missing"' : q{},
929 (defined $self->{'_no3'}) ? ' 3prime="missing"' : q{}, q{>},
930 $self->{'_result'}[0], "</coord>\n";
931 print {*STDOUT} ' <name>', $self->{'_result'}[5], "</name>\n";
932 print {*STDOUT} ' <note>', $self->{'_result'}[6], "</note>\n";
933 print {*STDOUT} ' <product>', $self->{'_result'}[4],
934 "</product>\n";
935 print {*STDOUT} ' <seq type="prt" length="',
936 length($self->{'_result'}[8]), '">', $self->{'_result'}[8],
937 "</seq>\n";
938 print {*STDOUT} ' <model hmm="', $self->{'_hmmor'}[1], '">',
939 $self->{'_hmmor'}[0], "</model>\n";
940 print {*STDOUT} " </cds>\n";
941 print {*STDOUT} " </output>\n";
942 print {*STDOUT} " </sequence>\n";
943 }
944 else
945 { # fasta format
946 print {*STDOUT} "\n>", $self->{'_seqref'}->display_id, q{|},
947 $self->{'_result'}[5],
948 ((defined $self->{'_pseudo'}) ? '|PSEUDOGENE' : q{}), "\n";
949 my $i = 0;
950 while (length($self->{'_result'}[7]) > ($i * 80))
951 {
952 print {*STDOUT} substr($self->{'_result'}[7], ($i++ * 80), 80),
953 "\n";
954 }
955 }
956 }
957 return;
958 }
959  
960 =head2 _translation
961  
962 Title : _translation
963 Usage : my ( $start, $end ) = $self->_translation();
964 Function: format initiation and stop codons for regex.
965 Returns : array with initiation and stop codons.
966 Args : none.
967  
968 =cut
969  
970 sub _translation
971 {
972 my $self = shift;
973 my @table = qw(A T C G);
974 my @var = ();
975 my @var2 = ();
976 my $var_i = 0;
977 my $var2_i = 0;
978 my $myCodonTable = Bio::Tools::CodonTable->new(-id => $self->{'_table'});
979 for (my $i = 0 ; $i < 4 ; $i++)
980 {
981  
982 for (my $j = 0 ; $j < 4 ; $j++)
983 {
984 for (my $k = 0 ; $k < 4 ; $k++)
985 {
986 $var[$var_i++] = $table[$i] . $table[$j] . $table[$k]
987 if $myCodonTable->is_start_codon(
988 $table[$i] . $table[$j] . $table[$k]);
989 $var2[$var2_i++] = $table[$i] . $table[$j] . $table[$k]
990 if $myCodonTable->is_ter_codon(
991 $table[$i] . $table[$j] . $table[$k]);
992 }
993 }
994 }
995 @var = ('ATG') if ($self->{'_aug'});
996 return (join(q{|}, @var), join(q{|}, @var2));
997 }
998  
999 # and that's all the module
1000 1;