Bio/Align/ProteinStatistics.pm

filter: Statementstmt 86.2% 75 / 87 Branchbran 70.0% 21 / 30 Conditioncond 40.4% 19 / 47 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 91.6% 11 / 12 Podpod 100% 5 / 5 totaltotal 65.3% 134 / 205 CC 34 SCAR 31.0
CC
34
Cov
70%
_check_ambiguity_protein
51.1
distance
14.3
D_Kimura
9.0
CRAP 22.3
« Bio/TreeIO/NewickParser.pm ↑ Summary Bio/Range.pm »
1 #
2 # BioPerl module for Bio::Align::ProteinStatistics
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason-at-bioperl.org>
7 #
8 # Copyright Jason Stajich
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::Align::ProteinStatistics - Calculate Protein Alignment statistics (mostly distances)
17  
18 =head1 SYNOPSIS
19  
20 use Bio::Align::ProteinStatistics;
21 use Bio::AlignIO;
22 my $in = Bio::AlignIO->new(-format => 'fasta',
23 -file => 'pep-104.fasaln');
24 my $aln = $in->next_aln;
25  
26 my $pepstats = Bio::Align::ProteinStatistics->new();
27 $kimura = $protstats->distance(-align => $aln,
28 -method => 'Kimura');
29 print $kimura->print_matrix;
30  
31  
32 =head1 DESCRIPTION
33  
34 This object is for generating various statistics from a protein
35 alignment. Mostly it is where pairwise protein distances can be
36 calculated.
37  
38 =head1 REFERENCES
39  
40 D_Kimura - Kimura, M. 1983. The Neutral Theory of Molecular Evolution. CUP,
41 Cambridge.
42  
43 =head1 FEEDBACK
44  
45 =head2 Mailing Lists
46  
47 User feedback is an integral part of the evolution of this and other
48 Bioperl modules. Send your comments and suggestions preferably to
49 the Bioperl mailing list. Your participation is much appreciated.
50  
51 bioperl-l@bioperl.org - General discussion
52 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53  
54 =head2 Support
55  
56 Please direct usage questions or support issues to the mailing list:
57  
58 I<bioperl-l@bioperl.org>
59  
60 rather than to the module maintainer directly. Many experienced and
61 reponsive experts will be able look at the problem and quickly
62 address it. Please include a thorough description of the problem
63 with code and data examples if at all possible.
64  
65 =head2 Reporting Bugs
66  
67 Report bugs to the Bioperl bug tracking system to help us keep track
68 of the bugs and their resolution. Bug reports can be submitted via the
69 web:
70  
71 https://github.com/bioperl/bioperl-live/issues
72  
73 =head1 AUTHOR - Jason Stajich
74  
75 Email jason-at-bioperl.org
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the object methods.
80 Internal methods are usually preceded with a _
81  
82 =cut
83  
84  
85 # Let the code begin...
86  
87  
88 package Bio::Align::ProteinStatistics;
89 3 $Bio::Align::ProteinStatistics::VERSION = '1.7.8';
90 3 ▶use vars qw(%DistanceMethods $Precision $DefaultGapPenalty);
Subroutine BEGIN: called CC=1 SCAR=0.0
91 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 3 ▶use Bio::Align::PairwiseStatistics;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 3 ▶use Bio::Matrix::PhylipDist;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 3 %DistanceMethods = ('kimura|k' => 'Kimura',
97 );
98 3 $Precision = 5;
99 3 $DefaultGapPenalty = 0;
100  
101 3 ▶use base qw(Bio::Root::Root Bio::Align::StatisticsI);
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 =head2 new
104  
105 Title : new
106 Usage : my $obj = Bio::Align::ProteinStatistics->new();
107 Function: Builds a new Bio::Align::ProteinStatistics object
108 Returns : an instance of Bio::Align::ProteinStatistics
109 Args :
110  
111  
112 =cut
113  
114 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
115 2 my($class,@args) = @_;
116  
117 2 my $self = $class->SUPER::new(@args);
118 2 $self->pairwise_stats( Bio::Align::PairwiseStatistics->new());
119  
120 2 return $self;
121 }
122  
123 =head2 distance
124  
125 Title : distance
126 Usage : my $distance_mat = $stats->distance(-align => $aln,
127 -method => $method);
128 Function: Calculates a distance matrix for all pairwise distances of
129 sequences in an alignment.
130 Returns : L<Bio::Matrix::PhylipDist> object
131 Args : -align => Bio::Align::AlignI object
132 -method => String specifying specific distance method
133 (implementing class may assume a default)
134  
135 =cut
136  
137 2 ▶sub distance{
Subroutine distance: called CC=8 SCAR=26.6
Pod documented
138 2 my ($self,@args) = @_;
139 2 my ($aln,$method) = $self->_rearrange([qw(ALIGN METHOD)],@args);
140 2 ▶ if( ! defined $aln || ! ref ($aln) || ! $aln->isa('Bio::Align::AlignI') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $aln and ref $aln and $aln->isa("Bio::Align::AlignI")) 0 2 2
Condition33%2/6
defined $aln and ref $aln and $aln->isa("Bio::Align::AlignI")
!ll&&!rl&&r
002
defined $aln and ref $aln
!ll&&!rl&&r
002
MC/DC0%0/3
defined $aln and ref $aln and $aln->isa("Bio::Align::AlignI")
defined $aln ref $aln $aln->isa("Bio::Align::AlignI")
Truth table25%1/4
A and B and C
A= defined $alnB= ref $alnC= $aln->isa("Bio::Align::AlignI")
ABCresult
0XX0
10X0
1100
1111
141 0 $self->throw("Must supply a valid Bio::Align::AlignI for the -align parameter in distance");
142 }
143 2 ▶ $method ||= 'Kimura';
Condition50%1/2
$method ||= "Kimura"
l!l
20
MC/DC0%0/1
$method ||= "Kimura"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
144 2 foreach my $m ( keys %DistanceMethods ) {
145 2 ▶ if(defined $m && $method =~ /$m/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m and $method =~ /$m/i) 2 0 2
Condition33%1/3
defined $m and $method =~ /$m/i
!ll&&!rl&&r
002
MC/DC0%0/2
defined $m and $method =~ /$m/i
defined $m $method =~ /$m/i
Truth table33%1/3
A and B
A= defined $mB= $method =~ /$m/i
ABresult
0X0
100
111
146 2 my $mtd = "D_$DistanceMethods{$m}";
147 2 return $self->$mtd($aln);
148 }
149 }
150 0 $self->warn("Unrecognized distance method $method must be one of [".
151 join(',',$self->available_distance_methods())."]");
152 0 return;
153 }
154  
155 =head2 available_distance_methods
156  
157 Title : available_distance_methods
158 Usage : my @methods = $stats->available_distance_methods();
159 Function: Enumerates the possible distance methods
160 Returns : Array of strings
161 Args : none
162  
163  
164 =cut
165  
166 0 ▶sub available_distance_methods{
Subroutine available_distance_methods: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($self,@args) = @_;
168 0 return values %DistanceMethods;
169 }
170  
171 =head2 D - distance methods
172  
173  
174 =cut
175  
176  
177 =head2 D_Kimura
178  
179 Title : D_Kimura
180 Usage : my $matrix = $pepstats->D_Kimura($aln);
181 Function: Calculate Kimura protein distance (Kimura 1983) which
182 approximates PAM distance
183 D = -ln ( 1 - p - 0.2 * p^2 )
184 Returns : L<Bio::Matrix::PhylipDist>
185 Args : L<Bio::Align::AlignI>
186  
187  
188 =cut
189  
190 # Kimura, M. 1983. The Neutral Theory of Molecular Evolution. CUP, Cambridge.
191  
192 2 ▶sub D_Kimura{
Subroutine D_Kimura: called CC=9 SCAR=22.0
Pod documented
193 2 my ($self,$aln) = @_;
194 2 ▶ return 0 unless $self->_check_arg($aln);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_arg($aln) 0 2 2
195 # ambiguities ignored at this point
196 2 my (@seqs,@names,@values,%dist);
197 2 my $seqct = 0;
198 2 foreach my $seq ( $aln->each_seq) {
199 20 push @names, $seq->display_id;
200 20 push @seqs, uc $seq->seq();
201 20 $seqct++;
202 }
203 2 my $len = $aln->length;
204 2 my $precisionstr = "%.$Precision"."f";
205  
206 2 ▶ for( my $i = 0; $i < $seqct-1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $seqct - 1) 18 2 20
207 # (diagonals) distance is 0 for same sequence
208 18 $dist{$names[$i]}->{$names[$i]} = [$i,$i];
209 18 $values[$i][$i] = sprintf($precisionstr,0);
210 18 ▶ for( my $j = $i+1; $j < $seqct; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $seqct) 106 18 124
211 106 my ($scored,$match) = (0,0);
212 106 ▶ for( my $k=0; $k < $len; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $len) 42184 106 42290
213 42184 my $m1 = substr($seqs[$i],$k,1);
214 42184 my $m2 = substr($seqs[$j],$k,1);
215 42184 ▶ if( $m1 ne '-' && $m2 ne '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m1 ne "-" and $m2 ne "-") 31488 10696 42184
Condition100%3/3
$m1 ne "-" and $m2 ne "-"
!ll&&!rl&&r
6724397231488
MC/DC100%2/2
$m1 ne "-" and $m2 ne "-"
$m1 ne "-" $m2 ne "-"
Truth table100%3/3
A and B
A= $m1 ne "-"B= $m2 ne "-"
ABresult
0X0
100
111
216 # score is number of scored bases (alignable bases)
217 # it could have also come from
218 # my $L = $self->pairwise_stats->number_of_comparable_bases($pairwise);
219 # match is number of matches weighting ambiguity bases
220 # as well
221 31488 $match += _check_ambiguity_protein($m1,$m2);
222 31488 $scored++;
223 }
224 }
225 # From Felsenstein's PHYLIP documentation:
226 # This is very quick to do but has some obvious
227 # limitations. It does not take into account which amino
228 # acids differ or to what amino acids they change, so some
229 # information is lost. The units of the distance measure
230 # are fraction of amino acids differing, as also in the
231 # case of the PAM distance. If the fraction of amino acids
232 # differing gets larger than 0.8541 the distance becomes
233 # infinite.
234  
235 106 my $D = 1 - ( $match / $scored );
236 106 ▶ if( $D < 0.8541 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($D < 0.8541) { } 97 9 106
237 97 $D = - log ( 1 - $D - (0.2 * ($D ** 2)));
238 97 $values[$j][$i] = $values[$i][$j] = sprintf($precisionstr,$D);
239 } else {
240 9 $values[$j][$i] = $values[$i][$j] = ' NaN';
241 }
242 # fwd and rev lookup
243 106 $dist{$names[$i]}->{$names[$j]} = [$i,$j];
244 106 $dist{$names[$j]}->{$names[$i]} = [$i,$j];
245  
246 # (diagonals) distance is 0 for same sequence
247 106 $dist{$names[$j]}->{$names[$j]} = [$j,$j];
248 106 $values[$j][$j] = sprintf($precisionstr,0);
249  
250 }
251 }
252 2 return Bio::Matrix::PhylipDist->new(-program => 'bioperl_PEPstats',
253 -matrix => \%dist,
254 -names => \@names,
255 -values => \@values);
256
257 }
258  
259 # some methods from EMBOSS distmat
260 sub _check_ambiguity_protein
261 31488 ▶{
Subroutine _check_ambiguity_protein: called CC=15 SCAR=39.3
262 31488 my ($t1,$t2) = @_;
263 31488 my $n = 0;
264  
265 31488 ▶ if( $t1 ne 'X' && $t1 eq $t2 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($t1 ne 'X' and $t1 eq $t2) { } 18672 12816 31488
elsif ($t1 eq 'B' and $t2 =~ /[DN]/ or $t2 eq 'B' and $t1 =~ /[DN]/ or ($t1 eq 'Z' and $t2 =~ /[EQ]/ or $t2 eq 'Z' and $t1 =~ /[EQ]/)) { } 0 12816 12816
elsif ($t1 eq 'X' and $t2 eq 'X') { } 0 12816 12816
elsif ($t1 eq 'X' or $t2 eq 'X') { } 0 12816 12816
Condition66%2/3
$t1 ne "X" and $t1 eq $t2
!ll&&!rl&&r
01281618672
MC/DC50%1/2
$t1 ne "X" and $t1 eq $t2
$t1 ne "X" $t1 eq $t2
Truth table66%2/3
A and B
A= $t1 ne "X"B= $t1 eq $t2
ABresult
0X0
100
111
266 18672 $n = 1.0;
267 ▶ } elsif( (($t1 eq 'B' && $t2 =~ /[DN]/ ) ||
Condition33%7/21
$t1 eq "B" and $t2 =~ /[DN]/ or $t2 eq "B" and $t1 =~ /[DN]/ or ($t1 eq "Z" and $t2 =~ /[EQ]/ or $t2 eq "Z" and $t1 =~ /[EQ]/)
l!l&&r!l&&!r
0012816
$t1 eq "B" and $t2 =~ /[DN]/ or $t2 eq "B" and $t1 =~ /[DN]/
l!l&&r!l&&!r
0012816
$t1 eq "B" and $t2 =~ /[DN]/
!ll&&!rl&&r
1281600
$t2 eq "B" and $t1 =~ /[DN]/
!ll&&!rl&&r
1281600
$t1 eq "Z" and $t2 =~ /[EQ]/ or $t2 eq "Z" and $t1 =~ /[EQ]/
l!l&&r!l&&!r
0012816
$t1 eq "Z" and $t2 =~ /[EQ]/
!ll&&!rl&&r
1281600
$t2 eq "Z" and $t1 =~ /[EQ]/
!ll&&!rl&&r
1281600
MC/DC0%0/8
$t1 eq "B" and $t2 =~ /[DN]/ or $t2 eq "B" and $t1 =~ /[DN]/ or ($t1 eq "Z" and $t2 =~ /[EQ]/ or $t2 eq "Z" and $t1 =~ /[EQ]/)
$t1 eq "B" $t2 =~ /[DN]/ $t2 eq "B" $t1 =~ /[DN]/ $t1 eq "Z" $t2 =~ /[EQ]/ $t2 eq "Z" $t1 =~ /[EQ]/
Truth table3%1/31
A and B or C and D or E and F or G and H
A= $t1 eq "B"B= $t2 =~ /[DN]/C= $t2 eq "B"D= $t1 =~ /[DN]/E= $t1 eq "Z"F= $t2 =~ /[EQ]/G= $t2 eq "Z"H= $t1 =~ /[EQ]/
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X0X0X111
0X0X10111
0X1011XX1
0X100X111
0X1010111
100X11XX1
100X0X111
100X10111
101011XX1
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
268 ($t2 eq 'B' && $t1 =~ /[DN]/ )) ||
269
270 (($t1 eq 'Z' && $t2 =~ /[EQ]/) ||
271 ($t2 eq 'Z' && $t1 =~ /[EQ]/ ))) {
272 0 $n = 0.5;
273 ▶ } elsif ( $t1 eq 'X' && $t2 eq 'X' ) {
Condition33%1/3
$t1 eq "X" and $t2 eq "X"
!ll&&!rl&&r
1281600
MC/DC0%0/2
$t1 eq "X" and $t2 eq "X"
$t1 eq "X" $t2 eq "X"
Truth table33%1/3
A and B
A= $t1 eq "X"B= $t2 eq "X"
ABresult
0X0
100
111
274 0 $n = 0.0025;
275 ▶ } elsif( $t1 eq 'X' || $t2 eq 'X' ) {
Condition33%1/3
$t1 eq "X" or $t2 eq "X"
l!l&&r!l&&!r
0012816
MC/DC0%0/2
$t1 eq "X" or $t2 eq "X"
$t1 eq "X" $t2 eq "X"
Truth table33%1/3
A or B
A= $t1 eq "X"B= $t2 eq "X"
ABresult
1X1
011
000
276 0 $n = 0.05;
277 }
278 31488 return $n;
279 }
280  
281 =head2 Data Methods
282  
283 =cut
284  
285 =head2 pairwise_stats
286  
287 Title : pairwise_stats
288 Usage : $obj->pairwise_stats($newval)
289 Function:
290 Returns : value of pairwise_stats
291 Args : newvalue (optional)
292  
293  
294 =cut
295  
296 2 ▶sub pairwise_stats{
Subroutine pairwise_stats: called CC=2 SCAR=7.0
Pod documented
297 2 my ($self,$value) = @_;
298 2 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 0 2
299 2 $self->{'_pairwise_stats'} = $value;
300 }
301 2 return $self->{'_pairwise_stats'};
302  
303 }
304  
305 2 ▶sub _check_arg {
Subroutine _check_arg: called CC=4 SCAR=19.4
306 2 my($self,$aln ) = @_;
307 2 ▶ if( ! defined $aln || ! $aln->isa('Bio::Align::AlignI') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $aln && $aln->isa('Bio::Align::AlignI')) { } 0 2 2
elsif ($aln->get_seq_by_pos(1)->alphabet ne 'protein') { } 0 2 2
Condition33%1/3
defined $aln && $aln->isa('Bio::Align::AlignI')
!ll&&!rl&&r
002
MC/DC0%0/2
defined $aln && $aln->isa('Bio::Align::AlignI')
defined $aln $aln->isa('Bio::Align::AlignI')
Truth table33%1/3
A && B
A= defined $alnB= $aln->isa('Bio::Align::AlignI')
ABresult
0X0
100
111
308 0 $self->warn("Must provide a Bio::Align::AlignI compliant object to Bio::Align::DNAStatistics");
309 0 return 0;
310 } elsif( $aln->get_seq_by_pos(1)->alphabet ne 'protein' ) {
311 0 $self->warn("Must provide a protein alignment to Bio::Align::ProteinStatistics, you provided a " . $aln->get_seq_by_pos(1)->alphabet);
312 0 return 0;
313 }
314 2 return 1;
315 }
316  
317 3 1;
« Bio/TreeIO/NewickParser.pm ↑ Summary Bio/Range.pm »