Beispiel #1
0
def test_match(man):
    """
    http://tinkerpop.apache.org/docs/3.3.4/reference/#match-step

    First example
    """
    errors = []

    G = man.setGraph("swapi")

    query = G.query().V().match([
        __.as_('a').in_('residents').as_('b'),
        __.as_('b').has(gripql.eq('name', 'Tatooine')),
        __.as_('b').out('films').as_('c'),
        __.as_('c').has(gripql.eq('title', "A New Hope"))
    ]).select(['a', 'c'])

    count = 0
    for row in query.execute(stream=True):
        count += 1
        if len(row) != 2:
            errors.append("Incorrect number of marks returned in row")
            continue
        if row["a"]['data']['name'] not in [
                "Luke Skywalker", "Biggs Darklighter", "R5-D4",
                "Beru Whitesun lars", "Owen Lars", "Darth Vader", "C-3PO"
        ]:
            errors.append("Incorrect 'a' return")
        if row["c"]['data']['title'] != "A New Hope":
            errors.append("Incorrect 'c' return")

    if count != 7:
        errors.append("Incorrect return count: %d != %d" % (count, 7))

    return errors
Beispiel #2
0
    def find_variants_for_genes_bmeg(self, genes, disease, dataset="tcga"):

        gene_ids = {}
        for g in genes:
            for i in self.O.query().V().hasLabel("Gene").has(
                    gripql.eq("symbol", g)):
                gene_ids[g] = i.gid

            # Find all the tumor aliquots in tcga for the disease
        proj_id = "Project:TCGA-" + disease
        q = self.O.query().V(proj_id).in_("InProject").in_("SampleFor").in_(
            "AliquotFor").has(
                gripql.eq("gdc_attributes.sample_type", "Primary Tumor")).as_(
                    "sample").in_("CallsetFor").select("sample")
        all_aliquots = []
        for row in q:
            all_aliquots.append(row.gid)
        if len(all_aliquots) == 0:
            return 0

        q = self.O.query().V(all_aliquots).as_("sample").in_(
            "CallsetFor").outV("AlleleCall")

        #
        # q = q.has(gripql.within("ensembl_gene", list(gene_ids.values()))).as_("variant")
        #
        # q = q.distinct("gid")

        mutations = []
        for i in q:
            if hasattr(i['data'],
                       'effect'):  # not all rows have 'effect' attribute
                mutations.append(i.data.effect)

        return mutations
Beispiel #3
0
    def get_variant_info(self, gene_name, mutation):
        # Prepend 'p.' to the mutation name where 'p'
        # represents a protein
        mutation = 'p.' + mutation

        assoc_ids = self.O.query().V().hasLabel("Gene").has(gripql.eq("$.symbol", gene_name)) \
            .out("alleles").has(gripql.eq("hgvsp_short", mutation)) \
            .out("g2p_associations").render("_gid").execute()

        res = []
        for gid in assoc_ids:
            assoc_q = self.O.query().V().hasLabel("G2PAssociation").has(
                gripql.eq("$._gid", gid))

            def safe_get_first(l):
                if len(l) == 0:
                    return None
                return l[0]

            response_type = safe_get_first(
                assoc_q.render("response_type").execute())
            disease_name = safe_get_first(
                assoc_q.out("phenotypes").render("name").execute())
            url = safe_get_first(
                assoc_q.out("publications").render("url").execute())
            compounds = assoc_q.out("compounds").execute()
            drug_name = safe_get_first(
                list(
                    map(lambda c: get_drug_name_from_compound(c["data"]),
                        compounds)))

            res.append([drug_name, response_type, disease_name, url])
        return res
Beispiel #4
0
    def find_mutation_frequency(self, gene, disease):

        # find gene id

        for i in self.O.query().V().hasLabel("Gene").has(
                gripql.eq("symbol", gene)):
            gene_id = i.gid

        # Find all the tumor aliquots in tcga for the disease
        proj_id = "Project:TCGA-" + disease.upper()
        q = self.O.query().V(proj_id).in_("InProject").in_("SampleFor").in_(
            "AliquotFor").has(
                gripql.eq("gdc_attributes.sample_type", "Primary Tumor")).as_(
                    "sample").in_("CallsetFor").select("sample")
        all_aliquots = []
        for row in q:
            all_aliquots.append(row.gid)
        if len(all_aliquots) == 0:
            return 0

        q = self.O.query().V(all_aliquots).as_("sample").in_(
            "CallsetFor").outE("AlleleCall")
        q = q.has(gripql.eq("ensembl_gene", gene_id)).as_("variant")
        q = q.distinct("_from")

        mut_samples = []
        for i in q:
            mut_samples.append(i.gid)

        freq = (float(len(mut_samples)) / float(len(all_aliquots)))  #* 100

        # print (freq)
        return freq
