annotate cravat_convert/cravat_convert.py @ 24:e46db72f5b74 draft

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