annotate cravat_convert/cravat_convert.py @ 18:cde9e74d9fdf draft

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