aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/species.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-10-27 10:24:28 +0300
committerFrederick Muriuki Muriithi2021-10-27 10:24:28 +0300
commit151ef8bbe23d85f74cdab690073afd4a9329e78a (patch)
treec9b4e3ba48943ac9e73732b3541281d8982befad /gn3/db/species.py
parent79c56677d32fb0b7ed05d22df3813def8f14b89a (diff)
downloadgenenetwork3-151ef8bbe23d85f74cdab690073afd4a9329e78a.tar.gz
Remove if clauses: replace with dict
Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/partial-correlations.gmi * Remove the if clauses to simplify the code flow: use a dictionary of queries and select the appropriate query from the dictionary instead.
Diffstat (limited to 'gn3/db/species.py')
-rw-r--r--gn3/db/species.py20
1 files changed, 8 insertions, 12 deletions
diff --git a/gn3/db/species.py b/gn3/db/species.py
index 1e5015f..abcbf64 100644
--- a/gn3/db/species.py
+++ b/gn3/db/species.py
@@ -47,17 +47,13 @@ def translate_to_mouse_gene_id(species: str, geneid: int, conn: Any) -> int:
return geneid
with conn.cursor as cursor:
- if species == "rat":
- cursor.execute(
- "SELECT mouse FROM GeneIDXRef WHERE rat = %s", geneid)
- rat_geneid = cursor.fetchone()
- if rat_geneid:
- return rat_geneid[0]
-
- cursor.execute(
- "SELECT mouse FROM GeneIDXRef WHERE human = %s", geneid)
- human_geneid = cursor.fetchone()
- if human_geneid:
- return human_geneid[0]
+ query = {
+ "rat": "SELECT mouse FROM GeneIDXRef WHERE rat = %s"
+ "human": "SELECT mouse FROM GeneIDXRef WHERE human = %s"
+ }
+ cursor.execute(query[species], geneid)
+ translated_gene_id = cursor.fetchone()
+ if translated_gene_id:
+ return translated_gene_id[0]
return 0 # default if all else fails