Beispiel #5
0
def test_has_eq(O):
    errors = []
    setupGraph(O)

    count = 0
    for i in O.query().V().has(gripql.eq("_gid", "vertex3")):
        count += 1
        if i['gid'] != "vertex3":
            errors.append("Wrong vertex returned %s" % (i))
    if count != 1:
        errors.append(
            "Fail: O.query().V().has(gripql.eq(\"_gid\", \"vertex3\")) %s != %s" %
            (count, 1))

    count = 0
    for i in O.query().V().has(gripql.eq("_label", "person")):
        count += 1
        if i['label'] != "person":
            errors.append("Wrong vertex label %s" % (i['label']))
    if count != 4:
        errors.append(
            "Fail: O.query().V().has(gripql.eq(\"_label\", \"person\")) %s != %s" %
            (count, 4))

    count = 0
    for i in O.query().V().has(gripql.eq("occupation", "jedi")):
        count += 1
        if i['gid'] not in ["vertex2", "vertex5"]:
            errors.append("Wrong vertex returned %s" % (i))
    if count != 2:
        errors.append(
            "Fail: O.query().V().has(gripql.eq(\"occupation\", \"jedi\")) %s != %s" %
            (count, 2))

    return errors
Beispiel #6
0
def test_render(O):
    errors = []

    O.addVertex("1", "Person", {"name": "marko", "age": "29"})
    O.addVertex("2", "Person", {"name": "vadas", "age": "27"})
    O.addVertex("3", "Software", {"name": "lop", "lang": "java"})
    O.addVertex("4", "Person", {"name": "josh", "age": "32"})
    O.addVertex("5", "Software", {"name": "ripple", "lang": "java"})
    O.addVertex("6", "Person", {"name": "peter", "age": "35"})

    O.addEdge("1", "3", "created", {"weight": 0.4})
    O.addEdge("1", "2", "knows", {"weight": 0.5})
    O.addEdge("1", "4", "knows", {"weight": 1.0})
    O.addEdge("4", "3", "created", {"weight": 0.4})
    O.addEdge("6", "3", "created", {"weight": 0.2})
    O.addEdge("4", "5", "created", {"weight": 1.0})

    query = O.query().V().where(gripql.eq("_label", "Person")).render({
        "Name":
        "name",
        "Age":
        "age"
    })
    for row in query:
        if 'Age' not in row or "Name" not in row:
            errors.append("Missing fields")

    query = O.query().V().where(gripql.eq("_label", "Person")).render({
        "Name":
        "name",
        "NonExistent":
        "non-existent"
    })
    for row in query:
        if 'NonExistent' not in row or "Name" not in row:
            errors.append("Missing fields")

    query = O.query().V().where(gripql.eq("_label",
                                          "Person")).render(["name", "age"])
    for row in query:
        if not isinstance(row, list):
            errors.append("unexpected output format")
        if len(row) != 2:
            errors.append("Missing fields")

    query = O.query().V().where(gripql.eq("_label", "Person")).render(
        ["name", "non-existent"])
    for row in query:
        if not isinstance(row, list):
            errors.append("unexpected output format")
        if len(row) != 2:
            errors.append("Missing fields")

    return errors
Beispiel #7
0
def test_has_or(O):
    errors = []
    setupGraph(O)

    count = 0
    for i in O.query().V().has(gripql.or_(gripql.eq("occupation", "sith"), gripql.eq("occupation", "jedi"))):
        count += 1
        if i['gid'] not in ["vertex2", "vertex5", "vertex6"]:
            errors.append("Wrong vertex returned %s" % (i))
    if count != 3:
        errors.append(
            "Fail: O.query().V().has(gripql.or_(gripql.eq(\"occupation\", \"sith\"), gripql.eq(\"occupation\", \"jedi\"))) %s != %s" %
            (count, 3))

    return errors
