From 547bd24ec422dc33622d30c5b3562cde32e0e98f Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 19 May 2025 13:06:06 -0500 Subject: Add function to save the numerical data for phenotypes. --- uploader/phenotypes/models.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'uploader/phenotypes/models.py') diff --git a/uploader/phenotypes/models.py b/uploader/phenotypes/models.py index fe4ccac..48e64da 100644 --- a/uploader/phenotypes/models.py +++ b/uploader/phenotypes/models.py @@ -310,3 +310,29 @@ def create_new_phenotypes(conn: mdb.Connection, } for row in cursor.fetchall()) return _phenos + + +def save_phenotypes_data( + conn: mdb.Connection, + table: str, + data: Iterable[dict] +) -> tuple[dict, ...]: + """Save new phenotypes data into the database.""" + _table_details = { + "PublishData": {"table": "PublishData", "valueCol": "value"}, + "PublishSE": {"table": "PublishSE", "valueCol": "error"}, + "NStrain": {"table": "PublishData", "valueCol": "count"} + }[table] + saved_data = tuple() + with conn.cursor(cursorclass=DictCursor) as cursor: + for batch in take(data, 5000): + cursor.executemany( + (f"INSERT INTO {_table_details['table']}" + f"(Id, StrainId, {_table_details['valueCol']}) " + "VALUES " + f"(%(data_id)s, %(sample_id)s, %({_table_details['valueCol']})s) " + "RETURNING *"), + tuple(batch)) + _data = data + tuple(cursor.fetchall()) + + return saved_data -- cgit v1.2.3