lib/Bio/ToolBox/Data/Stream.pm

filter: Statementstmt 68.5% 120 / 175 Branchbran 45.6% 42 / 92 Conditioncond 47.8% 22 / 46 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 86.6% 13 / 15 Podpod 100% 8 / 8 totaltotal 57.4% 208 / 362 CC 67 SCAR 46.1
CC
67
Cov
59%
new
199.2
copy_column
20.0
write_row
12.5
CRAP 100.5
« script/data2fasta.pl ↑ Summary script/ucsc_table2gff3.pl »
1 package Bio::ToolBox::Data::Stream;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw(carp cluck croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base 'Bio::ToolBox::Data::core';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Bio::ToolBox::Data::Feature;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '2.02';
11  
12 #### Initialize ####
13  
14 9 ▶sub new {
Subroutine new: called CC=39 SCAR=52.9
Pod documented
15 9 my $class = shift;
16 9 my %args = @_;
17  
18 # file arguments
19 9 ▶ $args{in} ||= $args{file} || undef;
Condition60%3/5
$args{"in"} ||= $args{"file"} || undef
l!l&&r!l&&!r
702
$args{'file'} || undef
l!l
02
MC/DC50%1/2
$args{"in"} ||= $args{"file"} || undef
$args{"in"} $args{'file'}
Truth table66%2/3
A ||= B
A= $args{"in"}B= $args{'file'}
ABresult
1X1
011
000
20 9 ▶ $args{out} ||= undef;
Condition100%2/2
$args{"out"} ||= undef
l!l
27
MC/DC100%1/1
$args{"out"} ||= undef
$args{"out"}
Truth table100%2/2
A
A= $args{"out"}
Aresult
11
00
21 9 ▶ unless ( $args{in} or $args{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"in"} or $args{"out"}) 0 9 9
Condition66%2/3
$args{"in"} or $args{"out"}
l!l&&r!l&&!r
720
MC/DC0%0/2
$args{"in"} or $args{"out"}
$args{"in"} $args{"out"}
Truth table66%2/3
A or B
A= $args{"in"}B= $args{"out"}
ABresult
1X1
011
000
22 0 cluck q(ERROR: a filename must be specified with 'in' or 'out' argument keys!);
23 0 return;
24 }
25 9 ▶ if ( defined $args{in} and defined $args{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"in"} and defined $args{"out"}) 0 9 9
Condition66%2/3
defined $args{"in"} and defined $args{"out"}
!ll&&!rl&&r
270
MC/DC0%0/2
defined $args{"in"} and defined $args{"out"}
defined $args{"in"} defined $args{"out"}
Truth table66%2/3
A and B
A= defined $args{"in"}B= defined $args{"out"}
ABresult
0X0
100
111
26 0 cluck q(ERROR: cannot define both 'in' and 'out' arguments!);
27 0 return;
28 }
29 9 ▶ $args{noheader} ||= 0;
Condition50%1/2
$args{"noheader"} ||= 0
l!l
09
MC/DC0%0/1
$args{"noheader"} ||= 0
$args{"noheader"}
Truth table50%1/2
A
A= $args{"noheader"}
Aresult
11
00
30  
31 # prepare object
32 9 my $self = $class->SUPER::new();
33  
34 # open an existing file for reading
35 9 ▶ if ( $args{in} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'in'}) { } 7 2 9
elsif ($args{'out'}) { } 2 0 2
36  
37 # check and open file
38 7 my $filename = $self->check_file( $args{in} );
39 7 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 7 7
40 0 carp sprintf "ERROR: file '%s' does not exist!", $args{in};
41 0 return;
42 }
43 7 $self->add_file_metadata($filename);
44 7 ▶ $self->open_to_read_fh or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->open_to_read_fh or return 7 0 7
45 7 $self->{mode} = 0; # read mode
46  
47 # parse column headers
48 7 $self->parse_headers( $args{noheader} );
49 7 $self->{line_count} = $self->{header_line_count};
50  
51 # add example row, this will get tossed when the first next_row() is called
52 7 $self->{data_table}->[1] = $self->{example};
53 7 delete $self->{example};
54 }
55  
56 # prepare to write to a new stream
57 elsif ( $args{out} ) {
58  
59 # add file name information
60 2 $self->add_file_metadata( $args{out} );
61  
62 # we will not open the file handle quite yet in case the user
63 # wants to modify metadata
64 2 $self->{mode} = 1; # set to write mode
65 2 $self->{fh} = undef;
66  
67 # get names of columns user may have passed
68 2 my @columns;
69 2 ▶ if ( exists $args{columns} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'columns'}) { } 1 1 2
elsif (exists $args{'datasets'}) { } 0 1 1
70 1 @columns = @{ $args{columns} };
71 }
72 elsif ( exists $args{datasets} ) {
73 0 @columns = @{ $args{datasets} };
74 }
75  
76 # add the column names
77 2 ▶ if (@columns) {
Branch: 4 branches
BranchTrueFalseTotal
if (@columns) { } 1 1 2
elsif (exists $args{'gff'} and $args{'gff'}) { } 0 1 1
elsif (exists $args{'bed'} and $args{'bed'}) { } 1 0 1
elsif (exists $args{'ucsc'} and $args{'ucsc'}) { } 0 0 0
78 1 foreach my $c (@columns) {
79 4 $self->add_column($c);
80 }
81 1 ▶ if ( exists $args{noheader} and defined $args{noheader} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"noheader"} and defined $args{"noheader"}) 1 0 1
Condition33%1/3
exists $args{"noheader"} and defined $args{"noheader"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $args{"noheader"} and defined $args{"noheader"}
exists $args{"noheader"} defined $args{"noheader"}
Truth table33%1/3
A and B
A= exists $args{"noheader"}B= defined $args{"noheader"}
ABresult
0X0
100
111
82 1 $self->headers( $args{noheader} );
83 }
84 }
85 ▶ elsif ( exists $args{gff} and $args{gff} ) {
Condition33%1/3
exists $args{"gff"} and $args{"gff"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args{"gff"} and $args{"gff"}
exists $args{"gff"} $args{"gff"}
Truth table33%1/3
A and B
A= exists $args{"gff"}B= $args{"gff"}
ABresult
0X0
100
111
86  
87 # use standard names for the number of columns indicated
88 # we trust that the user knows the subtle difference between gff versions
89 0 $self->add_gff_metadata( $args{gff} );
90 0 ▶ unless ( $self->extension =~ /g[tf]f/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->extension =~ /g[tf]f/) 0 0 0
91 $self->{extension} =
92 $args{gff} == 2.5 ? '.gtf'
93 0 ▶ : $args{gff} == 3 ? '.gff3'
Branch: 2 branches
BranchTrueFalseTotal
$args{'gff'} == 2.5 ? : 0 0 0
$args{'gff'} == 3 ? : 0 0 0
94 : '.gff';
95 }
96 }
97 ▶ elsif ( exists $args{bed} and $args{bed} ) {
Condition33%1/3
exists $args{"bed"} and $args{"bed"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $args{"bed"} and $args{"bed"}
exists $args{"bed"} $args{"bed"}
Truth table33%1/3
A and B
A= exists $args{"bed"}B= $args{"bed"}
ABresult
0X0
100
111
98  
99 # use standard names for the number of columns indicated
100 1 ▶ unless ( $args{bed} =~ /^\d{1,2}$/ and $args{bed} >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"bed"} =~ /^\d{1,2}$/ and $args{"bed"} >= 3) 0 1 1
Condition33%1/3
$args{"bed"} =~ /^\d{1,2}$/ and $args{"bed"} >= 3
!ll&&!rl&&r
001
MC/DC0%0/2
$args{"bed"} =~ /^\d{1,2}$/ and $args{"bed"} >= 3
$args{"bed"} =~ /^\d{1,2}$/ $args{"bed"} >= 3
Truth table33%1/3
A and B
A= $args{"bed"} =~ /^\d{1,2}$/B= $args{"bed"} >= 3
ABresult
0X0
100
111
101 0 carp 'ERROR: bed parameter must be an integer 3-12!';
102 0 return;
103 }
104 1 $self->add_bed_metadata( $args{bed} );
105 1 ▶ unless ( $self->extension =~ /bed|peak/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->extension =~ /bed|peak/) 0 1 1
106 0 $self->{extension} = '.bed';
107 }
108 }
109 ▶ elsif ( exists $args{ucsc} and $args{ucsc} ) {
Condition0%0/3
exists $args{"ucsc"} and $args{"ucsc"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"ucsc"} and $args{"ucsc"}
exists $args{"ucsc"} $args{"ucsc"}
Truth table0%0/3
A and B
A= exists $args{"ucsc"}B= $args{"ucsc"}
ABresult
0X0
100
111
110  
111 # a ucsc format such as refFlat, genePred, or genePredExt
112 0 my $u = $self->add_ucsc_metadata( $args{ucsc} );
113 0 ▶ unless ($u) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u) 0 0 0
114 0 carp 'ERROR: unrecognized number of columns for ucsc format!';
115 0 return;
116 }
117 0 ▶ unless ( $self->extension =~ m/(?: ucsc | ref+lat | genepred )/xi ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->extension =~ /(?: ucsc | ref+lat | genepred )/xi) 0 0 0
118 0 $self->{extension} = '.ucsc';
119 }
120 }
121  
122 # else it will be an empty object with no columns
123  
124 # append gz if necessary
125 2 ▶ if ( exists $args{gz} and $args{gz} and $self->extension !~ /gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"gz"} and $args{"gz"} and not $self->extension =~ /gz$/) 0 2 2
Condition33%2/6
exists $args{"gz"} and $args{"gz"} and not $self->extension =~ /gz$/
!ll&&!rl&&r
200
exists $args{"gz"} and $args{"gz"}
!ll&&!rl&&r
200
MC/DC0%0/3
exists $args{"gz"} and $args{"gz"} and not $self->extension =~ /gz$/
exists $args{"gz"} $args{"gz"} not $self->extension =~ /gz$/
Truth table25%1/4
A and B and not(C)
A= exists $args{"gz"}B= $args{"gz"}C= not $self->extension =~ /gz$/
ABCresult
0XX0
10X0
1100
1111
126 0 $self->{extension} .= '.gz';
127 }
128  
129 # rebuild the filename after modifying the extension
130 2 $self->{filename} = $self->{path} . $self->{basename} . $self->{extension};
131  
132 # add feature
133 2 ▶ $args{feature} ||= $args{features} || undef;
Condition40%2/5
$args{"feature"} ||= $args{"features"} || undef
l!l&&r!l&&!r
002
$args{'features'} || undef
l!l
02
MC/DC0%0/2
$args{"feature"} ||= $args{"features"} || undef
$args{"feature"} $args{'features'}
Truth table33%1/3
A ||= B
A= $args{"feature"}B= $args{'features'}
ABresult
1X1
011
000
134 2 ▶ $self->feature( $args{feature} ) unless $self->feature;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->feature 1 1 2
135 }
136  
137 9 return $self;
138 }
139  
140 2 ▶sub duplicate {
Subroutine duplicate: called CC=9 SCAR=22.5
Pod documented
141 2 my $self = shift;
142 2 ▶ my $filename = shift || undef;
Condition100%2/2
shift() || undef
l!l
11
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
143 2 ▶ if ( $filename and $filename eq $self->filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename and $filename eq $self->filename) 0 2 2
Condition66%2/3
$filename and $filename eq $self->filename
!ll&&!rl&&r
110
MC/DC0%0/2
$filename and $filename eq $self->filename
$filename $filename eq $self->filename
Truth table66%2/3
A and B
A= $filenameB= $filename eq $self->filename
ABresult
0X0
100
111
144 0 carp 'ERROR: provided filename is not unique from that in metadata!';
145 0 return;
146 }
147  
148 # duplicate the data structure
149 2 my $columns = $self->list_columns;
150 2 my $Dup;
151 2 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 1 1 2
152 1 ▶ $Dup = $self->new(
Branch: 1 branch
BranchTrueFalseTotal
$Dup = $self->new("out", $filename, "columns", $columns) or return 1 0 1
153 'out' => $filename,
154 'columns' => $columns,
155 ) or return;
156 }
157 else {
158 1 ▶ $Dup = Bio::ToolBox::Data->new( 'columns' => $columns )
Branch: 1 branch
BranchTrueFalseTotal
$Dup = "Bio::ToolBox::Data"->new("columns", $columns) or return 1 0 1
159 or return;
160 }
161  
162 # copy the metadata
163 2 for my $i ( 1 .. $self->number_columns ) {
164  
165 # column metadata
166 14 my %md = $self->metadata($i);
167 14 $Dup->{$i} = \%md;
168 }
169 2 foreach (qw(feature program db bed gff vcf ucsc headers)) {
170  
171 # various keys
172 16 $Dup->{$_} = $self->{$_};
173 }
174 2 my @comments = $self->comments;
175 2 push @{ $Dup->{comments} }, @comments;
176  
177 2 return $Dup;
178 }
179  
180 ### Column manipulation
181  
182 4 ▶sub add_column {
Subroutine add_column: called CC=5 SCAR=18.2
Pod documented
183 4 my ( $self, $name ) = @_;
184 4 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
185 4 ▶ unless ( $self->mode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->mode) 0 4 4
186 0 confess 'FATAL: We have a read-only Stream object, cannot add columns';
187 0 return;
188 }
189 4 ▶ if ( defined $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fh"}) 0 4 4
190  
191 # Stream file handle is opened
192 0 confess 'FATAL: Cannot modify columns when a Stream file handle is opened!';
193 0 return;
194 }
195  
196 4 my $column = $self->number_columns + 1;
197 4 $self->{$column} = {
198 'name' => $name,
199 'index' => $column,
200 };
201 4 $self->{data_table}->[0][$column] = $name;
202 4 $self->{number_columns} = $column;
203 4 ▶ delete $self->{column_indices} if exists $self->{column_indices};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"column_indices"} 0 4 4
204  
205 4 return $column;
206 }
207  
208 0 ▶sub copy_column {
Subroutine copy_column: not called CC=4 SCAR=30.0
Pod documented
209 0 my $self = shift;
210 0 ▶ unless ( $self->mode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->mode) 0 0 0
211 0 confess 'FATAL: We have a read-only Stream object, cannot add columns';
212 }
213 0 ▶ if ( defined $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fh"}) 0 0 0
214  
215 # Stream file handle is opened
216 0 confess 'FATAL: modify columns when a Stream file handle is opened!';
217 }
218 0 my $index = shift;
219 0 ▶ return unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
220  
221 0 my $new_index = $self->add_column( $self->name($index) );
222 0 $self->copy_metadata( $index, $new_index );
223 0 return $new_index;
224 }
225  
226 #### Row Access ####
227  
228 # aliases
229 1 *next_line = *read_line = \&next_row;
230 1 *add_row = *add_line = *write_line = \&write_row;
231  
232 # stop once warnings
233 1 *next_line if 0;
234 1 *read_line if 0;
235 1 *add_row if 0;
236 1 *add_line if 0;
237 1 *write_line if 0;
238  
239 207 ▶sub next_row {
Subroutine next_row: called CC=4 SCAR=14.7
Pod documented
240 207 my $self = shift;
241 207 ▶ if ( $self->{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"}) 0 207 207
242 0 confess 'FATAL: Stream object is write-only! cannot read';
243 }
244  
245 # read and add the next line in the file
246 207 ▶ my $line = $self->{fh}->getline or return;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $self->{"fh"}->getline or return 201 6 207
247 201 $self->{line_count}++;
248 201 ▶ if ( substr( $line, 0, 1 ) eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 1) eq "#") 0 201 201
249  
250 # we shouldn't have internal comment lines, but just in case....
251 # could be a gff3 pragma
252 0 $self->add_comment($line);
253 0 return $self->next_row;
254 }
255  
256 # add the current line to the data table as row 1
257 201 pop @{ $self->{data_table} }; # remove the old line
258 201 $self->add_data_line($line);
259  
260 # return the feature
261 201 return Bio::ToolBox::Data::Feature->new(
262 'data' => $self,
263 'index' => 1,
264 );
265 }
266  
267 78 ▶sub write_row {
Subroutine write_row: called CC=9 SCAR=25.3
Pod documented
268 78 my $self = shift;
269 78 my $data = shift;
270 78 ▶ unless ( $self->{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"mode"}) 0 78 78
271 0 confess 'FATAL: Stream object is read-only! cannot write';
272 }
273  
274 # open the file handle if it hasn't been opened yet
275 78 ▶ unless ( defined $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 2 76 78
276  
277 # we first write a standard empty data file with metadata and headers
278 2 my $newfile = $self->write_file( $self->filename );
279 2 ▶ unless ($newfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newfile) 0 2 2
280 0 croak "FATAL: unable to write file! $OS_ERROR";
281 }
282  
283 # just in case the filename is changed when writing the file
284 2 ▶ if ( $newfile ne $self->filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newfile ne $self->filename) 0 2 2
285 0 $self->add_file_metadata($newfile);
286 }
287  
288 # then we re-open the file for appending
289 2 ▶ my $fh = $self->open_to_write_fh( $newfile, undef, 1 )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->open_to_write_fh($newfile, undef, 1) or croak("FATAL: unable to append to file $newfile! $Bio::ToolBox::Data::Stream::OS_ERROR") 2 0 2
290 or croak "FATAL: unable to append to file $newfile! $OS_ERROR";
291 2 $self->{fh} = $fh;
292 }
293  
294 # identify what kind of data we are dealing with
295 78 my $data_ref = ref $data;
296 78 ▶ if ( $data_ref eq 'Bio::ToolBox::Data::Feature' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data_ref eq 'Bio::ToolBox::Data::Feature') { } 39 39 78
elsif ($data_ref eq 'ARRAY') { } 39 0 39
297  
298 # user passed a Feature object
299 39 $self->{fh}->printf( "%s\n", join( "\t", ( $data->row_values ) ) );
300 }
301 elsif ( $data_ref eq 'ARRAY' ) {
302  
303 # user passed an array of values
304 39 $self->{fh}->printf( "%s\n", join( "\t", @{$data} ) );
305 }
306 else {
307 # assume the passed data is a string
308 # make sure it has a newline
309 0 ▶ unless ( $data =~ /\n$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ /\n$/) 0 0 0
310 0 $data .= "\n";
311 }
312 0 $self->{fh}->print($data);
313 }
314 78 return 1;
315 }
316  
317 0 ▶sub iterate {
Subroutine iterate: not called CC=3 SCAR=24.8
Pod documented
318 0 my $self = shift;
319 0 my $code = shift;
320 0 ▶ unless ( ref($code) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code eq "CODE") 0 0 0
321 0 carp 'ERROR: iterate_function() method requires a code reference!';
322 0 return;
323 }
324 0 ▶ while ( my $row = $self->next_row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next_row) 0 0 0
325 0 &{$code}($row);
326 }
327 0 return 1;
328 }
329  
330 #### File handle ####
331  
332 4 ▶sub mode {
Subroutine mode: called CC=1 SCAR=0.0
Pod documented
333 4 my $self = shift;
334 4 return $self->{mode};
335 }
336  
337 9 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
338 9 my $self = shift;
339 9 $self->close_fh;
340 }
341  
342 1 1;
343  
344 __END__
« script/data2fasta.pl ↑ Summary script/ucsc_table2gff3.pl »