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