pori-python 1.0.0__tar.gz → 1.2.0__tar.gz

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.
Files changed (58) hide show
  1. {pori_python-1.0.0/pori_python.egg-info → pori_python-1.2.0}/PKG-INFO +21 -5
  2. {pori_python-1.0.0 → pori_python-1.2.0}/README.md +20 -4
  3. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/util.py +49 -3
  4. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/annotate.py +102 -88
  5. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/connection.py +35 -3
  6. pori_python-1.2.0/pori_python/ipr/constants.py +28 -0
  7. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/inputs.py +84 -9
  8. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/ipr.py +63 -17
  9. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/main.py +89 -154
  10. {pori_python-1.0.0 → pori_python-1.2.0/pori_python.egg-info}/PKG-INFO +21 -5
  11. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python.egg-info/SOURCES.txt +1 -2
  12. {pori_python-1.0.0 → pori_python-1.2.0}/setup.cfg +3 -3
  13. pori_python-1.2.0/tests/test_graphkb/data.py +198 -0
  14. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/test_match.py +13 -1
  15. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_annotate.py +92 -2
  16. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_inputs.py +75 -27
  17. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_ipr.py +56 -30
  18. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_main.py +1 -0
  19. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_upload.py +9 -7
  20. pori_python-1.0.0/pori_python/ipr/constants.py +0 -12
  21. pori_python-1.0.0/pori_python/ipr/py.typed +0 -0
  22. pori_python-1.0.0/tests/test_graphkb/data.py +0 -78
  23. {pori_python-1.0.0 → pori_python-1.2.0}/LICENSE +0 -0
  24. {pori_python-1.0.0 → pori_python-1.2.0}/MANIFEST.in +0 -0
  25. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/__init__.py +0 -0
  26. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/__init__.py +0 -0
  27. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/constants.py +0 -0
  28. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/genes.py +0 -0
  29. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/match.py +0 -0
  30. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/statement.py +0 -0
  31. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/graphkb/vocab.py +0 -0
  32. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/__init__.py +0 -0
  33. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/content.spec.json +0 -0
  34. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/summary.py +0 -0
  35. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/therapeutic_options.py +0 -0
  36. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/ipr/util.py +0 -0
  37. {pori_python-1.0.0/pori_python/graphkb → pori_python-1.2.0/pori_python}/py.typed +0 -0
  38. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python/types.py +0 -0
  39. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python.egg-info/dependency_links.txt +0 -0
  40. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python.egg-info/entry_points.txt +0 -0
  41. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python.egg-info/requires.txt +0 -0
  42. {pori_python-1.0.0 → pori_python-1.2.0}/pori_python.egg-info/top_level.txt +0 -0
  43. {pori_python-1.0.0 → pori_python-1.2.0}/pyproject.toml +0 -0
  44. {pori_python-1.0.0 → pori_python-1.2.0}/setup.py +0 -0
  45. {pori_python-1.0.0 → pori_python-1.2.0}/tests/__init__.py +0 -0
  46. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/__init__.py +0 -0
  47. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/test_genes.py +0 -0
  48. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/test_graphkb.py +0 -0
  49. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/test_statement.py +0 -0
  50. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/test_util.py +0 -0
  51. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_graphkb/test_vocab.py +0 -0
  52. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/__init__.py +0 -0
  53. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/constants.py +0 -0
  54. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_connection.py +0 -0
  55. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_probe.py +0 -0
  56. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_summary.py +0 -0
  57. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/test_util.py +0 -0
  58. {pori_python-1.0.0 → pori_python-1.2.0}/tests/test_ipr/util.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pori_python
3
- Version: 1.0.0
3
+ Version: 1.2.0
4
4
  Home-page: https://github.com/bcgsc/pori_python
5
5
  Author-email: dat@bcgsc.ca
6
6
  Maintainer-email: dat@bcgsc.ca
