about summary refs log tree commit diff
diff options
context:
space:
mode:
authorzsloan <zachary.a.sloan@gmail.com>2025-11-26 17:37:59 +0000
committerzsloan <zachary.a.sloan@gmail.com>2025-11-26 17:38:28 +0000
commite6c44577b2489f7b1523cb6a5fd332015bde25c5 (patch)
tree796745b2fd54eeadbaaa90f23625eab289df9cc5
parentab025022b97e0b89f72a280a980e3188d8b7922c (diff)
downloadgenenetwork3-e6c44577b2489f7b1523cb6a5fd332015bde25c5.tar.gz
Add description updating code for Batch Edit
-rw-r--r--gn3/db/phenotypes.py45
1 files changed, 43 insertions, 2 deletions
diff --git a/gn3/db/phenotypes.py b/gn3/db/phenotypes.py
index 0339aba..711dacc 100644
--- a/gn3/db/phenotypes.py
+++ b/gn3/db/phenotypes.py
@@ -1,16 +1,16 @@
 # pylint: disable=[R0902, R0903]
 """This contains all the necessary functions that access the phenotypes from
 the db"""
-from typing import Optional
+from typing import Optional, Any, Dict
 from dataclasses import dataclass
 
 from MySQLdb.cursors import DictCursor
 
 from gn3.db_utils import Connection as DBConnection
 
+from .datasets import retrieve_group_id
 from .query_tools import mapping_to_query_columns
 
-
 @dataclass(frozen=True)
 class Phenotype:
     """Data Type that represents a Phenotype"""
@@ -195,3 +195,44 @@ def update_cross_reference(conn, dataset_id, trait_name, data:dict) -> int:
                 **data
             })
         return cursor.rowcount
+
+def batch_update_descriptions(
+    conn: Any, diff_data: Dict
+):
+    """Given sample data diffs, execute all relevant update/insert/delete queries"""
+
+    # If PubMed_ID exists update the
+    # Post_publication_description; otherwise update the
+    # Pre_publication_description
+    update_query = """
+        UPDATE Phenotype
+        JOIN PublishXRef ON PublishXRef.PhenotypeId = Phenotype.Id
+        LEFT JOIN Publication pub ON PublishXRef.PublicationId = pub.Id
+        SET
+          Phenotype.Post_publication_description =
+            CASE WHEN pub.PubMed_ID IS NOT NULL AND pub.PubMed_ID <> 0
+                 THEN %(new_desc)s
+                 ELSE Phenotype.Post_publication_description END,
+          Phenotype.Pre_publication_description =
+            CASE WHEN pub.PubMed_ID IS NULL OR pub.PubMed_ID = 0
+                 THEN %(new_desc)s
+                 ELSE Phenotype.Pre_publication_description END
+        WHERE PublishXRef.InbredSetId=%(dataset_id)s
+          AND PublishXRef.Id=%(trait_id)s
+    """
+
+    for group_trait, changes in diff_data.items():
+        group_name, trait_id = group_trait.split(":")
+        group_id = retrieve_group_id(conn, group_name)
+
+        with conn.cursor() as cursor:
+            # Pass the new description twice: one for the post column CASE
+            # and one for the pre column CASE, followed by dataset and trait
+            # identifiers.
+            cursor.execute(update_query, {
+                "new_desc": changes['Current'],
+                "dataset_id": group_id,
+                "trait_id": trait_id,
+            })
+
+    return diff_data