def output(density, up, down, extra, frag_size, n_tss, n_reads): smooth_filter_size = 101 shift = int(frag_size / 2) pos = range(-up, down + 1) n = len(density["left"]) - 2 * extra - 1 left = (density["left"].astype(float) / n_reads) * 1e9 / n_tss left_smooth = dsp.savitzky_golay_filter(left, smooth_filter_size, order = 4) print "\n".join("{0}|{1}|{2}|left".format(a, b, c) for a, b, c in zip(pos[extra:(extra + n)], left[extra:(extra + n)], left_smooth[extra:(extra + n)])) right = (density["right"].astype(float) / n_reads) * 1e9 / n_tss right_smooth = dsp.savitzky_golay_filter(right, smooth_filter_size, order = 4) print "\n".join("{0}|{1}|{2}|right".format(a, b, c) for a, b, c in zip(pos[extra:(extra + n)], right[extra:(extra + n)], right_smooth[extra:(extra + n)])) combined = numpy.zeros(len(left), dtype = float) cs = extra + 1 left_start = extra - frag_size // 2 + 1 combined[cs:(cs + n)] += left[left_start:(left_start + n)] right_start = extra + frag_size // 2 + 1 combined[cs:(cs + n)] += right[right_start:(right_start + n)] combined_smooth = dsp.savitzky_golay_filter(combined, smooth_filter_size, order = 4) print "\n".join("{0}|{1}|{2}|combined".format(a, b, c) for a, b, c in zip(pos[extra:(extra + n)], combined[extra:(extra + n)], combined_smooth[extra:(extra + n)]))
def smooth(bins, window_size, by_strand): for chrom in bins: if not by_strand: bins[chrom] = dsp.savitzky_golay_filter(bins[chrom], window_size, order = 2, deriv = 0) else: bins[chrom][0] = dsp.savitzky_golay_filter(bins[chrom][0], window_size, order = 2, deriv = 0) bins[chrom][1] = dsp.savitzky_golay_filter(bins[chrom][1], window_size, order = 2, deriv = 0)