diff options
author | Pjotr Prins | 2024-04-02 02:24:36 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-09-12 07:40:03 -0500 |
commit | 8fa885e66c9e31fc1b0a5329c6b061cf017995c8 (patch) | |
tree | cb63ec303f5ba0e90c46a958da732d1c714398f8 /gn3 | |
parent | 439b430d69bece15a503ab5ea5ea2435e1e5f6fb (diff) | |
download | genenetwork3-8fa885e66c9e31fc1b0a5329c6b061cf017995c8.tar.gz |
Make sure TMPDIR directories exist!
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/api/rqtl.py | 2 | ||||
-rw-r--r-- | gn3/app.py | 4 | ||||
-rw-r--r-- | gn3/computations/rqtl.py | 40 | ||||
-rw-r--r-- | gn3/fs_helpers.py | 14 |
4 files changed, 35 insertions, 25 deletions
diff --git a/gn3/api/rqtl.py b/gn3/api/rqtl.py index 36bc3da..bd50900 100644 --- a/gn3/api/rqtl.py +++ b/gn3/api/rqtl.py @@ -9,7 +9,7 @@ from flask import request from gn3.debug import __pk__ from gn3.computations.rqtl import generate_rqtl_cmd, process_rqtl_mapping, \ process_rqtl_pairscan, process_perm_output -from gn3.fs_helpers import assert_paths_exist +from gn3.fs_helpers import assert_paths_exist, get_tmpdir rqtl = Blueprint("rqtl", __name__) @@ -28,6 +28,7 @@ from gn3.api.sampledata import sampledata from gn3.api.llm import gnqa from gn3.auth import oauth2 from gn3.case_attributes import caseattr +from gn3.fs_helpers import get_tmpdir def create_app(config: Union[Dict, str, None] = None) -> Flask: @@ -57,6 +58,9 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask: logging.info("Guix Profile: '%s'.", os.environ.get("GUIX_PROFILE")) logging.info("Python Executable: '%s'.", sys.executable) + if "TMPDIR" in os.environ: + app.config.from_envvar('TMPDIR') + CORS( app, origins=app.config["CORS_ORIGINS"], diff --git a/gn3/computations/rqtl.py b/gn3/computations/rqtl.py index 8b1b316..b53efbd 100644 --- a/gn3/computations/rqtl.py +++ b/gn3/computations/rqtl.py @@ -11,7 +11,7 @@ from flask import current_app from gn3.commands import compose_rqtl_cmd from gn3.computations.gemma import generate_hash_of_string -from gn3.fs_helpers import get_hash_of_files, assert_path_exists +from gn3.fs_helpers import get_hash_of_files, assert_path_exists, get_tmpdir from gn3.debug import __pk__ @@ -68,13 +68,11 @@ def process_rqtl_mapping(file_name: str) -> List: # Later I should probably redo this using csv.read to avoid the # awkwardness with removing quotes with [1:-1] - with open( - os.path.join( - current_app.config.get("TMPDIR", "/tmp"), "output", file_name - ), - "r", - encoding="utf-8", - ) as the_file: + outdir = os.path.join(get_tmpdir(),"output") + if not os.path.isdir(outdir): + os.mkdir(outdir) + + with open( os.path.join(outdir,file_name),"r",encoding="utf-8") as the_file: for line in the_file: line_items = line.split(",") if line_items[1][1:-1] == "chr" or not line_items: @@ -115,13 +113,11 @@ def pairscan_for_figure(file_name: str) -> Dict: figure_data = {} # Open the file with the actual results, written as a list of lists - with open( - os.path.join( - current_app.config.get("TMPDIR", "/tmp"), "output", file_name - ), - "r", - encoding="utf8", - ) as the_file: + outdir = os.path.join(get_tmpdir(),"output") + if not os.path.isdir(outdir): + os.mkdir(outdir) + + with open( os.path.join(outdir,file_name),"r",encoding="utf-8") as the_file: lod_results = [] for i, line in enumerate(the_file): if i == 0: # Skip first line @@ -322,15 +318,11 @@ def process_perm_output(file_name: str) -> Tuple[List, float, float]: suggestive and significant thresholds""" perm_results = [] - with open( - os.path.join( - current_app.config.get("TMPDIR", "/tmp"), - "output", - "PERM_" + file_name, - ), - "r", - encoding="utf-8", - ) as the_file: + outdir = os.path.join(get_tmpdir(),"output") + if not os.path.isdir(outdir): + os.mkdir(outdir) + + with open( os.path.join(outdir,file_name),"r",encoding="utf-8") as the_file: for i, line in enumerate(the_file): if i == 0: # Skip header line diff --git a/gn3/fs_helpers.py b/gn3/fs_helpers.py index 845c48b..1b226bc 100644 --- a/gn3/fs_helpers.py +++ b/gn3/fs_helpers.py @@ -13,6 +13,20 @@ from typing import List from typing import ValuesView from werkzeug.utils import secure_filename +from flask import current_app + +def get_tmpdir() -> str: + """Get the temp directory from the FLASK tmpdir setting. If it is not set, set it to /tmp. Note + that the app should check for environment settings to initialize its TMPDIR. + """ + tmpdir = current_app.config.get("TMPDIR") + if not tmpdir: + tmpdir = "/tmp" + if not os.path.isdir(tmpdir): + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), f"TMPDIR {tmpdir} is not a valid directory") + + return tmpdir + def assert_path_exists(path: str, throw_error: bool = True) -> bool: """Throw error if any of them do not exist.""" if not os.path.isfile(path): |