about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--wqflask/wqflask/marker_regression/rqtl_mapping.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/wqflask/wqflask/marker_regression/rqtl_mapping.py b/wqflask/wqflask/marker_regression/rqtl_mapping.py
index 34b10fc5..e6421fe2 100644
--- a/wqflask/wqflask/marker_regression/rqtl_mapping.py
+++ b/wqflask/wqflask/marker_regression/rqtl_mapping.py
@@ -1,6 +1,7 @@
 import csv
 import hashlib
 import io
+import json
 import requests
 import shutil
 from typing import Dict
@@ -10,6 +11,8 @@ from typing import TextIO
 
 import numpy as np
 
+from flask import g
+
 from base.webqtlConfig import TMPDIR
 from base.trait import create_trait
 from utility.tools import locate, GN3_LOCAL_URL
@@ -35,6 +38,10 @@ def run_rqtl(trait_name, vals, samples, dataset, mapping_scale, model, method, n
         "scale": mapping_scale
     }
 
+    if cofactors:
+        covarstruct_file = write_covarstruct_file(cofactors)
+        post_data["covarstruct"] = covarstruct_file
+
     if do_control == "true" and control_marker:
         post_data["control"] = control_marker
 
@@ -63,6 +70,32 @@ def get_hash_of_textio(the_file: TextIO) -> str:
     return hash_of_file
 
 
+def write_covarstruct_file(cofactors: str) -> str:
+    """
+    Given list of cofactors (as comma-delimited string), write
+    a comma-delimited file where the first column consists of cofactor names
+    and the second column indicates whether they're numerical or categorical
+    """
+    datatype_query = "SELECT value FROM TraitMetadata WHERE type='trait_data_type'"
+    trait_datatype_json = json.loads(g.db.execute(datatype_query).fetchone()[0])
+
+    covar_struct_file = io.StringIO()
+    writer = csv.writer(covar_struct_file, delimiter="\t", quoting = csv.QUOTE_NONE)
+    for cofactor in cofactors.split(","):
+        datatype = trait_datatype_json[cofactor] if cofactor in trait_datatype_json else "numerical"
+        cofactor_name = cofactor.split(":")[0]
+        writer.writerow([cofactor_name, datatype])
+
+    hash_of_file = get_hash_of_textio(covar_struct_file)
+    file_path = TMPDIR + hash_of_file + ".csv"
+
+    with open(file_path, "w") as fd:
+        covar_struct_file.seek(0)
+        shutil.copyfileobj(covar_struct_file, fd)
+
+    return file_path
+
+
 def write_phenotype_file(trait_name: str,
                          samples: List[str],
                          vals: List,