From 8a910ebe01ed46ef374daa091e18a65a855070c2 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 18 Jul 2023 13:09:06 +0300 Subject: Fetch metadata for a single phenotype. --- gn3/db/phenotypes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'gn3') 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() -- cgit v1.2.3