Egaz/Command/normalize.pm

filter: Statementstmt 94.3% 117 / 124 Branchbran 70.5% 24 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 90.4% 160 / 177 CC 24 SCAR 23.7
CC
24
Cov
89%
execute
18.9
validate_args
5.0
__ANON__
3.0
CRAP 10.7
« Egaz/Command/template.pm ↑ Summary Egaz/Common.pm »
1 package App::Egaz::Command::normalize;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use App::Egaz -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use App::Egaz::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
10 2 return 'normalize lav files';
11 }
12  
13 6 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
14 return (
15 6 [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ],
16 [ 'tlen=i', 'target length', { default => 0 }, ],
17 [ 'qlen=i', 'query length', { default => 0 }, ],
18 { show_defaults => 1, }
19 );
20 }
21  
22 6 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
23 6 return "egaz normalize [options] <infile>";
24 }
25  
26 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
27 1 my $desc;
28 1 $desc .= ucfirst(abstract) . ".\n";
29 1 $desc .= <<MARKDOWN;
30  
31 * infile == stdin means reading from STDIN
32 * Start coordinates of output is 1-based
33 * Set --tlen and/or --qlen on partitioned sequences
34 * Ported from kentUtils src/hg/utils/automation/blastz-normalizeLav
35  
36 MARKDOWN
37  
38 1 return $desc;
39 }
40  
41 5 ▶sub validate_args {
Subroutine validate_args: called CC=5 SCAR=16.1
Pod documented
42 5 my ( $self, $opt, $args ) = @_;
43  
44 5 ▶ if ( @{$args} != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 1) 1 4 5
45 1 my $message = "This command need one input file.\n\tIt found";
46 1 $message .= sprintf " [%s]", $_ for @{$args};
47 1 $message .= ".\n";
48 1 $self->usage_error($message);
49 }
50 4 for ( @{$args} ) {
51 4 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 1 3 4
52 1 $self->usage_error("The input file [$_] doesn't exist.");
53 }
54 }
55  
56 }
57  
58 3 ▶sub execute {
Subroutine execute: called CC=18 SCAR=29.4
Pod documented
59 3 my ( $self, $opt, $args ) = @_;
60  
61 #----------------------------#
62 # write outputs
63 #----------------------------#
64 3 my $out_fh;
65 3 ▶ if ( lc( $opt->{outfile} ) eq "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{'outfile'} eq 'stdout') { } 3 0 3
66 3 $out_fh = *STDOUT{IO};
67 }
68 else {
69 0 open $out_fh, ">", $opt->{outfile};
70 }
71  
72 #----------------------------#
73 # load lav
74 #----------------------------#
75 3 my $lav_content = Path::Tiny::path( $args->[0] )->slurp;
76 3 my @lavs = grep {/^[ds] /} split /\#\:lav\n/, $lav_content;
77 3 my $d_stanza = shift @lavs;
78 3 $d_stanza = "d {\n normalize-lav $opt->{tlen} $opt->{qlen}\n}\n" . $d_stanza;
79  
80 3 print {$out_fh} "#:lav\n";
81 3 print {$out_fh} $d_stanza;
82  
83 3 for my $lav (@lavs) {
84 8 print {$out_fh} "#:lav\n";
85  
86 8 my $t_from = 0;
87 8 my $q_from = 0;
88 8 my $t_to = 0;
89 8 my $q_to = 0;
90 8 my $isrc = 0;
91  
92 #----------------------------#
93 # s-stanza
94 #----------------------------#
95 # "<filename>[-]" <start> <stop> [<rev_comp_flag> <sequence_number>]
96 8 $lav =~ /s \{\s+(.+?)\s+\}/s;
97 8 my $s_stanza = $1;
98 8 my @s_lines = $s_stanza =~ /(.+ \s+ \d+ \s+ \d+ \s+ \d+ \s+ \d+)/gx;
99 8 ▶ if ( scalar @s_lines != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @s_lines != 2) 0 8 8
100 0 Carp::croak "s-stanza error.\n";
101 }
102  
103 8 print {$out_fh} "s {\n";
104  
105 8 ▶ $s_lines[0] =~ /^\s*("[^"]*")\s+(\d+)\s+(\d+)\s+(.*)$/ or die;
Branch: 1 branch
BranchTrueFalseTotal
$s_lines[0] =~ /^\s*("[^"]*")\s+(\d+)\s+(\d+)\s+(.*)$/ or die 8 0 8
106 8 $t_from = $2;
107 8 $t_to = $3;
108 8 print {$out_fh} " $1 1 " . List::Util::max( $t_to, $opt->{tlen} ) . " $4\n";
109  
110 8 ▶ $s_lines[1] =~ /^\s*("[^"]*")\s+(\d+)\s+(\d+)\s+(.*)$/ or die;
Branch: 1 branch
BranchTrueFalseTotal
$s_lines[1] =~ /^\s*("[^"]*")\s+(\d+)\s+(\d+)\s+(.*)$/ or die 8 0 8
111 8 $q_from = $2;
112 8 $q_to = $3;
113 8 print {$out_fh} " $1 1 " . List::Util::max( $q_to, $opt->{qlen} ) . " $4\n";
114  
115 8 $isrc = scalar( $1 =~ /-"$/ );
116  
117 8 print {$out_fh} "}\n";
118  
119 #----------------------------#
120 # h-stanza
121 #----------------------------#
122 8 ▶ if ( $lav =~ /h \{\n(.+?)\}/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lav =~ /h \{\n(.+?)\}/s) 8 0 8
123 8 my $h_stanza = $1;
124 8 print {$out_fh} "h {\n$h_stanza}\n";
125 }
126  
127 #----------------------------#
128 # a-stanza
129 #----------------------------#
130 # abs: 1....from....to....qlen
131 # rel: .....1.......n.........
132 #
133 # n == (to-from+1)
134 # rev(x) == n-x+1
135 # abs(x) == from+x-1
136 #
137 # abs(rev(x)) == from+(to-from+1-x+1)-1 == to-x+1
138 # Rev(abs(rev(x))) == qlen-(to-x+1)+1 == qlen-to+x
139  
140 800 ▶ my $St = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 800 my $x = shift;
142 800 return $x + $t_from - 1;
143 8 };
144 800 ▶ my $Mq = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
145 800 my $x = shift;
146 800 ▶ if ( $opt->{qlen} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'qlen'} == 0) { } 674 126 800
147 674 return $x;
148 }
149 else {
150 126 ▶ return $isrc ? $opt->{qlen} - $q_to + $x : $q_from + $x - 1;
Branch: 1 branch
BranchTrueFalseTotal
$isrc ? : 120 6 126
151 }
152 8 };
153  
154 8 my @a_stanzas = $lav =~ /a \{\s+(.+?)\s+\}/sg;
155 8 for my $a_stanza (@a_stanzas) {
156 14 print {$out_fh} "a {\n";
157 14 for my $line ( split /\n/, $a_stanza ) {
158 428 ▶ if ( $line =~ /^\s*s\s+(\d+)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^\s*s\s+(\d+)/) { } 14 414 428
elsif ($line =~ /^\s*b\s+(\d+)\s+(\d+)/) { } 14 400 414
elsif ($line =~ /^\s*e\s+(\d+)\s+(\d+)/) { } 14 386 400
elsif ($line =~ /^\s*l\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) { } 386 0 386
159 14 printf {$out_fh} " s %d\n", $1;
160 }
161 elsif ( $line =~ /^\s*b\s+(\d+)\s+(\d+)/ ) {
162 14 printf {$out_fh} " b %d %d\n", $St->($1), $Mq->($2);
163 }
164 elsif ( $line =~ /^\s*e\s+(\d+)\s+(\d+)/ ) {
165 14 printf {$out_fh} " e %d %d\n", $St->($1), $Mq->($2);
166 }
167 elsif ( $line =~ /^\s*l\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ ) {
168 386 printf {$out_fh} " l %d %d %d %d %g\n", $St->($1), $Mq->($2),
169 $St->($3), $Mq->($4), $5;
170 }
171 }
172 14 print {$out_fh} "}\n";
173 }
174  
175 #----------------------------#
176 # x-stanza
177 #----------------------------#
178 8 ▶ if ( $lav =~ /x \{\n(.+?)\}/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lav =~ /x \{\n(.+?)\}/s) 0 8 8
179 0 my $x_stanza = $1;
180 0 print {$out_fh} "x {\n$x_stanza}\n";
181 }
182  
183 #----------------------------#
184 # m-stanza
185 #----------------------------#
186 8 ▶ if ( $lav =~ /m \{\n(.+?)\}/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lav =~ /m \{\n(.+?)\}/s) 3 5 8
187 3 my $m_stanza = $1;
188 3 print {$out_fh} "m {\n";
189 3 for my $line ( split /\n/, $m_stanza ) {
190 3 ▶ if ( $line =~ /^\s*n\s+(\d+)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*n\s+(\d+)/) { } 3 0 3
elsif ($line =~ /^\s*x\s+(\d+)\s+(\d+)/) { } 0 0 0
191 3 printf {$out_fh} " n %d\n", $1;
192 }
193 elsif ( $line =~ /^\s*x\s+(\d+)\s+(\d+)/ ) {
194 0 printf {$out_fh} " x %d %d\n", $1 + $t_from - 1, $2 + $t_from - 1;
195 }
196 }
197 3 print {$out_fh} "}\n";
198 }
199 }
200  
201 3 print {$out_fh} "#:eof\n";
202 3 close $out_fh;
203 }
204  
205 19 1;
« Egaz/Command/template.pm ↑ Summary Egaz/Common.pm »