Exemplo n.º 1
0
def show_arg(arg, layout=None, leaves=None, mut=None, recomb_width=.4,
             win=None):
    """Visualize an ARG"""

    if win is None:
        win = summon.Window()
    else:
        win.clear_groups()

    # ensure layout
    if layout is None:
        layout = layout_arg(arg, leaves)

    # callbacks
    def branch_click(node, parent):
        print(node.name, parent.name)

    # draw ARG
    win.add_group(draw_arg(arg, layout, recomb_width=recomb_width,
                           branch_click=branch_click))

    # draw mutations
    if mut:
        g = group()
        for node, parent, pos, t in mut:
            x1, y1, x2, y2 = get_branch_layout(layout, node, parent)
            g.append(group(draw_mark(x1, t, col=(0,0,1)), color(1,1,1)))
        win.add_group(g)
    return win
Exemplo 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
Exemplo n.º 3
0
    def show(self, winsize=(600, 600), winpos=None):
        self.win = summon.Window("dotplot", size=winsize, position=winpos)
        self.win.set_bgcolor(1, 1, 1)
        self.win.add_group(self.draw())
        self.win.home()

        self.menu = DotplotMenu(self)
        self.win.set_menu(self.menu)

        self.win.set_binding(input_key("c"), self.toggle_chrom_divs)
        self.win.set_binding(input_key("t"), self.toggle_trace)
Exemplo n.º 4
0
    def show(self):
        if self.win == None or not self.win.is_open():
            self.win = summon.Window("synteny")

        if self.rootid == None:
            self.rootid = self.win.get_root()

        if self.visid == None:
            self.win.set_size(*self.winsize)
            self.win.set_bgcolor(1, 1, 1)
            self.win.set_binding(input_key("c"), self.toggle_controls)
            self.win.set_binding(input_key("l"), self.toggle_labels)
            self.visid = self.win.insert_group(self.rootid, group())
Exemplo n.º 5
0
def show_coal_track2(tree_track):

    win = summon.Window()


    bgcolor = (1, 1, 1, .1)
    cmap = util.rainbow_color_map(low=0.0, high=1.0)
    tracks = {}

    maxage = 0
    for (start, end), tree in tree_track:
        print(start)
        l = []
        times = treelib.get_tree_timestamps(tree)
        nleaves = len(tree.leaves())
        maxage2 = 0
        for node in tree:
            if len(node.children) > 1:
                age = times[node]
                freq = len(node.leaves()) / float(nleaves)
                #sizes = [len(x.leaves()) for x in node.children]
                #m = max(sizes)
                #n = sum(sizes)
                #pval = 2 * (n - m) / float(n - 1)
                l.extend([color(*cmap.get(freq)), start, age, end, age])
                if age > maxage2:
                    maxage2 = age
        win.add_group(group(lines(*l), color(*bgcolor),
                      box(start, 0, end, maxage2, fill=True)))
        if maxage2 > maxage:
            maxage = maxage2

    def func():
        x, y = win.get_mouse_pos()
        print("pos=%s age=%f" % (util.int2pretty(int(x)), y))
    win.add_group(hotspot("click", 0, 0, end, maxage,
                          func))

    win.home("exact")


    return win
