| 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 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use 5.010; | ||
| 21 | use Bio::AlignIO; | ||
| 22 | use Bio::SimpleAlign; | ||
| 23 | use Bio::LocatableSeq; | ||
| 24 | use Data::Dumper; | ||
| 25 | use List::Util qw(shuffle); | ||
| 26 | use Bio::Align::Utilities qw(:all); | ||
| 27 | use Exporter (); | ||
| 28 | |||
| 29 | if ($ENV{'DEBUG'}) { use Data::Dumper } | ||
| 30 | |||
| 31 | use vars qw(@ISA @EXPORT @EXPORT_OK); | ||
| 32 | |||
| 33 | @ISA = qw(Exporter); | ||
| 34 | |||
| 35 | # FIXME: some of these have too generic names like | ||
| 36 | # "upper_case" or "concat". | ||
| 37 | @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 | use Bio::BPWrapper; | ||
| 49 | # Package global variables | ||
| 50 | my ($in, $out, $aln, %opts, $file, $in_format, $out_format, @alns, $binary); | ||
| 51 | |||
| 52 | 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 | 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 | my $opts_ref = shift; | ||
| 116 | Bio::BPWrapper::common_opts($opts_ref); | ||
| 117 | %opts = %{$opts_ref}; | ||
| 118 | |||
| 119 | # This is the format that aln-manipulations expects by default | ||
| 120 | my $default_format = "clustalw"; | ||
| 121 | |||
| 122 | # assume we're getting input from standard input | ||
| 123 | |||
| 124 | $in_format = $opts{"input"} || $default_format; | ||
| 125 | |||
| 126 | if ($opts{"concat"}) { | ||
| 127 | while ($file = shift @ARGV) { | ||
| 128 | $in = Bio::AlignIO->new(-file => $file, -format => $in_format); | ||
| 129 | while ($aln=$in->next_aln()) { push @alns, $aln } | ||
| 130 | } | ||
| 131 | } else { | ||
| 132 | $file = shift @ARGV || "STDIN"; # If no more arguments were given on the command line, | ||
| 133 | $in = Bio::AlignIO->new(-format => $in_format, ($file eq "STDIN")? (-fh => \*STDIN) : (-file => $file)); | ||
| 134 | $aln = $in->next_aln() | ||
| 135 | } | ||
| 136 | |||
| 137 | $binary = $opts{"binary"} ? 1 : 0; | ||
| 138 | |||
| 139 | #### Options which *require an output FH* go *after* this #### | ||
| 140 | $out_format = $opts{"output"} || $default_format; | ||
| 141 | $out = Bio::AlignIO->new(-format => $out_format, -fh => \*STDOUT) unless $out_format eq 'paml' | ||
| 142 | } | ||
| 143 | |||
| 144 | sub can_handle { | ||
| 145 | my $option = shift; | ||
| 146 | return defined($opt_dispatch{$option}) | ||
| 147 | } | ||
| 148 | |||
| 149 | sub handle_opt { | ||
| 150 | my $option = shift; | ||
| 151 | # This passes option name to all functions | ||
| 152 | $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 | my @seq; | ||
| 163 | my $ct=0; | ||
| 164 | |||
| 165 | foreach my $seq ($aln->each_seq()) { | ||
| 166 | my $id = $seq->display_id(); | ||
| 167 | if ($seq->seq() =~ /^-+$/) { print STDERR "all gaps: $file\t$id\n"; next } | ||
| 168 | $ct++; | ||
| 169 | push @seq, $seq | ||
| 170 | } | ||
| 171 | |||
| 172 | die "No computable sequences: less than 2 seq.\n" unless $ct >= 2; | ||
| 173 | print $ct, "\t", $aln->length(), "\n"; | ||
| 174 | foreach (@seq) { | ||
| 175 | print $_->display_id(), "\n"; | ||
| 176 | 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 | my $opts = shift; | ||
| 194 | for my $option (keys %{$opts}) { | ||
| 195 | next if ($option eq 'input') || ($option eq 'output') || ($option eq 'noflatname') || ($option eq 'binary'); # Don't process these options: they are for AlignIO | ||
| 196 | |||
| 197 | if (can_handle($option)) { handle_opt($option) } # If there is a function to handle the current option, execute it | ||
| 198 | else { warn "Missing handler for: $option\n" } | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | $aln->set_displayname_flat() unless $opts{"noflatname"}; | ||
| 203 | if ($out_format eq 'paml') { &write_out_paml($aln) } | ||
| 204 | else { $out->write_aln($aln) } | ||
| 205 | } | ||
| 206 | |||
| 207 | sub phylip_non_interleaved { | ||
| 208 | my @seq; | ||
| 209 | my $ct=0; | ||
| 210 | |||
| 211 | foreach my $seq ($aln->each_seq()) { | ||
| 212 | my $id = $seq->display_id(); | ||
| 213 | if ($seq->seq() =~ /^-+$/) { print STDERR "all gaps: $file\t$id\n"; next } | ||
| 214 | $ct++; | ||
| 215 | push @seq, $seq | ||
| 216 | } | ||
| 217 | |||
| 218 | die "No computable sequences: less than 2 seq.\n" unless $ct >= 2; | ||
| 219 | print "\t", $ct, "\t", $aln->length(), "\n"; | ||
| 220 | foreach (@seq) { | ||
| 221 | printf "%-50s", $_->display_id(); | ||
| 222 | print $_->seq(), "\n" | ||
| 223 | } | ||
| 224 | exit; | ||
| 225 | } | ||
| 226 | |||
| 227 | ###################### subroutine ###################### | ||
| 228 | |||
| 229 | sub split_cdhit { | ||
| 230 | my $cls_file = $opts{'split-cdhit'}; | ||
| 231 | open IN, "<" . $cls_file || die "cdhit clstr file not found: $cls_file\n"; | ||
| 232 | my %clusters; | ||
| 233 | my $cl_id; | ||
| 234 | my @mem; | ||
| 235 | while (<IN>) { | ||
| 236 | my $line = $_; | ||
| 237 | chomp $line; | ||
| 238 | if ($line =~ /^>(\S+)\s+(\d+)/) { | ||
| 239 | $cl_id = $1 . "_" . $2; | ||
| 240 | my @mem = (); | ||
| 241 | $clusters{$cl_id} = \@mem; | ||
| 242 | } else { | ||
| 243 | my $ref = $clusters{$cl_id}; | ||
| 244 | my @mems = @$ref; | ||
| 245 | my @els = split /\s+/, $line; | ||
| 246 | my $seq_id = $els[2]; | ||
| 247 | $seq_id =~ s/>//; | ||
| 248 | $seq_id =~ s/\.\.\.$//; | ||
| 249 | push @mems, $seq_id; | ||
| 250 | $clusters{$cl_id} = \@mems; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | # print Dumper(\%clusters); | ||
| 254 | my %seqs; | ||
| 255 | foreach my $seq ($aln->each_seq()) { | ||
| 256 | my $id = $seq->display_id(); | ||
| 257 | $seqs{$id} = $seq; | ||
| 258 | } | ||
| 259 | |||
| 260 | foreach my $id (keys %clusters) { | ||
| 261 | my $out = Bio::AlignIO->new( -file => ">" . $file . "-". $id . ".aln", -format => 'clustalw'); | ||
| 262 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 263 | my @seqids = @{ $clusters{$id} }; | ||
| 264 | foreach my $seq_id (@seqids) { | ||
| 265 | $new_aln->add_seq($seqs{$seq_id}); | ||
| 266 | } | ||
| 267 | $new_aln->set_displayname_flat(); | ||
| 268 | # $new_aln = &_remove_common_gaps($new_aln); | ||
| 269 | $out->write_aln($new_aln); | ||
| 270 | } | ||
| 271 | exit; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _remove_common_gaps { | ||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | } | ||
| 279 | |||
| 280 | sub trim_ends { | ||
| 281 | my (@seqs, @gaps); | ||
| 282 | foreach my $seq ($aln->each_seq()) { | ||
| 283 | my $id = $seq->display_id(); | ||
| 284 | my @nts = split //, $seq->seq(); | ||
| 285 | my $gap_start = 0; | ||
| 286 | my $new; | ||
| 287 | for (my $i=0; $i< $aln->length(); $i++) { | ||
| 288 | if ($nts[$i] eq '-') { | ||
| 289 | if ($gap_start) { # gap -> gap | ||
| 290 | $new->{end}++; | ||
| 291 | } else { # nt -> gap | ||
| 292 | $gap_start = 1; | ||
| 293 | $new = { 'start' => $i+1, 'end' => $i+1, 'seq_name' => $id } | ||
| 294 | } | ||
| 295 | } else { | ||
| 296 | if ($gap_start) { # gap -> nt | ||
| 297 | $gap_start = 0; | ||
| 298 | push @gaps, $new; | ||
| 299 | } else { # nt -> nt | ||
| 300 | next; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | } | ||
| 304 | push @gaps, $new if $gap_start; | ||
| 305 | } | ||
| 306 | |||
| 307 | my (@three_end_gaps, @five_end_gaps); | ||
| 308 | |||
| 309 | foreach my $gap (@gaps) { | ||
| 310 | $gap->{length} = $gap->{end} - $gap->{start} + 1; | ||
| 311 | push @three_end_gaps, $gap if $gap->{start} == 1; | ||
| 312 | push @five_end_gaps, $gap if $gap->{end} == $aln->length; | ||
| 313 | } | ||
| 314 | |||
| 315 | return unless @three_end_gaps or @five_end_gaps; | ||
| 316 | |||
| 317 | my $longest_three_end = 0; | ||
| 318 | my $longest_five_start = 0; | ||
| 319 | my $longest_three_length = 0; | ||
| 320 | my $longest_five_length = 0; | ||
| 321 | |||
| 322 | foreach my $gap (@three_end_gaps) { | ||
| 323 | if ($gap->{length} > $longest_three_length) { | ||
| 324 | $longest_three_end = $gap->{end}; | ||
| 325 | $longest_three_length = $gap->{length}; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | foreach my $gap (@five_end_gaps) { | ||
| 330 | if ($gap->{length} > $longest_five_length) { | ||
| 331 | $longest_five_start = $gap->{start}; | ||
| 332 | $longest_five_length = $gap->{length}; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | # print STDERR $longest_three, "\t", $longest_five, "\n"; | ||
| 337 | if (@three_end_gaps) { | ||
| 338 | print STDERR Dumper(\@three_end_gaps); | ||
| 339 | print STDERR $longest_three_end, "\n"; | ||
| 340 | $aln = $aln->slice($longest_three_end + 1, $aln->length); | ||
| 341 | } | ||
| 342 | |||
| 343 | if (@five_end_gaps) { | ||
| 344 | print STDERR Dumper(\@five_end_gaps); | ||
| 345 | print STDERR $longest_five_start, "\n"; | ||
| 346 | $aln = $aln->slice(1, $longest_five_start - $longest_three_end - 1); | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | sub gap_states { | ||
| 351 | my (@seqs, @gaps); | ||
| 352 | foreach my $seq ($aln->each_seq()) { | ||
| 353 | my $id = $seq->display_id(); | ||
| 354 | my @nts = split //, $seq->seq(); | ||
| 355 | my $gap_start = 0; | ||
| 356 | my $new; | ||
| 357 | for (my $i=0; $i< $aln->length(); $i++) { | ||
| 358 | if ($nts[$i] eq '-') { | ||
| 359 | if ($gap_start) { # gap -> gap | ||
| 360 | $new->{end}++; | ||
| 361 | } else { # nt -> gap | ||
| 362 | $gap_start = 1; | ||
| 363 | $new = { 'start' => $i+1, 'end' => $i+1, 'seq_name' => $id } | ||
| 364 | } | ||
| 365 | } else { | ||
| 366 | if ($gap_start) { # gap -> nt | ||
| 367 | $gap_start = 0; | ||
| 368 | push @gaps, $new; | ||
| 369 | } else { # nt -> nt | ||
| 370 | next; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | } | ||
| 374 | push @gaps, $new if $gap_start; | ||
| 375 | } | ||
| 376 | my (%gap_freqs, @uniq_gaps); | ||
| 377 | foreach my $gap (@gaps) { | ||
| 378 | my $id = $gap->{start} . "-" . $gap->{end}; | ||
| 379 | $gap->{id} = $id; | ||
| 380 | $gap_freqs{$id}++; | ||
| 381 | } | ||
| 382 | |||
| 383 | foreach my $id (keys %gap_freqs) { | ||
| 384 | 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 | 'counts' => $gap_freqs{$id}, | ||
| 391 | }; | ||
| 392 | } | ||
| 393 | |||
| 394 | 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 | exit; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub gap_states_matrix { | ||
| 400 | my (@seq_ids, @gaps); | ||
| 401 | foreach my $seq ($aln->each_seq()) { | ||
| 402 | my $id = $seq->display_id(); | ||
| 403 | push @seq_ids, $id; | ||
| 404 | my @nts = split //, $seq->seq(); | ||
| 405 | my $gap_start = 0; | ||
| 406 | my $new; | ||
| 407 | for (my $i=0; $i< $aln->length(); $i++) { | ||
| 408 | if ($nts[$i] eq '-') { | ||
| 409 | if ($gap_start) { # gap -> gap | ||
| 410 | $new->{end}++; | ||
| 411 | } else { # nt -> gap | ||
| 412 | $gap_start = 1; | ||
| 413 | $new = { 'start' => $i+1, 'end' => $i+1, 'seq_name' => $id } | ||
| 414 | } | ||
| 415 | } else { | ||
| 416 | if ($gap_start) { # gap -> nt | ||
| 417 | $gap_start = 0; | ||
| 418 | push @gaps, $new; | ||
| 419 | } else { # nt -> nt | ||
| 420 | next; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 424 | push @gaps, $new if $gap_start; | ||
| 425 | } | ||
| 426 | my (%gap_freqs, @uniq_gaps, %gap_presence); | ||
| 427 | foreach my $gap (@gaps) { | ||
| 428 | my $id = $gap->{start} . "-" . $gap->{end}; | ||
| 429 | $gap->{id} = $id; | ||
| 430 | $gap_freqs{$id}++; | ||
| 431 | $gap_presence{$id}->{$gap->{seq_name}} = 1; | ||
| 432 | } | ||
| 433 | |||
| 434 | foreach my $id (keys %gap_freqs) { | ||
| 435 | 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 | 'counts' => $gap_freqs{$id}, | ||
| 442 | 'id' => $id | ||
| 443 | }; | ||
| 444 | } | ||
| 445 | |||
| 446 | my @gaps_sorted = sort {$a->{start} <=> $b->{start} || $a->{end} <=> $b->{end}} @uniq_gaps; | ||
| 447 | foreach (@gaps_sorted) { print "\t", $_->{id}} | ||
| 448 | print "\n"; | ||
| 449 | foreach my $sid (sort @seq_ids) { | ||
| 450 | print $sid; | ||
| 451 | foreach my $u_gap (@gaps_sorted) { | ||
| 452 | print "\t", $gap_presence{$u_gap->{id}}->{$sid} || 0; | ||
| 453 | } | ||
| 454 | 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 | 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 | printf "%.4f\n", $aln->average_percentage_identity(); | ||
| 474 | exit | ||
| 475 | } | ||
| 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 | my $replicates = bootstrap_replicates($aln,1); | ||
| 486 | $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 | my $block_length = 3 * $opts{"codon-view"}; | ||
| 502 | my $aln_length = $aln->length(); | ||
| 503 | my $num_seqs = $aln->num_sequences(); | ||
| 504 | my $min_pad = 4; # Minimum padding between sequence and ID | ||
| 505 | my $seq_matrix; | ||
| 506 | my @seqs = ($aln->each_seq); | ||
| 507 | my @display_ids; | ||
| 508 | |||
| 509 | # Find longest id length, add id/sequence padding | ||
| 510 | my $max_id_len = _find_max_id_len(\@seqs); | ||
| 511 | |||
| 512 | # id length includes padding | ||
| 513 | $max_id_len += $min_pad; | ||
| 514 | |||
| 515 | # Extract display_ids and sequences from AlignIO object. | ||
| 516 | foreach my $seq (@seqs) { | ||
| 517 | my @seq_str = split '', $seq->seq(); | ||
| 518 | push @$seq_matrix, \@seq_str; | ||
| 519 | push @display_ids, $seq->display_id; | ||
| 520 | |||
| 521 | # Pad display ids so that space between them and sequence is consistent | ||
| 522 | $display_ids[-1] = _pad_display_id($display_ids[-1], $max_id_len) | ||
| 523 | } | ||
| 524 | |||
| 525 | my $nuc_count = 0; | ||
| 526 | |||
| 527 | # Loop over each sequence. | ||
| 528 | for (my $i = 0; $i < $num_seqs; $i++) { | ||
| 529 | |||
| 530 | # Print count at end of block when we are starting out a new block | ||
| 531 | _print_positions($nuc_count, $aln_length, $max_id_len) if $i == 0; | ||
| 532 | |||
| 533 | # Loop over nucleotides | ||
| 534 | for (my $j = $nuc_count; $j < $aln_length; $j++) { | ||
| 535 | |||
| 536 | # When we're starting, or starting a new block, print the display id's. | ||
| 537 | print $display_ids[$i] if $j % $block_length == 0; | ||
| 538 | |||
| 539 | print "$$seq_matrix[$i]->[$j]"; | ||
| 540 | print " " if ((($j + 1) % 3) == 0); | ||
| 541 | |||
| 542 | # When we've reached the end of the alignment or a block | ||
| 543 | if ($j + 1 == $aln_length || (($j + 1) % $block_length) == 0) { | ||
| 544 | if ($i + 1 == $num_seqs) { $nuc_count = $j + 1 } # If this is the last sequence, save the ending (next) position. | ||
| 545 | else { print "\n" } # Otherwise, start on the next line. | ||
| 546 | last # In either case, need to exit this loop. | ||
| 547 | } | ||
| 548 | } # END for LOOP OVER NUCLEOTIDES | ||
| 549 | |||
| 550 | # Finish if we've reached the end of the alignment, and the last sequence | ||
| 551 | if (($i + 1 == $num_seqs) && ($nuc_count == $aln_length)) { print "\n"; last } | ||
| 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)) { | ||
| 556 | $i = -1; # Always increments after a loop; next increment sets to 0. | ||
| 557 | print "\n\n" | ||
| 558 | } | ||
| 559 | } # END for LOOP OVER SEQUENCES | ||
| 560 | |||
| 561 | # Can't let script terminate normally: produces traditional alignment output | ||
| 562 | 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 | _del_or_pick($opts{"delete"}, "remove_seq", 0) | ||
| 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 | $aln = $aln->remove_gaps() | ||
| 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 | say $aln->length(); | ||
| 596 | exit | ||
| 597 | } | ||
| 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 | $aln->match() | ||
| 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 | say $aln->num_sequences(); | ||
| 619 | exit | ||
| 620 | } | ||
| 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 | _del_or_pick($opts{"pick"}, "add_seq", 1) | ||
| 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 | $aln = $aln->set_new_reference($opts{"refseq"}) | ||
| 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 | my ($begin, $end) = split(/\s*,\s*/, $opts{"slice"}); | ||
| 661 | |||
| 662 | # Allow for one parameter to be omitted. Default $begin to the | ||
| 663 | # beginning of the alignment, and $end to the end. | ||
| 664 | $begin = 1 if $begin eq "-"; | ||
| 665 | $end = $aln->length if $end eq "-"; | ||
| 666 | $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 | $aln->verbose(1); | ||
| 679 | $aln = $aln->uniq_seq(); | ||
| 680 | } | ||
| 681 | |||
| 682 | sub _has_gap { | ||
| 683 | my $ref = shift; | ||
| 684 | foreach (@$ref) { | ||
| 685 | return 1 if $_ eq '-'; | ||
| 686 | } | ||
| 687 | return 0; | ||
| 688 | } | ||
| 689 | |||
| 690 | sub _has_singleton { | ||
| 691 | my $ref = shift; | ||
| 692 | foreach my $key (keys %$ref) { | ||
| 693 | return 1 if $ref->{$key} == 1; | ||
| 694 | } | ||
| 695 | 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 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 707 | my $len=$aln->length(); | ||
| 708 | my (@seq_ids, @inf_sites, %bin_chars); | ||
| 709 | |||
| 710 | # Go through each column and save variable sites | ||
| 711 | my $ref_bases = &_get_a_site_v2(); #print Dumper($ref_bases); exit; | ||
| 712 | foreach (sort keys %$ref_bases) { push @seq_ids, $_ } | ||
| 713 | for (my $i=1; $i<=$len; $i++) { | ||
| 714 | my (%seen, @bases); | ||
| 715 | foreach my $id (@seq_ids) { push @bases, $ref_bases->{$id}->{$i}; } | ||
| 716 | %seen = %{&_seen_bases(\@bases)}; | ||
| 717 | next if &_has_gap( [ values %seen ] ); | ||
| 718 | next if keys %seen != 2; | ||
| 719 | next if &_has_singleton(\%seen); | ||
| 720 | my ($base1, $base2) = sort keys %seen; | ||
| 721 | $bin_chars{$i}{$base1} = 0; | ||
| 722 | $bin_chars{$i}{$base2} = 1; | ||
| 723 | push @inf_sites, $i; | ||
| 724 | } | ||
| 725 | |||
| 726 | die "informative sites not found\n" unless @inf_sites; | ||
| 727 | foreach (@inf_sites) { warn $_, "\n" } | ||
| 728 | |||
| 729 | # Recreate the object for output | ||
| 730 | foreach my $id (@seq_ids) { | ||
| 731 | my $seq_str; | ||
| 732 | foreach my $i (@inf_sites) { | ||
| 733 | $seq_str .= $binary ? $bin_chars{$i}->{$ref_bases->{$id}->{$i}} : $ref_bases->{$id}->{$i}; | ||
| 734 | } | ||
| 735 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1); | ||
| 736 | my $end = $loc_seq->end; | ||
| 737 | $loc_seq->end($end); | ||
| 738 | $new_aln->add_seq($loc_seq) | ||
| 739 | } | ||
| 740 | |||
| 741 | $aln = $new_aln | ||
| 742 | } | ||
| 743 | |||
| 744 | =head2 variable_sites() | ||
| 745 | |||
| 746 | Extracts variable sites. | ||
| 747 | |||
| 748 | =cut | ||
| 749 | sub variable_sites { | ||
| 750 | $aln = $aln->remove_gaps(); | ||
| 751 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 752 | my $len=$aln->length(); | ||
| 753 | my (%seq_ids, @sites, @var_sites); | ||
| 754 | |||
| 755 | # Go through each column and save variable sites | ||
| 756 | for (my $i=1; $i<=$len; $i++) { | ||
| 757 | my ($ref_bases, $ref_ids) = &_get_a_site($i); | ||
| 758 | %seq_ids = %{$ref_ids}; | ||
| 759 | my $is_constant = &_is_constant(&_paste_nt($ref_bases)); | ||
| 760 | if ($is_constant < 1) { push @sites, $ref_bases; push @var_sites, $i } | ||
| 761 | } | ||
| 762 | |||
| 763 | foreach (@var_sites) { warn $_, "\n" } | ||
| 764 | |||
| 765 | # Recreate the object for output | ||
| 766 | foreach my $id (sort keys %seq_ids) { | ||
| 767 | my $seq_str; | ||
| 768 | foreach my $aln_site (@sites) { | ||
| 769 | foreach (@$aln_site) { $seq_str .= $_->{nt} if $_->{id} eq $id } | ||
| 770 | } | ||
| 771 | |||
| 772 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1); | ||
| 773 | my $end = $loc_seq->end; | ||
| 774 | $loc_seq->end($end); | ||
| 775 | $new_aln->add_seq($loc_seq) | ||
| 776 | } | ||
| 777 | |||
| 778 | $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 | my $window_sz = $opts{"window"}; | ||
| 792 | for my $i (1 .. ($aln->length() - $window_sz + 1)) { | ||
| 793 | my $slice = $aln->slice($i, $i + $window_sz - 1); | ||
| 794 | my $pi = (100 - $slice->average_percentage_identity()) / 100; | ||
| 795 | printf "%d\t%d\t%.4f\n", $i, $i + $window_sz - 1, $pi | ||
| 796 | } | ||
| 797 | exit | ||
| 798 | } | ||
| 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 | $aln = cat(@alns) | ||
| 813 | } | ||
| 814 | |||
| 815 | |||
| 816 | sub conserved_blocks { | ||
| 817 | my $len=$aln->length(); | ||
| 818 | my $nseq = $aln->num_sequences(); | ||
| 819 | my $min_block_size = $opts{"conblocks"}; | ||
| 820 | my %seq_ids; | ||
| 821 | |||
| 822 | die "Alignment contains only one sequence: $file\n" if $nseq < 2; | ||
| 823 | |||
| 824 | my (@blocks, $block); | ||
| 825 | my $in_block=0; | ||
| 826 | for (my $i=1; $i<=$len; $i++) { | ||
| 827 | my ($ref_bases, $ref_ids) = &_get_a_site($i); | ||
| 828 | %seq_ids = %{$ref_ids}; | ||
| 829 | my $is_constant = &_is_constant(&_paste_nt($ref_bases)); | ||
| 830 | if ($in_block) { # previous site is a contant one | ||
| 831 | if ($is_constant) { | ||
| 832 | $block->{length} ++; | ||
| 833 | my @sites = @{$block->{sites}}; | ||
| 834 | push @sites, $ref_bases; | ||
| 835 | $block->{sites} = \@sites; | ||
| 836 | if ($i == $len) { | ||
| 837 | warn "Leaving a constant block at the end of alignment: $i\n"; | ||
| 838 | push @blocks, $block if $block->{length} >= $min_block_size | ||
| 839 | } | ||
| 840 | } else { | ||
| 841 | $in_block = 0; | ||
| 842 | push @blocks, $block if $block->{length} >= $min_block_size; | ||
| 843 | warn "Leaving a constant block at $i\n" | ||
| 844 | } | ||
| 845 | } else { # previous site not a constant one | ||
| 846 | if ($is_constant) { # entering a block | ||
| 847 | warn "Entering a constant block at site $i ...\n"; | ||
| 848 | $in_block=1; | ||
| 849 | $block = {start => $i, length => 1, num_seq => $nseq, sites => [($ref_bases)]} # start a new block | ||
| 850 | } | ||
| 851 | } | ||
| 852 | } | ||
| 853 | |||
| 854 | foreach my $bl (@blocks) { | ||
| 855 | my $out = Bio::AlignIO->new(-file=> ">$file" . ".slice-". $bl->{start} . ".aln" , -format=>'clustalw'); | ||
| 856 | my $block_aln = Bio::SimpleAlign->new(); | ||
| 857 | foreach my $id (sort keys %seq_ids) { | ||
| 858 | my ($seq_str, $ungapped_start, $ungapped_end); | ||
| 859 | my @sites = @{$bl->{sites}}; | ||
| 860 | for (my $i = 0; $i <= $#sites; $i++) { | ||
| 861 | my $ref_chars = $sites[$i]; | ||
| 862 | foreach (@$ref_chars) { | ||
| 863 | next unless $_->{id} eq $id; | ||
| 864 | $ungapped_start = $_->{ungapped_pos} if $i == 0; | ||
| 865 | $ungapped_end = $_->{ungapped_pos} if $i == $#sites; | ||
| 866 | $seq_str .= $_->{nt} | ||
| 867 | } | ||
| 868 | } | ||
| 869 | |||
| 870 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => $ungapped_start, -end => $ungapped_end); | ||
| 871 | $block_aln->add_seq($loc_seq) | ||
| 872 | } | ||
| 873 | $out->write_aln($block_aln) | ||
| 874 | } | ||
| 875 | exit | ||
| 876 | } | ||
| 877 | |||
| 878 | sub get_consensus { | ||
| 879 | my $percent_threshold = $opts{"consensus"}; | ||
| 880 | 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 | $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 | $aln = dna_to_aa_aln($aln) | ||
| 899 | } | ||
| 900 | |||
| 901 | sub remove_gapped_cols_in_one_seq { | ||
| 902 | my $id = $opts{"erasecol"}; | ||
| 903 | my $nmatch=0; | ||
| 904 | my $ref_seq; | ||
| 905 | foreach ($aln->each_seq) { | ||
| 906 | if ($_->id() =~ /$id/) { $nmatch++; $ref_seq = $_ } | ||
| 907 | } | ||
| 908 | die "Quit. No ref seq found or more than one ref seq!\n" if !$nmatch || $nmatch > 1; | ||
| 909 | my ($ct_gap, $ref) = &_get_gaps($ref_seq); | ||
| 910 | warn "Original length: " . $aln->length() . "\n"; | ||
| 911 | if ($ct_gap) { | ||
| 912 | my @args; | ||
| 913 | push @args, [$_, $_] foreach @$ref; | ||
| 914 | $aln = $aln->remove_columns(@args); | ||
| 915 | warn "New length: " . $aln->length() . "\n" | ||
| 916 | } else { | ||
| 917 | warn "No gap: " . $aln->length() . "\n" | ||
| 918 | } | ||
| 919 | } | ||
| 920 | |||
| 921 | sub colnum_from_residue_pos { | ||
| 922 | my ($id, $pos) = split /\s*,\s*/, $opts{"aln-index"}; | ||
| 923 | print $aln->column_from_residue_number($id, $pos), "\n"; | ||
| 924 | exit | ||
| 925 | } | ||
| 926 | |||
| 927 | =head2 list_ids() | ||
| 928 | |||
| 929 | List all sequence ids. | ||
| 930 | |||
| 931 | =cut | ||
| 932 | |||
| 933 | sub list_ids { | ||
| 934 | my @ids; | ||
| 935 | foreach ($aln->each_seq) { push @ids, $_->display_id() } | ||
| 936 | say join "\n", @ids; | ||
| 937 | exit | ||
| 938 | } | ||
| 939 | |||
| 940 | sub permute_states { | ||
| 941 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 942 | my $len=$aln->length(); | ||
| 943 | my $nseq = $aln->num_sequences(); | ||
| 944 | my @seq_ids; | ||
| 945 | |||
| 946 | die "Alignment contains only one sequence: $file\n" if $nseq < 2; | ||
| 947 | |||
| 948 | my @sites; | ||
| 949 | my $ref_bases = &_get_a_site_v2(); | ||
| 950 | foreach (sort keys %$ref_bases) { push @seq_ids, $_ } | ||
| 951 | for (my $i=1; $i<=$len; $i++) { | ||
| 952 | my @bases; | ||
| 953 | foreach (keys %$ref_bases) { push @bases, $ref_bases->{$_}->{$i} } | ||
| 954 | @bases = shuffle(@bases); | ||
| 955 | for (my $j=0; $j<$nseq; $j++) { $ref_bases->{$seq_ids[$j]}->{$i} = $bases[$j] } | ||
| 956 | } | ||
| 957 | |||
| 958 | foreach my $id (@seq_ids) { | ||
| 959 | my $seq_str; | ||
| 960 | for (my $i=1; $i<=$len; $i++) { $seq_str .= $ref_bases->{$id}->{$i} } | ||
| 961 | |||
| 962 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1); | ||
| 963 | my $end = $loc_seq->end; | ||
| 964 | $loc_seq->end($end); | ||
| 965 | $new_aln->add_seq($loc_seq) | ||
| 966 | } | ||
| 967 | $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 | use Bio::SeqIO; | ||
| 980 | my $cds_in = Bio::SeqIO->new(-file=>$opts{pep2dna}, -format=>'fasta'); | ||
| 981 | my %CDSs; | ||
| 982 | while (my $seq = $cds_in->next_seq()) { $CDSs{$seq->display_id()} = $seq } | ||
| 983 | $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 | my $num_seqs = $aln->num_sequences; | ||
| 1004 | my $sample_size = ($opts{"resample"} == 0) ? int($num_seqs / 2) : $opts{"resample"}; | ||
| 1005 | |||
| 1006 | die "Error: sample size ($sample_size) exceeds number of sequences in alignment: ($num_seqs)" if $sample_size > $num_seqs; | ||
| 1007 | |||
| 1008 | # Use Reservoir Sampling to pick random sequences. | ||
| 1009 | my @sampled = (1 .. $sample_size); | ||
| 1010 | for my $j ($sample_size + 1 .. $num_seqs) { | ||
| 1011 | $sampled[ rand(@sampled) ] = $j if rand() <= ($sample_size / $j) | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | warn "Sampled the following sequences: @sampled\n\n"; | ||
| 1015 | my $tmp_aln = $aln->select_noncont(@sampled); | ||
| 1016 | $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 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 1030 | my $len = $aln->length(); | ||
| 1031 | my $nseq = $aln->num_sequences(); | ||
| 1032 | my %seq_ids; | ||
| 1033 | |||
| 1034 | die "Alignment contains only one sequence: $file\n" if $nseq < 2; | ||
| 1035 | |||
| 1036 | my @sites; | ||
| 1037 | for (my $i=1; $i<=$len; $i++) { | ||
| 1038 | my ($ref_bases, $ref_ids) = &_get_a_site($i); | ||
| 1039 | %seq_ids = %{$ref_ids}; | ||
| 1040 | push @sites, $ref_bases | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | @sites = shuffle(@sites); | ||
| 1044 | |||
| 1045 | my @order; | ||
| 1046 | push @order, $_->[0]->{pos} foreach @sites; | ||
| 1047 | print STDERR "Shuffled site order:\t", join(",", @order); | ||
| 1048 | print STDERR "\n"; | ||
| 1049 | |||
| 1050 | foreach my $id (sort keys %seq_ids) { | ||
| 1051 | my $seq_str; | ||
| 1052 | foreach my $aln_site (@sites) { | ||
| 1053 | foreach (@$aln_site) { $seq_str .= $_->{nt} if $_->{id} eq $id } | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1); | ||
| 1057 | my $end = $loc_seq->end; | ||
| 1058 | $loc_seq->end($end); | ||
| 1059 | $new_aln->add_seq($loc_seq); | ||
| 1060 | } | ||
| 1061 | $aln = $new_aln; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | sub random_slice { | ||
| 1065 | my $slice_length = $opts{'random-slice'}; | ||
| 1066 | my $len=$aln->length(); | ||
| 1067 | my $start = int(rand($len - $slice_length+1)); | ||
| 1068 | my $end = $start + $slice_length - 1; | ||
| 1069 | $aln = $aln->slice($start, $end); | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | sub select_third_sites { | ||
| 1073 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 1074 | my $len=$aln->length(); | ||
| 1075 | my $nseq = $aln->num_sequences(); | ||
| 1076 | my @seq_ids; | ||
| 1077 | |||
| 1078 | die "Alignment contains only one sequence: $file\n" if $nseq < 2; | ||
| 1079 | |||
| 1080 | my $ref_bases = &_get_a_site_v2(); | ||
| 1081 | foreach (sort keys %$ref_bases) { push @seq_ids, $_ } | ||
| 1082 | |||
| 1083 | my @sites; | ||
| 1084 | for (my $i=3; $i<=$len; $i+=3) { push @sites, $i } | ||
| 1085 | |||
| 1086 | foreach my $id (sort @seq_ids) { | ||
| 1087 | my $seq_str; | ||
| 1088 | $seq_str .= $ref_bases->{$id}->{$_} foreach @sites; | ||
| 1089 | |||
| 1090 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1); | ||
| 1091 | my $end = $loc_seq->end; | ||
| 1092 | $loc_seq->end($end); | ||
| 1093 | |||
| 1094 | $new_aln->add_seq($loc_seq) | ||
| 1095 | } | ||
| 1096 | $aln = $new_aln | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | sub remove_third_sites { | ||
| 1100 | my $new_aln = Bio::SimpleAlign->new(); | ||
| 1101 | my $len=$aln->length(); | ||
| 1102 | my $nseq = $aln->num_sequences(); | ||
| 1103 | my @seq_ids; | ||
| 1104 | |||
| 1105 | die "Alignment contains only one sequence: $file\n" if $nseq < 2; | ||
| 1106 | |||
| 1107 | my $ref_bases = &_get_a_site_v2(); | ||
| 1108 | foreach (sort keys %$ref_bases) { push @seq_ids, $_ } | ||
| 1109 | |||
| 1110 | my @sites; | ||
| 1111 | for (my $i=1; $i<=$len; $i++) { push @sites, $i if $i % 3 } | ||
| 1112 | |||
| 1113 | foreach my $id (sort @seq_ids) { | ||
| 1114 | my $seq_str; | ||
| 1115 | $seq_str .= $ref_bases->{$id}->{$_} foreach @sites; | ||
| 1116 | |||
| 1117 | my $loc_seq = Bio::LocatableSeq->new(-seq => $seq_str, -id => $id, -start => 1); | ||
| 1118 | my $end = $loc_seq->end; | ||
| 1119 | $loc_seq->end($end); | ||
| 1120 | |||
| 1121 | $new_aln->add_seq($loc_seq) | ||
| 1122 | } | ||
| 1123 | $aln = $new_aln | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | sub upper_case { | ||
| 1127 | $aln->uppercase() | ||
| 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 | my $display_id = shift; | ||
| 1137 | my $max_len = shift; | ||
| 1138 | my $padding = ($max_len - length($display_id)); | ||
| 1139 | $display_id .= " " x $padding; | ||
| 1140 | 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 | my $nuc_count = shift; | ||
| 1146 | my $aln_length = shift; | ||
| 1147 | my $block_length = 3 * $opts{"codon-view"}; | ||
| 1148 | my $max_id_len = shift; | ||
| 1149 | my $num_spaces = 0; | ||
| 1150 | |||
| 1151 | my $start_pos = $nuc_count + 1; | ||
| 1152 | my $last_pos = 0; | ||
| 1153 | my $offset = 0; | ||
| 1154 | if (($nuc_count + $block_length) >= $aln_length) { | ||
| 1155 | $last_pos = $aln_length; | ||
| 1156 | my $diff = $aln_length - $nuc_count; | ||
| 1157 | $offset = $diff + ($diff) / 3 + ($diff % 3) - 2 # $diff % 3 gives the number of extra non-codon nucleotides | ||
| 1158 | } else { | ||
| 1159 | $last_pos = $nuc_count + $block_length; | ||
| 1160 | $offset = $block_length + ($block_length) / 3 - 2 | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | # -1 since we are also printing the starting position. | ||
| 1164 | $num_spaces += $offset - 1; | ||
| 1165 | |||
| 1166 | # $last_pos_len = length of last_pos treated as a string (ie length(335) = 3) | ||
| 1167 | my $last_pos_len = length($last_pos); | ||
| 1168 | |||
| 1169 | # Pad $start_pos with $num_blanks blanks if it is shorter than $last_pos | ||
| 1170 | my $num_blanks = $last_pos_len - length($start_pos); | ||
| 1171 | $start_pos = " " x $num_blanks . "$start_pos" if length($start_pos) < $last_pos_len; | ||
| 1172 | |||
| 1173 | for (my $i = 0; $i < $last_pos_len; $i++) { | ||
| 1174 | 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 | my ($id_list, $method, $need_new) = @_; | ||
| 1188 | my $new_aln = ($need_new) ? Bio::SimpleAlign->new() : $aln; | ||
| 1189 | |||
| 1190 | my @selected = split(/\s*,\s*/, $id_list); | ||
| 1191 | foreach my $seq ($aln->each_seq) { | ||
| 1192 | my $seqid = $seq->display_id(); | ||
| 1193 | foreach (@selected) { | ||
| 1194 | next unless $seqid eq $_; | ||
| 1195 | $new_aln->$method($seq) | ||
| 1196 | } | ||
| 1197 | } | ||
| 1198 | $aln = $new_aln if $need_new == 1 | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | sub _get_gaps { | ||
| 1202 | my $seq = shift; | ||
| 1203 | my $seq_str = $seq->seq(); | ||
| 1204 | my @chars = split //, $seq_str; | ||
| 1205 | my $cts = 0; | ||
| 1206 | my @pos=(); | ||
| 1207 | for (my $i=0; $i<=$#chars; $i++) { | ||
| 1208 | if ($chars[$i] eq '-') { push @pos, $i; $cts++ } | ||
| 1209 | } | ||
| 1210 | warn "Found " . scalar(@pos) ." gaps at (@pos) on " . $seq->id() . "\n"; | ||
| 1211 | return ($cts, \@pos) | ||
| 1212 | } | ||
| 1213 | |||
| 1214 | sub _paste_nt { | ||
| 1215 | my $ref = shift; | ||
| 1216 | my @nts; | ||
| 1217 | push @nts, $_->{nt} foreach @$ref; | ||
| 1218 | return \@nts | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | sub _get_a_site { | ||
| 1222 | my $pos = shift; | ||
| 1223 | my (@chars, %seq_ids); | ||
| 1224 | |||
| 1225 | foreach my $seq ($aln->each_seq) { | ||
| 1226 | my $ungapped = 0; | ||
| 1227 | $seq_ids{$seq->id()}++; | ||
| 1228 | my $state; | ||
| 1229 | for (my $i = 1; $i <= $pos; $i++) { | ||
| 1230 | $state = $seq->subseq($i, $i); | ||
| 1231 | $ungapped++ unless $state eq '-' | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | push @chars, { | ||
| 1235 | nt => $seq->subseq($pos, $pos), | ||
| 1236 | ungapped_pos => ($state eq '-') ? "gap" : $ungapped++, | ||
| 1237 | id => $seq->id(), | ||
| 1238 | pos => $pos, | ||
| 1239 | } | ||
| 1240 | } | ||
| 1241 | return (\@chars, \%seq_ids) | ||
| 1242 | } | ||
| 1243 | |||
| 1244 | sub _seen_bases { | ||
| 1245 | my %count; | ||
| 1246 | my $ref = shift; | ||
| 1247 | my @array = @$ref; | ||
| 1248 | my $constant = 1; | ||
| 1249 | |||
| 1250 | $count{$_}++ foreach @array; | ||
| 1251 | return \%count; | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | sub _is_constant { | ||
| 1255 | my %count; | ||
| 1256 | my $ref = shift; | ||
| 1257 | my @array = @$ref; | ||
| 1258 | my $constant = 1; | ||
| 1259 | |||
| 1260 | $count{$_}++ foreach @array; | ||
| 1261 | my @keys = keys %count; | ||
| 1262 | $constant = 0 if @keys > 1; | ||
| 1263 | return $constant | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | sub _column_status { | ||
| 1267 | my %count; | ||
| 1268 | my $ref = shift; | ||
| 1269 | my @array = @$ref; | ||
| 1270 | my $st = { gap => 0, informative => 1, constant => 1 }; | ||
| 1271 | |||
| 1272 | foreach (@array) { | ||
| 1273 | $count{$_}++; | ||
| 1274 | $st->{gap} = 1 if $_ =~ /[\-\?]/ | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | my @keys = keys %count; | ||
| 1278 | foreach (values %count) { | ||
| 1279 | if ($_ < 2) { $st->{informative} = 0; last } | ||
| 1280 | } | ||
| 1281 | $st->{constant} = 0 if @keys > 1; | ||
| 1282 | return $st | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | sub _get_a_site_v2 { | ||
| 1286 | my %seq_ids; | ||
| 1287 | my $len = $aln->length(); | ||
| 1288 | foreach my $seq ($aln->each_seq) { | ||
| 1289 | my $id = $seq->id(); | ||
| 1290 | for (my $i = 1; $i <= $len; $i++) { $seq_ids{$id}{$i} = $seq->subseq($i, $i) } | ||
| 1291 | } | ||
| 1292 | return \%seq_ids | ||
| 1293 | } | ||
| 1294 | |||
| 1295 | sub _find_max_id_len { | ||
| 1296 | my $seqs = shift; | ||
| 1297 | my @sorted_by_length = sort {length $a->display_id <=> length $b->display_id} @$seqs; | ||
| 1298 | return length $sorted_by_length[-1]->display_id | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | 1; | ||
| 1302 | __END__ |