annotate cravat_convert/cravat_convert.py @ 12:4b860b1f92fa draft

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