| 1 | package Bioinf::Basic; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: FASTA I/O in XS, BLAST hit ranking, and alignment plots and tables | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Taken from ~/Scripts/bioinf.pm (as it stood on 2026-08-19): fasta2hash, | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # get_best_alignment_hit, hash2fasta_file, msa_quality_table, msa_phylo_plot and | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # clustal_view_residues. fasta2hash and hash2fasta_file are now XS (Basic.xs). | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # msa_phylo_plot is now plot_msa and plot_phylo, and no longer takes a BLAST | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # report and a proteome database: each is given the sequences, aligns them with | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # Clustal Omega and draws the result itself, with share/msa_plot.py (from | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # ~/Scripts/HitList/scripts/x.my.align.py) in place of that script and of the | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # R/ggtree tree. msa_quality_table's table is drawn by that script too, where | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # bioinf.pm had Matplotlib::Simple's colored_table draw it. clustalo, blastp and | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # that Python all come from Alien::Bioinf, never from PATH. Alien::Bioinf is only | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # recommended, not required, since it cannot be installed everywhere (NCBI | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # builds BLAST+ for few platforms) and fasta2hash, hash2fasta_file and | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # get_best_alignment_hit do not need it; the functions that do load it through | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # _alien, which says what is missing. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | require 5.010; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | use Cwd 'getcwd'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use Sys::Hostname 'hostname'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use XSLoader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | our @EXPORT_OK = qw(clustal_view_residues fasta2hash get_best_alignment_hit hash2fasta_file msa_quality_table plot_msa plot_phylo); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | XSLoader::load('Bioinf::Basic', $VERSION); | |||||||||||||||||||||||||||||||||||||||||
| 30 | # The calling script's name, for _creator. It is looked up at load time, as | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # FindBin always does, but FindBin croaks when $0 is not a file on disk (after | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # $0 is assigned to, or under an embedder), and that must not stop this module | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # loading, so $0 itself stands in then. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | my $RealScript = eval { require FindBin; $FindBin::RealScript } // $0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
eval {
do {
require FindBin;
$FindBin::RealScript
}
} // $0
MC/DC0%0/2
eval {
do {
require FindBin;
$FindBin::RealScript
}
} // $0
eval {
do {
require FindBin;
$FindBin::RealScript
}
}
$0
Truth table66%2/3
A // B
A= eval {
do {
require FindBin;
$FindBin::RealScript
}
}B= $0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # ---- private helpers | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # The name => value pairs in @$list as a hash ref. Dies unless they hold every | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # @$required key and nothing outside @$required and @$optional. Its messages | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # are prefixed with $sub, the public function whose arguments they are, not | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # with "_args". | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 17 | ▶ | sub _args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_args: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 17 | my ($list, $sub, $required, $optional) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 17 | ▶ | croak "$sub: takes name => value pairs, not a hash ref" if @{ $list } == 1 && ref $list->[0] eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@{$list;} == 1 and ref $list->[0] eq "HASH"
MC/DC0%0/2
@{$list;} == 1 and ref $list->[0] eq "HASH"
@{$list;} == 1
ref $list->[0] eq "HASH"
Truth table33%1/3
A and B
A= @{$list;} == 1B= ref $list->[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 17 | ▶ | croak "$sub: takes name => value pairs, and was given an odd number of arguments" if @{ $list } % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 17 | my $args = { @{ $list } }; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 17 | my @missing = grep { !defined $args->{$_} } @{ $required }; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | croak "$sub: needs " . join(', ', map { "\"$_\"" } @missing) if @missing; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 17 | my %ok = map { $_ => 1 } @{ $required }, @{ $optional }; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | my @bad = sort grep { !$ok{$_} } keys %{ $args }; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | croak "$sub: doesn't know " . join(', ', map { "\"$_\"" } @bad) . '; it takes ' . join(', ', sort keys %ok) if @bad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 17 | $args; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # The "Creator" written into an image's metadata, after the one | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # Matplotlib::Simple writes: | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # which script, run where, called which sub of which version of this module, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # on which computer (its hostname, OS and perl). | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _creator { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_creator: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my ($sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | getcwd() . "/$RealScript called using \"$sub\" in " . __FILE__ . " version $VERSION on host " . hostname() | |||||||||||||||||||||||||||||||||||||||||
| 62 | . " ($^O, perl " . sprintf('%vd', $^V) . ')'; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 9 | ▶ | sub _json_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 9 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 9 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | ▶ | open my $fh, '<:raw', $file or croak "_json_file: can't read $file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | JSON::MaybeXS::decode_json(<$fh>); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub _run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my @cmd = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | system(@cmd) == 0 or croak "_run: \"@cmd\" failed: " . ($? == -1 ? $! : 'exit ' . ($? >> 8)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # Alien::Bioinf, loaded for $sub, or a message saying that $sub needs it. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | sub _alien { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_alien: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | my ($sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | eval { require Alien::Bioinf; 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | or croak "_alien: $sub needs Alien::Bioinf, for Clustal Omega, BLAST+ and the plotting Python, and it can't be loaded: $@"; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | 'Alien::Bioinf'; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # share/msa_plot.py run with @args under Alien::Bioinf's Python, for $sub. The | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # arguments go in a JSON file rather than on the command line: perl's | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # system(LIST) on Windows does not escape a '"' inside an argument, and the | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # Creator and the JSON options are full of them. A string that perl holds as | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # bytes is taken to be UTF-8 if it decodes as such, which is how the command | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # line used to pass it on; encode_json alone would read such bytes as Latin-1. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _python { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_python: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my ($sub, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my @chars = map { my $s = "$_"; utf8::decode($s) unless utf8::is_utf8($s); $s } @args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $argfile = _tmp('.json'); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | open my $fh, '>:raw', $argfile or croak "_python: can't write $argfile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | print {$fh} JSON::MaybeXS::encode_json(\@chars); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | close $fh or croak "_python: can't write $argfile: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | _run(_alien($sub)->python, _script(), '--argfile', $argfile); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # $data as JSON text: characters, not UTF-8 bytes, since it is one argument | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # that _python encodes along with the rest. encode_json here would read a byte | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # string's UTF-8 as Latin-1, so that _python's decoding came too late. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub _json_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my ($data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | JSON::MaybeXS->new(utf8 => 0, canonical => 1)->encode($data); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # "wrote $file" to STDOUT, as LikeR's write_table prints it: $file in black | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # (30) on $bg (an ANSI background, 43 = yellow or 46 = cyan) and then reset | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # (0), written out rather than loaded from Term::ANSIColor; the reset comes | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # before the newline so the colour doesn't run on into the next line. The | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # colour is left out when STDOUT is not a terminal, where the escapes would | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # only be noise in a log or a test harness. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | sub _wrote { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wrote: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my ($file, $bg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | print STDOUT -t STDOUT ? "wrote \e[30;${bg}m$file\e[0m\n" : "wrote $file\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | ▶ | sub _tmp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tmp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | my ($suffix) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | require File::Temp; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | my ($fh, $name) = File::Temp::tempfile(SUFFIX => $suffix, UNLINK => 1, TMPDIR => 1); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | $name; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub _script { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_script: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | require File::ShareDir; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | File::ShareDir::dist_file('Bioinf-Basic', 'msa_plot.py'); | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # The FASTA file's records and their deflines in file order. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub _fasta_ordered { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fasta_ordered: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my @order; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $fh = _open_fasta($file); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $h = _read_fasta($fh, undef, $file, \@order); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ($h, \@order); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 31 | ▶ | sub _open_fasta { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open_fasta: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 31 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 31 | ▶ | croak "_open_fasta: \"$file\" doesn't exist or isn't a readable file" unless defined $file && -f $file && -r _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $file and -f $file and -r _
defined $file and -f $file
MC/DC33%1/3
defined $file and -f $file and -r _
defined $file
-f $file
-r _
Truth table50%2/4
A and B and C
A= defined $fileB= -f $fileC= -r _
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 30 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 30 | ▶ | if ($file =~ /\.gz\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 4 | ▶ | open $fh, '-|', 'gzip', '-dc', $file or croak "_open_fasta: can't run gzip on $file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 26 | ▶ | open $fh, '<:raw', $file or croak "_open_fasta: can't read $file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 30 | $fh; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | ▶ | sub _first_letter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_first_letter: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 6 | my ($string) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | ▶ | return $1 if $string =~ /^([A-Za-z])/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | croak "_first_letter: \"$string\" doesn't start with a letter, so it has no first letter"; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # Narrow but unambiguous labels, e.g. "$\it{C.al.}$", for [genus, species, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # strain] triples ('' for no strain). One letter of the species cannot tell | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # C.albicans from C.auris, so this takes the shortest species prefix that | ||||||||||||||||||||||||||||||||||||||||||
| 167 | # separates every entry; the strain is always kept, since it is all that | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # separates the three C.neoformans. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 2 | ▶ | sub _abbreviated_labels { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_abbreviated_labels: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | my ($names) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 2 | my $max = 0; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | foreach my $n (@{ $names }) { | |||||||||||||||||||||||||||||||||||||||||
| 173 | 3 | ▶ | $max = length $n->[1] if length $n->[1] > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | foreach my $n (1 .. $max) { | |||||||||||||||||||||||||||||||||||||||||
| 176 | my @labels = map { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 3 | ▶ | '$\it{' . ucfirst(_first_letter($_->[0])) . '.' . lc(substr($_->[1], 0, $n)) . '.}$' . ($_->[2] ne '' ? " $_->[2]" : '') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | } @{ $names }; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | return \@labels unless grep { $seen{$_}++ } @labels; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | croak "_abbreviated_labels: can't tell " . join(', ', map { join '.', @{ $_ } } @{ $names }) . ' apart, even with the full species name and strain'; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | # ---- FASTA | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 31 | ▶ | sub fasta2hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fasta2hash: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 31 | my ($file, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 31 | my $fh = _open_fasta($file); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 30 | my $r = _read_fasta($fh, $key, $file); | |||||||||||||||||||||||||||||||||||||||||
| 191 | # A gzip that fails part-way, on a truncated or corrupt .gz, has merely | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # stopped writing, so its exit status is the only sign of it. That is | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # checked unless $key was found, when reading may have stopped early and | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # the closed pipe have killed gzip with a SIGPIPE. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 28 | ▶ | unless (close $fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | ▶ | my $why = $! ? "$!" : $? & 127 ? 'gzip was killed by signal ' . ($? & 127) : 'gzip exited ' . ($? >> 8); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | ▶ | croak "fasta2hash: couldn't read all of $file: $why" unless defined $key && defined $r; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and defined $r
MC/DC0%0/2
defined $key and defined $r
defined $key
defined $r
Truth table66%2/3
A and B
A= defined $keyB= defined $r
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 26 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 11 | ▶ | return $r if defined $r; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 3 | croak "fasta2hash: couldn't find \"$key\" in $file"; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 15 | ▶ | croak "fasta2hash: couldn't find any sequences in $file" unless %{ $r }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 14 | $r; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 11 | ▶ | sub hash2fasta_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash2fasta_file: called CC=10 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 11 | my ($hash, $filename, $order, $width) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 11 | ▶ | croak "hash2fasta_file: 1st argument must be a hash ref" unless ref $hash eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 10 | ▶ | croak "hash2fasta_file: 2nd argument must be a file name" unless defined $filename && ref $filename eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $filename and ref $filename eq ""
MC/DC0%0/2
defined $filename and ref $filename eq ""
defined $filename
ref $filename eq ""
Truth table33%1/3
A and B
A= defined $filenameB= ref $filename eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 10 | ▶ | croak "hash2fasta_file: 3rd argument must be an array ref or undef" if defined $order && ref $order ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $order and ref $order ne "ARRAY"
MC/DC100%2/2
defined $order and ref $order ne "ARRAY"
defined $order
ref $order ne "ARRAY"
Truth table100%3/3
A and B
A= defined $orderB= ref $order ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 9 | ▶ | open my $fh, '>:raw', $filename or croak "hash2fasta_file: can't write $filename: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 9 | ▶ | _write_fasta($fh, $hash, $order // [sort keys %{ $hash }], $width // 80); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
$order // [sort(keys %{$hash;})]
$width // 80
MC/DC100%1/1
$order // [sort(keys %{$hash;})]
$order
MC/DC100%1/1
$width // 80
$width
Truth table100%2/2
A
A= $order
Truth table100%2/2
A
A= $width
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 7 | ▶ | close $fh or croak "hash2fasta_file: can't write $filename: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 7 | $filename; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | # ---- BLAST | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 11 | ▶ | sub get_best_alignment_hit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_best_alignment_hit: called CC=19 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 11 | my ($json_file, $sort_criterion) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 11 | ▶ | $sort_criterion //= 'evalue'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$sort_criterion //= "evalue"
MC/DC100%1/1
$sort_criterion //= "evalue"
$sort_criterion
Truth table100%2/2
A
A= $sort_criterion
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | # For each query in one BLAST JSON file (-outfmt 15), that query's hits as | ||||||||||||||||||||||||||||||||||||||||||
| 224 | # an array sorted best-first on $sort_criterion, one element per hit: the | ||||||||||||||||||||||||||||||||||||||||||
| 225 | # hit's single best hsp plus the fields that say which subject it is. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | # | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # The hits are an array, not a hash on the hit title, because two hits can | ||||||||||||||||||||||||||||||||||||||||||
| 228 | # share a title (one protein deposited under two accessions): in | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # identify.target/potato/new/f.sambunicum.json 76 hits carry 44 distinct | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # titles, and a hash kept 44. | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 11 | ▶ | croak "get_best_alignment_hit: \"$json_file\" doesn't exist or isn't a readable file" unless defined $json_file && -f $json_file && -r _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $json_file and -f $json_file and -r _
defined $json_file and -f $json_file
MC/DC33%1/3
defined $json_file and -f $json_file and -r _
defined $json_file
-f $json_file
-r _
Truth table50%2/4
A and B and C
A= defined $json_fileB= -f $json_fileC= -r _
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | # Which way is better for each numeric hsp field: -1 = smaller, 1 = larger. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # The other hsp keys (hseq, qseq, midline, num, *_from/*_to) are content | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # or position, not quality. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 10 | my %better = (align_len => 1, bit_score => 1, evalue => -1, gaps => -1, identity => 1, positive => 1, score => 1); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 10 | ▶ | my $direction = $better{$sort_criterion} | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$better{$sort_criterion} // croak(qq[get_best_alignment_hit: "$sort_criterion" isn't a sortable hsp field; use one of ] . join(", ", sort(keys %better)))
MC/DC100%1/1
$better{$sort_criterion} // croak(qq[get_best_alignment_hit: "$sort_criterion" isn't a sortable hsp field; use one of ] . join(", ", sort(keys %better)))
$better{$sort_criterion}
Truth table100%2/2
A
A= $better{$sort_criterion}
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | // croak "get_best_alignment_hit: \"$sort_criterion\" isn't a sortable hsp field; use one of " . join(', ', sort keys %better); | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 9 | my $blast = _json_file($json_file); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 9 | my %alignments; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 9 | foreach my $report (@{ $blast->{BlastOutput2} }) { | |||||||||||||||||||||||||||||||||||||||||
| 241 | 10 | my @hits; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 10 | foreach my $hit (@{ $report->{report}{results}{search}{hits} }) { | |||||||||||||||||||||||||||||||||||||||||
| 243 | 40 | ▶ | croak "get_best_alignment_hit: a hit has more than 1 description" if @{ $hit->{description} } > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 40 | my $best; # undef until an hsp is seen; a hit with no hsps has nothing to record | |||||||||||||||||||||||||||||||||||||||||
| 245 | 40 | foreach my $hsp (@{ $hit->{hsps} }) { | |||||||||||||||||||||||||||||||||||||||||
| 246 | croak "get_best_alignment_hit: an hsp of \"$hit->{description}[0]{title}\" in $json_file has no \"$sort_criterion\" to sort on" | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 56 | ▶ | unless defined $hsp->{$sort_criterion}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 56 | ▶ | unless (defined $best) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 40 | $best = $hsp; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 40 | next; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 16 | my $cmp = $direction * ($hsp->{$sort_criterion} <=> $best->{$sort_criterion}); | |||||||||||||||||||||||||||||||||||||||||
| 253 | # BLAST rounds any strong e-value to 0, so ties are the rule; the bit score breaks them | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 16 | ▶ | $cmp = $hsp->{bit_score} <=> $best->{bit_score} if $cmp == 0 && $sort_criterion ne 'bit_score'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cmp == 0 and $sort_criterion ne "bit_score"
MC/DC50%1/2
$cmp == 0 and $sort_criterion ne "bit_score"
$cmp == 0
$sort_criterion ne "bit_score"
Truth table66%2/3
A and B
A= $cmp == 0B= $sort_criterion ne "bit_score"
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 16 | ▶ | $best = $hsp if $cmp > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 40 | ▶ | next unless defined $best; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | # A shallow copy (an hsp holds only numbers and strings), so these | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # fields are not written into $blast. The hit's own "num" is not | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # merged, as it would shadow the hsp's; its "len" is the subject's | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # full length, so it arrives as "hit_len" beside the hsp's "align_len". | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 40 | push @hits, { %{ $best }, | |||||||||||||||||||||||||||||||||||||||||
| 263 | accession => $hit->{description}[0]{accession}, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | hit_len => $hit->{len}, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | id => $hit->{description}[0]{id}, | ||||||||||||||||||||||||||||||||||||||||||
| 266 | title => $hit->{description}[0]{title}, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 40 | }; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | # Indices are sorted so the last tiebreak can be BLAST's own order; the | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # array runs best-first, hence $b before $a. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | my @order = sort { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 10 | ($direction * ($hits[$b]{$sort_criterion} <=> $hits[$a]{$sort_criterion})) | |||||||||||||||||||||||||||||||||||||||||
| 273 | || ($hits[$b]{bit_score} <=> $hits[$a]{bit_score}) | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 64 | ▶ | || ($a <=> $b) | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$direction * ($hits[$b]{$sort_criterion} <=> $hits[$a]{$sort_criterion}) || $hits[$b]{'bit_score'} <=> $hits[$a]{'bit_score'} || $a <=> $b
$direction * ($hits[$b]{$sort_criterion} <=> $hits[$a]{$sort_criterion}) or $hits[$b]{"bit_score"} <=> $hits[$a]{"bit_score"}
MC/DC0%0/3
$direction * ($hits[$b]{$sort_criterion} <=> $hits[$a]{$sort_criterion}) || $hits[$b]{'bit_score'} <=> $hits[$a]{'bit_score'} || $a <=> $b
$direction * ($hits[$b]{$sort_criterion} <=> $hits[$a]{$sort_criterion})
$hits[$b]{"bit_score"} <=> $hits[$a]{"bit_score"}
$a <=> $b
Truth table75%3/4
A or B || C
A= $direction * ($hits[$b]{$sort_criterion} <=> $hits[$a]{$sort_criterion})B= $hits[$b]{"bit_score"} <=> $hits[$a]{"bit_score"}C= $a <=> $b
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | } 0 .. $#hits; | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # Two queries of one title would leave only the second's hits, so that | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # is refused rather than lost silently. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 10 | my $query = $report->{report}{results}{search}{query_title}; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 10 | ▶ | croak "get_best_alignment_hit: two queries in $json_file are both titled \"$query\"" if exists $alignments{$query}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 9 | $alignments{$query} = [ @hits[@order] ]; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 8 | \%alignments; | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | # ---- alignment plots and tables | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | # Dies unless $n, given to $sub as $what, is a 1-based residue number. Without | ||||||||||||||||||||||||||||||||||||||||||
| 288 | # this, 0 and the negative numbers index @col from its end in plot_msa and | ||||||||||||||||||||||||||||||||||||||||||
| 289 | # clustal_view_residues, and silently mean the last residues. | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 6 | ▶ | sub _residue_number { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_residue_number: called CC=4 SCAR=16.9
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 6 | my ($sub, $what, $n) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 6 | ▶ | croak "$sub: $what must be a residue number, 1 or more, not " . (defined $n ? "\"$n\"" : 'undef') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $n and $n =~ /\A[1-9][0-9]*\z/
MC/DC0%0/2
defined $n and $n =~ /\A[1-9][0-9]*\z/
defined $n
$n =~ /\A[1-9][0-9]*\z/
Truth table33%1/3
A and B
A= defined $nB= $n =~ /\A[1-9][0-9]*\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | unless defined $n && $n =~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | # "fasta" -- a FASTA file name or a hash ref of name => sequence -- written to a | ||||||||||||||||||||||||||||||||||||||||||
| 297 | # temporary FASTA file with its gaps stripped, so that an input that is already | ||||||||||||||||||||||||||||||||||||||||||
| 298 | # aligned is aligned afresh. clustalo's --dealign does the same, but prints a | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # "FORCED DEBUG" warning about them first; blastp would take a gap for a residue. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # Returns that file and the sequence names in input order (sorted for a hash). | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | ▶ | sub _ungapped_fasta { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ungapped_fasta: called CC=3 SCAR=13.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | my ($fasta) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 1 | my ($seqs, @names); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | if (ref $fasta eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | $seqs = $fasta; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | @names = sort keys %{ $seqs }; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } elsif (ref $fasta eq '') { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $order; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ($seqs, $order) = _fasta_ordered($fasta); | |||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | @names = @{ $order }; | |||||||||||||||||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | croak "_ungapped_fasta: \"fasta\" must be a FASTA file name or a hash ref of name => sequence, not a " . ref($fasta) . ' ref'; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 1 | my %ungapped = map { (my $s = $seqs->{$_}) =~ tr/-.//d; $_ => $s } @names; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | (hash2fasta_file(\%ungapped, _tmp('.fa'), \@names), \@names); | |||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | # Aligns "fasta" with clustalo for $sub. The alignment goes to "msa.file" and | ||||||||||||||||||||||||||||||||||||||||||
| 319 | # the guide tree to "tree.file" when those are given, and to temporary files | ||||||||||||||||||||||||||||||||||||||||||
| 320 | # otherwise; the tree is not asked for at all unless it is kept or $want_tree. | ||||||||||||||||||||||||||||||||||||||||||
| 321 | # Returns the hash ref of files to report (the alignment always, the tree only | ||||||||||||||||||||||||||||||||||||||||||
| 322 | # when it was named), the tree's file, the sequence names in input order | ||||||||||||||||||||||||||||||||||||||||||
| 323 | # (sorted for a hash), and an array ref of the clustalo command as it was run, | ||||||||||||||||||||||||||||||||||||||||||
| 324 | # with the ungapped FASTA it read as its "--in"; or nothing, after a warning, | ||||||||||||||||||||||||||||||||||||||||||
| 325 | # for fewer than 2 sequences. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 1 | ▶ | sub _align { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_align: called CC=10 SCAR=35.1
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 1 | my ($args, $sub, $want_tree) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | my ($in, $names) = _ungapped_fasta($args->{fasta}); | |||||||||||||||||||||||||||||||||||||||||
| 329 | 1 | ▶ | if (@{ $names } < 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | carp "$sub: clustalo needs at least 2 sequences, and there " . (@{ $names } ? 'is only 1' : 'are none'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | ▶ | my %r = ('msa.file' => $args->{'msa.file'} // _tmp('.aln.fa')); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args->{"msa.file"} // _tmp(".aln.fa")
MC/DC0%0/2
$args->{"msa.file"} // _tmp(".aln.fa")
$args->{"msa.file"}
_tmp(".aln.fa")
Truth table33%1/3
A // B
A= $args->{"msa.file"}B= _tmp(".aln.fa")
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 1 | ▶ | my $tree = $args->{'tree.file'} // ($want_tree ? _tmp('.newick') : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"tree.file"} // ($want_tree ? _tmp(".newick") : undef)
MC/DC0%0/2
$args->{"tree.file"} // ($want_tree ? _tmp(".newick") : undef)
$args->{"tree.file"}
($want_tree ? _tmp(".newick") : undef)
Truth table33%1/3
A // B
A= $args->{"tree.file"}B= ($want_tree ? _tmp(".newick") : undef)
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 1 | ▶ | $r{'tree.file'} = $tree if defined $args->{'tree.file'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | my @cmd = (_alien($sub)->clustalo, '--in', $in, '--out', $r{'msa.file'}, '--outfmt=fa', '--force', | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | '--threads', $args->{threads} // 1, (defined $tree ? "--guidetree-out=$tree" : ()), @{ $args->{'clustal.args'} // [] }); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$args->{"threads"} // 1
$args->{"clustal.args"} // []
MC/DC0%0/1
$args->{"threads"} // 1
$args->{"threads"}
MC/DC0%0/1
$args->{"clustal.args"} // []
$args->{"clustal.args"}
Truth table0%0/2
A
A= $args->{"threads"}
Truth table0%0/2
A
A= $args->{"clustal.args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | _run(@cmd); | |||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | (\%r, $tree, $names, \@cmd); | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 6 | ▶ | sub plot_msa { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plot_msa: called CC=23 SCAR=57.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 6 | my $sub = 'plot_msa'; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 6 | my $args = _args(\@_, $sub, [qw(fasta filename)], [qw(active.site.aa clustal.args labels msa.file order query threads title | |||||||||||||||||||||||||||||||||||||||||
| 345 | tree.file xlabel ylabel)]); | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 6 | my $sites = $args->{'active.site.aa'}; | |||||||||||||||||||||||||||||||||||||||||
| 347 | 6 | ▶ | if (defined $sites) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 5 | ▶ | croak "$sub: \"active.site.aa\" must be a hash ref of label => residue number" unless ref $sites eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 4 | ▶ | croak "$sub: needs \"query\" to place \"active.site.aa\"" unless defined $args->{query}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 4 | _residue_number($sub, "active site $_", $sites->{$_}) foreach sort keys %{ $sites }; | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1 | ▶ | my ($r, undef, $names) = _align($args, $sub, 0) or return {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | my $aln = fasta2hash($r->{'msa.file'}); | |||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | my @order = @{ $args->{order} // $names }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"order"} // $names
MC/DC0%0/2
$args->{"order"} // $names
$args->{"order"}
$names
Truth table0%0/3
A // B
A= $args->{"order"}B= $names
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | my @unknown = grep { !defined $aln->{$_} } @order; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | croak "$sub: \"order\" names sequences that aren't in the alignment: @unknown" if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | my $labels = $args->{labels} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"labels"} // {}
MC/DC0%0/1
$args->{"labels"} // {}
$args->{"labels"}
Truth table0%0/2
A
A= $args->{"labels"}
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | # Two sequences shown under one label would be one row of the image, fed | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # two deflines. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | my %by_label; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | push @{ $by_label{ $labels->{$_} // $_ } }, $_ foreach @order; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$labels->{$_} // $_
MC/DC0%0/2
$labels->{$_} // $_
$labels->{$_}
$_
Truth table0%0/3
A // B
A= $labels->{$_}B= $_
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | foreach my $label (sort keys %by_label) { | |||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | croak "$sub: " . join(', ', map { "\"$_\"" } @{ $by_label{$label} }) . " would all be drawn as \"$label\"" | |||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | if @{ $by_label{$label} } > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | my @segments; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | if (defined $sites) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | ▶ | my $q = $aln->{ $args->{query} } // croak "$sub: the query \"$args->{query}\" isn't in the alignment"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$aln{$args->{"query"}} // croak(qq[${sub}: the query "$args->{"query"}" isn't in the alignment])
MC/DC0%0/1
$$aln{$args->{"query"}} // croak(qq[${sub}: the query "$args->{"query"}" isn't in the alignment])
$$aln{$args->{"query"}}
Truth table0%0/2
A
A= $$aln{$args->{"query"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | # 1-based residue number => 0-based alignment column | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my (@col, %sites); | |||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | while ($q =~ /[^-]/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | push @col, pos($q) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | foreach my $label (sort keys %{ $sites }) { | |||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | my $n = $sites->{$label}; | |||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | $sites{$label} = $col[$n - 1] // croak "$sub: active site $label ($n) is past the end of \"$args->{query}\", which has " . scalar(@col) . ' residues'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$col[$n - 1] // croak(qq[${sub}: active site $label ($n) is past the end of "$args->{"query"}", which has ] . scalar(@col) . ' residues')
MC/DC0%0/1
$col[$n - 1] // croak(qq[${sub}: active site $label ($n) is past the end of "$args->{"query"}", which has ] . scalar(@col) . ' residues')
$col[$n - 1]
Truth table0%0/2
A
A= $col[$n - 1]
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | @segments = ('--s', _json_text(\%sites)); | |||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | my %shown = map { ($labels->{$_} // $_) => $aln->{$_} } @order; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$labels->{$_} // $_
MC/DC0%0/2
$labels->{$_} // $_
$labels->{$_}
$_
Truth table0%0/3
A // B
A= $labels->{$_}B= $_
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | my $fa = hash2fasta_file(\%shown, _tmp('.fa'), [map { $labels->{$_} // $_ } @order], 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$labels->{$_} // $_
MC/DC0%0/2
$labels->{$_} // $_
$labels->{$_}
$_
Truth table0%0/3
A // B
A= $labels->{$_}B= $_
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | _python($sub, '--f', $fa, '--o', $args->{filename}, '--c', _creator($sub), '--quiet', | ||||||||||||||||||||||||||||||||||||||||||
| 383 | (defined $args->{title} ? ('--t', $args->{title}) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 384 | (defined $args->{xlabel} ? ('--x', $args->{xlabel}) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | (defined $args->{ylabel} ? ('--y', $args->{ylabel}) : ()), @segments); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | _wrote($args->{filename}, 43); # yellow | |||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | $r->{filename} = $args->{filename}; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | $r; | |||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # "path (SHA-256 hex)" of an existing file, for the provenance of an image. | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | ▶ | sub _file_id { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_id: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | require Digest::SHA; | |||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | File::Spec->rel2abs($file) . ' (SHA-256 ' . Digest::SHA->new(256)->addfile($file, 'b')->hexdigest . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | # Everything plot_phylo knows about where its tree came from, as the Dublin | ||||||||||||||||||||||||||||||||||||||||||
| 400 | # Core fields msa_plot.py writes into the image beside the Creator: what was | ||||||||||||||||||||||||||||||||||||||||||
| 401 | # read (Source), how the tree was made (Description), the files kept with it | ||||||||||||||||||||||||||||||||||||||||||
| 402 | # (Relation), and the Perl-side software that took part (Contributor). $cmd, | ||||||||||||||||||||||||||||||||||||||||||
| 403 | # $names and $msa are what _align returned, and undef when nothing was aligned. | ||||||||||||||||||||||||||||||||||||||||||
| 404 | # msa_plot.py adds the date, the newick it drew and its digest, the tip names, | ||||||||||||||||||||||||||||||||||||||||||
| 405 | # and its own Python, matplotlib, Biopython and NumPy. | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | ▶ | sub _phylo_provenance { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_phylo_provenance: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my ($args, $tree, $cmd, $names, $msa) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my $v = _alien('plot_phylo')->versions; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | my %p = (Title => $args->{title} // 'Phylogenetic tree'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"title"} // "Phylogenetic tree"
MC/DC0%0/1
$args->{"title"} // "Phylogenetic tree"
$args->{"title"}
Truth table0%0/2
A
A= $args->{"title"}
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | my @contrib = ("Bioinf::Basic $VERSION (" . __FILE__ . ')', "Alien::Bioinf $Alien::Bioinf::VERSION", | |||||||||||||||||||||||||||||||||||||||||
| 412 | "perl " . sprintf('%vd', $^V) . " ($^X)"); | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | if (defined $cmd) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | my $fasta = $args->{fasta}; | |||||||||||||||||||||||||||||||||||||||||
| 415 | # $cmd->[2] is clustalo's "--in", the ungapped copy _ungapped_fasta wrote | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | $p{Source} = (ref $fasta ? 'a hash ref of ' . scalar(@{ $names }) . ' sequences' : 'the FASTA file ' . _file_id($fasta)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | . ', written with its gaps stripped to ' . _file_id($cmd->[2]) . ', which Clustal Omega aligned into ' | ||||||||||||||||||||||||||||||||||||||||||
| 418 | . _file_id($msa); | ||||||||||||||||||||||||||||||||||||||||||
| 419 | $p{Description} = "The guide tree Clustal Omega $v->{clustalo} wrote while aligning " | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | . scalar(@{ $names }) . ' sequences, run as: ' . join(' ', map { /[^\w\/.,:=+-]/ ? "'$_'" : $_ } @{ $cmd }) . '.'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | unshift @contrib, "Clustal Omega $v->{clustalo} ($cmd->[0])"; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | ▶ | my @kept = map { defined $args->{$_} ? "$_ " . File::Spec->rel2abs($args->{$_}) : () } qw(msa.file tree.file); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | $p{Relation} = 'kept with it: ' . join('; ', @kept) if @kept; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | $p{Source} = 'the newick file ' . _file_id($tree); | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | $p{Description} = 'Drawn from an existing newick file; no alignment was made.'; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | $p{Contributor} = \@contrib; | |||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | \%p; | |||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | sub plot_phylo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plot_phylo: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | my $sub = 'plot_phylo'; | |||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my $args = _args(\@_, $sub, [], [qw(clustal.args fasta labels msa.file output.file threads title tree.file)]); | |||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | $args->{'output.file'} //= 'phylo.svg'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"output.file"} //= "phylo.svg"
MC/DC0%0/1
$args->{"output.file"} //= "phylo.svg"
$args->{"output.file"}
Truth table0%0/2
A
A= $args->{"output.file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | my ($r, $tree, $names, $cmd); | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if (defined $args->{fasta}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | ($r, $tree, $names, $cmd) = _align($args, $sub, 1) or return {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # no alignment to make, so "tree.file" is the tree to draw, not where to keep one | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | ▶ | $tree = $args->{'tree.file'} // croak "$sub: needs \"fasta\" to align, or \"tree.file\" to draw"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"tree.file"} // croak(qq[${sub}: needs "fasta" to align, or "tree.file" to draw])
MC/DC0%0/1
$args->{"tree.file"} // croak(qq[${sub}: needs "fasta" to align, or "tree.file" to draw])
$args->{"tree.file"}
Truth table0%0/2
A
A= $args->{"tree.file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | my @moot = grep { defined $args->{$_} } qw(clustal.args msa.file threads); | |||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | croak "$sub: was given " . join(', ', map { "\"$_\"" } @moot) . ' but no "fasta" to align' if @moot; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | croak "$sub: \"$tree\" doesn't exist or isn't a readable file" unless -f $tree && -r _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $tree and -r _
MC/DC0%0/2
-f $tree and -r _
-f $tree
-r _
Truth table0%0/3
A and B
A= -f $treeB= -r _
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | $r = { 'tree.file' => $tree }; | |||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | my $labels = $args->{labels} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"labels"} // {}
MC/DC0%0/1
$args->{"labels"} // {}
$args->{"labels"}
Truth table0%0/2
A
A= $args->{"labels"}
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | _python($sub, '--tree', $tree, '--o', $args->{'output.file'}, '--c', _creator($sub), | ||||||||||||||||||||||||||||||||||||||||||
| 449 | '--meta', _json_text(_phylo_provenance($args, $tree, $cmd, $names, $r->{'msa.file'})), | ||||||||||||||||||||||||||||||||||||||||||
| 450 | (defined $args->{title} ? ('--t', $args->{title}) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | ▶ | (%{ $labels } ? ('--l', _json_text($labels)) : ())); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | $r->{'output.file'} = $args->{'output.file'}; | |||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | $r; | |||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 2 | ▶ | sub msa_quality_table { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
msa_quality_table: called CC=54 SCAR=65.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 2 | my $sub = 'msa_quality_table'; | |||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | my $args = _args(\@_, $sub, ['filename'], [qw(alignment.json cb_label cb_max cb_min cblogscale default_undefined fasta | |||||||||||||||||||||||||||||||||||||||||
| 459 | logscale.add metric msa.file normalize order show.numbers title unaligned.fa)]); | ||||||||||||||||||||||||||||||||||||||||||
| 460 | 2 | my %metric = map { $_ => 1 } qw(num bit_score score evalue identity positive align_len); | |||||||||||||||||||||||||||||||||||||||||
| 461 | 2 | ▶ | my $metric = $args->{metric} // 'score'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"metric"} // "score"
MC/DC0%0/1
$args->{"metric"} // "score"
$args->{"metric"}
Truth table50%1/2
A
A= $args->{"metric"}
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | 2 | ▶ | croak "$sub: \"$metric\" isn't one of the metrics: " . join(', ', sort keys %metric) unless $metric{$metric}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 463 | # "unaligned.fa" is the old name for "fasta" | ||||||||||||||||||||||||||||||||||||||||||
| 464 | 2 | ▶ | croak "$sub: was given both \"fasta\" and \"unaligned.fa\", which are the same thing" if defined $args->{fasta} && defined $args->{'unaligned.fa'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"fasta"} and defined $args->{"unaligned.fa"}
MC/DC0%0/2
defined $args->{"fasta"} and defined $args->{"unaligned.fa"}
defined $args->{"fasta"}
defined $args->{"unaligned.fa"}
Truth table33%1/3
A and B
A= defined $args->{"fasta"}B= defined $args->{"unaligned.fa"}
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 2 | ▶ | my $fasta = $args->{fasta} // $args->{'unaligned.fa'}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args->{"fasta"} // $args->{"unaligned.fa"}
MC/DC0%0/2
$args->{"fasta"} // $args->{"unaligned.fa"}
$args->{"fasta"}
$args->{"unaligned.fa"}
Truth table33%1/3
A // B
A= $args->{"fasta"}B= $args->{"unaligned.fa"}
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | # All-against-all blastp of the sequences: given as the parsed report, read | ||||||||||||||||||||||||||||||||||||||||||
| 467 | # from "alignment.json", or -- when that file does not exist yet, or none is | ||||||||||||||||||||||||||||||||||||||||||
| 468 | # named -- made by running blastp on "fasta", and kept in "alignment.json" | ||||||||||||||||||||||||||||||||||||||||||
| 469 | # for next time if that is named. | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 2 | my $aj = $args->{'alignment.json'}; | |||||||||||||||||||||||||||||||||||||||||
| 471 | 2 | my $blast; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 2 | ▶ | if (ref $aj eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 2 | $blast = $aj; | |||||||||||||||||||||||||||||||||||||||||
| 474 | ▶ | } elsif (defined $aj && -f $aj) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $aj and -f $aj
MC/DC0%0/2
defined $aj and -f $aj
defined $aj
-f $aj
Truth table0%0/3
A and B
A= defined $ajB= -f $aj
| |||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | $blast = _json_file($aj); | |||||||||||||||||||||||||||||||||||||||||
| 476 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | croak "$sub: needs \"fasta\" to align, or an existing \"alignment.json\"" . (defined $aj ? " ($aj doesn't exist yet)" : '') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | unless defined $fasta; | ||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | my ($in) = _ungapped_fasta($fasta); | |||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | ▶ | $aj //= _tmp('.json'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$aj //= _tmp(".json")
MC/DC0%0/2
$aj //= _tmp(".json")
$aj
_tmp(".json")
Truth table0%0/3
A //= B
A= $ajB= _tmp(".json")
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | _run(_alien($sub)->blast('blastp'), '-query', $in, '-subject', $in, '-out', $aj, '-outfmt', 15); | |||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | $blast = _json_file($aj); | |||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 2 | my (%data, $max); | |||||||||||||||||||||||||||||||||||||||||
| 485 | 2 | foreach my $query (@{ $blast->{BlastOutput2} }) { | |||||||||||||||||||||||||||||||||||||||||
| 486 | 4 | foreach my $hit_list (@{ $query->{report}{results}{bl2seq} }) { | |||||||||||||||||||||||||||||||||||||||||
| 487 | 4 | foreach my $hit (@{ $hit_list->{hits} }) { | |||||||||||||||||||||||||||||||||||||||||
| 488 | 8 | ▶ | my $value = $hit->{hsps}[0]{$metric} // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$hit->{"hsps"}[0]{$metric} // (next)
MC/DC0%0/1
$hit->{"hsps"}[0]{$metric} // (next)
$hit->{"hsps"}[0]{$metric}
Truth table50%1/2
A
A= $hit->{"hsps"}[0]{$metric}
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | 8 | $data{ $hit_list->{query_title} }{ $hit->{description}[0]{title} } = $value; | |||||||||||||||||||||||||||||||||||||||||
| 490 | 8 | ▶ | $max = $value if !defined $max || $value > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $max or $value > $max
MC/DC50%1/2
not defined $max or $value > $max
not defined $max
$value > $max
Truth table66%2/3
not(A) or B
A= not defined $maxB= $value > $max
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||
| 494 | 2 | ▶ | croak "$sub: no \"$metric\" values in the BLAST report" unless defined $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 2 | ▶ | my @order = @{ $args->{order} // [sort { lc $a cmp lc $b } keys %data] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"order"} // [(sort {lc $a cmp lc $b;} keys %data)]
MC/DC100%1/1
$args->{"order"} // [(sort {lc $a cmp lc $b;} keys %data)]
$args->{"order"}
Truth table100%2/2
A
A= $args->{"order"}
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 2 | my (@row_labels, @names); | |||||||||||||||||||||||||||||||||||||||||
| 497 | 2 | foreach my $key (@order) { | |||||||||||||||||||||||||||||||||||||||||
| 498 | 3 | ▶ | my ($genus, $species) = $key =~ /^([^.]+)\.(.+)/ or croak "$sub: can't get a genus and species from \"$key\""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 3 | my ($sp, @strain) = split /\./, $species; # C.neoformans.B.3501A | |||||||||||||||||||||||||||||||||||||||||
| 500 | 3 | push @names, [$genus, $sp, "@strain"]; | |||||||||||||||||||||||||||||||||||||||||
| 501 | 3 | ▶ | push @row_labels, '$\it{' . ucfirst(_first_letter($genus)) . ". $sp}\$" . (@strain ? " @strain" : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 2 | my @col_labels = @{ _abbreviated_labels(\@names) }; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 2 | ▶ | my $add = $args->{'logscale.add'} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"logscale.add"} // 0
MC/DC0%0/1
$args->{"logscale.add"} // 0
$args->{"logscale.add"}
Truth table50%1/2
A
A= $args->{"logscale.add"}
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 2 | ▶ | my $norm = ($args->{normalize} // 0) > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"normalize"} // 0
MC/DC100%1/1
$args->{"normalize"} // 0
$args->{"normalize"}
Truth table100%2/2
A
A= $args->{"normalize"}
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | # Normalized, every value is divided by the largest, with "logscale.add" | ||||||||||||||||||||||||||||||||||||||||||
| 507 | # added to both so that the largest is still 1. That needs a largest above | ||||||||||||||||||||||||||||||||||||||||||
| 508 | # 0, which "evalue" does not have when BLAST has rounded every e-value to 0. | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 2 | ▶ | croak "$sub: can't normalize \"$metric\", since its largest value" . ($add ? ' plus "logscale.add"' : '') . ' is ' . ($max + $add) . ', not above 0' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$norm and $max + $add <= 0
MC/DC50%1/2
$norm and $max + $add <= 0
$norm
$max + $add <= 0
Truth table66%2/3
A and B
A= $normB= $max + $add <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | if $norm && $max + $add <= 0; | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 1 | my $log = $args->{cblogscale}; | |||||||||||||||||||||||||||||||||||||||||
| 512 | 1 | my (@cells, $lo, $hi, $lo_positive); | |||||||||||||||||||||||||||||||||||||||||
| 513 | 1 | foreach my $i (0 .. $#order) { | |||||||||||||||||||||||||||||||||||||||||
| 514 | 1 | foreach my $j (0 .. $#order) { | |||||||||||||||||||||||||||||||||||||||||
| 515 | # a pair with no hit stays out of the table, rather than claiming a | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # score of 0, and is drawn grey | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 1 | ▶ | my $value = $data{ $order[$i] }{ $order[$j] } // $args->{default_undefined}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$data{$order[$i]}{$order[$j]} // $args->{"default_undefined"}
MC/DC0%0/2
$data{$order[$i]}{$order[$j]} // $args->{"default_undefined"}
$data{$order[$i]}{$order[$j]}
$args->{"default_undefined"}
Truth table33%1/3
A // B
A= $data{$order[$i]}{$order[$j]}B= $args->{"default_undefined"}
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 1 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | $value += $add; | |||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | $value /= $max + $add if $norm; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 0 | ▶ | $lo = $value if !defined $lo || $value < $lo; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $lo or $value < $lo
MC/DC0%0/2
not defined $lo or $value < $lo
not defined $lo
$value < $lo
Truth table0%0/3
not(A) or B
A= not defined $loB= $value < $lo
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | ▶ | $hi = $value if !defined $hi || $value > $hi; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $hi or $value > $hi
MC/DC0%0/2
not defined $hi or $value > $hi
not defined $hi
$value > $hi
Truth table0%0/3
not(A) or B
A= not defined $hiB= $value > $hi
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | ▶ | $lo_positive = $value if $value > 0 && (!defined $lo_positive || $value < $lo_positive); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$value > 0 and !defined($lo_positive) || $value < $lo_positive
not defined $lo_positive or $value < $lo_positive
MC/DC0%0/3
$value > 0 and !defined($lo_positive) || $value < $lo_positive
$value > 0
not defined $lo_positive
$value < $lo_positive
Truth table0%0/4
A and not(B) or C
A= $value > 0B= not defined $lo_positiveC= $value < $lo_positive
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 1 | $cells[$i][$j] = $value; | |||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 1 | ▶ | croak "$sub: no pair of the sequences in \"order\" has a \"$metric\" value, or a \"default_undefined\", to draw" unless defined $hi; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | # A normalized scale runs from 0 to 1, but a log scale can't start at 0 or | ||||||||||||||||||||||||||||||||||||||||||
| 530 | # below, so it starts at the smallest value above 0 instead, as it does | ||||||||||||||||||||||||||||||||||||||||||
| 531 | # when not normalized. | ||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | ▶ | @{ $args }{qw(cb_min cb_max)} = ($log ? undef : 0, 1) if $norm; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | ▶ | $lo = $args->{cb_min} // ($log ? $lo_positive : $lo); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"cb_min"} // ($log ? $lo_positive : $lo)
MC/DC0%0/2
$args->{"cb_min"} // ($log ? $lo_positive : $lo)
$args->{"cb_min"}
($log ? $lo_positive : $lo)
Truth table0%0/3
A // B
A= $args->{"cb_min"}B= ($log ? $lo_positive : $lo)
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | ▶ | $hi = $args->{cb_max} // $hi; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"cb_max"} // $hi
MC/DC0%0/2
$args->{"cb_max"} // $hi
$args->{"cb_max"}
$hi
Truth table0%0/3
A // B
A= $args->{"cb_max"}B= $hi
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | ▶ | croak "$sub: \"cblogscale\" needs a value above 0 to start the scale at, or a \"cb_min\" above 0" if $log && (!defined $lo || $lo <= 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$log and !defined($lo) || $lo <= 0
not defined $lo or $lo <= 0
MC/DC0%0/3
$log and !defined($lo) || $lo <= 0
$log
not defined $lo
$lo <= 0
Truth table0%0/4
A and not(B) or C
A= $logB= not defined $loC= $lo <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | my $table = _tmp('.json'); | |||||||||||||||||||||||||||||||||||||||||
| 538 | 0 | ▶ | open my $fh, '>:raw', $table or croak "$sub: can't write $table: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | ▶ | print {$fh} JSON::MaybeXS::encode_json({ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$args->{"title"} // ""
MC/DC0%0/1
$args->{"title"} // ""
$args->{"title"}
Truth table0%0/2
A
A= $args->{"title"}
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | cells => \@cells, cols => \@col_labels, rows => \@row_labels, vmin => $lo + 0, vmax => $hi + 0, | ||||||||||||||||||||||||||||||||||||||||||
| 541 | log => $log ? JSON::MaybeXS::true() : JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||||||||||
| 542 | numbers => $args->{'show.numbers'} ? JSON::MaybeXS::true() : JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||||||||||
| 543 | title => $args->{title} // '', cblabel => $args->{cb_label}, | ||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | ▶ | close $fh or croak "$sub: can't write $table: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | _python($sub, '--table', $table, '--o', $args->{filename}, '--c', _creator($sub)); | |||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | $args->{filename}; | |||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | # $text with every character that LaTeX treats specially written so that it | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # prints as itself. "\^" would be a circumflex accent, not a caret, and "\~" a | ||||||||||||||||||||||||||||||||||||||||||
| 552 | # tilde accent, so those two (and the backslash) are spelled as text commands. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 2 | ▶ | sub _latex_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_latex_text: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | 2 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 555 | 2 | my %special = ('\\' => '\\textbackslash{}', '^' => '\\textasciicircum{}', '~' => '\\textasciitilde{}'); | |||||||||||||||||||||||||||||||||||||||||
| 556 | 2 | ▶ | $text =~ s/([\\^~#\$%&_{}])/$special{$1} \/\/ "\\$1"/ge; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$special{$1} // "\\$1"
MC/DC100%1/1
$special{$1} // "\\$1"
$special{$1}
Truth table100%2/2
A
A= $special{$1}
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 2 | $text; | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | 9 | ▶ | sub clustal_view_residues { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clustal_view_residues: called CC=50 SCAR=55.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | 9 | my $sub = 'clustal_view_residues'; | |||||||||||||||||||||||||||||||||||||||||
| 562 | # msa.file: a FASTA file, aligned (such as plot_msa's) or not; color.residues: | ||||||||||||||||||||||||||||||||||||||||||
| 563 | # {protein => {1-based residue number => colour}}, the colour an xcolor | ||||||||||||||||||||||||||||||||||||||||||
| 564 | # name or an [r, g, b] ref; order: proteins top to bottom; row.width: | ||||||||||||||||||||||||||||||||||||||||||
| 565 | # columns per block (100); split: blocks per LaTeX table (4); track: a | ||||||||||||||||||||||||||||||||||||||||||
| 566 | # protein whose coloured residue numbers get a row of their own; threads and | ||||||||||||||||||||||||||||||||||||||||||
| 567 | # clustal.args: for clustalo, if msa.file has to be aligned. | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 9 | my $args = _args(\@_, $sub, ['msa.file', 'output.tex.file'], [qw(caption clustal.args color.residues label | |||||||||||||||||||||||||||||||||||||||||
| 569 | order row.width split table.text.size threads track)]); | ||||||||||||||||||||||||||||||||||||||||||
| 570 | 9 | ▶ | my $color = $args->{'color.residues'} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"color.residues"} // {}
MC/DC100%1/1
$args->{"color.residues"} // {}
$args->{"color.residues"}
Truth table100%2/2
A
A= $args->{"color.residues"}
| |||||||||||||||||||||||||||||||||||||||||||
| 571 | 9 | ▶ | croak "$sub: \"color.residues\" must be a hash ref" unless ref $color eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | # 0 would never end the loop over blocks, and divide by 0 in the tables | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 9 | foreach my $k ('row.width', 'split') { | |||||||||||||||||||||||||||||||||||||||||
| 574 | 15 | ▶ | croak "$sub: \"$k\" must be a whole number, 1 or more, not \"$args->{$k}\"" if defined $args->{$k} && $args->{$k} !~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args->{$k} and not $args->{$k} =~ /\A[1-9][0-9]*\z/
MC/DC50%1/2
defined $args->{$k} and not $args->{$k} =~ /\A[1-9][0-9]*\z/
defined $args->{$k}
not $args->{$k} =~ /\A[1-9][0-9]*\z/
Truth table66%2/3
A and not(B)
A= defined $args->{$k}B= not $args->{$k} =~ /\A[1-9][0-9]*\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | 3 | ▶ | croak "$sub: \"msa.file\" $args->{'msa.file'} doesn't exist" unless -e $args->{'msa.file'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 577 | 3 | my $data = fasta2hash($args->{'msa.file'}); | |||||||||||||||||||||||||||||||||||||||||
| 578 | 3 | ▶ | croak "$sub: has no sequences to show in $args->{'msa.file'}" unless %{ $data }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 3 | my %len = map { length $_ => 1 } values %{ $data }; | |||||||||||||||||||||||||||||||||||||||||
| 580 | 3 | ▶ | if (keys %len > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | # Sequences of different lengths can't be an alignment, so align them, | ||||||||||||||||||||||||||||||||||||||||||
| 582 | # into a temporary file: "msa.file" is the input, and is never written. | ||||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | my %clustal = map { $_ => $args->{$_} } grep { defined $args->{$_} } qw(clustal.args threads); | |||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | my ($r) = _align({ %clustal, fasta => $args->{'msa.file'} }, $sub, 0); | |||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | $data = fasta2hash($r->{'msa.file'}); | |||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | %len = map { length $_ => 1 } values %{ $data }; | |||||||||||||||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||||||||||||||||
| 588 | 3 | my $track = $args->{track}; | |||||||||||||||||||||||||||||||||||||||||
| 589 | 3 | ▶ | croak "$sub: tracker \"$track\" isn't in the alignment" if defined $track && !defined $data->{$track}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $track and not defined $data->{$track}
MC/DC0%0/2
defined $track and not defined $data->{$track}
defined $track
not defined $data->{$track}
Truth table33%1/3
A and not(B)
A= defined $trackB= not defined $data->{$track}
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | 3 | my ($aln_len) = keys %len; | |||||||||||||||||||||||||||||||||||||||||
| 591 | 3 | my @undef = grep { !defined $data->{$_} } sort keys %{ $color }; | |||||||||||||||||||||||||||||||||||||||||
| 592 | 3 | ▶ | croak "$sub: \"color.residues\" names proteins that aren't in the alignment: @undef" if @undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 593 | 3 | ▶ | my @proteins = @{ $args->{order} // [sort { lc $a cmp lc $b } keys %{ $data }] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"order"} // [(sort {lc $a cmp lc $b;} keys %{$data;})]
MC/DC0%0/1
$args->{"order"} // [(sort {lc $a cmp lc $b;} keys %{$data;})]
$args->{"order"}
Truth table50%1/2
A
A= $args->{"order"}
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | 3 | my @bad = grep { !defined $data->{$_} } @proteins; | |||||||||||||||||||||||||||||||||||||||||
| 595 | 3 | ▶ | croak "$sub: \"order\" names proteins that aren't in the alignment: @bad" if @bad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 596 | 3 | ▶ | my $width = $args->{'row.width'} // 100; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"row.width"} // 100
MC/DC0%0/1
$args->{"row.width"} // 100
$args->{"row.width"}
Truth table50%1/2
A
A= $args->{"row.width"}
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | # Alignment column => colour, and for the tracked protein column => its | ||||||||||||||||||||||||||||||||||||||||||
| 598 | # residue number. A coloured column is coloured in every protein. | ||||||||||||||||||||||||||||||||||||||||||
| 599 | 3 | my (%col_color, %col_number); | |||||||||||||||||||||||||||||||||||||||||
| 600 | 3 | foreach my $protein (sort keys %{ $color }) { | |||||||||||||||||||||||||||||||||||||||||
| 601 | 2 | my @col; | |||||||||||||||||||||||||||||||||||||||||
| 602 | 2 | ▶ | while ($data->{$protein} =~ /[A-Za-z]/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 603 | 8 | push @col, pos($data->{$protein}) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | foreach my $n (sort { $a <=> $b } keys %{ $color->{$protein} }) { | |||||||||||||||||||||||||||||||||||||||||
| 606 | 2 | my $c = $color->{$protein}{$n}; | |||||||||||||||||||||||||||||||||||||||||
| 607 | 2 | _residue_number($sub, "a residue of $protein in \"color.residues\"", $n); | |||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | ▶ | my $col = $col[$n - 1] // croak "$sub: $protein has no residue $n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$col[$n - 1] // croak("${sub}: $protein has no residue $n")
MC/DC0%0/1
$col[$n - 1] // croak("${sub}: $protein has no residue $n")
$col[$n - 1]
Truth table0%0/2
A
A= $col[$n - 1]
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | ▶ | croak "$sub: the colour of $protein residue $n must be a name or 3 numbers" if ref $c && (ref $c ne 'ARRAY' || @{ $c } != 3); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $c and ref $c ne "ARRAY" || @{$c;} != 3
ref $c ne "ARRAY" or @{$c;} != 3
MC/DC0%0/3
ref $c and ref $c ne "ARRAY" || @{$c;} != 3
ref $c
ref $c ne "ARRAY"
@{$c;} != 3
Truth table0%0/4
A and B or C
A= ref $cB= ref $c ne "ARRAY"C= @{$c;} != 3
| |||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | $col_color{$col} = $c; | |||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | ▶ | $col_number{$col} = $n if defined $track && $protein eq $track; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $track and $protein eq $track
MC/DC0%0/2
defined $track and $protein eq $track
defined $track
$protein eq $track
Truth table0%0/3
A and B
A= defined $trackB= $protein eq $track
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 1 | my (@table, %count); | |||||||||||||||||||||||||||||||||||||||||
| 615 | 1 | ▶ | for (my $start = 0; $start < $aln_len; $start += $width) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 616 | 1 | ▶ | my $w = $aln_len - $start < $width ? $aln_len - $start : $width; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | 1 | foreach my $protein (@proteins) { | |||||||||||||||||||||||||||||||||||||||||
| 618 | 2 | my @seq = split //, substr($data->{$protein}, $start, $w); | |||||||||||||||||||||||||||||||||||||||||
| 619 | 2 | $count{$protein} += grep { /[A-Za-z]/ } @seq; | |||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | ▶ | foreach my $col (grep { $_ >= $start && $_ < $start + $w } keys %col_color) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_ >= $start && $_ < $start + $w
MC/DC0%0/2
$_ >= $start && $_ < $start + $w
$_ >= $start
$_ < $start + $w
Truth table0%0/3
A && B
A= $_ >= $startB= $_ < $start + $w
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | my ($c, $i) = ($col_color{$col}, $col - $start); | |||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | ▶ | $seq[$i] = ref $c ? '{\color[rgb]{' . join(',', @{ $c }) . "}$seq[$i]}" : "\\textcolor{$c}{$seq[$i]}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 2 | my $name = _latex_text($protein); | |||||||||||||||||||||||||||||||||||||||||
| 625 | 2 | push @table, ["\\textit{$name}", '\texttt{' . join('', @seq) . '}', $count{$protein}]; | |||||||||||||||||||||||||||||||||||||||||
| 626 | 2 | ▶ | next unless defined $track && $protein eq $track; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $track and $protein eq $track
MC/DC0%0/2
defined $track and $protein eq $track
defined $track
$protein eq $track
Truth table33%1/3
A and B
A= defined $trackB= $protein eq $track
| |||||||||||||||||||||||||||||||||||||||||||
| 627 | # Each residue number written from its own column rightwards, one | ||||||||||||||||||||||||||||||||||||||||||
| 628 | # digit per column so the row stays aligned, and moved right past | ||||||||||||||||||||||||||||||||||||||||||
| 629 | # the end of the previous number rather than overwriting it. | ||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | my @row = ('-') x $w; | |||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | my $free = 0; # first column not yet taken by a number | |||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | ▶ | foreach my $col (sort { $a <=> $b } grep { $_ >= $start && $_ < $start + $w } keys %col_number) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_ >= $start && $_ < $start + $w
MC/DC0%0/2
$_ >= $start && $_ < $start + $w
$_ >= $start
$_ < $start + $w
Truth table0%0/3
A && B
A= $_ >= $startB= $_ < $start + $w
| |||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | ▶ | my $at = $col - $start < $free ? $free : $col - $start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | my @digits = split //, $col_number{$col}; | |||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | ▶ | last if $at + @digits > $w; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 636 | 0 | @row[$at .. $at + $#digits] = @digits; | |||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | $free = $at + @digits + 1; | |||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | push @table, ["\\textit{$name} track", '\texttt{' . join('', @row) . '}', $count{$protein}]; | |||||||||||||||||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||||||||||||||||
| 641 | 1 | push @table, ['\hline']; | |||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||
| 643 | 1 | ▶ | my $per_table = ($args->{split} // 4) * (1 + @proteins + (defined $track ? 1 : 0)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$args->{"split"} // 4
MC/DC0%0/1
$args->{"split"} // 4
$args->{"split"}
Truth table50%1/2
A
A= $args->{"split"}
| |||||||||||||||||||||||||||||||||||||||||||
| 644 | 1 | ▶ | my $size = $args->{'table.text.size'} // '\footnotesize'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"table.text.size"} // "\\footnotesize"
MC/DC0%0/1
$args->{"table.text.size"} // "\\footnotesize"
$args->{"table.text.size"}
Truth table50%1/2
A
A= $args->{"table.text.size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 645 | 1 | ▶ | my $caption = $args->{caption} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"caption"} // ""
MC/DC0%0/1
$args->{"caption"} // ""
$args->{"caption"}
Truth table50%1/2
A
A= $args->{"caption"}
| |||||||||||||||||||||||||||||||||||||||||||
| 646 | 1 | ▶ | open my $tex, '>', $args->{'output.tex.file'} or croak "$sub: can't write $args->{'output.tex.file'}: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 647 | 1 | print {$tex} "%written by $0, calling $sub in " . __FILE__ . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 648 | 1 | my $n_tables = int((@table + $per_table - 1) / $per_table); | |||||||||||||||||||||||||||||||||||||||||
| 649 | 1 | foreach my $t (0 .. $n_tables - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 650 | 1 | my $end = ($t + 1) * $per_table - 1; | |||||||||||||||||||||||||||||||||||||||||
| 651 | 1 | ▶ | $end = $#table if $end > $#table; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 652 | 1 | print {$tex} "\\begin{table}[htp]$size\n\\begin{tabular}{|c|l|c|} \\hline\n", | |||||||||||||||||||||||||||||||||||||||||
| 653 | '\textbf{Track} & \textbf{Sequence} & \textbf{Length}\\\\ \hline', "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 1 | ▶ | print {$tex} join(' & ', @{ $_ }), ($_->[-1] eq '\hline' ? "\n" : "\n\\\\\n") foreach @table[$t * $per_table .. $end]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 655 | 1 | ▶ | print {$tex} "\\end{tabular}\n\\caption{$caption", ($t > 0 && $caption ne '' ? ' (continued)' : ''), "}\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$t > 0 and $caption ne ""
MC/DC0%0/2
$t > 0 and $caption ne ""
$t > 0
$caption ne ""
Truth table33%1/3
A and B
A= $t > 0B= $caption ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | ▶ | print {$tex} "\\label{tab:$args->{label}", ($n_tables > 1 ? $t : ''), "}\n" if defined $args->{label}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | 1 | print {$tex} "\\end{table} \\FloatBarrier\n"; | |||||||||||||||||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 1 | ▶ | close $tex or croak "$sub: can't write $args->{'output.tex.file'}: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 660 | 1 | _wrote($args->{'output.tex.file'}, 46); # cyan | |||||||||||||||||||||||||||||||||||||||||
| 661 | 1 | $args->{'output.tex.file'}; | |||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||
| 666 | __END__ | ||||||||||||||||||||||||||||||||||||||||||