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

filter: Statementstmt 61.6% 66 / 107 Branchbran 25.0% 8 / 32 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 52.9% 90 / 170 CC 27 SCAR 47.2
CC
27
Cov
50%
execute
187.3
validate_args
6.0
BEGIN
1.0
CRAP 112.6
« blib/lib/App/Dazz/Command/cover.pm ↑ Summary blib/lib/App/Dazz/Command/overlap2.pm »
1 package App::Dazz::Command::contained;
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 => "discard contained unitigs";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
12 return (
13 5 [ "outfile|o=s", "output filename, [stdout] for screen", ],
14 [ "len|l=i", "minimal length of overlaps", { default => 500 }, ],
15 [ "idt|i=f", "minimal identity of overlaps", { default => 0.98 }, ],
16 [ "proportion=f", "nearly contained proportion", { default => 0.98 }, ],
17 [ "prefix=s", "prefix of names", { default => "infile" }, ],
18 [ "tmp=s", "user defined tempdir", ],
19 [ "parallel|p=i", "number of threads", { default => 8 }, ],
20 [ "verbose|v", "verbose mode", ],
21 { show_defaults => 1, }
22 );
23 }
24  
25 5 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
26 5 return "dazz contained [options] <infile> [more infiles]";
27 }
28  
29 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
30 1 my $desc;
31 1 $desc .= ucfirst(abstract) . ".\n";
32 1 $desc .= "\tAll operations are running in a tempdir and no intermediate files are kept.\n";
33 1 return $desc;
34 }
35  
36 4 ▶sub validate_args {
Subroutine validate_args: called CC=6 SCAR=18.0
Pod documented
37 4 my ( $self, $opt, $args ) = @_;
38  
39 4 ▶ if ( @{$args} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} < 1) 1 3 4
40 1 my $message = "This command need one or more input files.\n\tIt found";
41 1 $message .= sprintf " [%s]", $_ for @{$args};
42 1 $message .= ".\n";
43 1 $self->usage_error($message);
44 }
45 3 for ( @{$args} ) {
46 4 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 2 2 4
47 2 $self->usage_error("The input file [$_] doesn't exist.");
48 }
49 }
50  
51 1 ▶ if ( !exists $opt->{outfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt->{"outfile"}) 0 1 1
52 0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".contained.fasta";
53 }
54  
55 }
56  
57 1 ▶sub execute {
Subroutine execute: called CC=22 SCAR=52.3
Pod documented
58 1 my ( $self, $opt, $args ) = @_;
59  
60 # absolute pathes as we will chdir to tempdir later
61 1 my @infiles;
62 1 for my $infile ( @{$args} ) {
63 1 push @infiles, Path::Tiny::path($infile)->absolute->stringify;
64 }
65  
66 1 ▶ if ( lc $opt->{outfile} ne "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{"outfile"} ne "stdout") 0 1 1
67 0 $opt->{outfile} = Path::Tiny::path( $opt->{outfile} )->absolute->stringify;
68 }
69  
70 # record cwd, we'll return there
71 1 my $cwd = Path::Tiny->cwd;
72 1 my $tempdir;
73 1 ▶ if ( $opt->{tmp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'tmp'}) { } 0 1 1
74 $tempdir = Path::Tiny->tempdir(
75 TEMPLATE => "dazz_contained_XXXXXXXX",
76 DIR => $opt->{tmp},
77 0 );
78 }
79 else {
80 1 $tempdir = Path::Tiny->tempdir("dazz_contained_XXXXXXXX");
81 }
82 1 chdir $tempdir;
83  
84 1 my $basename = $tempdir->basename();
85 1 $basename =~ s/\W+/_/g;
86  
87 { # filter short contigs then rename reads as there're duplicated names
88 1 for my $i ( 0 .. $#infiles ) {
89 1 my $cmd;
90 1 $cmd .= "faops filter -a $opt->{len} -l 0 $infiles[$i] stdout";
91 1 $cmd .= " | faops dazz -p $opt->{prefix}_$i stdin infile.$i.fasta";
92  
93 1 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, }, );
94 }
95 }
96  
97 { # overlaps
98 1 my $cmd;
99 1 $cmd .= "dazz overlap";
100 1 $cmd .= " --len $opt->{len} --idt $opt->{idt} --parallel $opt->{parallel}";
101 1 $cmd .= sprintf " infile.%d.fasta", $_ for ( 0 .. $#infiles );
102 1 $cmd .= " -o contained.ovlp.tsv";
103 1 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
104  
105 1 ▶ if ( !$tempdir->child("contained.ovlp.tsv")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("contained.ovlp.tsv")->is_file) 1 0 1
106 1 Carp::croak "Failed: create contained.ovlp.tsv\n";
107 }
108 }
109  
110 { # discard some SR
111 1 my @discards;
112 my %seen_pair;
113 0 for my $line ( $tempdir->child("contained.ovlp.tsv")->lines( { chomp => 1, } ) ) {
114 0 my $info = App::Dazz::Common::parse_ovlp_line($line);
115  
116 # ignore self overlapping
117 0 ▶ next if $info->{f_id} eq $info->{g_id};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"f_id"} eq $info->{"g_id"} 0 0 0
118  
119 # ignore poor overlaps
120 0 ▶ next if $info->{ovlp_idt} < $opt->{idt};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"ovlp_idt"} < $opt->{"idt"} 0 0 0
121 0 ▶ next if $info->{ovlp_len} < $opt->{len};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"ovlp_len"} < $opt->{"len"} 0 0 0
122  
123 # skip duplicated overlaps
124 0 my $pair = join( "-", sort ( $info->{f_id}, $info->{g_id} ) );
125 0 ▶ next if $seen_pair{$pair};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_pair{$pair} 0 0 0
126 0 $seen_pair{$pair}++;
127  
128 # discard contained SR
129 0 ▶ if ( $info->{contained} eq q{contains} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"contained"} eq "contains") 0 0 0
130 0 push @discards, $info->{g_id};
131 0 next;
132 }
133 0 ▶ if ( $info->{contained} eq q{contained} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"contained"} eq "contained") 0 0 0
134 0 push @discards, $info->{f_id};
135 0 next;
136 }
137  
138 # discard nearly contained SR
139 0 my $f_r = $info->{ovlp_len} / $info->{f_len};
140 0 my $g_r = $info->{ovlp_len} / $info->{g_len};
141  
142 0 ▶ if ( $f_r <= $opt->{proportion} and $g_r > $opt->{proportion} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_r <= $opt->{"proportion"} and $g_r > $opt->{"proportion"}) 0 0 0
Condition0%0/3
$f_r <= $opt->{"proportion"} and $g_r > $opt->{"proportion"}
!ll&&!rl&&r
000
MC/DC0%0/2
$f_r <= $opt->{"proportion"} and $g_r > $opt->{"proportion"}
$f_r <= $opt->{"proportion"} $g_r > $opt->{"proportion"}
Truth table0%0/3
A and B
A= $f_r <= $opt->{"proportion"}B= $g_r > $opt->{"proportion"}
ABresult
0X0
100
111
143 0 push @discards, $info->{g_id};
144 0 next;
145 }
146  
147 0 ▶ if ( $g_r <= $opt->{proportion} and $f_r > $opt->{proportion} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g_r <= $opt->{"proportion"} and $f_r > $opt->{"proportion"}) 0 0 0
Condition0%0/3
$g_r <= $opt->{"proportion"} and $f_r > $opt->{"proportion"}
!ll&&!rl&&r
000
MC/DC0%0/2
$g_r <= $opt->{"proportion"} and $f_r > $opt->{"proportion"}
$g_r <= $opt->{"proportion"} $f_r > $opt->{"proportion"}
Truth table0%0/3
A and B
A= $g_r <= $opt->{"proportion"}B= $f_r > $opt->{"proportion"}
ABresult
0X0
100
111
148 0 push @discards, $info->{f_id};
149 0 next;
150 }
151  
152 0 ▶ if ( $f_r > $opt->{proportion} and $g_r > $opt->{proportion} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_r > $opt->{"proportion"} and $g_r > $opt->{"proportion"}) 0 0 0
Condition0%0/3
$f_r > $opt->{"proportion"} and $g_r > $opt->{"proportion"}
!ll&&!rl&&r
000
MC/DC0%0/2
$f_r > $opt->{"proportion"} and $g_r > $opt->{"proportion"}
$f_r > $opt->{"proportion"} $g_r > $opt->{"proportion"}
Truth table0%0/3
A and B
A= $f_r > $opt->{"proportion"}B= $g_r > $opt->{"proportion"}
ABresult
0X0
100
111
153 0 ▶ if ( $info->{f_len} >= $info->{g_len} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'f_len'} >= $info->{'g_len'}) { } 0 0 0
154 0 push @discards, $info->{g_id};
155 }
156 else {
157 0 push @discards, $info->{f_id};
158 }
159 0 next;
160 }
161 }
162  
163 0 @discards = App::Fasops::Common::uniq( sort @discards );
164 0 $tempdir->child("discard.txt")->spew( map {"$_\n"} @discards );
165 }
166  
167 { # Outputs. stdout is handeld by faops
168 0 my $cmd = "cat";
169 0 $cmd .= sprintf " infile.%d.fasta", $_ for ( 0 .. $#infiles );
170 0 $cmd .= " | faops some -i -l 0 stdin discard.txt $opt->{outfile}";
171 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
172 }
173  
174 0 chdir $cwd;
175 }
176  
177 11 1;
« blib/lib/App/Dazz/Command/cover.pm ↑ Summary blib/lib/App/Dazz/Command/overlap2.pm »