blib/lib/Bio/Community/Meta/Beta.pm

filter: Statementstmt 96.2% 256 / 266 Branchbran 60.0% 66 / 110 Conditioncond 86.6% 13 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 97.5% 40 / 41 Podpod n/a totaltotal 86.4% 382 / 442 CC 70 SCAR 16.6
CC
70
Cov
86%
_unifrac
14.4
_min_permuted
10.1
_jaccard
9.3
CRAP 5.3
« blib/lib/Bio/Community/TaxonomyUtils.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/qiime.pm »
1 # BioPerl module for Bio::Community::Meta::Beta
2 #
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
4 #
5 # Copyright 2011-2014 Florent Angly <florent.angly@gmail.com>
6 #
7 # You may distribute this module under the same terms as perl itself
8  
9  
10 =head1 NAME
11  
12 Bio::Community::Meta::Beta - Beta diversity or distance separating communities
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::Meta;
17 use Bio::Community::Meta::Beta;
18  
19 # Beta diversity of two communities
20 my $beta = Bio::Community::Meta::Beta->new(
21 -metacommunity => Bio::Community::Meta->new(-communities => [$community1, $community2] ),
22 -type => 'euclidean',
23 );
24 my $value = $beta->get_beta;
25  
26 # Beta diversity between all pairs of communities in the given metacommunity
27 $beta = Bio::Community::Meta::Beta->new(
28 -metacommunity => $meta,
29 -type => 'hellinger',
30 );
31 my ($average_value, $value_hashref) = $beta->get_all_beta;
32  
33 =head1 DESCRIPTION
34  
35 The Bio::Community::Beta module quantifies how dissimilar communities are
36 by calculating their beta diversity. The more different communities are, the
37 larger their beta diversity. Some beta diversity metrics are proper distance
38 measures (in the mathematical sense).
39  
40 Since the relative abundance of community members is not always proportional to
41 member counts (see weights() in Bio::Community::Member and use_weights() in
42 Bio::Community), the beta diversity measured here are always based on relative
43 abundance (as a fractional number between 0 and 1, not as a percentage), even
44 for beta diversity metrics that are usually based on number of observations
45 (counts).
46  
47 =head1 METRICS
48  
49 Qualitative and quantitive measures of beta diversity are available and can be
50 specified with the C<type()> method:
51  
52 =head2 Qualitative
53  
54 Qualitative metrics are based on the presence or absence of community members
55 only.
56  
57 =over
58  
59 =item jaccard
60  
61 The Jaccard distance (between 0 and 1), i.e. the fraction of non-shared species
62 relative to the overall richness of the metacommunity.
63  
64 =item sorensen
65  
66 The Sørensen dissimilarity, or Whittaker's species turnover (between 0 and 1),
67 i.e. the fraction of non-shared species relative to the average richness in the
68 metacommunity.
69  
70 =item shared
71  
72 The percentage of species shared (between 0 and 100), relative to the least
73 rich community. Note: this is the opposite of a beta diversity measure since
74 the higher the percent of species shared, the smaller the beta diversity.
75  
76 =back
77  
78 =head2 Quantitative
79  
80 =over
81  
82 =item 1-norm
83  
84 The 1-norm, or Manhattan distance, i.e. the sum of difference in abundance for all species.
85  
86 =item 2-norm (euclidean)
87  
88 The 2-norm, or euclidean distance.
89  
90 =item infinity-norm
91  
92 The infinity-norm, i.e. the maximum difference in abundance over all species.
93  
94 =item hellinger
95  
96 Like the euclidean distance, but constrained between 0 and 1.
97  
98 =item bray-curtis
99  
100 The Bray-Curtis dissimilarity (or Sørensen quantitative index), which varies
101 between 0 and 1.
102  
103 =item morisita-horn
104  
105 The Morisita-Horn dissimilarity, which varies between 0 and 1. Affected
106 strongly by the abundance of the most abundant species, but not by sample size
107 or richness.
108  
109 =item permuted
110  
111 A beta diversity measure between 0 and 100, representing the percentage of the
112 dominant species in the first community with a permuted abundance rank in the
113 second community. As a special case, when no species are shared (and the
114 percentage permuted is meaningless), undef is returned.
115  
116 =item maxiphi
117  
118 A beta diversity measure between 0 and 1, based on the percentage of species
119 shared and the percentage of top species permuted (that have had a change in
120 abundance rank).
121  
122 =back
123  
124 =head1 AUTHOR
125  
126 Florent Angly L<florent.angly@gmail.com>
127  
128 =head1 SUPPORT AND BUGS
129  
130 User feedback is an integral part of the evolution of this and other Bioperl
131 modules. Please direct usage questions or support issues to the mailing list,
132 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
133 experienced and reponsive experts will be able look at the problem and quickly
134 address it. Please include a thorough description of the problem with code and
135 data examples if at all possible.
136  
137 If you have found a bug, please report it on the BioPerl bug tracking system
138 to help us keep track the bugs and their resolution:
139 L<https://redmine.open-bio.org/projects/bioperl/>
140  
141 =head1 COPYRIGHT
142  
143 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
144  
145 This library is free software; you can redistribute it and/or modify
146 it under the same terms as Perl itself, either Perl version 5.10.1 or,
147 at your option, any later version of Perl 5 you may have available.
148  
149 =head1 APPENDIX
150  
151 The rest of the documentation details each of the object
152 methods. Internal methods are usually preceded with a _
153  
154 =head2 new
155  
156 Function: Create a new Bio::Community::Meta::Beta object
157 Usage : my $beta = Bio::Community::Meta::Beta->new(
158 -metacommunity => $meta,
159 -type => 'euclidean',
160 );
161 Args : -metacommunity : See metacommunity(). This is required!
162 -type : See type().
163 Returns : a Bio::Community::Meta::Beta object
164  
165 =cut
166  
167  
168 package Bio::Community::Meta::Beta;
169  
170 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 3 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 3 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 3 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 3 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
176 3 ▶use Bio::Community::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 3 extends 'Bio::Root::Root';
179  
180  
181 =head2 metacommunity
182  
183 Function: Get or set the communities to process, given as a metacommunity.
184 Usage : my $meta = $beta->metacommunity;
185 Args : A Bio::Community::Meta object
186 Returns : A Bio::Community::Meta object
187  
188 =cut
189  
190 3 has metacommunity => (
191 is => 'ro',
192 isa => 'Bio::Community::Meta',
193 required => 1,
194 lazy => 0,
195 init_arg => '-metacommunity',
196 );
197  
198  
199 =head2 type
200  
201 Function: Get or set the beta diversity metric to calculate.
202 Usage : my $type = $beta->type;
203 Args : String for the desired type of beta diversity ('2-norm' by default).
204 See L</METRICS> for details.
205 Returns : String for the desired type of beta diversity
206  
207 =cut
208  
209 3 has type => (
210 is => 'rw',
211 isa => 'DistanceType',
212 required => 0,
213 lazy => 1,
214 default => '2-norm',
215 init_arg => '-type',
216 );
217  
218  
219 =head2 get_beta
220  
221 Function: Calculate the beta diversity between two communities. The input
222 metacommunity should contain exactly two communities. The distance is
223 calculated based on the relative abundance (in %) of the members (not
224 their counts).
225 Usage : my $value = $beta->get_beta;
226 Args : None
227 Returns : A number for the beta diversity value
228  
229 =cut
230  
231 ####after new => sub { # prevents inlining
232 #### or maybe try BUILD
233 3 ▶method get_beta () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_beta: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 113 113
234 113 return $self->_get_pairwise_beta($self->metacommunity);
235 };
236  
237  
238 3 ▶method _get_pairwise_beta ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _get_pairwise_beta: called CC=4 SCAR=14.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 116 116
if scalar @_ > 1 0 116 116
239 116 my $num_comm = $meta->get_communities_count;
240 116 ▶ if ($num_comm != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_comm != 2) 0 116 116
241 # Die because extra communities will affect $meta->get_all_members()
242 0 $self->throw("Cannot calculate pairwise beta diversity because the ".
243 "metacommunity contains $num_comm communities. Expected exactly two.");
244 }
245 116 my $metric = '_'.$self->type;
246 116 $metric =~ s/-/_/g;
247 116 return $self->$metric($meta);
248 };
249  
250  
251 =head2 get_all_beta
252  
253 Function: Similar to get_beta(), but return the beta diversity between all pairs
254 of communities in the given metacommunity and also return their
255 average beta diversity.
256 Usage : my ($average, $betas) = $beta->get_all_beta;
257 Args : None
258 Returns : * A number for the average beta diversity
259 * A hashref of hashref with the value of all pairwise beta diversities,
260 keyed by the community names. To get the beta diversity of a specific
261 pair of communities, do:
262 my $value = $betas->{$name1}->{$name2};
263 or:
264 my $value = $betas->{$name2}->{$name1};
265  
266 =cut
267  
268 3 ▶method get_all_beta () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_all_beta: called CC=6 SCAR=18.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
269 1 my $communities = $self->metacommunity->get_all_communities;
270 1 my $num_communities = scalar @$communities;
271 1 my $average = 0;
272 1 my $num_pairs = 0;
273 1 my $betas;
274 1 for my $i (0 .. $num_communities - 1) {
275 3 my $community1 = $communities->[$i];
276 3 my $name1 = $community1->name;
277 3 for my $j ($i + 1 .. $num_communities -1) {
278 3 my $community2 = $communities->[$j];
279 3 my $name2 = $community2->name;
280 3 my $meta = Bio::Community::Meta->new(-communities => [$community1, $community2]);
281 3 my $val = $self->_get_pairwise_beta($meta);
282 3 $num_pairs++;
283 3 $average += $val;
284 3 ▶ if (exists $betas->{$name1}->{$name2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $betas->{$name1}{$name2}) { } 0 3 3
285 0 $self->throw("There are several communities called '$name2'.");
286 } else {
287 3 $betas->{$name1}->{$name2} = $betas->{$name2}->{$name1} = $val;
288 }
289 }
290 }
291 1 ▶ $average = ($num_pairs > 0) ? ($average / $num_pairs) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$num_pairs > 0 ? : 1 0 1
292 1 return $average, $betas;
293 }
294  
295  
296 3 ▶method _p_norm ($meta, $power) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _p_norm: called CC=5 SCAR=16.2
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 35 35
unless scalar @_ > 1 0 35 35
if scalar @_ > 2 0 35 35
297 # Calculate the p-norm. If power is 1, this is the 1-norm. If power is 2,
298 # this is the 2-norm (a.k.a. euclidean distance).
299 35 my ($community1, $community2) = @{$meta->get_all_communities};
300 35 my $sumdiff = 0;
301 35 for my $member (@{$meta->get_all_members}) {
302 123 my $abundance1 = $community1->get_rel_ab($member) / 100;
303 123 my $abundance2 = $community2->get_rel_ab($member) / 100;
304 123 $sumdiff += ( abs($abundance1 - $abundance2) )**$power;
305 }
306 35 my $val = $sumdiff ** (1/$power);
307 35 return $val;
308 }
309  
310  
311 3 ▶method _1_norm ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _1_norm: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
312 # Calculate the 1-norm
313 10 return $self->_p_norm($meta, 1);
314 }
315  
316  
317 3 ▶method _2_norm ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _2_norm: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 12 12
if scalar @_ > 1 0 12 12
318 # Calculate the 2-norm
319 12 return $self->_p_norm($meta, 2);
320 }
321  
322  
323 # The euclidean distance is the same as the 2-norm
324 3 *_euclidean = \&_2_norm;
325  
326  
327 3 ▶method _infinity_norm ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _infinity_norm: called CC=5 SCAR=16.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
328 # Calculate the infinity-norm.
329 10 my ($community1, $community2) = @{$meta->get_all_communities};
330 10 my $val = 0;
331 10 for my $member (@{$meta->get_all_members}) {
332 38 my $abundance1 = $community1->get_rel_ab($member) / 100;
333 38 my $abundance2 = $community2->get_rel_ab($member) / 100;
334 38 my $diff = abs($abundance1 - $abundance2);
335 38 ▶ if ($diff > $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff > $val) 17 21 38
336 17 $val = $diff;
337 }
338 }
339 10 return $val;
340 }
341  
342  
343 3 ▶method _hellinger ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _hellinger: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 13 13
if scalar @_ > 1 0 13 13
344 # Calculate the Hellinger distance.
345 13 return $self->_p_norm($meta, 2) / sqrt(2);
346 }
347  
348  
349 3 ▶method _bray_curtis ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _bray_curtis: called CC=6 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
350 # Calculate the Bray-Curtis dissimilarity index BC:
351 # BC = 1 - sum( min(r_i, r_j) )
352 # where r_i and r_j are the relative abundance (fractional) for species in
353 # common between both sites.
354 # Can also be written as:
355 # BC = sum( c_i - c_j ) / sum( c_i + c_j )
356 # where c_i and c_j are the counts for all observed species.
357 10 my ($community1, $community2) = @{$meta->get_all_communities};
358 10 my $sumdiff = 0;
359 10 for my $member (@{$meta->get_all_members}) {
360 38 my $abundance1 = $community1->get_rel_ab($member) / 100;
361 38 ▶ next if $abundance1 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $abundance1 == 0 10 28 38
362 28 my $abundance2 = $community2->get_rel_ab($member) / 100;
363 28 ▶ next if $abundance2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $abundance2 == 0 7 21 28
364 21 $sumdiff += min($abundance1, $abundance2);
365 }
366 10 return 1 - $sumdiff;
367 }
368  
369  
370 3 ▶method _morisita_horn ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _morisita_horn: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
371 # Calculate the Morisita-Horn dissimilarity MH:
372 # MH = 1- Cmh
373 # where:
374 # CmH = 1 - 2 sum(ani * bni) / [(da + db)(aN)(bN)]
375 # aN = total # of indiv in site A
376 # ani = # of individuals in ith species in site A
377 # da = sum(ani^2) / aN^2
378 10 my ($community1, $community2) = @{$meta->get_all_communities};
379 10 my ($aN, $bN) = (1, 1);
380 10 my ($sumprod, $da, $db) = (0, 0);
381 10 for my $member (@{$meta->get_all_members}) {
382 38 my $ani = $community1->get_rel_ab($member) / 100;
383 38 my $bni = $community2->get_rel_ab($member) / 100;
384 38 $sumprod += $ani * $bni;
385 38 $da += $ani**2;
386 38 $db += $bni**2;
387 }
388 #$da /= $aN;
389 #$db /= $bN;
390 10 return 1 - 2 * $sumprod / (($da + $db) * $aN * $bN);
391 }
392  
393  
394 3 ▶method _jaccard ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _jaccard: called CC=9 SCAR=22.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
395 # Calculate the Jaccard distance dJ:
396 # dJ = 1 - (#spp in common / total richness)
397 10 my ($community1, $community2) = @{$meta->get_all_communities};
398 10 my ($num_shared, $num_total) = (0, 0);
399 10 for my $member (@{$meta->get_all_members}) {
400 38 my $ab1 = $community1->get_rel_ab($member);
401 38 my $ab2 = $community2->get_rel_ab($member);
402 38 ▶ if ( ($ab1 > 0) || ($ab2 > 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ab1 > 0 or $ab2 > 0) 38 0 38
Condition66%2/3
$ab1 > 0 or $ab2 > 0
l!l&&r!l&&!r
28100
MC/DC0%0/2
$ab1 > 0 or $ab2 > 0
$ab1 > 0 $ab2 > 0
Truth table66%2/3
A or B
A= $ab1 > 0B= $ab2 > 0
ABresult
1X1
011
000
403 38 $num_total++;
404 38 ▶ if ( ($ab1 > 0) && ($ab2 > 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ab1 > 0 and $ab2 > 0) 21 17 38
Condition100%3/3
$ab1 > 0 and $ab2 > 0
!ll&&!rl&&r
10721
MC/DC100%2/2
$ab1 > 0 and $ab2 > 0
$ab1 > 0 $ab2 > 0
Truth table100%3/3
A and B
A= $ab1 > 0B= $ab2 > 0
ABresult
0X0
100
111
405 21 $num_shared++;
406 }
407 }
408 }
409 10 ▶ return ($num_total > 0) ? (1 - $num_shared / $num_total) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$num_total > 0 ? : 10 0 10
410 }
411  
412  
413 3 ▶method _sorensen ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _sorensen: called CC=7 SCAR=19.6
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
414 # Calculate the Sørensen dissimilarity dS:
415 # dS = 1 - (#spp in common / average richness)
416 # = 1 - 2* #spp in common / (richness A + richness B)
417 10 my ($community1, $community2) = @{$meta->get_all_communities};
418 10 my $num_shared = 0;
419 10 for my $member (@{$meta->get_all_members}) {
420 38 ▶ if ( ($community1->get_rel_ab($member) > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($community1->get_rel_ab($member) > 0 and $community2->get_rel_ab($member) > 0) 21 17 38
Condition100%3/3
$community1->get_rel_ab($member) > 0 and $community2->get_rel_ab($member) > 0
!ll&&!rl&&r
10721
MC/DC100%2/2
$community1->get_rel_ab($member) > 0 and $community2->get_rel_ab($member) > 0
$community1->get_rel_ab($member) > 0 $community2->get_rel_ab($member) > 0
Truth table100%3/3
A and B
A= $community1->get_rel_ab($member) > 0B= $community2->get_rel_ab($member) > 0
ABresult
0X0
100
111
421 ($community2->get_rel_ab($member) > 0) ) {
422 21 $num_shared++;
423 }
424 }
425 10 my $richness_sum = $community1->get_richness + $community2->get_richness;
426 10 ▶ return ($richness_sum > 0) ? 1 - (2 * $num_shared / $richness_sum) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$richness_sum > 0 ? : 10 0 10
427 }
428  
429  
430 3 ▶method _shared ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _shared: called CC=6 SCAR=18.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 21 21
if scalar @_ > 1 0 21 21
431 # Percentage of species in common between two communities, relative to the
432 # least rich community.
433 21 my ($community1, $community2) = @{$meta->get_all_communities};
434 21 my $num_shared = 0;
435 21 for my $member (@{$meta->get_all_members}) {
436 77 ▶ if ( ($community1->get_rel_ab($member) > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($community1->get_rel_ab($member) > 0 and $community2->get_rel_ab($member) > 0) 43 34 77
Condition100%3/3
$community1->get_rel_ab($member) > 0 and $community2->get_rel_ab($member) > 0
!ll&&!rl&&r
201443
MC/DC100%2/2
$community1->get_rel_ab($member) > 0 and $community2->get_rel_ab($member) > 0
$community1->get_rel_ab($member) > 0 $community2->get_rel_ab($member) > 0
Truth table100%3/3
A and B
A= $community1->get_rel_ab($member) > 0B= $community2->get_rel_ab($member) > 0
ABresult
0X0
100
111
437 ($community2->get_rel_ab($member) > 0) ) {
438 43 $num_shared++;
439 }
440 }
441 21 return $num_shared / min($community1->get_richness,$community2->get_richness) * 100;
442 }
443  
444  
445 3 ▶method _permuted ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _permuted: called CC=5 SCAR=16.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 20 20
if scalar @_ > 1 0 20 20
446 # Percent of top species with a permuted rank-abundance between 2 communities.
447 # The exact number cannot be calculated for certain because the random
448 # permutation of x species could generate the same sequence (but it would be
449 # extremely unlikely). The best we can do is calculate a minimum bound for
450 # the number of species permuted. Do this once for the species of community1
451 # once, and then for the members of community2 and return the average of the
452 # two.This should be a reasonable approximation of the true percent of
453 # species permuted.
454  
455  
456 #### should it really be the average or simply relative to the least rich community
457  
458 20 my $min_p1 = $self->_min_permuted($meta);
459 20 my $min_p2 = $self->_min_permuted($meta);
460 20 my $p;
461 20 ▶ if ( (defined $min_p1) && defined($min_p2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_p1 and defined $min_p2) 16 4 20
Condition66%2/3
defined $min_p1 and defined $min_p2
!ll&&!rl&&r
4016
MC/DC50%1/2
defined $min_p1 and defined $min_p2
defined $min_p1 defined $min_p2
Truth table66%2/3
A and B
A= defined $min_p1B= defined $min_p2
ABresult
0X0
100
111
462 16 $p = ($min_p1 + $min_p2) / 2;
463 }
464  
465 20 return $p;
466 }
467  
468  
469 3 ▶method _min_permuted ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _min_permuted: called CC=10 SCAR=23.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 40 40
if scalar @_ > 1 0 40 40
470 # Estimate the minimum percent of permuted species in community1. Do this by
471 # going through members of community2 in increasing abundance rank order and
472 # comparing their position to that of the same member in community1 (if
473 # shared). If there are no species shared, return undef.
474  
475 40 my $min_permuted;
476  
477 40 my ($community1, $community2) = @{$meta->get_all_communities};
478  
479 40 my $i = 0;
480 40 my $richness = $community2->get_richness;
481 40 ▶ while ($i < $richness) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $richness) 84 8 92
482 84 $i++;
483 84 my $member = $community2->get_member_by_rank($i);
484  
485 # Skip this member if it is not shared
486 84 ▶ next if not $community1->get_rel_ab($member);
Branch: 1 branch
BranchTrueFalseTotal
unless $community1->get_rel_ab($member) 24 60 84
487  
488 60 my $j = $community1->get_rank($member);
489  
490 # Record the first mapping as the min permuted
491 60 ▶ if (not defined $min_permuted) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min_permuted) 32 28 60
492 32 $min_permuted = $j;
493 }
494  
495 60 ▶ if ($j > $min_permuted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > $min_permuted) 0 60 60
496 # Member rank in second community conflicts with minimum permutation
497 # assumption. Increase minimum permutation.
498 0 $min_permuted = $j;
499 }
500  
501 # Finish if the permutation limit has been reached without conflicts
502 60 ▶ last if $i >= $min_permuted;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $min_permuted 32 28 60
503  
504 }
505  
506 40 ▶ if (defined $min_permuted) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_permuted) 32 8 40
507 32 ▶ if ($min_permuted == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_permuted == 1) 16 16 32
508 16 $min_permuted--;
509 }
510 32 $min_permuted = $min_permuted / $community1->get_richness * 100;
511 }
512  
513 40 return $min_permuted;
514 }
515  
516  
517 3 ▶method _maxiphi ($meta) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _maxiphi: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 10 10
if scalar @_ > 1 0 10 10
518 # Given S, the fraction shared, and P, the fraction permuted, calculate the
519 # MaxiPhi beta diversity M as:
520 # M = 1 - S*(2-P)/2
521 #
522 # M ranges from 0 (low beta diversity, similar communities), to 1 (high beta
523 # diversity, dissimilar communities). The weight of the percent permuted
524 # parameter is proportional to the percent shared. At 0% shared, the fraction
525 # permuted has no weight in the index, while at 100% shared, the fraction
526 # permuted and fraction shared have the same weight.
527 #
528 # For example:
529 # for 100 % shared, 0 % permuted -> M = 0
530 # for 100 % shared, 100 % permuted -> M = 0.5
531 # for 0 % shared, 0 % permuted -> M = 1
532 # for 0 % shared, 100 % permuted -> M = 1
533  
534  
535 # Calculate the fraction shared
536 10 my $s = $self->_shared($meta) / 100;
537  
538 # Calculate the fraction permuted
539 10 my $p = $self->_permuted($meta);
540 10 ▶ if (not defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 2 8 10
541 2 $p = 100; # but any value between 0 and 100 would work as well
542 }
543 10 $p /= 100;
544  
545 # Calculate the Maxiphi index
546 10 my $m = 1 - $s * (2-$p) / 2;
547 10 return $m;
548 }
549  
550  
551 3 ▶method _unifrac ($meta, $tree) {
Subroutine BEGIN: called CC=1 SCAR=4.9
Subroutine _unifrac: not called CC=4 SCAR=26.7
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
552 #### TODO: unifrac
553 0 $self->throw_not_implemented;
554 }
555  
556  
557 #######
558 # TODO:
559 # Many more beta diversity indices to calculate:
560 # Unifrac
561 # ...
562 #######
563  
564  
565 3 __PACKAGE__->meta->make_immutable;
566  
567 3 1;
« blib/lib/Bio/Community/TaxonomyUtils.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/qiime.pm »