annotate cravat_convert/cravat_convert.py @ 26:9d8c12fa6888 draft default tip

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