| 1 | package App::Egaz::Command::blastmatch; | ||||||||||||||
| 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 'matched positions by blastn in genome sequences'; | |||||||||||||
| 14 | } | ||||||||||||||
| 15 | |||||||||||||||
| 16 | 3 | ▶ | sub opt_spec { | ||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 17 | return ( | ||||||||||||||
| 18 | 3 | [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ], | |||||||||||||
| 19 | [ "perchr", "one (fake) runlist per chromosome", ], | ||||||||||||||
| 20 | [ "coverage|c=f", "coverage of identical matches", { default => 0.9 }, ], | ||||||||||||||
| 21 | [ "batch=i", "batch size of blast records", { default => 500000 }, ], | ||||||||||||||
| 22 | [ "parallel|p=i", "number of threads", { default => 2 }, ], | ||||||||||||||
| 23 | [ "verbose|v", "verbose mode", ], | ||||||||||||||
| 24 | { show_defaults => 1, } | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 3 | ▶ | sub usage_desc { | ||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 29 | 3 | return "egaz blastmatch [options] <infile>"; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 1 | ▶ | sub description { | ||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 33 | 1 | my $desc; | |||||||||||||
| 34 | 1 | $desc .= ucfirst(abstract) . ".\n"; | |||||||||||||
| 35 | 1 | $desc .= <<'MARKDOWN'; | |||||||||||||
| 36 | |||||||||||||||
| 37 | * <infile> is reports produced by `egaz blastn` | ||||||||||||||
| 38 | * <infile> can't be stdin | ||||||||||||||
| 39 | * --perchr produces fake runlists, there might be overlaps, e.g. I:17221-25234,21428-25459 | ||||||||||||||
| 40 | |||||||||||||||
| 41 | MARKDOWN | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 1 | return $desc; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 2 | ▶ | sub validate_args { | ||||||||||||
|
Subroutine
validate_args: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 47 | 2 | my ( $self, $opt, $args ) = @_; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 2 | ▶ | if ( @{$args} != 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 1 | my $message = "This command need one input file.\n\tIt found"; | |||||||||||||
| 51 | 1 | $message .= sprintf " [%s]", $_ for @{$args}; | |||||||||||||
| 52 | 1 | $message .= ".\n"; | |||||||||||||
| 53 | 1 | $self->usage_error($message); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | 1 | for ( @{$args} ) { | |||||||||||||
| 56 | 1 | ▶ | if ( !Path::Tiny::path($_)->is_file ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 1 | $self->usage_error("The input file [$_] doesn't exist."); | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | ▶ | sub execute { | ||||||||||||
|
Subroutine
execute: not called CC=20 SCAR=60.4
Pod
documented
| |||||||||||||||
| 64 | 0 | my ( $self, $opt, $args ) = @_; | |||||||||||||
| 65 | |||||||||||||||
| 66 | #----------------------------# | ||||||||||||||
| 67 | # Parse reports | ||||||||||||||
| 68 | #----------------------------# | ||||||||||||||
| 69 | 0 | print STDERR "Parse reports\n"; | |||||||||||||
| 70 | 0 | ▶ | my $worker = sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||
| 71 | 0 | my ( $self, $chunk_ref, $chunk_id ) = @_; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | my $wid = MCE->wid; | |||||||||||||
| 74 | 0 | ▶ | print STDERR "* Process task [$chunk_id] by worker #$wid\n" if $opt->{verbose}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | my @lines = @{$chunk_ref}; | |||||||||||||
| 77 | 0 | my %heads; | |||||||||||||
| 78 | 0 | for my $line (@lines) { | |||||||||||||
| 79 | 0 | ▶ | next if $line =~ /^#/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 0 | chomp $line; | |||||||||||||
| 81 | |||||||||||||||
| 82 | # qseqid sseqid qstart qend sstart send qlen slen nident | ||||||||||||||
| 83 | 0 | my @fields = grep {defined} split /\s+/, $line; | |||||||||||||
| 84 | 0 | ▶ | if ( @fields != 9 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | print "Fields error: $line\n"; | |||||||||||||
| 86 | 0 | next; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | my $query_name = $fields[0]; | |||||||||||||
| 90 | 0 | my $hit_name = $fields[1]; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | my $query_length = $fields[6]; | |||||||||||||
| 93 | 0 | my $identical_match = $fields[8]; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | my ( $h_start, $h_end ) = ( $fields[4], $fields[5] ); | |||||||||||||
| 96 | 0 | ▶ | if ( $h_start > $h_end ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 0 | ( $h_start, $h_end ) = ( $h_end, $h_start ); | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | my $query_coverage = $identical_match / $query_length; | |||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | ▶ | if ( $query_coverage >= $opt->{coverage} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | my $head = "$hit_name(+):$h_start-$h_end"; | |||||||||||||
| 104 | 0 | $heads{$head}++; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | ▶ | printf STDERR " " x 4 . "Gather %d pieces\n", scalar keys %heads if $opt->{verbose}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | printf STDERR " " x 4 . "Last query name is %s\n", ( split /\s+/, $lines[-1] )[0] | ||||||||||||||
| 110 | 0 | ▶ | if $opt->{verbose}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 0 | MCE->gather(%heads); | |||||||||||||
| 112 | 0 | }; | |||||||||||||
| 113 | |||||||||||||||
| 114 | MCE::Flow::init { | ||||||||||||||
| 115 | chunk_size => $opt->{batch}, | ||||||||||||||
| 116 | max_workers => $opt->{parallel}, | ||||||||||||||
| 117 | 0 | }; | |||||||||||||
| 118 | 0 | my %ranges = mce_flow_f $worker, $args->[0]; | |||||||||||||
| 119 | 0 | MCE::Flow::finish; | |||||||||||||
| 120 | |||||||||||||||
| 121 | #----------------------------# | ||||||||||||||
| 122 | # Remove ranges fully contained by others | ||||||||||||||
| 123 | #----------------------------# | ||||||||||||||
| 124 | 0 | print STDERR "Convert nodes to sets\n"; | |||||||||||||
| 125 | 0 | my $nodes_of_chr = {}; | |||||||||||||
| 126 | 0 | my %set_of; | |||||||||||||
| 127 | 0 | for my $node ( keys %ranges ) { | |||||||||||||
| 128 | 0 | my ( $chr, $set, undef ) = string_to_set($node); | |||||||||||||
| 129 | 0 | ▶ | if ( !exists $nodes_of_chr->{$chr} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | $nodes_of_chr->{$chr} = []; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | 0 | push @{ $nodes_of_chr->{$chr} }, $node; | |||||||||||||
| 133 | 0 | $set_of{$node} = $set; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | print STDERR "Sort by positions at chromosomes\n"; | |||||||||||||
| 137 | |||||||||||||||
| 138 | # end positions | ||||||||||||||
| 139 | 0 | for my $chr ( sort keys %{$nodes_of_chr} ) { | |||||||||||||
| 140 | 0 | my @temps = map { $_->[0] } | |||||||||||||
| 141 | 0 | sort { $a->[1] <=> $b->[1] } | |||||||||||||
| 142 | 0 | map { /\:(?:\d+)\-(\d+)/; [ $_, $1 ] } @{ $nodes_of_chr->{$chr} }; | |||||||||||||
| 143 | 0 | $nodes_of_chr->{$chr} = \@temps; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | # start positions | ||||||||||||||
| 147 | 0 | for my $chr ( sort keys %{$nodes_of_chr} ) { | |||||||||||||
| 148 | 0 | my @temps = map { $_->[0] } | |||||||||||||
| 149 | 0 | sort { $a->[1] <=> $b->[1] } | |||||||||||||
| 150 | 0 | map { /\:(\d+)/; [ $_, $1 ] } @{ $nodes_of_chr->{$chr} }; | |||||||||||||
| 151 | 0 | $nodes_of_chr->{$chr} = \@temps; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | |||||||||||||||
| 154 | 0 | print STDERR "Overlaps between ranges\n"; | |||||||||||||
| 155 | 0 | my %to_remove; | |||||||||||||
| 156 | 0 | for my $chr ( sort keys %{$nodes_of_chr} ) { | |||||||||||||
| 157 | 0 | my @nodes = @{ $nodes_of_chr->{$chr} }; | |||||||||||||
| 158 | |||||||||||||||
| 159 | 0 | my $vicinity = 10; | |||||||||||||
| 160 | 0 | for my $idx ( 0 .. $#nodes - $vicinity ) { | |||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | for my $i ( 0 .. $vicinity - 1 ) { | |||||||||||||
| 163 | 0 | for my $j ( $i .. $vicinity - 1 ) { | |||||||||||||
| 164 | 0 | my $node_i = $nodes[ $idx + $i ]; | |||||||||||||
| 165 | 0 | my $set_i = $set_of{$node_i}; | |||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | my $node_j = $nodes[ $idx + $j ]; | |||||||||||||
| 168 | 0 | my $set_j = $set_of{$node_j}; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 0 | ▶ | if ( $set_i->larger_than($set_j) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 171 | 0 | $to_remove{$node_j}++; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | elsif ( $set_j->larger_than($set_i) ) { | ||||||||||||||
| 174 | 0 | $to_remove{$node_i}++; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | } | ||||||||||||||
| 178 | } | ||||||||||||||
| 179 | } | ||||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 0 | printf STDERR " " x 4 . "Remove [%d] nested nodes\n", scalar keys %to_remove; | |||||||||||||
| 183 | 0 | my @sorted; | |||||||||||||
| 184 | 0 | for my $chr ( sort keys %{$nodes_of_chr} ) { | |||||||||||||
| 185 | 0 | for my $node ( @{ $nodes_of_chr->{$chr} } ) { | |||||||||||||
| 186 | 0 | ▶ | if ( !exists $to_remove{$node} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 187 | 0 | push @sorted, $node; | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | } | ||||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | #----------------------------# | ||||||||||||||
| 193 | # Write outputs | ||||||||||||||
| 194 | #----------------------------# | ||||||||||||||
| 195 | 0 | my $out_fh; | |||||||||||||
| 196 | 0 | ▶ | if ( lc( $opt->{outfile} ) eq "stdout" ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | 0 | $out_fh = *STDOUT{IO}; | |||||||||||||
| 198 | } | ||||||||||||||
| 199 | else { | ||||||||||||||
| 200 | 0 | open $out_fh, ">", $opt->{outfile}; | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | |||||||||||||||
| 203 | 0 | ▶ | if ( $opt->{perchr} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 204 | 0 | my $runlist_of; | |||||||||||||
| 205 | 0 | for my $node (@sorted) { | |||||||||||||
| 206 | 0 | my ( $chr, $set, undef ) = string_to_set($node); | |||||||||||||
| 207 | 0 | ▶ | if ( !exists $runlist_of->{$chr} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 208 | 0 | $runlist_of->{$chr} = []; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | 0 | push @{ $runlist_of->{$chr} }, $set->runlist; | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | 0 | for my $chr ( sort keys %{$runlist_of} ) { | |||||||||||||
| 214 | 0 | my $range = "$chr:" . join( ",", @{ $runlist_of->{$chr} } ); | |||||||||||||
| 215 | 0 | print {$out_fh} $range . "\n"; | |||||||||||||
| 216 | } | ||||||||||||||
| 217 | } | ||||||||||||||
| 218 | else { | ||||||||||||||
| 219 | 0 | for my $node (@sorted) { | |||||||||||||
| 220 | 0 | my ( $chr, $set, undef ) = string_to_set($node); | |||||||||||||
| 221 | 0 | my $range = "$chr:" . $set->runlist; | |||||||||||||
| 222 | 0 | print {$out_fh} $range . "\n"; | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | } | ||||||||||||||
| 225 | |||||||||||||||
| 226 | 0 | close $out_fh; | |||||||||||||
| 227 | } | ||||||||||||||
| 228 | |||||||||||||||
| 229 | 0 | ▶ | sub string_to_set { | ||||||||||||
|
Subroutine
string_to_set: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 230 | 0 | my $node = shift; | |||||||||||||
| 231 | |||||||||||||||
| 232 | 0 | my ( $chr_part, $runlist ) = split /:/, $node; | |||||||||||||
| 233 | |||||||||||||||
| 234 | 0 | my $head_qr = qr{ | |||||||||||||
| 235 | (?:(?P<name>[\w_]+)\.)? | ||||||||||||||
| 236 | (?P<chr_name>[\w-]+) | ||||||||||||||
| 237 | (?:\((?P<chr_strand>.+)\))? | ||||||||||||||
| 238 | }xi; | ||||||||||||||
| 239 | 0 | $chr_part =~ $head_qr; | |||||||||||||
| 240 | |||||||||||||||
| 241 | 0 | my $chr = $+{chr_name}; | |||||||||||||
| 242 | 0 | my $strand = "+"; | |||||||||||||
| 243 | 0 | ▶ | if ( defined $+{chr_strand} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 244 | 0 | $strand = $+{chr_strand}; | |||||||||||||
| 245 | } | ||||||||||||||
| 246 | 0 | my $set = AlignDB::IntSpan->new($runlist); | |||||||||||||
| 247 | |||||||||||||||
| 248 | 0 | return ( $chr, $set, $strand ); | |||||||||||||
| 249 | } | ||||||||||||||
| 250 | |||||||||||||||
| 251 | 19 | 1; | |||||||||||||