| 1 | =encoding utf8 | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Bio::Wrapper::PopManipulations - Functions for biopop | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Bio::BPWrapper::PopManipulations; | ||
| 10 | # Set options hash ... | ||
| 11 | initialize(\%opts); | ||
| 12 | write_out(\%opts); | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Bio::BPWrapper::PopManipulations; | ||
| 17 | |||
| 18 | # MK method is broken: change to comma-delimited way of specifying in group and out groups. | ||
| 19 | use strict; # Still on 5.10, so need this for strict | ||
| 20 | use warnings; | ||
| 21 | use 5.010; | ||
| 22 | use Bio::AlignIO; | ||
| 23 | use Bio::Align::DNAStatistics; | ||
| 24 | |||
| 25 | #use Bio::PopGen::IO; | ||
| 26 | use FindBin; # Find the location of PopGenStatistics | ||
| 27 | use lib "$FindBin::Bin"; # to use it as a lib path | ||
| 28 | #use PopGenStatistics; | ||
| 29 | use Bio::PopGen::Utilities; | ||
| 30 | use Bio::PopGen::Statistics; | ||
| 31 | use Bio::PopGen::Population; | ||
| 32 | #use Algorithm::Numerical::Sample qw(sample); | ||
| 33 | use List::Util qw(shuffle sum); | ||
| 34 | #use Math::Random::MT::Auto qw(rand); | ||
| 35 | #use Statistics::Basic qw(:all); | ||
| 36 | use Bio::Tools::CodonTable; | ||
| 37 | use Data::Dumper; | ||
| 38 | use Exporter (); | ||
| 39 | |||
| 40 | use vars qw(@ISA @EXPORT @EXPORT_OK); | ||
| 41 | |||
| 42 | @ISA = qw(Exporter); | ||
| 43 | |||
| 44 | # FIXME: some of these might be put in | ||
| 45 | # a common routine like print_version | ||
| 46 | @EXPORT = qw(initialize can_handle handle_opt | ||
| 47 | print_distance print_heterozygosity print_mismatch_distr | ||
| 48 | count_four_gametes print_diversity bi_partition | ||
| 49 | bisites_for_r bisites snp_noncoding snp_coding | ||
| 50 | snp_coding_log print_num_snps | ||
| 51 | ); | ||
| 52 | |||
| 53 | # Package global variables | ||
| 54 | my ($opts, $flags, $aln_file, $aln, $in, | ||
| 55 | $pop, $sample_size, $stat_obj, @stats, $sim, | ||
| 56 | $sim_type, @ingroups, @outgroups, $dist_method, $dna_stats, | ||
| 57 | $pop_stats, @var_sites, @exgroups, $ingroup, $outgroup, $pop_cds, | ||
| 58 | $myCodonTable | ||
| 59 | ); | ||
| 60 | |||
| 61 | |||
| 62 | use Bio::BPWrapper; | ||
| 63 | my $VERSION = $Bio::BPWrapper::VERSION; | ||
| 64 | |||
| 65 | my %opt_dispatch = ( | ||
| 66 | 'bisites' => \&bisites, | ||
| 67 | 'bisites-for-r' => \&bisites_for_r, | ||
| 68 | 'bihaps' => \&count_four_gametes, | ||
| 69 | 'distance' => \&print_distance, | ||
| 70 | 'heterozygosity' => \&print_heterozygosity, | ||
| 71 | 'mismatch' => \&print_mismatch_distr, | ||
| 72 | 'mut-info' => \&pairwise_mutual_info, | ||
| 73 | 'pi' => \&print_diversity, | ||
| 74 | 'stats' => \&print_stats, | ||
| 75 | 'segsites' => \&print_num_snps, | ||
| 76 | 'snp-coding' => \&snp_coding, | ||
| 77 | 'snp-coding-long' => \&snp_coding_long, | ||
| 78 | 'snp-noncoding' => \&snp_noncoding, | ||
| 79 | 'bipart' => \&bi_partition, | ||
| 80 | # 'mutrec' => \&_mutation_or_recombination, | ||
| 81 | # 'simmk' => \&_sim_mk, | ||
| 82 | # 'kaks' => \&_print_kaks_calc, | ||
| 83 | ); | ||
| 84 | |||
| 85 | # The following options cannot be used along with distance or kaks opts | ||
| 86 | my @popgen_list = qw(stats mismatch simmk); | ||
| 87 | $myCodonTable = Bio::Tools::CodonTable->new( -id => 11 ); | ||
| 88 | |||
| 89 | ##################### initializer & option handlers ################### | ||
| 90 | |||
| 91 | ## TODO DNAStatistics subs | ||
| 92 | |||
| 93 | sub initialize { | ||
| 94 | ($opts, $flags) = @_; | ||
| 95 | Bio::BPWrapper::common_opts($opts); | ||
| 96 | |||
| 97 | $aln_file = shift @ARGV || "STDIN"; # If no more arguments were given on the command line, assume we're getting input from standard input | ||
| 98 | |||
| 99 | my $in_format = $flags->{"input"} // 'fasta'; | ||
| 100 | |||
| 101 | if ($aln_file eq "STDIN") { $in = Bio::AlignIO->new(-format => $in_format, -fh => \*STDIN) } # We're getting input from STDIN | ||
| 102 | else { $in = Bio::AlignIO->new(-format => $in_format, -file => "<$aln_file") } # Filename, or '-', was given | ||
| 103 | |||
| 104 | $aln = $in->next_aln; | ||
| 105 | |||
| 106 | $sample_size = $flags->{"sample_size"} // undef; | ||
| 107 | # @ingroups = split /\s+,\s+/, join ',', @{ $opts->{"ingroup"} } // undef if $opts->{"ingroup"};; | ||
| 108 | # @outgroups = split /\s+,\s+/, join ',', @{ $opts->{"outgroup"} } // undef if $opts->{"outgroup"}; | ||
| 109 | # @exgroups = split /\s+,\s+/, join ',', @{ $opts->{"exclude"} } // undef if $opts->{"exclude"}; | ||
| 110 | $dist_method = $flags->{"dist-method"} // undef; | ||
| 111 | if ($opts->{"distance"} || $opts->{"kaks"}) { | ||
| 112 | die "Cannot use distance or kaks options together with any of the following: @popgen_list\n" if &_in_list($opts, \@popgen_list); | ||
| 113 | $dna_stats = Bio::Align::DNAStatistics->new(); | ||
| 114 | } else { | ||
| 115 | $pop = Bio::PopGen::Utilities->aln_to_population(-alignment => $aln, -include_monomorphic => $opts->{"snp-noncoding"} || $opts->{'bisites'} || $opts->{'bihaps'} || $opts->{'bisites-for-r'}? 0:1, -site_model => 'all'); | ||
| 116 | $pop_cds = Bio::PopGen::Utilities->aln_to_population(-alignment => $aln, -include_monomorphic => 0, -site_model => 'codon') if $opts->{"snp-coding"} || $opts->{"snp-coding-long"}; | ||
| 117 | # $stat_obj = PopGenStatistics->new(); | ||
| 118 | $pop_stats = Bio::PopGen::Statistics->new() | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub can_handle { | ||
| 123 | my $option = shift; | ||
| 124 | return defined($opt_dispatch{$option}) | ||
| 125 | } | ||
| 126 | |||
| 127 | sub handle_opt { | ||
| 128 | my $option = shift; | ||
| 129 | # This passes option name to all functions | ||
| 130 | $opt_dispatch{$option}->($option) | ||
| 131 | } | ||
| 132 | |||
| 133 | ######################## subroutine ############################# | ||
| 134 | |||
| 135 | sub print_distance { | ||
| 136 | my $warn_bad_dist_method; | ||
| 137 | local $SIG{__WARN__} = sub { $warn_bad_dist_method .= shift }; | ||
| 138 | my $dist_matrix = $dna_stats->distance(-align => $aln, -method => $dist_method); | ||
| 139 | die "$warn_bad_dist_method\nQuitting on bad distance method...\n" if $warn_bad_dist_method; | ||
| 140 | say $dist_matrix->print_matrix | ||
| 141 | } | ||
| 142 | |||
| 143 | sub print_heterozygosity { | ||
| 144 | print "Heterozygosity=>\n"; | ||
| 145 | for my $name ($pop->get_marker_names()) { | ||
| 146 | my $marker = $pop->get_Marker($name); | ||
| 147 | my @alleles = $marker->get_Alleles(); | ||
| 148 | my %allele_freqs = $marker->get_Allele_Frequencies(); | ||
| 149 | push @var_sites, $name; | ||
| 150 | printf "\t\t%s\t%.4f\n", $name, &heterozygosity(\%allele_freqs) | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | sub print_mismatch_distr { | ||
| 155 | my $num_seq = $aln->num_sequences(); | ||
| 156 | my @seqs; | ||
| 157 | push @seqs, $_ foreach $aln->each_seq; | ||
| 158 | for (my $i = 0; $i < $num_seq - 1; $i++) { | ||
| 159 | for (my $j = $i + 1; $j < $num_seq; $j++) { | ||
| 160 | my $new = Bio::SimpleAlign->new(); | ||
| 161 | $new->add_seq($seqs[$i]); | ||
| 162 | $new->add_seq($seqs[$j]); | ||
| 163 | printf "%.4f\n", (100 - $new->percentage_identity) / 100 | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | sub count_four_gametes { # four gamete test for recombination (and wilson's test of compatibility; add epistasis) | ||
| 169 | my @valid_sites = &_two_allele_nogap_informative_sites($pop); | ||
| 170 | my $ref_seqs = &_base_at_snp_sites($pop, \@valid_sites); | ||
| 171 | my %myseqs = %$ref_seqs; | ||
| 172 | my (%states, %haps); | ||
| 173 | |||
| 174 | foreach my $site (@valid_sites) { | ||
| 175 | my $pop_marker = $pop->get_Marker($site); | ||
| 176 | my %freqs = $pop_marker->get_Allele_Frequencies; #print Dumper(\%freqs); next; | ||
| 177 | my @nts = sort keys %freqs; | ||
| 178 | $states{$site} = \@nts; | ||
| 179 | } | ||
| 180 | |||
| 181 | for (my $i = 0; $i < $#valid_sites; $i++) { | ||
| 182 | for (my $j = $i+1; $j <= $#valid_sites; $j++) { | ||
| 183 | foreach my $id ( keys %myseqs) { | ||
| 184 | my $hap = $myseqs{$id}->{$valid_sites[$i]} . $myseqs{$id}->{$valid_sites[$j]}; | ||
| 185 | $haps{$i . "-" . $j}->{$hap}++; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | for (my $i = 0; $i < $#valid_sites; $i++) { | ||
| 191 | my ($base_i_a, $base_i_b) = @{ $states{$valid_sites[$i]} }; | ||
| 192 | for (my $j = $i+1; $j <= $#valid_sites; $j++) { | ||
| 193 | my ($base_j_a, $base_j_b) = @{ $states{$valid_sites[$j]} }; | ||
| 194 | my $ct1 = $haps{$i . "-" . $j}->{$base_i_a . $base_j_a} || 0; | ||
| 195 | my $ct2 = $haps{$i . "-" . $j}->{$base_i_a . $base_j_b} || 0; | ||
| 196 | my $ct3 = $haps{$i . "-" . $j}->{$base_i_b . $base_j_a} || 0; | ||
| 197 | my $ct4 = $haps{$i . "-" . $j}->{$base_i_b . $base_j_b} || 0; | ||
| 198 | # my $comp = ($ct1 && $ct2 && $ct3 && $ct4) ? 0 : 1; | ||
| 199 | my $ct_zero = 0; | ||
| 200 | foreach ($ct1, $ct2, $ct3, $ct4) {$ct_zero++ if !$_} | ||
| 201 | print join "\t", ($i, | ||
| 202 | $j, | ||
| 203 | $valid_sites[$i], | ||
| 204 | $valid_sites[$j], | ||
| 205 | $ct1, $ct2, $ct3, $ct4, &_shanon_counts([ ($ct1, $ct2, $ct3, $ct4) ]), | ||
| 206 | $ct_zero == 0 ? 0 : 1, # competible if $ct_zero > 0 | ||
| 207 | $ct_zero == 2 ? 1 : 0); # epistatic if $ct_zero == 2 | ||
| 208 | print "\n"; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _two_allele_nogap_informative_sites { | ||
| 214 | my $mypop = shift; | ||
| 215 | my @valids; | ||
| 216 | my @sites = $mypop->get_marker_names(); | ||
| 217 | die "No polymorphic sites: $aln_file\n" if ! scalar @sites; | ||
| 218 | for my $site ( sort {$a <=> $b} @sites ) { | ||
| 219 | my $pop_marker = $mypop->get_Marker($site); | ||
| 220 | my @alleles = $pop_marker->get_Alleles(); | ||
| 221 | next if &_has_gap($site); # skip gapped sites | ||
| 222 | next if @alleles == 1; | ||
| 223 | if (scalar @alleles > 2) { warn $site, ": more than 2 alleles.", join (",", @alleles), "\n"; next } | ||
| 224 | next if &_not_informative($site); | ||
| 225 | push @valids, $site; | ||
| 226 | } | ||
| 227 | return @valids; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub _not_informative { | ||
| 231 | my $site = shift; | ||
| 232 | my %seen_allele; | ||
| 233 | foreach my $ind ($pop->get_Individuals) { | ||
| 234 | my @genotypes = $ind->get_Genotypes(-marker => $site); | ||
| 235 | my $id = $ind->unique_id(); | ||
| 236 | my $geno = shift @genotypes; | ||
| 237 | my ($allele) = $geno->get_Alleles(); | ||
| 238 | my @ids; | ||
| 239 | @ids = @{$seen_allele{$allele}} if $seen_allele{$allele}; | ||
| 240 | push @ids, $id; | ||
| 241 | $seen_allele{$allele} = \@ids; | ||
| 242 | } | ||
| 243 | my $not_informative = 0; | ||
| 244 | foreach my $allele (keys %seen_allele) { | ||
| 245 | my @ids = @{$seen_allele{$allele}}; | ||
| 246 | $not_informative = 1 if @ids == 1; | ||
| 247 | } | ||
| 248 | return $not_informative; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub print_diversity { | ||
| 252 | printf "%s\t%.4f\n", "Nucleotide diversity =>", $pop_stats->pi($pop) | ||
| 253 | } | ||
| 254 | |||
| 255 | sub bi_partition { | ||
| 256 | my @sites = $pop->get_marker_names(); | ||
| 257 | my %biparts; | ||
| 258 | for my $site ( sort {$a <=> $b} @sites ) { | ||
| 259 | my $pop_marker = $pop->get_Marker($site); | ||
| 260 | my @alleles = $pop_marker->get_Alleles(); | ||
| 261 | next if &_has_gap($site); # skip gapped sites | ||
| 262 | next if @alleles == 1; # skip constant sites | ||
| 263 | next if scalar @alleles > 2; # skip if more than two states | ||
| 264 | my %seen_allele; | ||
| 265 | foreach my $ind ($pop->get_Individuals) { | ||
| 266 | my @genotypes = $ind->get_Genotypes(-marker => $site); | ||
| 267 | my $id = $ind->unique_id(); | ||
| 268 | my $geno = shift @genotypes; | ||
| 269 | my ($allele) = $geno->get_Alleles(); | ||
| 270 | my @ids; | ||
| 271 | @ids = @{$seen_allele{$allele}} if $seen_allele{$allele}; | ||
| 272 | push @ids, $id; | ||
| 273 | $seen_allele{$allele} = \@ids; | ||
| 274 | } | ||
| 275 | my $not_informative = 0; | ||
| 276 | my $newick_string = "informative_site_" . $site . "_("; | ||
| 277 | foreach my $allele (keys %seen_allele) { | ||
| 278 | my @ids = @{$seen_allele{$allele}}; | ||
| 279 | $not_informative = 1 if @ids == 1; | ||
| 280 | $newick_string .= "("; | ||
| 281 | $newick_string .= join ",", @ids; | ||
| 282 | $newick_string .= ")"; | ||
| 283 | } | ||
| 284 | next if $not_informative; | ||
| 285 | $newick_string .= ");"; | ||
| 286 | $newick_string =~ s/\)\(/\),\(/; | ||
| 287 | print $newick_string, "\n"; | ||
| 288 | # print $site, "=>\t", Dumper(\%seen_allele); | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | sub bisites_for_r { | ||
| 293 | my @valid_sites = &_two_allele_nogap_sites($pop); | ||
| 294 | say STDERR "bi-allelic, non-gapped sites:\t", scalar @valid_sites, "\t", join ",", @valid_sites; | ||
| 295 | my $ref_seqs = &_base_at_snp_sites($pop, \@valid_sites); | ||
| 296 | my %myseqs = %$ref_seqs; | ||
| 297 | foreach my $id (keys %myseqs) { | ||
| 298 | print $id; | ||
| 299 | for my $site ( sort {$a <=> $b} @valid_sites ) { | ||
| 300 | print "\t", $myseqs{$id}->{$site} . $myseqs{$id}->{$site}; | ||
| 301 | } | ||
| 302 | print "\n"; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | sub bisites { | ||
| 307 | my @valid_sites = &_two_allele_nogap_sites($pop); | ||
| 308 | say STDERR "bi-allelic, non-gapped sites:\t", scalar @valid_sites, "\t", join ",", @valid_sites; | ||
| 309 | my $ref_seqs = &_base_at_snp_sites($pop, \@valid_sites); | ||
| 310 | my %myseqs = %$ref_seqs; | ||
| 311 | foreach my $id (keys %myseqs) { | ||
| 312 | print ">", $id, "\n"; | ||
| 313 | my $varseq = ""; | ||
| 314 | for my $site ( sort {$a <=> $b} @valid_sites ) { | ||
| 315 | $varseq .= $myseqs{$id}->{$site}; | ||
| 316 | } | ||
| 317 | print $varseq, "\n"; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | sub _base_at_snp_sites { | ||
| 322 | my $mypop = shift; | ||
| 323 | my $ref_site = shift; | ||
| 324 | my @mysites = @$ref_site; | ||
| 325 | my %bases; | ||
| 326 | foreach my $ind ($mypop->get_Individuals) { | ||
| 327 | my $id = $ind->unique_id(); | ||
| 328 | for my $site ( sort {$a <=> $b} @mysites ) { | ||
| 329 | my @genotypes = $ind->get_Genotypes(-marker => $site); | ||
| 330 | my $geno = shift @genotypes; | ||
| 331 | my ($allele) = $geno->get_Alleles(); | ||
| 332 | $bases{$id}->{$site} = $allele; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | return \%bases; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub snp_noncoding { | ||
| 339 | my @sites = $pop->get_marker_names(); | ||
| 340 | # warn "total variable sites: ", join ",", @sites, "\n\nTotal=>", scalar(@sites), "\n\n"; | ||
| 341 | if ( ! scalar @sites ) { | ||
| 342 | die "No polymorphic sites: $aln_file\n"; | ||
| 343 | } | ||
| 344 | |||
| 345 | for my $site ( sort {$a <=> $b} @sites ) { | ||
| 346 | my $pop_marker = $pop->get_Marker($site); | ||
| 347 | my @alleles = $pop_marker->get_Alleles(); #print $name, "=>\t", Dumper(\@alleles); next; | ||
| 348 | next if &_has_gap($site); # skip gapped sites | ||
| 349 | if (scalar @alleles > 2) { warn $site, ": more than 2 alleles.", join (",", @alleles), "\n"; next } # consider only 2-state polymorphic sites | ||
| 350 | my %freqs = $pop_marker->get_Allele_Frequencies; #print Dumper(\%freqs); next; | ||
| 351 | my @nts = sort { $freqs{$a} <= $freqs{$b} } keys %freqs; | ||
| 352 | my $shanon = &_shanon_index(\%freqs); | ||
| 353 | say join "\t", ($aln_file, $site, $nts[0], $freqs{$nts[0]}, $nts[1], $freqs{$nts[1]}, $shanon); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | sub _shanon_counts { | ||
| 358 | my $ref = shift; | ||
| 359 | my @cts = @$ref; | ||
| 360 | my $h = 0; | ||
| 361 | my $sum = 0; | ||
| 362 | foreach my $ct ( @cts) { | ||
| 363 | next unless $ct; | ||
| 364 | $sum += $ct; | ||
| 365 | } | ||
| 366 | foreach my $ct ( @cts) { | ||
| 367 | next unless $ct; | ||
| 368 | my $freq = $ct/$sum; | ||
| 369 | $h += -1 * $freq * log($freq)/log(2); | ||
| 370 | } | ||
| 371 | return sprintf "%.6f", $h; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub _shanon_index { | ||
| 375 | my $ref = shift; | ||
| 376 | my %f = %$ref; | ||
| 377 | my $h = 0; | ||
| 378 | foreach my $base (keys %f) { | ||
| 379 | my $freq = $f{$base}; | ||
| 380 | $h += -1 * $freq * log($freq)/log(2); | ||
| 381 | } | ||
| 382 | return sprintf "%.6f", $h; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub snp_coding { | ||
| 386 | my @sites = $pop_cds->get_marker_names(); | ||
| 387 | # warn "total variable sites: ", join ",", @sites, "\n\nTotal=>", scalar(@sites), "\n\n"; | ||
| 388 | if ( ! scalar @sites ) { | ||
| 389 | die "No polymorphic sites: $aln_file\n"; | ||
| 390 | } | ||
| 391 | |||
| 392 | for my $site ( sort {$a <=> $b} @sites ) { | ||
| 393 | my $pop_marker = $pop_cds->get_Marker($site); | ||
| 394 | my @alleles = $pop_marker->get_Alleles(); #print $name, "=>\t", Dumper(\@alleles); next; | ||
| 395 | next if &_has_gap_codon($site); # skip gapped sites | ||
| 396 | if (scalar @alleles > 2) { warn $site, ": more than 2 alleles.", join (",", @alleles), "\n"; next } # consider only 2-state polymorphic sites | ||
| 397 | my %freqs = $pop_marker->get_Allele_Frequencies; # print $out_aa, "=>", Dumper(\%freqs); next; | ||
| 398 | my ($minor, $major, $syn) = &_syn_nonsyn(\%freqs); | ||
| 399 | my $snp_site = 3 * $site + &_snp_position($minor->{codon}, $major->{codon}); | ||
| 400 | my $shanon = &_shanon_index(\%freqs); | ||
| 401 | say join "\t", ($aln_file, $site, $snp_site, $syn, $minor->{codon}, $minor->{aa}, $minor->{freq}, $major->{codon}, $major->{aa}, $major->{freq}, $shanon); | ||
| 402 | |||
| 403 | # foreach my $ind ($pop_cds->get_Individuals) { | ||
| 404 | # my @genotypes = $ind->get_Genotypes(-marker => $site); | ||
| 405 | # my $id = $ind->unique_id(); | ||
| 406 | # my $geno = shift @genotypes; | ||
| 407 | # my ($allele) = $geno->get_Alleles(); | ||
| 408 | # say join "\t", ($aln_file, $site, $id, $snp_site, $syn, $allele); | ||
| 409 | # } | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | sub snp_coding_long { | ||
| 415 | my @sites = $pop_cds->get_marker_names(); | ||
| 416 | # warn "total variable sites: ", join ",", @sites, "\n\nTotal=>", scalar(@sites), "\n\n"; | ||
| 417 | if ( ! scalar @sites ) { | ||
| 418 | die "No polymorphic sites: $aln_file\n"; | ||
| 419 | } | ||
| 420 | |||
| 421 | for my $site ( sort {$a <=> $b} @sites ) { | ||
| 422 | my $pop_marker = $pop_cds->get_Marker($site); | ||
| 423 | my @alleles = $pop_marker->get_Alleles(); #print $name, "=>\t", Dumper(\@alleles); next; | ||
| 424 | next if &_has_gap_codon($site); # skip gapped sites | ||
| 425 | if (scalar @alleles > 2) { warn $site, ": more than 2 alleles.", join (",", @alleles), "\n"; next } # consider only 2-state polymorphic sites | ||
| 426 | my %freqs = $pop_marker->get_Allele_Frequencies; # print $out_aa, "=>", Dumper(\%freqs); next; | ||
| 427 | my ($minor, $major, $syn) = &_syn_nonsyn(\%freqs); | ||
| 428 | my $snp_site = 3 * $site + &_snp_position($minor->{codon}, $major->{codon}); | ||
| 429 | |||
| 430 | foreach my $ind ($pop_cds->get_Individuals) { | ||
| 431 | my @genotypes = $ind->get_Genotypes(-marker => $site); | ||
| 432 | my $id = $ind->unique_id(); | ||
| 433 | my $geno = shift @genotypes; | ||
| 434 | my ($allele) = $geno->get_Alleles(); | ||
| 435 | say join "\t", ($aln_file, $site, $id, $snp_site, $syn, $allele); | ||
| 436 | } | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | =head2 write_out $opts | ||
| 441 | |||
| 442 | Does the bulk of calling other tree routines based on $opts set. | ||
| 443 | After L<Bio::Wrapper::PopManipulations::initialize> should be called first. | ||
| 444 | |||
| 445 | =cut | ||
| 446 | |||
| 447 | sub write_out | ||
| 448 | { | ||
| 449 | my $opts_ref = shift; | ||
| 450 | my %opts = %{$opts_ref}; | ||
| 451 | for my $option (keys %opts) { | ||
| 452 | # If there is a function to handle the current option, execute it | ||
| 453 | if (can_handle($option)) { handle_opt($option); exit } | ||
| 454 | else { warn "Missing handler for: $option\n" } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | #sub print_stats { | ||
| 459 | # @stats = _parse_stats(); | ||
| 460 | # my $len = $aln->length(); | ||
| 461 | |||
| 462 | # foreach my $stat (@stats) { | ||
| 463 | # $stat = lc($stat); | ||
| 464 | # given ($stat) { | ||
| 465 | # when (/^(pi)|(theta)$/) { printf "$stat:\t%.6f\n", $stat_obj->$stat($pop, $len) } | ||
| 466 | # when ("tajima_d") { printf "tajima_D:\t%.6f\n", $stat_obj->tajima_D($pop) } | ||
| 467 | # when ("mk") { _mk_counts() } | ||
| 468 | # } | ||
| 469 | # } | ||
| 470 | #} | ||
| 471 | |||
| 472 | sub print_num_snps { | ||
| 473 | print $pop_stats->segregating_sites_count($pop), "\n" | ||
| 474 | } | ||
| 475 | |||
| 476 | ####################### internal subroutine ########################### | ||
| 477 | |||
| 478 | sub _snp_position { | ||
| 479 | my ($cd1, $cd2) = @_; | ||
| 480 | return 1 if substr($cd1, 0, 1) ne substr($cd2, 0, 1); | ||
| 481 | return 2 if substr($cd1, 1, 1) ne substr($cd2, 1, 1); | ||
| 482 | return 3 if substr($cd1, 2, 1) ne substr($cd2, 2, 1); | ||
| 483 | } | ||
| 484 | |||
| 485 | sub _in_list { | ||
| 486 | my ($ref1, $ref2) = @_; | ||
| 487 | foreach my $i (%$ref1) { | ||
| 488 | foreach my $j (@$ref2) { | ||
| 489 | return 1 if $j eq $j; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | return 0; | ||
| 493 | } | ||
| 494 | |||
| 495 | sub _parse_stats { | ||
| 496 | return split(/,/, join(',', @{ $opts->{"stats"} })) | ||
| 497 | } | ||
| 498 | |||
| 499 | sub _best_sample_size { | ||
| 500 | my @group = @_; | ||
| 501 | # Checks if $sample_size was defined previously. | ||
| 502 | # If it was, make sure it does not exceed the size of the group | ||
| 503 | # If it was not, use the size of the group | ||
| 504 | return $sample_size ? ($sample_size > @group ? @group : $sample_size) : @group | ||
| 505 | } | ||
| 506 | |||
| 507 | |||
| 508 | sub _mk_counts { | ||
| 509 | die "Error: ingroup and outgroup options required when using MK test.\n" unless $ingroup && $outgroup; | ||
| 510 | |||
| 511 | my $in_group = Bio::PopGen::Population->new(); | ||
| 512 | my $out_group = Bio::PopGen::Population->new(); | ||
| 513 | my (@out, @in); | ||
| 514 | for my $ind ($pop->get_Individuals) { | ||
| 515 | push @in, $ind if $ind->unique_id =~ /^$ingroup/; | ||
| 516 | push @out, $ind if $ind->unique_id =~ /^$outgroup/ | ||
| 517 | } | ||
| 518 | |||
| 519 | my @in_shuffled = shuffle @in; | ||
| 520 | my @out_shuffled = shuffle @out; | ||
| 521 | my $size = _best_sample_size(@in_shuffled); # ingroup size | ||
| 522 | my @in_sample = sample(-set => \@in_shuffled, -sample_size => $size); | ||
| 523 | $size = _best_sample_size(@out_shuffled); # outgroup size | ||
| 524 | |||
| 525 | my @out_sample = sample(-set => \@out_shuffled, -sample_size => $size); | ||
| 526 | |||
| 527 | $in_group->add_Individual(@in_sample); | ||
| 528 | $out_group->add_Individual(@out_sample); | ||
| 529 | |||
| 530 | my $mk1 = $stat_obj->mcdonald_kreitman($in_group, $out_group); | ||
| 531 | |||
| 532 | # my $mk2 = $stat_obj->mcdonald_kreitman($out_group, $in_group); | ||
| 533 | say join "\t", ($mk1->{poly_N}, $mk1->{fixed_N}, $mk1->{poly_S}, $mk1->{fixed_S}) | ||
| 534 | } | ||
| 535 | |||
| 536 | sub _has_gap_codon { # internal methods | ||
| 537 | my $pos = shift @_; | ||
| 538 | foreach my $seq ($aln->each_seq) { | ||
| 539 | return 1 if $seq->subseq(3*$pos+1, 3*$pos+3) =~ /-/; | ||
| 540 | } | ||
| 541 | return 0; | ||
| 542 | } | ||
| 543 | |||
| 544 | sub _has_gap { # internal methods | ||
| 545 | my $pos = shift @_; | ||
| 546 | foreach my $seq ($aln->each_seq) { | ||
| 547 | return 1 if $seq->subseq($pos, $pos) eq '-'; | ||
| 548 | } | ||
| 549 | return 0; | ||
| 550 | } | ||
| 551 | |||
| 552 | sub _syn_nonsyn { | ||
| 553 | my $ref = shift; | ||
| 554 | my %fr = %$ref; | ||
| 555 | |||
| 556 | my ($codon1, $codon2) = sort { $fr{$a} <=> $fr{$b} } keys %fr; | ||
| 557 | my ($aa1, $aa2) = ($myCodonTable->translate(uc($codon1)), $myCodonTable->translate(uc($codon2)) ); | ||
| 558 | |||
| 559 | my $minor = { | ||
| 560 | 'codon' => uc($codon1), | ||
| 561 | 'aa' => $aa1, | ||
| 562 | 'freq' => sprintf "%.4f", $fr{$codon1} | ||
| 563 | }; | ||
| 564 | |||
| 565 | my $major = { | ||
| 566 | 'codon' => uc($codon2), | ||
| 567 | 'aa' => $aa2, | ||
| 568 | 'freq' => sprintf "%.4f", $fr{$codon2} | ||
| 569 | }; | ||
| 570 | my $syn = $aa1 eq $aa2 ? 1 : 0; | ||
| 571 | return ($minor, $major, $syn); | ||
| 572 | } | ||
| 573 | |||
| 574 | |||
| 575 | =begin | ||
| 576 | local $@; | ||
| 577 | my $results = eval { $dna_stats->$call(@arg_list) }; | ||
| 578 | die "Encountered $@\n" if $@; | ||
| 579 | for my $an (@$results) { | ||
| 580 | say "comparing " . $an->{'Seq1'} . " and " . $an->{'Seq2'} | ||
| 581 | unless $calc_type eq "average"; | ||
| 582 | for (sort keys %$an) { | ||
| 583 | next if /Seq/; | ||
| 584 | printf("%-9s %.4f \n", $_, $an->{$_}) | ||
| 585 | } | ||
| 586 | say "\n" | ||
| 587 | } | ||
| 588 | =cut | ||
| 589 | |||
| 590 | |||
| 591 | 1; |