Пример #1
0
def unique_query():

    # parse parameters
    status, val = parse_resource_field()

    # bad args.
    if status == 1:
        return val

    # good args.
    resource, field = val

    # special case for oncotree.
    if resource == 'clinical' and field == 'ONCOTREE_PRIMARY_DIAGNOSIS_NAME':

        # make oncotree.
        onco_tree = oncotreenx.build_oncotree(settings.DATA_ONCOTREE_FILE)

        # loop over every-node and do text match.
        #results = set([onco_tree.node[n]['text'] for n in onco_tree.nodes()])
        #results.remove("root")
        #results = list(results)

        # turn into
        results = list()
        for n in onco_tree.nodes():
            tmp = {
                'text': onco_tree.node[n]['text'],
                'code': n
            }
            results.append(tmp)

    else:

        # search for this field.
        db = app.data.driver.db
        results = db[resource].distinct(field)

        # remove non.
        tmp = set(results)
        if None in tmp:
            tmp.remove(None)
            results = list(tmp)

    # encode response.
    data = json.dumps({'resource': resource, 'field': field, 'values': results})
    resp = Response(response=data,
        status=200,
        mimetype="application/json")

    return resp
Пример #2
0
def autocomplete_query():
    db = app.data.driver.db

    # parse parameters
    status, val = parse_resource_field()

    # parse the value.
    gene = request.args.get("gene")

    # bad args.
    if status == 1:
        return val

    resource, field = val
    results = list(
        db.genomic.aggregate([
            {
                "$match": {
                    "TRUE_HUGO_SYMBOL": gene,
                    field: {
                        "$ne": None
                    }
                }
            },
            {
                "$group": {
                    "_id": "$TRUE_HUGO_SYMBOL",
                    field: {
                        "$addToSet": f"${field}"
                    }
                }
            },
        ]))

    if len(results) > 0 and field in results[0]:
        results = results[0][field]
    else:
        results = []

    # encode response.
    data = json.dumps({
        'resource': resource,
        'field': field,
        'values': results
    })
    resp = Response(response=data, status=200, mimetype="application/json")

    return resp
Пример #3
0
def autocomplete_query():

    # parse parameters
    status, val = parse_resource_field()

    # parse the value.
    value = request.args.get("value")
    gene = request.args.get("gene")

    # bad args.
    if status == 1:
        return val

    # good args.
    resource, field = val

    # get the type.
    if resource == "genomic":
        schema = data_model.genomic_schema[field]['type']

    else:
        schema = data_model.clinical_schema[field]['type']

    # special cases.
    if resource == 'clinical' and field == 'ONCOTREE_PRIMARY_DIAGNOSIS_NAME':

        # make oncotree.
        onco_tree = oncotreenx.build_oncotree(settings.DATA_ONCOTREE_FILE)

        # loop over every-node and do text match.
        hit_set = set()
        for n in onco_tree.nodes():

            #TODO: Verify this doesn't need a decode
            a = onco_tree.node[n]['text'].lower()
            b = value.lower()
            if a.count(b) > 0:

                # get predecessors and ancestors
                hit_set.add(n)
                hit_set = hit_set.union(set(onco_tree.predecessors(n)))
                hit_set = hit_set.union(set(onco_tree.successors(n)))

        # remove root.
        if 'root' in hit_set:
            hit_set.remove('root')

        # convert to full text.
        results = [onco_tree.node[n]['text'] for n in hit_set]

    else:

        # only support string and integer.
        if schema not in set(['string', 'integer']):
            data = json.dumps({'error': 'unsupported field type: %s' % schema})
            resp = Response(response=data,
                status=400,
                mimetype="application/json")
            return resp

        # handle string.
        db = app.data.driver.db
        if schema == "string":

            # finalize search term.
            term = '.*%s.*' % value

            # first make query.
            if gene is None:
                query = db[resource].find({
                    field: {'$regex': term, '$options': '-i'}
                })
            else:
                query = db[resource].find({"$and": [
                    {field: {'$regex': term, '$options': '-i'}},
                    {"$or": [
                        {'TRUE_HUGO_SYMBOL': gene},
                        {'CNV_HUGO_SYMBOL': gene},
                    ]}
                ]})


        else:

            # finalize the search term.
            term = "/^%s.*/.test(this.%s)" % (value, field)

            # first make the query
            if gene is None:
                query = db[resource].find({"$and": [
                    {'$where': term},
                ]})

            else:
                query = db[resource].find({"$and": [
                    {'$where': term},
                    {"$or": [
                        {'TRUE_HUGO_SYMBOL': gene},
                        {'CNV_HUGO_SYMBOL': gene},
                    ]}
                ]})

        # extract distinct from query
        results = query.distinct(field)

    # remove non.
    tmp = set(results)
    if None in tmp:
        tmp.remove(None)
        results = list(tmp)

    # encode response.
    data = json.dumps({'resource': resource, 'field': field, 'values': results})
    resp = Response(response=data,
                    status=200,
                    mimetype="application/json")

    return resp