Egaz/Command/exactmatch.pm

filter: Statementstmt 37.6% 35 / 93 Branchbran 9.3% 3 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 6 / 6 totaltotal 36.7% 54 / 147 CC 23 SCAR 53.9
CC
23
Cov
30%
execute
380.0
validate_args
5.0
BEGIN
1.0
CRAP 219.8
« Egaz/Command/multiz.pm ↑ Summary Egaz/Command/blastmatch.pm »
1 package App::Egaz::Command::exactmatch;
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 'exact matched positions in genome sequences';
11 }
12  
13 4 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
14 return (
15 4 [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ],
16 [ 'length|l=i', 'match length of mummer', { default => 100 }, ],
17 [ 'discard=i', 'discard blocks with copy number larger than this', { default => 0 }, ],
18 [ 'debug', 'write a file of exactmatch.json for debugging', ],
19 { show_defaults => 1, }
20 );
21 }
22  
23 4 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
24 4 return "egaz exactmatch [options] <infile> <genome.fa>";
25 }
26  
27 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
28 1 my $desc;
29 1 $desc .= ucfirst(abstract) . ".\n";
30 1 $desc .= <<'MARKDOWN';
31  
32 * <infile> can't be stdin
33 * `mummer` or `sparsemem` should be in $PATH
34  
35 MARKDOWN
36  
37 1 return $desc;
38 }
39  
40 3 ▶sub validate_args {
Subroutine validate_args: called CC=5 SCAR=16.1
Pod documented
41 3 my ( $self, $opt, $args ) = @_;
42  
43 3 ▶ if ( @{$args} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 2) 1 2 3
44 1 my $message = "This command need two input files.\n\tIt found";
45 1 $message .= sprintf " [%s]", $_ for @{$args};
46 1 $message .= ".\n";
47 1 $self->usage_error($message);
48 }
49 2 for ( @{$args} ) {
50 2 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 2 0 2
51 2 $self->usage_error("The input file [$_] doesn't exist.");
52 }
53 }
54  
55 }
56  
57 0 ▶sub execute {
Subroutine execute: not called CC=19 SCAR=59.4
Pod documented
58 0 my ( $self, $opt, $args ) = @_;
59  
60 #----------------------------#
61 # Run sparsemem
62 #----------------------------#
63 #@type Path::Tiny
64 0 my $mem_result = Path::Tiny->tempfile;
65 0 App::Egaz::Common::run_sparsemem( $mem_result, $args->[0], $args->[1], $opt->{length} );
66  
67 #----------------------------#
68 # Parse sparsemem
69 #----------------------------#
70 0 tie my %match_of, "Tie::IxHash";
71 0 my $fh_mem = $mem_result->openr;
72 0 my $cur_name;
73 0 ▶ while ( my $line = <$fh_mem> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh_mem)) 0 0 0
74 0 ▶ if ( $line =~ /^\>/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\>/) { } 0 0 0
elsif ($line =~ /^\s*[\w-]+/) { } 0 0 0
75 0 $line =~ s/^\>\s*//;
76 0 chomp $line;
77 0 $cur_name = $line;
78 }
79 elsif ( $line =~ /^\s*[\w-]+/ ) {
80 0 chomp $line;
81  
82 # XII 1065146 1 6369
83 # ID
84 # the position in the reference sequence
85 # the position in the query sequence
86 # the length of the match
87 0 my @fields = grep {/\S+/} split /\s+/, $line;
88  
89 0 ▶ if ( @fields != 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields != 4) 0 0 0
90 0 warn " Line [$line] seems wrong.\n";
91 0 next;
92 }
93  
94 0 ▶ if ( !exists $match_of{$cur_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $match_of{$cur_name}) 0 0 0
95 0 $match_of{$cur_name} = [];
96 }
97 0 push @{ $match_of{$cur_name} }, \@fields;
98 }
99 else { # Blank line, do nothing
100 }
101 }
102 0 close $fh_mem;
103  
104 #----------------------------#
105 # Get exact matches
106 #----------------------------#
107 0 my $size_of = App::Egaz::Common::get_size( $args->[0]);
108 0 tie my %locations, "Tie::IxHash"; # genome location - simple location
109 0 for my $seq_name ( keys %match_of ) {
110 0 my $ori_name = $seq_name;
111 0 my $chr_strand = "+";
112 0 ▶ if ( $ori_name =~ / Reverse/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ori_name =~ / Reverse/) 0 0 0
113 0 $ori_name =~ s/ Reverse//;
114 0 $chr_strand = "-";
115 }
116 0 my $species_name = App::RL::Common::decode_header($ori_name)->{name};
117  
118 0 for my $f ( @{ $match_of{$seq_name} } ) {
119 0 ▶ next unless $f->[2] == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->[2] == 1 0 0 0
120 0 ▶ next unless $f->[3] == $size_of->{$ori_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $f->[3] == $size_of->{$ori_name} 0 0 0
121  
122 0 my $header = App::RL::Common::encode_header(
123 { name => $species_name,
124 chr => $f->[0],
125 start => $f->[1],
126 end => $f->[1] + $f->[3] - 1,
127 strand => $chr_strand,
128 }
129 );
130 0 ▶ if ( !exists $locations{$ori_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $locations{$ori_name}) 0 0 0
131 0 $locations{$ori_name} = {};
132 }
133  
134 0 $locations{$ori_name}->{$header} = 1;
135 }
136 }
137  
138 #----------------------------#
139 # Write replace tsv file
140 #----------------------------#
141 0 my $out_fh;
142 0 ▶ if ( lc( $opt->{outfile} ) eq "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{'outfile'} eq 'stdout') { } 0 0 0
143 0 $out_fh = *STDOUT{IO};
144 }
145 else {
146 0 open $out_fh, ">", $opt->{outfile};
147 }
148  
149 0 for my $ori_name ( keys %locations ) {
150 0 my @matches = keys %{ $locations{$ori_name} };
151 0 ▶ if ( $opt->{discard} and @matches > $opt->{discard} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt->{'discard'} and @matches > $opt->{'discard'}) { } 0 0 0
elsif (@matches > 1) { } 0 0 0
elsif ($ori_name ne $matches[0]) { } 0 0 0
Condition0%0/3
$opt->{"discard"} and @matches > $opt->{"discard"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"discard"} and @matches > $opt->{"discard"}
$opt->{"discard"} @matches > $opt->{"discard"}
Truth table0%0/3
A and B
A= $opt->{"discard"}B= @matches > $opt->{"discard"}
ABresult
0X0
100
111
152 0 printf STDERR " %s\tgot %d matches, discard it\n", $ori_name, scalar(@matches);
153 0 print {$out_fh} "$ori_name\n";
154 }
155 elsif ( @matches > 1 ) {
156 0 printf STDERR " %s\tgot %d matches\n", $ori_name, scalar(@matches);
157 0 print {$out_fh} join( "\t", $ori_name, @matches ) . "\n";
158 }
159 elsif ( $ori_name ne $matches[0] ) {
160 0 printf STDERR " %s\tgot wrong position, %s\n", $ori_name, $matches[0];
161 0 print {$out_fh} join( "\t", $ori_name, @matches ) . "\n";
162 }
163 }
164  
165 #----------------------------#
166 # Dump exactmatch.json
167 #----------------------------#
168 # YAML::Syck can't Dump Tie::IxHash
169 0 ▶ if ($opt->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"debug"}) 0 0 0
170 0 Path::Tiny::path("exactmatch.json")->spew(
171 JSON::to_json(
172 { mem_result => $mem_result->slurp,
173 match_of => \%match_of,
174 locations => \%locations
175 },
176 { utf8 => 1, pretty => 1 },
177 )
178 );
179 }
180 }
181  
182 19 1;
« Egaz/Command/multiz.pm ↑ Summary Egaz/Command/blastmatch.pm »