annotate SNP_Mapping.py @ 10:7d6bccd5f88c draft

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