lib/Suffix/Trie.pm

filter: Statementstmt 92.5% 74 / 80 Branchbran 70.0% 7 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 3 totaltotal 85.7% 96 / 112 CC 10 SCAR 8.5
CC
10
Cov
89%
_suffix_trie
5.0
_nestedKeys
3.0
suffixes
2.0
CRAP 2.3
↑ Summary
1 #!/usr/bin/env perl
2  
3 # Native perl Suffix Trie
4 # Code borrowed and modified from:
5 # https://rosettacode.org/wiki/Suffix_tree#Perl
6 # Author: Lee Katz <lkatz@cdc.gov>
7  
8 package Suffix::Trie;
9 1 require 5.12.0;
10 1 our $VERSION=0.1;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use File::Basename qw/basename fileparse dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Temp qw/tempdir tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our @EXPORT_OK = qw(
22 @fastqExt @fastaExt
23 );
24  
25 1 our @fastqExt=qw(.fastq.gz .fastq .fq .fq.gz);
26 1 our @fastaExt=qw(.fasta .fna .faa .mfa .fas .fa);
27  
28 # TODO if 'die' is imported by a script, redefine
29 # sig die in that script as this function.
30 0 ▶local $SIG{'__DIE__'} = sub { my $e = $_[0]; $e =~ s/(at [^\s]+? line \d+\.$)/\nStopped $1/; die("$0: ".(caller(1))[3].": ".$e); };
Subroutine __ANON__: not called CC=1 SCAR=3.5
31  
32 =pod
33  
34 =head1 NAME
35  
36 Suffix::Trie
37  
38 =head1 SYNOPSIS
39  
40 A module for pure Perl Suffix Trie. Core code taken from https://rosettacode.org/wiki/Suffix_tree#Perl
41  
42 use strict;
43 use warnings;
44 use Data::Dumper;
45 use Suffix::Trie;
46  
47 my $trie = Suffix::Trie->new("mississippi");
48 # Get all substrings into an array reference
49 print Dumper $trie->suffixes();
50 # Get the actual trie in a hash reference
51 print Dumper $trie->trie;
52  
53 =cut
54  
55 3 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
56 3 my($class,$str,$settings)=@_;
57  
58 # Initialize the object and then bless it
59 3 my $self={
60 str => $str,
61 trie => undef,
62 suffixes => undef,
63 };
64  
65 3 bless($self);
66  
67 3 $self->_create_trie();
68  
69 3 return $self;
70 }
71  
72 # Some getters
73 0 ▶sub trie{
Subroutine trie: not called CC=1 SCAR=6.9
Pod undocumented
74 0 my($self)=@_;
75 0 return $self->{trie};
76 }
77 3 ▶sub suffixes{
Subroutine suffixes: called CC=2 SCAR=7.1
Pod undocumented
78 3 my($self)=@_;
79 3 ▶ if(defined $self->{suffixes}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"suffixes"}) 0 3 3
80 0 return $self->{suffixes};
81 }
82  
83 # recurse into the trie and get all keys
84 3 my @keys;
85 3 _nestedKeys($self->{trie},\@keys);
86 3 @keys = sort {$a cmp $b} uniq(@keys);
87 3 $self->{suffixes} = \@keys;
88 3 return $self->{suffixes};
89 }
90 272 ▶sub _nestedKeys{
Subroutine _nestedKeys: called CC=3 SCAR=11.1
91 272 my($hashRef, $keys)=@_;
92 272 ▶ $keys //= [];
Condition50%1/2
$keys //= []
l!l
2720
MC/DC0%0/1
$keys //= []
$keys
Truth table50%1/2
A
A= $keys
Aresult
11
00
93 272 for my $key(keys(%$hashRef)){
94 269 push(@$keys, $key);
95 269 _nestedKeys($$hashRef{$key}, $keys);
96 }
97 }
98  
99 3 ▶sub _create_trie{
Subroutine _create_trie: called CC=2 SCAR=7.0
100 3 my($self)=@_;
101 3 my $str = $self->{str};
102  
103 # ensure that the string ends in a $
104 3 $str=~s/\$*$/\$/;
105
106 # Test for extraneous $
107 3 my $testStr=substr($str, 0, -1); # leaves out the last character
108 3 ▶ die "ERROR: found a dollar sign in the string" if($testStr=~/\$/);
Branch: 1 branch
BranchTrueFalseTotal
if $testStr =~ /\$/ 0 3 3
109 3 $self->{trie} = _suffix_trie(_suffixHash($str));
110 }
111  
112 # https://rosettacode.org/wiki/Suffix_tree#Perl
113 118 ▶sub _classify{
Subroutine _classify: called CC=2 SCAR=6.9
114 118 my $h = {};
115 118 for (@_) { push @{$h->{substr($_,0,1)}}, $_ }
116 118 return $h;
117 }
118 # https://rosettacode.org/wiki/Suffix_tree#Perl
119 # TODO expose this function
120 # TODO return list of strings or hash of strings
121 3 ▶sub _suffixHash{
Subroutine _suffixHash: called CC=1 SCAR=0.0
122 3 my $str = shift;
123 3 map { substr $str, $_ } 0 .. length($str) - 1;
124 }
125 # https://rosettacode.org/wiki/Suffix_tree#Perl
126 288 ▶sub _suffix_trie {
Subroutine _suffix_trie: called CC=5 SCAR=16.1
127 288 ▶ return +{} if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 288 288
128 288 ▶ return +{ $_[0] => +{} } if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 170 118 288
129 118 my $h = {};
130 118 my $classif = _classify @_;
131 118 for my $key (keys %$classif) {
132 my $subtree = _suffix_trie(
133 285 map { substr $_, 1 } @{$classif->{$key}}
134 );
135 285 my @subkeys = keys %$subtree;
136 285 ▶ if (@subkeys == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subkeys == 1) { } 186 99 285
137 186 my ($subkey) = @subkeys;
138 186 $h->{"$key$subkey"} = $subtree->{$subkey};
139 99 } else { $h->{$key} = $subtree }
140 }
141 118 return $h;
142 }
143  
144  
145 1 1;
↑ Summary