Exemplo n.º 6
0
def show_smc(smc,
             mut=None,
             show_labels=False,
             branch_click=None,
             use_names=False):
    """
    """
    def draw_labels(tree, layout):
        return group(*[
            text_clip(names[leaf.name], layout[leaf][0] -
                      .4, layout[leaf][1], layout[leaf][0] +
                      .4, layout[leaf][1] - 1e4, 4, 20, "top", "center")
            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

    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")

    def on_scroll_window(win):
        region = win.get_visible()
        print region

    def on_resize_window(win):
        region = win.get_visible()
        print region

    branch_color = (1, 1, 1)
    spr_color = (1, 0, 0, .5)
    recomb_color = (1, 0, 0)

    # create window
    win = summon.Window()
    win.set_binding(input_key("]"), lambda: trans_camera(win, treewidth, 0))
    win.set_binding(input_key("["), lambda: trans_camera(win, -treewidth, 0))
    win.add_view_change_listener(lambda: on_scroll_window(win))
    #win.remove_resize_listener(lambda : on_resize_window(win))

    treex = 0
    step = 2

    names = []
    seq_range = [0, 0]
    treewidth = 10
    tree = None
    layout = None

    for item in smc:
        if item["tag"] == "NAMES":
            names = item["names"]
            if not use_names:
                names = map(str, range(len(names)))

            treewidth = len(names)

        elif item["tag"] == "RANGE":
            seq_range = [item["start"], item["end"]]

        elif item["tag"] == "TREE":
            tree = item["tree"]

            layout = treelib.layout_tree(tree, xscale=1, yscale=1)
            treelib.layout_tree_vertical(layout, leaves=0)
            #map_layout(layout, yfunc=minlog)

            region_text = text_clip("%d-%d" % (item["start"], item["end"]),
                                    treewidth * .05, 0, treewidth * .95,
                                    -max(l[1] for l in layout.values()), 4, 20,
                                    "center", "top")

            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()),
                    zoom_clamp(translate(0, -20, region_text),
                               axis=(treewidth, 0),
                               miny=1.0,
                               maxy=1.0)))

            clicking = group()
            g.append(clicking)

        elif item["tag"] == "SPR":

            rx, ry = layout[tree[item["recomb_node"]]]
            ry = item["recomb_time"]
            cx, cy = layout[tree[item["coal_node"]]]
            cy = item["coal_time"]

            g.append(
                group(
                    lines(color(*spr_color), rx, ry, cx, cy),
                    mark_tree(tree,
                              layout,
                              item["recomb_node"],
                              time=item["recomb_time"],
                              col=recomb_color)))

            treex += treewidth + step
    '''
    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.home("exact")

    return win
Exemplo n.º 7
0
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
Exemplo n.º 8
0
    recombs = list(x.pos for x in arghmm.iter_visible_recombs(arg))
    print "recomb", recombs

    pos = recombs[0] + 1
    tree = arg.get_marginal_tree(pos - .5)
    last_tree = arg.get_marginal_tree(pos - 1 - .5)

    r, c = arghmm.find_recomb_coal(tree, last_tree, pos=pos)

    #treelib.draw_tree_names(last_tree.get_tree(), minlen=5, maxlen=5)
    #treelib.draw_tree_names(tree.get_tree(), minlen=5, maxlen=5)

    model = arghmm.ArgHmm(arg, seqs, new_name="n%d" % (k - 1), times=times)

    #================================================================
    win = summon.Window()

    layout = argvis.layout_arg(last_tree)
    win.add_group(argvis.draw_arg(last_tree, layout))

    layout2 = argvis.layout_arg(tree)
    for node, (x, y) in layout2.items():
        layout2[node] = (x + 20, y)

    win.add_group(argvis.draw_arg(tree, layout2))

    recomb = tree[r[0]]
    x, y = layout2[recomb]
    win.add_group(argvis.draw_mark(x, y, col=(0, 0, 1)))

    coal = last_tree[c[0]]
Exemplo n.º 9
0
    def show(self):
        """Display browser"""
        self.enable_updating(False)

        # create window
        if self.win == None or not self.win.is_open():
            newwin = True
            self.win = summon.Window(position=self.winpos, size=self.winsize)

            self.win.set_bgcolor(1, 1, 1)
            self.win.select.set_callback(self.on_select)

            # zooming
            self.win.set_binding(input_click("right", "down"), "focus")
            self.win.set_binding(input_motion("right", "down"), "zoomx")
            self.win.set_binding(input_click("right", "down", "shift"),
                                 "focus")
            self.win.set_binding(input_motion("right", "down", "shift"),
                                 "zoomy")

            # create left window
            pos = self.win.get_position()
            self.leftwin = summon.Window(
                " ",
                size=(150, self.win.get_size()[1]),
                position=(pos[0] - 150 - self.win.get_decoration()[0], pos[1]))

            self.leftwin.set_bgcolor(1, 1, 1)

            self.left_ensemble = multiwindow.WindowEnsemble(
                [self.leftwin, self.win],
                stacky=True,
                sameh=True,
                tiey=True,
                piny=True,
                master=self.win)

            # menu
            self.sidebar = hud.SideBar(self.win, width=150)
            self.sidebar.add_item(
                hud.MenuItem("toggle ruler (r)", self.toggle_ruler))
            self.win.set_binding(input_key("r"), self.toggle_ruler)

        else:
            newwin = False
            self.win.clear_groups()

        # determine largest chromosome size
        maxchrom = max(x.length() for x in self.chroms)

        # add line to left window
        maxname = max(len(x.seqname) for x in self.chroms) * 20
        self.leftwin.add_group(
            lines(color(1, 1, 1), 0, 0, -maxname, 0, 0, 0, 0,
                  self.chrom_step * len(self.chroms), 0,
                  self.chrom_step * len(self.chroms), -maxname,
                  self.chrom_step * len(self.chroms)))

        # draw chromosome labels
        self.win.add_group(self.draw_chrom_labels())
        self.leftwin.add_group(self.draw_chrom_labels())

        # draw all tracks
        for track in self._tracks:
            self.win.add_group(track.draw())

        if newwin:
            # setup left window
            self.leftwin.home()

            # setup window
            w, h = self.win.get_size()
            bottom = float(
                min(self.chrom_pos[i][1]
                    for i in self.chroms) + 2 * self.chrom_step)

            self.win.set_visible(0, -2 * self.chrom_step, maxchrom, bottom)
            self.win.focus(w / 2, h / 2)
            self.win.zoom(1, maxchrom / abs(-2 * self.chrom_step - bottom))
            self.win.zoom(.9, .9)

        self.enable_updating(True)
Exemplo n.º 10
0
    def show(self,
             species=None,
             chrom=None,
             start=None,
             end=None,
             width=800,
             height=400):
        self.enable_updating(False)

        view_change = False

        # initialize window if needed
        if self.win == None:
            self.win = summon.Window("StackBrowser",
                                     size=self.winsize,
                                     position=self.winpos)
            self.win.set_bgcolor(1, 1, 1)

            # TODO: add binding query for zooming
            self.win.set_binding(input_click("right", "down"), "focus")
            self.win.set_binding(input_motion("right", "down"), "zoomx")
            self.win.set_binding(input_click("right", "down", "shift"),
                                 "focus")
            self.win.set_binding(input_motion("right", "down", "shift"),
                                 "zoomy")

            # initialize root group
            self.gid = self.win.add_group(group())

            view_change = True

        # initialize left window if needed
        if self.show_left_window:
            if self.leftwin == None:
                self.leftwin = summon.Window(" ", size=(150, height))
                self.leftwin.set_bgcolor(1, 1, 1)

                # initialize root group
                self.leftgid = self.leftwin.add_group(group())

                self.left_ensemble = multiwindow.WindowEnsemble(
                    [self.leftwin, self.win],
                    stacky=True,
                    sameh=True,
                    tiey=True,
                    piny=True,
                    master=self.win)
                self.leftwin.set_boundary(0, util.INF, -150, -util.INF)
        else:
            if self.leftwin:
                self.leftwin.close()
                self.leftwin = None

        # initialize view
        if species != None:
            view_change = True
            self.set_view(species, chrom, start, end)

        # perform drawing
        self.gid = self.win.replace_group(self.gid, self.stack.draw())
        if self.leftwin:
            self.leftid = self.leftwin.replace_group(self.leftgid,
                                                     self.stack.draw_left())

        # center window view
        if view_change:
            stacksize = self.stack.get_size()
            stackpos = self.stack.pos

            w, h = self.win.get_size()
            self.win.set_visible(stackpos[0], stackpos[1],
                                 stackpos[0] + stacksize[0],
                                 stackpos[1] - stacksize[1])
            self.win.focus(w / 2, h / 2)
            self.win.zoom(1, stacksize[0] / (5.0 * stacksize[1]))
            self.win.zoom(.9, .9)

        self.enable_updating()