diff options
-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 |