comparison cravat_convert/cravat_convert.py @ 16:a9944bb8a9b8 draft

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