annotate VDM_Mapping.py @ 0:eebca11ac82e draft

Uploaded
author gregory-minevich
date Mon, 08 Oct 2012 13:49:36 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
1 #!/usr/bin/python
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
2
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
3 import re
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
4 import sys
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
5 import optparse
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
6 import csv
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
7 import re
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
8 import pprint
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
9 from decimal import *
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
10 from rpy import *
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
11
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
12 def main():
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
13 csv.field_size_limit(1000000000)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
14
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
15 parser = optparse.OptionParser()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
16 parser.add_option('-v', '--sample_vcf', dest = 'sample_vcf', action = 'store', type = 'string', default = None, help = "Sample VCF from GATK Unified Genotyper")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
17 parser.add_option('-l', '--loess_span', dest = 'loess_span', action = 'store', type = 'float', default = .01, help = "Loess span")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
18 parser.add_option('-d', '--d_yaxis', dest = 'd_yaxis', action = 'store', type = 'float', default = .7, help = "y-axis upper limit for dot plot")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
19 parser.add_option('-y', '--h_yaxis', dest = 'h_yaxis', action = 'store', type = 'int', default = 5, help = "y-axis upper limit for histogram plot")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
20 parser.add_option('-c', '--points_color', dest = 'points_color', action = 'store', type = 'string', default = "gray27", help = "Color for data points")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
21 parser.add_option('-k', '--loess_color', dest = 'loess_color', action = 'store', type = 'string', default = "red", help = "Color for loess regression line")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
22 parser.add_option('-z', '--standardize', dest = 'standardize', default= 'true', help = "Standardize X-axis")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
23 parser.add_option('-b', '--break_file', dest = 'break_file', action = 'store', type = 'string', default = 'C.elegans', help = "File defining the breaks per chromosome")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
24 parser.add_option('-x', '--bin_size', dest = 'bin_size', action = 'store', type = 'int', default = 1000000, help = "Size of histogram bins, default is 1mb")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
25 parser.add_option('-n', '--normalize_bins', dest = 'normalize_bins', default= 'true', help = "Normalize histograms")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
26
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
27
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
28 parser.add_option('-o', '--output', dest = 'output', action = 'store', type = 'string', default = None, help = "Output file name")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
29 parser.add_option('-s', '--location_plot_output', dest = 'location_plot_output', action = 'store', type = 'string', default = "SNP_Mapping_Plot.pdf", help = "Output file name of SNP plots by chromosomal location")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
30
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
31 (options, args) = parser.parse_args()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
32
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
33 vcf_info = parse_vcf(sample_vcf = options.sample_vcf)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
34
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
35 output_vcf_info(output = options.output, vcf_info = vcf_info)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
36
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
37 #output plot with all ratios
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
38 rounded_bin_size = int(round((float(options.bin_size) / 1000000), 1) * 1000000)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
39
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
40 normalized_histogram_bins_per_mb = calculate_normalized_histogram_bins_per_xbase(vcf_info = vcf_info, xbase = rounded_bin_size, normalize_bins = options.normalize_bins)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
41 normalized_histogram_bins_per_5kb = calculate_normalized_histogram_bins_per_xbase(vcf_info = vcf_info, xbase = (rounded_bin_size / 2), normalize_bins = options.normalize_bins)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
42
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
43 break_dict = parse_breaks(break_file = options.break_file)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
44
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
45 output_scatter_plots_by_location(location_plot_output = options.location_plot_output, vcf_info = vcf_info, loess_span=options.loess_span, d_yaxis=options.d_yaxis, h_yaxis=options.h_yaxis, points_color=options.points_color, loess_color=options.loess_color, standardize =options.standardize, normalized_hist_per_mb = normalized_histogram_bins_per_mb, normalized_hist_per_5kb = normalized_histogram_bins_per_5kb, breaks = break_dict, rounded_bin_size = rounded_bin_size)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
46
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
47
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
48 def skip_headers(reader = None, i_file = None):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
49 # count headers
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
50 comment = 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
51 while reader.next()[0].startswith('#'):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
52 comment = comment + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
53
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
54 # skip headers
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
55 i_file.seek(0)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
56 for i in range(0, comment):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
57 reader.next()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
58
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
59 def parse_breaks(break_file = None):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
60 if break_file == 'C.elegans':
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
61 break_dict = { 'I' : 16 , 'II' : 16, 'III' : 14, 'IV' : 18, 'V' : 21, 'X' : 18 }
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
62 return break_dict
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
63 elif break_file == 'Arabadopsis':
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
64 break_dict = { '1' : 31 , '2' : 20, '3' : 24, '4' : 19, '5' : 27 }
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
65 return break_dict
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
66 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
67 i_file = open(break_file, 'rU')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
68 break_dict = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
69 reader = csv.reader(i_file, delimiter = '\t')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
70 for row in reader:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
71 chromosome = row[0].upper()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
72 chromosome = re.sub("chr", "", chromosome, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
73 chromosome = re.sub("CHROMOSOME_", "", chromosome, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
74 break_count = row[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
75 break_dict[chromosome] = int(break_count)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
76 return break_dict
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
77
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
78
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
79 def location_comparer(location_1, location_2):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
80 chr_loc_1 = location_1.split(':')[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
81 pos_loc_1 = int(location_1.split(':')[1])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
82
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
83 chr_loc_2 = location_2.split(':')[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
84 pos_loc_2 = int(location_2.split(':')[1])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
85
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
86 if chr_loc_1 == chr_loc_2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
87 if pos_loc_1 < pos_loc_2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
88 return -1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
89 elif pos_loc_1 == pos_loc_1:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
90 return 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
91 elif pos_loc_1 > pos_loc_2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
92 return 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
93 elif chr_loc_1 < chr_loc_2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
94 return -1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
95 elif chr_loc_1 > chr_loc_2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
96 return 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
97
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
98 def output_vcf_info(output = None, vcf_info = None):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
99 o_file = open(output, 'wb')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
100 writer = csv.writer(o_file, delimiter = '\t')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
101
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
102 writer.writerow(["#Chr\t", "Pos\t", "Alt Count\t", "Ref Count\t", "Read Depth\t", "Ratio\t"])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
103
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
104 location_sorted_vcf_info_keys = sorted(vcf_info.keys(), cmp=location_comparer)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
105
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
106 for location in location_sorted_vcf_info_keys:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
107 alt_allele_count, ref_allele_count, read_depth, ratio = vcf_info[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
108
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
109 location_info = location.split(':')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
110 chromosome = location_info[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
111 position = location_info[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
112
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
113 writer.writerow([chromosome, position, alt_allele_count, ref_allele_count, read_depth, ratio])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
114
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
115 o_file.close()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
116
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
117 def output_scatter_plots_by_location(location_plot_output = None, vcf_info = None, loess_span="", d_yaxis="", h_yaxis="", points_color="", loess_color="", standardize=None, normalized_hist_per_mb = None, normalized_hist_per_5kb = None, breaks = None, rounded_bin_size = 1000000):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
118 positions = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
119 current_chr = ""
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
120 prev_chr = ""
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
121
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
122 x_label = "Location (Mb)"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
123 filtered_label = ''
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
124
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
125 location_sorted_vcf_info_keys = sorted(vcf_info.keys(), cmp=location_comparer)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
126
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
127 break_unit = Decimal(rounded_bin_size) / Decimal(1000000)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
128 max_breaks = max(breaks.values())
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
129
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
130 try:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
131 r.pdf(location_plot_output, 8, 8)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
132
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
133 for location in location_sorted_vcf_info_keys:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
134 current_chr = location.split(':')[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
135 position = location.split(':')[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
136
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
137 alt_allele_count, ref_allele_count, read_depth, ratio = vcf_info[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
138
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
139 if prev_chr != current_chr:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
140 if prev_chr != "":
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
141 hist_dict_mb = get_hist_dict_by_chr(normalized_hist_per_xbase = normalized_hist_per_mb, chr = prev_chr)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
142 hist_dict_5kb = get_hist_dict_by_chr(normalized_hist_per_xbase = normalized_hist_per_5kb, chr = prev_chr)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
143
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
144 plot_data(chr_dict = positions, hist_dict_mb = hist_dict_mb, hist_dict_5kb = hist_dict_5kb, chr = prev_chr + filtered_label, x_label = "Location (Mb)", divide_position = True, draw_secondary_grid_lines = True, loess_span=loess_span, d_yaxis=d_yaxis, h_yaxis=h_yaxis, points_color=points_color, loess_color=loess_color, breaks = breaks[prev_chr], standardize=standardize, max_breaks = max_breaks, break_unit = break_unit)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
145
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
146 prev_chr = current_chr
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
147 positions = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
148
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
149 positions[position] = ratio
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
150
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
151 hist_dict_mb = get_hist_dict_by_chr(normalized_hist_per_xbase = normalized_hist_per_mb, chr = current_chr)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
152 hist_dict_5kb = get_hist_dict_by_chr(normalized_hist_per_xbase = normalized_hist_per_5kb, chr = current_chr)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
153
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
154 plot_data(chr_dict = positions, hist_dict_mb = hist_dict_mb, hist_dict_5kb = hist_dict_5kb, chr = current_chr + filtered_label, x_label = "Location (Mb)", divide_position = True, draw_secondary_grid_lines = True, loess_span=loess_span, d_yaxis=d_yaxis, h_yaxis=h_yaxis, points_color=points_color, loess_color=loess_color, breaks = breaks[current_chr], standardize=standardize, max_breaks = max_breaks, break_unit = break_unit)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
155
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
156 r.dev_off()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
157
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
158 except Exception as inst:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
159 print inst
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
160 print "There was an error creating the location plot pdf... Please try again"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
161
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
162 def get_hist_dict_by_chr(normalized_hist_per_xbase = None, chr = ''):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
163 hist_dict = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
164
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
165 for location in normalized_hist_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
166 chromosome = location.split(':')[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
167 if chromosome == chr:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
168 position = int(location.split(':')[1])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
169 hist_dict[position] = normalized_hist_per_xbase[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
170
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
171 max_location = max(hist_dict.keys(), key=int)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
172 for i in range(1, max_location):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
173 if i not in hist_dict:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
174 hist_dict[i] = 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
175
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
176 return hist_dict
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
177
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
178 def plot_data(chr_dict = None, hist_dict_mb = None, hist_dict_5kb = None, chr = "", x_label = "", divide_position = False, draw_secondary_grid_lines = False, loess_span=None, d_yaxis=None, h_yaxis=None, points_color="", loess_color="", breaks = None, standardize= None, max_breaks = 1, break_unit = 1):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
179 ratios = "c("
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
180 positions = "c("
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
181
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
182 for position in chr_dict:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
183 ratio = chr_dict[position]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
184 if divide_position:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
185 position = float(position) / 1000000.0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
186 positions = positions + str(position) + ", "
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
187 ratios = ratios + str(ratio) + ", "
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
188
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
189 if len(ratios) == 2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
190 ratios = ratios + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
191 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
192 ratios = ratios[0:len(ratios) - 2] + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
193
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
194 if len(positions) == 2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
195 positions = positions + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
196 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
197 positions = positions[0:len(positions) - 2] + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
198
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
199 r("x <- " + positions)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
200 r("y <- " + ratios)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
201
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
202 hist_mb_values = "c("
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
203 for position in sorted(hist_dict_mb):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
204 hist_mb_values = hist_mb_values + str(hist_dict_mb[position]) + ", "
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
205
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
206 if len(hist_mb_values) == 2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
207 hist_mb_values = hist_mb_values + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
208 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
209 hist_mb_values = hist_mb_values[0:len(hist_mb_values) - 2] + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
210
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
211 hist_5kb_values = "c("
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
212 for position in sorted(hist_dict_5kb):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
213 hist_5kb_values = hist_5kb_values + str(hist_dict_5kb[position]) + ", "
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
214
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
215 if len(hist_5kb_values) == 2:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
216 hist_5kb_values = hist_5kb_values + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
217 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
218 hist_5kb_values = hist_5kb_values[0:len(hist_5kb_values) - 2] + ")"
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
219
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
220 r("xz <- " + hist_mb_values)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
221 r("yz <- " + hist_5kb_values)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
222
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
223
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
224 max_break_str = str(max_breaks)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
225 break_unit_str = str(Decimal(break_unit))
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
226 half_break_unit_str = str(Decimal(break_unit) / Decimal(2))
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
227 break_penta_unit_str = str(Decimal(break_unit) * Decimal(5))
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
228
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
229 if (standardize=='true'):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
230 r("plot(x, y, ,cex=0.60, xlim=c(0," + max_break_str + "), main='LG " + chr + "', xlab= '" + x_label + "', ylim = c(0, %f " %d_yaxis + "), ylab='Ratios of mapping strain alleles/total reads (at SNP positions)', pch=18, col='"+ points_color +"')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
231 r("lines(loess.smooth(x, y, span = %f "%loess_span + "), lwd=5, col='"+ loess_color +"')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
232 r("axis(1, at=seq(0, " + max_break_str + ", by=" + break_unit_str + "), labels=FALSE, tcl=-0.5)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
233 r("axis(1, at=seq(0, " + max_break_str + ", by=" + half_break_unit_str + "), labels=FALSE, tcl=-0.25)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
234 r("axis(2, at=seq(floor(min(y)), 1, by=0.1), labels=FALSE, tcl=-0.2)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
235 elif (standardize=='false'):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
236 r("plot(x, y, cex=0.60, main='LG " + chr + "', xlab= '" + x_label + "', ylim = c(0, %f " %d_yaxis + "), ylab='Ratios of mapping strain alleles/total reads (at SNP positions)', pch=18, col='"+ points_color +"')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
237 r("lines(loess.smooth(x, y, span = %f "%loess_span + "), lwd=5, col='"+ loess_color +"')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
238 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by= " + break_unit_str + "), labels=FALSE, tcl=-0.5)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
239 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by= " + half_break_unit_str + "), labels=FALSE, tcl=-0.25)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
240 r("axis(2, at=seq(floor(min(y)), 1, by=0.1), labels=FALSE, tcl=-0.2)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
241
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
242 if draw_secondary_grid_lines:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
243 r("abline(h = seq(floor(min(y)), 1, by=0.1), v = seq(floor(min(x)), length(x), by= 1), col='gray')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
244 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
245 r("grid(lty = 1, col = 'gray')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
246
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
247 if (standardize=='true'):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
248 r("barplot(xz, xlim=c(0, " + max_break_str + "), ylim = c(0, " + str(h_yaxis) + "), yaxp=c(0, " + str(h_yaxis) + ", 1), space = 0, col='darkgray', width = " + break_unit_str + ", xlab='Location (Mb)', ylab='Normalized frequency of pure parental alleles ', main='LG " + chr + "')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
249 r("barplot(yz, space = 0, add=TRUE, width = " + half_break_unit_str + ", col=rgb(1, 0, 0, 1))")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
250 r("axis(1, hadj = 1, at=seq(0, " + max_break_str + ", by= " + break_unit_str + "), labels=FALSE, tcl=-0.5)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
251 r("axis(1, at=seq(0, " + max_break_str + ", by= " + break_penta_unit_str + "), labels=TRUE, tcl=-0.5)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
252 r("axis(1, at=seq(0, " + max_break_str + ", by= " + half_break_unit_str + "), labels=FALSE, tcl=-0.25)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
253 elif (standardize=='false'):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
254 r("barplot(xz, ylim = c(0, " + str(h_yaxis) + "), yaxp=c(0, " + str(h_yaxis) + ", 1), space = 0, col='darkgray', width = 1, xlab='Location (Mb)', ylab='Normalized frequency of pure parental alleles ', main='LG " + chr + "')")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
255 r("barplot(yz, space = 0, add=TRUE, width = 0.5, col=rgb(1, 0, 0, 1))")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
256 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by= " + break_unit_str + "), labels=FALSE, tcl=-0.5)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
257 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by= " + break_penta_unit_str + "), labels=TRUE, tcl=-0.5)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
258 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by= " + half_break_unit_str + "), labels=FALSE, tcl=-0.25)")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
259
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
260
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
261
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
262 def calculate_normalized_histogram_bins_per_xbase(vcf_info = None, xbase = 1000000, normalize_bins = None):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
263 normalized_histogram_bins_per_xbase = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
264
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
265 ref_snp_count_per_xbase = get_ref_snp_count_per_xbase(vcf_info = vcf_info, xbase = xbase)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
266 mean_one_ratio_snp_count_per_chromosome = get_mean_one_ratio_snp_count_per_chromosome(vcf_info = vcf_info, xbase = xbase)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
267 one_ratio_snp_count_per_xbase = get_one_ratio_snp_count_per_xbase(vcf_info = vcf_info, xbase = xbase)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
268
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
269 for location in ref_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
270 chromosome = location.split(':')[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
271 mean_one_ratio_snp_count = mean_one_ratio_snp_count_per_chromosome[chromosome]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
272 ref_snp_count = ref_snp_count_per_xbase[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
273
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
274 one_ratio_snp_count = 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
275 if location in one_ratio_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
276 one_ratio_snp_count = one_ratio_snp_count_per_xbase[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
277
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
278 if normalize_bins == 'true':
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
279 if one_ratio_snp_count == 0:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
280 normalized_histogram_bins_per_xbase[location] = 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
281 elif one_ratio_snp_count == ref_snp_count:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
282 normalized_histogram_bins_per_xbase[location] = (Decimal(one_ratio_snp_count**2) * Decimal(mean_one_ratio_snp_count))
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
283 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
284 normalized_histogram_bins_per_xbase[location] = (Decimal(one_ratio_snp_count**2) / (Decimal(ref_snp_count)-Decimal(one_ratio_snp_count))) * Decimal(mean_one_ratio_snp_count)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
285 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
286 normalized_histogram_bins_per_xbase[location] = one_ratio_snp_count
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
287
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
288 return normalized_histogram_bins_per_xbase
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
289
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
290
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
291 def get_ref_snp_count_per_xbase(vcf_info = None, xbase = 1000000):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
292 ref_snps_per_xbase = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
293
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
294 for location in vcf_info:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
295 location_info = location.split(':')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
296
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
297 chromosome = location_info[0].upper()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
298 chromosome = re.sub("chr", "", chromosome, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
299 chromosome = re.sub("CHROMOSOME_", "", chromosome, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
300
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
301 position = location_info[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
302 xbase_position = (int(position) / xbase) + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
303
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
304 location = chromosome + ":" + str(xbase_position)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
305 if location in ref_snps_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
306 ref_snps_per_xbase[location] = ref_snps_per_xbase[location] + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
307 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
308 ref_snps_per_xbase[location] = 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
309
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
310 return ref_snps_per_xbase
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
311
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
312
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
313
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
314 def get_mean_one_ratio_snp_count_per_chromosome(vcf_info, xbase = 1000000):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
315 sample_snp_count_per_xbase = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
316
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
317 for location in vcf_info:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
318 alt_allele_count, ref_allele_count, read_depth, ratio = vcf_info[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
319
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
320 location_info = location.split(':')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
321 chromosome = location_info[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
322 position = location_info[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
323 xbase_position = (int(position) / xbase) + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
324 xbase_location = chromosome + ":" + str(xbase_position)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
325
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
326 if ratio == 1:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
327 if xbase_location in sample_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
328 sample_snp_count_per_xbase[xbase_location] = sample_snp_count_per_xbase[xbase_location] + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
329 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
330 sample_snp_count_per_xbase[xbase_location] = 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
331
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
332 elif ratio != 1 and xbase_location not in sample_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
333 sample_snp_count_per_xbase[xbase_location] = 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
334
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
335 mean_one_ratio_snp_count_per_chromosome = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
336 for location in sample_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
337 chromosome = location.split(':')[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
338 sample_count = sample_snp_count_per_xbase[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
339 if chromosome in mean_one_ratio_snp_count_per_chromosome:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
340 mean_one_ratio_snp_count_per_chromosome[chromosome].append(sample_count)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
341 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
342 mean_one_ratio_snp_count_per_chromosome[chromosome] = [sample_count]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
343
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
344 for chromosome in mean_one_ratio_snp_count_per_chromosome:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
345 summa = sum(mean_one_ratio_snp_count_per_chromosome[chromosome])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
346 count = len(mean_one_ratio_snp_count_per_chromosome[chromosome])
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
347
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
348 mean_one_ratio_snp_count_per_chromosome[chromosome] = Decimal(summa) / Decimal(count)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
349
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
350 return mean_one_ratio_snp_count_per_chromosome
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
351
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
352
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
353 def get_one_ratio_snp_count_per_xbase(vcf_info = None, xbase = 1000000):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
354 one_ratio_snp_count_per_xbase = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
355
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
356 for location in vcf_info:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
357 alt_allele_count, ref_allele_count, read_depth, ratio = vcf_info[location]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
358
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
359 location_info = location.split(':')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
360 chromosome = location_info[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
361 position = location_info[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
362 xbase_position = (int(position) / xbase) + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
363 xbase_location = chromosome + ":" + str(xbase_position)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
364
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
365 if ratio == 1:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
366 if xbase_location in one_ratio_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
367 one_ratio_snp_count_per_xbase[xbase_location] = one_ratio_snp_count_per_xbase[xbase_location] + 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
368 else:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
369 one_ratio_snp_count_per_xbase[xbase_location] = 1
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
370
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
371 elif ratio != 1 and xbase_location not in one_ratio_snp_count_per_xbase:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
372 one_ratio_snp_count_per_xbase[xbase_location] = 0
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
373
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
374 return one_ratio_snp_count_per_xbase
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
375
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
376
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
377 def parse_vcf(sample_vcf = None):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
378 i_file = open(sample_vcf, 'rU')
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
379 reader = csv.reader(i_file, delimiter = '\t', quoting = csv.QUOTE_NONE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
380
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
381 skip_headers(reader = reader, i_file = i_file)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
382 vcf_info = {}
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
383
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
384 for row in reader:
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
385 chromosome = row[0].upper()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
386 chromosome = re.sub("chr", "", chromosome, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
387 chromosome = re.sub("CHROMOSOME_", "", chromosome, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
388
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
389 if chromosome != 'MTDNA':
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
390 position = row[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
391 #ref_allele = row[2]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
392 #read_depth = row[3]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
393 #read_bases = row[4]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
394
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
395 vcf_format_info = row[8].split(":")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
396 vcf_allele_freq_data = row[9]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
397
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
398 read_depth_data_index = vcf_format_info.index("DP")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
399 read_depth = vcf_allele_freq_data.split(":")[read_depth_data_index]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
400
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
401 ref_and_alt_counts_data_index = vcf_format_info.index("AD")
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
402 ref_and_alt_counts = vcf_allele_freq_data.split(":")[ref_and_alt_counts_data_index]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
403 ref_allele_count = ref_and_alt_counts.split(",")[0]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
404 alt_allele_count = ref_and_alt_counts.split(",")[1]
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
405
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
406 location = chromosome + ":" + position
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
407
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
408 if Decimal(read_depth!=0):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
409 getcontext().prec = 6
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
410 ratio = Decimal(alt_allele_count) / Decimal(read_depth)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
411
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
412 vcf_info[location] = (alt_allele_count, ref_allele_count, read_depth, ratio)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
413
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
414 #debug line
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
415 #print chromosome, position, read_depth, ref_allele_count, alt_allele_count, ratio, id
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
416
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
417 i_file.close()
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
418
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
419 return vcf_info
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
420
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
421 def parse_read_bases(read_bases = None, alt_allele = None):
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
422 read_bases = re.sub('\$', '', read_bases)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
423 read_bases = re.sub('\^[^\s]', '', read_bases)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
424
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
425 ref_allele_matches = re.findall("\.|\,", read_bases)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
426 ref_allele_count = len(ref_allele_matches)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
427
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
428 alt_allele_matches = re.findall(alt_allele, read_bases, flags = re.IGNORECASE)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
429 alt_allele_count = len(alt_allele_matches)
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
430
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
431 #debug line
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
432 #print read_bases, alt_allele, alt_allele_count, ref_allele_count
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
433
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
434 return ref_allele_count, alt_allele_count
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
435
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
436 if __name__ == "__main__":
eebca11ac82e Uploaded
gregory-minevich
parents:
diff changeset
437 main()