blib/lib/Bio/Community.pm

filter: Statementstmt 93.8% 276 / 294 Branchbran 66.4% 93 / 140 Conditioncond 73.5% 25 / 34 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 92.7% 51 / 55 Podpod n/a totaltotal 82.9% 452 / 545 CC 86 SCAR 17.1
CC
86
Cov
84%
get_rank
12.1
get_member_by_rank
11.7
_create_hash_iter
9.2
CRAP 5.5
« blib/lib/Bio/Community/IO/Driver/generic.pm ↑ Summary blib/lib/Bio/Community/TaxonomyUtils.pm »
1 # BioPerl module for Bio::Community
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 - A biological community
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community;
17
18 my $community = Bio::Community->new( -name => 'soil_1' );
19 $community->add_member( $member1 ); # add 1 such Bio::Community::Member
20 $community->add_member( $member2, 3 ); # add 3 such members
21  
22 print "There are ".$community->get_members_count." members in the community\n";
23 print "The total diversity is ".$community->get_richness." species\n";
24  
25 while (my $member = $community->next_member) {
26 my $member_id = $member->id;
27 my $member_count = $community->get_count($member);
28 my $member_rel_ab = $community->get_rel_ab($member);
29 print "The relative abundance of member $member_id is $member_rel_ab % ($member_count counts)\n";
30 }
31  
32 =head1 DESCRIPTION
33  
34 The Bio::Community module represents communities of biological organisms. It is
35 composed of Bio::Community::Member objects at a specified abundance. Each member
36 can represent a species (e.g. an elephant, a bacterium), taxon, OTU, or any
37 proxy for a species.
38  
39 =head1 AUTHOR
40  
41 Florent Angly L<florent.angly@gmail.com>
42  
43 =head1 SUPPORT AND BUGS
44  
45 User feedback is an integral part of the evolution of this and other Bioperl
46 modules. Please direct usage questions or support issues to the mailing list,
47 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
48 experienced and reponsive experts will be able look at the problem and quickly
49 address it. Please include a thorough description of the problem with code and
50 data examples if at all possible.
51  
52 If you have found a bug, please report it on the BioPerl bug tracking system
53 to help us keep track the bugs and their resolution:
54 L<https://redmine.open-bio.org/projects/bioperl/>
55  
56 =head1 COPYRIGHT
57  
58 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
59  
60 This library is free software; you can redistribute it and/or modify
61 it under the same terms as Perl itself, either Perl version 5.10.1 or,
62 at your option, any later version of Perl 5 you may have available.
63  
64 =head1 APPENDIX
65  
66 The rest of the documentation details each of the object
67 methods. Internal methods are usually preceded with a _
68  
69 =head2 new
70  
71 Function: Create a new Bio::Community object
72 Usage : my $community = Bio::Community->new( ... );
73 Args : -name and -use_weights, see below...
74 Returns : a new Bio::Community object
75  
76 =cut
77  
78  
79 package Bio::Community;
80  
81 20 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 20 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 20 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 20 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 20 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 20 ▶use Bio::Community::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 20 our $VERSION = '0.001007'; # 0.1.7
89  
90 20 extends 'Bio::Root::Root';
91  
92  
93 =head2 name
94  
95 Function: Get or set the name of the community
96 Usage : $community->name('ocean sample 3');
97 my $name = $community->name();
98 Args : string for the name
99 Returns : string for the name
100  
101 =cut
102  
103 20 has name => (
104 is => 'rw',
105 isa => 'Str',
106 lazy => 1,
107 default => 'Unnamed',
108 init_arg => '-name',
109 );
110  
111  
112 =head2 use_weights
113  
114 Function: Set whether or not relative abundance should be normalized by taking
115 into accout the weights of the different members (e.g. genome length,
116 gene copy number). Refer to the C<Bio::Community::Member->weights()>
117 method for more details. The default is to use the weights that have
118 given to community members.
119 Usage : $community->use_weights(1);
120 Args : boolean
121 Returns : boolean
122  
123 =cut
124  
125 20 has use_weights => (
126 is => 'rw',
127 isa => 'Bool',
128 lazy => 1,
129 default => 1,
130 init_arg => '-use_weights',
131 );
132  
133  
134 =head2 get_average_weights
135  
136 Function: If any weights have been set using Bio::Community::IO, return their
137 averages.
138 Usage : my $averages = $community->get_average_weights;
139 Args : none
140 Returns : Arrayref of averages (one average for each file of weights)
141  
142 =cut
143  
144 20 has _average_weights => (
145 is => 'ro',
146 #isa => 'PositiveNum', # too costly for an internal method
147 lazy => 1,
148 default => \&_calc_average_weights, # calculate avg weights if not already set
149 init_arg => undef,
150 reader => 'get_average_weights',
151 writer => '_set_average_weights',
152 );
153  
154  
155 20 ▶method _calc_average_weights () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _calc_average_weights: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 8 8
156 # Calculate average weights in community and return them
157 8 my $community_average_weights = [];
158 8 ▶ while (my $member = $self->next_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $self->next_member) 11 8 19
159 11 my $rel_ab = $self->get_rel_ab($member);
160 11 my $weights = $member->weights;
161 11 for my $i (0 .. scalar @$weights - 1) {
162 16 $community_average_weights->[$i] += $rel_ab / 100 * $weights->[$i];
163 }
164 }
165 8 return $community_average_weights;
166 }
167  
168  
169 =head2 get_members_count
170  
171 Function: Get the total count of members sampled from the community.
172 Usage : my $total_count = $community->get_members_count();
173 Args : none
174 Returns : integer
175  
176 =cut
177  
178 20 has _total_count => (
179 is => 'ro',
180 #isa => 'PositiveNum', # too costly for an internal method
181 lazy => 1,
182 default => 0,
183 init_arg => undef,
184 reader => 'get_members_count',
185 writer => '_set_members_count',
186 );
187  
188  
189 =head2 get_members_abundance, set_members_abundance
190  
191 Function: Get or set the total abundance of members in the community. Setting
192 this option implies that you know the total abundance of the members
193 in the community, even though you have not have sampled them all. If
194 this value has not been set explicitly, this method returns
195 C<get_members_count> by default.
196 Usage : $community->set_members_abundance( 1.63e6 );
197 # or
198 my $total_abundance = $community->get_members_abundance();
199 Args : number
200 Returns : number
201  
202 =cut
203  
204 20 has _total_abundance => (
205 is => 'rw',
206 #isa => 'PositiveNum', # too costly for an internal method
207 lazy => 1,
208 default => 0,
209 init_arg => undef,
210 reader => '_get_members_abundance',
211 writer => 'set_members_abundance',
212 predicate => '_has_members_abundance',
213 );
214  
215 20 ▶method get_members_abundance ( ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_members_abundance: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 5 5
216 5 ▶ return $self->_has_members_abundance ?
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_members_abundance ? : 3 2 5
217 $self->_get_members_abundance :
218 $self->get_members_count ;
219 }
220  
221  
222 20 has _weighted_count => (
223 is => 'rw',
224 #isa => 'PositiveNum', # too costly for an internal method
225 lazy => 1,
226 default => 0,
227 init_arg => undef,
228 );
229  
230  
231 has _members => (
232 is => 'rw',
233 isa => 'HashRef',
234 lazy => 1,
235 206 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
236 20 init_arg => undef,
237 );
238  
239  
240 has _counts => (
241 is => 'rw',
242 isa => 'HashRef',
243 lazy => 1,
244 191 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
245 20 init_arg => undef,
246 );
247  
248  
249 has _ranks_hash_weighted => (
250 is => 'rw',
251 isa => 'HashRef',
252 lazy => 1,
253 124 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
254 20 init_arg => undef,
255 clearer => '_clear_ranks_hash_weighted',
256 );
257  
258  
259 has _ranks_arr_weighted => (
260 is => 'rw',
261 isa => 'ArrayRef',
262 lazy => 1,
263 195 ▶ default => sub{ [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
264 20 init_arg => undef,
265 clearer => '_clear_ranks_arr_weighted',
266 );
267  
268  
269 has _ranks_hash_unweighted => (
270 is => 'rw',
271 isa => 'HashRef',
272 lazy => 1,
273 2 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
274 20 init_arg => undef,
275 clearer => '_clear_ranks_hash_unweighted',
276 );
277  
278  
279 has _ranks_arr_unweighted => (
280 is => 'rw',
281 isa => 'ArrayRef',
282 lazy => 1,
283 192 ▶ default => sub{ [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
284 20 init_arg => undef,
285 clearer => '_clear_ranks_arr_unweighted',
286 );
287  
288  
289 20 has _richness => (
290 is => 'rw',
291 isa => 'Maybe[Int]',
292 lazy => 1,
293 default => undef,
294 init_arg => undef,
295 clearer => '_clear_richness',
296 );
297  
298  
299 20 has _members_iterator => (
300 is => 'rw',
301 isa => 'Maybe[HashRef]',
302 lazy => 1,
303 default => undef,
304 init_arg => undef,
305 clearer => '_clear_members_iterator',
306 );
307  
308 20 has _has_changed => (
309 is => 'rw',
310 isa => 'Bool',
311 lazy => 1,
312 default => 0,
313 init_arg => undef,
314 );
315  
316  
317 =head2 add_member
318  
319 Function: Add members to a community
320 Usage : $community->add_member($member, 3);
321 Args : * a Bio::Community::Member to add
322 * how many of this member to add (positive number, default: 1)
323 Returns : 1 on success
324  
325 =cut
326  
327 #method add_member ( Bio::Community::Member $member, Count $count = 1 ) {
328 20 ▶method add_member ( $member, $count = 1 ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine add_member: called CC=4 SCAR=13.9
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 542 542
scalar @_ > 1 ? : 534 8 542
if scalar @_ > 2 0 542 542
329 542 my $member_id = $member->id;
330 542 $self->_counts->{$member_id} += $count;
331 542 $self->_members->{$member_id} = $member;
332 542 $self->_set_members_count( $self->get_members_count + $count );
333 542 $self->_weighted_count( $self->_weighted_count + $count / _prod($member->weights) );
334 542 $self->_has_changed(1);
335 542 return 1;
336 }
337  
338  
339 =head2 remove_member
340  
341 Function: Remove members from a community
342 Usage : $community->remove_member($member, 3);
343 Args : * A Bio::Community::Member to remove
344 * Optional: how many of this member to remove. If no value is
345 provided, all such members are removed.
346 Returns : Number of this member removed
347  
348 =cut
349  
350 #method remove_member ( Bio::Community::Member $member, Count $count = 1 ) {
351 20 ▶method remove_member ( $member, $count? ) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine remove_member: called CC=8 SCAR=21.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 12 12
if scalar @_ > 2 0 12 12
352 # Sanity checks
353 12 my $member_id = $member->id;
354 12 my $counts = $self->_counts;
355 12 ▶ if (exists $counts->{$member_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $counts->{$member_id}) { } 11 1 12
356 # Remove existing member
357 11 my $existing_count = $counts->{$member_id};
358 11 ▶ if ( defined($count) && ($count > $existing_count) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count and $count > $existing_count) 0 11 11
Condition66%2/3
defined $count and $count > $existing_count
!ll&&!rl&&r
1010
MC/DC0%0/2
defined $count and $count > $existing_count
defined $count $count > $existing_count
Truth table66%2/3
A and B
A= defined $countB= $count > $existing_count
ABresult
0X0
100
111
359 0 $self->throw("Error: More members to remove ($count) than there are in the community (".$counts->{$member}."\n");
360 }
361 # Now remove unwanted members
362 11 ▶ if (not defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 10 1 11
363 10 $count = $existing_count;
364 }
365 11 $counts->{$member_id} -= $count;
366 11 ▶ if ($counts->{$member_id} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counts->{$member_id} == 0) 10 1 11
367 10 delete $counts->{$member_id};
368 10 delete $self->_members->{$member_id};
369 }
370 11 $self->_set_members_count( $self->get_members_count - $count );
371 11 $self->_weighted_count( $self->_weighted_count - $count / _prod($member->weights) );
372 11 $self->_has_changed(1);
373 } else {
374 # Nothing to remove
375 1 $count = 0;
376 }
377 12 return $count;
378 }
379  
380  
381 =head2 next_member
382  
383 Function: Access the next member in a community (in no specific order). Be
384 warned that each time you change the community, this iterator has to
385 start again from the beginning! By default, a single iterator is
386 created. However, if you need several independent iterators, simply
387 provide an arbitrary iterator name.
388 Usage : # Get members through the default iterator
389 my $member = $community->next_member();
390 # Get members through an independent, named iterator
391 my $member = $community->next_member('other_ite');
392 Args : an optional name to give to the iterator (must not start with '_')
393 Returns : a Bio::Community::Member object
394  
395 =cut
396  
397 20 ▶method next_member ( $iter_name = 'default' ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_member: called CC=6 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
scalar @_ > 0 ? : 2918 27 2945
if scalar @_ > 1 0 2945 2945
398 2945 ▶ $self->_reset if $self->_has_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_changed 34 2911 2945
399  
400 2945 my $iters = $self->_members_iterator;
401  
402 # Create a named iterator
403 2945 my $iter;
404 2945 ▶ if (not exists $iters->{$iter_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $iters->{$iter_name}) { } 847 2098 2945
405 # Create new iterator
406 847 $iter = $self->_create_hash_val_iter( $self->_members );
407 847 $iters->{$iter_name} = $iter;
408 } else {
409 2098 $iter = $iters->{$iter_name};
410 }
411  
412 # Get next member from iterator
413 2945 my $member = $iter->();
414  
415 # Delete iterator when done
416 2945 ▶ if (not defined $member) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $member) 846 2099 2945
417 846 delete $iters->{$iter_name};
418 }
419  
420 2945 $self->_members_iterator($iters);
421 2945 return $member;
422 }
423  
424  
425 20 ▶method _create_hash_iter ($data) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _create_hash_iter: not called CC=3 SCAR=22.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
426 # Iteratively return hash key-value pairs
427 0 my %h = %$data;
428 return sub {
429 0 ▶ my ($key, $val) = each %h;
Subroutine __ANON__: not called CC=2 SCAR=17.9
430 0 ▶ return unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
431 0 return ($key, $val);
432 0 };
433 }
434  
435  
436 20 ▶method _create_array_iter ($data) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _create_array_iter: not called CC=3 SCAR=21.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
437 # Iteratively return array values
438 0 my @r = @$data;
439 return sub {
440 0 ▶ return shift @r;
Subroutine __ANON__: not called CC=1 SCAR=6.9
441 0 };
442 }
443  
444  
445 20 ▶method _create_hash_val_iter ($data) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _create_hash_val_iter: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 847 847
if scalar @_ > 1 0 847 847
446 # Iteratively return hash values
447 847 my @r = values %$data;
448 return sub {
449 2945 ▶ return shift @r;
Subroutine __ANON__: called CC=1 SCAR=0.0
450 847 };
451 }
452  
453  
454 =head2 get_all_members
455  
456 Function: Generate a list of all members in the community.
457 Usage : my $members = $community->get_all_members();
458 Args : An arrayref of Bio::Community objects
459 Returns : An arrayref of Bio::Community::Member objects
460  
461 =cut
462  
463 20 ▶method get_all_members () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_all_members: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 158 158
464 # Get all members in a hash
465 158 my $all_members = {};
466 158 ▶ while (my $member = $self->next_member('_community_get_all_members_ite')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $self->next_member("_community_get_all_members_ite")) 396 158 554
467 # Members are defined by their ID
468 396 $all_members->{$member->id} = $member;
469 }
470  
471 # Convert member hash to an array
472 158 $all_members = [values %$all_members];
473  
474 158 return $all_members;
475 }
476  
477  
478 =head2 get_member_by_id
479  
480 Function: Fetch a member based on its ID.
481 Usage : my $member = $community->get_member_by_id(3);
482 Args : integer for the member ID
483 Returns : a Bio::Community::Member object or undef if member was not found
484  
485 =cut
486  
487 20 ▶method get_member_by_id (Str $member_id) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get_member_by_id: called CC=5 SCAR=16.7
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 69 69
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($member_id) 0 69 69
if scalar @_ > 1 0 69 69
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
6720
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
488 69 return $self->_members->{$member_id};
489 }
490  
491  
492 =head2 get_member_by_rank
493  
494 Function: Fetch a member based on its abundance rank. A smaller rank corresponds
495 to a larger relative abundance.
496 Usage : my $member = $community->get_member_by_rank(1);
497 Args : strictly positive integer for the member rank
498 Returns : a Bio::Community::Member object or undef if member was not found
499  
500 =cut
501  
502 20 ▶method get_member_by_rank (AbundanceRank $rank) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get_member_by_rank: called CC=11 SCAR=24.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 441 441
unless ($Method::Signatures::mutc{"cache"}{"AbundanceRank"} ||= "Method::Signatures"->_make_constraint("AbundanceRank"))->check($rank) 0 441 441
if scalar @_ > 1 0 441 441
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"AbundanceRank"} ||= "Method::Signatures"->_make_constraint("AbundanceRank")
l!l&&r!l&&!r
431100
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"AbundanceRank"} ||= "Method::Signatures"->_make_constraint("AbundanceRank")
$Method::Signatures::mutc{"cache"}{"AbundanceRank"} "Method::Signatures"->_make_constraint("AbundanceRank")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"AbundanceRank"}B= "Method::Signatures"->_make_constraint("AbundanceRank")
ABresult
1X1
011
000
503 441 ▶ $self->_reset if $self->_has_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_changed 3 438 441
504 441 ▶ if ( $self->use_weights && (scalar @{$self->_ranks_arr_weighted} == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_weights and scalar @{$self->_ranks_arr_weighted;} == 0) 129 312 441
Condition100%3/3
$self->use_weights and scalar @{$self->_ranks_arr_weighted;} == 0
!ll&&!rl&&r
7305129
MC/DC100%2/2
$self->use_weights and scalar @{$self->_ranks_arr_weighted;} == 0
$self->use_weights scalar @{$self->_ranks_arr_weighted;} == 0
Truth table100%3/3
A and B
A= $self->use_weightsB= scalar @{$self->_ranks_arr_weighted;} == 0
ABresult
0X0
100
111
505 # Calculate the relative abundance ranks unless they already exist
506 129 $self->_calc_ranks();
507 }
508 441 ▶ if ( (not $self->use_weights) && (scalar @{$self->_ranks_arr_unweighted} == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->use_weights and scalar @{$self->_ranks_arr_unweighted;} == 0) 0 441 441
Condition66%2/3
not $self->use_weights and scalar @{$self->_ranks_arr_unweighted;} == 0
!ll&&!rl&&r
43470
MC/DC0%0/2
not $self->use_weights and scalar @{$self->_ranks_arr_unweighted;} == 0
not $self->use_weights scalar @{$self->_ranks_arr_unweighted;} == 0
Truth table66%2/3
not(A) and B
A= not $self->use_weightsB= scalar @{$self->_ranks_arr_unweighted;} == 0
ABresult
0X0
100
111
509 # Calculate the count ranks unless they already exist
510 0 $self->_calc_ranks();
511 }
512 441 ▶ my $member = $self->use_weights ? $self->_ranks_arr_weighted->[$rank-1] :
Branch: 1 branch
BranchTrueFalseTotal
$self->use_weights ? : 434 7 441
513 $self->_ranks_arr_unweighted->[$rank-1];
514 441 return $member;
515 }
516  
517  
518 ####
519 # TODO: get_member_by_rel_ab
520 # get_member_by_abs_ab
521 # get_member_by_count
522 ####
523  
524  
525 =head2 get_richness
526  
527 Function: Report the community richness or number of different types of members.
528 This is a form of alpha diversity.
529 Usage : my $alpha_richness = $community->get_richness();
530 Args : none
531 Returns : integer for the richness
532  
533 =cut
534  
535 20 ▶method get_richness () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_richness: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 625 625
536 625 ▶ $self->_reset if $self->_has_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_changed 154 471 625
537 625 ▶ if (not defined $self->_richness) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_richness) 192 433 625
538  
539 # Try to calculate the richness from the abundance ranks if available
540 192 my $num_members = scalar( @{$self->_ranks_arr_weighted} ) ||
541 192 ▶ scalar( @{$self->_ranks_arr_unweighted} ) ;
Condition50%1/2
scalar @{$self->_ranks_arr_weighted;} || scalar @{$self->_ranks_arr_unweighted;}
l!l
0192
MC/DC0%0/1
scalar @{$self->_ranks_arr_weighted;} || scalar @{$self->_ranks_arr_unweighted;}
scalar @{$self->_ranks_arr_weighted;}
Truth table50%1/2
A
A= scalar @{$self->_ranks_arr_weighted;}
Aresult
11
00
542  
543 # If rank abundance are not available, calculate richness manually
544 192 ▶ if ($num_members == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_members == 0) 191 1 192
545 191 ▶ while ($self->next_member('_get_alpha_richness_ite')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next_member("_get_alpha_richness_ite")) 476 191 667
546 476 $num_members++;
547 }
548 }
549  
550 # Save richness for later re-use
551 192 $self->_richness($num_members);
552 }
553 625 return $self->_richness;
554 }
555  
556  
557 =head2 get_count
558  
559 Function: Fetch the abundance or count of a member
560 Usage : my $count = $community->get_count($member);
561 Args : a Bio::Community::Member object
562 Returns : An integer for the count of this member, including zero if the member
563 was not present in the community.
564  
565 =cut
566  
567 #method get_count (Bio::Community::Member $member) {
568 20 ▶method get_count ($member) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine get_count: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1970 1970
if scalar @_ > 1 0 1970 1970
569 1970 ▶ return $self->_counts->{$member->id} || 0;
Condition100%2/2
$self->_counts->{$member->id} || 0
l!l
1764206
MC/DC100%1/1
$self->_counts->{$member->id} || 0
$self->_counts->{$member->id}
Truth table100%2/2
A
A= $self->_counts->{$member->id}
Aresult
11
00
570 }
571  
572  
573 =head2 get_rel_ab
574  
575 Function: Determine the relative abundance (in percent) of a member in the
576 community.
577 Usage : my $rel_ab = $community->get_rel_ab($member);
578 Args : a Bio::Community::Member object
579 Returns : an integer between 0 and 100 for the relative abundance of this member
580  
581 =cut
582  
583 20 ▶method get_rel_ab (Bio::Community::Member $member) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get_rel_ab: called CC=7 SCAR=19.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1394 1394
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 1394 1394
if scalar @_ > 1 0 1394 1394
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
138860
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} "Method::Signatures"->_make_constraint("Bio::Community::Member")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Member"}B= "Method::Signatures"->_make_constraint("Bio::Community::Member")
ABresult
1X1
011
000
584 1394 my $rel_ab = 0;
585 1394 my ($weight, $weighted_count);
586 1394 ▶ if ($self->use_weights) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_weights) { } 1387 7 1394
587 1387 $weight = _prod($member->weights);
588 1387 $weighted_count = $self->_weighted_count;
589 } else {
590 7 $weight = 1;
591 7 $weighted_count = $self->get_members_count;
592 }
593 1394 ▶ if ($weighted_count != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted_count != 0) 1390 4 1394
594 1390 $rel_ab = $self->get_count($member) * 100 / ($weight * $weighted_count);
595 }
596 1394 return $rel_ab;
597 }
598  
599  
600 =head2 get_abs_ab
601  
602 Function: Determine the absolute abundance of a member in the community, i.e.,
603 its C<get_rel_ab()> multiplied by its C<get_members_abundance()>.
604 Usage : my $abs_ab = $community->get_abs_ab($member);
605 Args : a Bio::Community::Member object
606 Returns : a number for the absolute abundance of this member
607  
608 =cut
609  
610 20 ▶method get_abs_ab (Bio::Community::Member $member) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine get_abs_ab: called CC=5 SCAR=17.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 2 2
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 2 2
if scalar @_ > 1 0 2 2
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
200
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} "Method::Signatures"->_make_constraint("Bio::Community::Member")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Member"}B= "Method::Signatures"->_make_constraint("Bio::Community::Member")
ABresult
1X1
011
000
611 2 return $self->get_rel_ab($member) / 100 * $self->get_members_abundance;
612 }
613  
614  
615 =head2 get_rank
616  
617 Function: Determine the abundance rank of a member in the community. The
618 organism with the highest relative abundance has rank 1, the second-
619 most abundant has rank 2, etc.
620 Usage : my $rank = $community->get_rank($member);
621 Args : a Bio::Community::Member object
622 Returns : integer for the abundance rank of this member or undef if the member
623 was not found
624  
625 =cut
626  
627 20 ▶method get_rank (Bio::Community::Member $member) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get_rank: called CC=12 SCAR=24.9
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 66 66
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 66 66
if scalar @_ > 1 0 66 66
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
6510
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} "Method::Signatures"->_make_constraint("Bio::Community::Member")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Member"}B= "Method::Signatures"->_make_constraint("Bio::Community::Member")
ABresult
1X1
011
000
628 66 ▶ $self->_reset if $self->_has_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_changed 1 65 66
629 66 my $member_id = $member->id;
630 66 ▶ if ( $self->get_member_by_id($member_id) ) { # If the member exists
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_member_by_id($member_id)) 65 1 66
631 65 ▶ if ( $self->use_weights && (scalar @{$self->_ranks_arr_weighted} == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_weights and scalar @{$self->_ranks_arr_weighted;} == 0) 8 57 65
Condition100%3/3
$self->use_weights and scalar @{$self->_ranks_arr_weighted;} == 0
!ll&&!rl&&r
5528
MC/DC100%2/2
$self->use_weights and scalar @{$self->_ranks_arr_weighted;} == 0
$self->use_weights scalar @{$self->_ranks_arr_weighted;} == 0
Truth table100%3/3
A and B
A= $self->use_weightsB= scalar @{$self->_ranks_arr_weighted;} == 0
ABresult
0X0
100
111
632 # Calculate relative abundance based ranks if ranks do not already exist
633 8 $self->_calc_ranks();
634 }
635 65 ▶ if ( (not $self->use_weights) && (scalar @{$self->_ranks_arr_unweighted} == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->use_weights and scalar @{$self->_ranks_arr_unweighted;} == 0) 2 63 65
Condition100%3/3
not $self->use_weights and scalar @{$self->_ranks_arr_unweighted;} == 0
!ll&&!rl&&r
6032
MC/DC100%2/2
not $self->use_weights and scalar @{$self->_ranks_arr_unweighted;} == 0
not $self->use_weights scalar @{$self->_ranks_arr_unweighted;} == 0
Truth table100%3/3
not(A) and B
A= not $self->use_weightsB= scalar @{$self->_ranks_arr_unweighted;} == 0
ABresult
0X0
100
111
636 # Calculate relative abundance based ranks if ranks do not already exist
637 2 $self->_calc_ranks();
638 }
639 }
640 my $rank = $self->use_weights ? $self->_ranks_hash_weighted->{$member->id} :
641 66 ▶ $self->_ranks_hash_unweighted->{$member->id};
Branch: 1 branch
BranchTrueFalseTotal
$self->use_weights ? : 60 6 66
642 66 return $rank;
643 }
644  
645  
646 20 ▶method _calc_ranks () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _calc_ranks: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 139 139
647 # Calculate the abundance ranks of the community members. Save them in a hash
648 # and as an array.
649  
650 # 1/ Get abundance of all members and sort them
651 139 my $members = $self->get_all_members;
652 139 my $rel_abs = [ map { $self->get_rel_ab($_) } @$members ];
653  
654 # 2/ Save ranks in an array
655 139 ($rel_abs, $members) = _two_array_sort($rel_abs, $members);
656 139 my $weighted = $self->use_weights;
657 139 ▶ if ($weighted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted) { } 137 2 139
658 137 $self->_ranks_arr_weighted( $members );
659 } else {
660 2 $self->_ranks_arr_unweighted( $members );
661 }
662  
663 # 3/ Save ranks in a hash
664 139 for my $rank (1 .. scalar @$members) {
665 348 my $member = $members->[$rank-1];
666 348 ▶ if ($weighted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted) { } 343 5 348
667 343 $self->_ranks_hash_weighted->{$member->id} = $rank;
668 } else {
669 5 $self->_ranks_hash_unweighted->{$member->id} = $rank;
670 }
671 }
672  
673 139 return 1;
674 }
675  
676  
677 20 ▶method _reset () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _reset: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 192 192
678 # Re-initialize some attributes when the community has changed
679 192 $self->_clear_ranks_hash_weighted();
680 192 $self->_clear_ranks_arr_weighted();
681 192 $self->_clear_ranks_hash_unweighted();
682 192 $self->_clear_ranks_arr_unweighted();
683 192 $self->_clear_richness();
684 192 $self->_clear_members_iterator();
685 192 $self->_has_changed(0);
686 192 return 1;
687 }
688  
689  
690 20 ▶func _two_array_sort ($l1, $l2) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _two_array_sort: called CC=5 SCAR=16.2
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 139 139
unless scalar @_ > 1 0 139 139
if scalar @_ > 2 0 139 139
691 # Sort 2 arrays by doing a decreasing numeric sort of the first one and
692 # keeping the match of the elements of the second with those of the first one
693 139 my @ids = map { [ $$l1[$_], $$l2[$_] ] } (0..$#$l1);
694 139 @ids = sort { $b->[0] <=> $a->[0] } @ids;
695 139 my @k1;
696 my @k2;
697 139 ▶ for (my $i = 0; $i < scalar @ids; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @ids) 348 139 487
698 348 $k1[$i] = $ids[$i][0];
699 348 $k2[$i] = $ids[$i][1];
700 }
701 139 return \@k1, \@k2;
702 }
703  
704  
705 20 ▶func _prod ($arr) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _prod: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1940 1940
if scalar @_ > 1 0 1940 1940
706 # Calculate the product of the numbers in an array
707 1940 my $prod = 1;
708 1940 $prod *= $_ for @$arr;
709 1940 return $prod;
710 }
711  
712  
713 20 __PACKAGE__->meta->make_immutable;
714  
715 20 1;
« blib/lib/Bio/Community/IO/Driver/generic.pm ↑ Summary blib/lib/Bio/Community/TaxonomyUtils.pm »