Egaz/Command/multiz.pm

filter: Statementstmt 21.1% 44 / 208 Branchbran 11.5% 6 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod 100% 6 / 6 totaltotal 24.2% 68 / 281 CC 36 SCAR 54.6
CC
36
Cov
19%
execute
506.0
__ANON__
72.0
validate_args
7.0
CRAP 235.6
↑ Summary Egaz/Command/exactmatch.pm »
1 package App::Egaz::Command::multiz;
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 MCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use MCE::Flow;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use App::Egaz -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use App::Egaz::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
13 2 return 'multiz step by step';
14 }
15  
16 5 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
17 return (
18 5 [ "outdir|o=s", "Output directory", ],
19 [ "tree=s", "a rooted newick tree", ],
20 [ "target=s", "target name, this command can automatically pick one", ],
21 [ "keeptmp", "keep intermediate files", ],
22 [ "parallel|p=i", "number of threads", { default => 2 }, ],
23 { show_defaults => 1, }
24 );
25 }
26  
27 5 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
28 5 return "egaz multiz [options] <maf dir> [more dirs]";
29 }
30  
31 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
32 1 my $desc;
33 1 $desc .= ucfirst(abstract) . ".\n";
34 1 $desc .= <<'MARKDOWN';
35  
36 * <maf dirs> are directories containing multiple .maf or .maf.gz files
37 * `multiz` should be in $PATH
38 * Use a modified [`multiz`](https://github.com/wang-q/multiz) supports gzipped .maf files
39 * [Original `multiz`](https://www.bx.psu.edu/miller_lab/)
40  
41 MARKDOWN
42  
43 1 return $desc;
44 }
45  
46 4 ▶sub validate_args {
Subroutine validate_args: called CC=7 SCAR=19.5
Pod documented
47 4 my ( $self, $opt, $args ) = @_;
48  
49 4 ▶ if ( @{$args} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} < 1) 1 3 4
50 1 my $message = "This command need one or more input directories.\n\tIt found";
51 1 $message .= sprintf " [%s]", $_ for @{$args};
52 1 $message .= ".\n";
53 1 $self->usage_error($message);
54 }
55 3 for ( @{$args} ) {
56 4 ▶ if ( !( Path::Tiny::path($_)->is_dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_dir) 2 2 4
57 2 $self->usage_error("The input directory [$_] doesn't exist.");
58 }
59 }
60  
61 1 ▶ if ( $opt->{tree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"tree"}) 1 0 1
62 1 ▶ if ( !( Path::Tiny::path( $opt->{tree} )->is_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not Path::Tiny::path($opt->{"tree"})->is_file) 1 0 1
63 1 $self->usage_error("The newick tree file [$opt->{tree}] doesn't exist.");
64 }
65 }
66 }
67  
68 0 ▶sub execute {
Subroutine execute: not called CC=22 SCAR=62.3
Pod documented
69 0 my ( $self, $opt, $args ) = @_;
70  
71 #----------------------------#
72 # inputs
73 #----------------------------#
74 0 my $suffix = '.maf';
75 0 my @files = File::Find::Rule->file->name("*$suffix")->in( @{$args} );
76 0 ▶ if ( scalar @files == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @files == 0) 0 0 0
77 0 $suffix = '.maf.gz';
78 0 @files = sort File::Find::Rule->file->name("*$suffix")->in( @{$args} );
79 }
80 0 printf STDERR "* $suffix files: [%d]\n", scalar @files;
81  
82 0 ▶ if ( scalar @files == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @files == 0) 0 0 0
83 0 Carp::croak "Can't find .maf or .maf.gz files\n";
84 }
85  
86 #----------------------------#
87 # Gather species list
88 #----------------------------#
89 #---
90 #Q_aliena:
91 # NC_020152:
92 # - t/Q_rubravsQ_aliena/mafSynNet/NC_020152.synNet.maf.gz
93 #Q_aquifolioides:
94 # NC_020152:
95 # - t/Q_rubravsQ_aquifolioides/mafSynNet/NC_020152.synNet.maf.gz
96 0 my $file_of = {}; # all info here
97 0 my %seen; # count
98 my @potential_targets;
99 0 my @species; # species list gathered from maf files; and then shift target out
100  
101 {
102 0 print STDERR "Get species list\n";
103 0 ▶ my $worker = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 my ( $self, $chunk_ref, $chunk_id ) = @_;
105 0 my $file = $chunk_ref->[0];
106  
107 0 my $cmd
108 = "gzip -dcf $file "
109 . q{ | perl -nl -e '/^s (\w+)/ or next; print $1' }
110 . q{ | sort | uniq};
111 0 my @list = grep { defined $_ } split /\n/, `$cmd`;
112 0 ▶ if ( @list > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list > 2) 0 0 0
113 0 Carp::croak "There are three or more species in [$file].\n"
114 . YAML::Syck::Dump( \@list );
115 }
116  
117 0 MCE->gather( $file, [@list] );
118 0 };
119 MCE::Flow::init {
120 chunk_size => 1,
121 max_workers => $opt->{parallel},
122 0 };
123 0 my %list_of = mce_flow $worker, \@files;
124 0 MCE::Flow::finish;
125  
126 0 print STDERR "Assign files to species\n";
127 0 for my $file (@files) {
128 0 my @list = @{ $list_of{$file} };
129 0 $seen{$_}++ for @list;
130  
131 0 my $chr_name
132 = Path::Tiny::path($file)->basename( ".net$suffix", ".synNet$suffix", $suffix );
133 0 for my $sp (@list) {
134 0 ▶ if ( exists $file_of->{$sp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $file_of->{$sp}) { } 0 0 0
135 0 ▶ if ( exists $file_of->{$sp}{$chr_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $file_of->{$sp}{$chr_name}) { } 0 0 0
136 0 push @potential_targets, $sp;
137 0 push @{ $file_of->{$sp}{$chr_name} }, $file;
138 }
139 else {
140 0 $file_of->{$sp}{$chr_name} = [$file];
141 }
142 0 $file_of->{$sp}{chr_set}->insert($chr_name);
143 }
144 else {
145 0 my $chr_set = Set::Scalar->new;
146 0 $chr_set->insert($chr_name);
147 0 $file_of->{$sp} = { $chr_name => [$file], chr_set => $chr_set };
148 }
149 }
150 }
151  
152 0 @potential_targets = List::MoreUtils::PP::uniq(@potential_targets);
153 0 (@species) = map { $_->[0] }
154 0 sort { $b->[1] <=> $a->[1] }
155 0 map { [ $_, $seen{$_} ] }
156 keys %seen;
157 }
158  
159 #----------------------------#
160 # check number of species
161 #----------------------------#
162 0 ▶ if ( scalar @species < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @species < 3) 0 0 0
163 0 print STDERR "There're too few species, [@species].\n";
164 0 print STDERR "Can't run multiz.\n";
165  
166 0 ▶ if ( !$opt->{outdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"outdir"}) 0 0 0
167 0 ▶ if ( $opt->{target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'target'}) { } 0 0 0
168 0 $opt->{outdir} = $opt->{target} . "_n2";
169 }
170 else {
171 0 $opt->{outdir} = $species[0] . "_n2";
172 }
173 }
174  
175 0 Path::Tiny::path( $opt->{outdir} )->mkpath();
176 0 print STDERR "Just copy pairwise maf to [$opt->{outdir}]\n";
177  
178 0 for my $file (@files) {
179 0 Path::Tiny::path($file)->copy( $opt->{outdir} );
180 }
181  
182 0 return;
183 }
184  
185 #----------------------------#
186 # check target
187 #----------------------------#
188 0 ▶ if ( @potential_targets > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@potential_targets > 1) { } 0 0 0
189 0 Carp::croak "There are more than 1 potential targets\n"
190 . YAML::Syck::Dump( \@potential_targets );
191 }
192 else {
193 0 ▶ if ( !$opt->{target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{'target'}) { } 0 0 0
194 0 $opt->{target} = shift @species;
195 0 ▶ if ( $opt->{target} eq $potential_targets[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'target'} eq $potential_targets[0]) { } 0 0 0
196 printf STDERR "%s appears %d times, use it as target.\n", $opt->{target},
197 0 $seen{ $opt->{target} };
198 }
199 else {
200 0 Carp::croak "Can't find target.\n";
201 }
202 }
203 else {
204 0 my ($dummy) = grep { $_ eq $opt->{target} } @species;
205 0 ▶ if ( !defined $dummy ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $dummy) { } 0 0 0
elsif ($opt->{'target'} eq $potential_targets[0]) { } 0 0 0
206 0 Carp::croak "Can't find target_name [$opt->{target}] in .maf files.\n";
207 }
208 elsif ( $opt->{target} eq $potential_targets[0] ) {
209 0 print STDERR "Assigned target [$opt->{target}] is OK\n";
210 0 @species = grep { $_ ne $opt->{target} } @species;
211 }
212 else {
213 0 Carp::croak "Assigned target [$opt->{target}] isn't OK.\n"
214 . "It should be [$potential_targets[0]].\n";
215 }
216 }
217 }
218  
219 #----------------------------#
220 # Find chromosomes
221 #----------------------------#
222 0 my @chrs = sort $file_of->{ $opt->{target} }{chr_set}->members; # all target chromosomes
223 {
224 0 print STDERR "Target chromosomes are [@chrs]\n";
225  
226 # check other species occurrence number
227 0 my @occurrence = sort { $b <=> $a } List::MoreUtils::PP::uniq( @seen{@species} );
228 0 ▶ if ( @occurrence > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@occurrence > 1) 0 0 0
229 0 print STDERR "Species occurrence number inconsistency [@occurrence]\n";
230 0 print STDERR "We will skip some chromosomes\n";
231 0 print STDERR YAML::Syck::Dump \%seen;
232 0 print STDERR "\n";
233  
234 0 my $intersect_chr_set = $file_of->{ $opt->{target} }{chr_set}->clone;
235 0 for my $sp ( keys %{$file_of} ) {
236 0 $intersect_chr_set = $intersect_chr_set->intersection( $file_of->{$sp}{chr_set} );
237 }
238 0 @chrs = sort $intersect_chr_set->members;
239 0 print STDERR "Chromosomes to be processed are [@chrs]\n";
240 }
241  
242 # sort @species by distances in tree
243 0 ▶ if ( $opt->{tree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"tree"}) 0 0 0
244 0 my $ladder = App::Egaz::Common::ladder( $opt->{tree}, $opt->{target} );
245 0 ▶ my @orders = map { ref eq 'ARRAY' ? @$_ : $_ } @{$ladder};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
246 0 my %item_order = map { $orders[$_] => $_ } 0 .. $#orders;
247  
248 0 @species = map { $_->[0] }
249 0 sort { $a->[1] <=> $b->[1] }
250 0 map { [ $_, $item_order{$_} ] } @species;
251 }
252  
253 0 print STDERR "Order of stitch [@species]\n";
254 }
255  
256 0 ▶ if ( !$opt->{outdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"outdir"}) 0 0 0
257 0 $opt->{outdir} = $opt->{target} . "_n" . ( scalar(@species) + 1 );
258 0 print STDERR "--outdir set to [$opt->{outdir}]\n";
259 }
260 0 Path::Tiny::path( $opt->{outdir} )->mkpath();
261  
262 YAML::Syck::DumpFile(
263 0 Path::Tiny::path( $opt->{outdir}, 'info.yml' )->stringify,
264 { file_of => $file_of,
265 chrs => \@chrs,
266 }
267 );
268  
269 #----------------------------#
270 # Finally, multiz comes
271 #----------------------------#
272 0 ▶ my $worker = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
273 0 my ( $self, $chunk_ref, $chunk_id ) = @_;
274  
275 0 my $chr_name = $chunk_ref->[0];
276  
277 # multiz.v11.2: -- aligning two files of alignment blocks where top rows are
278 # always the reference, reference in both files cannot have duplicats
279 # args: [R=?] [M=?] file1 file2 v? [out1 out2] [nohead] [all]
280 # R(30) radius in dynamic programming.
281 # M(1) minimum output width.
282 # out1 out2(null) null: stdout; out1 out2: file names for collecting unused input.
283 # nohead(null) null: output maf header; nohead: not to output maf header.
284 # all(null) null: not to output single-row blocks; all: output all blocks.
285 #
286 # multiz t/Q_rubravsQ_aliena/mafSynNet/NC_020152.synNet.maf.gz \
287 # t/Q_rubravsQ_aquifolioides/mafSynNet/NC_020152.synNet.maf.gz \
288 # 1 out1 out2 > step1.NC_020152.maf
289 # multiz step1.chr1.maf t/Q_rubravsQ_baronii/mafSynNet/NC_020152.synNet.maf.gz \
290 # 1 out1 out2 > step2.NC_020152.maf
291  
292 0 my @species_copy = @species;
293 0 my ( $species1, $species2 );
294 0 my $maf_step;
295 0 my $step = 1;
296 0 my $str = '';
297 0 ▶ while (@species_copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (@species_copy) 0 0 0
298 0 my ( $maf1, $maf2 );
299  
300 0 ▶ if ( !defined $species1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $species1) { } 0 0 0
301 0 $species1 = shift @species_copy;
302 0 $maf1 = $file_of->{$species1}{$chr_name}[0];
303 }
304 else {
305 0 $maf1 = $maf_step;
306 }
307 0 ▶ if ( !defined $species2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $species2) 0 0 0
308 0 $species2 = shift @species_copy;
309 0 $maf2 = $file_of->{$species2}{$chr_name}[0];
310 }
311  
312 0 my $out1 = "$opt->{outdir}/$chr_name.out1";
313 0 my $out2 = "$opt->{outdir}/$chr_name.out2";
314  
315 0 ▶ $maf_step
Branch: 1 branch
BranchTrueFalseTotal
@species_copy ? : 0 0 0
316 = @species_copy
317 ? "$opt->{outdir}/$chr_name.step$step.maf"
318 : "$opt->{outdir}/$chr_name.maf";
319  
320 # here we set out1 and out2 to discard unused synteny
321 # Omit out1 and out2, unused synteny will be printed to stdout and
322 # reused by following multiz processes
323 0 my $cmd
324 = "multiz" . " M=10"
325 . " $maf1"
326 . " $maf2" . " 1 "
327 . " $out1"
328 . " $out2"
329 . " > $maf_step";
330 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => 1, } );
331 0 print STDERR "Step [$step] .maf file generated.\n";
332  
333 0 $str .= "$chr_name.step$step,";
334 0 $str .= "$species1,$species2,";
335 0 for my $file ( $maf1, $maf2, $out1, $out2, $maf_step ) {
336 0 $str .= Number::Format::format_bytes( -s $file, base => 1000 );
337 0 $str .= ",";
338 }
339 0 $str .= Number::Format::format_bytes( ( -s $maf_step ) / ( $step + 2 ), base => 1000 );
340 0 $str .= "\n";
341  
342 0 $species1 = "step$step";
343 0 $species2 = undef;
344 0 $step++;
345 }
346  
347 0 ▶ if ( !$opt->{keeptmp} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"keeptmp"}) 0 0 0
348 0 Path::Tiny::path( $opt->{outdir}, "$chr_name.out1" )->remove;
349 0 Path::Tiny::path( $opt->{outdir}, "$chr_name.out2" )->remove;
350 0 for ( Path::Tiny::path( $opt->{outdir} )->children(qr/^$chr_name\.step/) ) {
351 0 $_->remove;
352 }
353 }
354  
355 0 my $cmd = "gzip " . "$opt->{outdir}/$chr_name.maf";
356 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => 1, } );
357  
358 0 print STDERR $str;
359 0 Path::Tiny::path( $opt->{outdir}, "$chr_name.temp.csv" )->remove;
360 0 Path::Tiny::path( $opt->{outdir}, "$chr_name.temp.csv" )->spew($str);
361  
362 0 return;
363 0 };
364  
365 0 my $mce = MCE->new( chunk_size => 1, max_workers => $opt->{parallel}, );
366 0 $mce->foreach( \@chrs, $worker );
367  
368 #----------------------------#
369 # Summary
370 #----------------------------#
371 {
372 0 my $cmd = "echo 'step,spe1,spe2,maf1,maf2,out1,out2,size,per_size'"
373 . " > $opt->{outdir}/steps.csv";
374 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => 1, } );
375  
376 0 $cmd
377 = "find $opt->{outdir} -type f -name '*.temp.csv'"
378 . " | sort -n"
379 . " | xargs cat >> $opt->{outdir}/steps.csv";
380 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => 1, } );
381  
382 0 $cmd = "find $opt->{outdir} -type f -name '*.temp.csv'" . " | xargs rm";
383 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => 1, } );
384 }
385  
386 0 return;
387 }
388  
389 19 1;
↑ Summary Egaz/Command/exactmatch.pm »