maint: restructure to use Dist::Zilla
[bioperl-live.git] / lib / Bio / Search / Hit / PullHitI.pm
blob307877a1ffcaa94ab8b82594a2363bcfdb374e86
2 # BioPerl module Bio::Search::Hit::PullHitI
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Sendu Bala <bix@sendu.me.uk>
8 # You may distribute this module under the same terms as perl itself
10 # POD documentation - main docs before the code
12 =head1 NAME
14 Bio::Search::Hit::PullHitI - Bio::Search::Hit::HitI interface for pull parsers.
16 =head1 SYNOPSIS
18 # This is an interface and cannot be instantiated
20 # typically one gets HitI objects from a SearchIO stream via a ResultI
21 use Bio::SearchIO;
22 my $parser = Bio::SearchIO->new(-format => 'hmmer_pull',
23 -file => 't/data/hmmpfam.out');
25 my $result = $parser->next_result;
26 my $hit = $result->next_hit;
28 $hit_name = $hit->name();
30 $desc = $hit->description();
32 $len = $hit->length
34 $alg = $hit->algorithm();
36 $score = $hit->raw_score();
38 $significance = $hit->significance();
40 $rank = $hit->rank(); # the Nth hit for a specific query
42 while( $hsp = $obj->next_hsp()) { ... } # process in iterator fashion
44 for my $hsp ( $obj->hsps()()) { ... } # process in list fashion
46 =head1 DESCRIPTION
48 This object handles the hit data from a database sequence search.
50 PullHitI is for fast implementations that only do parsing work on the hit
51 data when you actually request information by calling one of the HitI
52 methods.
54 Many methods of HitI are implemented in a way suitable for inheriting classes
55 that use Bio::PullParserI. It only really makes sense for PullHit modules to be
56 created by (and have as a -parent) PullResult modules.
58 In addition to the usual -chunk and -parent, -hit_data is all you should supply
59 when making a PullHit object. This will store that data and make it accessible
60 via _raw_hit_data, which you can access in your subclass. It would be best to
61 simply provide the data as the input -chunk instead, if the raw data is large
62 enough.
64 =head1 FEEDBACK
66 =head2 Mailing Lists
68 User feedback is an integral part of the evolution of this and other
69 Bioperl modules. Send your comments and suggestions preferably to one
70 of the Bioperl mailing lists. Your participation is much appreciated.
72 bioperl-l@bioperl.org - General discussion
73 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
75 =head2 Support
77 Please direct usage questions or support issues to the mailing list:
79 I<bioperl-l@bioperl.org>
81 rather than to the module maintainer directly. Many experienced and
82 reponsive experts will be able look at the problem and quickly
83 address it. Please include a thorough description of the problem
84 with code and data examples if at all possible.
86 =head2 Reporting Bugs
88 Report bugs to the Bioperl bug tracking system to help us keep track
89 the bugs and their resolution. Bug reports can be submitted via the
90 web:
92 https://github.com/bioperl/bioperl-live/issues
94 =head1 AUTHOR - Sendu Bala
96 Email bix@sendu.me.uk
98 =head1 COPYRIGHT
100 Copyright (c) 2006 Sendu Bala. All Rights Reserved.
102 =head1 DISCLAIMER
104 This software is provided "as is" without warranty of any kind.
106 =head1 APPENDIX
108 The rest of the documentation details each of the object
109 methods. Internal methods are usually preceded with a _
111 =cut
113 # Let the code begin...
115 package Bio::Search::Hit::PullHitI;
117 use Bio::Search::SearchUtils;
119 use strict;
121 use base qw(Bio::PullParserI Bio::Search::Hit::HitI);
123 =head2 _setup
125 Title : _setup
126 Usage : $self->_setup(@args)
127 Function: Implementers should call this to setup common fields and deal with
128 common arguments to new().
129 Returns : n/a
130 Args : @args received in new().
132 =cut
134 sub _setup {
135 my ($self, @args) = @_;
137 # fields most subclasses probably will want
138 $self->_fields( { ( next_hsp => undef,
139 num_hsps => undef,
140 hsps => undef,
141 hit_start => undef,
142 query_start => undef,
143 hit_end => undef,
144 query_end => undef,
145 length => undef,
146 name => undef ,
147 accession => undef ) } );
149 my ($parent, $chunk, $hit_data) = $self->_rearrange([qw(PARENT
150 CHUNK
151 HIT_DATA)], @args);
152 $self->throw("Need -parent or -chunk to be defined") unless $parent || $chunk;
154 $self->parent($parent) if $parent;
156 if ($chunk) {
157 my ($io, $start, $end) = (undef, 0, undef);
158 if (ref($chunk) eq 'ARRAY') {
159 ($io, $start, $end) = @{$chunk};
161 else {
162 $io = $chunk;
164 $self->chunk($io, -start => $start, -end => $end);
167 $self->_raw_hit_data($hit_data) if $hit_data;
170 sub _raw_hit_data {
171 my $self = shift;
172 if (@_) {
173 $self->{_raw_hit_data} = shift;
175 return $self->{_raw_hit_data};
179 # Some of these methods are written explitely to avoid HitI throwing not
180 # implemented; if it didn't do that then PullParserI AUTOLOAD would have
181 # cought them.
184 =head2 name
186 Title : name
187 Usage : $hit_name = $hit->name();
188 Function: returns the name of the Hit sequence
189 Returns : a scalar string
190 Args : none
192 The B<name> of a hit is unique within a Result or within an Iteration.
194 =cut
196 sub name {
197 return shift->get_field('name');
200 =head2 description
202 Title : description
203 Usage : $desc = $hit->description();
204 Function: Retrieve the description for the hit
205 Returns : a scalar string
206 Args : none
208 =cut
210 sub description {
211 return shift->get_field('description');
214 =head2 accession
216 Title : accession
217 Usage : $acc = $hit->accession();
218 Function: Retrieve the accession (if available) for the hit
219 Returns : a scalar string (empty string if not set)
220 Args : none
222 =cut
224 sub accession {
225 return shift->get_field('accession');
228 =head2 locus
230 Title : locus
231 Usage : $acc = $hit->locus();
232 Function: Retrieve the locus(if available) for the hit
233 Returns : a scalar string (empty string if not set)
234 Args : none
236 =cut
238 sub locus {
239 return shift->get_field('locus');
242 =head2 length
244 Title : length
245 Usage : my $len = $hit->length
246 Function: Returns the length of the hit
247 Returns : integer
248 Args : none
250 =cut
252 sub length {
253 return shift->get_field('length');
256 =head2 algorithm
258 Title : algorithm
259 Usage : $alg = $hit->algorithm();
260 Function: Gets the algorithm specification that was used to obtain the hit
261 For BLAST, the algorithm denotes what type of sequence was aligned
262 against what (BLASTN: dna-dna, BLASTP prt-prt, BLASTX translated
263 dna-prt, TBLASTN prt-translated dna, TBLASTX translated
264 dna-translated dna).
265 Returns : a scalar string
266 Args : none
268 =cut
270 sub algorithm {
271 return shift->get_field('algorithm');
274 =head2 raw_score
276 Title : raw_score
277 Usage : $score = $hit->raw_score();
278 Function: Gets the "raw score" generated by the algorithm. What
279 this score is exactly will vary from algorithm to algorithm,
280 returning undef if unavailable.
281 Returns : a scalar value
282 Args : none
284 =cut
286 sub raw_score {
287 return shift->get_field('score');
290 =head2 score
292 Equivalent to L<raw_score()|raw_score>
294 =cut
296 sub score {
297 return shift->get_field('score');
300 =head2 significance
302 Title : significance
303 Usage : $significance = $hit->significance();
304 Function: Used to obtain the E or P value of a hit, i.e. the probability that
305 this particular hit was obtained purely by random chance. If
306 information is not available (nor calculatable from other
307 information sources), return undef.
308 Returns : a scalar value or undef if unavailable
309 Args : none
311 =cut
313 sub significance {
314 return shift->get_field('significance');
317 =head2 bits
319 Usage : $hit_object->bits();
320 Purpose : Gets the bit score of the best HSP for the current hit.
321 Example : $bits = $hit_object->bits();
322 Returns : Integer or double for FASTA reports
323 Argument : n/a
324 Comments : For BLAST1, the non-bit score is listed in the summary line.
326 See Also : L<score()|score>
328 =cut
330 sub bits {
331 return shift->get_field('bits');
334 =head2 next_hsp
336 Title : next_hsp
337 Usage : while( $hsp = $obj->next_hsp()) { ... }
338 Function : Returns the next available High Scoring Pair
339 Example :
340 Returns : L<Bio::Search::HSP::HSPI> object or null if finished
341 Args : none
343 =cut
345 sub next_hsp {
346 return shift->get_field('next_hsp');
349 =head2 hsps
351 Usage : $hit_object->hsps();
352 Purpose : Get a list containing all HSP objects.
353 : Get the numbers of HSPs for the current hit.
354 Example : @hsps = $hit_object->hsps();
355 : $num = $hit_object->hsps(); # alternatively, use num_hsps()
356 Returns : Array context : list of L<Bio::Search::HSP::BlastHSP> objects.
357 : Scalar context: integer (number of HSPs).
358 : (Equivalent to num_hsps()).
359 Argument : n/a. Relies on wantarray
360 Throws : Exception if the HSPs have not been collected.
362 See Also : L<hsp()|hsp>, L<num_hsps()|num_hsps>
364 =cut
366 sub hsps {
367 return shift->get_field('hsps');
370 =head2 num_hsps
372 Usage : $hit_object->num_hsps();
373 Purpose : Get the number of HSPs for the present Blast hit.
374 Example : $nhsps = $hit_object->num_hsps();
375 Returns : Integer
376 Argument : n/a
377 Throws : Exception if the HSPs have not been collected.
379 See Also : L<hsps()|hsps>
381 =cut
383 sub num_hsps {
384 return shift->get_field('num_hsps');
388 # HitI/ GenericHit methods that are unrelated to simply parsing information
389 # directly out of a file, but need more complex calculation; mostly not
390 # implemented here.
393 =head2 seq_inds
395 Usage : $hit->seq_inds( seq_type, class, collapse );
396 Purpose : Get a list of residue positions (indices) across all HSPs
397 : for identical or conserved residues in the query or sbjct sequence.
398 Example : @s_ind = $hit->seq_inds('query', 'identical');
399 : @h_ind = $hit->seq_inds('hit', 'conserved');
400 : @h_ind = $hit->seq_inds('hit', 'conserved', 1);
401 Returns : Array of integers
402 : May include ranges if collapse is non-zero.
403 Argument : [0] seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
404 : ('sbjct' is synonymous with 'hit')
405 : [1] class = 'identical' or 'conserved' or 'nomatch' or 'gap'
406 : (default = 'identical')
407 : (can be shortened to 'id' or 'cons')
408 : Note that 'conserved' includes identical unless you use
409 : 'conserved-not-identical'
410 : [2] collapse = boolean, if non-zero, consecutive positions are
411 : merged using a range notation, e.g.,
412 : "1 2 3 4 5 7 9 10 11" collapses to "1-5 7 9-11". This
413 : is useful for consolidating long lists. Default = no
414 : collapse.
415 Throws : n/a.
417 See Also : L<Bio::Search::HSP::HSPI::seq_inds()|Bio::Search::HSP::HSPI>
419 =cut
421 sub seq_inds {
422 my ($self, $seqType, $class, $collapse) = @_;
424 $seqType ||= 'query';
425 $class ||= 'identical';
426 $collapse ||= 0;
428 $seqType = 'hit' if $seqType eq 'sbjct';
430 my $storage_name = '_seq_inds_'.$seqType.'_'.$class;
431 unless (defined $self->{$storage_name}) {
432 my @inds;
433 foreach my $hsp ($self->hsps) {
434 # This will merge data for all HSPs together.
435 push @inds, $hsp->seq_inds($seqType, $class);
438 # Need to remove duplicates and sort the merged positions, unless gaps.
439 if (@inds && $class ne 'gap') {
440 my %tmp = map { $_, 1 } @inds;
441 @inds = sort {$a <=> $b} keys %tmp;
444 $self->{$storage_name} = \@inds;
447 my @inds = @{$self->{$storage_name}};
448 $collapse ? &Bio::Search::SearchUtils::collapse_nums(@inds) : @inds;
451 =head2 rewind
453 Title : rewind
454 Usage : $hit->rewind;
455 Function: Allow one to reset the HSP iterator to the beginning if possible
456 Returns : none
457 Args : none
459 =cut
461 sub rewind {
462 shift->throw_not_implemented();
465 =head2 overlap
467 Usage : $hit_object->overlap( [integer] );
468 Purpose : Gets/Sets the allowable amount overlap between different HSP
469 sequences.
470 Example : $hit_object->overlap(5);
471 : $overlap = $hit_object->overlap;
472 Returns : Integer.
473 Argument : integer.
474 Throws : n/a
475 Status : Deprecated
476 Comments : This value isn't used for anything
478 =cut
480 sub overlap {
481 my $self = shift;
482 if (@_) { $self->{_overlap} = shift }
483 return $self->{_overlap} || 0;
486 =head2 n
488 Usage : $hit_object->n();
489 Purpose : Gets the N number for the current Blast hit.
490 : This is the number of HSPs in the set which was ascribed
491 : the lowest P-value (listed on the description line).
492 : This number is not the same as the total number of HSPs.
493 : To get the total number of HSPs, use num_hsps().
494 Example : $n = $hit_object->n();
495 Returns : Integer
496 Argument : n/a
497 Throws : Exception if HSPs have not been set (BLAST2 reports).
498 Comments : Note that the N parameter is not reported in gapped BLAST2.
499 : Calling n() on such reports will result in a call to num_hsps().
500 : The num_hsps() method will count the actual number of
501 : HSPs in the alignment listing, which may exceed N in
502 : some cases.
504 See Also : L<num_hsps()|num_hsps>
506 =cut
508 sub n {
509 return shift->get_field('num_hsps');
512 =head2 p
514 Usage : $hit_object->p( [format] );
515 Purpose : Get the P-value for the best HSP of the given BLAST hit.
516 : (Note that P-values are not provided with NCBI Blast2 reports).
517 Example : $p = $sbjct->p;
518 : $p = $sbjct->p('exp'); # get exponent only.
519 : ($num, $exp) = $sbjct->p('parts'); # split sci notation into parts
520 Returns : Float or scientific notation number (the raw P-value, DEFAULT).
521 : Integer if format == 'exp' (the magnitude of the base 10 exponent).
522 : 2-element list (float, int) if format == 'parts' and P-value
523 : is in scientific notation (See Comments).
524 Argument : format: string of 'raw' | 'exp' | 'parts'
525 : 'raw' returns value given in report. Default. (1.2e-34)
526 : 'exp' returns exponent value only (34)
527 : 'parts' returns the decimal and exponent as a
528 : 2-element list (1.2, -34) (See Comments).
529 Throws : Warns if no P-value is defined. Uses expect instead.
530 Comments : Using the 'parts' argument is not recommended since it will not
531 : work as expected if the P-value is not in scientific notation.
532 : That is, floats are not converted into sci notation before
533 : splitting into parts.
535 See Also : L<expect()|expect>, L<signif()|signif>,
536 L<Bio::Search::BlastUtils::get_exponent()|Bio::Search::BlastUtils>
538 =cut
540 sub p {
541 shift->throw_not_implemented;
544 =head2 hsp
546 Usage : $hit_object->hsp( [string] );
547 Purpose : Get a single HSPI object for the present HitI object.
548 Example : $hspObj = $hit_object->hsp; # same as 'best'
549 : $hspObj = $hit_object->hsp('best');
550 : $hspObj = $hit_object->hsp('worst');
551 Returns : Object reference for a L<Bio::Search::HSP::HSPI> object.
552 Argument : String (or no argument).
553 : No argument (default) = highest scoring HSP (same as 'best').
554 : 'best' = highest scoring HSP.
555 : 'worst' = lowest scoring HSP.
556 Throws : Exception if an unrecognized argument is used.
558 See Also : L<hsps()|hsps>, L<num_hsps>()
560 =cut
562 sub hsp {
563 shift->throw_not_implemented;
566 =head2 logical_length
568 Usage : $hit_object->logical_length( [seq_type] );
569 : (mostly intended for internal use).
570 Purpose : Get the logical length of the hit sequence.
571 : If the Blast is a TBLASTN or TBLASTX, the returned length
572 : is the length of the would-be amino acid sequence (length/3).
573 : For all other BLAST flavors, this function is the same as length().
574 Example : $len = $hit_object->logical_length();
575 Returns : Integer
576 Argument : seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
577 ('sbjct' is synonymous with 'hit')
578 Throws : n/a
579 Comments : This is important for functions like frac_aligned_query()
580 : which need to operate in amino acid coordinate space when dealing
581 : with [T]BLAST[NX] type reports.
583 See Also : L<length()|length>, L<frac_aligned_query()|frac_aligned_query>,
584 L<frac_aligned_hit()|frac_aligned_hit>
586 =cut
588 sub logical_length {
589 my ($self, $type) = @_;
590 $type ||= 'query';
591 $type = lc($type);
592 $type = 'hit' if $type eq 'sbjct';
593 if ($type eq 'query') {
594 return $self->get_field('query_length');
596 elsif ($type eq 'hit') {
597 return $self->get_field('length');
601 =head2 rank
603 Title : rank
604 Usage : $obj->rank($newval)
605 Function: Get/Set the rank of this Hit in the Query search list
606 i.e. this is the Nth hit for a specific query
607 Returns : value of rank
608 Args : newvalue (optional)
610 =cut
612 sub rank {
613 return shift->get_field('rank');
616 =head2 each_accession_number
618 Title : each_accession_number
619 Usage : $obj->each_accession_number
620 Function: Get each accession number listed in the description of the hit.
621 If there are no alternatives, then only the primary accession will
622 be given (if there is one).
623 Returns : list of all accession numbers in the description
624 Args : none
626 =cut
628 sub each_accession_number {
629 my $self = shift;
630 my $accession = $self->get_field('accession') if $self->has_field('accession');
631 my $desc = $self->get_field('description') if $self->has_field('description');
632 return unless $accession || $desc;
634 my @accnums;
635 push (@accnums, $accession) if $accession;
637 if (defined $desc) {
638 while ($desc =~ /(\b\S+\|\S*\|\S*\s?)/g) {
639 my $id = $1;
640 my $acc;
641 if ($id =~ /(?:gb|emb|dbj|sp|pdb|bbs|ref|tp[gde])\|(.*)\|(?:.*)/) {
642 ($acc) = split /\./, $1;
644 elsif ($id =~ /(?:pir|prf|pat|gnl)\|(?:.*)\|(.*)/) {
645 ($acc) = split /\./, $1;
647 elsif ($id =~ /(?:gim|gi|bbm|bbs|lcl)\|(?:\d*)/) {
648 $acc = $id;
650 elsif ($id =~ /(?:oth)\|(.*)\|(?:.*)\|(?:.*)/ ) { # discontinued...
651 $acc = $1;
653 else {
654 $acc = $id;
656 push(@accnums, $acc);
659 return @accnums;
662 =head2 tiled_hsps
664 Usage : $hit_object->tiled_hsps( [integer] );
665 Purpose : Gets/Sets an indicator for whether or not the HSPs in this Hit
666 : have been tiled.
667 Example : $hit_object->tiled_hsps(1);
668 : if( $hit_object->tiled_hsps ) { # do something }
669 Returns : Boolean (1 or 0)
670 Argument : integer (optional)
671 Throws : n/a
672 Status : Deprecated
673 Notes : This value is not used for anything
675 =cut
677 sub tiled_hsps {
678 my $self = shift;
679 if (@_) { $self->{_hsps_are_tiled} = shift }
680 return $self->{_hsps_are_tiled} || 0;
683 =head2 strand
685 Usage : $sbjct->strand( [seq_type] );
686 Purpose : Gets the strand(s) for the query, sbjct, or both sequences
687 : in the best HSP of the BlastHit object after HSP tiling.
688 : Only valid for BLASTN, TBLASTX, BLASTX-query, TBLASTN-hit.
689 Example : $qstrand = $sbjct->strand('query');
690 : $sstrand = $sbjct->strand('hit');
691 : ($qstrand, $sstrand) = $sbjct->strand();
692 Returns : scalar context: integer '1', '-1', or '0'
693 : array context without args: list of two strings (queryStrand, sbjctStrand)
694 : Array context can be "induced" by providing an argument of 'list' or 'array'.
695 Argument : In scalar context: seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
696 ('sbjct' is synonymous with 'hit')
697 Throws : n/a
698 Comments : This method requires that all HSPs be tiled. If they have not
699 : already been tiled, they will be tiled first automatically..
700 : If you don't want the tiled data, iterate through each HSP
701 : calling strand() on each (use hsps() to get all HSPs).
703 : Formerly (prior to 10/21/02), this method would return the
704 : string "-1/1" for hits with HSPs on both strands.
705 : However, now that strand and frame is properly being accounted
706 : for during HSP tiling, it makes more sense for strand()
707 : to return the strand data for the best HSP after tiling.
709 : If you really want to know about hits on opposite strands,
710 : you should be iterating through the HSPs using methods on the
711 : HSP objects.
713 : A possible use case where knowing whether a hit has HSPs
714 : on both strands would be when filtering via SearchIO for hits with
715 : this property. However, in this case it would be better to have a
716 : dedicated method such as $hit->hsps_on_both_strands(). Similarly
717 : for frame. This could be provided if there is interest.
719 See Also : L<Bio::Search::HSP::HSPI::strand>()
721 =cut
723 sub strand {
724 shift->throw_not_implemented;
727 =head2 frame
729 Usage : $hit_object->frame();
730 Purpose : Gets the reading frame for the best HSP after HSP tiling.
731 : This is only valid for BLASTX and TBLASTN/X type reports.
732 Example : $frame = $hit_object->frame();
733 Returns : Integer (-2 .. +2)
734 Argument : n/a
735 Throws : Exception if HSPs have not been set.
736 Comments : This method requires that all HSPs be tiled. If they have not
737 : already been tiled, they will be tiled first automatically..
738 : If you don't want the tiled data, iterate through each HSP
739 : calling frame() on each (use hsps() to get all HSPs).
741 See Also : L<hsps()|hsps>
743 =cut
745 sub frame {
746 shift->throw_not_implemented;
749 =head2 length_aln
751 Usage : $hit_object->length_aln( [seq_type] );
752 Purpose : Get the total length of the aligned region for query or sbjct seq.
753 : This number will include all HSPs, and excludes gaps.
754 Example : $len = $hit_object->length_aln(); # default = query
755 : $lenAln = $hit_object->length_aln('query');
756 Returns : Integer
757 Argument : seq_Type = 'query' or 'hit' or 'sbjct' (Default = 'query')
758 ('sbjct' is synonymous with 'hit')
759 Throws : Exception if the argument is not recognized.
760 Comments : This method will report the logical length of the alignment,
761 : meaning that for TBLAST[NX] reports, the length is reported
762 : using amino acid coordinate space (i.e., nucleotides / 3).
764 See Also : L<length()|length>, L<frac_aligned_query()|frac_aligned_query>,
765 L<frac_aligned_hit()|frac_aligned_hit>, L<gaps()|gaps>,
766 L<Bio::Search::SearchUtils::tile_hsps()|Bio::Search::SearchUtils>,
767 L<Bio::Search::HSP::BlastHSP::length()|Bio::Search::HSP::BlastHSP>
769 =cut
771 sub length_aln {
772 my ($self, $seqType) = @_;
773 $seqType ||= 'query';
774 $seqType = 'hit' if $seqType eq 'sbjct';
776 my %non_gaps = map { $_, 1 } $self->seq_inds($seqType, 'conserved'),
777 $self->seq_inds($seqType, 'no_match');
778 return scalar(keys %non_gaps);
781 =head2 gaps
783 Usage : $hit_object->gaps( [seq_type] );
784 Purpose : Get the number of gaps in the aligned query, hit, or both sequences.
785 : Data is summed across all HSPs.
786 Example : $qgaps = $hit_object->gaps('query');
787 : $hgaps = $hit_object->gaps('hit');
788 : $tgaps = $hit_object->gaps(); # default = total (query + hit)
789 Returns : scalar context: integer
790 : array context without args: two-element list of integers
791 : (queryGaps, hitGaps)
792 : Array context can be forced by providing an argument of 'list' or
793 : 'array'.
795 : CAUTION: Calling this method within printf or sprintf is arrray
796 : context.
797 : So this function may not give you what you expect. For example:
798 : printf "Total gaps: %d", $hit->gaps();
799 : Actually returns a two-element array, so what gets printed
800 : is the number of gaps in the query, not the total
802 Argument : seq_type: 'query' | 'hit' or 'sbjct' | 'total' | 'list'
803 : (default = 'total') ('sbjct' is synonymous with 'hit')
804 Comments : If you need data for each HSP, use hsps() and then interate
805 : through each HSP object.
807 =cut
809 sub gaps {
810 my ($self, $seqType) = @_;
812 $seqType ||= (wantarray ? 'list' : 'total');
813 $seqType = 'hit' if $seqType eq 'sbjct';
815 if ($seqType =~ /list|array/i) {
816 return (scalar($self->seq_inds('query', 'gap')), scalar($self->seq_inds('hit', 'gap')));
818 elsif ($seqType eq 'total') {
819 return (scalar($self->seq_inds('query', 'gap')) + scalar($self->seq_inds('hit', 'gap'))) || 0;
821 else {
822 return scalar($self->seq_inds($seqType, 'gap')) || 0;
826 =head2 matches
828 Usage : $hit_object->matches( [class] );
829 Purpose : Get the total number of identical or conserved matches
830 : (or both) across all HSPs.
831 : (Note: 'conservative' matches are indicated as 'positives'
832 : in BLAST reports.)
833 Example : ($id,$cons) = $hit_object->matches(); # no argument
834 : $id = $hit_object->matches('id');
835 : $cons = $hit_object->matches('cons');
836 Returns : Integer or a 2-element array of integers
837 Argument : [0] class = 'id' | 'cons' OR none.
838 : [1] seq_type = 'query' or 'hit' or 'sbjct' (default = 'query')
839 : ('sbjct' is synonymous with 'hit')
840 : If no argument is provided, both identical and conservative
841 : numbers are returned in a two element list.
842 : (Other terms can be used to refer to the conservative
843 : matches, e.g., 'positive'. All that is checked is whether or
844 : not the supplied string starts with 'id'. If not, the
845 : conservative matches are returned.)
847 =cut
849 sub matches {
850 my ($self, $class, $seqType) = @_;
852 # no query/hit choice? The answer differs depending on sequence, since
853 # hsps could overlap on one sequence and not the other. Added an option,
854 # but otherwise will assume 'hit'
855 $seqType ||= 'hit';
856 $seqType = 'hit' if $seqType eq 'sbjct';
858 unless (exists $self->{_id_matches}) {
859 $self->{_id_matches}->{hit} = scalar($self->seq_inds('hit', 'identical'));
860 $self->{_id_matches}->{query} = scalar($self->seq_inds('query', 'identical'));
862 unless (exists $self->{_con_matches}) {
863 foreach my $type ('hit', 'query') {
864 # 'conserved-not-identical' can give us 'identical' matches if hsps
865 # overlapped so have to get the difference
866 my %identicals = map { $_ => 1 } $self->seq_inds($type, 'identical');
867 my @conserved = $self->seq_inds($type, 'conserved-not-identical');
869 my $real_conserved;
870 foreach (@conserved) {
871 unless (exists $identicals{$_}) {
872 $real_conserved++;
875 $self->{_con_matches}->{$type} = $real_conserved;
880 unless ($class) {
881 return ($self->{_id_matches}->{$seqType}, $self->{_con_matches}->{$seqType});
883 else {
884 if ($class =~ /^id/i) {
885 return $self->{_id_matches}->{$seqType};
887 else {
888 return $self->{_con_matches}->{$seqType};
891 return;
894 =head2 start
896 Usage : $sbjct->start( [seq_type] );
897 Purpose : Gets the start coordinate for the query, sbjct, or both sequences
898 : in the object. If there is more than one HSP, the lowest start
899 : value of all HSPs is returned.
900 Example : $qbeg = $sbjct->start('query');
901 : $sbeg = $sbjct->start('hit');
902 : ($qbeg, $sbeg) = $sbjct->start();
903 Returns : scalar context: integer
904 : array context without args: list of two integers (queryStart,
905 : sbjctStart)
906 : Array context can be "induced" by providing an argument of 'list'
907 : or 'array'.
908 Argument : 'query' or 'hit' or 'sbjct' (default = 'query') ('sbjct' is
909 synonymous with 'hit')
911 =cut
913 sub start {
914 my ($self, $seqType) = @_;
916 unless ($self->get_field('num_hsps')) {
917 Bio::Search::SearchUtils::_warn_about_no_hsps($self);
918 return;
921 $seqType ||= (wantarray ? 'list' : 'query');
922 $seqType = 'hit' if $seqType eq 'sbjct';
924 if ($seqType =~ /list|array/i) {
925 return ($self->get_field('query_start'), $self->get_field('hit_start'));
927 elsif ($seqType eq 'hit') {
928 return $self->get_field('hit_start');
930 elsif ($seqType eq 'query') {
931 return $self->get_field('query_start');
933 else {
934 $self->throw("Unknown sequence type '$seqType'");
938 =head2 end
940 Usage : $sbjct->end( [seq_type] );
941 Purpose : Gets the end coordinate for the query, sbjct, or both sequences
942 : in the object. If there is more than one HSP, the largest end
943 : value of all HSPs is returned.
944 Example : $qend = $sbjct->end('query');
945 : $send = $sbjct->end('hit');
946 : ($qend, $send) = $sbjct->end();
947 Returns : scalar context: integer
948 : array context without args: list of two integers
949 : (queryEnd, sbjctEnd)
950 : Array context can be "induced" by providing an argument
951 : of 'list' or 'array'.
952 Argument : 'query' or 'hit' or 'sbjct' (default = 'query') ('sbjct' is
953 synonymous with 'hit')
955 =cut
957 sub end {
958 my ($self, $seqType) = @_;
960 unless ($self->get_field('num_hsps')) {
961 Bio::Search::SearchUtils::_warn_about_no_hsps($self);
962 return;
965 $seqType ||= (wantarray ? 'list' : 'query');
966 $seqType = 'hit' if $seqType eq 'sbjct';
968 if ($seqType =~ /list|array/i) {
969 return ($self->get_field('query_end'), $self->get_field('hit_end'));
971 elsif ($seqType eq 'hit') {
972 return $self->get_field('hit_end');
974 elsif ($seqType eq 'query') {
975 return $self->get_field('query_end');
977 else {
978 $self->throw("Unknown sequence type '$seqType'");
982 =head2 range
984 Usage : $sbjct->range( [seq_type] );
985 Purpose : Gets the (start, end) coordinates for the query or sbjct sequence
986 : in the HSP alignment.
987 Example : ($qbeg, $qend) = $sbjct->range('query');
988 : ($sbeg, $send) = $sbjct->range('hit');
989 Returns : Two-element array of integers
990 Argument : seq_type = string, 'query' or 'hit' or 'sbjct' (default = 'query')
991 ('sbjct' is synonymous with 'hit')
992 Throws : n/a
994 See Also : L<start()|start>, L<end()|end>
996 =cut
998 sub range {
999 my ($self, $seqType) = @_;
1000 $seqType ||= 'query';
1001 $seqType = 'hit' if $seqType eq 'sbjct';
1002 return ($self->start($seqType), $self->end($seqType));
1005 =head2 frac_identical
1007 Usage : $hit_object->frac_identical( [seq_type] );
1008 Purpose : Get the overall fraction of identical positions across all HSPs.
1009 : The number refers to only the aligned regions and does not
1010 : account for unaligned regions in between the HSPs, if any.
1011 Example : $frac_iden = $hit_object->frac_identical('query');
1012 Returns : Float (2-decimal precision, e.g., 0.75).
1013 Argument : seq_type: 'query' | 'hit' or 'sbjct' | 'total'
1014 : default = 'query' (but see comments below).
1015 : ('sbjct' is synonymous with 'hit')
1017 =cut
1019 sub frac_identical {
1020 my ($self, $seqType) = @_;
1021 $seqType ||= 'query';
1022 $seqType = lc($seqType);
1023 $seqType = 'hit' if $seqType eq 'sbjct';
1025 my $ident = $self->matches('id', $seqType);
1026 my $total = $self->length_aln($seqType);
1027 my $ratio = $ident / $total;
1028 my $ratio_rounded = sprintf( "%.3f", $ratio);
1030 # Round down if normal rounding yields 1 (just like blast)
1031 $ratio_rounded = 0.999 if (($ratio_rounded == 1) && ($ratio < 1));
1032 return $ratio_rounded;
1035 =head2 frac_conserved
1037 Usage : $hit_object->frac_conserved( [seq_type] );
1038 Purpose : Get the overall fraction of conserved positions across all HSPs.
1039 : The number refers to only the aligned regions and does not
1040 : account for unaligned regions in between the HSPs, if any.
1041 Example : $frac_cons = $hit_object->frac_conserved('hit');
1042 Returns : Float (2-decimal precision, e.g., 0.75).
1043 Argument : seq_type: 'query' | 'hit' or 'sbjct' | 'total'
1044 : default = 'query' (but see comments below).
1045 : ('sbjct' is synonymous with 'hit')
1047 =cut
1049 sub frac_conserved {
1050 my ($self, $seqType) = @_;
1051 $seqType ||= 'query';
1052 $seqType = lc($seqType);
1053 $seqType = 'hit' if $seqType eq 'sbjct';
1055 my $consv = $self->matches('cons');
1056 my $total = $self->length_aln($seqType);
1057 my $ratio = $consv / $total;
1058 my $ratio_rounded = sprintf( "%.3f", $ratio);
1060 # Round down iff normal rounding yields 1 (just like blast)
1061 $ratio_rounded = 0.999 if (($ratio_rounded == 1) && ($ratio < 1));
1062 return $ratio_rounded;
1065 =head2 frac_aligned_query
1067 Usage : $hit_object->frac_aligned_query();
1068 Purpose : Get the fraction of the query sequence which has been aligned
1069 : across all HSPs (not including intervals between non-overlapping
1070 : HSPs).
1071 Example : $frac_alnq = $hit_object->frac_aligned_query();
1072 Returns : Float (2-decimal precision, e.g., 0.75).
1073 Argument : none
1074 Comments : If you need data for each HSP, use hsps() and then interate
1075 : through the HSP objects.
1077 =cut
1079 sub frac_aligned_query {
1080 my $self = shift;
1081 return sprintf("%.2f", $self->length_aln('query') / $self->logical_length('query'));
1084 =head2 frac_aligned_hit
1086 Usage : $hit_object->frac_aligned_hit();
1087 Purpose : Get the fraction of the hit (sbjct) sequence which has been aligned
1088 : across all HSPs (not including intervals between non-overlapping
1089 : HSPs).
1090 Example : $frac_alnq = $hit_object->frac_aligned_hit();
1091 Returns : Float (2-decimal precision, e.g., 0.75).
1092 Argument : none
1093 Comments : If you need data for each HSP, use hsps() and then interate
1094 : through the HSP objects.
1096 =cut
1098 sub frac_aligned_hit {
1099 my $self = shift;
1100 return sprintf( "%.2f", $self->length_aln('sbjct') / $self->logical_length('sbjct'));
1103 =head2 num_unaligned_hit
1105 Usage : $hit_object->num_unaligned_hit();
1106 Purpose : Get the number of the unaligned residues in the hit sequence.
1107 : Sums across all all HSPs.
1108 Example : $num_unaln = $hit_object->num_unaligned_hit();
1109 Returns : Integer
1110 Argument : none
1111 Comments : If you need data for each HSP, use hsps() and then interate
1112 : through the HSP objects.
1114 =cut
1116 sub num_unaligned_hit {
1117 my $self = shift;
1118 # why does this method even exist?!
1119 return $self->gaps('hit');
1122 =head2 num_unaligned_query
1124 Usage : $hit_object->num_unaligned_query();
1125 Purpose : Get the number of the unaligned residues in the query sequence.
1126 : Sums across all all HSPs.
1127 Example : $num_unaln = $hit_object->num_unaligned_query();
1128 Returns : Integer
1129 Argument : none
1130 Comments : If you need data for each HSP, use hsps() and then interate
1131 : through the HSP objects.
1133 =cut
1135 sub num_unaligned_query {
1136 my $self = shift;
1137 # why does this method even exist?!
1138 return $self->gaps('query');
1141 # aliasing for Steve's method names
1142 *hit_description = \&description;
1143 *hit_length = \&length;