annotate cravat_convert/cravat_convert.py @ 5:7048ccf0ff7b draft

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