blib/lib/App/Dazz/Command/cover.pm

filter: Statementstmt 24.2% 33 / 136 Branchbran 10.7% 3 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 100% 5 / 5 totaltotal 27.7% 50 / 180 CC 20 SCAR 48.0
CC
20
Cov
22%
execute
240.0
validate_args
6.6
description
2.0
CRAP 121.6
« blib/lib/App/Dazz/Command/merge.pm ↑ Summary blib/lib/App/Dazz/Command/contained.pm »
1 package App::Dazz::Command::cover;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use App::Dazz -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use App::Dazz::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use constant abstract => "trusted regions in the first file covered by the second";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
12 return (
13 3 [ "outfile|o=s", "output filename, [stdout] for screen", ],
14 [ "range|r=s", "ranges of first sequences", { required => 1 }, ],
15 [ 'coverage|c=i', 'minimal coverage', { default => 2 }, ],
16 [ 'max|m=i', 'maximal coverage', { default => 200 }, ],
17 [ "len|l=i", "minimal length of overlaps", { default => 1000 }, ],
18 [ "idt|i=f", "minimal identity of overlaps", { default => 0.85 }, ],
19 [ "tmp=s", "user defined tempdir", ],
20 [ "verbose|v", "verbose mode", ],
21 { show_defaults => 1, }
22 );
23 }
24  
25 3 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
26 3 return "dazz cover [options] <.ovlp.tsv>";
27 }
28  
29 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
30 0 my $desc;
31 0 $desc .= ucfirst(abstract) . ".\n";
32 0 $desc .= "\tAll operations are running in a tempdir and no intermediate files are kept.\n";
33 0 return $desc;
34 }
35  
36 3 ▶sub validate_args {
Subroutine validate_args: called CC=6 SCAR=18.8
Pod documented
37 3 my ( $self, $opt, $args ) = @_;
38  
39 3 ▶ if ( @{$args} != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 1) 1 2 3
40 1 my $message = "This command need one input file.\n\tIt found";
41 1 $message .= sprintf " [%s]", $_ for @{$args};
42 1 $message .= ".\n";
43 1 $self->usage_error($message);
44 }
45 2 for ( @{$args} ) {
46 2 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 2 0 2
47 2 $self->usage_error("The input file [$_] doesn't exist.");
48 }
49 }
50  
51 0 ▶ if ( !exists $opt->{outfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt->{"outfile"}) 0 0 0
52 0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".cover.json";
53 }
54 }
55  
56 0 ▶sub execute {
Subroutine execute: not called CC=15 SCAR=54.8
Pod documented
57 0 my ( $self, $opt, $args ) = @_;
58  
59 # make paths absolute before we chdir
60 0 my $infile = Path::Tiny::path( $args->[0] )->absolute->stringify;
61  
62 0 ▶ if ( lc $opt->{outfile} ne "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{"outfile"} ne "stdout") 0 0 0
63 0 $opt->{outfile} = Path::Tiny::path( $opt->{outfile} )->absolute->stringify;
64 }
65  
66 # record cwd, we'll return there
67 0 my $cwd = Path::Tiny->cwd;
68 0 my $tempdir;
69 0 ▶ if ( $opt->{tmp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'tmp'}) { } 0 0 0
70 $tempdir = Path::Tiny->tempdir(
71 TEMPLATE => "dazz_cover_XXXXXXXX",
72 DIR => $opt->{tmp},
73 0 );
74 }
75 else {
76 0 $tempdir = Path::Tiny->tempdir("dazz_cover_XXXXXXXX");
77 }
78 0 chdir $tempdir;
79  
80 0 my $basename = $tempdir->basename();
81 0 $basename =~ s/\W+/_/g;
82  
83 #@type AlignDB::IntSpan
84 0 my $first_range = AlignDB::IntSpan->new->add_runlist( $opt->{range} );
85  
86 {
87 # paf to meancov
88 0 my $cmd;
89 0 $cmd .= "ovlpr covered";
90 0 $cmd .= " $infile";
91 0 $cmd .= " --coverage $opt->{max}";
92 0 $cmd .= " --mean";
93 0 $cmd .= " --len $opt->{len} --idt $opt->{idt}";
94 0 $cmd .= " -o $basename.meancov.txt";
95 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
96  
97 0 ▶ if ( !$tempdir->child("$basename.meancov.txt")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("$basename.meancov.txt")->is_file) 0 0 0
98 0 Carp::croak "Failed: create $basename.meancov.txt\n";
99 }
100 }
101  
102 # anchor_id => mean coverage
103 0 my $coverage_of = {};
104 0 my $len_of = {};
105 0 my $stat = Statistics::Descriptive::Full->new();
106 0 for my $line ( App::RL::Common::read_lines("$basename.meancov.txt") ) {
107 0 my @parts = split "\t", $line;
108 0 ▶ next unless @parts == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 3 0 0 0
109  
110 0 my $seq_id = $parts[0];
111 0 ▶ next unless $first_range->contains($seq_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $first_range->contains($seq_id) 0 0 0
112  
113 0 $len_of->{$seq_id} = $parts[1];
114  
115 0 $coverage_of->{$seq_id} = $parts[2];
116 0 $stat->add_data( $parts[2] );
117 }
118  
119 0 my $meta_of = { TRUSTED => $first_range->copy, };
120 {
121 0 my $median = $stat->median();
122 0 my @abs_res = map { abs( $median - $_ ) } $stat->get_data();
123 0 my $abs_res_stat = Statistics::Descriptive::Full->new();
124 0 $abs_res_stat->add_data(@abs_res);
125 0 my $MAD = $abs_res_stat->median();
126  
127 # the scale factor
128 0 my $k = 3;
129  
130 0 my $lower_limit = ( $median - $k * $MAD ) / 2;
131 0 my $upper_limit = ( $median + $k * $MAD ) * 1.5;
132  
133 0 $meta_of->{NON_OVERLAPPED} = AlignDB::IntSpan->new;
134 0 $meta_of->{REPEAT_LIKE} = AlignDB::IntSpan->new;
135 0 for my $key ( keys %{$coverage_of} ) {
136 0 ▶ if ( $coverage_of->{$key} < $lower_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coverage_of->{$key} < $lower_limit) 0 0 0
137 0 $meta_of->{NON_OVERLAPPED}->add($key);
138 }
139 0 ▶ if ( $coverage_of->{$key} > $upper_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coverage_of->{$key} > $upper_limit) 0 0 0
140 0 $meta_of->{REPEAT_LIKE}->add($key);
141 }
142 }
143  
144 0 $meta_of->{COV_MEDIAN} = $median;
145 0 $meta_of->{COV_MAD} = $MAD;
146 0 $meta_of->{COV_LOWER_LIMIT} = $lower_limit;
147 0 $meta_of->{COV_UPPER_LIMIT} = $upper_limit;
148  
149 0 $meta_of->{TRUSTED}->subtract( $meta_of->{NON_OVERLAPPED} );
150 0 $meta_of->{TRUSTED}->subtract( $meta_of->{REPEAT_LIKE} );
151 }
152  
153 {
154 # paf to covered
155 0 my $cmd;
156 0 $cmd .= "ovlpr covered";
157 0 $cmd .= " $infile";
158 0 $cmd .= " --coverage $opt->{coverage}";
159 0 $cmd .= " --len $opt->{len} --idt $opt->{idt}";
160 0 $cmd .= " -o $basename.covered.txt";
161 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
162  
163 0 ▶ if ( !$tempdir->child("$basename.covered.txt")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("$basename.covered.txt")->is_file) 0 0 0
164 0 Carp::croak "Failed: create $basename.covered.txt\n";
165 }
166 }
167  
168 # anchor_id => covered region
169 0 my $covered_of = {};
170 0 $meta_of->{PARTIAL_COVERED} = AlignDB::IntSpan->new;
171 0 for my $line ( App::RL::Common::read_lines("$basename.covered.txt") ) {
172 0 my @parts = split ":", $line;
173 0 ▶ next unless @parts == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 2 0 0 0
174  
175 0 my $seq_id = $parts[0];
176 0 ▶ next unless $first_range->contains($seq_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $first_range->contains($seq_id) 0 0 0
177  
178 0 my $covered = AlignDB::IntSpan->new()->add_runlist( $parts[1] );
179  
180 0 ▶ if ( $covered->size < $len_of->{$seq_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($covered->size < $len_of->{$seq_id}) 0 0 0
181 0 $meta_of->{TRUSTED}->remove($seq_id);
182 0 $meta_of->{PARTIAL_COVERED}->add($seq_id);
183 0 $covered_of->{$seq_id} = $covered->runlist;
184 }
185 }
186  
187 # {
188 # # Create covered.fasta
189 # $tempdir->child("covered.fasta")->remove;
190 # for my $serial ( sort { $a <=> $b } keys %{$covered_of} ) {
191 # if ( $trusted->contains($serial) ) {
192 # my $cmd;
193 # $cmd .= "DBshow -U $basename $serial";
194 # $cmd .= " | faops replace -l 0 stdin first.replace.tsv stdout";
195 # $cmd .= " >> covered.fasta";
196 # App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
197 # }
198 # else {
199 #
200 # #@type AlignDB::IntSpan
201 # my $region = $covered_of->{$serial}{ $opt->{coverage} };
202 #
203 # for my $set ( $region->sets ) {
204 # next if $set->size < $opt->{len};
205 #
206 # my $cmd;
207 # $cmd .= "DBshow -U $basename $serial";
208 # $cmd .= " | faops replace -l 0 stdin first.replace.tsv stdout";
209 # $cmd .= " | faops frag -l 0 stdin @{[$set->min]} @{[$set->max]} stdout";
210 # $cmd .= " >> covered.fasta";
211 # App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
212 # }
213 # }
214 # }
215 #
216 # if ( !$tempdir->child("covered.fasta")->is_file ) {
217 # Carp::croak "Failed: create covered.fasta\n";
218 # }
219 # }
220  
221 {
222 0 $meta_of->{TRUSTED} = $meta_of->{TRUSTED}->runlist;
223 0 $meta_of->{NON_OVERLAPPED} = $meta_of->{NON_OVERLAPPED}->runlist;
224 0 $meta_of->{REPEAT_LIKE} = $meta_of->{REPEAT_LIKE}->runlist;
225 0 $meta_of->{PARTIAL_COVERED} = $meta_of->{PARTIAL_COVERED}->runlist;
226 0 $meta_of->{TOTAL_RANGE} = $first_range->runlist;
227  
228 0 $tempdir->child("meta.cover.json")
229 ->spew( JSON::to_json( $meta_of, { pretty => 1, canonical => 1, } ) );
230 0 $tempdir->child("meta.cover.json")->copy( $opt->{outfile} );
231  
232 $tempdir->child("partial.txt")
233 0 ->spew( map { sprintf "%s:%s\n", $_, $covered_of->{$_} } keys %{$covered_of} );
234 0 $tempdir->child("partial.txt")->copy("$opt->{outfile}.partial.txt");
235  
236 0 YAML::Syck::DumpFile( "coverage.yml", $coverage_of );
237 0 $tempdir->child("coverage.yml")->copy("$opt->{outfile}.coverage.yml");
238 }
239  
240 0 chdir $cwd;
241 }
242  
243 11 1;
« blib/lib/App/Dazz/Command/merge.pm ↑ Summary blib/lib/App/Dazz/Command/contained.pm »