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,154 @@
1
+ import json
2
+ import os
3
+ import pandas as pd
4
+ import pytest
5
+ import sys
6
+ from typing import Dict
7
+ from unittest.mock import MagicMock, patch
8
+
9
+ from pori_python.ipr.connection import IprConnection
10
+ from pori_python.ipr.main import command_interface
11
+ from pori_python.types import IprGene
12
+
13
+ from .constants import EXCLUDE_INTEGRATION_TESTS
14
+
15
+ EXCLUDE_BCGSC_TESTS = os.environ.get("EXCLUDE_BCGSC_TESTS") == "1"
16
+ EXCLUDE_ONCOKB_TESTS = os.environ.get("EXCLUDE_ONCOKB_TESTS") == "1"
17
+
18
+
19
+ def get_test_spec():
20
+ ipr_spec = {"components": {"schemas": {"genesCreate": {"properties": {}}}}}
21
+ ipr_gene_keys = IprGene.__required_keys__ | IprGene.__optional_keys__
22
+ for key in ipr_gene_keys:
23
+ ipr_spec["components"]["schemas"]["genesCreate"]["properties"][key] = ""
24
+ return ipr_spec
25
+
26
+
27
+ def get_test_file(name: str) -> str:
28
+ return os.path.join(os.path.dirname(__file__), "test_data", name)
29
+
30
+
31
+ @pytest.fixture(scope="module")
32
+ def report_upload_content(tmp_path_factory) -> Dict:
33
+ mock = MagicMock()
34
+ json_file = tmp_path_factory.mktemp("inputs") / "content.json"
35
+ json_file.write_text(
36
+ json.dumps(
37
+ {
38
+ "blargh": "some fake content",
39
+ "comparators": [
40
+ {"analysisRole": "expression (disease)", "name": "1"},
41
+ {"analysisRole": "expression (primary site)", "name": "2"},
42
+ {"analysisRole": "expression (biopsy site)", "name": "3"},
43
+ {"analysisRole": "expression (internal pancancer cohort)", "name": "4"},
44
+ ],
45
+ "patientId": "PATIENT001",
46
+ "project": "TEST",
47
+ "expressionVariants": json.loads(
48
+ pd.read_csv(get_test_file("expression.short.tab"), sep="\t").to_json(
49
+ orient="records"
50
+ )
51
+ ),
52
+ "smallMutations": json.loads(
53
+ pd.read_csv(get_test_file("small_mutations.short.tab"), sep="\t").to_json(
54
+ orient="records"
55
+ )
56
+ ),
57
+ "copyVariants": json.loads(
58
+ pd.read_csv(get_test_file("copy_variants.short.tab"), sep="\t").to_json(
59
+ orient="records"
60
+ )
61
+ ),
62
+ "structuralVariants": json.loads(
63
+ pd.read_csv(get_test_file("fusions.tab"), sep="\t").to_json(orient="records")
64
+ ),
65
+ "kbDiseaseMatch": "colorectal cancer",
66
+ },
67
+ allow_nan=False,
68
+ )
69
+ )
70
+ with patch.object(
71
+ sys,
72
+ "argv",
73
+ [
74
+ "ipr",
75
+ "--username",
76
+ os.environ.get("IPR_USER", os.environ["USER"]),
77
+ "--password",
78
+ os.environ["IPR_PASS"],
79
+ "--ipr_url",
80
+ "http://fake.url.ca",
81
+ "--graphkb_username",
82
+ os.environ.get("GRAPHKB_USER", os.environ["USER"]),
83
+ "--graphkb_password",
84
+ os.environ.get("GRAPHKB_PASS", os.environ["IPR_PASS"]),
85
+ "--graphkb_url",
86
+ os.environ.get("GRAPHKB_URL", False),
87
+ "--content",
88
+ str(json_file),
89
+ "--therapeutics",
90
+ ],
91
+ ):
92
+ with patch.object(IprConnection, "upload_report", new=mock):
93
+ with patch.object(IprConnection, "get_spec", return_value=get_test_spec()):
94
+ command_interface()
95
+
96
+ assert mock.called
97
+
98
+ report_content = mock.call_args[0][0]
99
+ return report_content
100
+
101
+
102
+ @pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
103
+ class TestCreateReport:
104
+ def test_main_sections_present(self, report_upload_content: Dict) -> None:
105
+ sections = set(report_upload_content.keys())
106
+
107
+ for section in [
108
+ "structuralVariants",
109
+ "expressionVariants",
110
+ "copyVariants",
111
+ "smallMutations",
112
+ "kbMatches",
113
+ "genes",
114
+ ]:
115
+ assert section in sections
116
+
117
+ def test_kept_low_quality_fusion(self, report_upload_content: Dict) -> None:
118
+ fusions = [(sv["gene1"], sv["gene2"]) for sv in report_upload_content["structuralVariants"]]
119
+ if (
120
+ EXCLUDE_BCGSC_TESTS
121
+ ): # may be missing statements assoc with SUZ12 if no access to bcgsc data
122
+ assert ("SARM1", "CDKL2") in fusions
123
+ else:
124
+ assert ("SARM1", "SUZ12") in fusions
125
+
126
+ def test_pass_through_content_added(self, report_upload_content: Dict) -> None:
127
+ # check the passthorough content was added
128
+ assert "blargh" in report_upload_content
129
+
130
+ def test_found_fusion_partner_gene(self, report_upload_content: Dict) -> None:
131
+ genes = report_upload_content["genes"]
132
+ # eg, A1BG
133
+ assert any([g.get("knownFusionPartner", False) for g in genes])
134
+
135
+ @pytest.mark.skipif(EXCLUDE_ONCOKB_TESTS, reason="excluding tests that depend on oncokb data")
136
+ def test_found_oncogene(self, report_upload_content: Dict) -> None:
137
+ genes = report_upload_content["genes"]
138
+ # eg, ZBTB20
139
+ assert any([g.get("oncogene", False) for g in genes])
140
+
141
+ @pytest.mark.skipif(EXCLUDE_ONCOKB_TESTS, reason="excluding tests that depend on oncokb data)")
142
+ def test_found_tumour_supressor(self, report_upload_content: Dict) -> None:
143
+ genes = report_upload_content["genes"]
144
+ # eg, ZNRF3
145
+ assert any([g.get("tumourSuppressor", False) for g in genes])
146
+
147
+ def test_found_kb_statement_related_gene(self, report_upload_content: Dict) -> None:
148
+ genes = report_upload_content["genes"]
149
+ assert any([g.get("kbStatementRelated", False) for g in genes])
150
+
151
+ @pytest.mark.skipif(EXCLUDE_ONCOKB_TESTS, reason="excluding tests that depend on oncokb data")
152
+ def test_found_cancer_gene_list_match_gene(self, report_upload_content: Dict) -> None:
153
+ genes = report_upload_content["genes"]
154
+ assert any([g.get("cancerGeneListMatch", False) for g in genes])
@@ -0,0 +1,72 @@
1
+ import os
2
+ import pandas as pd
3
+ import pytest
4
+ from typing import Dict
5
+ from unittest.mock import MagicMock, patch
6
+
7
+ from pori_python.ipr.connection import IprConnection
8
+ from pori_python.ipr.main import create_report
9
+
10
+ from .constants import EXCLUDE_INTEGRATION_TESTS
11
+
12
+ EXCLUDE_BCGSC_TESTS = os.environ.get("EXCLUDE_BCGSC_TESTS") == "1"
13
+
14
+
15
+ def get_test_file(name: str) -> str:
16
+ return os.path.join(os.path.dirname(__file__), "test_data", name)
17
+
18
+
19
+ @pytest.fixture(scope="module")
20
+ def probe_upload_content() -> Dict:
21
+ mock = MagicMock()
22
+ with patch.object(IprConnection, "upload_report", new=mock):
23
+ with patch.object(IprConnection, "get_spec", return_value={}):
24
+ create_report(
25
+ content={
26
+ "patientId": "PATIENT001",
27
+ "project": "TEST",
28
+ "smallMutations": pd.read_csv(
29
+ get_test_file("small_mutations_probe.tab"),
30
+ sep="\t",
31
+ dtype={"chromosome": "string"},
32
+ ).to_dict("records"),
33
+ "structuralVariants": pd.read_csv(
34
+ get_test_file("fusions.tab"), sep="\t"
35
+ ).to_dict("records"),
36
+ "blargh": "some fake content",
37
+ "kbDiseaseMatch": "colorectal cancer",
38
+ },
39
+ username=os.environ["IPR_USER"],
40
+ password=os.environ["IPR_PASS"],
41
+ log_level="info",
42
+ ipr_url="http://fake.url.ca",
43
+ graphkb_username=os.environ.get("GRAPHKB_USER", os.environ["IPR_USER"]),
44
+ graphkb_password=os.environ.get("GRAPHKB_PASS", os.environ["IPR_PASS"]),
45
+ graphkb_url=os.environ.get("GRAPHKB_URL", False),
46
+ )
47
+
48
+ assert mock.called
49
+
50
+ report_content = mock.call_args[0][0]
51
+ return report_content
52
+
53
+
54
+ @pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
55
+ class TestCreateReport:
56
+ def test_found_probe_small_mutations(self, probe_upload_content: Dict) -> None:
57
+ assert probe_upload_content["smallMutations"]
58
+
59
+ @pytest.mark.skipif(
60
+ EXCLUDE_BCGSC_TESTS, reason="excluding tests that depend on BCGSC-specific data"
61
+ )
62
+ def test_found_probe_small_mutations_match(self, probe_upload_content: Dict) -> None:
63
+ # verify each probe had a KB match
64
+ for sm_probe in probe_upload_content["smallMutations"]:
65
+ match_list = [
66
+ kb_match
67
+ for kb_match in probe_upload_content["kbMatches"]
68
+ if kb_match["variant"] == sm_probe["key"]
69
+ ]
70
+ assert (
71
+ match_list
72
+ ), f"probe match failure: {sm_probe['gene']} {sm_probe['proteinChange']} key: {sm_probe['proteinChange']}"
@@ -0,0 +1,157 @@
1
+ from unittest.mock import MagicMock
2
+
3
+ from pori_python.ipr.summary import (
4
+ GRAPHKB_GUI,
5
+ get_preferred_drug_representation,
6
+ substitute_sentence_template,
7
+ )
8
+
9
+
10
+ class TestGetPreferredDrugRepresentation:
11
+ def test_prefers_non_alias(self):
12
+ api = MagicMock(
13
+ query=MagicMock(
14
+ side_effect=[
15
+ [],
16
+ [
17
+ {"sourceId": "1", "alias": False, "source": "source", "name": "name"},
18
+ {"sourceId": "2", "alias": True, "source": "source", "name": "name"},
19
+ ],
20
+ ]
21
+ )
22
+ )
23
+ rec = get_preferred_drug_representation(api, "anything")
24
+ assert rec["sourceId"] == "1"
25
+
26
+ def test_prefers_non_deprecated(self):
27
+ api = MagicMock(
28
+ query=MagicMock(
29
+ side_effect=[
30
+ [],
31
+ [
32
+ {"sourceId": "1", "deprecated": True, "source": "source", "name": "name"},
33
+ {"sourceId": "2", "deprecated": False, "source": "source", "name": "name"},
34
+ ],
35
+ ]
36
+ )
37
+ )
38
+ rec = get_preferred_drug_representation(api, "anything")
39
+ assert rec["sourceId"] == "2"
40
+
41
+ def test_prefers_lower_sort_source(self):
42
+ api = MagicMock(
43
+ query=MagicMock(
44
+ side_effect=[
45
+ [{"@rid": "source2", "sort": 0}, {"@rid": "source1", "sort": 1}],
46
+ [
47
+ {"sourceId": "1", "deprecated": False, "source": "source1", "name": "name"},
48
+ {"sourceId": "2", "deprecated": False, "source": "source2", "name": "name"},
49
+ ],
50
+ ]
51
+ )
52
+ )
53
+ rec = get_preferred_drug_representation(api, "anything")
54
+ assert rec["sourceId"] == "2"
55
+
56
+ def test_prefers_newer_version(self):
57
+ api = MagicMock(
58
+ query=MagicMock(
59
+ side_effect=[
60
+ [],
61
+ [
62
+ {
63
+ "sourceId": "2",
64
+ "deprecated": True,
65
+ "source": "source",
66
+ "name": "name",
67
+ "sourceIdVersion": "1",
68
+ },
69
+ {
70
+ "sourceId": "2",
71
+ "deprecated": True,
72
+ "source": "source",
73
+ "name": "name",
74
+ "sourceIdVersion": "2",
75
+ },
76
+ ],
77
+ ]
78
+ )
79
+ )
80
+ rec = get_preferred_drug_representation(api, "anything")
81
+ assert rec["sourceIdVersion"] == "1"
82
+
83
+
84
+ class TestSubstituteSentenceTemplate:
85
+ def test_multiple_diseases_no_matches(self):
86
+ template = "{conditions:variant} is associated with {relevance} to {subject} in {conditions:disease} ({evidence})"
87
+ relevance = {"displayName": "senitivity"}
88
+ disease_matches = {"1"}
89
+ diseases = [
90
+ {"@class": "Disease", "@rid": "2", "displayName": "disease 1"},
91
+ {"@class": "Disease", "@rid": "3", "displayName": "disease 2"},
92
+ ]
93
+ variants = [
94
+ {
95
+ "@class": "CategoryVariant",
96
+ "displayName": "KRAS increased RNA expression",
97
+ "@rid": "4",
98
+ }
99
+ ]
100
+ subjects = [{"@class": "Therapy", "displayName": "some drug", "@rid": "5"}]
101
+ sentence = substitute_sentence_template(
102
+ template, diseases + variants, subjects, relevance, [], ["6", "7"], disease_matches
103
+ )
104
+ assert (
105
+ sentence
106
+ == f'KRAS increased RNA expression is associated with senitivity to some drug in other disease types (<a href="{GRAPHKB_GUI}/data/table?complex=eyJ0YXJnZXQiOiBbIjYiLCAiNyJdfQ%3D%3D&%40class=Statement" target="_blank" rel="noopener"></a>)'
107
+ )
108
+
109
+ def test_multiple_diseases_some_matches(self):
110
+ template = "{conditions:variant} is associated with {relevance} to {subject} in {conditions:disease} ({evidence})"
111
+ relevance = {"displayName": "senitivity"}
112
+ disease_matches = {"1"}
113
+ diseases = [
114
+ {"@class": "Disease", "@rid": "2", "displayName": "disease 2"},
115
+ {"@class": "Disease", "@rid": "1", "displayName": "disease 1"},
116
+ {"@class": "Disease", "@rid": "3", "displayName": "disease 3"},
117
+ ]
118
+ variants = [
119
+ {
120
+ "@class": "CategoryVariant",
121
+ "displayName": "KRAS increased RNA expression",
122
+ "@rid": "4",
123
+ }
124
+ ]
125
+ subjects = [{"@class": "Therapy", "displayName": "some drug", "@rid": "5"}]
126
+ sentence = substitute_sentence_template(
127
+ template, diseases + variants, subjects, relevance, [], ["6", "7"], disease_matches
128
+ )
129
+ assert (
130
+ sentence
131
+ == f'KRAS increased RNA expression is associated with senitivity to some drug in disease 1, and other disease types (<a href="{GRAPHKB_GUI}/data/table?complex=eyJ0YXJnZXQiOiBbIjYiLCAiNyJdfQ%3D%3D&%40class=Statement" target="_blank" rel="noopener"></a>)'
132
+ )
133
+
134
+ def test_multiple_diseases_only_matches(self):
135
+ template = "{conditions:variant} is associated with {relevance} to {subject} in {conditions:disease} ({evidence})"
136
+ relevance = {"displayName": "senitivity"}
137
+ disease_matches = {"1", "2", "3"}
138
+ diseases = [
139
+ {"@class": "Disease", "@rid": "2", "displayName": "disease 2"},
140
+ {"@class": "Disease", "@rid": "1", "displayName": "disease 1"},
141
+ {"@class": "Disease", "@rid": "3", "displayName": "disease 3"},
142
+ ]
143
+ variants = [
144
+ {
145
+ "@class": "CategoryVariant",
146
+ "displayName": "KRAS increased RNA expression",
147
+ "@rid": "4",
148
+ }
149
+ ]
150
+ subjects = [{"@class": "Therapy", "displayName": "some drug", "@rid": "5"}]
151
+ sentence = substitute_sentence_template(
152
+ template, diseases + variants, subjects, relevance, [], ["6", "7"], disease_matches
153
+ )
154
+ assert (
155
+ sentence
156
+ == f'KRAS increased RNA expression is associated with senitivity to some drug in disease 1, disease 2, and disease 3 (<a href="{GRAPHKB_GUI}/data/table?complex=eyJ0YXJnZXQiOiBbIjYiLCAiNyJdfQ%3D%3D&%40class=Statement" target="_blank" rel="noopener"></a>)'
157
+ )
@@ -0,0 +1,274 @@
1
+ import json
2
+ import os
3
+ import pandas as pd
4
+ import pytest
5
+ import sys
6
+ import uuid
7
+ from typing import Generator
8
+ from unittest.mock import patch
9
+
10
+ from pori_python.ipr.connection import IprConnection
11
+ from pori_python.ipr.main import command_interface
12
+ from pori_python.types import IprGene
13
+
14
+ from .constants import EXCLUDE_INTEGRATION_TESTS
15
+
16
+ EXCLUDE_BCGSC_TESTS = os.environ.get("EXCLUDE_BCGSC_TESTS") == "1"
17
+ EXCLUDE_ONCOKB_TESTS = os.environ.get("EXCLUDE_ONCOKB_TESTS") == "1"
18
+ INCLUDE_UPLOAD_TESTS = os.environ.get("INCLUDE_UPLOAD_TESTS", 0) == "1"
19
+
20
+
21
+ def get_test_spec():
22
+ ipr_spec = {"components": {"schemas": {"genesCreate": {"properties": {}}}}}
23
+ ipr_gene_keys = IprGene.__required_keys__ | IprGene.__optional_keys__
24
+ for key in ipr_gene_keys:
25
+ ipr_spec["components"]["schemas"]["genesCreate"]["properties"][key] = ""
26
+ return ipr_spec
27
+
28
+
29
+ def get_test_file(name: str) -> str:
30
+ return os.path.join(os.path.dirname(__file__), "test_data", name)
31
+
32
+
33
+ @pytest.fixture(scope="module")
34
+ def loaded_reports(tmp_path_factory) -> Generator:
35
+ json_file = tmp_path_factory.mktemp("inputs") / "content.json"
36
+ async_json_file = tmp_path_factory.mktemp("inputs") / "async_content.json"
37
+ patient_id = f"TEST_{str(uuid.uuid4())}"
38
+ async_patient_id = f"TEST_ASYNC_{str(uuid.uuid4())}"
39
+ json_contents = {
40
+ "comparators": [
41
+ {"analysisRole": "expression (disease)", "name": "1"},
42
+ {"analysisRole": "expression (primary site)", "name": "2"},
43
+ {"analysisRole": "expression (biopsy site)", "name": "3"},
44
+ {
45
+ "analysisRole": "expression (internal pancancer cohort)",
46
+ "name": "4",
47
+ },
48
+ ],
49
+ "patientId": patient_id,
50
+ "project": "TEST",
51
+ "sampleInfo": [
52
+ {
53
+ "sample": "Constitutional",
54
+ "biopsySite": "Normal tissue",
55
+ "sampleName": "SAMPLE1-PB",
56
+ "primarySite": "Blood-Peripheral",
57
+ "collectionDate": "11-11-11",
58
+ },
59
+ {
60
+ "sample": "Tumour",
61
+ "pathoTc": "90%",
62
+ "biopsySite": "hepatic",
63
+ "sampleName": "SAMPLE2-FF-1",
64
+ "primarySite": "Vena Cava-Hepatic",
65
+ "collectionDate": "12-12-12",
66
+ },
67
+ ],
68
+ "expressionVariants": json.loads(
69
+ pd.read_csv(get_test_file("expression.short.tab"), sep="\t").to_json(orient="records")
70
+ ),
71
+ "smallMutations": json.loads(
72
+ pd.read_csv(get_test_file("small_mutations.short.tab"), sep="\t").to_json(
73
+ orient="records"
74
+ )
75
+ ),
76
+ "copyVariants": json.loads(
77
+ pd.read_csv(get_test_file("copy_variants.short.tab"), sep="\t").to_json(
78
+ orient="records"
79
+ )
80
+ ),
81
+ "structuralVariants": json.loads(
82
+ pd.read_csv(get_test_file("fusions.tab"), sep="\t").to_json(orient="records")
83
+ ),
84
+ "kbDiseaseMatch": "colorectal cancer",
85
+ }
86
+ json_file.write_text(
87
+ json.dumps(
88
+ json_contents,
89
+ allow_nan=False,
90
+ )
91
+ )
92
+
93
+ json_contents["patientId"] = async_patient_id
94
+ async_json_file.write_text(
95
+ json.dumps(
96
+ json_contents,
97
+ allow_nan=False,
98
+ )
99
+ )
100
+
101
+ argslist = [
102
+ "ipr",
103
+ "--username",
104
+ os.environ.get("IPR_USER", os.environ["USER"]),
105
+ "--password",
106
+ os.environ["IPR_PASS"],
107
+ "--graphkb_username",
108
+ os.environ.get("GRAPHKB_USER", os.environ.get("IPR_USER", os.environ["USER"])),
109
+ "--graphkb_password",
110
+ os.environ.get("GRAPHKB_PASS", os.environ["IPR_PASS"]),
111
+ "--ipr_url",
112
+ os.environ["IPR_TEST_URL"],
113
+ "--graphkb_url",
114
+ os.environ.get("GRAPHKB_URL", False),
115
+ "--therapeutics",
116
+ ]
117
+
118
+ sync_argslist = argslist.copy()
119
+ sync_argslist.extend(["--content", str(json_file)])
120
+ with patch.object(sys, "argv", sync_argslist):
121
+ with patch.object(IprConnection, "get_spec", return_value=get_test_spec()):
122
+ command_interface()
123
+
124
+ async_argslist = argslist.copy()
125
+ async_argslist.extend(["--content", str(async_json_file), "--async_upload"])
126
+ with patch.object(sys, "argv", async_argslist):
127
+ with patch.object(IprConnection, "get_spec", return_value=get_test_spec()):
128
+ command_interface()
129
+
130
+ ipr_conn = IprConnection(
131
+ username=os.environ.get("IPR_USER", os.environ["USER"]),
132
+ password=os.environ["IPR_PASS"],
133
+ url=os.environ["IPR_TEST_URL"],
134
+ )
135
+ loaded_report = ipr_conn.get(uri=f"reports?searchText={patient_id}")
136
+ async_loaded_report = ipr_conn.get(uri=f"reports?searchText={async_patient_id}")
137
+
138
+ loaded_reports_result = {
139
+ "sync": (patient_id, loaded_report),
140
+ "async": (async_patient_id, async_loaded_report),
141
+ }
142
+ yield loaded_reports_result
143
+ ipr_conn.delete(uri=f"reports/{loaded_report['reports'][0]['ident']}")
144
+ ipr_conn.delete(uri=f"reports/{async_loaded_report['reports'][0]['ident']}")
145
+
146
+
147
+ def get_section(loaded_report, section_name):
148
+ ident = loaded_report[1]["reports"][0]["ident"]
149
+ ipr_conn = IprConnection(
150
+ username=os.environ.get("IPR_USER", os.environ["USER"]),
151
+ password=os.environ["IPR_PASS"],
152
+ url=os.environ["IPR_TEST_URL"],
153
+ )
154
+ return ipr_conn.get(uri=f"reports/{ident}/{section_name}")
155
+
156
+
157
+ def compare_sections(section1, section2):
158
+ """Removes values that will be distinct from sections, then checks if they
159
+ are otherwise equal"""
160
+ for key in ("ident", "updatedAt", "createdAt", "deletedAt"):
161
+ for section in [section1, section2]:
162
+ [item.pop(key, None) for item in section]
163
+ for item in section:
164
+ for subitem in item.keys():
165
+ if isinstance(item[subitem], dict):
166
+ item[subitem].pop(key, None)
167
+ if isinstance(item[subitem], list):
168
+ [subsubitem.pop(key, None) for subsubitem in item[subitem]]
169
+ if item[subitem] != []:
170
+ item[subitem] = sorted(item[subitem], key=lambda d: str(d))
171
+ if isinstance(section1, list):
172
+ section2_items = [str(item) for item in section2]
173
+ section2_items.sort()
174
+ section1_items = [str(item) for item in section1]
175
+ section1_items.sort()
176
+ return str(section2_items) == str(section1_items)
177
+ return str(section1) == str(section2)
178
+
179
+
180
+ @pytest.mark.skipif(
181
+ not INCLUDE_UPLOAD_TESTS, reason="excluding tests of upload to live ipr instance"
182
+ )
183
+ @pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
184
+ class TestCreateReport:
185
+ def test_patient_id_loaded_once(self, loaded_reports) -> None:
186
+ sync_patient_id = loaded_reports["sync"][0]
187
+ assert loaded_reports["sync"][1]["total"] == 1
188
+ assert loaded_reports["sync"][1]["reports"][0]["patientId"] == sync_patient_id
189
+ async_patient_id = loaded_reports["async"][0]
190
+ assert loaded_reports["async"][1]["total"] == 1
191
+ assert loaded_reports["async"][1]["reports"][0]["patientId"] == async_patient_id
192
+
193
+ def test_expression_variants_loaded(self, loaded_reports) -> None:
194
+ section = get_section(loaded_reports["sync"], "expression-variants")
195
+ kbmatched = [item for item in section if item["kbMatches"]]
196
+ assert "PTP4A3" in [item["gene"]["name"] for item in kbmatched]
197
+ async_section = get_section(loaded_reports["async"], "expression-variants")
198
+ assert compare_sections(section, async_section)
199
+
200
+ def test_structural_variants_loaded(self, loaded_reports) -> None:
201
+ section = get_section(loaded_reports["sync"], "structural-variants")
202
+ kbmatched = [item for item in section if item["kbMatches"]]
203
+ assert "(EWSR1,FLI1):fusion(e.7,e.4)" in [item["displayName"] for item in kbmatched]
204
+ async_section = get_section(loaded_reports["async"], "structural-variants")
205
+ assert compare_sections(section, async_section)
206
+
207
+ def test_small_mutations_loaded(self, loaded_reports) -> None:
208
+ section = get_section(loaded_reports["sync"], "small-mutations")
209
+ kbmatched = [item for item in section if item["kbMatches"]]
210
+ assert "FGFR2:p.R421C" in [item["displayName"] for item in kbmatched]
211
+ assert "CDKN2A:p.T18M" in [item["displayName"] for item in kbmatched]
212
+ async_section = get_section(loaded_reports["async"], "small-mutations")
213
+ assert compare_sections(section, async_section)
214
+
215
+ def test_copy_variants_loaded(self, loaded_reports) -> None:
216
+ section = get_section(loaded_reports["sync"], "copy-variants")
217
+ kbmatched = [item for item in section if item["kbMatches"]]
218
+ assert ("ERBB2", "amplification") in [
219
+ (item["gene"]["name"], item["displayName"]) for item in kbmatched
220
+ ]
221
+ async_section = get_section(loaded_reports["async"], "copy-variants")
222
+ assert compare_sections(section, async_section)
223
+
224
+ def test_kb_matches_loaded(self, loaded_reports) -> None:
225
+ section = get_section(loaded_reports["sync"], "kb-matches")
226
+ observed_and_matched = set(
227
+ [(item["kbVariant"], item["variant"]["displayName"]) for item in section]
228
+ )
229
+ for pair in [
230
+ ("ERBB2 amplification", "amplification"),
231
+ ("FGFR2 mutation", "FGFR2:p.R421C"),
232
+ ("PTP4A3 overexpression", "increased expression"),
233
+ ("EWSR1 and FLI1 fusion", "(EWSR1,FLI1):fusion(e.7,e.4)"),
234
+ ("CDKN2A mutation", "CDKN2A:p.T18M"),
235
+ ]:
236
+ assert pair in observed_and_matched
237
+ async_section = get_section(loaded_reports["async"], "kb-matches")
238
+ assert compare_sections(section, async_section)
239
+
240
+ def test_therapeutic_targets_loaded(self, loaded_reports) -> None:
241
+ section = get_section(loaded_reports["sync"], "therapeutic-targets")
242
+ therapeutic_target_genes = set([item["gene"] for item in section])
243
+ for gene in ["CDKN2A", "ERBB2", "FGFR2", "PTP4A3"]:
244
+ assert gene in therapeutic_target_genes
245
+ async_section = get_section(loaded_reports["async"], "therapeutic-targets")
246
+ assert compare_sections(section, async_section)
247
+
248
+ def test_genomic_alterations_identified_loaded(self, loaded_reports) -> None:
249
+ section = get_section(loaded_reports["sync"], "summary/genomic-alterations-identified")
250
+ variants = set([item["geneVariant"] for item in section])
251
+ for variant in [
252
+ "FGFR2:p.R421C",
253
+ "PTP4A3 (high_percentile)",
254
+ "ERBB2 (Amplification)",
255
+ "(EWSR1,FLI1):fusion(e.7,e.4)",
256
+ "CDKN2A:p.T18M",
257
+ ]:
258
+ assert variant in variants
259
+ async_section = get_section(
260
+ loaded_reports["async"], "summary/genomic-alterations-identified"
261
+ )
262
+ assert compare_sections(section, async_section)
263
+
264
+ def test_analyst_comments_loaded(self, loaded_reports) -> None:
265
+ sync_section = get_section(loaded_reports["sync"], "summary/analyst-comments")
266
+ assert sync_section["comments"]
267
+ async_section = get_section(loaded_reports["async"], "summary/analyst-comments")
268
+ assert async_section["comments"]
269
+ assert sync_section["comments"] == async_section["comments"]
270
+
271
+ def test_sample_info_loaded(self, loaded_reports) -> None:
272
+ sync_section = get_section(loaded_reports["sync"], "sample-info")
273
+ async_section = get_section(loaded_reports["async"], "sample-info")
274
+ assert compare_sections(sync_section, async_section)