forked from BioAnalyticResource/BAR_API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastpheno.py
More file actions
311 lines (255 loc) · 11.4 KB
/
fastpheno.py
File metadata and controls
311 lines (255 loc) · 11.4 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
Date: Aug 2023
Author: Vince L
Fastpheno endpoint for retrieving tree data
"""
import re
from flask import request
from flask_restx import Namespace, Resource
from api import db
from api.models.fastpheno import Sites, Flights, Trees, TreesFlightsJoinTbl, Bands
from api.utils.bar_utils import BARUtils
from markupsafe import escape
fastpheno = Namespace("FastPheno", description="FastPheno API service", path="/fastpheno")
@fastpheno.route("/get_bands/<string:site>/<string:flight_id>/<string:band>")
class FastPheno(Resource):
@fastpheno.param("site", _in="path", default="Pintendre")
@fastpheno.param("flight_id", _in="path", default="14")
@fastpheno.param("band", _in="path", default="398nm")
def get(self, site, flight_id, band):
"""Returns all band values for a given site, flight ID, and band name"""
site = str(escape(site)).capitalize()
flight_id = str(escape(flight_id))
band = str(escape(band))
if not re.search(r"^[a-zA-Z]{1,15}$", site):
return BARUtils.error_exit("Invalid site name"), 400
if not BARUtils.is_integer(flight_id):
return BARUtils.error_exit("Invalid flight ID"), 400
if not re.search(r"^[a-zA-Z0-9_]{1,20}$", band):
return BARUtils.error_exit("Invalid band"), 400
rows = db.session.execute(
db.select(Trees, TreesFlightsJoinTbl, Bands)
.select_from(Bands)
.join(
TreesFlightsJoinTbl,
(Bands.trees_pk == TreesFlightsJoinTbl.trees_pk) & (Bands.flights_pk == TreesFlightsJoinTbl.flights_pk),
)
.join(Trees, Bands.trees_pk == Trees.trees_pk)
.join(Flights, Bands.flights_pk == Flights.flights_pk)
.join(Sites, Flights.sites_pk == Sites.sites_pk)
.where(Sites.site_name == site, Bands.flights_pk == int(flight_id), Bands.band == band)
).all()
if len(rows) == 0:
return BARUtils.error_exit("No data found for the given parameters"), 400
res = [
{
"trees_pk": t.trees_pk,
"seq_id": t.seq_id,
"longitude": float(t.longitude),
"latitude": float(t.latitude),
"x_pos": t.x_pos,
"y_pos": t.y_pos,
"height_2022": t.height_2022,
"block_num": t.block_num,
"tree_site_id": t.tree_site_id,
"confidence": float(tf.confidence) if tf.confidence is not None else None,
"band_value": float(b.value),
}
for t, tf, b in rows
]
return BARUtils.success_exit(res)
@fastpheno.route("/get_trees/<string:tree_site_id>")
class FastPhenoTrees(Resource):
@fastpheno.param("tree_site_id", _in="path", default="619")
@fastpheno.param("site", _in="query", required=False, default="Pintendre")
def get(self, tree_site_id):
"""Returns trees for a given genotype. Accepts letter codes (e.g. 'c') or numeric
genotype prefixes (e.g. '619' matches '619.03'). Optionally filter by site name."""
tree_site_id = str(escape(tree_site_id))
site = request.args.get("site")
if not re.search(r"^[a-zA-Z0-9.]{1,15}$", tree_site_id):
return BARUtils.error_exit("Invalid tree site ID"), 400
if site is not None:
site = str(escape(site)).capitalize()
if not re.search(r"^[a-zA-Z]{1,15}$", site):
return BARUtils.error_exit("Invalid site name"), 400
query = (
db.select(Sites, Trees)
.select_from(Sites)
.join(Trees, Trees.sites_pk == Sites.sites_pk)
.where(db.or_(Trees.tree_site_id == tree_site_id, Trees.tree_site_id.like(f"{tree_site_id}.%")))
)
if site is not None:
query = query.where(Sites.site_name == site)
rows = db.session.execute(query).all()
if len(rows) == 0:
return BARUtils.error_exit("No data found for the given parameters"), 400
res = [
{
"site_name": s.site_name,
"trees_pk": t.trees_pk,
"seq_id": t.seq_id,
"longitude": float(t.longitude),
"latitude": float(t.latitude),
"tree_site_id": t.tree_site_id,
"external_link": t.external_link,
}
for s, t in rows
]
return BARUtils.success_exit(res)
@fastpheno.route("/timeseries/tree/<string:seq_id>/<string:band>")
class FastPhenoTimeSeries(Resource):
@fastpheno.param("seq_id", _in="path", default="PIN_2547")
@fastpheno.param("band", _in="path", default="398nm")
def get(self, seq_id, band):
"""Returns all band values + confidence for a single tree across all flights, ordered by flight date, for time series."""
seq_id = str(escape(seq_id)).upper()
band = str(escape(band))
if not re.search(r"^[A-Z]{2,5}_\d{1,6}$", seq_id):
return BARUtils.error_exit("Invalid seq_id"), 400
if not re.search(r"^[a-zA-Z0-9_]{1,20}$", band):
return BARUtils.error_exit("Invalid band"), 400
rows = db.session.execute(
db.select(Flights, TreesFlightsJoinTbl, Bands)
.select_from(Bands)
.join(
TreesFlightsJoinTbl,
(Bands.trees_pk == TreesFlightsJoinTbl.trees_pk) & (Bands.flights_pk == TreesFlightsJoinTbl.flights_pk),
)
.join(Trees, Bands.trees_pk == Trees.trees_pk)
.join(Flights, Bands.flights_pk == Flights.flights_pk)
.where(Trees.seq_id == seq_id, Bands.band == band)
.order_by(Flights.flight_date)
).all()
if len(rows) == 0:
return BARUtils.error_exit("No data found for the given parameters"), 400
res = [
{
"flight_date": f.flight_date.isoformat(),
"flights_pk": f.flights_pk,
"confidence": float(tf.confidence) if tf.confidence is not None else None,
"band_value": float(b.value),
}
for f, tf, b in rows
]
return BARUtils.success_exit(res)
@fastpheno.route("/timeseries/genotype/<string:tree_site_id>/<string:band>/aggregate")
class FastPhenoGenotypeTimeSeries(Resource):
@fastpheno.param("tree_site_id", _in="path", default="619")
@fastpheno.param("band", _in="path", default="398nm")
@fastpheno.param("site", _in="query", required=False, default="Pintendre")
def get(self, tree_site_id, band):
"""Returns AVG and STDDEV of band values per flight date across all trees sharing a
genotype (e.g. '619' matches all tree_site_id values starting with '619.'). Intended
for genotype-level time series plotting. Optionally filter by site name."""
tree_site_id = str(escape(tree_site_id))
band = str(escape(band))
site = request.args.get("site")
if not re.search(r"^[a-zA-Z0-9.]{1,15}$", tree_site_id):
return BARUtils.error_exit("Invalid tree site ID"), 400
if not re.search(r"^[a-zA-Z0-9_]{1,20}$", band):
return BARUtils.error_exit("Invalid band"), 400
if site is not None:
site = str(escape(site)).capitalize()
if not re.search(r"^[a-zA-Z]{1,15}$", site):
return BARUtils.error_exit("Invalid site name"), 400
query = (
db.select(
Flights.flight_date,
Flights.flights_pk,
db.func.avg(Bands.value).label("avg_value"),
db.func.std(Bands.value).label("std_value"),
db.func.count(Bands.value).label("n_trees"),
)
.select_from(Bands)
.join(
TreesFlightsJoinTbl,
(Bands.trees_pk == TreesFlightsJoinTbl.trees_pk) & (Bands.flights_pk == TreesFlightsJoinTbl.flights_pk),
)
.join(Trees, Bands.trees_pk == Trees.trees_pk)
.join(Flights, Bands.flights_pk == Flights.flights_pk)
.join(Sites, Flights.sites_pk == Sites.sites_pk)
.where(
db.or_(Trees.tree_site_id == tree_site_id, Trees.tree_site_id.like(f"{tree_site_id}.%")),
Bands.band == band,
)
.group_by(Flights.flights_pk, Flights.flight_date)
.order_by(Flights.flight_date)
)
if site is not None:
query = query.where(Sites.site_name == site)
rows = db.session.execute(query).all()
if len(rows) == 0:
return BARUtils.error_exit("No data found for the given parameters"), 400
res = [
{
"flight_date": r.flight_date.isoformat(),
"flights_pk": r.flights_pk,
"avg_value": float(r.avg_value),
"std_value": float(r.std_value) if r.std_value is not None else None,
"n_trees": r.n_trees,
}
for r in rows
]
return BARUtils.success_exit(res)
@fastpheno.route("/sites")
class FastPhenoSites(Resource):
def get(self):
"""Returns all sites with coordinates, for initializing the map view."""
rows = db.session.execute(db.select(Sites).order_by(Sites.site_name)).scalars().all()
res = [
{
"sites_pk": s.sites_pk,
"site_name": s.site_name,
"lat": float(s.lat),
"lng": float(s.lng),
"site_desc": s.site_desc,
}
for s in rows
]
return BARUtils.success_exit(res)
@fastpheno.route("/flights/<int:sites_pk>")
class FastPhenoFlights(Resource):
@fastpheno.param("sites_pk", _in="path", default=1)
def get(self, sites_pk):
"""Returns all flights for a given site, ordered by date, for populating the flight dropdown."""
if not BARUtils.is_integer(str(sites_pk)):
return BARUtils.error_exit("Invalid sites_pk"), 400
rows = (
db.session.execute(db.select(Flights).where(Flights.sites_pk == sites_pk).order_by(Flights.flight_date))
.scalars()
.all()
)
if len(rows) == 0:
return BARUtils.error_exit("No flights found for the given site"), 400
res = [
{
"flights_pk": f.flights_pk,
"flight_date": f.flight_date.isoformat(),
"pilot": f.pilot,
"height": float(f.height) if f.height is not None else None,
"speed": float(f.speed) if f.speed is not None else None,
}
for f in rows
]
return BARUtils.success_exit(res)
@fastpheno.route("/bands/available/<int:flights_pk>")
class FastPhenoBandsAvailable(Resource):
@fastpheno.param("flights_pk", _in="path", default=14)
def get(self, flights_pk):
"""Returns all distinct band names available for a given flight."""
if not BARUtils.is_integer(str(flights_pk)):
return BARUtils.error_exit("Invalid flights_pk"), 400
rows = (
db.session.execute(
db.select(Bands.band)
.where(Bands.flights_pk == flights_pk)
.distinct()
.order_by(db.func.cast(db.func.regexp_replace(Bands.band, "[^0-9]", ""), db.Integer))
)
.scalars()
.all()
)
if len(rows) == 0:
return BARUtils.error_exit("No bands found for the given flight"), 400
return BARUtils.success_exit(rows)