def show_marginal_trees(arg, mut=None):

    win = summon.Window()
    x = 0
    step = 2
    treewidth = len(list(arg.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))

    blocks = arglib.iter_recomb_blocks(arg)

    for tree, block in izip(arglib.iter_marginal_trees(arg), blocks):
        pos = block[0]
        print pos

        leaves = sorted((x for x in tree.leaves()), key=lambda x: x.name)
        layout = layout_arg(tree, leaves)
        win.add_group(
            translate(x, 0, color(1,1,1),
                      draw_tree(tree, layout),
                      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")))

        # mark responsible recomb node
        for node in tree:
            if pos != 0.0 and node.pos == pos:
                nx, ny = layout[node]
                win.add_group(draw_mark(x + nx, ny))

        # draw mut
        if mut:
            for node, parent, mpos, t in mut:
                if (node.name in tree and node.name != tree.root.name and
                    block[0] < mpos < block[1]):
                    nx, ny = layout[tree[node.name]]
                    win.add_group(draw_mark(x + nx, t, col=(0,0,1)))
                if node.name in tree and tree[node.name].parents:
                    nx, ny = layout[tree[node.name]]
                    py = layout[tree[node.name].parents[0]][1]
                    start = arg[node.name].data["ancestral"][0][0]
                    win.add_group(lines(color(0,1,0),
                                        x+nx, ny, x+nx, py,
                                        color(1,1,1)))


        x += treewidth

    win.set_visible(* win.get_root().get_bounding() + ("exact",))

    return win
Esempio n. 2
0
def show_marginal_trees(arg, mut=None):

    win = summon.Window()
    x = 0
    step = 2
    treewidth = len(list(arg.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))

    blocks = arglib.iter_recomb_blocks(arg)

    for tree, block in zip(arglib.iter_marginal_trees(arg), blocks):
        pos = block[0]
        print(pos)

        leaves = sorted((x for x in tree.leaves()), key=lambda x: x.name)
        layout = layout_arg(tree, leaves)
        win.add_group(
            translate(x, 0, color(1,1,1),
                      draw_tree(tree, layout),
                      text_clip(
                    "%d-%d" % (block[0], block[1]),
                    treewidth*.05, 0,
                    treewidth*.95, -max(l[1] for l in list(layout.values())),
                    4, 20,
                    "center", "top")))

        # mark responsible recomb node
        for node in tree:
            if pos != 0.0 and node.pos == pos:
                nx, ny = layout[node]
                win.add_group(draw_mark(x + nx, ny))

        # draw mut
        if mut:
            for node, parent, mpos, t in mut:
                if (node.name in tree and node.name != tree.root.name and
                    block[0] < mpos < block[1]):
                    nx, ny = layout[tree[node.name]]
                    win.add_group(draw_mark(x + nx, t, col=(0,0,1)))
                if node.name in tree and tree[node.name].parents:
                    nx, ny = layout[tree[node.name]]
                    py = layout[tree[node.name].parents[0]][1]
                    start = arg[node.name].data["ancestral"][0][0]
                    win.add_group(lines(color(0,1,0),
                                        x+nx, ny, x+nx, py,
                                        color(1,1,1)))


        x += treewidth

    win.set_visible(* win.get_root().get_bounding() + ("exact",))

    return win
Esempio n. 3
0
    def test_local_trees(self):

        rho = 1.5e-8   # recomb/site/gen
        l = 10000      # length of locus
        k = 10         # number of lineages
        n = 2*1e4      # effective popsize

        arg = arglib.sample_arg(k, n, rho, 0, l)
        blocks1 = util.cget(arglib.iter_local_trees(arg, 200, 1200), 0)
        blocks2 = list(arglib.iter_recomb_blocks(arg, 200, 1200))
        self.assertEqual(blocks1, blocks2)
Esempio n. 4
0
def get_mutation_split_tracks(arg, mut_splits, mut_pos):

    mut_splits_set = set(mut_splits)
    mut_split_tracks = defaultdict(lambda: [])

    # find regions for splits
    i = 0
    for block, tree in zip(arglib.iter_recomb_blocks(arg),
                            arglib.iter_marginal_trees(arg)):
        for node in tree:
            if len(node.children) != 2 or node.children[0] == node.children[1]:
                continue
            split = tuple(sorted(tree.leaf_names(node)))
            if split in mut_splits_set:
                regions = mut_split_tracks[split]
                if len(regions) > 0 and regions[-1][1] == block[0]:
                    # extend region
                    regions[-1] = (regions[-1][0], block[1])
                else:
                    # add new region
                    regions.append(block)

    # keep only tracks who have a mutation in their interval
    mut_tracks = []
    for i in range(len(mut_pos)):
        for region in mut_split_tracks[mut_splits[i]]:
            if region[0] < mut_pos[i] < region[1]:
                a = util.binsearch(mut_pos, region[0])[0]
                a = a if a is not None else -.5
                b = util.binsearch(mut_pos, region[1])[1]
                b = b if b is not None else len(mut_pos)-.5
                mut_tracks.append((a, b))
                break
        else:
            assert False, i

    return mut_tracks
Esempio n. 5
0
def get_mutation_split_tracks(arg, mut_splits, mut_pos):

    mut_splits_set = set(mut_splits)
    mut_split_tracks = defaultdict(lambda: [])

    # find regions for splits
    i = 0
    for block, tree in izip(arglib.iter_recomb_blocks(arg), arglib.iter_marginal_trees(arg)):
        for node in tree:
            if len(node.children) != 2 or node.children[0] == node.children[1]:
                continue
            split = tuple(sorted(tree.leaf_names(node)))
            if split in mut_splits_set:
                regions = mut_split_tracks[split]
                if len(regions) > 0 and regions[-1][1] == block[0]:
                    # extend region
                    regions[-1] = (regions[-1][0], block[1])
                else:
                    # add new region
                    regions.append(block)

    # keep only tracks who have a mutation in their interval
    mut_tracks = []
    for i in xrange(len(mut_pos)):
        for region in mut_split_tracks[mut_splits[i]]:
            if region[0] < mut_pos[i] < region[1]:
                a = util.binsearch(mut_pos, region[0])[0]
                a = a if a is not None else -0.5
                b = util.binsearch(mut_pos, region[1])[1]
                b = b if b is not None else len(mut_pos) - 0.5
                mut_tracks.append((a, b))
                break
        else:
            assert False, i

    return mut_tracks