lib/Bio/ToolBox/Parser.pm

filter: Statementstmt 81.6% 151 / 185 Branchbran 70.7% 75 / 106 Conditioncond 57.6% 15 / 26 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 85.1% 23 / 27 Podpod 100% 20 / 20 totaltotal 76.1% 288 / 378 CC 65 SCAR 33.8
CC
65
Cov
76%
new
63.7
comments
12.0
next_top_feature
6.4
CRAP 29.3
« lib/Bio/ToolBox/SeqFeature.pm ↑ Summary script/split_data_file.pl »
1 package Bio::ToolBox::Parser;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(carp cluck croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Bio::ToolBox::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Bio::ToolBox::SeqFeature; # alternative to Bio::SeqFeature::Lite
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '2.02';
12  
13 36 ▶sub new {
Subroutine new: called CC=39 SCAR=41.5
Pod documented
14 36 my $class = shift;
15  
16 # passed arguments
17 36 my %args;
18 36 ▶ if ( scalar(@_) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 36 36
19 0 $args{file} = shift;
20 }
21 else {
22 36 %args = @_;
23 }
24  
25 # determine file, format, and parser subclass
26 36 ▶ my $file = $args{file} || $args{table} || undef;
Condition40%2/5
$args{'file'} || $args{'table'} || undef
l!l
360
$args{"file"} or $args{"table"}
l!l&&r!l&&!r
3600
MC/DC0%0/2
$args{'file'} || $args{'table'} || undef
$args{"file"} $args{"table"}
Truth table33%1/3
A or B
A= $args{"file"}B= $args{"table"}
ABresult
1X1
011
000
27 36 ▶ my $flavor = $args{flavor} || undef;
Condition100%2/2
$args{'flavor'} || undef
l!l
1323
MC/DC100%1/1
$args{'flavor'} || undef
$args{'flavor'}
Truth table100%2/2
A
A= $args{'flavor'}
Aresult
11
00
28 36 ▶ my $filetype = $args{filetype} || $args{'format'} || undef;
Condition80%4/5
$args{'filetype'} || $args{'format'} || undef
l!l
1323
$args{"filetype"} or $args{"format"}
l!l&&r!l&&!r
13023
MC/DC50%1/2
$args{'filetype'} || $args{'format'} || undef
$args{"filetype"} $args{"format"}
Truth table66%2/3
A or B
A= $args{"filetype"}B= $args{"format"}
ABresult
1X1
011
000
29 36 ▶ if ( not $flavor or not $filetype ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flavor and $filetype) 23 13 36
Condition66%2/3
$flavor and $filetype
!ll&&!rl&&r
23013
MC/DC50%1/2
$flavor and $filetype
$flavor $filetype
Truth table66%2/3
A and B
A= $flavorB= $filetype
ABresult
0X0
100
111
30 23 ▶ if ( $class =~ m/Bio::ToolBox::Parser::(\w+)/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /Bio::ToolBox::Parser::(\w+)/x) 23 0 23
31  
32 # we got the flavor directly from a flavored parser new function
33 23 ▶ $flavor ||= $1;
Condition33%1/3
$flavor ||= $1
l!l&&r!l&&!r
0230
MC/DC0%0/2
$flavor ||= $1
$flavor $1
Truth table33%1/3
A ||= B
A= $flavorB= $1
ABresult
1X1
011
000
34 }
35 23 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 23 0 23
36  
37 # get the flavor and file type directly by tasting the file
38 # we use this in preference over what user may have provided
39 23 ( $flavor, $filetype ) = Bio::ToolBox::Data->taste_file( $args{file} );
40 }
41 }
42 36 ▶ if ( $flavor and $flavor =~ m/^(?: gff | bed | ucsc )$/xi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flavor and $flavor =~ /^(?: gff | bed | ucsc )$/xi) { } 36 0 36
Condition33%1/3
$flavor and $flavor =~ /^(?: gff | bed | ucsc )$/xi
!ll&&!rl&&r
0036
MC/DC0%0/2
$flavor and $flavor =~ /^(?: gff | bed | ucsc )$/xi
$flavor $flavor =~ /^(?: gff | bed | ucsc )$/xi
Truth table33%1/3
A and B
A= $flavorB= $flavor =~ /^(?: gff | bed | ucsc )$/xi
ABresult
0X0
100
111
43 36 $class = 'Bio::ToolBox::Parser::' . $flavor;
44 36 load $class;
45 }
46 else {
47 # let the caller print errors as appropriate
48 0 return;
49 }
50  
51 # initialize self
52 36 my $self = {
53 'fh' => undef,
54 'file' => $file,
55 'filetype' => $filetype,
56 'do_gene' => 1,
57 'do_exon' => 0,
58 'do_cds' => 0,
59 'do_utr' => 0,
60 'do_codon' => 0,
61 'do_name' => 0,
62 'share' => 0,
63 'simplify' => 0,
64 'source' => undef,
65 'typelist' => q(), # string list of observed feature types
66 'seq_ids' => {}, # hash of seq_id to length
67 'loaded' => {}, # hash of primary_id to SeqFeature object
68 'top_features' => [], # list of top features
69 'eof' => 0,
70 'comments' => [], # array of comment lines
71 'convertor_sub' => undef,
72 'sfclass' => 'Bio::ToolBox::SeqFeature',
73 };
74 36 bless $self, $class;
75  
76 # parser specific object keys
77 36 ▶ if ( $flavor eq 'bed' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flavor eq 'bed') { } 20 16 36
elsif ($flavor eq 'gff') { } 11 5 16
elsif ($flavor eq 'ucsc') { } 5 0 5
78 20 $self->{bed} = undef;
79 20 $self->{line_count} = 0;
80 }
81 elsif ( $flavor eq 'gff' ) {
82 11 ▶ if ( $filetype and $filetype eq 'gtf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filetype and $filetype eq 'gtf') { } 4 7 11
Condition66%2/3
$filetype and $filetype eq "gtf"
!ll&&!rl&&r
074
MC/DC50%1/2
$filetype and $filetype eq "gtf"
$filetype $filetype eq "gtf"
Truth table66%2/3
A and B
A= $filetypeB= $filetype eq "gtf"
ABresult
0X0
100
111
83 4 $self->{gtf} = 1;
84 }
85 else {
86 7 $self->{gtf} = 0;
87 }
88 11 $self->{orphans} = [];
89 11 $self->{duplicate_ids} = {};
90 }
91 elsif ( $flavor eq 'ucsc' ) {
92 5 $self->{share} = 1; # always true
93 5 $self->{source} = 'UCSC'; # presumptive default
94 5 $self->{gene2seqf} = {}; # hash of gene names to SeqFeature objects
95 5 $self->{id2count} = {}; # hash of seen IDs
96 5 $self->{counts} = {}; # hash of RNA types to count
97 5 $self->{refseqsum} = {}; # RefSeq Summary external data
98 5 $self->{refseqstat} = {}; # RefSeq Stats external data
99 5 $self->{kgxref} = {}; # knownGene RefSeq external data
100 5 $self->{ensembldata} = {}; # Ensembl external data
101 5 $self->{line_count} = 0;
102 }
103  
104 # process remaining arguments
105 36 ▶ if ( exists $args{simplify} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"simplify"}) 4 32 36
106 4 $self->simplify( $args{simplify} );
107 }
108 36 ▶ if ( exists $args{do_gene} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"do_gene"}) 5 31 36
109 5 $self->do_gene( $args{do_gene} );
110 }
111 36 ▶ if ( exists $args{do_exon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"do_exon"}) 9 27 36
112 9 $self->do_exon( $args{do_exon} );
113 }
114 36 ▶ if ( exists $args{do_cds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"do_cds"}) 5 31 36
115 5 $self->do_cds( $args{do_cds} );
116 }
117 36 ▶ if ( exists $args{do_utr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"do_utr"}) 1 35 36
118 1 $self->do_utr( $args{do_utr} );
119 }
120 36 ▶ if ( exists $args{do_codon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"do_codon"}) 0 36 36
121 0 $self->do_codon( $args{do_codon} );
122 }
123 36 ▶ if ( exists $args{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"source"}) 0 36 36
124 0 $self->source( $args{source} );
125 }
126 36 ▶ if ( exists $args{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"class"}) 18 18 36
127 18 my $class = $args{class};
128 18 eval { load $class; };
129 18 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Bio::ToolBox::Parser::EVAL_ERROR) { } 0 18 18
130 0 croak $EVAL_ERROR;
131 }
132 else {
133 18 $self->{sfclass} = $class;
134 }
135 }
136 36 ▶ if ( $flavor eq 'ucsc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flavor eq "ucsc") 5 31 36
137  
138 # lots of accessory files for UCSC tables
139 5 ▶ if ( exists $args{refseqsum} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'refseqsum'}) { } 0 5 5
elsif (exists $args{'summary'}) { } 0 5 5
140 0 $self->load_extra_data( $args{refseqsum}, 'refseqsum' );
141 }
142 elsif ( exists $args{summary} ) {
143 0 $self->load_extra_data( $args{summary}, 'refseqsum' );
144 }
145 5 ▶ if ( exists $args{refseqstat} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'refseqstat'}) { } 0 5 5
elsif (exists $args{'status'}) { } 0 5 5
146 0 $self->load_extra_data( $args{refseqstat}, 'refseqstat' );
147 }
148 elsif ( exists $args{status} ) {
149 0 $self->load_extra_data( $args{status}, 'refseqstat' );
150 }
151 5 ▶ if ( exists $args{kgxref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"kgxref"}) 0 5 5
152 0 $self->load_extra_data( $args{kgxref}, 'kgxref' );
153 }
154 5 ▶ if ( exists $args{ensembltogenename} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'ensembltogenename'}) { } 0 5 5
elsif (exists $args{'ensname'}) { } 0 5 5
155 0 $self->load_extra_data( $args{ensembltogenename}, 'ensembltogene' );
156 }
157 elsif ( exists $args{ensname} ) {
158 0 $self->load_extra_data( $args{ensname}, 'ensembltogene' );
159 }
160 5 ▶ if ( exists $args{ensemblsource} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'ensemblsource'}) { } 0 5 5
elsif (exists $args{'enssrc'}) { } 1 4 5
161 0 $self->load_extra_data( $args{ensemblsource}, 'ensemblsource' );
162 }
163 elsif ( exists $args{enssrc} ) {
164 1 $self->load_extra_data( $args{enssrc}, 'ensemblsource' );
165 }
166 }
167  
168 # open the file
169 36 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 36 0 36
170 36 $self->open_file;
171 }
172  
173 # finished
174 36 return $self;
175 }
176  
177 697 ▶sub do_gene {
Subroutine do_gene: called CC=3 SCAR=11.0
Pod documented
178 697 my $self = shift;
179 697 ▶ return 0 if ( ref($self) eq 'Bio::ToolBox::Parser::bed' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "Bio::ToolBox::Parser::bed" 12 685 697
180 685 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 675 685
181 10 $self->{'do_gene'} = shift;
182 }
183 685 return $self->{'do_gene'};
184 }
185  
186 379 ▶sub do_exon {
Subroutine do_exon: called CC=2 SCAR=6.9
Pod documented
187 379 my $self = shift;
188 379 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 18 361 379
189 18 $self->{'do_exon'} = shift;
190 }
191 379 return $self->{'do_exon'};
192 }
193  
194 389 ▶sub do_cds {
Subroutine do_cds: called CC=2 SCAR=6.9
Pod documented
195 389 my $self = shift;
196 389 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 383 389
197 6 $self->{'do_cds'} = shift;
198 }
199 389 return $self->{'do_cds'};
200 }
201  
202 111 ▶sub do_utr {
Subroutine do_utr: called CC=2 SCAR=6.9
Pod documented
203 111 my $self = shift;
204 111 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 110 111
205 1 $self->{'do_utr'} = shift;
206 }
207 111 return $self->{'do_utr'};
208 }
209  
210 150 ▶sub do_codon {
Subroutine do_codon: called CC=2 SCAR=7.6
Pod documented
211 150 my $self = shift;
212 150 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 150 150
213 0 $self->{'do_codon'} = shift;
214 }
215 150 return $self->{'do_codon'};
216 }
217  
218 460 ▶sub do_name {
Subroutine do_name: called CC=3 SCAR=11.3
Pod documented
219 460 my $self = shift;
220 460 ▶ return 0 unless ( ref($self) eq 'Bio::ToolBox::Parser::ucsc' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq "Bio::ToolBox::Parser::ucsc" 229 231 460
221  
222 # does nothing with gff and bed
223 231 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 231 231
224 0 $self->{'do_name'} = shift;
225 }
226 231 return $self->{'do_name'};
227 }
228  
229 623 ▶sub share {
Subroutine share: called CC=3 SCAR=11.3
Pod documented
230 623 my $self = shift;
231 623 ▶ return 0 unless ( ref($self) eq 'Bio::ToolBox::Parser::ucsc' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq "Bio::ToolBox::Parser::ucsc" 229 394 623
232  
233 # does nothing with gff and bed
234 394 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 394 394
235 0 $self->{'share'} = shift;
236 }
237 394 return $self->{'share'};
238 }
239  
240 666 ▶sub simplify {
Subroutine simplify: called CC=3 SCAR=11.0
Pod documented
241 666 my $self = shift;
242 666 ▶ return 0 unless ( ref($self) eq 'Bio::ToolBox::Parser::gff' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq "Bio::ToolBox::Parser::gff" 10 656 666
243  
244 # does nothing with ucsc and bed
245 656 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 7 649 656
246 7 $self->{simplify} = shift;
247 }
248 656 return $self->{simplify};
249 }
250  
251 209 ▶sub source {
Subroutine source: called CC=2 SCAR=6.9
Pod documented
252 209 my $self = shift;
253 209 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 204 209
254 5 $self->{'source'} = shift;
255 }
256 209 return $self->{'source'};
257 }
258  
259 85 ▶sub filetype {
Subroutine filetype: called CC=1 SCAR=0.0
Pod documented
260 85 return shift->{filetype};
261 }
262  
263 0 ▶sub number_loaded {
Subroutine number_loaded: not called CC=1 SCAR=6.9
Pod documented
264 0 my $self = shift;
265 0 return scalar keys %{ $self->{loaded} };
266 }
267  
268 36 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod documented
269 36 return shift->{file};
270 }
271  
272 1152 ▶sub fh {
Subroutine fh: called CC=1 SCAR=0.0
Pod documented
273 1152 return shift->{fh};
274 }
275  
276 0 ▶sub comments {
Subroutine comments: not called CC=3 SCAR=24.8
Pod documented
277 0 my $self = shift;
278 0 my @comments;
279 0 foreach ( @{ $self->{comments} } ) {
280 0 push @comments, $_;
281 }
282 0 ▶ return wantarray ? @comments : \@comments;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
283 }
284  
285 0 ▶sub seq_ids {
Subroutine seq_ids: not called CC=2 SCAR=17.9
Pod documented
286 0 my $self = shift;
287 0 my @s = keys %{ $self->{seq_ids} };
288 0 ▶ return wantarray ? @s : \@s;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
289 }
290  
291 0 ▶sub seq_id_lengths {
Subroutine seq_id_lengths: not called CC=1 SCAR=6.9
Pod documented
292 0 my $self = shift;
293 0 return $self->{seq_ids};
294 }
295  
296 131 ▶sub next_top_feature {
Subroutine next_top_feature: called CC=6 SCAR=18.6
Pod documented
297 131 my $self = shift;
298  
299 # return next item
300 131 ▶ if ( exists $self->{top_feature_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"top_feature_index"}) 120 11 131
301 120 my $i = $self->{top_feature_index};
302 120 ▶ if ( $i == $self->{last_top_feature_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $self->{"last_top_feature_index"}) 8 112 120
303 8 delete $self->{top_feature_index};
304 8 delete $self->{last_top_feature_index};
305 8 return undef;
306 }
307 112 $self->{top_feature_index} += 1;
308 112 return $self->{top_features}->[$i];
309 }
310  
311 # otherwise, it's our first time
312  
313 # check that we have an open filehandle
314 11 ▶ unless ( $self->fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fh) 0 11 11
315 0 croak 'FATAL: no annotation file loaded to parse!';
316 }
317 11 ▶ unless ( $self->{'eof'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 11 11
318 0 ▶ $self->parse_file or croak 'FATAL: unable to parse file!';
Branch: 1 branch
BranchTrueFalseTotal
$self->parse_file or croak("FATAL: unable to parse file!") 0 0 0
319 }
320  
321 # set up index
322 11 $self->{top_feature_index} = 1;
323 11 $self->{last_top_feature_index} = scalar( @{ $self->{top_features} } );
324 11 return $self->{top_features}->[0];
325 }
326  
327 12 ▶sub top_features {
Subroutine top_features: called CC=3 SCAR=11.0
Pod documented
328 12 my $self = shift;
329 12 ▶ unless ( $self->{'eof'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 6 6 12
330 6 $self->parse_file;
331 }
332 12 my @features = @{ $self->{top_features} };
333 12 ▶ return wantarray ? @features : \@features;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 0 12
334 }
335  
336 77 ▶sub fetch {
Subroutine fetch: called CC=4 SCAR=15.6
Pod documented
337 77 my ( $self, $id ) = @_;
338 77 ▶ return unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 77 77
339 77 ▶ unless ( $self->{'eof'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"eof"}) 0 77 77
340 0 $self->parse_file;
341 }
342 77 ▶ return $self->{loaded}{$id} || undef;
Condition50%1/2
$self->{'loaded'}{$id} || undef
l!l
770
MC/DC0%0/1
$self->{'loaded'}{$id} || undef
$self->{'loaded'}{$id}
Truth table50%1/2
A
A= $self->{'loaded'}{$id}
Aresult
11
00
343 }
344  
345 2 1;
346  
347 __END__
« lib/Bio/ToolBox/SeqFeature.pm ↑ Summary script/split_data_file.pl »