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.
Files changed (49) hide show
  1. pori_python/__init__.py +1 -0
  2. pori_python/graphkb/__init__.py +2 -0
  3. pori_python/graphkb/constants.py +220 -0
  4. pori_python/graphkb/genes.py +540 -0
  5. pori_python/graphkb/match.py +710 -0
  6. pori_python/graphkb/py.typed +0 -0
  7. pori_python/graphkb/statement.py +48 -0
  8. pori_python/graphkb/util.py +523 -0
  9. pori_python/graphkb/vocab.py +198 -0
  10. pori_python/ipr/__init__.py +0 -0
  11. pori_python/ipr/annotate.py +373 -0
  12. pori_python/ipr/connection.py +214 -0
  13. pori_python/ipr/constants.py +9 -0
  14. pori_python/ipr/inputs.py +593 -0
  15. pori_python/ipr/ipr.py +390 -0
  16. pori_python/ipr/main.py +512 -0
  17. pori_python/ipr/py.typed +0 -0
  18. pori_python/ipr/summary.py +412 -0
  19. pori_python/ipr/therapeutic_options.py +83 -0
  20. pori_python/ipr/util.py +173 -0
  21. pori_python/types.py +177 -0
  22. pori_python-0.1.0.dist-info/LICENSE +674 -0
  23. pori_python-0.1.0.dist-info/METADATA +151 -0
  24. pori_python-0.1.0.dist-info/RECORD +49 -0
  25. pori_python-0.1.0.dist-info/WHEEL +5 -0
  26. pori_python-0.1.0.dist-info/dependency_links.txt +1 -0
  27. pori_python-0.1.0.dist-info/entry_points.txt +2 -0
  28. pori_python-0.1.0.dist-info/top_level.txt +2 -0
  29. tests/__init__.py +0 -0
  30. tests/test_graphkb/__init__.py +0 -0
  31. tests/test_graphkb/data.py +78 -0
  32. tests/test_graphkb/test_genes.py +306 -0
  33. tests/test_graphkb/test_graphkb.py +32 -0
  34. tests/test_graphkb/test_match.py +662 -0
  35. tests/test_graphkb/test_statement.py +100 -0
  36. tests/test_graphkb/test_util.py +151 -0
  37. tests/test_graphkb/test_vocab.py +85 -0
  38. tests/test_ipr/__init__.py +0 -0
  39. tests/test_ipr/constants.py +3 -0
  40. tests/test_ipr/test_annotate.py +149 -0
  41. tests/test_ipr/test_connection.py +97 -0
  42. tests/test_ipr/test_inputs.py +295 -0
  43. tests/test_ipr/test_ipr.py +338 -0
  44. tests/test_ipr/test_main.py +154 -0
  45. tests/test_ipr/test_probe.py +72 -0
  46. tests/test_ipr/test_summary.py +157 -0
  47. tests/test_ipr/test_upload.py +274 -0
  48. tests/test_ipr/test_util.py +29 -0
  49. tests/test_ipr/util.py +9 -0
