about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-07-28 10:19:54 +0300
committerFrederick Muriuki Muriithi2022-07-28 10:19:54 +0300
commit921b5fe752e4a947595a86572592d5bbdeb16ead (patch)
tree4d9a62be2bcadd2029f9d3cceb4c9e97373c7d33
parent3c8da2cae39efc25b320b78e2a1ed16afc1c5b8a (diff)
downloadgenenetwork3-921b5fe752e4a947595a86572592d5bbdeb16ead.tar.gz
Add command to run the sample correlations in an external process
-rw-r--r--gn3/api/correlation.py14
-rw-r--r--gn3/commands.py25
2 files changed, 31 insertions, 8 deletions
diff --git a/gn3/api/correlation.py b/gn3/api/correlation.py
index 1667302..6e70899 100644
--- a/gn3/api/correlation.py
+++ b/gn3/api/correlation.py
@@ -9,13 +9,13 @@ from flask import request
 from flask import current_app
 
 from gn3.settings import SQL_URI
-from gn3.commands import run_async_cmd, compose_pcorrs_command
 from gn3.db_utils import database_connector
+from gn3.commands import run_sample_corr_cmd
 from gn3.responses.pcorrs_responses import build_response
+from gn3.commands import run_async_cmd, compose_pcorrs_command
 from gn3.computations.correlations import map_shared_keys_to_values
 from gn3.computations.correlations import compute_tissue_correlation
 from gn3.computations.correlations import compute_all_lit_correlation
-from gn3.computations.correlations import compute_all_sample_correlation
 
 correlation = Blueprint("correlation", __name__)
 
@@ -31,9 +31,8 @@ def compute_sample_integration(corr_method="pearson"):
     this_trait_data = correlation_input.get("trait_data")
 
     results = map_shared_keys_to_values(target_samplelist, target_data_values)
-    correlation_results = compute_all_sample_correlation(corr_method=corr_method,
-                                                         this_trait=this_trait_data,
-                                                         target_dataset=results)
+    correlation_results = run_sample_corr_cmd(
+        corr_method, this_trait_data, results)
 
     return jsonify(correlation_results)
 
@@ -50,9 +49,8 @@ def compute_sample_r(corr_method="pearson"):
     this_trait_data = correlation_input.get("this_trait")
     target_dataset_data = correlation_input.get("target_dataset")
 
-    correlation_results = compute_all_sample_correlation(corr_method=corr_method,
-                                                         this_trait=this_trait_data,
-                                                         target_dataset=target_dataset_data)
+    correlation_results = run_sample_corr_cmd(
+        corr_method, this_trait_data, target_dataset_data)
 
     return jsonify({
         "corr_results": correlation_results
diff --git a/gn3/commands.py b/gn3/commands.py
index 0b8db5b..d45d9e8 100644
--- a/gn3/commands.py
+++ b/gn3/commands.py
@@ -2,6 +2,8 @@
 commands"""
 import sys
 import json
+import pickle
+import tempfile
 import subprocess
 
 from datetime import datetime
@@ -14,6 +16,7 @@ from typing import Sequence
 from uuid import uuid4
 from redis.client import Redis  # Used only in type hinting
 
+from gn3.random import random_string
 from gn3.exceptions import RedisConnectionError
 
 
@@ -120,6 +123,28 @@ Returns the name of the specific redis hash for the specific task.
         conn.hset(name=unique_id, key="env", value=json.dumps(env))
     return unique_id
 
+def run_sample_corr_cmd(method, this_trait_data, target_dataset_data):
+    "Run the sample correlations in an external process, returning the results."
+    with tempfile.TemporaryDirectory() as tempdir:
+        traitfile = f"{tempdir}/traitfile_{random_string(10)}"
+        targetfile = f"{tempdir}/targetdb_{random_string(10)}"
+        destfile = f"{tempdir}/corrs_{random_string(10)}"
+        with open(traitfile, "wb") as trtfl:
+            pickle.dump(this_trait_data, trtfl)
+
+        with open(targetfile, "wb") as targfl:
+            pickle.dump(target_dataset_data, targfl)
+
+            subprocess.run(
+                ["python3", "-m", "scripts.sample_correlations", method,
+                 traitfile, targetfile, destfile],
+                check=True)
+
+            with open(destfile, "rb") as dstfl:
+                correlation_results = pickle.load(dstfl)
+
+    return correlation_results
+
 def run_cmd(cmd: str, success_codes: Tuple = (0,), env: str = None) -> Dict:
     """Run CMD and return the CMD's status code and output as a dict"""
     parsed_cmd = json.loads(cmd)