about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-07-18 12:34:47 +0300
committerFrederick Muriuki Muriithi2023-07-18 13:08:40 +0300
commitbf90bc3f8cd09df8170c220d607f510869e3d323 (patch)
tree5415bac9e8d8b1dbfc81b225750d2d91a61d5072
parent088b6181457ef4e528b7dcbf04b7aca29b274d1f (diff)
downloadgenenetwork3-bf90bc3f8cd09df8170c220d607f510869e3d323.tar.gz
Fetch single phenotype trait by `dataset_id` and `trait_name`.
-rw-r--r--gn3/db/phenotypes.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/gn3/db/phenotypes.py b/gn3/db/phenotypes.py
index 0ed22d4..744df34 100644
--- a/gn3/db/phenotypes.py
+++ b/gn3/db/phenotypes.py
@@ -1,9 +1,12 @@
 # pylint: disable=[R0902, R0903]
 """This contains all the necessary functions that access the phenotypes from
 the db"""
+from typing import Optional
 from dataclasses import dataclass
 
-from typing import Optional
+from MySQLdb.cursors import DictCursor
+
+from wqflask.database import Connection as DBConnection
 
 
 @dataclass(frozen=True)
@@ -158,3 +161,20 @@ probeset_mapping = {
     "probe_set_note_by_rw": "Probe_set_Note_by_RW",
     "flag": "flag"
 }
+
+def fetch_trait(conn: DBConnection, dataset_id: int, trait_name: str) -> dict:
+    """Fetch phenotype 'traits' by `dataset_id` and `trait_name`."""
+    query = (
+        "SELECT "
+        "pxr.Id AS _id, pxr.Id as trait_name, pxr.PhenotypeId AS phenotype_id, "
+        "pxr.PublicationId AS publication_id, pxr.DataId AS data_id, "
+        "pxr.mean, pxr.locus, pxr.LRS as lrs, pxr.additive, "
+        "pxr.Sequence as sequence, pxr.comments "
+        "FROM PublishFreeze AS pf INNER JOIN InbredSet AS iset "
+        "ON pf.InbredSetId=iset.Id "
+        "INNER JOIN PublishXRef AS pxr ON iset.Id=pxr.InbredSetId "
+        "WHERE pf.Id=%(dataset_id)s AND pxr.Id=%(trait_name)s")
+    with conn.cursor(cursorclass=DictCursor) as cursor:
+        cursor.execute(
+            query, {"dataset_id": dataset_id, "trait_name": trait_name})
+        return cursor.fetchone()