annotate SNP_Mapping.py @ 26:e195dcd19bc5 draft

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