lib/Bio/BPWrapper/AlnManipulations.pm

filter: Statementstmt 56.7% 372 / 655 Branchbran 46.9% 106 / 226 Conditioncond 52.3% 22 / 42 MC/DCmcdc 32.0% 8 / 25 Subroutinesub 77.9% 53 / 68 Podpod 58.9% 23 / 39 totaltotal 55.3% 584 / 1055 CC 192 SCAR 42.8
CC
192
Cov
54%
trim_ends
342.0
gap_states_matrix
306.0
gap_states
182.0
CRAP 72.0
« blib/lib/Bio/BPWrapper/TreeManipulations.pm ↑ Summary blib/lib/Bio/BPWrapper/AlnManipulations.pm »
1 =encoding utf8
2  
3 =head1 NAME
4  
5 Bio::BPWrapper::AlnManipulations - Functions for L<bioaln>
6  
7 =head1 SYNOPSIS
8  
9 use Bio::BPWrapper::AlnManipulations;
10 # Set options hash ...
11 initialize(\%opts);
12 write_out(\%opts);
13  
14 =cut
15  
16 package Bio::BPWrapper::AlnManipulations;
17  
18 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 29 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 29 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 29 ▶use Bio::SimpleAlign;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 29 ▶use Bio::LocatableSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 29 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 29 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 29 ▶use Bio::Align::Utilities qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 29 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 29 ▶if ($ENV{'DEBUG'}) { use Data::Dumper }
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"}) 0 29 29
30  
31 29 ▶use vars qw(@ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 29 @ISA = qw(Exporter);
34  
35 # FIXME: some of these have too generic names like
36 # "upper_case" or "concat".
37 29 @EXPORT = qw(initialize can_handle handle_opt write_out write_out_paml
38 phylip_non_interleaved split_cdhit trim_ends gap_states
39 gap_states_matrix print_avp_id bootstrap draw_codon_view del_seqs
40 remove_gaps print_length print_match print_num_seq pick_seq
41 change_ref aln_slice get_uniq binary_informative variables_sites
42 avg_id_by_win concat conserve_blocks get_consensus dns_to_protein
43 remove_gapped_cols_in_one_seq colnum_from_residue_pos
44 list_ids premute_states protein_to_dna sample_seqs
45 shuffle_sites random_slice select_third_sites remove_third_sites
46 upper_case );
47  
48 29 ▶use Bio::BPWrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 # Package global variables
50 29 my ($in, $out, $aln, %opts, $file, $in_format, $out_format, @alns, $binary);
51  
52 29 my $VERSION = $Bio::BPWrapper::VERSION;
53  
54 ## For new options, just add an entry into this table with the same
55 ## key as in the GetOpts function in the main program. Make the key be
56 ## a reference to the handler subroutine (defined below), and test
57 ## that it works.
58 29 my %opt_dispatch = (
59 "avpid" => \&print_avp_id,
60 "bootstrap" => \&bootstrap,
61 "codon-view" => \&draw_codon_view,
62 "delete" => \&del_seqs,
63 "nogaps" => \&remove_gaps,
64 "length" => \&print_length,
65 "match" => \&print_match,
66 "numseq" => \&print_num_seq,
67 "pick" => \&pick_seqs,
68 "refseq" => \&change_ref,
69 "slice" => \&aln_slice,
70 "split-cdhit" => \&split_cdhit,
71 "uniq" => \&get_unique,
72 "varsites" => \&variable_sites,
73 "window" => \&avg_id_by_win,
74 "concat" => \&concat,
75 "conblocks" => \&conserved_blocks,
76 "consensus" => \&get_consensus,
77 "dna2pep" => \&dna_to_protein,
78 "erasecol" => \&remove_gapped_cols_in_one_seq,
79 "aln-index" => \&colnum_from_residue_pos,
80 "listids" => \&list_ids,
81 "permute-states" => \&permute_states,
82 "pep2dna" => \&protein_to_dna,
83 "resample" => \&sample_seqs,
84 "shuffle-sites" => \&shuffle_sites,
85 "select-third" => \&select_third_sites,
86 "remove-third" => \&remove_third_sites,
87 "random-slice" => \&random_slice,
88 "uppercase" => \&upper_case,
89 "gapstates" => \&gap_states,
90 "gapstates2" => \&gap_states_matrix,
91 "trimends" => \&trim_ends,
92 "bin-inform" => \&binary_informative,
93 "phy-nonint" => \&phylip_non_interleaved
94 );
95  
96  
97 ##################### initializer & option handlers ###################
98  
99 ## TODO Formal testing!
100  
101 =head1 SUBROUTINES
102  
103 =head2 initialize()
104  
105 Sets up most of the actions to be performed on an alignment.
106  
107 Call this right after setting up an options hash.
108  
109 Sets package variables: C<$in_format>, C<$binary>, C<$out_format>, and C<$out>.
110  
111  
112 =cut
113  
114 sub initialize {
115 29 ▶ my $opts_ref = shift;
Subroutine initialize: called CC=10 SCAR=23.4
Pod documented
116 29 Bio::BPWrapper::common_opts($opts_ref);
117 29 %opts = %{$opts_ref};
118  
119 # This is the format that aln-manipulations expects by default
120 29 my $default_format = "clustalw";
121  
122 # assume we're getting input from standard input
123  
124 29 ▶ $in_format = $opts{"input"} || $default_format;
Condition66%2/3
$opts{'input'} || $default_format
l!l&&r!l&&!r
1280
MC/DC0%0/2
$opts{'input'} || $default_format
$opts{'input'} $default_format
Truth table66%2/3
A || B
A= $opts{'input'}B= $default_format
ABresult
1X1
011
000
125  
126 29 ▶ if ($opts{"concat"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'concat'}) { } 1 28 29
127 1 ▶ while ($file = shift @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file = shift @ARGV) 1 1 2
128 1 $in = Bio::AlignIO->new(-file => $file, -format => $in_format);
129 1 ▶ while ($aln=$in->next_aln()) { push @alns, $aln }
Branch: 1 branch
BranchTrueFalseTotal
if ($aln = $in->next_aln) 1 1 2
130 }
131 } else {
132 28 ▶ $file = shift @ARGV || "STDIN"; # If no more arguments were given on the command line,
Condition50%1/2
shift @ARGV || 'STDIN'
l!l
280
MC/DC0%0/1
shift @ARGV || 'STDIN'
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
133 28 ▶ $in = Bio::AlignIO->new(-format => $in_format, ($file eq "STDIN")? (-fh => \*STDIN) : (-file => $file));
Branch: 1 branch
BranchTrueFalseTotal
$file eq 'STDIN' ? : 0 28 28
134 28 $aln = $in->next_aln()
135 }
136  
137 29 ▶ $binary = $opts{"binary"} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'binary'} ? : 0 29 29
138  
139 #### Options which *require an output FH* go *after* this ####
140 29 ▶ $out_format = $opts{"output"} || $default_format;
Condition66%2/3
$opts{'output'} || $default_format
l!l&&r!l&&!r
1280
MC/DC0%0/2
$opts{'output'} || $default_format
$opts{'output'} $default_format
Truth table66%2/3
A || B
A= $opts{'output'}B= $default_format
ABresult
1X1
011
000
141 29 ▶ $out = Bio::AlignIO->new(-format => $out_format, -fh => \*STDOUT) unless $out_format eq 'paml'
Branch: 1 branch
BranchTrueFalseTotal
unless $out_format eq "paml" 29 0 29
142 }
143  
144 sub can_handle {
145 26 ▶ my $option = shift;
Subroutine can_handle: called CC=1 SCAR=0.0
Pod undocumented
146 26 return defined($opt_dispatch{$option})
147 }
148  
149 sub handle_opt {
150 26 ▶ my $option = shift;
Subroutine handle_opt: called CC=1 SCAR=0.0
Pod undocumented
151 # This passes option name to all functions
152 26 $opt_dispatch{$option}->($option)
153 }
154  
155 =head2 write_out_paml()
156  
157 Writes output in PAML format.
158  
159 =cut
160  
161 sub write_out_paml() {
162 0 ▶ my @seq;
Subroutine write_out_paml: not called CC=5 SCAR=34.0
Pod documented
163 0 my $ct=0;
164  
165 0 foreach my $seq ($aln->each_seq()) {
166 0 my $id = $seq->display_id();
167 0 ▶ if ($seq->seq() =~ /^-+$/) { print STDERR "all gaps: $file\t$id\n"; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->seq =~ /^-+$/) 0 0 0
168 0 $ct++;
169 0 push @seq, $seq
170 }
171  
172 0 ▶ die "No computable sequences: less than 2 seq.\n" unless $ct >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct >= 2 0 0 0
173 0 print $ct, "\t", $aln->length(), "\n";
174 0 foreach (@seq) {
175 0 print $_->display_id(), "\n";
176 0 print $_->seq(), "\n"
177 }
178 }
179  
180 =head2 write_out()
181  
182 Performs the bulk of the alignment actions actions set via
183 L<C<initialize(\%opts)>|/initialize> and calls
184 L<C<$AlignIO-E<gt>write_aln()>|https://metacpan.org/pod/Bio::AlignIO#write_aln>
185 or L<C<write_out_paml()>|/write_out_paml>.
186  
187 Call this after calling C<#initialize(\%opts)>.
188  
189 =cut
190  
191 sub write_out($) {
192  
193 29 ▶ my $opts = shift;
Subroutine write_out: called CC=9 SCAR=22.5
Pod documented
194 29 for my $option (keys %{$opts}) {
195 29 ▶ next if ($option eq 'input') || ($option eq 'output') || ($option eq 'noflatname') || ($option eq 'binary'); # Don't process these options: they are for AlignIO
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "input" or $option eq "output" or $option eq "noflatname" or $option eq "binary" 1 26 27
Condition88%8/9
$option eq "input" or $option eq "output" or $option eq "noflatname" or $option eq "binary"
l!l&&r!l&&!r
3026
$option eq "input" or $option eq "output" or $option eq "noflatname"
l!l&&r!l&&!r
2126
$option eq "input" or $option eq "output"
l!l&&r!l&&!r
1127
MC/DC75%3/4
$option eq "input" or $option eq "output" or $option eq "noflatname" or $option eq "binary"
$option eq "input" $option eq "output" $option eq "noflatname" $option eq "binary"
Truth table80%4/5
A or B or C or D
A= $option eq "input"B= $option eq "output"C= $option eq "noflatname"D= $option eq "binary"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
196  
197 26 ▶ if (can_handle($option)) { handle_opt($option) } # If there is a function to handle the current option, execute it
Branch: 1 branch
BranchTrueFalseTotal
if (can_handle($option)) { } 26 0 26
198 0 else { warn "Missing handler for: $option\n" }
199 }
200  
201  
202 21 ▶ $aln->set_displayname_flat() unless $opts{"noflatname"};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noflatname"} 20 1 21
203 21 ▶ if ($out_format eq 'paml') { &write_out_paml($aln) }
Branch: 1 branch
BranchTrueFalseTotal
if ($out_format eq 'paml') { } 0 21 21
204 21 else { $out->write_aln($aln) }
205 }
206  
207 sub phylip_non_interleaved {
208 0 ▶ my @seq;
Subroutine phylip_non_interleaved: not called CC=5 SCAR=34.0
Pod undocumented
209 0 my $ct=0;
210  
211 0 foreach my $seq ($aln->each_seq()) {
212 0 my $id = $seq->display_id();
213 0 ▶ if ($seq->seq() =~ /^-+$/) { print STDERR "all gaps: $file\t$id\n"; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->seq =~ /^-+$/) 0 0 0
214 0 $ct++;
215 0 push @seq, $seq
216 }
217  
218 0 ▶ die "No computable sequences: less than 2 seq.\n" unless $ct >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct >= 2 0 0 0
219 0 print "\t", $ct, "\t", $aln->length(), "\n";
220 0 foreach (@seq) {
221 0 printf "%-50s", $_->display_id();
222 0 print $_->seq(), "\n"
223 }
224 0 exit;
225 }
226  
227 ###################### subroutine ######################
228  
229 sub split_cdhit {
230 0 ▶ my $cls_file = $opts{'split-cdhit'};
Subroutine split_cdhit: not called CC=7 SCAR=40.3
Pod undocumented
231 0 ▶ open IN, "<" . $cls_file || die "cdhit clstr file not found: $cls_file\n";
Condition0%0/2
'<' . $cls_file || die("cdhit clstr file not found: $cls_file\n")
l!l
00
MC/DC0%0/1
'<' . $cls_file || die("cdhit clstr file not found: $cls_file\n")
'<' . $cls_file
Truth table0%0/2
A
A= '<' . $cls_file
Aresult
11
00
232 0 my %clusters;
233 my $cl_id;
234 0 my @mem;
235 0 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
236 0 my $line = $_;
237 0 chomp $line;
238 0 ▶ if ($line =~ /^>(\S+)\s+(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^>(\S+)\s+(\d+)/) { } 0 0 0
239 0 $cl_id = $1 . "_" . $2;
240 0 my @mem = ();
241 0 $clusters{$cl_id} = \@mem;
242 } else {
243 0 my $ref = $clusters{$cl_id};
244 0 my @mems = @$ref;
245 0 my @els = split /\s+/, $line;
246 0 my $seq_id = $els[2];
247 0 $seq_id =~ s/>//;
248 0 $seq_id =~ s/\.\.\.$//;
249 0 push @mems, $seq_id;
250 0 $clusters{$cl_id} = \@mems;
251 }
252 }
253 # print Dumper(\%clusters);
254 0 my %seqs;
255 0 foreach my $seq ($aln->each_seq()) {
256 0 my $id = $seq->display_id();
257 0 $seqs{$id} = $seq;
258 }
259  
260 0 foreach my $id (keys %clusters) {
261 0 my $out = Bio::AlignIO->new( -file => ">" . $file . "-". $id . ".aln", -format => 'clustalw');
262 0 my $new_aln = Bio::SimpleAlign->new();
263 0 my @seqids = @{ $clusters{$id} };
264 0 foreach my $seq_id (@seqids) {
265 0 $new_aln->add_seq($seqs{$seq_id});
266 }
267 0 $new_aln->set_displayname_flat();
268 # $new_aln = &_remove_common_gaps($new_aln);
269 0 $out->write_aln($new_aln);
270 }
271 0 exit;
272 }
273  
274 ▶sub _remove_common_gaps {
Subroutine _remove_common_gaps: not called CC=1 SCAR=0.0
275  
276  
277  
278 }
279  
280 sub trim_ends {
281 0 ▶ my (@seqs, @gaps);
Subroutine trim_ends: not called CC=18 SCAR=58.3
Pod undocumented
282 0 foreach my $seq ($aln->each_seq()) {
283 0 my $id = $seq->display_id();
284 0 my @nts = split //, $seq->seq();
285 0 my $gap_start = 0;
286 0 my $new;
287 0 ▶ for (my $i=0; $i< $aln->length(); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $aln->length) 0 0 0
288 0 ▶ if ($nts[$i] eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nts[$i] eq '-') { } 0 0 0
289 0 ▶ if ($gap_start) { # gap -> gap
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_start) { } 0 0 0
290 0 $new->{end}++;
291 } else { # nt -> gap
292 0 $gap_start = 1;
293 0 $new = { 'start' => $i+1, 'end' => $i+1, 'seq_name' => $id }
294 }
295 } else {
296 0 ▶ if ($gap_start) { # gap -> nt
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_start) { } 0 0 0
297 0 $gap_start = 0;
298 0 push @gaps, $new;
299 } else { # nt -> nt
300 0 next;
301 }
302 }
303 }
304 0 ▶ push @gaps, $new if $gap_start;
Branch: 1 branch
BranchTrueFalseTotal
if $gap_start 0 0 0
305 }
306  
307 0 my (@three_end_gaps, @five_end_gaps);
308  
309 0 foreach my $gap (@gaps) {
310 0 $gap->{length} = $gap->{end} - $gap->{start} + 1;
311 0 ▶ push @three_end_gaps, $gap if $gap->{start} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $gap->{"start"} == 1 0 0 0
312 0 ▶ push @five_end_gaps, $gap if $gap->{end} == $aln->length;
Branch: 1 branch
BranchTrueFalseTotal
if $gap->{"end"} == $aln->length 0 0 0
313 }
314  
315 0 ▶ return unless @three_end_gaps or @five_end_gaps;
Branch: 1 branch
BranchTrueFalseTotal
unless @three_end_gaps or @five_end_gaps 0 0 0
Condition0%0/3
@three_end_gaps or @five_end_gaps
l!l&&r!l&&!r
000
MC/DC0%0/2
@three_end_gaps or @five_end_gaps
@three_end_gaps @five_end_gaps
Truth table0%0/3
A or B
A= @three_end_gapsB= @five_end_gaps
ABresult
1X1
011
000
316  
317 0 my $longest_three_end = 0;
318 0 my $longest_five_start = 0;
319 0 my $longest_three_length = 0;
320 0 my $longest_five_length = 0;
321  
322 0 foreach my $gap (@three_end_gaps) {
323 0 ▶ if ($gap->{length} > $longest_three_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gap->{"length"} > $longest_three_length) 0 0 0
324 0 $longest_three_end = $gap->{end};
325 0 $longest_three_length = $gap->{length};
326 }
327 }
328  
329 0 foreach my $gap (@five_end_gaps) {
330 0 ▶ if ($gap->{length} > $longest_five_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gap->{"length"} > $longest_five_length) 0 0 0
331 0 $longest_five_start = $gap->{start};
332 0 $longest_five_length = $gap->{length};
333 }
334 }
335  
336 # print STDERR $longest_three, "\t", $longest_five, "\n";
337 0 ▶ if (@three_end_gaps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@three_end_gaps) 0 0 0
338 0 print STDERR Dumper(\@three_end_gaps);
339 0 print STDERR $longest_three_end, "\n";
340 0 $aln = $aln->slice($longest_three_end + 1, $aln->length);
341 }
342  
343 0 ▶ if (@five_end_gaps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@five_end_gaps) 0 0 0
344 0 print STDERR Dumper(\@five_end_gaps);
345 0 print STDERR $longest_five_start, "\n";
346 0 $aln = $aln->slice(1, $longest_five_start - $longest_three_end - 1);
347 }
348 }
349  
350 sub gap_states {
351 0 ▶ my (@seqs, @gaps);
Subroutine gap_states: not called CC=13 SCAR=52.0
Pod undocumented
352 0 foreach my $seq ($aln->each_seq()) {
353 0 my $id = $seq->display_id();
354 0 my @nts = split //, $seq->seq();
355 0 my $gap_start = 0;
356 0 my $new;
357 0 ▶ for (my $i=0; $i< $aln->length(); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $aln->length) 0 0 0
358 0 ▶ if ($nts[$i] eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nts[$i] eq '-') { } 0 0 0
359 0 ▶ if ($gap_start) { # gap -> gap
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_start) { } 0 0 0
360 0 $new->{end}++;
361 } else { # nt -> gap
362 0 $gap_start = 1;
363 0 $new = { 'start' => $i+1, 'end' => $i+1, 'seq_name' => $id }
364 }
365 } else {
366 0 ▶ if ($gap_start) { # gap -> nt
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_start) { } 0 0 0
367 0 $gap_start = 0;
368 0 push @gaps, $new;
369 } else { # nt -> nt
370 0 next;
371 }
372 }
373 }
374 0 ▶ push @gaps, $new if $gap_start;
Branch: 1 branch
BranchTrueFalseTotal
if $gap_start 0 0 0
375 }
376 0 my (%gap_freqs, @uniq_gaps);
377 0 foreach my $gap (@gaps) {
378 0 my $id = $gap->{start} . "-" . $gap->{end};
379 0 $gap->{id} = $id;
380 0 $gap_freqs{$id}++;
381 }
382  
383 0 foreach my $id (keys %gap_freqs) {
384 0 my ($start, $end) = split /-/, $id;
385 push @uniq_gaps, {
386 'start' => $start,
387 'end' => $end,
388 'is_edge' => ($start == 1 || $end == $aln->length) ? 1 : 0,
389 'in_frame' => ($end - $start + 1) % 3 ? 0 : 1,
390 0 ▶ 'counts' => $gap_freqs{$id},
Branch: 2 branches
BranchTrueFalseTotal
$start == 1 || $end == $aln->length ? : 0 0 0
($end - $start + 1) % 3 ? : 0 0 0
Condition0%0/3
$start == 1 or $end == $aln->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$start == 1 or $end == $aln->length
$start == 1 $end == $aln->length
Truth table0%0/3
A or B
A= $start == 1B= $end == $aln->length
ABresult
1X1
011
000
391 };
392 }
393  
394 0 foreach my $gap (@uniq_gaps) { say join "\t", ($file, $gap->{start}, $gap->{end}, $gap->{is_edge}, $gap->{in_frame}, $gap->{counts}, $aln->length()) }
395 # print Dumper(\@uniq_gaps);
396 0 exit;
397 }
398  
399 sub gap_states_matrix {
400 0 ▶ my (@seq_ids, @gaps);
Subroutine gap_states_matrix: not called CC=17 SCAR=57.2
Pod undocumented
401 0 foreach my $seq ($aln->each_seq()) {
402 0 my $id = $seq->display_id();
403 0 push @seq_ids, $id;
404 0 my @nts = split //, $seq->seq();
405 0 my $gap_start = 0;
406 0 my $new;
407 0 ▶ for (my $i=0; $i< $aln->length(); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $aln->length) 0 0 0
408 0 ▶ if ($nts[$i] eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nts[$i] eq '-') { } 0 0 0
409 0 ▶ if ($gap_start) { # gap -> gap
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_start) { } 0 0 0
410 0 $new->{end}++;
411 } else { # nt -> gap
412 0 $gap_start = 1;
413 0 $new = { 'start' => $i+1, 'end' => $i+1, 'seq_name' => $id }
414 }
415 } else {
416 0 ▶ if ($gap_start) { # gap -> nt
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_start) { } 0 0 0
417 0 $gap_start = 0;
418 0 push @gaps, $new;
419 } else { # nt -> nt
420 0 next;
421 }
422 }
423 }
424 0 ▶ push @gaps, $new if $gap_start;
Branch: 1 branch
BranchTrueFalseTotal
if $gap_start 0 0 0
425 }
426 0 my (%gap_freqs, @uniq_gaps, %gap_presence);
427 0 foreach my $gap (@gaps) {
428 0 my $id = $gap->{start} . "-" . $gap->{end};
429 0 $gap->{id} = $id;
430 0 $gap_freqs{$id}++;
431 0 $gap_presence{$id}->{$gap->{seq_name}} = 1;
432 }
433  
434 0 foreach my $id (keys %gap_freqs) {
435 0 my ($start, $end) = split /-/, $id;
436 push @uniq_gaps, {
437 'start' => $start,
438 'end' => $end,
439 'is_edge' => ($start == 1 || $end == $aln->length) ? 1 : 0,
440 'in_frame' => ($end - $start + 1) % 3 ? 0 : 1,
441 0 ▶ 'counts' => $gap_freqs{$id},
Branch: 2 branches
BranchTrueFalseTotal
$start == 1 || $end == $aln->length ? : 0 0 0
($end - $start + 1) % 3 ? : 0 0 0
Condition0%0/3
$start == 1 or $end == $aln->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$start == 1 or $end == $aln->length
$start == 1 $end == $aln->length
Truth table0%0/3
A or B
A= $start == 1B= $end == $aln->length
ABresult
1X1
011
000
442 'id' => $id
443 };
444 }
445  
446 0 ▶ my @gaps_sorted = sort {$a->{start} <=> $b->{start} || $a->{end} <=> $b->{end}} @uniq_gaps;
Branch: 1 branch
BranchTrueFalseTotal
$Bio::BPWrapper::AlnManipulations::a->{"start"} <=> $Bio::BPWrapper::AlnManipulations::b->{"start"} or $Bio::BPWrapper::AlnManipulations::a->{"end"} <=> $Bio::BPWrapper::AlnManipulations::b->{"end"} 0 0 0
447 0 foreach (@gaps_sorted) { print "\t", $_->{id}}
448 0 print "\n";
449 0 foreach my $sid (sort @seq_ids) {
450 0 print $sid;
451 0 foreach my $u_gap (@gaps_sorted) {
452 0 ▶ print "\t", $gap_presence{$u_gap->{id}}->{$sid} || 0;
Condition0%0/2
$gap_presence{$u_gap->{'id'}}{$sid} || 0
l!l
00
MC/DC0%0/1
$gap_presence{$u_gap->{'id'}}{$sid} || 0
$gap_presence{$u_gap->{'id'}}{$sid}
Truth table0%0/2
A
A= $gap_presence{$u_gap->{'id'}}{$sid}
Aresult
11
00
453 }
454 0 print "\n";
455 }
456  
457 # foreach my $gap (@uniq_gaps) { say join "\t", ($file, $gap->{start}, $gap->{end}, $gap->{is_edge}, $gap->{in_frame}, $gap->{counts}, $aln->length()) }
458 # print Dumper(\@uniq_gaps);
459 0 exit;
460 }
461  
462 =head2 print_avp_id
463  
464 Print the average percent identity of an alignment.
465  
466 Wraps
467 L<Bio::SimpleAlign-E<gt>average_percentage_identity()|https://metacpan.org/pod/Bio::SimpleAlign#average_percentage_identity>.
468  
469  
470 =cut
471  
472 sub print_avp_id {
473 1 ▶ printf "%.4f\n", $aln->average_percentage_identity();
Subroutine print_avp_id: called CC=1 SCAR=0.0
Pod documented
474 exit
475 1 }
476  
477 =head2 boostrap()
478  
479 Produce a bootstrapped
480 alignment. L<Bio::Align::Utilities-E<gt>bootstrap()|https://metacpan.org/pod/Bio::Align::Utilities#bootstrap_replicates>.
481  
482 =cut
483  
484 sub bootstrap {
485 1 ▶ my $replicates = bootstrap_replicates($aln,1);
Subroutine bootstrap: called CC=1 SCAR=0.0
486 1 $aln = shift @$replicates
487 }
488  
489 =head2 draw_codon_view()
490  
491 Print a CLUSTALW-like alignment, but separated by codons. Intended for
492 use with DNA sequences. Block-final position numbers are printed at
493 the end of every alignment block at the point of wrapping, and
494 block-initial counts appear over first nucleotide in a block.
495  
496 =cut
497  
498 sub draw_codon_view {
499 # my $aln = shift;
500 # Is 20 by default. Blocks are measured in CODONS, so mult by 3
501 1 ▶ my $block_length = 3 * $opts{"codon-view"};
Subroutine draw_codon_view: called CC=14 SCAR=26.4
Pod documented
502 1 my $aln_length = $aln->length();
503 1 my $num_seqs = $aln->num_sequences();
504 1 my $min_pad = 4; # Minimum padding between sequence and ID
505 1 my $seq_matrix;
506 1 my @seqs = ($aln->each_seq);
507 1 my @display_ids;
508  
509 # Find longest id length, add id/sequence padding
510 1 my $max_id_len = _find_max_id_len(\@seqs);
511  
512 # id length includes padding
513 1 $max_id_len += $min_pad;
514  
515 # Extract display_ids and sequences from AlignIO object.
516 1 foreach my $seq (@seqs) {
517 13 my @seq_str = split '', $seq->seq();
518 13 push @$seq_matrix, \@seq_str;
519 13 push @display_ids, $seq->display_id;
520  
521 # Pad display ids so that space between them and sequence is consistent
522 13 $display_ids[-1] = _pad_display_id($display_ids[-1], $max_id_len)
523 }
524  
525 1 my $nuc_count = 0;
526  
527 # Loop over each sequence.
528 1 ▶ for (my $i = 0; $i < $num_seqs; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_seqs) 26 0 26
529  
530 # Print count at end of block when we are starting out a new block
531 26 ▶ _print_positions($nuc_count, $aln_length, $max_id_len) if $i == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 2 24 26
532  
533 # Loop over nucleotides
534 26 ▶ for (my $j = $nuc_count; $j < $aln_length; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $aln_length) 1560 0 1560
535  
536 # When we're starting, or starting a new block, print the display id's.
537 1560 ▶ print $display_ids[$i] if $j % $block_length == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $j % $block_length == 0 26 1534 1560
538  
539 1560 print "$$seq_matrix[$i]->[$j]";
540 1560 ▶ print " " if ((($j + 1) % 3) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 1) % 3 == 0 520 1040 1560
541  
542 # When we've reached the end of the alignment or a block
543 1560 ▶ if ($j + 1 == $aln_length || (($j + 1) % $block_length) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 1 == $aln_length or ($j + 1) % $block_length == 0) 26 1534 1560
Condition100%3/3
$j + 1 == $aln_length or ($j + 1) % $block_length == 0
l!l&&r!l&&!r
13131534
MC/DC100%2/2
$j + 1 == $aln_length or ($j + 1) % $block_length == 0
$j + 1 == $aln_length ($j + 1) % $block_length == 0
Truth table100%3/3
A or B
A= $j + 1 == $aln_lengthB= ($j + 1) % $block_length == 0
ABresult
1X1
011
000
544 26 ▶ if ($i + 1 == $num_seqs) { $nuc_count = $j + 1 } # If this is the last sequence, save the ending (next) position.
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 == $num_seqs) { } 2 24 26
545 24 else { print "\n" } # Otherwise, start on the next line.
546 last # In either case, need to exit this loop.
547 26 }
548 } # END for LOOP OVER NUCLEOTIDES
549  
550 # Finish if we've reached the end of the alignment, and the last sequence
551 26 ▶ if (($i + 1 == $num_seqs) && ($nuc_count == $aln_length)) { print "\n"; last }
Branch: 2 branches
BranchTrueFalseTotal
if ($i + 1 == $num_seqs and $nuc_count == $aln_length) { } 1 25 26
elsif ($i + 1 == $num_seqs and $nuc_count < $aln_length) { } 1 48 49
Condition100%3/3
$i + 1 == $num_seqs and $nuc_count == $aln_length
!ll&&!rl&&r
2411
MC/DC100%2/2
$i + 1 == $num_seqs and $nuc_count == $aln_length
$i + 1 == $num_seqs $nuc_count == $aln_length
Truth table100%3/3
A and B
A= $i + 1 == $num_seqsB= $nuc_count == $aln_length
ABresult
0X0
100
111
552  
553 # If we haven't reached the end of the alignment, but we've run through
554 # all sequences, print final block position and start at first sequence.
555 ▶ elsif (($i + 1 == $num_seqs) && ($nuc_count < $aln_length)) {
Condition66%2/3
$i + 1 == $num_seqs and $nuc_count < $aln_length
!ll&&!rl&&r
2401
MC/DC50%1/2
$i + 1 == $num_seqs and $nuc_count < $aln_length
$i + 1 == $num_seqs $nuc_count < $aln_length
Truth table66%2/3
A and B
A= $i + 1 == $num_seqsB= $nuc_count < $aln_length
ABresult
0X0
100
111
556 1 $i = -1; # Always increments after a loop; next increment sets to 0.
557 1 print "\n\n"
558 }
559 } # END for LOOP OVER SEQUENCES
560  
561 # Can't let script terminate normally: produces traditional alignment output
562 1 exit 0
563 }
564  
565 =head2 del_seqs()
566  
567 Delete sequences based on their id. Option takes a comma-separated list of ids.
568 The list of sequences to delete is in C<$opts{"delete"}> which is set via
569 L<C<#initilize(\%opts)>|/initialize>
570  
571 =cut
572  
573 sub del_seqs {
574 1 ▶ _del_or_pick($opts{"delete"}, "remove_seq", 0)
Subroutine del_seqs: called CC=1 SCAR=0.0
Pod documented
575 }
576  
577 =head2 remove_gaps()
578  
579 Remove gaps (and returns an de-gapped alignment). Wraps
580 L<Bio::SimpleAlign-E<gt>remove_gaps()|https://metacpan.org/pod/Bio::SimpleAlign#remove_gaps>.
581  
582 =cut
583  
584 sub remove_gaps {
585 1 ▶ $aln = $aln->remove_gaps()
Subroutine remove_gaps: called CC=1 SCAR=0.0
Pod documented
586 }
587  
588 =head2 print_length()
589  
590 Print alignment length. Wraps L<Bio::SimpleAlign-E<gt>length()|https://metacpan.org/pod/Bio::SimpleAlign#length>.
591  
592 =cut
593  
594 sub print_length {
595 1 ▶ say $aln->length();
Subroutine print_length: called CC=1 SCAR=0.0
Pod documented
596 exit
597 1 }
598  
599 =head2 print_match()
600  
601 Go through all columns and change residues identical to the reference
602 sequence to be the match character, '.' Wraps
603 L<Bio::SimpleAlign-E<gt>match()|https://metacpan.org/pod/Bio::SimpleAlign#match>.
604  
605 =cut
606  
607 sub print_match {
608 1 ▶ $aln->match()
Subroutine print_match: called CC=1 SCAR=0.0
Pod documented
609 }
610  
611 =head2 print_num_seq()
612  
613 Print number of sequences in alignment.
614  
615 =cut
616  
617 sub print_num_seq {
618 1 ▶ say $aln->num_sequences();
Subroutine print_num_seq: called CC=1 SCAR=0.0
Pod documented
619 exit
620 1 }
621  
622 =head2 pick_seqs()
623  
624 Pick sequences based on their id. Option takes a comma-separated list
625 of ids. The sequences to pick set in C<$opts{"pick"}> which is set via
626 L<C<#initilize(\%opts)>|/initialize>.
627  
628 =cut
629  
630 sub pick_seqs {
631 1 ▶ _del_or_pick($opts{"pick"}, "add_seq", 1)
Subroutine pick_seqs: called CC=1 SCAR=0.0
Pod documented
632 }
633  
634 =head2 change_ref()
635  
636 Change the reference sequence to be what is in C<$opts{"refseq"}>
637 which is set via L<C<#initilize(\%opts)>|/initialize>. Wraps
638 L<Bio::SimpleAlign-E<gt>set_new_reference()|https://metacpan.org/pod/Bio::SimpleAlign#set_new_reference>.
639  
640 =cut
641  
642 sub change_ref {
643 1 ▶ $aln = $aln->set_new_reference($opts{"refseq"})
Subroutine change_ref: called CC=1 SCAR=0.0
Pod documented
644 }
645  
646  
647 =head2 aln_slice()
648  
649 Get a slice of the alignment. The slice is specifiedn
650 C<$opts{"slice"}> which is set via L<C<#initilize(\%opts)>|/initialize>.
651  
652 Wraps
653 L<Bio::SimpleAlign-E<gt>slice()|https://metacpan.org/pod/Bio::SimpleAlign#slice>
654 with improvements.
655  
656 =cut
657  
658  
659 sub aln_slice { # get alignment slice
660 1 ▶ my ($begin, $end) = split(/\s*,\s*/, $opts{"slice"});
Subroutine aln_slice: called CC=3 SCAR=11.4
Pod documented
661  
662 # Allow for one parameter to be omitted. Default $begin to the
663 # beginning of the alignment, and $end to the end.
664 1 ▶ $begin = 1 if $begin eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $begin eq "-" 0 1 1
665 1 ▶ $end = $aln->length if $end eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $end eq "-" 0 1 1
666 1 $aln = $aln->slice($begin, $end)
667 }
668  
669 =head2 get_unique()
670  
671 Extract the alignment of unique sequences. Wraps
672 L<Bio::SimpleAlign-E<gt>uniq_seq()|https://metacpan.org/pod/Bio::SimpleAlign#uniq_seq>.
673  
674 =cut
675  
676  
677 sub get_unique() {
678 1 ▶ $aln->verbose(1);
Subroutine get_unique: called CC=1 SCAR=0.0
Pod documented
679 1 $aln = $aln->uniq_seq();
680 }
681  
682 sub _has_gap {
683 0 ▶ my $ref = shift;
Subroutine _has_gap: not called CC=3 SCAR=24.8
684 0 foreach (@$ref) {
685 0 ▶ return 1 if $_ eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "-" 0 0 0
686 }
687 0 return 0;
688 }
689  
690 sub _has_singleton {
691 0 ▶ my $ref = shift;
Subroutine _has_singleton: not called CC=3 SCAR=24.8
692 0 foreach my $key (keys %$ref) {
693 0 ▶ return 1 if $ref->{$key} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{$key} == 1 0 0 0
694 }
695 0 return 0;
696 }
697  
698 =head2 binary_informative
699  
700 extract binary and informative sites (for clique): discard constant,
701 3/4-states, non-informative
702  
703 =cut
704  
705 sub binary_informative {
706 0 ▶ my $new_aln = Bio::SimpleAlign->new();
Subroutine binary_informative: not called CC=12 SCAR=50.5
Pod documented
707 0 my $len=$aln->length();
708 0 my (@seq_ids, @inf_sites, %bin_chars);
709  
710 # Go through each column and save variable sites
711 0 my $ref_bases = &_get_a_site_v2(); #print Dumper($ref_bases); exit;
712 0 foreach (sort keys %$ref_bases) { push @seq_ids, $_ }
713 0 ▶ for (my $i=1; $i<=$len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 0 0 0
714 0 my (%seen, @bases);
715 0 foreach my $id (@seq_ids) { push @bases, $ref_bases->{$id}->{$i}; }
716 0 %seen = %{&_seen_bases(\@bases)};
717 0 ▶ next if &_has_gap( [ values %seen ] );
Branch: 1 branch
BranchTrueFalseTotal
if &_has_gap([values %seen]) 0 0 0
718 0 ▶ next if keys %seen != 2;
Branch: 1 branch
BranchTrueFalseTotal
if keys %seen != 2 0 0 0
719 0 ▶ next if &_has_singleton(\%seen);
Branch: 1 branch
BranchTrueFalseTotal
if &_has_singleton(\%seen) 0 0 0
720 0 my ($base1, $base2) = sort keys %seen;
721 0 $bin_chars{$i}{$base1} = 0;
722 0 $bin_chars{$i}{$base2} = 1;
723 0 push @inf_sites, $i;
724 }
725  
726 0 ▶ die "informative sites not found\n" unless @inf_sites;
Branch: 1 branch
BranchTrueFalseTotal
unless @inf_sites 0 0 0
727 0 foreach (@inf_sites) { warn $_, "\n" }
728  
729 # Recreate the object for output
730 0 foreach my $id (@seq_ids) {
731 0 my $seq_str;
732 0 foreach my $i (@inf_sites) {
733 0 ▶ $seq_str .= $binary ? $bin_chars{$i}->{$ref_bases->{$id}->{$i}} : $ref_bases->{$id}->{$i};
Branch: 1 branch
BranchTrueFalseTotal
$binary ? : 0 0 0
734 }
735 0 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1);
736 0 my $end = $loc_seq->end;
737 0 $loc_seq->end($end);
738 0 $new_aln->add_seq($loc_seq)
739 }
740  
741 0 $aln = $new_aln
742 }
743  
744 =head2 variable_sites()
745  
746 Extracts variable sites.
747  
748 =cut
749 sub variable_sites {
750 1 ▶ $aln = $aln->remove_gaps();
Subroutine variable_sites: called CC=8 SCAR=20.8
Pod documented
751 1 my $new_aln = Bio::SimpleAlign->new();
752 1 my $len=$aln->length();
753 1 my (%seq_ids, @sites, @var_sites);
754  
755 # Go through each column and save variable sites
756 1 ▶ for (my $i=1; $i<=$len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 90 1 91
757 90 my ($ref_bases, $ref_ids) = &_get_a_site($i);
758 90 %seq_ids = %{$ref_ids};
759 90 my $is_constant = &_is_constant(&_paste_nt($ref_bases));
760 90 ▶ if ($is_constant < 1) { push @sites, $ref_bases; push @var_sites, $i }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_constant < 1) 2 88 90
761 }
762  
763 1 foreach (@var_sites) { warn $_, "\n" }
764  
765 # Recreate the object for output
766 1 foreach my $id (sort keys %seq_ids) {
767 13 my $seq_str;
768 13 foreach my $aln_site (@sites) {
769 26 ▶ foreach (@$aln_site) { $seq_str .= $_->{nt} if $_->{id} eq $id }
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"id"} eq $id 26 312 338
770 }
771  
772 13 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1);
773 13 my $end = $loc_seq->end;
774 13 $loc_seq->end($end);
775 13 $new_aln->add_seq($loc_seq)
776 }
777  
778 1 $aln = $new_aln
779 }
780  
781 =head2 avg_id_by_win()
782  
783 Calculate pairwise average sequence difference by windows (overlapping
784 windows with fixed step of 1). The window size is set in
785 C<$opts{"window"}> which is set via L<C<#initilize(\%opts)>|/initialize>.
786  
787 =cut
788  
789  
790 sub avg_id_by_win {
791 1 ▶ my $window_sz = $opts{"window"};
Subroutine avg_id_by_win: called CC=2 SCAR=6.9
Pod documented
792 1 for my $i (1 .. ($aln->length() - $window_sz + 1)) {
793 61 my $slice = $aln->slice($i, $i + $window_sz - 1);
794 61 my $pi = (100 - $slice->average_percentage_identity()) / 100;
795 61 printf "%d\t%d\t%.4f\n", $i, $i + $window_sz - 1, $pi
796 }
797 exit
798 1 }
799  
800 =head2 concat()
801  
802 Concatenate multiple alignments sharing the same set of unique
803 IDs. This is normally used for concatenating individual gene
804 alignments of the same set of samples to a single one for making a
805 "supertree". Wraps
806 L<Bio::Align::UtilitiesE<gt>cat()|https://metacpan.org/pod/Bio::Align::Utilities#cat>.
807  
808 =cut
809  
810  
811 sub concat {
812 1 ▶ $aln = cat(@alns)
Subroutine concat: called CC=1 SCAR=0.0
Pod documented
813 }
814  
815  
816 sub conserved_blocks {
817 1 ▶ my $len=$aln->length();
Subroutine conserved_blocks: called CC=16 SCAR=27.7
Pod undocumented
818 1 my $nseq = $aln->num_sequences();
819 1 my $min_block_size = $opts{"conblocks"};
820 1 my %seq_ids;
821  
822 1 ▶ die "Alignment contains only one sequence: $file\n" if $nseq < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $nseq < 2 0 1 1
823  
824 1 my (@blocks, $block);
825 1 my $in_block=0;
826 1 ▶ for (my $i=1; $i<=$len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 120 1 121
827 120 my ($ref_bases, $ref_ids) = &_get_a_site($i);
828 120 %seq_ids = %{$ref_ids};
829 120 my $is_constant = &_is_constant(&_paste_nt($ref_bases));
830 120 ▶ if ($in_block) { # previous site is a contant one
Branch: 1 branch
BranchTrueFalseTotal
if ($in_block) { } 87 33 120
831 87 ▶ if ($is_constant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_constant) { } 85 2 87
832 85 $block->{length} ++;
833 85 my @sites = @{$block->{sites}};
834 85 push @sites, $ref_bases;
835 85 $block->{sites} = \@sites;
836 85 ▶ if ($i == $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $len) 1 84 85
837 1 warn "Leaving a constant block at the end of alignment: $i\n";
838 1 ▶ push @blocks, $block if $block->{length} >= $min_block_size
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"length"} >= $min_block_size 1 0 1
839 }
840 } else {
841 2 $in_block = 0;
842 2 ▶ push @blocks, $block if $block->{length} >= $min_block_size;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"length"} >= $min_block_size 2 0 2
843 2 warn "Leaving a constant block at $i\n"
844 }
845 } else { # previous site not a constant one
846 33 ▶ if ($is_constant) { # entering a block
Branch: 1 branch
BranchTrueFalseTotal
if ($is_constant) 3 30 33
847 3 warn "Entering a constant block at site $i ...\n";
848 3 $in_block=1;
849 3 $block = {start => $i, length => 1, num_seq => $nseq, sites => [($ref_bases)]} # start a new block
850 }
851 }
852 }
853  
854 1 foreach my $bl (@blocks) {
855 3 my $out = Bio::AlignIO->new(-file=> ">$file" . ".slice-". $bl->{start} . ".aln" , -format=>'clustalw');
856 3 my $block_aln = Bio::SimpleAlign->new();
857 3 foreach my $id (sort keys %seq_ids) {
858 39 my ($seq_str, $ungapped_start, $ungapped_end);
859 39 my @sites = @{$bl->{sites}};
860 39 ▶ for (my $i = 0; $i <= $#sites; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#sites) 1144 39 1183
861 1144 my $ref_chars = $sites[$i];
862 1144 foreach (@$ref_chars) {
863 14872 ▶ next unless $_->{id} eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"id"} eq $id 13728 1144 14872
864 1144 ▶ $ungapped_start = $_->{ungapped_pos} if $i == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 39 1105 1144
865 1144 ▶ $ungapped_end = $_->{ungapped_pos} if $i == $#sites;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#sites 39 1105 1144
866 $seq_str .= $_->{nt}
867 1144 }
868 }
869  
870 39 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => $ungapped_start, -end => $ungapped_end);
871 39 $block_aln->add_seq($loc_seq)
872 }
873 3 $out->write_aln($block_aln)
874 }
875 exit
876 1 }
877  
878 sub get_consensus {
879 1 ▶ my $percent_threshold = $opts{"consensus"};
Subroutine get_consensus: called CC=1 SCAR=0.0
Pod undocumented
880 1 my $consense = Bio::LocatableSeq->new(
881 -seq => $aln->consensus_string($percent_threshold),
882 -id => "Consensus_$percent_threshold",
883 -start => 1,
884 -end => $aln->length()
885 );
886 1 $aln->add_seq($consense)
887 }
888  
889 =head2 dna_to_protein()
890  
891 Align CDS sequences according to their corresponding protein
892 alignment. Wraps
893 L<Bio::Align::Utilities-E<gt>aa_to_dna_aln()|https://metacpan.org/pod/Bio::Align::Utilities#aa_to_dna_aln>.
894  
895 =cut
896  
897 sub dna_to_protein {
898 1 ▶ $aln = dna_to_aa_aln($aln)
Subroutine dna_to_protein: called CC=1 SCAR=0.0
Pod documented
899 }
900  
901 sub remove_gapped_cols_in_one_seq {
902 1 ▶ my $id = $opts{"erasecol"};
Subroutine remove_gapped_cols_in_one_seq: called CC=7 SCAR=20.0
Pod undocumented
903 1 my $nmatch=0;
904 1 my $ref_seq;
905 1 foreach ($aln->each_seq) {
906 13 ▶ if ($_->id() =~ /$id/) { $nmatch++; $ref_seq = $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ($_->id =~ /$id/) 1 12 13
907 }
908 1 ▶ die "Quit. No ref seq found or more than one ref seq!\n" if !$nmatch || $nmatch > 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $nmatch or $nmatch > 1 0 1 1
Condition33%1/3
not $nmatch or $nmatch > 1
l!l&&r!l&&!r
001
MC/DC0%0/2
not $nmatch or $nmatch > 1
not $nmatch $nmatch > 1
Truth table33%1/3
not(A) or B
A= not $nmatchB= $nmatch > 1
ABresult
1X1
011
000
909 1 my ($ct_gap, $ref) = &_get_gaps($ref_seq);
910 1 warn "Original length: " . $aln->length() . "\n";
911 1 ▶ if ($ct_gap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct_gap) { } 1 0 1
912 1 my @args;
913 1 push @args, [$_, $_] foreach @$ref;
914 1 $aln = $aln->remove_columns(@args);
915 1 warn "New length: " . $aln->length() . "\n"
916 } else {
917 0 warn "No gap: " . $aln->length() . "\n"
918 }
919 }
920  
921 sub colnum_from_residue_pos {
922 1 ▶ my ($id, $pos) = split /\s*,\s*/, $opts{"aln-index"};
Subroutine colnum_from_residue_pos: called CC=1 SCAR=0.0
Pod undocumented
923 1 print $aln->column_from_residue_number($id, $pos), "\n";
924 exit
925 1 }
926  
927 =head2 list_ids()
928  
929 List all sequence ids.
930  
931 =cut
932  
933 sub list_ids {
934 1 ▶ my @ids;
Subroutine list_ids: called CC=2 SCAR=6.9
Pod documented
935 1 foreach ($aln->each_seq) { push @ids, $_->display_id() }
936 1 say join "\n", @ids;
937 exit
938 1 }
939  
940 sub permute_states {
941 1 ▶ my $new_aln = Bio::SimpleAlign->new();
Subroutine permute_states: called CC=8 SCAR=20.8
Pod undocumented
942 1 my $len=$aln->length();
943 1 my $nseq = $aln->num_sequences();
944 1 my @seq_ids;
945  
946 1 ▶ die "Alignment contains only one sequence: $file\n" if $nseq < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $nseq < 2 0 1 1
947  
948 1 my @sites;
949 1 my $ref_bases = &_get_a_site_v2();
950 1 foreach (sort keys %$ref_bases) { push @seq_ids, $_ }
951 1 ▶ for (my $i=1; $i<=$len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 120 1 121
952 120 my @bases;
953 120 foreach (keys %$ref_bases) { push @bases, $ref_bases->{$_}->{$i} }
954 120 @bases = shuffle(@bases);
955 120 ▶ for (my $j=0; $j<$nseq; $j++) { $ref_bases->{$seq_ids[$j]}->{$i} = $bases[$j] }
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $nseq) 1560 120 1680
956 }
957  
958 1 foreach my $id (@seq_ids) {
959 13 my $seq_str;
960 13 ▶ for (my $i=1; $i<=$len; $i++) { $seq_str .= $ref_bases->{$id}->{$i} }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 1560 13 1573
961  
962 13 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1);
963 13 my $end = $loc_seq->end;
964 13 $loc_seq->end($end);
965 13 $new_aln->add_seq($loc_seq)
966 }
967 1 $aln = $new_aln
968 }
969  
970 =head2 protein_to_dna()
971  
972 Align CDS sequences according to their corresponding protein
973 alignment. Wraps
974 L<Bio::Align::Utilities-E<gt>aa_to_dna_aln()https://metacpan.org/pod/Bio::Align::Utilities#aa_to_dna_aln>.
975  
976 =cut
977  
978 sub protein_to_dna {
979 29 ▶ use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
980 1 ▶ my $cds_in = Bio::SeqIO->new(-file=>$opts{pep2dna}, -format=>'fasta');
Subroutine protein_to_dna: called CC=2 SCAR=6.9
Pod documented
981 1 my %CDSs;
982 1 ▶ while (my $seq = $cds_in->next_seq()) { $CDSs{$seq->display_id()} = $seq }
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = $cds_in->next_seq) 13 1 14
983 1 $aln = aa_to_dna_aln($aln, \%CDSs);
984 }
985  
986 =head2 sample_seqs()
987  
988 Picks I<n> random sequences from input alignment and produces a new
989 alignment consisting of those sequences.
990  
991 If n is not given, default is the number of sequences in alignment
992 divided by 2, rounded down.
993  
994 This functionality uses an implementation of Reservoir Sampling, based
995 on the algorithm found here:
996 http://blogs.msdn.com/b/spt/archive/2008/02/05/reservoir-sampling.aspx
997  
998 =cut
999  
1000 sub sample_seqs {
1001 # If option was given with no number, take the integer part of num_sequences/2
1002 # Its OK to use int() here (especially since we want to round towards 0)
1003 1 ▶ my $num_seqs = $aln->num_sequences;
Subroutine sample_seqs: called CC=5 SCAR=16.2
Pod documented
1004 1 ▶ my $sample_size = ($opts{"resample"} == 0) ? int($num_seqs / 2) : $opts{"resample"};
Branch: 1 branch
BranchTrueFalseTotal
$opts{'resample'} == 0 ? : 1 0 1
1005  
1006 1 ▶ die "Error: sample size ($sample_size) exceeds number of sequences in alignment: ($num_seqs)" if $sample_size > $num_seqs;
Branch: 1 branch
BranchTrueFalseTotal
if $sample_size > $num_seqs 0 1 1
1007  
1008 # Use Reservoir Sampling to pick random sequences.
1009 1 my @sampled = (1 .. $sample_size);
1010 1 for my $j ($sample_size + 1 .. $num_seqs) {
1011 7 ▶ $sampled[ rand(@sampled) ] = $j if rand() <= ($sample_size / $j)
Branch: 1 branch
BranchTrueFalseTotal
if rand() <= $sample_size / $j 4 3 7
1012 }
1013  
1014 1 warn "Sampled the following sequences: @sampled\n\n";
1015 1 my $tmp_aln = $aln->select_noncont(@sampled);
1016 1 $aln = $tmp_aln
1017 }
1018  
1019 =head2 shuffle_sites()
1020  
1021 Make a shuffled (not bootstraped) alignment. This operation randomizes
1022 alignment columns. It is used for testing the signficance of long-runs
1023 of conserved sites in an alignment (e.g., conserved intergenic spacers
1024 [IGSs]).
1025  
1026 =cut
1027  
1028 sub shuffle_sites {
1029 0 ▶ my $new_aln = Bio::SimpleAlign->new();
Subroutine shuffle_sites: not called CC=8 SCAR=42.8
Pod documented
1030 0 my $len = $aln->length();
1031 0 my $nseq = $aln->num_sequences();
1032 0 my %seq_ids;
1033  
1034 0 ▶ die "Alignment contains only one sequence: $file\n" if $nseq < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $nseq < 2 0 0 0
1035  
1036 0 my @sites;
1037 0 ▶ for (my $i=1; $i<=$len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 0 0 0
1038 0 my ($ref_bases, $ref_ids) = &_get_a_site($i);
1039 0 %seq_ids = %{$ref_ids};
1040 0 push @sites, $ref_bases
1041 }
1042  
1043 0 @sites = shuffle(@sites);
1044  
1045 0 my @order;
1046 0 push @order, $_->[0]->{pos} foreach @sites;
1047 0 print STDERR "Shuffled site order:\t", join(",", @order);
1048 0 print STDERR "\n";
1049  
1050 0 foreach my $id (sort keys %seq_ids) {
1051 0 my $seq_str;
1052 0 foreach my $aln_site (@sites) {
1053 0 ▶ foreach (@$aln_site) { $seq_str .= $_->{nt} if $_->{id} eq $id }
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"id"} eq $id 0 0 0
1054 }
1055  
1056 0 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1);
1057 0 my $end = $loc_seq->end;
1058 0 $loc_seq->end($end);
1059 0 $new_aln->add_seq($loc_seq);
1060 }
1061 0 $aln = $new_aln;
1062 }
1063  
1064 sub random_slice {
1065 0 ▶ my $slice_length = $opts{'random-slice'};
Subroutine random_slice: not called CC=1 SCAR=6.9
Pod undocumented
1066 0 my $len=$aln->length();
1067 0 my $start = int(rand($len - $slice_length+1));
1068 0 my $end = $start + $slice_length - 1;
1069 0 $aln = $aln->slice($start, $end);
1070 }
1071  
1072 sub select_third_sites {
1073 1 ▶ my $new_aln = Bio::SimpleAlign->new();
Subroutine select_third_sites: called CC=6 SCAR=17.9
Pod undocumented
1074 1 my $len=$aln->length();
1075 1 my $nseq = $aln->num_sequences();
1076 1 my @seq_ids;
1077  
1078 1 ▶ die "Alignment contains only one sequence: $file\n" if $nseq < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $nseq < 2 0 1 1
1079  
1080 1 my $ref_bases = &_get_a_site_v2();
1081 1 foreach (sort keys %$ref_bases) { push @seq_ids, $_ }
1082  
1083 1 my @sites;
1084 1 ▶ for (my $i=3; $i<=$len; $i+=3) { push @sites, $i }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 40 1 41
1085  
1086 1 foreach my $id (sort @seq_ids) {
1087 13 my $seq_str;
1088 13 $seq_str .= $ref_bases->{$id}->{$_} foreach @sites;
1089  
1090 13 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1);
1091 13 my $end = $loc_seq->end;
1092 13 $loc_seq->end($end);
1093  
1094 13 $new_aln->add_seq($loc_seq)
1095 }
1096 1 $aln = $new_aln
1097 }
1098  
1099 sub remove_third_sites {
1100 0 ▶ my $new_aln = Bio::SimpleAlign->new();
Subroutine remove_third_sites: not called CC=7 SCAR=40.3
Pod undocumented
1101 0 my $len=$aln->length();
1102 0 my $nseq = $aln->num_sequences();
1103 0 my @seq_ids;
1104  
1105 0 ▶ die "Alignment contains only one sequence: $file\n" if $nseq < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $nseq < 2 0 0 0
1106  
1107 0 my $ref_bases = &_get_a_site_v2();
1108 0 foreach (sort keys %$ref_bases) { push @seq_ids, $_ }
1109  
1110 0 my @sites;
1111 0 ▶ for (my $i=1; $i<=$len; $i++) { push @sites, $i if $i % 3 }
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $len) 0 0 0
if $i % 3 0 0 0
1112  
1113 0 foreach my $id (sort @seq_ids) {
1114 0 my $seq_str;
1115 0 $seq_str .= $ref_bases->{$id}->{$_} foreach @sites;
1116  
1117 0 my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1);
1118 0 my $end = $loc_seq->end;
1119 0 $loc_seq->end($end);
1120  
1121 0 $new_aln->add_seq($loc_seq)
1122 }
1123 0 $aln = $new_aln
1124 }
1125  
1126 sub upper_case {
1127 1 ▶ $aln->uppercase()
Subroutine upper_case: called CC=1 SCAR=0.0
Pod undocumented
1128 }
1129  
1130 ########################## internal subroutine #######################
1131  
1132 # For use in draw_codon_view
1133 # Pad display ids with a minimum of 4 spaces using the longest display id as a reference point for length. Pass-by-reference, so don't return array.
1134 # Return length of longest id plus padding.
1135 sub _pad_display_id {
1136 13 ▶ my $display_id = shift;
Subroutine _pad_display_id: called CC=1 SCAR=0.0
1137 13 my $max_len = shift;
1138 13 my $padding = ($max_len - length($display_id));
1139 13 $display_id .= " " x $padding;
1140 13 return $display_id
1141 }
1142  
1143 # Used by draw_codon_view. Calculates position of final position in alinged block, prints the current position there.
1144 sub _print_positions {
1145 2 ▶ my $nuc_count = shift;
Subroutine _print_positions: called CC=4 SCAR=13.9
1146 2 my $aln_length = shift;
1147 2 my $block_length = 3 * $opts{"codon-view"};
1148 2 my $max_id_len = shift;
1149 2 my $num_spaces = 0;
1150  
1151 2 my $start_pos = $nuc_count + 1;
1152 2 my $last_pos = 0;
1153 2 my $offset = 0;
1154 2 ▶ if (($nuc_count + $block_length) >= $aln_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nuc_count + $block_length >= $aln_length) { } 1 1 2
1155 1 $last_pos = $aln_length;
1156 1 my $diff = $aln_length - $nuc_count;
1157 1 $offset = $diff + ($diff) / 3 + ($diff % 3) - 2 # $diff % 3 gives the number of extra non-codon nucleotides
1158 } else {
1159 1 $last_pos = $nuc_count + $block_length;
1160 1 $offset = $block_length + ($block_length) / 3 - 2
1161 }
1162  
1163 # -1 since we are also printing the starting position.
1164 2 $num_spaces += $offset - 1;
1165  
1166 # $last_pos_len = length of last_pos treated as a string (ie length(335) = 3)
1167 2 my $last_pos_len = length($last_pos);
1168  
1169 # Pad $start_pos with $num_blanks blanks if it is shorter than $last_pos
1170 2 my $num_blanks = $last_pos_len - length($start_pos);
1171 2 ▶ $start_pos = " " x $num_blanks . "$start_pos" if length($start_pos) < $last_pos_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $start_pos < $last_pos_len 2 0 2
1172  
1173 2 ▶ for (my $i = 0; $i < $last_pos_len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last_pos_len) 5 2 7
1174 5 print " " x $max_id_len . substr($start_pos, $i, 1) . " " x ($num_spaces) . substr($last_pos, $i, 1) . "\n"
1175 }
1176 }
1177  
1178 # Function: _del_or_pick
1179 # Desc: Internal function. Generic code for either picking or deleting a sequence from an alignment. Used by del_seqs and pick_seqs.
1180 # Input:
1181 # $id_list, a user-supplied string consisting of comma-separated seq id values
1182 # $method, the name of the Bio::SimpleAlign method to use (remove_seq or add_seq)
1183 # $need_new, a flag indicating whether a new Bio::SimpleAlign object is needed
1184 # Returns: Nothing; uses the $aln global variable
1185  
1186 sub _del_or_pick {
1187 2 ▶ my ($id_list, $method, $need_new) = @_;
Subroutine _del_or_pick: called CC=6 SCAR=17.9
1188 2 ▶ my $new_aln = ($need_new) ? Bio::SimpleAlign->new() : $aln;
Branch: 1 branch
BranchTrueFalseTotal
$need_new ? : 1 1 2
1189  
1190 2 my @selected = split(/\s*,\s*/, $id_list);
1191 2 foreach my $seq ($aln->each_seq) {
1192 26 my $seqid = $seq->display_id();
1193 26 foreach (@selected) {
1194 65 ▶ next unless $seqid eq $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $seqid eq $_ 60 5 65
1195 5 $new_aln->$method($seq)
1196 }
1197 }
1198 2 ▶ $aln = $new_aln if $need_new == 1
Branch: 1 branch
BranchTrueFalseTotal
if $need_new == 1 1 1 2
1199 }
1200  
1201 sub _get_gaps {
1202 1 ▶ my $seq = shift;
Subroutine _get_gaps: called CC=3 SCAR=11.0
1203 1 my $seq_str = $seq->seq();
1204 1 my @chars = split //, $seq_str;
1205 1 my $cts = 0;
1206 1 my @pos=();
1207 1 ▶ for (my $i=0; $i<=$#chars; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chars) 120 1 121
1208 120 ▶ if ($chars[$i] eq '-') { push @pos, $i; $cts++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($chars[$i] eq "-") 30 90 120
1209 }
1210 1 warn "Found " . scalar(@pos) ." gaps at (@pos) on " . $seq->id() . "\n";
1211 1 return ($cts, \@pos)
1212 }
1213  
1214 sub _paste_nt {
1215 210 ▶ my $ref = shift;
Subroutine _paste_nt: called CC=2 SCAR=6.9
1216 210 my @nts;
1217 210 push @nts, $_->{nt} foreach @$ref;
1218 return \@nts
1219 210 }
1220  
1221 sub _get_a_site {
1222 210 ▶ my $pos = shift;
Subroutine _get_a_site: called CC=5 SCAR=16.1
1223 210 my (@chars, %seq_ids);
1224  
1225 210 foreach my $seq ($aln->each_seq) {
1226 2730 my $ungapped = 0;
1227 2730 $seq_ids{$seq->id()}++;
1228 2730 my $state;
1229 2730 ▶ for (my $i = 1; $i <= $pos; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $pos) 147615 2730 150345
1230 147615 $state = $seq->subseq($i, $i);
1231 147615 ▶ $ungapped++ unless $state eq '-'
Branch: 1 branch
BranchTrueFalseTotal
unless $state eq "-" 109635 37980 147615
1232 }
1233  
1234 2730 ▶ push @chars, {
Branch: 1 branch
BranchTrueFalseTotal
$state eq '-' ? : 360 2370 2730
1235 nt => $seq->subseq($pos, $pos),
1236 ungapped_pos => ($state eq '-') ? "gap" : $ungapped++,
1237 id => $seq->id(),
1238 pos => $pos,
1239 }
1240 }
1241 210 return (\@chars, \%seq_ids)
1242 }
1243  
1244 sub _seen_bases {
1245 0 ▶ my %count;
Subroutine _seen_bases: not called CC=2 SCAR=17.9
1246 0 my $ref = shift;
1247 0 my @array = @$ref;
1248 0 my $constant = 1;
1249  
1250 0 $count{$_}++ foreach @array;
1251 0 return \%count;
1252 }
1253  
1254 sub _is_constant {
1255 210 ▶ my %count;
Subroutine _is_constant: called CC=3 SCAR=11.0
1256 210 my $ref = shift;
1257 210 my @array = @$ref;
1258 210 my $constant = 1;
1259  
1260 210 $count{$_}++ foreach @array;
1261 210 my @keys = keys %count;
1262 210 ▶ $constant = 0 if @keys > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @keys > 1 34 176 210
1263 210 return $constant
1264 }
1265  
1266 sub _column_status {
1267 0 ▶ my %count;
Subroutine _column_status: not called CC=6 SCAR=37.4
1268 0 my $ref = shift;
1269 0 my @array = @$ref;
1270 0 my $st = { gap => 0, informative => 1, constant => 1 };
1271  
1272 0 foreach (@array) {
1273 0 $count{$_}++;
1274 0 ▶ $st->{gap} = 1 if $_ =~ /[\-\?]/
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /[\-\?]/ 0 0 0
1275 }
1276  
1277 0 my @keys = keys %count;
1278 0 foreach (values %count) {
1279 0 ▶ if ($_ < 2) { $st->{informative} = 0; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < 2) 0 0 0
1280 }
1281 0 ▶ $st->{constant} = 0 if @keys > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @keys > 1 0 0 0
1282 0 return $st
1283 }
1284  
1285 sub _get_a_site_v2 {
1286 2 ▶ my %seq_ids;
Subroutine _get_a_site_v2: called CC=3 SCAR=11.0
1287 2 my $len = $aln->length();
1288 2 foreach my $seq ($aln->each_seq) {
1289 26 my $id = $seq->id();
1290 26 ▶ for (my $i = 1; $i <= $len; $i++) { $seq_ids{$id}{$i} = $seq->subseq($i, $i) }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $len) 3120 26 3146
1291 }
1292 2 return \%seq_ids
1293 }
1294  
1295 sub _find_max_id_len {
1296 1 ▶ my $seqs = shift;
Subroutine _find_max_id_len: called CC=1 SCAR=0.0
1297 1 my @sorted_by_length = sort {length $a->display_id <=> length $b->display_id} @$seqs;
1298 1 return length $sorted_by_length[-1]->display_id
1299 }
1300  
1301 29 1;
1302 __END__
« blib/lib/Bio/BPWrapper/TreeManipulations.pm ↑ Summary blib/lib/Bio/BPWrapper/AlnManipulations.pm »