Example #1
0
    def test_est_popsize(self):
        """
        Fully sample an ARG from stratch using API
        """

        k = 50
        rho = 1.5e-8
        mu = 2.5e-8
        length = int(1e6)
        times = arghmm.get_time_points(ntimes=30, maxtime=200000)
        popsize = 1e4
        refine = 0

        util.tic("sim ARG")
        arg = arghmm.sample_arg_dsmc(k, 2 * popsize,
                                     rho, start=0, end=length, times=times)
        #arg = arglib.sample_arg_smc(k, 2 * popsize,
        #                            rho, start=0, end=length)
        #arg = arglib.sample_arg(k, 2 * popsize, rho, start=0, end=length)
        util.toc()

        x = []
        for tree in arglib.iter_marginal_trees(arg):
            arglib.remove_single_lineages(tree)
            x.append(mle_popsize_tree(tree, mintime=0))
        
        p = plot(x, ymin=0)
        p.plot([0, len(x)], [popsize, popsize], style='lines')
        
        pause()
Example #2
0
    def test_est_popsize(self):
        """
        Fully sample an ARG from stratch using API
        """

        k = 50
        rho = 1.5e-8
        mu = 2.5e-8
        length = int(1e6)
        times = arghmm.get_time_points(ntimes=30, maxtime=200000)
        popsize = 1e4
        refine = 0

        util.tic("sim ARG")
        arg = arghmm.sample_arg_dsmc(k, 2 * popsize,
                                     rho, start=0, end=length, times=times)
        #arg = arglib.sample_arg_smc(k, 2 * popsize,
        #                            rho, start=0, end=length)
        #arg = arglib.sample_arg(k, 2 * popsize, rho, start=0, end=length)
        util.toc()

        x = []
        for tree in arglib.iter_marginal_trees(arg):
            arglib.remove_single_lineages(tree)
            x.append(mle_popsize_tree(tree, mintime=0))
        
        p = plot(x, ymin=0)
        p.plot([0, len(x)], [popsize, popsize], style='lines')
        
        pause()
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
Example #4
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
Example #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 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
Example #6
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