-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescendants.py
527 lines (472 loc) · 19.3 KB
/
descendants.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"""
Methods for finding descendant entities (participants in families, biospecimens
in those participants, etc).
"""
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor, as_completed
import psycopg2
import psycopg2.extras
from kf_utils.dataservice.patch import hide_entities, unhide_entities
from kf_utils.dataservice.scrape import yield_entities
def _accumulate(func, *args, **kwargs):
return list(func(*args, **kwargs))
# Maps of direct foreign key descendancy from studies down to genomic files
# {parent_endpoint: [(child_endpoint, link_on_parent, link_on_child), ...], ...}
_db_descendancy = {
"study": [
("participant", "kf_id", "study_id"),
# We need to specially handle getting to families from studies, because
# the database layout does not match the logical data arrangement, so
# just add a stub here for family.
("family", None, None),
],
"family": [("participant", "kf_id", "family_id")],
"participant": [
("family_relationship", "kf_id", "participant1_id"),
("family_relationship", "kf_id", "participant2_id"),
("outcome", "kf_id", "participant_id"),
("phenotype", "kf_id", "participant_id"),
("diagnosis", "kf_id", "participant_id"),
("biospecimen", "kf_id", "participant_id"),
],
"biospecimen": [
("biospecimen_genomic_file", "kf_id", "biospecimen_id"),
("biospecimen_diagnosis", "kf_id", "biospecimen_id"),
],
"biospecimen_genomic_file": [("genomic_file", "genomic_file_id", "kf_id")],
"genomic_file": [
("read_group_genomic_file", "kf_id", "genomic_file_id"),
("sequencing_experiment_genomic_file", "kf_id", "genomic_file_id"),
("biospecimen_genomic_file", "kf_id", "genomic_file_id"),
],
"read_group_genomic_file": [("read_group", "read_group_id", "kf_id")],
"sequencing_experiment_genomic_file": [
("sequencing_experiment", "sequencing_experiment_id", "kf_id")
],
}
_api_descendancy = {
"studies": [
("participants", "kf_id", "study_id"),
("families", "kf_id", "study_id"),
],
"families": [("participants", "kf_id", "family_id")],
"participants": [
("family-relationships", "kf_id", "participant1_id"),
("family-relationships", "kf_id", "participant2_id"),
("outcomes", "kf_id", "participant_id"),
("phenotypes", "kf_id", "participant_id"),
("diagnoses", "kf_id", "participant_id"),
("biospecimens", "kf_id", "participant_id"),
],
"biospecimens": [
("genomic-files", "kf_id", "biospecimen_id"),
("biospecimen-diagnoses", "kf_id", "biospecimen_id"),
],
"genomic-files": [
("read-groups", "kf_id", "genomic_file_id"),
("read-group-genomic-files", "kf_id", "genomic_file_id"),
("sequencing-experiments", "kf_id", "genomic_file_id"),
("sequencing-experiment-genomic-files", "kf_id", "genomic_file_id"),
("biospecimen-genomic-files", "kf_id", "genomic_file_id"),
],
}
def find_gfs_with_extra_contributors(api_or_db_url, bs_kfids, gf_kfids=None):
"""
Given a set of biospecimen KFIDs, find the KFIDs of descendant genomic
files that also descend from biospecimens that aren't included in the given
set. If you already know the full set of descendant genomic files, you may
pass them in to save some time.
Special performance note: a database connect url will run MUCH faster
compared to a dataservice api host
:param api_or_db_url: dataservice api host _or_ database connect url
e.g. "https://kf-api-dataservice.kidsfirstdrc.org" or
"postgres://<USERNAME>:<PASSWORD>@kf-dataservice-postgres-prd.kids-first.io:5432/kfpostgresprd"
:param bs_kfids: iterable of biospecimen KFIDs
:param gf_kfids: iterable of genomic file KFIDs (optional)
:returns: sets of KFIDs of genomic files with contributing biospecimens not
included in bs_kfids, divided into these groups:
"all_visible": all extra contributors are visible in the dataservice
"all_hidden": all extra contributors are hidden in the dataservice
"mixed_visibility": some extra contributors are hidden and some not
Example:
If BS_12345678 and BS_87654321 both contribute to GF_11112222, but you only
specify one of the two BSIDs, then GF_11112222 will be returned. If you
specify both of them, then GF_11112222 will _not_ be returned. The exact
nature of the return will depend on the visibility of the extra
contributors.
"""
if api_or_db_url.startswith(("http:", "https:")):
return _find_gfs_with_extra_contributors_with_http_api(
api_or_db_url, bs_kfids, gf_kfids=None
)
else:
return _find_gfs_with_extra_contributors_with_db_conn(
api_or_db_url, bs_kfids, gf_kfids=None
)
def _find_gfs_with_extra_contributors_with_db_conn(
db_url, bs_kfids, gf_kfids=None
):
"""See find_gfs_with_extra_contributors"""
sql = (
"select distinct extra.genomic_file_id, biospecimen.visible from"
" biospecimen_genomic_file bg join biospecimen_genomic_file extra"
" on bg.genomic_file_id = extra.genomic_file_id"
" join biospecimen"
" on biospecimen.kf_id = extra.biospecimen_id"
" where bg.biospecimen_id in %s and extra.biospecimen_id not in %s"
)
bs_kfids = tuple(bs_kfids)
kfid_tuples = (bs_kfids, bs_kfids)
if gf_kfids:
kfid_tuples = (bs_kfids, bs_kfids, tuple(gf_kfids))
sql += " and extra.genomic_file_id in %s"
has_extra_contributors = {
"mixed_visibility": set(),
"hidden": set(),
"visible": set(),
}
storage = defaultdict(set)
with psycopg2.connect(db_url) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute(sql, kfid_tuples)
for r in cur.fetchall():
storage[r["genomic_file_id"]].add(r["visible"])
for gfid, visset in storage.items():
if (False in visset) and (True in visset):
has_extra_contributors["mixed_visibility"].add(gfid)
elif False in visset:
has_extra_contributors["hidden"].add(gfid)
else:
has_extra_contributors["visible"].add(gfid)
return has_extra_contributors
def _find_gfs_with_extra_contributors_with_http_api(
api_url, bs_kfids, gf_kfids=None
):
"""See find_gfs_with_extra_contributors"""
bs_kfids = set(bs_kfids)
if not gf_kfids:
gf_kfids = set()
with ThreadPoolExecutor() as tpex:
futures = [
tpex.submit(
_accumulate,
yield_entities,
api_url,
"biospecimen-genomic-files",
{"biospecimen_id": k},
show_progress=True,
)
for k in bs_kfids
]
for f in as_completed(futures):
for bg in f.result():
gf_kfids.add(bg["_links"]["genomic_file"].rsplit("/", 1)[1])
else:
gf_kfids = set(gf_kfids)
has_extra_contributors = {
"mixed_visibility": set(),
"hidden": set(),
"visible": set(),
}
with ThreadPoolExecutor() as tpex:
futures = {
tpex.submit(
_accumulate,
yield_entities,
api_url,
"biospecimens",
{"genomic_file_id": g},
show_progress=True,
): g
for g in gf_kfids
}
for f in as_completed(futures):
g = futures[f]
contribs = {
bs["kf_id"]: (bs["visible"] is True) for bs in f.result()
}
contrib_kfids = set(contribs.keys())
if not contrib_kfids.issubset(bs_kfids):
extra_kfids = contrib_kfids - bs_kfids
extras_visible = set(contribs[k] for k in extra_kfids)
if (False in extras_visible) and (True in extras_visible):
has_extra_contributors["mixed_visibility"].add(g)
elif False in extras_visible:
has_extra_contributors["hidden"].add(g)
else:
has_extra_contributors["visible"].add(g)
return has_extra_contributors
def find_descendants_by_kfids(
api_or_db_url,
parent_endpoint,
parents,
ignore_gfs_with_hidden_external_contribs,
kfids_only=True,
):
"""
Given a set of KFIDs from a specified endpoint, find the KFIDs of all
descendant entities.
Given a family kfid, the result will be all participants in that family,
all of the participants' biospecimens/outcomes/phenotypes/etc, all of
their biospecimens' resultant genomic files, and all of the genomic files'
sequencing experiments and read groups.
Given a set of genomic file kfids, the result will be just their sequencing
experiments and read groups.
If you plan to make the discovered descendants visible, you should set
ignore_gfs_with_hidden_external_contribs=True so that you don't accidentally
unhide a genomic file that has hidden contributing biospecimens.
If you plan to make the discovered descendants hidden, you should set
ignore_gfs_with_hidden_external_contribs=False so that everything linked to
the hidden biospecimens also get hidden.
Special performance note: a database connect url will run MUCH faster
compared to a dataservice api host
:param api_or_db_url: dataservice api host _or_ database connect url
e.g. "https://kf-api-dataservice.kidsfirstdrc.org" or
"postgres://<USERNAME>:<PASSWORD>@kf-dataservice-postgres-prd.kids-first.io:5432/kfpostgresprd"
:param parent_endpoint: endpoint of the starting kfids being passed in
:param parents: iterable of starting kfids or entities associated with the
parent_endpoint
:param ignore_gfs_with_hidden_external_contribs: whether to ignore
genomic files (and their descendants) that contain information from
hidden biospecimens unrelated to the given parents.
:param kfids_only: only return KFIDs, not entire entities
:returns: dict mapping endpoints to their sets of discovered kfids
"""
use_api = api_or_db_url.startswith(("http:", "https:"))
if use_api:
parent_type = parent_endpoint
else:
endpoint_to_table = {
"studies": "study",
"participants": "participant",
"family-relationships": "family_relationship",
"outcomes": "outcome",
"phenotypes": "phenotype",
"diagnoses": "diagnosis",
"biospecimens": "biospecimen",
"families": "family",
"biospecimen-genomic-files": "biospecimen_genomic_file",
"biospecimen-diagnoses": "biospecimen_diagnosis",
"genomic-files": "genomic_file",
"read-group-genomic-files": "read_group_genomic_file",
"sequencing-experiment-genomic-files": "sequencing_experiment_genomic_file",
"read-groups": "read_group",
"sequencing-experiments": "sequencing_experiment",
}
table_to_endpoint = {v: k for k, v in endpoint_to_table.items()}
parent_type = endpoint_to_table[parent_endpoint]
if use_api:
descendancy = _api_descendancy
else:
descendancy = _db_descendancy
db_conn = psycopg2.connect(api_or_db_url)
db_cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if isinstance(parents, str):
parents = [parents]
if isinstance(next(iter(parents), None), dict):
parent_kfids = set(p["kf_id"] for p in parents)
descendants = {parent_type: {p["kf_id"]: p for p in parents}}
else:
parent_kfids = set(parents)
if use_api:
descendants = {
parent_type: {
e["kf_id"]: e
for e in yield_entities(api_or_db_url, None, parent_kfids)
}
}
else:
query = f"select distinct * from {parent_type} where kf_id in %s"
db_cur.execute(query, (tuple(parent_kfids | {None}),))
descendants = {
parent_type: {p["kf_id"]: dict(p) for p in db_cur.fetchall()}
}
done = set()
for t in descendancy.keys():
if t != parent_type:
done.add(t)
else:
break
def _inner(parent_type, parent_kfids, descendants):
if parent_type in done:
return
done.add(parent_type)
for (child_type, link_on_parent, link_on_child) in descendancy.get(
parent_type, []
):
if use_api:
with ThreadPoolExecutor() as tpex:
futures = [
tpex.submit(
_accumulate,
yield_entities,
api_or_db_url,
child_type,
{link_on_child: k},
show_progress=True,
)
for k in parent_kfids
]
children = {
e["kf_id"]: e
for f in as_completed(futures)
for e in f.result()
}
else:
# special case for getting to families from studies
if parent_type == "study" and child_type == "family":
query = (
"select distinct family.* from family join participant"
" on participant.family_id = family.kf_id join study on"
" participant.study_id = study.kf_id where study.kf_id "
"in %s"
)
else:
query = (
f"select distinct {child_type}.* from {child_type} join {parent_type}"
f" on {child_type}.{link_on_child} = {parent_type}.{link_on_parent}"
f" where {parent_type}.kf_id in %s"
)
db_cur.execute(query, (tuple(parent_kfids | {None}),))
children = {c["kf_id"]: dict(c) for c in db_cur.fetchall()}
if children:
descendants[child_type] = descendants.get(child_type, dict())
descendants[child_type].update(children)
if (
child_type == "genomic_file"
) and ignore_gfs_with_hidden_external_contribs:
# Ignore multi-specimen genomic files that have hidden
# contributing specimens which are not in the descendants
extra_contrib_gfs = find_gfs_with_extra_contributors(
api_or_db_url,
descendants["biospecimen"],
descendants["genomic_file"],
)
to_remove = (
extra_contrib_gfs["hidden"]
| extra_contrib_gfs["mixed_visibility"]
)
descendants["genomic_file"] = {
k: v
for k, v in descendants["genomic_file"].items()
if k not in to_remove
}
for (child_type, _, _) in descendancy.get(parent_type, []):
if descendants.get(child_type):
_inner(child_type, descendants[child_type].keys(), descendants)
_inner(parent_type, parent_kfids, descendants)
if not use_api:
descendants = {table_to_endpoint[k]: v for k, v in descendants.items()}
if kfids_only:
for k, v in descendants.items():
descendants[k] = set(descendants[k])
return descendants
def find_descendants_by_filter(
api_url,
endpoint,
filter,
ignore_gfs_with_hidden_external_contribs,
kfids_only=True,
db_url=None,
):
"""
Similar to find_descendants_by_kfids but starts with an API endpoint filter
instead of a list of endpoint KFIDs.
"""
things = list(yield_entities(api_url, endpoint, filter, show_progress=True))
if kfids_only:
things = [t["kf_id"] for t in things]
descendants = find_descendants_by_kfids(
db_url or api_url,
endpoint,
things,
ignore_gfs_with_hidden_external_contribs,
kfids_only=kfids_only,
)
return descendants
def hide_descendants_by_filter(
api_url, endpoint, filter, gf_acl=None, db_url=None, dry_run=False
):
"""
Be aware that this and unhide_descendants_by_filter are not symmetrical.
Hiding hides partially contributed descendants, but showing only shows
partially contributed descendants if all other contributors are visible.
If you anticipate needing symmetrical behavior, keep a record of what you
change.
"""
desc = find_descendants_by_filter(
api_url,
endpoint,
filter,
ignore_gfs_with_hidden_external_contribs=False,
kfids_only=False,
db_url=db_url,
)
changed = []
for es in desc.values():
changed.extend(hide_entities(api_url, es.values(), gf_acl, dry_run))
return changed
def unhide_descendants_by_filter(
api_url, endpoint, filter, db_url=None, dry_run=False
):
"""
Be aware that this and hide_descendants_by_filter are not symmetrical.
Hiding hides partially contributed descendants, but showing only shows
partially contributed descendants if all other contributors are visible.
If you anticipate needing symmetrical behavior, keep a record of what you
change.
"""
desc = find_descendants_by_filter(
api_url,
endpoint,
filter,
ignore_gfs_with_hidden_external_contribs=True,
kfids_only=False,
db_url=db_url,
)
changed = []
for es in desc.values():
changed.extend(unhide_entities(api_url, es.values(), dry_run))
return changed
def hide_descendants_by_kfids(
api_url, endpoint, kfids, gf_acl=None, db_url=None, dry_run=False
):
"""
Be aware that this and unhide_descendants_by_kfids are not symmetrical.
Hiding hides partially contributed descendants, but showing only shows
partially contributed descendants if all other contributors are visible.
If you anticipate needing symmetrical behavior, keep a record of what you
change.
"""
desc = find_descendants_by_kfids(
db_url or api_url,
endpoint,
kfids,
ignore_gfs_with_hidden_external_contribs=False,
kfids_only=False,
)
changed = []
for es in desc.values():
changed.extend(hide_entities(api_url, es.values(), gf_acl, dry_run))
return changed
def unhide_descendants_by_kfids(
api_url, endpoint, kfids, db_url=None, dry_run=False
):
"""
Be aware that this and hide_descendants_by_kfids are not symmetrical.
Hiding hides partially contributed descendants, but showing only shows
partially contributed descendants if all other contributors are visible.
If you anticipate needing symmetrical behavior, keep a record of what you
change.
"""
desc = find_descendants_by_kfids(
db_url or api_url,
endpoint,
kfids,
ignore_gfs_with_hidden_external_contribs=True,
kfids_only=False,
)
changed = []
for es in desc.values():
changed.extend(unhide_entities(api_url, es.values(), dry_run))
return changed