aboutsummaryrefslogtreecommitdiff
path: root/qc_app/db
diff options
context:
space:
mode:
Diffstat (limited to 'qc_app/db')
-rw-r--r--qc_app/db/datasets.py38
-rw-r--r--qc_app/db/platforms.py25
-rw-r--r--qc_app/db/tissues.py22
3 files changed, 84 insertions, 1 deletions
diff --git a/qc_app/db/datasets.py b/qc_app/db/datasets.py
index 086c103..5f6a2d5 100644
--- a/qc_app/db/datasets.py
+++ b/qc_app/db/datasets.py
@@ -1,4 +1,6 @@
"""Functions for accessing the database relating to datasets."""
+from datetime import date
+
import MySQLdb as mdb
from MySQLdb.cursors import DictCursor
@@ -43,9 +45,43 @@ def probeset_datasets_by_study(conn: mdb.Connection,
(studyid,))
return tuple(dict(row) for row in cursor.fetchall())
-def probe_study_by_id(conn: mdb.Connection, studyid: int) -> dict:
+def probeset_study_by_id(conn: mdb.Connection, studyid: int) -> dict:
"""Retrieve ProbeSet study by ID"""
with conn.cursor(cursorclass=DictCursor) as cursor:
cursor.execute("SELECT * FROM ProbeFreeze WHERE Id=%s",
(studyid,))
return dict(cursor.fetchone())
+
+def probeset_create_study(conn: mdb.Connection,#pylint: disable=[too-many-arguments]
+ populationid: int,
+ platformid: int,
+ tissueid: int,
+ studyname: str,
+ studyfullname: str = "",
+ studyshortname: str = ""):
+ """Create a new ProbeSet study."""
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ studydata = {
+ "platid": platformid,
+ "tissueid": tissueid,
+ "name": studyname,
+ "fname": studyfullname or studyname,
+ "sname": studyshortname,
+ "today": date.today().isoformat(),
+ "popid": populationid
+ }
+ cursor.execute(
+ """
+ INSERT INTO ProbeFreeze(
+ ChipId, TissueId, Name, FullName, ShortName, CreateTime,
+ InbredSetId
+ ) VALUES (
+ %(platid)s, %(tissueid)s, %(name)s, %(fname)s, %(sname)s,
+ %(today)s, %(popid)s
+ )
+ """,
+ studydata)
+ studyid = cursor.lastrowid
+ cursor.execute("UPDATE ProbeFreeze SET ProbeFreezeId=%s",
+ (studyid,))
+ return {**studydata, "studyid": studyid}
diff --git a/qc_app/db/platforms.py b/qc_app/db/platforms.py
new file mode 100644
index 0000000..cb527a7
--- /dev/null
+++ b/qc_app/db/platforms.py
@@ -0,0 +1,25 @@
+"""Handle db interactions for platforms."""
+from typing import Optional
+
+import MySQLdb as mdb
+from MySQLdb.cursors import DictCursor
+
+def platforms_by_species(
+ conn: mdb.Connection, speciesid: int) -> tuple[dict, ...]:
+ """Retrieve platforms by the species"""
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ cursor.execute("SELECT * FROM GeneChip WHERE SpeciesId=%s "
+ "ORDER BY GeneChipName ASC",
+ (speciesid,))
+ return tuple(dict(row) for row in cursor.fetchall())
+
+def platform_by_id(conn: mdb.Connection, platformid: int) -> Optional[dict]:
+ """Retrieve a platform by its ID"""
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ cursor.execute("SELECT * FROM GeneChip WHERE Id=%s",
+ (platformid,))
+ result = cursor.fetchone()
+ if bool(result):
+ return dict(result)
+
+ return None
diff --git a/qc_app/db/tissues.py b/qc_app/db/tissues.py
new file mode 100644
index 0000000..ebf24fd
--- /dev/null
+++ b/qc_app/db/tissues.py
@@ -0,0 +1,22 @@
+"""Handle db interactions for tissue."""
+from typing import Optional
+
+import MySQLdb as mdb
+from MySQLdb.cursors import DictCursor
+
+def all_tissues(conn: mdb.Connection) -> tuple[dict, ...]:
+ """All available tissue."""
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ cursor.execute("SELECT * FROM Tissue ORDER BY TissueName")
+ return tuple(dict(row) for row in cursor.fetchall())
+
+def tissue_by_id(conn: mdb.Connection, tissueid: int) -> Optional[dict]:
+ """Retrieve a tissue by its ID"""
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ cursor.execute("SELECT * FROM Tissue WHERE Id=%s",
+ (tissueid,))
+ result = cursor.fetchone()
+ if bool(result):
+ return dict(result)
+
+ return None