From a4e6fddea8a25b30b775482ee9717386389486ad Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 5 May 2025 14:26:01 -0500 Subject: Save/Create new phenotypes in the database. --- uploader/phenotypes/models.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/uploader/phenotypes/models.py b/uploader/phenotypes/models.py index 9ff89ae..fe4ccac 100644 --- a/uploader/phenotypes/models.py +++ b/uploader/phenotypes/models.py @@ -1,12 +1,13 @@ """Database and utility functions for phenotypes.""" import logging -from typing import Optional from functools import reduce from datetime import datetime +from typing import Optional, Iterable import MySQLdb as mdb from MySQLdb.cursors import Cursor, DictCursor +from functional_tools import take from gn_libs.mysqldb import debug_query logger = logging.getLogger(__name__) @@ -287,3 +288,25 @@ def phenotypes_data_by_ids( debug_query(cursor, logger) return tuple( reduce(__organise_by_phenotype__, cursor.fetchall(), {}).values()) + + +def create_new_phenotypes(conn: mdb.Connection, + phenotypes: Iterable[dict]) -> tuple[dict, ...]: + """Add entirely new phenotypes to the database.""" + _phenos = tuple() + with conn.cursor(cursorclass=DictCursor) as cursor: + for batch in take(phenotypes, 1000): + cursor.executemany( + ("INSERT INTO " + "Phenotypes(Pre_publication_description, Original_description, Units, Authorized_Users) " + "VALUES (%(id)s, %(description)s, %(units)s, 'robwilliams') " + "RETURNING *"), + tuple(batch)) + _phenos = _phenos + tuple({ + "phenotype_id": row["Id"], + "id": row["Pre_publication_description"], + "description": row["Original_description"], + "units": row["Units"] + } for row in cursor.fetchall()) + + return _phenos -- cgit v1.2.3