| 1 | package App::Egaz::Command::lav2axt; | ||||||||||||||||||||||||||||
| 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 .axt'; | |||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 5 | return ( [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ], | |||||||||||||||||||||||||||
| 15 | { show_defaults => 1, } ); | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | sub usage_desc { | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 5 | return "egaz lav2axt [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 | 4 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 4 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | 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 | 3 | for ( @{$args} ) { | |||||||||||||||||||||||||||
| 44 | 3 | ▶ | next if lc $_ eq "stdin"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | 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 | 2 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=24 SCAR=32.5
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 2 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #----------------------------# | ||||||||||||||||||||||||||||
| 56 | # load lav | ||||||||||||||||||||||||||||
| 57 | #----------------------------# | ||||||||||||||||||||||||||||
| 58 | 2 | my $lav_content = Path::Tiny::path( $args->[0] )->slurp; | |||||||||||||||||||||||||||
| 59 | 2 | my @lavs = grep {/^[ds] /} split /\#\:lav\n/, $lav_content; | |||||||||||||||||||||||||||
| 60 | 2 | my $d_stanza = shift @lavs; # Not needed by this program | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # print YAML::Syck::Dump \@lavs; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #----------------------------# | ||||||||||||||||||||||||||||
| 65 | # write outputs | ||||||||||||||||||||||||||||
| 66 | #----------------------------# | ||||||||||||||||||||||||||||
| 67 | 2 | my $out_fh; | |||||||||||||||||||||||||||
| 68 | 2 | ▶ | if ( lc( $opt->{outfile} ) eq "stdout" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 2 | $out_fh = *STDOUT{IO}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||
| 72 | 0 | open $out_fh, ">", $opt->{outfile}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | my $serial = 0; | |||||||||||||||||||||||||||
| 76 | 2 | my %cache; # cache fasta files | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | for my $lav (@lavs) { | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | #----------------------------# | ||||||||||||||||||||||||||||
| 81 | # s-stanza | ||||||||||||||||||||||||||||
| 82 | #----------------------------# | ||||||||||||||||||||||||||||
| 83 | # "<filename>[-]" <start> <stop> [<rev_comp_flag> <sequence_number>] | ||||||||||||||||||||||||||||
| 84 | 8 | $lav =~ /s \{\s+(.+?)\s+\}/s; | |||||||||||||||||||||||||||
| 85 | 8 | my $s_stanza = $1; | |||||||||||||||||||||||||||
| 86 | 8 | my @s_lines = $s_stanza =~ /(.+ \s+ \d+ \s+ \d+ \s+ \d+ \s+ \d+)/gx; | |||||||||||||||||||||||||||
| 87 | 8 | ▶ | if ( scalar @s_lines != 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | Carp::croak "s-stanza error.\n"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 8 | $s_lines[0] =~ /\s*\"?(.+?)\-?\"? \s+ (\d+) \s+ (\d+) \s+ (\d+) \s+ (\d+)/x; | |||||||||||||||||||||||||||
| 92 | 8 | my ( $t_file, $t_seq_start, $t_seq_stop, $t_strand, $t_contig ) = ( $1, $2, $3, $4, $5 ); | |||||||||||||||||||||||||||
| 93 | 8 | ▶ | if ( !-e $t_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 4 | $t_file = App::Egaz::Common::resolve_file( $t_file, | |||||||||||||||||||||||||||
| 95 | Path::Tiny::path( $args->[0] )->parent(), "." ); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 8 | ▶ | if ( $t_seq_start != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | Carp::croak "Target sequence doesn't start at 1\n"; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 8 | $s_lines[1] =~ /\s*\"?(.+?)\-?\"? \s+ (\d+) \s+ (\d+) \s+ (\d+) \s+ (\d+)/x; | |||||||||||||||||||||||||||
| 102 | 8 | my ( $q_file, $q_seq_start, $q_seq_stop, $q_strand, $q_contig ) = ( $1, $2, $3, $4, $5 ); | |||||||||||||||||||||||||||
| 103 | 8 | ▶ | if ( !-e $q_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 4 | $q_file = App::Egaz::Common::resolve_file( $q_file, | |||||||||||||||||||||||||||
| 105 | Path::Tiny::path( $args->[0] )->parent(), "." ); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 8 | ▶ | if ( $q_seq_start != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | Carp::croak "Query sequence doesn't start at 1\n"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | #----------------------------# | ||||||||||||||||||||||||||||
| 112 | # h-stanza | ||||||||||||||||||||||||||||
| 113 | #----------------------------# | ||||||||||||||||||||||||||||
| 114 | 8 | $lav =~ /h \{\s+(.+?)\s+\}/s; | |||||||||||||||||||||||||||
| 115 | 8 | my $h_stanza = $1; | |||||||||||||||||||||||||||
| 116 | 8 | my @h_lines = $h_stanza =~ /(.+)/g; | |||||||||||||||||||||||||||
| 117 | 8 | ▶ | if ( scalar @h_lines != 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | Carp::croak "h-stanza error.\n"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #----------------------------# | ||||||||||||||||||||||||||||
| 122 | # generate two sequences | ||||||||||||||||||||||||||||
| 123 | #----------------------------# | ||||||||||||||||||||||||||||
| 124 | 8 | ▶ | if ( !exists $cache{$t_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | my $seq_of = App::Fasops::Common::read_fasta($t_file); | |||||||||||||||||||||||||||
| 126 | $cache{$t_file} = { | ||||||||||||||||||||||||||||
| 127 | seq_of => $seq_of, | ||||||||||||||||||||||||||||
| 128 | 2 | seq_names => [ keys %{$seq_of} ], | |||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 8 | my $t_name = $cache{$t_file}->{seq_names}[ $t_contig - 1 ]; | |||||||||||||||||||||||||||
| 132 | 8 | my $t_seq = $cache{$t_file}->{seq_of}{$t_name}; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 8 | ▶ | if ( !exists $cache{$q_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 2 | my $seq_of = App::Fasops::Common::read_fasta($q_file); | |||||||||||||||||||||||||||
| 136 | $cache{$q_file} = { | ||||||||||||||||||||||||||||
| 137 | seq_of => $seq_of, | ||||||||||||||||||||||||||||
| 138 | 2 | seq_names => [ keys %{$seq_of} ], | |||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 8 | my $q_name = $cache{$q_file}->{seq_names}[ $q_contig - 1 ]; | |||||||||||||||||||||||||||
| 142 | 8 | my $q_seq = $cache{$q_file}->{seq_of}{$q_name}; | |||||||||||||||||||||||||||
| 143 | 8 | ▶ | if ($q_strand) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 4 | $q_seq = App::Fasops::Common::revcom($q_seq); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | #----------------------------# | ||||||||||||||||||||||||||||
| 148 | # generate axt alignments | ||||||||||||||||||||||||||||
| 149 | #----------------------------# | ||||||||||||||||||||||||||||
| 150 | 8 | my @a_stanzas = $lav =~ /a \{\s+(.+?)\s+\}/sg; | |||||||||||||||||||||||||||
| 151 | 8 | for my $a_stanza (@a_stanzas) { | |||||||||||||||||||||||||||
| 152 | 20 | my $alignment_target = ''; | |||||||||||||||||||||||||||
| 153 | 20 | my $alignment_query = ''; | |||||||||||||||||||||||||||
| 154 | 20 | my @align_pieces = $a_stanza =~ /\s*l (\d+ \d+ \d+ \d+) \d+/g; | |||||||||||||||||||||||||||
| 155 | 20 | my $former_end_target = ''; | |||||||||||||||||||||||||||
| 156 | 20 | my $former_end_query = ''; | |||||||||||||||||||||||||||
| 157 | 20 | for my $align_piece (@align_pieces) { | |||||||||||||||||||||||||||
| 158 | 508 | ▶ | unless ( $align_piece =~ /(\d+) (\d+) (\d+) (\d+)/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | Carp::croak "l-line error\n"; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 508 | my ( $t_begin, $q_begin, $t_end, $q_end ) = ( $1, $2, $3, $4 ); | |||||||||||||||||||||||||||
| 162 | 508 | my $t_del = ''; | |||||||||||||||||||||||||||
| 163 | 508 | my $q_del = ''; | |||||||||||||||||||||||||||
| 164 | 508 | ▶ | if ( $alignment_target | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$alignment_target and $t_begin - $former_end_target > 1
MC/DC100%2/2
$alignment_target and $t_begin - $former_end_target > 1
$alignment_target
$t_begin - $former_end_target > 1
Truth table100%3/3
A and B
A= $alignment_targetB= $t_begin - $former_end_target > 1
| |||||||||||||||||||||||||||||
| 165 | && ( $t_begin - $former_end_target > 1 ) ) | ||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 244 | $q_del = '-' x ( $t_begin - $former_end_target - 1 ); | |||||||||||||||||||||||||||
| 168 | 244 | $alignment_query .= $q_del; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 508 | ▶ | if ( $alignment_query | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$alignment_query and $q_begin - $former_end_query > 1
MC/DC100%2/2
$alignment_query and $q_begin - $former_end_query > 1
$alignment_query
$q_begin - $former_end_query > 1
Truth table100%3/3
A and B
A= $alignment_queryB= $q_begin - $former_end_query > 1
| |||||||||||||||||||||||||||||
| 171 | && ( $q_begin - $former_end_query > 1 ) ) | ||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 244 | $t_del = '-' x ( $q_begin - $former_end_query - 1 ); | |||||||||||||||||||||||||||
| 174 | 244 | $alignment_target .= $t_del; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 508 | my $length_target = $t_end - $t_begin + 1 + ( length $q_del ); | |||||||||||||||||||||||||||
| 177 | 508 | my $length_query = $q_end - $q_begin + 1 + ( length $t_del ); | |||||||||||||||||||||||||||
| 178 | 508 | $alignment_target | |||||||||||||||||||||||||||
| 179 | .= substr( $t_seq, ( $t_begin - 1 - ( length $q_del ) ), $length_target ); | ||||||||||||||||||||||||||||
| 180 | 508 | $alignment_query | |||||||||||||||||||||||||||
| 181 | .= substr( $q_seq, ( $q_begin - 1 - ( length $t_del ) ), $length_query ); | ||||||||||||||||||||||||||||
| 182 | 508 | ▶ | if ( ( length $alignment_query ) ne ( length $alignment_target ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | Carp::croak "Target length doesn't match query's in the alignment.\n"; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 508 | $former_end_target = $t_end; | |||||||||||||||||||||||||||
| 186 | 508 | $former_end_query = $q_end; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # b-line, begins | ||||||||||||||||||||||||||||
| 190 | 20 | ▶ | unless ( $a_stanza =~ /\s*b (\d+) (\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | Carp::croak "No b-line.\n"; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 20 | my $t_from = $1; | |||||||||||||||||||||||||||
| 194 | 20 | my $q_from = $2; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # e-line, ends | ||||||||||||||||||||||||||||
| 197 | 20 | ▶ | unless ( $a_stanza =~ /\s*e (\d+) (\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | Carp::croak "No e-line.\n"; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 20 | my $t_to = $1; | |||||||||||||||||||||||||||
| 201 | 20 | my $q_to = $2; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # s-line, scores | ||||||||||||||||||||||||||||
| 204 | 20 | ▶ | unless ( $a_stanza =~ /\s*s (\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | Carp::croak "No s-line.\n"; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 20 | my $score = $1; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # only keep the first part in fasta header | ||||||||||||||||||||||||||||
| 210 | 20 | ($t_name) = split /\W+/, $t_name; | |||||||||||||||||||||||||||
| 211 | 20 | ($q_name) = split /\W+/, $q_name; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # prepare axt header | ||||||||||||||||||||||||||||
| 214 | 20 | my $axt_head = $serial; | |||||||||||||||||||||||||||
| 215 | 20 | $axt_head .= " $t_name $t_from $t_to"; | |||||||||||||||||||||||||||
| 216 | 20 | $axt_head .= " $q_name $q_from $q_to "; | |||||||||||||||||||||||||||
| 217 | 20 | ▶ | $axt_head .= $q_strand ? '-' : '+'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 20 | $axt_head .= " $score\n"; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # write axt file | ||||||||||||||||||||||||||||
| 221 | 20 | print {$out_fh} $axt_head; | |||||||||||||||||||||||||||
| 222 | 20 | print {$out_fh} "$alignment_target\n"; | |||||||||||||||||||||||||||
| 223 | 20 | print {$out_fh} "$alignment_query\n\n"; | |||||||||||||||||||||||||||
| 224 | 20 | $serial++; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 2 | close $out_fh; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | return; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 19 | 1; | |||||||||||||||||||||||||||