Skip to content

Commit b61185c

Browse files
Merge pull request #650 from bento-platform/chore/discovery/sort-nested-matches
chore(discovery): sort nested matches in match responses
2 parents ac8a39b + 8abc4f7 commit b61185c

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

chord_metadata_service/discovery/matches.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ async def experiment_result_matches(
105105
creation_date=er.creation_date,
106106
created_by=er.created_by,
107107
extra_properties=er.extra_properties,
108-
# ----------------------------------------------------------------------------------------------------
108+
# ------------------------------------------------------------------------------------------------------
109109
experiments=[v async for v in er.experiments.values_list("id", flat=True)],
110110
phenopacket=ctx.get("phenopacket") or (phenopacket.id if phenopacket else None),
111-
# ----------------------------------------------------------------------------------------------------
111+
# ------------------------------------------------------------------------------------------------------
112112
**(
113113
dict(
114114
project=scope.project_id or (str(first_exp.dataset.project_id) if first_exp else None),
@@ -135,20 +135,24 @@ async def experiment_matches(
135135
):
136136
# TODO: right now, experiment results are not filtered even if a query is executed on them.
137137
phenopacket = (await exp.biosample.phenopackets.afirst()) if exp.biosample else None # TODO: n+1?
138+
experiment_results = await experiment_result_matches(
139+
exp.experiment_results,
140+
scope,
141+
dt_permissions,
142+
False,
143+
{**ctx, "phenopacket": phenopacket.id if phenopacket else None, "experiment": str(exp.id)},
144+
)
145+
experiment_results.sort(key=lambda er: er.id)
138146
res.append(
139147
MatchExperiment(
140148
id=str(exp.id),
141149
experiment_type=exp.experiment_type,
142150
study_type=exp.study_type,
143-
results=await experiment_result_matches(
144-
exp.experiment_results,
145-
scope,
146-
dt_permissions,
147-
False,
148-
{**ctx, "phenopacket": phenopacket.id if phenopacket else None, "experiment": str(exp.id)},
149-
),
151+
results=experiment_results,
152+
# ------------------------------------------------------------------------------------------------------
150153
biosample=str(exp.biosample.id) if exp.biosample else None,
151154
phenopacket=str(phenopacket.id) if phenopacket else None,
155+
# ------------------------------------------------------------------------------------------------------
152156
**(dict(
153157
project=scope.project_id or str(exp.dataset.project_id),
154158
dataset=scope.dataset_id or str(exp.dataset_id)
@@ -173,24 +177,26 @@ async def biosample_matches(
173177
p = str(p_obj.id)
174178
ds = str(p_obj.dataset_id)
175179

180+
# TODO: prefetch all the time, even when not filtering?
181+
experiments = (
182+
await experiment_matches(
183+
getattr(b, "experiment_matches", b.experiments),
184+
scope,
185+
dt_permissions,
186+
False,
187+
{**ctx, "biosample": str(b.id)},
188+
)
189+
) if dt_permissions[DATA_TYPE_EXPERIMENT].data else None
190+
191+
if experiments:
192+
experiments.sort(key=lambda e: e.id)
193+
176194
res.append(
177195
MatchBiosample(
178196
id=str(b.id),
179197
individual_id=str(b.individual_id) if b.individual_id else None,
180198
phenopacket=p,
181-
experiments=(
182-
# TODO: prefetch all the time, even when not filtering?
183-
(
184-
await experiment_matches(
185-
getattr(b, "experiment_matches", b.experiments),
186-
scope,
187-
dt_permissions,
188-
False,
189-
{**ctx, "biosample": str(b.id)},
190-
)
191-
)
192-
if dt_permissions[DATA_TYPE_EXPERIMENT].data else None
193-
),
199+
experiments=experiments,
194200
**(dict(project=scope.project_id, dataset=ds) if root else dict()),
195201
)
196202
)
@@ -211,13 +217,16 @@ async def phenopacket_matches(
211217
s_id = phe.subject_id
212218
# TODO: prefetch all the time, even when not filtering.
213219
# TODO: return both all biosamples and matching biosamples?
220+
214221
biosamples = await biosample_matches(
215222
getattr(phe, "biosample_matches", phe.biosamples),
216223
scope,
217224
dt_permissions,
218225
False,
219226
{**ctx, "phenopacket": phe_id},
220227
)
228+
biosamples.sort(key=lambda b: b.id)
229+
221230
res.append(
222231
MatchPhenopacket(
223232
id=phe_id,

0 commit comments

Comments
 (0)