1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
"Handle inserting data into the database"
import os
import json
from functools import reduce
import requests
from redis import Redis
from MySQLdb.cursors import DictCursor
from flask import request, Blueprint, render_template, current_app as app
from . import jobs
from .db_utils import database_connection
dbinsertbp = Blueprint("dbinsert", __name__)
def render_error(error_msg):
"Render the generic error page"
return render_template("dbupdate_error.html", error_message=error_msg), 400
def make_menu_items_grouper(grouping_fn=lambda item: item):
"Build function to be used to group menu items."
def __grouper__(acc, row):
grouping = grouping_fn(row[2])
row_values = (row[0].strip(), row[1].strip())
if acc.get(grouping) is None:
return {**acc, grouping: (row_values,)}
return {**acc, grouping: (acc[grouping] + (row_values,))}
return __grouper__
def species() -> tuple:
"Retrieve the species from the database."
with database_connection() as conn:
with conn.cursor(cursorclass=DictCursor) as cursor:
cursor.execute(
"SELECT SpeciesId, SpeciesName, LOWER(Name) AS Name, MenuName "
"FROM Species")
return tuple(cursor.fetchall())
return tuple()
def genechips():
"Retrieve the genechip information from the database"
def __organise_by_species__(acc, chip):
speciesid = chip["SpeciesId"]
if acc.get(speciesid) is None:
return {**acc, speciesid: (chip,)}
return {**acc, species: acc[speciesid] + (chip,)}
with database_connection() as conn:
with conn.cursor(cursorclass=DictCursor) as cursor:
cursor.execute("SELECT * FROM GeneChip ORDER BY GeneChipName ASC")
return reduce(__organise_by_species__, cursor.fetchall(), {})
return {}
def studies_by_species_and_platform(speciesid:int, genechipid:int) -> tuple:
"Retrieve the studies by the related species and gene platform"
with database_connection() as conn:
with conn.cursor(cursorclass=DictCursor) as cursor:
query = (
"SELECT Species.SpeciesId, ProbeFreeze.* "
"FROM Species INNER JOIN InbredSet "
"ON Species.SpeciesId=InbredSet.SpeciesId "
"INNER JOIN ProbeFreeze "
"ON InbredSet.InbredSetId=ProbeFreeze.InbredSetId "
"WHERE Species.SpeciesId = %s "
"AND ProbeFreeze.ChipId = %s")
cursor.execute(query, (speciesid, genechipid))
return tuple(cursor.fetchall())
return tuple()
@dbinsertbp.route("/platform", methods=["POST"])
def select_platform():
"Select the platform (GeneChipId) used for the data."
job_id = request.form["job_id"]
with Redis.from_url(app.config["REDIS_URL"], decode_responses=True) as rconn:
job = jobs.job(rconn, job_id)
if job:
filename = job["filename"]
filepath = f"{app.config['UPLOAD_FOLDER']}/{filename}"
if os.path.exists(filepath):
default_species = 1
gchips = genechips()
return render_template(
"select_platform.html", filename=filename,
filetype=job["filetype"], default_species=default_species,
species=species(), genechips=gchips[default_species],
genechips_data=json.dumps(gchips))
return render_error(f"File '{filename}' no longer exists.")
return render_error(f"Job '{job_id}' no longer exists.")
return render_error("Unknown error")
@dbinsertbp.route("/study", methods=["POST"])
def select_study():
"View to select/create the study (ProbeFreeze) associated with the data."
form = request.form
try:
assert form.get("filename"), "filename"
assert form.get("filetype"), "filetype"
assert form.get("species"), "species"
assert form.get("genechipid"), "platform"
speciesid = form["species"]
genechipid = form["genechipid"]
the_studies = studies_by_species_and_platform(speciesid, genechipid)
return render_template(
"select_study.html", filename=form["filename"],
filetype=form["filetype"], species=speciesid, genechipid=genechipid,
studies=the_studies)
except AssertionError as aserr:
return render_error(f"Missing data: {aserr.args[0]}")
@dbinsertbp.route("/select-dataset", methods=["POST"])
def select_dataset():
"Select the dataset to add the file contents against"
job_id = request.form["job_id"]
with Redis.from_url(app.config["REDIS_URL"], decode_responses=True) as rconn:
job = jobs.job(rconn, job_id)
if job:
filename = job["filename"]
filepath = f"{app.config['UPLOAD_FOLDER']}/{filename}"
if os.path.exists(filepath):
req = requests.get(
"https://genenetwork.org/api3/api/menu/generate/json")
menu_contents = req.json()
default_species = "mouse"
mouse_groups = reduce(
make_menu_items_grouper(
lambda item: item.strip()[7:].strip()),
menu_contents["groups"][default_species], {})
default_group = "BXD"
group_types = reduce(
make_menu_items_grouper(),
menu_contents["types"][default_species][default_group], {})
default_type = group_types[tuple(group_types)[0]][0][0]
datasets = menu_contents[
"datasets"][default_species][default_group][
default_type]
gchips = genechips()
return render_template(
"select_dataset.html", filename=filename,
species=menu_contents["species"],
default_species=default_species, groups=mouse_groups,
types=group_types, datasets=datasets,
menu_contents=json.dumps(menu_contents),
genechips_data=json.dumps(gchips),
genechips=gchips.get(default_species, []),
filetype=job["filetype"])
return render_error(f"File '{filename}' no longer exists.")
return render_error(f"Job '{job_id}' no longer exists.")
return render_error("Unknown error")
@dbinsertbp.route("/insert_data", methods=["POST"])
def insert_data():
"Preview the data before triggering entry into the database"
form = request.form
filename = form["filename"]
filepath = f"{app.config['UPLOAD_FOLDER']}/{filename}"
if os.path.exists(filepath):
try:
species = form["species"]
filetype = form["filetype"]
datasetid = int(form["dataset"])
genechipid = int(form["genechipid"])
return (f"Would insert '{species}' data in '{filetype}' file "
f"'{filepath}' into the database with the dataset "
f"'{datasetid}' and genechip '{genechipid}'.")
except ValueError as verr:
msg = "::".join(verr.args)
return render_error(f"Invalid value: {msg}")
return render_error(f"File '{filename}' no longer exists.")
|