pori-python 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pori_python/__init__.py +1 -0
- pori_python/graphkb/__init__.py +2 -0
- pori_python/graphkb/constants.py +220 -0
- pori_python/graphkb/genes.py +540 -0
- pori_python/graphkb/match.py +710 -0
- pori_python/graphkb/py.typed +0 -0
- pori_python/graphkb/statement.py +48 -0
- pori_python/graphkb/util.py +523 -0
- pori_python/graphkb/vocab.py +198 -0
- pori_python/ipr/__init__.py +0 -0
- pori_python/ipr/annotate.py +373 -0
- pori_python/ipr/connection.py +214 -0
- pori_python/ipr/constants.py +9 -0
- pori_python/ipr/inputs.py +593 -0
- pori_python/ipr/ipr.py +390 -0
- pori_python/ipr/main.py +512 -0
- pori_python/ipr/py.typed +0 -0
- pori_python/ipr/summary.py +412 -0
- pori_python/ipr/therapeutic_options.py +83 -0
- pori_python/ipr/util.py +173 -0
- pori_python/types.py +177 -0
- pori_python-0.1.0.dist-info/LICENSE +674 -0
- pori_python-0.1.0.dist-info/METADATA +151 -0
- pori_python-0.1.0.dist-info/RECORD +49 -0
- pori_python-0.1.0.dist-info/WHEEL +5 -0
- pori_python-0.1.0.dist-info/dependency_links.txt +1 -0
- pori_python-0.1.0.dist-info/entry_points.txt +2 -0
- pori_python-0.1.0.dist-info/top_level.txt +2 -0
- tests/__init__.py +0 -0
- tests/test_graphkb/__init__.py +0 -0
- tests/test_graphkb/data.py +78 -0
- tests/test_graphkb/test_genes.py +306 -0
- tests/test_graphkb/test_graphkb.py +32 -0
- tests/test_graphkb/test_match.py +662 -0
- tests/test_graphkb/test_statement.py +100 -0
- tests/test_graphkb/test_util.py +151 -0
- tests/test_graphkb/test_vocab.py +85 -0
- tests/test_ipr/__init__.py +0 -0
- tests/test_ipr/constants.py +3 -0
- tests/test_ipr/test_annotate.py +149 -0
- tests/test_ipr/test_connection.py +97 -0
- tests/test_ipr/test_inputs.py +295 -0
- tests/test_ipr/test_ipr.py +338 -0
- tests/test_ipr/test_main.py +154 -0
- tests/test_ipr/test_probe.py +72 -0
- tests/test_ipr/test_summary.py +157 -0
- tests/test_ipr/test_upload.py +274 -0
- tests/test_ipr/test_util.py +29 -0
- tests/test_ipr/util.py +9 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
6
|
+
import zlib
|
|
7
|
+
from typing import Dict, List
|
|
8
|
+
|
|
9
|
+
from .constants import DEFAULT_URL
|
|
10
|
+
from .util import logger
|
|
11
|
+
|
|
12
|
+
IMAGE_MAX = 20 # cannot upload more than 20 images at a time
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class IprConnection:
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
username: str,
|
|
19
|
+
password: str,
|
|
20
|
+
url: str = os.environ.get("IPR_URL", DEFAULT_URL),
|
|
21
|
+
):
|
|
22
|
+
self.token = None
|
|
23
|
+
self.url = url
|
|
24
|
+
self.username = username
|
|
25
|
+
self.password = password
|
|
26
|
+
self.headers = {
|
|
27
|
+
"Accept": "application/json",
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
"Content-Encoding": "deflate",
|
|
30
|
+
}
|
|
31
|
+
self.cache: Dict[str, List[Dict]] = {}
|
|
32
|
+
self.request_count = 0
|
|
33
|
+
|
|
34
|
+
def request(self, endpoint: str, method: str = "GET", **kwargs) -> Dict:
|
|
35
|
+
"""Request wrapper to handle adding common headers and logging
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
endpoint (string): api endpoint, excluding the base uri
|
|
39
|
+
method (str, optional): the http method. Defaults to 'GET'.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
dict: the json response as a python dict
|
|
43
|
+
"""
|
|
44
|
+
url = f"{self.url}/{endpoint}"
|
|
45
|
+
self.request_count += 1
|
|
46
|
+
kwargs_header = kwargs.pop("headers", None)
|
|
47
|
+
if kwargs_header:
|
|
48
|
+
headers = json.loads(kwargs_header)
|
|
49
|
+
else:
|
|
50
|
+
headers = self.headers
|
|
51
|
+
resp = requests.request(
|
|
52
|
+
method, url, headers=headers, auth=(self.username, self.password), **kwargs
|
|
53
|
+
)
|
|
54
|
+
try:
|
|
55
|
+
resp.raise_for_status()
|
|
56
|
+
except requests.exceptions.HTTPError as err:
|
|
57
|
+
# try to get more error details
|
|
58
|
+
message = str(err)
|
|
59
|
+
try:
|
|
60
|
+
message += " " + resp.json()["error"]["message"]
|
|
61
|
+
except Exception:
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
raise requests.exceptions.HTTPError(message)
|
|
65
|
+
if resp.status_code == 204: # TODO: address this in api
|
|
66
|
+
return {"status_code": 204}
|
|
67
|
+
return resp.json()
|
|
68
|
+
|
|
69
|
+
def post(self, uri: str, data: Dict = {}, **kwargs) -> Dict:
|
|
70
|
+
"""Convenience method for making post requests"""
|
|
71
|
+
return self.request(
|
|
72
|
+
uri,
|
|
73
|
+
method="POST",
|
|
74
|
+
data=zlib.compress(json.dumps(data, allow_nan=False).encode("utf-8")),
|
|
75
|
+
**kwargs,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def get(self, uri: str, data: Dict = {}, **kwargs) -> Dict:
|
|
79
|
+
"""Convenience method for making get requests"""
|
|
80
|
+
return self.request(
|
|
81
|
+
uri,
|
|
82
|
+
method="GET",
|
|
83
|
+
data=zlib.compress(json.dumps(data, allow_nan=False).encode("utf-8")),
|
|
84
|
+
**kwargs,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def delete(self, uri: str, data: Dict = {}, **kwargs) -> Dict:
|
|
88
|
+
"""Convenience method for making delete requests"""
|
|
89
|
+
return self.request(
|
|
90
|
+
uri,
|
|
91
|
+
method="DELETE",
|
|
92
|
+
data=zlib.compress(json.dumps(data, allow_nan=False).encode("utf-8")),
|
|
93
|
+
headers=json.dumps({"Accept": "*/*"}),
|
|
94
|
+
**kwargs,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
def upload_report(
|
|
98
|
+
self, content: Dict, mins_to_wait: int = 5, async_upload: bool = False
|
|
99
|
+
) -> Dict:
|
|
100
|
+
if async_upload:
|
|
101
|
+
# if async is used, the response for reports-async contains either 'jobStatus'
|
|
102
|
+
# or 'report'. jobStatus is no longer available once the report is successfully
|
|
103
|
+
# uploaded.
|
|
104
|
+
initial_result = self.post("reports-async", content)
|
|
105
|
+
|
|
106
|
+
report_id = initial_result["ident"]
|
|
107
|
+
|
|
108
|
+
def check_status_result(result):
|
|
109
|
+
if result.get("report", False):
|
|
110
|
+
return "upload complete"
|
|
111
|
+
if result.get("jobStatus", False) and result["jobStatus"].get("state", False):
|
|
112
|
+
return result["jobStatus"]["state"]
|
|
113
|
+
raise Exception(
|
|
114
|
+
"async report get returned with no report or jobStatus, or unexpected jobStatus type"
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def check_status(interval: int = 5, num_attempts: int = 5):
|
|
118
|
+
for i in range(num_attempts):
|
|
119
|
+
logger.info(f"checking report loading status in {interval} seconds")
|
|
120
|
+
time.sleep(interval)
|
|
121
|
+
current_status = self.get(f"reports-async/{report_id}")
|
|
122
|
+
|
|
123
|
+
check_result = check_status_result(current_status)
|
|
124
|
+
|
|
125
|
+
if check_result == "upload complete":
|
|
126
|
+
return current_status
|
|
127
|
+
|
|
128
|
+
if check_result == "failed":
|
|
129
|
+
raise Exception(
|
|
130
|
+
f"async report upload failed with reason: {current_status.get('jobStatus', {}).get('failedReason', 'Unknown')}"
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
if check_result not in [
|
|
134
|
+
"active",
|
|
135
|
+
"ready",
|
|
136
|
+
"waiting",
|
|
137
|
+
"completed",
|
|
138
|
+
]:
|
|
139
|
+
raise Exception(f"async report upload in unexpected state: {check_result}")
|
|
140
|
+
|
|
141
|
+
return current_status
|
|
142
|
+
|
|
143
|
+
current_status = check_status()
|
|
144
|
+
check_result = check_status_result(current_status)
|
|
145
|
+
|
|
146
|
+
if check_result in ["active", "waiting"]:
|
|
147
|
+
current_status = check_status(interval=30)
|
|
148
|
+
check_result = check_status_result(current_status)
|
|
149
|
+
|
|
150
|
+
if check_result in ["active", "waiting"]:
|
|
151
|
+
current_status = check_status(interval=60, num_attempts=mins_to_wait)
|
|
152
|
+
check_result = check_status_result(current_status)
|
|
153
|
+
|
|
154
|
+
if check_result in ["active", "waiting"]:
|
|
155
|
+
raise Exception(
|
|
156
|
+
f"async report upload taking longer than expected: {current_status}"
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
return current_status
|
|
160
|
+
else:
|
|
161
|
+
return self.post("reports", content)
|
|
162
|
+
|
|
163
|
+
def set_analyst_comments(self, report_id: str, data: Dict) -> Dict:
|
|
164
|
+
"""
|
|
165
|
+
Update report comments to an existing report
|
|
166
|
+
|
|
167
|
+
TODO:
|
|
168
|
+
Add to main upload.
|
|
169
|
+
Pending: https://www.bcgsc.ca/jira/browse/DEVSU-1177
|
|
170
|
+
"""
|
|
171
|
+
return self.request(
|
|
172
|
+
f"/reports/{report_id}/summary/analyst-comments",
|
|
173
|
+
method="PUT",
|
|
174
|
+
data=zlib.compress(json.dumps(data, allow_nan=False).encode("utf-8")),
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
def post_images(self, report_id: str, files: Dict[str, str], data: Dict[str, str] = {}) -> None:
|
|
178
|
+
"""
|
|
179
|
+
Post images to the report
|
|
180
|
+
"""
|
|
181
|
+
file_keys = list(files.keys())
|
|
182
|
+
start_index = 0
|
|
183
|
+
image_errors = set()
|
|
184
|
+
while start_index < len(file_keys):
|
|
185
|
+
current_files = {}
|
|
186
|
+
for key in file_keys[start_index : start_index + IMAGE_MAX]:
|
|
187
|
+
path = files[key]
|
|
188
|
+
if not os.path.exists(path):
|
|
189
|
+
raise FileNotFoundError(path)
|
|
190
|
+
current_files[key] = path
|
|
191
|
+
open_files = {k: open(f, "rb") for (k, f) in current_files.items()}
|
|
192
|
+
try:
|
|
193
|
+
resp = self.request(
|
|
194
|
+
f"reports/{report_id}/image",
|
|
195
|
+
method="POST",
|
|
196
|
+
data=data,
|
|
197
|
+
files=open_files,
|
|
198
|
+
headers=json.dumps({}),
|
|
199
|
+
)
|
|
200
|
+
for status in resp:
|
|
201
|
+
if status.get("upload") != "successful":
|
|
202
|
+
image_errors.add(status["key"])
|
|
203
|
+
finally:
|
|
204
|
+
for handler in open_files.values():
|
|
205
|
+
handler.close()
|
|
206
|
+
start_index += IMAGE_MAX
|
|
207
|
+
if image_errors:
|
|
208
|
+
raise ValueError(f'Error uploading images ({", ".join(sorted(list(image_errors)))})')
|
|
209
|
+
|
|
210
|
+
def get_spec(self) -> Dict:
|
|
211
|
+
"""
|
|
212
|
+
Get the current IPR spec, for the purposes of current report upload fields
|
|
213
|
+
"""
|
|
214
|
+
return self.request("/spec.json", method="GET")
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
TMB_HIGH = 10.0 # genomic mutations per mb - https://www.bcgsc.ca/jira/browse/GERO-296
|
|
9
|
+
TMB_HIGH_CATEGORY = "high mutation burden"
|