Beispiel #1
0
    def _make_graph(self, junction_exon_triples):
        self.graph = graphlite.connect(":memory:", graphs=DIRECTIONS)
        self.exons = tuple(junction_exon_triples[self.exon_col].unique())
        self.n_exons = len(self.exons)
        self.junctions = tuple(junction_exon_triples[self.junction_col].unique())

        # Exons are always first to make iteration easy
        self.items = tuple(np.concatenate([self.exons, self.junctions]))
        self.item_to_region = pd.Series(map(Region, self.items), index=self.items)

        with self.graph.transaction() as tr:
            for i, row in self.junction_exon_triples.iterrows():
                junction = row[self.junction_col]
                exon = row[self.exon_col]

                junction_i = self.items.index(junction)
                exon_i = self.items.index(exon)

                self.log.debug("\n{} is {} of {}\n".format(exon, row.direction, junction))
                self.log.debug("{} is {} of {}\n".format(junction, opposite(row.direction), exon))

                tr.store(getattr(V(exon_i), row.direction)(junction_i))
                tr.store(getattr(V(junction_i), opposite(row.direction))(exon_i))

        # To speed up queries
        self.graph.db.execute("ANALYZE upstream")
        self.graph.db.execute("ANALYZE downstream")
Beispiel #2
0
def graph_items(exon_start_stop, transcripts, chrom, strand):
    from outrigger.index.events import stringify_location, opposite

    graph = connect(":memory:", graphs=['upstream', 'downstream'])

    items = []
    triples = set()

    for transcript, exons in transcripts:
        for exon1, exon2 in zip(exons, exons[1:]):

            start1, stop1 = exon_start_stop[exon1]
            start2, stop2 = exon_start_stop[exon2]
            exon1_location = stringify_location(chrom, start1, stop1, strand,
                                                'exon')
            exon2_location = stringify_location(chrom, start2, stop2, strand,
                                                'exon')

            start = stop1 + 1
            stop = start2 - 1

            junction_location = stringify_location(chrom, start, stop, strand,
                                                   'junction')

            if exon1_location not in items:
                items.append(exon1_location)
            if exon2_location not in items:
                items.append(exon2_location)
            if junction_location not in items:
                items.append(junction_location)

            # Get unique integer for junction
            junction_i = items.index(junction_location)

            if strand == '-':
                exon1_triple = exon1_location, 'downstream', junction_location
                exon2_triple = exon2_location, 'upstream', junction_location
            else:
                exon1_triple = exon1_location, 'upstream', junction_location
                exon2_triple = exon2_location, 'downstream', junction_location

            exon_triples = exon1_triple, exon2_triple

            with graph.transaction() as tr:
                for exon_triple in exon_triples:
                    if exon_triple not in triples:
                        triples.add(exon_triple)

                        exon, direction, junction = exon_triple

                        # Get unique integer for exon
                        exon_i = items.index(exon)
                        tr.store(getattr(V(exon_i), direction)(junction_i))
                        tr.store(
                            getattr(V(junction_i),
                                    opposite(direction))(exon_i))
                    else:
                        continue
    items = tuple(items)
    return graph, items
 def __init__(self, path=None):
     self.path = path
     if not self.path:
         self.path = 'people.db'
     self._engine = create_engine('sqlite:///{}'.format(self.path))
     self._session = None
     self._graph = graphlite.connect(self.path, graphs=['begat'])
Beispiel #4
0
    def _make_graph(self, junction_exon_triples):
        self.graph = graphlite.connect(":memory:", graphs=DIRECTIONS)
        self.exons = tuple(junction_exon_triples[self.exon_col].unique())
        self.n_exons = len(self.exons)
        self.junctions = tuple(
            junction_exon_triples[self.junction_col].unique())

        # Exons are always first to make iteration easy
        self.items = tuple(np.concatenate([self.exons, self.junctions]))
        self.item_to_region = pd.Series(map(Region, self.items),
                                        index=self.items)

        with self.graph.transaction() as tr:
            for i, row in self.junction_exon_triples.iterrows():
                junction = row[self.junction_col]
                exon = row[self.exon_col]

                junction_i = self.items.index(junction)
                exon_i = self.items.index(exon)

                self.log.debug('\n{} is {} of {}\n'.format(
                    exon, row.direction, junction))
                self.log.debug('{} is {} of {}\n'.format(
                    junction, opposite(row.direction), exon))

                tr.store(getattr(V(exon_i), row.direction)(junction_i))
                tr.store(
                    getattr(V(junction_i), opposite(row.direction))(exon_i))

        # To speed up queries
        self.graph.db.execute("ANALYZE upstream")
        self.graph.db.execute("ANALYZE downstream")
Beispiel #5
0
def graph_items(exon_start_stop, transcripts, chrom, strand):
    from outrigger.index.events import stringify_location, opposite

    graph = connect(":memory:", graphs=['upstream', 'downstream'])

    items = []
    triples = set()

    for transcript, exons in transcripts:
        for exon1, exon2 in zip(exons, exons[1:]):

            start1, stop1 = exon_start_stop[exon1]
            start2, stop2 = exon_start_stop[exon2]
            exon1_location = stringify_location(chrom, start1, stop1, strand,
                                                'exon')
            exon2_location = stringify_location(chrom, start2, stop2, strand,
                                                'exon')

            start = stop1 + 1
            stop = start2 - 1

            junction_location = stringify_location(chrom, start, stop, strand,
                                                   'junction')

            if exon1_location not in items:
                items.append(exon1_location)
            if exon2_location not in items:
                items.append(exon2_location)
            if junction_location not in items:
                items.append(junction_location)

            # Get unique integer for junction
            junction_i = items.index(junction_location)

            if strand == '-':
                exon1_triple = exon1_location, 'downstream', junction_location
                exon2_triple = exon2_location, 'upstream', junction_location
            else:
                exon1_triple = exon1_location, 'upstream', junction_location
                exon2_triple = exon2_location, 'downstream', junction_location

            exon_triples = exon1_triple, exon2_triple

            with graph.transaction() as tr:
                for exon_triple in exon_triples:
                    if exon_triple not in triples:
                        triples.add(exon_triple)

                        exon, direction, junction = exon_triple

                        # Get unique integer for exon
                        exon_i = items.index(exon)
                        tr.store(getattr(V(exon_i), direction)(junction_i))
                        tr.store(getattr(V(junction_i), opposite(direction))(
                            exon_i))
                    else:
                        continue
    items = tuple(items)
    return graph, items
Beispiel #6
0
def graph(request):
    g = connect(':memory:', graphs=['likes', 'knows'])

    with g.transaction() as tr:
        # 1 knows 2,3,4
        # 2,3 knows 1
        # 1 likes 2,3
        for i in (2, 3, 4):
            tr.store(V(1).knows(i))
            if i != 4:
                tr.store(V(i).knows(1))
                tr.store(V(1).likes(i))

    request.addfinalizer(g.close)
    return g
Beispiel #7
0
#!/usr/bin/env python

from graphlite import connect, V

if __name__ == '__main__':
    graph = connect(':memory:', graphs=['knows'])

    nums = []

    with open('input', 'r') as f:
        for line in f:
            src, dst = line.split(' <-> ')
            src = int(src)
            dst = [int(x) for x in dst.split(', ')]

            nums += [src]

            with graph.transaction() as tr:
                for d in dst:
                    tr.store(V(src).knows(d))
                    tr.store(V(d).knows(src))
                    nums += [d]

    nums = set(nums)

    query = graph.find(V(0).knows)
    groupsize = 0
    group = [0]

    if False:
        # disabled because it overflows the stack