FAST/Bio/Search/Hit/GenericHit.pm untested

filter: Statementstmt 0.0% 0 / 465 Branchbran 0.0% 0 / 288 Conditioncond 0.0% 0 / 153 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 49 Podpod 0.0% 0 / 46 totaltotal 0.0% 0 / 1001 SCAR 99.6
CC
145
Cov
0%
strand
156.0
start
132.0
end
132.0
CRAP 21170.0
1 #
2 # BioPerl module for FAST::Bio::Search::Hit::GenericHit
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason@bioperl.org>
7 #
8 # Copyright Jason Stajich
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 FAST::Bio::Search::Hit::GenericHit - A generic implementation of the FAST::Bio::Search::Hit::HitI interface
17  
18 =head1 SYNOPSIS
19  
20 use FAST::Bio::Search::Hit::GenericHit;
21 my $hit = FAST::Bio::Search::Hit::GenericHit->new(-algorithm => 'blastp');
22  
23 # typically one gets HitI objects from a SearchIO stream via a ResultI
24 use FAST::Bio::SearchIO;
25 my $parser = FAST::Bio::SearchIO->new(-format => 'blast', -file => 'result.bls');
26  
27 my $result = $parser->next_result;
28 my $hit = $result->next_hit;
29  
30 # TODO: Describe how to configure a SearchIO stream so that it generates
31 # GenericHit objects.
32  
33 =head1 DESCRIPTION
34  
35 This object handles the hit data from a Database Sequence Search such
36 as FASTA or BLAST.
37  
38 Unless you're writing a parser, you won't ever need to create a
39 GenericHit or any other HitI-implementing object. If you use
40 the SearchIO system, HitI objects are created automatically from
41 a SearchIO stream which returns FAST::Bio::Search::Hit::HitI objects.
42  
43 For documentation on what you can do with GenericHit (and other HitI
44 objects), please see the API documentation in
45 L<FAST::Bio::Search::Hit::HitI|FAST::Bio::Search::Hit::HitI>.
46  
47 =head1 FEEDBACK
48  
49 =head2 Mailing Lists
50  
51 User feedback is an integral part of the evolution of this and other
52 Bioperl modules. Send your comments and suggestions preferably to
53 the Bioperl mailing list. Your participation is much appreciated.
54  
55 bioperl-l@bioperl.org - General discussion
56 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
57  
58 =head2 Support
59  
60 Please direct usage questions or support issues to the mailing list:
61  
62 I<bioperl-l@bioperl.org>
63  
64 rather than to the module maintainer directly. Many experienced and
65 reponsive experts will be able look at the problem and quickly
66 address it. Please include a thorough description of the problem
67 with code and data examples if at all possible.
68  
69 =head2 Reporting Bugs
70  
71 Report bugs to the Bioperl bug tracking system to help us keep track
72 of the bugs and their resolution. Bug reports can be submitted via the
73 web:
74  
75 https://redmine.open-bio.org/projects/bioperl/
76  
77 =head1 AUTHOR - Jason Stajich and Steve Chervitz
78  
79 Email jason-at-bioperl-dot-org
80 Email sac-at-bioperl-dot-org
81  
82 =head1 CONTRIBUTORS
83  
84 Sendu Bala, bix@sendu.me.uk
85  
86 =head1 APPENDIX
87  
88 The rest of the documentation details each of the object methods.
89 Internal methods are usually preceded with a _
90  
91 =cut
92  
93  
94 # Let the code begin...
95  
96  
97 package FAST::Bio::Search::Hit::GenericHit;
98 use strict;
99  
100 use FAST::Bio::Search::SearchUtils;
101  
102 use base qw(FAST::Bio::Root::Root FAST::Bio::Search::Hit::HitI);
103  
104 =head2 new
105  
106 Title : new
107 Usage : my $obj = FAST::Bio::Search::Hit::GenericHit->new();
108 Function: Builds a new FAST::Bio::Search::Hit::GenericHit object
109 Returns : FAST::Bio::Search::Hit::GenericHit
110 Args : -name => Name of Hit (required)
111 -description => Description (optional)
112 -accession => Accession number (optional)
113 -ncbi_gi => NCBI GI UID (optional)
114 -length => Length of the Hit (optional)
115 -score => Raw Score for the Hit (optional)
116 -bits => Bit Score for the Hit (optional)
117 -significance => Significance value for the Hit (optional)
118 -algorithm => Algorithm used (BLASTP, FASTX, etc...)
119 -hsps => Array ref of HSPs for this Hit.
120 -found_again => boolean, true if hit appears in a
121 "previously found" section of a PSI-Blast report.
122 -hsp_factory => FAST::Bio::Factory::ObjectFactoryI able to create HSPI
123 objects.
124  
125 =cut
126  
127 sub new {
128 my($class,@args) = @_;
129  
130 my $self = $class->SUPER::new(@args);
131 my ($hsps, $name,$query_len,$desc, $acc, $locus, $length,
132 $score,$algo,$signif,$bits, $p,
133 $rank, $hsp_factory, $gi, $iter, $found) = $self->_rearrange([qw(HSPS
134 NAME
135 QUERY_LEN
136 DESCRIPTION
137 ACCESSION
138 LOCUS
139 LENGTH SCORE ALGORITHM
140 SIGNIFICANCE BITS P
141 RANK
142 HSP_FACTORY
143 NCBI_GI
144 ITERATION
145 FOUND_AGAIN)], @args);
146
147 defined $query_len && $self->query_length($query_len);
148  
149 if( ! defined $name ) {
150 $self->throw("Must have defined a valid name for Hit");
151 } else {
152 $self->name($name);
153 }
154  
155 defined $acc && $self->accession($acc);
156 defined $locus && $self->locus($locus);
157 defined $desc && $self->description($desc);
158 defined $length && $self->length($length);
159 defined $algo && $self->algorithm($algo);
160 defined $signif && $self->significance($signif);
161 defined $score && $self->raw_score($score);
162 defined $bits && $self->bits($bits);
163 defined $rank && $self->rank($rank);
164 defined $hsp_factory && $self->hsp_factory($hsp_factory);
165 defined $gi && $self->ncbi_gi($gi);
166 defined $iter && $self->iteration($iter);
167 defined $found && $self->found_again($found);
168 # p() has a weird interface, so this is a hack workaround
169 if (defined $p) {
170 $self->{_p} = $p;
171 }
172  
173 $self->{'_iterator'} = 0;
174 if( defined $hsps ) {
175 if( ref($hsps) !~ /array/i ) {
176 $self->warn("Did not specify a valid array ref for the param HSPS ($hsps)");
177 } else {
178 my $hspcount=0;
179 while( @{$hsps} ) {
180 $hspcount++;
181 $self->add_hsp(shift @{$hsps} );
182 }
183 $self->{'_hsps'} = undef if $hspcount == 0;
184 }
185 }
186 else {
187 $self->{'_hsps'} = undef;
188 }
189  
190 return $self;
191 }
192  
193 =head2 add_hsp
194  
195 Title : add_hsp
196 Usage : $hit->add_hsp($hsp)
197 Function: Add a HSP to the collection of HSPs for a Hit
198 Returns : number of HSPs in the Hit
199 Args : FAST::Bio::Search::HSP::HSPI object, OR hash ref containing data suitable
200 for creating a HSPI object (&hsp_factory must be set to get it back)
201  
202 =cut
203  
204 sub add_hsp {
205 my ($self,$hsp) = @_;
206 if (!defined $hsp || (ref($hsp) ne 'HASH' && !$hsp->isa('FAST::Bio::Search::HSP::HSPI'))) {
207 $self->throw("Must provide a valid FAST::Bio::Search::HSP::HSPI object or hash ref to object: $self method: add_hsp value: $hsp");
208 return;
209 }
210
211 push @{$self->{'_hsps'}}, $hsp;
212 if (ref($hsp) eq 'HASH') {
213 $self->{_hashes}->{$#{$self->{'_hsps'}}} = 1;
214 }
215 return scalar @{$self->{'_hsps'}};
216 }
217  
218 =head2 hsp_factory
219  
220 Title : hsp_factory
221 Usage : $hit->hsp_factory($hsp_factory)
222 Function: Get/set the factory used to build HSPI objects if necessary.
223 Returns : FAST::Bio::Factory::ObjectFactoryI
224 Args : FAST::Bio::Factory::ObjectFactoryI
225  
226 =cut
227  
228 sub hsp_factory {
229 my $self = shift;
230 if (@_) { $self->{_hsp_factory} = shift }
231 return $self->{_hsp_factory} || return;
232 }
233  
234 =head2 FAST::Bio::Search::Hit::HitI methods
235  
236 Implementation of FAST::Bio::Search::Hit::HitI methods
237  
238 =head2 name
239  
240 Title : name
241 Usage : $hit_name = $hit->name();
242 Function: returns the name of the Hit sequence
243 Returns : a scalar string
244 Args : [optional] scalar string to set the name
245  
246 =cut
247  
248 sub name {
249 my ($self,$value) = @_;
250 my $previous = $self->{'_name'};
251 if( defined $value || ! defined $previous ) {
252 $value = $previous = '' unless defined $value;
253 $self->{'_name'} = $value;
254 }
255 return $previous;
256 }
257  
258 =head2 accession
259  
260 Title : accession
261 Usage : $acc = $hit->accession();
262 Function: Retrieve the accession (if available) for the hit
263 Returns : a scalar string (empty string if not set)
264 Args : none
265  
266 =cut
267  
268 sub accession {
269 my ($self,$value) = @_;
270 my $previous = $self->{'_accession'};
271 if( defined $value || ! defined $previous ) {
272 $value = $previous = '' unless defined $value;
273 $self->{'_accession'} = $value;
274 }
275 return $previous;
276 }
277  
278 =head2 description
279  
280 Title : description
281 Usage : $desc = $hit->description();
282 Function: Retrieve the description for the hit
283 Returns : a scalar string
284 Args : [optional] scalar string to set the descrition
285  
286 =cut
287  
288 sub description {
289 my ($self,$value) = @_;
290 my $previous = $self->{'_description'};
291 if( defined $value || ! defined $previous ) {
292 $value = $previous = '' unless defined $value;
293 $self->{'_description'} = $value;
294 }
295 return $previous;
296 }
297  
298 =head2 length
299  
300 Title : length
301 Usage : my $len = $hit->length
302 Function: Returns the length of the hit
303 Returns : integer
304 Args : [optional] integer to set the length
305  
306 =cut
307  
308 sub length {
309 my ($self,$value) = @_;
310 my $previous = $self->{'_length'};
311 if( defined $value || ! defined $previous ) {
312 $value = $previous = 0 unless defined $value;
313 $self->{'_length'} = $value;
314 }
315 return $previous;
316 }
317  
318  
319 =head2 algorithm
320  
321 Title : algorithm
322 Usage : $alg = $hit->algorithm();
323 Function: Gets the algorithm specification that was used to obtain the hit
324 For BLAST, the algorithm denotes what type of sequence was aligned
325 against what (BLASTN: dna-dna, BLASTP prt-prt, BLASTX translated
326 dna-prt, TBLASTN prt-translated dna, TBLASTX translated
327 dna-translated dna).
328 Returns : a scalar string
329 Args : [optional] scalar string to set the algorithm
330  
331 =cut
332  
333 sub algorithm {
334 my ($self,$value) = @_;
335 my $previous = $self->{'_algorithm'};
336 if( defined $value || ! defined $previous ) {
337 $value = $previous = '' unless defined $value;
338 $self->{'_algorithm'} = $value;
339 }
340 return $previous;
341 }
342  
343 =head2 raw_score
344  
345 Title : raw_score
346 Usage : $score = $hit->raw_score();
347 Function: Gets the "raw score" generated by the algorithm. What
348 this score is exactly will vary from algorithm to algorithm,
349 returning undef if unavailable.
350 Returns : a scalar value
351 Args : [optional] scalar value to set the raw score
352  
353 =cut
354  
355 sub raw_score {
356 my ($self,$value) = @_;
357 my $previous = $self->{'_score'};
358 if( defined $value ) {
359 $self->{'_score'} = $value;
360 } elsif ( ! defined $previous ) {
361 # Set the bits of the Hit to that of the top HSP.
362 unless( defined $self->{'_hsps'}->[0] ) {
363 $self->warn("No HSPs for this minimal Hit (".$self->name.")\n".
364 "If using NCBI BLAST, check bits() instead");
365 return;
366 }
367 # use 'score' if available
368 if ( defined( ($self->hsps)[0]->score ) ) {
369 $previous = $self->{'_score'} = ($self->hsps)[0]->score;
370 }
371 # otherwise use 'bits'
372 elsif ( defined( ($self->hsps)[0]->bits ) ) {
373 $previous = $self->{'_score'} = ($self->hsps)[0]->bits;
374 }
375 }
376 return $previous;
377 }
378  
379 =head2 score
380  
381 Equivalent to L<raw_score()|raw_score>
382  
383 =cut
384  
385 sub score { shift->raw_score(@_); }
386  
387 =head2 significance
388  
389 Title : significance
390 Usage : $significance = $hit->significance();
391 Function: Used to obtain the E or P value of a hit, i.e. the probability that
392 this particular hit was obtained purely by random chance. If
393 information is not available (nor calculatable from other
394 information sources), return undef.
395 Returns : a scalar value or undef if unavailable
396 Args : [optional] scalar value to set the significance
397  
398 =cut
399  
400 sub significance {
401 my ($self,$value) = @_;
402 my $previous = $self->{'_significance'};
403 if( defined $value ) {
404 $self->{'_significance'} = $value;
405 } elsif ( ! defined $previous ) {
406 unless( defined $self->{'_hsps'}->[0] ) {
407 $self->warn("No HSPs for this Hit (".$self->name.")");
408 return;
409 }
410 # Set the significance of the Hit to that of the top HSP.
411 $previous = $self->{'_significance'} = ($self->hsps)[0]->significance;
412 }
413  
414 return $previous;
415 }
416  
417 =head2 bits
418  
419 Usage : $hit_object->bits();
420 Purpose : Gets the bit score of the best HSP for the current hit.
421 Example : $bits = $hit_object->bits();
422 Returns : Integer or undef if bit score is not set
423 Argument : n/a
424 Comments : For BLAST1, the non-bit score is listed in the summary line.
425  
426 See Also : L<score()|score>
427  
428 =cut
429  
430 sub bits {
431 my ($self,$value) = @_;
432 my $previous = $self->{'_bits'};
433 if( defined $value ) {
434 $self->{'_bits'} = $value;
435 } elsif ( ! defined $previous ) {
436 # Set the bits of the Hit to that of the top HSP.
437 unless( defined $self->{'_hsps'}->[0] ) {
438 $self->warn("No HSPs for this minimal Hit (".$self->name.")\n".
439 "If using WU-BLAST, check raw_score() instead");
440 return;
441 }
442 $previous = $self->{'_bits'} = ($self->hsps)[0]->bits;
443 }
444 return $previous;
445 }
446  
447 =head2 next_hsp
448  
449 Title : next_hsp
450 Usage : while( $hsp = $obj->next_hsp()) { ... }
451 Function : Returns the next available High Scoring Pair
452 Example :
453 Returns : FAST::Bio::Search::HSP::HSPI object or null if finished
454 Args : none
455  
456 =cut
457  
458 sub next_hsp {
459 my $self = shift;
460 $self->{'_iterator'} = 0 unless defined $self->{'_iterator'};
461 return unless
462 defined($self->{'_hsps'})
463 && $self->{'_iterator'} <= scalar @{$self->{'_hsps'}};
464
465 my $iterator = $self->{'_iterator'}++;
466 my $hsp = $self->{'_hsps'}->[$iterator] || return;
467 if (ref($hsp) eq 'HASH') {
468 my $factory = $self->hsp_factory || $self->throw("Tried to get a HSP, but it was a hash ref and we have no hsp factory");
469 $hsp = $factory->create_object(%{$hsp});
470 $self->{'_hsps'}->[$iterator] = $hsp;
471 delete $self->{_hashes}->{$iterator};
472 }
473 return $hsp;
474 }
475  
476  
477 =head2 hsps
478  
479 Usage : $hit_object->hsps();
480 Purpose : Get a list containing all HSP objects.
481 : Get the numbers of HSPs for the current hit.
482 Example : @hsps = $hit_object->hsps();
483 : $num = $hit_object->hsps(); # alternatively, use num_hsps()
484 Returns : Array context : list of FAST::Bio::Search::HSP::BlastHSP.pm objects.
485 : Scalar context: integer (number of HSPs).
486 : (Equivalent to num_hsps()).
487 Argument : n/a. Relies on wantarray
488 Throws : Exception if the HSPs have not been collected.
489  
490 See Also : L<hsp()|hsp>, L<num_hsps()|num_hsps>
491  
492 =cut
493  
494 sub hsps {
495 my $self = shift;
496 foreach my $i (keys %{$self->{_hashes} || {}}) {
497 my $factory = $self->hsp_factory || $self->throw("Tried to get a HSP, but it was a hash ref and we have no hsp factory");
498 $self->{'_hsps'}->[$i] = $factory->create_object(%{$self->{'_hsps'}->[$i]});
499 delete $self->{_hashes}->{$i};
500 }
501
502 return wantarray() ? @{$self->{'_hsps'} || []} : scalar(@{$self->{'_hsps'} || []});
503 }
504  
505 =head2 num_hsps
506  
507 Usage : $hit_object->num_hsps();
508 Purpose : Get the number of HSPs for the present hit.
509 Example : $nhsps = $hit_object->num_hsps();
510 Returns : Integer or '-' if HSPs have not been callected
511 Argument : n/a
512  
513 See Also : L<hsps()|hsps>
514  
515 =cut
516  
517 sub num_hsps {
518 my $self = shift;
519
520 unless ($self->{'_hsps'}) {
521 return '-';
522 }
523
524 return scalar(@{$self->{'_hsps'}});
525 }
526  
527 =head2 rewind
528  
529 Title : rewind
530 Usage : $hit->rewind;
531 Function: Allow one to reset the HSP iterator to the beginning
532 Since this is an in-memory implementation
533 Returns : none
534 Args : none
535  
536 =cut
537  
538 sub rewind{
539 my ($self) = @_;
540 $self->{'_iterator'} = 0;
541 }
542  
543 =head2 ambiguous_aln
544  
545 Usage : $ambig_code = $hit_object->ambiguous_aln();
546 Purpose : Sets/Gets ambiguity code data member.
547 Example : (see usage)
548 Returns : String = 'q', 's', 'qs', '-'
549 : 'q' = query sequence contains overlapping sub-sequences
550 : while sbjct does not.
551 : 's' = sbjct sequence contains overlapping sub-sequences
552 : while query does not.
553 : 'qs' = query and sbjct sequence contains overlapping sub-sequences
554 : relative to each other.
555 : '-' = query and sbjct sequence do not contains multiple domains
556 : relative to each other OR both contain the same distribution
557 : of similar domains.
558 Argument : n/a
559 Throws : n/a
560 Comment : Note: "sbjct" is synonymous with "hit"
561  
562 =cut
563  
564 sub ambiguous_aln {
565 my $self = shift;
566 if(@_) { $self->{'_ambiguous_aln'} = shift; }
567 $self->{'_ambiguous_aln'} || '-';
568 }
569  
570 =head2 overlap
571  
572 See documentation in L<FAST::Bio::Search::Hit::HitI::overlap()|FAST::Bio::Search::Hit::HitI>
573  
574 =cut
575  
576 sub overlap {
577 my $self = shift;
578 if(@_) { $self->{'_overlap'} = shift; }
579 defined $self->{'_overlap'} ? $self->{'_overlap'} : 0;
580 }
581  
582  
583 =head2 n
584  
585 Usage : $hit_object->n();
586 Purpose : Gets the N number for the current hit.
587 : This is the number of HSPs in the set which was ascribed
588 : the lowest P-value (listed on the description line).
589 : This number is not the same as the total number of HSPs.
590 : To get the total number of HSPs, use num_hsps().
591 Example : $n = $hit_object->n();
592 Returns : Integer
593 Argument : n/a
594 Throws : Exception if HSPs have not been set (BLAST2 reports).
595 Comments : Note that the N parameter is not reported in gapped BLAST2.
596 : Calling n() on such reports will result in a call to num_hsps().
597 : The num_hsps() method will count the actual number of
598 : HSPs in the alignment listing, which may exceed N in
599 : some cases.
600  
601 See Also : L<num_hsps()|num_hsps>
602  
603 =cut
604  
605 sub n {
606 my $self = shift;
607  
608 # The check for $self->{'_n'} is a remnant from the 'query' mode days
609 # in which the sbjct object would collect data from the description
610 # line only.
611  
612 my ($n);
613 if(not defined($self->{'_n'})) {
614 if( $self->hsp ) {
615 $n = $self->hsp->n;
616 }
617 } else {
618 $n = $self->{'_n'};
619 }
620 $n ||= $self->num_hsps;
621  
622 return $n;
623 }
624  
625 =head2 p
626  
627 Usage : $hit_object->p( [format] );
628 Purpose : Get the P-value for the best HSP of the given BLAST hit.
629 : (Note that P-values are not provided with NCBI Blast2 reports).
630 Example : $p = $sbjct->p;
631 : $p = $sbjct->p('exp'); # get exponent only.
632 : ($num, $exp) = $sbjct->p('parts'); # split sci notation into parts
633 Returns : Float or scientific notation number (the raw P-value, DEFAULT).
634 : Integer if format == 'exp' (the magnitude of the base 10 exponent).
635 : 2-element list (float, int) if format == 'parts' and P-value
636 : is in scientific notation (See Comments).
637 Argument : format: string of 'raw' | 'exp' | 'parts'
638 : 'raw' returns value given in report. Default. (1.2e-34)
639 : 'exp' returns exponent value only (34)
640 : 'parts' returns the decimal and exponent as a
641 : 2-element list (1.2, -34) (See Comments).
642 Throws : Warns if no P-value is defined. Uses expect instead.
643 Comments : Using the 'parts' argument is not recommended since it will not
644 : work as expected if the P-value is not in scientific notation.
645 : That is, floats are not converted into sci notation before
646 : splitting into parts.
647  
648 See Also : L<expect()|expect>, L<significance()|significance>, L<FAST::Bio::Search::SearchUtils::get_exponent()|FAST::Bio::Search::SearchUtils>
649  
650 =cut
651  
652 sub p {
653 # Some duplication of logic for p(), expect() and signif() for the sake of performance.
654 my ($self, $fmt) = @_;
655  
656 my $val = $self->{'_p'};
657  
658 # $val can be zero.
659 if(!defined $val) {
660 # P-value not defined, must be a NCBI Blast2 report.
661 # Use expect instead.
662 $self->warn( "P-value not defined. Using significance() instead.");
663 $val = $self->significance();
664 }
665  
666 return $val if not $fmt or $fmt =~ /^raw/i;
667 ## Special formats: exponent-only or as list.
668 return &FAST::Bio::Search::SearchUtils::get_exponent($val) if $fmt =~ /^exp/i;
669 return (split (/eE/, $val)) if $fmt =~ /^parts/i;
670  
671 ## Default: return the raw P-value.
672 return $val;
673 }
674  
675 =head2 hsp
676  
677 Usage : $hit_object->hsp( [string] );
678 Purpose : Get a single HSPI object for the present HitI object.
679 Example : $hspObj = $hit_object->hsp; # same as 'best'
680 : $hspObj = $hit_object->hsp('best');
681 : $hspObj = $hit_object->hsp('worst');
682 Returns : Object reference for a FAST::Bio::Search::HSP::BlastHSP.pm object.
683 Argument : String (or no argument).
684 : No argument (default) = highest scoring HSP (same as 'best').
685 : 'best' or 'first' = highest scoring HSP.
686 : 'worst' or 'last' = lowest scoring HSP.
687 Throws : Exception if the HSPs have not been collected.
688 : Exception if an unrecognized argument is used.
689  
690 See Also : L<hsps()|hsps>, L<num_hsps>()
691  
692 =cut
693  
694 sub hsp {
695 my( $self, $option ) = @_;
696 $option ||= 'best';
697
698 if (not ref $self->{'_hsps'}) {
699 $self->throw("Can't get HSPs: data not collected.");
700 }
701  
702 my @hsps = $self->hsps;
703
704 return $hsps[0] if $option =~ /best|first|1/i;
705 return $hsps[$#hsps] if $option =~ /worst|last/i;
706  
707 $self->throw("Can't get HSP for: $option\n" .
708 "Valid arguments: 'best', 'worst'");
709 }
710  
711 =head2 logical_length
712  
713 Usage : $hit_object->logical_length( [seq_type] );
714 : (mostly intended for internal use).
715 Purpose : Get the logical length of the hit sequence.
716 : This is necessary since the number of identical/conserved residues
717 : can be in terms of peptide sequence space, yet the query and/or hit
718 : sequence are in nucleotide space.
719 Example : $len = $hit_object->logical_length();
720 Returns : Integer
721 Argument : seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
722 ('sbjct' is synonymous with 'hit')
723 Throws : n/a
724 Comments :
725 : In the case of BLAST flavors:
726 : For TBLASTN reports, the length of the aligned portion of the
727 : nucleotide hit sequence is divided by 3; for BLASTX reports,
728 : the length of the aligned portion of the nucleotide query
729 : sequence is divided by 3. For TBLASTX reports, the length of
730 : both hit and query sequence are converted.
731 :
732 : This is important for functions like frac_aligned_query()
733 : which need to operate in amino acid coordinate space when dealing
734 : with [T]BLAST[NX] type reports.
735  
736 See Also : L<length()|length>, L<frac_aligned_query()|frac_aligned_query>, L<frac_aligned_hit()|frac_aligned_hit>
737  
738 =cut
739  
740 sub logical_length {
741 my $self = shift;
742 my $seqType = shift || 'query';
743 $seqType = 'sbjct' if $seqType eq 'hit';
744  
745 my ($length, $logical);
746 my $algo = $self->algorithm;
747  
748 # For the sbjct, return logical sbjct length
749 if( $seqType eq 'sbjct' ) {
750 $length = $self->length;
751 } else {
752 # Otherwise, return logical query length
753 $length = $self->query_length();
754 $self->throw("Must have defined query_len") unless ( $length );
755 }
756  
757 $logical = FAST::Bio::Search::SearchUtils::logical_length($algo, $seqType, $length);
758  
759 return int($logical);
760 }
761  
762 =head2 length_aln
763  
764 Usage : $hit_object->length_aln( [seq_type] );
765 Purpose : Get the total length of the aligned region for query or sbjct seq.
766 : This number will include all HSPs
767 Example : $len = $hit_object->length_aln(); # default = query
768 : $lenAln = $hit_object->length_aln('query');
769 Returns : Integer
770 Argument : seq_Type = 'query' or 'hit' or 'sbjct' (Default = 'query')
771 ('sbjct' is synonymous with 'hit')
772 Throws : Exception if the argument is not recognized.
773 Comments : This method will report the logical length of the alignment,
774 : meaning that for TBLAST[NX] reports, the length is reported
775 : using amino acid coordinate space (i.e., nucleotides / 3).
776 :
777 : This method requires that all HSPs be tiled. If they have not
778 : already been tiled, they will be tiled first automatically..
779 : If you don't want the tiled data, iterate through each HSP
780 : calling length() on each (use hsps() to get all HSPs).
781  
782 See Also : L<length()|length>, L<frac_aligned_query()|frac_aligned_query>, L<frac_aligned_hit()|frac_aligned_hit>, L<gaps()|gaps>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>, L<FAST::Bio::Search::HSP::BlastHSP::length()|FAST::Bio::Search::HSP::BlastHSP>
783  
784 =cut
785  
786 sub length_aln {
787 my( $self, $seqType, $num ) = @_;
788  
789 $seqType ||= 'query';
790 $seqType = 'sbjct' if $seqType eq 'hit';
791  
792 # Setter:
793 if( defined $num) {
794 return $self->{'_length_aln_'.$seqType} = $num;
795 }
796  
797 unless ($self->{'_hsps'}) {
798 #return wantarray ? ('-','-') : '-';
799 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
800 return '-';
801 }
802  
803 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
804  
805 my $data = $self->{'_length_aln_'.$seqType};
806
807 ## If we don't have data, figure out what went wrong.
808 if(!$data) {
809 $self->throw("Can't get length aln for sequence type \"$seqType\". " .
810 "Valid types are 'query', 'hit', 'sbjct' ('sbjct' = 'hit')");
811 }
812 return $data;
813 }
814  
815 =head2 gaps
816  
817 Usage : $hit_object->gaps( [seq_type] );
818 Purpose : Get the number of gaps in the aligned query, hit, or both sequences.
819 : Data is summed across all HSPs.
820 Example : $qgaps = $hit_object->gaps('query');
821 : $hgaps = $hit_object->gaps('hit');
822 : $tgaps = $hit_object->gaps(); # default = total (query + hit)
823 Returns : scalar context: integer
824 : array context without args: two-element list of integers
825 : (queryGaps, hitGaps)
826 : Array context can be forced by providing an argument of 'list' or 'array'.
827 :
828 : CAUTION: Calling this method within printf or sprintf is arrray context.
829 : So this function may not give you what you expect. For example:
830 : printf "Total gaps: %d", $hit->gaps();
831 : Actually returns a two-element array, so what gets printed
832 : is the number of gaps in the query, not the total
833 :
834 Argument : seq_type: 'query' | 'hit' or 'sbjct' | 'total' | 'list' (default = 'total')
835 ('sbjct' is synonymous with 'hit')
836 Throws : n/a
837 Comments : If you need data for each HSP, use hsps() and then interate
838 : through each HSP object.
839 : This method requires that all HSPs be tiled. If they have not
840 : already been tiled, they will be tiled first automatically..
841 : Not relying on wantarray since that will fail in situations
842 : such as printf "%d", $hit->gaps() in which you might expect to
843 : be printing the total gaps, but evaluates to array context.
844  
845 See Also : L<length_aln()|length_aln>
846  
847 =cut
848  
849 sub gaps {
850 my( $self, $seqType, $num ) = @_;
851  
852 $seqType ||= (wantarray ? 'list' : 'total');
853 $seqType = 'sbjct' if $seqType eq 'hit';
854  
855 unless ($self->{'_hsps'}) {
856 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
857 return wantarray ? ('-','-') : '-';
858 #return '-';
859 }
860  
861 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
862  
863 $seqType = lc($seqType);
864  
865 if( defined $num ) {
866 $self->throw("Can't set gaps for seqType '$seqType'. Must be 'query' or 'hit'\n") unless ($seqType eq 'sbjct' or $seqType eq 'query');
867  
868 return $self->{'_gaps_'.$seqType} = $num;
869 }
870 elsif($seqType =~ /list|array/i) {
871 return ($self->{'_gaps_query'}, $self->{'_gaps_sbjct'});
872 }
873 elsif($seqType eq 'total') {
874 return ($self->{'_gaps_query'} + $self->{'_gaps_sbjct'}) || 0;
875 } else {
876 return $self->{'_gaps_'.$seqType} || 0;
877 }
878 }
879  
880  
881 =head2 matches
882  
883 See documentation in L<FAST::Bio::Search::Hit::HitI::matches()|FAST::Bio::Search::Hit::HitI>
884  
885 =cut
886  
887 sub matches {
888 my( $self, $arg1, $arg2) = @_;
889 my(@data,$data);
890  
891 unless ($self->{'_hsps'}) {
892 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
893 return wantarray ? ('-','-') : '-';
894 }
895  
896 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
897  
898 unless( $arg1 ) {
899 @data = ($self->{'_totalIdentical'}, $self->{'_totalConserved'});
900  
901 return @data;
902 } else {
903  
904 if( defined $arg2 ) {
905 $self->{'_totalIdentical'} = $arg1;
906 $self->{'_totalConserved'} = $arg2;
907 return ( $arg1, $arg2 );
908 }
909 elsif($arg1 =~ /^id/i) {
910 $data = $self->{'_totalIdentical'};
911 } else {
912 $data = $self->{'_totalConserved'};
913 }
914 #print STDERR "\nmatches(): id=$self->{'_totalIdentical'}, cons=$self->{'_totalConserved'}\n\n";
915 return $data;
916 }
917
918 ## If we make it to here, it is likely the case that
919 ## the parser constructed a minimal hit object from the summary line only.
920 ## It either delibrately skipped parsing the alignment section,
921 ## or was not able to because it was absent (due to blast executable parameter
922 ## setting such as -b 0 (B=0 for WU-BLAST) )
923 #$self->throw("Can't get identical or conserved data: no data.");
924 }
925  
926  
927 =head2 start
928  
929 Usage : $sbjct->start( [seq_type] );
930 Purpose : Gets the start coordinate for the query, sbjct, or both sequences
931 : in the BlastHit object. If there is more than one HSP, the lowest start
932 : value of all HSPs is returned.
933 Example : $qbeg = $sbjct->start('query');
934 : $sbeg = $sbjct->start('hit');
935 : ($qbeg, $sbeg) = $sbjct->start();
936 Returns : scalar context: integer
937 : array context without args: list of two integers (queryStart, sbjctStart)
938 : Array context can be "induced" by providing an argument of 'list' or 'array'.
939 Argument : In scalar context: seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
940 ('sbjct' is synonymous with 'hit')
941 Throws : n/a
942  
943 See Also : L<end()|end>, L<range()|range>, L<strand()|strand>,
944 L<FAST::Bio::Search::HSP::BlastHSP::start|FAST::Bio::Search::HSP::BlastHSP>
945  
946 =cut
947  
948 sub start {
949 my ($self, $seqType, $num) = @_;
950  
951 unless ($self->{'_hsps'}) {
952 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
953 return wantarray ? ('-','-') : '-';
954 }
955  
956 $seqType ||= (wantarray ? 'list' : 'query');
957 $seqType = 'sbjct' if $seqType eq 'hit';
958  
959 if( defined $num ) {
960 $seqType = "_\L$seqType\E";
961 return $self->{$seqType.'Start'} = $num;
962 }
963  
964 # If there is only one HSP, defer this call to the solitary HSP.
965 if($self->num_hsps == 1) {
966 return $self->hsp->start($seqType);
967 }
968 else {
969 # Tiling normally generates $self->{'_queryStart'} and
970 # $self->{'_sbjctStart'}, but is very slow. If we haven't tiled,
971 # find the answer quickly without tiling.
972 unless (defined $self->{'_queryStart'}) {
973 my $earliest_query_start;
974 my $earliest_sbjct_start;
975 foreach my $hsp ($self->hsps) {
976 my $this_query_start = $hsp->start('query');
977 if (! defined $earliest_query_start || $this_query_start < $earliest_query_start) {
978 $earliest_query_start = $this_query_start;
979 }
980
981 my $this_sbjct_start = $hsp->start('sbjct');
982 if (! defined $earliest_sbjct_start || $this_sbjct_start < $earliest_sbjct_start) {
983 $earliest_sbjct_start = $this_sbjct_start;
984 }
985 }
986 $self->{'_queryStart'} = $earliest_query_start;
987 $self->{'_sbjctStart'} = $earliest_sbjct_start;
988 }
989
990
991 if ($seqType =~ /list|array/i) {
992 return ($self->{'_queryStart'}, $self->{'_sbjctStart'});
993 } else {
994 ## Sensitive to member name changes.
995 $seqType = "_\L$seqType\E";
996 return $self->{$seqType.'Start'};
997 }
998 }
999 }
1000  
1001  
1002 =head2 end
1003  
1004 Usage : $sbjct->end( [seq_type] );
1005 Purpose : Gets the end coordinate for the query, sbjct, or both sequences
1006 : in the BlastHit object. If there is more than one HSP,
1007 the largest end
1008 : value of all HSPs is returned.
1009 Example : $qend = $sbjct->end('query');
1010 : $send = $sbjct->end('hit');
1011 : ($qend, $send) = $sbjct->end();
1012 Returns : scalar context: integer
1013 : array context without args: list of two integers
1014 : (queryEnd, sbjctEnd)
1015 : Array context can be "induced" by providing an argument
1016 : of 'list' or 'array'.
1017 Argument : In scalar context: seq_type = 'query' or 'sbjct'
1018 : (case insensitive). If not supplied, 'query' is used.
1019 Throws : n/a
1020  
1021 See Also : L<start()|start>, L<range()|range>, L<strand()|strand>
1022  
1023 =cut
1024  
1025 sub end {
1026 my ($self, $seqType, $num) = @_;
1027  
1028 unless ($self->{'_hsps'}) {
1029 return wantarray ? ('-','-') : '-';
1030 }
1031  
1032 $seqType ||= (wantarray ? 'list' : 'query');
1033 $seqType = 'sbjct' if $seqType eq 'hit';
1034  
1035 if( defined $num ) {
1036 $seqType = "_\L$seqType\E";
1037 return $self->{$seqType.'Stop'} = $num;
1038 }
1039  
1040 # If there is only one HSP, defer this call to the solitary HSP.
1041 if($self->num_hsps == 1) {
1042 return $self->hsp->end($seqType);
1043 }
1044 else {
1045 # Tiling normally generates $self->{'_queryStop'} and
1046 # $self->{'_sbjctStop'}, but is very slow. If we haven't tiled,
1047 # find the answer quickly without tiling.
1048 unless (defined $self->{'_queryStop'}) {
1049 my $latest_query_end;
1050 my $latest_sbjct_end;
1051 foreach my $hsp ($self->hsps) {
1052 my $this_query_end = $hsp->end('query');
1053 if (! defined $latest_query_end || $this_query_end > $latest_query_end) {
1054 $latest_query_end = $this_query_end;
1055 }
1056
1057 my $this_sbjct_end = $hsp->end('sbjct');
1058 if (! defined $latest_sbjct_end || $this_sbjct_end > $latest_sbjct_end) {
1059 $latest_sbjct_end = $this_sbjct_end;
1060 }
1061 }
1062 $self->{'_queryStop'} = $latest_query_end;
1063 $self->{'_sbjctStop'} = $latest_sbjct_end;
1064 }
1065
1066
1067 if($seqType =~ /list|array/i) {
1068 return ($self->{'_queryStop'}, $self->{'_sbjctStop'});
1069 } else {
1070 ## Sensitive to member name changes.
1071 $seqType = "_\L$seqType\E";
1072 return $self->{$seqType.'Stop'};
1073 }
1074 }
1075 }
1076  
1077 =head2 range
1078  
1079 Usage : $sbjct->range( [seq_type] );
1080 Purpose : Gets the (start, end) coordinates for the query or sbjct sequence
1081 : in the HSP alignment.
1082 Example : ($qbeg, $qend) = $sbjct->range('query');
1083 : ($sbeg, $send) = $sbjct->range('hit');
1084 Returns : Two-element array of integers
1085 Argument : seq_type = string, 'query' or 'hit' or 'sbjct' (default = 'query')
1086 ('sbjct' is synonymous with 'hit')
1087 Throws : n/a
1088  
1089 See Also : L<start()|start>, L<end()|end>
1090  
1091 =cut
1092  
1093 sub range {
1094 my ($self, $seqType) = @_;
1095 $seqType ||= 'query';
1096 $seqType = 'sbjct' if $seqType eq 'hit';
1097 return ($self->start($seqType), $self->end($seqType));
1098 }
1099  
1100  
1101 =head2 frac_identical
1102  
1103 Usage : $hit_object->frac_identical( [seq_type] );
1104 Purpose : Get the overall fraction of identical positions across all HSPs.
1105 : The number refers to only the aligned regions and does not
1106 : account for unaligned regions in between the HSPs, if any.
1107 Example : $frac_iden = $hit_object->frac_identical('query');
1108 Returns : Float (2-decimal precision, e.g., 0.75).
1109 Argument : seq_type: 'query' | 'hit' or 'sbjct' | 'total'
1110 : default = 'query' (but see comments below).
1111 : ('sbjct' is synonymous with 'hit')
1112 Throws : n/a
1113 Comments :
1114 : To compute the fraction identical, the logical length of the
1115 : aligned portion of the sequence is used, meaning that
1116 : in the case of BLAST flavors, for TBLASTN reports, the length of
1117 : the aligned portion of the
1118 : nucleotide hit sequence is divided by 3; for BLASTX reports,
1119 : the length of the aligned portion of the nucleotide query
1120 : sequence is divided by 3. For TBLASTX reports, the length of
1121 : both hit and query sequence are converted.
1122 : This is necessary since the number of identical residues is
1123 : in terms of peptide sequence space.
1124 :
1125 : Different versions of Blast report different values for the total
1126 : length of the alignment. This is the number reported in the
1127 : denominators in the stats section:
1128 : "Identical = 34/120 Positives = 67/120".
1129 : NCBI BLAST uses the total length of the alignment (with gaps)
1130 : WU-BLAST uses the length of the query sequence (without gaps).
1131 :
1132 : Therefore, when called with an argument of 'total',
1133 : this method will report different values depending on the
1134 : version of BLAST used. Total does NOT take into account HSP
1135 : tiling, so it should not be used.
1136 :
1137 : To get the fraction identical among only the aligned residues,
1138 : ignoring the gaps, call this method without an argument or
1139 : with an argument of 'query' or 'hit'.
1140 :
1141 : If you need data for each HSP, use hsps() and then iterate
1142 : through the HSP objects.
1143 : This method requires that all HSPs be tiled. If they have not
1144 : already been tiled, they will be tiled first automatically.
1145  
1146 See Also : L<frac_conserved()|frac_conserved>, L<frac_aligned_query()|frac_aligned_query>, L<matches()|matches>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>
1147  
1148 =cut
1149  
1150 sub frac_identical {
1151 my ($self, $seqType) = @_;
1152 $seqType ||= 'query';
1153 $seqType = 'sbjct' if $seqType eq 'hit';
1154  
1155 ## Sensitive to member name format.
1156 $seqType = lc($seqType);
1157  
1158 unless ($self->{'_hsps'}) {
1159 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1160 #return wantarray ? ('-','-') : '-';
1161 return '-';
1162 }
1163  
1164 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1165  
1166 my $ident = $self->matches('id');
1167 my $total = $self->length_aln($seqType);
1168 my $ratio = $ident / $total;
1169 my $ratio_rounded = sprintf( "%.3f", $ratio);
1170  
1171 # Round down iff normal rounding yields 1 (just like blast)
1172 $ratio_rounded = 0.999 if (($ratio_rounded == 1) && ($ratio < 1));
1173 return $ratio_rounded;
1174 }
1175  
1176  
1177 =head2 frac_conserved
1178  
1179 Usage : $hit_object->frac_conserved( [seq_type] );
1180 Purpose : Get the overall fraction of conserved positions across all HSPs.
1181 : The number refers to only the aligned regions and does not
1182 : account for unaligned regions in between the HSPs, if any.
1183 Example : $frac_cons = $hit_object->frac_conserved('hit');
1184 Returns : Float (2-decimal precision, e.g., 0.75).
1185 Argument : seq_type: 'query' | 'hit' or 'sbjct' | 'total'
1186 : default = 'query' (but see comments below).
1187 : ('sbjct' is synonymous with 'hit')
1188 Throws : n/a
1189 Comments :
1190 : To compute the fraction conserved, the logical length of the
1191 : aligned portion of the sequence is used, meaning that
1192 : in the case of BLAST flavors, for TBLASTN reports, the length of
1193 : the aligned portion of the
1194 : nucleotide hit sequence is divided by 3; for BLASTX reports,
1195 : the length of the aligned portion of the nucleotide query
1196 : sequence is divided by 3. For TBLASTX reports, the length of
1197 : both hit and query sequence are converted.
1198 : This is necessary since the number of conserved residues is
1199 : in terms of peptide sequence space.
1200 :
1201 : Different versions of Blast report different values for the total
1202 : length of the alignment. This is the number reported in the
1203 : denominators in the stats section:
1204 : "Positives = 34/120 Positives = 67/120".
1205 : NCBI BLAST uses the total length of the alignment (with gaps)
1206 : WU-BLAST uses the length of the query sequence (without gaps).
1207 :
1208 : Therefore, when called with an argument of 'total',
1209 : this method will report different values depending on the
1210 : version of BLAST used. Total does NOT take into account HSP
1211 : tiling, so it should not be used.
1212 :
1213 : To get the fraction conserved among only the aligned residues,
1214 : ignoring the gaps, call this method without an argument or
1215 : with an argument of 'query' or 'hit'.
1216 :
1217 : If you need data for each HSP, use hsps() and then interate
1218 : through the HSP objects.
1219 : This method requires that all HSPs be tiled. If they have not
1220 : already been tiled, they will be tiled first automatically.
1221  
1222 See Also : L<frac_identical()|frac_identical>, L<matches()|matches>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>
1223  
1224 =cut
1225  
1226 sub frac_conserved {
1227 my ($self, $seqType) = @_;
1228 $seqType ||= 'query';
1229 $seqType = 'sbjct' if $seqType eq 'hit';
1230  
1231 ## Sensitive to member name format.
1232 $seqType = lc($seqType);
1233  
1234 unless ($self->{'_hsps'}) {
1235 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1236 #return wantarray ? ('-','-') : '-';
1237 return '-';
1238 }
1239  
1240 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1241  
1242 my $consv = $self->matches('cons');
1243 my $total = $self->length_aln($seqType);
1244 my $ratio = $consv / $total;
1245 my $ratio_rounded = sprintf( "%.3f", $ratio);
1246  
1247 # Round down iff normal rounding yields 1 (just like blast)
1248 $ratio_rounded = 0.999 if (($ratio_rounded == 1) && ($ratio < 1));
1249 return $ratio_rounded;
1250 }
1251  
1252  
1253  
1254  
1255 =head2 frac_aligned_query
1256  
1257 Usage : $hit_object->frac_aligned_query();
1258 Purpose : Get the fraction of the query sequence which has been aligned
1259 : across all HSPs (not including intervals between non-overlapping
1260 : HSPs).
1261 Example : $frac_alnq = $hit_object->frac_aligned_query();
1262 Returns : Float (2-decimal precision, e.g., 0.75).
1263 Argument : n/a
1264 Throws : n/a
1265 Comments : If you need data for each HSP, use hsps() and then interate
1266 : through the HSP objects.
1267 : This method requires that all HSPs be tiled. If they have not
1268 : already been tiled, they will be tiled first automatically.
1269  
1270 See Also : L<frac_aligned_hit()|frac_aligned_hit>, L<logical_length()|logical_length>, L<length_aln()|length_aln>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>
1271  
1272 =cut
1273  
1274 sub frac_aligned_query {
1275 my $self = shift;
1276  
1277 unless ($self->{'_hsps'}) {
1278 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1279 #return wantarray ? ('-','-') : '-';
1280 return '-';
1281 }
1282  
1283 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1284  
1285 sprintf( "%.2f", $self->length_aln('query') /
1286 $self->logical_length('query'));
1287 }
1288  
1289  
1290  
1291 =head2 frac_aligned_hit
1292  
1293 Usage : $hit_object->frac_aligned_hit();
1294 Purpose : Get the fraction of the hit (sbjct) sequence which has been aligned
1295 : across all HSPs (not including intervals between non-overlapping
1296 : HSPs).
1297 Example : $frac_alnq = $hit_object->frac_aligned_hit();
1298 Returns : Float (2-decimal precision, e.g., 0.75).
1299 Argument : n/a
1300 Throws : n/a
1301 Comments : If you need data for each HSP, use hsps() and then interate
1302 : through the HSP objects.
1303 : This method requires that all HSPs be tiled. If they have not
1304 : already been tiled, they will be tiled first automatically.
1305  
1306 See Also : L<frac_aligned_query()|frac_aligned_query>, L<matches()|matches>, , L<logical_length()|logical_length>, L<length_aln()|length_aln>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>
1307  
1308 =cut
1309  
1310 sub frac_aligned_hit {
1311 my $self = shift;
1312  
1313 unless ($self->{'_hsps'}) {
1314 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1315 #return wantarray ? ('-','-') : '-';
1316 return '-';
1317 }
1318  
1319 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1320  
1321 sprintf( "%.2f", $self->length_aln('sbjct') / $self->logical_length('sbjct'));
1322 }
1323  
1324  
1325 ## These methods are being maintained for backward compatibility.
1326  
1327 =head2 frac_aligned_sbjct
1328  
1329 Same as L<frac_aligned_hit()|frac_aligned_hit>
1330  
1331 =cut
1332  
1333 *frac_aligned_sbjct = \&frac_aligned_hit;
1334  
1335 =head2 num_unaligned_sbjct
1336  
1337 Same as L<num_unaligned_hit()|num_unaligned_hit>
1338  
1339 =cut
1340  
1341 *num_unaligned_sbjct = \&num_unaligned_hit;
1342  
1343  
1344 =head2 num_unaligned_hit
1345  
1346 Usage : $hit_object->num_unaligned_hit();
1347 Purpose : Get the number of the unaligned residues in the hit sequence.
1348 : Sums across all all HSPs.
1349 Example : $num_unaln = $hit_object->num_unaligned_hit();
1350 Returns : Integer
1351 Argument : n/a
1352 Throws : n/a
1353 Comments : See notes regarding logical lengths in the comments for frac_aligned_hit().
1354 : They apply here as well.
1355 : If you need data for each HSP, use hsps() and then interate
1356 : through the HSP objects.
1357 : This method requires that all HSPs be tiled. If they have not
1358 : already been tiled, they will be tiled first automatically..
1359  
1360 See Also : L<num_unaligned_query()|num_unaligned_query>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>, L<frac_aligned_hit()|frac_aligned_hit>
1361  
1362 =cut
1363  
1364 sub num_unaligned_hit {
1365 my $self = shift;
1366  
1367 unless ($self->{'_hsps'}) {
1368 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1369 #return wantarray ? ('-','-') : '-';
1370 return '-';
1371 }
1372  
1373 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1374  
1375 my $num = $self->logical_length('sbjct') - $self->length_aln('sbjct');
1376 ($num < 0 ? 0 : $num );
1377 }
1378  
1379  
1380 =head2 num_unaligned_query
1381  
1382 Usage : $hit_object->num_unaligned_query();
1383 Purpose : Get the number of the unaligned residues in the query sequence.
1384 : Sums across all all HSPs.
1385 Example : $num_unaln = $hit_object->num_unaligned_query();
1386 Returns : Integer
1387 Argument : n/a
1388 Throws : n/a
1389 Comments : See notes regarding logical lengths in the comments for frac_aligned_query().
1390 : They apply here as well.
1391 : If you need data for each HSP, use hsps() and then interate
1392 : through the HSP objects.
1393 : This method requires that all HSPs be tiled. If they have not
1394 : already been tiled, they will be tiled first automatically..
1395  
1396 See Also : L<num_unaligned_hit()|num_unaligned_hit>, L<frac_aligned_query()|frac_aligned_query>, L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>
1397  
1398 =cut
1399  
1400 sub num_unaligned_query {
1401 my $self = shift;
1402  
1403 unless ($self->{'_hsps'}) {
1404 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1405 #return wantarray ? ('-','-') : '-';
1406 return '-';
1407 }
1408  
1409 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1410  
1411 my $num = $self->logical_length('query') - $self->length_aln('query');
1412 ($num < 0 ? 0 : $num );
1413 }
1414  
1415  
1416  
1417 =head2 seq_inds
1418  
1419 Usage : $hit->seq_inds( seq_type, class, collapse );
1420 Purpose : Get a list of residue positions (indices) across all HSPs
1421 : for identical or conserved residues in the query or sbjct sequence.
1422 Example : @s_ind = $hit->seq_inds('query', 'identical');
1423 : @h_ind = $hit->seq_inds('hit', 'conserved');
1424 : @h_ind = $hit->seq_inds('hit', 'conserved', 1);
1425 Returns : Array of integers
1426 : May include ranges if collapse is non-zero.
1427 Argument : [0] seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
1428 : ('sbjct' is synonymous with 'hit')
1429 : [1] class = 'identical' or 'conserved' (default = 'identical')
1430 : (can be shortened to 'id' or 'cons')
1431 : (actually, anything not 'id' will evaluate to 'conserved').
1432 : [2] collapse = boolean, if non-zero, consecutive positions are merged
1433 : using a range notation, e.g., "1 2 3 4 5 7 9 10 11"
1434 : collapses to "1-5 7 9-11". This is useful for
1435 : consolidating long lists. Default = no collapse.
1436 Throws : n/a.
1437  
1438 See Also : L<FAST::Bio::Search::HSP::BlastHSP::seq_inds()|FAST::Bio::Search::HSP::BlastHSP>
1439  
1440 =cut
1441  
1442 sub seq_inds {
1443 my ($self, $seqType, $class, $collapse) = @_;
1444  
1445 $seqType ||= 'query';
1446 $class ||= 'identical';
1447 $collapse ||= 0;
1448  
1449 $seqType = 'sbjct' if $seqType eq 'hit';
1450  
1451 my (@inds, $hsp);
1452 foreach $hsp ($self->hsps) {
1453 # This will merge data for all HSPs together.
1454 push @inds, $hsp->seq_inds($seqType, $class);
1455 }
1456
1457 # Need to remove duplicates and sort the merged positions.
1458 if(@inds) {
1459 my %tmp = map { $_, 1 } @inds;
1460 @inds = sort {$a <=> $b} keys %tmp;
1461 }
1462  
1463 $collapse ? &FAST::Bio::Search::SearchUtils::collapse_nums(@inds) : @inds;
1464 }
1465  
1466  
1467 =head2 strand
1468  
1469 See documentation in L<FAST::Bio::Search::Hit::HitI::strand()|FAST::Bio::Search::Hit::HitI>
1470  
1471 =cut
1472  
1473 sub strand {
1474 my ($self, $seqType, $strnd) = @_;
1475  
1476 unless ($self->{'_hsps'}) {
1477 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1478 return wantarray ? ('-','-') : '-';
1479 #return '-';
1480 }
1481  
1482 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1483  
1484 $seqType ||= (wantarray ? 'list' : 'query');
1485 $seqType = 'sbjct' if $seqType eq 'hit';
1486  
1487 $seqType = lc($seqType);
1488  
1489 if( defined $strnd ) {
1490 $self->throw("Can't set strand for seqType '$seqType'. Must be 'query' or 'hit'\n") unless ($seqType eq 'sbjct' or $seqType eq 'query');
1491  
1492 return $self->{'_strand_'.$seqType} = $strnd;
1493 }
1494  
1495 my ($qstr, $hstr);
1496 # If there is only one HSP, defer this call to the solitary HSP.
1497 if($self->num_hsps == 1) {
1498 return $self->hsp->strand($seqType);
1499 }
1500 elsif( defined $self->{'_strand_query'}) {
1501 # Get the data computed during hsp tiling.
1502 $qstr = $self->{'_strand_query'};
1503 $hstr = $self->{'_strand_sbjct'}
1504 }
1505 else {
1506 # otherwise, iterate through all HSPs collecting strand info.
1507 # This will return the string "-1/1" if there are HSPs on different strands.
1508 # NOTE: This was the pre-10/21/02 procedure which will no longer be used,
1509 # (unless the above elsif{} is commented out).
1510 my (%qstr, %hstr);
1511 foreach my $hsp( $self->hsps ) {
1512 my ( $q, $h ) = $hsp->strand();
1513 $qstr{ $q }++;
1514 $hstr{ $h }++;
1515 }
1516 $qstr = join( '/', sort keys %qstr);
1517 $hstr = join( '/', sort keys %hstr);
1518 }
1519  
1520 if($seqType =~ /list|array/i) {
1521 return ($qstr, $hstr);
1522 } elsif( $seqType eq 'query' ) {
1523 return $qstr;
1524 } else {
1525 return $hstr;
1526 }
1527 }
1528  
1529 =head2 frame
1530  
1531 See documentation in L<FAST::Bio::Search::Hit::HitI::frame()|FAST::Bio::Search::Hit::HitI>
1532  
1533 =cut
1534  
1535 sub frame {
1536 my( $self, $frm ) = @_;
1537  
1538 unless ($self->{'_hsps'}) {
1539 FAST::Bio::Search::SearchUtils::_warn_about_no_hsps($self);
1540 #return wantarray ? ('-','-') : '-';
1541 return '-';
1542 }
1543  
1544 FAST::Bio::Search::SearchUtils::tile_hsps($self) unless $self->tiled_hsps;
1545  
1546 if( defined $frm ) {
1547 return $self->{'_frame'} = $frm;
1548 }
1549  
1550 # The check for $self->{'_frame'} is a remnant from the 'query' mode days
1551 # in which the sbjct object would collect data from the description line only.
1552  
1553 my ($frame);
1554 if(not defined($self->{'_frame'})) {
1555 $frame = $self->hsp->frame('hit');
1556 } else {
1557 $frame = $self->{'_frame'};
1558 }
1559 return $frame;
1560 }
1561  
1562 =head2 rank
1563  
1564 Title : rank
1565 Usage : $obj->rank($newval)
1566 Function: Get/Set the rank of this Hit in the Query search list
1567 i.e. this is the Nth hit for a specific query
1568 Returns : value of rank
1569 Args : newvalue (optional)
1570  
1571  
1572 =cut
1573  
1574 sub rank {
1575 my $self = shift;
1576 return $self->{'_rank'} = shift if @_;
1577 return $self->{'_rank'} || 1;
1578 }
1579  
1580 =head2 locus
1581  
1582 Title : locus
1583 Usage : $locus = $hit->locus();
1584 Function: Retrieve the locus (if available) for the hit
1585 Returns : a scalar string (empty string if not set)
1586 Args : none
1587  
1588 =cut
1589  
1590 sub locus {
1591 my ($self,$value) = @_;
1592 my $previous = $self->{'_locus'};
1593 if( defined $value || ! defined $previous ) {
1594 unless (defined $value) {
1595 if ($self->{'_name'} =~/(gb|emb|dbj|ref)\|(.*)\|(.*)/) {
1596 $value = $previous = $3;
1597 } else {
1598 $value = $previous = '';
1599 }
1600 }
1601 $self->{'_locus'} = $value;
1602 }
1603 return $previous;
1604 }
1605  
1606 =head2 each_accession_number
1607  
1608 Title : each_accession_number
1609 Usage : @each_accession_number = $hit->each_accession_number();
1610 Function: Get each accession number listed in the description of the hit.
1611 If there are no alternatives, then only the primary accession will
1612 be given
1613 Returns : list of all accession numbers in the description
1614 Args : none
1615  
1616 =cut
1617  
1618 sub each_accession_number {
1619 my ($self,$value) = @_;
1620 my $desc = $self->{'_description'};
1621 #put primary accnum on the list
1622 my @accnums;
1623 push (@accnums,$self->{'_accession'});
1624 if( defined $desc ) {
1625 while ($desc =~ /(\b\S+\|\S*\|\S*\s?)/g) {
1626 my $id = $1;
1627 my ($acc, $version);
1628 if ($id =~ /(gb|emb|dbj|sp|pdb|bbs|ref|tp[gde])\|(.*)\|(.*)/) {
1629 ($acc, $version) = split /\./, $2;
1630 } elsif ($id =~ /(pir|prf|pat|gnl)\|(.*)\|(.*)/) {
1631 ($acc, $version) = split /\./, $3;
1632 } elsif( $id =~ /(gim|gi|bbm|bbs|lcl)\|(\d*)/) {
1633 $acc = $id;
1634 } elsif( $id =~ /(oth)\|(.*)\|(.*)\|(.*)/ ) { # discontinued...
1635 ($acc,$version) = ($2);
1636 } else {
1637 #punt, not matching the db's at ftp://ftp.ncbi.nih.gov/blast/db/README
1638 #Database Name Identifier Syntax
1639 #============================ ========================
1640 #GenBank gb|accession|locus
1641 #EMBL Data Library emb|accession|locus
1642 #DDBJ, DNA Database of Japan dbj|accession|locus
1643 #NBRF PIR pir||entry
1644 #Protein Research Foundation prf||name
1645 #SWISS-PROT sp|accession|entry name
1646 #Brookhaven Protein Data Bank pdb|entry|chain
1647 #Patents pat|country|number
1648 #GenInfo Backbone Id bbs|number
1649 #General database identifier gnl|database|identifier
1650 #NCBI Reference Sequence ref|accession|locus
1651 #Local Sequence identifier lcl|identifier
1652 $acc=$id;
1653 }
1654 push(@accnums, $acc);
1655 }
1656 }
1657 return @accnums;
1658 }
1659  
1660 =head2 tiled_hsps
1661  
1662 See documentation in L<FAST::Bio::Search::SearchUtils::tile_hsps()|FAST::Bio::Search::SearchUtils>
1663  
1664 =cut
1665  
1666 sub tiled_hsps {
1667 my $self = shift;
1668 return $self->{'_tiled_hsps'} = shift if @_;
1669 return $self->{'_tiled_hsps'};
1670 }
1671  
1672 =head2 query_length
1673  
1674 Title : query_length
1675 Usage : $obj->query_length($newval)
1676 Function: Get/Set the query_length
1677 Returns : value of query_length (a scalar)
1678 Args : on set, new value (a scalar or undef, optional)
1679  
1680  
1681 =cut
1682  
1683 sub query_length {
1684 my $self = shift;
1685  
1686 return $self->{'_query_length'} = shift if @_;
1687 return $self->{'_query_length'};
1688 }
1689  
1690 =head2 ncbi_gi
1691  
1692 Title : ncbi_gi
1693 Usage : $acc = $hit->ncbi_gi();
1694 Function: Retrieve the NCBI Unique ID (aka the GI #),
1695 if available, for the hit
1696 Returns : a scalar string (empty string if not set)
1697 Args : none
1698  
1699 =cut
1700  
1701 sub ncbi_gi {
1702 my ($self,$value) = @_;
1703 if( defined $value ) {
1704 $self->{'_ncbi_gi'} = $value;
1705 } else {
1706 $self->{'_ncbi_gi'} = $self->name =~ m{^gi\|(\d+)} ? $1 : '';
1707 }
1708 return $self->{'_ncbi_gi'};
1709 }
1710  
1711  
1712 # sort method for HSPs
1713  
1714 =head2 sort_hits
1715  
1716 Title : sort_hsps
1717 Usage : $result->sort_hsps(\&sort_function)
1718 Function : Sorts the available HSP objects by a user-supplied function. Defaults to sort
1719 by descending score.
1720 Returns : n/a
1721 Args : A coderef for the sort function. See the documentation on the Perl sort()
1722 function for guidelines on writing sort functions.
1723 Note : To access the special variables $a and $b used by the Perl sort() function
1724 the user function must access FAST::Bio::Search::Hit::HitI namespace.
1725 For example, use :
1726 $hit->sort_hsps( sub{$FAST::Bio::Search::Result::HitI::a->length <=>
1727 $FAST::Bio::Search::Result::HitI::b->length});
1728 NOT $hit->sort_hsps($a->length <=> $b->length);
1729  
1730 =cut
1731  
1732 sub sort_hsps {
1733 my ($self, $coderef) = @_;
1734 my @sorted_hsps;
1735  
1736 if ($coderef) {
1737 $self->throw('sort_hsps requires a sort function passed as a subroutine reference')
1738 unless (ref($coderef) eq 'CODE');
1739 }
1740 else {
1741 $coderef = \&_default_sort_hsps;
1742 # throw a warning?
1743 }
1744  
1745 my @hsps = $self->hsps();
1746 eval {@sorted_hsps = sort $coderef @hsps };
1747  
1748 if ($@) {
1749 $self->throw("Unable to sort hsps: $@");
1750 }
1751 else {
1752 $self->{'_hsps'} = \@sorted_hsps;
1753 1;
1754 }
1755 }
1756  
1757 =head2 iteration
1758  
1759 Usage : $hit->iteration( $iteration_num );
1760 Purpose : Gets the iteration number in which the Hit was found.
1761 Example : $iteration_num = $sbjct->iteration();
1762 Returns : Integer greater than or equal to 1
1763 Non-PSI-BLAST reports may report iteration as 1, but this number
1764 is only meaningful for PSI-BLAST reports.
1765 Argument : iteration_num (optional, used when setting only)
1766 Throws : none
1767  
1768 See Also : L<found_again()|found_again>
1769  
1770 =cut
1771  
1772 sub iteration{
1773 my ($self,$value) = @_;
1774 if( defined $value) {
1775 $self->{'_psiblast_iteration'} = $value;
1776 }
1777 return $self->{'_psiblast_iteration'};
1778 }
1779  
1780 =head2 found_again
1781  
1782 Title : found_again
1783 Usage : $hit->found_again;
1784 $hit->found_again(1);
1785 Purpose : Gets a boolean indicator whether or not the hit has
1786 been found in a previous iteration.
1787 This is only applicable to PSI-BLAST reports.
1788  
1789 This method indicates if the hit was reported in the
1790 "Sequences used in model and found again" section of the
1791 PSI-BLAST report or if it was reported in the
1792 "Sequences not found previously or not previously below threshold"
1793 section of the PSI-BLAST report. Only for hits in iteration > 1.
1794  
1795 Example : if( $hit->found_again()) { ... };
1796 Returns : Boolean, true (1) if the hit has been found in a
1797 previous PSI-BLAST iteration.
1798 Returns false (0 or undef) for hits that have not occurred in a
1799 previous PSI-BLAST iteration.
1800 Argument : Boolean (1 or 0). Only used for setting.
1801 Throws : none
1802  
1803 See Also : L<iteration()|iteration>
1804  
1805 =cut
1806  
1807 sub found_again {
1808 my $self = shift;
1809 return $self->{'_found_again'} = shift if @_;
1810 return $self->{'_found_again'};
1811 }
1812  
1813 1;