@@ -61,6 +61,7 @@ For documentation on how to create reports using the IPR adaptor, see the [main
61
61
 
62
62
  - [Getting Started](#getting-started)
63
63
  - [Install (For developers)](#install-for-developers)
64
+ - [JSON Validate and Upload to IPR](#json-validate-and-upload-to-ipr)
64
65
  - [Documentation](#documentation)
65
66
  - [Deployment (Publishing)](#deployment-publishing)
66
67
 
@@ -97,7 +98,7 @@ Note that IPR tests will try to use the BCGSC production GraphKB API by default.
97
98
  If you want to test interaction with a different instance, you will need to
98
99
  set the GraphKB variables.
99
100
 
100
- Set EXCLUDE vars to 1 if you don't want to run these tests.
101
+ Set EXCLUDE vars to 1 if you don't want to run these tests.
101
102
  ONCOKB and BCGSC tests are enabled by default.
102
103
 
103
104
  ```bash
@@ -107,23 +108,38 @@ export IPR_URL='http://localhost:8081/api'
107
108
  export GRAPHKB_USER='pori_admin'
108
109
  export GRAPHKB_PASS='pori_admin'
109
110
  export GRAPHKB_URL='http://localhost:8080/api'
110
- EXCLUDE_BCGSC_TESTS = 1
111
- EXCLUDE_ONCOKB_TESTS = 1
111
+ export EXCLUDE_BCGSC_TESTS=1
112
+ export EXCLUDE_ONCOKB_TESTS=1
112
113
  ```
113
114
 
114
115
  If you want to run tests that upload reports to a live IPR instance,
115
116
  specify the url of the IPR API you want to use and set the test var to 1.
116
117
  These tests are disabled by default.
117
118
 
119
+ The created reports are deleted by default. If you want to keep them,
120
+ set DELETE_UPLOAD_TEST_REPORTS to 0 in the env.
121
+
118
122
  ```bash
119
123
  export IPR_TEST_URL='http://localhost:8081/api'
120
- INCLUDE_UPLOAD_TESTS = 1
124
+ export INCLUDE_UPLOAD_TESTS=1
125
+ export DELETE_UPLOAD_TEST_REPORTS=0
121
126
  ```
122
127
 
123
128
  ```bash
124
129
  pytest tests
125
130
  ```
126
131
 
132
+ ### JSON Validate and Upload to IPR
133
+ If you only want to validate the json content, use
134
+ ```bash
135
+ ipr --password $IPR_PASS -c 'path/to/content.json' --validate_json
136
+ ```
137
+
138
+ If you only want to upload the json directly to ipr and skip all the preprocessing, use
139
+ ```bash
140
+ ipr --password $IPR_PASS -c 'path/to/content.json' --upload_json
141
+ ```
142
+
127
143
  ## Documentation
128
144
 
129
145
  The user documentation for this tool is hosted with the [main documentation site](https://bcgsc.github.io/pori/).
@@ -15,6 +15,7 @@ For documentation on how to create reports using the IPR adaptor, see the [main
15
15
 
16
16
  - [Getting Started](#getting-started)
17
17
  - [Install (For developers)](#install-for-developers)
18
+ - [JSON Validate and Upload to IPR](#json-validate-and-upload-to-ipr)
18
19
  - [Documentation](#documentation)
19
20
  - [Deployment (Publishing)](#deployment-publishing)
20
21
 
@@ -51,7 +52,7 @@ Note that IPR tests will try to use the BCGSC production GraphKB API by default.
51
52
  If you want to test interaction with a different instance, you will need to
52
53
  set the GraphKB variables.
53
54
 
54
- Set EXCLUDE vars to 1 if you don't want to run these tests.
55
+ Set EXCLUDE vars to 1 if you don't want to run these tests.
55
56
  ONCOKB and BCGSC tests are enabled by default.
56
57
 
57
58
  ```bash
@@ -61,23 +62,38 @@ export IPR_URL='http://localhost:8081/api'
61
62
  export GRAPHKB_USER='pori_admin'
62
63
  export GRAPHKB_PASS='pori_admin'
63
64
  export GRAPHKB_URL='http://localhost:8080/api'
64
- EXCLUDE_BCGSC_TESTS = 1
65
- EXCLUDE_ONCOKB_TESTS = 1
65
+ export EXCLUDE_BCGSC_TESTS=1
66
+ export EXCLUDE_ONCOKB_TESTS=1
66
67
  ```
67
68
 
68
69
  If you want to run tests that upload reports to a live IPR instance,
69
70
  specify the url of the IPR API you want to use and set the test var to 1.
70
71
  These tests are disabled by default.
71
72
 
73
+ The created reports are deleted by default. If you want to keep them,
74
+ set DELETE_UPLOAD_TEST_REPORTS to 0 in the env.
75
+
72
76
  ```bash
73
77
  export IPR_TEST_URL='http://localhost:8081/api'
74
- INCLUDE_UPLOAD_TESTS = 1
78
+ export INCLUDE_UPLOAD_TESTS=1
79
+ export DELETE_UPLOAD_TEST_REPORTS=0
75
80
  ```
76
81
 
77
82
  ```bash
78
83
  pytest tests
79
84
  ```
80
85
 
86
+ ### JSON Validate and Upload to IPR
87
+ If you only want to validate the json content, use
88
+ ```bash
89
+ ipr --password $IPR_PASS -c 'path/to/content.json' --validate_json
90
+ ```
91
+
92
+ If you only want to upload the json directly to ipr and skip all the preprocessing, use
93
+ ```bash
94
+ ipr --password $IPR_PASS -c 'path/to/content.json' --upload_json
95
+ ```
96
+
81
97
  ## Documentation
82
98
 
83
99
  The user documentation for this tool is hosted with the [main documentation site](https://bcgsc.github.io/pori/).
@@ -9,6 +9,7 @@ import re
9
9
  import time
10
10
  from datetime import datetime
11
11
  from typing import Any, Dict, Iterable, List, Optional, Union, cast
12
+ from urllib.parse import urlsplit
12
13
  from urllib3.util.retry import Retry
13
14
 
14
15
  from pori_python.types import ParsedVariant, PositionalVariant, Record
@@ -112,6 +113,7 @@ class GraphKBConnection:
112
113
  )
113
114
  self.http.mount("https://", HTTPAdapter(max_retries=retries))
114
115
  self.token = ""
116
+ self.token_kc = ""
115
117
  self.url = url
116
118
  self.username = username
117
119
  self.password = password
@@ -163,11 +165,16 @@ class GraphKBConnection:
163
165
  # about catching OSError as well as ConnectionError:
164
166
  # https://stackoverflow.com/questions/74253820
165
167
  attempts = range(15)
168
+ need_refresh_login = False
166
169
  for attempt in attempts:
167
170
  if attempt > 0:
168
171
  time.sleep(2) # wait between retries
169
172
  try:
170
- self.refresh_login()
173
+
174
+ if need_refresh_login:
175
+ self.refresh_login()
176
+ need_refresh_login = False
177
+
171
178
  self.request_count += 1
172
179
  resp = requests.request(
173
180
  method, url, headers=self.headers, timeout=timeout, **kwargs
@@ -175,6 +182,7 @@ class GraphKBConnection:
175
182
  if resp.status_code == 401 or resp.status_code == 403:
176
183
  logger.debug(f"/{endpoint} - {resp.status_code} - retrying")
177
184
  # try to re-login if the token expired
185
+ need_refresh_login = True
178
186
  continue
179
187
  else:
180
188
  break
@@ -207,12 +215,45 @@ class GraphKBConnection:
207
215
  """Convenience method for making post requests."""
208
216
  return self.request(uri, method="POST", data=json.dumps(data), **kwargs)
209
217
 
210
- def login(self, username: str, password: str) -> None:
218
+ def login_demo(self) -> None:
219
+ """
220
+ KBDEV-1328
221
+ GraphKB login in GSC's PORI online demo is done in 2 steps:
222
+ 1. get a first token from KeyCloak using username and password; self.login_demo()
223
+ 2. get a second token from the GraphKB API using keyCloakToken; self.login()
224
+ """
225
+ url_parts = urlsplit(self.url)
226
+ base_url = f"{url_parts.scheme}://{url_parts.netloc}"
227
+
228
+ try:
229
+ resp = requests.request(
230
+ url=f"{base_url}/auth/realms/PORI/protocol/openid-connect/token",
231
+ method="POST",
232
+ data={
233
+ "client_id": "GraphKB",
234
+ "grant_type": "password",
235
+ "password": self.password,
236
+ "username": self.username,
237
+ },
238
+ )
239
+ except Exception as err:
240
+ logger.debug(f"unable to fetch a token from KeyCloak: {err}")
241
+ raise err
242
+
243
+ resp.raise_for_status()
244
+ content = resp.json()
245
+ self.token_kc = content["access_token"]
246
+
247
+ def login(self, username: str, password: str, pori_demo: bool = False) -> None:
211
248
  self.username = username
212
249
  self.password = password
213
250
  connect_timeout = 7
214
251
  read_timeout = 61
215
252
 
253
+ # KBDEV-1328. Alt. GraphKB login for GSC's PORI online demo
254
+ if pori_demo or "pori-demo" in self.url:
255
+ self.login_demo()
256
+
216
257
  # use requests package directly to avoid recursion loop on login failure
217
258
  attempts = range(10)
218
259
  for attempt in attempts:
@@ -225,7 +266,12 @@ class GraphKBConnection:
225
266
  method="POST",
226
267
  headers=self.headers,
227
268
  timeout=(connect_timeout, read_timeout),
228
- data=json.dumps({"username": username, "password": password}),
269
+ data=json.dumps(
270
+ # KBDEV-1328. Alt. GraphKB login for GSC's PORI online demo
271
+ {"keyCloakToken": self.token_kc}
272
+ if self.token_kc
273
+ else {"username": username, "password": password}
274
+ ),
229
275
  )
230
276
  break
231
277
  except (requests.exceptions.ConnectionError, OSError) as err:
@@ -8,7 +8,7 @@ from requests.exceptions import HTTPError
8
8
 
9
9
  from pandas import isnull
10
10
  from tqdm import tqdm
11
- from typing import Dict, List, Sequence, cast
11
+ from typing import Dict, List, Sequence
12
12
 
13
13
  from pori_python.graphkb import GraphKBConnection
14
14
  from pori_python.graphkb import match as gkb_match
@@ -20,13 +20,14 @@ from pori_python.types import (
20
20
  IprCopyVariant,
21
21
  IprExprVariant,
22
22
  IprSignatureVariant,
23
+ IprSmallMutationVariant,
23
24
  IprStructuralVariant,
24
25
  KbMatch,
25
26
  Statement,
26
27
  Variant,
27
28
  )
28
29
 
29
- from .constants import TMB_HIGH_CATEGORY
30
+ from .constants import TMB_SIGNATURE
30
31
  from .ipr import convert_statements_to_alterations
31
32
  from .util import convert_to_rid_set, logger
32
33
 
@@ -308,92 +309,6 @@ def annotate_positional_variants(
308
309
  return alterations
309
310
 
310
311
 
311
- def annotate_msi(
312
- graphkb_conn: GraphKBConnection,
313
- disease_matches: List[str],
314
- msi_category: str = "microsatellite instability",
315
- ) -> List[KbMatch]:
316
- """Annotate microsatellite instablity from GraphKB in the IPR alterations format.
317
-
318
- Match to GraphKb Category variants with similar names
319
- Args:
320
- graphkb_conn (GraphKBConnection): the graphkb api connection object
321
- disease_matches (list.str): GraphKB disease RIDs
322
- msi_category (str): such as 'microsatellite instability'
323
-
324
- Returns:
325
- list of kbMatches records for IPR
326
- """
327
- gkb_matches = []
328
- msi_categories = graphkb_conn.query(
329
- {
330
- "target": {
331
- "target": "CategoryVariant",
332
- "filters": {
333
- "reference1": {
334
- "target": "Signature",
335
- "filters": {"name": msi_category},
336
- }
337
- },
338
- },
339
- "queryType": "similarTo",
340
- "returnProperties": ["@rid", "displayName"],
341
- }
342
- )
343
- if msi_categories:
344
- msi_variants = [cast(Variant, var) for var in msi_categories]
345
- for ipr_row in get_ipr_statements_from_variants(
346
- graphkb_conn, msi_variants, disease_matches
347
- ):
348
- ipr_row["variant"] = msi_category
349
- ipr_row["variantType"] = "msi"
350
- gkb_matches.append(ipr_row)
351
- return gkb_matches
352
-
353
-
354
- def annotate_tmb(
355
- graphkb_conn: GraphKBConnection,
356
- disease_matches: List[str],
357
- category: str = TMB_HIGH_CATEGORY,
358
- ) -> List[KbMatch]:
359
- """Annotate Tumour Mutation Burden (tmb) categories from GraphKB in the IPR alterations format.
360
-
361
- Match to GraphKb Category variants with similar names
362
- Args:
363
- graphkb_conn (GraphKBConnection): the graphkb api connection object
364
- disease_matches (list.str): GraphKB disease RIDs
365
- category: such as 'high mutation burden'
366
-
367
- Returns:
368
- list of kbMatches records for IPR
369
- """
370
- gkb_matches = []
371
- categories = graphkb_conn.query(
372
- {
373
- "target": {
374
- "target": "CategoryVariant",
375
- "filters": {
376
- "reference1": {
377
- "target": "Signature",
378
- "filters": {"OR": [{"name": category}, {"displayName": category}]},
379
- }
380
- },
381
- },
382
- "queryType": "similarTo",
383
- "returnProperties": ["@rid", "displayName"],
384
- }
385
- )
386
- if categories:
387
- cat_variants = [cast(Variant, var) for var in categories]
388
- for ipr_row in get_ipr_statements_from_variants(
389
- graphkb_conn, cat_variants, disease_matches
390
- ):
391
- ipr_row["variant"] = category
392
- ipr_row["variantType"] = "tmb"
393
- gkb_matches.append(ipr_row)
394
- return gkb_matches
395
-
396
-
397
312
  def annotate_signature_variants(
398
313
  graphkb_conn: GraphKBConnection,
399
314
  disease_matches: List[str],
@@ -425,6 +340,20 @@ def annotate_signature_variants(
425
340
  variant["variantTypeName"],
426
341
  reference_class="Signature",
427
342
  )
343
+ # KBDEV-1246
344
+ # Keep support for 'high mutation burden' until statement datafix
345
+ if (
346
+ variant["signatureName"] == TMB_SIGNATURE
347
+ and TMB_SIGNATURE != 'high mutation burden'
348
+ ):
349
+ matched_variants.extend(
350
+ gkb_match.match_category_variant(
351
+ graphkb_conn,
352
+ 'high mutation burden',
353
+ variant["variantTypeName"],
354
+ reference_class="Signature",
355
+ )
356
+ )
428
357
  # Matching GKB Variants to GKB Statements
429
358
  for ipr_row in get_ipr_statements_from_variants(
430
359
  graphkb_conn, matched_variants, disease_matches
@@ -444,3 +373,88 @@ def annotate_signature_variants(
444
373
  )
445
374
 
446
375
  return alterations
376
+
377
+
378
+ def annotate_variants(
379
+ graphkb_conn: GraphKBConnection,
380
+ interactive: bool = False,
381
+ disease_matches: List[str] = [],
382
+ signature_variants: List[IprSignatureVariant] = [],
383
+ small_mutations: List[IprSmallMutationVariant] = [],
384
+ structural_variants: List[IprStructuralVariant] = [],
385
+ copy_variants: List[IprCopyVariant] = [],
386
+ expression_variants: List[IprExprVariant] = [],
387
+ ) -> List[Hashabledict]:
388
+ """Annotating (matching to GraphKB) all observed variants, per type
389
+ Args:
390
+ graphkb_conn: the graphkb api connection object
391
+ interactive: progressbars for interactive users
392
+ disease_matches: list of matched disease RID strings,
393
+ signature_variants: signature CategoryVariants (incl. cosmic, dmmr, hla, tmb & msi),
394
+ small_mutations: small PositionalVariants,
395
+ structural_variants: structural PositionalVariants (incl. fusion)
396
+ copy_variants: copy number CategoryVariants (e.g. of type 'copy loss', 'copy gain', etc.),
397
+ expression_variants: expression CategoryVariant (e.g. of type 'overexpression', etc. ),
398
+ Returns:
399
+ A list of matched Statements to GraphKB
400
+ """
401
+ gkb_matches: List[Hashabledict] = []
402
+
403
+ # MATCHING SIGNATURE CATEGORY VARIANTS
404
+ logger.info(f"annotating {len(signature_variants)} signatures")
405
+ gkb_matches.extend(
406
+ annotate_signature_variants(
407
+ graphkb_conn, disease_matches, signature_variants, show_progress=interactive
408
+ )
409
+ )
410
+ logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
411
+
412
+ # MATCHING SMALL MUTATIONS
413
+ logger.info(f"annotating {len(small_mutations)} small mutations")
414
+ gkb_matches.extend(
415
+ annotate_positional_variants(
416
+ graphkb_conn, small_mutations, disease_matches, show_progress=interactive
417
+ )
418
+ )
419
+ logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
420
+
421
+ # MATCHING STRUCTURAL VARIANTS
422
+ logger.info(f"annotating {len(structural_variants)} structural variants")
423
+ gkb_matches.extend(
424
+ annotate_positional_variants(
425
+ graphkb_conn,
426
+ structural_variants,
427
+ disease_matches,
428
+ show_progress=interactive,
429
+ )
430
+ )
431
+ logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
432
+
433
+ # MATCHING COPY VARIANTS
434
+ logger.info(f"annotating {len(copy_variants)} copy variants")
435
+ gkb_matches.extend(
436
+ [
437
+ Hashabledict(copy_var)
438
+ for copy_var in annotate_copy_variants(
439
+ graphkb_conn, disease_matches, copy_variants, show_progress=interactive
440
+ )
441
+ ]
442
+ )
443
+ logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
444
+
445
+ # MATCHING EXPRESSION VARIANTS
446
+ logger.info(f"annotating {len(expression_variants)} expression variants")
447
+ gkb_matches.extend(
448
+ [
449
+ Hashabledict(exp_var)
450
+ for exp_var in annotate_expression_variants(
451
+ graphkb_conn,
452
+ disease_matches,
453
+ expression_variants,
454
+ show_progress=interactive,
455
+ )
456
+ ]
457
+ )
458
+ logger.debug(f"\tgkb_matches: {len(gkb_matches)}")
459
+
460
+ return gkb_matches
@@ -95,13 +95,34 @@ class IprConnection:
95
95
  )
96
96
 
97
97
  def upload_report(
98
- self, content: Dict, mins_to_wait: int = 5, async_upload: bool = False
98
+ self,
99
+ content: Dict,
100
+ mins_to_wait: int = 5,
101
+ async_upload: bool = False,
102
+ ignore_extra_fields: bool = False,
99
103
  ) -> Dict:
100
104
  if async_upload:
101
105
  # if async is used, the response for reports-async contains either 'jobStatus'
102
106
  # or 'report'. jobStatus is no longer available once the report is successfully
103
107
  # uploaded.
104
- initial_result = self.post("reports-async", content)
108
+
109
+ projects = self.get("project")
110
+ project_names = [item['name'] for item in projects]
111
+
112
+ # if project is not exist, create one
113
+ if content['project'] not in project_names:
114
+ logger.info(
115
+ f"Project not found - attempting to create project {content['project']}"
116
+ )
117
+ try:
118
+ self.post("project", {'name': content['project']})
119
+ except Exception as err:
120
+ raise Exception(f"Project creation failed due to {err}")
121
+
122
+ if ignore_extra_fields:
123
+ initial_result = self.post("reports-async?ignore_extra_fields=true", content)
124
+ else:
125
+ initial_result = self.post("reports-async", content)
105
126
 
106
127
  report_id = initial_result["ident"]
107
128
 
@@ -158,7 +179,10 @@ class IprConnection:
158
179
 
159
180
  return current_status
160
181
  else:
161
- return self.post("reports", content)
182
+ if ignore_extra_fields:
183
+ return self.post("reports?ignore_extra_fields=true", content)
184
+ else:
185
+ return self.post("reports", content)
162
186
 
163
187
  def set_analyst_comments(self, report_id: str, data: Dict) -> Dict:
164
188
  """
@@ -212,3 +236,11 @@ class IprConnection:
212
236
  Get the current IPR spec, for the purposes of current report upload fields
213
237
  """
214
238
  return self.request("/spec.json", method="GET")
239
+
240
+ def validate_json(self, content: Dict) -> Dict:
241
+ """
242
+ Validate the provided json schema
243
+ """
244
+ result = self.post("reports/schema", content)
245
+ logger.info(f"{result['message']}")
246
+ return result
@@ -0,0 +1,28 @@
1
+ DEFAULT_URL = "https://iprstaging-api.bcgsc.ca/api"
2
+ GERMLINE_BASE_TERMS = ("pharmacogenomic", "cancer predisposition") # based on graphkb.constants
3
+ VARIANT_CLASSES = {"Variant", "CategoryVariant", "PositionalVariant", "CatalogueVariant"}
4
+
5
+ # all possible values for review status are: ['pending', 'not required', 'passed', 'failed', 'initial']
6
+ FAILED_REVIEW_STATUS = "failed"
7
+
8
+ # Signatures
9
+ COSMIC_SIGNATURE_VARIANT_TYPE = "high signature"
10
+ HLA_SIGNATURE_VARIANT_TYPE = "signature present"
11
+ TMB_SIGNATURE = "mutation burden"
12
+ TMB_SIGNATURE_HIGH_THRESHOLD = (
13
+ 10.0 # genomic mutations per mb - https://www.bcgsc.ca/jira/browse/GERO-296
14
+ )
15
+ TMB_SIGNATURE_VARIANT_TYPE = "high signature"
16
+ # Mapping micro-satellite from pipeline terms to GraphKB terms
17
+ MSI_MAPPING = {
18
+ 'microsatellite instability': { # MSI
19
+ 'displayName': 'microsatellite instability high signature',
20
+ 'signatureName': 'microsatellite instability',
21
+ 'variantTypeName': 'high signature',
22
+ },
23
+ 'microsatellite stable': { # MSS
24
+ 'displayName': 'microsatellite stable signature present',
25
+ 'signatureName': 'microsatellite stable',
26
+ 'variantTypeName': 'signature present',
27
+ },
28
+ }
@@ -7,10 +7,11 @@ from __future__ import annotations
7
7
  import json
8
8
  import jsonschema
9
9
  import os
10
- import re
11
10
  import pandas as pd
11
+ import re
12
12
  from Bio.Data.IUPACData import protein_letters_3to1
13
- from typing import Callable, Dict, Iterable, List, Set, Tuple, cast
13
+ from numpy import nan
14
+ from typing import Any, Callable, Dict, Iterable, List, Set, Tuple, cast
14
15
 
15
16
  from pori_python.graphkb.match import INPUT_COPY_CATEGORIES, INPUT_EXPRESSION_CATEGORIES
16
17
  from pori_python.types import (
@@ -26,6 +27,9 @@ from .constants import (
26
27
  COSMIC_SIGNATURE_VARIANT_TYPE,
27
28
  DEFAULT_URL,
28
29
  HLA_SIGNATURE_VARIANT_TYPE,
30
+ MSI_MAPPING,
31
+ TMB_SIGNATURE,
32
+ TMB_SIGNATURE_VARIANT_TYPE,
29
33
  )
30
34
  from .util import hash_key, logger, pandas_falsy
31
35
 
@@ -442,19 +446,13 @@ def preprocess_cosmic(rows: Iterable[Dict]) -> Iterable[Dict]:
442
446
  Process cosmic inputs into preformatted signature inputs
443
447
  Note: Cosmic and dMMR already evaluated against thresholds in gsc_report
444
448
  """
445
- cosmic = set()
446
- for row in rows:
447
- if not set(SIGV_COSMIC).issubset(row.keys()):
448
- continue
449
- cosmic.add(row[SIGV_COSMIC[0]])
450
-
451
449
  return [
452
450
  {
453
451
  "displayName": f"{signature} {COSMIC_SIGNATURE_VARIANT_TYPE}",
454
452
  "signatureName": signature,
455
453
  "variantTypeName": COSMIC_SIGNATURE_VARIANT_TYPE,
456
454
  }
457
- for signature in cosmic
455
+ for signature in rows
458
456
  ]
459
457
 
460
458
 
@@ -480,6 +478,83 @@ def preprocess_hla(rows: Iterable[Dict]) -> Iterable[Dict]:
480
478
  ]
481
479
 
482
480
 
481
+ def preprocess_tmb(
482
+ tmb_high: float, tmburMutationBurden: Dict = None, genomeTmb: str = None
483
+ ) -> Iterable[Dict]:
484
+ """
485
+ Process tumour mutation burden (tmb) input(s) into preformatted signature input.
486
+ Get compared to threshold; signature CategoryVariant created only if threshold met.
487
+ """
488
+ tmbur_tmb_val = nan
489
+ tmb_val = 0.0
490
+
491
+ # tmburMutationBurden, for backwards compatibility purpose
492
+ # derived from tmburMutationBurden["genomeIndelTmb"] + tmburMutationBurden["genomeSnvTmb"]
493
+ if tmburMutationBurden:
494
+ try:
495
+ tmbur_tmb_val = float(
496
+ tmburMutationBurden["genomeIndelTmb"] + tmburMutationBurden["genomeSnvTmb"]
497
+ )
498
+ if genomeTmb == None:
499
+ logger.error(
500
+ "backwards compatibility: deriving genomeTmb from tmburMutationBurden genomeIndelTmb + genomeSnvTmb"
501
+ )
502
+ tmb_val = tmbur_tmb_val
503
+ except Exception as err:
504
+ logger.error(f"tmburMutationBurden parsing failure: {err}")
505
+
506
+ # genomeTmb
507
+ # SDEV-4811 - mutation burden is now expected to be uploaded in genomeTmb as mutations/megabase
508
+ if genomeTmb != None and genomeTmb != "":
509
+ try:
510
+ tmb_val = float(genomeTmb)
511
+ if tmburMutationBurden and tmbur_tmb_val != tmb_val:
512
+ logger.warning(
513
+ f"genomeTmb given {tmb_val} does not match tmburMutationBurden TMB {tmbur_tmb_val}"
514
+ )
515
+ except Exception as err:
516
+ logger.error(f"genomeTmb parsing failure {genomeTmb}: {err}")
517
+
518
+ # comparaing tmb_val to threshold
519
+ # Signature CategoryVariant created only if threshold met
520
+ if tmb_val >= tmb_high:
521
+ return [
522
+ {
523
+ "displayName": f'{TMB_SIGNATURE} {TMB_SIGNATURE_VARIANT_TYPE}',
524
+ "signatureName": TMB_SIGNATURE,
525
+ "variantTypeName": TMB_SIGNATURE_VARIANT_TYPE,
526
+ }
527
+ ]
528
+ return []
529
+
530
+
531
+ def preprocess_msi(msi: Any) -> Iterable[Dict]:
532
+ """
533
+ Process micro-satellite input into preformatted signature input.
534
+ Both msi & mss gets mapped to corresponding GraphKB Signature CategoryVariants.
535
+ """
536
+ if msi:
537
+
538
+ # MSI category is given from upstream (only one msi variant per library)
539
+ if isinstance(msi, list):
540
+ # msi is given as a list of one dict
541
+ msi_cat = msi[0].get("kbCategory", "")
542
+ elif isinstance(msi, str):
543
+ # msi is given as a string
544
+ msi_cat = msi
545
+ else:
546
+ # msi is given as a dict; uncatched error if not.
547
+ msi_cat = msi.get("kbCategory", "")
548
+
549
+ msi_variant = MSI_MAPPING.get(msi_cat, None)
550
+
551
+ # Signature CategoryVariant created either for msi or mss
552
+ if msi_variant:
553
+ return [msi_variant]
554
+
555
+ return []
556
+
557
+
483
558
  def check_variant_links(
484
559
  small_mutations: List[IprSmallMutationVariant],
485
560
  expression_variants: List[IprExprVariant],