aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-07-18 13:09:06 +0300
committerFrederick Muriuki Muriithi2023-07-18 13:09:06 +0300
commit8a910ebe01ed46ef374daa091e18a65a855070c2 (patch)
tree8691dc5cb5419de3443e43b8528d34067705bf41
parentbf90bc3f8cd09df8170c220d607f510869e3d323 (diff)
downloadgenenetwork3-8a910ebe01ed46ef374daa091e18a65a855070c2.tar.gz
Fetch metadata for a single phenotype.
-rw-r--r--gn3/db/phenotypes.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/gn3/db/phenotypes.py b/gn3/db/phenotypes.py
index 744df34..54630b4 100644
--- a/gn3/db/phenotypes.py
+++ b/gn3/db/phenotypes.py
@@ -178,3 +178,19 @@ def fetch_trait(conn: DBConnection, dataset_id: int, trait_name: str) -> dict:
cursor.execute(
query, {"dataset_id": dataset_id, "trait_name": trait_name})
return cursor.fetchone()
+
+def _mapping_to_query_columns_(mapping_dict: dict[str, str]) -> tuple[str, ...]:
+ """
+ Internal function to convert mapping dicts into column clauses for queries.
+ """
+ return tuple(f"{tcol} as {dcol}" for dcol, tcol in mapping_dict.items())
+
+def fetch_metadata(conn: DBConnection, phenotype_id: int) -> dict:
+ """Get the phenotype metadata by ID."""
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ cols = ', '.join(_mapping_to_query_columns_(phenotype_mapping))
+ cursor.execute(
+ (f"SELECT Id as id, {cols} FROM Phenotype "
+ "WHERE Id=%(phenotype_id)s"),
+ {"phenotype_id": phenotype_id})
+ return cursor.fetchone()