From 6494a093e6d96015f65416a8c3fde88a9a3c8c71 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Wed, 15 Sep 2021 16:17:02 +0300 Subject: init wgcna file to run r script and preprocess data --- gn3/computations/wgcna.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 gn3/computations/wgcna.py (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From a3ef298678b32ee83cac7bd7462d3d92f8eaec26 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 08:23:35 +0300 Subject: function to parse form data and write to json file --- gn3/computations/wgcna.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index e69de29..3819b62 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -0,0 +1,18 @@ +"""module contains code to preprocess and call wgcna script""" + +import os +import json +import uuid +from gn3.settings import TMPDIR + + +def dump_wgcna_data(request_data): + """function to dump request data to json file""" + filename = f"{str(uuid.uuid4())}.json" + + temp_file_path = os.path.join(TMPDIR, filename) + + with open(temp_file_path, "w") as output_file: + json.dump(request_data, output_file) + + return temp_file_path -- cgit v1.2.3 From 38cbc95f216548d4a2f46b8e25cd328ff8c52d62 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 08:54:01 +0300 Subject: add function to compose and run wgcna script --- gn3/computations/wgcna.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index 3819b62..bcd3a0a 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -5,8 +5,10 @@ import json import uuid from gn3.settings import TMPDIR +from gn3.commands import run_cmd -def dump_wgcna_data(request_data): + +def dump_wgcna_data(request_data: dict): """function to dump request data to json file""" filename = f"{str(uuid.uuid4())}.json" @@ -16,3 +18,16 @@ def dump_wgcna_data(request_data): json.dump(request_data, output_file) return temp_file_path + + +def compose_wgcna_cmd(rscript_path: str, temp_file_path: str): + """function to componse wgcna cmd""" + cmd = f"Rscript {rscript_path} {temp_file_path}" + return cmd + + +def call_wgcna_script(rscript_path: str, request_data: dict): + """function to call wgcna script""" + generated_file = dump_wgcna_data(request_data) + cmd = compose_gemma_cmd(rscript_path, generated_file) + run_cmd(cmd=cmd) -- cgit v1.2.3 From 8854e3070f32bed95cb489eb36e7f258c02ec46e Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 09:32:42 +0300 Subject: add initial endpoint for wgcna --- gn3/api/wgcna.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 gn3/api/wgcna.py (limited to 'gn3') diff --git a/gn3/api/wgcna.py b/gn3/api/wgcna.py new file mode 100644 index 0000000..a3bacdd --- /dev/null +++ b/gn3/api/wgcna.py @@ -0,0 +1,13 @@ +"""endpoint to run wgcna analysis""" +from flask import Blueprint +from flask import request + +wgcna = Blueprint("wgcna", __name__) + + +@wgcna.route("/run_wgcna", methods=["POST"]) +def run_wgcna(): + + _wgcna_data = request.json + + return "success", 200 -- cgit v1.2.3 From 6f9fd1a1866e292ec4b84c214efd61ccb9f5bb58 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 09:45:16 +0300 Subject: register wgcna blueprint --- gn3/app.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gn3') diff --git a/gn3/app.py b/gn3/app.py index 046b5de..b2f77f9 100644 --- a/gn3/app.py +++ b/gn3/app.py @@ -9,6 +9,7 @@ from gn3.api.rqtl import rqtl from gn3.api.general import general from gn3.api.correlation import correlation from gn3.api.data_entry import data_entry +from gn3.api.wgcna import wgcna def create_app(config: Union[Dict, str, None] = None) -> Flask: @@ -32,4 +33,5 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask: app.register_blueprint(rqtl, url_prefix="/api/rqtl") app.register_blueprint(correlation, url_prefix="/api/correlation") app.register_blueprint(data_entry, url_prefix="/api/dataentry") + app.register_blueprint(wgcna, url_prefix="/api/wgcna") return app -- cgit v1.2.3 From 3d8f7f069d76cd6ee35b5e9d72d37e38721188d2 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 10:13:10 +0300 Subject: run cmd and add exception handler --- gn3/computations/wgcna.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index bcd3a0a..f0f0fa2 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -29,5 +29,9 @@ def compose_wgcna_cmd(rscript_path: str, temp_file_path: str): def call_wgcna_script(rscript_path: str, request_data: dict): """function to call wgcna script""" generated_file = dump_wgcna_data(request_data) - cmd = compose_gemma_cmd(rscript_path, generated_file) - run_cmd(cmd=cmd) + cmd = compose_wgcna_cmd(rscript_path, generated_file) + + try: + return run_cmd(cmd) + except Exception as error: + raise error -- cgit v1.2.3 From f53a8a98206b4e8aedbf4b86e49f41ea140c9c6a Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 14:02:13 +0300 Subject: pass user input to call script --- gn3/api/wgcna.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'gn3') diff --git a/gn3/api/wgcna.py b/gn3/api/wgcna.py index a3bacdd..5d49493 100644 --- a/gn3/api/wgcna.py +++ b/gn3/api/wgcna.py @@ -1,13 +1,21 @@ """endpoint to run wgcna analysis""" from flask import Blueprint from flask import request +from flask import current_app + +from gn3.computations.wgcna import call_wgcna_script wgcna = Blueprint("wgcna", __name__) @wgcna.route("/run_wgcna", methods=["POST"]) def run_wgcna(): + """run wgcna:output should be a json with a the data""" + + wgcna_data = request.json + + wgcna_script = current_app.config["WGCNA_RSCRIPT"] - _wgcna_data = request.json + results = call_wgcna_script(wgcna_script, wgcna_data) - return "success", 200 + return results, 200 -- cgit v1.2.3 From e8f3a7131e8f1fcaf803dedab3177374f46e3709 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 16 Sep 2021 14:04:23 +0300 Subject: Add WGCNA_SCRIT env to settings --- gn3/settings.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gn3') diff --git a/gn3/settings.py b/gn3/settings.py index f4866d5..d3d7ea0 100644 --- a/gn3/settings.py +++ b/gn3/settings.py @@ -24,3 +24,6 @@ GN2_BASE_URL = "http://www.genenetwork.org/" # biweight script BIWEIGHT_RSCRIPT = "~/genenetwork3/scripts/calculate_biweight.R" + +# wgcna script +WGCNA_RSCRIPT = "wgcna_analysis.R" -- cgit v1.2.3 From 6f7c60e6c438ada9a3b9032c8e4509351384e04f Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Wed, 22 Sep 2021 00:49:22 +0300 Subject: jsonify results --- gn3/api/wgcna.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/api/wgcna.py b/gn3/api/wgcna.py index 5d49493..89784c4 100644 --- a/gn3/api/wgcna.py +++ b/gn3/api/wgcna.py @@ -2,6 +2,7 @@ from flask import Blueprint from flask import request from flask import current_app +from flask import jsonify from gn3.computations.wgcna import call_wgcna_script @@ -18,4 +19,4 @@ def run_wgcna(): results = call_wgcna_script(wgcna_script, wgcna_data) - return results, 200 + return jsonify(results), 200 -- cgit v1.2.3 From 1c392b4b2786d196af6b882e80270b8cb839f554 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 23 Sep 2021 15:38:50 +0300 Subject: read generated files from r and return output --- gn3/computations/wgcna.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index f0f0fa2..689bc2d 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -22,7 +22,7 @@ def dump_wgcna_data(request_data: dict): def compose_wgcna_cmd(rscript_path: str, temp_file_path: str): """function to componse wgcna cmd""" - cmd = f"Rscript {rscript_path} {temp_file_path}" + cmd = f"Rscript ./scripts/{rscript_path} {temp_file_path}" return cmd @@ -32,6 +32,9 @@ def call_wgcna_script(rscript_path: str, request_data: dict): cmd = compose_wgcna_cmd(rscript_path, generated_file) try: - return run_cmd(cmd) + + run_cmd(cmd) + with open(generated_file, "r") as outputfile: + return results except Exception as error: raise error -- cgit v1.2.3 From 0ca5f01422883d97e42dd37e59ffacdcf9a65af9 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 23 Sep 2021 15:58:25 +0300 Subject: Return r error if returncode!=0 --- gn3/computations/wgcna.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index 689bc2d..e9b76e8 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -22,6 +22,7 @@ def dump_wgcna_data(request_data: dict): def compose_wgcna_cmd(rscript_path: str, temp_file_path: str): """function to componse wgcna cmd""" + # (todo):issue relative paths to abs paths cmd = f"Rscript ./scripts/{rscript_path} {temp_file_path}" return cmd @@ -33,8 +34,16 @@ def call_wgcna_script(rscript_path: str, request_data: dict): try: - run_cmd(cmd) + run_cmd_results = run_cmd(cmd) + with open(generated_file, "r") as outputfile: - return results + + if run_cmd_results["code"] != 0: + return run_cmd_results + return { + "data": json.load(outputfile), + **run_cmd_results + } + # return json.load(outputfile) except Exception as error: raise error -- cgit v1.2.3 From 62a5047be6cee5f692d44f97410cab11a01e3396 Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Thu, 23 Sep 2021 16:05:17 +0300 Subject: minor fixes for endpoint --- gn3/api/wgcna.py | 3 +++ gn3/computations/wgcna.py | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/api/wgcna.py b/gn3/api/wgcna.py index 89784c4..fa044cf 100644 --- a/gn3/api/wgcna.py +++ b/gn3/api/wgcna.py @@ -19,4 +19,7 @@ def run_wgcna(): results = call_wgcna_script(wgcna_script, wgcna_data) + if results.get("data") is None: + return jsonify(results), 401 + return jsonify(results), 200 diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index e9b76e8..436a888 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -44,6 +44,5 @@ def call_wgcna_script(rscript_path: str, request_data: dict): "data": json.load(outputfile), **run_cmd_results } - # return json.load(outputfile) except Exception as error: raise error -- cgit v1.2.3 From f51a09e2c5f594425531fe26e8237b09fb6909ae Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Mon, 27 Sep 2021 17:29:27 +0300 Subject: return str error for exception --- gn3/computations/wgcna.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index 436a888..1ee55f5 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -45,4 +45,7 @@ def call_wgcna_script(rscript_path: str, request_data: dict): **run_cmd_results } except Exception as error: - raise error + # relook at handling errors gn3 + return { + "output": str(error) + } -- cgit v1.2.3 From b6097cff6431e50c132f46dc0d3e1b841897da0f Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Mon, 27 Sep 2021 19:14:58 +0300 Subject: add file not found exception --- gn3/computations/wgcna.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gn3') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index 1ee55f5..fd508fa 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -44,8 +44,8 @@ def call_wgcna_script(rscript_path: str, request_data: dict): "data": json.load(outputfile), **run_cmd_results } - except Exception as error: + except FileNotFoundError: # relook at handling errors gn3 return { - "output": str(error) + "output": "output file not found" } -- cgit v1.2.3