def _test_prog_infsites(): make_clean_dir("test/tmp/test_prog_infsites") run_cmd("""bin/arg-sim \ -k 40 -L 200000 \ -N 1e4 -r 1.5e-8 -m 2.5e-8 --infsites \ --ntimes 20 --maxtime 400e3 \ -o test/tmp/test_prog_infsites/0""") make_clean_dir("test/tmp/test_prog_infsites/0.sample") run_cmd("""bin/arg-sample \ -s test/tmp/test_prog_infsites/0.sites \ -N 1e4 -r 1.5e-8 -m 2.5e-8 \ --ntimes 5 --maxtime 100e3 -c 1 \ --climb 0 -n 20 --infsites \ -x 1 \ -o test/tmp/test_prog_infsites/0.sample/out""") arg = argweaver.read_arg( "test/tmp/test_prog_infsites/0.sample/out.0.smc.gz") sites = argweaver.read_sites("test/tmp/test_prog_infsites/0.sites") print "names", sites.names print noncompats = [] for block, tree in arglib.iter_local_trees(arg): tree = tree.get_tree() treelib.remove_single_children(tree) phylo.hash_order_tree(tree) for pos, col in sites.iter_region(block[0] + 1, block[1] + 1): assert block[0] + 1 <= pos <= block[1] + 1, (block, pos) split = sites_split(sites.names, col) node = arglib.split_to_tree_branch(tree, split) if node is None: noncompats.append(pos) print "noncompat", block, pos, col print phylo.hash_tree(tree) print tree.leaf_names() print "".join(col[sites.names.index(name)] for name in tree.leaf_names()) print split print print "num noncompats", len(noncompats)
def _test_prog_infsites(): make_clean_dir("test/data/test_prog_infsites") run_cmd("""bin/arg-sim \ -k 40 -L 200000 \ -N 1e4 -r 1.5e-8 -m 2.5e-8 --infsites \ --ntimes 20 --maxtime 400e3 \ -o test/data/test_prog_infsites/0""") make_clean_dir("test/data/test_prog_infsites/0.sample") run_cmd("""bin/arg-sample \ -s test/data/test_prog_infsites/0.sites \ -N 1e4 -r 1.5e-8 -m 2.5e-8 \ --ntimes 5 --maxtime 100e3 -c 1 \ --climb 0 -n 20 --infsites \ -x 1 \ -o test/data/test_prog_infsites/0.sample/out""") arg = argweaver.read_arg( "test/data/test_prog_infsites/0.sample/out.0.smc.gz") sites = argweaver.read_sites("test/data/test_prog_infsites/0.sites") print "names", sites.names print noncompats = [] for block, tree in arglib.iter_local_trees(arg): tree = tree.get_tree() treelib.remove_single_children(tree) phylo.hash_order_tree(tree) for pos, col in sites.iter_region(block[0]+1, block[1]+1): assert block[0]+1 <= pos <= block[1]+1, (block, pos) split = sites_split(sites.names, col) node = arglib.split_to_tree_branch(tree, split) if node is None: noncompats.append(pos) print "noncompat", block, pos, col print phylo.hash_tree(tree) print tree.leaf_names() print "".join(col[sites.names.index(name)] for name in tree.leaf_names()) print split print print "num noncompats", len(noncompats)
def show_tree_track(tree_track, mut=None, show_labels=False, use_blocks=False, branch_click=None): """ tree_track = [((start, end), tree), ...] """ def draw_labels(tree, layout): return group(* [text_clip(leaf.name, layout[leaf][0], layout[leaf][1], 1, layout[leaf][1] + 1e4, 4, 20, "middle", "left") for leaf in tree.leaves()]) def branch_hotspot(node, parent, x, y, y2): def func(): branch_click(node, parent) return hotspot("click", x-.5, y, x+.5, y2, func) def print_branch(node, parent): print "node", node.name tree_track = iter(tree_track) if mut: mut = util.PushIter(mut) block, tree = tree_track.next() if branch_click is True: branch_click = print_branch win = summon.Window() treex = 0 step = 2 treewidth = len(list(tree.leaves())) + step def trans_camera(win, x, y): v = win.get_visible() win.set_visible(v[0]+x, v[1]+y, v[2]+x, v[3]+y, "exact") win.set_binding(input_key("]"), lambda : trans_camera(win, treewidth, 0)) win.set_binding(input_key("["), lambda : trans_camera(win, -treewidth, 0)) for block, tree in chain([(block, tree)], tree_track): pos = block[0] print pos layout = treelib.layout_tree(tree, xscale=1, yscale=1) treelib.layout_tree_vertical(layout, leaves=0) g = win.add_group( translate(treex, 0, color(1,1,1), sumtree.draw_tree(tree, layout, vertical=True), (draw_labels(tree, layout) if show_labels else group()), text_clip( "%d-%d" % (block[0], block[1]), treewidth*.05, 0, treewidth*.95, -max(l[1] for l in layout.values()), 4, 20, "center", "top"))) clicking = group() g.append(clicking) # hotspots if branch_click: for node in tree: if node.parent: x, y = layout[node] x2, y2 = layout[node.parent] clicking.append(branch_hotspot(node, node.parent, x, y, y2)) #win.add_group(clicking) # draw mut if mut: for mpos, age, chroms in mut: if block[0] < mpos < block[1]: node = arglib.split_to_tree_branch(tree, chroms) parent = node.parent if node and parent: t = random.uniform(layout[node][1], layout[parent][1]) nx, ny = layout[node] win.add_group(draw_mark(treex + nx, t, col=(0,0,1))) elif mpos > block[1]: mut.push((mpos, age, chroms)) break treex += treewidth #win.set_visible(* win.get_root().get_bounding() + ("exact",)) win.home("exact") return win
def show_tree_track(tree_track, mut=None, show_labels=False, use_blocks=False, branch_click=None): """ tree_track = [((start, end), tree), ...] """ def draw_labels(tree, layout): return group(*[ text_clip(leaf.name, layout[leaf][0], layout[leaf][1], 1, layout[leaf][1] + 1e4, 4, 20, "middle", "left") for leaf in tree.leaves() ]) def branch_hotspot(node, parent, x, y, y2): def func(): branch_click(node, parent) return hotspot("click", x - .5, y, x + .5, y2, func) def print_branch(node, parent): print "node", node.name tree_track = iter(tree_track) if mut: mut = util.PushIter(mut) block, tree = tree_track.next() if branch_click is True: branch_click = print_branch win = summon.Window() treex = 0 step = 2 treewidth = len(list(tree.leaves())) + step def trans_camera(win, x, y): v = win.get_visible() win.set_visible(v[0] + x, v[1] + y, v[2] + x, v[3] + y, "exact") win.set_binding(input_key("]"), lambda: trans_camera(win, treewidth, 0)) win.set_binding(input_key("["), lambda: trans_camera(win, -treewidth, 0)) for block, tree in chain([(block, tree)], tree_track): pos = block[0] print pos layout = treelib.layout_tree(tree, xscale=1, yscale=1) treelib.layout_tree_vertical(layout, leaves=0) g = win.add_group( translate( treex, 0, color(1, 1, 1), sumtree.draw_tree(tree, layout, vertical=True), (draw_labels(tree, layout) if show_labels else group()), text_clip("%d-%d" % (block[0], block[1]), treewidth * .05, 0, treewidth * .95, -max(l[1] for l in layout.values()), 4, 20, "center", "top"))) clicking = group() g.append(clicking) # hotspots if branch_click: for node in tree: if node.parent: x, y = layout[node] x2, y2 = layout[node.parent] clicking.append(branch_hotspot(node, node.parent, x, y, y2)) #win.add_group(clicking) # draw mut if mut: for mpos, age, chroms in mut: if block[0] < mpos < block[1]: node = arglib.split_to_tree_branch(tree, chroms) parent = node.parent if node and parent: t = random.uniform(layout[node][1], layout[parent][1]) nx, ny = layout[node] win.add_group(draw_mark(treex + nx, t, col=(0, 0, 1))) elif mpos > block[1]: mut.push((mpos, age, chroms)) break treex += treewidth #win.set_visible(* win.get_root().get_bounding() + ("exact",)) win.home("exact") return win