Beispiel #8
0
def test_has_and(O):
    errors = []
    setupGraph(O)

    count = 0
    for i in O.query().V().has(gripql.and_(gripql.eq("_label", "person"), gripql.eq("occupation", "jedi"))):
        count += 1
        if i['gid'] not in ["vertex2", "vertex5"]:
            errors.append("Wrong vertex returned %s" % (i))
    if count != 2:
        errors.append(
            "Fail: O.query().V().has(gripql.and_(gripql.eq(\"_label\", \"person\"), gripql.eq(\"occupation\", \"jedi\"))) %s != %s" %
            (count, 2))

    return errors
Beispiel #9
0
def test_has_not(O):
    errors = []
    setupGraph(O)

    count = 0
    for i in O.query().V().has(gripql.not_(gripql.eq("_label", "person"))):
        count += 1
        if i['gid'] not in ["vertex3", "vertex4"]:
            errors.append("Wrong vertex returned %s" % (i))
    if count != 2:
        errors.append(
            "Fail: O.query().V().has(gripql.not_(gripql.eq(\"_label\", \"person\"))) %s != %s" %
            (count, 2))

    count = 0
    for i in O.query().V().has(gripql.not_(gripql.neq("_label", "person"))):
        count += 1
        if i['gid'] not in ["vertex1", "vertex2", "vertex5", "vertex6"]:
            errors.append("Wrong vertex returned %s" % (i))
    if count != 4:
        errors.append(
            "Fail: O.query().V().has(gripql.not_(gripql.neq(\"_label\", \"person\"))) %s != %s" %
            (count, 4))

    return errors
Beispiel #10
0
def test_select(O):
    errors = []

    O.addVertex("1", "Reaction", {"action": "up"})
    O.addVertex("2", "Protein", {"symbol": "MDM2"})
    O.addVertex("3", "Protein", {"symbol": "TP53"})
    O.addVertex("4", "Reaction", {"action": "up"})
    O.addVertex("5", "Protein", {"symbol": "HNF4"})
    O.addVertex("6", "Protein", {"symbol": "MED1"})

    O.addEdge("1", "2", "controller", {})
    O.addEdge("1", "3", "controlled", {})
    O.addEdge("4", "5", "controller", {})
    O.addEdge("4", "6", "controlled", {})

    q = O.query().V().hasLabel("Reaction").as_("reaction")
    q = q.out("controller").has(gripql.eq("symbol", "MDM2")).select("reaction")
    q = q.out("controlled")

    found = 0
    for row in q:
        found += 1
        if row.data.symbol != "TP53":
            errors.append("Bad connection found")

    if found != 1:
        errors.append("Incorrect number of reactions found: %s != 1" % (found))
    return errors
Beispiel #11
0
def test_distinct(O):
    errors = []

    O.addVertex("1", "Person", {"name": "marko", "age": 29})
    O.addVertex("2", "Person", {"name": "vadas", "age": 25})
    O.addVertex("4", "Person", {"name": "josh", "age": 32})
    O.addVertex("6", "Person", {"name": "peter", "age": 35})
    O.addVertex("7", "Person", {"name": "marko", "age": 41})
    O.addVertex("9", "Person", {"name": "alex", "age": 30})
    O.addVertex("10", "Person", {"name": "alex", "age": 45})
    O.addVertex("11", "Person", {"name": "steve", "age": 26})
    O.addVertex("12", "Person", {"name": "alice", "age": 22})
    O.addVertex("13", "Person", {"name": "wanda", "age": 36})
    O.addVertex("5", "Software", {"name": "ripple", "lang": "java"})
    O.addVertex("3", "Software", {"name": "lop", "lang": "java"})
    O.addVertex("8", "Software", {"name": "funnel", "lang": "go"})
    O.addVertex("14", "Software", {"name": "grip", "lang": None})

    O.addEdge("1", "5", "developer", gid="edge1")
    O.addEdge("7", "5", "developer", gid="edge2")
    O.addEdge("3", "8", "dependency", gid="edge3")

    count = 0
    for i in O.query().V().distinct():
        count += 1
    if count != 14:
        errors.append("Distinct %s != %s" % (count, 14))

    count = 0
    for i in O.query().V().distinct("_gid"):
        count += 1
    if count != 14:
        errors.append("Distinct %s != %s" % (count, 14))

    count = 0
    for i in O.query().V().distinct("name"):
        count += 1
    if count != 12:
        errors.append("Distinct %s != %s" % (count, 12))

    count = 0
    for i in O.query().V().distinct("lang"):
        count += 1
    if count != 3:
        errors.append("Distinct %s != %s" % (count, 3))

    count = 0
    for i in O.query().V().distinct("non-existent-field"):
        count += 1
    if count != 0:
        errors.append("Distinct %s != %s" % (count, 0))

    count = 0
    for i in O.query().V().where(gripql.eq(
            "_label", "Person")).mark("person").out().distinct("$person.name"):
        count += 1
    if count != 1:
        errors.append("Distinct %s != %s" % (count, 1))

    return errors
