diff options
author | Frederick Muriuki Muriithi | 2025-05-19 13:06:06 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2025-05-19 14:20:34 -0500 |
commit | 547bd24ec422dc33622d30c5b3562cde32e0e98f (patch) | |
tree | 153813a50199810bfb4704891d404f893d34e8ee | |
parent | aa486e50b5d10592123daf557b1c10825d0cb94b (diff) | |
download | gn-uploader-547bd24ec422dc33622d30c5b3562cde32e0e98f.tar.gz |
Add function to save the numerical data for phenotypes.
-rw-r--r-- | uploader/phenotypes/models.py | 26 |
1 files changed, 26 insertions, 0 deletions
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 |