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

filter: Statementstmt 98.4% 196 / 199 Branchbran 62.5% 55 / 88 Conditioncond 52.3% 11 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 97.0% 33 / 34 Podpod n/a totaltotal 83.4% 297 / 356 CC 53 SCAR 16.1
CC
53
Cov
85%
write_member
10.5
_generate_members
8.2
_write_metacommunity_finish
7.3
CRAP 5.0
« blib/lib/Bio/Community/Meta/Beta.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/gaas.pm »
1 # BioPerl module for Bio::Community::IO::Driver::qiime
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::qiime - Driver to read and write files in the QIIME format
13  
14 =head1 SYNOPSIS
15  
16 my $in = Bio::Community::IO->new( -file => 'qiime_communities.txt', -format => 'qiime' );
17  
18 # See Bio::Community::IO for more information
19  
20 =head1 DESCRIPTION
21  
22 This Bio::Community::IO::Driver::qiime driver reads and writes files generated by QIIME
23 (L<http://qiime.org/>). Multiple communities can be recorded in a tab-delimited
24 file. Here is an example of QIIME OTU table file:
25  
26 # QIIME v1.3.0 OTU table
27 #OTU ID soil marine freshwater
28 0 3 11 0
29 1 10 24 0
30 2 0 230 110
31 3 0 30 80
32  
33 Note that the first line is an arbitrary comment line, which can be used to
34 specify a metacommunity name. The second line contains column names. Here is
35 the same OTU table with a given metacommunity name (the first line) and
36 assignments to the GreenGenes taxonomy:
37  
38 # biome_comparison_2013
39 #OTU ID soil marine freshwater Consensus Lineage
40 0 3 11 0 k__Bacteria;p__Cyanobacteria;c__;o__Chroococcales;f__;g__Synechococcus;s__
41 1 10 24 0 k__Bacteria;p__TM6;c__;o__;f__;g__;s__
42 2 0 230 110 k__Bacteria;p__Cyanobacteria;c__;o__Oscillatoriales;f__;g__Trichodesmium;s__Trichodesmium erythraeum
43 3 0 30 80 k__Bacteria;p__Acidobacteria;c__Solibacteres;o__Solibacterales;f__Solibacteraceae;g__Candidatus Solibacter;s__
44  
45 To be recognized, the last column must match the pattern *lineage* or *taxon*.
46  
47 For each Bio::Community::Member $member generated from a QIIME file, $member->id()
48 contains the OTU ID, while $member->desc() holds the content of the consensus
49 lineage field.
50  
51 B<Note>: QIIME also provides OTU tables summarized at the different taxonomic levels,
52 with relative abundance instead of counts:
53  
54 Taxon soil marine freshwater
55 k__Bacteria;p__Acidobacteria 0.0 0.1016949153 0.4210526316
56 k__Bacteria;p__Cyanobacteria 0.2307692308 0.8169491525 0.5789473684
57 k__Bacteria;p__TM6 0.7692307692 0.0813559322 0.0
58  
59 These tables have to be read and written using the Bio::Community::IO::Driver::generic
60 module, B<not> with Bio::Community::IO::Driver::qiime.
61  
62 =head1 CONSTRUCTOR
63  
64 See L<Bio::Community::IO>.
65  
66 =head1 AUTHOR
67  
68 Florent Angly L<florent.angly@gmail.com>
69  
70 =head1 SUPPORT AND BUGS
71  
72 User feedback is an integral part of the evolution of this and other Bioperl
73 modules. Please direct usage questions or support issues to the mailing list,
74 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
75 experienced and reponsive experts will be able look at the problem and quickly
76 address it. Please include a thorough description of the problem with code and
77 data examples if at all possible.
78  
79 If you have found a bug, please report it on the BioPerl bug tracking system
80 to help us keep track the bugs and their resolution:
81 L<https://redmine.open-bio.org/projects/bioperl/>
82  
83 =head1 COPYRIGHT
84  
85 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
86  
87 This library is free software; you can redistribute it and/or modify
88 it under the same terms as Perl itself, either Perl version 5.10.1 or,
89 at your option, any later version of Perl 5 you may have available.
90  
91 =cut
92  
93  
94 package Bio::Community::IO::Driver::qiime;
95  
96 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use Bio::Community::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 4 extends 'Bio::Community::IO';
102 4 with 'Bio::Community::Role::IO',
103 'Bio::Community::Role::Table';
104  
105  
106 4 our $multiple_communities = 1; # format supports several communities per file
107 4 our $explicit_ids = 1; # IDs are explicitly recorded
108 #### sorting only effective for first community???
109 4 our $default_sort_members = 0; # unsorted
110 4 our $default_abundance_type = 'count'; # absolute count (positive integer)
111 4 our $default_missing_string = 0; # empty members get a '0'
112  
113  
114 18 ▶around BUILDARGS => func ($orig, $class, %args) {
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 18 18
unless scalar @_ > 1 0 18 18
115 18 $args{-start_line} = 2;
116 18 return $class->$orig(%args);
117 4 };
118  
119  
120 4 has '_line' => (
121 is => 'rw',
122 isa => 'PositiveInt',
123 required => 0,
124 init_arg => undef,
125 default => 1,
126 lazy => 1,
127 );
128  
129  
130 4 has '_col' => (
131 is => 'rw',
132 isa => 'PositiveInt',
133 required => 0,
134 init_arg => undef,
135 default => 1,
136 lazy => 1,
137 );
138  
139  
140 4 has '_skip_last_col' => (
141 is => 'rw',
142 isa => 'Bool',
143 required => 0,
144 init_arg => undef,
145 default => 0,
146 lazy => 1,
147 );
148  
149  
150 has '_members' => (
151 is => 'rw',
152 isa => 'ArrayRef', # ArrayRef[Bio::Community::Member] but keep it lean
153 required => 0,
154 init_arg => undef,
155 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 4 lazy => 1,
157 predicate => '_has_members',
158 );
159  
160  
161 has '_id2line' => (
162 is => 'rw',
163 isa => 'HashRef', # HashRef[String] but keep it lean
164 required => 0,
165 init_arg => undef,
166 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
167 4 lazy => 1,
168 );
169  
170  
171 has '_line2desc' => (
172 is => 'rw',
173 #isa => 'HashRef', # HashRef[PositiveInt] but keep it lean
174 required => 0,
175 init_arg => undef,
176 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
177 4 lazy => 1,
178 );
179  
180  
181 4 ▶method _generate_members () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _generate_members: called CC=8 SCAR=21.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 14 14
182 # Make members from the first column. Also, find out if they have a taxonomy.
183  
184 # Does the last column contain the taxonomy?
185 14 my $first_col_header = $self->_get_value(1, 1);
186 14 my $taxo_col;
187 14 ▶ if ($first_col_header =~ m/otu\s*id/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_col_header =~ /otu\s*id/i) { } 14 0 14
188 14 my $last_col_header = $self->_get_value(1, $self->_get_max_col);
189 14 ▶ if (defined $last_col_header) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_col_header) 14 0 14
190 14 ▶ if ($last_col_header eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($last_col_header eq '') { } 0 14 14
elsif ($last_col_header =~ /(?:lineage|taxon)/i) { } 9 5 14
191 0 $self->throw("Parsing error. It looks like the last column of the ".
192 "QIIME OTU table might be empty.\n");
193 } elsif ($last_col_header =~ m/(?:lineage|taxon)/i) {
194 # Will match 'Consensus lineage', 'taxonomy', etc
195 9 $taxo_col = $self->_get_max_col;
196 9 $self->_skip_last_col(1);
197 }
198 }
199 } else {
200 0 $self->warn("Could not recognize the headers of the QIIME OTU table, but ".
201 "assuming that a valid table was provided\n");
202 }
203  
204 # What are the members?
205 14 my @members;
206 14 my $col = 1;
207 14 my $line = 1; # first line of the table is a header
208 14 for my $line (2 .. $self->_get_max_line) {
209 # Get OTU ID if possible
210 46 my $otu_id = $self->_get_value($line, $col);
211 46 my $member = Bio::Community::Member->new( -id => $otu_id );
212 # Get taxonomic assignment if possible
213 46 ▶ if (defined $taxo_col) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taxo_col) 31 15 46
214 31 my $taxo_desc = $self->_get_value($line, $taxo_col);
215 31 $member->desc( $taxo_desc );
216 31 $self->_attach_taxon($member, $taxo_desc, 1);
217 }
218 46 $self->_attach_weights($member);
219 46 push @members, $member;
220 }
221 14 $self->_members(\@members);
222 }
223  
224  
225 4 ▶method next_member () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine next_member: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 107 107
226 107 my ($member, $count);
227 107 my $line = $self->_line;
228 107 ▶ while ( $line++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line++) 144 0 144
229 # Get the abundance of the member (undef if out-of-bounds)
230 144 $count = $self->_get_value($line, $self->_col);
231 # No more members for this community.
232 144 ▶ last if not defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 35 109 144
233 # Skip members with no abundance
234 109 ▶ next if not $count; # e.g. ''
Branch: 1 branch
BranchTrueFalseTotal
unless $count 36 73 109
235 73 ▶ next if $count == 0; # e.g. 0.0
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 1 72 73
236 # Get the member itself
237 72 $member = $self->_members->[$line - 2];
238 72 $self->_line($line);
239 72 last;
240 }
241 107 return $member, $count;
242 }
243  
244  
245 4 ▶method _next_community_init () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _next_community_init: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 46 46
246 # Go to start of next column and return name of new community or undef.
247 46 my $col = $self->_col + 1;
248 46 my $line = 1;
249 46 my $name;
250 46 ▶ if ( $self->_skip_last_col && ($col == $self->_get_max_col) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_skip_last_col and $col == $self->_get_max_col) { } 6 40 46
Condition100%3/3
$self->_skip_last_col and $col == $self->_get_max_col
!ll&&!rl&&r
20206
MC/DC100%2/2
$self->_skip_last_col and $col == $self->_get_max_col
$self->_skip_last_col $col == $self->_get_max_col
Truth table100%3/3
A and B
A= $self->_skip_last_colB= $col == $self->_get_max_col
ABresult
0X0
100
111
251 # At the taxonomy column. All communities were visited. Get out of the table
252 6 $col++;
253 } else {
254 40 $name = $self->_get_value($line, $col);
255 }
256 46 $self->_col( $col );
257 46 $self->_line( $line );
258 46 return $name;
259 }
260  
261  
262 4 ▶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 35 35
263 35 return 1;
264 }
265  
266  
267 4 ▶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 14 14
268 # Initialize the read process by generating all community members.
269 14 $self->_generate_members();
270 14 my $name = $self->_get_metacommunity_name;
271 14 return $name;
272 }
273  
274  
275 4 ▶method _get_metacommunity_name () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _get_metacommunity_name: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 14 14
276 14 my $name = $self->_get_start_content;
277 14 chomp $name;
278 14 $name =~ s/^#\s*//;
279 14 $name =~ s/^QIIME.*$//;
280 14 return $name;
281 }
282  
283  
284 4 ▶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 11 11
285 11 return 1;
286 }
287  
288  
289 4 ▶method write_member (Bio::Community::Member $member, Count $count) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine write_member: called CC=10 SCAR=23.5
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 18 18
unless ($Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member"))->check($member) 0 18 18
unless scalar @_ > 1 0 18 18
unless ($Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count"))->check($count) 0 18 18
if scalar @_ > 2 0 18 18
Condition66%4/6
$Method::Signatures::mutc{"cache"}{"Bio::Community::Member"} ||= "Method::Signatures"->_make_constraint("Bio::Community::Member")
l!l&&r!l&&!r
1710
$Method::Signatures::mutc{"cache"}{"Count"} ||= "Method::Signatures"->_make_constraint("Count")
l!l&&r!l&&!r
1710
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
290 18 my $id = $member->id;
291 18 my $line = $self->_id2line->{$id};
292 18 ▶ if (not defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 9 9 18
293 # This member has not been written previously for another community
294 9 $line = $self->_get_max_line + 1;
295 9 $self->_set_value( $line, 1, $member->id );
296 9 $self->_id2line->{$id} = $line;
297 }
298 18 ▶ if ( $member->desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member->desc) 6 12 18
299 # We'll have to write the description (taxonomy) if it is given
300 6 $self->_line2desc->{$line} = $member->desc;
301 }
302 18 $self->_set_value($line, $self->_col, $count);
303 18 $self->_line( $line + 1 );
304 18 return 1;
305 }
306  
307  
308 4 ▶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 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
309 # Write header for that community
310 9 my $line = 1;
311 9 my $col = $self->_col + 1;
312 9 $self->_set_value($line, $col, $community->name);
313 9 $self->_line( $line + 1);
314 9 $self->_col( $col );
315 9 return 1;
316 }
317  
318  
319 4 ▶method _write_headers ($name) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _write_headers: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
if scalar @_ > 1 0 3 3
320 # First line header / metacommunity name
321 3 my $header = '# ';
322 3 ▶ $header .= (not $name eq '') ? $name : 'QIIME v1.3.0 OTU table';
Branch: 1 branch
BranchTrueFalseTotal
!($name eq '') ? : 1 2 3
323 3 $header .= "\n";
324 3 $self->_print($header);
325 # First row header
326 3 $self->_set_value(1, 1, '#OTU ID');
327 }
328  
329  
330 4 ▶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
331 9 return 1;
332 }
333  
334  
335 4 ▶method _write_metacommunity_init (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_metacommunity_init: called CC=6 SCAR=18.9
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
336 # Write some generic header information
337 3 my $name;
338 3 ▶ if (defined $meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta) 3 0 3
339 3 $name = $meta->name;
340 }
341 3 $self->_write_headers($name);
342 3 return 1;
343 }
344  
345  
346 4 ▶method _write_metacommunity_finish (Bio::Community::Meta $meta) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _write_metacommunity_finish: called CC=7 SCAR=19.8
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
347 # Add taxonomy (desc) if available, but only when fh opened for reading
348 3 my $col = $self->_col + 1;
349 3 my $descs = $self->_line2desc;
350 3 ▶ if ( scalar keys %{$descs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$descs;}) 1 2 3
351 1 $self->_set_value(1, $col, 'Consensus Lineage');
352 1 ▶ while ( my ($line, $desc) = each %$descs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$descs) 3 1 4
353 3 $self->_set_value($line, $col, $desc);
354 }
355 }
356 3 return 1;
357 }
358  
359  
360  
361 4 __PACKAGE__->meta->make_immutable;
362  
363 4 1;
« blib/lib/Bio/Community/Meta/Beta.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/gaas.pm »