blib/lib/Bio/Community/Tools/Rarefier.pm

filter: Statementstmt 25.7% 63 / 245 Branchbran 3.5% 4 / 112 Conditioncond 3.8% 1 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 60.7% 17 / 28 Podpod n/a totaltotal 19.8% 85 / 428 CC 70 SCAR 52.7
CC
70
Cov
18%
_count_normalize
401.9
_bootstrap
358.1
_calc_repr
141.4
CRAP 195.3
↑ Summary blib/lib/Bio/Community/Tools/IdConverter.pm »
1 # BioPerl module for Bio::Community::Tools::Rarefier
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::Tools::Rarefier - Normalize communities by count
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::Tools::Rarefier;
17  
18 # Normalize communities in a metacommunity by repeatedly taking 1,000 random members
19 my $rarefier = Bio::Community::Tools::Rarefier->new(
20 -metacommunity => $meta,
21 -sample_size => 1000,
22 -threshold => 0.001, # stop bootstrap iterations when threshold is reached
23 );
24  
25 # Rarefied results, with decimal counts
26 my $average_community = $rarefier->get_avg_meta->[0];
27  
28 # Round counts to integer numbers
29 my $representative_community = $rarefier->get_repr_meta->[0];
30
31  
32 # Alternatively, specify a number of repetitions
33 my $rarefier = Bio::Community::Tools::Rarefier->new(
34 -metacommunity => $meta,
35 -sample_size => 1000,
36 -num_repetitions => 0.001, # stop after this number of bootstrap iterations
37 );
38  
39 # ... or assume an infinite number of repetitions
40 my $rarefier = Bio::Community::Tools::Rarefier->new(
41 -metacommunity => $meta,
42 -sample_size => 1000,
43 -num_repetitions => 'inf',
44 );
45  
46 =head1 DESCRIPTION
47  
48 This module takes a metacommunity and normalizes (rarefies) the communities it
49 contains by their number of counts.
50  
51 Comparing the composition and diversity of biological communities can be biased
52 by sampling artefacts. When comparing two identical communities, one for which
53 10,000 counts were made to one, to one with only 1,000 counts, the smaller
54 community will appear less diverse. A solution is to repeatedly bootstrap the
55 larger communities by taking 1,000 random members from it.
56  
57 This module uses L<Bio::Community::Sampler> to take random member from communities
58 and normalize them by their number of counts. After all random repetitions have
59 been performed, average communities or representative communities are returned.
60 These communities all have the same number of counts.
61  
62 =head1 AUTHOR
63  
64 Florent Angly L<florent.angly@gmail.com>
65  
66 =head1 SUPPORT AND BUGS
67  
68 User feedback is an integral part of the evolution of this and other Bioperl
69 modules. Please direct usage questions or support issues to the mailing list,
70 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
71 experienced and reponsive experts will be able look at the problem and quickly
72 address it. Please include a thorough description of the problem with code and
73 data examples if at all possible.
74  
75 If you have found a bug, please report it on the BioPerl bug tracking system
76 to help us keep track the bugs and their resolution:
77 L<https://redmine.open-bio.org/projects/bioperl/>
78  
79 =head1 COPYRIGHT
80  
81 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
82  
83 This library is free software; you can redistribute it and/or modify
84 it under the same terms as Perl itself, either Perl version 5.10.1 or,
85 at your option, any later version of Perl 5 you may have available.
86  
87 =head1 APPENDIX
88  
89 The rest of the documentation details each of the object
90 methods. Internal methods are usually preceded with a _
91  
92 =head2 new
93  
94 Function: Create a new Bio::Community::Tool::Rarefier object
95 Usage : my $rarefier = Bio::Community::Tool::Rarefier->new( );
96 Args : -metacommunity : see metacommunity()
97 -num_repetitions: see num_repetitions()
98 -sample_size : see sample_size()
99 -seed : see set_seed()
100 Returns : a new Bio::Community::Tools::Rarefier object
101  
102 =cut
103  
104  
105 package Bio::Community::Tools::Rarefier;
106  
107 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 2 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 2 ▶use Bio::Community::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶use Bio::Community::Meta::Beta;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 2 extends 'Bio::Root::Root';
118 2 with 'Bio::Community::Role::PRNG';
119  
120  
121 =head2 metacommunity
122  
123 Function: Get or set the metacommunity to normalize.
124 Usage : my $meta = $rarefier->metacommunity;
125 Args : A Bio::Community::Meta object
126 Returns : A Bio::Community::Meta object
127  
128 =cut
129  
130 has metacommunity => (
131 is => 'rw',
132 isa => 'Maybe[Bio::Community::Meta]',
133 required => 0,
134 default => undef,
135 lazy => 1,
136 init_arg => '-metacommunity',
137 0 ▶ trigger => sub { $_[0]->_clear_avg_meta; $_[0]->_clear_repr_meta },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 2 );
139  
140  
141 =head2 sample_size
142  
143 Function: Get or set the sample size, i.e. the number of members to pick randomly
144 at each iteration. It has to be smaller than the total count of the
145 smallest community or an error will be generated. If the sample size
146 is omitted, it defaults to the get_members_count() of the smallest community.
147 Usage : my $sample_size = $rarefier->sample_size;
148 Args : integer for the sample size
149 Returns : integer for the sample size
150  
151 =cut
152  
153 has sample_size => (
154 is => 'rw',
155 isa => 'Maybe[PositiveInt]',
156 required => 0,
157 default => undef,
158 lazy => 1,
159 init_arg => '-sample_size',
160 0 ▶ trigger => sub { $_[0]->_clear_avg_meta; $_[0]->_clear_repr_meta },
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 2 );
162  
163  
164 =head2 threshold
165  
166 Function: Get or set the threshold. While iterating, when the beta diversity or
167 distance between the average community and the average community at
168 the previous iteration decreases below this threshold, the
169 bootstrapping is stopped. By default, the threshold is 1e-5. The
170 num_repetitions() method provides an alternative way to specify when
171 to stop the computation. After communities have been normalized using
172 the num_repetitions() method instead of the threshold() method, the
173 beta diversity between the last two average communities repetitions
174 can be accessed using the threshold() method.
175 Usage : my $threshold = $rarefier->threshold;
176 Args : positive integer for the number of repetitions
177 Returns : positive integer for the (minimum) number of repetitions
178  
179 =cut
180  
181 has threshold => (
182 is => 'rw',
183 isa => 'Maybe[PositiveNum]',
184 required => 0,
185 default => 1E-5, # maybe impossible to reach lower thresholds for simplistic communities
186 lazy => 1,
187 init_arg => '-threshold',
188 0 ▶ trigger => sub { $_[0]->_clear_avg_meta; $_[0]->_clear_repr_meta },
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 2 );
190  
191  
192 =head2 num_repetitions
193  
194 Function: Get or set the number of bootstrap repetitions to perform. When given,
195 instead of relying on the threshold() to determine when to stop
196 repeating the bootstrap process, perform an arbitrary number of
197 repetitions. After communities have been normalized by count using
198 threshold() method, the number of repetitions actually done can be
199 accessed using this method. As a special case, specify 'inf' to
200 simulate an infinite number of repetitions.
201 Usage : my $repetitions = $rarefier->repetitions;
202 Args : positive integer or 'inf' number of repetitions
203 Returns : positive integer for the (minimum) number of repetitions
204  
205 =cut
206  
207 has num_repetitions => (
208 is => 'rw',
209 isa => 'Maybe[PositiveInt | Str]',
210 required => 0,
211 default => undef,
212 lazy => 1,
213 init_arg => '-num_repetitions',
214 trigger => sub {
215 0 ▶ $_[0]->_clear_avg_meta;
Subroutine __ANON__: not called CC=2 SCAR=17.9
216 0 $_[0]->_clear_repr_meta;
217 0 ▶ $_[0]->_clear_sampler if $_[0]->num_repetitions eq 'inf';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->num_repetitions eq "inf" 0 0 0
218 },
219 2 );
220  
221 2 *repetitions = \&num_repetitions;
222  
223  
224 =head2 get_seed, set_seed
225  
226 Usage : $sampler->set_seed(1234513451);
227 Function: Get or set the seed used to pick the random members.
228 Args : Positive integer
229 Returns : Positive integer
230  
231 =cut
232  
233  
234 =head2 verbose
235  
236 Function: Get or set verbose mode. In verbose mode, the current number of
237 iterations (and beta diversity if a threshold is used) is displayed.
238 Usage : $rarefier->verbose(1);
239 Args : 0 (default) or 1
240 Returns : 0 or 1
241  
242 =cut
243  
244 2 has verbose => (
245 is => 'rw',
246 isa => 'Bool',
247 required => 0,
248 default => 0,
249 lazy => 1,
250 init_arg => '-verbose',
251 );
252  
253  
254 =head2 drop
255  
256 Function: Get or set drop mode. In drop mode, this module silently drops
257 communities that do not have enough members instead of reporting an
258 error.
259 Usage : $rarefier->drop(1);
260 Args : 0 (default) or 1
261 Returns : 0 or 1
262  
263 =cut
264  
265 2 has drop => (
266 is => 'rw',
267 isa => 'Bool',
268 required => 0,
269 default => 0,
270 lazy => 1,
271 init_arg => '-drop',
272 );
273  
274  
275 =head2 get_avg_meta
276  
277 Function: Calculate an average metacommunity.
278 Usage : my $meta = $rarefier->get_avg_meta;
279 Args : none
280 Returns : Bio::Community::Meta object
281  
282 =cut
283  
284 has average_meta => (
285 is => 'rw',
286 isa => 'Maybe[Bio::Community::Meta]',
287 required => 0,
288 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 2 lazy => 1,
290 reader => 'get_avg_meta',
291 writer => '_set_avg_meta',
292 predicate => '_has_avg_meta',
293 clearer => '_clear_avg_meta',
294 );
295  
296 before get_avg_meta => sub {
297 1 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=8.1
298 1 ▶ $self->_count_normalize if not $self->_has_avg_meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_avg_meta 1 0 1
299 0 return 1;
300 2 };
301  
302  
303 =head2 get_repr_meta
304  
305 Function: Calculate a representative metacommunity.
306 Usage : my $meta = $rarefier->get_repr_meta;
307 Args : none
308 Returns : Bio::Community::Meta object
309  
310 =cut
311  
312 has representative_meta => (
313 is => 'rw',
314 isa => 'Bio::Community::Meta',
315 required => 0,
316 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
317 2 lazy => 1,
318 reader => 'get_repr_meta',
319 writer => '_set_repr_meta',
320 predicate => '_has_repr_meta',
321 clearer => '_clear_repr_meta',
322 );
323  
324 before get_repr_meta => sub {
325 1 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=3 SCAR=17.9
326 1 ▶ if (not $self->_has_repr_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_repr_meta) 1 0 1
327 1 my $average_meta = $self->get_avg_meta;
328 0 my $representative_meta = Bio::Community::Meta->new( -name => $average_meta->name );
329 0 ▶ while (my $average_community = $average_meta->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $average_community = $average_meta->next_community) 0 0 0
330 0 my $representative_community = $self->_calc_repr($average_community);
331 0 $representative_meta->add_communities([$representative_community]);
332 }
333 0 $self->_set_repr_meta($representative_meta);
334 }
335 0 return 1;
336 2 };
337  
338  
339 # Storage for a Sampler object
340 has _sampler => (
341 is => 'ro',
342 #isa => 'Bio::Community::Tools::Sampler',
343 required => 0,
344 default => sub {
345 0 ▶ require Bio::Community::Tools::Sampler;
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 0 return Bio::Community::Tools::Sampler->new( -seed => shift->get_seed ); },
347 2 lazy => 1,
348 predicate => '_has_sampler',
349 clearer => '_clear_sampler',
350 );
351  
352  
353 2 ▶method _count_normalize () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _count_normalize: called CC=23 SCAR=60.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
354 # Normalize communities by total count
355  
356 # Sanity check
357 1 my $meta = $self->metacommunity;
358 1 ▶ if ( (not $meta) || ($meta->get_communities_count == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta or $meta->get_communities_count == 0) 1 0 1
Condition33%1/3
not $meta or $meta->get_communities_count == 0
l!l&&r!l&&!r
100
MC/DC0%0/2
not $meta or $meta->get_communities_count == 0
not $meta $meta->get_communities_count == 0
Truth table33%1/3
not(A) or B
A= not $metaB= $meta->get_communities_count == 0
ABresult
1X1
011
000
359 1 $self->throw('Should have a metacommunity containing at least one community');
360 }
361  
362 # Get or set sample size
363 0 my $communities = $meta->get_all_communities;
364 0 my $min = min( map {$_->get_members_count} @$communities );
365 0 my $sample_size = $self->sample_size;
366 0 ▶ if (not defined $sample_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sample_size) { } 0 0 0
367 # Set sample size to smallest community size. Convert to integer if needed.
368 0 $sample_size = int $min;
369 0 $self->sample_size($sample_size);
370 } else {
371 0 ▶ if ($sample_size > $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sample_size > $min) 0 0 0
372 0 my $name;
373 0 for my $community (@$communities) {
374 0 ▶ if ($community->get_members_count == $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($community->get_members_count == $min) 0 0 0
375 0 $name = $community->name;
376 0 last;
377 }
378 }
379 0 ▶ $self->throw("Was given a sample size of $sample_size which is larger".
Branch: 1 branch
BranchTrueFalseTotal
unless $self->drop 0 0 0
380 " than counts in the smallest community, (name: '$name', counts: ".
381 "$min)") if not $self->drop;
382 }
383 }
384 0 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
385 0 print "Bootstrap sample size: $sample_size\n";
386 0 ▶ if ($self->num_repetitions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_repetitions) { } 0 0 0
387 0 print "Bootstrap number of repetitions: ".$self->num_repetitions."\n";
388 } else {
389 0 print "Bootstrap beta diversity threshold: ".$self->threshold."\n";
390 }
391 }
392  
393 # Initialize a Sampler if performing a finite number of repetitions
394 0 ▶ if ( not( defined $self->num_repetitions && $self->num_repetitions eq 'inf' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->num_repetitions and $self->num_repetitions eq "inf") 0 0 0
Condition0%0/3
defined $self->num_repetitions and $self->num_repetitions eq "inf"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->num_repetitions and $self->num_repetitions eq "inf"
defined $self->num_repetitions $self->num_repetitions eq "inf"
Truth table0%0/3
A and B
A= defined $self->num_repetitionsB= $self->num_repetitions eq "inf"
ABresult
0X0
100
111
395 0 $self->_sampler();
396 }
397  
398 # Bootstrap now
399 0 my $average_meta = Bio::Community::Meta->new( -name => $meta->name );
400 0 my $min_repetitions = POSIX::DBL_MAX;
401 0 my $max_threshold = 0;
402 0 for my $community ( @$communities ) {
403 0 my ($average, $repetitions, $beta_val);
404 0 my $count = $community->get_members_count;
405 0 ▶ if ($count > $sample_size) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count > $sample_size) { } 0 0 0
elsif ($count == $sample_size) { } 0 0 0
406 0 ($average, $repetitions, $beta_val) = $self->_bootstrap($community);
407 } elsif ($count == $sample_size) {
408 0 ($average, $repetitions, $beta_val) = ($community->clone, undef, undef);
409 } else {
410 0 next; # drop this community
411 }
412 0 my $name = $community->name;
413 #$name .= ' ' if $name;
414 #$name .= 'average';
415 0 $average->name($name);
416 0 ▶ if (defined $self->num_repetitions) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->num_repetitions) { } 0 0 0
417 0 ▶ $max_threshold = $beta_val if (defined $beta_val) && ($beta_val > $max_threshold);
Branch: 1 branch
BranchTrueFalseTotal
if defined $beta_val and $beta_val > $max_threshold 0 0 0
Condition0%0/3
defined $beta_val and $beta_val > $max_threshold
!ll&&!rl&&r
000
MC/DC0%0/2
defined $beta_val and $beta_val > $max_threshold
defined $beta_val $beta_val > $max_threshold
Truth table0%0/3
A and B
A= defined $beta_valB= $beta_val > $max_threshold
ABresult
0X0
100
111
418 } else {
419 0 ▶ $min_repetitions = $repetitions if (defined $repetitions) && ($repetitions < $min_repetitions);
Branch: 1 branch
BranchTrueFalseTotal
if defined $repetitions and $repetitions < $min_repetitions 0 0 0
Condition0%0/3
defined $repetitions and $repetitions < $min_repetitions
!ll&&!rl&&r
000
MC/DC0%0/2
defined $repetitions and $repetitions < $min_repetitions
defined $repetitions $repetitions < $min_repetitions
Truth table0%0/3
A and B
A= defined $repetitionsB= $repetitions < $min_repetitions
ABresult
0X0
100
111
420 }
421 0 $average_meta->add_communities([$average]);
422 }
423  
424 0 ▶ if (defined $self->num_repetitions) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->num_repetitions) { } 0 0 0
425 0 $self->threshold($max_threshold);
426 } else {
427 0 ▶ if ($min_repetitions == POSIX::DBL_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_repetitions == "1.79769313486232e+308") 0 0 0
428 0 $min_repetitions = 0;
429 }
430 0 $self->num_repetitions($min_repetitions);
431 }
432  
433 0 $self->_set_avg_meta($average_meta);
434  
435 0 return 1;
436 }
437  
438  
439 2 ▶method _bootstrap (Bio::Community $community) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _bootstrap: not called CC=19 SCAR=58.8
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
$Method::Signatures::mutc{"cache"}{"Bio::Community"} "Method::Signatures"->_make_constraint("Bio::Community")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community"}B= "Method::Signatures"->_make_constraint("Bio::Community")
ABresult
1X1
011
000
440 # Re-sample a community many times and report the average community
441 0 my $threshold = $self->threshold();
442 0 my $sample_size = $self->sample_size();
443 0 my $repetitions = $self->num_repetitions();
444  
445 # Set 'use_weights' to sample from counts (similar to unweighted relative abundances)
446 0 my $use_weights = $community->use_weights;
447 0 $community->use_weights(0);
448  
449 0 my $members = $community->get_all_members;
450  
451 0 my $verbose = $self->verbose;
452 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
453 0 print "Community '".$community->name."'\n";
454 }
455  
456 0 my $overall = Bio::Community->new(
457 -name => 'overall',
458 -use_weights => $use_weights,
459 );
460  
461 0 my $sampler;
462 0 ▶ if ($self->_has_sampler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_sampler) 0 0 0
463 0 $sampler = $self->_sampler;
464 0 $sampler->community($community);
465 }
466  
467 0 my $prev_overall = Bio::Community->new( -name => 'prev' );
468 0 my $iteration = 0;
469 0 my $beta_val;
470 0 while (1) {
471  
472 # Get a random community and add it to the overall community
473 0 $iteration++;
474  
475 0 ▶ if (not $sampler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sampler) 0 0 0
476 # In fact, do a single repetition where we add the relative abundance
477 # as counts into a new community
478 0 ▶ if ($sample_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sample_size == 0) { } 0 0 0
479 0 $overall = Bio::Community->new();
480 } else {
481 0 require Bio::Community::Tools::Transformer;
482 0 $overall = Bio::Community::Tools::Transformer->new(
483 -metacommunity => Bio::Community::Meta->new(-communities =>[$community]),
484 -type => 'relative',
485 )->get_transformed_meta->next_community;
486 }
487 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
488 0 print " iteration inf\n";
489 }
490 0 $beta_val = 0;
491 0 last; # Exit when assuming infinite number of repetitions
492 }
493  
494 # If the Sampler is defined
495 0 my $random = $sampler->get_rand_community($sample_size);
496 0 $self->_add( $overall, $random, $members );
497  
498 # We could divide here, but since the beta diversity is based on the
499 # relative abundance, not the counts, it would be the same. Hence, only
500 # divide at the end.
501  
502 0 my $meta = Bio::Community::Meta->new(-communities =>[$overall, $prev_overall]);
503  
504 0 ▶ if (not defined $repetitions) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $repetitions) { } 0 0 0
505 # Exit if beta diversity with last average community is small
506 0 $beta_val = Bio::Community::Meta::Beta->new(
507 -type => 'euclidean',
508 -metacommunity => $meta,
509 )->get_beta;
510 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
511 0 print " iteration $iteration, beta diversity $beta_val\n";
512 }
513 0 ▶ last if $beta_val < $threshold;
Branch: 1 branch
BranchTrueFalseTotal
if $beta_val < $threshold 0 0 0
514 0 $prev_overall = $overall->clone;
515 0 $prev_overall->name('prev');
516 } else {
517 # Exit if all repetitions have been done
518 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
519 0 print " iteration $iteration\n";
520 }
521 0 ▶ if ($iteration == $repetitions - 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($iteration == $repetitions - 1) { } 0 0 0
elsif ($iteration >= $repetitions) { } 0 0 0
522 0 $prev_overall = $overall->clone;
523 0 $prev_overall->name('prev');
524 } elsif ($iteration >= $repetitions) {
525 0 $beta_val = Bio::Community::Meta::Beta->new(
526 -type => 'euclidean',
527 -metacommunity => $meta,
528 )->get_beta;
529 0 last;
530 }
531 }
532  
533 }
534  
535 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
536 0 print "\n";
537 }
538  
539 0 $community->use_weights($use_weights);
540  
541 0 ▶ if (defined $sampler) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sampler) { } 0 0 0
542 0 $self->_divide( $overall, $iteration, $members );
543 } else {
544 0 ▶ $sample_size ||= 1; # protect against div/0 when size is 0
Condition0%0/2
$sample_size ||= 1
l!l
00
MC/DC0%0/1
$sample_size ||= 1
$sample_size
Truth table0%0/2
A
A= $sample_size
Aresult
11
00
545 0 $self->_divide( $overall, 100 / $sample_size, $members );
546 }
547  
548 0 return $overall, $iteration, $beta_val;
549 }
550  
551  
552 #method _add (Bio::Community $existing, Bio::Community $new, $members) {
553 2 ▶method _add ($existing, $new, $members) { # keep it lean
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine _add: not called CC=6 SCAR=35.0
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
unless scalar @_ > 2 0 0 0
if scalar @_ > 3 0 0 0
554 # Add a new community to an existing one
555 0 for my $member (@$members) {
556 0 my $count = $new->get_count($member);
557 0 $existing->add_member( $member, $count );
558 }
559 0 return 1;
560 }
561  
562  
563 2 ▶method _divide (Bio::Community $community, Num $divisor, $members) {
Subroutine BEGIN: called CC=1 SCAR=6.0
Subroutine _divide: not called CC=10 SCAR=45.5
Branch: 6 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 0 0
unless scalar @_ > 1 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures"->_make_constraint("Num"))->check($divisor) 0 0 0
unless scalar @_ > 2 0 0 0
if scalar @_ > 3 0 0 0
Condition0%0/6
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
000
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures"->_make_constraint("Num")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
$Method::Signatures::mutc{"cache"}{"Bio::Community"} "Method::Signatures"->_make_constraint("Bio::Community")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures"->_make_constraint("Num")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community"}B= "Method::Signatures"->_make_constraint("Bio::Community")
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures"->_make_constraint("Num")
ABresult
1X1
011
000
564 # Divide the counts in a community
565 0 for my $member (@$members) {
566 0 my $count = $community->get_count($member);
567 0 my $new_count = $count / $divisor;
568 0 my $diff = $count - $new_count;
569 0 $community->remove_member( $member, $diff );
570 }
571 0 return 1;
572 }
573  
574  
575 2 ▶method _calc_repr(Bio::Community $average) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _calc_repr: not called CC=12 SCAR=49.5
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($average) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
$Method::Signatures::mutc{"cache"}{"Bio::Community"} "Method::Signatures"->_make_constraint("Bio::Community")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community"}B= "Method::Signatures"->_make_constraint("Bio::Community")
ABresult
1X1
011
000
576 # Round the member count and add them into a new, representative community
577 0 my $cur_count = 0;
578 0 my $target_count = int( $average->get_members_count + 0.5 ); # round count like 999.9 to 1000
579  
580 0 my $name = $average->name;
581 #$name =~ s/\s*average$//;
582 #$name .= ' ' if $name;
583 #$name .= 'representative';
584  
585 0 my $representative = Bio::Community->new(
586 -name => $name,
587 -use_weights => $average->use_weights,
588 );
589  
590 0 my $richness = 0;
591 0 my $deltas;
592 my $members;
593 0 ▶ while ( my $member = $average->next_member('_calc_repr_ite') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $average->next_member("_calc_repr_ite")) 0 0 0
594 0 $richness++;
595 # Add member and count to the community
596 0 my $count = $average->get_count($member);
597 0 my $new_count = int( $count + 0.5 );
598 0 my $delta = $new_count - $count;
599 0 ▶ push @$deltas, $delta if $delta != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delta != 0 0 0 0
600 0 push @$members, $member;
601 0 ▶ next if $new_count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $new_count == 0 0 0 0
602 0 $representative->add_member( $member, $new_count );
603 0 $cur_count += $new_count;
604 }
605 0 $cur_count = int( $cur_count + 0.5 );
606  
607 # Adjust the last count
608 0 ▶ if ($cur_count != $target_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_count != $target_count) 0 0 0
609  
610 # Sort deltas numerically descending
611 0 ($deltas, $members) = Bio::Community::_two_array_sort($deltas, $members);
612  
613 0 ▶ if ($cur_count < $target_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_count < $target_count) { } 0 0 0
614 # Total count too small! Increment members with smallest delta
615 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$representative->get_members_count < $target_count and do { do { pop @$deltas; $representative->add_member(pop @$members, 1) } } 0 0 0
616 0 pop @$deltas;
617 0 $representative->add_member( pop(@$members), 1 );
618 } while ($representative->get_members_count < $target_count);
619 } else {
620 # Total count too large! Decrement members with largest delta
621 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$representative->get_members_count > $target_count and do { do { shift @$deltas; $representative->remove_member(shift @$members, 1) } } 0 0 0
622 0 shift @$deltas;
623 0 $representative->remove_member( shift(@$members), 1 );
624 } while ($representative->get_members_count > $target_count);
625 }
626  
627 }
628  
629 0 return $representative;
630 }
631  
632  
633 2 __PACKAGE__->meta->make_immutable;
634  
635 2 1;
↑ Summary blib/lib/Bio/Community/Tools/IdConverter.pm »