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

filter: Statementstmt 97.8% 324 / 331 Branchbran 67.5% 81 / 120 Conditioncond 58.3% 14 / 24 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 95.6% 44 / 46 Podpod n/a totaltotal 86.9% 467 / 537 CC 77 SCAR 18.2
CC
77
Cov
88%
write_member
14.3
_validate_biom
12.0
next_member
9.0
CRAP 6.2
« blib/lib/Bio/Community/IO/Driver/unifrac.pm ↑ Summary blib/lib/Bio/Community/Types.pm »
1 # BioPerl module for Bio::Community::IO::Driver::biom
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::biom - Driver to read and write files in the sparse BIOM format
13  
14 =head1 SYNOPSIS
15  
16 # Reading
17 my $in = Bio::Community::IO->new(
18 -file => 'biom_communities.txt',
19 -format => 'biom'
20 );
21 my $type = $in->get_matrix_type; # either dense or sparse
22  
23 # Writing
24 my $out = Bio::Community::IO->new(
25 -file => 'biom_communities.txt',
26 -format => 'biom',
27 -matrix_type => 'sparse', # default matrix type
28 );
29  
30 # See Bio::Community::IO for more information
31  
32 =head1 DESCRIPTION
33  
34 This Bio::Community::IO::Driver::biom driver reads and writes files in the BIOM format
35 version 1.0 described at L<http://biom-format.org/documentation/format_versions/biom-1.0.html>.
36 Multiple communities and additional metadata can be recorded in a BIOM file.
37 Here is an example of minimal sparse BIOM file:
38  
39 {
40 "id":null,
41 "format": "Biological Observation Matrix 0.9.1-dev",
42 "format_url": "http://biom-format.org",
43 "type": "OTU table",
44 "generated_by": "QIIME revision 1.4.0-dev",
45 "date": "2011-12-19T19:00:00",
46 "rows":[
47 {"id":"GG_OTU_1", "metadata":null},
48 {"id":"GG_OTU_2", "metadata":null},
49 {"id":"GG_OTU_3", "metadata":null},
50 {"id":"GG_OTU_4", "metadata":null},
51 {"id":"GG_OTU_5", "metadata":null}
52 ],
53 "columns": [
54 {"id":"Sample1", "metadata":null},
55 {"id":"Sample2", "metadata":null},
56 {"id":"Sample3", "metadata":null},
57 {"id":"Sample4", "metadata":null},
58 {"id":"Sample5", "metadata":null},
59 {"id":"Sample6", "metadata":null}
60 ],
61 "matrix_type": "sparse",
62 "matrix_element_type": "int",
63 "shape": [5, 6],
64 "data":[[0,2,1],
65 [1,0,5],
66 [1,1,1],
67 [1,3,2],
68 [1,4,3],
69 [1,5,1],
70 [2,2,1],
71 [2,3,4],
72 [2,4,2],
73 [3,0,2],
74 [3,1,1],
75 [3,2,1],
76 [3,5,1],
77 [4,1,1],
78 [4,2,1]
79 ]
80 }
81  
82 Columns (i.e. communities) can be expressed in a richer way, e.g.:
83  
84 {"id":"Sample1", "metadata":{
85 "BarcodeSequence":"CGCTTATCGAGA",
86 "LinkerPrimerSequence":"CATGCTGCCTCCCGTAGGAGT",
87 "BODY_SITE":"gut",
88 "Description":"human gut"}},
89  
90 The 'id' can be recovered from the name() method of the resulting Bio::Community.
91 Metadata fields are not recorded at this time, but will be in a future release.
92  
93 Rows (i.e. community members) can also be expressed in a richer form:
94  
95 {"id":"GG_OTU_1", "metadata":{"taxonomy":["k__Bacteria", "p__Proteobacteria", "c__Gammaproteobacteria", "o__Enterobacteriales", "f__Enterobacteriaceae", "g__Escherichia", "s__"]}},
96  
97 For each Bio::Community::Member generated, the id() method contains the 'id' and
98 desc() holds a concatenated version of the 'taxonomy' field. Note that you can
99 omit members entirely from a biom file and simply have community names and
100 metadata.
101  
102 The 'comment' field of biom files is not recorded and simply ignored.
103  
104 =head1 CONSTRUCTOR
105  
106 See L<Bio::Community::IO>.
107  
108 =head1 AUTHOR
109  
110 Florent Angly L<florent.angly@gmail.com>
111  
112 =head1 SUPPORT AND BUGS
113  
114 User feedback is an integral part of the evolution of this and other Bioperl
115 modules. Please direct usage questions or support issues to the mailing list,
116 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
117 experienced and reponsive experts will be able look at the problem and quickly
118 address it. Please include a thorough description of the problem with code and
119 data examples if at all possible.
120  
121 If you have found a bug, please report it on the BioPerl bug tracking system
122 to help us keep track the bugs and their resolution:
123 L<https://redmine.open-bio.org/projects/bioperl/>
124  
125 =head1 COPYRIGHT
126  
127 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
128  
129 This library is free software; you can redistribute it and/or modify
130 it under the same terms as Perl itself, either Perl version 5.10.1 or,
131 at your option, any later version of Perl 5 you may have available.
132  
133 =cut
134  
135  
136 package Bio::Community::IO::Driver::biom;
137  
138 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶use Bio::Community::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶use Bio::Community::TaxonomyUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶use JSON::XS qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 1 ▶use constant BIOM_NAME => 'Biological Observation Matrix 1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶use constant BIOM_URL => 'http://biom-format.org/documentation/format_versions/biom-1.0.html';
Subroutine BEGIN: called CC=1 SCAR=0.0
148 1 ▶use constant BIOM_MATRIX_TYPE => 'sparse'; # sparse or dense
Subroutine BEGIN: called CC=1 SCAR=0.0
149 1 ▶use constant BIOM_TYPE => 'OTU table';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 # "XYZ table" where XYZ is Pathway, Gene, Function, Ortholog, Metabolite or Taxon
151  
152 1 extends 'Bio::Community::IO';
153 1 with 'Bio::Community::Role::IO';
154  
155  
156 1 our $multiple_communities = 1; # format supports several communities per file
157 1 our $explicit_ids = 1; # IDs are explicitly recorded
158 1 our $default_sort_members = 0; # unsorted
159 1 our $default_abundance_type = 'count'; # absolute count (positive integer)
160 1 our $default_missing_string = 0; # empty members get a '0'
161  
162  
163 1 has 'matrix_type' => (
164 is => 'rw',
165 isa => 'Maybe[BiomMatrixType]',
166 required => 0,
167 init_arg => '-matrix_type',
168 default => undef,
169 lazy => 1,
170 reader => 'get_matrix_type',
171 writer => 'set_matrix_type',
172 predicate => '_has_matrix_type',
173 );
174  
175  
176 1 has 'matrix_element_type' => (
177 is => 'rw',
178 #isa => 'Str', # either int, float or unicode
179 required => 0,
180 init_arg => undef,
181 default => undef,
182 lazy => 1,
183 reader => '_get_matrix_element_type',
184 writer => '_set_matrix_element_type',
185 );
186  
187  
188 1 has '_json' => (
189 is => 'rw',
190 #isa => 'JSON::XS',
191 required => 0,
192 init_arg => undef,
193 default => undef,
194 lazy => 1,
195 predicate => '_has_json',
196 reader => '_get_json',
197 writer => '_set_json',
198 );
199  
200  
201 1 has '_max_line' => (
202 is => 'rw',
203 #isa => 'StrictlyPositiveInt',
204 required => 0,
205 init_arg => undef,
206 lazy => 1,
207 default => 0,
208 reader => '_get_max_line',
209 writer => '_set_max_line',
210 );
211  
212  
213 1 has '_max_col' => (
214 is => 'rw',
215 #isa => 'StrictlyPositiveInt',
216 required => 0,
217 init_arg => undef,
218 lazy => 1,
219 default => 0,
220 reader => '_get_max_col',
221 writer => '_set_max_col',
222 );
223  
224  
225 1 has '_line' => (
226 is => 'rw',
227 isa => 'PositiveInt',
228 required => 0,
229 init_arg => undef,
230 default => 0,
231 lazy => 1,
232 reader => '_get_line',
233 writer => '_set_line',
234 );
235  
236  
237 1 has '_col' => (
238 is => 'rw',
239 isa => 'PositiveInt',
240 required => 0,
241 init_arg => undef,
242 default => 0,
243 lazy => 1,
244 reader => '_get_col',
245 writer => '_set_col',
246 );
247  
248  
249 has '_members' => (
250 is => 'rw',
251 isa => 'HashRef', # HashRef{id} = Bio::Community::Member
252 required => 0,
253 init_arg => undef,
254 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 1 lazy => 1,
256 predicate => '_has_members',
257 reader => '_get_members',
258 writer => '_set_members',
259 );
260  
261  
262 has '_sorted_members' => (
263 is => 'rw',
264 isa => 'HashRef', # HashRef{species}{sample} = count
265 required => 0,
266 init_arg => undef,
267 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 1 lazy => 1,
269 reader => '_get_sorted_members',
270 writer => '_set_sorted_members',
271 );
272  
273  
274 has '_id2line' => (
275 is => 'rw',
276 isa => 'HashRef', # HashRef[String] but keep it lean
277 required => 0,
278 init_arg => undef,
279 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
280 1 lazy => 1,
281 );
282  
283  
284 1 ▶method _next_community_init () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_community_init: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 78 78
285 # Get community name and set column number
286 78 my $col = $self->_get_col + 1;
287 78 my $name;
288 78 ▶ if ($self->_get_col <= $self->_get_max_col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_col <= $self->_get_max_col) 78 0 78
289 78 $name = $self->_get_json->{'columns'}->[$col-1]->{'id'};
290 }
291 78 $self->_set_col( $col );
292 78 return $name;
293 }
294  
295  
296 1 ▶method _parse_json () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _parse_json: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 13 13
297 # Retrieve all text content
298 13 my $str = '';
299 13 ▶ while (my $line = $self->_readline(-raw => 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_readline("-raw", 1)) 1563 13 1576
300 1563 $str .= $line;
301 }
302  
303 # Parse JSON string
304 13 my $parser = JSON::XS->new();
305 13 my $json;
306 13 eval { $json = $parser->decode($str) };
307 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
308 0 $self->throw("Biom file is not properly JSON-formatted: $@");
309 }
310 13 $self->_validate_biom($json);
311 12 $self->_set_json($json);
312  
313 # Retrieve and store some information
314 12 my ($max_line, $max_col) = @{$json->{'shape'}};
315 12 $self->_set_max_line( $max_line );
316 12 $self->_set_max_col( $max_col );
317 12 $self->set_matrix_type($json->{'matrix_type'});
318 12 $self->_set_matrix_element_type($json->{'matrix_element_type'});
319  
320 12 return $json;
321 }
322  
323  
324 1 ▶method _validate_biom ($biom) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _validate_biom: called CC=11 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 13 13
if scalar @_ > 1 0 13 13
325 # Check that the biom content has all the mandatory fields. Also do some
326 # basic validation to ensure that parsing will be successful. It is not a
327 # comprehensive validation of the file format!
328  
329 13 my $msg = 'Biom file is invalid:';
330  
331 13 my @mandatory = qw( id format format_url type generated_by date rows columns
332 matrix_type matrix_element_type shape data );
333 13 for my $field (@mandatory) {
334 156 ▶ if (not exists $biom->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $biom->{$field}) 0 156 156
335 0 $self->throw("$msg missing mandatory '$field' field");
336 }
337 }
338  
339 13 for my $field (qw(rows columns)) {
340 25 for my $subfield (qw(id metadata)) {
341 50 for my $entry (@{$biom->{$field}}) {
342 256 ▶ if (not exists $entry->{$subfield}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $entry->{$subfield}) 1 255 256
343 1 $self->throw("$msg missing mandatory '$subfield' subfield of '$field' field");
344 }
345 }
346 }
347 }
348  
349 12 my $shape_rows = $biom->{'shape'}->[0];
350 12 my $rows = scalar @{$biom->{'rows'}};
351 12 ▶ if ($shape_rows != $rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape_rows != $rows) 0 12 12
352 0 $self->throw("$msg inconsistent number of rows between 'rows' ($rows) and 'shape' ($shape_rows)");
353 }
354  
355 12 my $shape_cols = $biom->{'shape'}->[1];
356 12 my $cols = scalar @{$biom->{'columns'}};
357 12 ▶ if ($shape_cols != $cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape_cols != $cols) 0 12 12
358 0 $self->throw("$msg inconsistent number of columns between 'columns' ($cols) and 'shape' ($shape_cols)");
359 }
360  
361 ### Validate that 'data' has the right shape?
362  
363 12 return 1;
364 }
365  
366  
367 1 ▶method _generate_members () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _generate_members: called CC=6 SCAR=18.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 12 12
368 12 my %members = ();
369 12 for my $row (1 .. $self->_get_max_line) {
370 59 my $json = $self->_get_json->{'rows'}->[$row-1];
371 59 my $id = $json->{'id'};
372 59 my $member = Bio::Community::Member->new( -id => $id );
373 59 my $metadata = $json->{'metadata'};
374 59 ▶ if (exists $metadata->{'taxonomy'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $metadata->{"taxonomy"}) 40 19 59
375 40 my $taxo_desc;
376 40 ▶ if (ref($metadata->{'taxonomy'}) eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $metadata->{'taxonomy'} eq 'SCALAR') { } 0 40 40
elsif (ref $metadata->{'taxonomy'} eq 'ARRAY') { } 40 0 40
377 0 $taxo_desc = $metadata->{'taxonomy'};
378 } elsif (ref($metadata->{'taxonomy'}) eq 'ARRAY') {
379 40 $taxo_desc = get_lineage_string($metadata->{'taxonomy'}, ' ');
380 }
381 40 $member->desc( $taxo_desc );
382 40 $self->_attach_taxon($member, $taxo_desc, 1);
383 }
384 #if (exists $members{$id}) {
385 # $self->warn("Member with ID $id is present multiple times... ".
386 # "Continuing despite the perils!");
387 #}
388 59 $self->_attach_weights($member);
389 59 $members{$id} = $member;
390 }
391  
392 12 $self->_set_members(\%members);
393 12 return 1;
394 }
395  
396  
397 1 ▶method _sort_members_by_community {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _sort_members_by_community: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
398 # Sort members by community to facilitate parsing
399 7 my %sorted_members;
400 7 my $json = $self->_get_json;
401 7 my $matrix = $json->{'data'};
402 7 my $rows = $json->{'rows'};
403 7 for my $i (0 .. scalar @$matrix - 1) {
404 131 my ($row, $sample, $count) = @{$matrix->[$i]};
405 131 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 130 1 131
406 130 my $species = $rows->[$row]->{'id'};
407 130 $sorted_members{$sample}{$species} += $count; # merge duplicates
408 }
409 }
410 7 $self->_set_sorted_members(\%sorted_members);
411 7 return 1;
412 }
413  
414  
415 1 ▶method next_member () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_member: called CC=9 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 241 241
416 241 my ($id, $member, $count);
417 241 my $col = $self->_get_col;
418 241 my $members = $self->_get_members;
419 241 my $json = $self->_get_json;
420 241 ▶ if (defined $json->{'matrix_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"matrix_type"}) 229 12 241
421 229 ▶ if ($json->{'matrix_type'} eq 'sparse') { # sparse matrix format
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{'matrix_type'} eq 'sparse') { } 166 63 229
422  
423 166 my $sorted_members = $self->_get_sorted_members;
424 166 my @ids = keys %{$sorted_members->{$col-1}};
425 166 ▶ if (scalar @ids > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ids > 0) 130 36 166
426 130 $id = shift @ids;
427 130 $count = delete $sorted_members->{$col-1}->{$id};
428 130 $self->_set_sorted_members($sorted_members);
429 }
430  
431 } else { # dense matrix format
432  
433 63 my $rows = $json->{'rows'};
434 63 my $matrix = $json->{'data'};
435 63 my $line = $self->_get_line;
436 63 ▶ while ( ++$line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$line) 108 0 108
437 # Get the abundance of the member (undef if out-of-bounds)
438 108 $count = $matrix->[$line-1]->[$col-1];
439 108 ▶ if (defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count) { } 90 18 108
440 90 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 45 45 90
441 45 $id = $rows->[$line-1]->{'id'};
442 45 $self->_set_line($line);
443 45 last;
444 }
445 } else {
446 # No more members for this community
447 18 $self->_set_line(0);
448 18 last;
449 }
450 }
451 }
452 }
453 241 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 175 66 241
454 175 $member = $members->{$id};
455 }
456 241 return $member, $count;
457 }
458  
459  
460 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 66 66
461 66 return 1;
462 }
463  
464  
465 1 ▶method _next_metacommunity_init () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_metacommunity_init: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 13 13
466 13 $self->_parse_json(); # Parse the JSON string
467 12 $self->_generate_members(); # Generate all Bio::Community::Members
468 # Sort members when reading sparse matrix
469 # (if there are no species in the biom file yet, matrix type is not defined)
470 12 my $matrix_type = $self->get_matrix_type;
471 12 ▶ if ( (defined $matrix_type) && ($matrix_type eq 'sparse') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix_type and $matrix_type eq "sparse") 7 5 12
Condition100%3/3
defined $matrix_type and $matrix_type eq "sparse"
!ll&&!rl&&r
237
MC/DC100%2/2
defined $matrix_type and $matrix_type eq "sparse"
defined $matrix_type $matrix_type eq "sparse"
Truth table100%3/3
A and B
A= defined $matrix_typeB= $matrix_type eq "sparse"
ABresult
0X0
100
111
472 7 $self->_sort_members_by_community();
473 }
474 12 my $name = $self->_get_json->{'id'};
475 12 return $name;
476 }
477  
478  
479 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 12 12
480 12 return 1;
481 }
482  
483  
484 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.7
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 33 33
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 33 33
if scalar @_ > 1 0 33 33
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
3300
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
485 # Write community information
486 33 my $json = $self->_get_json;
487 33 push @{$json->{'columns'}}, { 'id' => $community->name, 'metadata' => undef };
488 33 $self->_set_json($json);
489 33 $self->_set_col( $self->_get_col + 1);
490 33 return 1;
491 }
492  
493  
494 1 ▶method _write_headers ($name) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _write_headers: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 6 6
if scalar @_ > 1 0 6 6
495 6 my $json = {};
496 # Write some generic information
497 6 $json->{'id'} = $name;
498 6 $json->{'format'} = BIOM_NAME;
499 6 $json->{'format_url'} = BIOM_URL;
500 6 $json->{'type'} = BIOM_TYPE;
501 6 $json->{'generated_by'} = 'Bio::Community version '.$Bio::Community::VERSION;
502 6 $json->{'date'} = DateTime->now->datetime; # ISO 8601, e.g. 2011-12-19T19:00:00
503 6 $json->{'matrix_type'} = $self->get_matrix_type;
504 # Also create a couple of mandatory fields (but leave them empty)
505 6 $json->{'rows'} = undef;
506 6 $json->{'columns'} = undef;
507 6 $json->{'data'} = undef;
508 6 $self->_set_json($json);
509 6 return 1;
510 }
511  
512  
513 1 ▶method write_member (Bio::Community::Member $member, Count $count) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine write_member: called CC=14 SCAR=26.6
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 86 86
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 86 86
unless scalar @_ > 1 0 86 86
unless ($Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count"))->check($count) 0 86 86
if scalar @_ > 2 0 86 86
Condition66%4/6
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
8510
$Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count")
l!l&&r!l&&!r
8510
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
514 86 my $json = $self->_get_json;
515 86 my $id = $member->id;
516  
517 # Check if count is integer or float
518 86 ▶ if (not defined $self->_get_matrix_element_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_get_matrix_element_type) 5 81 86
519 # assume integer until proven otherwise
520 5 $self->_set_matrix_element_type('int');
521 }
522 86 ▶ if ($self->_get_matrix_element_type eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_matrix_element_type eq "int") 58 28 86
523 58 ▶ if ($count =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count =~ /\D/) 1 57 58
524 # Count has at least one non-digit character
525 1 $self->_set_matrix_element_type('float');
526 }
527 }
528  
529 # Update 'rows' records if needed
530 86 my $line = $self->_id2line->{$id};
531 86 ▶ if (not defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 29 57 86
532 # This member has not been written previously for another community
533 29 $line = $self->_get_line + 1;
534 29 my $member_rec = { 'id' => $id, 'metadata' => undef };
535 29 my $desc = $member->desc;
536 29 ▶ if (not $desc eq '') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($desc eq "") 20 9 29
537 20 my $taxonomy = Bio::Community::TaxonomyUtils::split_lineage_string($desc, 0);
538 20 $member_rec->{'metadata'}->{'taxonomy'} = $taxonomy;
539 }
540 29 push @{$json->{'rows'}}, $member_rec;
541 29 $self->_id2line->{$id} = $line;
542 29 $self->_set_line( $line );
543 }
544  
545 # Update 'data' records: use +0 to make counts be used as numbers (not strings)
546 86 my $col = $self->_get_col;
547 86 ▶ if ($self->get_matrix_type eq 'sparse') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_matrix_type eq 'sparse') { } 71 15 86
548 71 push @{$json->{'data'}}, [$line-1, $col-1, $count+0];
549 } else {
550 15 $json->{'data'}->[$line-1]->[$col-1] = $count+0;
551 }
552 86 $self->_set_json($json);
553  
554 86 return 1;
555 }
556  
557  
558 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 33 33
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community"))->check($community) 0 33 33
if scalar @_ > 1 0 33 33
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community"} ||= "Method::Signatures"->_make_constraint("Bio::Community")
l!l&&r!l&&!r
3300
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
559 33 return 1;
560 }
561  
562  
563 1 ▶method _write_metacommunity_init (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_metacommunity_init: called CC=7 SCAR=20.2
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 6 6
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 6 6
if scalar @_ > 1 0 6 6
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
600
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
564 # Set default matrix type to sparse
565 6 ▶ if (not $self->_has_matrix_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_matrix_type) 1 5 6
566 1 $self->set_matrix_type('sparse');
567 }
568 # Write some generic header information
569 6 my $name;
570 6 ▶ if (defined $meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta) 6 0 6
571 6 $name = $meta->name;
572 }
573 6 $self->_write_headers($name);
574 6 return 1;
575 }
576  
577  
578 1 ▶method _write_metacommunity_finish (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_metacommunity_finish: called CC=8 SCAR=21.0
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 6 6
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta"))->check($meta) 0 6 6
if scalar @_ > 1 0 6 6
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Bio::Community::Meta"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Meta")
l!l&&r!l&&!r
600
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
579 # Write JSON to file
580 6 my $rows = $self->_get_line;
581 6 my $cols = $self->_get_col;
582 6 ▶ if ($rows == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows == 0) 1 5 6
583 1 $self->_set_matrix_element_type( undef );
584 1 $self->set_matrix_type( undef );
585 }
586 6 my $json = $self->_get_json;
587 6 $json->{'shape'} = [$rows+0, $cols+0];
588 6 $json->{'matrix_element_type'} = $self->_get_matrix_element_type;
589 6 $json->{'matrix_type'} = $self->get_matrix_type;
590 6 ▶ if ((defined $self->get_matrix_type) && ($self->get_matrix_type eq 'dense')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_matrix_type and $self->get_matrix_type eq "dense") 1 5 6
Condition100%3/3
defined $self->get_matrix_type and $self->get_matrix_type eq "dense"
!ll&&!rl&&r
141
MC/DC100%2/2
defined $self->get_matrix_type and $self->get_matrix_type eq "dense"
defined $self->get_matrix_type $self->get_matrix_type eq "dense"
Truth table100%3/3
A and B
A= defined $self->get_matrix_typeB= $self->get_matrix_type eq "dense"
ABresult
0X0
100
111
591 1 $self->_fill_missing();
592 }
593 6 my $writer = JSON::XS->new->pretty;
594 6 my $str = $writer->encode($json);
595 6 $self->_print($str);
596 6 return 1;
597 }
598  
599  
600 1 ▶method _fill_missing () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _fill_missing: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
601 1 my $json = $self->_get_json;
602 1 my $data = $json->{'data'};
603 1 my $max_col = $self->_get_col - 1;
604 1 for my $row (0 .. scalar @$data - 1) {
605 5 my $row_data = $data->[$row];
606 5 for my $col (0 .. $max_col) {
607 30 ▶ if (not defined $row_data->[$col]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row_data->[$col]) 15 15 30
608 15 $row_data->[$col] = $default_missing_string;
609 }
610 }
611 }
612 1 $self->_set_json($json);
613 1 return 1;
614 }
615  
616  
617  
618 1 __PACKAGE__->meta->make_immutable;
619  
620 1 1;
« blib/lib/Bio/Community/IO/Driver/unifrac.pm ↑ Summary blib/lib/Bio/Community/Types.pm »