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,198 @@
|
|
|
1
|
+
from typing import Callable, Dict, Iterable, List, Set, cast
|
|
2
|
+
|
|
3
|
+
from pori_python.types import Ontology
|
|
4
|
+
|
|
5
|
+
from . import GraphKBConnection
|
|
6
|
+
from .util import convert_to_rid_list
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def query_by_name(ontology_class: str, base_term_name: str) -> Dict:
|
|
10
|
+
return {"target": ontology_class, "filters": {"name": base_term_name}}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_equivalent_terms(
|
|
14
|
+
conn: GraphKBConnection,
|
|
15
|
+
base_term_name: str,
|
|
16
|
+
root_exclude_term: str = "",
|
|
17
|
+
ontology_class: str = "Vocabulary",
|
|
18
|
+
ignore_cache: bool = False,
|
|
19
|
+
build_base_query: Callable = query_by_name,
|
|
20
|
+
) -> List[Ontology]:
|
|
21
|
+
"""
|
|
22
|
+
Get a list of terms equivalent to the current term up to the root term
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
base_term_name: the name to get superclasses of
|
|
26
|
+
root_exclude_term: the parent term to exlcude along with all of its parent terms
|
|
27
|
+
"""
|
|
28
|
+
base_records = convert_to_rid_list(conn.query(build_base_query(ontology_class, base_term_name)))
|
|
29
|
+
if not base_records:
|
|
30
|
+
return []
|
|
31
|
+
base_term_parents = cast(
|
|
32
|
+
List[Ontology],
|
|
33
|
+
conn.query(
|
|
34
|
+
{
|
|
35
|
+
"target": {"target": base_records, "queryType": "descendants"},
|
|
36
|
+
"queryType": "similarTo",
|
|
37
|
+
"treeEdges": [],
|
|
38
|
+
"returnProperties": ["sourceId", "sourceIdVersion", "deprecated", "name", "@rid"],
|
|
39
|
+
},
|
|
40
|
+
ignore_cache=ignore_cache,
|
|
41
|
+
),
|
|
42
|
+
)
|
|
43
|
+
if root_exclude_term:
|
|
44
|
+
root_records = convert_to_rid_list(
|
|
45
|
+
conn.query(build_base_query(ontology_class, root_exclude_term))
|
|
46
|
+
)
|
|
47
|
+
if not root_records:
|
|
48
|
+
return base_term_parents
|
|
49
|
+
|
|
50
|
+
exclude = set(
|
|
51
|
+
convert_to_rid_list(
|
|
52
|
+
conn.query(
|
|
53
|
+
{
|
|
54
|
+
"target": {"target": root_records, "queryType": "descendants"},
|
|
55
|
+
"queryType": "similarTo",
|
|
56
|
+
"treeEdges": [],
|
|
57
|
+
"returnProperties": [
|
|
58
|
+
"sourceId",
|
|
59
|
+
"sourceIdVersion",
|
|
60
|
+
"deprecated",
|
|
61
|
+
"name",
|
|
62
|
+
"@rid",
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
ignore_cache=ignore_cache,
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
return [term for term in base_term_parents if term["@rid"] not in exclude]
|
|
70
|
+
return base_term_parents
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def get_term_tree(
|
|
74
|
+
conn: GraphKBConnection,
|
|
75
|
+
base_term_name: str,
|
|
76
|
+
root_exclude_term: str = "",
|
|
77
|
+
ontology_class: str = "Vocabulary",
|
|
78
|
+
include_superclasses: bool = True,
|
|
79
|
+
ignore_cache: bool = False,
|
|
80
|
+
build_base_query: Callable = query_by_name,
|
|
81
|
+
) -> List[Ontology]:
|
|
82
|
+
"""
|
|
83
|
+
Get terms equivalent to the base term by traversing the subclassOf tree and expanding related
|
|
84
|
+
alias and cross reference edges
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
conn: the graphkb connection object
|
|
88
|
+
base_term_name: the term to use as the base of the subclass tree
|
|
89
|
+
ontology_class: the default class to query. Defaults to 'Vocabulary'
|
|
90
|
+
include_superclasses: when True the query will include superclasses of the current term
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
GraphKB records
|
|
94
|
+
|
|
95
|
+
Note: this must be done in 2 calls to avoid going up and down the tree in a single query (exclude adjacent siblings)
|
|
96
|
+
"""
|
|
97
|
+
# get all child terms of the subclass tree and disambiguate them
|
|
98
|
+
base_records = convert_to_rid_list(conn.query(build_base_query(ontology_class, base_term_name)))
|
|
99
|
+
if not base_records:
|
|
100
|
+
return []
|
|
101
|
+
child_terms = cast(
|
|
102
|
+
List[Ontology],
|
|
103
|
+
conn.query(
|
|
104
|
+
{
|
|
105
|
+
"target": {"target": base_records, "queryType": "ancestors"},
|
|
106
|
+
"queryType": "similarTo",
|
|
107
|
+
"treeEdges": [],
|
|
108
|
+
"returnProperties": ["sourceId", "sourceIdVersion", "deprecated", "name", "@rid"],
|
|
109
|
+
},
|
|
110
|
+
ignore_cache=ignore_cache,
|
|
111
|
+
),
|
|
112
|
+
)
|
|
113
|
+
# get all parent terms of the subclass tree and disambiguate them
|
|
114
|
+
if include_superclasses:
|
|
115
|
+
parent_terms = get_equivalent_terms(
|
|
116
|
+
conn,
|
|
117
|
+
base_term_name,
|
|
118
|
+
root_exclude_term=root_exclude_term,
|
|
119
|
+
ontology_class=ontology_class,
|
|
120
|
+
ignore_cache=ignore_cache,
|
|
121
|
+
build_base_query=build_base_query,
|
|
122
|
+
)
|
|
123
|
+
else:
|
|
124
|
+
parent_terms = []
|
|
125
|
+
|
|
126
|
+
terms = {}
|
|
127
|
+
# merge the two lists
|
|
128
|
+
for term in child_terms + parent_terms:
|
|
129
|
+
terms[term["@rid"]] = term
|
|
130
|
+
|
|
131
|
+
return list(terms.values())
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def get_term_by_name(
|
|
135
|
+
conn: GraphKBConnection,
|
|
136
|
+
name: str,
|
|
137
|
+
ontology_class: str = "Vocabulary",
|
|
138
|
+
ignore_cache: bool = False,
|
|
139
|
+
**kwargs,
|
|
140
|
+
) -> Ontology:
|
|
141
|
+
"""
|
|
142
|
+
Retrieve a vocaulary term by name
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
conn: the graphkb connection object
|
|
146
|
+
name: the name of the Vocabulary term to retrieve
|
|
147
|
+
|
|
148
|
+
Raises:
|
|
149
|
+
AssertionError: more than one term or no terms with that name were found
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
Dict: Vocabulary record
|
|
153
|
+
|
|
154
|
+
Raises:
|
|
155
|
+
AssertionError: if the term was not found or more than 1 match was found (expected to be unique)
|
|
156
|
+
"""
|
|
157
|
+
result = conn.query(
|
|
158
|
+
{
|
|
159
|
+
"target": ontology_class,
|
|
160
|
+
"filters": {"name": name},
|
|
161
|
+
"returnProperties": [
|
|
162
|
+
"sourceId",
|
|
163
|
+
"sourceIdVersion",
|
|
164
|
+
"deprecated",
|
|
165
|
+
"name",
|
|
166
|
+
"@rid",
|
|
167
|
+
"@class",
|
|
168
|
+
],
|
|
169
|
+
},
|
|
170
|
+
ignore_cache=ignore_cache,
|
|
171
|
+
**kwargs,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
if len(result) != 1:
|
|
175
|
+
raise AssertionError(f"unable to find term ({name}) by name")
|
|
176
|
+
return cast(Ontology, result[0])
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def get_terms_set(
|
|
180
|
+
graphkb_conn: GraphKBConnection, base_terms: Iterable[str], ignore_cache: bool = False
|
|
181
|
+
) -> Set[str]:
|
|
182
|
+
"""Get a set of vocabulary rids given some base/parent term names."""
|
|
183
|
+
base_terms = [base_terms] if isinstance(base_terms, str) else base_terms
|
|
184
|
+
cache_key = tuple(sorted(base_terms))
|
|
185
|
+
if graphkb_conn.cache.get(cache_key, None) and not ignore_cache:
|
|
186
|
+
return graphkb_conn.cache[cache_key]
|
|
187
|
+
terms = set()
|
|
188
|
+
for base_term in base_terms:
|
|
189
|
+
terms.update(
|
|
190
|
+
convert_to_rid_list(
|
|
191
|
+
get_term_tree(
|
|
192
|
+
graphkb_conn, base_term, include_superclasses=False, ignore_cache=ignore_cache
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
if not ignore_cache:
|
|
197
|
+
graphkb_conn.cache[cache_key] = terms
|
|
198
|
+
return terms
|
|
File without changes
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"""
|
|
2
|
+
handles annotating variants with annotation information from graphkb
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from requests.exceptions import HTTPError
|
|
8
|
+
|
|
9
|
+
from pandas import isnull
|
|
10
|
+
from tqdm import tqdm
|
|
11
|
+
from typing import Dict, List, Sequence, cast
|
|
12
|
+
|
|
13
|
+
from pori_python.graphkb import GraphKBConnection
|
|
14
|
+
from pori_python.graphkb import match as gkb_match
|
|
15
|
+
from pori_python.graphkb.match import INPUT_COPY_CATEGORIES
|
|
16
|
+
from pori_python.graphkb.statement import get_statements_from_variants
|
|
17
|
+
from pori_python.graphkb.util import FeatureNotFoundError
|
|
18
|
+
from pori_python.types import (
|
|
19
|
+
Hashabledict,
|
|
20
|
+
IprCopyVariant,
|
|
21
|
+
IprExprVariant,
|
|
22
|
+
IprStructuralVariant,
|
|
23
|
+
KbMatch,
|
|
24
|
+
Statement,
|
|
25
|
+
Variant,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from .constants import TMB_HIGH_CATEGORY
|
|
29
|
+
from .ipr import convert_statements_to_alterations
|
|
30
|
+
from .util import convert_to_rid_set, logger
|
|
31
|
+
|
|
32
|
+
REPORTED_COPY_VARIANTS = (INPUT_COPY_CATEGORIES.AMP, INPUT_COPY_CATEGORIES.DEEP)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def get_second_pass_variants(
|
|
36
|
+
graphkb_conn: GraphKBConnection, statements: List[Statement]
|
|
37
|
+
) -> List[Variant]:
|
|
38
|
+
"""Given a list of statements that have been matched, convert these to
|
|
39
|
+
new category variants to be used in a second-pass matching.
|
|
40
|
+
"""
|
|
41
|
+
# second-pass matching
|
|
42
|
+
all_inferred_matches: Dict[str, Variant] = {}
|
|
43
|
+
inferred_variants = {
|
|
44
|
+
(s["subject"]["@rid"], s["relevance"]["name"])
|
|
45
|
+
for s in statements
|
|
46
|
+
if s["subject"] and s["subject"]["@class"] in ("Feature", "Signature")
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for reference1, variant_type in inferred_variants:
|
|
50
|
+
variants = gkb_match.match_category_variant(graphkb_conn, reference1, variant_type)
|
|
51
|
+
|
|
52
|
+
for variant in variants:
|
|
53
|
+
all_inferred_matches[variant["@rid"]] = variant
|
|
54
|
+
inferred_matches: List[Variant] = list(all_inferred_matches.values())
|
|
55
|
+
return inferred_matches
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def get_ipr_statements_from_variants(
|
|
59
|
+
graphkb_conn: GraphKBConnection, matches: List[Variant], disease_name: str
|
|
60
|
+
) -> List[KbMatch]:
|
|
61
|
+
"""IPR upload formatted GraphKB statements from the list of variants.
|
|
62
|
+
|
|
63
|
+
Matches to GraphKB statements from the list of input variants. From these results matches
|
|
64
|
+
again with the inferred variants. Then returns the results formatted for upload to IPR
|
|
65
|
+
"""
|
|
66
|
+
if not matches:
|
|
67
|
+
return []
|
|
68
|
+
rows = []
|
|
69
|
+
statements = get_statements_from_variants(graphkb_conn, matches)
|
|
70
|
+
existing_statements = {s["@rid"] for s in statements}
|
|
71
|
+
|
|
72
|
+
for ipr_row in convert_statements_to_alterations(
|
|
73
|
+
graphkb_conn, statements, disease_name, convert_to_rid_set(matches)
|
|
74
|
+
):
|
|
75
|
+
rows.append(ipr_row)
|
|
76
|
+
|
|
77
|
+
# second-pass matching
|
|
78
|
+
inferred_matches = get_second_pass_variants(graphkb_conn, statements)
|
|
79
|
+
|
|
80
|
+
inferred_statements = [
|
|
81
|
+
s
|
|
82
|
+
for s in get_statements_from_variants(graphkb_conn, inferred_matches)
|
|
83
|
+
if s["@rid"] not in existing_statements # do not duplicate if non-inferred match
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
for ipr_row in convert_statements_to_alterations(
|
|
87
|
+
graphkb_conn, inferred_statements, disease_name, convert_to_rid_set(inferred_matches)
|
|
88
|
+
):
|
|
89
|
+
ipr_row["kbData"]["inferred"] = True
|
|
90
|
+
rows.append(ipr_row)
|
|
91
|
+
|
|
92
|
+
return rows
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def annotate_expression_variants(
|
|
96
|
+
graphkb_conn: GraphKBConnection,
|
|
97
|
+
variants: List[IprExprVariant],
|
|
98
|
+
disease_name: str,
|
|
99
|
+
show_progress: bool = False,
|
|
100
|
+
) -> List[KbMatch]:
|
|
101
|
+
"""Annotate expression variants with GraphKB in the IPR alterations format.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
graphkb_conn: the graphkb api connection object
|
|
105
|
+
variants: list of variants
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
list of kbMatches records for IPR
|
|
109
|
+
"""
|
|
110
|
+
skipped = 0
|
|
111
|
+
alterations = []
|
|
112
|
+
problem_genes = set()
|
|
113
|
+
logger.info(f"Starting annotation of {len(variants)} expression category_variants")
|
|
114
|
+
iterfunc = tqdm if show_progress else iter
|
|
115
|
+
for row in iterfunc(variants):
|
|
116
|
+
gene = row["gene"]
|
|
117
|
+
variant = row["variant"]
|
|
118
|
+
|
|
119
|
+
if not variant:
|
|
120
|
+
skipped += 1
|
|
121
|
+
logger.debug(f"Skipping malformed Expression {gene}: {row}")
|
|
122
|
+
continue
|
|
123
|
+
try:
|
|
124
|
+
matches = gkb_match.match_expression_variant(graphkb_conn, gene, variant)
|
|
125
|
+
for ipr_row in get_ipr_statements_from_variants(graphkb_conn, matches, disease_name):
|
|
126
|
+
ipr_row["variant"] = row["key"]
|
|
127
|
+
ipr_row["variantType"] = row.get("variantType", "exp")
|
|
128
|
+
alterations.append(ipr_row)
|
|
129
|
+
except FeatureNotFoundError as err:
|
|
130
|
+
problem_genes.add(gene)
|
|
131
|
+
logger.debug(f"Unrecognized gene ({gene} {variant}): {err}")
|
|
132
|
+
except ValueError as err:
|
|
133
|
+
logger.error(f"failed to match variants ({gene} {variant}): {err}")
|
|
134
|
+
|
|
135
|
+
if skipped:
|
|
136
|
+
logger.info(f"skipped matching {skipped} expression information rows")
|
|
137
|
+
if problem_genes:
|
|
138
|
+
logger.error(f"gene finding failures for expression {sorted(problem_genes)}")
|
|
139
|
+
logger.error(f"gene finding falure for {len(problem_genes)} expression genes")
|
|
140
|
+
logger.info(
|
|
141
|
+
f"matched {len(variants)} expression variants to {len(alterations)} graphkb annotations"
|
|
142
|
+
)
|
|
143
|
+
return alterations
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def annotate_copy_variants(
|
|
147
|
+
graphkb_conn: GraphKBConnection,
|
|
148
|
+
variants: List[IprCopyVariant],
|
|
149
|
+
disease_name: str,
|
|
150
|
+
show_progress: bool = False,
|
|
151
|
+
) -> List[KbMatch]:
|
|
152
|
+
"""Annotate allowed copy variants with GraphKB in the IPR alterations format.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
graphkb_conn: the graphkb api connection object
|
|
156
|
+
variants: list of variants
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
list of kbMatches records for IPR
|
|
160
|
+
"""
|
|
161
|
+
skipped = 0
|
|
162
|
+
alterations = []
|
|
163
|
+
problem_genes = set()
|
|
164
|
+
|
|
165
|
+
logger.info(f"Starting annotation of {len(variants)} copy category_variants")
|
|
166
|
+
iterfunc = tqdm if show_progress else iter
|
|
167
|
+
for row in iterfunc(variants):
|
|
168
|
+
gene = row["gene"]
|
|
169
|
+
variant = row["variant"]
|
|
170
|
+
|
|
171
|
+
if variant not in REPORTED_COPY_VARIANTS:
|
|
172
|
+
# https://www.bcgsc.ca/jira/browse/GERO-77
|
|
173
|
+
skipped += 1
|
|
174
|
+
logger.debug(f"Dropping {gene} copy change '{variant}' - not in REPORTED_COPY_VARIANTS")
|
|
175
|
+
continue
|
|
176
|
+
try:
|
|
177
|
+
matches = gkb_match.match_copy_variant(graphkb_conn, gene, variant)
|
|
178
|
+
for ipr_row in get_ipr_statements_from_variants(graphkb_conn, matches, disease_name):
|
|
179
|
+
ipr_row["variant"] = row["key"]
|
|
180
|
+
ipr_row["variantType"] = row.get("variantType", "cnv")
|
|
181
|
+
alterations.append(ipr_row)
|
|
182
|
+
except FeatureNotFoundError as err:
|
|
183
|
+
problem_genes.add(gene)
|
|
184
|
+
logger.debug(f"Unrecognized gene ({gene} {variant}): {err}")
|
|
185
|
+
except ValueError as err:
|
|
186
|
+
logger.error(f"failed to match variants ({gene} {variant}): {err}")
|
|
187
|
+
|
|
188
|
+
if skipped:
|
|
189
|
+
logger.info(
|
|
190
|
+
f"skipped matching {skipped} copy number variants not in {REPORTED_COPY_VARIANTS}"
|
|
191
|
+
)
|
|
192
|
+
if problem_genes:
|
|
193
|
+
logger.error(f"gene finding failures for copy variants {sorted(problem_genes)}")
|
|
194
|
+
logger.error(f"gene finding failure for {len(problem_genes)} copy variant genes")
|
|
195
|
+
logger.info(
|
|
196
|
+
f"matched {len(variants)} copy category variants to {len(alterations)} graphkb annotations"
|
|
197
|
+
)
|
|
198
|
+
return alterations
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def annotate_positional_variants(
|
|
202
|
+
graphkb_conn: GraphKBConnection,
|
|
203
|
+
variants: Sequence[IprStructuralVariant] | Sequence[Hashabledict],
|
|
204
|
+
disease_name: str,
|
|
205
|
+
show_progress: bool = False,
|
|
206
|
+
) -> List[Hashabledict]:
|
|
207
|
+
"""Annotate SNP, INDEL or fusion variant calls with GraphKB and return in IPR match format.
|
|
208
|
+
|
|
209
|
+
Hashable type is required to turn lists into sets.
|
|
210
|
+
Args:
|
|
211
|
+
graphkb_conn (GraphKBConnection): the graphkb api connection object
|
|
212
|
+
variants (list.<dict>): list of variants. Defaults to [].
|
|
213
|
+
disease_name (str): GraphKB disease name for statement matching. 'cancer' is most general
|
|
214
|
+
show_progress (bool): Progressbar displayed for long runs.
|
|
215
|
+
|
|
216
|
+
Returns:
|
|
217
|
+
Hashable list of kbMatches records for IPR
|
|
218
|
+
"""
|
|
219
|
+
VARIANT_KEYS = ("variant", "hgvsProtein", "hgvsCds", "hgvsGenomic")
|
|
220
|
+
errors = 0
|
|
221
|
+
alterations: List[Hashabledict] = []
|
|
222
|
+
problem_genes = set()
|
|
223
|
+
|
|
224
|
+
iterfunc = tqdm if show_progress else iter
|
|
225
|
+
for row in iterfunc(variants):
|
|
226
|
+
if not row.get("gene") and (not row.get("gene1") or not row.get("gene2")):
|
|
227
|
+
# https://www.bcgsc.ca/jira/browse/GERO-56?focusedCommentId=1234791&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1234791
|
|
228
|
+
# should not match single gene SVs
|
|
229
|
+
continue
|
|
230
|
+
|
|
231
|
+
for var_key in VARIANT_KEYS:
|
|
232
|
+
variant = row.get(var_key)
|
|
233
|
+
matches = []
|
|
234
|
+
if not variant or isnull(variant):
|
|
235
|
+
continue
|
|
236
|
+
try:
|
|
237
|
+
try:
|
|
238
|
+
matches = gkb_match.match_positional_variant(graphkb_conn, variant)
|
|
239
|
+
except HTTPError as parse_err:
|
|
240
|
+
# DEVSU-1885 - fix malformed single deletion described as substitution of blank
|
|
241
|
+
# eg. deletion described as substitution with nothing: 'chr1:g.150951027T>'
|
|
242
|
+
if (
|
|
243
|
+
variant[-1] == ">"
|
|
244
|
+
and "g." in variant
|
|
245
|
+
and variant[-2].isalpha()
|
|
246
|
+
and variant[-3].isnumeric()
|
|
247
|
+
):
|
|
248
|
+
logger.warning(
|
|
249
|
+
f"Assuming malformed deletion variant {variant} is {variant[:-2] + 'del'}"
|
|
250
|
+
)
|
|
251
|
+
variant = variant[:-2] + "del"
|
|
252
|
+
matches = gkb_match.match_positional_variant(graphkb_conn, variant)
|
|
253
|
+
else:
|
|
254
|
+
raise parse_err
|
|
255
|
+
|
|
256
|
+
for ipr_row in get_ipr_statements_from_variants(
|
|
257
|
+
graphkb_conn, matches, disease_name
|
|
258
|
+
):
|
|
259
|
+
ipr_row["variant"] = row["key"]
|
|
260
|
+
ipr_row["variantType"] = row.get(
|
|
261
|
+
"variantType", "mut" if row.get("gene") else "sv"
|
|
262
|
+
)
|
|
263
|
+
alterations.append(Hashabledict(ipr_row))
|
|
264
|
+
|
|
265
|
+
except FeatureNotFoundError as err:
|
|
266
|
+
logger.debug(f"failed to match positional variants ({variant}): {err}")
|
|
267
|
+
errors += 1
|
|
268
|
+
if "gene" in row:
|
|
269
|
+
problem_genes.add(row["gene"])
|
|
270
|
+
elif "gene1" in row and f"({row['gene1']})" in str(err):
|
|
271
|
+
problem_genes.add(row["gene1"])
|
|
272
|
+
elif "gene2" in row and f"({row['gene2']})" in str(err):
|
|
273
|
+
problem_genes.add(row["gene2"])
|
|
274
|
+
elif "gene1" in row and "gene2" in row:
|
|
275
|
+
problem_genes.add(row["gene1"])
|
|
276
|
+
problem_genes.add(row["gene2"])
|
|
277
|
+
else:
|
|
278
|
+
raise err
|
|
279
|
+
except HTTPError as err:
|
|
280
|
+
errors += 1
|
|
281
|
+
logger.error(f"failed to match positional variants ({variant}): {err}")
|
|
282
|
+
|
|
283
|
+
if problem_genes:
|
|
284
|
+
logger.error(f"gene finding failures for {sorted(problem_genes)}")
|
|
285
|
+
logger.error(f"{len(problem_genes)} gene finding failures for positional variants")
|
|
286
|
+
if errors:
|
|
287
|
+
logger.error(f"skipped {errors} positional variants due to errors")
|
|
288
|
+
|
|
289
|
+
# drop duplicates
|
|
290
|
+
alterations = list(set(alterations))
|
|
291
|
+
|
|
292
|
+
variant_types = ", ".join(sorted(set([alt["variantType"] for alt in alterations])))
|
|
293
|
+
logger.info(
|
|
294
|
+
f"matched {len(variants)} {variant_types} positional variants to {len(alterations)} graphkb annotations"
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
return alterations
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def annotate_msi(
|
|
301
|
+
graphkb_conn: GraphKBConnection,
|
|
302
|
+
disease_name: str = "cancer",
|
|
303
|
+
msi_category: str = "microsatellite instability",
|
|
304
|
+
) -> List[KbMatch]:
|
|
305
|
+
"""Annotate microsatellite instablity from GraphKB in the IPR alterations format.
|
|
306
|
+
|
|
307
|
+
Match to GraphKb Category variants with similar names
|
|
308
|
+
Args:
|
|
309
|
+
graphkb_conn: the graphkb api connection object
|
|
310
|
+
msi_category: such as 'microsatellite instability'
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
list of kbMatches records for IPR
|
|
314
|
+
"""
|
|
315
|
+
gkb_matches = []
|
|
316
|
+
msi_categories = graphkb_conn.query(
|
|
317
|
+
{
|
|
318
|
+
"target": {
|
|
319
|
+
"target": "CategoryVariant",
|
|
320
|
+
"filters": {
|
|
321
|
+
"reference1": {"target": "Signature", "filters": {"name": msi_category}}
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
"queryType": "similarTo",
|
|
325
|
+
"returnProperties": ["@rid", "displayName"],
|
|
326
|
+
}
|
|
327
|
+
)
|
|
328
|
+
if msi_categories:
|
|
329
|
+
msi_variants = [cast(Variant, var) for var in msi_categories]
|
|
330
|
+
for ipr_row in get_ipr_statements_from_variants(graphkb_conn, msi_variants, disease_name):
|
|
331
|
+
ipr_row["variant"] = msi_category
|
|
332
|
+
ipr_row["variantType"] = "msi"
|
|
333
|
+
gkb_matches.append(ipr_row)
|
|
334
|
+
return gkb_matches
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def annotate_tmb(
|
|
338
|
+
graphkb_conn: GraphKBConnection, disease_name: str = "cancer", category: str = TMB_HIGH_CATEGORY
|
|
339
|
+
) -> List[KbMatch]:
|
|
340
|
+
"""Annotate Tumour Mutation Burden (tmb) categories from GraphKB in the IPR alterations format.
|
|
341
|
+
|
|
342
|
+
Match to GraphKb Category variants with similar names
|
|
343
|
+
Args:
|
|
344
|
+
graphkb_conn: the graphkb api connection object
|
|
345
|
+
disease_name: oncotree disease name for graphkb matching.
|
|
346
|
+
category: such as 'high mutation burden'
|
|
347
|
+
|
|
348
|
+
Returns:
|
|
349
|
+
list of kbMatches records for IPR
|
|
350
|
+
"""
|
|
351
|
+
gkb_matches = []
|
|
352
|
+
categories = graphkb_conn.query(
|
|
353
|
+
{
|
|
354
|
+
"target": {
|
|
355
|
+
"target": "CategoryVariant",
|
|
356
|
+
"filters": {
|
|
357
|
+
"reference1": {
|
|
358
|
+
"target": "Signature",
|
|
359
|
+
"filters": {"OR": [{"name": category}, {"displayName": category}]},
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
"queryType": "similarTo",
|
|
364
|
+
"returnProperties": ["@rid", "displayName"],
|
|
365
|
+
}
|
|
366
|
+
)
|
|
367
|
+
if categories:
|
|
368
|
+
cat_variants = [cast(Variant, var) for var in categories]
|
|
369
|
+
for ipr_row in get_ipr_statements_from_variants(graphkb_conn, cat_variants, disease_name):
|
|
370
|
+
ipr_row["variant"] = category
|
|
371
|
+
ipr_row["variantType"] = "tmb"
|
|
372
|
+
gkb_matches.append(ipr_row)
|
|
373
|
+
return gkb_matches
|