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

filter: Statementstmt 34.4% 50 / 145 Branchbran 4.7% 2 / 42 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 14 / 18 Podpod n/a totaltotal 31.1% 67 / 215 CC 33 SCAR 46.1
CC
33
Cov
27%
get_numbers
189.1
_get_ticks
141.4
get_strings
15.7
CRAP 100.3
« blib/lib/Bio/Community/Tools/Summarizer.pm ↑ Summary blib/lib/Bio/Community/Tools/Transformer.pm »
1 # BioPerl module for Bio::Community::Tools::Accumulator
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::Accumulator - Species accumulation curves
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::Tools::Accumulator;
17  
18 # A collector curve
19 my $collector = Bio::Community::Tools::Accumulator->new(
20 -metacommunity => $meta,
21 -type => 'collector',
22 );
23 my $numbers = $collector->get_numbers;
24 # or
25 my $strings = $collector->get_strings;
26  
27 # A rarefaction curve, with custom parameters
28 my $rarefaction = Bio::Community::Tools::Accumulator->new(
29 -metacommunity => $meta,
30 -type => 'rarefaction',
31 -num_repetitions => 100,
32 -num_ticks => 8,
33 -tick_spacing => 'linear',
34 -alpha_types => ['simpson', 'shannon'],
35 );
36 my $numbers = $rarefaction->get_numbers;
37  
38 =head1 DESCRIPTION
39  
40 This module takes a metacommunity and produces one of two types of species
41 accumulation curves: a rarefaction curve or a collector curve.
42  
43 In a rarefaction curve, an increasing number of randomly drawn members is
44 sampled from the given communities and alpha diversity is calculated. In a
45 collector curve, an increasing number of communities is randomly drawn and
46 combined and their cumulative alpha diversity is determined.
47  
48 The average alpha diversity for the different sampling sizes is reported either
49 as array references or a tab-delimited string. Note that no plot is actually
50 drawn.
51  
52 =head1 AUTHOR
53  
54 Florent Angly L<florent.angly@gmail.com>
55  
56 =head1 SUPPORT AND BUGS
57  
58 User feedback is an integral part of the evolution of this and other Bioperl
59 modules. Please direct usage questions or support issues to the mailing list,
60 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
61 experienced and reponsive experts will be able look at the problem and quickly
62 address it. Please include a thorough description of the problem with code and
63 data examples if at all possible.
64  
65 If you have found a bug, please report it on the BioPerl bug tracking system
66 to help us keep track the bugs and their resolution:
67 L<https://redmine.open-bio.org/projects/bioperl/>
68  
69 =head1 COPYRIGHT
70  
71 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
72  
73 This library is free software; you can redistribute it and/or modify
74 it under the same terms as Perl itself, either Perl version 5.10.1 or,
75 at your option, any later version of Perl 5 you may have available.
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the object
80 methods. Internal methods are usually preceded with a _
81  
82 =head2 new
83  
84 Function: Create a new Bio::Community::Tool::Accumulator object
85 Usage : my $accumulator = Bio::Community::Tool::Accumulator->new( );
86 Args : -metacommunity: see metacommunity()
87 -type : 'rarefaction' or 'collector'
88 -num_repetitions : see num_repetitions()
89 -num_ticks : see num_ticks()
90 -tick_spacing : see tick_spacing()
91 -alpha_types : see alpha_types()
92 -seed : see set_seed()
93 Returns : a new Bio::Community::Tools::Accumulator object
94  
95 =cut
96  
97  
98 package Bio::Community::Tools::Accumulator;
99  
100 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use Bio::Community::Alpha;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 ▶use Bio::Community::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 ▶use Bio::Community::Tools::Rarefier;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 1 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
108 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 1 extends 'Bio::Root::Root';
111  
112  
113 =head2 metacommunity
114  
115 Function: Get or set the metacommunity to normalize.
116 Usage : my $meta = $accumulator->metacommunity;
117 Args : A Bio::Community::Meta object
118 Returns : A Bio::Community::Meta object
119  
120 =cut
121  
122 1 has metacommunity => (
123 is => 'rw',
124 isa => 'Maybe[Bio::Community::Meta]',
125 required => 0,
126 default => undef,
127 lazy => 1,
128 init_arg => '-metacommunity',
129 );
130  
131  
132 =head2 type
133  
134 Function: Get or set the type of accumulation curve to produce.
135 Usage : my $type = $accumulator->type;
136 Args : String of the accumulation type: 'rarefaction' (default) or 'collector'
137 Returns : String of the accumulation type
138  
139 =cut
140  
141 1 has type => (
142 is => 'rw',
143 isa => 'AccumulationType',
144 required => 0,
145 default => 'rarefaction',
146 lazy => 1,
147 init_arg => '-type',
148 );
149  
150  
151 =head2 num_repetitions
152  
153 Function: Get or set the number of num_repetitions to do at each sampling depth.
154 Usage : my $num_repetitions = $accumulator->num_repetitions;
155 Args : positive integer for the number of repetitions
156 Returns : positive integer for the number of repetitions
157  
158 =cut
159  
160 1 has num_repetitions => (
161 is => 'rw',
162 isa => 'Maybe[PositiveInt | Str]',
163 required => 0,
164 default => 10,
165 lazy => 1,
166 init_arg => '-num_repetitions',
167 );
168  
169  
170 =head2 num_ticks
171  
172 Function: For rarefaction curves, get or set how many different numbers of
173 individuals to sample, for the smallest community. This number may
174 not always be honored because ticks have to be integer numbers.
175 Usage : my $num_ticks = $accumulator->num_ticks;
176 Args : positive integer for the number of ticks (default: 10)
177 Returns : positive integer for the number of ticks
178  
179 =cut
180  
181 1 has num_ticks => (
182 is => 'rw',
183 isa => 'NumTicks',
184 required => 0,
185 default => 12,
186 lazy => 1,
187 init_arg => '-num_ticks',
188 );
189  
190  
191 =head2 tick_spacing
192  
193 Function: Get or set the type of spacing between the ticks of a rarefaction
194 curve.
195 Usage : my $tick_spacing = $accumulator->tick_spacing;
196 Args : String, either 'logarithmic' (default) or 'linear'
197 Returns : String
198  
199 =cut
200  
201 1 has tick_spacing => (
202 is => 'rw',
203 isa => 'SpacingType',
204 required => 0,
205 default => 'logarithmic',
206 lazy => 1,
207 init_arg => '-tick_spacing',
208 );
209  
210  
211 =head2 alpha_types
212  
213 Function: Get or set the type of alpha diversity to calculate.
214 Usage : my $alphas = $accumulator->alpha_types;
215 Args : Arrayref of alpha diversity types (['observed'] by default).
216 See C<type()> in L<Bio::Community::Alpha> for details.
217 Returns : Arrayref of alpha diversity types.
218  
219 =cut
220  
221 has alpha_types => (
222 is => 'rw',
223 isa => 'ArrayRef[AlphaType]',
224 required => 0,
225 0 ▶ default => sub { ['observed'] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 1 lazy => 1,
227 init_arg => '-alpha_types',
228 );
229  
230  
231 =head2 verbose
232  
233 Function: Get or set verbose mode. This displays the number of ticks to use
234 before the accumulation curve itself is computed.
235 Usage : $accumulator->verbose(1);
236 Args : 0 (default) or 1
237 Returns : 0 or 1
238  
239 =cut
240  
241 1 has verbose => (
242 is => 'rw',
243 isa => 'Bool',
244 required => 0,
245 default => 0,
246 lazy => 1,
247 init_arg => '-verbose',
248 );
249  
250  
251 =head2 get_numbers
252  
253 Function: Calculate the accumulation curve and return the numbers.
254 Usage : my $nums = $accumulator->get_numbers;
255 Args : none
256 Returns : A structure containing the average alpha diversity of the communities
257 for each tick value, for each requested alpha type:
258 { alpha_type => [ [tick, alpha1, alpha2, ... ], ... ], ... }
259  
260 =cut
261  
262 1 ▶method get_numbers {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_numbers: called CC=17 SCAR=52.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
263  
264 # Sanity check
265 1 my $meta = $self->metacommunity;
266 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
267 1 $self->throw('Should have a metacommunity containing at least one community');
268 }
269  
270 0 my $ticks = $self->_get_ticks(); # Determine range of sample sizes
271 0 ▶ print "Using ticks: ".join(' ', @$ticks)."\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
272  
273 0 my $rarefier;
274 0 my $comm_names = [map { $_->name } @{$meta->get_all_communities}];
275 0 ▶ if ($self->type eq 'rarefaction') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq 'rarefaction') { } 0 0 0
276 # Rarefaction curve
277 0 $rarefier = Bio::Community::Tools::Rarefier->new(
278 -metacommunity => $meta,
279 -num_repetitions => 1,
280 -drop => 1,
281 -verbose => 0,
282 );
283 } else {
284 # Collector curve
285 0 $comm_names = ['collector'];
286 }
287  
288 0 my $avg_alphas;
289 0 for my $tick (@$ticks) {
290  
291 0 for my $rep (1 .. $self->num_repetitions) {
292  
293 # Rarefy or collect communities
294 0 my $acc_meta;
295 0 ▶ if ($rarefier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rarefier) { } 0 0 0
296 0 $rarefier->sample_size($tick);
297 0 $acc_meta = $rarefier->get_repr_meta;
298 } else {
299 0 $acc_meta = $self->_collect_comms($tick);
300 }
301  
302 0 for my $alpha (@{$self->alpha_types}) {
303  
304 # Calculate the alpha diversity of the communities
305 0 for my $i (0 .. $#$comm_names) {
306 0 ▶ my $acc_comm = $rarefier ?
Branch: 1 branch
BranchTrueFalseTotal
$rarefier ? : 0 0 0
307 $acc_meta->get_community_by_name($comm_names->[$i]) :
308 $acc_meta->get_metacommunity;
309  
310 0 ▶ my $val = Bio::Community::Alpha->new(
Branch: 1 branch
BranchTrueFalseTotal
if $acc_comm 0 0 0
311 -community => $acc_comm,
312 -type => $alpha,
313 )->get_alpha
314 if $acc_comm;
315  
316 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
317 0 $avg_alphas->{$alpha}->{$tick}->[$i] += $val;
318 } else {
319 0 $avg_alphas->{$alpha}->{$tick}->[$i] = undef;
320 }
321  
322 }
323  
324 }
325  
326 }
327  
328 }
329  
330 # Divide results to obtain averages and format in a friendly way
331 0 my $res;
332 0 for my $alpha (@{$self->alpha_types}) {
333 0 for my $tick (@$ticks) {
334 0 ▶ my @vals = map { defined($_) ? $_ / $self->num_repetitions : '' } @{$avg_alphas->{$alpha}->{$tick}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
335 0 push @{$res->{$alpha}}, [$tick, @vals];
336 }
337 }
338  
339 0 return $res;
340 }
341  
342  
343 =head2 get_strings
344  
345 Function: Calculate the accumulation curves and return them as strings.
346 Usage : my $strings = $accumulator->get_strings;
347 Args : none
348 Returns : A arrayref of strings, each of which represents the accumulation
349 curve for a given alpha diversity type.
350  
351 =cut
352  
353 1 ▶method get_strings {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine get_strings: not called CC=4 SCAR=27.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
354 0 my $comm_names = [map {$_->name} @{$self->metacommunity->get_all_communities}];
355 0 my $header = join("\t", '', @$comm_names)."\n";
356 0 my $nums = $self->get_numbers;
357 0 my @res;
358 0 for my $alpha (@{$self->alpha_types}) {
359 0 my $str = '# '.$alpha.$header;
360 0 for my $row (@{$nums->{$alpha}}) {
361 0 $str .= join("\t", @$row)."\n";
362 }
363 0 push @res, $str;
364 }
365 0 return \@res;
366 }
367  
368  
369 1 ▶method _collect_comms ($num) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _collect_comms: not called CC=3 SCAR=21.8
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
370 # Create a community that is the combination of a random subset of the
371 # communities in the given metacommunity
372 0 my @rand_comms = shuffle @{$self->metacommunity->get_all_communities};
373 0 @rand_comms = @rand_comms[0..$num-1];
374 0 my $meta = Bio::Community::Meta->new( -communities => \@rand_comms );
375 0 return $meta;
376 }
377  
378  
379 1 ▶method _get_ticks {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _get_ticks: not called CC=12 SCAR=49.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
380 # Calculate which number of members or communities to sample from for the
381 # accumulation curve.
382  
383 # Sanity check
384 0 my $meta = $self->metacommunity;
385 0 ▶ if ( (not $meta) || ($meta->get_communities_count == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta or $meta->get_communities_count == 0) 0 0 0
Condition0%0/3
not $meta or $meta->get_communities_count == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $meta or $meta->get_communities_count == 0
not $meta $meta->get_communities_count == 0
Truth table0%0/3
not(A) or B
A= not $metaB= $meta->get_communities_count == 0
ABresult
1X1
011
000
386 0 $self->throw('Should have a metacommunity containing at least one community');
387 }
388  
389 0 my @ticks;
390 0 ▶ if ($self->type eq 'collector') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq 'collector') { } 0 0 0
391 # Ticks for a collector curve, i.e. number of communities
392 0 @ticks = 0 .. $meta->get_communities_count;
393  
394 } else {
395 # Ticks for a rarefaction curve, i.e. number of individuals
396 0 my $comms = $meta->get_all_communities;
397  
398 0 my $counts = [ map {$_->get_members_count} @$comms ];
399 0 my $sort_order = [ sort { $counts->[$a] <=> $counts->[$b] } 0..$#$comms ];
400 0 my $min_count = $counts->[0];
401 0 my $max_count = $counts->[1];
402  
403 0 @ticks = (0);
404 0 my $num_ticks = $self->num_ticks - 1;
405 0 my $linear_spacing = $self->tick_spacing eq 'linear';
406  
407 0 ▶ my $param = $linear_spacing ?
Branch: 1 branch
BranchTrueFalseTotal
$linear_spacing ? : 0 0 0
408 ($min_count-1) / ($num_ticks-1) :
409 ($min_count-1) / (exp($num_ticks-1)-1);
410  
411 0 my $tick_num = -1;
412 0 for my $i (@$sort_order) {
413 0 my $count = $counts->[$i];
414 0 $count = int( $count + 0.5 ); # round
415 0 my $tick;
416 0 while (1) {
417 0 $tick_num++;
418 0 ▶ $tick = $linear_spacing ?
Branch: 1 branch
BranchTrueFalseTotal
$linear_spacing ? : 0 0 0
419 1 + $tick_num * $param :
420 $param*(exp($tick_num)-1)+1;
421 0 $tick = int( $tick + 0.5 ); # round
422 0 ▶ if ($tick < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tick < $count) { } 0 0 0
423 0 ▶ push @ticks, $tick if $tick != $ticks[-1]; # avoid dups
Branch: 1 branch
BranchTrueFalseTotal
if $tick != $ticks[-1] 0 0 0
424 } else {
425 0 ▶ push @ticks, $count if $count != $ticks[-1]; # avoid dups
Branch: 1 branch
BranchTrueFalseTotal
if $count != $ticks[-1] 0 0 0
426 0 ▶ $tick_num-- if $tick > $count;
Branch: 1 branch
BranchTrueFalseTotal
if $tick > $count 0 0 0
427 0 last;
428 }
429 }
430 }
431  
432 }
433  
434 0 return \@ticks;
435 }
436  
437 1 __PACKAGE__->meta->make_immutable;
438  
439 1 1;
« blib/lib/Bio/Community/Tools/Summarizer.pm ↑ Summary blib/lib/Bio/Community/Tools/Transformer.pm »