Mercurial > repos > in_silico > cravat_vcf_convert
comparison cravat_convert/cravat_convert.py @ 20:a0b39b857c7a draft
Uploaded
author | in_silico |
---|---|
date | Wed, 18 Jul 2018 10:17:38 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
19:5de40a6dd491 | 20:a0b39b857c7a |
---|---|
1 ''' | |
2 Convert a VCF format file to Cravat format file | |
3 ''' | |
4 | |
5 import os | |
6 import argparse | |
7 from vcf_converter import CravatConverter | |
8 from __future__ import print_function | |
9 | |
10 def get_vcf_mapping(): | |
11 """ : VCF Headers mapped to their index position in a row of VCF values. | |
12 : These are only the mandatory columns, per the VCF spec. | |
13 """ | |
14 return { | |
15 'CHROM': 0, | |
16 'POS': 1, | |
17 'ID': 2, | |
18 'REF': 3, | |
19 'ALT': 4, | |
20 'QUAL': 5, | |
21 'FILTER': 6, | |
22 'INFO': 7 | |
23 } | |
24 | |
25 | |
26 def get_args(): | |
27 parser = argparse.ArgumentParser() | |
28 parser.add_argument('--input', | |
29 '-i', | |
30 required = True, | |
31 help='Input path to a VCF file for conversion',) | |
32 parser.add_argument('--output', | |
33 '-o', | |
34 default = None, | |
35 help = 'Output path to write the cravat file to') | |
36 return parser.parse_args() | |
37 | |
38 | |
39 def convert(in_path, out_path=None, cr_sep='\t', cr_newline='\n'): | |
40 """ : Convert a VCF file to a Cravat file. | |
41 : Arguments: | |
42 : in_path: <str> path to input vcf file | |
43 : out_path: <str> path to output cravat file. Will defualt to cravat_converted.txt in the input directory. | |
44 : cr_sep: <str> the value delimiter for the output cravat file. Default value of '\\t'. | |
45 : out_newline: <str> the newline delimiter in the output cravat file. Default of '\\n' | |
46 """ | |
47 if not out_path: | |
48 base, _ = os.path.split(in_path) | |
49 out_path = os.path.join(base, "cravat_converted.txt") | |
50 | |
51 with open(in_path, 'r') as in_file, \ | |
52 open(out_path, 'w') as out_file: | |
53 | |
54 # cr_count will be used to generate the 'TR' field of the cravat rows (first header) | |
55 cr_count = 0 | |
56 # VCF lines are always assumed to be '+' strand, as VCF doesn't specify that attribute | |
57 strand = '+' | |
58 # VCF converter. Adjusts position, reference, and alternate for Cravat formatting. | |
59 converter = CravatConverter() | |
60 # A dictionary of mandatory vcf headers mapped to their row indices | |
61 vcf_mapping = get_vcf_mapping() | |
62 | |
63 for line in in_file: | |
64 if line.startswith("#"): | |
65 continue | |
66 line = line.strip().split() | |
67 # row is dict of VCF headers mapped to corresponding values of this line | |
68 row = { header: line[index] for header, index in vcf_mapping.items() } | |
69 for alt in row["ALT"].split(","): | |
70 new_pos, new_ref, new_alt = converter.extract_vcf_variant(strand, row["POS"], row["REF"], alt) | |
71 new_pos, new_ref, new_alt = str(new_pos), str(new_ref), str(new_alt) | |
72 cr_line = cr_sep.join([ | |
73 'TR' + str(cr_count), row['CHROM'], new_pos, strand, new_ref, new_alt, row['ID'] | |
74 ]) | |
75 out_file.write(cr_line + cr_newline) | |
76 cr_count += 1 | |
77 | |
78 | |
79 if __name__ == "__main__": | |
80 cli_args = get_args() | |
81 if cli_args.output == None: | |
82 base, _ = os.path.split(cli_args.input) | |
83 cli_args.output = os.path.join(base, "cravat_converted.txt") | |
84 convert(in_path = cli_args.input, out_path = cli_args.output) |