pori-python 0.1.2__tar.gz → 1.1.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 (59) hide show
  1. {pori_python-0.1.2/pori_python.egg-info → pori_python-1.1.0}/PKG-INFO +11 -6
  2. {pori_python-0.1.2 → pori_python-1.1.0}/README.md +8 -4
  3. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/genes.py +3 -0
  4. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/match.py +95 -21
  5. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/util.py +7 -1
  6. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/annotate.py +160 -73
  7. pori_python-1.1.0/pori_python/ipr/constants.py +28 -0
  8. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/content.spec.json +12 -3
  9. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/inputs.py +172 -3
  10. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/ipr.py +279 -13
  11. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/main.py +165 -139
  12. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/summary.py +133 -10
  13. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/types.py +52 -1
  14. {pori_python-0.1.2 → pori_python-1.1.0/pori_python.egg-info}/PKG-INFO +11 -6
  15. {pori_python-0.1.2 → pori_python-1.1.0}/setup.cfg +1 -1
  16. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/data.py +4 -4
  17. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/test_match.py +27 -12
  18. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_annotate.py +103 -7
  19. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_inputs.py +168 -0
  20. pori_python-1.1.0/tests/test_ipr/test_ipr.py +734 -0
  21. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_main.py +16 -2
  22. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_probe.py +34 -24
  23. pori_python-1.1.0/tests/test_ipr/test_summary.py +355 -0
  24. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_upload.py +114 -32
  25. pori_python-0.1.2/pori_python/ipr/constants.py +0 -9
  26. pori_python-0.1.2/tests/test_ipr/test_ipr.py +0 -338
  27. pori_python-0.1.2/tests/test_ipr/test_summary.py +0 -157
  28. {pori_python-0.1.2 → pori_python-1.1.0}/LICENSE +0 -0
  29. {pori_python-0.1.2 → pori_python-1.1.0}/MANIFEST.in +0 -0
  30. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/__init__.py +0 -0
  31. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/__init__.py +0 -0
  32. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/constants.py +0 -0
  33. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/py.typed +0 -0
  34. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/statement.py +0 -0
  35. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/graphkb/vocab.py +0 -0
  36. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/__init__.py +0 -0
  37. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/connection.py +0 -0
  38. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/py.typed +0 -0
  39. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/therapeutic_options.py +0 -0
  40. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python/ipr/util.py +0 -0
  41. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python.egg-info/SOURCES.txt +0 -0
  42. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python.egg-info/dependency_links.txt +0 -0
  43. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python.egg-info/entry_points.txt +0 -0
  44. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python.egg-info/requires.txt +0 -0
  45. {pori_python-0.1.2 → pori_python-1.1.0}/pori_python.egg-info/top_level.txt +0 -0
  46. {pori_python-0.1.2 → pori_python-1.1.0}/pyproject.toml +0 -0
  47. {pori_python-0.1.2 → pori_python-1.1.0}/setup.py +0 -0
  48. {pori_python-0.1.2 → pori_python-1.1.0}/tests/__init__.py +0 -0
  49. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/__init__.py +0 -0
  50. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/test_genes.py +0 -0
  51. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/test_graphkb.py +0 -0
  52. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/test_statement.py +0 -0
  53. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/test_util.py +0 -0
  54. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_graphkb/test_vocab.py +0 -0
  55. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/__init__.py +0 -0
  56. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/constants.py +0 -0
  57. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_connection.py +0 -0
  58. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/test_util.py +0 -0
  59. {pori_python-0.1.2 → pori_python-1.1.0}/tests/test_ipr/util.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: pori_python
3
- Version: 0.1.2
3
+ Version: 1.1.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
@@ -42,6 +42,7 @@ Requires-Dist: black; extra == "dev"
42
42
  Requires-Dist: flake8-annotations; extra == "dev"
43
43
  Requires-Dist: isort; extra == "dev"
44
44
  Requires-Dist: mypy; extra == "dev"
45
+ Dynamic: license-file
45
46
 
46
47
 
47
48
  # PORI Python Adaptor
@@ -96,7 +97,7 @@ Note that IPR tests will try to use the BCGSC production GraphKB API by default.
96
97
  If you want to test interaction with a different instance, you will need to
97
98
  set the GraphKB variables.
98
99
 
99
- Set EXCLUDE vars to 1 if you don't want to run these tests.
100
+ Set EXCLUDE vars to 1 if you don't want to run these tests.
100
101
  ONCOKB and BCGSC tests are enabled by default.
