annotate cravat_convert/cravat_convert.py @ 25:c5f0a7e538ff draft

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