@@ -0,0 +1,412 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import json
5
+ from typing import Callable, Dict, List, Sequence, Set, Tuple, cast
6
+ from urllib.parse import urlencode
7
+
8
+ from pori_python.graphkb import GraphKBConnection
9
+ from pori_python.graphkb.constants import RELEVANCE_BASE_TERMS
10
+ from pori_python.graphkb.statement import categorize_relevance
11
+ from pori_python.graphkb.util import convert_to_rid_list
12
+ from pori_python.graphkb.vocab import get_term_tree
13
+ from pori_python.ipr.inputs import create_graphkb_sv_notation
14
+ from pori_python.types import Hashabledict, IprVariant, KbMatch, Ontology, Record, Statement
15
+
16
+ from .util import (
17
+ convert_to_rid_set,
18
+ generate_ontology_preference_key,
19
+ get_preferred_drug_representation,
20
+ get_preferred_gene_name,
21
+ logger,
22
+ )
23
+
24
+ OTHER_DISEASES = "other disease types"
25
+ ENTREZ_GENE_URL = "https://www.ncbi.nlm.nih.gov/gene"
26
+ # TODO: https://www.bcgsc.ca/jira/browse/DEVSU-1181
27
+ GRAPHKB_GUI = "https://graphkb.bcgsc.ca"
28
+
29
+
30
+ def filter_by_record_class(
31
+ record_list: Sequence[Record], *record_classes, exclude: bool = False
32
+ ) -> List[Record]:
33
+ """Given a list of records, return the subset matching a class or list of classes."""
34
+
35
+ def check(name: str) -> bool:
36
+ if exclude:
37
+ return name not in record_classes
38
+ else:
39
+ return name in record_classes
40
+
41
+ return [rec for rec in record_list if check(rec["@class"])]
42
+
43
+
44
+ def natural_join(word_list: List[str]) -> str:
45
+ if len(word_list) > 1:
46
+ return ", ".join(word_list[:-1]) + ", and " + word_list[-1]
47
+ return "".join(word_list)
48
+
49
+
50
+ def get_displayname(rec: Record) -> str:
51
+ ret_val = rec.get("displayName", rec["@rid"])
52
+ return str(ret_val)
53
+
54
+
55
+ def natural_join_records(
56
+ records: Sequence[Record], covert_to_word: Callable[[Record], str] = get_displayname
57
+ ) -> str:
58
+ word_list = sorted(list({covert_to_word(rec) for rec in records}))
59
+ return natural_join(word_list)
60
+
61
+
62
+ def create_graphkb_link(record_ids: List[str], record_class: str = "Statement") -> str:
63
+ """
64
+ Create a link for a set of statements to the GraphKB client
65
+ """
66
+ record_ids = sorted(list(set(record_ids)))
67
+ if len(record_ids) == 1:
68
+ return f'{GRAPHKB_GUI}/view/{record_class}/{record_ids[0].replace("#", "")}'
69
+ complex_param = base64.b64encode(json.dumps({"target": record_ids}).encode("utf-8"))
70
+ search_params = {"complex": complex_param, "@class": record_class}
71
+ return f"{GRAPHKB_GUI}/data/table?{urlencode(search_params)}"
72
+
73
+
74
+ def merge_diseases(
75
+ diseases: List[Ontology] | List[Record], disease_matches: Set[str] = set()
76
+ ) -> str:
77
+ if len(convert_to_rid_set(diseases) - disease_matches) >= 2 and all(
78
+ [d["@class"] == "Disease" for d in diseases]
79
+ ):
80
+ words = sorted(
81
+ list(set([get_displayname(s) for s in diseases if s["@rid"] in disease_matches]))
82
+ )
83
+ words.append(OTHER_DISEASES)
84
+ return natural_join(words)
85
+ else:
86
+ return natural_join_records(diseases)
87
+
88
+
89
+ def substitute_sentence_template(
90
+ template: str,
91
+ conditions: List[Ontology],
92
+ subjects: List[Ontology],
93
+ relevance: Ontology,
94
+ evidence: List[Ontology],
95
+ statement_rids: List[str] = [],
96
+ disease_matches: Set[str] = set(),
97
+ ) -> str:
98
+ """Create the filled-in sentence template for a given template and list of substitutions
99
+ which may be the result of the aggregation of 1 or more statements.
100
+ """
101
+ disease_conditions = filter_by_record_class(conditions, "Disease")
102
+ variant_conditions = filter_by_record_class(
103
+ conditions, "CategoryVariant", "CatalogueVariant", "PositionalVariant"
104
+ )
105
+ other_conditions = filter_by_record_class(
106
+ conditions,
107
+ "CategoryVariant",
108
+ "CatalogueVariant",
109
+ "PositionalVariant",
110
+ "Disease",
111
+ exclude=True,
112
+ )
113
+ result = template.replace(r"{relevance}", relevance["displayName"])
114
+
115
+ if r"{subject}" in template:
116
+ # remove subject from the conditions replacements
117
+ subjects_ids = convert_to_rid_set(subjects)
118
+ disease_conditions = [
119
+ cast(Ontology, d) for d in disease_conditions if d["@rid"] not in subjects_ids
120
+ ]
121
+ variant_conditions = [
122
+ cast(Ontology, d) for d in variant_conditions if d["@rid"] not in subjects_ids
123
+ ]
124
+ other_conditions = [d for d in other_conditions if d["@rid"] not in subjects_ids]
125
+
126
+ result = result.replace(r"{subject}", merge_diseases(subjects, disease_matches))
127
+
128
+ if r"{conditions:disease}" in template:
129
+ result = result.replace(
130
+ r"{conditions:disease}", merge_diseases(disease_conditions, disease_matches)
131
+ )
132
+ else:
133
+ other_conditions.extend(disease_conditions)
134
+
135
+ if r"{conditions:variant}" in template:
136
+ result = result.replace(r"{conditions:variant}", natural_join_records(variant_conditions))
137
+ else:
138
+ other_conditions.extend(variant_conditions)
139
+
140
+ result = result.replace(r"{conditions}", natural_join_records(other_conditions))
141
+
142
+ link_url = create_graphkb_link(statement_rids) if statement_rids else ""
143
+
144
+ if r"{evidence}" in template:
145
+ evidence_str = ", ".join(sorted(list({e["displayName"] for e in evidence})))
146
+ if link_url:
147
+ evidence_str = f'<a href="{link_url}" target="_blank" rel="noopener">{evidence_str}</a>'
148
+ result = result.replace(r"{evidence}", evidence_str)
149
+
150
+ return result
151
+
152
+
153
+ def aggregate_statements(
154
+ graphkb_conn: GraphKBConnection,
155
+ template: str,
156
+ statements: List[Statement],
157
+ disease_matches: Set[str],
158
+ ) -> Dict[str, str]:
159
+ """
160
+ Group Statements that only differ in disease conditions and evidence
161
+ """
162
+ hash_other: Dict[Tuple, List[Statement]] = {}
163
+
164
+ def generate_key(statement: Statement) -> Tuple:
165
+ result = [
166
+ cond.get("displayName", cond["@rid"])
167
+ for cond in filter_by_record_class(statement["conditions"], "Disease", exclude=True)
168
+ if cond["@rid"] != statement["subject"]["@rid"]
169
+ ]
170
+ if statement.get("subject", {}).get("@class", "Disease") != "Disease":
171
+ subject = statement["subject"]
172
+ if subject["@class"] == "Therapy":
173
+ alt = get_preferred_drug_representation(graphkb_conn, subject["@rid"])
174
+ statement["subject"] = alt
175
+ result.append(statement["subject"]["displayName"])
176
+ result.append(statement["relevance"]["displayName"])
177
+ result.append(statement["displayNameTemplate"])
178
+ return tuple(sorted(set(result)))
179
+
180
+ for statement in statements:
181
+ key = generate_key(statement)
182
+ hash_other.setdefault(key, []).append(statement)
183
+
184
+ result = {}
185
+ for key, group in hash_other.items():
186
+ conditions = []
187
+ subjects = []
188
+ evidence = []
189
+ relevance = group[0]["relevance"]
190
+ template = group[0]["displayNameTemplate"]
191
+ for statement in group:
192
+ conditions.extend(statement["conditions"])
193
+ evidence.extend(statement["evidence"])
194
+ subjects.append(statement["subject"])
195
+
196
+ sentence = substitute_sentence_template(
197
+ template,
198
+ conditions,
199
+ subjects,
200
+ relevance,
201
+ evidence,
202
+ statement_rids=convert_to_rid_list(group),
203
+ disease_matches=disease_matches,
204
+ )
205
+
206
+ for statement in group:
207
+ result[statement["@rid"]] = sentence
208
+ return result
209
+
210
+
211
+ def display_variant(variant: IprVariant) -> str:
212
+ """Short, human readable variant description string."""
213
+ gene = variant.get("gene", "")
214
+ if not gene and "gene1" in variant and "gene2" in variant:
215
+ gene = f'({variant.get("gene1", "")},{variant.get("gene2", "")})'
216
+
217
+ if variant.get("kbCategory"):
218
+ return f'{variant.get("kbCategory")} of {gene}'
219
+
220
+ # Special display of IprFusionVariant with exons
221
+ if variant.get("exon1") or variant.get("exon2"):
222
+ return create_graphkb_sv_notation(variant) # type: ignore
223
+
224
+ # Use chosen legacy 'proteinChange' or an hgvs description of lowest detail.
225
+ hgvs = variant.get(
226
+ "proteinChange",
227
+ variant.get("hgvsProtein", variant.get("hgvsCds", variant.get("hgvsGenomic", ""))),
228
+ )
229
+
230
+ if gene and hgvs:
231
+ return f"{gene}:{hgvs}"
232
+ elif variant.get("variant"):
233
+ return str(variant.get("variant"))
234
+
235
+ raise ValueError(f"Unable to form display_variant of {variant}")
236
+
237
+
238
+ def display_variants(gene_name: str, variants: List[IprVariant]) -> str:
239
+ result = sorted(list({v for v in [display_variant(e) for e in variants] if gene_name in v}))
240
+ variants_text = natural_join(result)
241
+ if len(result) > 1:
242
+ return (
243
+ f"Multiple variants of the gene {gene_name} were observed in this case: {variants_text}"
244
+ )
245
+ elif result:
246
+ return f"{variants_text[0].upper()}{variants_text[1:]} was observed in this case."
247
+ return ""
248
+
249
+
250
+ def create_section_html(
251
+ graphkb_conn: GraphKBConnection,
252
+ gene_name: str,
253
+ sentences_by_statement_id: Dict[str, str],
254
+ statements: Dict[str, Statement],
255
+ exp_variants: List[IprVariant],
256
+ ) -> str:
257
+ """
258
+ Generate HTML for a gene section of the comments
259
+ """
260
+ output = [f"<h2>{gene_name}</h2>"]
261
+
262
+ sentence_categories: Dict[str, str] = {}
263
+
264
+ for statement_id, sentence in sentences_by_statement_id.items():
265
+ relevance = statements[statement_id]["relevance"]["@rid"]
266
+ category = categorize_relevance(
267
+ graphkb_conn, relevance, RELEVANCE_BASE_TERMS + [("resistance", ["no sensitivity"])]
268
+ )
269
+ sentence_categories[sentence] = category
270
+
271
+ # get the entrez gene descriptive hugo name
272
+ genes = graphkb_conn.query(
273
+ {
274
+ "target": "Feature",
275
+ "filters": {
276
+ "AND": [
277
+ {"source": {"target": "Source", "filters": {"name": "entrez gene"}}},
278
+ {"name": gene_name},
279
+ {"biotype": "gene"},
280
+ ]
281
+ },
282
+ }
283
+ )
284
+ genes = sorted(genes, key=generate_ontology_preference_key) # type: ignore
285
+
286
+ variants_text = display_variants(gene_name, exp_variants)
287
+ if not variants_text:
288
+ # exclude sections where they are not linked to an experimental variant. this can occur when there are co-occurent statements collected
289
+ return ""
290
+ if genes and genes[0].get("description", ""):
291
+ description = ". ".join(genes[0]["description"].split(". ")[:2]) # type: ignore
292
+ sourceId = genes[0].get("sourceId", "")
293
+
294
+ output.append(
295
+ f"""
296
+ <blockquote class="entrez_description" cite="{ENTREZ_GENE_URL}/{sourceId}">
297
+ {description}.
298
+ </blockquote>
299
+ <p>
300
+ {variants_text}
301
+ </p>
302
+ """
303
+ )
304
+
305
+ sentences_used: Set[str] = set()
306
+
307
+ for section in [
308
+ {s for (s, v) in sentence_categories.items() if v == "diagnostic"},
309
+ {s for (s, v) in sentence_categories.items() if v == "biological"},
310
+ {s for (s, v) in sentence_categories.items() if v in ["therapeutic", "prognostic"]},
311
+ {
312
+ s
313
+ for (s, v) in sentence_categories.items()
314
+ if v not in ["diagnostic", "biological", "therapeutic", "prognostic", "resistance"]
315
+ },
316
+ {s for (s, v) in sentence_categories.items() if v == "resistance"},
317
+ ]:
318
+ content = ". ".join(sorted(list(section - sentences_used)))
319
+ sentences_used.update(section)
320
+ output.append(f"<p>{content}</p>")
321
+ return "\n".join(output)
322
+
323
+
324
+ def section_statements_by_genes(
325
+ graphkb_conn: GraphKBConnection, statements: Sequence[Statement]
326
+ ) -> Dict[str, Set[str]]:
327
+ """Create Dict of statement @rid sets indexed by preferred gene names in conditions."""
328
+ genes: Dict[str, Set[str]] = {}
329
+
330
+ for statement in statements:
331
+ for condition in statement["conditions"]:
332
+ if condition.get("biotype", "") == "gene":
333
+ gene = get_preferred_gene_name(graphkb_conn, condition["@rid"])
334
+ genes.setdefault(gene, set()).add(statement["@rid"])
335
+ else:
336
+ for cond_ref_key in ("reference1", "reference2"):
337
+ cond_ref_gene = condition.get(cond_ref_key)
338
+ if cond_ref_gene:
339
+ gene = get_preferred_gene_name(graphkb_conn, str(cond_ref_gene))
340
+ genes.setdefault(gene, set()).add(statement["@rid"])
341
+
342
+ return genes
343
+
344
+
345
+ def auto_analyst_comments(
346
+ graphkb_conn: GraphKBConnection,
347
+ matches: Sequence[KbMatch] | Sequence[Hashabledict],
348
+ disease_name: str,
349
+ variants: Sequence[IprVariant],
350
+ ) -> str:
351
+ """Given a list of GraphKB matches, generate a text summary to add to the report."""
352
+ templates: Dict[str, List[Statement]] = {}
353
+ statements: Dict[str, Statement] = {}
354
+ variants_by_keys = {v["key"]: v for v in variants}
355
+ variant_keys_by_statement_ids: Dict[str, Set[str]] = {}
356
+
357
+ for match in matches:
358
+ rid = match["kbStatementId"]
359
+ exp_variant = match["variant"]
360
+ variant_keys_by_statement_ids.setdefault(rid, set()).add(exp_variant)
361
+
362
+ exp_variants_by_statements: Dict[str, List[IprVariant]] = {}
363
+ for rid, keys in variant_keys_by_statement_ids.items():
364
+ try:
365
+ exp_variants_by_statements[rid] = [variants_by_keys[key] for key in keys]
366
+ except KeyError as err:
367
+ logger.warning(f"No specific variant matched for {rid}:{keys} - {err}")
368
+ exp_variants_by_statements[rid] = []
369
+
370
+ disease_matches = convert_to_rid_set(
371
+ get_term_tree(graphkb_conn, disease_name, ontology_class="Disease")
372
+ )
373
+
374
+ # get details for statements
375
+ for match in matches:
376
+ rid = match["kbStatementId"].replace("#", "")
377
+ result = graphkb_conn.request(f"/statements/{rid}?neighbors=1")["result"]
378
+
379
+ templates.setdefault(result["displayNameTemplate"], []).append(result)
380
+ statements[result["@rid"]] = result
381
+
382
+ # aggregate similar sentences
383
+ sentences = {}
384
+ for template, group in templates.items():
385
+ sentences.update(aggregate_statements(graphkb_conn, template, group, disease_matches))
386
+
387
+ # section statements by genes
388
+ statements_by_genes = section_statements_by_genes(graphkb_conn, list(statements.values()))
389
+
390
+ output: List[str] = [
391
+ "<h3>The comments below were automatically generated from matches to GraphKB and have not been manually reviewed</h3>"
392
+ ]
393
+
394
+ for section, statement_rids in sorted(
395
+ statements_by_genes.items(), key=lambda x: len(x[1]), reverse=True
396
+ ):
397
+ exp_variants = {}
398
+ for variant_list in [exp_variants_by_statements[r] for r in statement_rids]:
399
+ for variant in variant_list:
400
+ exp_variants[variant["key"]] = variant
401
+
402
+ output.append(
403
+ create_section_html(
404
+ graphkb_conn,
405
+ section,
406
+ {r: sentences[r] for r in statement_rids},
407
+ {r: statements[r] for r in statement_rids},
408
+ list(exp_variants.values()),
409
+ )
410
+ )
411
+
412
+ return "\n".join(output)
@@ -0,0 +1,83 @@
1
+ """
2
+ upload variant and report information to IPR
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import pandas
8
+ from typing import Any, Dict, List, Sequence
9
+
10
+ from pori_python.graphkb import GraphKBConnection
11
+ from pori_python.types import Hashabledict, IprVariant, KbMatch
12
+
13
+ from .util import (
14
+ create_variant_name_tuple,
15
+ find_variant,
16
+ get_preferred_drug_representation,
17
+ get_terms_set,
18
+ )
19
+
20
+
21
+ def create_therapeutic_options(
22
+ graphkb_conn: GraphKBConnection,
23
+ kb_matches: List[KbMatch] | List[Hashabledict],
24
+ variants: Sequence[IprVariant],
25
+ ) -> List[Dict]:
26
+ """
27
+ Generate therapeutic options summary from the list of kb-matches
28
+ """
29
+ options: List[Dict[str, Any]] = []
30
+ resistance_markers = get_terms_set(graphkb_conn, ["no sensitivity"])
31
+
32
+ for match in kb_matches:
33
+ row_type = "therapeutic"
34
+ if match["category"] != "therapeutic" or match["relevance"] == "eligibility":
35
+ continue
36
+ if match["kbRelevanceId"] in resistance_markers:
37
+ row_type = "chemoresistance"
38
+ variant = find_variant(variants, match["variantType"], match["variant"])
39
+ drug = get_preferred_drug_representation(graphkb_conn, match["kbContextId"])
40
+
41
+ gene, variant_string = create_variant_name_tuple(variant)
42
+
43
+ options.append(
44
+ {
45
+ "gene": gene,
46
+ "type": row_type,
47
+ "therapy": drug["displayName"],
48
+ "therapyGraphkbId": drug["@rid"],
49
+ "context": match["relevance"],
50
+ "contextGraphkbId": match["kbRelevanceId"],
51
+ "variantGraphkbId": match["kbVariantId"],
52
+ "variant": variant_string,
53
+ "evidenceLevel": match["evidenceLevel"],
54
+ "kbStatementIds": match["kbStatementId"],
55
+ "notes": "",
56
+ }
57
+ )
58
+ if not options:
59
+ return options
60
+ options_df = pandas.DataFrame.from_records(options)
61
+
62
+ def delimited_list(inputs: List, delimiter: str = " / ") -> str:
63
+ return delimiter.join(sorted(list({i for i in inputs if i})))
64
+
65
+ options_df = options_df.groupby(["gene", "type", "therapy", "variant"]).agg(
66
+ {
67
+ "evidenceLevel": delimited_list,
68
+ "context": delimited_list,
69
+ "notes": lambda x: delimited_list(x, " "),
70
+ }
71
+ )
72
+ options_df = options_df.reset_index()
73
+ options = options_df.to_dict("records") # type: ignore
74
+ therapeutic_rank = 0
75
+ chemoresistance_rank = 0
76
+ for option in options:
77
+ if option["type"] == "therapeutic":
78
+ option["rank"] = therapeutic_rank
79
+ therapeutic_rank += 1
80
+ else:
81
+ option["rank"] = chemoresistance_rank
82
+ chemoresistance_rank += 1
83
+ return options
@@ -0,0 +1,173 @@
1
+ import hashlib
2
+ import json
3
+ import logging
4
+ import pandas as pd
5
+ from numpy import nan
6
+ from typing import Any, Dict, List, Sequence, Set, Tuple, cast
7
+
8
+ from pori_python.graphkb import GraphKBConnection
9
+ from pori_python.graphkb.vocab import get_term_tree
10
+ from pori_python.types import IprVariant, Ontology, Record
11
+
12
+ GENE_NEIGHBORS_MAX = 3
13
+
14
+ # name the logger after the package to make it simple to disable for packages using this one as a dependency
15
+ logger = logging.getLogger("ipr")
16
+ LOG_LEVELS = {
17
+ "info": logging.INFO,
18
+ "debug": logging.DEBUG,
19
+ "warn": logging.WARN,
20
+ "error": logging.ERROR,
21
+ }
22
+
23
+
24
+ def get_terms_set(graphkb_conn: GraphKBConnection, base_terms: List[str]) -> Set[str]:
25
+ terms = set()
26
+ for base_term in base_terms:
27
+ terms.update(
28
+ convert_to_rid_set(get_term_tree(graphkb_conn, base_term, include_superclasses=False))
29
+ )
30
+ return terms
31
+
32
+
33
+ def hash_key(key: Tuple[str]) -> str:
34
+ body = json.dumps({"key": key}, sort_keys=True)
35
+ hash_code = hashlib.md5(body.encode("utf-8")).hexdigest()
36
+ return hash_code
37
+
38
+
39
+ def convert_to_rid_set(records: Sequence[Record]) -> Set[str]:
40
+ return {r["@rid"] for r in records}
41
+
42
+
43
+ def trim_empty_values(obj: IprVariant, empty_values: Sequence = ("", None, nan)):
44
+ blacklist = ("gene1", "gene2") # allow null for sv genes
45
+ keys = list(obj.keys())
46
+
47
+ for key in keys:
48
+ if obj[key] in empty_values and key not in blacklist: # type: ignore
49
+ del obj[key] # type: ignore
50
+ return obj
51
+
52
+
53
+ def create_variant_name_tuple(variant: IprVariant) -> Tuple[str, str]:
54
+ """
55
+ Given an IPR variant row, create the variant representation to be used as the name
56
+ of the variant
57
+ """
58
+ variant_type = variant["variantType"]
59
+ gene = str(variant.get("gene", variant.get("gene1", "")))
60
+ if variant_type == "exp":
61
+ return (gene, str(variant.get("expressionState", "")))
62
+ elif variant_type == "cnv":
63
+ return (gene, str(variant.get("cnvState", "")))
64
+ variant_split = (
65
+ variant["variant"].split(":", 1)[1] if ":" in variant["variant"] else variant["variant"]
66
+ )
67
+
68
+ gene2 = str(variant.get("gene2", ""))
69
+ if gene and gene2:
70
+ gene = f"{gene}, {gene2}"
71
+ elif gene2:
72
+ gene = gene2
73
+
74
+ return (gene, variant_split)
75
+
76
+
77
+ def find_variant(
78
+ all_variants: Sequence[IprVariant], variant_type: str, variant_key: str
79
+ ) -> IprVariant:
80
+ """
81
+ Find a variant in a list of variants by its key and type
82
+ """
83
+ for variant in all_variants:
84
+ if variant["key"] == variant_key and variant["variantType"] == variant_type:
85
+ return variant
86
+ raise KeyError(f"expected variant ({variant_key}, {variant_type}) does not exist")
87
+
88
+
89
+ def generate_ontology_preference_key(record: Ontology, sources_sort: Dict[str, int] = {}) -> Tuple:
90
+ """Generate a tuple key for comparing preferred ontology terms."""
91
+ return (
92
+ record.get("name") == record.get("sourceId"),
93
+ record.get("deprecated", False),
94
+ record.get("alias", False),
95
+ bool(record.get("dependency", "")),
96
+ sources_sort.get(str(record.get("source")), 99999),
97
+ record["sourceId"],
98
+ record.get("sourceIdVersion", ""),
99
+ record["name"],
100
+ )
101
+
102
+
103
+ def get_alternatives(graphkb_conn: GraphKBConnection, record_id: str) -> List[Ontology]:
104
+ rec_list = graphkb_conn.query(
105
+ {"target": [record_id], "queryType": "similarTo", "treeEdges": []}
106
+ )
107
+ return [cast(Ontology, rec) for rec in rec_list]
108
+
109
+
110
+ def get_preferred_drug_representation(
111
+ graphkb_conn: GraphKBConnection, drug_record_id: str
112
+ ) -> Ontology:
113
+ """Given a Drug record, follow its linked records to find the preferred
114
+ representation by following alias, deprecating, and cross reference links.
115
+ """
116
+
117
+ source_preference = {
118
+ r["@rid"]: r["sort"] # type: ignore
119
+ for r in graphkb_conn.query({"target": "Source", "returnProperties": ["sort", "@rid"]})
120
+ }
121
+ drugs = sorted(
122
+ get_alternatives(graphkb_conn, drug_record_id),
123
+ key=lambda rec: generate_ontology_preference_key(rec, source_preference),
124
+ )
125
+ return cast(Ontology, drugs[0])
126
+
127
+
128
+ def get_preferred_gene_name(
129
+ graphkb_conn: GraphKBConnection, record_id: str, neighbors: int = GENE_NEIGHBORS_MAX
130
+ ) -> str:
131
+ """Given some Feature record ID return the preferred gene name."""
132
+ record = graphkb_conn.get_record_by_id(record_id)
133
+ biotype = record.get("biotype", "")
134
+ genes = []
135
+ expanded_gene_names = graphkb_conn.query({"target": [record_id], "neighbors": neighbors})
136
+ assert len(expanded_gene_names) == 1, "get_preferred_gene_name should have single result"
137
+ expanded: Dict[str, List] = expanded_gene_names[0] # type: ignore
138
+ if biotype != "gene":
139
+ for edge in expanded.get("out_ElementOf", []):
140
+ target = edge["in"]
141
+ if target.get("biotype") == "gene":
142
+ genes.append(target)
143
+
144
+ for edge_type in [
145
+ "out_AliasOf",
146
+ "in_AliasOf",
147
+ "in_DeprecatedBy",
148
+ "out_CrossReferenceOf",
149
+ "in_CrossReferenceOf",
150
+ ]:
151
+ target_name = "out" if edge_type.startswith("in") else "in"
152
+ for edge in expanded.get(edge_type, []):
153
+ target = edge[target_name]
154
+ if target.get("biotype") == "gene":
155
+ genes.append(target)
156
+ genes = sorted(
157
+ genes,
158
+ key=lambda gene: (
159
+ gene["deprecated"],
160
+ bool(gene["dependency"]),
161
+ "_" in gene["name"],
162
+ gene["name"].startswith("ens"),
163
+ ),
164
+ )
165
+ if genes:
166
+ return genes[0]["displayName"]
167
+ # fallback to the input displayName
168
+ return str(record.get("displayName", ""))
169
+
170
+
171
+ def pandas_falsy(field: Any) -> bool:
172
+ """Check if a field is python falsy or pandas null."""
173
+ return bool(pd.isnull(field) or not field)