File Coverage

blib/lib/App/RL/Command/coverage.pm
Criterion Covered Total %
statement 88 90 97.7
branch 19 26 73.0
condition n/a
subroutine 11 11 100.0
pod 6 6 100.0
total 124 133 93.2


line stmt bran cond sub pod time code
1             package App::RL::Command::coverage;
2 15     15   16256 use strict;
  15         48  
  15         915  
3 15     15   95 use warnings;
  15         42  
  15         760  
4 15     15   111 use autodie;
  15         42  
  15         139  
5              
6 15     15   101654 use App::RL -command;
  15         50  
  15         244  
7 15     15   8178 use App::RL::Common;
  15         43  
  15         22515  
8              
9             sub abstract {
10 2     2 1 93 return 'output detailed depths of coverages on chromosomes';
11             }
12              
13             sub opt_spec {
14             return (
15 6     6 1 87 [ "outfile|o=s", "output filename. [stdout] for screen", ],
16             [ "size|s=s", "chr.sizes", { required => 1 }, ],
17             [ "max|m=i", "count to this depth", { default => 1 }, ],
18             { show_defaults => 1, }
19             );
20             }
21              
22             sub usage_desc {
23 6     6 1 75935 return "runlist coverage [options] [more infiles]";
24             }
25              
26             sub description {
27 1     1 1 1289 my $desc;
28 1         6 $desc .= ucfirst(abstract) . ".\n";
29 1         3 $desc .= <<'MARKDOWN';
30              
31             Like `runlist cover`, also output depths of coverages
32              
33             I:1-100
34             I(+):90-150
35             S288c.I(-):190-200 # Species names will be omitted
36              
37             MARKDOWN
38              
39 1         3 return $desc;
40             }
41              
42             sub validate_args {
43 4     4 1 7371 my ( $self, $opt, $args ) = @_;
44              
45 4 100       11 if ( !@{$args} ) {
  4         25  
46 1         2 my $message = "This command need one or more input files.\n\tIt found";
47 1         3 $message .= sprintf " [%s]", $_ for @{$args};
  1         3  
48 1         5 $message .= ".\n";
49 1         12 $self->usage_error($message);
50             }
51 3         11 for ( @{$args} ) {
  3         13  
52 3 50       16 next if lc $_ eq "stdin";
53 3 100       24 if ( !Path::Tiny::path($_)->is_file ) {
54 1         231 $self->usage_error("The input file [$_] doesn't exist.");
55             }
56             }
57              
58 2 50       300 if ( !exists $opt->{outfile} ) {
59 0         0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".yml";
60             }
61             }
62              
63             sub execute {
64 2     2 1 27 my ( $self, $opt, $args ) = @_;
65              
66 2         20 my $length_of = App::RL::Common::read_sizes( $opt->{size} );
67              
68 2         5 my %count_of; # YAML::Sync can't Dump tied hashes
69 2         12 for my $depth ( 0 .. $opt->{max} ) {
70 8 50       22 if ( !exists $count_of{$depth} ) {
71 8         24 $count_of{$depth} = {};
72             }
73             }
74             {
75 2         5 for my $chr ( keys %{$length_of} ) {
  2         4  
  2         14  
76 32         1877 $count_of{0}->{$chr} = App::RL::Common::new_set();
77 32         435 $count_of{0}->{$chr}->add_pair( 1, $length_of->{$chr} );
78             }
79             }
80              
81 2         137 for my $infile ( @{$args} ) {
  2         13  
82 2         12 for my $line ( App::RL::Common::read_lines($infile) ) {
83 10 50       47 next if substr( $line, 0, 1 ) eq "#";
84              
85 10         38 my $info = App::RL::Common::decode_header($line);
86 10 50       30 next unless App::RL::Common::info_is_valid($info);
87              
88 10         44 my $chr_name = $info->{chr};
89 10 50       81 next unless exists $length_of->{$chr_name};
90              
91             # count depth
92 10         32 my $set = App::RL::Common::new_set()->add_pair( $info->{start}, $info->{end} );
93 10         904 $set = $count_of{0}->{$chr_name}->intersect($set);
94 10         4199 DEPTH: for my $cur ( 0 .. $opt->{max} ) {
95 21 100       78 if ( !exists $count_of{$cur}->{$chr_name} ) {
96 5         16 $count_of{$cur}->{$chr_name} = App::RL::Common::new_set();
97             }
98              
99             #
100 21         110 my $iset_cur = $count_of{$cur}->{$chr_name}->intersect($set);
101              
102 21 100       6662 if ( $iset_cur->is_empty ) {
103 9         176 $count_of{$cur}->{$chr_name}->add($set);
104 9         940 $set->clear;
105 9         125 last DEPTH;
106             }
107             else {
108 12         191 $count_of{$cur}->{$chr_name}->add($set);
109 12         1331 $set = $iset_cur;
110             }
111             }
112             }
113             }
114              
115             # remove regions from lower coverages
116 2         32 my $max_depth = List::Util::max( keys %count_of );
117 2         10 for my $i ( 0 .. $max_depth - 1 ) {
118 6         14 for my $j ( $i + 1 .. $max_depth ) {
119 16         20 for my $chr_name ( keys %{ $count_of{$i} } ) {
  16         58  
120 107 100       1739 if ( exists $count_of{$j}->{$chr_name} ) {
121 6         29 $count_of{$i}->{$chr_name}->remove( $count_of{$j}->{$chr_name} );
122             }
123             }
124             }
125             }
126              
127             # IntSpan to runlist
128 2         9 for my $key ( keys %count_of ) {
129 8 100       195 if ( keys %{ $count_of{$key} } == 0 ) {
  8         32  
130 3         11 delete $count_of{$key};
131 3         11 next;
132             }
133 5         10 for my $chr_name ( keys %{ $count_of{$key} } ) {
  5         15  
134 37         1379 $count_of{$key}->{$chr_name} = $count_of{$key}->{$chr_name}->runlist;
135             }
136             }
137              
138             #----------------------------#
139             # Output
140             #----------------------------#
141 2         41 my $out_fh;
142 2 50       11 if ( lc( $opt->{outfile} ) eq "stdout" ) {
143 2         15 $out_fh = *STDOUT;
144             }
145             else {
146 0         0 open $out_fh, ">", $opt->{outfile};
147             }
148              
149 2         5 print {$out_fh} YAML::Syck::Dump( \%count_of );
  2         18  
150 2         402 close $out_fh;
151             }
152              
153             1;