Exemple #1
0
def iter_chroms(hits):
    """
    Returns an iterator of iterators it, such that each it iterates over
    hits from the same species and chromosome.
    """

    hits = util.PushIter(hits)

    try:
        hit = hits.next()
    except StopIteration:
        # no hits to iterate
        return

    # initiate which species and chrom we are going to start with
    last_sp = [hit[0].species]
    last_chrom = [hit[0].seqname]
    hits.push(hit)

    def inner_iter(hits):
        """An iterator of hits from only one species, chromome"""
        for hit in hits:
            if hit[0].species != last_sp[0] or hit[0].seqname != last_chrom[0]:
                # if species,chrom changes, push hit back and return
                last_sp[0] = hit[0].species
                last_chrom[0] = hit[0].seqname
                hits.push(hit)
                return
            yield hit

    while hits.peek(None) != None:
        yield inner_iter(hits)
Exemple #2
0
    def __init__(self, filename, parse_trees=False, apply_spr=False):

        if isinstance(filename, basestring):
            # read SMC from file
            self._filename = filename
            self._infile = iter_smc_file(filename,
                                         parse_trees=parse_trees,
                                         apply_spr=apply_spr)
            self._stream = util.PushIter(self._infile)
        else:
            # read SMC from list of items
            self._filename = None
            self._infile = None
            self._stream = util.PushIter(filename)

        # read header
        header_tags = ("NAMES", "REGION")
        self.header = {}
        while self._stream.peek({"tag": ""})["tag"] in header_tags:
            self.header.update(self._stream.next())
            del self.header["tag"]
Exemple #3
0
def iter_windows(hits, radius):
    """Iterate through blast hits using a window with a radius in the
       query genome"""

    hits = util.PushIter(hits)
    cache = LinkedList()

    upstream = set()
    downstream = set()

    try:
        center = hits.next()
    except StopIteration:
        return

    while True:
        # discard anyone in the upstream that is not within radius distance
        for hit in list(upstream):
            if hit[0].end + radius < center[0].start:
                upstream.remove(hit)

        # populate downstream with all regions within in radius
        for hit in hits:
            if hit[0].start - radius > center[0].end:
                hits.push(hit)
                break
            downstream.add(hit)
            cache.append(hit)

        yield (center, upstream, downstream)

        # populate upstream
        upstream.add(center)

        # move center to next hit
        try:
            center = cache.pop_front()
        except IndexError:
            break

        # remove new center from downstream
        downstream.remove(center)
Exemple #4
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