about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--gn3/api/correlation.py6
-rw-r--r--gn3/api/ctl.py5
-rw-r--r--gn3/api/wgcna.py4
-rw-r--r--gn3/computations/ctl.py8
-rw-r--r--gn3/computations/partial_correlations.py14
-rw-r--r--gn3/computations/qtlreaper.py9
-rw-r--r--gn3/computations/rust_correlation.py19
-rw-r--r--gn3/computations/wgcna.py11
-rw-r--r--gn3/db/traits.py3
-rw-r--r--gn3/heatmaps.py1
-rw-r--r--tests/integration/test_partial_correlations.py2
-rw-r--r--tests/unit/computations/test_wgcna.py9
12 files changed, 51 insertions, 40 deletions
diff --git a/gn3/api/correlation.py b/gn3/api/correlation.py
index 6af96d7..3667a24 100644
--- a/gn3/api/correlation.py
+++ b/gn3/api/correlation.py
@@ -9,7 +9,6 @@ from flask import Blueprint
 from flask import request
 from flask import current_app
 
-from gn3.settings import SQL_URI
 from gn3.db_utils import database_connection
 from gn3.commands import run_sample_corr_cmd
 from gn3.responses.pcorrs_responses import build_response
@@ -144,7 +143,10 @@ def partial_correlation():
             cmd=command,
             job_queue=compute_job_queue(current_app),
             options={
-                "env": {"PYTHONPATH": ":".join(sys.path), "SQL_URI": SQL_URI},
+                "env": {
+                    "PYTHONPATH": ":".join(sys.path),
+                    "SQL_URI": current_app.config["SQL_URI"]
+                },
             },
             log_level=logging.getLevelName(
                 current_app.logger.getEffectiveLevel()).lower())
diff --git a/gn3/api/ctl.py b/gn3/api/ctl.py
index ac33d63..39c286f 100644
--- a/gn3/api/ctl.py
+++ b/gn3/api/ctl.py
@@ -2,7 +2,7 @@
 
 from flask import Blueprint
 from flask import request
-from flask import jsonify
+from flask import jsonify, current_app
 
 from gn3.computations.ctl import call_ctl_script
 
@@ -18,7 +18,8 @@ def run_ctl():
     """
     ctl_data = request.json
 
-    (cmd_results, response) = call_ctl_script(ctl_data)
+    (cmd_results, response) = call_ctl_script(
+        ctl_data, current_app.config["TMPDIR"])
     return (jsonify({
         "results": response
     }), 200) if response is not None else (jsonify({"error": str(cmd_results)}), 401)
diff --git a/gn3/api/wgcna.py b/gn3/api/wgcna.py
index fa044cf..5468a2e 100644
--- a/gn3/api/wgcna.py
+++ b/gn3/api/wgcna.py
@@ -17,7 +17,9 @@ def run_wgcna():
 
     wgcna_script = current_app.config["WGCNA_RSCRIPT"]
 
-    results = call_wgcna_script(wgcna_script, wgcna_data)
+    results = call_wgcna_script(wgcna_script,
+                                wgcna_data,
+                                current_app.config["TMPDIR"])
 
     if results.get("data") is None:
         return jsonify(results), 401
diff --git a/gn3/computations/ctl.py b/gn3/computations/ctl.py
index f881410..5c004ea 100644
--- a/gn3/computations/ctl.py
+++ b/gn3/computations/ctl.py
@@ -6,13 +6,11 @@ from gn3.computations.wgcna import dump_wgcna_data
 from gn3.computations.wgcna import compose_wgcna_cmd
 from gn3.computations.wgcna import process_image
 
-from gn3.settings import TMPDIR
 
-
-def call_ctl_script(data):
+def call_ctl_script(data, tmpdir):
     """function to call ctl script"""
-    data["imgDir"] = TMPDIR
-    temp_file_name = dump_wgcna_data(data)
+    data["imgDir"] = tmpdir
+    temp_file_name = dump_wgcna_data(data, tmpdir)
     cmd = compose_wgcna_cmd("ctl_analysis.R", temp_file_name)
 
     cmd_results = run_cmd(cmd)
diff --git a/gn3/computations/partial_correlations.py b/gn3/computations/partial_correlations.py
index 6eb6c2f..8674910 100644
--- a/gn3/computations/partial_correlations.py
+++ b/gn3/computations/partial_correlations.py
@@ -16,7 +16,6 @@ import pandas
 import pingouin
 from scipy.stats import pearsonr, spearmanr
 
-from gn3.settings import TEXTDIR
 from gn3.chancy import random_string
 from gn3.function_helpers import  compose
 from gn3.data_helpers import parse_csv_line
@@ -668,9 +667,14 @@ def check_for_common_errors(# pylint: disable=[R0914]
     return non_error_result
 
 def partial_correlations_with_target_db(# pylint: disable=[R0913, R0914, R0911 too-many-positional-arguments]
-        conn: Any, primary_trait_name: str,
-        control_trait_names: Tuple[str, ...], method: str,
-        criteria: int, target_db_name: str) -> dict:
+        conn: Any,
+        primary_trait_name: str,
+        control_trait_names: Tuple[str, ...],
+        method: str,
+        criteria: int,
+        target_db_name: str,
+        textdir: str
+) -> dict:
     """
     This is the 'ochestration' function for the partial-correlation feature.
 
