def mktest(codon_alns, codon_table=default_codon_table, alpha=0.05): """McDonald-Kreitman test for neutrality (PMID: 1904993) This method counts changes rather than sites (http://mkt.uab.es/mkt/help_mkt.asp). Arguments: - codon_alns - list of CodonAlignment to compare (each CodonAlignment object corresponds to gene sampled from a species) Return the p-value of test result """ import copy if not all(isinstance(i, CodonAlignment) for i in codon_alns): raise TypeError("mktest accepts CodonAlignment list.") codon_aln_len = [i.get_alignment_length() for i in codon_alns] if len(set(codon_aln_len)) != 1: raise RuntimeError("CodonAlignment object for mktest should be of" " equal length.") codon_num = codon_aln_len[0] // 3 # prepare codon_dict (taking stop codon as an extra amino acid) codon_dict = copy.deepcopy(codon_table.forward_table) for stop in codon_table.stop_codons: codon_dict[stop] = 'stop' # prepare codon_lst codon_lst = [] for codon_aln in codon_alns: codon_lst.append([]) for i in codon_aln: codon_lst[-1].append(_get_codon_list(i.seq)) codon_set = [] for i in range(codon_num): uniq_codons = [] for j in codon_lst: uniq_codon = set(k[i] for k in j) uniq_codons.append(uniq_codon) codon_set.append(uniq_codons) syn_fix, nonsyn_fix, syn_poly, nonsyn_poly = 0, 0, 0, 0 G, nonsyn_G = _get_codon2codon_matrix(codon_table=codon_table) for i in codon_set: all_codon = i[0].union(*i[1:]) if '-' in all_codon or len(all_codon) == 1: continue fix_or_not = all(len(k) == 1 for k in i) if fix_or_not: # fixed nonsyn_subgraph = _get_subgraph(all_codon, nonsyn_G) subgraph = _get_subgraph(all_codon, G) this_non = _count_replacement(all_codon, nonsyn_subgraph) this_syn = _count_replacement(all_codon, subgraph) - this_non nonsyn_fix += this_non syn_fix += this_syn else: # not fixed nonsyn_subgraph = _get_subgraph(all_codon, nonsyn_G) subgraph = _get_subgraph(all_codon, G) this_non = _count_replacement(all_codon, nonsyn_subgraph) this_syn = _count_replacement(all_codon, subgraph) - this_non nonsyn_poly += this_non syn_poly += this_syn return _G_test([syn_fix, nonsyn_fix, syn_poly, nonsyn_poly])
def mktest(codon_alns, codon_table=default_codon_table, alpha=0.05): """McDonald-Kreitman test for neutrality (PMID: 1904993) This method counts changes rather than sites (http://mkt.uab.es/mkt/help_mkt.asp). Arguments: - codon_alns - list of CodonAlignment to compare (each CodonAlignment object corresponds to gene sampled from a species) Return the p-value of test result """ import copy if not all([isinstance(i, CodonAlignment) for i in codon_alns]): raise TypeError("mktest accepts CodonAlignment list.") codon_aln_len = [i.get_alignment_length() for i in codon_alns] if len(set(codon_aln_len)) != 1: raise RuntimeError("CodonAlignment object for mktest should be of" " equal length.") codon_num = codon_aln_len[0] // 3 # prepare codon_dict (taking stop codon as an extra amino acid) codon_dict = copy.deepcopy(codon_table.forward_table) for stop in codon_table.stop_codons: codon_dict[stop] = 'stop' # prepare codon_lst codon_lst = [] for codon_aln in codon_alns: codon_lst.append([]) for i in codon_aln: codon_lst[-1].append(_get_codon_list(i.seq)) codon_set = [] for i in range(codon_num): uniq_codons = [] for j in codon_lst: uniq_codon = set([k[i] for k in j]) uniq_codons.append(uniq_codon) codon_set.append(uniq_codons) syn_fix, nonsyn_fix, syn_poly, nonsyn_poly = 0, 0, 0, 0 G, nonsyn_G = _get_codon2codon_matrix(codon_table=codon_table) for i in codon_set: all_codon = i[0].union(*i[1:]) if '-' in all_codon or len(all_codon) == 1: continue fix_or_not = all([len(k) == 1 for k in i]) if fix_or_not: # fixed nonsyn_subgraph = _get_subgraph(all_codon, nonsyn_G) subgraph = _get_subgraph(all_codon, G) this_non = _count_replacement(all_codon, nonsyn_subgraph) this_syn = _count_replacement(all_codon, subgraph) - this_non nonsyn_fix += this_non syn_fix += this_syn else: # not fixed nonsyn_subgraph = _get_subgraph(all_codon, nonsyn_G) subgraph = _get_subgraph(all_codon, G) this_non = _count_replacement(all_codon, nonsyn_subgraph) this_syn = _count_replacement(all_codon, subgraph) - this_non nonsyn_poly += this_non syn_poly += this_syn return _G_test([syn_fix, nonsyn_fix, syn_poly, nonsyn_poly])