Beispiel #12
0
def test_match(O):
    """
    http://tinkerpop.apache.org/docs/3.3.4/reference/#match-step

    First example
    """
    errors = []

    O.addVertex("1", "Person", {"name": "marko", "age": "29"})
    O.addVertex("2", "Person", {"name": "vadas", "age": "27"})
    O.addVertex("3", "Software", {"name": "lop", "lang": "java"})
    O.addVertex("4", "Person", {"name": "josh", "age": "32"})
    O.addVertex("5", "Software", {"name": "ripple", "lang": "java"})
    O.addVertex("6", "Person", {"name": "peter", "age": "35"})

    O.addEdge("1", "3", "created", {"weight": 0.4})
    O.addEdge("1", "2", "knows", {"weight": 0.5})
    O.addEdge("1", "4", "knows", {"weight": 1.0})
    O.addEdge("4", "3", "created", {"weight": 0.4})
    O.addEdge("6", "3", "created", {"weight": 0.2})
    O.addEdge("4", "5", "created", {"weight": 1.0})

    query = O.query().V().match([
        __.as_('a').out('created').as_('b'),
        __.as_('b').has(gripql.eq('name', 'lop')),
        __.as_('b').in_('created').as_('c'),
        __.as_('c').has(gripql.eq('age', "29"))
    ]).select(['a', 'c'])

    count = 0
    for row in query.execute(stream=True):
        count += 1
        if len(row) != 2:
            errors.append("Incorrect number of marks returned in row")
            continue
        if row["a"]['data']['name'] not in ["marko", "josh", "peter"]:
            errors.append("Incorrect 'a' return")
        if row["c"]['data']['name'] != "marko":
            errors.append("Incorrect 'c' return")

    if count != 3:
        errors.append("Incorrect return count: %d != %d" % (count, 3))

    return errors
Beispiel #13
0
def test_match_count(O):
    errors = []

    O.addVertex("1", "Person", {"name": "marko", "age": "29"})
    O.addVertex("2", "Person", {"name": "vadas", "age": "27"})
    O.addVertex("3", "Software", {"name": "lop", "lang": "java"})
    O.addVertex("4", "Person", {"name": "josh", "age": "32"})
    O.addVertex("5", "Software", {"name": "ripple", "lang": "java"})
    O.addVertex("6", "Person", {"name": "peter", "age": "35"})

    O.addEdge("1", "3", "created", {"weight": 0.4})
    O.addEdge("1", "2", "knows", {"weight": 0.5})
    O.addEdge("1", "4", "knows", {"weight": 1.0})
    O.addEdge("4", "3", "created", {"weight": 0.4})
    O.addEdge("6", "3", "created", {"weight": 0.2})
    O.addEdge("4", "5", "created", {"weight": 1.0})

    query = O.query().V().match([
        O.query().mark('a').out('created').mark('b'),
        O.query().mark('b').where(gripql.eq('$.name', 'lop')),
        O.query().mark('b').in_('created').mark('c'),
        O.query().mark('c').where(gripql.eq('$.age', "29"))
    ]).select(['a', 'c'])

    count = 0
    for row in query.execute(stream=True):
        count += 1
        if len(row) != 2:
            errors.append("Incorrect number of marks returned in row")
            continue
        if row["c"]['data']['name'] != "marko":
            errors.append("Incorrect return")

    if count != 3:
        errors.append("Incorrect return count: %d != %d" % (count, 3))

    return errors
Beispiel #14
0
def test_select(man):
    errors = []

    G = man.setGraph("swapi")

    q = G.query().V().hasLabel("Character").as_("person")
    q = q.out("homeworld").has(gripql.eq("name", "Tatooine")).select("person")
    q = q.out("species")

    found = 0
    for row in q:
        found += 1
        if row.data.name not in ["Human", "Droid"]:
            errors.append("Bad connection found: %s" % (row.data.name))

    if found != 7:
        errors.append("Incorrect number of people found: %s != 7" % (found))
    return errors
