File Coverage

blib/lib/App/RL/Command/stat2.pm
Criterion Covered Total %
statement 119 126 94.4
branch 28 40 70.0
condition n/a
subroutine 12 12 100.0
pod 6 7 85.7
total 165 185 89.1


line stmt bran cond sub pod time code
1             package App::RL::Command::stat2;
2 15     15   16571 use strict;
  15         48  
  15         634  
3 15     15   105 use warnings;
  15         40  
  15         1056  
4 15     15   109 use autodie;
  15         40  
  15         136  
5              
6 15     15   107546 use App::RL -command;
  15         50  
  15         302  
7 15     15   8444 use App::RL::Common;
  15         47  
  15         33500  
8              
9             sub abstract {
10 2     2 1 89 return 'coverage statistics on another runlist for runlists';
11             }
12              
13             sub opt_spec {
14             return (
15 8     8 1 141 [ "outfile|o=s", "output filename. [stdout] for screen" ],
16             [ "op=s", "operations: intersect, union, diff or xor", { default => "intersect" } ],
17             [ "size|s=s", "chr.sizes", { required => 1 } ],
18             [ "base|b=s", "basename of infile2", ],
19             [ "remove|r", "remove 'chr0' from chromosome names" ],
20             [ "mk", "first YAML file contains multiple sets of runlists" ],
21             [ "all", "only write whole genome stats" ],
22             { show_defaults => 1, }
23             );
24             }
25              
26             sub usage_desc {
27 8     8 1 95993 return "runlist stat2 [options] ";
28             }
29              
30             sub description {
31 1     1 1 2451 my $desc;
32 1         5 $desc .= ucfirst(abstract) . ".\n";
33 1         4 return $desc;
34             }
35              
36             sub validate_args {
37 6     6 1 17613 my ( $self, $opt, $args ) = @_;
38              
39 6 100       20 if ( @{$args} != 2 ) {
  6         36  
40 2         6 my $message = "This command need two input files.\n\tIt found";
41 2         5 $message .= sprintf " [%s]", $_ for @{$args};
  2         11  
42 2         8 $message .= ".\n";
43 2         21 $self->usage_error($message);
44             }
45 4         16 for ( @{$args} ) {
  4         19  
46 7 50       384 next if lc $_ eq "stdin";
47 7 100       57 if ( !Path::Tiny::path($_)->is_file ) {
48 1         204 $self->usage_error("The input file [$_] doesn't exist.");
49             }
50             }
51              
52 3 50       229 if ( $opt->{op} =~ /^dif/i ) {
    50          
    50          
    0          
53 0         0 $opt->{op} = 'diff';
54             }
55             elsif ( $opt->{op} =~ /^uni/i ) {
56 0         0 $opt->{op} = 'union';
57             }
58             elsif ( $opt->{op} =~ /^int/i ) {
59 3         14 $opt->{op} = 'intersect';
60             }
61             elsif ( $opt->{op} =~ /^xor/i ) {
62 0         0 $opt->{op} = 'xor';
63             }
64             else {
65 0         0 Carp::confess "[@{[$opt->{op}]}] invalid\n";
  0         0  
66             }
67              
68 3 50       15 if ( !exists $opt->{base} ) {
69 3         15 $opt->{base} = Path::Tiny::path( $args->[1] )->basename( ".yaml", ".yml" );
70             }
71              
72 3 50       482 if ( !exists $opt->{outfile} ) {
73 0         0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . $opt->{op} . ".csv";
74             }
75             }
76              
77             sub execute {
78 3     3 1 33 my ( $self, $opt, $args ) = @_;
79              
80             #----------------------------#
81             # Loading
82             #----------------------------#
83 3         36 my $length_of = App::RL::Common::read_sizes( $opt->{size}, $opt->{remove} );
84              
85             # file1
86 3         18 my $s1_of = {};
87 3         10 my @keys;
88 3 100       18 if ( $opt->{mk} ) {
89 1         22 my $yml = YAML::Syck::LoadFile( $args->[0] );
90 1         281 @keys = sort keys %{$yml};
  1         11  
91              
92 1         4 for my $key (@keys) {
93 5         27 $s1_of->{$key} = App::RL::Common::runlist2set( $yml->{$key}, $opt->{remove} );
94             }
95             }
96             else {
97 2         11 @keys = ("__single");
98             $s1_of->{__single}
99 2         21 = App::RL::Common::runlist2set( YAML::Syck::LoadFile( $args->[0] ), $opt->{remove} );
100             }
101              
102             # file2
103 3         57 my $s2 = App::RL::Common::runlist2set( YAML::Syck::LoadFile( $args->[1] ), $opt->{remove} );
104              
105             #----------------------------#
106             # Operating
107             #----------------------------#
108 3         26 my $op_result_of = { map { $_ => {} } @keys };
  7         37  
109              
110 3         11 for my $key (@keys) {
111 7         19 my $s1 = $s1_of->{$key};
112              
113             # give empty set to non-existing chrs
114 7         17 for my $s ( $s1, $s2 ) {
115 14         73 for my $chr ( sort keys %{$length_of} ) {
  14         80  
116 114 100       390 if ( !exists $s->{$chr} ) {
117 23         52 $s->{$chr} = App::RL::Common::new_set();
118             }
119             }
120             }
121              
122             # operate on each chr
123 7         30 for my $chr ( sort keys %{$length_of} ) {
  7         26  
124 57         124 my $op = $opt->{op};
125 57         232 my $op_set = $s1->{$chr}->$op( $s2->{$chr} );
126 57         27276 $op_result_of->{$key}{$chr} = $op_set;
127             }
128             }
129              
130             # warn YAML::Syck::Dump $s2;
131             # warn YAML::Syck::Dump $op_result_of;
132              
133             #----------------------------#
134             # Calcing
135             #----------------------------#
136 3         17 my $out_fh;
137 3 50       29 if ( lc( $opt->{outfile} ) eq "stdout" ) {
138 3         36 $out_fh = *STDOUT;
139             }
140             else {
141 0         0 open $out_fh, ">", $opt->{outfile};
142             }
143              
144             my $header = sprintf "key,chr,chr_length,size,%s_length,%s_size,c1,c2,ratio\n",
145 3         41 $opt->{base}, $opt->{base};
146 3 100       16 if ( $opt->{mk} ) {
147 1 50       6 if ( $opt->{all} ) {
148 1         9 $header =~ s/chr\,//;
149             }
150 1         4 my @lines = ($header);
151              
152 1         4 for my $key (@keys) {
153             my @key_lines
154 5         16 = csv_lines( $s1_of->{$key}, $length_of, $s2, $op_result_of->{$key}, $opt->{all} );
155 5         19 $_ = "$key,$_" for @key_lines;
156 5         12 push @lines, @key_lines;
157             }
158              
159 1         5 print {$out_fh} $_ for @lines;
  6         123  
160             }
161             else {
162 2         21 $header =~ s/key\,//;
163 2 100       11 if ( $opt->{all} ) {
164 1         6 $header =~ s/chr\,//;
165             }
166 2         8 my @lines = ($header);
167              
168             push @lines,
169             csv_lines( $s1_of->{__single}, $length_of, $s2, $op_result_of->{__single},
170 2         18 $opt->{all} );
171              
172 2         16 print {$out_fh} $_ for @lines;
  20         531  
173             }
174              
175 3         68 close $out_fh;
176             }
177              
178             sub csv_lines {
179 7     7 0 15 my $set_of = shift;
180 7         11 my $length_of = shift;
181 7         11 my $s2 = shift;
182 7         12 my $op_result_of = shift;
183 7         13 my $all = shift;
184              
185 7         9 my @lines;
186 7         17 my ( $all_length, $all_size, $all_s2_length, $all_s2_size, );
187 7         13 for my $chr ( sort keys %{$length_of} ) {
  7         51  
188 57         127 my $length = $length_of->{$chr};
189 57         166 my $size = $set_of->{$chr}->size;
190              
191 57         13539 my $s2_length = $s2->{$chr}->size;
192 57         1739 my $s2_size = $op_result_of->{$chr}->size;
193              
194 57         1077 $all_length += $length;
195 57         98 $all_size += $size;
196 57         92 $all_s2_length += $s2_length;
197 57         91 $all_s2_size += $s2_size;
198              
199 57         118 my $c1 = $size / $length;
200 57 100       150 my $c2 = $s2_length == 0 ? 0 : $s2_size / $s2_length;
201 57 100       125 my $ratio = $c1 == 0 ? 0 : $c2 / $c1;
202              
203 57         622 my $line = sprintf "%s,%d,%d,%d,%d,%.4f,%.4f,%.4f\n", $chr, $length, $size, $s2_length,
204             $s2_size, $c1, $c2, $ratio;
205 57         193 push @lines, $line;
206             }
207              
208 7         30 my $all_c1 = $all_size / $all_length;
209 7 50       24 my $all_c2 = $all_s2_length == 0 ? 0 : $all_s2_size / $all_s2_length;
210 7 50       21 my $all_ratio = $all_c1 == 0 ? 0 : $all_c2 / $all_c1;
211              
212             # only keep whole genome
213 7         54 my $all_line = sprintf "all,%d,%d,%d,%d,%.4f,%.4f,%.4f\n", $all_length, $all_size,
214             $all_s2_length, $all_s2_size, $all_c1, $all_c2, $all_ratio;
215 7 100       23 if ($all) {
216 6         17 @lines = ();
217 6         30 $all_line =~ s/all,//;
218             }
219 7         22 push @lines, $all_line;
220              
221 7         33 return @lines;
222             }
223              
224             1;