def eval_proposal(self, proposal): """Compute probability of proposal""" #util.tic("eval") # compute recon probability info = {} # DEBUG counts = coal.count_lineages_per_branch(self.coal_tree, proposal.coal_recon, proposal.locus_tree) maxcount = max(x[0] for x in counts.values()) #util.logger("max lineage count %d" % maxcount) if maxcount > 10: return -util.INF p = dlcoal.prob_dlcoal_recon_topology(self.coal_tree, proposal.coal_recon, proposal.locus_tree, proposal.locus_recon, proposal.locus_events, proposal.daughters, self.stree, self.n, self.duprate, self.lossrate, self.pretime, self.premean, nsamples=self.nsamples, add_spec=False, info=info) proposal.data = info #util.toc() return p
def count_dup_loss_coal_tree(coal_tree, extra, stree, gene2species, implied=True, locus_mpr=True): """count dup loss coal""" if not locus_mpr: raise Exception("not implemented") # TODO: use locus_recon and locus_events rather than MPR # (currently, phylo.py reconciliation functions fail for non-MPR) locus_tree = extra["locus_tree"] locus_recon = phylo.reconcile(locus_tree, stree, gene2species) locus_events = phylo.label_events(locus_tree, locus_recon) coal_recon = extra["coal_recon"] ndup, nloss, nappear = phylo.count_dup_loss_tree(locus_tree, stree, gene2species, locus_recon, locus_events) # add implied speciation nodes if desired # this must be added AFTER counting dups and losses since it affects loss inference if implied: added = phylo.add_implied_spec_nodes(locus_tree, stree, locus_recon, locus_events) # count coals ncoal = 0 counts = coal.count_lineages_per_branch(coal_tree, coal_recon, locus_tree) for lnode, (count_bot, count_top) in counts.iteritems(): n = max(count_top-1, 0) locus_recon[lnode].data['coal'] += n ncoal += n if implied: phylo.remove_implied_spec_nodes(locus_tree, added, locus_recon, locus_events) return ndup, nloss, ncoal, nappear
def assert_bounded_coal_lineages(coal_tree, coal_recon, locus_tree, daughters): """Ensure bounded coalescent for daughter lineages (using lineage counts).""" lineages = coal.count_lineages_per_branch(coal_tree, coal_recon, locus_tree) for dnode in daughters: if lineages[dnode][1] != 1: return False return True
def propose_daughters(coal_tree, coal_recon, locus_tree, locus_events): lineages = coal.count_lineages_per_branch(coal_tree, coal_recon, locus_tree) daughters = set() for node, event in locus_events.iteritems(): if event == "dup": # choose one of the children of node to be a daughter children = [child for child in node.children if lineages[child][1] == 1] if len(children) > 0: daughters.add(children[stats.sample([1] * len(children))]) return daughters
def propose_daughters(coal_tree, coal_recon, locus_tree, locus_events): lineages = coal.count_lineages_per_branch(coal_tree, coal_recon, locus_tree) daughters = set() for node, event in locus_events.iteritems(): if event == "dup": # choose one of the children of node to be a daughter children = [ child for child in node.children if lineages[child][1] == 1 ] if len(children) > 0: daughters.add(children[stats.sample([1] * len(children))]) return daughters
def count_dup_loss_coal_tree(coal_tree, extra, stree, gene2species, implied=True, locus_mpr=True): """count dup loss coal""" if not locus_mpr: raise Exception("not implemented") # TODO: use locus_recon and locus_events rather than MPR # (currently, phylo.py reconciliation functions fail for non-MPR) locus_tree = extra["locus_tree"] locus_recon = phylo.reconcile(locus_tree, stree, gene2species) locus_events = phylo.label_events(locus_tree, locus_recon) coal_recon = extra["coal_recon"] ndup, nloss, nappear = phylo.count_dup_loss_tree(locus_tree, stree, gene2species, locus_recon, locus_events) # add implied speciation nodes if desired # this must be added AFTER counting dups and losses since it affects loss inference if implied: added = phylo.add_implied_spec_nodes(locus_tree, stree, locus_recon, locus_events) # count coals ncoal = 0 counts = coal.count_lineages_per_branch(coal_tree, coal_recon, locus_tree) for lnode, (count_bot, count_top) in counts.iteritems(): n = max(count_top - 1, 0) locus_recon[lnode].data['coal'] += n ncoal += n if implied: phylo.remove_implied_spec_nodes(locus_tree, added, locus_recon, locus_events) return ndup, nloss, ncoal, nappear
def count_coal(tree, recon, stree): """Count the number of (deep) coalescences in a gene tree""" counts = coal.count_lineages_per_branch(tree, recon, stree) ncoal = sum(max(x[1] - 1, 0) for x in counts.itervalues()) return ncoal
draw_tree_names(ex["locus_tree"], scale=.5e-7, minlen=8) print exp(dlcoal.prob_multicoal_recon_topology( coal_tree, ex["coal_recon"], ex["locus_tree"], n, ex["daughters"])) print exp(dlcoal.prob_multicoal_recon_topology2( coal_tree, ex["coal_recon"], ex["locus_tree"], n, ex["daughters"])) if 0: draw_tree_names(coal_tree, scale=1e-7) print exp(coal.prob_multicoal_recon_topology( coal_tree, ex["coal_recon"], ex["locus_tree"], n)) print exp(coal.prob_multicoal_recon_topology_old( coal_tree, ex["coal_recon"], ex["locus_tree"], n)) if 0: pd(coal.count_lineages_per_branch( coal_tree, ex["coal_recon"], ex["locus_tree"])) #============================================================================= show_plots = False def show_plot(): if show_plots: raw_input() if __name__ == "__main__": if "--" in sys.argv:
def count_coal(tree, recon, stree): """Count the number of (deep) coalescences in a gene tree""" counts = coal.count_lineages_per_branch(tree, recon, stree) ncoal = sum(max(x[1]-1, 0) for x in counts.itervalues()) return ncoal