blib/lib/Bio/Community/Role/Table.pm

filter: Statementstmt 97.0% 196 / 202 Branchbran 72.0% 62 / 86 Conditioncond 81.2% 13 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 95.0% 19 / 20 Podpod n/a totaltotal 88.3% 295 / 334 CC 51 SCAR 21.8
CC
51
Cov
89%
_read_table
17.2
_get_value
10.2
_set_value
9.0
CRAP 8.9
« blib/lib/Bio/Community/IO.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/unifrac.pm »
1 # BioPerl module for Bio::Community::Role::Table
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::Role::Table - Role to read/write data tables and provide random
13 access to their cells
14  
15 =head1 SYNOPSIS
16  
17 package My::Package;
18  
19 use Moose;
20 extends 'Bio::Root::IO';
21 with 'Bio::Community::Role::Table';
22  
23 # Use the new(), _read_table(), _get_value(), _set_value(), _insert_line(),
24 # _delete_col() and _write_table()
25 # methods as needed
26 # ...
27  
28 1;
29  
30 =head1 DESCRIPTION
31  
32 This role implements methods to read and write file structured as a table
33 containing rows and columns. When reading a table from a file, an index is kept
34 to provide random-access to any cell of the table. When writing a table to a file,
35 cell data can also be given in any order. It is kept in memory until the file is
36 written to disk.
37  
38 Objects are constructed with the new() method. Since Table-consuming classes
39 must inherit from Bio::Root::IO, all Bio::Root::IO options are accepted, e.g.
40 -file, -fh, -string, -flush, etc. Other constructors are detailed in the
41 L<APPENDIX>.
42  
43 =head1 AUTHOR
44  
45 Florent Angly L<florent.angly@gmail.com>
46  
47 =head1 SUPPORT AND BUGS
48  
49 User feedback is an integral part of the evolution of this and other Bioperl
50 modules. Please direct usage questions or support issues to the mailing list,
51 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
52 experienced and reponsive experts will be able look at the problem and quickly
53 address it. Please include a thorough description of the problem with code and
54 data examples if at all possible.
55  
56 If you have found a bug, please report it on the BioPerl bug tracking system
57 to help us keep track the bugs and their resolution:
58 L<https://redmine.open-bio.org/projects/bioperl/>
59  
60 =head1 COPYRIGHT
61  
62 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
63  
64 This library is free software; you can redistribute it and/or modify
65 it under the same terms as Perl itself, either Perl version 5.10.1 or,
66 at your option, any later version of Perl 5 you may have available.
67  
68 =head1 APPENDIX
69  
70 The rest of the documentation details each of the object
71 methods. Internal methods are usually preceded with a _
72  
73 =cut
74  
75  
76 package Bio::Community::Role::Table;
77  
78 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 9 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 9 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83  
84 # Consuming class has to inherit from Bio::Root::IO
85 9 requires '_fh',
86 '_readline',
87 '_print';
88  
89  
90 =head2 delim
91  
92 Usage : my $delim = $in->delim;
93 Function: When reading or writing a table, get or set the delimiter, i.e. the
94 characters that delimit the columns of the table. The default is the
95 tab character "\t".
96 Args : A string
97 Returns : A string
98  
99 =cut
100  
101 9 has 'delim' => (
102 is => 'rw',
103 isa => 'Str',
104 required => 0,
105 init_arg => '-delim',
106 lazy => 1,
107 default => "\t",
108 );
109  
110  
111 # New lines can be: \n, \r\n, ... Record number of newline chars here
112 9 has '_num_eol_chars' => (
113 is => 'rw',
114 #isa => 'PositiveInt',
115 required => 0,
116 lazy => 1,
117 default => 0,
118 );
119  
120  
121 =head2 start_line
122  
123 Usage : my $line_num = $in->start_line;
124 Function: When reading a table, get or set the line number at which the table
125 starts. The default is 1, i.e. the table starts at the first line of
126 the file. This option is not used when writing a table, but see
127 _write_table() for details.
128 Args : A strictly positive number
129 Returns : A strictly positive number
130  
131 =cut
132  
133 9 has 'start_line' => (
134 is => 'ro',
135 isa => 'StrictlyPositiveInt',
136 required => 0,
137 init_arg => '-start_line',
138 lazy => 1,
139 default => 1,
140 );
141  
142  
143 =head2 end_line
144  
145 Usage : my $line_num = $in->end_line;
146 Function: When reading a table, get or set the line number at which the table
147 ends. If undef (the default), the table ends at the last line of the
148 file.
149 Args : A strictly positive number or undef
150 Returns : A strictly positive number or undef
151  
152 =cut
153  
154 9 has 'end_line' => (
155 is => 'ro',
156 isa => 'Maybe[StrictlyPositiveInt]',
157 required => 0,
158 init_arg => '-end_line',
159 lazy => 1,
160 default => undef,
161 );
162  
163  
164 =head2 missing_string
165  
166 Usage : my $missing = $out->missing_string;
167 Function: When reading a table, get or set the line number at which the table
168 ends. If undef (the default), the table ends at the last line of the
169 file.
170 Args : A string, e.g. '', '0', 'n/a', '-'
171 Returns : A string
172  
173 =cut
174  
175 9 has 'missing_string' => (
176 is => 'ro',
177 isa => 'Str',
178 required => 0,
179 init_arg => '-missing_string',
180 lazy => 1,
181 default => '0',
182 );
183  
184  
185 =head2 _get_start_content
186  
187 Usage : my $txt = $in->_get_start_content;
188 Function: After the table has been parsed, this returns everything before -start_line
189 Args : A strictly positive number
190 Returns : A strictly positive number
191  
192 =cut
193  
194 9 has '_start_content' => (
195 is => 'ro',
196 isa => 'Maybe[Str]',
197 required => 0,
198 init_arg => undef,
199 lazy => 1,
200 default => '',
201 reader => '_get_start_content',
202 writer => '_set_start_content',
203 );
204  
205  
206 =head2 _get_max_line
207  
208 Usage : my $num_lines = $in->_get_max_line;
209 Function: Get the number of lines in the table
210 Args : None
211 Returns : Positive integer
212  
213 =cut
214  
215 9 has '_max_line' => (
216 is => 'rw',
217 #isa => 'StrictlyPositiveInt',
218 required => 0,
219 init_arg => undef,
220 lazy => 1,
221 default => 0,
222 reader => '_get_max_line',
223 writer => '_set_max_line',
224 );
225  
226  
227 =head2 _get_max_col
228  
229 Usage : my $num_cols = $in->_get_max_col;
230 Function: Get the number of columns in the table
231 Args : None
232 Returns : Positive integer
233  
234 =cut
235  
236 9 has '_max_col' => (
237 is => 'rw',
238 #isa => 'StrictlyPositiveInt',
239 required => 0,
240 init_arg => undef,
241 lazy => 1,
242 default => 0,
243 reader => '_get_max_col',
244 writer => '_set_max_col',
245 );
246  
247  
248 9 ▶method BUILD ($args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 66 66
if scalar @_ > 1 0 66 66
249 # After object constructed with new(), index table if filehandle is readable
250 66 ▶ if ($self->mode eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "r") 49 17 66
251 49 $self->_read_table;
252 }
253 }
254  
255  
256 # Index of the location of the cells (when reading a table)
257 has '_index' => (
258 is => 'rw',
259 #isa => 'ArrayRef[PositiveInt]',
260 required => 0,
261 init_arg => undef,
262 lazy => 1,
263 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 9 predicate => '_has_index',
265 );
266  
267  
268 # Value contained in the table cells (when writing a table)
269 has '_values' => (
270 is => 'rw',
271 #isa => 'ArrayRef[Str]'
272 required => 0,
273 init_arg => undef,
274 lazy => 1,
275 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
276 9 );
277  
278  
279 9 has '_was_written' => (
280 is => 'rw',
281 #isa => 'Bool',
282 required => 0,
283 init_arg => undef,
284 lazy => 1,
285 default => 0,
286 );
287  
288  
289 =head2 _read_table
290  
291 Usage : $in->_read_table;
292 Function: Read the table in the file and index the position of its cells.
293 Args : None
294 Returns : 1 on success
295  
296 =cut
297  
298 9 ▶method _read_table () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _read_table: called CC=17 SCAR=28.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 50 50
299 # Index the file the first time
300  
301 50 ▶ if ( $self->_has_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_index) 1 49 50
302 1 return 1;
303 }
304  
305 49 my $start_line = $self->start_line;
306 49 my $end_line = $self->end_line;
307 49 ▶ if ( (defined $end_line) && ($end_line < $start_line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end_line and $end_line < $start_line) 0 49 49
Condition66%2/3
defined $end_line and $end_line < $start_line
!ll&&!rl&&r
4810
MC/DC0%0/2
defined $end_line and $end_line < $start_line
defined $end_line $end_line < $start_line
Truth table66%2/3
A and B
A= defined $end_lineB= $end_line < $start_line
ABresult
0X0
100
111
308 0 $self->throw("Error: Got start ($start_line) greater than end ($end_line)\n");
309 }
310  
311 49 my @index; # array of file offsets
312 49 my ($max_line, $max_col) = (0, 0);
313  
314 49 my $delim = $self->delim;
315 49 my $delim_length = length $delim;
316  
317 49 my $file_offset = 0;
318 49 my $num_eol_chars;
319  
320 # In Windows, text files have '\r\n' as line separator, but when reading in
321 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
322 # "length $_" will report 4 although the line is 5 bytes in length.
323 # We assume that all lines have the same line separator and only read current line.
324 49 my $fh = $self->_fh;
325 49 my $init_pos = tell($fh);
326 49 my $init_line = $.;
327 49 ▶ my $curr_line = <$fh> || '';
Condition50%1/2
readline $fh || ''
l!l
490
MC/DC0%0/1
readline $fh || ''
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
328 49 my $pos_diff = tell($fh) - $init_pos;
329 49 my $correction = $pos_diff - length $curr_line;
330 49 $fh->input_line_number($init_line); # Rewind line number $.
331 49 seek $fh, $init_pos, 0; # Rewind position to proceed to read the file
332  
333 49 ▶ while (my $line = $self->_readline(-raw => 1)) { # _readline is from Bio::Root::IO
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_readline("-raw", 1)) 249 49 298
334  
335 249 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 249 249
336  
337 249 ▶ if (not defined $num_eol_chars) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num_eol_chars) 49 200 249
338 # Count line length
339 49 $line =~ m/([\r\n]?\n)$/; # last line may not match
340 49 ▶ $num_eol_chars = length($1||'') + $correction;
Condition100%2/2
$1 || ''
l!l
481
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
341 }
342  
343 249 my $line_length = length($line) + $correction;
344  
345 # Do not index the line if it is before or after the table
346 249 ▶ if ($. < $start_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($. < $start_line) 17 232 249
347 17 $self->_set_start_content( $self->_get_start_content . $line );
348 17 $file_offset += $line_length;
349 17 next;
350 }
351 232 ▶ if ( (defined $end_line) && ($. > $end_line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end_line and $. > $end_line) 1 231 232
Condition100%3/3
defined $end_line and $. > $end_line
!ll&&!rl&&r
22921
MC/DC100%2/2
defined $end_line and $. > $end_line
defined $end_line $. > $end_line
Truth table100%3/3
A and B
A= defined $end_lineB= $. > $end_line
ABresult
0X0
100
111
352 1 next;
353 }
354  
355 # Save the offset of the first line of the table
356 231 ▶ if (scalar @index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @index == 0) 49 182 231
357 49 push @index, $file_offset;
358 }
359  
360 # Index the line
361 231 my $line_offset = 0;
362 231 my @matches;
363 231 while ( 1 ) {
364 761 my $match = index($line, $delim, $line_offset);
365 761 ▶ if ($match == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match == -1) { } 231 530 761
366 # Reached end of line. Register it and move on to next line.
367 231 $match = length( $line ) + $correction - $num_eol_chars;
368 231 push @matches, $match + $file_offset;
369 231 $file_offset += $line_length;
370 231 last;
371 } else {
372 # Save the match
373 530 push @matches, $match + $file_offset;
374 530 $line_offset = $match + $delim_length;
375 }
376 }
377 231 my $nof_cols = scalar @matches;
378 231 ▶ if ($nof_cols != $max_col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nof_cols != $max_col) 49 182 231
379 49 ▶ if ($max_col == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_col == 0) { } 49 0 49
380 # Initialize the number of columns
381 49 $max_col = $nof_cols;
382 } else {
383 0 $self->throw( "Error: Got $nof_cols columns at line $. but got a ".
384 "different number ($max_col) at the previous line\n" );
385 }
386 }
387 231 $max_line++;
388 231 push @index, @matches;
389 }
390  
391 49 $self->_num_eol_chars( $num_eol_chars );
392 49 $self->_index(\@index);
393 49 $self->_set_max_line($max_line);
394 49 $self->_set_max_col($max_col);
395  
396 49 return 1;
397 }
398  
399  
400 =head2 _get_value
401  
402 Usage : my $value = $in->_get_value(1, 3);
403 Function: Get the value of the cell given its position in the table (line and
404 column).
405 Args : A strictly positive integer for the line
406 A strictly positive integer for the column
407 Returns : A string for the value of the table at the given line and column
408 or
409 undef if line or column was out-of-bounds
410  
411 =cut
412  
413 #method _get_value (StrictlyPositiveInt $line, StrictlyPositiveInt $col) {
414 9 ▶method _get_value ($line, $col) { # this function is called a lot, keep it lean
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _get_value: called CC=10 SCAR=23.2
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1054 1054
unless scalar @_ > 1 0 1054 1054
if scalar @_ > 2 0 1054 1054
415 1054 my $val;
416 1054 my $max_col = $self->_get_max_col;
417 1054 ▶ if ( ($line <= $self->_get_max_line) && ($col <= $max_col) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line <= $self->_get_max_line and $col <= $max_col) 915 139 1054
Condition100%3/3
$line <= $self->_get_max_line and $col <= $max_col
!ll&&!rl&&r
10831915
MC/DC100%2/2
$line <= $self->_get_max_line and $col <= $max_col
$line <= $self->_get_max_line $col <= $max_col
Truth table100%3/3
A and B
A= $line <= $self->_get_max_lineB= $col <= $max_col
ABresult
0X0
100
111
418  
419 # Retrieve the value if it is within the bounds of the table
420 915 my $pos = ($line - 1) * $max_col + $col - 1;
421 915 my $offset = $self->_index->[$pos];
422  
423 # Adjust offset
424 915 ▶ if ( $pos % $max_col ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos % $max_col) { } 660 255 915
425 # For columns (except first one), account for prepended delimiter
426 660 my $delim_len = length $self->delim;
427 660 $offset += $delim_len;
428 } else {
429 255 ▶ if ($pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) 216 39 255
430 # For lines (except first one), account for preprended new line chars
431 216 $offset += $self->_num_eol_chars;
432 }
433 }
434  
435 # Read value
436 915 ▶ seek( $self->_fh, $offset, 0 ) or $self->throw("Could not seek on filehandle at offset $offset: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
seek $self->_fh, $offset, 0 or $self->throw("Could not seek on filehandle at offset ${offset}: $!\n") 0 915 915
437 915 ▶ defined( read( $self->_fh, $val, $self->_index->[$pos+1] - $offset ) ) or $self->throw("Could not read from filehandle: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
defined read($self->_fh, $val, $self->_index->[$pos + 1] - $offset) or $self->throw("Could not read from filehandle: $!\n") 0 915 915
438 # Note: read() returns the number of characters read, 0 at end of file, or undef if there was an error
439
440 }
441 1054 return $val;
442 }
443  
444  
445 =head2 _set_value
446  
447 Usage : $out->_set_value(1, 3, $value);
448 Function: Set the element at the given line and column of the table.
449 Args : A strictly positive integer for the line
450 A strictly positive integer for the column
451 A string for the value of the table at the given line and column
452 Returns : 1 for success
453  
454 =cut
455  
456 #method _set_value (StrictlyPositiveInt $line, StrictlyPositiveInt $col, $value) {
457 9 ▶method _set_value ($line, $col, $value) { # this function is called a lot, keep it lean
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _set_value: called CC=9 SCAR=22.0
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 144 144
unless scalar @_ > 1 0 144 144
unless scalar @_ > 2 0 144 144
if scalar @_ > 3 0 144 144
458 144 my $pos = 0;
459 144 my $values = $self->_values;
460 144 my $max_lines = $self->_get_max_line;
461 144 my $max_cols = $self->_get_max_col;
462  
463 # Extend table with columns if needed
464 144 ▶ if ($col > $max_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col > $max_cols) 43 101 144
465 43 my $new_max_cols = $col;
466 43 my $max_idx = $new_max_cols * $max_lines - 1;
467 43 my @padding = ($self->missing_string) x ($new_max_cols - $max_cols);
468 43 ▶ for ( my $idx = $max_cols;
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $max_idx) 62 43 105
469 $idx <= $max_idx;
470 $idx += $new_max_cols ) {
471 62 splice @$values, $idx, 0, @padding;
472 }
473 43 $max_cols = $new_max_cols;
474 43 $self->_set_max_col($new_max_cols);
475 }
476  
477 # Extend table with lines if needed
478 144 ▶ if ($line > $max_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > $max_lines) 47 97 144
479 47 my $new_max_lines = $line;
480 47 my $max_idx = $max_cols * $new_max_lines - 1;
481 47 my @padding = ($self->missing_string) x $max_cols;
482 47 ▶ for ( my $idx = $max_cols * $max_lines;
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $max_idx) 49 47 96
483 $idx <= $max_idx;
484 $idx += $max_cols ) {
485 49 splice @$values, $idx, 0, @padding;
486 }
487 47 $max_lines = $new_max_lines;
488 47 $self->_set_max_line($new_max_lines);
489 }
490  
491 # Set new value
492 144 $pos = ($line - 1) * $max_cols + $col - 1;
493 144 $values->[$pos] = $value;
494  
495 144 return 1;
496 }
497  
498  
499 =head2 _insert_line
500  
501 Usage : $out->_insert_line(3, ['sample1', 3, 10.9, 'Mus musculus']);
502 Function: Insert a line of values in the table, at the indicated line, shifting
503 all other lines down. You can also append a line to the table by
504 providing the maximum line number + 1.
505 Args : A strictly positive integer for the line at which to insert
506 An arrayref containing the values to insert (must match table width)
507 Returns : 1 for success
508  
509 =cut
510  
511 #method _insert_line (StrictlyPositiveInt $line, ArrayRef $values) {
512 9 ▶method _insert_line ($line, $insert_values) { # this function is called a lot, keep it lean
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _insert_line: called CC=8 SCAR=21.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 29 29
unless scalar @_ > 1 0 29 29
if scalar @_ > 2 0 29 29
513 29 my $max_lines = $self->_get_max_line;
514 29 ▶ if ($line > $max_lines + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > $max_lines + 1) 0 29 29
515 0 $self->throw("Could not insert line beyond last line of table.");
516 }
517  
518 29 my $max_cols = $self->_get_max_col;
519 29 ▶ if ( ($max_cols > 0) && (scalar @$insert_values != $max_cols) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_cols > 0 and scalar @$insert_values != $max_cols) 0 29 29
Condition66%2/3
$max_cols > 0 and scalar @$insert_values != $max_cols
!ll&&!rl&&r
4250
MC/DC0%0/2
$max_cols > 0 and scalar @$insert_values != $max_cols
$max_cols > 0 scalar @$insert_values != $max_cols
Truth table66%2/3
A and B
A= $max_cols > 0B= scalar @$insert_values != $max_cols
ABresult
0X0
100
111
520 0 $self->throw("Could not insert a line because it did not match table width ($max_cols).");
521 }
522  
523 29 my $idx = $max_cols * ($line - 1);
524 29 splice @{$self->_values}, $idx, 0, @$insert_values;
525  
526 29 $self->_set_max_line($max_lines + 1);
527 29 ▶ if ($max_cols == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_cols == 0) 4 25 29
528 4 $self->_set_max_col(scalar @$insert_values);
529 }
530  
531 29 return 1;
532 }
533  
534  
535 =head2 _delete_col
536  
537 Usage : $out->_delete_col(3);
538 Function: Delete a column of the table.
539 Args : A strictly positive integer for the column to delete.
540 Returns : 1 for success
541  
542 =cut
543  
544 #method _delete_col (StrictlyPositiveInt $col) {
545 9 ▶method _delete_col ($col) { # this function is called a lot, keep it lean
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _delete_col: called CC=6 SCAR=18.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
if scalar @_ > 1 0 3 3
546  
547 3 my $max_cols = $self->_get_max_col;
548 3 ▶ if ( $col > $max_cols ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col > $max_cols) 0 3 3
549 0 $self->throw("Could not delete column $col because the table has only $max_cols columns.");
550 }
551  
552 # Extend table with columns if needed
553 3 my $max_lines = $self->_get_max_line;
554 3 my $max_idx = $max_cols * $max_lines - 1;
555 3 my $values = $self->_values;
556 3 ▶ for (my $idx = $max_idx; $idx >= 0; $idx -= $max_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 13 3 16
557 13 splice @$values, $idx, 1;
558 }
559  
560 3 $self->_set_max_col( $max_cols - 1 );
561  
562 3 ▶ if ($self->_get_max_col == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_max_col == 0) 1 2 3
563 1 $self->_set_max_line( 0 );
564 }
565  
566 3 return 1;
567 }
568  
569  
570 =head2 _write_table
571  
572 Usage : $out->_write_table;
573 Function: Write the content of the cells in the table to a file. It is
574 generally called automatically when closing the object. However, if
575 you want header lines before or after the table, you can write them
576 to file using the _print() method of Bio::Root::IO prior and after
577 calling _write_table().
578 Args : None
579 Returns : 1 on success
580  
581 =cut
582  
583 9 ▶method _write_table () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _write_table: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 16 16
584 16 my $delim = $self->delim;
585 16 my $values = $self->_values;
586 16 my $max_cols = $self->_get_max_col;
587 16 for my $line ( 1 .. $self->_get_max_line ) {
588 73 my $start = ($line - 1) * $max_cols;
589 73 my $end = $line * $max_cols - 1;
590 73 $self->_print( join( $delim, @$values[$start..$end] ) . "\n" );
591 }
592 16 $self->_was_written(1);
593 16 return 1;
594 }
595  
596  
597 9 1;
« blib/lib/Bio/Community/IO.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/unifrac.pm »