blib/lib/Bio/Community/IO/Driver/unifrac.pm

filter: Statementstmt 98.3% 173 / 176 Branchbran 66.2% 53 / 80 Conditioncond 51.8% 14 / 27 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 93.5% 29 / 31 Podpod n/a totaltotal 81.9% 272 / 332 CC 52 SCAR 19.5
CC
52
Cov
85%
write_member
18.7
_write_metacommunity_finish
7.8
next_member
7.0
CRAP 7.1
« blib/lib/Bio/Community/Role/Table.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/biom.pm »
1 # BioPerl module for Bio::Community::IO::Driver::unifrac
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::Driver::unifrac - Driver to read and write files in the (Fast)Unifrac format
13  
14 =head1 SYNOPSIS
15  
16 my $in = Bio::Community::IO->new( -file => 'unifrac_commmunities.txt', -format => 'unifrac' );
17  
18 # See Bio::Community::IO for more information
19  
20 =head1 DESCRIPTION
21  
22 This Bio::Community::IO::Driver::unifrac driver reads and writes Unifrac environment
23 files and FastUnifrac sample ID mapping files, whose format is described at
24 L<http://bmf2.colorado.edu/unifrac/help.psp#env_file> and
25 L<http://bmf2.colorado.edu/fastunifrac/help.psp#sample_id_mapping_file>. In this
26 tab-delimited format, the first column is a sequence ID, the second is the name
27 of a community, and the optional third column contains the number of
28 observations of this sequence in the community. Multiple communities can be
29 written in a Unifrac formatted-file and spaces are not supported in community
30 name or member description. Example:
31  
32 Sequence.1 Sample.1 1
33 Sequence.1 Sample.2 2
34 Sequence.2 Sample.1 15
35 Sequence.3 Sample.1 2
36 Sequence.4 Sample.2 8
37 Sequence.5 Sample.1 4
38 Sequence.6 Sample.3 1
39 Sequence.6 Sample.2 1
40  
41 For each Bio::Community::Member $member generated from a Unifrac file,
42 $member->desc() contains the content of the first field, i.e. the first column.
43 Since the Unifrac format does not specify a member ID, one is automatically
44 generated and can be retrieved using $member->id().
45  
46 Note that member counts (the third column) is optional. Example:
47  
48 Sequence.1 Sample.1
49 Sequence.1 Sample.2
50 Sequence.2 Sample.1
51 Sequence.3 Sample.1
52 Sequence.4 Sample.2
53 Sequence.5 Sample.1
54 Sequence.6 Sample.3
55 Sequence.6 Sample.2
56  
57 In this case the data is to be interpreted as presence/absence data. When
58 reading a Unifrac file without counts, all members are given a count of 1.
59 Conversely, when writing a Unifrac file, if all members have a count of 1, then
60 the third column is not written. Also, when writing Unifrac files, any spaces in
61 community member name or member description is replaced by a dot.
62  
63 =head1 CONSTRUCTOR
64  
65 See L<Bio::Community::IO>.
66  
67 =head1 AUTHOR
68  
69 Florent Angly L<florent.angly@gmail.com>
70  
71 =head1 SUPPORT AND BUGS
72  
73 User feedback is an integral part of the evolution of this and other Bioperl
74 modules. Please direct usage questions or support issues to the mailing list,
75 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
76 experienced and reponsive experts will be able look at the problem and quickly
77 address it. Please include a thorough description of the problem with code and
78 data examples if at all possible.
79  
80 If you have found a bug, please report it on the BioPerl bug tracking system
81 to help us keep track the bugs and their resolution:
82 L<https://redmine.open-bio.org/projects/bioperl/>
83  
84 =head1 COPYRIGHT
85  
86 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
87  
88 This library is free software; you can redistribute it and/or modify
89 it under the same terms as Perl itself, either Perl version 5.10.1 or,
90 at your option, any later version of Perl 5 you may have available.
91  
92 =cut
93  
94  
95 package Bio::Community::IO::Driver::unifrac;
96  
97 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use Bio::Community::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 1 extends 'Bio::Community::IO';
103 1 with 'Bio::Community::Role::IO',
104 'Bio::Community::Role::Table';
105  
106  
107 1 our $multiple_communities = 1; # format supports several communities per file
108 1 our $explicit_ids = 0; # IDs are not explicitly recorded
109 #### sorting only effective for first community???
110 1 our $default_sort_members = 0; # unsorted
111 1 our $default_abundance_type = 'count'; # absolute count (positive integer)
112 1 our $default_missing_string = 0; # empty members get a '0'
113  
114  
115 1 has '_line' => (
116 is => 'rw',
117 isa => 'PositiveInt',
118 required => 0,
119 init_arg => undef,
120 default => 0,
121 lazy => 1,
122 );
123  
124  
125 1 has '_col' => (
126 is => 'rw',
127 isa => 'PositiveInt',
128 required => 0,
129 init_arg => undef,
130 default => 2,
131 lazy => 1,
132 );
133  
134  
135 has '_members' => (
136 is => 'rw',
137 isa => 'HashRef', # HashRef[Bio::Community::Member] but keep it lean
138 required => 0,
139 init_arg => undef,
140 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 1 lazy => 1,
142 predicate => '_has_members',
143 );
144  
145  
146 1 has '_current_name' => (
147 is => 'rw',
148 isa => 'Str',
149 required => 0,
150 init_arg => undef,
151 default => undef,
152 lazy => 1,
153 );
154  
155  
156 has '_community_names' => (
157 is => 'rw',
158 isa => 'ArrayRef', # Arrayref[Str] but keep it lean
159 required => 0,
160 init_arg => undef,
161 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 1 lazy => 1,
163 predicate => '_has_community_names',
164 );
165  
166  
167 has '_desc2line' => (
168 is => 'rw',
169 isa => 'HashRef', # HashRef[String] but keep it lean
170 required => 0,
171 init_arg => undef,
172 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
173 1 lazy => 1,
174 );
175  
176  
177 1 has '_qualitative_unifrac' => (
178 is => 'rw',
179 isa => 'Bool', # automatically detect if it's qualitative Unifrac
180 required => 0,
181 init_arg => undef,
182 default => 1,
183 lazy => 1,
184 );
185  
186  
187 1 has '_write_desc' => (
188 is => 'rw',
189 isa => 'Bool',
190 required => 0,
191 init_arg => undef,
192 default => undef,
193 lazy => 1,
194 predicate => '_has_write_desc',
195 );
196  
197  
198 1 ▶method _generate_community_names () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _generate_community_names: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
199 # Read all possible community names from the second column
200 7 my %names;
201 my @names;
202 7 my $col = 2;
203 7 for my $line (1 .. $self->_get_max_line) {
204 60 my $name = $self->_get_value($line, $col);
205 60 ▶ next if exists $names{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $names{$name} 39 21 60
206 21 $names{$name} = undef;
207 21 push @names, $name;
208 }
209 7 $self->_community_names(\@names);
210 7 return 1;
211 }
212  
213  
214 1 ▶method _generate_members () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _generate_members: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
215 # Make members from the first column
216 7 my %members;
217 7 my $col = 1;
218 7 for my $line (1 .. $self->_get_max_line) {
219 60 my $desc = $self->_get_value($line, $col);
220 60 my $member = Bio::Community::Member->new( -desc => $desc );
221 # Skip if member with same desc was already generated
222 60 ▶ next if exists $members{$desc};
Branch: 1 branch
BranchTrueFalseTotal
if exists $members{$desc} 18 42 60
223 42 $self->_attach_taxon($member, $desc, 1);
224 42 $self->_attach_weights($member);
225 42 $members{$desc} = $member;
226 }
227 7 $self->_members(\%members);
228 7 return 1;
229 }
230  
231  
232 1 ▶method next_member () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_member: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 79 79
233 79 my ($member, $count);
234 79 my $line = $self->_line;
235  
236 79 while ( 1 ) {
237 201 $line++;
238  
239 # Get the community that this member belongs to (undef if out-of-bounds)
240 201 my $name = $self->_get_value($line, $self->_col);
241  
242 # No more members for this community.
243 201 ▶ last if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 21 180 201
244  
245 # Skip members if it does not belong to wanted community
246 180 ▶ next unless $name eq $self->_current_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq $self->_current_name 120 60 180
247  
248 # Fetch member count
249 60 ▶ if ($self->_get_max_col == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_max_col == 3) { } 44 16 60
250 # Quantitative data
251 44 $count = $self->_get_value($line, $self->_col + 1);
252 } else {
253 # Presence/absence data
254 16 $count = 1;
255 }
256  
257 # Skip members with no abundance
258 60 ▶ next if not $count; # e.g. ''
Branch: 1 branch
BranchTrueFalseTotal
unless $count 2 58 60
259 58 ▶ next if $count == 0; # e.g. 0.0
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 58 58
260  
261 # Get the member itself
262 58 my $desc = $self->_get_value($line, $self->_col - 1);
263 58 $member = $self->_members->{$desc};
264 58 $self->_line($line);
265 58 last;
266 }
267 79 return $member, $count;
268 }
269  
270  
271 1 ▶method _next_community_init () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_community_init: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 27 27
272 # Go to start of next column and return name of new community
273 27 my $name = shift @{$self->_community_names};
274 27 ▶ $self->_current_name($name) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 21 6 27
275 27 $self->_line(0);
276 27 return $name;
277 }
278  
279  
280 1 ▶method _next_community_finish () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_community_finish: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 21 21
281 21 return 1;
282 }
283  
284  
285 1 ▶method _next_metacommunity_init () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_metacommunity_init: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
286 # Generate all community members and read all community names
287 7 $self->_generate_community_names();
288 7 $self->_generate_members();
289 7 my $name = ''; # no provision for metacommunity name in this format
290 7 return $name;
291 }
292  
293  
294 1 ▶method _next_metacommunity_finish () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_metacommunity_finish: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
295 7 return 1;
296 }
297  
298  
299 1 ▶method write_member (Bio::Community::Member $member, Count $count) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine write_member: called CC=17 SCAR=29.3
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 26 26
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 26 26
unless scalar @_ > 1 0 26 26
unless ($Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count"))->check($count) 0 26 26
if scalar @_ > 2 0 26 26
Condition66%4/6
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
2510
$Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count")
l!l&&r!l&&!r
2510
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 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
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Count"}B= "Method::Signatures"->_make_constraint("Count")
ABresult
1X1
011
000
300  
301 # Determine whether to write desc or id for all members
302 26 ▶ if (not $self->_has_write_desc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_write_desc) 3 23 26
303 3 ▶ if ( (defined $member->desc) && (not $member->desc eq '') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $member->desc and not $member->desc eq '') { } 3 0 3
Condition33%1/3
defined $member->desc and not $member->desc eq ""
!ll&&!rl&&r
003
MC/DC0%0/2
defined $member->desc and not $member->desc eq ""
defined $member->desc not $member->desc eq ""
Truth table33%1/3
A and not(B)
A= defined $member->descB= not $member->desc eq ""
ABresult
0X0
100
111
304 3 $self->_write_desc(1);
305 } else {
306 0 $self->_write_desc(0);
307 }
308 }
309  
310 # Replace spaces by dot in description
311 26 ▶ my $desc = $self->_write_desc ? $member->desc : $member->id;
Branch: 1 branch
BranchTrueFalseTotal
$self->_write_desc ? : 26 0 26
312 26 $desc =~ s/ /./g;
313  
314 26 my $desc2line = $self->_desc2line;
315 26 my $line = $desc2line->{$desc};
316  
317 26 my $values = [$desc, $self->_current_name, $count];
318  
319 26 ▶ if (not defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line) { } 18 8 26
320  
321 # This member has not been written previously for another community
322 # append to table
323 18 $line = $self->_get_max_line + 1;
324 18 $self->_insert_line($line, $values);
325 18 $desc2line->{$desc} = $line;
326  
327 } else {
328  
329 # Member already seen. Insert it in table next to where this member is
330 # located for other communities. Not sure that this is required by the
331 # Unifrac format, but this is safer this way.
332 8 $self->_insert_line($line+1, $values);
333 8 ▶ while (my ($memdesc, $memline) = each %$desc2line) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$desc2line) 48 8 56
334 48 ▶ if ($memline >= $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($memline >= $line) 28 20 48
335 # Increment line numbers
336 28 $desc2line->{$memdesc}++;
337 }
338 }
339  
340 }
341  
342 26 ▶ if ($self->_qualitative_unifrac && $count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_qualitative_unifrac and $count > 1) 2 24 26
Condition100%3/3
$self->_qualitative_unifrac and $count > 1
!ll&&!rl&&r
1682
MC/DC100%2/2
$self->_qualitative_unifrac and $count > 1
$self->_qualitative_unifrac $count > 1
Truth table100%3/3
A and B
A= $self->_qualitative_unifracB= $count > 1
ABresult
0X0
100
111
343 2 $self->_qualitative_unifrac( 0 );
344 }
345  
346 26 $self->_line( $line + 1 );
347  
348 26 return 1;
349 }
350  
351  
352 1 ▶method _write_community_init (Bio::Community $community) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_community_init: called CC=5 SCAR=16.8
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 9 9
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 9 9
if scalar @_ > 1 0 9 9
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
900
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
353 # Replace spaces by dot in community name
354 9 my $name = $community->name;
355 9 $name =~ s/ /./g;
356 9 $self->_current_name( $name );
357 9 return 1;
358 }
359  
360  
361 1 ▶method _write_community_finish (Bio::Community $community) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_community_finish: called CC=5 SCAR=17.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 9 9
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 9 9
if scalar @_ > 1 0 9 9
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
900
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
362 9 return 1;
363 }
364  
365  
366 1 ▶method _write_metacommunity_init (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_metacommunity_init: called CC=5 SCAR=17.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 3 3
if scalar @_ > 1 0 3 3
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
300
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 table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"}B= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
ABresult
1X1
011
000
367 3 return 1;
368 }
369  
370  
371 1 ▶method _write_metacommunity_finish (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_metacommunity_finish: called CC=7 SCAR=20.5
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 3 3
if scalar @_ > 1 0 3 3
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
300
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 table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"}B= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
ABresult
1X1
011
000
372 3 ▶ if ( ($self->_get_max_col == 3) && $self->_qualitative_unifrac ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_max_col == 3 and $self->_qualitative_unifrac) 1 2 3
Condition66%2/3
$self->_get_max_col == 3 and $self->_qualitative_unifrac
!ll&&!rl&&r
021
MC/DC50%1/2
$self->_get_max_col == 3 and $self->_qualitative_unifrac
$self->_get_max_col == 3 $self->_qualitative_unifrac
Truth table66%2/3
A and B
A= $self->_get_max_col == 3B= $self->_qualitative_unifrac
ABresult
0X0
100
111
373 # Delete last column (the counts)
374 1 $self->_delete_col(3);
375 }
376 3 return 1;
377 }
378  
379  
380  
381 1 __PACKAGE__->meta->make_immutable;
382  
383 1 1;
« blib/lib/Bio/Community/Role/Table.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/biom.pm »