101
102
 
102
103
  ```bash
@@ -106,17 +107,21 @@ export IPR_URL='http://localhost:8081/api'
106
107
  export GRAPHKB_USER='pori_admin'
107
108
  export GRAPHKB_PASS='pori_admin'
108
109
  export GRAPHKB_URL='http://localhost:8080/api'
109
- EXCLUDE_BCGSC_TESTS = 1
110
- EXCLUDE_ONCOKB_TESTS = 1
110
+ export EXCLUDE_BCGSC_TESTS=1
111
+ export EXCLUDE_ONCOKB_TESTS=1
111
112
  ```
112
113
 
113
114
  If you want to run tests that upload reports to a live IPR instance,
114
115
  specify the url of the IPR API you want to use and set the test var to 1.
115
116
  These tests are disabled by default.
116
117
 
118
+ The created reports are deleted by default. If you want to keep them,
119
+ set DELETE_UPLOAD_TEST_REPORTS to 0 in the env.
120
+
117
121
  ```bash
118
122
  export IPR_TEST_URL='http://localhost:8081/api'
119
- INCLUDE_UPLOAD_TESTS = 1
123
+ export INCLUDE_UPLOAD_TESTS=1
124
+ export DELETE_UPLOAD_TEST_REPORTS=0
120
125
  ```
121
126
 
122
127
  ```bash
@@ -51,7 +51,7 @@ Note that IPR tests will try to use the BCGSC production GraphKB API by default.
51
51
  If you want to test interaction with a different instance, you will need to
52
52
  set the GraphKB variables.
53
53
 
54
- Set EXCLUDE vars to 1 if you don't want to run these tests.
54
+ Set EXCLUDE vars to 1 if you don't want to run these tests.
55
55
  ONCOKB and BCGSC tests are enabled by default.
56
56
 
57
57
  ```bash
@@ -61,17 +61,21 @@ export IPR_URL='http://localhost:8081/api'
61
61
  export GRAPHKB_USER='pori_admin'
62
62
  export GRAPHKB_PASS='pori_admin'
63
63
  export GRAPHKB_URL='http://localhost:8080/api'
64
- EXCLUDE_BCGSC_TESTS = 1
65
- EXCLUDE_ONCOKB_TESTS = 1
64
+ export EXCLUDE_BCGSC_TESTS=1
65
+ export EXCLUDE_ONCOKB_TESTS=1
66
66
  ```
67
67
 
68
68
  If you want to run tests that upload reports to a live IPR instance,
69
69
  specify the url of the IPR API you want to use and set the test var to 1.
70
70
  These tests are disabled by default.
71
71
 
72
+ The created reports are deleted by default. If you want to keep them,
73
+ set DELETE_UPLOAD_TEST_REPORTS to 0 in the env.
74
+
72
75
  ```bash
73
76
  export IPR_TEST_URL='http://localhost:8081/api'
74
- INCLUDE_UPLOAD_TESTS = 1
77
+ export INCLUDE_UPLOAD_TESTS=1
78
+ export DELETE_UPLOAD_TEST_REPORTS=0
75
79
  ```
76
80
 
77
81
  ```bash
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  from typing import Any, Dict, List, Sequence, Set, Tuple, cast
6
+ from typing_extensions import deprecated
6
7
 
7
8
  from pori_python.types import IprGene, Ontology, Record, Statement, Variant
8
9
 