@@ -755,7 +759,7 @@ def partial_correlations_with_target_db(# pylint: disable=[R0913, R0914, R0911 t
         threshold,
         conn)
 
-    database_filename = get_filename(conn, target_db_name, TEXTDIR)
+    database_filename = get_filename(conn, target_db_name, textdir)
     all_correlations = partial_corrs(
         conn, check_res["common_primary_control_samples"],
         check_res["fixed_primary_values"], check_res["fixed_control_values"],
diff --git a/gn3/computations/qtlreaper.py b/gn3/computations/qtlreaper.py
index e0d9d0b..ff83b33 100644
--- a/gn3/computations/qtlreaper.py
+++ b/gn3/computations/qtlreaper.py
@@ -7,7 +7,6 @@ import subprocess
 from typing import Union
 
 from gn3.chancy import random_string
-from gn3.settings import TMPDIR
 
 def generate_traits_file(samples, trait_values, traits_filename):
     """
@@ -41,10 +40,12 @@ def create_output_directory(path: str):
 # pylint: disable=[too-many-arguments, too-many-positional-arguments]
 def run_reaper(
         reaper_cmd: str,
-        genotype_filename: str, traits_filename: str,
+        genotype_filename: str,
+        traits_filename: str,
+        output_dir: str,
         other_options: tuple = ("--n_permutations", "1000"),
-        separate_nperm_output: bool = False,
-        output_dir: str = TMPDIR):
+        separate_nperm_output: bool = False
+):
     """
     Run the QTLReaper command to compute the QTLs.
 
diff --git a/gn3/computations/rust_correlation.py b/gn3/computations/rust_correlation.py
index d1ad3bc..359b73a 100644
--- a/gn3/computations/rust_correlation.py
+++ b/gn3/computations/rust_correlation.py
@@ -13,11 +13,10 @@ from flask import current_app
 
 from gn3.computations.qtlreaper import create_output_directory
 from gn3.chancy import random_string
-from gn3.settings import TMPDIR
 
 
-def generate_input_files(dataset: list[str],
-                         output_dir: str = TMPDIR) -> tuple[str, str]:
+def generate_input_files(
+        dataset: list[str], output_dir: str) -> tuple[str, str]:
     """function generates outputfiles and inputfiles"""
     tmp_dir = f"{output_dir}/correlation"
     create_output_directory(tmp_dir)
@@ -50,17 +49,23 @@ def generate_json_file(
 
 
 def run_correlation(
-        dataset, trait_vals: str, method: str, delimiter: str,
-        corr_type: str = "sample", top_n: int = 500):
+        dataset,
+        trait_vals: str,
+        method: str,
+        delimiter: str,
+        tmpdir: str,
+        corr_type: str = "sample",
+        top_n: int = 500
+):
     """entry function to call rust correlation"""
 
     # pylint: disable=[too-many-arguments, too-many-positional-arguments]
     correlation_command = current_app.config["CORRELATION_COMMAND"] # make arg?
-    (tmp_dir, tmp_file) = generate_input_files(dataset)
+    (tmp_dir, tmp_file) = generate_input_files(dataset, tmpdir)
     (output_file, json_file) = generate_json_file(
         tmp_dir=tmp_dir, tmp_file=tmp_file, method=method, delimiter=delimiter,
         x_vals=trait_vals)
-    command_list = [correlation_command, json_file, TMPDIR]
+    command_list = [correlation_command, json_file, tmpdir]
     try:
         subprocess.run(command_list, check=True, capture_output=True)
     except subprocess.CalledProcessError as cpe:
diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py
index d1f7b32..3229a0e 100644
--- a/gn3/computations/wgcna.py
+++ b/gn3/computations/wgcna.py
@@ -7,17 +7,16 @@ import subprocess
 from pathlib import Path
 
 
-from gn3.settings import TMPDIR
 from gn3.commands import run_cmd
 
 
-def dump_wgcna_data(request_data: dict):
+def dump_wgcna_data(request_data: dict, tmpdir: str):
     """function to dump request data to json file"""
     filename = f"{str(uuid.uuid4())}.json"
 
-    temp_file_path = os.path.join(TMPDIR, filename)
+    temp_file_path = os.path.join(tmpdir, filename)
 
-    request_data["TMPDIR"] = TMPDIR
+    request_data["TMPDIR"] = tmpdir
 
     with open(temp_file_path, "w", encoding="utf-8") as output_file:
         json.dump(request_data, output_file)
@@ -65,9 +64,9 @@ def compose_wgcna_cmd(rscript_path: str, temp_file_path: str):
     return cmd
 
 
-def call_wgcna_script(rscript_path: str, request_data: dict):
+def call_wgcna_script(rscript_path: str, request_data: dict, tmpdir: str):
     """function to call wgcna script"""
-    generated_file = dump_wgcna_data(request_data)
+    generated_file = dump_wgcna_data(request_data, tmpdir)
     cmd = compose_wgcna_cmd(rscript_path, generated_file)
 
     # stream_cmd_output(request_data, cmd)  disable streaming of data
diff --git a/gn3/db/traits.py b/gn3/db/traits.py
index fa13fcc..fbac0da 100644
--- a/gn3/db/traits.py
+++ b/gn3/db/traits.py
@@ -3,7 +3,6 @@ import os
 from functools import reduce
 from typing import Any, Dict, Sequence
 
-from gn3.settings import TMPDIR
 from gn3.chancy import random_string
 from gn3.function_helpers import compose
 from gn3.db.datasets import retrieve_trait_dataset
@@ -690,7 +689,7 @@ def retrieve_trait_data(trait: dict, conn: Any, samplelist: Sequence[str] = tupl
     return {}
 
 
-def generate_traits_filename(base_path: str = TMPDIR):
+def generate_traits_filename(base_path: str):
     """Generate a unique filename for use with generated traits files."""
     return (
         f"{os.path.abspath(base_path)}/traits_test_file_{random_string(10)}.txt")
diff --git a/gn3/heatmaps.py b/gn3/heatmaps.py
index 511996a..80c38e8 100644
--- a/gn3/heatmaps.py
+++ b/gn3/heatmaps.py
@@ -145,6 +145,7 @@ def build_heatmap(
         app.config['REAPER_COMMAND'],
         genotype_filename,
         traits_filename,
+        output_dir=app.config["TMPDIR"],
         separate_nperm_output=True
     )
 
diff --git a/tests/integration/test_partial_correlations.py b/tests/integration/test_partial_correlations.py
index fc9f64f..56af260 100644
--- a/tests/integration/test_partial_correlations.py
+++ b/tests/integration/test_partial_correlations.py
@@ -221,4 +221,4 @@ def test_part_corr_api_with_mix_of_existing_and_non_existing_control_traits(
     criteria = 10
     with pytest.warns(UserWarning):
         partial_correlations_with_target_db(
-            db_conn, primary, controls, method, criteria, target)
+            db_conn, primary, controls, method, criteria, target, "/tmp")
diff --git a/tests/unit/computations/test_wgcna.py b/tests/unit/computations/test_wgcna.py
index 55432af..acba10e 100644
--- a/tests/unit/computations/test_wgcna.py
+++ b/tests/unit/computations/test_wgcna.py
@@ -85,9 +85,9 @@ class TestWgcna(TestCase):
             mock_img.return_value = b"AFDSFNBSDGJJHH"
 
             results = call_wgcna_script(
-                "Rscript/GUIX_PATH/scripts/r_file.R", request_data)
+                "Rscript/GUIX_PATH/scripts/r_file.R", request_data, "/tmp")
 
-            mock_dumping_data.assert_called_once_with(request_data)
+            mock_dumping_data.assert_called_once_with(request_data, "/tmp")
 
             mock_compose_wgcna.assert_called_once_with(
                 "Rscript/GUIX_PATH/scripts/r_file.R",
@@ -119,7 +119,7 @@ class TestWgcna(TestCase):
 
             mock_run_cmd.return_value = expected_error
             self.assertEqual(call_wgcna_script(
-                "input_file.R", ""), expected_error)
+                "input_file.R", "", "/tmp"), expected_error)
 
     @pytest.mark.skip(
         "This test assumes that the system will always be invoked from the root"
@@ -166,8 +166,7 @@ class TestWgcna(TestCase):
 
             file_name_generator.return_value = "facb73ff-7eef-4053-b6ea-e91d3a22a00c"
 
-            results = dump_wgcna_data(
-                expected_input)
+            results = dump_wgcna_data(expected_input, "/tmp")
 
             file_handler.assert_called_once_with(
                 "/tmp/facb73ff-7eef-4053-b6ea-e91d3a22a00c.json", 'w', encoding='utf-8')