annotate cravat_convert/cravat_convert.py @ 20:a0b39b857c7a draft

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