t/SeqFeature/Generic.t: fix typo on required module for testing
[bioperl-live.git] / lib / Bio / Search / HSP / PullHSPI.pm
blob7a3ee0aea987c93531b90abfc8fe963acecee0d7
2 # BioPerl module for Bio::Search::HSP::PullHSPI
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::HSP::PullHSPI - Bio::Search::HSP::HSPI interface for pull parsers.
16 =head1 SYNOPSIS
18 # This is an interface and cannot be instantiated
20 # generally we use Bio::SearchIO to build these objects
21 use Bio::SearchIO;
22 my $in = Bio::SearchIO->new(-format => 'hmmer_pull',
23 -file => 'result.hmmer');
25 while (my $result = $in->next_result) {
26 while (my $hit = $result->next_hit) {
27 while (my $hsp = $hit->next_hsp) {
28 $r_type = $hsp->algorithm;
29 $pvalue = $hsp->p();
30 $evalue = $hsp->evalue();
31 $frac_id = $hsp->frac_identical( ['query'|'hit'|'total'] );
32 $frac_cons = $hsp->frac_conserved( ['query'|'hit'|'total'] );
33 $gaps = $hsp->gaps( ['query'|'hit'|'total'] );
34 $qseq = $hsp->query_string;
35 $hseq = $hsp->hit_string;
36 $homo_string = $hsp->homology_string;
37 $len = $hsp->length( ['query'|'hit'|'total'] );
38 $len = $hsp->length( ['query'|'hit'|'total'] );
39 $rank = $hsp->rank;
45 =head1 DESCRIPTION
47 PullHSP is for fast implementations that only do parsing work on the hsp
48 data when you actually request information by calling one of the HSPI
49 methods.
51 Many methods of HSPI are implemented in a way suitable for inheriting classes
52 that use Bio::PullParserI. It only really makes sense for PullHSP modules to be
53 created by (and have as a -parent) PullHit modules.
55 In addition to the usual -chunk and -parent, -hsp_data is all you should supply
56 when making a PullHSP object. This will store that data and make it accessible
57 via _raw_hsp_data, which you can access in your subclass. It would be best to
58 simply provide the data as the input -chunk instead, if the raw data is large
59 enough.
61 =head1 SEE ALSO
63 This module inherits methods from these other modules:
65 L<Bio::SeqFeatureI>,
66 L<Bio::SeqFeature::FeaturePair>
67 L<Bio::SeqFeature::SimilarityPair>
69 Please refer to these modules for documentation of the
70 many additional inherited methods.
72 =head1 FEEDBACK
74 =head2 Mailing Lists
76 User feedback is an integral part of the evolution of this and other
77 Bioperl modules. Send your comments and suggestions preferably to
78 the Bioperl mailing list. Your participation is much appreciated.
80 bioperl-l@bioperl.org - General discussion
81 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
83 =head2 Support
85 Please direct usage questions or support issues to the mailing list:
87 I<bioperl-l@bioperl.org>
89 rather than to the module maintainer directly. Many experienced and
90 reponsive experts will be able look at the problem and quickly
91 address it. Please include a thorough description of the problem
92 with code and data examples if at all possible.
94 =head2 Reporting Bugs
96 Report bugs to the Bioperl bug tracking system to help us keep track
97 of the bugs and their resolution. Bug reports can be submitted via the
98 web:
100 https://github.com/bioperl/bioperl-live/issues
102 =head1 AUTHOR - Sendu Bala
104 Email bix@sendu.me.uk
106 =head1 COPYRIGHT
108 Copyright (c) 2006 Sendu Bala. All Rights Reserved.
110 =head1 DISCLAIMER
112 This software is provided "as is" without warranty of any kind.
114 =head1 APPENDIX
116 The rest of the documentation details each of the object methods.
117 Internal methods are usually preceded with a _
119 =cut
121 # Let the code begin...
123 package Bio::Search::HSP::PullHSPI;
126 use strict;
128 use base qw(Bio::Search::HSP::HSPI Bio::PullParserI);
130 =head2 _setup
132 Title : _setup
133 Usage : $self->_setup(@args)
134 Function: Implementers should call this to setup common fields and deal with
135 common arguments to new().
136 Returns : n/a
137 Args : @args received in new().
139 =cut
141 sub _setup {
142 my ($self, @args) = @_;
144 # fields most subclasses probably will want
145 $self->_fields( { ( hsp_length => undef,
146 identical => undef,
147 percent_identity => undef,
148 conserved => undef,
149 hsp_gaps => undef,
150 query_gaps => undef,
151 hit_gaps => undef,
152 evalue => undef,
153 pvalue => undef,
154 score => undef,
155 query_start => undef,
156 query_end => undef,
157 query_string => undef,
158 hit_start => undef,
159 hit_end => undef,
160 hit_string => undef,
161 homology_string => undef,
162 rank => undef,
163 seq_inds => undef,
164 hit_identical_inds => undef,
165 hit_conserved_inds => undef,
166 hit_nomatch_inds => undef,
167 hit_gap_inds => undef,
168 query_identical_inds => undef,
169 query_conserved_inds => undef,
170 query_nomatch_inds => undef,
171 query_gap_inds => undef ) } );
173 my ($parent, $chunk, $hsp_data) = $self->_rearrange([qw(PARENT
174 CHUNK
175 HSP_DATA)], @args);
177 $self->throw("Need -parent or -chunk to be defined") unless defined $parent || $chunk;
179 $self->parent($parent) if $parent;
181 if ($chunk) {
182 my ($io, $start, $end) = (undef, 0, undef);
183 if (ref($chunk) eq 'ARRAY') {
184 ($io, $start, $end) = @{$chunk};
186 else {
187 $io = $chunk;
189 $self->chunk($io, -start => $start, -end => $end);
192 $self->_raw_hsp_data($hsp_data) if $hsp_data;
194 return $self;
197 sub _raw_hsp_data {
198 my $self = shift;
199 if (@_) {
200 $self->{_raw_hsp_data} = shift;
202 return $self->{_raw_hsp_data};
206 # Some of these methods are written explitely to avoid HSPI throwing not
207 # implemented or the wrong ancestor class being used to answer the method;
208 # if it didn't do that then PullParserI AUTOLOAD would have cought them.
211 =head2 algorithm
213 Title : algorithm
214 Usage : my $r_type = $hsp->algorithm
215 Function: Obtain the name of the algorithm used to obtain the HSP
216 Returns : string (e.g., BLASTP)
217 Args : none
219 =cut
221 sub algorithm {
222 return shift->get_field('algorithm');
225 =head2 pvalue
227 Title : pvalue
228 Usage : my $pvalue = $hsp->pvalue();
229 Function: Returns the P-value for this HSP or undef
230 Returns : float or exponential (2e-10)
231 Args : none
233 =cut
235 sub pvalue {
236 return shift->get_field('pvalue');
239 =head2 evalue
241 Title : evalue
242 Usage : my $evalue = $hsp->evalue();
243 Function: Returns the e-value for this HSP
244 Returns : float or exponential (2e-10)
245 Args : none
247 =cut
249 sub evalue {
250 return shift->get_field('evalue');
253 *expect = \&evalue;
255 =head2 frac_identical
257 Title : frac_identical
258 Usage : my $frac_id = $hsp->frac_identical( ['query'|'hit'|'total'] );
259 Function: Returns the fraction of identitical positions for this HSP
260 Returns : Float in range 0.0 -> 1.0
261 Args : 'query' = num identical / length of query seq (without gaps)
262 'hit' = num identical / length of hit seq (without gaps)
263 'total' = num identical / length of alignment (with gaps)
264 default = 'total'
266 =cut
268 sub frac_identical {
269 my ($self, $type) = @_;
271 $type = lc $type if defined $type;
272 $type = 'hit' if (defined $type && $type =~ /subject|sbjct/);
273 $type = 'total' if (! defined $type || $type eq 'hsp' || $type !~ /query|hit|subject|sbjct|total/);
275 my $ratio = $self->num_identical($type) / $self->length($type);
276 return sprintf( "%.4f", $ratio);
279 =head2 frac_conserved
281 Title : frac_conserved
282 Usage : my $frac_cons = $hsp->frac_conserved( ['query'|'hit'|'total'] );
283 Function : Returns the fraction of conserved positions for this HSP.
284 This is the fraction of symbols in the alignment with a
285 positive score.
286 Returns : Float in range 0.0 -> 1.0
287 Args : 'query' = num conserved / length of query seq (without gaps)
288 'hit' = num conserved / length of hit seq (without gaps)
289 'total' = num conserved / length of alignment (with gaps)
290 default = 'total'
292 =cut
294 sub frac_conserved {
295 my ($self, $type) = @_;
297 $type = lc $type if defined $type;
298 $type = 'hit' if (defined $type && $type =~ /subject|sbjct/);
299 $type = 'total' if (! defined $type || $type eq 'hsp' || $type !~ /query|hit|subject|sbjct|total/);
301 my $ratio = $self->num_conserved($type) / $self->length($type);
302 return sprintf( "%.4f", $ratio);
305 =head2 num_identical
307 Title : num_identical
308 Usage : $obj->num_identical($newval)
309 Function: returns the number of identical residues in the alignment
310 Returns : integer
311 Args : integer (optional)
313 =cut
315 sub num_identical {
316 my $self = shift;
317 return scalar($self->seq_inds('hit', 'identical'));
320 =head2 num_conserved
322 Title : num_conserved
323 Usage : $obj->num_conserved($newval)
324 Function: returns the number of conserved residues in the alignment
325 Returns : inetger
326 Args : integer (optional)
328 =cut
330 sub num_conserved {
331 my $self = shift;
332 return scalar($self->seq_inds('hit', 'conserved-not-identical'));
335 =head2 gaps
337 Title : gaps
338 Usage : my $gaps = $hsp->gaps( ['query'|'hit'|'total'] );
339 Function : Get the number of gap characters in the query, hit, or total alignment.
340 Returns : Integer, number of gap characters or 0 if none
341 Args : 'query', 'hit' or 'total'; default = 'total'
343 =cut
345 sub gaps {
346 my ($self, $type) = @_;
347 $type = lc $type if defined $type;
348 $type = 'total' if (! defined $type || $type eq 'hsp' || $type !~ /query|hit|subject|sbjct|total/);
349 $type = 'hit' if $type =~ /sbjct|subject/;
351 if ($type eq 'total') {
352 return scalar($self->seq_inds('hit', 'gap')) + scalar($self->seq_inds('query', 'gap'));
354 return scalar($self->seq_inds($type, 'gap'));
357 =head2 query_string
359 Title : query_string
360 Usage : my $qseq = $hsp->query_string;
361 Function: Retrieves the query sequence of this HSP as a string
362 Returns : string
363 Args : none
365 =cut
367 sub query_string {
368 return shift->get_field('query_string');
371 =head2 hit_string
373 Title : hit_string
374 Usage : my $hseq = $hsp->hit_string;
375 Function: Retrieves the hit sequence of this HSP as a string
376 Returns : string
377 Args : none
379 =cut
381 sub hit_string {
382 return shift->get_field('hit_string');
385 =head2 homology_string
387 Title : homology_string
388 Usage : my $homo_string = $hsp->homology_string;
389 Function: Retrieves the homology sequence for this HSP as a string.
390 : The homology sequence is the string of symbols in between the
391 : query and hit sequences in the alignment indicating the degree
392 : of conservation (e.g., identical, similar, not similar).
393 Returns : string
394 Args : none
396 =cut
398 sub homology_string {
399 return shift->get_field('homology_string');
402 =head2 length
404 Title : length
405 Usage : my $len = $hsp->length( ['query'|'hit'|'total'] );
406 Function : Returns the length of the query or hit in the alignment (without gaps)
407 or the aggregate length of the HSP (including gaps;
408 this may be greater than either hit or query )
409 Returns : integer
410 Args : 'query' = length of query seq (without gaps)
411 'hit' = length of hit seq (without gaps)
412 'total' = length of alignment (with gaps)
413 default = 'total'
414 Args : none
416 =cut
418 sub length {
419 my ($self, $type) = @_;
420 $type = 'total' unless defined $type;
421 $type = lc $type;
423 if ($type =~ /^q/i) {
424 return $self->query->length;
426 elsif ($type =~ /^(hit|subject|sbjct)/) {
427 return $self->hit->length;
429 else {
430 return $self->hit->length + $self->gaps('hit');
434 =head2 hsp_length
436 Title : hsp_length
437 Usage : my $len = $hsp->hsp_length()
438 Function: shortcut length('hsp')
439 Returns : floating point between 0 and 100
440 Args : none
442 =cut
444 sub hsp_length {
445 return shift->length('total');
448 =head2 percent_identity
450 Title : percent_identity
451 Usage : my $percentid = $hsp->percent_identity()
452 Function: Returns the calculated percent identity for an HSP
453 Returns : floating point between 0 and 100
454 Args : none
456 =cut
458 sub percent_identity{
459 my ($self) = @_;
460 return $self->frac_identical('hsp') * 100;
463 =head2 get_aln
465 Title : get_aln
466 Usage : my $aln = $hsp->get_aln
467 Function: Returns a Bio::SimpleAlign representing the HSP alignment
468 Returns : Bio::SimpleAlign
469 Args : none
471 =cut
473 sub get_aln {
474 my $self = shift;
476 require Bio::LocatableSeq;
477 require Bio::SimpleAlign;
478 my $aln = Bio::SimpleAlign->new();
479 my $hs = $self->seq('hit');
480 my $qs = $self->seq('query');
481 if ($hs && $qs) {
482 $aln->add_seq($hs);
483 $aln->add_seq($qs);
484 return $aln;
486 return;
489 =head2 seq_inds
491 Title : seq_inds
492 Purpose : Get a list of residue positions (indices) for all identical
493 : or conserved residues in the query or sbjct sequence.
494 Example : @s_ind = $hsp->seq_inds('query', 'identical');
495 : @h_ind = $hsp->seq_inds('hit', 'conserved');
496 : @h_ind = $hsp->seq_inds('hit', 'conserved', 1);
497 Returns : List of integers
498 : May include ranges if collapse is true.
499 Argument : seq_type = 'query' or 'hit' or 'sbjct' (default = query)
500 ('sbjct' is synonymous with 'hit')
501 class = 'identical' or 'conserved' or 'nomatch' or 'gap'
502 (default = identical)
503 (can be shortened to 'id' or 'cons')
504 Note that 'conserved' includes identical unless you
505 use 'conserved-not-identical'
507 collapse = boolean, if true, consecutive positions are merged
508 using a range notation, e.g., "1 2 3 4 5 7 9 10 11"
509 collapses to "1-5 7 9-11". This is useful for
510 consolidating long lists. Default = no collapse.
511 Throws : n/a.
512 Comments :
514 See Also : L<Bio::Search::BlastUtils::collapse_nums()|Bio::Search::BlastUtils>, L<Bio::Search::Hit::HitI::seq_inds()|Bio::Search::Hit::HitI>
516 =cut
518 sub seq_inds {
519 my ($self, $seqType, $class, $collapse) = @_;
521 $seqType ||= 'query';
522 $class ||= 'identical';
523 $collapse ||= 0;
524 $seqType = lc($seqType);
525 $class = lc($class);
526 $seqType = 'hit' if $seqType eq 'sbjct';
528 my $t = substr($seqType,0,1);
529 if ($t eq 'q') {
530 $seqType = 'query';
532 elsif ($t eq 's' || $t eq 'h') {
533 $seqType = 'hit';
535 else {
536 $self->warn("unknown seqtype $seqType using 'query'");
537 $seqType = 'query';
540 $t = substr($class,0,1);
541 if ($t eq 'c') {
542 if ($class eq 'conserved-not-identical') {
543 $class = 'conserved';
545 else {
546 $class = 'conservedall';
549 elsif ($t eq 'i') {
550 $class = 'identical';
552 elsif ($t eq 'n') {
553 $class = 'nomatch';
555 elsif ($t eq 'g') {
556 $class = 'gap';
558 else {
559 $self->warn("unknown sequence class $class using 'identical'");
560 $class = 'identical';
563 $seqType .= '_';
564 $class .= '_inds';
566 my @ary;
567 if ($class eq 'conservedall_inds') {
568 my %tmp = map { $_, 1 } @{$self->get_field($seqType.'conserved_inds')},
569 @{$self->get_field($seqType.'identical_inds')};
570 @ary = sort {$a <=> $b} keys %tmp;
572 else {
573 @ary = @{$self->get_field($seqType.$class)};
576 return $collapse ? &Bio::Search::SearchUtils::collapse_nums(@ary) : @ary;
579 =head2 Inherited from L<Bio::SeqFeature::SimilarityPair>
581 These methods come from L<Bio::SeqFeature::SimilarityPair>
583 =head2 query
585 Title : query
586 Usage : my $query = $hsp->query
587 Function: Returns a SeqFeature representing the query in the HSP
588 Returns : Bio::SeqFeature::Similarity
589 Args : [optional] new value to set
592 =head2 hit
594 Title : hit
595 Usage : my $hit = $hsp->hit
596 Function: Returns a SeqFeature representing the hit in the HSP
597 Returns : Bio::SeqFeature::Similarity
598 Args : [optional] new value to set
601 =head2 significance
603 Title : significance
604 Usage : $evalue = $obj->significance();
605 $obj->significance($evalue);
606 Function: Get/Set the significance value (see Bio::SeqFeature::SimilarityPair)
607 Returns : significance value (scientific notation string)
608 Args : significance value (sci notation string)
610 =cut
612 sub significance {
613 return shift->get_field('evalue');
616 =head2 score
618 Title : score
619 Usage : my $score = $hsp->score();
620 Function: Returns the score for this HSP or undef
621 Returns : numeric
622 Args : [optional] numeric to set value
624 =cut
626 sub score {
627 return shift->get_field('score');
630 =head2 bits
632 Title : bits
633 Usage : my $bits = $hsp->bits();
634 Function: Returns the bit value for this HSP or undef
635 Returns : numeric
636 Args : none
638 =cut
640 sub bits {
641 return shift->get_field('bits');
644 # override
646 =head2 strand
648 Title : strand
649 Usage : $hsp->strand('query')
650 Function: Retrieves the strand for the HSP component requested
651 Returns : +1 or -1 (0 if unknown)
652 Args : 'hit' or 'subject' or 'sbjct' to retrieve the strand of the subject
653 'query' to retrieve the query strand (default)
654 'list' or 'array' to retrieve both query and hit together
656 =cut
658 sub strand {
659 my $self = shift;
660 my $val = shift;
661 $val = 'query' unless defined $val;
662 $val =~ s/^\s+//;
664 if ($val =~ /^q/i) {
665 return $self->query->strand(@_);
667 elsif ($val =~ /^hi|^s/i) {
668 return $self->hit->strand(@_);
670 elsif ($val =~ /^list|array/i) {
671 return ($self->query->strand(@_), $self->hit->strand(@_) );
673 else {
674 $self->warn("unrecognized component '$val' requested\n");
676 return 0;
679 =head2 start
681 Title : start
682 Usage : $hsp->start('query')
683 Function: Retrieves the start for the HSP component requested
684 Returns : integer, or list of two integers (query start and subject start) in
685 list context
686 Args : 'hit' or 'subject' or 'sbjct' to retrieve the start of the subject
687 'query' to retrieve the query start (default)
689 =cut
691 sub start {
692 my $self = shift;
693 my $val = shift;
694 $val = (wantarray ? 'list' : 'query') unless defined $val;
695 $val =~ s/^\s+//;
697 if ($val =~ /^q/i) {
698 return $self->query->start(@_);
700 elsif ($val =~ /^(hi|s)/i) {
701 return $self->hit->start(@_);
703 elsif ($val =~ /^list|array/i) {
704 return ($self->query->start(@_), $self->hit->start(@_) );
706 else {
707 $self->warn("unrecognized component '$val' requested\n");
709 return 0;
712 =head2 end
714 Title : end
715 Usage : $hsp->end('query')
716 Function: Retrieves the end for the HSP component requested
717 Returns : integer, or list of two integers (query end and subject end) in
718 list context
719 Args : 'hit' or 'subject' or 'sbjct' to retrieve the end of the subject
720 'query' to retrieve the query end (default)
722 =cut
724 sub end {
725 my $self = shift;
726 my $val = shift;
727 $val = (wantarray ? 'list' : 'query') unless defined $val;
728 $val =~ s/^\s+//;
730 if ($val =~ /^q/i) {
731 return $self->query->end(@_);
733 elsif ($val =~ /^(hi|s)/i) {
734 return $self->hit->end(@_);
736 elsif ($val =~ /^list|array/i) {
737 return ($self->query->end(@_), $self->hit->end(@_) );
739 else {
740 $self->warn("unrecognized end component '$val' requested\n");
742 return 0;
745 =head2 seq
747 Usage : $hsp->seq( [seq_type] );
748 Purpose : Get the query or sbjct sequence as a Bio::Seq.pm object.
749 Example : $seqObj = $hsp->seq('query');
750 Returns : Object reference for a Bio::LocatableSeq object.
751 Argument : seq_type = 'query' or 'hit' or 'sbjct' (default = 'query').
752 : ('sbjct' is synonymous with 'hit')
753 : default is 'query'
755 =cut
757 sub seq {
758 my ($self, $seqType) = @_;
759 $seqType ||= 'query';
760 $seqType = 'hit' if $seqType eq 'sbjct';
761 if ($seqType =~ /^(m|ho)/i ) {
762 $self->throw("cannot call seq on the homology match string, it isn't really a sequence, use get_aln to convert the HSP to a Bio::AlignIO and generate a consensus from that.");
765 my $str = $self->seq_str($seqType) || return;
766 require Bio::LocatableSeq;
767 my $id = ($seqType =~ /^q/i) ? $self->query->seq_id : $self->hit->seq_id;
768 return Bio::LocatableSeq->new( -ID => $id,
769 -SEQ => $str,
770 -START => $self->start($seqType),
771 -END => $self->end($seqType),
772 -STRAND => $self->strand($seqType),
773 -FORCE_NSE => $id ? 0 : 1,
774 -DESC => "$seqType sequence " );
778 =head2 seq_str
780 Usage : $hsp->seq_str( seq_type );
781 Purpose : Get the full query, sbjct, or 'match' sequence as a string.
782 : The 'match' sequence is the string of symbols in between the
783 : query and sbjct sequences.
784 Example : $str = $hsp->seq_str('query');
785 Returns : String
786 Argument : seq_Type = 'query' or 'hit' or 'sbjct' or 'match'
787 : ('sbjct' is synonymous with 'hit')
788 : default is 'query'
789 Throws : Exception if the argument does not match an accepted seq_type.
790 Comments :
792 See Also : L<seq()|seq>, L<seq_inds()|seq_inds>, B<_set_match_seq()>
794 =cut
796 sub seq_str {
797 my $self = shift;
798 my $type = shift || 'query';
800 if ($type =~ /^q/i) {
801 return $self->query_string(@_);
803 elsif ($type =~ /^(s)|(hi)/i) {
804 return $self->hit_string(@_);
806 elsif ($type =~ /^(ho)|(ma)/i) {
807 return $self->homology_string(@_);
809 else {
810 $self->warn("unknown sequence type $type");
812 return '';
815 =head2 rank
817 Usage : $hsp->rank( [string] );
818 Purpose : Get the rank of the HSP within a given Blast hit.
819 Example : $rank = $hsp->rank;
820 Returns : Integer (1..n) corresponding to the order in which the HSP
821 appears in the BLAST report.
823 =cut
825 sub rank {
826 return shift->get_field('rank');
829 =head2 matches
831 Usage : $hsp->matches(-seq => 'hit'|'query',
832 -start => $start,
833 -stop => $stop);
834 Purpose : Get the total number of identical and conservative matches
835 : in the query or sbjct sequence for the given HSP. Optionally can
836 : report data within a defined interval along the seq.
837 Example : ($id,$cons) = $hsp_object->matches(-seq => 'hit');
838 : ($id,$cons) = $hsp_object->matches(-seq => 'query',
839 -start => 300,
840 -stop => 400);
841 Returns : 2-element array of integers
842 Argument : (1) seq_type = 'query' or 'hit' or 'sbjct' (default = query)
843 : ('sbjct' is synonymous with 'hit')
844 : (2) start = Starting coordinate (optional)
845 : (3) stop = Ending coordinate (optional)
847 =cut
849 sub matches {
850 my ($self, @args) = @_;
851 my($seqType, $beg, $end) = $self->_rearrange([qw(SEQ START STOP)], @args);
852 $seqType ||= 'query';
853 $seqType = 'hit' if $seqType eq 'sbjct';
855 my @data;
856 if ((!defined $beg && !defined $end) || ! $self->seq_str('match')) {
857 push @data, ($self->num_identical, $self->num_conserved);
859 else {
860 $beg ||= 0;
861 $end ||= 0;
862 my ($start, $stop) = $self->range($seqType);
864 if ($beg == 0) {
865 $beg = $start;
866 $end = $beg+$end;
868 elsif ($end == 0) {
869 $end = $stop;
870 $beg = $end-$beg;
873 if ($end >= $stop) {
874 $end = $stop;
876 else {
877 $end += 1;
879 if ($beg < $start) {
880 $beg = $start;
883 my $seq = substr($self->seq_str('homology'), $beg-$start, ($end-$beg));
885 if (!CORE::length $seq) {
886 $self->throw("Undefined sub-sequence ($beg,$end). Valid range = $start - $stop");
888 ## Get data for a substring.
889 $seq =~ s/ //g; # remove space (no info).
890 my $len_cons = CORE::length $seq;
891 $seq =~ s/\+//g; # remove '+' characters (conservative substitutions)
892 my $len_id = CORE::length $seq;
893 push @data, ($len_id, $len_cons);
896 return @data;
899 =head2 n
901 Usage : $hsp_obj->n()
902 Purpose : Get the N value (num HSPs on which P/Expect is based).
903 Returns : Integer or null string if not defined.
904 Argument : n/a
905 Throws : n/a
906 Comments : The 'N' value is listed in parenthesis with P/Expect value:
907 : e.g., P(3) = 1.2e-30 ---> (N = 3).
908 : Not defined in NCBI Blast2 with gaps.
909 : This typically is equal to the number of HSPs but not always.
911 =cut
913 sub n {
914 return shift->get_field('num_hsps');
917 =head2 range
919 Usage : $hsp->range( [seq_type] );
920 Purpose : Gets the (start, end) coordinates for the query or sbjct sequence
921 : in the HSP alignment.
922 Example : ($query_beg, $query_end) = $hsp->range('query');
923 : ($hit_beg, $hit_end) = $hsp->range('hit');
924 Returns : Two-element array of integers
925 Argument : seq_type = string, 'query' or 'hit' or 'sbjct' (default = 'query')
926 : ('sbjct' is synonymous with 'hit')
927 Throws : n/a
928 Comments : This is a convenience method for constructions such as
929 ($hsp->query->start, $hsp->query->end)
931 =cut
933 sub range {
934 my ($self, $seqType) = @_;
935 $seqType ||= 'query';
937 my ($start, $end);
938 if ($seqType eq 'query') {
939 $start = $self->query->start;
940 $end = $self->query->end;
942 else {
943 $start = $self->hit->start;
944 $end = $self->hit->end;
946 return ($start, $end);
949 #*** would want cigar stuff from GenericHSP - move to HSPI?