annotate SNP_Mapping.py @ 20:98d409af683c draft

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