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
14 Bio::Search::HSP::PullHSPI - Bio::Search::HSP::HSPI interface for pull parsers.
18 # This is an interface and cannot be instantiated
20 # generally we use Bio::SearchIO to build these objects
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;
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'] );
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
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
63 This module inherits methods from these other modules:
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.
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
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.
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
100 https://github.com/bioperl/bioperl-live/issues
102 =head1 AUTHOR - Sendu Bala
104 Email bix@sendu.me.uk
108 Copyright (c) 2006 Sendu Bala. All Rights Reserved.
112 This software is provided "as is" without warranty of any kind.
116 The rest of the documentation details each of the object methods.
117 Internal methods are usually preceded with a _
121 # Let the code begin...
123 package Bio
::Search
::HSP
::PullHSPI
;
128 use base
qw(Bio::Search::HSP::HSPI Bio::PullParserI);
133 Usage : $self->_setup(@args)
134 Function: Implementers should call this to setup common fields and deal with
135 common arguments to new().
137 Args : @args received in new().
142 my ($self, @args) = @_;
144 # fields most subclasses probably will want
145 $self->_fields( { ( hsp_length
=> undef,
147 percent_identity
=> undef,
155 query_start
=> undef,
157 query_string
=> undef,
161 homology_string
=> 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
177 $self->throw("Need -parent or -chunk to be defined") unless defined $parent || $chunk;
179 $self->parent($parent) if $parent;
182 my ($io, $start, $end) = (undef, 0, undef);
183 if (ref($chunk) eq 'ARRAY') {
184 ($io, $start, $end) = @
{$chunk};
189 $self->chunk($io, -start
=> $start, -end
=> $end);
192 $self->_raw_hsp_data($hsp_data) if $hsp_data;
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.
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)
222 return shift->get_field('algorithm');
228 Usage : my $pvalue = $hsp->pvalue();
229 Function: Returns the P-value for this HSP or undef
230 Returns : float or exponential (2e-10)
236 return shift->get_field('pvalue');
242 Usage : my $evalue = $hsp->evalue();
243 Function: Returns the e-value for this HSP
244 Returns : float or exponential (2e-10)
250 return shift->get_field('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)
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
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)
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);
307 Title : num_identical
308 Usage : $obj->num_identical($newval)
309 Function: returns the number of identical residues in the alignment
311 Args : integer (optional)
317 return scalar($self->seq_inds('hit', 'identical'));
322 Title : num_conserved
323 Usage : $obj->num_conserved($newval)
324 Function: returns the number of conserved residues in the alignment
326 Args : integer (optional)
332 return scalar($self->seq_inds('hit', 'conserved-not-identical'));
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'
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'));
360 Usage : my $qseq = $hsp->query_string;
361 Function: Retrieves the query sequence of this HSP as a string
368 return shift->get_field('query_string');
374 Usage : my $hseq = $hsp->hit_string;
375 Function: Retrieves the hit sequence of this HSP as a 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).
398 sub homology_string
{
399 return shift->get_field('homology_string');
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 )
410 Args : 'query' = length of query seq (without gaps)
411 'hit' = length of hit seq (without gaps)
412 'total' = length of alignment (with gaps)
419 my ($self, $type) = @_;
420 $type = 'total' unless defined $type;
423 if ($type =~ /^q/i) {
424 return $self->query->length;
426 elsif ($type =~ /^(hit|subject|sbjct)/) {
427 return $self->hit->length;
430 return $self->hit->length + $self->gaps('hit');
437 Usage : my $len = $hsp->hsp_length()
438 Function: shortcut length('hsp')
439 Returns : floating point between 0 and 100
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
458 sub percent_identity
{
460 return $self->frac_identical('hsp') * 100;
466 Usage : my $aln = $hsp->get_aln
467 Function: Returns a Bio::SimpleAlign representing the HSP alignment
468 Returns : Bio::SimpleAlign
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');
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.
514 See Also : L<Bio::Search::BlastUtils::collapse_nums()|Bio::Search::BlastUtils>, L<Bio::Search::Hit::HitI::seq_inds()|Bio::Search::Hit::HitI>
519 my ($self, $seqType, $class, $collapse) = @_;
521 $seqType ||= 'query';
522 $class ||= 'identical';
524 $seqType = lc($seqType);
526 $seqType = 'hit' if $seqType eq 'sbjct';
528 my $t = substr($seqType,0,1);
532 elsif ($t eq 's' || $t eq 'h') {
536 $self->warn("unknown seqtype $seqType using 'query'");
540 $t = substr($class,0,1);
542 if ($class eq 'conserved-not-identical') {
543 $class = 'conserved';
546 $class = 'conservedall';
550 $class = 'identical';
559 $self->warn("unknown sequence class $class using 'identical'");
560 $class = 'identical';
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;
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>
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
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
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)
613 return shift->get_field('evalue');
619 Usage : my $score = $hsp->score();
620 Function: Returns the score for this HSP or undef
622 Args : [optional] numeric to set value
627 return shift->get_field('score');
633 Usage : my $bits = $hsp->bits();
634 Function: Returns the bit value for this HSP or undef
641 return shift->get_field('bits');
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
661 $val = 'query' unless defined $val;
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(@_) );
674 $self->warn("unrecognized component '$val' requested\n");
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
686 Args : 'hit' or 'subject' or 'sbjct' to retrieve the start of the subject
687 'query' to retrieve the query start (default)
694 $val = (wantarray ?
'list' : 'query') unless defined $val;
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(@_) );
707 $self->warn("unrecognized component '$val' requested\n");
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
719 Args : 'hit' or 'subject' or 'sbjct' to retrieve the end of the subject
720 'query' to retrieve the query end (default)
727 $val = (wantarray ?
'list' : 'query') unless defined $val;
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(@_) );
740 $self->warn("unrecognized end component '$val' requested\n");
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')
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,
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 " );
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');
786 Argument : seq_Type = 'query' or 'hit' or 'sbjct' or 'match'
787 : ('sbjct' is synonymous with 'hit')
789 Throws : Exception if the argument does not match an accepted seq_type.
792 See Also : L<seq()|seq>, L<seq_inds()|seq_inds>, B<_set_match_seq()>
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(@_);
810 $self->warn("unknown sequence type $type");
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.
826 return shift->get_field('rank');
831 Usage : $hsp->matches(-seq => 'hit'|'query',
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',
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)
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';
856 if ((!defined $beg && !defined $end) || ! $self->seq_str('match')) {
857 push @data, ($self->num_identical, $self->num_conserved);
862 my ($start, $stop) = $self->range($seqType);
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);
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.
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.
914 return shift->get_field('num_hsps');
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')
928 Comments : This is a convenience method for constructions such as
929 ($hsp->query->start, $hsp->query->end)
934 my ($self, $seqType) = @_;
935 $seqType ||= 'query';
938 if ($seqType eq 'query') {
939 $start = $self->query->start;
940 $end = $self->query->end;
943 $start = $self->hit->start;
944 $end = $self->hit->end;
946 return ($start, $end);
949 #*** would want cigar stuff from GenericHSP - move to HSPI?