-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfind.py
284 lines (246 loc) · 8.24 KB
/
find.py
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
from operator import itemgetter
from fastapi import APIRouter, Depends, Form
from jinja2 import Environment, PackageLoader, select_autoescape
from nmdc_runtime.util import get_nmdc_jsonschema_dict, nmdc_activity_collection_names
from pymongo.database import Database as MongoDatabase
from starlette.responses import HTMLResponse
from toolz import merge, assoc_in
from nmdc_runtime.api.core.util import raise404_if_none
from nmdc_runtime.api.db.mongo import get_mongo_db, activity_collection_names
from nmdc_runtime.api.endpoints.util import (
find_resources,
strip_oid,
find_resources_spanning,
pipeline_find_resources,
)
from nmdc_runtime.api.models.metadata import Doc
from nmdc_runtime.api.models.util import (
FindResponse,
FindRequest,
entity_attributes_to_index,
PipelineFindRequest,
PipelineFindResponse,
)
router = APIRouter()
@router.get(
"/studies",
response_model=FindResponse,
response_model_exclude_unset=True,
)
def find_studies(
req: FindRequest = Depends(),
mdb: MongoDatabase = Depends(get_mongo_db),
):
return find_resources(req, mdb, "study_set")
@router.get(
"/studies/{study_id}",
response_model=Doc,
response_model_exclude_unset=True,
)
def find_study_by_id(
study_id: str,
mdb: MongoDatabase = Depends(get_mongo_db),
):
return strip_oid(raise404_if_none(mdb["study_set"].find_one({"id": study_id})))
@router.get(
"/biosamples",
response_model=FindResponse,
response_model_exclude_unset=True,
)
def find_biosamples(
req: FindRequest = Depends(),
mdb: MongoDatabase = Depends(get_mongo_db),
):
return find_resources(req, mdb, "biosample_set")
@router.get(
"/biosamples/{sample_id}",
response_model=Doc,
response_model_exclude_unset=True,
)
def find_biosample_by_id(
sample_id: str,
mdb: MongoDatabase = Depends(get_mongo_db),
):
return strip_oid(raise404_if_none(mdb["biosample_set"].find_one({"id": sample_id})))
@router.get(
"/data_objects",
response_model=FindResponse,
response_model_exclude_unset=True,
)
def find_data_objects(
req: FindRequest = Depends(),
mdb: MongoDatabase = Depends(get_mongo_db),
):
return find_resources(req, mdb, "data_object_set")
@router.get(
"/data_objects/study/{study_id}",
response_model_exclude_unset=True,
)
def find_data_objects_for_study(
study_id: str,
mdb: MongoDatabase = Depends(get_mongo_db),
):
rv = {"biosample_set": {}, "data_object_set": []}
data_object_ids = set()
study = raise404_if_none(
mdb.study_set.find_one({"id": study_id}, ["id"]), detail="Study not found"
)
for biosample in mdb.biosample_set.find({"part_of": study["id"]}, ["id"]):
rv["biosample_set"][biosample["id"]] = {"omics_processing_set": {}}
for opa in mdb.omics_processing_set.find(
{"has_input": biosample["id"]}, ["id", "has_output"]
):
rv["biosample_set"][biosample["id"]]["omics_processing_set"][opa["id"]] = {
"has_output": {}
}
for do_id in opa.get("has_output", []):
data_object_ids.add(do_id)
rv["biosample_set"][biosample["id"]]["omics_processing_set"][opa["id"]][
"has_output"
][do_id] = {}
for coll_name in nmdc_activity_collection_names():
acts = list(
mdb[coll_name].find({"has_input": do_id}, ["id", "has_output"])
)
if acts:
data_object_ids |= {
do for act in acts for do in act.get("has_output", [])
}
rv["biosample_set"][biosample["id"]]["omics_processing_set"][
opa["id"]
]["has_output"][do_id][coll_name] = {
act["id"]: act.get("has_output", []) for act in acts
}
rv["data_object_set"] = [
strip_oid(d)
for d in mdb.data_object_set.find({"id": {"$in": list(data_object_ids)}})
]
return rv
@router.get(
"/data_objects/{data_object_id}",
response_model=Doc,
response_model_exclude_unset=True,
)
def find_data_object_by_id(
data_object_id: str,
mdb: MongoDatabase = Depends(get_mongo_db),
):
return strip_oid(
raise404_if_none(mdb["data_object_set"].find_one({"id": data_object_id}))
)
@router.get(
"/activities",
response_model=FindResponse,
response_model_exclude_unset=True,
)
def find_activities(
req: FindRequest = Depends(),
mdb: MongoDatabase = Depends(get_mongo_db),
):
return find_resources_spanning(req, mdb, activity_collection_names(mdb))
@router.get(
"/activities/{activity_id}",
response_model=Doc,
response_model_exclude_unset=True,
)
def find_activity_by_id(
activity_id: str,
mdb: MongoDatabase = Depends(get_mongo_db),
):
doc = None
for name in activity_collection_names(mdb):
doc = mdb[name].find_one({"id": activity_id})
if doc is not None:
return strip_oid(doc)
return raise404_if_none(doc)
jinja_env = Environment(
loader=PackageLoader("nmdc_runtime"), autoescape=select_autoescape()
)
def attr_index_sort_key(attr):
return "_" if attr == "id" else attr
def documentation_links(jsonschema_dict, collection_names):
rv = {"Activity": []}
for cn in collection_names:
last_part = jsonschema_dict["$defs"]["Database"]["properties"][cn]["items"][
"$ref"
].split("/")[-1]
entity_attrs = list(jsonschema_dict["$defs"][last_part]["properties"])
if last_part in ("Biosample", "Study", "DataObject"):
assoc_path = [cn]
else:
assoc_path = ["activity_set", cn]
rv = assoc_in(
rv,
assoc_path,
{
"collection_name": cn,
"entity_url": "https://microbiomedata.github.io/nmdc-schema/"
+ last_part,
"entity_name": last_part,
"entity_attrs": sorted(
[
{
"url": f"https://microbiomedata.github.io/nmdc-schema/{a}",
"attr_name": a,
}
for a in entity_attrs
],
key=itemgetter("attr_name"),
),
},
)
return rv
@router.get("/search", response_class=HTMLResponse)
def search_page(
mdb: MongoDatabase = Depends(get_mongo_db),
):
template = jinja_env.get_template("search.html")
indexed_entity_attributes = merge(
{n: {"id"} for n in activity_collection_names(mdb)},
{
coll: sorted(attrs | {"id"}, key=attr_index_sort_key)
for coll, attrs in entity_attributes_to_index.items()
},
)
doc_links = documentation_links(
get_nmdc_jsonschema_dict(),
(
list(activity_collection_names(mdb))
+ ["biosample_set", "study_set", "data_object_set"]
),
)
html_content = template.render(
activity_collection_names=sorted(activity_collection_names(mdb)),
indexed_entity_attributes=indexed_entity_attributes,
doc_links=doc_links,
)
return HTMLResponse(content=html_content, status_code=200)
@router.post(
"/pipeline_search",
response_model=PipelineFindResponse,
response_model_exclude_unset=True,
)
def pipeline_search(
req: PipelineFindRequest = Depends(),
mdb: MongoDatabase = Depends(get_mongo_db),
):
return pipeline_find_resources(req, mdb)
@router.post(
"/pipeline_search_form",
response_model=PipelineFindResponse,
response_model_exclude_unset=True,
)
def pipeline_search(
pipeline_spec: str = Form(...),
description: str = Form(...),
mdb: MongoDatabase = Depends(get_mongo_db),
):
req = PipelineFindRequest(pipeline_spec=pipeline_spec, description=description)
return pipeline_find_resources(req, mdb)
@router.get("/pipeline_search", response_class=HTMLResponse)
def pipeline_search(
mdb: MongoDatabase = Depends(get_mongo_db),
):
template = jinja_env.get_template("pipeline_search.html")
html_content = template.render()
return HTMLResponse(content=html_content, status_code=200)