annotate SNP_Mapping.py @ 32:11da66cb7216 draft

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