Beispiel #15
0
conn = gripql.Connection("http://localhost:8201")
G = conn.graph("covid")

with open("geojson-counties-fips.json") as handle:
    counties = json.loads(handle.read())

countiesSub = {"type": "FeatureCollection", "features": []}
for c in counties['features']:
    if c['properties']['STATE'] == "41":
        countiesSub['features'].append(c)

curDate = "2020-04-14 23:33:31"

q = G.query().V().hasLabel("SummaryLocation").has(
    gripql.eq("province_state", "OR")).as_("a")
q = q.out("summary_reports").has(gripql.eq("date", curDate)).as_("b")
q = q.render(["$a._gid", "$b.confirmed", "$b.deaths", "$b.recovered"])

mapData = {}
for i in q:
    mapData[i[0]] = {
        "fips": i[0],
        "confirmed": i[1],
        "deaths": i[2],
        "recovered": i[3]
    }
mapDF = pd.DataFrame(mapData).transpose()

fig = px.choropleth_mapbox(mapDF,
                           geojson=countiesSub,
Beispiel #16
0
import json
import datetime
import gripql
import plotly.express as px
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html


conn = gripql.Connection("http://localhost:8201")

G = conn.graph("covid")

q = G.query().V().hasLabel("SummaryLocation").has(gripql.eq("province_state", "OR")).as_("a")
q = q.out("summary_reports").as_("b").render(["$a._gid", "$b._data"])

res = list(q)
data = {}
for k, v in res:
    if k not in data:
        data[k] = {}
    date_time_str = v['date']
    try:
        d = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M:%S")
    except ValueError:
        try:
            d = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M")
        except ValueError:
            d = datetime.datetime.strptime(date_time_str, "%m/%d/%y %H:%M")
Beispiel #17
0
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize

from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html

from app import app

conn = gripql.Connection("http://localhost:8201")
G = conn.graph("covid")

#get Oregon Counties
q = G.query().V().hasLabel("SummaryLocation").has(
    gripql.eq("province_state", "OR"))
q = q.render(["$._gid", "$.county"])

countyOptions = list({"label": a[1], "value": a[0]} for a in q)

countyDropDown = dcc.Dropdown(id='opt-county-dropdown',
                              options=countyOptions,
                              value=countyOptions[0]['value'])


def getCountySummaryReports(fips):
    q = G.query().V(fips).out("summary_reports").render(
        ["date", "confirmed", "deaths", "recovered"])
    return list(q)

Beispiel #18
0
    def find_drugs_for_mutation_dataset(self, genes, dataset):

        program = "Program:" + dataset.upper()

        q = self.O.query().V(program).in_("InProgram").in_("InProject").in_(
            "SampleFor").in_("AliquotFor").distinct("_gid")
        all_aliquots = []
        for row in q:
            all_aliquots.append(row.gid)

        # GENES = ["CDKN2A", "PTEN", "TP53", "SMAD4"]
        gene_ids = {}
        for g in genes:
            for i in self.O.query().V().hasLabel("Gene").has(
                    gripql.eq("symbol", g)):
                gene_ids[g] = i.gid

        #Scan <dataset> cell lines based on mutation status
        mut_samples = {}
        norm_samples = {}

        q = self.O.query().V(all_aliquots).as_("sample").in_(
            "CallsetFor").outE("AlleleCall")
        q = q.has(gripql.within("ensembl_gene",
                                list(gene_ids.values()))).as_("variant")
        q = q.render({
            "sample": "$sample._gid",
            "gene": "$variant._data.ensembl_gene"
        })

        for res in q:
            mut_samples[res.gene] = mut_samples.get(res.gene, set()) | set(
                [res.sample])

        # get dataset samples without mutation
        for i in gene_ids.values():
            norm_samples[i] = list(
                set(all_aliquots).difference(mut_samples[i]))

            print("%s Positive Set: %d" % (i, len(mut_samples[i])))
            print("%s Negative Set: %d" % (i, len(norm_samples[i])))

        # Get response values for the positive set (samples with mutation) and collect AUC value by drug
        pos_response = {}
        compound = {}
        for g in gene_ids.values():
            pos_response[g] = {}
            q = self.O.query().V(list(mut_samples[g])).in_("ResponseIn").has(
                gripql.eq("source",
                          dataset)).as_("a").out("ResponseTo").as_("b").select(
                              ["a", "b"])
            for row in q:
                if hasattr(row['a']['data'],
                           'act_area'):  # not all rows have 'amax' attribute
                    v = row['a']['data']['act_area']
                else:
                    v = 0

                id = row['b']['gid']
                compound[id] = row['b']['data']['name']

                if id not in pos_response[g]:
                    pos_response[g][id] = [v]
                else:
                    pos_response[g][id].append(v)

        #Get response values for the negative set (samples without mutation) and collect AUC value by drug
        neg_response = {}
        for g in gene_ids.values():
            neg_response[g] = {}
            q = self.O.query().V(list(norm_samples[g])).in_("ResponseIn").has(
                gripql.eq("source",
                          dataset)).as_("a").out("ResponseTo").as_("b").select(
                              ["a", "b"])
            for row in q:
                if hasattr(row['a']['data'],
                           'act_area'):  # not all rows have 'amax' attribute
                    v = row['a']['data']['act_area']
                else:
                    v = 0
                id = row['b']['gid']
                compound[id] = row['b']['data']['name']

                if id not in neg_response[g]:
                    neg_response[g][id] = [v]
                else:
                    neg_response[g][id].append(v)

        #Collect t-test statistics
        drugs = set(
            itertools.chain.from_iterable(i.keys()
                                          for i in pos_response.values()))
        out = []
        for drug in drugs:
            for g in gene_ids.values():
                if drug in pos_response[g] and drug in neg_response[g]:
                    row = {"drug": drug, "mutation": g}

                    mut_values = pos_response[g][drug]
                    norm_values = neg_response[g][drug]
                    if len(mut_values) > 5 and len(norm_values) > 5:
                        s = stats.ttest_ind(mut_values,
                                            norm_values,
                                            equal_var=False)
                        if s.pvalue <= 0.05 and s.statistic > 0:  # means drug is significantly effective
                            out.append(compound[drug])

        # print(out)

        # get names of compounds
        return out
Beispiel #19
0
def getCountyPopulation(fips):
    q = G.query().V(fips).out("census").has(gripql.eq("gender", None)).render(
        ["population"])
    population = sum(list(a[0] for a in q))
    return population
Beispiel #20
0
def test_has_complex(O):
    errors = []
    setupGraph(O)

    count = 0
    for i in O.query().V().has(
            gripql.and_(
                gripql.eq("_label", "person"),
                gripql.not_(
                    gripql.or_(
                        gripql.eq("occupation", "jedi"),
                        gripql.eq("occupation", "sith")
                    )
                )
            )
    ):
        count += 1
        if i['gid'] != "vertex1":
            errors.append("Wrong vertex returned %s" % (i))
    if count != 1:
        errors.append(
            "Fail: O.query().V().has(gripql.and_(gripql.eq(\"_label\", \"person\"), gripql.not_(gripql.or_(gripql.eq(\"occupation\", \"jedi\"), gripql.eq(\"occupation\", \"sith\"))))) %s != %s" %
            (count, 1)
        )

    count = 0
    for i in O.query().V().has(
            gripql.not_(
                gripql.or_(
                    gripql.eq("_label", "robot"),
                    gripql.eq("occupation", "jedi"),
                )
            )
    ):
        count += 1
        if i['gid'] not in ["vertex1", "vertex6"]:
            errors.append("Wrong vertex returned %s" % (i))
    if count != 2:
        errors.append(
            "Fail: O.query().V().has(gripql.not_(gripql.and_(gripql.eq(\"_label\", \"robot\"), gripql.eq(\"occupation\", \"jedi\")))) %s != %s" %
            (count, 2)
        )

    count = 0
    for i in O.query().V().has(
            gripql.not_(
                gripql.or_(
                    gripql.eq("_label", "robot"),
                    gripql.or_(
                        gripql.eq("occupation", "jedi"),
                        gripql.contains("starships", "millennium falcon")
                    )
                )
            )
    ):
        count += 1
        if i['gid'] != "vertex6":
            errors.append("Wrong vertex returned %s" % (i))
    if count != 1:
        errors.append(
            "Fail: O.query().V().has(gripql.not_(gripql.or_(gripql.eq(\"_label\", \"robot\"), gripql.or_(gripql.eq(\"occupation\", \"jedi\"),  gripql.contains(\"starships\", \"millennium falcon\"))))) %s != %s" %
            (count, 1)
        )

    count = 0
    for i in O.query().V().has(
            gripql.not_(
                gripql.and_(
                    gripql.eq("_label", "robot"),
                    gripql.or_(
                        gripql.eq("occupation", "jedi"),
                        gripql.contains("starships", "millennium falcon")
                    )
                )
            )
    ):
        count += 1
    if count != 6:
        errors.append(
            "Fail: O.query().V().has(gripql.not_(gripql.and_(gripql.eq(\"_label\", \"robot\"), gripql.or_(gripql.eq(\"occupation\", \"jedi\"),  gripql.contains(\"starships\", \"millennium falcon\"))))) %s != %s" %
            (count, 6)
        )

    return errors
Beispiel #21
0
    def find_drugs_for_mutation_dataset(self, genes, dataset):
        dataset = dataset.upper()
        program = "Program:" + dataset
        q = self.O.query().V(program).out("projects").out("cases").distinct(
            "_gid")

        all_cases = []
        for row in q:
            all_cases.append(row.gid)

        gene_ids = {}
        for i in self.O.query().V().hasLabel("Gene").has(
                gripql.within("symbol", genes)):
            gene_ids[i.data.symbol] = i.gid

        mut_cases = {}
        norm_cases = {}

        q = self.O.query().V(all_cases).as_("ds")

        if dataset != "CCLE":
            q = q.out("same_as").has(gripql.eq("project_id", "Project:CCLE"))

        q = q.out("samples").out("aliquots").out("somatic_callsets")
        q = q.outE("alleles").has(
            gripql.within("ensembl_gene", list(gene_ids.values())))
        q = q.render({"case": "$ds._gid", "gene": "$._data.ensembl_gene"})

        for res in q:
            mut_cases[res.gene] = mut_cases.get(res.gene, set()) | set(
                [res.case])

        #get CCLE samples without mutation
        for i in gene_ids.values():
            norm_cases[i] = list(set(all_cases).difference(mut_cases[i]))

            print("%s Positive Set: %d" % (i, len(mut_cases[i])))
            print("%s Negative Set: %d" % (i, len(norm_cases[i])))

        names = {}

        area_metric = "act_area" if dataset == "CCLE" else "auc"
        area_metric = "aac"

        pos_response = {}
        for g in gene_ids.values():
            pos_response[g] = {}
            q = self.O.query().V(list(
                mut_cases[g])).as_("a").out("samples").out("aliquots")
            q = q.out("drug_response").as_("a").out("compounds").as_("b")
            q = q.select(["a", "b"])
            for row in q:
                if hasattr(row["a"]["data"], area_metric):
                    v = row["a"]["data"][area_metric]
                else:
                    v = 0

                id = row["b"]["gid"]
                names[id] = get_drug_name_from_compound(row["b"]["data"])

                if id not in pos_response[g]:
                    pos_response[g][id] = [v]
                else:
                    pos_response[g][id].append(v)

        neg_response = {}
        for g in gene_ids.values():
            neg_response[g] = {}
            q = self.O.query().V(list(
                norm_cases[g])).as_("a").out("samples").out("aliquots")
            q = q.out("drug_response").as_("a").out("compounds").as_("b")
            q = q.select(["a", "b"])
            for row in q:
                if hasattr(row["a"]["data"], area_metric):
                    v = row["a"]["data"][area_metric]
                else:
                    v = 0

                id = row["b"]["gid"]
                names[id] = get_drug_name_from_compound(row["b"]["data"])
                if id not in neg_response[g]:
                    neg_response[g][id] = [v]
                else:
                    neg_response[g][id].append(v)

        drugs = set(
            itertools.chain.from_iterable(i.keys()
                                          for i in pos_response.values()))
        out = []
        for drug in drugs:
            for g in gene_ids.values():
                if drug in pos_response[g] and drug in neg_response[g]:
                    row = {"drug": drug, "mutation": g}
                    mut_values = pos_response[g][drug]
                    norm_values = neg_response[g][drug]
                    if len(mut_values) > 5 and len(norm_values) > 5:
                        s = stats.ttest_ind(mut_values,
                                            norm_values,
                                            equal_var=False)
                        if s.pvalue <= 0.05:  # means drug is significantly effective
                            n = names[drug]
                            out.append(n)

        return out
Beispiel #22
0
 def eq(self, *args, **kwargs):
     return gripql.eq(*args, **kwargs)