blib/lib/App/Dazz/Common.pm

filter: Statementstmt 68.6% 136 / 198 Branchbran 63.7% 37 / 58 Conditioncond 42.8% 6 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 86.6% 26 / 30 Podpod 0.0% 0 / 12 totaltotal 65.0% 208 / 320 CC 48 SCAR 29.5
CC
48
Cov
66%
judge_distance
110.0
g2gv
20.0
g2gv0
12.0
CRAP 19.2
« blib/lib/App/Dazz/Command/overlap.pm ↑ Summary blib/lib/App/Dazz/Command/show2ovlp.pm »
1 package App::Dazz::Common;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use IPC::Cmd qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Path::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Statistics::Descriptive qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use YAML::Syck qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 ▶use AlignDB::IntSpan;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use AlignDB::Stopwatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use App::RL::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use App::Fasops::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶sub get_len_from_header {
Subroutine get_len_from_header: called CC=5 SCAR=16.3
Pod undocumented
24 2 my $fa_fn = shift;
25  
26 2 my %len_of;
27  
28 my $fa_fh;
29 2 ▶ if ( lc $fa_fn eq 'stdin' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $fa_fn eq 'stdin') { } 0 2 2
30 0 $fa_fh = *STDIN{IO};
31 }
32 else {
33 2 open $fa_fh, "<", $fa_fn;
34 }
35  
36 2 ▶ while ( my $line = <$fa_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fa_fh)) 48 2 50
37 48 ▶ if ( substr( $line, 0, 1 ) eq ">" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 1) eq ">") 24 24 48
38 24 ▶ if ( $line =~ /\/(\d+)\/\d+_(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[/(\d+)/\d+_(\d+)]) 24 0 24
39 24 $len_of{$1} = $2;
40 }
41 }
42 }
43  
44 2 close $fa_fh;
45  
46 2 return \%len_of;
47 }
48  
49 1 ▶sub get_replaces {
Subroutine get_replaces: called CC=4 SCAR=14.0
Pod undocumented
50 1 my $fn = shift;
51  
52 1 my $replace_of = {};
53 1 my @lines = Path::Tiny::path($fn)->lines( { chomp => 1 } );
54  
55 1 for my $line (@lines) {
56 12 my @fields = split /\t/, $line;
57 12 ▶ if ( @fields == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == 2) 12 0 12
58 12 ▶ if ( $fields[0] =~ /\/(\d+)\/\d+_\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[0] =~ m[/(\d+)/\d+_\d+]) 12 0 12
59 12 $replace_of->{$1} = $fields[1];
60 }
61 }
62 }
63  
64 1 return $replace_of;
65 }
66  
67 19 ▶sub exec_cmd {
Subroutine exec_cmd: called CC=4 SCAR=14.3
Pod undocumented
68 19 my $cmd = shift;
69 19 my $opt = shift;
70  
71 19 ▶ if ( defined $opt and ref $opt eq "HASH" and $opt->{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt and ref $opt eq "HASH" and $opt->{"verbose"}) 10 9 19
Condition50%3/6
defined $opt and ref $opt eq "HASH" and $opt->{"verbose"}
!ll&&!rl&&r
0910
defined $opt and ref $opt eq "HASH"
!ll&&!rl&&r
0019
MC/DC33%1/3
defined $opt and ref $opt eq "HASH" and $opt->{"verbose"}
defined $opt ref $opt eq "HASH" $opt->{"verbose"}
Truth table50%2/4
A and B and C
A= defined $optB= ref $opt eq "HASH"C= $opt->{"verbose"}
ABCresult
0XX0
10X0
1100
1111
72 10 print STDERR "CMD: ", $cmd, "\n";
73 }
74  
75 19 system $cmd;
76 }
77  
78 103 ▶sub parse_ovlp_line {
Subroutine parse_ovlp_line: called CC=1 SCAR=0.0
Pod undocumented
79 103 my $line = shift;
80  
81 103 chomp $line;
82 103 my @fields = split "\t", $line;
83  
84 103 my $info = {
85 f_id => $fields[0],
86 g_id => $fields[1],
87 ovlp_len => $fields[2],
88 ovlp_idt => $fields[3],
89 f_strand => $fields[4],
90 f_B => $fields[5],
91 f_E => $fields[6],
92 f_len => $fields[7],
93 g_strand => $fields[8],
94 g_B => $fields[9],
95 g_E => $fields[10],
96 g_len => $fields[11],
97 contained => $fields[12],
98 };
99  
100 103 return $info;
101 }
102  
103 51 ▶sub beg_end {
Subroutine beg_end: called CC=3 SCAR=11.2
Pod undocumented
104 51 my $beg = shift;
105 51 my $end = shift;
106  
107 51 ▶ if ( $beg > $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beg > $end) 0 51 51
108 0 ( $beg, $end ) = ( $end, $beg );
109 }
110  
111 51 ▶ if ( $beg == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beg == 0) 7 44 51
112 7 $beg = 1;
113 }
114  
115 51 return ( $beg, $end );
116 }
117  
118  
119 0 ▶sub serial2name {
Subroutine serial2name: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my $dazz_db = shift;
121  
122 0 my $serials = shift;
123  
124 0 my $cmd = sprintf "DBshow -n %s %s ", $dazz_db, join( " ", @{$serials} );
125 0 my @lines = map { $_ =~ s/^>//; $_; } grep {defined} split /\n/, `$cmd`;
126  
127 0 my %name_of;
128 0 for my $i ( 0 .. $#lines ) {
129 0 $name_of{ $serials->[$i] } = $lines[$i];
130 }
131  
132 0 return \%name_of;
133 }
134  
135 0 ▶sub judge_distance {
Subroutine judge_distance: not called CC=10 SCAR=47.0
Pod undocumented
136 0 my $d_ref = shift;
137 0 ▶ my $max_dis = shift || 5000;
Condition0%0/2
shift() || 5000
l!l
00
MC/DC0%0/1
shift() || 5000
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
138  
139 0 ▶ return 0 unless defined $d_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d_ref 0 0 0
140  
141 0 my $sum = 0;
142 0 my $min = $d_ref->[0];
143 0 my $max = $min;
144 0 for my $d ( @{$d_ref} ) {
145 0 $sum += $d;
146 0 ▶ if ( $d < $min ) { $min = $d; }
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $min) 0 0 0
147 0 ▶ if ( $d > $max ) { $max = $d; }
Branch: 1 branch
BranchTrueFalseTotal
if ($d > $max) 0 0 0
148 }
149 0 my $avg = $sum / scalar( @{$d_ref} );
150 0 ▶ return 0 if $avg > $max_dis;
Branch: 1 branch
BranchTrueFalseTotal
if $avg > $max_dis 0 0 0
151 0 ▶ return 0 if $avg == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $avg == 0 0 0 0
152  
153 # max k-mer is 127.
154 # For k-unitigs, overlaps are less than k-mer
155 0 my $v = $max - $min;
156 0 ▶ if ( $v < 20 or abs( $v / $avg ) < 0.2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < 20 or abs $v / $avg < 0.2) { } 0 0 0
Condition0%0/3
$v < 20 or abs $v / $avg < "0.2"
l!l&&r!l&&!r
000
MC/DC0%0/2
$v < 20 or abs $v / $avg < "0.2"
$v < 20 abs $v / $avg < "0.2"
Truth table0%0/3
A or B
A= $v < 20B= abs $v / $avg < "0.2"
ABresult
1X1
011
000
157 0 return 1;
158 }
159 else {
160 0 return 0;
161 }
162 }
163  
164 0 ▶sub g2gv {
Subroutine g2gv: not called CC=4 SCAR=30.0
Pod undocumented
165 0 require GraphViz;
166  
167 #@type Graph
168 0 my $g = shift;
169 0 my $fn = shift;
170  
171 0 my $gv = GraphViz->new( directed => 1 );
172  
173 0 for my $v ( $g->vertices ) {
174 0 $gv->add_node($v);
175 }
176  
177 0 for my $e ( $g->edges ) {
178 0 ▶ if ( $g->has_edge_weight( @{$e} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->has_edge_weight(@{$e;})) { } 0 0 0
179 0 $gv->add_edge( @{$e}, label => $g->get_edge_weight( @{$e} ) );
180 }
181 else {
182 0 $gv->add_edge( @{$e} );
183 }
184 }
185  
186 0 Path::Tiny::path($fn)->spew_raw( $gv->as_png );
187 }
188  
189 0 ▶sub g2gv0 {
Subroutine g2gv0: not called CC=3 SCAR=24.8
Pod undocumented
190 0 require GraphViz;
191  
192 #@type Graph
193 0 my $g = shift;
194 0 my $fn = shift;
195  
196 0 my $gv = GraphViz->new( directed => 0 );
197  
198 0 for my $v ( $g->vertices ) {
199 0 $gv->add_node($v);
200 }
201  
202 0 for my $e ( $g->edges ) {
203 0 $gv->add_edge( @{$e} );
204 }
205  
206 0 Path::Tiny::path($fn)->spew_raw( $gv->as_png );
207 }
208  
209 1 ▶sub transitive_reduction {
Subroutine transitive_reduction: called CC=9 SCAR=22.0
Pod undocumented
210  
211 #@type Graph
212 1 my $g = shift;
213  
214 1 my $count = 0;
215 1 my $prev_count;
216 1 while (1) {
217 3 ▶ last if defined $prev_count and $prev_count == $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_count and $prev_count == $count 1 2 3
Condition100%3/3
defined $prev_count and $prev_count == $count
!ll&&!rl&&r
111
MC/DC100%2/2
defined $prev_count and $prev_count == $count
defined $prev_count $prev_count == $count
Truth table100%3/3
A and B
A= defined $prev_countB= $prev_count == $count
ABresult
0X0
100
111
218 2 $prev_count = $count;
219  
220 2 for my $v ( $g->vertices ) {
221 8 ▶ next if $g->out_degree($v) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $g->out_degree($v) < 2 6 2 8
222  
223 2 my @s = sort { $a cmp $b } $g->successors($v);
224 2 for my $i ( 0 .. $#s ) {
225 5 for my $j ( 0 .. $#s ) {
226 13 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 5 8 13
227 8 ▶ if ( $g->is_reachable( $s[$i], $s[$j] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->is_reachable($s[$i], $s[$j])) 4 4 8
228 4 $g->delete_edge( $v, $s[$j] );
229  
230 4 $count++;
231 }
232 }
233 }
234 }
235 }
236  
237 1 return $count;
238 }
239  
240 # https://metacpan.org/source/GSULLIVAN/String-LCSS-1.00/lib/String/LCSS.pm
241 # `undef` is returned if the susbstring length is one char or less.
242 # In scalar context, returns the substring.
243 # In array context, returns the index of the match root in the two args.
244 8 ▶sub lcss {
Subroutine lcss: called CC=4 SCAR=13.9
Pod undocumented
245 8 my $solns0 = ( _lcss( $_[0], $_[1] ) )[0];
246 8 ▶ return unless $solns0;
Branch: 1 branch
BranchTrueFalseTotal
unless $solns0 0 8 8
247 8 my @match = @{$solns0};
248 8 ▶ return if length $match[0] == 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $match[0] == 1 2 6 8
249 6 ▶ return wantarray ? @match : $match[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
250 }
251  
252 8 ▶sub _lcss {
Subroutine _lcss: called CC=7 SCAR=19.5
253  
254 # Return array-of-arrays of longest substrings and indices
255 8 my ( $r1, $r2 ) = @_;
256 8 my ( $l1, $l2, ) = ( length $r1, length $r2, );
257 8 ▶ ( $r1, $r2, $l1, $l2, ) = ( $r2, $r1, $l2, $l1, ) if $l1 > $l2;
Branch: 1 branch
BranchTrueFalseTotal
if $l1 > $l2 3 5 8
258  
259 8 my ( $best, @solns ) = 0;
260 8 for my $start ( 0 .. $l2 - 1 ) {
261 67 for my $l ( reverse 1 .. $l1 - $start ) {
262 302 my $substr = substr( $r1, $start, $l );
263 302 my $o = index( $r2, $substr );
264 302 ▶ next if $o < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o < 0 119 183 302
265 183 ▶ if ( $l > $best ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l > $best) { } 9 174 183
elsif ($l == $best) { } 2 172 174
266 9 $best = length $substr;
267 9 @solns = [ $substr, $start, $o ];
268 }
269 elsif ( $l == $best ) {
270 2 push @solns, [ $substr, $start, $o ];
271 }
272 }
273 }
274 8 return @solns;
275 }
276  
277 3 ▶sub histogram_percentile {
Subroutine histogram_percentile: called CC=4 SCAR=14.0
Pod undocumented
278 3 my $hist_of = shift;
279 3 my $fraction = shift;
280  
281 3 my @keys = sort { $a <=> $b } keys %{$hist_of};
282 3 ▶ if ( scalar @keys == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys == 0) 0 3 3
283 0 return 0;
284 }
285  
286 3 my $target = List::Util::sum( values %{$hist_of} ) * $fraction;
287 3 my $cumulative = 0;
288  
289 3 for my $i (@keys) {
290 175 $cumulative += $hist_of->{$i};
291  
292 175 ▶ if ( $cumulative >= $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cumulative >= $target) 3 172 175
293 3 return $i;
294 }
295 }
296  
297 0 return $keys[-1];
298 }
299  
300 13 1;
« blib/lib/App/Dazz/Command/overlap.pm ↑ Summary blib/lib/App/Dazz/Command/show2ovlp.pm »