@@ -256,6 +257,7 @@ def get_preferred_gene_name(
256
257
  return gene_names[0]
257
258
 
258
259
 
260
+ @deprecated("Use get_gene_linked_cancer_predisposition_info instead")
259
261
  def get_cancer_predisposition_info(
260
262
  conn: GraphKBConnection, source: str = PREFERRED_GENE_SOURCE_NAME
261
263
  ) -> Tuple[List[str], Dict[str, str]]:
@@ -360,6 +362,7 @@ def get_gene_linked_cancer_predisposition_info(
360
362
  return sorted(genes), variants
361
363
 
362
364
 
365
+ @deprecated("Use get_gene_linked_pharmacogenomic_info instead")
363
366
  def get_pharmacogenomic_info(
364
367
  conn: GraphKBConnection, source: str = PREFERRED_GENE_SOURCE_NAME
365
368
  ) -> Tuple[List[str], Dict[str, str]]:
@@ -31,7 +31,12 @@ from .util import (
31
31
  looks_like_rid,
32
32
  stringifyVariant,
33
33
  )
34
- from .vocab import get_equivalent_terms, get_term_tree, get_terms_set
34
+ from .vocab import (
35
+ get_equivalent_terms,
36
+ get_term_by_name,
37
+ get_term_tree,
38
+ get_terms_set,
39
+ )
35
40
 
36
41
  FEATURES_CACHE: Set[str] = set()
37
42
 
@@ -124,50 +129,62 @@ def cache_missing_features(conn: GraphKBConnection) -> None:
124
129
 
125
130
  def match_category_variant(
126
131
  conn: GraphKBConnection,
127
- gene_name: str,
132
+ reference_name: str,
128
133
  category: str,
129
134
  root_exclude_term: str = "",
130
135
  gene_source: str = "",
131
136
  gene_is_source_id: bool = False,
132
137
  ignore_cache: bool = False,
138
+ reference_class: str = 'Feature',
133
139
  ) -> List[Variant]:
134
140
  """
135
141
  Returns a list of variants matching the input variant
136
142
 
137
143
  Args:
138
144
  conn (GraphKBConnection): the graphkb connection object
139
- gene_name (str): the name of the gene the variant is in reference to
145
+ reference_name (str): the name of the Feature(gene)/Signature the variant is in reference to
140
146
  category (str): the variant category (ex. copy loss)
141
147
  gene_source: The source database the gene is defined by (ex. ensembl)
142
148
  gene_is_source_id: Indicates the gene name(s) input should be treated as sourceIds not names
149
+ reference_class (str): Class name of the variant reference. Default to 'Feature'
143
150
  Raises:
144
151
  FeatureNotFoundError: The gene could not be found in GraphKB
145
152
 
146
153
  Returns:
147
154
  Array.<dict>: List of variant records from GraphKB which match the input
148
155
  """
149
- # disambiguate the gene to find all equivalent representations
150
- features = convert_to_rid_list(
151
- get_equivalent_features(
152
- conn,
153
- gene_name,
154
- source=gene_source,
155
- is_source_id=gene_is_source_id,
156
- ignore_cache=ignore_cache,
156
+ # disambiguate the reference to find all equivalent representations
157
+ references: List[str] = []
158
+ if reference_class == 'Feature':
159
+ references = convert_to_rid_list(
160
+ get_equivalent_features(
161
+ conn,
162
+ reference_name,
163
+ source=gene_source,
164
+ is_source_id=gene_is_source_id,
165
+ ignore_cache=ignore_cache,
166
+ )
157
167
  )
158
- )
159
-
160
- if not features:
161
- raise FeatureNotFoundError(
162
- f"unable to find the gene ({gene_name}) or any equivalent representations"
168
+ if not references:
169
+ raise FeatureNotFoundError(
170
+ f"unable to find the gene ({reference_name}) or any equivalent representations"
171
+ )
172
+ if reference_class == 'Signature':
173
+ references = convert_to_rid_list(
174
+ get_equivalent_terms(
175
+ conn,
176
+ reference_name.lower(),
177
+ ontology_class='Signature',
178
+ ignore_cache=ignore_cache,
179
+ )
163
180
  )
164
181
 
165
182
  # get the list of terms that we should match
166
- terms = convert_to_rid_list(
183
+ types = convert_to_rid_list(
167
184
  get_term_tree(conn, category, root_exclude_term, ignore_cache=ignore_cache)
168
185
  )
169
186
 
170
- if not terms:
187
+ if not types:
171
188
  raise ValueError(f"unable to find the term/category ({category}) or any equivalent")
172
189
 
173
190
  # find the variant list
@@ -178,8 +195,8 @@ def match_category_variant(
178
195
  "target": {
179
196
  "target": "CategoryVariant",
180
197
  "filters": [
181
- {"reference1": features, "operator": "IN"},
182
- {"type": terms, "operator": "IN"},
198
+ {"reference1": references, "operator": "IN"},
199
+ {"type": types, "operator": "IN"},
183
200
  ],
184
201
  },
185
202
  "queryType": "similarTo",
@@ -275,7 +292,55 @@ def positions_overlap(
275
292
  return start is None or pos == start
276
293
 
277
294
 
295
+ def equivalent_types(
296
+ conn: GraphKBConnection,
297
+ type1: str,
298
+ type2: str,
299
+ strict: bool = False,
300
+ ) -> bool:
301
+ """
302
+ Compare 2 variant types to determine if they should match
303
+
304
+ Args:
305
+ conn: the graphkb connection object
306
+ type1: type from the observed variant we want to match to the DB
307
+ type2: type from the DB variant
308
+ strict: wether or not only the specific-to-generic ones are considered.
309
+ By default (false), not only specific types can match more generic ones,
310
+ but generic types can also match more specific ones.
311
+
312
+ Returns:
313
+ bool: True if the types can be matched
314
+ """
315
+
316
+ # Convert rid to displayName if needed
317
+ if looks_like_rid(type1):
318
+ type1 = conn.get_records_by_id([type1])[0]['displayName']
319
+ if looks_like_rid(type2):
320
+ type2 = conn.get_records_by_id([type2])[0]['displayName']
321
+
322
+ # Get type terms from observed variant
323
+ terms1 = []
324
+ if strict:
325
+ try:
326
+ terms1.append(get_term_by_name(conn, type1)['@rid'])
327
+ except:
328
+ pass
329
+ else:
330
+ terms1 = get_terms_set(conn, [type1])
331
+
332
+ # Get type terms from DB variant
333
+ terms2 = get_terms_set(conn, [type2])
334
+
335
+ # Check for intersect
336
+ if len(terms2.intersection(terms1)) == 0:
337
+ return False
338
+
339
+ return True
340
+
341
+
278
342
  def compare_positional_variants(
343
+ conn: GraphKBConnection,
279
344
  variant: Union[PositionalVariant, ParsedVariant],
280
345
  reference_variant: Union[PositionalVariant, ParsedVariant],
281
346
  generic: bool = True,
@@ -378,6 +443,11 @@ def compare_positional_variants(
378
443
  elif len(variant["refSeq"]) != len(reference_variant["refSeq"]): # type: ignore
379
444
  return False
380
445
 
446
+ # Equivalent types
447
+ if variant.get('type') and reference_variant.get('type'):
448
+ if not equivalent_types(conn, variant["type"], reference_variant["type"]):
449
+ return False
450
+
381
451
  return True
382
452
 
383
453
 
@@ -598,10 +668,14 @@ def match_positional_variant(
598
668
  ):
599
669
  # TODO: Check if variant and reference_variant should be interchanged
600
670
  if compare_positional_variants(
601
- variant=parsed, reference_variant=cast(PositionalVariant, row), generic=True
671
+ conn,
672
+ variant=parsed,
673
+ reference_variant=cast(PositionalVariant, row),
674
+ generic=True,
602
675
  ):
603
676
  filtered_similarAndGeneric.append(row)
604
677
  if compare_positional_variants(
678
+ conn,
605
679
  variant=parsed,
606
680
  reference_variant=cast(PositionalVariant, row),
607
681
  generic=False, # Similar variants only
@@ -163,11 +163,16 @@ class GraphKBConnection:
163
163
  # about catching OSError as well as ConnectionError:
164
164
  # https://stackoverflow.com/questions/74253820
165
165
  attempts = range(15)
166
+ need_refresh_login = False
166
167
  for attempt in attempts:
167
168
  if attempt > 0:
168
169
  time.sleep(2) # wait between retries
169
170
  try:
170
- self.refresh_login()
171
+
172
+ if need_refresh_login:
173
+ self.refresh_login()
174
+ need_refresh_login = False
175
+
171
176
  self.request_count += 1
172
177
  resp = requests.request(
173
178
  method, url, headers=self.headers, timeout=timeout, **kwargs
@@ -175,6 +180,7 @@ class GraphKBConnection:
175
180
  if resp.status_code == 401 or resp.status_code == 403:
176
181
  logger.debug(f"/{endpoint} - {resp.status_code} - retrying")
177
182
  # try to re-login if the token expired
183
+ need_refresh_login = True
178
184
  continue
179
185
  else:
180
186
  break
@@ -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
@@ -19,13 +19,15 @@ from pori_python.types import (
19
19
  Hashabledict,
20
20
  IprCopyVariant,
21
21
  IprExprVariant,
22
+ IprSignatureVariant,
23
+ IprSmallMutationVariant,
22
24
  IprStructuralVariant,
23
25
  KbMatch,
24
26
  Statement,
25
27
  Variant,
26
28
  )
27
29
 
28
- from .constants import TMB_HIGH_CATEGORY
30
+ from .constants import TMB_SIGNATURE
29
31
  from .ipr import convert_statements_to_alterations
30
32
  from .util import convert_to_rid_set, logger
31
33
 
@@ -56,7 +58,7 @@ def get_second_pass_variants(
56
58
 
57
59
 
58
60
  def get_ipr_statements_from_variants(
59
- graphkb_conn: GraphKBConnection, matches: List[Variant], disease_name: str
61
+ graphkb_conn: GraphKBConnection, matches: List[Variant], disease_matches: List[str]
60
62
  ) -> List[KbMatch]:
61
63
  """IPR upload formatted GraphKB statements from the list of variants.
62
64
 
@@ -66,11 +68,12 @@ def get_ipr_statements_from_variants(
66
68
  if not matches:
67
69
  return []
68
70
  rows = []
71
+
69
72
  statements = get_statements_from_variants(graphkb_conn, matches)
70
73
  existing_statements = {s["@rid"] for s in statements}
71
74
 
72
75
  for ipr_row in convert_statements_to_alterations(
73
- graphkb_conn, statements, disease_name, convert_to_rid_set(matches)
76
+ graphkb_conn, statements, disease_matches, convert_to_rid_set(matches)
74
77
  ):
75
78
  rows.append(ipr_row)
76
79
 
@@ -84,7 +87,10 @@ def get_ipr_statements_from_variants(
84
87
  ]
85
88
 
86
89
  for ipr_row in convert_statements_to_alterations(
87
- graphkb_conn, inferred_statements, disease_name, convert_to_rid_set(inferred_matches)
90
+ graphkb_conn,
91
+ inferred_statements,
92
+ disease_matches,
93
+ convert_to_rid_set(inferred_matches),
88
94
  ):
89
95
  ipr_row["kbData"]["inferred"] = True
90
96
  rows.append(ipr_row)
@@ -94,15 +100,17 @@ def get_ipr_statements_from_variants(
94
100
 
95
101
  def annotate_expression_variants(
96
102
  graphkb_conn: GraphKBConnection,
103
+ disease_matches: List[str],
97
104
  variants: List[IprExprVariant],
98
- disease_name: str,
99
105
  show_progress: bool = False,
100
106
  ) -> List[KbMatch]:
101
107
  """Annotate expression variants with GraphKB in the IPR alterations format.
102
108
 
103
109
  Args:
104
- graphkb_conn: the graphkb api connection object
105
- variants: list of variants
110
+ graphkb_conn (GraphKBConnection): the graphkb api connection object
111
+ disease_matches (list.str): GraphKB disease RIDs
112
+ variants (list.IprExprVariant): list of variants.
113
+ show_progress (bool): Progressbar displayed for long runs.
106
114
 
107
115
  Returns:
108
116
  list of kbMatches records for IPR
@@ -122,7 +130,7 @@ def annotate_expression_variants(
122
130
  continue
123
131
  try:
124
132
  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):
133
+ for ipr_row in get_ipr_statements_from_variants(graphkb_conn, matches, disease_matches):
126
134
  ipr_row["variant"] = row["key"]
127
135
  ipr_row["variantType"] = row.get("variantType", "exp")
128
136
  alterations.append(ipr_row)
@@ -145,15 +153,17 @@ def annotate_expression_variants(
145
153
 
146
154
  def annotate_copy_variants(
147
155
  graphkb_conn: GraphKBConnection,
156
+ disease_matches: List[str],
148
157
  variants: List[IprCopyVariant],
149
- disease_name: str,
150
158
  show_progress: bool = False,
151
159
  ) -> List[KbMatch]:
152
160
  """Annotate allowed copy variants with GraphKB in the IPR alterations format.
153
161
 
154
162
  Args:
155
- graphkb_conn: the graphkb api connection object
156
- variants: list of variants
163
+ graphkb_conn (GraphKBConnection): the graphkb api connection object
164
+ disease_matches (list.str): GraphKB disease RIDs
165
+ variants (list.IprCopyVariant): list of variants.
166
+ show_progress (bool): Progressbar displayed for long runs.
157
167
 
158
168
  Returns:
159
169
  list of kbMatches records for IPR
@@ -175,7 +185,7 @@ def annotate_copy_variants(
175
185
  continue
176
186
  try:
177
187
  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):
188
+ for ipr_row in get_ipr_statements_from_variants(graphkb_conn, matches, disease_matches):
179
189
  ipr_row["variant"] = row["key"]
180
190
  ipr_row["variantType"] = row.get("variantType", "cnv")
181
191
  alterations.append(ipr_row)
@@ -201,7 +211,7 @@ def annotate_copy_variants(
201
211
  def annotate_positional_variants(
202
212
  graphkb_conn: GraphKBConnection,
203
213
  variants: Sequence[IprStructuralVariant] | Sequence[Hashabledict],
204
- disease_name: str,
214
+ disease_matches: List[str],
205
215
  show_progress: bool = False,
206
216
  ) -> List[Hashabledict]:
207
217
  """Annotate SNP, INDEL or fusion variant calls with GraphKB and return in IPR match format.
@@ -209,8 +219,8 @@ def annotate_positional_variants(
209
219
  Hashable type is required to turn lists into sets.
210
220
  Args:
211
221
  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
222
+ variants (Sequence): list of variants.
223
+ disease_matches (list.str): GraphKB disease RIDs
214
224
  show_progress (bool): Progressbar displayed for long runs.
215
225
 
216
226
  Returns:
@@ -254,7 +264,9 @@ def annotate_positional_variants(
254
264
  raise parse_err
255
265
 
256
266
  for ipr_row in get_ipr_statements_from_variants(
257
- graphkb_conn, matches, disease_name
267
+ graphkb_conn,
268
+ matches,
269
+ disease_matches,
258
270
  ):
259
271
  ipr_row["variant"] = row["key"]
260
272
  ipr_row["variantType"] = row.get(
@@ -297,77 +309,152 @@ def annotate_positional_variants(
297
309
  return alterations
298
310
 
299
311
 
300
- def annotate_msi(
312
+ def annotate_signature_variants(
301
313
  graphkb_conn: GraphKBConnection,
302
- disease_name: str = "cancer",
303
- msi_category: str = "microsatellite instability",
314
+ disease_matches: List[str],
315
+ variants: List[IprSignatureVariant] = [],
316
+ show_progress: bool = False,
304
317
  ) -> List[KbMatch]:
305
- """Annotate microsatellite instablity from GraphKB in the IPR alterations format.
318
+ """Annotate Signature variants with GraphKB in the IPR alterations format.
319
+
320
+ Match to corresponding GraphKB Variants, then to linked GraphKB Statements
306
321
 
307
- Match to GraphKb Category variants with similar names
308
322
  Args:
309
- graphkb_conn: the graphkb api connection object
310
- msi_category: such as 'microsatellite instability'
323
+ graphkb_conn (GraphKBConnection): the graphkb api connection object
324
+ disease_matches (list.str): GraphKB disease RIDs
325
+ variants (list.IprSignatureVariant): list of signature variants
326
+ show_progress (bool): progressbar displayed for long runs; default to False
311
327
 
312
328
  Returns:
313
329
  list of kbMatches records for IPR
314
330
  """
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
- }
331
+ alterations: List[Hashabledict] = []
332
+
333
+ iterfunc = tqdm if show_progress else iter
334
+ for variant in iterfunc(variants):
335
+ try:
336
+ # Matching signature variant to GKB Variants
337
+ matched_variants: List[Variant] = gkb_match.match_category_variant(
338
+ graphkb_conn,
339
+ variant["signatureName"],
340
+ variant["variantTypeName"],
341
+ reference_class="Signature",
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
+ )
357
+ # Matching GKB Variants to GKB Statements
358
+ for ipr_row in get_ipr_statements_from_variants(
359
+ graphkb_conn, matched_variants, disease_matches
360
+ ):
361
+ ipr_row["variant"] = variant["key"]
362
+ ipr_row["variantType"] = "sigv"
363
+ alterations.append(Hashabledict(ipr_row))
364
+
365
+ except ValueError as err:
366
+ logger.error(f"failed to match signature category variant '{variant}': {err}")
367
+
368
+ # drop duplicates
369
+ alterations = list(set(alterations))
370
+
371
+ logger.info(
372
+ f"matched {len(variants)} signature category variants to {len(alterations)} graphkb annotations"
327
373
  )
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
374
 
375
+ return alterations
336
376
 
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
377
 
342
- Match to GraphKb Category variants with similar names
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
343
389
  Args:
344
390
  graphkb_conn: the graphkb api connection object
345
- disease_name: oncotree disease name for graphkb matching.
346
- category: such as 'high mutation burden'
347
-
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. ),
348
398
  Returns:
349
- list of kbMatches records for IPR
399
+ A list of matched Statements to GraphKB
350
400
  """
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
- }
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
+ )
366
418
  )
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)
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
+
373
460
  return gkb_matches