Created
March 16, 2021 18:55
-
-
Save nickloman/75a0ba5ea393b2911774df3f04b3b5e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse as ap | |
import sys | |
import vcf | |
import yaml | |
VERSION = 0.1 | |
def filter_variants(yaml_variants): | |
variants = [] | |
for v in yaml_variants: | |
if v['type'] != 'deletion' and v['type'] != 'insertion': | |
variants.append(v) | |
return variants | |
def go(args): | |
vcf_reader = vcf.Reader('template.vcf') | |
vcf_writer = vcf.Writer(open(args.output, 'w'), vcf_reader) | |
y = yaml.safe_load(open(args.yaml)) | |
variants = filter_variants(y['variants']) | |
for row in variants: | |
record = vcf.model._Record('NC_045512', | |
int(row['one-based-reference-position']), | |
'.', | |
row['reference-base'], | |
[vcf.model._Substitution(row['variant-base'])], | |
'.', | |
'PASS', | |
{}, | |
'.', sample_indexes=[]) | |
vcf_writer.write_record(record) | |
if __name__ == '__main__': | |
parser = ap.ArgumentParser( | |
prog='csv-to-vcf.py', | |
conflict_handler='resolve', | |
description=("Produce basic VCF file from a CSV file.") | |
) | |
parser.add_argument('yaml', metavar="YAML_FILE", type=str, | |
help='variant definition file') | |
parser.add_argument('--output', metavar="STRING", type=str, | |
default='/dev/stdout', | |
help='File to write VCF output to (Default STDOUT).') | |
parser.add_argument('--version', action='version', | |
version='%(prog)s {0}'.format(VERSION)) | |
if len(sys.argv) == 1: | |
parser.print_help() | |
sys.exit(0) | |
args = parser.parse_args() | |
vars = go(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment