blib/lib/BioX/Tax/Kraken.pm

filter: Statementstmt 100% 136 / 136 Branchbran 94.2% 49 / 52 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 24 / 24 Podpod 100% 8 / 8 totaltotal 97.7% 221 / 226 CC 29 SCAR 13.4
CC
29
Cov
98%
is_ancestor
9.0
lineage
5.0
lca
5.0
CRAP 3.8
↑ Summary
1 package BioX::Tax::Kraken 0.001003;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use IPC::Cmd qw/can_run/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use List::Util qw/any first uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use constant N_VALID_COLUMNS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use constant PARENT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use constant NAME => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use constant RANK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17  
18 4 my ($class, $fn_in) = @_;
19  
20 4 my $self = bless {fn => $fn_in}, $class;
21  
22 4 $self->_set_handle();
23 4 $self->_load();
24  
25 3 return $self;
26  
27 }
28  
29 283 ▶sub parent {
Subroutine parent: called CC=3 SCAR=11.0
Pod documented
30  
31 283 my ($self, $tid) = @_;
32  
33 283 ▶ return if (! defined $self->{tax}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$tid} 1 282 283
34 return $self->{_c_parent}->{$tid}
35 282 ▶ if (defined $self->{_c_parent}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_c_parent"}{$tid} 198 84 282
36 84 my $p = (split "\0", $self->{tax}->{$tid})[PARENT];
37 84 $self->{_c_parent}->{$tid} = $p;
38  
39 84 return $p;
40  
41 }
42  
43 5 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
44  
45 5 my ($self, $tid) = @_;
46  
47 5 ▶ return if (! defined $self->{tax}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$tid} 1 4 5
48 return $self->{_c_name}->{$tid}
49 4 ▶ if (defined $self->{_c_name}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_c_name"}{$tid} 0 4 4
50 4 my $n = (split "\0", $self->{tax}->{$tid})[NAME];
51 4 $self->{_c_name}->{$tid} = $n;
52  
53 4 return $n;
54  
55 }
56  
57 4 ▶sub rank {
Subroutine rank: called CC=3 SCAR=11.0
Pod documented
58  
59 4 my ($self, $tid) = @_;
60  
61 4 ▶ return if (! defined $self->{tax}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$tid} 1 3 4
62 return $self->{_c_rank}->{$tid}
63 3 ▶ if (defined $self->{_c_rank}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_c_rank"}{$tid} 0 3 3
64 3 my $r = (split "\0", $self->{tax}->{$tid})[RANK];
65 3 $self->{_c_rank}->{$tid} = $r;
66  
67 3 return $r;
68  
69 }
70  
71 2 ▶sub children {
Subroutine children: called CC=2 SCAR=6.9
Pod documented
72  
73 2 my ($self, $tid) = @_;
74  
75 2 ▶ return if (! defined $self->{tax}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$tid} 1 1 2
76 return grep {
77 81 $self->is_ancestor($_, $tid)
78 1 } keys %{ $self->{tax} };
79  
80 }
81  
82 229 ▶sub is_ancestor {
Subroutine is_ancestor: called CC=9 SCAR=22.0
Pod documented
83  
84 229 my ($self, $child, $parent, $visited) = @_;
85  
86 229 ▶ $visited //= {}; # on first iteration
Condition100%2/2
$visited //= {}
l!l
13891
MC/DC100%1/1
$visited //= {}
$visited
Truth table100%2/2
A
A= $visited
Aresult
11
00
87  
88 229 ▶ return if (! defined $self->{tax}->{$child});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$child} 1 228 229
89 228 ▶ return if (! defined $self->{tax}->{$parent});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$parent} 1 227 228
90 227 ▶ return 0 if ($child eq $parent); # save some time, maybe
Branch: 1 branch
BranchTrueFalseTotal
if $child eq $parent 2 225 227
91  
92 225 my $tag = "$child\b$parent";
93 return $self->{_c_is_ancestor}->{$tag}
94 225 ▶ if defined $self->{_c_is_ancestor}->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_c_is_ancestor"}{$tag} 81 144 225
95  
96 # Detect cycle: if we've visited this child in this query, stop
97 144 ▶ if ($visited->{$child}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($visited->{$child}++) 1 143 144
98 1 die "Cycle detected at $child! Bailing defensively...";
99 }
100
101 143 my $p = $self->parent($child);
102 143 my $result;
103 143 ▶ if ($p eq $parent) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p eq $parent) { } 3 140 143
elsif ($p eq $child) { } 2 138 140
104 3 $result = 1;
105 }
106 # the root node is a parent of itself;
107 # prevent endless recursion here
108 elsif ($p eq $child) {
109 2 $result = 0;
110 }
111 else {
112 138 $result = $self->is_ancestor( $p, $parent, $visited );
113 }
114 140 $self->{_c_is_ancestor}->{$tag} = $result;
115 140 delete $visited->{$child};
116  
117 140 return $result;
118  
119 }
120  
121 # alias method
122 3 *a_isa_b = \&is_ancestor;
123  
124 15 ▶sub lineage {
Subroutine lineage: called CC=5 SCAR=16.1
Pod documented
125  
126 15 my ($self, $tid) = @_;
127 15 my $orig = $tid;
128 15 ▶ return if (! defined $self->{tax}->{$tid});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tax"}{$tid} 1 14 15
129 return split( "\0", $self->{_c_lineage}->{$tid} )
130 14 ▶ if defined $self->{_c_lineage}->{$tid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_c_lineage"}{$tid} 6 8 14
131  
132 8 my @lineage = ($tid);
133 8 my $parent = $self->parent($tid);
134 8 my $visited = { $tid => 1 };
135  
136 8 ▶ while ($parent ne $tid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent ne $tid) 130 7 137
137 130 ▶ if ($visited->{$parent}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($visited->{$parent}++) 1 129 130
138 1 die "Cycle detected in lineage of $orig at node $parent";
139 }
140 129 push @lineage, $parent;
141 129 $tid = $parent;
142 129 $parent = $self->parent($tid);
143 }
144 7 @lineage = reverse @lineage;
145 7 $self->{_c_lineage}->{$orig} = join "\0", @lineage;
146 7 return @lineage;
147  
148 }
149  
150 6 ▶sub lca {
Subroutine lca: called CC=5 SCAR=16.1
Pod documented
151  
152 6 my ($self, @ids) = @_;
153  
154 6 ▶ return if (! scalar @ids);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @ids 1 5 6
155 5 ▶ return if any {! defined $self->{tax}->{$_} } @ids;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not defined $self->{"tax"}{$_}; } , @ids) 1 4 5
156  
157 4 my @l = map { [ $self->lineage($_) ] } @ids;
158 4 my $lca;
159 4 my $i = 0;
160 4 while (1) {
161 42 my @lvl = uniq map { $_->[$i] } @l;
162 42 ▶ last if (any {! defined $_} @lvl);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not defined $_; } , @lvl) 1 41 42
163 41 ▶ last if (scalar @lvl > 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @lvl > 1 3 38 41
164 38 $lca = $lvl[0];
165 38 ++$i;
166 }
167
168 4 return $lca;
169  
170 }
171  
172  
173 4 ▶sub _set_handle {
Subroutine _set_handle: called CC=3 SCAR=11.0
174  
175 4 my ($self) = @_;
176  
177 4 my $fh_in;
178 4 ▶ if ( _is_xz($self->{fn}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_xz($self->{'fn'})) { } 1 3 4
179 1 ▶ my $XZCAT = can_run('xzcat')
Condition50%1/2
can_run("xzcat") // die("Require 'xzcat' for XZ-compressed inputs")
l!l
10
MC/DC0%0/1
can_run("xzcat") // die("Require 'xzcat' for XZ-compressed inputs")
can_run("xzcat")
Truth table50%1/2
A
A= can_run("xzcat")
Aresult
11
00
180 // die "Require 'xzcat' for XZ-compressed inputs";
181 1 open $fh_in, '-|', $XZCAT, $self->{fn};
182 }
183 else {
184 3 open $fh_in, '<', $self->{fn};
185 }
186 4 $self->{fh} = $fh_in;
187  
188 }
189  
190  
191 4 ▶sub _is_xz {
Subroutine _is_xz: called CC=2 SCAR=6.9
192  
193 4 my ($fn) = @_;
194  
195 3 ▶ use constant XZ_MAGIC => "\xFD7zXZ\x00";
Subroutine BEGIN: called CC=1 SCAR=0.0
196 4 open my $in, '<:raw', $fn;
197 4 my $n = read $in, my $buf, 6;
198 4 close $in;
199  
200 4 ▶ return 0 if ($n != 6);
Branch: 1 branch
BranchTrueFalseTotal
if $n != 6 0 4 4
201 4 return $buf eq XZ_MAGIC
202  
203 }
204  
205 4 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.0
206  
207 4 my ($self) = @_;
208  
209 4 my $fh = $self->{fh};
210 4 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 265 3 268
211 265 chomp $line;
212 265 my @f = split "\t", $line;
213 265 ▶ if (scalar @f != N_VALID_COLUMNS) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @f != 4) 1 264 265
214 1 die sprintf(
215 "Invalid taxDB format: expected %s columns, found %s",
216 N_VALID_COLUMNS,
217 scalar @f,
218 );
219 }
220 # uses < 50% memory to store data as null-padded strings instead of
221 # Perl nested arrays or hashes. Can be significantly slower to extract
222 # data on millions of calls, though.
223 264 $self->{tax}->{$f[0]} = join "\0", @f[1..3];
224 };
225 3 close $fh;
226 3 $self->{fh} = undef;
227  
228 }
229  
230 3 1;
231  
232  
233 __END__
↑ Summary