/
Sbjct.pm
2408 lines (1893 loc) · 79.4 KB
/
Sbjct.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#------------------------------------------------------------------------------
# PACKAGE : Bio::Tools::Blast::Sbjct.pm
# AUTHOR : Steve A. Chervitz (sac@genome.stanford.edu)
# CREATED : 7 October 1996
# STATUS : Alpha
# REVISION: $Id$
#
# For the latest version and documentation, visit the distribution site:
# http://genome-www.stanford.edu/perlOOP/bioperl/blast/
#
# To generate documentation, run this module through pod2html
# (preferably from Perl v5.004 or better).
#
# Copyright (c) 1996-2000 Steve A. Chervitz. All Rights Reserved.
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#------------------------------------------------------------------------------
package Bio::Tools::Blast::Sbjct;
use Bio::Root::Global qw(:devel);
use Bio::Root::Object ();
@ISA = qw( Bio::Root::Object );
use strict;
use vars qw($ID $VERSION %SUMMARY_OFFSET $Revision);
$ID = 'Bio::Tools::Blast::Sbjct';
$VERSION = 0.09;
$Revision = '$Id$'; #'
my $_prog = '';
my $_signif_fmt = '';
## POD Documentation:
=head1 NAME
Bio::Tools::Blast::Sbjct.pm - Bioperl BLAST "Hit" object
=head1 SYNOPSIS
=head2 Object Creation
The construction of HSP objects is handled by B<Bio::Tools::Blast.pm>.
You should not need to use this package directly. See L<_initialize>()
for a description of constructor parameters.
require Bio::Tools::Blast::Sbjct;
$hit = new Bio::Tools::Blast::Sbjct (-DATA =>\@hitData,
-PARENT =>$self,
-NAME =>5,
-RANK =>5,
-RANK_BY =>'order',
-MAKE =>'query' (or 'sbjct'),
-OVERLAP =>2,
-PROGRAM =>'TBLASTN'
);
@hitData includes the summary line for the hit as element [0], plus
all lines from the HSP alignment section of the BLAST report for
the present hit.
=head1 INSTALLATION
This module is included with the central Bioperl distribution:
http://bio.perl.org/Core/Latest
ftp://bio.perl.org/pub/DIST
Follow the installation instructions included in the README file.
=head1 DESCRIPTION
The Bio::Tools::Blast::Sbjct.pm module encapsulates data and methods for
parsing and manipulating "hits" from a BLAST report.
This module is a utility module used by the Bio::Tools::Blast.pm
and is not intended for separate use.
In Blast lingo, the "sbjct" sequences are all the sequences
in a target database which were compared against a "query" sequence.
The terms "sbjct" and "hit" will be used interchangeably in this and related modules.
This module supports BLAST versions 1.x and 2.x, gapped and ungapped.
=head2 HSP Tiling and Ambiguous Alignments
If a Blast hit has more than one HSP, the Bio::Tools::Blast::Sbjct.pm object has
the ability to merge overlapping HSPs into contiguous blocks. This permits the Sbjct object
to sum data across all HSPs without counting data in the overlapping regions
multiple times, which would happen if data from each overlapping HSP are simply summed.
HSP tiling is performed automatically when methods of the Sbjct object that rely on
tiled data are invoked. These include L<frac_identical>(), L<frac_conserved>(), L<gaps>(),
L<frac_aligned_query>(), L<frac_aligned_hit>(), L<num_unaligned_query>(), L<num_unaligned_hit>().
It also permits the assessment of an "ambiguous alignment" if the query (or sbjct)
sequences from different HSPs overlap. The existence of an overlap could indicate a
biologically interesting region in the sequence, such as a repeated domain.
The Sbjct object uses the -OVERLAP parameter to determine when two sequences overlap;
if this is set to 2 -- the default -- then any two sbjct or query HSP sequences
must overlap by more than two residues to get merged into the same contig and counted
as an overlap. See the L<BUGS> section below for "issues" with HSP tiling.
The results of the HSP tiling is reported with the following ambiguity codes:
'q' = Query sequence contains multiple sub-sequences matching
a single region in the sbjct sequence.
's' = Sbjct sequence contains multiple sub-sequences matching
a single region in the query sequence.
'qs' = Both query and sbjct sequences contain more than one
sub-sequence with similarity to the other sequence.
For addition information about ambiguous BLAST alignments, see
L<_tile_hsps>() and
http://www-genome.stanford.edu/Sacch3D/help/ambig_aln.html
=head1 DEPENDENCIES
Bio::Tools::Blast::Sbjct.pm is a concrete class that inherits from B<Bio::Root::Object.pm>
and relies on two other modules:
=over 4
=item B<Bio::Tools::Blast::HSP.pm>
Encapsulates a single high-scoring segment pair within a hit.
=item B<Bio::Tools::Blast.pm>
Provides a container for Sbjct.pm objects.
=back
Bio::Tools::Blast::Sbjct.pm does not currently inherit from Bio::Root::Vector.pm
since Bio::Root::Vector.pm may be re-designed to make it usable via delegation.
Thus, a Blast.pm object would manage a vector of Sbjct.pm objects.
Stay tuned.
=head1 BUGS
One consequence of the HSP tiling is that methods that rely on HSP tiling such as
L<frac_identical>(), L<frac_conserved>(), L<gaps>() etc. may report misleading numbers
when C<-OVERLAP> is set to a large number.
For example, say we have two HSPs and the query sequence tile as follows:
1 8 22 30 40 60
Full seq: ------------------------------------------------------------
* ** * **
HSP1: --------------- (6 identical matches)
** ** **
HSP2: ------------- (6 identical matches)
If C<-OVERLAP> is set to some number over 4, HSP1 and HSP2 will not be tiled into a single
contig and their numbers of identical matches will be added, giving a total of 12,
not 10 if they had be combined into one contig. This can lead to number greater than 1.0 for
methods L<frac_identical>() and L<frac_conserved>(). This is less of an issue with gapped
Blast since it tends to combine HSPs that would be listed separately without gapping.
(Fractions E<gt>1.0 can be viewed as a signal for an interesting alignment that warrants
further inspection, thus turning this bug into a feature).
Using large values for C<-OVERLAP> can lead to incorrect numbers reported by methods that
rely on HSP tiling but can be useful if you care more about detecting ambiguous alignments.
Setting C<-OVERLAP> to zero will lead to the most accurate numbers for the tiling-dependent
methods but will be useless for detecting overlapping HSPs since all HSPs will appear to
overlap.
=head1 SEE ALSO
Bio::Tools::Blast::HSP.pm - Blast HSP object.
Bio::Tools::Blast.pm - Blast object.
Bio::Root::Object.pm - Proposed base class for all Bioperl objects.
Links:
http://bio.perl.org/Core/POD/Tools/Blast/HSP.pm.html
http://bio.perl.org/Projects/modules.html - Online module documentation
http://bio.perl.org/Projects/Blast/ - Bioperl Blast Project
http://bio.perl.org/ - Bioperl Project Homepage
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other Bioperl modules.
Send your comments and suggestions preferably to one of the Bioperl mailing lists.
Your participation is much appreciated.
vsns-bcd-perl@lists.uni-bielefeld.de - General discussion
vsns-bcd-perl-guts@lists.uni-bielefeld.de - Technically-oriented discussion
http://bio.perl.org/MailList.html - About the mailing lists
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track the bugs and
their resolution. Bug reports can be submitted via email or the web:
bioperl-bugs@bio.perl.org
http://bio.perl.org/bioperl-bugs/
=head1 AUTHOR
Steve A. Chervitz, sac@genome.stanford.edu
See the L<FEEDBACK> section for where to send bug reports and comments.
=head1 VERSION
Bio::Tools::Blast::Sbjct.pm, 0.09
=head1 COPYRIGHT
Copyright (c) 1996-98 Steve A. Chervitz. All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
#
##
###
#### END of main POD documentation.
###
##
#'
=head1 APPENDIX
Methods beginning with a leading underscore are considered private
and are intended for internal use by this module. They are
B<not> considered part of the public interface and are described here
for documentation purposes only.
=cut
#####################################################################################
## CONSTRUCTOR ##
#####################################################################################
=head2 _initialize
Usage : n/a; automatically called by Bio::Root::Object::new()
: Bio::Tools::Blast::Sbjct.pm objects are constructed
: automatically by Bio::Tools::Blast.pm, so there is no need
: for direct consumption.
Purpose : Initializes key varaiables and calls methods to parse a single Blast hit.
: Constructs Bio::Tools::Blast::HSP.pm objects for each
: high-scoring segment pair (HSP).
: Calls superclass constructor first (Bio::Root::Object.pm).
Returns : n/a
Argument : Named Parameters passed from new()
: ALL TAGS MUST BE UPPERCASE (does not call _rearrange()).
: -DATA => array reference holding all data for a single hit.
: The first element should hold the description
: line (from the desctiption section at the top of
: the report), remaining lines should hold all lines
: within the HSP alignment listing section of report.
: -PARENT => object reference to a Bio::Tools::Blast.pm object.
: -NAME => string (typically same as -RANK, just a temporary
: name to use until the actual name of hit is parsed),
: -RANK => integer,
: -RANK_BY => 'order',
: -OVERLAP => integer (maximum overlap between adjacent
: HSPs when tiling)
: -PROGRAM => string (type of Blast blastp, blastn, etc).
See Also : L<_set_id>(), L<_set_hsps>(), L<_tile_hsps>(), B<Bio::Root::Object.pm>::new, B<Bio::Tools::Blast.pm>::_set_hit_db
=cut
#-------------------
sub _initialize {
#-------------------
my( $self, %param ) = @_;
# $make not currently used.
my $make = $self->SUPER::_initialize( %param );
# Set various class data.
$_prog = $param{-PROGRAM} || '';
$_signif_fmt = $param{-SIGNIF_FMT};
$self->{'_rank'} = $param{-RANK} || '';
$self->_set_id( $param{-DATA}->[0]);
$self->_set_hsps( @{$param{-DATA}} );
$self->{'_overlap'} = $param{-OVERLAP} || 0;
}
#--------------
sub destroy {
#--------------
my $self=shift;
if($self->{'_hsps'}) {
foreach($self->hsps) {
$_->destroy;
undef $_;
}
undef $self->{'_hsps'};
}
$DEBUG==2 && print STDERR "DESTROYING $self ${\$self->name}";
$self->SUPER::destroy;
}
#####################################################################################
## ACCESSORS ##
#####################################################################################
=head2 rank
Usage : $sbjct->rank( integer or string );
Purpose : Sets/Gets the rank of the current Sbjct object relative to
: other Sbjct objects managed by a given Blast object.
Example : $sbjct->rank(1);
Returns : The current rank value.
Argument : Integer or string to be used for ranking the hit
: relative to other hits.
Throws : n/a
Comments : The rank usually corresponds to the order the listing
: of hits in the BLAST report from lowest to highest p-value.
: Rank need not be restricted to this value.
: rank() may be provided by a delegated or inherited
: iterator class in the future (such as Bio::Root::Vector.pm).
=cut
#-----------
sub rank {
#-----------
my $self = shift;
if(@_) {$self->{'_rank'} = shift; }
$self->{'_rank'};
}
=head2 _set_id
Usage : n/a; automatically called by _initialize()
Purpose : Sets the name of the Sbjct sequence from the BLAST summary line.
: The identifier is assumed to be the first
: chunk of non-whitespace characters in the description line
: Does not assume any semantics in the structure of the identifier
: (Formerly, this method attempted to extract database name from
: the seq identifiers, but this was prone to break).
Returns : n/a
Argument : String containing description line of the hit from Blast report
: or first line of an alignment section.
Throws : Warning if cannot locate sequence ID.
See Also : L<_initialize>(), B<Bio::Tools::Blast.pm>::database
=cut
#---------------
sub _set_id {
#---------------
my( $self, $desc ) = @_;
my ($seqID1, $seqID2, $dbID, @seqDat);
local $_ = $desc;
my @linedat = split();
my $data = $linedat[0];
# New strategy: Assume only that the ID is the first white space
# delimited chunk. Not attempting to extract database name.
# Clients will have to interpret it as necessary.
if($data =~ /^(\S+)\s*/) {
$self->name($1);
}
else {
$self->warn("Can't locate sequence identifier in summary line.", "Line = $data");
$data = 'Unknown sequence ID' if not $data;
$self->name($data);
}
$self->{'_db'} = '-';
# Old strategy: assumes semantics in the identifier
# and tries to separate out database and id components.
# Too fancy and fragile! SAC, 2000-02-18
# # Proceeding from more standard (NCBI-like) to less standard.
# if($data =~ /(\S+?)[:\|]+(\S+?)[:\|]+(\S*)/) {
# # matches: database|id1|id2 or database||id1||id2 or database:id1:id2
# $dbID = $1;
# $seqID1 = $2;
# $seqID2 = $3;
# if($seqID2 eq $seqID1) { undef($seqID2); }
#
# } elsif($data =~ /(\S+?)[:\|]+(\S+)/) {
# # matches: database|id1 or database:id1
# $dbID = $1;
# $seqID1 = $2;
#
# } elsif($data =~ /^(\S+)\s+([gb|emb|dbj|sp|pir])\s+(\S+)*/) {
# # matches: id1 database id2
# $seqID1 = $1;
# $dbID = $2;
# $seqID2 = $3;
#
# } elsif($data =~ /^(\S+)\s*/) {
# $seqID1 = $1;
# }
#
# ## Combine the multiple IDs.
# $seqID2 = scalar($seqID2) ? "/$seqID2" : '';
#
# if( !scalar $seqID1) {
# $self->warn("Can't locate sequence identifier in summary line.", "Line = $data");
# $self->name('Unknown sequence ID');
# } else {
# $self->name($seqID1.$seqID2);
# }
# $self->{'_db'} = $dbID || '-';
}
=head2 _set_hsps
Usage : n/a; called automatically during object construction.
Purpose : Creates HSP.pm objects for each HSP in a BLAST hit alignment.
: Also collects the full description of the hit from the
: HSP alignment section.
Returns : n/a
Argument : List of strings containing raw BLAST report data for
: a single hit's HSP alignment data.
Throws : Warnings for each HSP.pm object that fails to be constructed.
: Exception if no HSP.pm objects can be constructed.
: Exception if can't parse length data for hit sequence.
Comments : Requires Bio::Tools::Blast::HSP.pm.
: Sets the description using the full string present in
: the alignment data.
: Also sets Expect and P-values for the Sbjct object by
: copying from the HSP object.
: While this sacrifices some memory efficiency, it
: improves access speed for these critical data.
See Also : L<_initialize>(), L<_set_desc>()
=cut
#--------------
sub _set_hsps {
#--------------
my( $self, @data ) = @_;
my $start = 0;
my $hspCount = 0;
require Bio::Tools::Blast::HSP;
# printf STDERR "$ID _set_hsps(). DATA (%d lines) =\n@data\n", scalar(@data); <STDIN>;
my( @hspData, @hspList, @errs, @bad_names );
my($line, $set_desc, @desc);
$set_desc = 0;
hit_loop:
foreach $line( @data ) {
if( $line =~ /^\s*Length = ([\d,]+)/ ) {
$self->_set_desc(@desc);
$set_desc = 1;
($self->{'_length'} = $1) =~ s/,//g; # get rid of commas
next hit_loop;
} elsif( !$set_desc) {
$line =~ s/^\s+|\s+$//g;
push @desc, $line;
next hit_loop;
} elsif( $line =~ /^\s*Score/ ) {
## This block is for setting multiple HSPs.
if( not scalar @hspData ) {
$start = 1;
push @hspData, $line;
next hit_loop;
} elsif( scalar @hspData) {
$hspCount++;
$DEBUG and do{ print STDERR +( $hspCount % 10 ? "+" : "+\n" ); };
# print STDERR "\n$ID: setting HSP: ${\$self->name}\n";
my $hspObj = eval { new Bio::Tools::Blast::HSP(-DATA =>\@hspData,
-PARENT =>$self,
-NAME =>$hspCount,
-PROGRAM =>$_prog,
);
};
if($@) {
# print "$ID: ERROR:\n$@";<STDIN>;
push @errs, $@;
push @bad_names, "#$hspCount";
$hspObj->destroy if ref $hspObj;
undef $hspObj;
} else {
push @hspList, $hspObj;
if (!defined($self->{'_expect'}) || $hspObj->expect() < $self->{'_expect'}) {
$self->{'_expect'} = $hspObj->expect();
}
if (!defined($self->{'_p'}) || $hspObj->p() < $self->{'_p'}) {
$self->{'_p'} = $hspObj->p();
}
}
@hspData = ();
push @hspData, $line;
next;
} else {
push @hspData, $line;
}
} elsif( $start ) {
## This block is for setting the last HSP (which may be the first as well!).
if( $line =~ /^(end|>|Parameters|CPU|Database:)/ ) {
$hspCount++;
$DEBUG and do{ print STDERR +( $hspCount % 10 ? "+" : "+\n" ); };
# print STDERR "\n$ID: setting HSP: ${\$self->name}\n";
my $hspObj = eval { new Bio::Tools::Blast::HSP(-DATA =>\@hspData,
-PARENT =>$self,
-NAME =>$hspCount,
-PROGRAM =>$_prog,
);
};
if($@) {
# print "$ID: ERROR:\n$@";<STDIN>;
push @errs, $@;
push @bad_names, "#$hspCount";
$hspObj->destroy if ref $hspObj;
undef $hspObj;
} else {
push @hspList, $hspObj;
if (!defined($self->{'_expect'}) || $hspObj->expect() < $self->{'_expect'}) {
$self->{_expect} = $hspObj->expect();
}
if (!defined($self->{'_p'}) || $hspObj->p() < $self->{'_p'}) {
$self->{'_p'} = $hspObj->p();
}
}
} else {
push @hspData, $line;
}
}
}
# print STDERR "\n--------> Done building HSPs for ${\$self->name}\n";
$self->{'_length'} or $self->throw( "Can't determine hit sequence length.");
# Adjust logical length based on BLAST flavor.
if($_prog =~ /TBLAST[NX]/) {
$self->{'_logical_length'} = $self->{'_length'} / 3;
}
# Handling errors as done in Blast.pm. (as of version 0.073)
if(@errs) {
my ($str);
# When there are many errors, in most of the cases, they are
# caused by the same problem. Only need to see full data for
# the first one.
if(@errs > 2) {
$str = "SHOWING FIRST EXCEPTION ONLY:\n$errs[0]";
$self->clear_err(); # clearing the existing set of errors.
# Not necessary, unless the -RECORD_ERR =>1
# constructor option was used for Blast object.
} else {
$str = join("\n",@errs);
}
if( not scalar @hspList) {
$self->throw("Failed to create any HSP objects for $hspCount potential HSP(s).",
"\n\nTRAPPED EXCEPTION(S):\n$str\nEND TRAPPED EXCEPTION(S)\n"
);
} else {
$self->warn(sprintf("Could not create HSP objects for %d HSP(s): %s", scalar(@errs), join(', ',@bad_names)),
"\n\nTRAPPED EXCEPTION(S):\n$str\nEND TRAPPED EXCEPTION(S)\n"
);
}
} else {
$self->{'_hsps'} = \@hspList;
}
}
=head2 _set_desc
Usage : n/a; called automatically by _set_hsps()
Purpose : Sets the description of the hit sequence.
: For sequence without descriptions, sets description to "-".
Argument : Array containing description (multiple lines).
Comments : _set_hsps() calls this method with the data from the
: HSP alignment listing, which contains the complete description.
: (Formerly, this was called from the _set_desc_data() method initially.)
See Also : _set_hsps()
=cut
#--------------
sub _set_desc {
#--------------
my( $self, @desc ) = @_;
my( $desc);
# print "$ID: RAW DESC:\n@desc";<STDIN>;
$desc = join(" ", @desc);
if($desc) {
$desc =~ s/^\s*\S+\s+//; # remove the sequence ID(s)
$desc =~ s/^[\s!]+//;
$desc =~ s/ \d+$//;
$desc =~ s/\.+$//;
$self->{'_desc'} = $desc || '-';
} else {
$self->{'_desc'} = '-';
}
# print "$ID: _set_desc = $desc";<STDIN>;
}
=head2 _tile_hsps
Usage : n/a; called automatically during object construction or
: as needed by methods that rely on having tiled data.
Purpose : Collect statistics about the aligned sequences in a set of HSPs.
: Calculates the following data across all HSPs:
: -- total alignment length
: -- total identical residues
: -- total conserved residues
Returns : n/a
Argument : n/a
Throws : n/a
Status : Experimental
Comments :
: This method performs more careful summing of data across
: all HSPs in the Sbjct object. Simply summing the data from all HSPs
: will overestimate the actual length of the alignment if there is
: overlap between different HSPs (often the case).
: The strategy is to tile the HSPs and sum over the
: contigs, collecting data separately from overlapping and
: non-overlapping regions of each HSP. To facilitate this, the
: HSP.pm object now permits extraction of data from sub-sections
: of an HSP.
:
: Additional useful information is collected from the results
: of the tiling. It is possible that sub-sequences in
: different HSPs will overlap significantly. In this case, it
: is impossible to create a single unambiguous alignment by
: concatenating the HSPs. The ambiguity may indicate the
: presence of multiple, similar domains in one or both of the
: aligned sequences. This ambiguity is recorded using the
: ambiguous_aln() method.
:
: This method does not attempt to discern biologically
: significant vs. insignificant overlaps. The allowable amount of
: overlap can be set with the overlap() method or with the -OVERLAP
: parameter used when constructing the Blast & Sbjct objects.
:
: For a given hit, both the query and the sbjct sequences are
: tiled independently.
:
: -- If only query sequence HSPs overlap,
: this may suggest multiple domains in the sbjct.
: -- If only sbjct sequence HSPs overlap,
: this may suggest multiple domains in the query.
: -- If both query & sbjct sequence HSPs overlap,
: this suggests multiple domains in both.
: -- If neither query & sbjct sequence HSPs overlap,
: this suggests either no multiple domains in either
: sequence OR that both sequences have the same
: distribution of multiple similar domains.
:
: This method can deal with the special case of when multiple
: HSPs exactly overlap.
:
: Efficiency concerns:
: Speed will be an issue for sequences with numerous HSPs.
:
Bugs : Currently, _tile_hsps() does not properly account for
: the number of non-tiled but overlapping HSPs, which becomes a problem
: as overlap() grows. Large values overlap() may thus lead to
: incorrect statistics for some hits. For best results, keep overlap()
: below 5 (DEFAULT IS 2). For more about this, see the "HSP Tiling and
: Ambiguous Alignments" section.
See Also : L<_adjust_contigs>(), L<ambiguous_aln>(), L<overlap>(), L<frac_identical>(), L<frac_conserved>(), L<frac_aligned_query>(), L<frac_aligned_hit>(), L<num_unaligned_query>(), L<num_unaligned_hit>(), L<HSP Tiling and Ambiguous Alignments>
=cut
#--------------
sub _tile_hsps {
#--------------
my $self = shift;
# my $gapped = $self->parent->gapped || 0; # no special treatment
$self->{'_tile_hsps'} = 1;
$self->{'_gaps_query'} = 0;
$self->{'_gaps_sbjct'} = 0;
## Simple summation scheme. Valid if there is only one HSP.
if((defined($self->{'_n'}) and $self->{'_n'} == 1) or $self->num_hsps == 1) {
my $hsp = $self->hsp;
$self->{'_length_aln_query'} = $hsp->length('query');
$self->{'_length_aln_sbjct'} = $hsp->length('sbjct');
$self->{'_length_aln_total'} = $hsp->length('total');
($self->{'_totalIdentical'},$self->{'_totalConserved'}) = $hsp->matches();
$self->{'_gaps_query'} = $hsp->gaps('query');
$self->{'_gaps_sbjct'} = $hsp->gaps('sbjct');
# print "_tile_hsps(): single HSP, easy stats.\n";
return;
} else {
# print STDERR "$ID: _tile_hsps: summing multiple HSPs\n";
$self->{'_length_aln_query'} = 0;
$self->{'_length_aln_sbjct'} = 0;
$self->{'_length_aln_total'} = 0;
$self->{'_totalIdentical'} = 0;
$self->{'_totalConserved'} = 0;
}
## More than one HSP. Must tile HSPs.
# printf "\nTiling HSPs for %s (BLAST: %s)\n",$self->name, $self->parent->name;
my($hsp, $qstart, $qstop, $sstart, $sstop);
my(@qcontigs, @scontigs);
my $qoverlap = 0;
my $soverlap = 0;
my $max_overlap = $self->{'_overlap'};
foreach $hsp ($self->hsps()) {
# printf " HSP: %s\n%s\n",$hsp->name, $hsp->str('query');
# printf " Length = %d; Identical = %d; Conserved = %d; Conserved(1-10): %d",$hsp->length, $hsp->length(-TYPE=>'iden'), $hsp->length(-TYPE=>'cons'), $hsp->length(-TYPE=>'cons',-START=>0,-STOP=>10); <STDIN>;
($qstart, $qstop) = $hsp->range('query');
($sstart, $sstop) = $hsp->range('sbjct');
my ($qgaps, $sgaps) = $hsp->gaps();
$self->{'_gaps_query'} += $qgaps;
$self->{'_gaps_sbjct'} += $sgaps;
$self->{'_length_aln_total'} += $hsp->length;
## Collect contigs in the query sequence.
$qoverlap = &_adjust_contigs('query', $hsp, $qstart, $qstop, \@qcontigs, $max_overlap);
## Collect contigs in the sbjct sequence (needed for domain data and gapped Blast).
$soverlap = &_adjust_contigs('sbjct', $hsp, $sstart, $sstop, \@scontigs, $max_overlap);
## Collect overall start and stop data for query and sbjct over all HSPs.
if(not defined $self->{'_queryStart'}) {
$self->{'_queryStart'} = $qstart;
$self->{'_queryStop'} = $qstop;
$self->{'_sbjctStart'} = $sstart;
$self->{'_sbjctStop'} = $sstop;
} else {
$self->{'_queryStart'} = ($qstart < $self->{'_queryStart'} ? $qstart : $self->{'_queryStart'});
$self->{'_queryStop'} = ($qstop > $self->{'_queryStop'} ? $qstop : $self->{'_queryStop'});
$self->{'_sbjctStart'} = ($sstart < $self->{'_sbjctStart'} ? $sstart : $self->{'_sbjctStart'});
$self->{'_sbjctStop'} = ($sstop > $self->{'_sbjctStop'} ? $sstop : $self->{'_sbjctStop'});
}
}
## Collect data across the collected contigs.
# print "\nQUERY CONTIGS:\n";
# print " gaps = $self->{'_gaps_query'}\n";
foreach(@qcontigs) {
# print " query contig: $_->{'start'} - $_->{'stop'}\n";
# print " iden = $_->{'iden'}; cons = $_->{'cons'}\n";
$self->{'_length_aln_query'} += $_->{'stop'} - $_->{'start'} + 1;
$self->{'_totalIdentical'} += $_->{'iden'};
$self->{'_totalConserved'} += $_->{'cons'};
}
## Collect data for sbjct contigs. Important for gapped Blast.
## The totalIdentical and totalConserved numbers will be the same
## as determined for the query contigs.
# print "\nSBJCT CONTIGS:\n";
# print " gaps = $self->{'_gaps_sbjct'}\n";
foreach(@scontigs) {
# print " sbjct contig: $_->{'start'} - $_->{'stop'}\n";
# print " iden = $_->{'iden'}; cons = $_->{'cons'}\n";
$self->{'_length_aln_sbjct'} += $_->{'stop'} - $_->{'start'} + 1;
}
# <STDIN>;
if($qoverlap) {
if($soverlap) { $self->ambiguous_aln('qs');
# print "\n*** AMBIGUOUS ALIGNMENT: Query and Sbjct\n\n";
}
else { $self->ambiguous_aln('q');
# print "\n*** AMBIGUOUS ALIGNMENT: Query\n\n";
}
} elsif($soverlap) {
$self->ambiguous_aln('s');
# print "\n*** AMBIGUOUS ALIGNMENT: Sbjct\n\n";
}
# Adjust length based on BLAST flavor.
my $prog = $self->parent->program;
if($prog eq 'TBLASTN') {
$self->{'_length_aln_sbjct'} /= 3;
} elsif($prog eq 'BLASTX' ) {
$self->{'_length_aln_query'} /= 3;
} elsif($prog eq 'TBLASTX') {
$self->{'_length_aln_query'} /= 3;
$self->{'_length_aln_sbjct'} /= 3;
}
}
=head2 _adjust_contigs
Usage : n/a; called automatically during object construction.
Purpose : Builds HSP contigs for a given BLAST hit.
: Utility method called by _tile_hsps()
Returns :
Argument :
Throws : Exceptions propagated from Bio::Tools::Blast::HSP::matches()
: for invalid sub-sequence ranges.
Status : Experimental
Comments : This method does not currently support gapped alignments.
: Also, it does not keep track of the number of HSPs that
: overlap within the amount specified by overlap().
: This will lead to significant tracking errors for large
: overlap values.
See Also : L<overlap>(), L<_tile_hsps>(), B<Bio::Tools::Blast::HSP.pm>::matches
=cut
#-------------------
sub _adjust_contigs {
#-------------------
my ($seqType, $hsp, $start, $stop, $contigs_ref, $max_overlap) = @_;
my $overlap = 0;
my ($numID, $numCons);
# print "Testing $seqType data: HSP (${\$hsp->name}); $start, $stop\n";
foreach(@$contigs_ref) {
# print " Contig: $_->{'start'} - $_->{'stop'}, iden= $_->{'iden'}, cons= $_->{'cons'}\n";
## Test special case of a nested HSP. Skip it.
if($start >= $_->{'start'} and $stop <= $_->{'stop'}) {
# print "----> Nested HSP. Skipping.\n";
$overlap = 1;
next;
}
## Test for overlap at beginning of contig.
if($start < $_->{'start'} and $stop > ($_->{'start'} + $max_overlap)) {
# print "----> Overlaps beg: existing beg,end: $_->{'start'},$_->{'stop'}, new beg,end: $start,$stop\n";<STDIN>;
# Collect stats over the non-overlapping region.
eval {
($numID, $numCons) = $hsp->matches(-SEQ =>$seqType,
-START =>$start,
-STOP =>$_->{'start'}-1);
};
if($@) { warn "\a\n$@\n"; }
else {
$_->{'start'} = $start; # Assign a new start coordinate to the contig
$_->{'iden'} += $numID; # and add new data to #identical, #conserved.
$_->{'cons'} += $numCons;
$overlap = 1;
}
}
## Test for overlap at end of contig.
if($stop > $_->{'stop'} and $start < ($_->{'stop'} - $max_overlap)) {
# print "----> Overlaps end: existing beg,end: $_->{'start'},$_->{'stop'}, new beg,end: $start,$stop\n";<STDIN>;
# Collect stats over the non-overlapping region.
eval {
($numID,$numCons) = $hsp->matches(-SEQ =>$seqType,
-START =>$_->{'stop'},
-STOP =>$stop);
};
if($@) { warn "\a\n$@\n"; }
else {
$_->{'stop'} = $stop; # Assign a new stop coordinate to the contig
$_->{'iden'} += $numID; # and add new data to #identical, #conserved.
$_->{'cons'} += $numCons;
$overlap = 1;
}
}
$overlap && do {
# print " New Contig data:\n";
# print " Contig: $_->{'start'} - $_->{'stop'}, iden= $_->{'iden'}, cons= $_->{'cons'}\n";
last;
};
}
## If there is no overlap, add the complete HSP data.
!$overlap && do {
# print "No overlap. Adding new contig.\n";
($numID,$numCons) = $hsp->matches(-SEQ=>$seqType);
push @$contigs_ref, {'start'=>$start, 'stop'=>$stop,
'iden'=>$numID, 'cons'=>$numCons };
};
# <STDIN>;
$overlap;
}
=head2 ambiguous_aln
Usage : $ambig_code = $sbjct_object->ambiguous_aln();
Purpose : Sets/Gets ambiguity code data member.
Example : (see usage)
Returns : String = 'q', 's', 'qs', '-'
: 'q' = query sequence contains overlapping sub-sequences
: while sbjct does not.
: 's' = sbjct sequence contains overlapping sub-sequences
: while query does not.
: 'qs' = query and sbjct sequence contains overlapping sub-sequences
: relative to each other.
: '-' = query and sbjct sequence do not contains multiple domains
: relative to each other OR both contain the same distribution
: of similar domains.
Argument : n/a
Throws : n/a
Status : Experimental
See Also : L<_tile_hsps>(), L<HSP Tiling and Ambiguous Alignments>
=cut
#--------------------
sub ambiguous_aln {
#--------------------
my $self = shift;
if(@_) { $self->{'_ambiguous_aln'} = shift; }
$self->{'_ambiguous_aln'} || '-';
}
=head2 overlap
Usage : $blast_object->overlap( [integer] );
Purpose : Gets/Sets the allowable amount overlap between different HSP sequences.
Example : $blast_object->overlap(5);
: $overlap = $blast_object->overlap();
Returns : Integer.
Argument : integer.
Throws : n/a
Status : Experimental
Comments : Any two HSPs whose sequences overlap by less than or equal
: to the overlap() number of resides will be considered separate HSPs
: and will not get tiled by _adjust_contigs().
See Also : L<_adjust_contigs>(), L<BUGS>
=cut
#-------------
sub overlap {
#-------------
my $self = shift;
if(@_) { $self->{'_overlap'} = shift; }
defined $self->{'_overlap'} ? $self->{'_overlap'} : 0;
}
=head2 score
Usage : $sbjct_object->score();
Purpose : Gets the BLAST score of the best HSP for the current Blast hit.
Example : $score = $sbjct_object->score();
Returns : Integer
Argument : n/a
Throws : n/a
See Also : L<bits>()
=cut
#----------
sub score {
#----------