lib/Bio/ToolBox/Data.pm

filter: Statementstmt 53.6% 344 / 641 Branchbran 40.4% 132 / 326 Conditioncond 36.5% 76 / 208 MC/DCmcdc 17.8% 21 / 118 Subroutinesub 88.2% 30 / 34 Podpod 100% 22 / 22 totaltotal 46.3% 625 / 1349 CC 268 SCAR 63.4
CC
268
Cov
47%
summary_file
2756.0
add_transcript_length
306.0
new
278.4
CRAP 564.6
« lib/Bio/ToolBox/db_helper/seqfasta.pm ↑ Summary script/get_datasets.pl »
1 package Bio::ToolBox::Data;
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 Carp qw(carp cluck croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use List::Util qw(sum0 uniqint);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base 'Bio::ToolBox::Data::core';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Bio::ToolBox::Data::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Bio::ToolBox::db_helper qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 get_new_feature_list
12 get_new_genome_list
13 get_db_feature
14 4 );
15 4 ▶use Bio::ToolBox::utility qw(simplify_dataset_name sane_chromo_sort);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 our $VERSION = '2.03';
19  
20 ### Initialize
21  
22 72 ▶sub new {
Subroutine new: called CC=49 SCAR=56.3
Pod documented
23 72 my $class = shift;
24 72 my %args = @_;
25 72 ▶ if ( ref $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 25 47 72
26 25 $class = ref $class;
27 }
28  
29 # check for important arguments
30 72 ▶ $args{features} ||= $args{feature} || 'gene';
Condition60%3/5
$args{"features"} ||= $args{"feature"} || "gene"
l!l&&r!l&&!r
0720
$args{'feature'} || 'gene'
l!l
369
MC/DC0%0/2
$args{"features"} ||= $args{"feature"} || "gene"
$args{"features"} $args{'feature'}
Truth table33%1/3
A ||= B
A= $args{"features"}B= $args{'feature'}
ABresult
1X1
011
000
31 72 ▶ $args{stream} ||= $args{Stream} || 0;
Condition60%3/5
$args{"stream"} ||= $args{"Stream"} || 0
l!l&&r!l&&!r
1071
$args{'Stream'} || 0
l!l
071
MC/DC50%1/2
$args{"stream"} ||= $args{"Stream"} || 0
$args{"stream"} $args{'Stream'}
Truth table66%2/3
A ||= B
A= $args{"stream"}B= $args{'Stream'}
ABresult
1X1
011
000
32 72 ▶ $args{file} ||= $args{in} || undef;
Condition100%5/5
$args{"file"} ||= $args{"in"} || undef
l!l&&r!l&&!r
12258
$args{'in'} || undef
l!l
258
MC/DC100%2/2
$args{"file"} ||= $args{"in"} || undef
$args{"file"} $args{'in'}
Truth table100%3/3
A ||= B
A= $args{"file"}B= $args{'in'}
ABresult
1X1
011
000
33 72 ▶ $args{parse} ||= 0;
Condition100%2/2
$args{"parse"} ||= 0
l!l
765
MC/DC100%1/1
$args{"parse"} ||= 0
$args{"parse"}
Truth table100%2/2
A
A= $args{"parse"}
Aresult
11
00
34 72 ▶ $args{noheader} ||= 0;
Condition50%1/2
$args{"noheader"} ||= 0
l!l
072
MC/DC0%0/1
$args{"noheader"} ||= 0
$args{"noheader"}
Truth table50%1/2
A
A= $args{"noheader"}
Aresult
11
00
35  
36 # check for stream
37 72 ▶ if ( $args{stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"stream"}) 1 71 72
38 1 $class = 'Bio::ToolBox::Data::Stream';
39 1 load($class);
40 1 return $class->new(@_);
41 }
42  
43 # initialize
44 71 my $self = $class->SUPER::new();
45  
46 # prepare a new table based on the provided arguments
47 71 ▶ if ( $args{file} and $args{parse} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'file'} and $args{'parse'}) { } 7 64 71
elsif ($args{'file'}) { } 6 58 64
elsif (exists $args{'db'} and $args{'features'}) { } 1 57 58
Condition100%3/3
$args{"file"} and $args{"parse"}
!ll&&!rl&&r
5867
MC/DC100%2/2
$args{"file"} and $args{"parse"}
$args{"file"} $args{"parse"}
Truth table100%3/3
A and B
A= $args{"file"}B= $args{"parse"}
ABresult
0X0
100
111
48  
49 # parse from file
50 7 ▶ $args{subfeature} ||= q();
Condition100%2/2
$args{"subfeature"} ||= ""
l!l
25
MC/DC100%1/1
$args{"subfeature"} ||= ""
$args{"subfeature"}
Truth table100%2/2
A
A= $args{"subfeature"}
Aresult
11
00
51 7 ▶ unless ( $self->parse_table( \%args ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parse_table(\%args)) 5 2 7
52 5 my $l = $self->load_file( $args{file} );
53 5 ▶ return unless $l;
Branch: 1 branch
BranchTrueFalseTotal
unless $l 0 5 5
54 5 ▶ if ( $self->database
Branch: 1 branch
BranchTrueFalseTotal
if ($self->database and $self->database =~ /^Parsed:(.+)$/x and $self->feature_type eq "named") 5 0 5
Condition33%2/6
$self->database and $self->database =~ /^Parsed:(.+)$/x and $self->feature_type eq "named"
!ll&&!rl&&r
005
$self->database and $self->database =~ /^Parsed:(.+)$/x
!ll&&!rl&&r
005
MC/DC0%0/3
$self->database and $self->database =~ /^Parsed:(.+)$/x and $self->feature_type eq "named"
$self->database $self->database =~ /^Parsed:(.+)$/x $self->feature_type eq "named"
Truth table25%1/4
A and B and C
A= $self->databaseB= $self->database =~ /^Parsed:(.+)$/xC= $self->feature_type eq "named"
ABCresult
0XX0
10X0
1100
1111
55 and $self->database =~ /^Parsed:(.+)$/x
56 and $self->feature_type eq 'named' )
57 {
58  
59 # looks like the loaded file was from a previously parsed table
60 # let's try this again
61 # this may die if it doesn't work
62 5 $args{file} = $1;
63 5 $args{feature} = $self->feature;
64 5 $self->parse_table( \%args );
65 }
66 }
67 }
68 elsif ( $args{file} ) {
69  
70 # load from file
71 6 ▶ unless ( $self->load_file( $args{file}, $args{noheader} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->load_file($args{"file"}, $args{"noheader"})) 0 6 6
72 0 return;
73 }
74 }
75 ▶ elsif ( exists $args{db} and $args{features} ) {
Condition66%2/3
exists $args{"db"} and $args{"features"}
!ll&&!rl&&r
5701
MC/DC50%1/2
exists $args{"db"} and $args{"features"}
exists $args{"db"} $args{"features"}
Truth table66%2/3
A and B
A= exists $args{"db"}B= $args{"features"}
ABresult
0X0
100
111
76  
77 # generate new list
78 1 $self->feature( $args{features} );
79 1 $self->database( $args{db} );
80 1 $args{data} = $self;
81 1 my $result;
82 1 ▶ if ( $args{features} eq 'genome' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'features'} eq 'genome') { } 1 0 1
83  
84 # create a genomic interval list
85 # first must parse any exclusion list provided as a new Data object
86 1 ▶ $args{blacklist} ||= $args{exclude} || undef;
Condition40%2/5
$args{"blacklist"} ||= $args{"exclude"} || undef
l!l&&r!l&&!r
001
$args{'exclude'} || undef
l!l
01
MC/DC0%0/2
$args{"blacklist"} ||= $args{"exclude"} || undef
$args{"blacklist"} $args{'exclude'}
Truth table33%1/3
A ||= B
A= $args{"blacklist"}B= $args{'exclude'}
ABresult
1X1
011
000
87 1 ▶ if ( defined $args{blacklist} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"blacklist"}) 0 1 1
88 my $exclusion_Data = $self->new(
89 file => $args{blacklist},
90 0 parse => 0 # we don't need to parse
91 );
92 0 ▶ if ( $exclusion_Data and $exclusion_Data->feature_type eq 'coordinate' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclusion_Data and $exclusion_Data->feature_type eq 'coordinate') { } 0 0 0
Condition0%0/3
$exclusion_Data and $exclusion_Data->feature_type eq "coordinate"
!ll&&!rl&&r
000
MC/DC0%0/2
$exclusion_Data and $exclusion_Data->feature_type eq "coordinate"
$exclusion_Data $exclusion_Data->feature_type eq "coordinate"
Truth table0%0/3
A and B
A= $exclusion_DataB= $exclusion_Data->feature_type eq "coordinate"
ABresult
0X0
100
111
93 0 printf " Loaded %d exclusion list items\n",
94 $exclusion_Data->number_rows;
95 0 delete $args{blacklist};
96 0 delete $args{exclude};
97 0 $args{exclude} = $exclusion_Data;
98 }
99 else {
100 0 carp
101 "ERROR: Cannot not load exclusion coordinate list file $args{blacklist}!";
102 0 return;
103 }
104 }
105 1 $result = get_new_genome_list(%args);
106 }
107 else {
108 0 $result = get_new_feature_list(%args);
109 }
110 1 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 1 1
111 0 carp "ERROR: Cannot generate new $args{features} list!";
112 0 return;
113 }
114 }
115 else {
116 # a new empty structure
117  
118 # check to see if user provided column names
119 57 ▶ $args{columns} ||= $args{datasets} || undef;
Condition60%3/5
$args{"columns"} ||= $args{"datasets"} || undef
l!l&&r!l&&!r
3054
$args{'datasets'} || undef
l!l
054
MC/DC50%1/2
$args{"columns"} ||= $args{"datasets"} || undef
$args{"columns"} $args{'datasets'}
Truth table66%2/3
A ||= B
A= $args{"columns"}B= $args{'datasets'}
ABresult
1X1
011
000
120 57 ▶ if ( defined $args{columns} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $args{'columns'}) { } 3 54 57
elsif (exists $args{'gff'} and $args{'gff'}) { } 0 54 54
elsif (exists $args{'bed'} and $args{'bed'}) { } 0 54 54
elsif (exists $args{'ucsc'} and $args{'ucsc'}) { } 0 108 108
121 3 foreach my $c ( @{ $args{columns} } ) {
122 21 $self->add_column($c);
123 }
124 3 ▶ $self->{feature} = $args{feature} if exists $args{feature};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"feature"} 0 3 3
125 }
126  
127 # or possibly a specified format structure
128 ▶ elsif ( exists $args{gff} and $args{gff} ) {
Condition33%1/3
exists $args{"gff"} and $args{"gff"}
!ll&&!rl&&r
5400
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
129  
130 # use standard names for the number of columns indicated
131 # we trust that the user knows the subtle difference between gff versions
132 0 $self->add_gff_metadata( $args{gff} );
133 0 ▶ unless ( $self->extension =~ /g[tf]f/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->extension =~ /g[tf]f/) 0 0 0
134 $self->{extension} =
135 $args{gff} == 2.5 ? '.gtf'
136 0 ▶ : $args{gff} == 3 ? '.gff3'
Branch: 2 branches
BranchTrueFalseTotal
$args{'gff'} == 2.5 ? : 0 0 0
$args{'gff'} == 3 ? : 0 0 0
137 : '.gff';
138 }
139 }
140 ▶ elsif ( exists $args{bed} and $args{bed} ) {
Condition33%1/3
exists $args{"bed"} and $args{"bed"}
!ll&&!rl&&r
5400
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
141  
142 # use standard names for the number of columns indicated
143 0 ▶ 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 0 0
Condition0%0/3
$args{"bed"} =~ /^\d{1,2}$/ and $args{"bed"} >= 3
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"bed"} =~ /^\d{1,2}$/ and $args{"bed"} >= 3
$args{"bed"} =~ /^\d{1,2}$/ $args{"bed"} >= 3
Truth table0%0/3
A and B
A= $args{"bed"} =~ /^\d{1,2}$/B= $args{"bed"} >= 3
ABresult
0X0
100
111
144 0 carp 'ERROR: bed parameter must be an integer 3-12!';
145 0 return;
146 }
147 0 $self->add_bed_metadata( $args{bed} );
148 0 ▶ unless ( $self->extension =~ /bed|peak/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->extension =~ /bed|peak/) 0 0 0
149 0 $self->{extension} = '.bed';
150 }
151 }
152 ▶ elsif ( exists $args{ucsc} and $args{ucsc} ) {
Condition33%1/3
exists $args{"ucsc"} and $args{"ucsc"}
!ll&&!rl&&r
5400
MC/DC0%0/2
exists $args{"ucsc"} and $args{"ucsc"}
exists $args{"ucsc"} $args{"ucsc"}
Truth table33%1/3
A and B
A= exists $args{"ucsc"}B= $args{"ucsc"}
ABresult
0X0
100
111
153  
154 # a ucsc format such as refFlat, genePred, or genePredExt
155 0 my $u = $self->add_ucsc_metadata( $args{ucsc} );
156 0 ▶ unless ($u) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u) 0 0 0
157 0 carp 'ERROR: unrecognized number of columns for ucsc format!';
158 0 return;
159 }
160 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
161 0 $self->{extension} = '.ucsc';
162 }
163 }
164 }
165  
166 71 return $self;
167 }
168  
169 1 ▶sub duplicate {
Subroutine duplicate: called CC=4 SCAR=13.9
Pod documented
170 1 my $self = shift;
171  
172 # duplicate the data structure
173 1 my $columns = $self->list_columns;
174 1 ▶ my $Dupe = $self->new( 'columns' => $columns, )
Branch: 1 branch
BranchTrueFalseTotal
my $Dupe = $self->new("columns", $columns) or return 1 0 1
175 or return;
176  
177 # copy the metadata
178 1 for my $i ( 1 .. $self->number_columns ) {
179  
180 # column metadata
181 9 my %md = $self->metadata($i);
182 9 $Dupe->{$i} = \%md;
183 }
184 1 foreach (qw(feature program db bed gff ucsc vcf headers)) {
185  
186 # various keys
187 8 $Dupe->{$_} = $self->{$_};
188 }
189 1 my @comments = $self->comments;
190 1 push @{ $Dupe->{comments} }, @comments;
191  
192 1 return $Dupe;
193 }
194  
195 18 ▶sub parse_table {
Subroutine parse_table: called CC=39 SCAR=42.2
Pod documented
196 18 my $self = shift;
197 18 my $args = shift;
198 18 my $file;
199 18 ▶ if ( ref($args) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 12 6 18
200 12 ▶ $file = $args->{file} || q();
Condition50%1/2
$args->{'file'} || ''
l!l
120
MC/DC0%0/1
$args->{'file'} || ''
$args->{'file'}
Truth table50%1/2
A
A= $args->{'file'}
Aresult
11
00
201 }
202 else {
203 # no hash reference, assume just a file name
204 6 $file = $args;
205 6 $args = {}; # empty hash for below
206 }
207 18 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 18 18
208 0 carp 'ERROR: no annotation file provided to parse!';
209 0 return;
210 }
211  
212 # taste the file and open parser object
213 18 ▶ unless ( $self->filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filename) 13 5 18
214 13 $self->add_file_metadata($file);
215 }
216 18 $file = $self->check_file($file);
217 18 my ( $flavor, $format ) = $self->taste_file($file);
218 18 my $parser;
219 18 ▶ if ( defined $flavor ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flavor) { } 13 5 18
220 13 eval {
221 13 load 'Bio::ToolBox::Parser';
222 13 $parser = Bio::ToolBox::Parser->new(
223 file => $file,
224 flavor => $flavor,
225 filetype => $format,
226 );
227 };
228 13 ▶ unless ($parser) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser) 0 13 13
229  
230 # no parser object means no suitable parser adapter
231 0 return 0;
232 }
233 }
234 else {
235 # not suitable for parsing
236 5 return 0;
237 }
238 13 $self->format($format); # specify the specific format
239  
240 # additional user parameters
241 13 ▶ my $feature = $args->{feature} || q();
Condition100%2/2
$args->{'feature'} || ''
l!l
49
MC/DC100%1/1
$args->{'feature'} || ''
$args->{'feature'}
Truth table100%2/2
A
A= $args->{'feature'}
Aresult
11
00
242 13 ▶ my $subfeature = $args->{subfeature} || q();
Condition100%2/2
$args->{'subfeature'} || ''
l!l
211
MC/DC100%1/1
$args->{'subfeature'} || ''
$args->{'subfeature'}
Truth table100%2/2
A
A= $args->{'subfeature'}
Aresult
11
00
243 my $simplify =
244 ( exists $args->{simplify} and defined $args->{simplify} )
245 ? $args->{simplify}
246 13 ▶ : 1; # default is to simplify
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'simplify'} && defined $args->{'simplify'} ? : 0 13 13
Condition33%1/3
exists $args->{"simplify"} and defined $args->{"simplify"}
!ll&&!rl&&r
1300
MC/DC0%0/2
exists $args->{"simplify"} and defined $args->{"simplify"}
exists $args->{"simplify"} defined $args->{"simplify"}
Truth table33%1/3
A and B
A= exists $args->{"simplify"}B= defined $args->{"simplify"}
ABresult
0X0
100
111
247  
248 # set feature based on the type list from the parser
249 13 my $typelist = $parser->typelist;
250 13 ▶ unless ($feature) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feature) 9 4 13
251 9 ▶ if ( $typelist =~ /\b gene \b/xi ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($typelist =~ /\b gene \b/xi) { } 2 7 9
elsif ($typelist =~ /rna/i) { } 1 6 7
252 2 $feature = 'gene';
253 }
254 elsif ( $typelist =~ /rna/i ) {
255 1 $feature = 'rna'; # generic RNA
256 }
257 }
258  
259 # set additional parser parameters
260 13 $parser->simplify($simplify);
261 13 ▶ if ($subfeature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfeature) 2 11 13
262 2 ▶ $parser->do_exon(1) if $subfeature =~ /exon/i;
Branch: 1 branch
BranchTrueFalseTotal
if $subfeature =~ /exon/i 1 1 2
263 2 ▶ $parser->do_cds(1) if $subfeature =~ /cds/i;
Branch: 1 branch
BranchTrueFalseTotal
if $subfeature =~ /cds/i 1 1 2
264 2 ▶ $parser->do_utr(1) if $subfeature =~ /utr | untranslated/xi;
Branch: 1 branch
BranchTrueFalseTotal
if $subfeature =~ /utr | untranslated/xi 0 2 2
265 2 ▶ $parser->do_codon(1) if $subfeature =~ /codon/i;
Branch: 1 branch
BranchTrueFalseTotal
if $subfeature =~ /codon/i 0 2 2
266 }
267 13 ▶ if ( $feature =~ /gene$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature =~ /gene$/i) { } 4 9 13
268 4 $parser->do_gene(1);
269 }
270 else {
271 9 $parser->do_gene(0);
272 }
273 13 my $mrna_check = 0;
274 13 ▶ if ( lc($feature) eq 'mrna' and $parser->typelist !~ /mrna/i and not $self->last_row )
Branch: 1 branch
BranchTrueFalseTotal
if (lc $feature eq "mrna" and not $parser->typelist =~ /mrna/i and not $self->last_row) 0 13 13
Condition50%3/6
lc $feature eq "mrna" and not $parser->typelist =~ /mrna/i and not $self->last_row
!ll&&!rl&&r
1300
lc $feature eq "mrna" and not $parser->typelist =~ /mrna/i
!ll&&!rl&&r
1210
MC/DC0%0/3
lc $feature eq "mrna" and not $parser->typelist =~ /mrna/i and not $self->last_row
lc $feature eq "mrna" not $parser->typelist =~ /mrna/i not $self->last_row
Truth table50%2/4
A and not(B) and not(C)
A= lc $feature eq "mrna"B= not $parser->typelist =~ /mrna/iC= not $self->last_row
ABCresult
0XX0
10X0
1100
1111
275 {
276 # user requested mRNA for a new data file but it's not present in the type list
277 # look for it the hard way by parsing CDS too - sigh
278 0 load( 'Bio::ToolBox::GeneTools', 'is_coding' );
279 0 $parser->do_cds(1);
280 0 $mrna_check = 1;
281 }
282  
283 # parse the table
284 13 ▶ $parser->parse_file or return;
Branch: 1 branch
BranchTrueFalseTotal
$parser->parse_file or return 13 0 13
285  
286 # store the SeqFeature objects
287 13 ▶ if ( $self->number_rows > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->number_rows > 0) { } 5 8 13
288  
289 # we already have a table, presumably representing the features
290 5 my $count = 0;
291 5 my @missing;
292 $self->iterate(
293 65 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
294 65 my $row = shift;
295 65 my $f = $parser->fetch( $row->id );
296 65 ▶ if ($f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f) { } 65 0 65
297 65 $self->store_seqfeature( $row->row_index, $f );
298 65 $count++;
299 }
300 else {
301 0 push @missing, $row->id;
302 }
303 }
304 5 );
305 5 ▶ if ( $count != $self->number_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != $self->number_rows) 0 5 5
306 0 my $n = $self->number_rows;
307 0 my $d = $n - $count;
308 0 my $list;
309 0 ▶ if ( scalar @missing <= 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @missing <= 10) { } 0 0 0
310 0 $list = join ', ', @missing;
311 }
312 else {
313 $list = sprintf "%s...", join ', ',
314 0 ▶ ( map { $_ || q() } @missing[ 0 .. 9 ] );
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
315 }
316 0 croak <<PARSEFAIL;
317  
318 There were $d features out of $n that failed to match to a corresponding SeqFeature
319 in the annotation file '$file'.
320 Missing IDs include: $list.
321 This may happen when the annotation file has changed or is not being parsed in the
322 same manner. Suggestions include using the original annotation file or generating
323 a new table.
324 PARSEFAIL
325 }
326 }
327 else {
328 # create a new table
329 8 $self->add_column('Primary_ID');
330 8 $self->add_column('Name');
331  
332 # check for chromosome exclude
333 8 my $chr_exclude;
334 8 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 8 0 8
335 8 ▶ $chr_exclude = $args->{chrskip} || undef;
Condition50%1/2
$args->{'chrskip'} || undef
l!l
08
MC/DC0%0/1
$args->{'chrskip'} || undef
$args->{'chrskip'}
Truth table50%1/2
A
A= $args->{'chrskip'}
Aresult
11
00
336 }
337  
338 # fill table with features
339 8 ▶ while ( my $f = $parser->next_top_feature ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $parser->next_top_feature) 120 8 128
340 120 ▶ if ($chr_exclude) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chr_exclude) 0 120 120
341 0 ▶ next if $f->seq_id =~ m/$chr_exclude/xi;
Branch: 1 branch
BranchTrueFalseTotal
if $f->seq_id =~ /$chr_exclude/xi 0 0 0
342 }
343 120 ▶ if ($feature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature) { } 105 15 120
344  
345 # specific features are requested, look for them
346 105 ▶ if ( ( $f->type =~ /$feature/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($f->type =~ /$feature/i or $mrna_check and is_coding($f)) 98 7 105
Condition50%3/6
$f->type =~ /$feature/i or $mrna_check and is_coding($f)
l!l&&r!l&&!r
9807
$mrna_check and is_coding($f)
!ll&&!rl&&r
700
MC/DC33%1/3
$f->type =~ /$feature/i or $mrna_check and is_coding($f)
$f->type =~ /$feature/i $mrna_check is_coding($f)
Truth table50%2/4
A or B and C
A= $f->type =~ /$feature/iB= $mrna_checkC= is_coding($f)
ABCresult
1XX1
0111
00X0
0100
347 or ( $mrna_check and is_coding($f) ) )
348 {
349 98 my $index = $self->add_row( [ $f->primary_id, $f->display_name ] );
350 98 $self->store_seqfeature( $index, $f );
351 }
352 }
353 else {
354 # no feature defined, take everything
355 15 my $index = $self->add_row( [ $f->primary_id, $f->display_name ] );
356 15 $self->store_seqfeature( $index, $f );
357 }
358 }
359 8 ▶ unless ( $self->number_rows > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->number_rows > 0) 0 8 8
360 0 printf
361 " Zero '%s' features found!\n Check your feature or try generic features like gene, mRNA, or transcript\n",
362 $feature;
363 }
364 8 $self->database("Parsed:$file");
365 8 $self->feature($feature);
366 8 ▶ $self->add_comment("Chromosomes excluded: $chr_exclude") if $chr_exclude;
Branch: 1 branch
BranchTrueFalseTotal
if $chr_exclude 0 8 8
367  
368 # add input parsed file metadata
369 8 $self->add_file_metadata($file);
370  
371 # but delete some stuff, just want basename
372 8 $self->{extension} = q();
373 8 $self->{filename} = q();
374 8 $self->{path} = q();
375 }
376  
377 # successfully parsed
378 13 return 1;
379 }
380  
381 ### Column manipulation
382  
383 2 ▶sub column_values {
Subroutine column_values: called CC=4 SCAR=14.5
Pod documented
384 2 my ( $self, $column ) = @_;
385 2 ▶ return unless defined $column;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column 0 2 2
386 2 ▶ return unless exists $self->{$column};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$column} 0 2 2
387 2 my @values = map { $self->value( $_, $column ) } ( 0 .. $self->last_row );
388 2 ▶ return wantarray ? @values : \@values;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
389 }
390  
391 43 ▶sub add_column {
Subroutine add_column: called CC=10 SCAR=28.8
Pod documented
392 43 my ( $self, $name ) = @_;
393 43 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 43 43
394 43 my $column = $self->number_columns + 1;
395  
396 # check for array of column data
397 43 my $name_ref = ref $name;
398 43 ▶ if ( $name_ref eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name_ref eq 'ARRAY') { } 2 41 43
elsif ($name_ref eq 'Bio::DB::GFF::Typename') { } 0 41 41
elsif ($name_ref eq '') { } 41 0 41
399 2 ▶ if ( $self->last_row > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_row > 1) { } 2 0 2
400  
401 # table has existing data beyond column headers
402 2 ▶ if ( $self->last_row == ( scalar @{$name} - 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_row == scalar @{$name;} - 1) { } 2 0 2
403  
404 # same number of elements, add it the table
405 2 $self->{$column} = {
406 'name' => $name->[0],
407 'index' => $column,
408 };
409 2 for my $r ( 0 .. $self->last_row ) {
410 158 $self->{data_table}->[$r][$column] = $name->[$r];
411 }
412 }
413 else {
414 # different number of elements
415 0 cluck 'ERROR: array has different number of elements than Data table!';
416 0 return;
417 }
418 }
419 else {
420 # table has no existing data
421 0 $self->{$column} = {
422 'name' => $name->[0],
423 'index' => $column,
424 };
425 0 for my $r ( 0 .. $#{$name} ) {
426  
427 # this may auto-vivify an undefined value as first element
428 0 $self->{data_table}->[$r][$column] = $name->[$r];
429 }
430 0 $self->{last_row} = $#{$name};
431 }
432 }
433 elsif ( $name_ref eq 'Bio::DB::GFF::Typename' ) {
434  
435 # a Typename object that was selected from a SeqFeature::Store database
436 0 $self->{$column} = {
437 'name' => $name->asString,
438 'index' => $column,
439 };
440 0 $self->{data_table}->[0][$column] = $name->asString;
441 }
442 elsif ( $name_ref eq q() ) {
443  
444 # just a name
445 41 $self->{$column} = {
446 'name' => $name,
447 'index' => $column,
448 };
449 41 $self->{data_table}->[0][$column] = $name;
450 }
451 else {
452 0 cluck
453 "ERROR: unrecognized reference '$name_ref'! pass a scalar value or array reference";
454 0 return;
455 }
456  
457 # update metadata
458 43 $self->{number_columns} = $column;
459 43 ▶ delete $self->{column_indices} if exists $self->{column_indices};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"column_indices"} 0 43 43
460  
461 43 return $column;
462 }
463  
464 1 ▶sub copy_column {
Subroutine copy_column: called CC=2 SCAR=7.0
Pod documented
465 1 my $self = shift;
466 1 my $index = shift;
467 1 ▶ return unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 1 1
468 1 my $data = $self->column_values($index);
469 1 my $new_index = $self->add_column($data);
470 1 $self->copy_metadata( $index, $new_index );
471 1 return $new_index;
472 }
473  
474 ### Rows and Data access
475  
476 361 ▶sub add_row {
Subroutine add_row: called CC=9 SCAR=28.2
Pod documented
477 361 my $self = shift;
478 361 my @row_data;
479 361 ▶ if ( $_[0] and ref( $_[0] ) eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0] and ref $_[0] eq 'ARRAY') { } 318 43 361
elsif ($_[0] and ref $_[0] eq 'Bio::ToolBox::Data::Feature') { } 43 0 43
elsif ($_[0] and $_[0] =~ /\t/) { } 0 0 0
Condition66%2/3
$_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
043318
MC/DC50%1/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0] ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
480 318 @row_data = @{ $_[0] };
481 }
482 ▶ elsif ( $_[0] and ref( $_[0] ) eq 'Bio::ToolBox::Data::Feature' ) {
Condition33%1/3
$_[0] and ref $_[0] eq "Bio::ToolBox::Data::Feature"
!ll&&!rl&&r
0043
MC/DC0%0/2
$_[0] and ref $_[0] eq "Bio::ToolBox::Data::Feature"
$_[0] ref $_[0] eq "Bio::ToolBox::Data::Feature"
Truth table33%1/3
A and B
A= $_[0]B= ref $_[0] eq "Bio::ToolBox::Data::Feature"
ABresult
0X0
100
111
483 43 @row_data = $_[0]->row_values;
484 }
485 ▶ elsif ( $_[0] and $_[0] =~ /\t/ ) {
Condition0%0/3
$_[0] and $_[0] =~ /\t/
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] =~ /\t/
$_[0] $_[0] =~ /\t/
Truth table0%0/3
A and B
A= $_[0]B= $_[0] =~ /\t/
ABresult
0X0
100
111
486 0 @row_data = split /\t/, $_[0];
487 }
488 else {
489 0 @row_data = map {'.'} ( 1 .. $self->{number_columns} );
490 }
491 361 ▶ if ( scalar(@row_data) > $self->{number_columns} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @row_data > $self->{'number_columns'}) { } 0 361 361
elsif (scalar @row_data < $self->{'number_columns'}) { } 0 361 361
492 0 carp
493 'ERROR: row added has more elements than table columns! truncating row elements';
494 0 splice @row_data, 0, $self->{number_columns};
495 }
496 elsif ( scalar(@row_data) < $self->{number_columns} ) {
497 0 push @row_data, ( '.' x ( $self->{number_columns} - scalar(@row_data) ) );
498 }
499 361 unshift @row_data, q(); # blank element to shift index
500 361 my $row_index = $self->{last_row} + 1;
501 361 $self->{data_table}->[$row_index] = \@row_data;
502 361 $self->{last_row}++;
503 361 return $row_index;
504 }
505  
506 10 ▶sub get_row {
Subroutine get_row: called CC=4 SCAR=14.0
Pod documented
507 10 my ( $self, $row_number ) = @_;
508 10 ▶ return unless defined $self->{data_table}->[$row_number];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data_table"}[$row_number] 0 10 10
509 10 my @options = (
510 'data' => $self,
511 'index' => $row_number,
512 );
513 10 ▶ if ( exists $self->{SeqFeatureObjects}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SeqFeatureObjects"} and defined $self->{"SeqFeatureObjects"}[$row_number]) 5 5 10
Condition66%2/3
exists $self->{"SeqFeatureObjects"} and defined $self->{"SeqFeatureObjects"}[$row_number]
!ll&&!rl&&r
505
MC/DC50%1/2
exists $self->{"SeqFeatureObjects"} and defined $self->{"SeqFeatureObjects"}[$row_number]
exists $self->{"SeqFeatureObjects"} defined $self->{"SeqFeatureObjects"}[$row_number]
Truth table66%2/3
A and B
A= exists $self->{"SeqFeatureObjects"}B= defined $self->{"SeqFeatureObjects"}[$row_number]
ABresult
0X0
100
111
514 and defined $self->{SeqFeatureObjects}->[$row_number] )
515 {
516 5 push @options, 'feature', $self->{SeqFeatureObjects}->[$row_number];
517 }
518 10 return Bio::ToolBox::Data::Feature->new(@options);
519 }
520  
521 2 ▶sub delete_row {
Subroutine delete_row: called CC=10 SCAR=35.5
Pod documented
522 2 my $self = shift;
523 2 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
524  
525 # select best method based on how many rows are requested
526 # empirical testing shows mostly equivalent with splice slightly faster
527 # but with large data tables and massive deletions splicing is increasingly slower
528 # 10M table: 1K deletion +8% comparatively, 5K -13%, 50K -230%, 500K -2000%
529 2 ▶ if ( scalar(@_) < 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 1000) { } 2 0 2
530 0 my @deleted = uniqint( sort { $b <=> $a } @_ );
531 2 ▶ while (@deleted) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deleted) 2 2 4
532 2 my $d = shift @deleted;
533 2 splice( @{ $self->{data_table} }, $d, 1 );
534 2 $self->{last_row}--;
535 2 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SeqFeatureObjects"}) 0 2 2
536 0 splice( @{ $self->{SeqFeatureObjects} }, $d, 1 );
537 }
538 }
539 }
540 else {
541 0 my %del = map { $_ => 1 } @_;
542 0 my @new_table;
543 0 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'SeqFeatureObjects'}) { } 0 0 0
544 0 my @new_seqf;
545 0 for my $i ( 0 .. $self->{last_row} ) {
546 0 ▶ next if exists $del{$i};
Branch: 1 branch
BranchTrueFalseTotal
if exists $del{$i} 0 0 0
547 0 push @new_table, $self->{data_table}->[$i];
548 0 push @new_seqf, $self->{SeqFeatureObjects}->[$i];
549 }
550 0 $self->{SeqFeatureObjects} = \@new_seqf;
551 }
552 else {
553 0 for my $i ( 0 .. $self->last_row ) {
554 0 ▶ next if exists $del{$i};
Branch: 1 branch
BranchTrueFalseTotal
if exists $del{$i} 0 0 0
555 0 push @new_table, $self->{data_table}->[$i];
556 }
557 }
558 0 $self->{data_table} = \@new_table;
559 0 $self->{last_row} = scalar(@new_table) - 1;
560 }
561 2 return 1;
562 }
563  
564 237 ▶sub row_values {
Subroutine row_values: called CC=2 SCAR=7.0
Pod documented
565 237 my ( $self, $index ) = @_;
566 my @data = # skip first value
567 237 @{ $self->{data_table}->[$index] }[ 1 .. $self->{number_columns} ];
568 237 ▶ return wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 237 0 237
569 }
570  
571 278 ▶sub value {
Subroutine value: called CC=4 SCAR=16.4
Pod documented
572 278 my ( $self, $row, $column, $value ) = @_;
573 278 ▶ return unless ( defined $row and defined $column );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row and defined $column 0 278 278
Condition33%1/3
defined $row and defined $column
!ll&&!rl&&r
00278
MC/DC0%0/2
defined $row and defined $column
defined $row defined $column
Truth table33%1/3
A and B
A= defined $rowB= defined $column
ABresult
0X0
100
111
574 278 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 278 278
575 0 $self->{data_table}->[$row][$column] = $value;
576 }
577 278 return $self->{data_table}->[$row][$column];
578 }
579  
580 11 ▶sub row_stream {
Subroutine row_stream: called CC=1 SCAR=0.0
Pod documented
581 11 my $self = shift;
582 11 return Bio::ToolBox::Data::Iterator->new($self);
583 }
584  
585 8 ▶sub iterate {
Subroutine iterate: called CC=3 SCAR=11.3
Pod documented
586 8 my $self = shift;
587 8 my $code = shift;
588 8 ▶ unless ( ref($code) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code eq "CODE") 0 8 8
589 0 carp 'ERROR: iterate_function() method requires a code reference!';
590 0 return;
591 }
592 8 my $stream = $self->row_stream;
593 8 ▶ while ( my $row = $stream->next_row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $stream->next_row) 152 8 160
594 152 &{$code}($row);
595 }
596 8 return 1;
597 }
598  
599 #### Stored SeqFeature manipulation
600  
601 178 ▶sub store_seqfeature {
Subroutine store_seqfeature: called CC=5 SCAR=17.5
Pod documented
602 178 my ( $self, $row_i, $seqfeature ) = @_;
603 178 ▶ unless ( defined $row_i and ref($seqfeature) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row_i and ref $seqfeature) 0 178 178
Condition33%1/3
defined $row_i and ref $seqfeature
!ll&&!rl&&r
00178
MC/DC0%0/2
defined $row_i and ref $seqfeature
defined $row_i ref $seqfeature
Truth table33%1/3
A and B
A= defined $row_iB= ref $seqfeature
ABresult
0X0
100
111
604 0 carp 'ERROR: must provide a row index and SeqFeature object!';
605 }
606 178 ▶ carp 'ERROR: invalid row index' unless ( $row_i <= $self->last_row );
Branch: 1 branch
BranchTrueFalseTotal
unless $row_i <= $self->last_row 0 178 178
607 178 ▶ $self->{SeqFeatureObjects} ||= [];
Condition100%2/2
$self->{"SeqFeatureObjects"} ||= []
l!l
16513
MC/DC100%1/1
$self->{"SeqFeatureObjects"} ||= []
$self->{"SeqFeatureObjects"}
Truth table100%2/2
A
A= $self->{"SeqFeatureObjects"}
Aresult
11
00
608 178 $self->{SeqFeatureObjects}->[$row_i] = $seqfeature;
609 178 return 1;
610 }
611  
612 0 ▶sub delete_seqfeature {
Subroutine delete_seqfeature: not called CC=3 SCAR=24.8
Pod documented
613 0 my ( $self, $row_i ) = @_;
614 0 ▶ carp 'ERROR: invalid row index' unless ( $row_i <= $self->last_row );
Branch: 1 branch
BranchTrueFalseTotal
unless $row_i <= $self->last_row 0 0 0
615 0 ▶ return unless $self->{SeqFeatureObjects};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SeqFeatureObjects"} 0 0 0
616 0 undef $self->{SeqFeatureObjects}->[$row_i];
617 }
618  
619 0 ▶sub collapse_gene_transcripts {
Subroutine collapse_gene_transcripts: not called CC=13 SCAR=52.0
Pod documented
620 0 my $self = shift;
621 0 ▶ unless ( $self->feature_type eq 'named' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->feature_type eq "named") 0 0 0
622 0 carp 'ERROR: Table does not contain named features!';
623 0 return;
624 }
625  
626 # load module
627 0 load Bio::ToolBox::GeneTools, qw(collapse_transcripts);
628  
629 # collapse the transcripts
630 0 my $success = 0;
631 0 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'SeqFeatureObjects'}) { } 0 0 0
632  
633 # we should have stored SeqFeature objects, probably from parsed table
634 0 for my $i ( 0 .. $self->last_row ) {
635 0 ▶ my $feature = $self->{SeqFeatureObjects}->[$i] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $feature = $self->{"SeqFeatureObjects"}[$i] or next 0 0 0
636 0 ▶ my $collSeqFeat = collapse_transcripts($feature) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $collSeqFeat = collapse_transcripts($feature) or next 0 0 0
637 0 $success += $self->store_seqfeature( $i, $collSeqFeat );
638 }
639 }
640 else {
641 # no stored SeqFeature objects, probably names pointing to a database
642 # we will have to fetch the feature from a database
643 0 my $db = $self->open_meta_database(1); # force open a new db connection
644 0 ▶ unless ($db) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
645 0 carp 'ERROR: No SeqFeature objects stored and no database connection!';
646 0 return;
647 }
648 0 my $name_i = $self->name_column;
649 0 my $id_i = $self->id_column;
650 0 my $type_i = $self->type_column;
651 0 for my $i ( 0 .. $self->last_row ) {
652 0 ▶ my $feature = get_db_feature(
Branch: 1 branch
BranchTrueFalseTotal
my $feature = get_db_feature("db", $db, "name", $self->value($i, $name_i) || undef, "type", $self->value($i, $type_i) || undef, "id", $self->value($i, $id_i) || undef) or next 0 0 0
Condition0%0/6
$self->value($i, $name_i) || undef
l!l
00
$self->value($i, $type_i) || undef
l!l
00
$self->value($i, $id_i) || undef
l!l
00
MC/DC0%0/1
$self->value($i, $name_i) || undef
$self->value($i, $name_i)
MC/DC0%0/1
$self->value($i, $type_i) || undef
$self->value($i, $type_i)
MC/DC0%0/1
$self->value($i, $id_i) || undef
$self->value($i, $id_i)
Truth table0%0/2
A
A= $self->value($i, $name_i)
Aresult
11
00
Truth table0%0/2
A
A= $self->value($i, $type_i)
Aresult
11
00
Truth table0%0/2
A
A= $self->value($i, $id_i)
Aresult
11
00
653 db => $db,
654 name => $self->value( $i, $name_i ) || undef,
655 type => $self->value( $i, $type_i ) || undef,
656 id => $self->value( $i, $id_i ) || undef,
657 ) or next;
658 0 ▶ my $collSeqFeat = collapse_transcripts($feature) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $collSeqFeat = collapse_transcripts($feature) or next 0 0 0
659 0 $success += $self->store_seqfeature( $i, $collSeqFeat );
660 }
661 }
662  
663 0 return $success;
664 }
665  
666 0 ▶sub add_transcript_length {
Subroutine add_transcript_length: not called CC=17 SCAR=57.2
Pod documented
667 0 my $self = shift;
668 0 ▶ my $subfeature = shift || 'exon';
Condition0%0/2
shift() || 'exon'
l!l
00
MC/DC0%0/1
shift() || 'exon'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
669  
670 # load module
671 0 load Bio::ToolBox::GeneTools, qw(get_transcript_length get_transcript_cds_length
672 get_transcript_5p_utr_length get_transcript_3p_utr_length);
673  
674 # determine name and calculation method
675 0 my $length_calculator;
676 my $length_name;
677 0 ▶ if ( $subfeature eq 'exon' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($subfeature eq 'exon') { } 0 0 0
elsif ($subfeature eq 'cds') { } 0 0 0
elsif ($subfeature eq '5p_utr') { } 0 0 0
elsif ($subfeature eq '3p_utr') { } 0 0 0
678 0 $length_calculator = \&get_transcript_length;
679 0 $length_name = 'Merged_Transcript_Length';
680 }
681 elsif ( $subfeature eq 'cds' ) {
682 0 $length_calculator = \&get_transcript_cds_length;
683 0 $length_name = 'Transcript_CDS_Length';
684 }
685 elsif ( $subfeature eq '5p_utr' ) {
686 0 $length_calculator = \&get_transcript_5p_utr_length;
687 0 $length_name = 'Transcript_5p_UTR_Length';
688 }
689 elsif ( $subfeature eq '3p_utr' ) {
690 0 $length_calculator = \&get_transcript_3p_utr_length;
691 0 $length_name = 'Transcript_3p_UTR_Length';
692 }
693 else {
694 0 carp "ERROR: unrecognized subfeature type '$subfeature'! No length calculated";
695 0 return;
696 }
697  
698 # add new column and calculate
699 0 my $length_i = $self->add_column($length_name);
700 0 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'SeqFeatureObjects'}) { } 0 0 0
701  
702 # we should have stored SeqFeature objects, probably from parsed table
703 0 for my $i ( 0 .. $self->last_row ) {
704 0 ▶ my $feature = $self->{SeqFeatureObjects}->[$i] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $feature = $self->{"SeqFeatureObjects"}[$i] or next 0 0 0
705 0 my $length = &{$length_calculator}($feature);
706 0 ▶ $self->{data_table}->[$i][$length_i] = $length || $feature->length;
Condition0%0/3
$length || $feature->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$length || $feature->length
$length $feature->length
Truth table0%0/3
A || B
A= $lengthB= $feature->length
ABresult
1X1
011
000
707 }
708 }
709 else {
710 # no stored SeqFeature objects, probably names pointing to a database
711 # we will have to fetch the feature from a database
712 0 my $db = $self->open_meta_database(1); # force open a new db connection
713 0 ▶ unless ($db) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
714 0 carp 'ERROR: No SeqFeature objects stored and no database connection!';
715 0 return;
716 }
717 0 my $name_i = $self->name_column;
718 0 my $id_i = $self->id_column;
719 0 my $type_i = $self->type_column;
720 0 for my $i ( 0 .. $self->last_row ) {
721 0 ▶ my $feature = get_db_feature(
Branch: 1 branch
BranchTrueFalseTotal
my $feature = get_db_feature("db", $db, "name", $self->value($i, $name_i) || undef, "type", $self->value($i, $type_i) || undef, "id", $self->value($i, $id_i) || undef) or next 0 0 0
Condition0%0/6
$self->value($i, $name_i) || undef
l!l
00
$self->value($i, $type_i) || undef
l!l
00
$self->value($i, $id_i) || undef
l!l
00
MC/DC0%0/1
$self->value($i, $name_i) || undef
$self->value($i, $name_i)
MC/DC0%0/1
$self->value($i, $type_i) || undef
$self->value($i, $type_i)
MC/DC0%0/1
$self->value($i, $id_i) || undef
$self->value($i, $id_i)
Truth table0%0/2
A
A= $self->value($i, $name_i)
Aresult
11
00
Truth table0%0/2
A
A= $self->value($i, $type_i)
Aresult
11
00
Truth table0%0/2
A
A= $self->value($i, $id_i)
Aresult
11
00
722 db => $db,
723 name => $self->value( $i, $name_i ) || undef,
724 type => $self->value( $i, $type_i ) || undef,
725 id => $self->value( $i, $id_i ) || undef,
726 ) or next;
727 0 my $length = &{$length_calculator}($feature);
728 0 ▶ $self->{data_table}->[$i][$length_i] = $length || $feature->length;
Condition0%0/3
$length || $feature->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$length || $feature->length
$length $feature->length
Truth table0%0/3
A || B
A= $lengthB= $feature->length
ABresult
1X1
011
000
729 0 $self->store_seqfeature( $i, $feature );
730  
731 # to store or not to store? May explode memory, but could save from
732 # expensive db calls later
733 }
734 }
735  
736 0 return $length_i;
737 }
738  
739 ### Data structure manipulation
740  
741 2 ▶sub sort_data {
Subroutine sort_data: called CC=21 SCAR=38.5
Pod documented
742 2 my $self = shift;
743 2 my $index = shift;
744 2 ▶ my $direction = shift || 'i';
Condition50%1/2
shift() || 'i'
l!l
20
MC/DC0%0/1
shift() || 'i'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
745  
746 # confirm options
747 2 ▶ return unless exists $self->{$index}{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$index}{"name"} 0 2 2
748 2 ▶ unless ( $direction eq 'i' or $direction eq 'd' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($direction eq "i" or $direction eq "d") 0 2 2
Condition66%2/3
$direction eq "i" or $direction eq "d"
l!l&&r!l&&!r
110
MC/DC0%0/2
$direction eq "i" or $direction eq "d"
$direction eq "i" $direction eq "d"
Truth table66%2/3
A or B
A= $direction eq "i"B= $direction eq "d"
ABresult
1X1
011
000
749 0 carp "ERROR: unrecognized sort order '$direction'! Must be i or d";
750 0 return;
751 }
752  
753 # put items into appropriate bins for sorting
754 2 my @numeric_items;
755 my @mixed_items;
756 2 for my $row_i ( 1 .. $self->last_row ) {
757 82 my $v = $self->value( $row_i, $index );
758 82 ▶ if ( $v =~ /\d/ && looks_like_number($v) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($v =~ /\d/ and &looks_like_number($v)) { } 0 82 82
elsif ($v =~ /^ (\d+) (\D+) (\d+) $/x) { } 0 82 82
elsif ($v =~ /^ (\d+) (\D.*) $/x) { } 0 82 82
elsif ($v =~ /^ (.*\D) (\d+) $/x) { } 78 4 82
Condition33%1/3
$v =~ /\d/ and &looks_like_number($v)
!ll&&!rl&&r
0820
MC/DC0%0/2
$v =~ /\d/ and &looks_like_number($v)
$v =~ /\d/ &looks_like_number($v)
Truth table33%1/3
A and B
A= $v =~ /\d/B= &looks_like_number($v)
ABresult
0X0
100
111
759 0 push @numeric_items, [ $v, $row_i ];
760 }
761 elsif ( $v =~ /^ (\d+) (\D+) (\d+) $/x ) {
762  
763 # integer at both ends of string
764 0 push @mixed_items, [ $1, $2, $3, $row_i ];
765 }
766 elsif ( $v =~ /^ (\d+) (\D.*) $/x ) {
767  
768 # integer at beginning of string
769 # use identical integer at the end
770 0 push @mixed_items, [ $1, $2, 1, $row_i ];
771 }
772 elsif ( $v =~ /^ (.*\D) (\d+) $/x ) {
773  
774 # integer at end of string
775 # use identical integer at the beginning
776 78 push @mixed_items, [ 1, $1, $2, $row_i ];
777 }
778 else {
779  
780 # no discernable integer in string
781 # we intentionally avoid integers in middle of string cus it's not clear
782 # those should be used in sorting, so just do the string asciibetically
783 # use identical integer at the beginning and end
784 4 push @mixed_items, [ 1, $v, 1, $row_i ];
785 }
786 }
787  
788 # sort each bin into replacement table
789 2 my @new_table;
790 2 push @new_table, $self->{data_table}->[0];
791 2 ▶ if ( $direction eq 'i' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq "i") 1 1 2
792 1 ▶ if (@numeric_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@numeric_items) 0 1 1
793 0 push @new_table, map { $self->{data_table}->[ $_->[1] ] }
794 0 sort { $a->[0] <=> $b->[0] } @numeric_items;
795 }
796 1 ▶ if (@mixed_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mixed_items) 1 0 1
797 4 push @new_table, map { $self->{data_table}->[ $_->[3] ] }
798 1 ▶ sort { $a->[0] <=> $b->[0] or $a->[1] cmp $b->[1] or $a->[2] <=> $b->[2] }
Condition33%2/6
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] || $Bio::ToolBox::Data::a->[1] cmp $Bio::ToolBox::Data::b->[1] || $Bio::ToolBox::Data::a->[2] <=> $Bio::ToolBox::Data::b->[2]
l!l&&r!l&&!r
400
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] or $Bio::ToolBox::Data::a->[1] cmp $Bio::ToolBox::Data::b->[1]
l!l&&r!l&&!r
040
MC/DC0%0/3
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] || $Bio::ToolBox::Data::a->[1] cmp $Bio::ToolBox::Data::b->[1] || $Bio::ToolBox::Data::a->[2] <=> $Bio::ToolBox::Data::b->[2]
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] $Bio::ToolBox::Data::a->[1] cmp $Bio::ToolBox::Data::b->[1] $Bio::ToolBox::Data::a->[2] <=> $Bio::ToolBox::Data::b->[2]
Truth table25%1/4
A or B || C
A= $Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0]B= $Bio::ToolBox::Data::a->[1] cmp $Bio::ToolBox::Data::b->[1]C= $Bio::ToolBox::Data::a->[2] <=> $Bio::ToolBox::Data::b->[2]
ABCresult
1XX1
01X1
0011
0000
799 @mixed_items;
800 }
801 }
802 2 ▶ if ( $direction eq 'd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq "d") 1 1 2
803 1 ▶ if (@numeric_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@numeric_items) 0 1 1
804 0 push @new_table, map { $self->{data_table}->[ $_->[1] ] }
805 0 sort { $b->[0] <=> $a->[0] } @numeric_items;
806 }
807 1 ▶ if (@mixed_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mixed_items) 1 0 1
808 78 push @new_table, map { $self->{data_table}->[ $_->[3] ] }
809 1 ▶ sort { $b->[0] <=> $a->[0] or $b->[1] cmp $a->[1] or $b->[2] <=> $a->[2] }
Condition33%2/6
$Bio::ToolBox::Data::b->[0] <=> $Bio::ToolBox::Data::a->[0] || $Bio::ToolBox::Data::b->[1] cmp $Bio::ToolBox::Data::a->[1] || $Bio::ToolBox::Data::b->[2] <=> $Bio::ToolBox::Data::a->[2]
l!l&&r!l&&!r
0770
$Bio::ToolBox::Data::b->[0] <=> $Bio::ToolBox::Data::a->[0] or $Bio::ToolBox::Data::b->[1] cmp $Bio::ToolBox::Data::a->[1]
l!l&&r!l&&!r
0077
MC/DC0%0/3
$Bio::ToolBox::Data::b->[0] <=> $Bio::ToolBox::Data::a->[0] || $Bio::ToolBox::Data::b->[1] cmp $Bio::ToolBox::Data::a->[1] || $Bio::ToolBox::Data::b->[2] <=> $Bio::ToolBox::Data::a->[2]
$Bio::ToolBox::Data::b->[0] <=> $Bio::ToolBox::Data::a->[0] $Bio::ToolBox::Data::b->[1] cmp $Bio::ToolBox::Data::a->[1] $Bio::ToolBox::Data::b->[2] <=> $Bio::ToolBox::Data::a->[2]
Truth table25%1/4
A or B || C
A= $Bio::ToolBox::Data::b->[0] <=> $Bio::ToolBox::Data::a->[0]B= $Bio::ToolBox::Data::b->[1] cmp $Bio::ToolBox::Data::a->[1]C= $Bio::ToolBox::Data::b->[2] <=> $Bio::ToolBox::Data::a->[2]
ABCresult
1XX1
01X1
0011
0000
810 @mixed_items;
811 }
812 }
813 2 $self->{data_table} = \@new_table;
814  
815 2 return 1;
816 }
817  
818 2 ▶sub gsort_data {
Subroutine gsort_data: called CC=12 SCAR=25.2
Pod documented
819 2 my $self = shift;
820  
821 # identify indices
822 2 my $chromo_i = $self->chromo_column;
823 2 my $start_i = $self->start_column;
824 2 ▶ my $stop_i = $self->stop_column || $start_i;
Condition66%2/3
$self->stop_column || $start_i
l!l&&r!l&&!r
101
MC/DC50%1/2
$self->stop_column || $start_i
$self->stop_column $start_i
Truth table66%2/3
A || B
A= $self->stop_columnB= $start_i
ABresult
1X1
011
000
825 2 my $coord_i = $self->coord_column;
826  
827 # collect the data by chromosome: start and end
828 2 my %chrom2row;
829 2 ▶ if ( $chromo_i and $start_i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chromo_i and $start_i) { } 1 1 2
elsif ($coord_i) { } 1 0 1
Condition66%2/3
$chromo_i and $start_i
!ll&&!rl&&r
101
MC/DC50%1/2
$chromo_i and $start_i
$chromo_i $start_i
Truth table66%2/3
A and B
A= $chromo_iB= $start_i
ABresult
0X0
100
111
830 1 for my $row ( 1 .. $self->last_row ) {
831 78 my $c = $self->{data_table}->[$row]->[$chromo_i];
832 78 ▶ $chrom2row{$c} ||= [];
Condition100%2/2
$chrom2row{$c} ||= []
l!l
771
MC/DC100%1/1
$chrom2row{$c} ||= []
$chrom2row{$c}
Truth table100%2/2
A
A= $chrom2row{$c}
Aresult
11
00
833 78 push @{ $chrom2row{$c} },
834 [
835 $self->{data_table}->[$row]->[$start_i],
836 $self->{data_table}->[$row]->[$stop_i],
837 78 $self->{data_table}->[$row]
838 ];
839 }
840 }
841 elsif ($coord_i) {
842 1 my $dt = $self->{data_table};
843 4 ▶ $self->iterate( sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
844 4 my $row = shift;
845 4 my $c = $row->seq_id;
846 4 ▶ $chrom2row{$c} ||= [];
Condition100%2/2
$chrom2row{$c} ||= []
l!l
31
MC/DC100%1/1
$chrom2row{$c} ||= []
$chrom2row{$c}
Truth table100%2/2
A
A= $chrom2row{$c}
Aresult
11
00
847 4 ▶ push @{ $chrom2row{$c} },
Condition33%1/3
$row->end || $row->start
l!l&&r!l&&!r
400
MC/DC0%0/2
$row->end || $row->start
$row->end $row->start
Truth table33%1/3
A || B
A= $row->endB= $row->start
ABresult
1X1
011
000
848 [
849 $row->start,
850 $row->end || $row->start,
851 $dt->[ $row->row_index ]
852 ];
853
854 1 } );
855 }
856 else {
857 0 carp 'ERROR: no chromosome and start/position columns to sort!';
858 0 return;
859 }
860  
861 # get sane chromosome order
862 2 my @chroms = sane_chromo_sort( keys %chrom2row );
863  
864 # sort the table
865 2 my @sorted_data;
866 2 push @sorted_data, $self->{data_table}->[0];
867 2 ▶ if ( $self->gff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gff) { } 1 1 2
868  
869 # sort by increasing start and decreasing end, i.e. decreasing length
870 # this should mostly handle putting genes/transcripts before exons
871 1 foreach my $chr (@chroms) {
872 78 push @sorted_data, map { $_->[2] }
873 1 ▶ sort { $a->[0] <=> $b->[0] or $b->[1] <=> $a->[1] } @{ $chrom2row{$chr} };
Condition100%3/3
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] || $Bio::ToolBox::Data::b->[1] <=> $Bio::ToolBox::Data::a->[1]
l!l&&r!l&&!r
167136
MC/DC100%2/2
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] || $Bio::ToolBox::Data::b->[1] <=> $Bio::ToolBox::Data::a->[1]
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] $Bio::ToolBox::Data::b->[1] <=> $Bio::ToolBox::Data::a->[1]
Truth table100%3/3
A || B
A= $Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0]B= $Bio::ToolBox::Data::b->[1] <=> $Bio::ToolBox::Data::a->[1]
ABresult
1X1
011
000
874 }
875 }
876 else {
877 # sort by increasing start followed by increasing end, i.e. increasing length
878 1 foreach my $chr (@chroms) {
879 4 push @sorted_data, map { $_->[2] }
880 1 ▶ sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @{ $chrom2row{$chr} };
Condition33%1/3
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] || $Bio::ToolBox::Data::a->[1] <=> $Bio::ToolBox::Data::b->[1]
l!l&&r!l&&!r
400
MC/DC0%0/2
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] || $Bio::ToolBox::Data::a->[1] <=> $Bio::ToolBox::Data::b->[1]
$Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0] $Bio::ToolBox::Data::a->[1] <=> $Bio::ToolBox::Data::b->[1]
Truth table33%1/3
A || B
A= $Bio::ToolBox::Data::a->[0] <=> $Bio::ToolBox::Data::b->[0]B= $Bio::ToolBox::Data::a->[1] <=> $Bio::ToolBox::Data::b->[1]
ABresult
1X1
011
000
881 }
882 }
883 2 $self->{data_table} = \@sorted_data;
884 2 return 1;
885 }
886  
887 1 ▶sub split_data {
Subroutine split_data: called CC=12 SCAR=40.2
Pod documented
888 1 my ( $self, $part, $total_parts ) = @_;
889  
890 1 ▶ unless ( $part and $total_parts ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($part and $total_parts) 0 1 1
Condition33%1/3
$part and $total_parts
!ll&&!rl&&r
001
MC/DC0%0/2
$part and $total_parts
$part $total_parts
Truth table33%1/3
A and B
A= $partB= $total_parts
ABresult
0X0
100
111
891 0 confess 'FATAL: ordinal part and total number of parts not passed';
892 }
893 1 my $part_length = int( $self->last_row / $total_parts );
894  
895 # check for SeqFeatureObjects array
896 1 ▶ if ( exists $self->{SeqFeatureObjects}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SeqFeatureObjects"} and scalar @{$self->{"SeqFeatureObjects"};} != scalar @{$self->{"data_table"};}) 0 1 1
Condition33%1/3
exists $self->{"SeqFeatureObjects"} and scalar @{$self->{"SeqFeatureObjects"};} != scalar @{$self->{"data_table"};}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $self->{"SeqFeatureObjects"} and scalar @{$self->{"SeqFeatureObjects"};} != scalar @{$self->{"data_table"};}
exists $self->{"SeqFeatureObjects"} scalar @{$self->{"SeqFeatureObjects"};} != scalar @{$self->{"data_table"};}
Truth table33%1/3
A and B
A= exists $self->{"SeqFeatureObjects"}B= scalar @{$self->{"SeqFeatureObjects"};} != scalar @{$self->{"data_table"};}
ABresult
0X0
100
111
897 0 and scalar @{ $self->{SeqFeatureObjects} } !=
898 0 scalar @{ $self->{data_table} } )
899 {
900  
901 # it needs to be the same length as the data table, it should be
902 0 ▶ while ( scalar @{ $self->{SeqFeatureObjects} } < scalar @{ $self->{data_table} } )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"SeqFeatureObjects"};} < scalar @{$self->{"data_table"};}) 0 0 0
903 {
904 0 push @{ $self->{SeqFeatureObjects} }, undef;
905 }
906 }
907  
908 # splicing based on which part we do
909 1 ▶ if ( $part == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($part == 1) { } 0 1 1
elsif ($part == $total_parts) { } 1 0 1
910  
911 # remove all but the first part
912 0 splice( @{ $self->{'data_table'} }, $part_length + 1 );
913 0 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SeqFeatureObjects"}) 0 0 0
914 0 splice( @{ $self->{SeqFeatureObjects} }, $part_length + 1 );
915 }
916 }
917 elsif ( $part == $total_parts ) {
918  
919 # remove all but the last part
920 1 splice( @{ $self->{'data_table'} }, 1, $part_length * ( $total_parts - 1 ) );
921 1 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SeqFeatureObjects"}) 0 1 1
922 0 splice( @{ $self->{SeqFeatureObjects} },
923 1, $part_length * ( $total_parts - 1 ) );
924 }
925 }
926 else {
927 # splicing in the middle requires two rounds
928  
929 # remove the last parts
930 0 splice( @{ $self->{'data_table'} }, ( $part * $part_length ) + 1 );
931  
932 # remove the first parts
933 0 splice( @{ $self->{'data_table'} }, 1, $part_length * ( $part - 1 ) );
934  
935 0 ▶ if ( exists $self->{SeqFeatureObjects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SeqFeatureObjects"}) 0 0 0
936 0 splice( @{ $self->{SeqFeatureObjects} }, ( $part * $part_length ) + 1 );
937 0 splice( @{ $self->{SeqFeatureObjects} }, 1, $part_length * ( $part - 1 ) );
938 }
939 }
940  
941 # update last row metadata
942 1 $self->{'last_row'} = scalar( @{ $self->{'data_table'} } ) - 1;
943  
944 # re-open a new un-cached database connection
945 1 ▶ if ( exists $self->{db_connection} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"db_connection"}) 0 1 1
946 0 delete $self->{db_connection};
947 }
948 1 return 1;
949 }
950  
951 ### File functions
952  
953 1 ▶sub reload_children {
Subroutine reload_children: called CC=10 SCAR=23.4
Pod documented
954 1 my $self = shift;
955 1 my @files = @_;
956 1 ▶ return unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 1 1
957  
958 # prepare Stream
959 1 my $class = 'Bio::ToolBox::Data::Stream';
960 1 load $class;
961  
962 # open first stream
963 1 my $first = shift @files;
964 1 my $Stream = $class->new( in => $first );
965 1 ▶ unless ($Stream) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Stream) 0 1 1
966 0 carp "ERROR: unable to load first child file $first";
967 0 return;
968 }
969  
970 # destroy current data table and metadata
971 1 $self->{data_table} = [];
972 1 for my $i ( 1 .. $self->number_columns ) {
973 0 delete $self->{$i};
974 }
975  
976 # copy the metadata
977 1 foreach (qw(program feature db bed gff ucsc headers number_columns last_row)) {
978 9 $self->{$_} = $Stream->{$_};
979 }
980 1 for my $i ( 1 .. $Stream->number_columns ) {
981 4 my %md = $Stream->metadata($i);
982 4 $self->{$i} = \%md;
983 }
984 1 push @{ $self->{comments} }, ( $Stream->comments );
985  
986 # first row column headers
987 1 $self->{data_table}->[0] = [ @{ $Stream->{data_table}->[0] } ];
988  
989 # load the data
990 1 ▶ while ( my $row = $Stream->next_row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $Stream->next_row) 39 1 40
991 39 $self->add_row($row);
992 }
993 1 $Stream->close_fh;
994  
995 # load remaining files
996 1 foreach my $file (@files) {
997 2 my $Stream1 = $class->new( in => $file );
998 2 ▶ if ( $Stream1->number_columns != $self->number_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Stream1->number_columns != $self->number_columns) 0 2 2
999 0 confess "FATAL: child file $file has a different number of columns!";
1000 }
1001 2 ▶ while ( my $row = $Stream1->next_row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $Stream1->next_row) 78 2 80
1002 78 my $a = $row->row_values;
1003 78 $self->add_row($a);
1004 }
1005 2 $Stream1->close_fh;
1006 }
1007 1 $self->verify;
1008  
1009 # clean up
1010 1 unlink( $first, @files );
1011 1 return $self->last_row;
1012 }
1013  
1014 ### Export summary data file
1015 0 ▶sub summary_file {
Subroutine summary_file: not called CC=52 SCAR=79.2
Pod documented
1016 0 my $self = shift;
1017  
1018 # Collect passed arguments
1019 0 my %args = @_;
1020  
1021 # parameters
1022 # either one or more datasets can be summarized
1023 0 ▶ my $outfile = $args{'filename'} || undef;
Condition0%0/2
$args{'filename'} || undef
l!l
00
MC/DC0%0/1
$args{'filename'} || undef
$args{'filename'}
Truth table0%0/2
A
A= $args{'filename'}
Aresult
11
00
1024 0 my @datasets;
1025 0 ▶ if ( $args{dataset} and ref( $args{dataset} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'dataset'} and ref $args{'dataset'} eq 'ARRAY') { } 0 0 0
elsif ($args{'dataset'} and ref $args{'dataset'} eq 'SCALAR') { } 0 0 0
Condition0%0/3
$args{"dataset"} and ref $args{"dataset"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"dataset"} and ref $args{"dataset"} eq "ARRAY"
$args{"dataset"} ref $args{"dataset"} eq "ARRAY"
Truth table0%0/3
A and B
A= $args{"dataset"}B= ref $args{"dataset"} eq "ARRAY"
ABresult
0X0
100
111
1026 0 @datasets = @{ $args{dataset} };
1027 }
1028 ▶ elsif ( $args{dataset} and ref( $args{dataset} ) eq 'SCALAR' ) {
Condition0%0/3
$args{"dataset"} and ref $args{"dataset"} eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"dataset"} and ref $args{"dataset"} eq "SCALAR"
$args{"dataset"} ref $args{"dataset"} eq "SCALAR"
Truth table0%0/3
A and B
A= $args{"dataset"}B= ref $args{"dataset"} eq "SCALAR"
ABresult
0X0
100
111
1029 0 push @datasets, $args{dataset};
1030 }
1031 0 my @startcolumns;
1032 0 ▶ if ( $args{startcolumn} and ref( $args{startcolumn} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'startcolumn'} and ref $args{'startcolumn'} eq 'ARRAY') { } 0 0 0
elsif ($args{'startcolumn'} and ref $args{'startcolumn'} eq 'SCALAR') { } 0 0 0
Condition0%0/3
$args{"startcolumn"} and ref $args{"startcolumn"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"startcolumn"} and ref $args{"startcolumn"} eq "ARRAY"
$args{"startcolumn"} ref $args{"startcolumn"} eq "ARRAY"
Truth table0%0/3
A and B
A= $args{"startcolumn"}B= ref $args{"startcolumn"} eq "ARRAY"
ABresult
0X0
100
111
1033 0 @startcolumns = @{ $args{startcolumn} };
1034 }
1035 ▶ elsif ( $args{startcolumn} and ref( $args{startcolumn} ) eq 'SCALAR' ) {
Condition0%0/3
$args{"startcolumn"} and ref $args{"startcolumn"} eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"startcolumn"} and ref $args{"startcolumn"} eq "SCALAR"
$args{"startcolumn"} ref $args{"startcolumn"} eq "SCALAR"
Truth table0%0/3
A and B
A= $args{"startcolumn"}B= ref $args{"startcolumn"} eq "SCALAR"
ABresult
0X0
100
111
1036 0 push @startcolumns, $args{startcolumn};
1037 }
1038 0 my @endcolumns;
1039 0 ▶ $args{endcolumn} ||= $args{stopcolumn};
Condition0%0/3
$args{"endcolumn"} ||= $args{"stopcolumn"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"endcolumn"} ||= $args{"stopcolumn"}
$args{"endcolumn"} $args{"stopcolumn"}
Truth table0%0/3
A ||= B
A= $args{"endcolumn"}B= $args{"stopcolumn"}
ABresult
1X1
011
000
1040 0 ▶ if ( $args{endcolumn} and ref( $args{endcolumn} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'endcolumn'} and ref $args{'endcolumn'} eq 'ARRAY') { } 0 0 0
elsif ($args{'endcolumn'} and ref $args{'endcolumn'} eq 'SCALAR') { } 0 0 0
Condition0%0/3
$args{"endcolumn"} and ref $args{"endcolumn"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"endcolumn"} and ref $args{"endcolumn"} eq "ARRAY"
$args{"endcolumn"} ref $args{"endcolumn"} eq "ARRAY"
Truth table0%0/3
A and B
A= $args{"endcolumn"}B= ref $args{"endcolumn"} eq "ARRAY"
ABresult
0X0
100
111
1041 0 @endcolumns = @{ $args{endcolumn} };
1042 }
1043 ▶ elsif ( $args{endcolumn} and ref( $args{endcolumn} ) eq 'SCALAR' ) {
Condition0%0/3
$args{"endcolumn"} and ref $args{"endcolumn"} eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"endcolumn"} and ref $args{"endcolumn"} eq "SCALAR"
$args{"endcolumn"} ref $args{"endcolumn"} eq "SCALAR"
Truth table0%0/3
A and B
A= $args{"endcolumn"}B= ref $args{"endcolumn"} eq "SCALAR"
ABresult
0X0
100
111
1044 0 push @endcolumns, $args{endcolumn};
1045 }
1046 0 ▶ $args{method} ||= 'mean';
Condition0%0/2
$args{"method"} ||= "mean"
l!l
00
MC/DC0%0/1
$args{"method"} ||= "mean"
$args{"method"}
Truth table0%0/2
A
A= $args{"method"}
Aresult
11
00
1047 0 ▶ if ( $args{method} ne 'mean'
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"method"} ne "mean" and $args{"method"} ne "median" and $args{"method"} ne "trimmean") 0 0 0
Condition0%0/6
$args{"method"} ne "mean" and $args{"method"} ne "median" and $args{"method"} ne "trimmean"
!ll&&!rl&&r
000
$args{"method"} ne "mean" and $args{"method"} ne "median"
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"method"} ne "mean" and $args{"method"} ne "median" and $args{"method"} ne "trimmean"
$args{"method"} ne "mean" $args{"method"} ne "median" $args{"method"} ne "trimmean"
Truth table0%0/4
A and B and C
A= $args{"method"} ne "mean"B= $args{"method"} ne "median"C= $args{"method"} ne "trimmean"
ABCresult
0XX0
10X0
1100
1111
1048 and $args{method} ne 'median'
1049 and $args{method} ne 'trimmean' )
1050 {
1051 0 carp 'ERROR: unrecognized summary file method!';
1052 0 return;
1053 }
1054  
1055 # Check required values
1056 0 ▶ unless ( defined $outfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outfile) 0 0 0
1057 0 ▶ if ( $self->basename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->basename) { } 0 0 0
1058  
1059 # use the opened file's filename if it exists
1060 # prepend the path if it exists
1061 # the extension will be added later
1062 0 $outfile = $self->path . $self->basename;
1063 }
1064 else {
1065 0 carp 'ERROR: no filename passed to write_summary_data!';
1066 0 return;
1067 }
1068 }
1069  
1070 # Identify possible dataset columns
1071 0 my %possibles;
1072 0 my %skip = map { $_ => 1 } qw (systematicname name id alias aliases type class
1073 geneclass chromosome chromo seq_id seqid start stop end gene strand
1074 length primary_id merged_transcript_length transcript_cds_length
1075 transcript_5p_utr_length transcript_3p_utr_length);
1076  
1077 # walk through the dataset names
1078 0 $possibles{unknown} = [];
1079 0 for my $i ( 1 .. $self->number_columns ) {
1080 0 ▶ if ( not exists $skip{ lc $self->{$i}{'name'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $skip{lc $self->{$i}{"name"}}) 0 0 0
1081 0 ▶ my $d = $self->metadata( $i, 'dataset' ) || undef;
Condition0%0/2
$self->metadata($i, 'dataset') || undef
l!l
00
MC/DC0%0/1
$self->metadata($i, 'dataset') || undef
$self->metadata($i, 'dataset')
Truth table0%0/2
A
A= $self->metadata($i, 'dataset')
Aresult
11
00
1082 0 ▶ if ( defined $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d) { } 0 0 0
1083  
1084 # we have what appears to be a dataset column
1085 0 ▶ $possibles{$d} ||= [];
Condition0%0/2
$possibles{$d} ||= []
l!l
00
MC/DC0%0/1
$possibles{$d} ||= []
$possibles{$d}
Truth table0%0/2
A
A= $possibles{$d}
Aresult
11
00
1086 0 push @{ $possibles{$d} }, $i;
1087 }
1088 else {
1089 # still an unknown possibility
1090 0 push @{ $possibles{unknown} }, $i;
1091 }
1092 }
1093 }
1094  
1095 # check datasets
1096 0 ▶ unless (@datasets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@datasets) 0 0 0
1097 0 ▶ if ( scalar( keys %possibles ) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %possibles > 1) 0 0 0
1098  
1099 # we will always have the unknown category, so anything more than one
1100 # means we found legitimate dataset columns
1101 0 delete $possibles{unknown};
1102 }
1103 0 @datasets = sort { $a cmp $b } keys %possibles;
1104 }
1105  
1106 # check starts
1107 0 ▶ if ( scalar @startcolumns != scalar @datasets ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @startcolumns != scalar @datasets) 0 0 0
1108 0 @startcolumns = (); # ignore what we were given?
1109 0 foreach my $d (@datasets) {
1110  
1111 # take the first column with this dataset
1112 0 push @startcolumns, $possibles{$d}->[0];
1113 }
1114 }
1115  
1116 # check stops
1117 0 ▶ if ( scalar @endcolumns != scalar @datasets ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @endcolumns != scalar @datasets) 0 0 0
1118 0 @endcolumns = (); # ignore what we were given?
1119 0 foreach my $d (@datasets) {
1120  
1121 # take the last column with this dataset
1122 0 push @endcolumns, $possibles{$d}->[-1];
1123 }
1124 }
1125  
1126 # Prepare Data object to store the summed data
1127 0 my $summed_data = $self->new(
1128 feature => 'averaged_windows',
1129 columns => [ 'Window', 'Midpoint' ],
1130 );
1131  
1132 # Go through each dataset
1133 0 foreach my $d ( 0 .. $#datasets ) {
1134  
1135 # Prepare score column name
1136 0 my $data_name = simplify_dataset_name( $datasets[$d] );
1137  
1138 # add column
1139 0 my $i = $summed_data->add_column($data_name);
1140 0 $summed_data->metadata( $i, 'dataset', $datasets[$d] );
1141  
1142 # tag for remembering we're working with percentile bins
1143 0 my $do_percentile = 0;
1144  
1145 # remember the row
1146 0 my $row = 1;
1147  
1148 # Collect summarized data
1149 0 for my $column ( $startcolumns[$d] .. $endcolumns[$d] ) {
1150  
1151 # determine the midpoint position of the window
1152 # this assumes the column metadata has start and stop
1153 0 my $midpoint = int(
1154 sum0(
1155 $self->metadata( $column, 'start' ),
1156 $self->metadata( $column, 'stop' )
1157 ) / 2
1158 );
1159  
1160 # convert midpoint to fraction of 1000 for plotting if necessary
1161 0 ▶ if ( substr( $self->name($column), -1 ) eq '%' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->name($column), -1) eq "%") 0 0 0
1162 0 $midpoint *= 10; # midpoint * 0.01 * 1000 bp
1163 0 $do_percentile++;
1164 }
1165 0 ▶ if ( $do_percentile and substr( $self->name($column), -2 ) eq 'bp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_percentile and substr($self->name($column), -2) eq "bp") 0 0 0
Condition0%0/3
$do_percentile and substr($self->name($column), -2) eq "bp"
!ll&&!rl&&r
000
MC/DC0%0/2
$do_percentile and substr($self->name($column), -2) eq "bp"
$do_percentile substr($self->name($column), -2) eq "bp"
Truth table0%0/3
A and B
A= $do_percentileB= substr($self->name($column), -2) eq "bp"
ABresult
0X0
100
111
1166  
1167 # working on the extension after the percentile bins
1168 0 $midpoint += 1000;
1169 }
1170  
1171 # collect the values in the column
1172 0 my @values;
1173 0 for my $row ( 1 .. $self->last_row ) {
1174 0 my $v = $self->value( $row, $column );
1175 0 ▶ if ( looks_like_number($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($v)) { } 0 0 0
1176 0 push @values, $v;
1177 }
1178 else {
1179 # we treat this as zero, as opposed to skipping it, so that we
1180 # do not over-emphasize the remaining signal from those columns
1181 # that do not have much signal to begin with
1182 # it distorts the interpretation
1183 0 push @values, 0;
1184 }
1185 }
1186  
1187 # adjust if log value
1188 0 ▶ my $log = $self->metadata( $column, 'log2' ) || 0;
Condition0%0/2
$self->metadata($column, 'log2') || 0
l!l
00
MC/DC0%0/1
$self->metadata($column, 'log2') || 0
$self->metadata($column, 'log2')
Truth table0%0/2
A
A= $self->metadata($column, 'log2')
Aresult
11
00
1189 0 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 0 0 0
1190 0 @values = map { 2**$_ } @values;
1191 }
1192  
1193 # determine mean value
1194 0 my $window_value;
1195 0 my $num_values = scalar(@values);
1196 0 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) { } 0 0 0
1197 0 ▶ if ( $args{method} eq 'mean' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'method'} eq 'mean') { } 0 0 0
elsif ($args{'method'} eq 'trimmean') { } 0 0 0
elsif ($args{'method'} eq 'median') { } 0 0 0
1198 0 $window_value = sum0(@values) / $num_values;
1199 }
1200 elsif ( $args{method} eq 'trimmean' ) {
1201 0 ▶ if ( scalar @values == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @values == 1) { } 0 0 0
elsif (scalar @values < 100) { } 0 0 0
1202 0 $window_value = $values[0];
1203 }
1204 elsif ( scalar @values < 100 ) {
1205  
1206 # use standard mean
1207 0 $window_value = sum0(@values) / $num_values;
1208 }
1209 else {
1210 0 @values = sort { $a <=> $b } @values;
1211 0 my $x = sprintf( "%.0f", $num_values / 100 );
1212 0 $window_value = sum0( @values[ $x .. ( $num_values - $x ) ] ) /
1213 ( $num_values - ( 2 * $x ) );
1214 }
1215 }
1216 elsif ( $args{method} eq 'median' ) {
1217 0 ▶ if ( scalar @values == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @values == 1) { } 0 0 0
elsif ($num_values & 1) { } 0 0 0
1218 0 $window_value = $values[0];
1219 }
1220 elsif ( $num_values & 1 ) {
1221  
1222 # odd number of values
1223 0 $window_value = $values[ ( $#values / 2 ) ];
1224 }
1225 else {
1226 0 my $mid = $num_values / 2;
1227 0 $window_value = sum0( $values[ $mid - 1 ], $values[$mid] ) / 2;
1228 }
1229 }
1230 0 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 0 0 0
1231 0 $window_value = log($window_value) / log(2);
1232 }
1233 }
1234 else {
1235 0 $window_value = 0;
1236 }
1237  
1238 # push to summed output
1239 0 ▶ if ( $d == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 0) { } 0 0 0
1240  
1241 # this is the first dataset, so we need to add a row
1242 $summed_data->add_row(
1243 0 [ $self->{$column}{'name'}, $midpoint, $window_value ] );
1244 }
1245 else {
1246 # we're summarizing multiple datasets, we already have name midpoint
1247 # first do sanity check
1248 0 ▶ if ( $summed_data->value( $row, 2 ) != $midpoint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($summed_data->value($row, 2) != $midpoint) 0 0 0
1249 0 carp
1250 'ERROR: unable to summarize multiple datasets with nonequal columns of data!';
1251 0 return;
1252 }
1253 0 $summed_data->value( $row, $i, $window_value );
1254 }
1255 0 $row++;
1256 }
1257 }
1258  
1259 # Write summed data
1260 0 $outfile =~ s/\.txt (\.gz)?$//xi; # strip any .txt or .gz extensions if present
1261 0 my $written_file = $summed_data->write_file(
1262 'filename' => sprintf( "%s_summary.txt", $outfile ),
1263 'gz' => 0,
1264 );
1265 0 return $written_file;
1266 }
1267  
1268 4 1;
1269  
1270 __END__
« lib/Bio/ToolBox/db_helper/seqfasta.pm ↑ Summary script/get_datasets.pl »