about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-10-29 08:00:27 +0300
committerFrederick Muriuki Muriithi2021-10-29 08:00:27 +0300
commit6bfc0e9e9bce5e2505588372ce55ba892db6bda0 (patch)
treeaa180439da417168217feb7d2c31e9487df408b2
parent1b5e448ccecdfa146942bbb3d5b22bbbccd492c9 (diff)
downloadgenenetwork3-6bfc0e9e9bce5e2505588372ce55ba892db6bda0.tar.gz
Complete `build_temporary_tissue_correlations_table`
Issue:
https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/partial-correlations.gmi

* gn3/computations/partial_correlations.py: Remove comments after updating
  usage of the function at call point
* gn3/db/correlations.py: Complete the implementation of the
  `build_temporary_tissue_correlations_table` function
-rw-r--r--gn3/computations/partial_correlations.py13
-rw-r--r--gn3/db/correlations.py36
2 files changed, 33 insertions, 16 deletions
diff --git a/gn3/computations/partial_correlations.py b/gn3/computations/partial_correlations.py
index d095185..5777a0b 100644
--- a/gn3/computations/partial_correlations.py
+++ b/gn3/computations/partial_correlations.py
@@ -173,19 +173,6 @@ def correlations_of_all_tissue_traits(
     `web.webqtl.correlation.correlationFunction.calculateCorrOfAllTissueTrait`
     function in GeneNetwork1.
     """
-    # The section below existed in the original function, but with the migration
-    # and the proposed rework (in the near future), the values from the database
-    # should be passed into this function, rather than have the function fetch
-    # the data for itself.
-    # ---------------------------------------------------
-    # primary_trait_symbol_value_dict = fetch_gene_symbol_tissue_value_dict_for_trait(
-    #     (trait_symbol,), probeset_freeze_id, conn)
-    # primary_trait_values = primary_trait_symbol_value_dict.vlaues()[0]
-    # symbol_value_dict = fetch_gene_symbol_tissue_value_dict_for_trait(
-    #     tuple(), probeset_freeze_id, conn)
-    # ---------------------------------------------------
-    # We might end up actually getting rid of this function all together as the
-    # rework is done.
     primary_trait_values = primary_trait_symbol_value_dict.values()[0]
     return batch_computed_tissue_correlation(
         primary_trait_values, symbol_value_dict, method)
diff --git a/gn3/db/correlations.py b/gn3/db/correlations.py
index 39ed499..28f050a 100644
--- a/gn3/db/correlations.py
+++ b/gn3/db/correlations.py
@@ -290,10 +290,40 @@ def build_temporary_tissue_correlations_table(
     This is a migration of the
     `web.webqtl.correlation.CorrelationPage.getTempTissueCorrTable` function in
     GeneNetwork1."""
+    # We should probably pass the `correlations_of_all_tissue_traits` function
+    # as an argument to this function and get rid of the two lines immediately
+    # following this comment.
+    from gn3.computations.partial_correlations import correlations_of_all_tissue_traits
     symbol_corr_dict, symbol_p_value_dict = correlations_of_all_tissue_traits(
-        trait_symbol, probeset_freeze_id, method, conn)
-    raise Exception("Unimplemented!!!")
-    return ""
+        fetch_gene_symbol_tissue_value_dict_for_trait(
+            (trait_symbol,), probeset_freeze_id, conn),
+        fetch_gene_symbol_tissue_value_dict_for_trait(
+            tuple(), probeset_freeze_id, conn),
+        method)
+
+    symbol_corr_list = sorted(
+        symbol_corr_dict.items(),
+        key=compare_tissue_correlation_absolute_values)
+
+    temp_table_name = f"TOPTISSUE{random_string(8)}"
+    create_query = (
+        "CREATE TEMPORARY TABLE {temp_table_name}"
+        "(Symbol varchar(100) PRIMARY KEY, Correlation float, PValue float)")
+    insert_query = (
+        f"INSERT INTO {temp_table_name}(Symbol, Correlation, PValue) "
+        " VALUES (%(symbol)s, %(correlation)s, %(pvalue)s)")
+
+    with conn.cursor() as cursor:
+        cursor.execute(create_query)
+        cursor.execute(
+            insert_query,
+            tuple({
+                "symbol": symbol,
+                "correlation": corr,
+                "pvalue": symbol_p_value_dict[symbol]
+            } for symbol, corr in symbol_corr_list[0: 2 * return_number]))
+
+    return temp_table_name
 
 def fetch_tissue_correlations(
         dataset: dict, trait_symbol: str, probeset_freeze_id: int, method: str,