annotate cravat_convert/cravat_convert.py @ 16:a9944bb8a9b8 draft

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