| 1 | package App::Egaz::Command::lav2psl; | ||||||||||||||||||||||||||||
| 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 'convert .lav files to .psl'; | |||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 4 | return ( [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ], | |||||||||||||||||||||||||||
| 15 | { show_defaults => 1, } ); | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub usage_desc { | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 4 | return "egaz lav2psl [options] <infile>"; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | sub description { | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 1 | my $desc; | |||||||||||||||||||||||||||
| 24 | 1 | $desc .= ucfirst(abstract) . ".\n"; | |||||||||||||||||||||||||||
| 25 | 1 | $desc .= <<MARKDOWN; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | * infile == stdin means reading from STDIN | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | MARKDOWN | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | return $desc; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 3 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | if ( @{$args} != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | my $message = "This command need one input file.\n\tIt found"; | |||||||||||||||||||||||||||
| 39 | 1 | $message .= sprintf " [%s]", $_ for @{$args}; | |||||||||||||||||||||||||||
| 40 | 1 | $message .= ".\n"; | |||||||||||||||||||||||||||
| 41 | 1 | $self->usage_error($message); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 2 | for ( @{$args} ) { | |||||||||||||||||||||||||||
| 44 | 2 | ▶ | next if lc $_ eq "stdin"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if ( !Path::Tiny::path($_)->is_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | $self->usage_error("The input file [$_] doesn't exist."); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=24 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 1 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #----------------------------# | ||||||||||||||||||||||||||||
| 56 | # load lav | ||||||||||||||||||||||||||||
| 57 | #----------------------------# | ||||||||||||||||||||||||||||
| 58 | 1 | my $lav_content = Path::Tiny::path( $args->[0] )->slurp; | |||||||||||||||||||||||||||
| 59 | 1 | my @lavs = grep {/^[ds] /} split /\#\:lav\n/, $lav_content; | |||||||||||||||||||||||||||
| 60 | 1 | my $d_stanza = shift @lavs; # Not needed by this program | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # print YAML::Syck::Dump \@lavs; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #----------------------------# | ||||||||||||||||||||||||||||
| 65 | # write outputs | ||||||||||||||||||||||||||||
| 66 | #----------------------------# | ||||||||||||||||||||||||||||
| 67 | 1 | my $out_fh; | |||||||||||||||||||||||||||
| 68 | 1 | ▶ | if ( lc( $opt->{outfile} ) eq "stdout" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | $out_fh = *STDOUT{IO}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||
| 72 | 0 | open $out_fh, ">", $opt->{outfile}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | for my $lav (@lavs) { | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #----------------------------# | ||||||||||||||||||||||||||||
| 78 | # s-stanza | ||||||||||||||||||||||||||||
| 79 | #----------------------------# | ||||||||||||||||||||||||||||
| 80 | # "<filename>[-]" <start> <stop> [<rev_comp_flag> <sequence_number>] | ||||||||||||||||||||||||||||
| 81 | 4 | $lav =~ /s \{\s+(.+?)\s+\}/s; | |||||||||||||||||||||||||||
| 82 | 4 | my $s_stanza = $1; | |||||||||||||||||||||||||||
| 83 | 4 | my @s_lines = $s_stanza =~ /(.+ \s+ \d+ \s+ \d+ \s+ \d+ \s+ \d+)/gx; | |||||||||||||||||||||||||||
| 84 | 4 | ▶ | if ( scalar @s_lines != 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | Carp::croak "s-stanza error.\n"; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | $s_lines[0] =~ /\s*\"?(.+?)\-?\"? \s+ (\d+) \s+ (\d+) \s+ (\d+) \s+ (\d+)/x; | |||||||||||||||||||||||||||
| 89 | 4 | my ( $t_file, $t_seq_start, $t_seq_stop, $t_strand, $t_contig ) = ( $1, $2, $3, $4, $5 ); | |||||||||||||||||||||||||||
| 90 | 4 | ▶ | if ( $t_seq_start != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | Carp::croak "Target sequence doesn't start at 1\n"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | $s_lines[1] =~ /\s*\"?(.+?)\-?\"? \s+ (\d+) \s+ (\d+) \s+ (\d+) \s+ (\d+)/x; | |||||||||||||||||||||||||||
| 95 | 4 | my ( $q_file, $q_seq_start, $q_seq_stop, $q_strand, $q_contig ) = ( $1, $2, $3, $4, $5 ); | |||||||||||||||||||||||||||
| 96 | 4 | ▶ | if ( $q_seq_start != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | Carp::croak "Query sequence doesn't start at 1\n"; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | #----------------------------# | ||||||||||||||||||||||||||||
| 101 | # h-stanza | ||||||||||||||||||||||||||||
| 102 | #----------------------------# | ||||||||||||||||||||||||||||
| 103 | 4 | $lav =~ /h \{\s+(.+?)\s+\}/s; | |||||||||||||||||||||||||||
| 104 | 4 | my $h_stanza = $1; | |||||||||||||||||||||||||||
| 105 | 4 | my @h_lines = $h_stanza =~ /(.+)/g; | |||||||||||||||||||||||||||
| 106 | 4 | ▶ | if ( scalar @h_lines != 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | Carp::croak "h-stanza error.\n"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | $h_lines[0] =~ m{">?\s*(\w+)}; | |||||||||||||||||||||||||||
| 111 | 4 | my $t_name = $1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 4 | $h_lines[1] =~ m{">?\s*(\w+)}; | |||||||||||||||||||||||||||
| 114 | 4 | my $q_name = $1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | ▶ | if ( $h_lines[1] =~ m{ \(reverse complement\)} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | if ( $q_strand == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | Carp::croak "q_strand from h-stanza doesn't match with s-stanza.\n"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | #----------------------------# | ||||||||||||||||||||||||||||
| 123 | # generate psl lines | ||||||||||||||||||||||||||||
| 124 | #----------------------------# | ||||||||||||||||||||||||||||
| 125 | 4 | my @a_stanzas = $lav =~ /a \{\s+(.+?)\s+\}/sg; | |||||||||||||||||||||||||||
| 126 | 4 | for my $a_stanza (@a_stanzas) { | |||||||||||||||||||||||||||
| 127 | 10 | my ( $match, $mismatch ) = ( 0, 0, ); | |||||||||||||||||||||||||||
| 128 | 10 | my ( $q_num_ins, $q_base_ins, $t_num_ins, $t_base_ins, ) = ( 0, 0, 0, 0, ); | |||||||||||||||||||||||||||
| 129 | 10 | my ( @sizes, @q_begins, @t_begins ); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 10 | my @align_pieces = $a_stanza =~ /\s*l (\d+ \d+ \d+ \d+ \d+)/g; | |||||||||||||||||||||||||||
| 132 | 10 | my $t_former_end; | |||||||||||||||||||||||||||
| 133 | my $q_former_end; | ||||||||||||||||||||||||||||
| 134 | 10 | for my $align_piece (@align_pieces) { | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 254 | ▶ | unless ( $align_piece =~ /(\d+) (\d+) (\d+) (\d+) (\d+)/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | Carp::croak "l-line error\n"; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 254 | my ( $t_begin, $q_begin, $t_end, $q_end, $percent_id ) = ( $1, $2, $3, $4, $5 ); | |||||||||||||||||||||||||||
| 140 | 254 | $t_begin--; | |||||||||||||||||||||||||||
| 141 | 254 | $q_begin--; | |||||||||||||||||||||||||||
| 142 | 254 | $percent_id = 0.01 * $percent_id; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 254 | my $bases = $q_end - $q_begin; | |||||||||||||||||||||||||||
| 145 | 254 | my $match_piece = App::Egaz::Common::round( $percent_id * $bases ); | |||||||||||||||||||||||||||
| 146 | 254 | $match += $match_piece; | |||||||||||||||||||||||||||
| 147 | 254 | $mismatch += $bases - $match_piece; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 254 | ▶ | if ( $t_former_end and $t_begin != $t_former_end ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$t_former_end and $t_begin != $t_former_end
MC/DC100%2/2
$t_former_end and $t_begin != $t_former_end
$t_former_end
$t_begin != $t_former_end
Truth table100%3/3
A and B
A= $t_former_endB= $t_begin != $t_former_end
| |||||||||||||||||||||||||||||
| 150 | 122 | $t_num_ins++; | |||||||||||||||||||||||||||
| 151 | 122 | $t_base_ins += $t_begin - $t_former_end; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 254 | ▶ | if ( $q_former_end and $q_begin != $q_former_end ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$q_former_end and $q_begin != $q_former_end
MC/DC100%2/2
$q_former_end and $q_begin != $q_former_end
$q_former_end
$q_begin != $q_former_end
Truth table100%3/3
A and B
A= $q_former_endB= $q_begin != $q_former_end
| |||||||||||||||||||||||||||||
| 154 | 122 | $q_num_ins++; | |||||||||||||||||||||||||||
| 155 | 122 | $q_base_ins += $q_begin - $q_former_end; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 254 | $t_former_end = $t_end; | |||||||||||||||||||||||||||
| 158 | 254 | $q_former_end = $q_end; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 254 | push @sizes, $t_end - $t_begin; | |||||||||||||||||||||||||||
| 161 | 254 | push @q_begins, $q_begin; | |||||||||||||||||||||||||||
| 162 | 254 | push @t_begins, $t_begin; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # b-line, begins | ||||||||||||||||||||||||||||
| 166 | 10 | ▶ | unless ( $a_stanza =~ /\s*b (\d+) (\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | Carp::croak "No b-line.\n"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 10 | my $t_from = $1; | |||||||||||||||||||||||||||
| 170 | 10 | my $q_from = $2; | |||||||||||||||||||||||||||
| 171 | 10 | $t_from--; | |||||||||||||||||||||||||||
| 172 | 10 | $q_from--; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # e-line, ends | ||||||||||||||||||||||||||||
| 175 | 10 | ▶ | unless ( $a_stanza =~ /\s*e (\d+) (\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | Carp::croak "No e-line.\n"; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 10 | my $t_to = $1; | |||||||||||||||||||||||||||
| 179 | 10 | my $q_to = $2; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # s-line, scores | ||||||||||||||||||||||||||||
| 182 | 10 | ▶ | unless ( $a_stanza =~ /\s*s (\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | Carp::croak "No s-line.\n"; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 10 | my $score = $1; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # prepare psl line | ||||||||||||||||||||||||||||
| 188 | 10 | my $psl_line; | |||||||||||||||||||||||||||
| 189 | 10 | $psl_line .= sprintf "%d\t%d\t0\t0\t", $match, $mismatch; | |||||||||||||||||||||||||||
| 190 | 10 | $psl_line .= sprintf "%d\t%d\t%d\t%d\t", $q_num_ins, $q_base_ins, $t_num_ins, | |||||||||||||||||||||||||||
| 191 | $t_base_ins,; | ||||||||||||||||||||||||||||
| 192 | 10 | ▶ | $psl_line .= sprintf "%s\t", $q_strand ? '-' : '+'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # if query is - strand, convert begin/end to genomic coordinates | ||||||||||||||||||||||||||||
| 195 | 10 | ▶ | if ($q_strand) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 8 | $psl_line .= sprintf "%s\t%d\t%d\t%d\t", $q_name, $q_seq_stop, $q_seq_stop - $q_to, | |||||||||||||||||||||||||||
| 197 | $q_seq_stop - $q_from; | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | else { | ||||||||||||||||||||||||||||
| 200 | 2 | $psl_line .= sprintf "%s\t%d\t%d\t%d\t", $q_name, $q_seq_stop, $q_from, $q_to; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 10 | $psl_line .= sprintf "%s\t%d\t%d\t%d\t", $t_name, $t_seq_stop, $t_from, $t_to; | |||||||||||||||||||||||||||
| 204 | 10 | $psl_line .= sprintf "%d\t", scalar @align_pieces; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 10 | $psl_line .= sprintf "%d,", $_ for (@sizes); | |||||||||||||||||||||||||||
| 207 | 10 | $psl_line .= "\t"; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 10 | $psl_line .= sprintf "%d,", $_ for (@q_begins); | |||||||||||||||||||||||||||
| 210 | 10 | $psl_line .= "\t"; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 10 | $psl_line .= sprintf "%d,", $_ for (@t_begins); | |||||||||||||||||||||||||||
| 213 | 10 | $psl_line .= "\n"; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 10 | print {$out_fh} $psl_line; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1 | close $out_fh; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | return; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 19 | 1; | |||||||||||||||||||||||||||