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

filter: Statementstmt 45.3% 69 / 152 Branchbran 23.0% 12 / 52 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 43.1% 97 / 225 CC 38 SCAR 58.2
CC
38
Cov
39%
execute
520.6
validate_args
8.1
BEGIN
1.0
CRAP 337.8
« blib/lib/App/Dazz/Command/group.pm ↑ Summary blib/lib/App/Dazz/Command/layout.pm »
1 package App::Dazz::Command::orient;
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 => "orient overlapped sequences to the same strand";
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 [ "restrict|r=s", "limit to known pairs", ],
15 [ "len|l=i", "minimal length of overlaps", { default => 1000 }, ],
16 [ "idt|i=f", "minimal identity of overlaps", { default => 0.85 }, ],
17 [ "tmp=s", "user defined tempdir", ],
18 [ "parallel|p=i", "number of threads", { default => 4 }, ],
19 [ "verbose|v", "verbose mode", ],
20 { show_defaults => 1, }
21 );
22 }
23  
24 5 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
25 5 return "dazz orient [options] <infiles>";
26 }
27  
28 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
29 1 my $desc;
30 1 $desc .= ucfirst(abstract) . ".\n";
31 1 $desc .= "\tThis command is for small files.\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=8 SCAR=20.9
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 6 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 1 5 6
47 1 $self->usage_error("The input file [$_] doesn't exist.");
48 }
49 }
50  
51 2 ▶ if ( exists $opt->{restrict} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"restrict"}) 2 0 2
52 2 ▶ if ( !Path::Tiny::path( $opt->{restrict} )->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($opt->{"restrict"})->is_file) 1 1 2
53 1 $self->usage_error("The restrict file [$opt->{restrict}] doesn't exist.\n");
54 }
55 }
56  
57 1 ▶ if ( !exists $opt->{outfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt->{"outfile"}) 0 1 1
58 0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".rc.fasta";
59 }
60 }
61  
62 1 ▶sub execute {
Subroutine execute: called CC=31 SCAR=62.6
Pod documented
63 1 my ( $self, $opt, $args ) = @_;
64  
65 # absolute pathes as we will chdir to tempdir later
66 1 my @infiles;
67 1 for my $infile ( @{$args} ) {
68 2 push @infiles, Path::Tiny::path($infile)->absolute->stringify;
69 }
70  
71 1 ▶ if ( lc $opt->{outfile} ne "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{"outfile"} ne "stdout") 0 1 1
72 0 $opt->{outfile} = Path::Tiny::path( $opt->{outfile} )->absolute->stringify;
73 }
74  
75 1 ▶ if ( $opt->{restrict} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"restrict"}) 1 0 1
76 1 $opt->{restrict} = Path::Tiny::path( $opt->{restrict} )->absolute->stringify;
77 }
78  
79 # record cwd, we'll return there
80 1 my $cwd = Path::Tiny->cwd;
81 1 my $tempdir;
82 1 ▶ if ( $opt->{tmp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'tmp'}) { } 0 1 1
83 $tempdir = Path::Tiny->tempdir(
84 TEMPLATE => "dazz_orient_XXXXXXXX",
85 DIR => $opt->{tmp},
86 0 );
87 }
88 else {
89 1 $tempdir = Path::Tiny->tempdir("dazz_orient_XXXXXXXX");
90 }
91 1 chdir $tempdir;
92  
93 1 my $basename = $tempdir->basename();
94 1 $basename =~ s/\W+/_/g;
95  
96 { # Sort by lengths
97 1 for my $i ( 0 .. $#infiles ) {
98 App::Dazz::Common::exec_cmd(
99 "faops size $infiles[$i] | sort -n -r -k2,2 | cut -f 1 > infile.$i.order.txt",
100 { verbose => $opt->{verbose}, },
101 2 );
102 App::Dazz::Common::exec_cmd(
103 "faops order $infiles[$i] infile.$i.order.txt infile.$i.fasta",
104 { verbose => $opt->{verbose}, },
105 2 );
106 }
107 }
108  
109 { # Preprocess reads to format them for dazzler
110 1 my $cmd = "cat";
111 1 $cmd .= sprintf " infile.%d.fasta", $_ for ( 0 .. $#infiles );
112 1 $cmd .= " | dazz dazzname stdin -o stdout";
113 1 $cmd .= " | faops filter -l 0 stdin renamed.fasta";
114 1 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
115  
116 1 ▶ if ( !$tempdir->child("renamed.fasta")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("renamed.fasta")->is_file) 1 0 1
117 1 Carp::croak "Failed: create renamed.fasta\n";
118 }
119  
120 0 ▶ if ( !$tempdir->child("stdout.replace.tsv")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("stdout.replace.tsv")->is_file) 0 0 0
121 0 Carp::croak "Failed: create stdout.replace.tsv\n";
122 }
123 }
124  
125 { # overlaps
126 1 my $cmd;
127 0 $cmd .= "dazz overlap renamed.fasta";
128 0 $cmd .= " --len $opt->{len} --idt $opt->{idt} --parallel $opt->{parallel}";
129 0 $cmd .= " -o renamed.ovlp.tsv";
130 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
131  
132 0 ▶ if ( !$tempdir->child("renamed.ovlp.tsv")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("renamed.ovlp.tsv")->is_file) 0 0 0
133 0 Carp::croak "Failed: create renamed.ovlp.tsv\n";
134 }
135 }
136  
137 # filter overlaps
138 0 ▶ if ( $opt->{restrict} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"restrict"}) 0 0 0
139 0 my $cmd;
140 0 $cmd .= "ovlpr replace renamed.ovlp.tsv stdout.replace.tsv -o stdout";
141 0 $cmd .= " | ovlpr restrict stdin $opt->{restrict} -o stdout";
142 0 $cmd .= " | ovlpr replace stdin stdout.replace.tsv -r -o stdout";
143 0 $cmd .= " > restrict.ovlp.tsv";
144 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
145  
146 0 ▶ if ( !$tempdir->child("restrict.ovlp.tsv")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("restrict.ovlp.tsv")->is_file) 0 0 0
147 0 Carp::croak "Failed: create restrict.ovlp.tsv\n";
148 }
149 }
150  
151 0 my $graph = Graph->new( directed => 0 );
152 { # Build ovlp graph
153 0 my @lines;
154 0 ▶ if ( $tempdir->child("restrict.ovlp.tsv")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tempdir->child('restrict.ovlp.tsv')->is_file) { } 0 0 0
155 0 @lines = $tempdir->child("restrict.ovlp.tsv")->lines( { chomp => 1, } );
156 }
157 else {
158 0 @lines = $tempdir->child("renamed.ovlp.tsv")->lines( { chomp => 1, } );
159 }
160  
161 # load strands
162 0 for my $line (@lines) {
163 0 my $info = App::Dazz::Common::parse_ovlp_line($line);
164  
165 # ignore self overlapping
166 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
167  
168 # ignore poor overlaps
169 0 ▶ next if $info->{ovlp_idt} < $opt->{idt};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"ovlp_idt"} < $opt->{"idt"} 0 0 0
170 0 ▶ next if $info->{ovlp_len} < $opt->{len};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"ovlp_len"} < $opt->{"len"} 0 0 0
171  
172 0 $graph->add_edge( $info->{f_id}, $info->{g_id}, );
173 $graph->set_edge_attribute( $info->{f_id}, $info->{g_id}, q{strand},
174 0 $info->{g_strand} );
175 }
176 }
177  
178 { # To positive strands in each cc
179 0 for my $cc ( $graph->connected_components ) {
180 0 my @pieces = @{$cc};
181 0 my $copy = scalar @pieces;
182  
183 0 ▶ next if $copy == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $copy == 1 0 0 0
184  
185 # set first sequence to positive strand
186 0 my $assigned = AlignDB::IntSpan->new(0);
187 0 $graph->set_vertex_attribute( $pieces[0], q{strand}, 0 );
188  
189 # need to be handled
190 0 my $unhandled = AlignDB::IntSpan->new->add_pair( 1, $copy - 1 );
191  
192 0 my $prev_size = $assigned->size;
193 0 my $cur_loop = 0; # existing point
194 0 ▶ while ( $assigned->size < $copy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assigned->size < $copy) 0 0 0
195 0 ▶ if ( $prev_size == $assigned->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_size == $assigned->size) { } 0 0 0
196 0 $cur_loop++;
197 0 ▶ last if $cur_loop > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_loop > 10 0 0 0
198 }
199 else {
200 0 $cur_loop = 0;
201 }
202 0 $prev_size = $assigned->size;
203  
204 0 for my $i ( $assigned->elements ) {
205 0 for my $j ( $unhandled->elements ) {
206 0 ▶ next unless $graph->has_edge( $pieces[$i], $pieces[$j] );
Branch: 1 branch
BranchTrueFalseTotal
unless $graph->has_edge($pieces[$i], $pieces[$j]) 0 0 0
207  
208 # assign strands
209 0 my $i_strand = $graph->get_vertex_attribute( $pieces[$i], q{strand} );
210 0 my $edge_strand
211 = $graph->get_edge_attribute( $pieces[$i], $pieces[$j], q{strand} );
212 0 ▶ if ( $edge_strand == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($edge_strand == 0) { } 0 0 0
213 0 $graph->set_vertex_attribute( $pieces[$j], q{strand}, $i_strand );
214 }
215 else {
216 0 ▶ if ( $i_strand == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_strand == 0) { } 0 0 0
217 0 $graph->set_vertex_attribute( $pieces[$j], q{strand}, 1 );
218 }
219 else {
220 0 $graph->set_vertex_attribute( $pieces[$j], q{strand}, 0 );
221 }
222 }
223 0 $unhandled->remove($j);
224 0 $assigned->add($j);
225 }
226 }
227 }
228 }
229 }
230  
231 { # RC
232 0 my @negs;
233 0 for my $i ( sort $graph->vertices ) {
234 0 my $i_strand = $graph->get_vertex_attribute( $i, q{strand} );
235 0 ▶ push @negs, $i if ( defined $i_strand and $i_strand == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $i_strand and $i_strand == 1 0 0 0
Condition0%0/3
defined $i_strand and $i_strand == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $i_strand and $i_strand == 1
defined $i_strand $i_strand == 1
Truth table0%0/3
A and B
A= defined $i_strandB= $i_strand == 1
ABresult
0X0
100
111
236 }
237  
238 0 $tempdir->child("rc.list")->spew( map {"$_\n"} @negs );
239  
240 0 my $cmd;
241 0 $cmd .= "faops rc -l 0 -n -f rc.list";
242 0 $cmd .= " renamed.fasta renamed.rc.fasta";
243 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
244  
245 0 ▶ if ( !$tempdir->child("renamed.rc.fasta")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("renamed.rc.fasta")->is_file) 0 0 0
246 0 Carp::croak "Failed: create renamed.rc.fasta\n";
247 }
248 }
249  
250 { # Outputs. stdout is handled by faops
251 0 my $cmd;
252 0 $cmd .= "faops replace -l 0 renamed.rc.fasta stdout.replace.tsv";
253 0 $cmd .= " $opt->{outfile}";
254 0 App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
255 }
256  
257 0 chdir $cwd;
258 }
259  
260 11 1;
« blib/lib/App/Dazz/Command/group.pm ↑ Summary blib/lib/App/Dazz/Command/layout.pm »