Skip to content

Commit

Permalink
feat: add support for SV TSV files (#75) (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Mar 15, 2023
1 parent 6f26d6b commit d4fffce
Show file tree
Hide file tree
Showing 8 changed files with 636 additions and 107 deletions.
44 changes: 30 additions & 14 deletions clinvar_this/batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,26 @@ def import_(config: config.Config, name: str, path: str, metadata: typing.Tuple[
logger.info("Creating new payload only")
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,
)
tsv_type = tsv.guess_tsv_type(path)
if tsv_type in (tsv.TsvType.SEQ_VAR, tsv.TsvType.STRUC_VAR):
batch_metadata = tsv.batch_metadata_from_mapping(metadata, use_defaults=True)
if tsv_type == tsv.TsvType.SEQ_VAR:
new_submission_container = tsv.seq_var_tsv_records_to_submission_container(
tsv.read_seq_var_tsv(path=path), batch_metadata
)
else: # tsv_type == tsv.TsvType.STRUC_VAR
new_submission_container = tsv.struc_var_tsv_records_to_submission_container(
tsv.read_struc_var_tsv(path=path), 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
else:
submission_container = new_submission_container
raise exceptions.IOException(f"Could not guess TSV file type from header for {path}")
_write_payload(submission_container, config.profile, name)
else: # pragma: no cover
raise exceptions.IOException(f"File extension of {path} cannot be handled.")
Expand All @@ -152,16 +160,24 @@ def _load_latest_payload(profile: str, name: str):
return common.CONVERTER.structure(payload_unstructured, models.SubmissionContainer)


def export(config: config.Config, name: str, path: str, force: bool = False):
def export(
config: config.Config, name: str, path: str, force: bool = False, struc_var: 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(config.profile, name)
tsv_records = tsv.submission_container_to_tsv_records(payload)
tsv.write_tsv(tsv_records, path=path)
if struc_var:
tsv.write_struc_var_tsv(
tsv_records=tsv.submission_container_to_struc_var_tsv_records(payload), path=path
)
else:
tsv.write_seq_var_tsv(
tsv_records=tsv.submission_container_to_seq_var_tsv_records(payload), path=path
)
else: # pragma: no cover
raise exceptions.IOException(f"File extension of {path} cannot be handled.")

Expand Down
9 changes: 8 additions & 1 deletion clinvar_this/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,23 @@ def batch_import(
@click.argument("name")
@click.argument("path")
@click.option("--force/--no-force", required=False, default=False, help="Overwrite existing files")
@click.option(
"--struc-var/--no-struc-var",
required=False,
default=False,
help="Export structural variants rather than sequence variants",
)
@click.pass_context
def batch_export(
ctx: click.Context,
name: str,
path: str,
force: bool = False,
struc_var: bool = False,
):
"""Export batch data to a given file"""
config_obj = load_config(ctx.obj["profile"])
batches.export(config_obj, name, path, force)
batches.export(config_obj, name, path, force, struc_var)


@batch.command("update-metadata")
Expand Down
Loading

0 comments on commit d4fffce

Please sign in to comment.