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
pori_python/ipr/main.py
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import datetime
|
|
5
|
+
import json
|
|
6
|
+
import jsonschema.exceptions
|
|
7
|
+
import logging
|
|
8
|
+
import os
|
|
9
|
+
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
|
|
10
|
+
from typing import Dict, List, Sequence
|
|
11
|
+
|
|
12
|
+
from pori_python.graphkb import GraphKBConnection
|
|
13
|
+
from pori_python.graphkb.genes import get_gene_information
|
|
14
|
+
from pori_python.types import Hashabledict, IprVariant
|
|
15
|
+
|
|
16
|
+
from .annotate import (
|
|
17
|
+
annotate_copy_variants,
|
|
18
|
+
annotate_expression_variants,
|
|
19
|
+
annotate_msi,
|
|
20
|
+
annotate_positional_variants,
|
|
21
|
+
annotate_tmb,
|
|
22
|
+
)
|
|
23
|
+
from .connection import IprConnection
|
|
24
|
+
from .constants import DEFAULT_URL, TMB_HIGH, TMB_HIGH_CATEGORY
|
|
25
|
+
from .inputs import (
|
|
26
|
+
check_comparators,
|
|
27
|
+
check_variant_links,
|
|
28
|
+
preprocess_copy_variants,
|
|
29
|
+
preprocess_expression_variants,
|
|
30
|
+
preprocess_small_mutations,
|
|
31
|
+
preprocess_structural_variants,
|
|
32
|
+
validate_report_content,
|
|
33
|
+
)
|
|
34
|
+
from .ipr import (
|
|
35
|
+
create_key_alterations,
|
|
36
|
+
filter_structural_variants,
|
|
37
|
+
germline_kb_matches,
|
|
38
|
+
select_expression_plots,
|
|
39
|
+
)
|
|
40
|
+
from .summary import auto_analyst_comments
|
|
41
|
+
from .therapeutic_options import create_therapeutic_options
|
|
42
|
+
from .util import LOG_LEVELS, logger, trim_empty_values
|
|
43
|
+
|
|
44
|
+
CACHE_GENE_MINIMUM = 5000
|
|
45
|
+
RENAMED_GENE_PROPERTIES = {
|
|
46
|
+
# old_name: new_name
|
|
47
|
+
"cancerRelated": "kbStatementRelated",
|
|
48
|
+
"cancerGene": "cancerGeneListMatch",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def file_path(path: str) -> str:
|
|
53
|
+
if not os.path.exists(path):
|
|
54
|
+
raise argparse.ArgumentTypeError(f"{repr(path)} is not a valid filename. does not exist")
|
|
55
|
+
return path
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def timestamp() -> str:
|
|
59
|
+
return datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def command_interface() -> None:
|
|
63
|
+
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
|
|
64
|
+
req = parser.add_argument_group("required arguments")
|
|
65
|
+
(req if not os.environ.get("USER") else parser).add_argument(
|
|
66
|
+
"--username",
|
|
67
|
+
required=not os.environ.get("USER"),
|
|
68
|
+
default=os.environ.get("USER"),
|
|
69
|
+
help="username to use connecting to graphkb/ipr",
|
|
70
|
+
)
|
|
71
|
+
req.add_argument("--password", required=True, help="password to use connecting to graphkb/ipr")
|
|
72
|
+
req.add_argument(
|
|
73
|
+
"-c", "--content", required=True, type=file_path, help="Report Content as JSON"
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
parser.add_argument("--ipr_url", default=os.environ.get("IPR_URL", DEFAULT_URL))
|
|
77
|
+
parser.add_argument(
|
|
78
|
+
"--graphkb_username", help="username to use connecting to graphkb if different from ipr"
|
|
79
|
+
)
|
|
80
|
+
parser.add_argument(
|
|
81
|
+
"--graphkb_password", help="password to use connecting to graphkb if different from ipr"
|
|
82
|
+
)
|
|
83
|
+
parser.add_argument("--graphkb_url", default=os.environ.get("GRAPHKB_URL", None))
|
|
84
|
+
parser.add_argument("--log_level", default="info", choices=LOG_LEVELS.keys())
|
|
85
|
+
parser.add_argument(
|
|
86
|
+
"--therapeutics", default=False, help="Generate therapeutic options", action="store_true"
|
|
87
|
+
)
|
|
88
|
+
parser.add_argument(
|
|
89
|
+
"--skip_comments",
|
|
90
|
+
default=False,
|
|
91
|
+
action="store_true",
|
|
92
|
+
help="Turn off generating the analyst comments section of the report",
|
|
93
|
+
)
|
|
94
|
+
parser.add_argument(
|
|
95
|
+
"-o", "--output_json_path", help="path to a JSON to output the report upload body"
|
|
96
|
+
)
|
|
97
|
+
parser.add_argument(
|
|
98
|
+
"-w",
|
|
99
|
+
"--always_write_output_json",
|
|
100
|
+
action="store_true",
|
|
101
|
+
help="Write to output_json_path on successful IPR uploads instead of just when the upload fails",
|
|
102
|
+
)
|
|
103
|
+
parser.add_argument(
|
|
104
|
+
"--async_upload",
|
|
105
|
+
default=False,
|
|
106
|
+
action="store_true",
|
|
107
|
+
help="True if reports-async ipr endpoint should be used instead of basic reports",
|
|
108
|
+
)
|
|
109
|
+
parser.add_argument(
|
|
110
|
+
"--mins_to_wait",
|
|
111
|
+
default=5,
|
|
112
|
+
action="store",
|
|
113
|
+
help="is using reports-async, number of minutes to wait before throwing error",
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
args = parser.parse_args()
|
|
117
|
+
|
|
118
|
+
with open(args.content, "r") as fh:
|
|
119
|
+
content = json.load(fh)
|
|
120
|
+
|
|
121
|
+
create_report(
|
|
122
|
+
username=args.username,
|
|
123
|
+
password=args.password,
|
|
124
|
+
content=content,
|
|
125
|
+
ipr_url=args.ipr_url,
|
|
126
|
+
graphkb_username=args.graphkb_username,
|
|
127
|
+
graphkb_password=args.graphkb_password,
|
|
128
|
+
graphkb_url=args.graphkb_url,
|
|
129
|
+
log_level=args.log_level,
|
|
130
|
+
output_json_path=args.output_json_path,
|
|
131
|
+
always_write_output_json=args.always_write_output_json,
|
|
132
|
+
generate_therapeutics=args.therapeutics,
|
|
133
|
+
generate_comments=not args.skip_comments,
|
|
134
|
+
async_upload=args.async_upload,
|
|
135
|
+
mins_to_wait=args.mins_to_wait,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def clean_unsupported_content(upload_content: Dict, ipr_spec: Dict = {}) -> Dict:
|
|
140
|
+
"""Remove unsupported content.
|
|
141
|
+
This content is either added to facilitate creation
|
|
142
|
+
or to support upcoming and soon to be supported content that we would like
|
|
143
|
+
to implement but is not yet supported by the upload
|
|
144
|
+
"""
|
|
145
|
+
if (
|
|
146
|
+
ipr_spec
|
|
147
|
+
and "components" in ipr_spec.keys()
|
|
148
|
+
and "schemas" in ipr_spec["components"].keys()
|
|
149
|
+
and "genesCreate" in ipr_spec["components"]["schemas"].keys()
|
|
150
|
+
and "properties" in ipr_spec["components"]["schemas"]["genesCreate"].keys()
|
|
151
|
+
):
|
|
152
|
+
genes_spec = ipr_spec["components"]["schemas"]["genesCreate"]["properties"].keys()
|
|
153
|
+
|
|
154
|
+
# check what ipr report upload expects and adjust contents to match
|
|
155
|
+
for old_name, new_name in RENAMED_GENE_PROPERTIES.items():
|
|
156
|
+
if old_name in genes_spec:
|
|
157
|
+
logger.warning(
|
|
158
|
+
f"Legacy IPR - Renaming property {new_name} to {old_name} for compatibility to ipr_spec"
|
|
159
|
+
)
|
|
160
|
+
for gene in upload_content["genes"]:
|
|
161
|
+
if new_name in gene:
|
|
162
|
+
gene[old_name] = gene[new_name]
|
|
163
|
+
gene.pop(new_name)
|
|
164
|
+
else:
|
|
165
|
+
outdate_properties = 0
|
|
166
|
+
for gene in upload_content["genes"]:
|
|
167
|
+
if old_name in gene:
|
|
168
|
+
gene[new_name] = gene[old_name]
|
|
169
|
+
gene.pop(old_name)
|
|
170
|
+
outdate_properties += 1
|
|
171
|
+
if outdate_properties:
|
|
172
|
+
logger.warning(
|
|
173
|
+
f"Renamed property {old_name} to {new_name} on {outdate_properties} genes for ipr_spec"
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
# remove any unhandled incompatible keys
|
|
177
|
+
removed_keys: Dict[str, int] = {}
|
|
178
|
+
for gene in upload_content["genes"]:
|
|
179
|
+
unsupported_keys = [key for key in gene.keys() if key not in genes_spec]
|
|
180
|
+
for key in unsupported_keys:
|
|
181
|
+
if key in removed_keys:
|
|
182
|
+
removed_keys[key] += 1
|
|
183
|
+
else:
|
|
184
|
+
removed_keys[key] = 1
|
|
185
|
+
gene.pop(key)
|
|
186
|
+
for key, count in removed_keys.items():
|
|
187
|
+
logger.warning(f"IPR unsupported property '{key}' removed from {count} genes.")
|
|
188
|
+
|
|
189
|
+
drop_columns = ["variant", "variantType", "histogramImage"]
|
|
190
|
+
# DEVSU-2034 - use a 'displayName'
|
|
191
|
+
VARIANT_LIST_KEYS = [
|
|
192
|
+
"expressionVariants",
|
|
193
|
+
"smallMutations",
|
|
194
|
+
"copyVariants",
|
|
195
|
+
"structuralVariants",
|
|
196
|
+
"probeResults",
|
|
197
|
+
"msi",
|
|
198
|
+
]
|
|
199
|
+
for variant_list_section in VARIANT_LIST_KEYS:
|
|
200
|
+
for variant in upload_content.get(variant_list_section, []):
|
|
201
|
+
if not variant.get("displayName"):
|
|
202
|
+
variant["displayName"] = (
|
|
203
|
+
variant.get("variant") or variant.get("kbCategory") or variant.get("key", "")
|
|
204
|
+
)
|
|
205
|
+
if variant_list_section == "probeResults":
|
|
206
|
+
# currently probeResults will error if they do NOT have a 'variant' column.
|
|
207
|
+
# smallMutations will error if they DO have a 'variant' column.
|
|
208
|
+
continue
|
|
209
|
+
for col in drop_columns:
|
|
210
|
+
if col in variant:
|
|
211
|
+
del variant[col]
|
|
212
|
+
# tmburMutationBurden is a single value, not list
|
|
213
|
+
if upload_content.get("tmburMutationBurden"):
|
|
214
|
+
if not upload_content["tmburMutationBurden"].get("displayName"):
|
|
215
|
+
upload_content["tmburMutationBurden"]["displayName"] = upload_content[
|
|
216
|
+
"tmburMutationBurden"
|
|
217
|
+
].get("kbCategory", "")
|
|
218
|
+
|
|
219
|
+
for row in upload_content["kbMatches"]:
|
|
220
|
+
del row["kbContextId"]
|
|
221
|
+
del row["kbRelevanceId"]
|
|
222
|
+
return upload_content
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def create_report(**kwargs) -> Dict:
|
|
226
|
+
logger.warning("Deprecated function 'create_report' called - use ipr_report instead")
|
|
227
|
+
return ipr_report(**kwargs)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def ipr_report(
|
|
231
|
+
username: str,
|
|
232
|
+
password: str,
|
|
233
|
+
content: Dict,
|
|
234
|
+
ipr_url: str = DEFAULT_URL,
|
|
235
|
+
log_level: str = "info",
|
|
236
|
+
output_json_path: str = "",
|
|
237
|
+
always_write_output_json: bool = False,
|
|
238
|
+
ipr_upload: bool = True,
|
|
239
|
+
interactive: bool = False,
|
|
240
|
+
graphkb_username: str = "",
|
|
241
|
+
graphkb_password: str = "",
|
|
242
|
+
graphkb_url: str = "",
|
|
243
|
+
generate_therapeutics: bool = False,
|
|
244
|
+
generate_comments: bool = True,
|
|
245
|
+
match_germline: bool = False,
|
|
246
|
+
custom_kb_match_filter=None,
|
|
247
|
+
async_upload: bool = False,
|
|
248
|
+
mins_to_wait: int = 5,
|
|
249
|
+
) -> Dict:
|
|
250
|
+
"""Run the matching and create the report JSON for upload to IPR.
|
|
251
|
+
|
|
252
|
+
Args:
|
|
253
|
+
username: the username for connecting to GraphKB and IPR
|
|
254
|
+
password: the password for connecting to GraphKB and IPR
|
|
255
|
+
ipr_url: base URL to use in connecting to IPR
|
|
256
|
+
log_level: the logging level
|
|
257
|
+
content: report content
|
|
258
|
+
output_json_path: path to a JSON file to output the report upload body.
|
|
259
|
+
always_write_output_json: with successful IPR upload
|
|
260
|
+
ipr_upload: upload report to ipr
|
|
261
|
+
interactive: progressbars for interactive users
|
|
262
|
+
cache_gene_minimum: minimum number of genes required for gene name caching optimization
|
|
263
|
+
graphkb_username: the username for connecting to GraphKB if diff from IPR
|
|
264
|
+
graphkb_password: the password for connecting to GraphKB if diff from IPR
|
|
265
|
+
graphkb_url: the graphkb url to use if not default
|
|
266
|
+
generate_therapeutics: create therapeutic options for upload with the report
|
|
267
|
+
generate_comments: create the analyst comments section for upload with the report
|
|
268
|
+
match_germline: match only germline statements to germline events and non-germline statements to non-germline events.
|
|
269
|
+
custom_kb_match_filter: function(List[kbMatch]) -> List[kbMatch]
|
|
270
|
+
async_upload: use report_async endpoint to upload reports
|
|
271
|
+
mins_to_wait: if using report_async, number of minutes to wait for success before exception raised
|
|
272
|
+
|
|
273
|
+
Returns:
|
|
274
|
+
ipr_conn.upload_report return dictionary
|
|
275
|
+
"""
|
|
276
|
+
# set the default logging configuration
|
|
277
|
+
logging.basicConfig(
|
|
278
|
+
level=LOG_LEVELS[log_level],
|
|
279
|
+
format="%(asctime)s %(name)s %(levelname)s %(message)s",
|
|
280
|
+
datefmt="%m-%d-%y %H:%M:%S",
|
|
281
|
+
)
|
|
282
|
+
# validate the JSON content follows the specification
|
|
283
|
+
try:
|
|
284
|
+
validate_report_content(content)
|
|
285
|
+
except jsonschema.exceptions.ValidationError as err:
|
|
286
|
+
logger.error("Failed schema check - report variants may be corrupted or unmatched.")
|
|
287
|
+
logger.error(f"Failed schema check: {err}")
|
|
288
|
+
|
|
289
|
+
kb_disease_match = content["kbDiseaseMatch"]
|
|
290
|
+
|
|
291
|
+
# validate the input variants
|
|
292
|
+
small_mutations = preprocess_small_mutations(content.get("smallMutations", []))
|
|
293
|
+
structural_variants = preprocess_structural_variants(content.get("structuralVariants", []))
|
|
294
|
+
copy_variants = preprocess_copy_variants(content.get("copyVariants", []))
|
|
295
|
+
expression_variants = preprocess_expression_variants(content.get("expressionVariants", []))
|
|
296
|
+
if expression_variants:
|
|
297
|
+
check_comparators(content, expression_variants)
|
|
298
|
+
|
|
299
|
+
genes_with_variants = check_variant_links(
|
|
300
|
+
small_mutations, expression_variants, copy_variants, structural_variants
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
# Setup connections
|
|
304
|
+
ipr_conn = IprConnection(username, password, ipr_url)
|
|
305
|
+
ipr_spec = ipr_conn.get_spec()
|
|
306
|
+
|
|
307
|
+
if graphkb_url:
|
|
308
|
+
logger.info(f"connecting to graphkb: {graphkb_url}")
|
|
309
|
+
graphkb_conn = GraphKBConnection(graphkb_url)
|
|
310
|
+
else:
|
|
311
|
+
graphkb_conn = GraphKBConnection()
|
|
312
|
+
|
|
313
|
+
gkb_user = graphkb_username if graphkb_username else username
|
|
314
|
+
gkb_pass = graphkb_password if graphkb_password else password
|
|
315
|
+
|
|
316
|
+
graphkb_conn.login(gkb_user, gkb_pass)
|
|
317
|
+
|
|
318
|
+
gkb_matches: List[Hashabledict] = []
|
|
319
|
+
|
|
320
|
+
# Signature category variants
|
|
321
|
+
tmb_variant: IprVariant = {} # type: ignore
|
|
322
|
+
tmb_matches = []
|
|
323
|
+
if "tmburMutationBurden" in content.keys():
|
|
324
|
+
tmb_val = 0.0
|
|
325
|
+
tmb = {}
|
|
326
|
+
try:
|
|
327
|
+
tmb = content.get("tmburMutationBurden", {})
|
|
328
|
+
tmb_val = tmb["genomeIndelTmb"] + tmb["genomeSnvTmb"]
|
|
329
|
+
except Exception as err:
|
|
330
|
+
logger.error(f"tmburMutationBurden parsing failure: {err}")
|
|
331
|
+
|
|
332
|
+
if tmb_val >= TMB_HIGH:
|
|
333
|
+
logger.warning(
|
|
334
|
+
f"GERO-296 - tmburMutationBurden high -checking graphkb matches for {TMB_HIGH_CATEGORY}"
|
|
335
|
+
)
|
|
336
|
+
if not tmb.get("key"):
|
|
337
|
+
tmb["key"] = TMB_HIGH_CATEGORY
|
|
338
|
+
if not tmb.get("kbCategory"):
|
|
339
|
+
tmb["kbCategory"] = TMB_HIGH_CATEGORY
|
|
340
|
+
|
|
341
|
+
# GERO-296 - try matching to graphkb
|
|
342
|
+
tmb_matches = annotate_tmb(graphkb_conn, kb_disease_match, TMB_HIGH_CATEGORY)
|
|
343
|
+
if tmb_matches:
|
|
344
|
+
tmb_variant["kbCategory"] = TMB_HIGH_CATEGORY # type: ignore
|
|
345
|
+
tmb_variant["variant"] = TMB_HIGH_CATEGORY
|
|
346
|
+
tmb_variant["key"] = tmb["key"]
|
|
347
|
+
tmb_variant["variantType"] = "tmb"
|
|
348
|
+
logger.info(
|
|
349
|
+
f"GERO-296 '{TMB_HIGH_CATEGORY}' matches {len(tmb_matches)} statements."
|
|
350
|
+
)
|
|
351
|
+
gkb_matches.extend([Hashabledict(tmb_statement) for tmb_statement in tmb_matches])
|
|
352
|
+
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
|
|
353
|
+
|
|
354
|
+
msi = content.get("msi", [])
|
|
355
|
+
msi_matches = []
|
|
356
|
+
msi_variant: IprVariant = {} # type: ignore
|
|
357
|
+
if msi:
|
|
358
|
+
# only one msi variant per library
|
|
359
|
+
if isinstance(msi, list):
|
|
360
|
+
msi_cat = msi[0].get("kbCategory")
|
|
361
|
+
elif isinstance(msi, str):
|
|
362
|
+
msi_cat = msi
|
|
363
|
+
else:
|
|
364
|
+
msi_cat = msi.get("kbCategory")
|
|
365
|
+
msi_variant = msi.copy()
|
|
366
|
+
logger.info(f"Matching GKB msi {msi_cat}")
|
|
367
|
+
msi_matches = annotate_msi(graphkb_conn, kb_disease_match, msi_cat)
|
|
368
|
+
if msi_matches:
|
|
369
|
+
msi_variant["kbCategory"] = msi_cat # type: ignore
|
|
370
|
+
msi_variant["variant"] = msi_cat
|
|
371
|
+
msi_variant["key"] = msi_cat
|
|
372
|
+
msi_variant["variantType"] = "msi"
|
|
373
|
+
logger.info(f"GERO-295 '{msi_cat}' matches {len(msi_matches)} msi statements.")
|
|
374
|
+
gkb_matches.extend([Hashabledict(msi) for msi in msi_matches])
|
|
375
|
+
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
|
|
376
|
+
|
|
377
|
+
logger.info(f"annotating {len(small_mutations)} small mutations")
|
|
378
|
+
gkb_matches.extend(
|
|
379
|
+
annotate_positional_variants(
|
|
380
|
+
graphkb_conn, small_mutations, kb_disease_match, show_progress=interactive
|
|
381
|
+
)
|
|
382
|
+
)
|
|
383
|
+
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
|
|
384
|
+
|
|
385
|
+
logger.info(f"annotating {len(structural_variants)} structural variants")
|
|
386
|
+
gkb_matches.extend(
|
|
387
|
+
annotate_positional_variants(
|
|
388
|
+
graphkb_conn, structural_variants, kb_disease_match, show_progress=interactive
|
|
389
|
+
)
|
|
390
|
+
)
|
|
391
|
+
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
|
|
392
|
+
|
|
393
|
+
logger.info(f"annotating {len(copy_variants)} copy variants")
|
|
394
|
+
gkb_matches.extend(
|
|
395
|
+
[
|
|
396
|
+
Hashabledict(copy_var)
|
|
397
|
+
for copy_var in annotate_copy_variants(
|
|
398
|
+
graphkb_conn, copy_variants, kb_disease_match, show_progress=interactive
|
|
399
|
+
)
|
|
400
|
+
]
|
|
401
|
+
)
|
|
402
|
+
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
|
|
403
|
+
|
|
404
|
+
logger.info(f"annotating {len(expression_variants)} expression variants")
|
|
405
|
+
gkb_matches.extend(
|
|
406
|
+
[
|
|
407
|
+
Hashabledict(exp_var)
|
|
408
|
+
for exp_var in annotate_expression_variants(
|
|
409
|
+
graphkb_conn, expression_variants, kb_disease_match, show_progress=interactive
|
|
410
|
+
)
|
|
411
|
+
]
|
|
412
|
+
)
|
|
413
|
+
logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
|
|
414
|
+
|
|
415
|
+
all_variants: Sequence[IprVariant]
|
|
416
|
+
all_variants = expression_variants + copy_variants + structural_variants + small_mutations # type: ignore
|
|
417
|
+
if msi_matches:
|
|
418
|
+
all_variants.append(msi_variant) # type: ignore
|
|
419
|
+
if tmb_matches:
|
|
420
|
+
all_variants.append(tmb_variant) # type: ignore
|
|
421
|
+
|
|
422
|
+
if match_germline:
|
|
423
|
+
# verify germline kb statements matched germline observed variants, not somatic variants
|
|
424
|
+
org_len = len(gkb_matches)
|
|
425
|
+
gkb_matches = [
|
|
426
|
+
Hashabledict(match) for match in germline_kb_matches(gkb_matches, all_variants)
|
|
427
|
+
]
|
|
428
|
+
num_removed = org_len - len(gkb_matches)
|
|
429
|
+
if num_removed:
|
|
430
|
+
logger.info(f"Removing {num_removed} germline events without medical matches.")
|
|
431
|
+
|
|
432
|
+
if custom_kb_match_filter:
|
|
433
|
+
logger.info(f"custom_kb_match_filter on {len(gkb_matches)} variants")
|
|
434
|
+
gkb_matches = [Hashabledict(match) for match in custom_kb_match_filter(gkb_matches)]
|
|
435
|
+
logger.info(f"\t custom_kb_match_filter left {len(gkb_matches)} variants")
|
|
436
|
+
|
|
437
|
+
key_alterations, variant_counts = create_key_alterations(gkb_matches, all_variants)
|
|
438
|
+
|
|
439
|
+
logger.info("fetching gene annotations")
|
|
440
|
+
gene_information = get_gene_information(graphkb_conn, sorted(genes_with_variants))
|
|
441
|
+
|
|
442
|
+
if generate_therapeutics:
|
|
443
|
+
logger.info("generating therapeutic options")
|
|
444
|
+
targets = create_therapeutic_options(graphkb_conn, gkb_matches, all_variants)
|
|
445
|
+
else:
|
|
446
|
+
targets = []
|
|
447
|
+
|
|
448
|
+
logger.info("generating analyst comments")
|
|
449
|
+
if generate_comments:
|
|
450
|
+
comments = {
|
|
451
|
+
"comments": auto_analyst_comments(
|
|
452
|
+
graphkb_conn, gkb_matches, disease_name=kb_disease_match, variants=all_variants
|
|
453
|
+
)
|
|
454
|
+
}
|
|
455
|
+
else:
|
|
456
|
+
comments = {"comments": ""}
|
|
457
|
+
|
|
458
|
+
# thread safe deep-copy the original content
|
|
459
|
+
output = json.loads(json.dumps(content))
|
|
460
|
+
output.update(
|
|
461
|
+
{
|
|
462
|
+
"kbMatches": [trim_empty_values(a) for a in gkb_matches], # type: ignore
|
|
463
|
+
"copyVariants": [
|
|
464
|
+
trim_empty_values(c) for c in copy_variants if c["gene"] in genes_with_variants
|
|
465
|
+
],
|
|
466
|
+
"smallMutations": [trim_empty_values(s) for s in small_mutations],
|
|
467
|
+
"expressionVariants": [
|
|
468
|
+
trim_empty_values(e)
|
|
469
|
+
for e in expression_variants
|
|
470
|
+
if e["gene"] in genes_with_variants
|
|
471
|
+
],
|
|
472
|
+
"kbDiseaseMatch": kb_disease_match,
|
|
473
|
+
"kbUrl": graphkb_conn.url,
|
|
474
|
+
"kbVersion": timestamp(),
|
|
475
|
+
"structuralVariants": [
|
|
476
|
+
trim_empty_values(s)
|
|
477
|
+
for s in filter_structural_variants(
|
|
478
|
+
structural_variants, gkb_matches, gene_information
|
|
479
|
+
)
|
|
480
|
+
],
|
|
481
|
+
"genes": gene_information,
|
|
482
|
+
"genomicAlterationsIdentified": key_alterations,
|
|
483
|
+
"variantCounts": variant_counts,
|
|
484
|
+
"analystComments": comments,
|
|
485
|
+
"therapeuticTarget": targets,
|
|
486
|
+
}
|
|
487
|
+
)
|
|
488
|
+
output.setdefault("images", []).extend(select_expression_plots(gkb_matches, all_variants))
|
|
489
|
+
|
|
490
|
+
output = clean_unsupported_content(output, ipr_spec)
|
|
491
|
+
ipr_result = None
|
|
492
|
+
upload_error = None
|
|
493
|
+
|
|
494
|
+
if ipr_upload:
|
|
495
|
+
try:
|
|
496
|
+
logger.info(f"Uploading to IPR {ipr_conn.url}")
|
|
497
|
+
ipr_result = ipr_conn.upload_report(output, mins_to_wait, async_upload)
|
|
498
|
+
logger.info(ipr_result)
|
|
499
|
+
output.update(ipr_result)
|
|
500
|
+
except Exception as err:
|
|
501
|
+
upload_error = err
|
|
502
|
+
logger.error(f"ipr_conn.upload_report failed: {err}", exc_info=True)
|
|
503
|
+
if output_json_path:
|
|
504
|
+
if always_write_output_json or not ipr_result:
|
|
505
|
+
logger.info(f"Writing IPR upload json to: {output_json_path}")
|
|
506
|
+
with open(output_json_path, "w") as fh:
|
|
507
|
+
fh.write(json.dumps(output))
|
|
508
|
+
logger.info(f"made {graphkb_conn.request_count} requests to graphkb")
|
|
509
|
+
logger.info(f"average load {int(graphkb_conn.load or 0)} req/s")
|
|
510
|
+
if upload_error:
|
|
511
|
+
raise upload_error
|
|
512
|
+
return output
|
pori_python/ipr/py.typed
ADDED
|
File without changes
|