about summary refs log tree commit diff
diff options
context:
space:
mode:
authorzsloan2021-08-17 20:50:31 +0000
committerzsloan2021-08-19 18:38:54 +0000
commit26958694322855f4ee91beb3b01e73afe75066cf (patch)
treec72b11b7b78aa835f18721e4d6388b259d7b28af
parent1393d777bd0fee4a25ba4b1f6af6ccb9ae24c0f8 (diff)
downloadgenenetwork2-26958694322855f4ee91beb3b01e73afe75066cf.tar.gz
Added function for getting the diff of sample values before and after user changes to show_trait.py
-rw-r--r--wqflask/wqflask/show_trait/show_trait.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py
index 4120c801..96f6901a 100644
--- a/wqflask/wqflask/show_trait/show_trait.py
+++ b/wqflask/wqflask/show_trait/show_trait.py
@@ -1,3 +1,5 @@
+from typing import Dict
+
 import string
 import datetime
 import uuid
@@ -805,3 +807,41 @@ def get_scales_from_genofile(file_location):
         return [["physic", "Mb"], ["morgan", "cM"]]
     else:
         return [["physic", "Mb"]]
+
+
+
+def get_diff_of_vals(new_vals: Dict, trait_id: str) -> Dict:
+    """ Get the diff between current sample values and the values in the DB
+
+    Given a dict of the changed values and the trait/dataset ID, return a Dict
+    with keys corresponding to each sample with a changed value and a value
+    that is a dict with keys for the old_value and new_value
+
+    """
+
+    trait_name = trait_id.split(":")[0]
+    dataset_name = trait_id.split(":")[1]
+    trait_ob = create_trait(name=trait_name, dataset_name=dataset_name)
+
+    old_vals = {sample : trait_ob.data[sample].value for sample in trait_ob.data}
+
+    shared_samples = set.union(set(new_vals.keys()), set(old_vals.keys()))
+
+    diff_dict = {}
+    for sample in shared_samples:
+        try:
+            new_val = float(new_vals[sample])
+        except:
+            new_val = "x"
+        try:
+            old_val = float(old_vals[sample])
+        except:
+            old_val = "x"
+
+        if new_val != old_val:
+            diff_dict[sample] = {
+                "new_val": new_val,
+                "old_val": old_val
+            }
+
+    return diff_dict