aboutsummaryrefslogtreecommitdiff
path: root/gn3/computations/gemma.py
diff options
context:
space:
mode:
authorBonfaceKilz2021-02-24 10:32:57 +0300
committerBonfaceKilz2021-02-24 14:20:29 +0300
commitf3f05970ea4dfaf84fea52d8f4212e72ff21b3d0 (patch)
treed0b5c99e0a23afb936a33cb1952bc64fce47b729 /gn3/computations/gemma.py
parent06f480b625fe5a240ddcdf3e6887f0796cfefb52 (diff)
downloadgenenetwork3-f3f05970ea4dfaf84fea52d8f4212e72ff21b3d0.tar.gz
Check if phenotype exists before creating one
Diffstat (limited to 'gn3/computations/gemma.py')
-rw-r--r--gn3/computations/gemma.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/gn3/computations/gemma.py b/gn3/computations/gemma.py
index ea0f86a..42b4b36 100644
--- a/gn3/computations/gemma.py
+++ b/gn3/computations/gemma.py
@@ -1,9 +1,11 @@
"""Procedures related gemma computations"""
+import os
import random
import string
from base64 import b64encode
from hashlib import md5
+from typing import List
def generate_random_n_string(n_length: int) -> str:
"""Generate a random string that is N chars long"""
@@ -16,11 +18,22 @@ def generate_hash_of_string(unhashed_str: str) -> str:
the end"""
hashed_str = md5(unhashed_str.encode("utf-8")).digest()
return b64encode(hashed_str).decode("utf-8").replace("==", "")
+
+
def generate_pheno_txt_file(trait_filename: str,
- values: str,
+ values: List,
tmpdir: str = "/tmp") -> str:
- """Given VALUES, and TMPDIR, generate a valide traits file"""
- trait_filename += f"_{generate_random_n_string(6)}"
+ """Given VALUES, and TMPDIR, generate a valid traits file"""
+ if not os.path.isdir(f"{tmpdir}/gn2/"):
+ os.mkdir(f"{tmpdir}/gn2/")
+ ext = trait_filename.partition(".")[-1]
+ if ext:
+ trait_filename = trait_filename.replace(f".{ext}", "")
+ ext = f".{ext}"
+ trait_filename += f"_{generate_hash_of_string(''.join(values))}{ext}"
+ # Early return if this already exists!
+ if os.path.isfile(f"{tmpdir}/gn2/{trait_filename}"):
+ return f"{tmpdir}/gn2/{trait_filename}"
with open(f"{tmpdir}/gn2/{trait_filename}", "w") as _file:
for value in values:
if value == "x":