def add_hap_markers(self, hap_var): """ Populates the table with bases that are associated with haplogroups. """ for hap in hap_var: self.markers[hap] = dict() for var in hap_var[hap]: pos = phylotree.pos_from_var(var) der = phylotree.der_allele(var) if der != self.refseq[pos]: self.markers[hap][pos] = der return
def test_der_allele(self): self.assertEqual(phylotree.der_allele("T14034C"), 'C') self.assertEqual(phylotree.der_allele("T182C!"), 'C') self.assertEqual(phylotree.der_allele("(T195C)"), 'C') self.assertEqual(phylotree.der_allele("(T195C!)"), 'C') self.assertEqual(phylotree.der_allele("C182T!!"), 'T') self.assertEqual(phylotree.der_allele("T10454c"), 'C')
def _check_contrib_phy_vars(phylo, obs_tab, contrib_prop, args): """ Checks if each candidate contributor from contribs passes our variant base check. The strategy for this is to start with the highest estimated contributors and an empty list of variant positions. For each contributor, we identify the variant bases that are unique from the previous candidates. We check the observation table to verify that those bases are observed in the sample. Args: phylo: the phylotree object that holds the variant information for these haplogroups obs_tab: Table of base observations for positions in the reference. contrib_prop: A list of lists, one for each contributor, containing: - The haplogroup ID for the contributor. - The proprotion estimate from EM args: argparse Namespace with user specified values for: min_var_reads: The minimum number of observations required to call a base as present in the mixture sample (int) frac_var_reads: The minimum fraction of observations required to call a base as present in the mixture sample (float) var_fraction: The minimum fraction of defining variants required to be observed to call a haplogroup a contributor (float) var_count: Call a haplogroup a contributor if there the number of observed variants is equal or greater than min_count (int) Returns: contrib_prop, with haplogroups that do not pass filters removed. """ used_vars = set() ignore_haps = set() if args.verbose: sys.stderr.write("Checking diagnostic variants:\n") for hap, _ in contrib_prop: # get variant for this haplogroup uniq_vars = set([(phylotree.pos_from_var(var), phylotree.der_allele(var)) for var in phylo.hap_var[hap]]) uniq_vars -= used_vars if args.verbose: sys.stderr.write("%s (%d unique variants)\n" % (hap, len(uniq_vars))) found_vars = set() for pos, der in sorted(uniq_vars): if args.verbose: var = "%d%s" % (pos + 1, der) sys.stderr.write(" %s: %d/%d\n" % (var.rjust(6), obs_tab.obs_at( pos, der), obs_tab.total_obs(pos))) threshold = max(args.min_var_reads, obs_tab.total_obs(pos) * args.frac_var_reads) if obs_tab.obs_at(pos, der) >= threshold: found_vars.add((pos, der)) if ((len(uniq_vars) == 0) or (args.var_count is not None and len(found_vars) >= args.var_count) or (float(len(found_vars)) / len(uniq_vars) >= args.var_fraction)): if args.verbose: sys.stderr.write( "Keeping '%s': " "%d/%d unique variant bases observed at " "least %d times.\n" % (hap, len(found_vars), len(uniq_vars), threshold)) # Looks good, these variants can't be used again. used_vars.update(found_vars) # Also add the ancestral bases for this haplogroup so we do not # mistake backmutations in another haplogroup as a novel allele. used_vars.update(phylo.get_ancestral(hap)) else: if args.verbose: sys.stderr.write( "Ignoring '%s': " "only %d/%d unique variant bases observed.\n" % (hap, len(found_vars), len(uniq_vars))) ignore_haps.add(hap) pass_contribs = [con for con in contrib_prop if con[0] not in ignore_haps] return pass_contribs