diff options
author | Frederick Muriuki Muriithi | 2025-05-05 14:26:01 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2025-05-05 16:20:10 -0500 |
commit | a4e6fddea8a25b30b775482ee9717386389486ad (patch) | |
tree | 2c24eab38a03f4c5e635ab2806c204dae9a9bc85 | |
parent | 5e5ac256740e7e0e44e09111b9015c2960fb4fa3 (diff) | |
download | gn-uploader-a4e6fddea8a25b30b775482ee9717386389486ad.tar.gz |
Save/Create new phenotypes in the database.
-rw-r--r-- | uploader/phenotypes/models.py | 25 |
1 files changed, 24 insertions, 1 deletions
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 |