blib/lib/Bio/Community/IO/FormatGuesser.pm

filter: Statementstmt 88.4% 145 / 164 Branchbran 62.5% 75 / 120 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 77.4% 237 / 306 CC 65 SCAR 26.7
CC
65
Cov
77%
_possibly_qiime
26.2
guess
19.7
_possibly_gaas
14.0
CRAP 14.5
« blib/lib/Bio/Community/Tools/Sampler.pm ↑ Summary blib/lib/Bio/Community/IO.pm »
1 # BioPerl module for Bio::Community::IO::FormatGuesser
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::FormatGuesser - Determine the format used by a community file
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::IO::FormatGuesser;
17  
18 my $guesser = Bio::Community::IO::FormatGuesser->new(
19 -file => 'file.txt',
20 );
21 my $format = $guesser->guess;
22  
23 =head1 DESCRIPTION
24  
25 Given a file containing one or several communities, try to guess the file format
26 used by examining the file content (not by looking at the file name).
27  
28 The guess() method will examine the data, line by line, until it finds a line
29 that is specific to a format. If no conclusive guess can be made, undef is returned.
30  
31 If the Bio::Community::IO::FormatGuesser object is given a filehandle which is
32 seekable, it will be restored to its original position on return from the
33 guess() method.
34  
35 =head2 Formats
36  
37 The following formats are currently supported:
38  
39 =over
40  
41 =item *
42  
43 generic (tab-delimited matrix, site-by-species table, QIIME summarized OTU tables, ...)
44  
45 =item *
46  
47 gaas
48  
49 =item *
50  
51 qiime
52  
53 =item *
54  
55 unifrac
56  
57 =item *
58  
59 biom
60  
61 =back
62  
63 See the documentation for the corresponding IO drivers to read and write these
64 formats in the Bio::Community::IO::* namespace.
65  
66 =head1 AUTHOR
67  
68 Florent Angly L<florent.angly@gmail.com>
69  
70 This module was inspired and based on the Bio::IO::GuessSeqFormat module written
71 by Andreas Kähäri <andreas.kahari@ebi.ac.uk> and contributors. Thanks to them!
72  
73 =head1 SUPPORT AND BUGS
74  
75 User feedback is an integral part of the evolution of this and other Bioperl
76 modules. Please direct usage questions or support issues to the mailing list,
77 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
78 experienced and reponsive experts will be able look at the problem and quickly
79 address it. Please include a thorough description of the problem with code and
80 data examples if at all possible.
81  
82 If you have found a bug, please report it on the BioPerl bug tracking system
83 to help us keep track the bugs and their resolution:
84 L<https://redmine.open-bio.org/projects/bioperl/>
85  
86 =head1 COPYRIGHT
87  
88 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
89  
90 This library is free software; you can redistribute it and/or modify
91 it under the same terms as Perl itself, either Perl version 5.10.1 or,
92 at your option, any later version of Perl 5 you may have available.
93  
94 =head1 APPENDIX
95  
96 The rest of the documentation details each of the object
97 methods. Internal methods are usually preceded with a _
98  
99 =head2 new
100  
101 Function: Create a new Bio::Community::IO::FormatGuesser object
102 Usage : my $guesser = Bio::Community::IO::FormatGuesser->new( );
103 Args : -text, -file or -fh. If more than one of these arguments was
104 provided, only one is used: -text has precendence over -file, which
105 has precedence over -fh.
106 Returns : a new Bio::Community::IO::FormatGuesser object
107  
108 =cut
109  
110  
111 package Bio::Community::IO::FormatGuesser;
112  
113 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 14 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 14 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 14 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 14 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 14 extends 'Bio::Root::Root';
120  
121  
122 14 my %formats = (
123 biom => \&_possibly_biom ,
124 gaas => \&_possibly_gaas ,
125 unifrac => \&_possibly_unifrac ,
126 generic => \&_possibly_generic ,
127 qiime => \&_possibly_qiime ,
128 );
129  
130 14 my $real_re = qr/^(?:(?i)(?:[+-]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))$/;
131 # regular expression to match a real number, taken from Regexp::Common
132  
133 =head2 file
134  
135 Usage : my $file = $guesser->file;
136 Function: Get or set the file from which to guess the format
137 Args : file path (string)
138 Returns : file path (string)
139  
140 =cut
141  
142 14 has 'file' => (
143 is => 'rw',
144 isa => 'Str',
145 required => 0,
146 lazy => 1,
147 default => undef,
148 init_arg => '-file',
149 predicate => '_has_file',
150 );
151  
152  
153 =head2 fh
154  
155 Usage : my $fh = $guesser->fh;
156 Function: Get or set the file handle from which to guess the format.
157 Args : file handle
158 Returns : file handle
159  
160 =cut
161  
162 14 has 'fh' => (
163 is => 'rw',
164 isa => 'FileHandle',
165 required => 0,
166 lazy => 1,
167 default => undef,
168 init_arg => '-fh',
169 predicate => '_has_fh',
170 );
171  
172  
173 =head2 text
174  
175 Usage : my $text = $guesser->text;
176 Function: Get or set the text from which to guess the format. In most, if not
177 all cases, the first few lines of a text string should be enough to
178 determine the format.
179 Args : text string
180 Returns : text string
181  
182 =cut
183  
184 14 has 'text' => (
185 is => 'rw',
186 isa => 'Str',
187 required => 0,
188 lazy => 1,
189 default => undef,
190 init_arg => '-text',
191 predicate => '_has_text',
192 );
193  
194  
195 =head2 guess
196  
197 Function: Guess the file format
198 Usage : my $format = $guesser->guess;
199 Args : format string (e.g. generic, qiime, etc)
200 Returns : format string (e.g. generic, qiime, etc)
201  
202 =cut
203  
204 14 ▶method guess () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine guess: called CC=18 SCAR=29.8
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 38 38
205 38 my $format;
206  
207 # Prepare input
208 38 my ($in, $original_pos);
209 {
210 ####local $Bio::Root::IO::HAS_EOL = 1; # Need Bioperl-dev (>1.6.922) for this to work
211 38 ▶ if ($self->_has_text) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_has_text) { } 3 35 38
elsif ($self->_has_file) { } 34 1 35
elsif ($self->_has_fh) { } 1 0 1
212 3 $in = Bio::Root::IO->new(-string => $self->text);
213 } elsif ($self->_has_file) {
214 34 $in = Bio::Root::IO->new(-file => $self->file);
215 } elsif ($self->_has_fh) {
216 1 $original_pos = tell($self->fh);
217 1 $in = Bio::Root::IO->new(-fh => $self->fh, -noclose => 1);
218 } else {
219 0 $self->throw('Need to provide -file, -fh or -text');
220 }
221 }
222  
223 # Read lines and try to attribute format
224 38 my %test_formats = %formats;
225 38 my %ok_formats;
226 38 my $line_num = 0;
227 38 ▶ while ( defined(my $line = $in->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $in->_readline)) 41 1 42
228  
229 # Read next line (and convert line endings). Exit if no lines left.
230 41 $line_num++;
231 41 chomp $line;
232  
233 # Skip white and empty lines.
234 41 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 41 41
235  
236 # Split fields
237 41 my @fields = split /\t/, $line;
238  
239 # Try all formats remaining
240 41 %ok_formats = ();
241 41 my ($test_format, $test_function);
242 41 ▶ while ( ($test_format, $test_function) = each (%test_formats) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %test_formats) 193 41 234
243 193 ▶ if ( &$test_function(\@fields, $line, $line_num) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$test_function(\@fields, $line, $line_num)) { } 44 149 193
244 # Line matches this format
245 44 $ok_formats{$test_format} = undef;
246 } else {
247 # Do not try to match this format with upcoming lines
248 149 delete $test_formats{$test_format};
249 }
250 }
251  
252 # Exit if there was a match to only one format
253 41 ▶ if (scalar keys %ok_formats == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %ok_formats == 1) 36 5 41
254 36 last;
255 }
256  
257 # Exit if no formats left to try
258 5 ▶ if (scalar keys %test_formats == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %test_formats == 0) 1 4 5
259 1 last;
260 }
261  
262 # Give up after having tested 100 lines
263 4 ▶ if ($line_num >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num >= 100) 0 4 4
264 0 last;
265 }
266  
267 }
268  
269 # If several formats matched. Assume 'generic' if possible, undef otherwise
270 38 ▶ if (scalar keys %ok_formats > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %ok_formats > 1) 0 38 38
271 0 for my $ok_format (keys %ok_formats) {
272 0 ▶ if (not $ok_format eq 'generic') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok_format eq "generic") 0 0 0
273 0 delete $ok_formats{$ok_format};
274 }
275 }
276 }
277  
278 38 ▶ if (scalar keys %ok_formats == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %ok_formats == 1) 36 2 38
279 36 $format = (keys %ok_formats)[0];
280 }
281  
282 # Cleanup
283 38 ▶ if ($in->noclose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->noclose) 1 37 38
284 # Reset filehandle cursor to original location
285 1 ▶ seek($self->fh, $original_pos, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $self->fh, $original_pos, 0 or $self->throw("Could not reset the cursor to its original position: $!") 0 1 1
286 or $self->throw("Could not reset the cursor to its original position: $!");
287 }
288 38 $in->close;
289  
290 38 return $format;
291 }
292  
293  
294 14 ▶func _possibly_biom ($fields, $line, $line_num) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _possibly_biom: called CC=9 SCAR=25.6
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 37 37
unless scalar @_ > 1 0 37 37
unless scalar @_ > 2 0 37 37
if scalar @_ > 3 0 37 37
295 # Example:
296 # {
297 # "id":null,
298 # "format": "Biological Observation Matrix 0.9.1-dev",
299 # "format_url": "http://biom-format.org",
300 # ...
301 37 my $ok = 0;
302 37 ▶ if ($line_num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num == 1) { } 37 0 37
303 37 ▶ if ($line =~ m/^{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^{/) 9 28 37
304 9 $ok = 1;
305 }
306 } else {
307 0 ▶ if ( ($line =~ m/"\S+":/) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /"\S+":/ or $line =~ /Biological Observation Matrix/) 0 0 0
Condition0%0/3
$line =~ /"\S+":/ or $line =~ /Biological Observation Matrix/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /"\S+":/ or $line =~ /Biological Observation Matrix/
$line =~ /"\S+":/ $line =~ /Biological Observation Matrix/
Truth table0%0/3
A or B
A= $line =~ /"\S+":/B= $line =~ /Biological Observation Matrix/
ABresult
1X1
011
000
308 ($line =~ m/Biological Observation Matrix/) ) {
309 0 $ok = 1;
310 }
311 }
312 37 return $ok;
313 }
314  
315  
316 14 ▶func _possibly_generic ($fields, $line, $line_num) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _possibly_generic: called CC=10 SCAR=23.5
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 41 41
unless scalar @_ > 1 0 41 41
unless scalar @_ > 2 0 41 41
if scalar @_ > 3 0 41 41
317 # Example:
318 # Species gut soda lake
319 # Streptococcus 241 334
320 # ...
321 # Columns from the second to the last must contain numbers.
322 41 my $ok = 0;
323 41 my $num_fields = scalar @$fields;
324 41 ▶ if ($num_fields >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_fields >= 2) 20 21 41
325 20 ▶ if ($line_num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num == 1) { } 16 4 20
326 16 ▶ $ok = 1 if $line !~ m/^#/; #### don't like this... too restrictive
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^#/ 11 5 16
327 #$ok = 1; ###
328 } else {
329 4 for my $i (1 .. $num_fields - 1) {
330 4 ▶ if ($fields->[$i] =~ $real_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->[$i] =~ /$real_re/) { } 0 4 4
331 0 $ok = 1;
332 } else {
333 4 $ok = 0;
334 4 last;
335 }
336 }
337 }
338 }
339 41 return $ok;
340 }
341  
342  
343 14 ▶func _possibly_gaas ($fields, $line, $line_num) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _possibly_gaas: called CC=10 SCAR=26.4
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 37 37
unless scalar @_ > 1 0 37 37
unless scalar @_ > 2 0 37 37
if scalar @_ > 3 0 37 37
344 # Example:
345 # # tax_name tax_id rel_abund
346 # Streptococcus pyogenes phage 315.1 198538 0.791035649011735
347 # or:
348 # # sequence_name sequence_id relative_abundance_%
349 # Milk vetch dwarf virus segment 9, complete sequence gi|20177473|ref|NC_003646.1| 42.6354640657824
350 # First field contains string, second field an ID, third field a number.
351 37 my $ok = 0;
352 37 my $num_fields = scalar @$fields;
353 37 ▶ if ($num_fields == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_fields == 3) 13 24 37
354 13 ▶ if ($line_num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num == 1) { } 13 0 13
355 13 ▶ if ($line =~ m/^#\s*.+name.+id.+abund.*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#\s*.+name.+id.+abund.*$/) 5 8 13
356 5 $ok = 1;
357 }
358 } else {
359 0 ▶ if ($line !~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /^#/) { } 0 0 0
360 0 $ok = 1;
361 } else {
362 0 ▶ if ($fields->[-1] =~ $real_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->[-1] =~ /$real_re/) 0 0 0
363 0 $ok = 1;
364 }
365 }
366 }
367 }
368 37 return $ok;
369 }
370  
371  
372 14 ▶func _possibly_unifrac ($fields, $line, $line_num) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _possibly_unifrac: called CC=9 SCAR=22.1
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 41 41
unless scalar @_ > 1 0 41 41
unless scalar @_ > 2 0 41 41
if scalar @_ > 3 0 41 41
373 # Example:
374 # Sequence.1 Sample.1 1
375 # or:
376 # Sequence.1 Sample.1
377 # There are no headers. Two first fields contain strings. Optional third
378 # field contains numbers.
379 41 my $ok = 0;
380 41 my $num_fields = scalar @$fields;
381 41 ▶ if ($line =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) { } 16 25 41
382 16 $ok = 0;
383 } else {
384 25 ▶ if ($num_fields == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num_fields == 2) { } 2 23 25
elsif ($num_fields == 3) { } 11 12 23
385 2 $ok = 1;
386 } elsif ($num_fields == 3) {
387 11 ▶ if ($fields->[-1] =~ $real_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->[-1] =~ /$real_re/) 6 5 11
388 6 $ok = 1;
389 }
390 }
391 }
392 41 return $ok;
393 }
394  
395  
396 14 ▶func _possibly_qiime ($fields, $line, $line_num) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _possibly_qiime: called CC=14 SCAR=32.6
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 37 37
unless scalar @_ > 1 0 37 37
unless scalar @_ > 2 0 37 37
if scalar @_ > 3 0 37 37
397 # Example:
398 # # QIIME v1.3.0 OTU table
399 # #OTU ID 20100302 20100304 20100823
400 # 0 40 0 76
401 # 1 0 142 2
402 # The first line can contain any comment. The second line must have
403 # the name of the columns. The last column can be an extra non-numeric
404 # column containing taxonomic information.
405 37 my $ok = 0;
406 37 my $num_fields = scalar @$fields;
407 37 ▶ if ($num_fields == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num_fields == 1) { } 21 16 37
elsif ($num_fields >= 2) { } 16 0 16
408 21 ▶ if ($line_num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_num == 1) 21 0 21
409 21 ▶ if ($line =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) 11 10 21
410 11 $ok = 1;
411 }
412 }
413 } elsif ($num_fields >= 2) {
414 16 ▶ if ($line_num == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line_num == 2) { } 0 16 16
elsif ($line_num > 2) { } 0 16 16
415 0 ▶ if ($line =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) 0 0 0
416 0 $ok = 1;
417 }
418 } elsif ($line_num > 2) {
419 0 for my $i (1 .. $num_fields - 2) {
420 0 ▶ if ($fields->[$i] =~ $real_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->[$i] =~ /$real_re/) { } 0 0 0
421 0 $ok = 1;
422 } else {
423 0 $ok = 0;
424 0 last;
425  
426 }
427 }
428 }
429 }
430 37 return $ok;
431 }
432  
433  
434 14 __PACKAGE__->meta->make_immutable;
435  
436 14 1;
« blib/lib/Bio/Community/Tools/Sampler.pm ↑ Summary blib/lib/Bio/Community/IO.pm »