blib/lib/Bio/Community/IO.pm

filter: Statementstmt 84.5% 360 / 426 Branchbran 58.4% 132 / 226 Conditioncond 57.5% 46 / 80 MC/DCmcdc 32.0% 16 / 50 Subroutinesub 77.6% 52 / 67 Podpod n/a totaltotal 71.3% 606 / 849 CC 147 SCAR 25.8
CC
147
Cov
73%
write_member
58.8
write_community
26.7
_write_community_init
22.2
CRAP 13.2
« blib/lib/Bio/Community/IO/FormatGuesser.pm ↑ Summary blib/lib/Bio/Community/Role/Table.pm »
1 # BioPerl module for Bio::Community::IO
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::IO - Read and write files that describe communities
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::IO;
17  
18 # Read communities from a file, one by one
19 my $in = Bio::Community::IO->new(
20 -file => 'otu_table.qiime',
21 -format => 'qiime', # format is optional
22 );
23 my $community1 = $in->next_community(); # a Bio::Community object
24 my $community2 = $in->next_community();
25 $in->close;
26  
27 # Write communities in another file
28 my $out = Bio::Community::IO->new(
29 -file => '>new_otu_table.generic',
30 -format => 'generic',
31 );
32 $out->write_community($community);
33 $out->close;
34  
35 # Re-read communities, but all at once
36 $in = Bio::Community::IO->new( -file => 'new_otu_table.generic' );
37 my $meta = $in->next_metacommunity(); # a Bio::Community::Meta object
38 $in->close;
39  
40 =head1 DESCRIPTION
41  
42 A Bio::Community::IO object implement methods to read and write communities in
43 formats used by popular programs such as BIOM, GAAS, QIIME, Unifrac, or as
44 generic tab-separated tables. The format should be automatically detected though
45 it can be manually specified. This module can also convert community member
46 abundance between counts, absolute abundance, relative abundance and fractions.
47  
48 When reading communities, the next_member() method is called by next_community(),
49 which itself is called by next_metacommunity(). Similarly, when writing,
50 write_member() is called by write_community(), which is called by
51 write_metacommunity().
52  
53 =head2 DRIVER IMPLEMENTATION
54  
55 Bio::Community::IO provides the higher-level organisation to read and write
56 community files, but it is the modules located in the Bio::Community::IO::Driver::*
57 namespaces that do the low-level format-specific work.
58  
59 All drivers are expected to implement specific methods, e.g. for reading:
60  
61 =over
62  
63 =item _next_metacommunity_init()
64  
65 A private hook called at the beginning of next_metacommunity() that returns the
66 name of the metacommunity (if applicable). It also allows drivers to do an
67 action before the metacommunity is read.
68  
69 =item _next_community_init()
70  
71 A private hook called at the beginning of next_community() that returns the name
72 of the community. It also allows drivers to do an action before the current
73 community is read.
74  
75 =item next_member()
76  
77 A public method that returns a Bio::Community::Member and its count in the
78 community being read.
79  
80 =item _next_community_finish()
81  
82 A private hook called at the end of next_community(). It allows drivers to do
83 an action after the current community has been read.
84  
85 =item _next_metacommunity_finish()
86  
87 A private hook called at the end of next_metacommunity(). It allows drivers to
88 do an action after the metacommunity has been read.
89  
90 =back
91  
92 Similarly, for a driver to write community information to a file or stream,
93 it should implement these methods:
94  
95 =over
96  
97 =item _write_metacommunity_init()
98  
99 A private hook called at the beginning of write_metacommunity() and that accepts
100 a Bio::Community::Meta as argument. It allows drivers to do an action before the
101 metacommunity is written.
102  
103 =item _write_community_init()
104  
105 A private hook called at the beginning of write_community() and that accepts
106 a Bio::Community as argument. It allows drivers to do an action before the
107 current community is written.
108  
109 =item write_member()
110  
111 A public method that accepts as arguments a Bio::Community::Member and its count
112 in the community being written, and processes them.
113  
114 =item _write_community_finish()
115  
116 A private hook called at the end of write_community() and that accepts a
117 Bio::Community as argument. It allows drivers to do an action after the
118 current community has been written.
119  
120 =back _write_metacommunity_finish()
121  
122 A private hook called at the end of write_metacommunity() and that accepts a
123 Bio::Community::Meta as argument. It allows drivers to do an action after the
124 metacommunity has been written.
125  
126 =back
127 =head1 AUTHOR
128  
129 Florent Angly L<florent.angly@gmail.com>
130  
131 =head1 SUPPORT AND BUGS
132  
133 User feedback is an integral part of the evolution of this and other Bioperl
134 modules. Please direct usage questions or support issues to the mailing list,
135 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
136 experienced and reponsive experts will be able look at the problem and quickly
137 address it. Please include a thorough description of the problem with code and
138 data examples if at all possible.
139  
140 If you have found a bug, please report it on the BioPerl bug tracking system
141 to help us keep track the bugs and their resolution:
142 L<https://redmine.open-bio.org/projects/bioperl/>
143  
144 =head1 COPYRIGHT
145  
146 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
147  
148 This library is free software; you can redistribute it and/or modify
149 it under the same terms as Perl itself, either Perl version 5.10.1 or,
150 at your option, any later version of Perl 5 you may have available.
151  
152 =head1 APPENDIX
153  
154 The rest of the documentation details each of the object
155 methods. Internal methods are usually preceded with a _
156  
157 =head2 new
158  
159 Function: Create a new Bio::Community::IO object
160 Usage : # Reading a file
161 my $in = Bio::Community::IO->new( -file => 'community.txt' );
162 # Writing a file
163 my $out = Bio::Community::IO->new( -file => '>community.txt',
164 -format => 'generic' );
165 Args : -file : Path of a community file. See file() in Bio::Root::IO.
166 -format : Format of the file, either 'generic', 'biom', 'gaas',
167 'qiime' or 'unifrac'. This is optional when reading a community
168 file because the format is automatically detected by the
169 Bio::Community::IO::FormatGuesser module. See also format() in
170 Bio::Root::IO.
171 -weight_files : Arrayref of files (or filehandles) that contain
172 weights to assign to members. See weight_files().
173 -weight_assign : When using files of weights, define what to do for
174 community members that do not have weights. See weight_assign().
175 -taxonomy: Given a Bio::DB::Taxonomy object, try to place the community
176 members in this taxonomy. See taxonomy().
177 -skip_empty_communities: Skip communities with no members. See
178 skip_empty_communities()
179 See the documentation for _initialize_io() in Bio::Root::IO for other
180 accepted constructors like -fh, -string, -input, or -url.
181 Returns : A Bio::Community::IO object
182  
183 =cut
184  
185  
186 package Bio::Community::IO;
187  
188 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
189 13 ▶use Moose::Util qw/does_role/;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 13 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
191 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 13 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 13 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 13 ▶use Bio::Community;
Subroutine BEGIN: called CC=1 SCAR=0.0
195 13 ▶use Bio::Community::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 13 ▶use Bio::Community::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 13 ▶use Bio::Community::IO::FormatGuesser;
Subroutine BEGIN: called CC=1 SCAR=0.0
198 use Bio::Community::TaxonomyUtils
199 13 ▶ qw(split_lineage_string get_taxon_lineage get_lineage_string clean_lineage_arr);
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 13 extends 'Bio::Root::Root',
202 'Bio::Root::IO';
203  
204  
205 13 has '_meta' => (
206 is => 'rw',
207 #isa => undef, # Bio::Community::Meta
208 required => 0,
209 init_arg => undef,
210 default => undef,
211 lazy => 1,
212 );
213  
214  
215 # Overriding new... Is there a better alternative?
216  
217 13 ▶func new ($class, @args) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine new: called CC=8 SCAR=22.3
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ > 0 0 76 76
218 76 ▶ my $real_class = Scalar::Util::blessed($class) || $class;
Condition33%1/3
builtin::blessed($class) || $class
l!l&&r!l&&!r
0760
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class) $class
Truth table33%1/3
A || B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
219  
220 # These all come from the same base, Moose::Object, so this is fine
221 76 my $params = $real_class->BUILDARGS(@args);
222 76 my $format = delete $params->{'-format'};
223 76 ▶ if (not defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $format) 11 65 76
224 # Try to guess format
225 11 my $guesser = Bio::Community::IO::FormatGuesser->new();
226 11 ▶ if ($params->{'-file'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'-file'}) { } 11 0 11
elsif ($params->{'-fh'}) { } 0 0 0
227 11 $guesser->file( $params->{'-file'} );
228 } elsif ($params->{'-fh'}) {
229 0 $guesser->fh( $params->{'-fh'} );
230 }
231 11 $format = $guesser->guess;
232 }
233 76 ▶ if (not defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $format) 0 76 76
234 0 $real_class->throw("Could not automatically detect input format.");
235 }
236  
237 # Use the real driver class here
238 76 $real_class = __PACKAGE__.'::Driver::'.$format;
239 76 Module::Runtime::use_module($real_class);
240 76 ▶ $class->throw("Module $real_class does not implement a community IO stream")
Branch: 1 branch
BranchTrueFalseTotal
unless $real_class->does("Bio::Community::Role::IO") 0 76 76
241 unless $real_class->does('Bio::Community::Role::IO');
242  
243 76 $params = $real_class->BUILDARGS(%$params);
244 76 my $self = Class::MOP::Class->initialize($real_class)->new_object($params);
245  
246 76 return $self;
247 }
248  
249  
250 13 ▶method BUILD ($args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 95 95
if scalar @_ > 1 0 95 95
251 # Start IOs
252 95 $self->_initialize_io(%$args);
253 95 return 1;
254 }
255  
256  
257 =head2 next_member
258  
259 Usage : my ($member, $count) = $in->next_member;
260 Function: Get the next member from the community and its abundance. This
261 function is implemented by the Bio::Community::IO::Driver used to
262 parse the given file format.
263 Args : None
264 Returns : An array containing:
265 A Bio::Community::Member object (or undef)
266 A positive number (or undef)
267  
268 =cut
269  
270 13 ▶method next_member () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine next_member: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
271 0 $self->throw_not_implemented;
272 }
273  
274  
275 =head2 next_community
276  
277 Usage : my $community = $in->next_community;
278 Function: Get the next community. Note that communities without members are
279 skipped.
280 Args : None
281 Returns : A Bio::Community object
282 or
283 undef if there were no communities left
284  
285 =cut
286  
287 13 ▶method next_community () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_community: called CC=10 SCAR=23.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 202 202
288 202 my $community;
289  
290 202 ▶ if (not defined $self->_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_meta) 37 165 202
291 37 $self->_next_metacommunity_init( );
292 37 $self->_meta(Bio::Community::Meta->new);
293 }
294  
295 202 while ( 1 ) { # Skip communities with no members
296  
297 # Initialize driver for next community and set community name
298 203 my $name = $self->_next_community_init;
299  
300 # All communities have been read
301 203 ▶ last if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 47 156 203
302  
303 # Create a new community object
304 156 $community = Bio::Community->new( -name => $name );
305  
306 # Reinitialize queue
307 156 my $count_queue = {};
308 156 my $member_queue = $self->_member_queue;
309  
310 # Populate the community with members
311 156 ▶ while ( my ($member, $count) = $self->next_member() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->next_member) 540 0 540
312  
313 # All members have been read
314 540 ▶ last if not defined $member;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $member 156 384 540
315  
316 # Skip members without proper weights for now
317 384 ▶ if (exists $member_queue->{$member->id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$member_queue{$member->id}) 7 377 384
318 7 $count_queue->{$member->id} = $count;
319 7 next;
320 }
321  
322 # Add this member to the community
323 377 $community->add_member($member, $count);
324 }
325 156 $self->_count_queue( $count_queue );
326  
327 # Process member queue now
328 156 ▶ if (scalar keys %$count_queue > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$count_queue > 0) 6 150 156
329 6 $self->_process_member_queue($community);
330 }
331  
332 156 $self->_next_community_finish;
333  
334 156 ▶ if ( ($community->get_richness > 0) || (not $self->skip_empty_communities) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($community->get_richness > 0 or not $self->skip_empty_communities) { } 155 1 156
Condition100%3/3
$community->get_richness > 0 or not $self->skip_empty_communities
l!l&&r!l&&!r
143121
MC/DC100%2/2
$community->get_richness > 0 or not $self->skip_empty_communities
$community->get_richness > 0 not $self->skip_empty_communities
Truth table100%3/3
A or not(B)
A= $community->get_richness > 0B= not $self->skip_empty_communities
ABresult
1X1
011
000
335 155 last;
336 } else {
337 1 $community = undef;
338 }
339  
340 }
341 # Community is undef if all communities have been seen
342 202 return $community;
343 }
344  
345  
346 13 ▶method _next_community_init () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _next_community_init: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
347 # Driver-side method to initialize new community and return its name
348 0 $self->throw_not_implemented;
349 }
350  
351  
352 13 ▶method _next_community_finish () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _next_community_finish: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
353 # Driver-side method to finalize a community
354 0 $self->throw_not_implemented;
355 }
356  
357  
358 =head2 next_metacommunity
359  
360 Usage : my $meta = $in->next_metacommunity;
361 Function: Get the next metacommunity. It may contain one or several communities
362 depending on the format of the file read,
363 Args : None
364 Returns : A Bio::Community::Meta object
365 or
366 undef after the metacommunity has been read
367  
368 =cut
369  
370 13 ▶method next_metacommunity () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_metacommunity: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 16 16
371 16 my $meta;
372 16 ▶ if (not defined $self->_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_meta) 15 1 16
373 15 $meta = Bio::Community::Meta->new();
374 15 my $name = $self->_next_metacommunity_init;
375 14 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 14 0 14
376 14 $meta->name($name);
377 }
378 14 $self->_meta($meta);
379 14 ▶ while (my $community = $self->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $self->next_community) 38 14 52
380 38 $self->_meta->add_communities([$community]);
381 }
382 # _next_metacommunity_finish will happen before close()
383 }
384 15 return $meta;
385 }
386  
387  
388 13 ▶method _next_metacommunity_init () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _next_metacommunity_init: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
389 # Driver-side method to initialize new metacommunity and return its name
390 0 $self->throw_not_implemented;
391 }
392  
393  
394 13 ▶method _next_metacommunity_finish () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _next_metacommunity_finish: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
395 # Driver-side method to finalize reading a metacommunity
396 0 $self->throw_not_implemented;
397 }
398  
399  
400 =head2 write_member
401  
402 Usage : $out->write_member($member, $abundance);
403 Function: Write the next member from the community and its count or relative
404 abundance. This function is implemented by a Bio::Community::IO::Driver
405 specific to the given file format.
406 Args : A Bio::Community::Member object
407 A positive number
408 Returns : 1 for success
409  
410 =cut
411  
412 13 ▶method write_member (Bio::Community::Member $member, Count $count) {
Subroutine BEGIN: called CC=1 SCAR=5.8
Subroutine write_member: not called CC=8 SCAR=40.7
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 0 0
unless scalar @_ > 1 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count"))->check($count) 0 0 0
if scalar @_ > 2 0 0 0
Condition0%0/6
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
000
$Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count")
l!l&&r!l&&!r
000
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")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count")
$Method::Signatures::mutc{"cache"}{"Count"} "Method::Signatures"->_make_constraint("Count")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Member"}B= "Method::Signatures"->_make_constraint("Bio::Community::Member")
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Count"}B= "Method::Signatures"->_make_constraint("Count")
ABresult
1X1
011
000
413 0 $self->throw_not_implemented;
414 }
415  
416  
417 =head2 write_community
418  
419 Usage : $out->write_community($community);
420 Function: Write the next community.
421 Args : A Bio::Community object
422 Returns : 1 for success
423  
424 =cut
425  
426 13 ▶method write_community (Bio::Community $community) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine write_community: called CC=18 SCAR=32.9
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 62 62
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 62 62
if scalar @_ > 1 0 62 62
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
5660
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 table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community"}B= "Method::Signatures"->_make_constraint("Bio::Community")
ABresult
1X1
011
000
427 62 ▶ if (not defined $self->_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_meta) 12 50 62
428 12 my $meta = Bio::Community::Meta->new;
429 12 $self->_write_metacommunity_init($meta);
430 12 $self->_meta($meta);
431 }
432  
433 # Write community but skip empty ones if desired
434 62 ▶ if ( ($community->get_richness > 0) || (not $self->skip_empty_communities) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($community->get_richness > 0 or not $self->skip_empty_communities) 62 0 62
Condition66%2/3
$community->get_richness > 0 or not $self->skip_empty_communities
l!l&&r!l&&!r
5660
MC/DC0%0/2
$community->get_richness > 0 or not $self->skip_empty_communities
$community->get_richness > 0 not $self->skip_empty_communities
Truth table66%2/3
A or not(B)
A= $community->get_richness > 0B= not $self->skip_empty_communities
ABresult
1X1
011
000
435 62 $self->_write_community_init($community);
436 62 ▶ if (not defined $self->_meta->get_community_by_name($community->name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_meta->get_community_by_name($community->name)) 45 17 62
437 45 $self->_meta->add_communities([$community]);
438 }
439 62 my $sort_members = $self->sort_members;
440 62 ▶ if ($sort_members == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sort_members == 1) { } 0 62 62
elsif ($sort_members == -1) { } 2 60 62
elsif ($sort_members == 0) { } 60 0 60
441 0 my $rank = $community->get_richness;
442 0 ▶ while ( my $member = $community->get_member_by_rank($rank) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->get_member_by_rank($rank)) 0 0 0
443 0 $self->_process_member($member, $community);
444 0 $rank--;
445 0 ▶ last if $rank == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rank == 0 0 0 0
446 }
447 } elsif ($sort_members == -1) {
448 2 my $rank = 1;
449 2 ▶ while ( my $member = $community->get_member_by_rank($rank) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->get_member_by_rank($rank)) 6 2 8
450 6 $self->_process_member($member, $community);
451 6 $rank++;
452 }
453 } elsif ($sort_members == 0) {
454 60 ▶ while ( my $member = $community->next_member('_write_community_ite') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->next_member("_write_community_ite")) 150 60 210
455 150 $self->_process_member($member, $community);
456 }
457 } else {
458 0 $self->throw("$sort_members is not a valid sort value.\n");
459 }
460 62 $self->_write_community_finish($community);
461 }
462  
463 62 ▶ if ( ($self->_meta->get_communities_count > 1) && (not $self->multiple_communities) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_meta->get_communities_count > 1 and not $self->multiple_communities) 0 62 62
Condition66%2/3
$self->_meta->get_communities_count > 1 and not $self->multiple_communities
!ll&&!rl&&r
13490
MC/DC0%0/2
$self->_meta->get_communities_count > 1 and not $self->multiple_communities
$self->_meta->get_communities_count > 1 not $self->multiple_communities
Truth table66%2/3
A and not(B)
A= $self->_meta->get_communities_count > 1B= not $self->multiple_communities
ABresult
0X0
100
111
464 0 $self->throw('Format '.$self->format.' only supports writing one community per file');
465 }
466  
467 62 return 1;
468 }
469  
470  
471 13 ▶method _write_community_init (Bio::Community $community) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _write_community_init: not called CC=5 SCAR=31.0
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
472 # Driver-side method to initialize writing a community
473 0 $self->throw_not_implemented;
474 }
475  
476  
477 13 ▶method _write_community_finish (Bio::Community $community) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _write_community_finish: not called CC=5 SCAR=31.0
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
478 # Driver-side method to finalize writing a community
479 0 $self->throw_not_implemented;
480 }
481  
482  
483 =head2 write_metacommunity
484  
485 Usage : $out->write_metacommunity($meta);
486 Function: Write a metacommunity.
487 Args : A Bio::Community::Meta object
488 Returns : 1 for success
489  
490 =cut
491  
492 13 ▶method write_metacommunity (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine write_metacommunity: called CC=7 SCAR=19.7
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 7 7
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 7 7
if scalar @_ > 1 0 7 7
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
160
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} "Method::Signatures"->_make_constraint("Bio::Community::Meta")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"}B= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
ABresult
1X1
011
000
493 7 ▶ if (not defined $self->_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->_meta) { } 6 1 7
494 6 $self->_meta($meta);
495 6 $self->_write_metacommunity_init($meta);
496 6 ▶ while (my $community = $meta->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $meta->next_community) 17 6 23
497 17 $self->write_community($community);
498 }
499 # _write_metacommunity_finish will happen before close()
500 } else {
501 1 $self->throw('Can write only one metacommunity');
502 }
503 6 return 1;
504 }
505  
506  
507 13 ▶method _write_metacommunity_init (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _write_metacommunity_init: not called CC=5 SCAR=31.0
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} "Method::Signatures"->_make_constraint("Bio::Community::Meta")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"}B= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
ABresult
1X1
011
000
508 # Driver-side method to initialize writing a metacommunity
509 0 $self->throw_not_implemented;
510 }
511  
512  
513 13 ▶method _write_metacommunity_finish (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine _write_metacommunity_finish: not called CC=5 SCAR=31.0
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} "Method::Signatures"->_make_constraint("Bio::Community::Meta")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"}B= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
ABresult
1X1
011
000
514 # Driver-side method to finalize writing a metacommunity
515 0 $self->throw_not_implemented;
516 }
517  
518  
519 before 'close' => sub {
520 83 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
521 83 ▶ if ($self->mode eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'r') { } 59 24 83
522 59 $self->_next_metacommunity_finish();
523 } else {
524 # Finish preparing the metacommunity for writing
525 24 $self->_write_metacommunity_finish($self->_meta);
526 # For objects consuming Bio::Community::Role::Table, write the table now
527 24 ▶ if (does_role($self, 'Bio::Community::Role::Table')) {
Branch: 1 branch
BranchTrueFalseTotal
if (does_role($self, "Bio::Community::Role::Table")) 16 8 24
528 16 ▶ $self->_write_table unless $self->_was_written;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_was_written 15 1 16
529 }
530 }
531 83 return 1;
532 13 };
533  
534  
535 #method _process_member (Bio::Community::Member $member, Bio::Community $community) {
536 13 ▶method _process_member ($member, $community) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _process_member: called CC=8 SCAR=22.4
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 156 156
unless scalar @_ > 1 0 156 156
if scalar @_ > 2 0 156 156
537 156 my $ab_value;
538 156 my $ab_type = $self->abundance_type;
539 156 ▶ if ($ab_type eq 'count') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ab_type eq 'count') { } 150 6 156
elsif ($ab_type eq 'absolute') { } 0 6 6
elsif ($ab_type eq 'percentage') { } 0 6 6
elsif ($ab_type eq 'fraction') { } 6 0 6
540 150 $ab_value = $community->get_count($member);
541 } elsif ($ab_type eq 'absolute') {
542 0 $ab_value = $community->get_abs_ab($member);
543 } elsif ($ab_type eq 'percentage') {
544 0 $ab_value = $community->get_rel_ab($member);
545 } elsif ($ab_type eq 'fraction') {
546 6 $ab_value = $community->get_rel_ab($member) / 100;
547 } else {
548 0 $self->throw("$ab_value is not a valid abundance type.\n");
549 }
550 156 $self->write_member($member, $ab_value);
551 156 return 1;
552 }
553  
554  
555 =head2 skip_empty_communities
556  
557 Usage : $in->skip_empty_communities;
558 Function: Get or set whether empty communities (with no members) should be
559 read/written or skipped.
560 Args : 0 or 1
561 Returns : 0 or 1
562  
563 =cut
564  
565 13 has 'skip_empty_communities' => (
566 is => 'rw',
567 isa => 'Bool',
568 required => 0,
569 lazy => 1,
570 default => 0,
571 init_arg => '-skip_empty_communities',
572 );
573  
574  
575 =head2 sort_members
576  
577 Usage : $in->sort_members();
578 Function: When writing a community to a file, sort the community members based
579 on their abundance: 0 (off), 1 (by increasing abundance), -1 (by
580 decreasing abundance). The default is specific to each driver used.
581 Args : 0, 1 or -1
582 Returns : 0, 1 or -1
583  
584 =cut
585  
586 has 'sort_members' => (
587 is => 'ro',
588 isa => 'NumericSort',
589 required => 0,
590 lazy => 1,
591 init_arg => '-sort_members',
592 28 ▶ default => sub { return eval('$'.ref(shift).'::default_sort_members') || 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
eval '$' . ref(shift()) . '::default_sort_members' || 0
l!l
325
MC/DC100%1/1
eval '$' . ref(shift()) . '::default_sort_members' || 0
eval '$' . ref(shift()) . '::default_sort_members'
Truth table100%2/2
A
A= eval '$' . ref(shift()) . '::default_sort_members'
Aresult
11
00
593 13 );
594  
595  
596 =head2 abundance_type
597  
598 Usage : $in->abundance_type();
599 Function: When writing a community to a file, report member abundance in one
600 of four possible representations:
601 * count : observed count
602 * absolute : absolute abundance
603 * percentage: relative abundance, in percent (0-100%)
604 * fraction : relative abundance, as a fractional number (0-1)
605 The default is specific to each driver
606 Args : count, absolute, percentage or fraction
607 Returns : count, absolute, percentage or fraction
608  
609 =cut
610  
611 has 'abundance_type' => (
612 is => 'ro',
613 isa => 'AbundanceRepr',
614 required => 0,
615 lazy => 1,
616 init_arg => '-abundance_type',
617 26 ▶ default => sub { return eval('$'.ref(shift).'::default_abundance_type') || 'percentage' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
eval '$' . ref(shift()) . '::default_abundance_type' || 'percentage'
l!l
260
MC/DC0%0/1
eval '$' . ref(shift()) . '::default_abundance_type' || 'percentage'
eval '$' . ref(shift()) . '::default_abundance_type'
Truth table50%1/2
A
A= eval '$' . ref(shift()) . '::default_abundance_type'
Aresult
11
00
618 13 );
619  
620  
621 =head2 missing_string
622  
623 Usage : $in->missing_string();
624 Function: When writing a community to a file, specify what abundance string to
625 use for members that are not present in the community. The default is
626 specific to each driver used.
627 Args : string e.g. '', '0', 'n/a', '-'
628 Returns : string
629  
630 =cut
631  
632 has 'missing_string' => (
633 is => 'ro',
634 isa => 'Str',
635 required => 0,
636 lazy => 1,
637 init_arg => '-missing_string',
638 2 ▶ default => sub { return eval('$'.ref(shift).'::default_missing_string') || 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
eval '$' . ref(shift()) . '::default_missing_string' || 0
l!l
02
MC/DC0%0/1
eval '$' . ref(shift()) . '::default_missing_string' || 0
eval '$' . ref(shift()) . '::default_missing_string'
Truth table50%1/2
A
A= eval '$' . ref(shift()) . '::default_missing_string'
Aresult
11
00
639 13 );
640  
641  
642 =head2 multiple_communities
643  
644 Usage : $in->multiple_communities();
645 Function: Return whether or not the file format can represent multiple
646 communities in a single file.
647 Args : 0 or 1
648 Returns : 0 or 1
649  
650 =cut
651  
652 has 'multiple_communities' => (
653 is => 'ro',
654 isa => 'Bool',
655 required => 0,
656 lazy => 1,
657 21 ▶ default => sub { return eval('$'.ref(shift).'::multiple_communities') || 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
eval '$' . ref(shift()) . '::multiple_communities' || 0
l!l
201
MC/DC100%1/1
eval '$' . ref(shift()) . '::multiple_communities' || 0
eval '$' . ref(shift()) . '::multiple_communities'
Truth table100%2/2
A
A= eval '$' . ref(shift()) . '::multiple_communities'
Aresult
11
00
658 13 );
659  
660  
661 =head2 explicit_ids
662  
663 Usage : $in->explicit_ids();
664 Function: Return whether or not the file format explicitly records member IDs.
665 Args : 0 or 1
666 Returns : 0 or 1
667  
668 =cut
669  
670 has 'explicit_ids' => (
671 is => 'ro',
672 isa => 'Bool',
673 required => 0,
674 lazy => 1,
675 5 ▶ default => sub { return eval('$'.ref(shift).'::explicit_ids') || 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
eval '$' . ref(shift()) . '::explicit_ids' || 0
l!l
32
MC/DC100%1/1
eval '$' . ref(shift()) . '::explicit_ids' || 0
eval '$' . ref(shift()) . '::explicit_ids'
Truth table100%2/2
A
A= eval '$' . ref(shift()) . '::explicit_ids'
Aresult
11
00
676 13 );
677  
678  
679 =head2 weight_files
680  
681 Usage : $in->weight_files();
682 Function: When reading a community, specify files (or filehandles opened in
683 read mode) containing weights to assign to the community members.
684 Each file can contain a different type of weight to add. The file
685 should contain at least two tab-delimited columns: the first one
686 should contain the ID, description or string lineage of the member
687 and the second one the weight to assign to this member. Other columns
688 are ignored. A tab-delimited header line starting with '#' and
689 containing the name of the weight can be included.
690 Args : arrayref of file names (or filehandles)
691 Returns : arrayref of filehandles
692  
693 =cut
694  
695 has 'weight_files' => (
696 is => 'rw',
697 isa => 'ArrayRefOfReadableFileHandles',
698 coerce => 1,
699 required => 0,
700 lazy => 1,
701 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
702 13 init_arg => '-weight_files',
703 trigger => \&_read_weights,
704 );
705  
706  
707 has '_weights' => (
708 is => 'rw',
709 #isa => 'ArrayRef[HashRef[Num]]', # keep internals light
710 required => 0,
711 lazy => 1,
712 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
713 13 predicate => '_has_weights',
714 );
715  
716  
717 has '_file_average_weights' => (
718 is => 'rw',
719 #isa => 'ArrayRef[Num]', # keep internals light
720 required => 0,
721 lazy => 1,
722 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
723 13 );
724  
725  
726 =head2 weight_names
727  
728 Usage : $in->weight_names();
729 Function: After weight files have been read, you can get the name of the
730 weights using this method. You can also set them manually.
731 Args : arrayref of weight names
732 Returns : arrayref of weight names
733  
734 =cut
735  
736 has 'weight_names' => ( # hashref of Bio::Community::Members, keyed by member ID
737 is => 'rw',
738 isa => 'ArrayRef[Str]',
739 required => 0,
740 lazy => 1,
741 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
742 13 );
743  
744  
745 # The member queue contains members that will need to be given proper weights
746 # and to be added to the community
747  
748 has '_member_queue' => ( # hashref of Bio::Community::Members, keyed by member ID
749 is => 'rw',
750 required => 0,
751 lazy => 1,
752 51 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
753 13 );
754  
755 has '_count_queue' => ( # hashref of member counts, keyed by member ID
756 is => 'rw',
757 required => 0,
758 lazy => 1,
759 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
760 13 );
761  
762  
763 13 ▶method _read_weights ($args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _read_weights: called CC=12 SCAR=24.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 6 6
if scalar @_ > 1 0 6 6
764 6 my $all_weights = [];
765 6 my $all_names = [];
766 6 my $file_average_weights = [];
767 6 for my $fh (@{$self->weight_files}) {
768 10 my $average = 0;
769 10 my $num = 0;
770 10 my $file_weights = {};
771 10 my $weight_name;
772 10 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 56 10 66
773 56 ▶ if ($line =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) 22 34 56
774 22 chomp $line;
775 22 my ($col1, $col2) = (split "\t", $line)[0..1];
776 22 ▶ if ( (defined $col1) && (defined $col2) && (not defined $weight_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col1 and defined $col2 and not defined $weight_name) 10 12 22
Condition83%5/6
defined $col1 and defined $col2 and not defined $weight_name
!ll&&!rl&&r
8410
defined $col1 and defined $col2
!ll&&!rl&&r
0814
MC/DC66%2/3
defined $col1 and defined $col2 and not defined $weight_name
defined $col1 defined $col2 not defined $weight_name
Truth table75%3/4
A and B and not(C)
A= defined $col1B= defined $col2C= not defined $weight_name
ABCresult
0XX0
10X0
1100
1111
777 # Process header
778 10 $weight_name = $col2;
779 10 $weight_name =~ s/^weight$//i;
780 }
781 22 next;
782 }
783 34 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 8 26 34
784 26 chomp $line;
785 26 my ($id, $weight) = (split "\t", $line)[0..1];
786 26 $file_weights->{$id} = $weight;
787 26 $average += $weight;
788 26 $num++;
789 }
790 10 ▶ $weight_name = '' if not defined $weight_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $weight_name 0 10 10
791 10 close $fh;
792 10 push @$all_weights, $file_weights;
793 10 push @$all_names, $weight_name;
794 10 ▶ $average /= $num if $num > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num > 0 10 0 10
795 10 push @$file_average_weights, $average;
796 }
797 6 $self->weight_names($all_names);
798 6 $self->_weights( $all_weights );
799 6 $self->_file_average_weights( $file_average_weights );
800 6 return 1;
801 }
802  
803  
804 =head2 weight_identifier
805  
806 Usage : $in->weight_identifier('id');
807 Function: Get or set whether to lookup and assign weights to community members
808 based on the member description or their ID.
809 Args : 'desc' (default), or 'id'
810 Returns : 'desc' or 'id'
811  
812 =cut
813  
814 13 has 'weight_identifier' => (
815 is => 'rw',
816 isa => 'IdentifyMembersByType',
817 required => 0,
818 lazy => 1,
819 default => 'desc',
820 init_arg => '-weight_identifier',
821 );
822  
823  
824 =head2 weight_assign
825  
826 Usage : $in->weight_assign();
827 Function: When using weights, specify what value to assign to the members for
828 which no weight is found in the provided weight file:
829 * $num : Check the member description against each file of weights.
830 If no weight is found in a file, assign the arbitrary weight
831 provided as argument to the member.
832 * file_average : Check the member description against each file of
833 weights. If no weight is found in a file, assign the average
834 weight in this file to the member.
835 * community_average : Check the member description against each file
836 of weights. If no weight is found in a file, the weight given
837 to the member is the average weight of all the other members in
838 in this community. If none of the community members have
839 weights, the weight assignment method defaults to 'file_average'
840 for this community. Note that because the assigned weight is
841 the average weight in this community, this means that the same
842 members will have different weights in different communities.
843 Note also that the processing of members with no explicit
844 weights can only be done after all other members have been
845 added and is effective only if the community is built using the
846 next_community() method.
847 * ancestor : Provided the member have a taxonomic assignment, check
848 the taxonomic lineage of this member against each file of
849 weights. When no weight is found for this taxonomic lineage in
850 a weight file, go up the taxonomic lineage of the member and
851 assign to it the weight of the first ancestor that has a
852 weight in the weights file. Fall back to the 'community_average'
853 method if no taxonomic information is available for this member
854 (for example a member with no BLAST hit), or if none of the
855 ancestors have a specified weight.
856 Args : 'file_average', 'community_average', 'ancestor' or a number
857 Returns : 'file_average', 'community_average', 'ancestor' or a number
858  
859 =cut
860  
861 13 has 'weight_assign' => (
862 is => 'rw',
863 isa => 'WeightAssignType',
864 required => 0,
865 lazy => 1,
866 default => 'file_average',
867 init_arg => '-weight_assign',
868 );
869  
870  
871 =head2 _attach_weights
872  
873 Usage : $in->_attach_weights($member);
874 Function: Once a member has been created, a driver should call this method
875 to attach the proper weights (read from the user-provided weight
876 files) to a member. If no member is provided, this method will not
877 complain and will do nothing.
878 Args : a Bio::Community::Member or nothing
879 Returns : 1 for success
880  
881 =cut
882  
883 13 ▶method _attach_weights (Maybe[Bio::Community::Member] $member) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine _attach_weights: called CC=21 SCAR=30.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 203 203
unless ($Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} ||= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]"))->check($member) 0 203 203
if scalar @_ > 1 0 203 203
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} ||= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
l!l&&r!l&&!r
20120
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} ||= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
$Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"}B= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
ABresult
1X1
011
000
884 # Once we have a member, attach weights to it
885 203 ▶ if ( defined($member) && $self->_has_weights ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $member and $self->_has_weights) 18 185 203
Condition66%2/3
defined $member and $self->_has_weights
!ll&&!rl&&r
018518
MC/DC50%1/2
defined $member and $self->_has_weights
defined $member $self->_has_weights
Truth table66%2/3
A and B
A= defined $memberB= $self->_has_weights
ABresult
0X0
100
111
886  
887 18 my $weights;
888 18 my $assign_method = $self->weight_assign;
889 18 my $weight_names = $self->weight_names;
890 18 for my $i (0 .. scalar @{$self->_weights} - 1) {
891 30 my $weight;
892 30 my $weight_type = $self->_weights->[$i];
893  
894 30 ▶ if ($assign_method eq 'ancestor') {
Branch: 1 branch
BranchTrueFalseTotal
if ($assign_method eq 'ancestor') { } 3 27 30
895 3 my $taxon = $member->taxon;
896 3 ▶ if (defined $taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taxon) 2 1 3
897 # Method based on member taxonomic lineage
898 2 my $lineage_arr = get_taxon_lineage($taxon);
899 2 my $lineage;
900 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
pop @$lineage_arr and do { do { $lineage = get_lineage_string(clean_lineage_arr($lineage_arr)); $weight = $weight_type->{$lineage}; if (not defined $weight and $lineage =~ s/ //g) { $weight = $weight_type->{$lineage}; } ; if (defined $weight) { my $weight_name = $weight_names->[$i] || 'weight number ' . ($i + 1); $self->debug(q[Member '] . get_lineage_string(get_taxon_lineage($taxon)) . q[' (ID ] . $member->id . ") got $weight_name from " . $lineage_arr->[-1]->node_name . ": $weight\n"); @$lineage_arr = (); } } } 2 2 4
901 4 $lineage = get_lineage_string(clean_lineage_arr($lineage_arr));
902 4 $weight = $weight_type->{$lineage};
903 4 ▶ if ( (not defined $weight) && ($lineage =~ s/ //g) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $weight and $lineage =~ s/ //g) 1 3 4
Condition100%3/3
not defined $weight and $lineage =~ s/ //g
!ll&&!rl&&r
211
MC/DC100%2/2
not defined $weight and $lineage =~ s/ //g
not defined $weight $lineage =~ s/ //g
Truth table100%3/3
not(A) and B
A= not defined $weightB= $lineage =~ s/ //g
ABresult
0X0
100
111
904 # If no weight found, try lineage again without white spaces
905 1 $weight = $weight_type->{$lineage};
906 }
907 4 ▶ if (defined $weight) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $weight) 2 2 4
908 # Weight found. Get ready to exit loop.
909 2 ▶ my $weight_name = $weight_names->[$i] || 'weight number '.($i+1);
Condition50%1/2
$weight_names->[$i] || 'weight number ' . ($i + 1)
l!l
20
MC/DC0%0/1
$weight_names->[$i] || 'weight number ' . ($i + 1)
$weight_names->[$i]
Truth table50%1/2
A
A= $weight_names->[$i]
Aresult
11
00
910 2 $self->debug("Member '".get_lineage_string(get_taxon_lineage($taxon)).
911 "' (ID ".$member->id.") got $weight_name from ".$lineage_arr->[-1]->node_name.
912 ": $weight\n");
913 2 @$lineage_arr = ();
914 }
915 } while ( pop @$lineage_arr );
916 }
917 3 ▶ if (not defined $weight) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $weight) 1 2 3
918 # Use the 'community_average' assignment method:
919 # Correct weight will be assigned when community is 100% created
920 1 $weight = 0;
921 1 $self->_member_queue->{$member->id}->{$i} = $member;
922 }
923  
924 } else {
925  
926 # Methods based on member description (or ID)
927 27 ▶ my $lookup = $self->weight_identifier eq 'desc' ? $member->desc : $member->id;
Branch: 1 branch
BranchTrueFalseTotal
$self->weight_identifier eq 'desc' ? : 18 9 27
928 27 ▶ if ( defined($lookup) && exists($weight_type->{$lookup}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lookup and exists $weight_type->{$lookup}) { } 14 13 27
Condition66%2/3
defined $lookup and exists $weight_type->{$lookup}
!ll&&!rl&&r
01314
MC/DC50%1/2
defined $lookup and exists $weight_type->{$lookup}
defined $lookup exists $weight_type->{$lookup}
Truth table66%2/3
A and B
A= defined $lookupB= exists $weight_type->{$lookup}
ABresult
0X0
100
111
929 # This member has a weight
930 14 $weight = $weight_type->{$lookup};
931 } else {
932 # This member has no weight, provide an alternative weight
933 13 ▶ if ($assign_method eq 'file_average') {
Branch: 2 branches
BranchTrueFalseTotal
if ($assign_method eq 'file_average') { } 8 5 13
elsif ($assign_method eq 'community_average') { } 3 2 5
934 # Use the average weight in the weight file
935 8 $weight = $self->_file_average_weights->[$i];
936 } elsif ($assign_method eq 'community_average') {
937 # Proper weight will be assigned when community is 100% created
938 3 $weight = 0;
939 3 $self->_member_queue->{$member->id}->{$i} = $member;
940 } else {
941 # Use an arbitrary weight
942 2 $weight = $assign_method;
943 }
944 }
945  
946 }
947  
948 30 push @$weights, $weight;
949 }
950  
951 18 $member->weights($weights);
952 }
953  
954 203 return 1;
955 }
956  
957  
958 13 ▶method _process_member_queue ($community) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _process_member_queue: called CC=10 SCAR=23.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 6 6
if scalar @_ > 1 0 6 6
959 # Now is the time to add the community average weight to members that lack
960 # weight, and to add the members themselves to the community
961  
962 6 my $counts = $self->_count_queue;
963 6 my $members = $self->_member_queue;
964  
965 # Calculate average weights in community
966 6 my $community_average_weights = $community->_calc_average_weights();
967  
968 # Default to file-average weight if needed
969 6 for my $i (0 .. scalar @{$self->_weights} - 1) {
970 8 ▶ if (not defined $community_average_weights->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $community_average_weights->[$i]) 2 6 8
971 2 $community_average_weights->[$i] = $self->_file_average_weights->[$i];
972 }
973 }
974  
975 # Assign average weight to members that need it
976 6 my $weight_names = $self->weight_names;
977 6 ▶ while ( my ($id, $count) = each %$counts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$counts) 7 6 13
978 # Clone member
979 7 my $trait_num = (keys %{$members->{$id}})[0];
980 7 my $member = $members->{$id}->{$trait_num}->clone;
981 7 my $member_weights = $member->weights;
982 # Update member weights
983 7 for my $i (0 .. scalar @{$self->_weights} - 1) {
984 10 ▶ my $weight_name = $weight_names->[$i] || 'weight number '.($i+1);
Condition100%2/2
$weight_names->[$i] || 'weight number ' . ($i + 1)
l!l
73
MC/DC100%1/1
$weight_names->[$i] || 'weight number ' . ($i + 1)
$weight_names->[$i]
Truth table100%2/2
A
A= $weight_names->[$i]
Aresult
11
00
985 10 ▶ if ( $member_weights->[$i] == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member_weights->[$i] == 0) 7 3 10
986 7 $member_weights->[$i] = $community_average_weights->[$i];
987 7 $self->debug("Member '".$member->desc."' (ID ".$member->id.") got ".
988 "average $weight_name from community '".$community->name."': ".
989 $community_average_weights->[$i]."\n");
990 }
991 }
992 # Add member to community
993 7 $community->add_member($member, $count);
994 }
995  
996 # If multiple weights, update averages now
997 6 ▶ if ( scalar @{$self->_weights} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->_weights;} > 1) 2 4 6
998 2 $community_average_weights = $community->_calc_average_weights();
999 }
1000  
1001 6 $community->_set_average_weights($community_average_weights);
1002  
1003 6 return 1;
1004 }
1005  
1006  
1007 =head2 taxonomy
1008  
1009 Usage : $in->taxonomy();
1010 Function: When reading communities, try to place the community members on the
1011 provided taxonomy (provided taxonomic assignments are specified in
1012 the input. Make sure that you use the same taxonomy as in the
1013 community file to ensure that members are placed.
1014
1015 As an alternative to using a full-fledged taxonomy, if you provide a
1016 Bio::DB::Taxonomy::list object containing no taxa, the taxonomy will
1017 be constructed on the fly from the taxonomic information provided in
1018 the community file. The advantages are that you build an arbitrary
1019 taxonomy, and this taxonomy contains only the taxa present in your
1020 samples, which is fast and memory efficient. A drawback is that
1021 unfortunately, you can only do this with community file formats that
1022 report full lineages (e.g. the qiime and generic formats).
1023  
1024 A basic curation is done on the taxonomy strings, so that a GreenGenes
1025 lineage such as:
1026 k__Archaea;p__Euryarchaeota;c__Thermoplasmata;o__E2;f__Marine group II;g__;s__
1027 becomes:
1028 k__Archaea;p__Euryarchaeota;c__Thermoplasmata;o__E2;f__Marine group II
1029 Or a Silva lineage such as:
1030 Bacteria; Cyanobacteria; Chloroplast; uncultured; Other; Other
1031 becomes:
1032 Bacteria; Cyanobacteria; Chloroplast; uncultured
1033  
1034 Args : Bio::DB::Taxonomy
1035 Returns : Bio::DB::Taxonomy
1036  
1037 =cut
1038  
1039 13 has 'taxonomy' => (
1040 is => 'rw',
1041 isa => 'Maybe[Bio::DB::Taxonomy]',
1042 required => 0,
1043 lazy => 1,
1044 default => undef,
1045 init_arg => '-taxonomy',
1046 trigger => \&_is_taxonomy_empty,
1047 );
1048  
1049  
1050 13 has '_onthefly_taxonomy' => (
1051 is => 'rw',
1052 #isa => 'Bool', # keep internals light
1053 required => 0,
1054 lazy => 1,
1055 default => 0,
1056 );
1057  
1058  
1059 13 ▶method _is_taxonomy_empty ($taxonomy) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _is_taxonomy_empty: called CC=5 SCAR=16.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 8 8
if scalar @_ > 1 0 8 8
1060 # If taxonomy object is a Bio::DB::Taxonomy and contains no taxa, mark that
1061 # we'll need to build the taxonomy on the fly
1062 8 ▶ if ( (ref $taxonomy eq 'Bio::DB::Taxonomy::list') && ($taxonomy->get_num_taxa == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $taxonomy eq "Bio::DB::Taxonomy::list" and $taxonomy->get_num_taxa == 0) 6 2 8
Condition66%2/3
ref $taxonomy eq "Bio::DB::Taxonomy::list" and $taxonomy->get_num_taxa == 0
!ll&&!rl&&r
026
MC/DC50%1/2
ref $taxonomy eq "Bio::DB::Taxonomy::list" and $taxonomy->get_num_taxa == 0
ref $taxonomy eq "Bio::DB::Taxonomy::list" $taxonomy->get_num_taxa == 0
Truth table66%2/3
A and B
A= ref $taxonomy eq "Bio::DB::Taxonomy::list"B= $taxonomy->get_num_taxa == 0
ABresult
0X0
100
111
1063 6 $self->_onthefly_taxonomy(1);
1064 }
1065 8 return 1;
1066 }
1067  
1068  
1069 =head2 _attach_taxon
1070  
1071 Usage : $in->_attach_taxon($member, $taxonomy_string);
1072 Function: Once a member has been created, a driver should call this method
1073 to attach the proper taxon object to the member. If no member is
1074 provided, this method will not complain and will do nothing.
1075 Args : * a Bio::Community::Member or nothing
1076 * the taxonomic string
1077 * whether the taxonomic string is a taxon name (1) or taxon ID (0)
1078 Returns : 1 for success
1079  
1080 =cut
1081  
1082 13 ▶method _attach_taxon (Maybe[Bio::Community::Member] $member, $taxo_str, $is_name) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine _attach_taxon: called CC=15 SCAR=27.8
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 169 169
unless ($Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} ||= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]"))->check($member) 0 169 169
unless scalar @_ > 1 0 169 169
unless scalar @_ > 2 0 169 169
if scalar @_ > 3 0 169 169
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} ||= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
l!l&&r!l&&!r
16180
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} ||= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
$Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"} "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Maybe[Bio::Community::Member]"}B= "Method::Signatures"->_make_constraint("Maybe[Bio::Community::Member]")
ABresult
1X1
011
000
1083 # Given a Bio::DB::Taxonomy::list with no taxa, build a taxonomy on the fly
1084 # with the provided member. Regardless of the given taxonomy object, place
1085 # the member place the member in the taxonomy. The taxonomy is defined by
1086 # $taxo_str. If $is_name is 0, $taxo_str is used as a taxon ID. If $is_name
1087 # is 1, $taxo_str should be a taxon name. See _get_lineage_arr();
1088 169 my $taxonomy = $self->taxonomy;
1089 169 ▶ if ( defined($member) && defined($taxonomy) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $member and defined $taxonomy) 29 140 169
Condition66%2/3
defined $member and defined $taxonomy
!ll&&!rl&&r
014029
MC/DC50%1/2
defined $member and defined $taxonomy
defined $member defined $taxonomy
Truth table66%2/3
A and B
A= defined $memberB= defined $taxonomy
ABresult
0X0
100
111
1090  
1091 # First do some lineage curation
1092 29 my @names;
1093 29 ▶ if ($is_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_name) 29 0 29
1094 29 @names = @{split_lineage_string($taxo_str)};
1095 }
1096  
1097 # Then add lineage to taxonomy if desired
1098 29 ▶ if ( $self->_onthefly_taxonomy && scalar @names > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_onthefly_taxonomy and scalar @names > 0) 19 10 29
Condition100%3/3
$self->_onthefly_taxonomy and scalar @names > 0
!ll&&!rl&&r
7319
MC/DC100%2/2
$self->_onthefly_taxonomy and scalar @names > 0
$self->_onthefly_taxonomy scalar @names > 0
Truth table100%3/3
A and B
A= $self->_onthefly_taxonomyB= scalar @names > 0
ABresult
0X0
100
111
1099 # Adding the same lineage multiple times is not an issue...
1100 19 $taxonomy->add_lineage( -names => \@names );
1101 }
1102  
1103 # Then find where the member belong in the taxonomy
1104 29 my $taxon;
1105 29 ▶ if ($is_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_name) { } 29 0 29
1106 # By taxon name
1107 29 ▶ if (scalar @names > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @names > 0) 25 4 29
1108 25 $taxon = $self->taxonomy->get_taxon( -names => \@names );
1109 }# else {
1110 # $self->warn("Could not place '$taxo_str' in the given taxonomy");
1111 #}
1112 } else {
1113 # By taxon ID
1114 0 $taxon = $self->taxonomy->get_taxon( -taxonid => $taxo_str );
1115 }
1116  
1117 # Finally, if member could be placed, update its taxon information
1118 29 ▶ if ($taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon) 25 4 29
1119 25 $member->taxon($taxon);
1120 }
1121 }
1122 169 return 1;
1123 }
1124  
1125  
1126 # Do not inline so that new() can be overridden
1127 13 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
1128  
1129 13 1;
« blib/lib/Bio/Community/IO/FormatGuesser.pm ↑ Summary blib/lib/Bio/Community/Role/Table.pm »