aboutsummaryrefslogtreecommitdiff
path: root/gn3/api/correlation.py
diff options
context:
space:
mode:
authorAlexander Kabui2021-03-16 10:36:58 +0300
committerGitHub2021-03-16 10:36:58 +0300
commit43d1bb7f6cd2b5890d5b3eb7c357caafda25a35c (patch)
tree73683272f32cffc860497a93b5c844c272252e67 /gn3/api/correlation.py
parent995f1dbd081eb64ad177f929615a4edee01cb68f (diff)
downloadgenenetwork3-43d1bb7f6cd2b5890d5b3eb7c357caafda25a35c.tar.gz
Refactor/clean up correlations (#4)
* initial commit for Refactor/clean-up-correlation * add python scipy dependency * initial commit for sample correlation * initial commit for sample correlation endpoint * initial commit for integration and unittest * initial commit for registering correlation blueprint * add and modify unittest and integration tests for correlation * Add compute compute_all_sample_corr method for correlation * add scipy to requirement txt file * add tissue correlation for trait list * add unittest for tissue correlation * add lit correlation for trait list * add unittests for lit correlation for trait list * modify lit correlarion for trait list * add unittests for lit correlation for trait list * add correlation metho in dynamic url * add file format for expected structure input while doing sample correlation * modify input data structure -> add trait id * update tests for sample r correlation * add compute all lit correlation method * add endpoint for computing lit_corr * add unit and integration tests for computing lit corr * add /api/correlation/tissue_corr/{corr_method} endpoint for tissue correlation * add unittest and integration tests for tissue correlation Co-authored-by: BonfaceKilz <bonfacemunyoki@gmail.com>
Diffstat (limited to 'gn3/api/correlation.py')
-rw-r--r--gn3/api/correlation.py77
1 files changed, 48 insertions, 29 deletions
diff --git a/gn3/api/correlation.py b/gn3/api/correlation.py
index 217b7ce..56b8381 100644
--- a/gn3/api/correlation.py
+++ b/gn3/api/correlation.py
@@ -1,44 +1,63 @@
-"""Endpoints for computing correlation"""
-import time
-from flask import Blueprint
+"""Endpoints for running correlations"""
+from unittest import mock
+
from flask import jsonify
+from flask import Blueprint
from flask import request
-from flask import g
-from sqlalchemy import create_engine
-from default_settings import SQL_URI
-from gn3.correlation.correlation_computations import compute_correlation
+from gn3.computations.correlations import compute_all_sample_correlation
+from gn3.computations.correlations import compute_all_lit_correlation
+from gn3.computations.correlations import compute_all_tissue_correlation
+
correlation = Blueprint("correlation", __name__)
-# xtodo implement neat db setup
-@correlation.before_request
-def connect_db():
- """add connection to db method"""
- print("@app.before_request connect_db")
- db_connection = getattr(g, '_database', None)
- if db_connection is None:
- print("Get new database connector")
- g.db = g._database = create_engine(SQL_URI, encoding="latin1")
+@correlation.route("/sample_r/<string:corr_method>", methods=["POST"])
+def compute_sample_r(corr_method="pearson"):
+ """correlation endpoint for computing sample r correlations\
+ api expects the trait data with has the trait and also the\
+ target_dataset data"""
+ correlation_input = request.get_json()
+
+ # xtodo move code below to compute_all_sampl correlation
+ this_trait_data = correlation_input.get("this_trait")
+ target_datasets = correlation_input.get("target_dataset")
+
+ correlation_results = compute_all_sample_correlation(corr_method=corr_method,
+ this_trait=this_trait_data,
+ target_dataset=target_datasets)
+
+ return jsonify({
+ "corr_results": correlation_results
+ })
+
- g.initial_time = time.time()
+@correlation.route("/lit_corr/<string:species>/<int:gene_id>", methods=["POST"])
+def compute_lit_corr(species=None, gene_id=None):
+ """api endpoint for doing lit correlation.results for lit correlation\
+ are fetched from the database this is the only case where the db\
+ might be needed for actual computing of the correlation results"""
+ database_instance = mock.Mock()
+ target_traits_gene_ids = request.get_json()
-@correlation.route("/corr_compute", methods=["POST"])
-def corr_compute_page():
- """api for doing correlation"""
+ lit_corr_results = compute_all_lit_correlation(
+ database_instance=database_instance, trait_lists=target_traits_gene_ids,
+ species=species, gene_id=gene_id)
- correlation_input = request.json
+ return jsonify(lit_corr_results)
- if correlation_input is None:
- return jsonify({"error": str("Bad request")}), 400
- try:
- corr_results = compute_correlation(
- correlation_input_data=correlation_input)
+@correlation.route("/tissue_corr/<string:corr_method>", methods=["POST"])
+def compute_tissue_corr(corr_method="pearson"):
+ """api endpoint fr doing tissue correlation"""
+ tissue_input_data = request.get_json()
+ primary_tissue_dict = tissue_input_data["primary_tissue"]
+ target_tissues_dict_list = tissue_input_data["target_tissues"]
- except Exception as error: # pylint: disable=broad-except
- return jsonify({"error": str(error)})
+ results = compute_all_tissue_correlation(primary_tissue_dict=primary_tissue_dict,
+ target_tissues_dict_list=target_tissues_dict_list,
+ corr_method=corr_method)
- return {"correlation_results": corr_results}
+ return jsonify(results) \ No newline at end of file