nmdc-runtime 2.10.0__py3-none-any.whl → 2.11.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nmdc-runtime might be problematic. Click here for more details.
- nmdc_runtime/Dockerfile +167 -0
- nmdc_runtime/api/analytics.py +22 -2
- nmdc_runtime/api/core/idgen.py +36 -6
- nmdc_runtime/api/db/mongo.py +0 -12
- nmdc_runtime/api/endpoints/find.py +65 -225
- nmdc_runtime/api/endpoints/lib/linked_instances.py +180 -0
- nmdc_runtime/api/endpoints/nmdcschema.py +65 -144
- nmdc_runtime/api/endpoints/objects.py +4 -11
- nmdc_runtime/api/endpoints/operations.py +0 -27
- nmdc_runtime/api/endpoints/queries.py +22 -0
- nmdc_runtime/api/endpoints/sites.py +0 -24
- nmdc_runtime/api/endpoints/util.py +57 -35
- nmdc_runtime/api/entrypoint.sh +7 -0
- nmdc_runtime/api/main.py +84 -60
- nmdc_runtime/api/models/util.py +12 -5
- nmdc_runtime/api/openapi.py +116 -180
- nmdc_runtime/api/swagger_ui/assets/custom-elements.js +522 -0
- nmdc_runtime/api/swagger_ui/assets/script.js +247 -0
- nmdc_runtime/api/swagger_ui/assets/style.css +155 -0
- nmdc_runtime/api/swagger_ui/swagger_ui.py +34 -0
- nmdc_runtime/minter/adapters/repository.py +21 -0
- nmdc_runtime/minter/domain/model.py +20 -0
- nmdc_runtime/site/changesheets/data/OmicsProcessing-to-catted-Biosamples.tsv +1561 -0
- nmdc_runtime/site/changesheets/scripts/missing_neon_soils_ecosystem_data.py +311 -0
- nmdc_runtime/site/changesheets/scripts/neon_soils_add_ncbi_ids.py +210 -0
- nmdc_runtime/site/dagster.yaml +53 -0
- nmdc_runtime/site/entrypoint-daemon.sh +26 -0
- nmdc_runtime/site/entrypoint-dagit-readonly.sh +26 -0
- nmdc_runtime/site/entrypoint-dagit.sh +26 -0
- nmdc_runtime/site/export/ncbi_xml.py +632 -11
- nmdc_runtime/site/export/ncbi_xml_utils.py +114 -0
- nmdc_runtime/site/graphs.py +7 -0
- nmdc_runtime/site/ops.py +92 -34
- nmdc_runtime/site/repository.py +2 -0
- nmdc_runtime/site/resources.py +16 -3
- nmdc_runtime/site/translation/submission_portal_translator.py +82 -14
- nmdc_runtime/site/workspace.yaml +13 -0
- nmdc_runtime/static/NMDC_logo.svg +1073 -0
- nmdc_runtime/static/ORCID-iD_icon_vector.svg +4 -0
- nmdc_runtime/static/README.md +5 -0
- nmdc_runtime/static/favicon.ico +0 -0
- nmdc_runtime/util.py +87 -1
- nmdc_runtime-2.11.0.dist-info/METADATA +46 -0
- {nmdc_runtime-2.10.0.dist-info → nmdc_runtime-2.11.0.dist-info}/RECORD +47 -57
- {nmdc_runtime-2.10.0.dist-info → nmdc_runtime-2.11.0.dist-info}/WHEEL +1 -2
- nmdc_runtime/api/endpoints/ids.py +0 -192
- nmdc_runtime/client/__init__.py +0 -0
- nmdc_runtime/containers.py +0 -14
- nmdc_runtime/core/__init__.py +0 -0
- nmdc_runtime/core/db/Database.py +0 -13
- nmdc_runtime/core/db/__init__.py +0 -0
- nmdc_runtime/core/exceptions/__init__.py +0 -23
- nmdc_runtime/core/exceptions/base.py +0 -47
- nmdc_runtime/core/exceptions/token.py +0 -13
- nmdc_runtime/domain/__init__.py +0 -0
- nmdc_runtime/domain/users/__init__.py +0 -0
- nmdc_runtime/domain/users/queriesInterface.py +0 -18
- nmdc_runtime/domain/users/userSchema.py +0 -37
- nmdc_runtime/domain/users/userService.py +0 -14
- nmdc_runtime/infrastructure/__init__.py +0 -0
- nmdc_runtime/infrastructure/database/__init__.py +0 -0
- nmdc_runtime/infrastructure/database/db.py +0 -3
- nmdc_runtime/infrastructure/database/models/__init__.py +0 -0
- nmdc_runtime/infrastructure/database/models/user.py +0 -1
- nmdc_runtime/lib/__init__.py +0 -1
- nmdc_runtime/lib/extract_nmdc_data.py +0 -33
- nmdc_runtime/lib/load_nmdc_data.py +0 -121
- nmdc_runtime/lib/nmdc_dataframes.py +0 -825
- nmdc_runtime/lib/nmdc_etl_class.py +0 -396
- nmdc_runtime/lib/transform_nmdc_data.py +0 -1117
- nmdc_runtime/site/drsobjects/__init__.py +0 -0
- nmdc_runtime/site/drsobjects/ingest.py +0 -93
- nmdc_runtime/site/drsobjects/registration.py +0 -131
- nmdc_runtime-2.10.0.dist-info/METADATA +0 -265
- nmdc_runtime-2.10.0.dist-info/top_level.txt +0 -1
- {nmdc_runtime-2.10.0.dist-info → nmdc_runtime-2.11.0.dist-info}/entry_points.txt +0 -0
- {nmdc_runtime-2.10.0.dist-info → nmdc_runtime-2.11.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""
|
|
2
|
+
|
|
3
|
+
This module houses logic for the `GET /nmdcschema/linked_instances` endpoint, defined as
|
|
4
|
+
`nmdc_runtime.api.endpoints.nmdcschema.linked_instances`, to avoid (further) bloating the
|
|
5
|
+
`nmdc_runtime.api.endpoints.nmdcschema` module.
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Literal, Any
|
|
10
|
+
|
|
11
|
+
from bson import ObjectId
|
|
12
|
+
from pymongo.collection import Collection as MongoCollection
|
|
13
|
+
from pymongo.database import Database as MongoDatabase
|
|
14
|
+
from toolz import merge
|
|
15
|
+
|
|
16
|
+
from nmdc_runtime.api.core.util import hash_from_str
|
|
17
|
+
from nmdc_runtime.util import get_class_name_to_collection_names_map, nmdc_schema_view
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def hash_from_ids_and_types(ids: list[str], types: list[str]) -> str:
|
|
21
|
+
"""A quick hash as a function of `ids` and `types`.
|
|
22
|
+
|
|
23
|
+
This will serve as part of a temporary mongo collection name.
|
|
24
|
+
Because it will only be "part of" the name, avoiding hash collisions isn't a priority.
|
|
25
|
+
|
|
26
|
+
Returns a hex digest truncated to 8 characters, so 16**8 ≈ 4M possible values.
|
|
27
|
+
"""
|
|
28
|
+
return hash_from_str(
|
|
29
|
+
",".join(sorted(ids)) + "." + ",".join(sorted(types)), algo="md5"
|
|
30
|
+
)[:8]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def temp_linked_instances_collection_name(ids: list[str], types: list[str]) -> str:
|
|
34
|
+
"""A name for a temporary mongo collection to store linked instances in service of an API request."""
|
|
35
|
+
return f"_runtime.tmp.linked_instances.{hash_from_ids_and_types(ids=ids,types=types)}.{ObjectId()}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def gather_linked_instances(
|
|
39
|
+
alldocs_collection: MongoCollection,
|
|
40
|
+
ids: list[str],
|
|
41
|
+
types: list[str],
|
|
42
|
+
) -> str:
|
|
43
|
+
"""Collect linked instances and stores them in a new temporary collection.
|
|
44
|
+
|
|
45
|
+
Run an aggregation pipeline over `alldocs_collection` that collects ∈`types` instances linked to `ids`.
|
|
46
|
+
The pipeline is run twice, once for each of {"downstream", "upstream"} directions.
|
|
47
|
+
"""
|
|
48
|
+
merge_into_collection_name = temp_linked_instances_collection_name(
|
|
49
|
+
ids=ids, types=types
|
|
50
|
+
)
|
|
51
|
+
for direction in ["downstream", "upstream"]:
|
|
52
|
+
_ = list(
|
|
53
|
+
alldocs_collection.aggregate(
|
|
54
|
+
pipeline_for_direction(
|
|
55
|
+
ids=ids,
|
|
56
|
+
types=types,
|
|
57
|
+
direction=direction,
|
|
58
|
+
merge_into_collection_name=merge_into_collection_name,
|
|
59
|
+
),
|
|
60
|
+
allowDiskUse=True,
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
return merge_into_collection_name
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def pipeline_for_direction(
|
|
67
|
+
ids: list[str],
|
|
68
|
+
types: list[str],
|
|
69
|
+
direction: Literal["downstream", "upstream"],
|
|
70
|
+
merge_into_collection_name: str,
|
|
71
|
+
alldocs_collection_name: str = "alldocs",
|
|
72
|
+
) -> list:
|
|
73
|
+
"""A pure function that returns the aggregation pipeline for `direction`.
|
|
74
|
+
|
|
75
|
+
The pipeline
|
|
76
|
+
- collects ∈`types` instances linked to `ids` along `direction`,
|
|
77
|
+
- retains only those document fields essential to the caller, and
|
|
78
|
+
- ensures the collected instances are present, and properly updated if applicable, in a merge-target collection.
|
|
79
|
+
"""
|
|
80
|
+
return pipeline_for_instances_linked_to_ids_by_direction(
|
|
81
|
+
ids=ids,
|
|
82
|
+
types=types,
|
|
83
|
+
direction=direction,
|
|
84
|
+
alldocs_collection_name=alldocs_collection_name,
|
|
85
|
+
) + [
|
|
86
|
+
{"$project": {"id": 1, "type": 1, f"_{direction}_of": 1}},
|
|
87
|
+
pipeline_stage_for_merging_instances_and_grouping_link_provenance_by_direction(
|
|
88
|
+
merge_into_collection_name=merge_into_collection_name, direction=direction
|
|
89
|
+
),
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def pipeline_for_instances_linked_to_ids_by_direction(
|
|
94
|
+
ids: list[str],
|
|
95
|
+
types: list[str],
|
|
96
|
+
direction: Literal["downstream", "upstream"],
|
|
97
|
+
alldocs_collection_name: str = "alldocs",
|
|
98
|
+
slim: bool = True,
|
|
99
|
+
) -> list[dict[str, Any]]:
|
|
100
|
+
"""
|
|
101
|
+
Returns an aggregation pipeline that:
|
|
102
|
+
- traverses the graph of documents in the alldocs collection, following `direction`-specific relationships
|
|
103
|
+
to discover documents linked to the documents given by `ids`.
|
|
104
|
+
- `$unwind`s the collected (via `$graphLookup`) docs,
|
|
105
|
+
- filters them by given `types` of interest,
|
|
106
|
+
- adds bookkeeping information about `direction`ality, and
|
|
107
|
+
- (optionally) projects only essential fields to reduce response latency and size.
|
|
108
|
+
"""
|
|
109
|
+
return [
|
|
110
|
+
{"$match": {"id": {"$in": ids}}},
|
|
111
|
+
{
|
|
112
|
+
"$graphLookup": {
|
|
113
|
+
"from": alldocs_collection_name,
|
|
114
|
+
"startWith": f"$_{direction}.id",
|
|
115
|
+
"connectFromField": f"_{direction}.id",
|
|
116
|
+
"connectToField": "id",
|
|
117
|
+
"as": f"{direction}_docs",
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
{"$unwind": {"path": f"${direction}_docs"}},
|
|
121
|
+
{"$match": {f"{direction}_docs._type_and_ancestors": {"$in": types}}},
|
|
122
|
+
{"$addFields": {f"{direction}_docs._{direction}_of": ["$id"]}},
|
|
123
|
+
{"$replaceRoot": {"newRoot": f"${direction}_docs"}},
|
|
124
|
+
] + ([{"$project": {"id": 1, "type": 1, f"_{direction}_of": 1}}] if slim else [])
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def pipeline_stage_for_merging_instances_and_grouping_link_provenance_by_direction(
|
|
128
|
+
merge_into_collection_name: str,
|
|
129
|
+
direction: Literal["downstream", "upstream"],
|
|
130
|
+
) -> dict[str, Any]:
|
|
131
|
+
"""
|
|
132
|
+
Returns an aggregation-pipeline step that merges its input document stream to a collection dedicated to serving
|
|
133
|
+
the caller in a manner amenable to pagination across multiple HTTP requests.
|
|
134
|
+
"""
|
|
135
|
+
return {
|
|
136
|
+
"$merge": {
|
|
137
|
+
"into": merge_into_collection_name,
|
|
138
|
+
"on": "_id",
|
|
139
|
+
"whenMatched": [
|
|
140
|
+
{
|
|
141
|
+
"$set": {
|
|
142
|
+
f"_{direction}_of": {
|
|
143
|
+
"$setUnion": [
|
|
144
|
+
f"$_{direction}_of",
|
|
145
|
+
f"$$new._{direction}_of",
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
],
|
|
151
|
+
"whenNotMatched": "insert",
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def hydrated(resources: list[dict], mdb: MongoDatabase) -> list[dict]:
|
|
157
|
+
"""Replace each `dict` in `resources` with a hydrated version.
|
|
158
|
+
|
|
159
|
+
Instead of returning the retrieved "full" documents as is, we merge each one with (a copy of) the corresponding
|
|
160
|
+
original document in *resources*, which includes additional fields, e.g. `_upstream_of` and `_downstream_of`.
|
|
161
|
+
"""
|
|
162
|
+
class_name_to_collection_names_map = get_class_name_to_collection_names_map(
|
|
163
|
+
nmdc_schema_view()
|
|
164
|
+
)
|
|
165
|
+
types_of_resources = {r["type"] for r in resources}
|
|
166
|
+
full_docs_by_id = {}
|
|
167
|
+
|
|
168
|
+
for type in types_of_resources:
|
|
169
|
+
resource_ids_of_type = [d["id"] for d in resources if d["type"] == type]
|
|
170
|
+
schema_collection = mdb.get_collection(
|
|
171
|
+
# Note: We are assuming that documents of a given type are only allowed (by the schema) to reside in one
|
|
172
|
+
# collection. Based on that assumption, we will query only the _first_ collection whose name we get from
|
|
173
|
+
# the map. This assumption is continuously verified prior to code deployment via
|
|
174
|
+
# `test_get_class_name_to_collection_names_map_has_one_and_only_one_collection_name_per_class_name`.
|
|
175
|
+
class_name_to_collection_names_map[type.removeprefix("nmdc:")][0]
|
|
176
|
+
)
|
|
177
|
+
for doc in schema_collection.find({"id": {"$in": resource_ids_of_type}}):
|
|
178
|
+
full_docs_by_id[doc["id"]] = doc
|
|
179
|
+
|
|
180
|
+
return [merge(r, full_docs_by_id[r["id"]]) for r in resources]
|
|
@@ -10,6 +10,10 @@ from refscan.lib.helpers import (
|
|
|
10
10
|
get_names_of_classes_eligible_for_collection,
|
|
11
11
|
)
|
|
12
12
|
|
|
13
|
+
from nmdc_runtime.api.endpoints.lib.linked_instances import (
|
|
14
|
+
gather_linked_instances,
|
|
15
|
+
hydrated,
|
|
16
|
+
)
|
|
13
17
|
from nmdc_runtime.config import IS_LINKED_INSTANCES_ENDPOINT_ENABLED
|
|
14
18
|
from nmdc_runtime.minter.config import typecodes
|
|
15
19
|
from nmdc_runtime.minter.domain.model import check_valid_ids
|
|
@@ -118,7 +122,7 @@ def get_nmdc_database_collection_stats(
|
|
|
118
122
|
@decorate_if(condition=IS_LINKED_INSTANCES_ENDPOINT_ENABLED)(
|
|
119
123
|
router.get(
|
|
120
124
|
"/nmdcschema/linked_instances",
|
|
121
|
-
response_model=ListResponse,
|
|
125
|
+
response_model=ListResponse[Doc],
|
|
122
126
|
response_model_exclude_unset=True,
|
|
123
127
|
)
|
|
124
128
|
)
|
|
@@ -147,23 +151,54 @@ def get_linked_instances(
|
|
|
147
151
|
examples=["nmdc:bsm-11-abc123"],
|
|
148
152
|
),
|
|
149
153
|
] = None,
|
|
154
|
+
hydrate: Annotated[
|
|
155
|
+
bool,
|
|
156
|
+
Query(
|
|
157
|
+
title="Hydrate",
|
|
158
|
+
description="Whether to include full documents in the response. The default is to include slim documents.",
|
|
159
|
+
),
|
|
160
|
+
] = False,
|
|
161
|
+
page_token: Annotated[
|
|
162
|
+
str | None,
|
|
163
|
+
Query(
|
|
164
|
+
title="Next page token",
|
|
165
|
+
description="""A bookmark you can use to fetch the _next_ page of resources. You can get this from the
|
|
166
|
+
`next_page_token` field in a previous response from this endpoint.\n\n_Example_:
|
|
167
|
+
`nmdc:sys0zr0fbt71`""",
|
|
168
|
+
examples=[
|
|
169
|
+
"nmdc:sys0zr0fbt71",
|
|
170
|
+
],
|
|
171
|
+
),
|
|
172
|
+
] = None,
|
|
173
|
+
max_page_size: Annotated[
|
|
174
|
+
int,
|
|
175
|
+
Query(
|
|
176
|
+
title="Resources per page",
|
|
177
|
+
description="How many resources you want _each page_ to contain, formatted as a positive integer.",
|
|
178
|
+
examples=[20],
|
|
179
|
+
),
|
|
180
|
+
] = 20,
|
|
150
181
|
mdb: MongoDatabase = Depends(get_mongo_db),
|
|
151
182
|
):
|
|
152
183
|
"""
|
|
153
184
|
Retrieves database instances that are both (a) linked to any of `ids`, and (b) of a type in `types`.
|
|
154
185
|
|
|
155
|
-
An [instance](https://linkml.io/linkml-model/latest/docs/specification/02instances/) is an object conforming to
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
186
|
+
An [instance](https://linkml.io/linkml-model/latest/docs/specification/02instances/) is an object conforming to a
|
|
187
|
+
class definition ([linkml:ClassDefinition](https://w3id.org/linkml/ClassDefinition)) in our database ([
|
|
188
|
+
nmdc:Database](https://w3id.org/nmdc/Database)). While a [nmdc:Database](https://w3id.org/nmdc/Database) is
|
|
189
|
+
organized into collections, every item in every database collection -- that is, every instance -- knows its
|
|
190
|
+
`type`, so we can (and here do) return a simple list of instances ([a LinkML CollectionInstance](
|
|
191
|
+
https://linkml.io/linkml-model/latest/docs/specification/02instances/#collections)). If hydrate is `False` (the
|
|
192
|
+
default), then the returned list contains "slim" documents that include only the `id` and `type` of each
|
|
193
|
+
instance. If hydrate is `True`, then the returned list contains "full" (aka <a
|
|
194
|
+
href="https://en.wikipedia.org/wiki/Hydration_(web_development)">"hydrated"</a>) documents of each instance,
|
|
195
|
+
suitable e.g. for a client to subsequently use to construct a corresponding
|
|
196
|
+
[nmdc:Database](https://w3id.org/nmdc/Database) instance with schema-compliant documents.
|
|
197
|
+
Both "slim" and "full" documents include (optional) `_upstream_of` and `_downstream_of` fields,
|
|
198
|
+
to indicate the returned document's relationship to `ids`.
|
|
199
|
+
|
|
200
|
+
From the nexus instance IDs given in `ids`, both "upstream" and "downstream" links are followed (transitively)
|
|
201
|
+
to collect the set of all instances linked to these `ids`.
|
|
167
202
|
|
|
168
203
|
* A link "upstream" is represented by a slot ([linkml:SlotDefinition](https://w3id.org/linkml/SlotDefinition))
|
|
169
204
|
for which the
|
|
@@ -186,16 +221,15 @@ def get_linked_instances(
|
|
|
186
221
|
[nmdc:InformationObject](https://w3id.org/nmdc/InformationObject),
|
|
187
222
|
[nmdc:Sample](https://w3id.org/nmdc/Sample), etc. -- may be given.
|
|
188
223
|
If no value for `types` is given, then all [nmdc:NamedThing](https://w3id.org/nmdc/NamedThing)s are returned.
|
|
189
|
-
|
|
190
|
-
<sup>†</sup>: actually, we do not (yet).
|
|
191
|
-
For now (see [microbiomedata/nmdc-runtime#1118](https://github.com/microbiomedata/nmdc-runtime/issues/1118)),
|
|
192
|
-
we return a short list of "fat" documents, each of which represents one of the `ids` and presents
|
|
193
|
-
representations of that id's downstream and upstream instances (currently just each instance's `id` and `type`)
|
|
194
|
-
as separate subdocument array fields.
|
|
195
224
|
"""
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
225
|
+
if page_token is not None:
|
|
226
|
+
rv = list_resources(
|
|
227
|
+
req=ListRequest(page_token=page_token, max_page_size=max_page_size), mdb=mdb
|
|
228
|
+
)
|
|
229
|
+
rv["resources"] = hydrated(rv["resources"], mdb) if hydrate else rv["resources"]
|
|
230
|
+
rv["resources"] = [strip_oid(d) for d in rv["resources"]]
|
|
231
|
+
return rv
|
|
232
|
+
|
|
199
233
|
ids_found = [d["id"] for d in mdb.alldocs.find({"id": {"$in": ids}}, {"id": 1})]
|
|
200
234
|
ids_not_found = list(set(ids) - set(ids_found))
|
|
201
235
|
if ids_not_found:
|
|
@@ -217,131 +251,18 @@ def get_linked_instances(
|
|
|
217
251
|
),
|
|
218
252
|
)
|
|
219
253
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
# the entities with documents identified by `ids`. It unwinds the collected (via `$graphLookup`) upstream docs,
|
|
223
|
-
# filters them by given `types` of interest, projects only essential fields to reduce response latency and size,
|
|
224
|
-
# and groups them by each of the given `ids`, i.e. re-winding the `$unwind`-ed upstream docs into an array for each
|
|
225
|
-
# given ID.
|
|
226
|
-
upstream_docs = list(
|
|
227
|
-
mdb.alldocs.aggregate(
|
|
228
|
-
[
|
|
229
|
-
{"$match": {"id": {"$in": ids}}},
|
|
230
|
-
{
|
|
231
|
-
"$graphLookup": {
|
|
232
|
-
"from": "alldocs",
|
|
233
|
-
"startWith": "$_upstream.id",
|
|
234
|
-
"connectFromField": "_upstream.id",
|
|
235
|
-
"connectToField": "id",
|
|
236
|
-
"as": "upstream_docs",
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
{"$unwind": {"path": "$upstream_docs"}},
|
|
240
|
-
{"$match": {"upstream_docs._type_and_ancestors": {"$in": types}}},
|
|
241
|
-
{"$project": {"id": 1, "upstream_docs": "$upstream_docs"}},
|
|
242
|
-
{
|
|
243
|
-
"$group": {
|
|
244
|
-
"_id": "$id",
|
|
245
|
-
"upstream_docs": {
|
|
246
|
-
"$addToSet": {
|
|
247
|
-
"id": "$upstream_docs.id",
|
|
248
|
-
"type": "$upstream_docs.type",
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
}
|
|
252
|
-
},
|
|
253
|
-
{
|
|
254
|
-
"$lookup": {
|
|
255
|
-
"from": "alldocs",
|
|
256
|
-
"localField": "_id",
|
|
257
|
-
"foreignField": "id",
|
|
258
|
-
"as": "selves",
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
{
|
|
262
|
-
"$project": {
|
|
263
|
-
"_id": 0,
|
|
264
|
-
"id": "$_id",
|
|
265
|
-
"upstream_docs": 1,
|
|
266
|
-
"type": {"$arrayElemAt": ["$selves.type", 0]},
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
],
|
|
270
|
-
allowDiskUse=True,
|
|
271
|
-
)
|
|
254
|
+
merge_into_collection_name = gather_linked_instances(
|
|
255
|
+
alldocs_collection=mdb.alldocs, ids=ids, types=types
|
|
272
256
|
)
|
|
273
257
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
# reduce response latency and size, and groups them by each of the given `ids`, i.e. re-winding the `$unwind`-ed
|
|
279
|
-
# downstream docs into an array for each given ID.
|
|
280
|
-
downstream_docs = list(
|
|
281
|
-
mdb.alldocs.aggregate(
|
|
282
|
-
[
|
|
283
|
-
{"$match": {"id": {"$in": ids}}},
|
|
284
|
-
{
|
|
285
|
-
"$graphLookup": {
|
|
286
|
-
"from": "alldocs",
|
|
287
|
-
"startWith": "$_downstream.id",
|
|
288
|
-
"connectFromField": "_downstream.id",
|
|
289
|
-
"connectToField": "id",
|
|
290
|
-
"as": "downstream_docs",
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
{"$unwind": {"path": "$downstream_docs"}},
|
|
294
|
-
{"$match": {"downstream_docs._type_and_ancestors": {"$in": types}}},
|
|
295
|
-
{
|
|
296
|
-
"$group": {
|
|
297
|
-
"_id": "$id",
|
|
298
|
-
"downstream_docs": {
|
|
299
|
-
"$addToSet": {
|
|
300
|
-
"id": "$downstream_docs.id",
|
|
301
|
-
"type": "$downstream_docs.type",
|
|
302
|
-
}
|
|
303
|
-
},
|
|
304
|
-
}
|
|
305
|
-
},
|
|
306
|
-
{
|
|
307
|
-
"$lookup": {
|
|
308
|
-
"from": "alldocs",
|
|
309
|
-
"localField": "_id",
|
|
310
|
-
"foreignField": "id",
|
|
311
|
-
"as": "selves",
|
|
312
|
-
}
|
|
313
|
-
},
|
|
314
|
-
{
|
|
315
|
-
"$project": {
|
|
316
|
-
"_id": 0,
|
|
317
|
-
"id": "$_id",
|
|
318
|
-
"downstream_docs": 1,
|
|
319
|
-
"type": {"$arrayElemAt": ["$selves.type", 0]},
|
|
320
|
-
}
|
|
321
|
-
},
|
|
322
|
-
],
|
|
323
|
-
allowDiskUse=True,
|
|
324
|
-
)
|
|
258
|
+
rv = list_resources(
|
|
259
|
+
ListRequest(page_token=page_token, max_page_size=max_page_size),
|
|
260
|
+
mdb,
|
|
261
|
+
merge_into_collection_name,
|
|
325
262
|
)
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
"id": id_,
|
|
330
|
-
"upstream_docs": [],
|
|
331
|
-
"downstream_docs": [],
|
|
332
|
-
}
|
|
333
|
-
for id_ in ids
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
# For each subject document that was upstream of or downstream of any documents, create a dictionary
|
|
337
|
-
# containing that subject document's `id`, its `type`, and the list of `id`s of the
|
|
338
|
-
# documents that it for upstream or or downstream of.
|
|
339
|
-
for d in upstream_docs + downstream_docs:
|
|
340
|
-
relations_by_id[d["id"]]["type"] = d["type"]
|
|
341
|
-
relations_by_id[d["id"]]["upstream_docs"] += d.get("upstream_docs", [])
|
|
342
|
-
relations_by_id[d["id"]]["downstream_docs"] += d.get("downstream_docs", [])
|
|
343
|
-
|
|
344
|
-
return {"resources": list(relations_by_id.values())}
|
|
263
|
+
rv["resources"] = hydrated(rv["resources"], mdb) if hydrate else rv["resources"]
|
|
264
|
+
rv["resources"] = [strip_oid(d) for d in rv["resources"]]
|
|
265
|
+
return rv
|
|
345
266
|
|
|
346
267
|
|
|
347
268
|
@router.get(
|
|
@@ -124,9 +124,8 @@ def get_object_info(
|
|
|
124
124
|
)
|
|
125
125
|
if object_id.startswith("sty-"):
|
|
126
126
|
url_to_try = f"https://data.microbiomedata.org/api/study/nmdc:{object_id}"
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
) # TODO use HEAD when enabled upstream
|
|
127
|
+
# TODO: Update this HTTP request to use the HTTP "HEAD" method once the upstream endpoint supports that method.
|
|
128
|
+
rv = requests.get(url_to_try, allow_redirects=True)
|
|
130
129
|
if rv.status_code != 404:
|
|
131
130
|
return RedirectResponse(
|
|
132
131
|
f"https://data.microbiomedata.org/details/study/nmdc:{object_id}",
|
|
@@ -134,9 +133,8 @@ def get_object_info(
|
|
|
134
133
|
)
|
|
135
134
|
elif object_id.startswith("bsm-"):
|
|
136
135
|
url_to_try = f"https://data.microbiomedata.org/api/biosample/nmdc:{object_id}"
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
) # TODO use HEAD when enabled upstream
|
|
136
|
+
# TODO: Update this HTTP request to use the HTTP "HEAD" method once the upstream endpoint supports that method.
|
|
137
|
+
rv = requests.get(url_to_try, allow_redirects=True)
|
|
140
138
|
if rv.status_code != 404:
|
|
141
139
|
return RedirectResponse(
|
|
142
140
|
f"https://data.microbiomedata.org/details/sample/nmdc:{object_id}",
|
|
@@ -270,8 +268,3 @@ def update_object(
|
|
|
270
268
|
doc_object_patched = merge(doc, object_patch.model_dump(exclude_unset=True))
|
|
271
269
|
mdb.operations.replace_one({"id": object_id}, doc_object_patched)
|
|
272
270
|
return doc_object_patched
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
@router.put("/objects/{object_id}", response_model=DrsObject)
|
|
276
|
-
def replace_object():
|
|
277
|
-
pass
|
|
@@ -76,30 +76,3 @@ def update_operation(
|
|
|
76
76
|
)
|
|
77
77
|
mdb.operations.replace_one({"id": op_id}, doc_op_patched)
|
|
78
78
|
return doc_op_patched
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
@router.post(
|
|
82
|
-
"/operations/{op_id}:wait",
|
|
83
|
-
description=(
|
|
84
|
-
"Wait until the operation is resolved or rejected before returning the result."
|
|
85
|
-
" This is a 'blocking' alternative to client-side polling, and may not be available"
|
|
86
|
-
" for operation types know to be particularly long-running."
|
|
87
|
-
),
|
|
88
|
-
)
|
|
89
|
-
def wait_operation():
|
|
90
|
-
pass
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
@router.post("/operations/{op_id}:cancel")
|
|
94
|
-
def cancel_operation():
|
|
95
|
-
pass
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
@router.post("/operations/{op_id}:pause")
|
|
99
|
-
def pause_operation():
|
|
100
|
-
pass
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
@router.post("/operations/{op_id}:resume")
|
|
104
|
-
def resume_operation():
|
|
105
|
-
pass
|
|
@@ -175,6 +175,28 @@ def run_query(
|
|
|
175
175
|
}
|
|
176
176
|
```
|
|
177
177
|
|
|
178
|
+
Get a specific study and all the biosamples associated with that study.
|
|
179
|
+
```
|
|
180
|
+
{
|
|
181
|
+
"aggregate": "study_set",
|
|
182
|
+
"pipeline": [
|
|
183
|
+
{
|
|
184
|
+
"$match": {
|
|
185
|
+
"id": "nmdc:sty-11-8fb6t785"
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
"$lookup": {
|
|
190
|
+
"from": "biosample_set",
|
|
191
|
+
"localField": "id",
|
|
192
|
+
"foreignField": "associated_studies",
|
|
193
|
+
"as": "biosamples_of_study"
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
178
200
|
Use the `cursor.id` from a previous response to get the next batch of results,
|
|
179
201
|
whether that batch is empty or non-empty.
|
|
180
202
|
```
|
|
@@ -87,30 +87,6 @@ def get_site(
|
|
|
87
87
|
return raise404_if_none(mdb.sites.find_one({"id": site_id}))
|
|
88
88
|
|
|
89
89
|
|
|
90
|
-
@router.patch("/sites/{site_id}", include_in_schema=False)
|
|
91
|
-
def update_site():
|
|
92
|
-
"""Not yet implemented"""
|
|
93
|
-
pass
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
@router.put("/sites/{site_id}", include_in_schema=False)
|
|
97
|
-
def replace_site():
|
|
98
|
-
"""Not yet implemented"""
|
|
99
|
-
pass
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
@router.get("/sites/{site_id}/capabilities", include_in_schema=False)
|
|
103
|
-
def list_site_capabilities(site_id: str):
|
|
104
|
-
"""Not yet implemented"""
|
|
105
|
-
pass
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
@router.put("/sites/{site_id}/capabilities", include_in_schema=False)
|
|
109
|
-
def replace_site_capabilities(site_id: str, capability_ids: List[str]):
|
|
110
|
-
"""Not yet implemented"""
|
|
111
|
-
pass
|
|
112
|
-
|
|
113
|
-
|
|
114
90
|
def verify_client_site_pair(
|
|
115
91
|
site_id: str,
|
|
116
92
|
mdb: pymongo.database.Database = Depends(get_mongo_db),
|