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