diff options
author | Alexander Kabui | 2021-03-30 18:13:35 +0300 |
---|---|---|
committer | Alexander Kabui | 2021-03-30 18:13:35 +0300 |
commit | b67938a6730e0dc557f0d4aa978e0b9aa9211772 (patch) | |
tree | fe5db4a91a8d325287271b132afe53533e1b6dd0 | |
parent | e6f10522833cbd75441766e5b8656b3f5925d6d7 (diff) | |
download | genenetwork3-b67938a6730e0dc557f0d4aa978e0b9aa9211772.tar.gz |
initial commit for creating dataset
-rw-r--r-- | gn3/api/traits.py | 48 | ||||
-rw-r--r-- | gn3/computations/datasets.py | 66 | ||||
-rw-r--r-- | tests/unit/computations/test_datasets.py | 70 |
3 files changed, 184 insertions, 0 deletions
diff --git a/gn3/api/traits.py b/gn3/api/traits.py new file mode 100644 index 0000000..b3ddcc0 --- /dev/null +++ b/gn3/api/traits.py @@ -0,0 +1,48 @@ +"""this module contains the all endpoints for traits""" +from unittest import mock + +from flask import Blueprint +from flask import jsonify +from flask import request + +from gn3.computations.traits import fetch_trait +from gn3.computations.traits import get_trait_info_data +trait = Blueprint("trait", __name__) + + +@trait.route("/") +def home(): + """initial endpoint for traits""" + return jsonify({"results": "success"}) + + +@trait.route("/<string:trait_name>/<string:dataset_name>") +def create_trait(trait_name, dataset_name): + """endpoints for creating trait first should\ + call the endpoint for creating the trait only\ + trait data is the sample data""" + + trait_dataset = mock.Mock() # xtodo should replace this with calling epoints + trait_dataset.name = dataset_name + + trait_results = fetch_trait(dataset=trait_dataset, trait_name=trait_name) + + return jsonify(trait_results) + + +@trait.route("/trait_info/<string:trait_name>", methods=["POST"]) +def fetch_trait_info(trait_name): + """api endpoint for fetching the trait info \ + expects the trait and trait dataset to have\ + been created """ + data = request.get_json() + + trait_dataset = data["trait_dataset"] + trait_data = data["trait"] + _trait_name = trait_name # should be used as key to return results + + database_instance = mock.Mock() + + results = get_trait_info_data(trait_dataset, trait_data, database_instance) + + return jsonify(results) diff --git a/gn3/computations/datasets.py b/gn3/computations/datasets.py new file mode 100644 index 0000000..92a7bdf --- /dev/null +++ b/gn3/computations/datasets.py @@ -0,0 +1,66 @@ +"""module contains the code all related to datasets""" +from unittest import mock + +from typing import Optional +from typing import List + + +def retrieve_trait_sample_data(dataset_id, dataset_type: str, trait_name: str) -> List: + """given the dataset id and trait_name fetch the\ + sample_name,value from the dataset""" + + # should pass the db as arg all do a setup + + _func_args = (dataset_id, dataset_type, trait_name) + dataset_query = get_query_for_dataset_sample(dataset_type) + + if dataset_query: + if dataset_type == "Publish": + formatted_query = dataset_query % (trait_name, dataset_id) + results = fetch_from_db_sample_data(formatted_query, mock.Mock()) + return results + + return [] + + return [] + + +def fetch_from_db_sample_data(formatted_query: str, database_instance) -> List: + """this is the function that does the actual fetching of\ + results from the database""" + cursor = database_instance.cursor() + cursor.execute(formatted_query) + results = cursor.fetchall() + + cursor.close() + + return results + + +def get_query_for_dataset_sample(dataset_type) -> Optional[str]: + """this functions contains querys for\ + getting sample data from the db depending in + dataset""" + dataset_query = {} + + pheno_query = """ + SELECT + Strain.Name, PublishData.value, PublishSE.error,NStrain.count, Strain.Name2 + FROM + (PublishData, Strain, PublishXRef, PublishFreeze) + left join PublishSE on + (PublishSE.DataId = PublishData.Id AND PublishSE.StrainId = PublishData.StrainId) + left join NStrain on + (NStrain.DataId = PublishData.Id AND + NStrain.StrainId = PublishData.StrainId) + WHERE + PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = %s AND + PublishFreeze.Id = %s AND PublishData.StrainId = Strain.Id + Order BY + Strain.Name + """ + + dataset_query["Publish"] = pheno_query + + return dataset_query.get(dataset_type) diff --git a/tests/unit/computations/test_datasets.py b/tests/unit/computations/test_datasets.py new file mode 100644 index 0000000..2cd58b2 --- /dev/null +++ b/tests/unit/computations/test_datasets.py @@ -0,0 +1,70 @@ +"""module contains tests from datasets""" +from unittest import TestCase +from unittest import mock + +from gn3.computations.datasets import retrieve_trait_sample_data +from gn3.computations.datasets import get_query_for_dataset_sample +from gn3.computations.datasets import fetch_from_db_sample_data + + +class TestDatasets(TestCase): + """class contains tests for datasets""" + + @mock.patch("gn3.computations.datasets.fetch_from_db_sample_data") + def test_retrieve_trait_sample_data(self, mock_fetch_sample_results): + """test retrieving sample data\ + for trait from the dataset""" + trait_name = "1419792_at" + dataset_id = "HC_M2_0606_P&" + dataset_type = "Publish" + + fetch_results = [('BXD32', 8.001, None, None, 'BXD32')] + + mock_fetch_sample_results.return_value = fetch_results + + results = retrieve_trait_sample_data( + dataset_id, dataset_type, trait_name) + self.assertEqual(mock_fetch_sample_results.call_count, 1) + self.assertEqual(results, fetch_results) + + def test_query_for_dataset_sample(self): + """test for getting query for sample data""" + + no_results = get_query_for_dataset_sample("does not exists") + + query_exists = get_query_for_dataset_sample("Publish") + + self.assertEqual(no_results, None) + self.assertIsInstance(query_exists, str) + + def test_fetch_from_db_sample_data(self): + """test for function that fetches sample\ + results from the database""" + + database_results = [('BXD31', 8.001, None, None, 'BXD31'), + ('BXD32', 7.884, None, None, 'BXD32'), + ('BXD42', 7.682, None, None, 'BXD42'), + ('BXD42', 7.682, None, None, 'BXD42'), + ('BXD40', 7.945, None, None, 'BXD40'), + ('BXD43', 7.873, None, None, 'BXD43') + ] + + database = mock.Mock() + db_cursor = mock.Mock() + db_cursor.execute.return_value = 6 + db_cursor.fetchall.return_value = database_results + database.cursor.return_value = db_cursor + + mock_pheno_query = """ + SELECT + Strain.Name, PublishData.value, PublishSE.error,NStrain.count, Strain.Name2 + WHERE + PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = 1419792_at AND + PublishFreeze.Id = '12' AND PublishData.StrainId = Strain.Id + Order BY + Strain.Name + """ + fetch_results = fetch_from_db_sample_data(mock_pheno_query, database) + + self.assertEqual(fetch_results, database_results) |