aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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()