|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Fix GLNexus output for input to `mehari annotate seqvars`. |
| 4 | +
|
| 5 | +First, note that multiallelic sites must be corrected by a call to |
| 6 | +`bcftools norm` already as an input to this script. |
| 7 | +
|
| 8 | +What we will do is splitting the `FORMAT/RNC` field with a comma |
| 9 | +as noodles-vcf expects a list of characters for rather than a |
| 10 | +string of length 2 for `##FORMAT=<ID=RNC,Number=2,Type=Character>`. |
| 11 | +""" |
| 12 | + |
| 13 | +from pathlib import Path |
| 14 | +import sys |
| 15 | +from typing import Annotated |
| 16 | + |
| 17 | +import typer |
| 18 | +import vcfpy |
| 19 | + |
| 20 | + |
| 21 | +def main( |
| 22 | + path_in: Annotated[Path, typer.Option(help="Path to input VCF")], |
| 23 | + path_out: Annotated[Path, typer.Option(help="Path to output VCF")], |
| 24 | + quiet: Annotated[bool, typer.Option(help="Disable verbose output")] = False, |
| 25 | +): |
| 26 | + """ |
| 27 | + Fix GLNexus output to be compatible with `noodles-vcf`. |
| 28 | +
|
| 29 | + cf: https://github.com/varfish-org/mehari/issues/356 |
| 30 | + """ |
| 31 | + if not quiet: |
| 32 | + print(f"Opening input file {path_in}", file=sys.stderr) |
| 33 | + reader = vcfpy.Reader.from_path(path_in) |
| 34 | + header = reader.header.copy() |
| 35 | + for line in header.lines: |
| 36 | + if line.key == "FORMAT" and line.mapping.get("ID") == "GQ": |
| 37 | + line.mapping["Number"] = 1 |
| 38 | + line.mapping["Type"] = "Integer" |
| 39 | + if not quiet: |
| 40 | + print(f"Opening output file {path_out}", file=sys.stderr) |
| 41 | + writer = vcfpy.Writer.from_path(path_out, header) |
| 42 | + if not quiet: |
| 43 | + print("Processing records...", file=sys.stderr) |
| 44 | + with reader, writer: |
| 45 | + for idx, record in enumerate(reader): |
| 46 | + if idx % 10_000 == 0: |
| 47 | + print( |
| 48 | + f" at {idx} records {record.CHROM}:{record.POS}", file=sys.stderr |
| 49 | + ) |
| 50 | + for call in record.calls: |
| 51 | + if "RNC" in call.data: |
| 52 | + if ( |
| 53 | + isinstance(call.data["RNC"], list) |
| 54 | + and len(call.data["RNC"]) == 1 |
| 55 | + ): |
| 56 | + call.data["RNC"] = list(call.data["RNC"][0]) |
| 57 | + writer.write_record(record) |
| 58 | + if not quiet: |
| 59 | + print("... done", file=sys.stderr) |
| 60 | + print("All done. Have a nice day!", file=sys.stderr) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + typer.run(main) |
0 commit comments