blib/lib/Bio/Community/Meta.pm

filter: Statementstmt 99.3% 141 / 142 Branchbran 65.3% 34 / 52 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 30 / 30 Podpod n/a totaltotal 87.8% 210 / 239 CC 33 SCAR 14.1
CC
33
Cov
89%
add_communities
10.3
remove_community
5.5
get_community_by_name
5.3
CRAP 4.1
« Build ↑ Summary blib/lib/Bio/Community/Member.pm »
1 # BioPerl module for Bio::Community::Meta
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 - A metacommunity, or group of communities
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::Meta;
17
18 my $meta = Bio::Community::Meta->new( -name => 'alpine' );
19  
20 # Get communities from somewhere and ...
21 $meta->add_communities( [$community1, $community2, $community3] );
22
23 print "Metacommunity contains:\n";
24 print " ".$meta->get_communities_count." communities\n";
25 print " ".$meta->get_richness." species\n";
26 print " ".$meta->get_members_count." individuals\n";
27  
28 =head1 DESCRIPTION
29  
30 The Bio::Community::Meta module represent metacommunities, or groups of
31 communities. This object holds several Bio::Community objects, for example
32 tree communities found at different sites of a forest. Random access to any of
33 the communities is provided. However, the communities can also be accessed
34 sequentially, in the order they were given. This makes Bio::Community::Meta
35 capable of representing communities along a natural gradient.
36  
37 =head1 AUTHOR
38  
39 Florent Angly L<florent.angly@gmail.com>
40  
41 =head1 SUPPORT AND BUGS
42  
43 User feedback is an integral part of the evolution of this and other Bioperl
44 modules. Please direct usage questions or support issues to the mailing list,
45 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
46 experienced and reponsive experts will be able look at the problem and quickly
47 address it. Please include a thorough description of the problem with code and
48 data examples if at all possible.
49  
50 If you have found a bug, please report it on the BioPerl bug tracking system
51 to help us keep track the bugs and their resolution:
52 L<https://redmine.open-bio.org/projects/bioperl/>
53  
54 =head1 COPYRIGHT
55  
56 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
57  
58 This library is free software; you can redistribute it and/or modify
59 it under the same terms as Perl itself, either Perl version 5.10.1 or,
60 at your option, any later version of Perl 5 you may have available.
61  
62 =head1 APPENDIX
63  
64 The rest of the documentation details each of the object
65 methods. Internal methods are usually preceded with a _
66  
67 =head2 new
68  
69 Function: Create a new Bio::Community::Meta object
70 Usage : my $meta = Bio::Community::Meta->new( ... );
71 Args : -name : See name()
72 -communities : See add_communities()
73 -identify_members_by: See identify_members_by()
74 Returns : A new Bio::Community::Meta object
75  
76 =cut
77  
78  
79 package Bio::Community::Meta;
80  
81 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 17 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 17 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 17 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 17 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 17 ▶use Bio::Community;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 17 ▶use Bio::Community::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90  
91 17 extends 'Bio::Root::Root';
92  
93  
94 17 ▶method BUILD ($args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 84 84
if scalar @_ > 1 0 84 84
95 # Process -communities constructor
96 84 my $communities = delete $args->{'-communities'};
97 84 ▶ if ($communities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($communities) 18 66 84
98 18 $self->add_communities($communities);
99 }
100 }
101  
102  
103 =head2 name
104  
105 Function: Get or set the name of the metacommunity
106 Usage : $meta->name('estuary_salinity_gradient');
107 my $name = $meta->name();
108 Args : String for the name
109 Returns : String for the name
110  
111 =cut
112  
113 17 has name => (
114 is => 'rw',
115 isa => 'Str',
116 lazy => 1,
117 default => '',
118 init_arg => '-name',
119 );
120  
121  
122 =head2 identify_members_by
123  
124 Function: Get or set how members are handled.
125 In the Bio::Community modules, members with the same ID are
126 considered identical, no matter what their description or taxonomy is.
127 If the communities added to the metacommunity come from different
128 sources or files, the IDs of the members are unsafe because the same
129 member in different communities will likely have a different ID and
130 be interpreted as a different member, whereas different members may
131 have the same ID and be counted as the same. To work around this, you
132 can specify to look at the desc() of the members and give the same ID
133 to members that have the same description.
134 Usage : $meta->identify_members_by('desc');
135 Args : String, either 'id' or 'desc'
136 Returns : String, either 'id' (default) or 'desc'
137  
138 =cut
139  
140 17 has identify_members_by => (
141 is => 'rw',
142 isa => 'IdentifyMembersByType',
143 lazy => 1,
144 default => 'id',
145 init_arg => '-identify_members_by',
146 );
147  
148  
149 has _members_lookup => (
150 is => 'rw',
151 #isa => 'HashRef',
152 lazy => 1,
153 1 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
154 17 init_arg => undef,
155 );
156  
157  
158 has _communities => (
159 is => 'rw',
160 #isa => 'Tie::IxHash', # Communities are stored in a sorted hash
161 lazy => 1,
162 46 ▶ default => sub { tie my %hash, 'Tie::IxHash'; return \%hash },
Subroutine __ANON__: called CC=1 SCAR=0.0
163 17 init_arg => undef,
164 );
165  
166  
167 =head2 add_communities
168  
169 Function: Add communities to a metacommunity. Communities have to have distinct
170 names. Do not change the name of a community after you have added it!
171 Usage : $meta->add_communities([$community1, $community2]);
172 Args : An arrayref of Bio::Community objects to add
173 Returns : 1 on success
174  
175 =cut
176  
177 17 ▶method add_communities ( ArrayRef[Bio::Community] $communities ) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine add_communities: called CC=10 SCAR=23.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 103 103
unless ($Method::Signatures::mutc{"cache"}{"ArrayRef[Bio::Community]"} ||= "Method::Signatures"->_make_constraint("ArrayRef[Bio::Community]"))->check($communities) 0 103 103
if scalar @_ > 1 0 103 103
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"ArrayRef[Bio::Community]"} ||= "Method::Signatures"->_make_constraint("ArrayRef[Bio::Community]")
l!l&&r!l&&!r
90130
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"ArrayRef[Bio::Community]"} ||= "Method::Signatures"->_make_constraint("ArrayRef[Bio::Community]")
$Method::Signatures::mutc{"cache"}{"ArrayRef[Bio::Community]"} "Method::Signatures"->_make_constraint("ArrayRef[Bio::Community]")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"ArrayRef[Bio::Community]"}B= "Method::Signatures"->_make_constraint("ArrayRef[Bio::Community]")
ABresult
1X1
011
000
178 103 my $comm_hash = $self->_communities;
179 103 for my $community (@$communities) {
180 # Identify members by description and fix IDs
181 123 ▶ if ($self->identify_members_by eq 'desc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->identify_members_by eq "desc") 2 121 123
182 2 my $members_lookup = $self->_members_lookup;
183 2 for my $member (@{$community->get_all_members}) {
184 9 my $count = $community->remove_member($member);
185 9 my $desc = $member->desc;
186 9 my $same_member = $members_lookup->{$desc};
187 9 ▶ if (defined $same_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $same_member) { } 4 5 9
188 # A members with same description exists. Use same Member object
189 4 $member = $same_member;
190 } else {
191 # This is a new member. Use a new ID.
192 5 $member->_auto_id();
193 5 $members_lookup->{$desc} = $member;
194 }
195 9 $community->add_member($member, $count);
196 }
197 }
198  
199 # Add community
200 123 my $name = $community->name;
201 123 ▶ if (exists $comm_hash->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $comm_hash->{$name}) 0 123 123
202 0 $self->throw("Could not add community '$name' because there already is".
203 " a community with this name in the metacommunity");
204 }
205 123 $comm_hash->{$name} = $community;
206  
207 }
208 103 $self->_communities( $comm_hash );
209 103 $self->_set_communities_count( $self->get_communities_count + scalar @$communities );
210 103 return 1;
211 }
212  
213  
214 =head2 remove_community
215  
216 Function: Remove a community from a metacommunity
217 Usage : $meta->remove_community($community);
218 Args : A Bio::Community to remove
219 Returns : 1 on success
220  
221 =cut
222  
223 17 ▶method remove_community ( Bio::Community $community ) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine remove_community: called CC=5 SCAR=17.0
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 1 1
if scalar @_ > 1 0 1 1
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
010
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 table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community"}B= "Method::Signatures"->_make_constraint("Bio::Community")
ABresult
1X1
011
000
224 1 delete $self->_communities->{$community->name};
225 1 $self->_set_communities_count( $self->get_communities_count - 1 );
226 1 return 1;
227 }
228  
229  
230 =head2 next_community
231  
232 Function: Access the next community in the metacommunity (in the order the
233 communities were added).
234 Usage : my $community = $meta->next_community();
235 Args : None
236 Returns : A Bio::Community object
237  
238 =cut
239  
240 17 ▶method next_community () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_community: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 500 500
241 500 my $community = (each %{$self->_communities})[1];
242 500 return $community;
243 }
244  
245  
246 =head2 get_all_communities
247  
248 Function: Generate a list of all communities in the metacommunity.
249 Usage : my $communities = $meta->get_all_communities();
250 Args : None
251 Returns : An arrayref of Bio::Community objects
252  
253 =cut
254  
255 17 ▶method get_all_communities () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_all_communities: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 169 169
256 169 my @all_communities = values %{$self->_communities};
257 169 return \@all_communities;
258 }
259  
260  
261 =head2 get_metacommunity
262  
263 Function: Generate a community that is the sum of all the members of the
264 communities. If sampling effort was unequal between your communities,
265 you may consider using L<Bio::Community::Tools::Rarefier>.
266 Usage : my $metacommunity = $meta->get_metacommunity();
267 Args : None
268 Returns : A new Bio::Community object
269  
270 =cut
271  
272 17 ▶method get_metacommunity () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_metacommunity: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
273 7 my $metacommunity = Bio::Community->new();
274 7 for my $community (@{$self->get_all_communities}) {
275 15 ▶ while (my $member = $community->next_member('_meta_get_metacommunity_ite')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->next_member("_meta_get_metacommunity_ite")) 29 15 44
276 29 $metacommunity->add_member( $member, $community->get_count($member) );
277 }
278 }
279 7 return $metacommunity;
280 }
281  
282  
283 =head2 get_community_by_name
284  
285 Function: Fetch a community based on its name
286 Usage : my $community = $meta->get_community_by_name('prairie');
287 Args : String of the community name to look for
288 Returns : A Bio::Community object or undef if the community was not found
289  
290 =cut
291  
292 17 ▶method get_community_by_name ( Str $name ) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get_community_by_name: called CC=5 SCAR=16.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 65 65
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($name) 0 65 65
if scalar @_ > 1 0 65 65
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
5870
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
293 65 my $community = $self->_communities->{$name};
294 65 return $community;
295 }
296  
297  
298 =head2 get_communities_count
299  
300 Function: Get the total number of communities in the metacommunity
301 Usage : my $nof_communities = $meta->get_communities_count();
302 Args : None
303 Returns : Integer for the count
304  
305 =cut
306  
307 17 has _communities_count => (
308 is => 'ro',
309 #isa => 'PositiveNum',
310 lazy => 1,
311 default => 0,
312 init_arg => undef,
313 reader => 'get_communities_count',
314 writer => '_set_communities_count',
315 );
316  
317  
318 ####
319 # TODO: get_community_by_position() method
320 ####
321  
322  
323 ####
324 # TODO: next_member() iterator method
325 ####
326  
327  
328 =head2 get_all_members
329  
330 Function: Generate a list of all members in the metacommunity. Every member
331 appears only once in this list, even if the member is present in
332 multiple communities. Remember that members with the same ID are
333 considered the same member.
334 Usage : my $members = $meta->get_all_members();
335 Args : None
336 Returns : An arrayref of Bio::Community::Member objects
337  
338 =cut
339  
340 17 ▶method get_all_members () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_all_members: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 136 136
341 # Get all members in a hash
342 136 my $all_members = {};
343 136 ▶ while (my $community = $self->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $self->next_community) 276 136 412
344 276 ▶ while (my $member = $community->next_member('_get_all_members_ite')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->next_member("_get_all_members_ite")) 740 276 1016
345 # Members are defined by their ID
346 740 $all_members->{$member->id} = $member;
347 }
348 }
349 # Convert member hash to an array
350 136 $all_members = [values %$all_members];
351 136 return $all_members;
352 }
353  
354  
355 =head2 get_members_count
356  
357 Function: Calculate the total count of members in the metacommunity.
358 Usage : my $nof_individuals = $meta->get_members_count;
359 Args : None
360 Returns : Integer for the count
361  
362 =cut
363  
364 17 ▶method get_members_count () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_members_count: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 14 14
365 14 my $total_count = 0;
366 14 my $all_members = {};
367 14 ▶ while (my $community = $self->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $self->next_community) 38 14 52
368 38 ▶ while (my $member = $community->next_member('_get_gamma_richness_ite')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->next_member("_get_gamma_richness_ite")) 88 38 126
369 88 $total_count += $community->get_count($member);
370 }
371 }
372 14 return $total_count;
373 }
374  
375  
376 =head2 get_richness
377  
378 Function: Calculate the richness of the metacommunity (number of different
379 types of members). This is a form of gamma diversity.
380 Usage : my $gamma_richness = $meta->get_richness();
381 Args : None
382 Returns : Integer for the richness
383  
384 =cut
385  
386 17 ▶method get_richness () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_richness: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 15 15
387 15 my $richness = scalar @{$self->get_all_members};
388 15 return $richness;
389 }
390  
391  
392 17 __PACKAGE__->meta->make_immutable;
393  
394 17 1;
« Build ↑ Summary blib/lib/Bio/Community/Member.pm »