diff options
author | Frederick Muriuki Muriithi | 2024-09-17 16:57:39 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-09-17 16:57:39 -0500 |
commit | 17defa03f395aa9895b524ef3125e138b3987507 (patch) | |
tree | 2d8174959409cf0b2da018e1d12439d473dbeba4 /uploader/genotypes/models.py | |
parent | 354a3907c191cda0b725e81ac15ed2af7db7aa2f (diff) | |
download | gn-uploader-17defa03f395aa9895b524ef3125e138b3987507.tar.gz |
Display some genotype information.
Diffstat (limited to 'uploader/genotypes/models.py')
-rw-r--r-- | uploader/genotypes/models.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/uploader/genotypes/models.py b/uploader/genotypes/models.py new file mode 100644 index 0000000..53c5fb8 --- /dev/null +++ b/uploader/genotypes/models.py @@ -0,0 +1,41 @@ +"""Functions for handling genotypes.""" +from typing import Optional + +import MySQLdb as mdb +from MySQLdb.cursors import DictCursor + +from uploader.db_utils import debug_query + +def genocode_by_population( + conn: mdb.Connection, population_id: int) -> tuple[dict, ...]: + """Get the allele/genotype codes.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT * FROM GenoCode WHERE InbredSetId=%s", + (population_id,)) + return tuple(dict(item) for item in cursor.fetchall()) + + +def genotype_markers_count(conn: mdb.Connection, species_id: int) -> int: + """Find the total count of the genotype markers for a species.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute( + "SELECT COUNT(Name) AS markers_count FROM Geno WHERE SpeciesId=%s", + (species_id,)) + return int(cursor.fetchone()["markers_count"]) + + +def genotype_markers( + conn: mdb.Connection, + species_id: int, + offset: int = 0, + limit: Optional[int] = None +) -> tuple[dict, ...]: + """Retrieve markers from the database.""" + _query = "SELECT * FROM Geno WHERE SpeciesId=%s" + if bool(limit) and limit > 0: + _query = _query + f" LIMIT {limit} OFFSET {offset}" + + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute(_query, (species_id,)) + debug_query(cursor) + return tuple(dict(row) for row in cursor.fetchall()) |