lib/Bio/ToolBox/Data/file.pm

filter: Statementstmt 54.0% 373 / 690 Branchbran 42.1% 198 / 470 Conditioncond 33.7% 61 / 181 MC/DCmcdc 6.8% 7 / 102 Subroutinesub 93.5% 29 / 31 Podpod 100% 21 / 21 totaltotal 46.0% 689 / 1495 CC 313 SCAR 69.0
CC
313
Cov
47%
write_file
2891.3
parse_headers
397.7
taste_file
271.3
CRAP 992.5
« lib/Bio/ToolBox/Data/core.pm ↑ Summary lib/Bio/ToolBox/db_helper.pm »
1 package Bio::ToolBox::Data::file;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw(carp cluck croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '2.03';
12  
13 # List of acceptable filename extensions
14 4 our $SUFFIX =
15 qr/\.(?: txt | tsv | gff3? | gtf | bed | bg | bdg | bedgraph | sgr | kgg | cdt | vcf | narrowpeak | broadpeak | gappedpeak | reff?lat | genepred | ucsc | maf) (?: \.gz | \.bz2 )?/xi;
16  
17 # gzip application
18 4 my $gzip_app;
19 my $bgzip_app;
20  
21 ### Load new version data table from file
22  
23 11 ▶sub load_file {
Subroutine load_file: called CC=11 SCAR=27.2
Pod documented
24 11 my $self = shift;
25 11 my $file = shift;
26 11 ▶ my $noheader = shift || 0; # may not be present
Condition50%1/2
shift() || 0
l!l
011
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
27  
28 # check that we have an empty table
29 11 ▶ if ( $self->last_row != 0 or $self->number_columns != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_row != 0 or $self->number_columns != 0) 0 11 11
Condition33%1/3
$self->last_row != 0 or $self->number_columns != 0
l!l&&r!l&&!r
0011
MC/DC0%0/2
$self->last_row != 0 or $self->number_columns != 0
$self->last_row != 0 $self->number_columns != 0
Truth table33%1/3
A or B
A= $self->last_row != 0B= $self->number_columns != 0
ABresult
1X1
011
000
30 0 confess 'FATAL: Cannot load file onto an existing data table!';
31 }
32  
33 # open the file and load metadata
34 11 ▶ unless ( $self->filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filename) 6 5 11
35 6 $self->add_file_metadata($file);
36 }
37 11 ▶ unless ( $self->check_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_file) 0 11 11
38 0 carp "ERROR: file '$file' cannot be read or is empty!";
39 0 return;
40 }
41 11 ▶ $self->open_to_read_fh or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->open_to_read_fh or return 11 0 11
42 11 $self->parse_headers($noheader);
43  
44 # Load the data table
45 11 ▶ while ( my $line = $self->{fh}->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->{"fh"}->getline) 203 11 214
46  
47 # the current file position should be at the beginning of the
48 # data table information
49 203 ▶ next if $line !~ m/\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\w+/ 0 203 203
50  
51 # skip comment and empty lines
52 203 ▶ if ( substr( $line, 0, 1 ) eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 1) eq "#") 0 203 203
53 0 $self->add_comment($line);
54 0 next;
55 }
56  
57 # process the line
58 203 $self->add_data_line($line);
59 }
60  
61 # completed loading the file
62 11 $self->close_fh;
63 11 delete $self->{fh};
64  
65 # verify the structure
66 11 ▶ return unless $self->verify;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verify 0 11 11
67  
68 # finished
69 11 return 1;
70 }
71  
72 47 ▶sub taste_file {
Subroutine taste_file: called CC=32 SCAR=56.0
Pod documented
73 47 my ( $self, $file ) = @_;
74 47 my $Taste = $self->new;
75 47 $Taste->add_file_metadata($file);
76 47 ▶ $Taste->check_file or return;
Branch: 1 branch
BranchTrueFalseTotal
$Taste->check_file or return 47 0 47
77 47 ▶ $Taste->open_to_read_fh or return;
Branch: 1 branch
BranchTrueFalseTotal
$Taste->open_to_read_fh or return 47 0 47
78 47 $Taste->parse_headers;
79  
80 # load first 10 data lines
81 47 my $i = 0;
82 47 ▶ while ( $i < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 390 27 417
83 390 ▶ my $line = $Taste->fh->getline or last;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $Taste->fh->getline or last 370 20 390
84 370 ▶ next if $line !~ m/\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\w+/ 0 370 370
85 370 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 370 370
86 370 $i += $Taste->add_data_line($line);
87 }
88 47 $Taste->close_fh;
89 47 $Taste->verify(1); # silently check the integrity of the file
90  
91 # check existing metadata
92 47 ▶ if ( $Taste->gff ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($Taste->gff) { } 12 35 47
elsif ($Taste->bed) { } 24 11 35
elsif ($Taste->ucsc) { } 0 11 11
elsif ($Taste->vcf) { } 0 11 11
elsif ($Taste->format) { } 0 11 11
93 12 return ( 'gff', $Taste->format );
94 }
95 elsif ( $Taste->bed ) {
96 24 return ( 'bed', $Taste->format );
97 }
98 elsif ( $Taste->ucsc ) {
99 0 return ( 'ucsc', $Taste->format );
100 }
101 elsif ( $Taste->vcf ) {
102 0 return ( 'vcf', $Taste->format );
103 }
104 elsif ( $Taste->format ) {
105 0 return ( q(), $Taste->format );
106 }
107  
108 # check if the number of columns match a known format, then verify
109 11 my $number = $Taste->number_columns;
110 11 ▶ if ( $number == 9 ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($number == 9) { } 0 11 11
elsif ($number == 10) { } 0 11 11
elsif ($number == 11) { } 0 11 11
elsif ($number == 12) { } 0 11 11
elsif ($number == 15) { } 0 11 11
elsif ($number == 16) { } 6 5 11
111  
112 # possibly a GFF file
113 0 $Taste->gff(2);
114 0 $Taste->verify(1);
115 0 ▶ if ( $Taste->gff == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->gff == 2) 0 0 0
116 0 return ( 'gff', $Taste->format );
117 }
118 }
119 elsif ( $number == 10 ) {
120  
121 # possibly a genePred file
122 0 $Taste->ucsc(10);
123 0 $Taste->verify(1);
124 0 ▶ if ( $Taste->ucsc == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 10) 0 0 0
125 0 return ( 'ucsc', $Taste->format );
126 }
127 0 $Taste->add_ucsc_metadata( 10, 1 ); # force metadata
128 0 $Taste->verify(1);
129 0 ▶ if ( $Taste->ucsc == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 10) 0 0 0
130 0 return ( 'ucsc', $Taste->format );
131 }
132 }
133 elsif ( $number == 11 ) {
134  
135 # possibly a refFlat file
136 0 $Taste->ucsc(11);
137 0 $Taste->verify(1);
138 0 ▶ if ( $Taste->ucsc == 11 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 11) 0 0 0
139 0 return ( 'ucsc', $Taste->format );
140 }
141 0 $Taste->add_ucsc_metadata( 11, 1 ); # force metadata
142 0 $Taste->verify(1);
143 0 ▶ if ( $Taste->ucsc == 11 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 11) 0 0 0
144 0 return ( 'ucsc', $Taste->format );
145 }
146 }
147 elsif ( $number == 12 ) {
148  
149 # possibly a knownGene or BED12 file
150 0 $Taste->ucsc(12);
151 0 $Taste->verify(1);
152 0 ▶ if ( $Taste->ucsc == 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 12) 0 0 0
153 0 return ( 'ucsc', $Taste->format );
154 }
155 0 $Taste->bed(12);
156 0 $Taste->verify(1);
157 0 ▶ if ( $Taste->bed == 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->bed == 12) 0 0 0
158 0 return ( 'bed', $Taste->format );
159 }
160 0 $Taste->add_ucsc_metadata( 12, 1 ); # force metadata
161 0 $Taste->verify(1);
162 0 ▶ if ( $Taste->ucsc == 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 12) 0 0 0
163 0 return ( 'ucsc', $Taste->format );
164 }
165 0 $Taste->add_bed_metadata( 12, 1 ); # force metadata
166 0 $Taste->verify(1);
167 0 ▶ if ( $Taste->bed == 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->bed == 12) 0 0 0
168 0 return ( 'bed', $Taste->format );
169 }
170 }
171 elsif ( $number == 15 ) {
172  
173 # possibly a genePredExt file
174 0 $Taste->ucsc(15);
175 0 $Taste->verify(1);
176 0 ▶ if ( $Taste->ucsc == 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 15) 0 0 0
177 0 return ( 'ucsc', $Taste->format );
178 }
179 0 $Taste->add_ucsc_metadata( 15, 1 ); # force metadata
180 0 $Taste->verify(1);
181 0 ▶ if ( $Taste->ucsc == 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 15) 0 0 0
182 0 return ( 'ucsc', $Taste->format );
183 }
184 }
185 elsif ( $number == 16 ) {
186  
187 # possibly a genePredExt file
188 6 $Taste->ucsc(16);
189 6 $Taste->verify(1);
190 6 ▶ if ( $Taste->ucsc == 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 16) 0 6 6
191 0 return ( 'ucsc', $Taste->format );
192 }
193 6 ▶ return 'ucsc' if $Taste->ucsc == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $Taste->ucsc == 16 0 6 6
194 6 $Taste->add_ucsc_metadata( 16, 1 ); # force metadata
195 6 $Taste->verify(1);
196 6 ▶ if ( $Taste->ucsc == 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Taste->ucsc == 16) 6 0 6
197 6 return ( 'ucsc', $Taste->format );
198 }
199 }
200 5 return ( undef, undef );
201 }
202  
203 11 ▶sub sample_gff_type_list {
Subroutine sample_gff_type_list: called CC=7 SCAR=20.1
Pod documented
204 11 my ( $self, $file ) = @_;
205 11 ▶ return unless $file =~ m/\.g[tf]f\d? (?:\.gz)? $/xi; # assume extension is accurate
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.g[tf]f\d? (?:\.gz)? $/xi 0 11 11
206 11 my $fh = $self->open_to_read_fh($file);
207 11 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 11 11
208 0 carp "ERROR: can't open $file!";
209 0 return;
210 }
211 11 my %types;
212 11 my $count = 0;
213 11 ▶ while ( $count < 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 1000) 1288 0 1288
214 1288 ▶ my $line = $fh->getline or last;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $fh->getline or last 1277 11 1288
215 1277 ▶ next if $line !~ m/\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\w+/ 0 1277 1277
216 1277 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 36 1241 1277
217 1241 my @fields = split /\t/, $line;
218 1241 $types{ $fields[2] } += 1;
219 1241 $count++;
220 }
221 11 $fh->close;
222 11 return join( ',', keys %types );
223 }
224  
225 65 ▶sub parse_headers {
Subroutine parse_headers: called CC=55 SCAR=59.9
Pod documented
226 65 my $self = shift;
227 65 ▶ my $noheader = shift || 0; # boolean to indicate no headers are present
Condition50%1/2
shift() || 0
l!l
065
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
228  
229 # filehandle
230 65 my $fh;
231 65 ▶ if ( exists $self->{fh} and $self->{fh} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'fh'} and $self->{'fh'}) { } 65 0 65
elsif ($self->filename) { } 0 0 0
Condition33%1/3
exists $self->{"fh"} and $self->{"fh"}
!ll&&!rl&&r
0065
MC/DC0%0/2
exists $self->{"fh"} and $self->{"fh"}
exists $self->{"fh"} $self->{"fh"}
Truth table33%1/3
A and B
A= exists $self->{"fh"}B= $self->{"fh"}
ABresult
0X0
100
111
232 65 $fh = $self->{fh};
233 }
234 elsif ( $self->filename ) {
235 0 $fh = $self->open_to_read_fh( $self->filename );
236 }
237 else {
238 0 confess
239 'FATAL: file metadata and/or open filehandle must be set before parsing headers!';
240 }
241  
242 # check that we have an empty table
243 65 ▶ if ( $self->last_row != 0 or $self->number_columns != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_row != 0 or $self->number_columns != 0) 0 65 65
Condition33%1/3
$self->last_row != 0 or $self->number_columns != 0
l!l&&r!l&&!r
0065
MC/DC0%0/2
$self->last_row != 0 or $self->number_columns != 0
$self->last_row != 0 $self->number_columns != 0
Truth table33%1/3
A or B
A= $self->last_row != 0B= $self->number_columns != 0
ABresult
1X1
011
000
244 0 confess 'FATAL: Cannot parse file headers onto an existing data table!';
245 }
246  
247 # read and parse the file
248 # we will ONLY parse the header lines prefixed with a #, as well as the
249 # first data row which contains the column names
250 65 $self->program(undef); # reset this to blank, it will be filled by file metadata
251 65 my $header_line_count = 0;
252 65 my $line = $fh->getline; # first line
253  
254 # check the first line for proper line endings
255 {
256 65 my $line2 = $line;
257 65 chomp($line2);
258 65 ▶ if ( $line2 =~ /[\r\n]+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line2 =~ /[\r\n]+/) 0 65 65
259 0 my $filename = $self->filename;
260 0 print STDERR "FATAL: File '$filename' does not have expected line endings!\n"
261 . " Try converting to native line endings and try again\n";
262 0 exit 1;
263 }
264 }
265  
266 65 ▶ while ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 160 65 225
267  
268 # we are not chomping the line here because of possible side effects
269 # with UCSC tables where we have to count elements in the first line
270 # and potential header lines, and the first line has a null value at
271 # the end
272  
273 # Parse the datafile metadata headers
274  
275 # no real line, just empty space
276 160 ▶ if ( $line !~ m/\w+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\w+/) 0 160 160
277 0 $header_line_count++;
278 0 $line = $fh->getline;
279 0 next;
280 }
281  
282 # the generating program
283 160 ▶ if ( $line =~ m/^\#\ Program\ (.+)$/x ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($line =~ /^\#\ Program\ (.+)$/x) { } 0 160 160
elsif ($line =~ /^\#\ Database\ (.+)$/x) { } 10 150 160
elsif ($line =~ /^\#\ Feature\ (.+)$/x) { } 4 146 150
elsif ($line =~ /^\#\ Column_(\d+)/x) { } 0 146 146
elsif ($line =~ /^\#\ Column(\d+):/x) { } 0 146 146
elsif ($line =~ /^\#\# (g[vf]f) . version\s+ ( [\d\.]+ )$/x) { } 13 133 146
elsif ($line =~ /^\#\# fileformat=VCFv ( [\d\.]+ )$/x) { } 0 133 133
elsif ($line =~ /^\#/) { } 62 71 133
elsif ($line =~ /^(?: track | browser )\s+/xi) { } 6 65 71
284 0 my $p = $1;
285 0 $self->program($p);
286 0 $header_line_count++;
287 }
288  
289 # the source database
290 elsif ( $line =~ m/^\#\ Database\ (.+)$/x ) {
291 10 my $d = $1;
292 10 $self->database($d);
293 10 $header_line_count++;
294 }
295  
296 # the type of feature in this datafile
297 elsif ( $line =~ m/^\#\ Feature\ (.+)$/x ) {
298 4 my $f = $1;
299 4 $self->feature($f);
300 4 $header_line_count++;
301 }
302  
303 # column or dataset specific information for BioToolBox versions < 1.70
304 elsif ( $line =~ m/^\#\ Column_(\d+)/x ) {
305  
306 # change index from 0-based to 1-base
307 0 my $index = $1 + 1;
308 0 $self->add_column_metadata( $line, $index );
309 0 $header_line_count++;
310 }
311  
312 # column or dataset specific information for BioToolBox versions >= 1.70
313 elsif ( $line =~ m/^\#\ Column(\d+):/x ) {
314  
315 # the column number will become the index number
316 0 my $index = $1;
317 0 $self->add_column_metadata( $line, $index );
318 0 $header_line_count++;
319 }
320  
321 # gff version header
322 elsif ( $line =~ m/^\#\# (g[vf]f) . version\s+ ( [\d\.]+ )$/x ) {
323  
324 # store the gff version in the hash
325 # this may or may not be present in the gff file, but want to keep
326 # it if it is
327 13 my $g = $1;
328 13 my $v = $2;
329 13 ▶ if ( $g eq 'gff' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g eq 'gff') { } 13 0 13
elsif ($g eq 'gvf') { } 0 0 0
330 13 $self->gff($v);
331 13 ▶ if ( $v == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v == 3) { } 9 4 13
elsif ($v > 2 and $v < 3) { } 4 0 4
332 9 $self->format('gff3');
333 }
334 ▶ elsif ( $v > 2 and $v < 3 ) {
Condition33%1/3
$v > 2 and $v < 3
!ll&&!rl&&r
004
MC/DC0%0/2
$v > 2 and $v < 3
$v > 2 $v < 3
Truth table33%1/3
A and B
A= $v > 2B= $v < 3
ABresult
0X0
100
111
335 4 $self->format('gtf');
336 }
337 else {
338 0 $self->format('gff');
339 }
340 }
341 elsif ( $g eq 'gvf' ) {
342 0 $self->format('gvf');
343 }
344  
345 # format gets properly set below when add
346 13 $header_line_count++;
347 }
348  
349 # VCF version header
350 elsif ( $line =~ m/^\#\# fileformat=VCFv ( [\d\.]+ )$/x ) {
351  
352 # store the VCF version in the hash
353 # this may or may not be present in the vcf file, but want to keep
354 # it if it is
355 0 my $v = $1;
356 0 $self->vcf($v);
357 0 $self->format( sprintf "VCFv%s", $v );
358 0 $self->add_comment($line); # so that it is written properly
359 0 $header_line_count++;
360 }
361  
362 # any other nonstandard header
363 elsif ( $line =~ /^\#/ ) {
364 62 $self->add_comment($line);
365 62 $header_line_count++;
366 }
367  
368 # a track or browser line
369 elsif ( $line =~ m/^(?: track | browser )\s+/xi ) {
370  
371 # common with wig, bed, or bedgraph files for use with UCSC genome browser
372 # treat as a comment line, there's not that much useful info here
373 6 ▶ if ( $line =~ /type=(\w+)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /type=(\w+)/i) 6 0 6
374 6 $self->format($1);
375 }
376 6 $self->add_comment($line);
377 6 $header_line_count++;
378 }
379  
380 # the remainder is the data table itself
381 else {
382 # the first row in the data table are (usually) the column names
383 # we only want the names, not the rest of the table
384  
385 # specific file formats have implicit predefined column formats
386 # these file formats do NOT have column headers
387 # we will first check for those file formats and process accordingly
388 65 ▶ my $format = $self->format || $self->extension;
Condition66%2/3
$self->format || $self->extension
l!l&&r!l&&!r
19460
MC/DC0%0/2
$self->format || $self->extension
$self->format $self->extension
Truth table66%2/3
A || B
A= $self->formatB= $self->extension
ABresult
1X1
011
000
389 65 my $count = scalar( split /\t/, $line );
390  
391 ### a GFF file
392 65 ▶ if ( $format =~ m/g[tvf]f/i ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($format =~ /g[tvf]f/i) { } 13 52 65
elsif ($format =~ /peak/i) { } 13 39 52
elsif ($format =~ /bg | bdg | bedgraph/xi and $count == 4) { } 0 39 39
elsif ($format =~ /bed/i) { } 23 16 39
elsif ($format =~ /(?: ref+lat | genepred | ucsc )/xi) { } 0 16 16
elsif ($format =~ /sgr/i) { } 0 16 16
393 13 ▶ if ( $count == 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 9) { } 13 0 13
394 13 $self->add_gff_metadata();
395 }
396 else {
397 0 print "WARNING: Incorrect column count for GFF format\n";
398 }
399 }
400  
401 ### a peak file
402 elsif ( $format =~ m/peak/i ) {
403 13 my $success = $self->add_peak_metadata($count);
404 13 ▶ unless ($success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 13 13
405  
406 # failed to add peak metadata because of incorrect column count
407 # check for a commented header line
408 0 ▶ if ( $self->_commented_header_line($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commented_header_line($line)) { } 0 0 0
409 0 my @header_names = split /\t/, pop @{ $self->{'comments'} };
410 0 chomp $header_names[-1];
411 0 $self->add_standard_metadata( \@header_names );
412  
413 # assume interbase and region
414 0 $self->interbase(1);
415 0 ▶ unless ( $self->feature ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->feature) 0 0 0
416 0 $self->feature('region');
417 }
418 }
419 else {
420 0 print
421 " WARNING: Incorrect column count for a known Encode Peak format\n";
422  
423 # first line will be column headers as below
424 }
425 }
426 }
427  
428 ### a BedGraph file
429 ▶ elsif ( $format =~ m/bg | bdg | bedgraph/xi and $count == 4 ) {
Condition33%1/3
$format =~ /bg | bdg | bedgraph/xi and $count == 4
!ll&&!rl&&r
3900
MC/DC0%0/2
$format =~ /bg | bdg | bedgraph/xi and $count == 4
$format =~ /bg | bdg | bedgraph/xi $count == 4
Truth table33%1/3
A and B
A= $format =~ /bg | bdg | bedgraph/xiB= $count == 4
ABresult
0X0
100
111
430 0 $self->add_bed_metadata($count);
431 }
432  
433 ### a Bed file
434 elsif ( $format =~ m/bed/i ) {
435  
436 # check for a commented header line first since these may be common
437 23 ▶ if ( $self->_commented_header_line($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commented_header_line($line)) { } 0 23 23
438 0 my @header_names = split /\t/, pop @{ $self->{'comments'} };
439 0 chomp $header_names[-1];
440 0 $self->add_standard_metadata( \@header_names );
441  
442 # we will not enforce bed structure by setting the bed flag
443 # but will assume 0-based formatting
444 0 $self->interbase(1);
445 0 ▶ unless ( $self->feature ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->feature) 0 0 0
446 0 $self->feature('region');
447 }
448 }
449 else {
450 23 $self->add_bed_metadata($count);
451 }
452 }
453  
454 ### a UCSC gene table
455 elsif ( $format =~ m/(?: ref+lat | genepred | ucsc )/xi ) {
456 0 my $s = $self->add_ucsc_metadata($count);
457 0 ▶ unless ($s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s) 0 0 0
458  
459 # failed to add ucsc metadata because of incorrect column count
460 # check for a commented header line
461 0 ▶ if ( $self->_commented_header_line($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commented_header_line($line)) { } 0 0 0
462 0 my @header_names = split /\t/, pop @{ $self->{'comments'} };
463 0 chomp $header_names[-1];
464 0 $self->add_standard_metadata( \@header_names );
465 }
466 else {
467 0 print
468 " WARNING: Incorrect column count for a standard UCSC table format\n";
469  
470 # first line will be column headers as below
471 }
472 }
473 }
474  
475 ### a SGR file
476 elsif ( $format =~ m/sgr/i ) {
477 0 $self->add_sgr_metadata;
478 }
479  
480 ### standard text file with assumed headers, i.e. everything else
481 # we know this because no columns have been set yet
482 65 ▶ unless ( $self->number_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->number_columns) 16 49 65
483  
484 # check for a commented header line
485 16 ▶ if ( $self->_commented_header_line($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_commented_header_line($line)) { } 0 16 16
486 0 my @header_names = split /\t/, pop @{ $self->{'comments'} };
487 0 chomp $header_names[-1];
488 0 $self->add_standard_metadata( \@header_names );
489 }
490 else {
491 # we have not yet parsed the row of data column names
492 # we will do so now
493 16 chomp $line;
494 16 my @header_names = split /\t/, $line;
495 16 $self->add_standard_metadata( \@header_names );
496  
497 # count as a header line
498 16 $header_line_count++;
499 }
500 }
501 }
502  
503 # keep processing by going to the next line until we have identified columns
504 160 ▶ if ( $self->number_columns == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->number_columns == 0) { } 95 65 160
505 95 $line = $fh->getline;
506 }
507 else {
508 65 undef $line;
509 }
510 }
511  
512 # No header was requested
513 65 ▶ if ( $noheader
Branch: 1 branch
BranchTrueFalseTotal
if ($noheader and not $self->bed and not $self->gff and not $self->vcf and not $self->ucsc) 0 65 65
Condition33%4/12
$noheader and not $self->bed and not $self->gff and not $self->vcf and not $self->ucsc
!ll&&!rl&&r
6500
$noheader and not $self->bed and not $self->gff and not $self->vcf
!ll&&!rl&&r
6500
$noheader and not $self->bed and not $self->gff
!ll&&!rl&&r
6500
$noheader and not $self->bed
!ll&&!rl&&r
6500
MC/DC0%0/5
$noheader and not $self->bed and not $self->gff and not $self->vcf and not $self->ucsc
$noheader not $self->bed not $self->gff not $self->vcf not $self->ucsc
Truth table16%1/6
A and not(B) and not(C) and not(D) and not(E)
A= $noheaderB= not $self->bedC= not $self->gffD= not $self->vcfE= not $self->ucsc
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
514 and not $self->bed
515 and not $self->gff
516 and not $self->vcf
517 and not $self->ucsc )
518 {
519 # which means that what we used as a header is actually the first data row
520  
521 # fix the column names
522 0 for my $i ( 1 .. $self->number_columns ) {
523 0 my $name = $self->name($i);
524 0 $self->name( $i, "Column$i ($name)" );
525 0 $self->{$i}{'AUTO'} = 3;
526 }
527  
528 # adjust metadata
529 0 $header_line_count -= 1;
530 0 $self->headers(0);
531 }
532  
533 # Header sanity check
534 # some exported file formats, such as from R, do not include a proper
535 # header for the first column, as these are assumed to be row names
536 # this will result in incorrectly parsed files where the last columns
537 # will be merged into one column with an internal tab - not good
538 # need to handle these
539 65 my $nextline = $fh->getline;
540 65 ▶ if ($nextline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextline) 65 0 65
541 65 my @nextdata = split /\t/, $nextline;
542 65 ▶ if ( scalar(@nextdata) - 1 == $self->number_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@nextdata) - 1 == $self->number_columns) 0 65 65
543  
544 # whoops! we caught a off-by-one discrepancy between header and data row
545 0 my $old_last = $self->last_column;
546 0 $fh->close; # having this open complicates changing columns....
547  
548 # add a new "column" (just metadata for now) and move it to the beginning
549 0 my $i = $self->add_column('Column1');
550 0 $self->reorder_column( $i, 1 .. $old_last );
551 }
552 65 ▶ if ( ref($self) eq 'Bio::ToolBox::Data::Stream' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Bio::ToolBox::Data::Stream") 7 58 65
553  
554 # store an example first line for Stream objects
555 7 chomp $nextdata[-1];
556 7 $self->{example} = \@nextdata;
557 }
558 }
559  
560 # re-open the file
561 # I tried using seek functions - but they don't work with binary gzip
562 # files, and I can't get the seek function to return the same position
563 # as simply advancing through the file like below
564 # so I'll just do it the old way and close/open and advance
565 65 ▶ $fh->close if $fh; # may have been closed from the sanity check above
Branch: 1 branch
BranchTrueFalseTotal
if $fh 65 0 65
566 65 $fh = $self->open_to_read_fh;
567 65 for ( 1 .. $header_line_count ) {
568 111 my $discard = $fh->getline;
569 }
570 65 $self->{header_line_count} = $header_line_count;
571 65 $self->{fh} = $fh;
572 65 return 1;
573 }
574  
575 774 ▶sub add_data_line {
Subroutine add_data_line: called CC=2 SCAR=7.2
Pod documented
576 774 my ( $self, $line ) = @_;
577  
578 # do not chomp the line yet, just split into an array
579 774 my @linedata = split /\t/, $line, $self->{number_columns};
580  
581 # chomp the last element
582 # we do this here to ensure the tab split above gets all of the values
583 # otherwise trailing null values aren't included in @linedata
584 774 chomp $linedata[-1];
585  
586 # check for extra remaining tabs
587 774 ▶ if ( index( $linedata[-1], "\t" ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($linedata[-1], "\t") != -1) 0 774 774
588 0 print STDERR
589 "FILE INCONSISTENCY ERRORS! line has additional tabs (columns) than headers!\n $line\n";
590 0 exit 1;
591 }
592  
593 # add the line of data
594 774 unshift @linedata, q(); # empty blank column
595 774 push @{ $self->{data_table} }, \@linedata;
596 774 $self->{last_row} += 1;
597 774 return 1;
598 }
599  
600 ### Parse the filename using the list suffix list
601 83 ▶sub add_file_metadata {
Subroutine add_file_metadata: called CC=4 SCAR=17.4
Pod documented
602 83 my ( $self, $filename ) = @_;
603 83 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 83 83
604 0 carp 'ERROR: no valid filename!';
605 0 return;
606 }
607 83 my ( $basename, $path, $extension ) = fileparse( $filename, $SUFFIX );
608 83 ▶ unless ($extension) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extension) 0 83 83
609  
610 # look for a nonstandard extension, allowing for .gz extension
611 0 ▶ if ( $filename =~ m/( \.\w+ (?:\.gz)? )$/xi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /( \.\w+ (?:\.gz)? )$/xi) 0 0 0
612 0 $extension = $1;
613 0 $basename =~ s/$extension\Z//x;
614 }
615 }
616 83 $self->{filename} = $filename;
617 83 $self->{basename} = $basename;
618 83 $self->{path} = $path;
619 83 $self->{extension} = $extension;
620 }
621  
622 ### Write out a data file
623 9 ▶sub write_file {
Subroutine write_file: called CC=104 SCAR=79.7
Pod documented
624 9 my $self = shift;
625  
626 # collect passed arguments
627 9 my %args;
628 9 ▶ if ( scalar(@_) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 8 1 9
629 8 $args{'filename'} = $_[0];
630 }
631 else {
632 1 %args = @_;
633 }
634 9 ▶ $args{'filename'} ||= $args{'file'} || undef;
Condition20%1/5
$args{"filename"} ||= $args{"file"} || undef
l!l&&r!l&&!r
900
$args{'file'} || undef
l!l
00
MC/DC0%0/2
$args{"filename"} ||= $args{"file"} || undef
$args{"filename"} $args{'file'}
Truth table33%1/3
A ||= B
A= $args{"filename"}B= $args{'file'}
ABresult
1X1
011
000
635 9 ▶ $args{'format'} ||= undef;
Condition50%1/2
$args{"format"} ||= undef
l!l
09
MC/DC0%0/1
$args{"format"} ||= undef
$args{"format"}
Truth table50%1/2
A
A= $args{"format"}
Aresult
11
00
636 9 ▶ unless ( exists $args{'gz'} ) { $args{'gz'} = undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"gz"}) 9 0 9
637  
638 # check filename
639 9 ▶ if ( not $args{'filename'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"filename"}) 0 9 9
640  
641 # user did not provide a filename, so we create one from the metadata
642 # but first check the format
643 0 ▶ if ( $self->verify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify) { } 0 0 0
644  
645 # recreate the filename from metadata
646 # the extension may have been automatically changed by verify method
647 # the user will have been warned
648 0 my $name = $self->path . $self->basename . $self->extension;
649 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
650 0 $args{'filename'} = $name;
651 }
652 else {
653 0 carp 'ERROR: no passed filename or original filename metadata!';
654 0 return;
655 }
656 }
657 else {
658 0 carp 'ERROR: no provided filename AND bad data structure!!';
659 0 return;
660 }
661 }
662  
663 # split filename into its base components
664 my ( $name, $path, $extension ) =
665 9 ▶ fileparse( $args{'filename'} || $self->filename, $SUFFIX );
Condition33%1/3
$args{'filename'} || $self->filename
l!l&&r!l&&!r
900
MC/DC0%0/2
$args{'filename'} || $self->filename
$args{'filename'} $self->filename
Truth table33%1/3
A || B
A= $args{'filename'}B= $self->filename
ABresult
1X1
011
000
666  
667 # check extension
668 9 ▶ unless ($extension) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extension) 0 9 9
669  
670 # try some possibilities
671 0 ▶ if ( $name =~ /^(.+)(\.\w+)(\.gz)?$/xi ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($name =~ /^(.+)(\.\w+)(\.gz)?$/xi) { } 0 0 0
elsif ($self->gff) { } 0 0 0
elsif ($self->bed) { } 0 0 0
elsif ($self->ucsc) { } 0 0 0
elsif ($self->vcf) { } 0 0 0
672  
673 # nonstandard extension
674 0 ▶ $extension = $3 ? $2 . $3 : $2;
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 0 0 0
675 0 $name = $1;
676 }
677 elsif ( $self->gff ) {
678 0 ▶ if ( $self->gff == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->gff == 3) { } 0 0 0
elsif ($self->gff > 2 and $self->gff < 3) { } 0 0 0
679 0 $extension = '.gff3';
680 }
681 ▶ elsif ( $self->gff > 2 and $self->gff < 3 ) {
Condition0%0/3
$self->gff > 2 and $self->gff < 3
!ll&&!rl&&r
000
MC/DC0%0/2
$self->gff > 2 and $self->gff < 3
$self->gff > 2 $self->gff < 3
Truth table0%0/3
A and B
A= $self->gff > 2B= $self->gff < 3
ABresult
0X0
100
111
682 0 $extension = '.gtf';
683 }
684 else {
685 0 $extension = '.gff';
686 }
687 }
688 elsif ( $self->bed ) {
689 0 ▶ if ( $self->format ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->format) { } 0 0 0
elsif ($self->number_columns == 4 and $self->name(4) =~ /score/i) { } 0 0 0
690  
691 # re-use the format value as the extension
692 0 $extension = sprintf ".%s", $self->format;
693 0 $extension =~ s/\d+$//; # remove any digit after the bed format
694 }
695 ▶ elsif ( $self->number_columns == 4
Condition0%0/3
$self->number_columns == 4 and $self->name(4) =~ /score/i
!ll&&!rl&&r
000
MC/DC0%0/2
$self->number_columns == 4 and $self->name(4) =~ /score/i
$self->number_columns == 4 $self->name(4) =~ /score/i
Truth table0%0/3
A and B
A= $self->number_columns == 4B= $self->name(4) =~ /score/i
ABresult
0X0
100
111
696 and $self->name(4) =~ /score/i )
697 {
698 0 $extension = '.bdg'; # looks like a bedGraph file
699 }
700 else {
701 0 $extension = '.bed'; # a regular bed file
702 }
703 }
704 elsif ( $self->ucsc ) {
705 0 ▶ if ( $self->format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format) { } 0 0 0
706  
707 # re-use the format value as the extension
708 0 $extension = sprintf( ".%s", $self->format );
709 }
710 else {
711 # use a generic ucsc format
712 0 $extension = '.ucsc';
713 }
714 }
715 elsif ( $self->vcf ) {
716 0 $extension = '.vcf';
717 }
718 else {
719 # presume a standard text file
720 0 $extension = '.txt';
721 }
722 }
723  
724 # Verify and adjust filename extension if necessary for specific formats
725 9 ▶ if ( $extension =~ /txt/i ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($extension =~ /txt/i) { } 6 3 9
elsif ($extension =~ /(?: g[tf]f )/xi) { } 0 3 3
elsif ($extension =~ /(?: bedgraph | bed | bdg | narrowpeak | broadpeak )/xi) { } 3 0 3
elsif ($extension =~ /vcf/i) { } 0 0 0
elsif ($extension =~ /sgr/i) { } 0 0 0
elsif ($extension =~ /(?: reff?lat | genepred | ucsc )/xi) { } 0 0 0
726 6 ▶ if ( not $self->headers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->headers) 0 6 6
727  
728 # set headers to true for ordinary text or tsv files
729 # unless they appear to be self-named generic names
730 # this is a hack around preserving no column names when noheader was used
731 # only checking the first column, which may not be sufficient in some cases
732 0 ▶ unless ( $self->name(1) =~ /^Column\d+ \s \(.+/x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name(1) =~ /^Column\d+ \s \(.+/x) 0 0 0
733 0 $self->headers(1);
734 }
735 }
736 }
737 elsif ( $extension =~ /(?: g[tf]f )/xi ) {
738 0 ▶ if ( not $self->gff ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->gff) 0 0 0
739  
740 # let's set it to true and see if it passes verification
741 0 ▶ $self->{'gff'} = $extension =~ /gtf/i ? 2.5 : 3; # default
Branch: 1 branch
BranchTrueFalseTotal
$extension =~ /gtf/i ? : 0 0 0
742 0 ▶ unless ( $self->verify and $self->gff ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->verify and $self->gff) 0 0 0
Condition0%0/3
$self->verify and $self->gff
!ll&&!rl&&r
000
MC/DC0%0/2
$self->verify and $self->gff
$self->verify $self->gff
Truth table0%0/3
A and B
A= $self->verifyB= $self->gff
ABresult
0X0
100
111
743 0 printf STDERR
744 " GFF structure invalid, re-setting extension from %s to %s\n",
745 $extension, $self->extension;
746 0 $extension = $self->extension;
747 }
748 }
749 }
750 elsif ( $extension =~ m/(?: bedgraph | bed | bdg | narrowpeak | broadpeak )/xi ) {
751 3 ▶ if ( not $self->bed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->bed) 0 3 3
752  
753 # let's set it to true and see if it passes verification
754 0 $self->{'bed'} = 1; # a fake true
755 0 ▶ unless ( $self->verify and $self->bed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->verify and $self->bed) 0 0 0
Condition0%0/3
$self->verify and $self->bed
!ll&&!rl&&r
000
MC/DC0%0/2
$self->verify and $self->bed
$self->verify $self->bed
Truth table0%0/3
A and B
A= $self->verifyB= $self->bed
ABresult
0X0
100
111
756 0 printf STDERR
757 " BED structure invalid, re-setting extension from %s to %s\n",
758 $extension, $self->extension;
759 0 $extension = $self->extension;
760 }
761 }
762 }
763 elsif ( $extension =~ /vcf/i ) {
764 0 ▶ if ( not $self->vcf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->vcf) 0 0 0
765  
766 # let's set it to true and see if it passes verification
767 0 $self->{'vcf'} = 1; # a fake true
768 0 ▶ unless ( $self->verify and $self->vcf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->verify and $self->vcf) 0 0 0
Condition0%0/3
$self->verify and $self->vcf
!ll&&!rl&&r
000
MC/DC0%0/2
$self->verify and $self->vcf
$self->verify $self->vcf
Truth table0%0/3
A and B
A= $self->verifyB= $self->vcf
ABresult
0X0
100
111
769 0 printf STDERR
770 " VCF structure changed, re-setting extension from %s to %s\n",
771 $extension, $self->extension;
772 0 $extension = $self->extension;
773 }
774 }
775 }
776 elsif ( $extension =~ /sgr/i ) {
777 0 ▶ unless ( $self->{'extension'} =~ /sgr/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"extension"} =~ /sgr/i) 0 0 0
778  
779 # original file was not SGR
780 # let's pretend it was and see if still passes
781 # the sgr verification relies on the recorded extension
782 0 $self->{'extension'} = '.sgr';
783 0 $self->verify;
784 0 ▶ if ( $self->extension =~ /txt/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extension =~ /txt/) 0 0 0
785 0 print STDERR
786 " SGR structure invalid, re-setting extension from $extension to .txt\n";
787 }
788 0 $extension = $self->{'extension'};
789 }
790 }
791 elsif ( $extension =~ m/(?: reff?lat | genepred | ucsc )/xi ) {
792 0 ▶ if ( $self->ucsc != $self->number_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ucsc != $self->number_columns) 0 0 0
793  
794 # it's not set as a ucsc data
795 # let's set it to true and see if it passes verification
796 0 $self->ucsc( $self->number_columns );
797 0 ▶ unless ( $self->verify and $self->ucsc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->verify and $self->ucsc) 0 0 0
Condition0%0/3
$self->verify and $self->ucsc
!ll&&!rl&&r
000
MC/DC0%0/2
$self->verify and $self->ucsc
$self->verify $self->ucsc
Truth table0%0/3
A and B
A= $self->verifyB= $self->ucsc
ABresult
0X0
100
111
798 0 printf STDERR
799 " UCSC structure invalid, re-setting extension from %s to %s\n",
800 $extension, $self->extension;
801 0 $extension = $self->extension;
802 }
803 }
804 }
805  
806 # determine format
807 # this is an arcane specification of whether we want a "simple" no metadata
808 # format, or an ordinary text format that may or may not have metadata
809 # it's currently not hurting much, so leave it in for now?
810 9 ▶ unless ( $args{'format'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"format"}) 9 0 9
811 9 ▶ if ( defined $args{'simple'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'simple'}) { } 0 9 9
elsif ($extension) { } 9 0 9
812  
813 # an old method of specifying simple
814 0 $args{'format'} = 'simple';
815 }
816 elsif ($extension) {
817  
818 # check extension from the parsed filename, if present
819 9 ▶ if ( $extension =~ /tsv|csv|sgr|cdt/xi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extension =~ /tsv|csv|sgr|cdt/xi) { } 0 9 9
820  
821 # these are simple formats, no headers
822 0 $args{'format'} = 'simple';
823 }
824 else {
825 # everything else is text
826 9 $args{'format'} = 'text';
827 }
828 }
829 else {
830 # somehow we got this far without defining? use default text
831 0 $args{'format'} = 'text';
832 }
833 }
834  
835 # check zip status if necessary
836 9 ▶ unless ( defined $args{'gz'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"gz"}) 9 0 9
837  
838 # look at filename extension as a clue
839 # in case we're overwriting the input file, keep the zip status
840 9 ▶ if ( $extension =~ m/\.vcf\.gz/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($extension =~ /\.vcf\.gz/i) { } 0 9 9
elsif ($extension =~ /\.gz$/i) { } 0 9 9
841  
842 # vcf requires bgzip
843 0 $args{'gz'} = 2;
844 }
845 elsif ( $extension =~ m/\.gz$/i ) {
846 0 $args{'gz'} = 1;
847 }
848 else {
849 9 $args{'gz'} = 0; # default
850 }
851 }
852  
853 # adjust gzip extension as necessary
854 9 ▶ if ( $args{'gz'} and $extension !~ m/\.gz$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'gz'} and not $extension =~ /\.gz$/i) { } 0 9 9
elsif (not $args{'gz'} and $extension =~ /\.gz$/i) { } 0 9 9
Condition33%1/3
$args{"gz"} and not $extension =~ /\.gz$/i
!ll&&!rl&&r
900
MC/DC0%0/2
$args{"gz"} and not $extension =~ /\.gz$/i
$args{"gz"} not $extension =~ /\.gz$/i
Truth table33%1/3
A and not(B)
A= $args{"gz"}B= not $extension =~ /\.gz$/i
ABresult
0X0
100
111
855 0 $extension .= '.gz';
856 }
857 ▶ elsif ( not $args{'gz'} and $extension =~ /\.gz$/i ) {
Condition33%1/3
not $args{"gz"} and $extension =~ /\.gz$/i
!ll&&!rl&&r
090
MC/DC0%0/2
not $args{"gz"} and $extension =~ /\.gz$/i
not $args{"gz"} $extension =~ /\.gz$/i
Truth table33%1/3
not(A) and B
A= not $args{"gz"}B= $extension =~ /\.gz$/i
ABresult
0X0
100
111
858 0 $extension =~ s/\.gz$//i;
859 }
860  
861 # check filename length
862 # assuming a maximum of 256, at least on Mac with HFS+, don't know about Linux
863 # don't even get me started on Windows NTFS path length limitation
864 9 ▶ if ( length( $name . $extension ) > 255 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name . $extension > 255) 0 9 9
865 0 my $limit = 253 - length($extension);
866 0 $name = substr( $name, 0, $limit ) . '..';
867 0 print STDERR " WARNING: filename too long! Truncating to $limit characters\n";
868 }
869  
870 # generate the new filename
871 9 my $newname = $path . $name . $extension;
872  
873 # Convert strand information
874 9 my $strand_i = $self->strand_column;
875 9 ▶ if ( $strand_i and ( $self->gff or $self->bed or $self->ucsc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strand_i and $self->gff || $self->bed || $self->ucsc) 1 8 9
Condition44%4/9
$strand_i and $self->gff || $self->bed || $self->ucsc
!ll&&!rl&&r
801
$self->gff or $self->bed or $self->ucsc
l!l&&r!l&&!r
100
$self->gff or $self->bed
l!l&&r!l&&!r
010
MC/DC25%1/4
$strand_i and $self->gff || $self->bed || $self->ucsc
$strand_i $self->gff $self->bed $self->ucsc
Truth table40%2/5
A and B or C or D
A= $strand_iB= $self->gffC= $self->bedD= $self->ucsc
ABCDresult
0XXX0
10000
11XX1
101X1
10011
876  
877 # convert to +/-/. nomenclature as necessary
878 1 ▶ if ( $self->gff ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->gff) { } 0 1 1
elsif ($self->bed or $self->ucsc) { } 1 0 1
879 0 for my $row ( 1 .. $self->last_row ) {
880 0 my $s = $self->{'data_table'}->[$row][$strand_i];
881 0 ▶ if ( $s =~ /\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\d/) 0 0 0
882 0 ▶ $s = $s == 1 ? '+' : $s == -1 ? '-' : '.';
Branch: 2 branches
BranchTrueFalseTotal
$s == 1 ? : 0 0 0
$s == -1 ? : 0 0 0
883 }
884 0 $self->{'data_table'}->[$row][$strand_i] = $s;
885 }
886 }
887 ▶ elsif ( $self->bed or $self->ucsc ) {
Condition33%1/3
$self->bed or $self->ucsc
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->bed or $self->ucsc
$self->bed $self->ucsc
Truth table33%1/3
A or B
A= $self->bedB= $self->ucsc
ABresult
1X1
011
000
888 1 for my $row ( 1 .. $self->last_row ) {
889 3 my $s = $self->{'data_table'}->[$row][$strand_i];
890 3 ▶ if ( $s =~ /\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\d/) 0 3 3
891 0 ▶ $s = $s >= 0 ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$s >= 0 ? : 0 0 0
892 }
893 3 $self->{'data_table'}->[$row][$strand_i] = $s;
894 }
895 }
896 }
897  
898 # Open file for writing
899 9 my $fh = $self->open_to_write_fh( $newname, $args{'gz'} );
900 9 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 9 9
901  
902 # Write the headers
903 9 ▶ if ( $args{'format'} eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"format"} eq "text") 9 0 9
904  
905 # default text format has metadata headers
906  
907 # write gff statement if gff format
908 9 ▶ if ( $self->gff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gff) 0 9 9
909 0 $fh->printf( "##gff-version %s\n", $self->gff );
910 }
911  
912 # Write the primary headers
913 9 ▶ unless ( $self->gff
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->gff or $self->bed or $self->ucsc or $self->vcf or $extension =~ /(?: sgr | kgg | cdt | peak )/xi) 5 4 9
Condition66%8/12
$self->gff or $self->bed or $self->ucsc or $self->vcf or $extension =~ /(?: sgr | kgg | cdt | peak )/xi
l!l&&r!l&&!r
405
$self->gff or $self->bed or $self->ucsc or $self->vcf
l!l&&r!l&&!r
405
$self->gff or $self->bed or $self->ucsc
l!l&&r!l&&!r
405
$self->gff or $self->bed
l!l&&r!l&&!r
045
MC/DC20%1/5
$self->gff or $self->bed or $self->ucsc or $self->vcf or $extension =~ /(?: sgr | kgg | cdt | peak )/xi
$self->gff $self->bed $self->ucsc $self->vcf $extension =~ /(?: sgr | kgg | cdt | peak )/xi
Truth table33%2/6
A or B or C or D or E
A= $self->gffB= $self->bedC= $self->ucscD= $self->vcfE= $extension =~ /(?: sgr | kgg | cdt | peak )/xi
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
914 or $self->bed
915 or $self->ucsc
916 or $self->vcf
917 or $extension =~ m/(?: sgr | kgg | cdt | peak )/xi )
918 {
919 # we only write these for normal text files, not defined format files
920  
921 5 ▶ if ( $self->program ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->program) 0 5 5
922 0 $fh->printf( "# Program %s\n", $self->program );
923 }
924 5 ▶ if ( $self->database ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->database) 5 0 5
925 5 $fh->printf( "# Database %s\n", $self->database );
926 }
927 5 ▶ if ( $self->feature ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->feature) 2 3 5
928 2 $fh->printf( "# Feature %s\n", $self->feature );
929 }
930 }
931  
932 # Write the miscellaneous headers
933 9 foreach my $c ( @{ $self->{'comments'} } ) {
934  
935 # write remaining miscellaneous header lines if present
936 # we do this for all files
937 7 ▶ unless ( $c =~ /\n$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c =~ /\n$/s) 5 2 7
938  
939 # append newline if not present
940 5 $c .= "\n";
941 }
942  
943 # check for comment character at beginning
944 7 ▶ if ( $c =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^#/) { } 6 1 7
945 6 $fh->print($c);
946 }
947 else {
948 1 $fh->print("# $c");
949 }
950 }
951  
952 # Write the column metadata headers
953 9 for my $i ( 1 .. $self->number_columns ) {
954  
955 # each column metadata in the hash is referenced by the column's
956 # index number as the key
957 # we will take each index one at a time in increasing order
958  
959 # some files do not need or tolerate metadata lines, for those
960 # known files the metadata lines will be skipped
961  
962 # these column metadata lines do not need to be written if they
963 # only have two values, presumably name and index, for files
964 # that don't normally have column headers, e.g. gff
965 32 ▶ if ( exists $self->{$i}{'AUTO'}
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{$i}{'AUTO'} and scalar keys %{$$self{$i};} == $self->{$i}{'AUTO'}) { } 21 11 32
elsif (scalar keys %{$$self{$i};} == 2) { } 11 0 11
elsif ($extension =~ /(?: sgr | kgg | cdt )/xi or $self->ucsc or $self->vcf) { } 0 0 0
Condition66%2/3
exists $self->{$i}{"AUTO"} and scalar keys %{$$self{$i};} == $self->{$i}{"AUTO"}
!ll&&!rl&&r
11021
MC/DC50%1/2
exists $self->{$i}{"AUTO"} and scalar keys %{$$self{$i};} == $self->{$i}{"AUTO"}
exists $self->{$i}{"AUTO"} scalar keys %{$$self{$i};} == $self->{$i}{"AUTO"}
Truth table66%2/3
A and B
A= exists $self->{$i}{"AUTO"}B= scalar keys %{$$self{$i};} == $self->{$i}{"AUTO"}
ABresult
0X0
100
111
966 21 and scalar( keys %{ $self->{$i} } ) == $self->{$i}{'AUTO'} )
967 {
968 # some of the metadata values were autogenerated and
969 # we have the same number of keys as were autogenerated
970 # no need to write these
971 21 next;
972 }
973 11 elsif ( scalar( keys %{ $self->{$i} } ) == 2 ) {
974  
975 # only two metadata keys exist, name and index
976 # these are so simple it's not worth writing them
977 11 next;
978 }
979 ▶ elsif ($extension =~ m/(?: sgr | kgg | cdt )/xi
Condition0%0/6
$extension =~ /(?: sgr | kgg | cdt )/xi or $self->ucsc or $self->vcf
l!l&&r!l&&!r
000
$extension =~ /(?: sgr | kgg | cdt )/xi or $self->ucsc
l!l&&r!l&&!r
000
MC/DC0%0/3
$extension =~ /(?: sgr | kgg | cdt )/xi or $self->ucsc or $self->vcf
$extension =~ /(?: sgr | kgg | cdt )/xi $self->ucsc $self->vcf
Truth table0%0/4
A or B or C
A= $extension =~ /(?: sgr | kgg | cdt )/xiB= $self->ucscC= $self->vcf
ABCresult
1XX1
01X1
0011
0000
980 or $self->ucsc
981 or $self->vcf )
982 {
983  
984 # these do not support metadata lines
985 0 next;
986 }
987  
988 # we will put each key=value pair into @pairs, listed asciibetically
989 0 my @pairs; # an array of the key value pairs from the metadata hash
990 # put name first
991 # we are no longer writing the index number
992 0 push @pairs, 'name=' . $self->{$i}{'name'};
993  
994 # put remainder in alphabetical order
995 0 foreach ( sort { $a cmp $b } keys %{ $self->{$i} } ) {
996 0 ▶ next if $_ eq 'name'; # already written
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "name" 0 0 0
997 0 ▶ next if $_ eq 'index'; # internal use only
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "index" 0 0 0
998 0 ▶ next if $_ eq 'AUTO'; # internal use only
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "AUTO" 0 0 0
999 0 push @pairs, $_ . '=' . $self->{$i}{$_};
1000 }
1001  
1002 # Finally write the header line, joining the pairs with a
1003 # semi-colon into a single string.
1004 # The column identifier is comprised of the word 'Column'
1005 # and the index number joined by '_'.
1006 0 $fh->printf( "# Column%d: %s\n", $i, join( ';', @pairs ) );
1007 }
1008 }
1009  
1010 # Write the table column headers, skipping the first column
1011 9 ▶ if ( $self->headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers) 6 3 9
1012 $fh->printf(
1013 "%s\n",
1014 join( "\t",
1015 6 @{ $self->{'data_table'}[0] }[ 1 .. $self->{'number_columns'} ] )
1016 );
1017 }
1018  
1019 # Write the data table
1020 9 ▶ if ( $args{'format'} eq 'simple' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'format'} eq 'simple') { } 0 9 9
1021  
1022 # the simple format will strip the non-value '.' from the table
1023 0 my $sep = "\t";
1024 0 ▶ if ($extension =~ /csv/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extension =~ /csv/i) 0 0 0
1025 0 $sep = ',';
1026 }
1027 0 my $n = $self->{'number_columns'};
1028 0 for my $i ( 1 .. $self->last_row ) {
1029 4 ▶ no warnings "uninitialized";
Subroutine BEGIN: called CC=1 SCAR=0.0
1030  
1031 # we will step though the data_table array one row at a time
1032 # skipping the first (empty) element
1033 # convert any non-value '.' to empty
1034 # and print using a tab-delimited format
1035 my @linedata =
1036 0 ▶ map { $_ eq '.' ? q() : $_ } @{ $self->{'data_table'}[$i] }[ 1 .. $n ];
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '.' ? : 0 0 0
1037 0 $fh->printf( "%s\n", join( $sep, @linedata ) );
1038 }
1039 }
1040  
1041 else {
1042 # normal data files
1043 9 my $n = $self->{'number_columns'};
1044 9 for my $i ( 1 .. $self->last_row ) {
1045 4 ▶ no warnings "uninitialized";
Subroutine BEGIN: called CC=1 SCAR=0.0
1046  
1047 # we will step though the data_table array one row at a time
1048 # we will join each row's array of elements into a string to print
1049 # using a tab-delimited format, skipping the first (empty) element
1050 $fh->printf( "%s\n",
1051 107 join( "\t", @{ $self->{'data_table'}[$i] }[ 1 .. $n ] ) );
1052 }
1053 }
1054  
1055 # done writing
1056 9 $fh->close;
1057  
1058 # if we made it this far, it should've been a success!
1059 # return the new file name as indication of success
1060 9 return $newname;
1061 }
1062  
1063 7 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
1064 7 return shift->write_file(@_);
1065 }
1066  
1067 #### Open a file for reading
1068 185 ▶sub open_to_read_fh {
Subroutine open_to_read_fh: called CC=13 SCAR=29.8
Pod documented
1069 185 my $self = shift;
1070 185 ▶ my $file = shift || undef;
Condition100%2/2
shift() || undef
l!l
55130
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1071 185 ▶ my $obj = ref($self) =~ /^Bio::ToolBox/x ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref($self) =~ /^Bio::ToolBox/x ? : 130 55 185
1072  
1073 # check file
1074 185 ▶ if ( not $file and $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file and $obj) 130 55 185
Condition66%2/3
not $file and $obj
!ll&&!rl&&r
550130
MC/DC50%1/2
not $file and $obj
not $file $obj
Truth table66%2/3
not(A) and B
A= not $fileB= $obj
ABresult
0X0
100
111
1075 130 ▶ $file = $self->filename || undef;
Condition50%1/2
$self->filename || undef
l!l
1300
MC/DC0%0/1
$self->filename || undef
$self->filename
Truth table50%1/2
A
A= $self->filename
Aresult
11
00
1076 }
1077 185 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 185 185
1078 0 carp 'ERROR: no filename provided or associated with object!';
1079 0 return;
1080 }
1081  
1082 # Open filehandle object as appropriate
1083 185 my $fh;
1084 185 ▶ if ( $file =~ /\.gz$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /\.gz$/i) { } 0 185 185
elsif ($file =~ /\.bz2$/i) { } 0 185 185
1085  
1086 # the file is compressed with gzip
1087 0 ▶ $fh = IO::File->new("gzip -dc $file |")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("gzip -dc $file |") or carp("ERROR: unable to read '${file}': $Bio::ToolBox::Data::file::OS_ERROR") 0 0 0
1088 or carp "ERROR: unable to read '$file': $OS_ERROR";
1089 }
1090 elsif ( $file =~ /\.bz2$/i ) {
1091  
1092 # the file is compressed with bzip2
1093 0 ▶ $fh = IO::File->new("bzip2 -dc $file |")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("bzip2 -dc $file |") or carp("ERROR: unable to read '${file}': $Bio::ToolBox::Data::file::OS_ERROR") 0 0 0
1094 or carp "ERROR: unable to read '$file': $OS_ERROR";
1095 }
1096 else {
1097 # the file is uncompressed and space hogging
1098 185 ▶ $fh = IO::File->new( $file, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($file, "r") or carp("ERROR: unable to read '${file}': $Bio::ToolBox::Data::file::OS_ERROR") 185 0 185
1099 or carp "ERROR: unable to read '$file': $OS_ERROR";
1100 }
1101  
1102 185 ▶ if ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) 130 55 185
1103 130 $self->{fh} = $fh;
1104 }
1105 185 return $fh;
1106 }
1107  
1108 #### Open a file for writing
1109 11 ▶sub open_to_write_fh {
Subroutine open_to_write_fh: called CC=30 SCAR=54.2
Pod documented
1110 11 my ( $self, $filename, $gz, $append ) = @_;
1111  
1112 # check filename
1113 11 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 11 11
1114 0 carp 'ERROR: no filename to write!';
1115 0 return;
1116 }
1117  
1118 # check filename length
1119 # assuming a maximum of 256, at least on Mac with HFS+, don't know about Linux
1120 11 my $name = fileparse($filename);
1121 11 ▶ if ( length $name > 255 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name > 255) 0 11 11
1122 0 carp 'ERROR: filename is too long! please shorten';
1123 0 return;
1124 }
1125  
1126 # check zip status if necessary
1127 11 ▶ unless ( defined $gz ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gz) 2 9 11
1128  
1129 # look at filename extension as a clue
1130 # in case we're overwriting the input file, keep the zip status
1131 2 ▶ if ( $filename =~ m/\.vcf (\.gz)? $/xi ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /\.vcf (\.gz)? $/xi) { } 0 2 2
elsif ($filename =~ /\.gz$/i) { } 0 2 2
1132 0 $gz = 2; # bgzip
1133 }
1134 elsif ( $filename =~ m/\.gz$/i ) {
1135 0 $gz = 1; # regular gzip
1136 }
1137 else {
1138 2 $gz = 0; # default
1139 }
1140 }
1141  
1142 # gzip compression application
1143 11 ▶ if ( $gz == 1 and not $gzip_app ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gz == 1 and not $gzip_app) { } 0 11 11
elsif ($gz == 2 and not $bgzip_app) { } 0 22 22
Condition33%1/3
$gz == 1 and not $gzip_app
!ll&&!rl&&r
1100
MC/DC0%0/2
$gz == 1 and not $gzip_app
$gz == 1 not $gzip_app
Truth table33%1/3
A and not(B)
A= $gz == 1B= not $gzip_app
ABresult
0X0
100
111
1144  
1145 # use parallel gzip if possible
1146 # this is stored in a global variable so we only have to look once
1147 0 $gzip_app = which('pigz');
1148 0 ▶ if ($gzip_app) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gzip_app) { } 0 0 0
1149 0 $gzip_app .= ' -p 3'; # use a conservative 3 processes, plus perl, so 4 total
1150 }
1151 else {
1152 # default is the standard gzip application
1153 # should be available in any application
1154 0 $gzip_app = which('gzip');
1155 }
1156 0 ▶ unless ($gzip_app) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gzip_app) 0 0 0
1157 0 carp
1158 'ERROR: No gzip application in PATH to open compressed file handle output!';
1159 0 $gz = 0;
1160 0 $filename =~ s/\.gz$//;
1161 }
1162 }
1163 ▶ elsif ( $gz == 2 and not $bgzip_app ) {
Condition33%1/3
$gz == 2 and not $bgzip_app
!ll&&!rl&&r
1100
MC/DC0%0/2
$gz == 2 and not $bgzip_app
$gz == 2 not $bgzip_app
Truth table33%1/3
A and not(B)
A= $gz == 2B= not $bgzip_app
ABresult
0X0
100
111
1164  
1165 # use parallel bgzip if possible
1166 # this is stored in a global variable so we only have to look once
1167 0 $bgzip_app = which('bgzip');
1168 0 ▶ if ($bgzip_app) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bgzip_app) 0 0 0
1169  
1170 # I'm going to assume this is a recent bgzip with multi-threading
1171 # use 3 threads, same as with pigz
1172 0 $bgzip_app .= ' -@ 3 -c';
1173 }
1174 0 ▶ unless ($bgzip_app) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bgzip_app) 0 0 0
1175 0 carp
1176 'ERROR: No bgzip application in PATH to open compressed file handle output!';
1177 0 $gz = 0;
1178 0 $filename =~ s/\.gz$//;
1179 }
1180 }
1181 11 ▶ my $gzipper = $gz == 1 ? $gzip_app : $gz == 2 ? $bgzip_app : undef;
Branch: 2 branches
BranchTrueFalseTotal
$gz == 1 ? : 0 11 11
$gz == 2 ? : 0 11 11
1182  
1183 # check file append mode
1184 11 ▶ unless ( defined $append ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $append) 9 2 11
1185  
1186 # default is not to append
1187 9 $append = 0;
1188 }
1189  
1190 # Generate appropriate filehandle object
1191 11 my $fh;
1192 11 ▶ if ( not $gzipper and not $append ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $gzipper || $append) { } 9 2 11
elsif ($gzipper and not $append) { } 0 2 2
elsif (not $gzipper and $append) { } 2 0 2
elsif ($gzipper and $append) { } 0 0 0
Condition66%2/3
$gzipper || $append
l!l&&r!l&&!r
029
MC/DC50%1/2
$gzipper || $append
$gzipper $append
Truth table66%2/3
A || B
A= $gzipperB= $append
ABresult
1X1
011
000
1193 9 ▶ $fh = IO::File->new( $filename, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($filename, "w") or carp("ERROR: cannot write to file '${filename}': $Bio::ToolBox::Data::file::OS_ERROR") 9 0 9
1194 or carp "ERROR: cannot write to file '$filename': $OS_ERROR";
1195 }
1196 ▶ elsif ( $gzipper and not $append ) {
Condition33%1/3
$gzipper and not $append
!ll&&!rl&&r
200
MC/DC0%0/2
$gzipper and not $append
$gzipper not $append
Truth table33%1/3
A and not(B)
A= $gzipperB= not $append
ABresult
0X0
100
111
1197 0 ▶ $fh = IO::File->new("| $gzipper >$filename")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("| $gzipper >$filename") or carp("ERROR: cannot write to compressed file '${filename}': $Bio::ToolBox::Data::file::OS_ERROR") 0 0 0
1198 or carp "ERROR: cannot write to compressed file '$filename': $OS_ERROR";
1199 }
1200 ▶ elsif ( not $gzipper and $append ) {
Condition33%1/3
not $gzipper and $append
!ll&&!rl&&r
002
MC/DC0%0/2
not $gzipper and $append
not $gzipper $append
Truth table33%1/3
not(A) and B
A= not $gzipperB= $append
ABresult
0X0
100
111
1201 2 ▶ $fh = IO::File->new(">> $filename")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new(">> $filename") or carp("ERROR: cannot append to file '${filename}': $Bio::ToolBox::Data::file::OS_ERROR") 2 0 2
1202 or carp "ERROR: cannot append to file '$filename': $OS_ERROR";
1203 }
1204 ▶ elsif ( $gzipper and $append ) {
Condition0%0/3
$gzipper and $append
!ll&&!rl&&r
000
MC/DC0%0/2
$gzipper and $append
$gzipper $append
Truth table0%0/3
A and B
A= $gzipperB= $append
ABresult
0X0
100
111
1205 0 ▶ $fh = IO::File->new("| $gzipper >>$filename")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("| $gzipper >>$filename") or carp("ERROR: cannot append to compressed file '${filename}': $Bio::ToolBox::Data::file::OS_ERROR") 0 0 0
1206 or carp "ERROR: cannot append to compressed file '$filename': $OS_ERROR";
1207 }
1208 11 ▶ return $fh if defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh 11 0 11
1209 }
1210  
1211 ### Subroutine to check for file existance
1212 83 ▶sub check_file {
Subroutine check_file: called CC=13 SCAR=41.2
Pod documented
1213 83 my $self = shift;
1214 83 my $filename;
1215 my $md; # need to remember if filename came from arguments or metadata
1216 83 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 25 58 83
elsif ($filename = $self->filename) { } 58 0 58
1217 25 $filename = shift @_;
1218 25 $md = 0;
1219 }
1220 elsif ( $filename = $self->filename ) {
1221 58 $md = 1;
1222 }
1223 else {
1224 0 return;
1225 }
1226  
1227 # check for file existance
1228 83 ▶ if ( -e $filename and -f _ and -r _ and -s _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename and -f _ and -r _ and -s _) { } 83 0 83
Condition33%3/9
-e $filename and -f _ and -r _ and -s _
!ll&&!rl&&r
0083
-e $filename and -f _ and -r _
!ll&&!rl&&r
0083
-e $filename and -f _
!ll&&!rl&&r
0083
MC/DC0%0/4
-e $filename and -f _ and -r _ and -s _
-e $filename -f _ -r _ -s _
Truth table20%1/5
A and B and C and D
A= -e $filenameB= -f _C= -r _D= -s _
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1229  
1230 # confirmed full filename and path
1231 83 return $filename;
1232 }
1233 else {
1234 # file name is either incomplete or non-existent
1235 # try adding some common file extensions in case those are missing
1236 0 foreach my $ext (qw(gz txt txt.gz bed bed.gz)) {
1237 0 my $new_filename = sprintf "%s.%s", $filename, $ext;
1238 0 ▶ if ( -e $new_filename and -f _ and -r _ and -s _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $new_filename and -f _ and -r _ and -s _) 0 0 0
Condition0%0/9
-e $new_filename and -f _ and -r _ and -s _
!ll&&!rl&&r
000
-e $new_filename and -f _ and -r _
!ll&&!rl&&r
000
-e $new_filename and -f _
!ll&&!rl&&r
000
MC/DC0%0/4
-e $new_filename and -f _ and -r _ and -s _
-e $new_filename -f _ -r _ -s _
Truth table0%0/5
A and B and C and D
A= -e $new_filenameB= -f _C= -r _D= -s _
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1239 0 printf " WARNING: File '%s' does not exist, using '%s' instead\n",
1240 $filename, $new_filename;
1241 0 ▶ if ($md) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md) 0 0 0
1242  
1243 # update metadata
1244 0 $self->add_file_metadata($new_filename);
1245 }
1246 0 return $new_filename;
1247 }
1248 }
1249 }
1250 }
1251  
1252 390 ▶sub fh {
Subroutine fh: called CC=2 SCAR=8.1
Pod documented
1253 390 my $self = shift;
1254 390 ▶ return $self->{fh} if exists $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"fh"} 390 0 390
1255 0 return;
1256 }
1257  
1258 76 ▶sub close_fh {
Subroutine close_fh: called CC=3 SCAR=13.1
Pod documented
1259 76 my $self = shift;
1260 76 ▶ $self->{fh}->close if ( exists $self->{fh} and $self->{fh} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"fh"} and $self->{"fh"} 76 0 76
Condition33%1/3
exists $self->{"fh"} and $self->{"fh"}
!ll&&!rl&&r
0076
MC/DC0%0/2
exists $self->{"fh"} and $self->{"fh"}
exists $self->{"fh"} $self->{"fh"}
Truth table33%1/3
A and B
A= exists $self->{"fh"}B= $self->{"fh"}
ABresult
0X0
100
111
1261 }
1262  
1263 ### Internal subroutine to check if a comment line contains headers
1264 39 ▶sub _commented_header_line {
Subroutine _commented_header_line: called CC=5 SCAR=17.6
1265 39 my ( $data, $line ) = @_;
1266  
1267 # only continue if we have comment lines
1268 39 ▶ unless ( scalar @{ $data->{'comments'} } >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$data->{"comments"};} >= 1) 27 12 39
1269 27 return 0;
1270 }
1271  
1272 # prepare arrays from the comment lines and current line
1273 12 my @commentdata = split /\t/, $data->{'comments'}->[-1];
1274 12 my @linedata = split /\t/, $line;
1275  
1276 # check if the counts are equal
1277 # we avoid using the comment for one-column data files because they are probably wrong
1278 12 ▶ if ( scalar @commentdata > 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @commentdata > 1 and scalar @linedata > 1 and scalar @commentdata == scalar @linedata) { } 0 12 12
Condition33%2/6
scalar @commentdata > 1 and scalar @linedata > 1 and scalar @commentdata == scalar @linedata
!ll&&!rl&&r
1200
scalar @commentdata > 1 and scalar @linedata > 1
!ll&&!rl&&r
1200
MC/DC0%0/3
scalar @commentdata > 1 and scalar @linedata > 1 and scalar @commentdata == scalar @linedata
scalar @commentdata > 1 scalar @linedata > 1 scalar @commentdata == scalar @linedata
Truth table25%1/4
A and B and C
A= scalar @commentdata > 1B= scalar @linedata > 1C= scalar @commentdata == scalar @linedata
ABCresult
0XX0
10X0
1100
1111
1279 and scalar @linedata > 1
1280 and scalar @commentdata == scalar @linedata )
1281 {
1282 0 return 1;
1283 }
1284 else {
1285 12 return 0;
1286 }
1287 }
1288  
1289 ### Internal subroutine to process metadata for standard columns
1290 0 ▶sub add_column_metadata {
Subroutine add_column_metadata: not called CC=4 SCAR=30.0
Pod documented
1291 0 my ( $data, $line, $index ) = @_;
1292  
1293 # strip the Column metadata identifier
1294 0 chomp $line;
1295 0 $line =~ s/^\#\ Column _? \d+ :? \ //x;
1296  
1297 # break up the column metadata
1298 0 my %temphash; # a temporary hash to put the column metadata into
1299 0 foreach my $pair ( split /;/, $line ) {
1300 0 my ( $key, $value ) = split /=/, $pair;
1301 0 ▶ next if $key eq 'index'; # very old versions may include this
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "index" 0 0 0
1302 0 $temphash{$key} = $value;
1303 }
1304 0 $temphash{'index'} = $index;
1305  
1306 # store the column metadata hash into the main data hash
1307 0 ▶ if ( exists $data->{$index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$index}) 0 0 0
1308  
1309 # this should never happen unless there is more than column metadata line
1310 # automatic metadata from standard formats should come later
1311 0 print "WARNING: more than one metadata line exists for index $index!\n";
1312 }
1313 0 $data->{$index} = \%temphash;
1314  
1315 0 return 1;
1316 }
1317  
1318 ### Subroutine to generate metadata for gff files
1319 # gff files have nine defined columns
1320 # there are different specifications and variants:
1321 # gff (v.1), gff v.2, gff v.2.5 (aka gtf), gff v.3 (gff3)
1322 # however, the columns are the same in all versions
1323 # more info on gff can be found http://gmod.org/wiki/GFF3
1324 13 ▶sub add_gff_metadata {
Subroutine add_gff_metadata: called CC=11 SCAR=37.3
Pod documented
1325 13 my ( $self, $version, $force ) = @_;
1326 13 ▶ $force ||= 0;
Condition50%1/2
$force ||= 0
l!l
013
MC/DC0%0/1
$force ||= 0
$force
Truth table50%1/2
A
A= $force
Aresult
11
00
1327  
1328 # set the gff version based on the extension if it isn't already
1329 # normally gff version should already be defined from pragma when parsing headers
1330 13 ▶ if ( not( $self->gff ) or $force ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->gff or $force) 0 13 13
Condition33%1/3
not $self->gff or $force
l!l&&r!l&&!r
0013
MC/DC0%0/2
not $self->gff or $force
not $self->gff $force
Truth table33%1/3
not(A) or B
A= not $self->gffB= $force
ABresult
1X1
011
000
1331 0 ▶ if ( defined $version ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $version) { } 0 0 0
elsif ($self->extension =~ /gtf/i) { } 0 0 0
elsif ($self->extension =~ /gff3/i) { } 0 0 0
1332 0 $self->gff($version);
1333 }
1334 elsif ( $self->extension =~ /gtf/i ) {
1335 0 $self->gff(2.5);
1336 }
1337 elsif ( $self->extension =~ /gff3/i ) {
1338 0 $self->gff(3);
1339 }
1340 else {
1341 0 $self->gff(2); # hope for the best
1342 }
1343 }
1344  
1345 # set format based on version
1346 13 ▶ if ( not $self->format ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->format) 0 13 13
1347 0 my $g = $self->gff;
1348 0 ▶ if ( $g == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g == 3) { } 0 0 0
elsif ($g > 2) { } 0 0 0
1349 0 $self->format('gff3');
1350 }
1351 elsif ( $g > 2 ) {
1352 0 $self->format('gtf');
1353 }
1354 else {
1355 0 $self->format('gff');
1356 }
1357 }
1358  
1359 # set the metadata
1360 13 $self->add_standard_metadata( $self->standard_column_names('gff'), $force );
1361 13 $self->interbase(0);
1362 13 $self->headers(0);
1363 13 ▶ unless ( $self->feature ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->feature) 13 0 13
1364 13 $self->feature('region');
1365 }
1366  
1367 13 return 1;
1368 }
1369  
1370 ### Subroutine to generate metadata for BED files
1371 # bed files have a loose format
1372 # they require a minimum of 3 columns, and have a max of 12
1373 # 3, 6, and 12 column files are the most common
1374 # there are also something called paired bed files floating around
1375 # The official details and specifications may be found at
1376 # http://genome.ucsc.edu/FAQ/FAQformat#format1
1377  
1378 # a special type of bed file is the bedgraph, using
1379 # either a bdg, bedgraph, or simply bed extension
1380 # these only have four columns, no more, no less
1381 # the fourth column is score, not name
1382 24 ▶sub add_bed_metadata {
Subroutine add_bed_metadata: called CC=10 SCAR=29.6
Pod documented
1383 24 my ( $self, $column_count, $force ) = @_;
1384 24 ▶ $force ||= 0;
Condition50%1/2
$force ||= 0
l!l
024
MC/DC0%0/1
$force ||= 0
$force
Truth table50%1/2
A
A= $force
Aresult
11
00
1385  
1386 # check bed type and collect column names
1387 24 my $names;
1388 24 ▶ if ( $self->format =~ /bedgraph/i or $self->extension =~ m/(?: bg | bdg | graph )/xi )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->format =~ /bedgraph/i or $self->extension =~ /(?: bg | bdg | graph )/xi) { } 0 24 24
elsif ($self->format =~ /bedpe/i or $self->extension =~ /bedpe/i) { } 0 24 24
Condition33%1/3
$self->format =~ /bedgraph/i or $self->extension =~ /(?: bg | bdg | graph )/xi
l!l&&r!l&&!r
0024
MC/DC0%0/2
$self->format =~ /bedgraph/i or $self->extension =~ /(?: bg | bdg | graph )/xi
$self->format =~ /bedgraph/i $self->extension =~ /(?: bg | bdg | graph )/xi
Truth table33%1/3
A or B
A= $self->format =~ /bedgraph/iB= $self->extension =~ /(?: bg | bdg | graph )/xi
ABresult
1X1
011
000
1389 {
1390 0 $self->{'bed'} = $column_count;
1391 0 $self->format('bedGraph');
1392 0 $names = $self->standard_column_names('bdg');
1393 }
1394 ▶ elsif ( $self->format =~ /bedpe/i or $self->extension =~ m/bedpe/i ) {
Condition33%1/3
$self->format =~ /bedpe/i or $self->extension =~ /bedpe/i
l!l&&r!l&&!r
0024
MC/DC0%0/2
$self->format =~ /bedpe/i or $self->extension =~ /bedpe/i
$self->format =~ /bedpe/i $self->extension =~ /bedpe/i
Truth table33%1/3
A or B
A= $self->format =~ /bedpe/iB= $self->extension =~ /bedpe/i
ABresult
1X1
011
000
1395 0 $self->{'bed'} = 0; # this will bypass verification checks which would fail
1396 0 $self->format('bedpe');
1397 0 $names = $self->standard_column_names('bedpe');
1398 }
1399 else {
1400 24 $self->{'bed'} = $column_count;
1401 24 $self->format( sprintf( "bed%d", $column_count ) );
1402 24 $names = $self->standard_column_names('bed12');
1403 }
1404  
1405 # set the column names
1406 24 my $column_names;
1407 24 ▶ if ( $column_count == scalar @{$names} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($column_count == scalar @{$names;}) { } 7 17 24
elsif ($column_count < scalar @{$names;}) { } 17 0 17
elsif ($column_count > scalar @{$names;}) { } 0 0 0
1408 7 $column_names = $names;
1409 }
1410 17 elsif ( $column_count < scalar @{$names} ) {
1411  
1412 # subset names as appropriate
1413 17 my $n = $column_count - 1;
1414 17 my @wanted_names = @{$names}[ 0 .. $n ];
1415 17 $column_names = \@wanted_names;
1416 }
1417 0 elsif ( $column_count > scalar @{$names} ) {
1418  
1419 # add additional names as appropriate
1420 0 $column_names = $names;
1421 0 my $n = scalar( @{$column_names} ) + 1;
1422 0 for my $i ( $n .. $column_count ) {
1423 0 push @{$column_names}, sprintf "Column_$i";
1424 }
1425 }
1426 else {
1427 0 confess "programming error!";
1428 }
1429 24 $self->add_standard_metadata( $column_names, $force );
1430  
1431 # add additional metadata
1432 24 $self->interbase(1);
1433 24 $self->headers(0);
1434 24 $self->feature('region');
1435  
1436 24 return 1;
1437 }
1438  
1439 ### Subroutine to generate metadata for broadpeak and narrowpeak files
1440 # three different types of peak files are available
1441 # see http://genome.ucsc.edu/FAQ/FAQformat.html
1442 13 ▶sub add_peak_metadata {
Subroutine add_peak_metadata: called CC=8 SCAR=23.2
Pod documented
1443 13 my ( $self, $column_count, $force ) = @_;
1444 13 ▶ $force ||= 0;
Condition50%1/2
$force ||= 0
l!l
013
MC/DC0%0/1
$force ||= 0
$force
Truth table50%1/2
A
A= $force
Aresult
11
00
1445  
1446 # check bed type and set metadata appropriately
1447 # most of these are bed6 plus extra columns
1448 13 my $column_names;
1449 13 ▶ if ( $self->format =~ /narrow/i or $self->extension =~ /narrow/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->format =~ /narrow/i or $self->extension =~ /narrow/i) { } 7 6 13
elsif ($self->format =~ /broad/i or $self->extension =~ /broad/i) { } 0 6 6
elsif ($self->format =~ /gapped/i or $self->extension =~ /gapped/i) { } 6 0 6
Condition66%2/3
$self->format =~ /narrow/i or $self->extension =~ /narrow/i
l!l&&r!l&&!r
076
MC/DC50%1/2
$self->format =~ /narrow/i or $self->extension =~ /narrow/i
$self->format =~ /narrow/i $self->extension =~ /narrow/i
Truth table66%2/3
A or B
A= $self->format =~ /narrow/iB= $self->extension =~ /narrow/i
ABresult
1X1
011
000
1450 7 $self->format('narrowPeak');
1451 7 $column_names = $self->standard_column_names('narrowpeak');
1452 }
1453 ▶ elsif ( $self->format =~ /broad/i or $self->extension =~ /broad/i ) {
Condition33%1/3
$self->format =~ /broad/i or $self->extension =~ /broad/i
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->format =~ /broad/i or $self->extension =~ /broad/i
$self->format =~ /broad/i $self->extension =~ /broad/i
Truth table33%1/3
A or B
A= $self->format =~ /broad/iB= $self->extension =~ /broad/i
ABresult
1X1
011
000
1454 0 $self->format('broadPeak'); # possibly redundant
1455 0 $column_names = $self->standard_column_names('broadpeak');
1456 }
1457 ▶ elsif ( $self->format =~ /gapped/i or $self->extension =~ /gapped/i ) {
Condition33%1/3
$self->format =~ /gapped/i or $self->extension =~ /gapped/i
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->format =~ /gapped/i or $self->extension =~ /gapped/i
$self->format =~ /gapped/i $self->extension =~ /gapped/i
Truth table33%1/3
A or B
A= $self->format =~ /gapped/iB= $self->extension =~ /gapped/i
ABresult
1X1
011
000
1458 6 $self->format('gappedPeak'); # possibly redundant
1459 6 $column_names = $self->standard_column_names('gappedpeak');
1460 }
1461 else {
1462  
1463 # unrecognized peak format
1464 0 return 0;
1465 }
1466 13 $self->add_standard_metadata( $column_names, $force );
1467  
1468 # add additional metadata
1469 13 $self->bed($column_count);
1470 13 $self->interbase(1);
1471 13 $self->headers(0);
1472 13 $self->{'feature'} = 'region';
1473  
1474 13 return 1;
1475 }
1476  
1477 ### Subroutine to generate metadata for various UCSC gene files
1478 # these are tricky, as we will try to determine contents by counting
1479 # the columns, which may not be accurate
1480 # not only that, but this presumes the extension even makes it recognizable
1481 # see http://genome.ucsc.edu/FAQ/FAQformat.html#format9 for details
1482 # also biotoolbox script ucsc_table2gff3.pl
1483 6 ▶sub add_ucsc_metadata {
Subroutine add_ucsc_metadata: called CC=8 SCAR=29.4
Pod documented
1484 6 my ( $self, $column_count, $force ) = @_;
1485 6 ▶ $force ||= 0;
Condition50%1/2
$force ||= 0
l!l
60
MC/DC0%0/1
$force ||= 0
$force
Truth table50%1/2
A
A= $force
Aresult
11
00
1486  
1487 # set format and determine column names;
1488 6 my $column_names;
1489 6 ▶ if ( $column_count == 16 ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($column_count == 16) { } 6 0 6
elsif ($column_count == 15) { } 0 0 0
elsif ($column_count == 12) { } 0 0 0
elsif ($column_count == 11) { } 0 0 0
elsif ($column_count == 10) { } 0 0 0
1490 6 $self->format('genePredExt');
1491 6 $column_names = $self->standard_column_names('ucsc16');
1492 }
1493 elsif ( $column_count == 15 ) {
1494 0 $self->format('genePredExt');
1495 0 $column_names = $self->standard_column_names('ucsc15');
1496 }
1497 elsif ( $column_count == 12 ) {
1498 0 $self->format('knownGene');
1499 0 $column_names = $self->standard_column_names('ucsc12');
1500 }
1501 elsif ( $column_count == 11 ) {
1502 0 $self->format('refFlat');
1503 0 $column_names = $self->standard_column_names('ucsc11');
1504 }
1505 elsif ( $column_count == 10 ) {
1506 0 $self->format('genePred');
1507 0 $column_names = $self->standard_column_names('ucsc10');
1508 }
1509 else {
1510  
1511 # unrecognized format
1512 0 return 0;
1513 }
1514 6 $self->add_standard_metadata( $column_names, $force );
1515  
1516 # set additional metadata
1517 6 $self->ucsc($column_count);
1518 6 $self->{'zerostart'} = 1; # cannot use interbase method
1519 6 $self->headers(0);
1520 6 ▶ unless ( $self->feature ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->feature) 6 0 6
1521 6 $self->feature('gene');
1522 }
1523  
1524 6 return 1;
1525 }
1526  
1527 ### Subroutine to generate metadata for SGR files
1528 # a sgr file contains three columns: chromo, position, score
1529 # this is a very simple file format, useful in exporting and
1530 # importing to binary BAR files used in T2, USeq, and IGB
1531 0 ▶sub add_sgr_metadata {
Subroutine add_sgr_metadata: not called CC=2 SCAR=17.9
Pod documented
1532 0 my $self = shift;
1533 0 ▶ my $force = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1534  
1535 # set column metadata
1536 0 $self->add_standard_metadata( $self->standard_column_names('sgr'), $force );
1537  
1538 # set additional metadata
1539 0 $self->format('sgr');
1540 0 $self->headers(0);
1541 0 $self->{'feature'} = 'region';
1542  
1543 0 return 1;
1544 }
1545  
1546 ### Internal subroutine to generate metadata for standard files
1547 72 ▶sub add_standard_metadata {
Subroutine add_standard_metadata: called CC=7 SCAR=20.7
Pod documented
1548 72 my ( $self, $namelist, $force ) = @_;
1549  
1550 # we will define the columns based on
1551 72 for my $i ( 0 .. $#{$namelist} ) {
1552  
1553 # columns are indexed in the metadata as base 1 integers
1554 659 my $j = $i + 1;
1555  
1556 # add file metadata for this column
1557 659 ▶ if ( exists $self->{$j} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$j}) { } 96 563 659
1558  
1559 96 ▶ if ( $self->{$j}->{'name'} eq $namelist->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$j}{'name'} eq $namelist->[$i]) { } 0 96 96
1560  
1561 # names match, assign table
1562 0 $self->{'data_table'}->[0]->[$j] = $namelist->[$i];
1563 }
1564 else {
1565 # names do not match, there will be errors!
1566 96 ▶ if ($force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force) { } 96 0 96
1567 96 $self->{$j}->{'name'} = $namelist->[$i];
1568 96 $self->{'data_table'}->[0]->[$j] = $namelist->[$i];
1569 }
1570 else {
1571 0 print
1572 " WARNING: metadata and header names for column $j do not match!\n";
1573  
1574 # go ahead and use the provided name if one is not set
1575 0 ▶ unless ( defined $self->{'data_table'}->[0]->[$j] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"data_table"}[0][$j]) 0 0 0
1576 0 $self->{'data_table'}->[0]->[$j] = $namelist->[$i];
1577 }
1578 }
1579 }
1580 }
1581 else {
1582 563 $self->{$j} = {
1583 'name' => $namelist->[$i],
1584 'index' => $j,
1585 'AUTO' => 3,
1586 };
1587 563 $self->{'data_table'}->[0]->[$j] = $namelist->[$i];
1588 }
1589 }
1590  
1591 # check the number of columns
1592 72 ▶ if ( scalar @{$namelist} != $self->{'number_columns'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$namelist;} != $self->{"number_columns"}) 66 6 72
1593 66 $self->{'number_columns'} = scalar @{$namelist};
1594 }
1595  
1596 72 return 1;
1597 }
1598  
1599 ### Internal subroutine to generate hash of standard file format column names
1600 56 ▶sub standard_column_names {
Subroutine standard_column_names: called CC=2 SCAR=7.2
Pod documented
1601 56 my ( $self, $type ) = @_;
1602  
1603 56 my %column_names = (
1604 'gff' => [qw(Chromosome Source Type Start Stop Score Strand Phase Group)],
1605 'bed12' => [
1606 qw(Chromosome Start0 End Name Score Strand
1607 thickStart0 thickEnd itemRGB blockCount blockSizes blockStarts0)
1608 ],
1609 'bed6' => [qw(Chromosome Start0 End Name Score Strand)],
1610 'bdg' => [qw(Chromosome Start0 End Score)],
1611 'narrowpeak' => [
1612 qw(Chromosome Start0 End Name Score Strand signalValue
1613 pValue qValue peak)
1614 ],
1615 'broadpeak' =>
1616 [qw(Chromosome Start0 End Name Score Strand signalValue pValue qValue)],
1617 'gappedpeak' => [
1618 qw(Chromosome Start0 End Name Score Strand
1619 thickStart0 thickEnd itemRGB blockCount blockSizes blockStarts0
1620 signalValue pValue qValue)
1621 ],
1622 'sgr' => [qw(Chromo Start Score)],
1623 'ucsc16' => [
1624 qw(bin name chrom strand txStart0 txEnd cdsStart0 cdsEnd exonCount
1625 exonStarts0 exonEnds score name2 cdsStartSt cdsEndStat exonFrames)
1626 ],
1627 'ucsc15' => [ # genepredext
1628 qw(name chrom strand txStart0 txEnd cdsStart0 cdsEnd exonCount
1629 exonStarts0 exonEnds score name2 cdsStartSt cdsEndStat exonFrames)
1630 ],
1631 'ucsc12' => [ # knowngene
1632 qw(name chrom strand txStart0 txEnd cdsStart0 cdsEnd exonCount
1633 exonStarts0 exonEnds proteinID alignID)
1634 ],
1635 'ucsc11' => [ # refflat
1636 qw(geneName transcriptName chrom strand txStart0 txEnd cdsStart0
1637 cdsEnd exonCount exonStarts0 exonEnds)
1638 ],
1639 'ucsc10' => [ # genepred
1640 qw(name chrom strand txStart0 txEnd cdsStart0 cdsEnd exonCount
1641 exonStarts exonEnds)
1642 ],
1643 'bedpe' => [ # Bedtools BEDPE format
1644 qw(chrom1 start1 end1 chrom2 start2 end2 name score strand1 strand2)
1645 ],
1646 );
1647 56 $column_names{'genepredext'} = $column_names{'ucsc15'};
1648 56 $column_names{'knowngene'} = $column_names{'ucsc12'};
1649 56 $column_names{'refflat'} = $column_names{'ucsc11'};
1650 56 $column_names{'genepred'} = $column_names{'ucsc10'};
1651  
1652 56 ▶ if ( exists $column_names{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $column_names{$type}) { } 56 0 56
1653 56 return $column_names{$type};
1654 }
1655 else {
1656 0 cluck "ERROR: unrecognized standard column name format '$type'!";
1657 0 return;
1658 }
1659 }
1660  
1661 4 1;
1662  
1663 __END__
« lib/Bio/ToolBox/Data/core.pm ↑ Summary lib/Bio/ToolBox/db_helper.pm »