-
Notifications
You must be signed in to change notification settings - Fork 1
/
batches.py
286 lines (242 loc) · 11.4 KB
/
batches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Management of batches."""
import datetime
import json
import pathlib
import typing
from attrs import evolve
from logzero import logger
from tabulate import tabulate
from clinvar_api import client, common, models
from clinvar_this import config, exceptions
from clinvar_this.io import tsv
#: Shared files directory
SHARE_DIR = pathlib.Path.home() / ".local" / "share" / "clinvar-this"
#: Format string
FORMAT_STR = "%Y%m%d%H%M%S"
def _list_get_batches(share_dir: pathlib.Path):
if not share_dir.exists():
return []
else:
return [share_dir / path for path in share_dir.glob("*") if path.is_dir()]
def list_(config: config.Config):
"""List batches to stdout."""
print(f"Listing batches at {SHARE_DIR}")
_ = config
paths = _list_get_batches(SHARE_DIR)
if not paths:
table = [["-- NO BATCHES YET --"]]
print(tabulate(table))
else:
table = [[path.name] for path in sorted(paths)]
print(tabulate(table, headers=["path"], tablefmt="grid"))
def gen_name() -> str:
"""Generate batch name that does not exist yet."""
base = datetime.date.today().strftime("%Y-%m-%d")
for i in range(1000):
dirname = "%s-%03d" % (base, i)
if not (SHARE_DIR / dirname).exists():
return dirname
else: # pragma: no cover
raise exceptions.IOException("Could not generate batch name")
def _write_payload(submission_container: models.SubmissionContainer, name: str):
"""Write out payload to a new JSON file."""
# Create directory for batch.
batch_dir = SHARE_DIR / name
batch_dir.mkdir(exist_ok=True, parents=True)
# Write out payload.
timestamp = datetime.datetime.now().strftime(FORMAT_STR)
payload_path = batch_dir / f"payload.{timestamp}.json"
payload_json = json.dumps(common.CONVERTER.unstructure(submission_container), indent=2)
with payload_path.open("wt") as outputf:
outputf.write(payload_json)
def _merge_submission_container(
base: models.SubmissionContainer,
patch: models.SubmissionContainer,
) -> models.SubmissionContainer:
"""Update base submission container with new one.
The following attributes will be copied from ``patch`` to ``base``:
- mode of inheritance
- clinical significance
- clinical significance description (ACMG grading)
- condition
"""
logger.info("Merging submission information...")
def merge_submission(
base: models.SubmissionClinvarSubmission,
patch: models.SubmissionClinvarSubmission,
) -> models.SubmissionClinvarSubmission:
return evolve(
base,
condition_set=patch.condition_set,
clinical_significance=patch.clinical_significance,
)
patch_clinvar_submission = {
clinvar_submission.local_key: clinvar_submission
for clinvar_submission in (patch.clinvar_submission or [])
}
clinvar_submissions = []
for submission in base.clinvar_submission or []:
if submission.local_key in patch_clinvar_submission:
clinvar_submissions.append(
merge_submission(submission, patch_clinvar_submission[submission.local_key])
)
else:
clinvar_submissions.append(submission)
result = evolve(base, clinvar_submission=clinvar_submissions)
logger.info("... done merging submission information")
return result
def import_(config: config.Config, name: str, path: str, metadata: typing.Tuple[str, ...]):
"""Import the data file at ``path`` into the batch of name ``name``."""
existing_payloads = list((SHARE_DIR / name).glob("payload.*.json"))
if existing_payloads:
logger.info("Loading existing payload for later merging with new one")
previous_submission_container = _load_latest_payload(name)
else:
previous_submission_container = None
if path.endswith(".tsv") or path.endswith(".txt"):
tsv_records = tsv.read_tsv(path=path)
batch_metadata = tsv.batch_metadata_from_mapping(metadata, use_defaults=True)
new_submission_container = tsv.tsv_records_to_submission_container(
tsv_records, batch_metadata
)
if previous_submission_container:
submission_container = _merge_submission_container(
base=previous_submission_container,
patch=new_submission_container,
)
else:
submission_container = new_submission_container
_write_payload(submission_container, name)
else:
raise exceptions.IOException(f"File extension of {path} cannot be handled.")
def _load_latest_payload(name):
submission_path = SHARE_DIR / name
payload_paths = list(sorted(submission_path.glob("payload.*.json")))
if not payload_paths:
raise exceptions.ClinvarThisException(f"Found no payload JSON file at {submission_path}")
payload_path = submission_path / payload_paths[-1]
with payload_path.open("rt") as inputf:
payload_json = inputf.read()
payload_unstructured = json.loads(payload_json)
return common.CONVERTER.structure(payload_unstructured, models.SubmissionContainer)
def export_(config: config.Config, name: str, path: str, force: bool = False):
"""Export the batch with the given ``name`` to the file at ``path``."""
if pathlib.Path(path).exists() and not force:
raise exceptions.IOException(
f"File at output path {path} already exists. Use --force to overwrite."
)
if path.endswith(".tsv") or path.endswith(".txt"):
payload = _load_latest_payload(name)
tsv_records = tsv.submission_container_to_tsv_records(payload)
tsv.write_tsv(tsv_records, path=path)
else:
raise exceptions.IOException(f"File extension of {path} cannot be handled.")
def update(config: config.Config, name: str, metadata: typing.Tuple[str, ...]):
"""Update the batch' meta data."""
batch_metadata = tsv.batch_metadata_from_mapping(metadata, use_defaults=False)
_ = batch_metadata
def submit(config: config.Config, name: str, *, use_testing: bool = False, dry_run: bool = False):
"""Submit the batch to ClinVar."""
client_obj = client.Client(
client.Config(auth_token=config.auth_token, use_testing=use_testing, use_dryrun=dry_run)
)
payload = _load_latest_payload(name)
logger.info("Initiating submission to ClinVar API")
client_res = client_obj.submit_data(payload)
# Terminate earlyier in dry-run mode.
if dry_run:
logger.info("In dry-run mode, not writing out response.")
return
timestamp = datetime.datetime.now().strftime(FORMAT_STR)
response_path = SHARE_DIR / name / f"submission-response.{timestamp}.json"
response_data = common.CONVERTER.unstructure(client_res)
logger.info("Writing out server response to %s", response_path)
with response_path.open("wt") as outputf:
json.dump(response_data, outputf)
logger.info(
"The ClinVar API has accepted your submission and will perform additional checks in the background."
)
logger.info(
(
"The next step is to run ``clinvar-this batch retrieve %s`` and wait until you get a "
"final success or error response"
),
name,
)
logger.info("All done. Have a nice day!")
def _retrieve_store_response(name: str, status_result: client.RetrieveStatusResult):
"""Store information from the retrieve status result into a new payload JSON file.
Currently, this is the clinvar accession only.
"""
logger.debug("Updating local payload from retrieve status response ...")
logger.debug("Obtaining local key to accession map")
local_key_to_accession = {}
for summary_response in status_result.summaries.values():
submissions = summary_response.submissions or []
for submission in submissions:
local_key_to_accession[
submission.identifiers.local_key
] = submission.identifiers.clinvar_accession
logger.debug("Update map is %s", local_key_to_accession)
logger.debug("Loading latest payload")
payload = _load_latest_payload(name)
logger.debug("Updating local payload")
clinvar_submission = [
evolve(
submission,
clinvar_accession=local_key_to_accession.get(
submission.local_key, submission.clinvar_accession
),
record_status=models.RecordStatus.UPDATE
if submission.local_key in local_key_to_accession
else models.RecordStatus.NOVEL,
)
for submission in payload.clinvar_submission
]
updated_payload = evolve(payload, clinvar_submission=clinvar_submission)
logger.debug("Write out updated payload")
_write_payload(updated_payload, name)
logger.debug("... done updating local payload from retrieve status response")
def retrieve(config: config.Config, name: str, *, use_testing: bool = False):
"""Retrieve current processing status from ClinVar."""
client_obj = client.Client(client.Config(auth_token=config.auth_token, use_testing=use_testing))
submission_path = SHARE_DIR / name
submission_response_paths = list(sorted(submission_path.glob("submission-response.*.json")))
if not submission_path.exists():
raise exceptions.ClinvarThisException(f"Submission does not exist at {submission_path}")
elif not submission_response_paths:
raise exceptions.ClinvarThisException(
f"Submission not submitted? No submission response at {submission_path}"
)
submission_response_path = submission_path / submission_response_paths[-1]
logger.info("Loading response from %s", submission_response_path)
with submission_response_path.open("rt") as inputf:
created = common.CONVERTER.structure(json.load(inputf), models.Created)
logger.info("Submission ID is %s", created.id)
logger.info("Initiating fetching of status from ClinVar API")
status_result = client_obj.retrieve_status(created.id)
timestamp = datetime.datetime.now().strftime(FORMAT_STR)
retrieve_response_path = submission_path / f"retrieve-response.{timestamp}.json"
logger.debug("Writing out response to %s", retrieve_response_path)
with retrieve_response_path.open("wt") as outputf:
json.dump(common.CONVERTER.unstructure(status_result), outputf, indent=2)
status_str = status_result.status.actions[0].status
if status_str in ["submitted", "processing"]:
logger.info(f"Status is {status_str}, be patient and check back in a while...")
logger.info(
"Submissions with errors tend to fail quickly while successful submissions tend to take a while."
)
elif status_str == "processed":
logger.info("Submission has been processed successfully")
logger.info("Will now update local information from response...")
_retrieve_store_response(name, status_result)
logger.info("... done updating local information from response")
elif status_str == "error":
logger.error("There were errors in your submission")
logger.info("Check the file %s for details", retrieve_response_path)
logger.info("Will now update local information from response...")
_retrieve_store_response(name, status_result)
logger.info("... done updating local information from response")
else:
logger.error("Status is %s and clinvar-this does not know how to handle this yet!")
raise exceptions.ClinvarThisException(f"Unknown status {status_str}")