pori-python 0.1.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.
- pori_python/__init__.py +1 -0
- pori_python/graphkb/__init__.py +2 -0
- pori_python/graphkb/constants.py +220 -0
- pori_python/graphkb/genes.py +540 -0
- pori_python/graphkb/match.py +710 -0
- pori_python/graphkb/py.typed +0 -0
- pori_python/graphkb/statement.py +48 -0
- pori_python/graphkb/util.py +523 -0
- pori_python/graphkb/vocab.py +198 -0
- pori_python/ipr/__init__.py +0 -0
- pori_python/ipr/annotate.py +373 -0
- pori_python/ipr/connection.py +214 -0
- pori_python/ipr/constants.py +9 -0
- pori_python/ipr/inputs.py +593 -0
- pori_python/ipr/ipr.py +390 -0
- pori_python/ipr/main.py +512 -0
- pori_python/ipr/py.typed +0 -0
- pori_python/ipr/summary.py +412 -0
- pori_python/ipr/therapeutic_options.py +83 -0
- pori_python/ipr/util.py +173 -0
- pori_python/types.py +177 -0
- pori_python-0.1.0.dist-info/LICENSE +674 -0
- pori_python-0.1.0.dist-info/METADATA +151 -0
- pori_python-0.1.0.dist-info/RECORD +49 -0
- pori_python-0.1.0.dist-info/WHEEL +5 -0
- pori_python-0.1.0.dist-info/dependency_links.txt +1 -0
- pori_python-0.1.0.dist-info/entry_points.txt +2 -0
- pori_python-0.1.0.dist-info/top_level.txt +2 -0
- tests/__init__.py +0 -0
- tests/test_graphkb/__init__.py +0 -0
- tests/test_graphkb/data.py +78 -0
- tests/test_graphkb/test_genes.py +306 -0
- tests/test_graphkb/test_graphkb.py +32 -0
- tests/test_graphkb/test_match.py +662 -0
- tests/test_graphkb/test_statement.py +100 -0
- tests/test_graphkb/test_util.py +151 -0
- tests/test_graphkb/test_vocab.py +85 -0
- tests/test_ipr/__init__.py +0 -0
- tests/test_ipr/constants.py +3 -0
- tests/test_ipr/test_annotate.py +149 -0
- tests/test_ipr/test_connection.py +97 -0
- tests/test_ipr/test_inputs.py +295 -0
- tests/test_ipr/test_ipr.py +338 -0
- tests/test_ipr/test_main.py +154 -0
- tests/test_ipr/test_probe.py +72 -0
- tests/test_ipr/test_summary.py +157 -0
- tests/test_ipr/test_upload.py +274 -0
- tests/test_ipr/test_util.py +29 -0
- tests/test_ipr/util.py +9 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
"""Methods for retrieving gene annotation lists from GraphKB."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, List, Sequence, Set, Tuple, cast
|
|
6
|
+
|
|
7
|
+
from pori_python.types import IprGene, Ontology, Record, Statement, Variant
|
|
8
|
+
|
|
9
|
+
from . import GraphKBConnection
|
|
10
|
+
from .constants import (
|
|
11
|
+
BASE_THERAPEUTIC_TERMS,
|
|
12
|
+
CANCER_GENE,
|
|
13
|
+
CHROMOSOMES,
|
|
14
|
+
FAILED_REVIEW_STATUS,
|
|
15
|
+
GENE_RETURN_PROPERTIES,
|
|
16
|
+
GSC_PHARMACOGENOMIC_SOURCE_EXCLUDE_LIST,
|
|
17
|
+
ONCOGENE,
|
|
18
|
+
ONCOKB_SOURCE_NAME,
|
|
19
|
+
PREFERRED_GENE_SOURCE,
|
|
20
|
+
PREFERRED_GENE_SOURCE_NAME,
|
|
21
|
+
RELEVANCE_BASE_TERMS,
|
|
22
|
+
TSO500_SOURCE_NAME,
|
|
23
|
+
TUMOUR_SUPPRESSIVE,
|
|
24
|
+
)
|
|
25
|
+
from .match import get_equivalent_features
|
|
26
|
+
from .util import get_rid, logger, looks_like_rid
|
|
27
|
+
from .vocab import get_terms_set
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _get_tumourigenesis_genes_list(
|
|
31
|
+
conn: GraphKBConnection, relevance: str, sources: List[str], ignore_cache: bool = False
|
|
32
|
+
) -> List[Ontology]:
|
|
33
|
+
statements = cast(
|
|
34
|
+
List[Statement],
|
|
35
|
+
conn.query(
|
|
36
|
+
{
|
|
37
|
+
"target": "Statement",
|
|
38
|
+
"filters": {
|
|
39
|
+
"AND": [
|
|
40
|
+
{"source": {"target": "Source", "filters": {"name": sources}}},
|
|
41
|
+
{"relevance": {"target": "Vocabulary", "filters": {"name": relevance}}},
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"returnProperties": [f"subject.{prop}" for prop in GENE_RETURN_PROPERTIES],
|
|
45
|
+
},
|
|
46
|
+
ignore_cache=ignore_cache,
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
genes: Dict[str, Ontology] = {}
|
|
51
|
+
|
|
52
|
+
for statement in statements:
|
|
53
|
+
if statement["subject"].get("biotype", "") == "gene":
|
|
54
|
+
record_id = statement["subject"]["@rid"]
|
|
55
|
+
genes[record_id] = statement["subject"]
|
|
56
|
+
|
|
57
|
+
return [gene for gene in genes.values()]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def get_oncokb_oncogenes(conn: GraphKBConnection) -> List[Ontology]:
|
|
61
|
+
"""Get the list of oncogenes stored in GraphKB derived from OncoKB.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
conn: the graphkb connection object
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
gene (Feature) records
|
|
68
|
+
"""
|
|
69
|
+
return _get_tumourigenesis_genes_list(conn, ONCOGENE, [ONCOKB_SOURCE_NAME])
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def get_oncokb_tumour_supressors(conn: GraphKBConnection) -> List[Ontology]:
|
|
73
|
+
"""Get the list of tumour supressor genes stored in GraphKB derived from OncoKB.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
conn: the graphkb connection object
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
gene (Feature) records
|
|
80
|
+
"""
|
|
81
|
+
return _get_tumourigenesis_genes_list(conn, TUMOUR_SUPPRESSIVE, [ONCOKB_SOURCE_NAME])
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def get_cancer_genes(conn: GraphKBConnection) -> List[Ontology]:
|
|
85
|
+
"""Get the list of cancer genes stored in GraphKB derived from OncoKB & TSO500.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
conn: the graphkb connection object
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
gene (Feature) records
|
|
92
|
+
"""
|
|
93
|
+
return _get_tumourigenesis_genes_list(
|
|
94
|
+
conn, CANCER_GENE, [ONCOKB_SOURCE_NAME, TSO500_SOURCE_NAME]
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def get_therapeutic_associated_genes(graphkb_conn: GraphKBConnection) -> List[Ontology]:
|
|
99
|
+
"""Genes related to a cancer-associated statement in Graphkb."""
|
|
100
|
+
therapeutic_relevance = get_terms_set(graphkb_conn, BASE_THERAPEUTIC_TERMS)
|
|
101
|
+
statements = graphkb_conn.query(
|
|
102
|
+
{
|
|
103
|
+
"target": "Statement",
|
|
104
|
+
"filters": {"relevance": sorted(list(therapeutic_relevance))},
|
|
105
|
+
"returnProperties": ["reviewStatus"]
|
|
106
|
+
+ [f"conditions.{prop}" for prop in GENE_RETURN_PROPERTIES]
|
|
107
|
+
+ [
|
|
108
|
+
f"conditions.reference{ref}.{prop}"
|
|
109
|
+
for prop in GENE_RETURN_PROPERTIES
|
|
110
|
+
for ref in ("1", "2")
|
|
111
|
+
],
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
genes: List[Ontology] = []
|
|
115
|
+
for statement in statements:
|
|
116
|
+
statement = cast(Statement, statement)
|
|
117
|
+
if statement["reviewStatus"] == "failed":
|
|
118
|
+
continue
|
|
119
|
+
for condition in statement["conditions"]:
|
|
120
|
+
if condition["@class"] == "Feature":
|
|
121
|
+
genes.append(condition)
|
|
122
|
+
elif condition["@class"].endswith("Variant"):
|
|
123
|
+
cond = cast(Variant, condition)
|
|
124
|
+
if cond["reference1"] and cond["reference1"]["@class"] == "Feature":
|
|
125
|
+
genes.append(cond["reference1"])
|
|
126
|
+
if cond["reference2"] and cond["reference2"]["@class"] == "Feature":
|
|
127
|
+
genes.append(cond["reference2"])
|
|
128
|
+
unique_genes: List[Ontology] = []
|
|
129
|
+
for gene in genes:
|
|
130
|
+
if not gene.get("deprecated", False):
|
|
131
|
+
if gene["@rid"] not in [g["@rid"] for g in unique_genes]:
|
|
132
|
+
unique_genes.append(gene)
|
|
133
|
+
return unique_genes
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def get_genes_from_variant_types(
|
|
137
|
+
conn: GraphKBConnection,
|
|
138
|
+
types: List[str],
|
|
139
|
+
source_record_ids: List[str] = [],
|
|
140
|
+
ignore_cache: bool = False,
|
|
141
|
+
) -> List[Ontology]:
|
|
142
|
+
"""Retrieve a list of Genes which are found in variants on the given types.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
conn: the graphkb connection object
|
|
146
|
+
types: list of names of variant types
|
|
147
|
+
source_record_ids: list of sources ids to filter genes by
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
List.<dict>: gene (Feature) records
|
|
151
|
+
"""
|
|
152
|
+
variant_filters: List[Dict[str, Any]] = []
|
|
153
|
+
if types:
|
|
154
|
+
variant_filters.append(
|
|
155
|
+
{"type": {"target": "Vocabulary", "filters": {"name": types, "operator": "IN"}}}
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
variants = cast(
|
|
159
|
+
List[Variant],
|
|
160
|
+
conn.query(
|
|
161
|
+
{
|
|
162
|
+
"target": "Variant",
|
|
163
|
+
"filters": variant_filters,
|
|
164
|
+
"returnProperties": ["reference1", "reference2"],
|
|
165
|
+
},
|
|
166
|
+
ignore_cache=ignore_cache,
|
|
167
|
+
),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
genes = set()
|
|
171
|
+
for variant in variants:
|
|
172
|
+
genes.add(variant["reference1"])
|
|
173
|
+
if variant["reference2"]:
|
|
174
|
+
genes.add(variant["reference2"])
|
|
175
|
+
if not genes:
|
|
176
|
+
return []
|
|
177
|
+
|
|
178
|
+
gene_filters: List[Dict[str, Any]] = [{"biotype": "gene"}]
|
|
179
|
+
if source_record_ids:
|
|
180
|
+
gene_filters.append({"source": source_record_ids, "operator": "IN"})
|
|
181
|
+
|
|
182
|
+
result = cast(
|
|
183
|
+
List[Ontology],
|
|
184
|
+
conn.query(
|
|
185
|
+
{
|
|
186
|
+
"target": list(genes),
|
|
187
|
+
"returnProperties": GENE_RETURN_PROPERTIES,
|
|
188
|
+
"filters": gene_filters,
|
|
189
|
+
},
|
|
190
|
+
ignore_cache=ignore_cache,
|
|
191
|
+
),
|
|
192
|
+
)
|
|
193
|
+
return result
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def get_preferred_gene_source_rid(
|
|
197
|
+
conn: GraphKBConnection, preferred_source_name=PREFERRED_GENE_SOURCE_NAME
|
|
198
|
+
):
|
|
199
|
+
"""Get the rid of the preferred gene source.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
ignore_cache (bool, optional): bypass the cache to always force a new request
|
|
203
|
+
preferred_source_name: name of the preferred gene symbol source to look up the rid of
|
|
204
|
+
Returns:
|
|
205
|
+
rid of the source record matching the preferred source name.
|
|
206
|
+
|
|
207
|
+
"""
|
|
208
|
+
if looks_like_rid(preferred_source_name):
|
|
209
|
+
return preferred_source_name
|
|
210
|
+
result = conn.query(
|
|
211
|
+
{
|
|
212
|
+
"target": {"target": "Source", "filters": {"name": preferred_source_name}},
|
|
213
|
+
"queryType": "similarTo",
|
|
214
|
+
}
|
|
215
|
+
)[0]["@rid"]
|
|
216
|
+
return result
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def get_preferred_gene_name(
|
|
220
|
+
conn: GraphKBConnection, gene_name: str, source: str = PREFERRED_GENE_SOURCE_NAME
|
|
221
|
+
) -> str:
|
|
222
|
+
"""Preferred gene symbol of a gene or transcript.
|
|
223
|
+
|
|
224
|
+
Args:
|
|
225
|
+
gene_name: the gene name to search features by
|
|
226
|
+
ignore_cache (bool, optional): bypass the cache to always force a new request
|
|
227
|
+
source: id of the preferred gene symbol source
|
|
228
|
+
Returns:
|
|
229
|
+
preferred displayName symbol.
|
|
230
|
+
|
|
231
|
+
Example:
|
|
232
|
+
return KRAS for get_preferred_gene_name(conn, 'NM_033360')
|
|
233
|
+
return KRAS for get_preferred_gene_name(conn, 'ENSG00000133703.11')
|
|
234
|
+
"""
|
|
235
|
+
source_rid = get_preferred_gene_source_rid(conn, source)
|
|
236
|
+
if gene_name in CHROMOSOMES:
|
|
237
|
+
logger.error(f"{gene_name} assumed to be a chromosome, not gene")
|
|
238
|
+
return ""
|
|
239
|
+
eq = get_equivalent_features(conn=conn, gene_name=gene_name)
|
|
240
|
+
genes = [m for m in eq if m.get("biotype") == "gene" and not m.get("deprecated")]
|
|
241
|
+
if not genes:
|
|
242
|
+
logger.error(f"No genes found for: {gene_name}")
|
|
243
|
+
return ""
|
|
244
|
+
if source_rid:
|
|
245
|
+
source_filtered_genes = [m for m in genes if m.get("source") == source_rid]
|
|
246
|
+
if not source_filtered_genes:
|
|
247
|
+
logger.error(f"No data from source {source_rid} for {gene_name}")
|
|
248
|
+
else:
|
|
249
|
+
genes = source_filtered_genes
|
|
250
|
+
|
|
251
|
+
gene_names = [g["displayName"] for g in genes if g]
|
|
252
|
+
if len(gene_names) > 1:
|
|
253
|
+
logger.error(
|
|
254
|
+
f"Multiple gene names found for: {gene_name} - using {gene_names[0]}, ignoring {gene_names[1:]}"
|
|
255
|
+
)
|
|
256
|
+
return gene_names[0]
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def get_cancer_predisposition_info(
|
|
260
|
+
conn: GraphKBConnection, source: str = PREFERRED_GENE_SOURCE_NAME
|
|
261
|
+
) -> Tuple[List[str], Dict[str, str]]:
|
|
262
|
+
genes, allvardata = get_gene_linked_cancer_predisposition_info(conn, source)
|
|
263
|
+
variants = {key: allvardata[key][0] for key in allvardata.keys()}
|
|
264
|
+
return genes, variants
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def get_gene_linked_cancer_predisposition_info(
|
|
268
|
+
conn: GraphKBConnection, source: str = PREFERRED_GENE_SOURCE
|
|
269
|
+
) -> Tuple[List[str], Dict[str, Tuple[str, List[str]]]]:
|
|
270
|
+
"""
|
|
271
|
+
Return two lists from GraphKB, one of cancer predisposition genes and one of associated variants.
|
|
272
|
+
|
|
273
|
+
GERO-272 - criteria for what counts as a "cancer predisposition" variant
|
|
274
|
+
|
|
275
|
+
In short:
|
|
276
|
+
* Statement 'source' is 'CGL'
|
|
277
|
+
* Statement 'relevance' is 'pathogenic'
|
|
278
|
+
* gene is gotten from any associated 'PositionalVariant' records
|
|
279
|
+
|
|
280
|
+
Example: https://graphkb.bcgsc.ca/view/Statement/155:11616
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
Returns:
|
|
285
|
+
genes: list of cancer predisposition genes
|
|
286
|
+
variants: dictionary mapping pharmacogenomic variant IDs to variant display names
|
|
287
|
+
"""
|
|
288
|
+
genes = set()
|
|
289
|
+
non_genes = set()
|
|
290
|
+
infer_genes = set()
|
|
291
|
+
variants: Dict[str, Tuple[str, List[str]]] = {}
|
|
292
|
+
|
|
293
|
+
terms: dict = {term: lst for term, lst in RELEVANCE_BASE_TERMS}
|
|
294
|
+
relevance_rids = list(get_terms_set(conn, terms.get("cancer predisposition", [])))
|
|
295
|
+
source_rid = get_preferred_gene_source_rid(conn, source)
|
|
296
|
+
|
|
297
|
+
predisp_statements = [
|
|
298
|
+
cast(Statement, record)
|
|
299
|
+
for record in conn.query(
|
|
300
|
+
{
|
|
301
|
+
"target": "Statement",
|
|
302
|
+
"filters": {
|
|
303
|
+
"AND": [
|
|
304
|
+
{
|
|
305
|
+
"evidence": {
|
|
306
|
+
"target": "Source",
|
|
307
|
+
"filters": {"@rid": get_rid(conn, "Source", "CGL")},
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
"relevance": {
|
|
312
|
+
"target": "Vocabulary",
|
|
313
|
+
"filters": {"@rid": relevance_rids},
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
]
|
|
317
|
+
},
|
|
318
|
+
"returnProperties": [
|
|
319
|
+
"conditions.@class",
|
|
320
|
+
"conditions.@rid",
|
|
321
|
+
"conditions.displayName",
|
|
322
|
+
"conditions.reference1.biotype",
|
|
323
|
+
"conditions.reference1.displayName",
|
|
324
|
+
"conditions.reference2.biotype",
|
|
325
|
+
"conditions.reference2.displayName",
|
|
326
|
+
],
|
|
327
|
+
},
|
|
328
|
+
ignore_cache=False,
|
|
329
|
+
)
|
|
330
|
+
]
|
|
331
|
+
for record in predisp_statements:
|
|
332
|
+
for condition in record["conditions"]:
|
|
333
|
+
if condition["@class"] == "PositionalVariant":
|
|
334
|
+
assoc_gene_list: List[str] = []
|
|
335
|
+
for reference in ["reference1", "reference2"]:
|
|
336
|
+
name = (condition.get(reference) or {}).get("displayName", "") # type: ignore
|
|
337
|
+
biotype = (condition.get(reference) or {}).get("biotype", "") # type: ignore
|
|
338
|
+
if name and biotype == "gene":
|
|
339
|
+
genes.add(name)
|
|
340
|
+
assoc_gene_list.append(name)
|
|
341
|
+
elif name:
|
|
342
|
+
gene = get_preferred_gene_name(conn, name, source_rid)
|
|
343
|
+
if gene:
|
|
344
|
+
infer_genes.add((gene, name, biotype))
|
|
345
|
+
assoc_gene_list.append(gene)
|
|
346
|
+
else:
|
|
347
|
+
non_genes.add((name, biotype))
|
|
348
|
+
logger.error(
|
|
349
|
+
f"Non-gene cancer predisposition {biotype}: {name} for {condition['displayName']}"
|
|
350
|
+
)
|
|
351
|
+
variants[condition["@rid"]] = (condition["displayName"], assoc_gene_list)
|
|
352
|
+
|
|
353
|
+
for gene, name, biotype in infer_genes:
|
|
354
|
+
logger.debug(f"Found gene '{gene}' for '{name}' ({biotype})")
|
|
355
|
+
genes.add(gene)
|
|
356
|
+
|
|
357
|
+
for name, biotype in non_genes:
|
|
358
|
+
logger.error(f"Unable to find gene for '{name}' ({biotype})")
|
|
359
|
+
|
|
360
|
+
return sorted(genes), variants
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def get_pharmacogenomic_info(
|
|
364
|
+
conn: GraphKBConnection, source: str = PREFERRED_GENE_SOURCE_NAME
|
|
365
|
+
) -> Tuple[List[str], Dict[str, str]]:
|
|
366
|
+
genes, allvardata = get_gene_linked_pharmacogenomic_info(conn, source)
|
|
367
|
+
variants = {key: allvardata[key][0] for key in allvardata.keys()}
|
|
368
|
+
return genes, variants
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def get_gene_linked_pharmacogenomic_info(
|
|
372
|
+
conn: GraphKBConnection, source: str = PREFERRED_GENE_SOURCE
|
|
373
|
+
) -> Tuple[List[str], Dict[str, Tuple[str, List[str]]]]:
|
|
374
|
+
"""
|
|
375
|
+
Return two lists from GraphKB, one of pharmacogenomic genes and one of associated variants.
|
|
376
|
+
|
|
377
|
+
SDEV-2733 - criteria for what counts as a "pharmacogenomic" variant
|
|
378
|
+
|
|
379
|
+
In short:
|
|
380
|
+
* Statement 'source' is not 'CGI' or 'CIViC'
|
|
381
|
+
* Statement 'relevance' is 'increased toxicity' or 'decreased toxicity'
|
|
382
|
+
* gene is gotten from any associated 'PositionalVariant' records
|
|
383
|
+
|
|
384
|
+
Example: https://graphkb.bcgsc.ca/view/Statement/154:9574
|
|
385
|
+
|
|
386
|
+
Returns:
|
|
387
|
+
genes: list of pharmacogenomic genes
|
|
388
|
+
variants: dictionary mapping pharmacogenomic variant IDs to variant display names
|
|
389
|
+
"""
|
|
390
|
+
genes = set()
|
|
391
|
+
non_genes = set()
|
|
392
|
+
infer_genes = set()
|
|
393
|
+
variants: Dict[str, Tuple] = {}
|
|
394
|
+
|
|
395
|
+
relevance_rids = list(get_terms_set(conn, "pharmacogenomic"))
|
|
396
|
+
source_rid = get_preferred_gene_source_rid(conn, source)
|
|
397
|
+
|
|
398
|
+
for record in conn.query(
|
|
399
|
+
{
|
|
400
|
+
"target": "Statement",
|
|
401
|
+
"filters": [
|
|
402
|
+
{"relevance": {"target": "Vocabulary", "filters": {"@rid": relevance_rids}}}
|
|
403
|
+
],
|
|
404
|
+
"returnProperties": [
|
|
405
|
+
"conditions.@class",
|
|
406
|
+
"conditions.@rid",
|
|
407
|
+
"conditions.displayName",
|
|
408
|
+
"conditions.reference1.biotype",
|
|
409
|
+
"conditions.reference1.displayName",
|
|
410
|
+
"conditions.reference2.biotype",
|
|
411
|
+
"conditions.reference2.displayName",
|
|
412
|
+
"source.name",
|
|
413
|
+
],
|
|
414
|
+
},
|
|
415
|
+
ignore_cache=False,
|
|
416
|
+
):
|
|
417
|
+
if record["source"]: # type: ignore
|
|
418
|
+
if record["source"]["name"].lower() in GSC_PHARMACOGENOMIC_SOURCE_EXCLUDE_LIST: # type: ignore
|
|
419
|
+
continue
|
|
420
|
+
|
|
421
|
+
for condition in record["conditions"]: # type: ignore
|
|
422
|
+
if condition["@class"] == "PositionalVariant":
|
|
423
|
+
assoc_gene_list = []
|
|
424
|
+
for reference in ["reference1", "reference2"]:
|
|
425
|
+
name = (condition.get(reference) or {}).get("displayName", "")
|
|
426
|
+
biotype = (condition.get(reference) or {}).get("biotype", "")
|
|
427
|
+
if name and biotype == "gene":
|
|
428
|
+
genes.add(name)
|
|
429
|
+
assoc_gene_list.append(name)
|
|
430
|
+
elif name:
|
|
431
|
+
gene = get_preferred_gene_name(conn, name, source_rid)
|
|
432
|
+
if gene:
|
|
433
|
+
infer_genes.add((gene, name, biotype))
|
|
434
|
+
assoc_gene_list.append(gene)
|
|
435
|
+
else:
|
|
436
|
+
non_genes.add((name, biotype))
|
|
437
|
+
logger.error(
|
|
438
|
+
f"Non-gene pharmacogenomic {biotype}: {name} for {condition['displayName']}"
|
|
439
|
+
)
|
|
440
|
+
variants[condition["@rid"]] = (condition["displayName"], assoc_gene_list)
|
|
441
|
+
for gene, name, biotype in infer_genes:
|
|
442
|
+
logger.debug(f"Found gene '{gene}' for '{name}' ({biotype})")
|
|
443
|
+
genes.add(gene)
|
|
444
|
+
|
|
445
|
+
for name, biotype in non_genes:
|
|
446
|
+
logger.error(f"Unable to find gene for '{name}' ({biotype})")
|
|
447
|
+
|
|
448
|
+
return sorted(genes), variants
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def convert_to_rid_set(records: List[Record] | List[Ontology]) -> Set[str]:
|
|
452
|
+
return {r["@rid"] for r in records}
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def get_gene_information(
|
|
456
|
+
graphkb_conn: GraphKBConnection, gene_names: Sequence[str]
|
|
457
|
+
) -> List[IprGene]:
|
|
458
|
+
"""Create a list of gene_info flag dicts for IPR report upload.
|
|
459
|
+
|
|
460
|
+
Function is originally from pori_ipr_python::annotate.py
|
|
461
|
+
|
|
462
|
+
Gene flags (categories) are: [
|
|
463
|
+
'cancerGeneListMatch', 'kbStatementRelated', 'knownFusionPartner',
|
|
464
|
+
'knownSmallMutation', 'oncogene', 'therapeuticAssociated', 'tumourSuppressor'
|
|
465
|
+
]
|
|
466
|
+
|
|
467
|
+
Args:
|
|
468
|
+
graphkb_conn ([type]): [description]
|
|
469
|
+
gene_names ([type]): [description]
|
|
470
|
+
Returns:
|
|
471
|
+
List of gene_info dicts of form [{'name':<gene_str>, <flag>: True}]
|
|
472
|
+
Keys of False values are simply omitted from ipr upload to reduce info transfer.
|
|
473
|
+
eg. [{'kbStatementRelated': True,
|
|
474
|
+
'knownFusionPartner': True,
|
|
475
|
+
'knownSmallMutation': True,
|
|
476
|
+
'name': 'TERT',
|
|
477
|
+
'oncogene': True}]
|
|
478
|
+
"""
|
|
479
|
+
logger.info("fetching variant related genes list")
|
|
480
|
+
# For query speed, only fetch the minimum needed details
|
|
481
|
+
ret_props = [
|
|
482
|
+
"conditions.@rid",
|
|
483
|
+
"conditions.@class",
|
|
484
|
+
"conditions.reference1",
|
|
485
|
+
"conditions.reference2",
|
|
486
|
+
"reviewStatus",
|
|
487
|
+
]
|
|
488
|
+
body: Dict[str, Any] = {"target": "Statement", "returnProperties": ret_props}
|
|
489
|
+
|
|
490
|
+
gene_names = sorted(set(gene_names))
|
|
491
|
+
statements = graphkb_conn.query(body)
|
|
492
|
+
statements = [s for s in statements if s.get("reviewStatus") != FAILED_REVIEW_STATUS]
|
|
493
|
+
|
|
494
|
+
gene_flags: Dict[str, Set[str]] = {
|
|
495
|
+
"kbStatementRelated": set(),
|
|
496
|
+
"knownFusionPartner": set(),
|
|
497
|
+
"knownSmallMutation": set(),
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
for statement in statements:
|
|
501
|
+
statement = cast(Statement, statement)
|
|
502
|
+
for condition in statement["conditions"]:
|
|
503
|
+
# ignore types, as there can be various types of conditions
|
|
504
|
+
if condition.get("reference1"):
|
|
505
|
+
gene_flags["kbStatementRelated"].add(condition["reference1"]) # type: ignore
|
|
506
|
+
if condition.get("reference2"):
|
|
507
|
+
# Having a reference2 implies the event is a fusion
|
|
508
|
+
gene_flags["kbStatementRelated"].add(condition["reference2"]) # type: ignore
|
|
509
|
+
gene_flags["knownFusionPartner"].add(condition["reference1"]) # type: ignore
|
|
510
|
+
gene_flags["knownFusionPartner"].add(condition["reference2"]) # type: ignore
|
|
511
|
+
elif condition["@class"] == "PositionalVariant":
|
|
512
|
+
# PositionalVariant without a reference2 implies a smallMutation type
|
|
513
|
+
gene_flags["knownSmallMutation"].add(condition["reference1"]) # type: ignore
|
|
514
|
+
|
|
515
|
+
logger.info("fetching oncogenes list")
|
|
516
|
+
gene_flags["oncogene"] = convert_to_rid_set(get_oncokb_oncogenes(graphkb_conn))
|
|
517
|
+
logger.info("fetching tumour supressors list")
|
|
518
|
+
gene_flags["tumourSuppressor"] = convert_to_rid_set(get_oncokb_tumour_supressors(graphkb_conn))
|
|
519
|
+
logger.info("fetching cancerGeneListMatch list")
|
|
520
|
+
gene_flags["cancerGeneListMatch"] = convert_to_rid_set(get_cancer_genes(graphkb_conn))
|
|
521
|
+
|
|
522
|
+
logger.info("fetching therapeutic associated genes lists")
|
|
523
|
+
gene_flags["therapeuticAssociated"] = convert_to_rid_set(
|
|
524
|
+
get_therapeutic_associated_genes(graphkb_conn)
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
logger.info(f"Setting gene_info flags on {len(gene_names)} genes")
|
|
528
|
+
result: List[IprGene] = []
|
|
529
|
+
for gene_name in gene_names:
|
|
530
|
+
equivalent = convert_to_rid_set(get_equivalent_features(graphkb_conn, gene_name))
|
|
531
|
+
row: Dict[str, str | bool] = {"name": gene_name}
|
|
532
|
+
flagged = False
|
|
533
|
+
for flag in gene_flags:
|
|
534
|
+
# make smaller JSON to upload since all default to false already
|
|
535
|
+
if equivalent.intersection(gene_flags[flag]):
|
|
536
|
+
row[flag] = flagged = True
|
|
537
|
+
if flagged:
|
|
538
|
+
result.append(cast(IprGene, row))
|
|
539
|
+
|
|
540
|
+
return result
|