annotate SNP_Mapping.py @ 12:9c28b8aebe84 draft

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