From ff80b5228e741c24d66d1d1c13702a34aac77a78 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 15 Apr 2021 02:15:39 +0300 Subject: test endpoint for fetching probeset data types --- gn3/api/datasets.py | 20 ++++++++++++++++++ gn3/computations/datasets.py | 50 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/api/datasets.py b/gn3/api/datasets.py index 3d6518e..2d13120 100644 --- a/gn3/api/datasets.py +++ b/gn3/api/datasets.py @@ -4,6 +4,8 @@ from flask import jsonify from gn3.computations.datasets import create_dataset from gn3.computations.datasets import get_traits_data +from gn3.computations.datasets import get_probeset_trait_data + from gn3.db_utils import database_connector @@ -42,3 +44,21 @@ def fetch_traits_data(dataset_name, dataset_type): conn.close() return jsonify({"results": results}) + + +@dataset.route("/fetch_probeset_data/") +def fetch_probeset_data(dataset_name): + """Endpoint for fetching probeset trait data""" + trait_sample_ids = [4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, + 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 29, 30, 31, + 35, 36, 37, 39, 98, 99, 100, 103, 487, 105, 106, 110, 115, + 116, 117, 118, 119, 120, 919, 147, + 121, 40, 41, 124, 125, 128, 135, 129, 130, 131, + 132, 134, 138, 139, 140, 141, 142, 144, + 145, 148, 149, 920, 922, 2, 3, 1, 1100] + + conn, _cursor = database_connector() + + results = get_probeset_trait_data(trait_sample_ids, conn, dataset_name) + + return jsonify({"results": results}) diff --git a/gn3/computations/datasets.py b/gn3/computations/datasets.py index 57e1fe1..b69583e 100644 --- a/gn3/computations/datasets.py +++ b/gn3/computations/datasets.py @@ -320,4 +320,52 @@ def get_traits_data(sample_ids, database_instance, dataset_name, dataset_type): # print(query) _results = fetch_from_db_sample_data(query, database_instance) - return {} + + return [] + + +def get_probeset_trait_data(strain_ids: List, conn, dataset_name) -> dict: + """function for getting trait data\ + for probeset data type similar to\ + get trait data only difference is that\ + it uses sub queries""" + + trait_data: dict = {} + + trait_id_name = {} + + traits_query = """ + SELECT ProbeSetXRef.DataId,ProbeSet.Name FROM (ProbeSet, ProbeSetXRef, ProbeSetFreeze) + WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id + and ProbeSetFreeze.Name = '{}' + and ProbeSet.Id = ProbeSetXRef.ProbeSetId + order by ProbeSet.Id + """.format(dataset_name) + + query = """ + SELECT * from ProbeSetData + where StrainID in ({}) + and id in (SELECT ProbeSetXRef.DataId FROM (ProbeSet, ProbeSetXRef, ProbeSetFreeze) + WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id + and ProbeSetFreeze.Name = '{}' + and ProbeSet.Id = ProbeSetXRef.ProbeSetId + order by ProbeSet.Id) + """.format(",".join(str(strain_id) for strain_id in strain_ids), dataset_name) + + with conn: + cursor = conn.cursor() + cursor.execute(query) + _results = cursor.fetchall() + cursor.execute(traits_query) + trait_id_name = dict(cursor.fetchall()) + + for trait_id, _strain_id, strain_value in _results: + trait_name = trait_id_name[trait_id] + if trait_data.get(trait_name): + trait_data[trait_name].append(strain_value) + else: + trait_data[trait_name] = [] + + trait_data[trait_name].append(strain_value) + + return trait_data -- cgit v1.2.3