Egaz/Command/lastz.pm

filter: Statementstmt 25.3% 35 / 138 Branchbran 5.5% 3 / 54 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 76.9% 10 / 13 Podpod 100% 6 / 6 totaltotal 23.8% 55 / 231 CC 39 SCAR 50.0
CC
39
Cov
19%
execute
306.0
validate_args
128.9
__ANON__
30.0
CRAP 148.8
« Egaz/Command/blastmatch.pm ↑ Summary Egaz/Command/lpcnam.pm »
1 package App::Egaz::Command::lastz;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use App::Egaz -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use App::Egaz::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
10 2 return 'lastz wrapper for two genomes or self alignments';
11 }
12  
13 4 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
14 return (
15 4 [ "outdir|o=s", "Output directory", { default => "." }, ],
16 [ "tp", "target sequences are partitioned", ],
17 [ "qp", "query sequences are partitioned", ],
18 [ "paired", "relationships between target and query are one-to-one", ],
19 [ "isself", "self-alignment", ],
20 [ "set|s=s", "use a predefined lastz parameter set", ],
21 [ "O=i", "Scoring: gap-open penalty", ],
22 [ "E=i", "Scoring: gap-extension penalty", ],
23 [ "Q=s", "Scoring: matrix file", ],
24 [ "C=i", "Aligning: chain option", ],
25 [ "T=i", "Aligning: words option", ],
26 [ "M=i", "Aligning: mask any base in seq1 hit this many times", ],
27 [ "K=i", "Dropping hsp: threshold for MSPs for the first pass", ],
28 [ "L=i", "Dropping hsp: threshold for gapped alignments for the second pass", ],
29 [ "H=i", "Dropping hsp: threshold to be interpolated between alignments", ],
30 [ "Y=i", "Dropping hsp: X-drop parameter for gapped extension", ],
31 [ "Z=i", "Speedup: increment between successive words", ],
32 [ "parallel|p=i", "number of threads", { default => 2 }, ],
33 [ "verbose|v", "verbose mode", ],
34 { show_defaults => 1, }
35 );
36 }
37  
38 4 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
39 4 return "egaz lastz [options] <path/target> <path/query>";
40 }
41  
42 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
43 1 my $desc;
44 1 $desc .= ucfirst(abstract) . ".\n";
45 1 $desc .= <<'MARKDOWN';
46  
47 * `lastz` should be in $PATH
48  
49 * `path/target` or `path/query` can be .fa files or directory containing multiple .fa files
50  
51 * Lastz will take the first sequence in target fasta files and all sequences in query fasta files.
52  
53 * For less confusions, each fasta files should contain only one sequence. Use `egaz prepseq` to do
54 this.
55  
56 * Fasta file naming rules: "seqfile.fa" or "seqfile.fa[from,to]"
57  
58 * Lav file naming rules: "[target]vs[query].N.lav"
59  
60 * Predefined parameter sets and scoring matrix can be found in `share/`
61  
62 * [`lastz` help](http://www.bx.psu.edu/~rsharris/lastz/README.lastz-1.04.15.html)
63  
64 * [`--isself`](http://www.bx.psu.edu/~rsharris/lastz/README.lastz-1.04.15.html#ex_self)
65  
66 MARKDOWN
67  
68 1 return $desc;
69 }
70  
71 3 ▶sub validate_args {
Subroutine validate_args: called CC=17 SCAR=48.6
Pod documented
72 3 my ( $self, $opt, $args ) = @_;
73  
74 3 ▶ if ( @{$args} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 2) 2 1 3
75 2 my $message = "This command need two input files/directories.\n\tIt found";
76 2 $message .= sprintf " [%s]", $_ for @{$args};
77 2 $message .= ".\n";
78 2 $self->usage_error($message);
79 }
80 1 for ( @{$args} ) {
81 1 ▶ if ( !( Path::Tiny::path($_)->is_file or Path::Tiny::path($_)->is_dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file or Path::Tiny::path($_)->is_dir) 1 0 1
Condition33%1/3
Path::Tiny::path($_)->is_file or Path::Tiny::path($_)->is_dir
l!l&&r!l&&!r
001
MC/DC0%0/2
Path::Tiny::path($_)->is_file or Path::Tiny::path($_)->is_dir
Path::Tiny::path($_)->is_file Path::Tiny::path($_)->is_dir
Truth table33%1/3
A or B
A= Path::Tiny::path($_)->is_fileB= Path::Tiny::path($_)->is_dir
ABresult
1X1
011
000
82 1 $self->usage_error("The input file/directory [$_] doesn't exist.");
83 }
84 }
85  
86 # load parameter sets
87 0 ▶ if ( $opt->{set} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"set"}) 0 0 0
88 0 print STDERR "* Use parameter set $opt->{set}\n";
89 0 my $yml_file = File::ShareDir::dist_file( 'App-Egaz', 'parameters.yml' );
90 0 my $yml = YAML::Syck::LoadFile($yml_file);
91  
92 0 ▶ if ( !exists $yml->{ $opt->{set} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$yml{$opt->{"set"}}) 0 0 0
93 0 $self->usage_error("--set [$opt->{set}] doesn't exist.");
94 }
95  
96 # Getopt::Long::Descriptive store opts in small cases
97 0 my $para_set = $yml->{ $opt->{set} };
98 0 for my $key ( map {lc} keys %{$para_set} ) {
99 0 ▶ next if $key eq "comment";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "comment" 0 0 0
100 0 ▶ next if defined $opt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{$key} 0 0 0
101 0 $opt->{$key} = $para_set->{ uc $key };
102 }
103 }
104  
105 # scoring matrix
106 0 ▶ if ( $opt->{q} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"q"}) 0 0 0
107 0 ▶ if ( $opt->{q} eq "default" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($opt->{'q'} eq 'default') { } 0 0 0
elsif ($opt->{'q'} eq 'distant') { } 0 0 0
elsif ($opt->{'q'} eq 'similar') { } 0 0 0
elsif ($opt->{'q'} eq 'similar2') { } 0 0 0
elsif (not Path::Tiny::path($opt->{'q'})->is_file) { } 0 0 0
108 0 $opt->{q} = File::ShareDir::dist_file( 'App-Egaz', 'matrix/default' );
109 }
110 elsif ( $opt->{q} eq "distant" ) {
111 0 $opt->{q} = File::ShareDir::dist_file( 'App-Egaz', 'matrix/distant' );
112 }
113 elsif ( $opt->{q} eq "similar" ) {
114 0 $opt->{q} = File::ShareDir::dist_file( 'App-Egaz', 'matrix/similar' );
115 }
116 elsif ( $opt->{q} eq "similar2" ) {
117 0 $opt->{q} = File::ShareDir::dist_file( 'App-Egaz', 'matrix/similar2' );
118 }
119 elsif ( !Path::Tiny::path( $opt->{q} )->is_file ) {
120 0 $self->usage_error("The matrix file [$opt->{q}] doesn't exist.\n");
121 }
122 }
123  
124 }
125  
126 0 ▶sub execute {
Subroutine execute: not called CC=17 SCAR=57.2
Pod documented
127 0 my ( $self, $opt, $args ) = @_;
128  
129 #@type Path::Tiny
130 0 my $outdir = Path::Tiny::path( $opt->{outdir} );
131 0 $outdir->mkpath();
132  
133 #----------------------------#
134 # inputs
135 #----------------------------#
136 0 my ( @t_files, @q_files );
137 0 ▶ if ( $opt->{tp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"tp"}) 0 0 0
138 0 @t_files = File::Find::Rule->file->name(qr{\.fa\[.+\]$})->in( $args->[0] );
139 }
140 0 ▶ if ( !scalar @t_files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @t_files) 0 0 0
141 0 @t_files = File::Find::Rule->file->name('*.fa')->in( $args->[0] );
142 }
143 0 ▶ if ( $opt->{qp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"qp"}) 0 0 0
144 0 @q_files = File::Find::Rule->file->name(qr{\.fa\[.+\]$})->in( $args->[1] );
145 }
146 0 ▶ if ( !scalar @q_files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @q_files) 0 0 0
147 0 @q_files = File::Find::Rule->file->name('*.fa')->in( $args->[1] );
148 }
149 0 printf STDERR "* T files: [%d]; Q files: [%d]\n", scalar @t_files, scalar @q_files;
150  
151 #----------------------------#
152 # lastz
153 #----------------------------#
154 {
155 0 my $lz_opt;
156 0 for my $key (qw{O E Q C T M K L H Y Z}) {
157 0 my $value = $opt->{ lc $key };
158 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
159 0 $lz_opt .= " $key=$value";
160 }
161 }
162 0 ▶ print STDERR $lz_opt if $lz_opt and $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $lz_opt and $opt->{"verbose"} 0 0 0
Condition0%0/3
$lz_opt and $opt->{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$lz_opt and $opt->{"verbose"}
$lz_opt $opt->{"verbose"}
Truth table0%0/3
A and B
A= $lz_optB= $opt->{"verbose"}
ABresult
0X0
100
111
163  
164 0 ▶ my $worker = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
165 0 my ( $self, $chunk_ref, $chunk_id ) = @_;
166  
167 0 my $job = $chunk_ref->[0];
168  
169 0 my ( $target, $query ) = split /\t/, $job;
170  
171 # naming the .lav file
172 # remove .fa or .fa[1,10000]
173 0 my $t_base = Path::Tiny::path($target)->basename;
174 0 $t_base =~ s/\..+?$//;
175 0 my $q_base = Path::Tiny::path($query)->basename;
176 0 $q_base =~ s/\..+?$//;
177  
178 0 my $lav_file;
179 0 my $i = 0;
180 0 while (1) {
181 0 my $file = "[${t_base}]vs[${q_base}].$i.lav";
182 0 $file = $outdir . "/" . $file;
183 0 ▶ if ( !-e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
184 0 $lav_file = $file;
185 0 last;
186 }
187 0 $i++;
188 }
189  
190 0 my $cmd = "lastz $target $query";
191 0 ▶ if ( $opt->{isself} and $target eq $query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"isself"} and $target eq $query) 0 0 0
Condition0%0/3
$opt->{"isself"} and $target eq $query
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"isself"} and $target eq $query
$opt->{"isself"} $target eq $query
Truth table0%0/3
A and B
A= $opt->{"isself"}B= $target eq $query
ABresult
0X0
100
111
192 0 $cmd = "lastz $target --self";
193 }
194 0 ▶ $cmd .= " $lz_opt" if $lz_opt;
Branch: 1 branch
BranchTrueFalseTotal
if $lz_opt 0 0 0
195 0 $cmd .= " > $lav_file";
196  
197 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
198  
199 0 return;
200 0 };
201  
202 # All jobs to be done
203 0 my @jobs;
204 0 ▶ if ( $opt->{paired} ) { # use the most similar chr name
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'paired'}) { } 0 0 0
205 0 for my $t_file ( sort @t_files ) {
206 0 my $t_base = Path::Tiny::path($t_file)->basename;
207 0 my ($q_file) = map { $_->[0] }
208 0 sort { $b->[1] <=> $a->[1] }
209 0 map { [ $_, similarity( Path::Tiny::path($_)->basename, $t_base ) ] } @q_files;
210 0 push @jobs, "$t_file\t$q_file";
211 }
212 }
213 else {
214 0 for my $t_file ( sort @t_files ) {
215 0 for my $q_file ( sort @q_files ) {
216 0 push @jobs, "$t_file\t$q_file";
217 }
218 }
219 }
220  
221 0 my $mce = MCE->new( chunk_size => 1, max_workers => $opt->{parallel}, );
222 0 $mce->foreach( \@jobs, $worker );
223 }
224  
225 #----------------------------#
226 # normalize
227 #----------------------------#
228 0 ▶ if ( $opt->{tp} or $opt->{qp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"tp"} or $opt->{"qp"}) 0 0 0
Condition0%0/3
$opt->{"tp"} or $opt->{"qp"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"tp"} or $opt->{"qp"}
$opt->{"tp"} $opt->{"qp"}
Truth table0%0/3
A or B
A= $opt->{"tp"}B= $opt->{"qp"}
ABresult
1X1
011
000
229 0 my @files = File::Find::Rule->file->name('*.lav')->in( $outdir->stringify );
230 0 printf STDERR "* .lav files: [%d]\n", scalar @files;
231  
232 0 my ( %t_length, %q_length );
233 0 ▶ if ( $opt->{tp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"tp"}) 0 0 0
234 %t_length = %{
235 0 App::RL::Common::read_sizes(
236 Path::Tiny::path( $args->[0], 'chr.sizes' )->stringify
237 )
238 };
239 }
240 0 ▶ if ( $opt->{qp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"qp"}) 0 0 0
241 %q_length = %{
242 0 App::RL::Common::read_sizes(
243 Path::Tiny::path( $args->[1], 'chr.sizes' )->stringify
244 )
245 };
246 }
247  
248 0 my $mce = MCE->new( chunk_size => 1, max_workers => $opt->{parallel}, );
249 $mce->foreach(
250 [ sort @files ],
251 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
252 0 my ( $self, $chunk_ref, $chunk_id ) = @_;
253  
254 0 my $file = $chunk_ref->[0];
255  
256 0 $file =~ /\[(.+?)\]vs\[(.+?)\]/;
257 0 my $t_name = $1;
258 0 my $q_name = $2;
259  
260 0 my $outfile = $file;
261 0 $outfile =~ s/\.lav$/\.norm\.lav/;
262  
263 0 ▶ my $t_len = $opt->{tp} ? $t_length{$t_name} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'tp'} ? : 0 0 0
264 0 ▶ my $q_len = $opt->{qp} ? $q_length{$q_name} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'qp'} ? : 0 0 0
265  
266 0 my $cmd = "egaz normalize" . " --tlen $t_len --qlen $q_len" . " $file -o $outfile";
267  
268 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
269 0 Path::Tiny::path($file)->remove;
270 }
271 0 );
272 }
273  
274 0 return;
275 }
276  
277 19 1;
« Egaz/Command/blastmatch.pm ↑ Summary Egaz/Command/lpcnam.pm »