from scipy import optimize
	def fitting_f(xvalues, *params):
		yvalues = []
		for x in xvalues:
			y = 1/(1+ 2.718281**(params[1]*x+params[0]))
			yvalues.append(y)
		return numpy.array(yvalues)
	X,Y = [], []
	for cellsource in xarr:
		X.extend(xarr[cellsource])
		Y.extend(yarr[cellsource])
		print cellsource, 'n=', len(xarr[cellsource])
	print 'spearman', stats.spearmanr(X, Y)
	coefficients_p, pcov = optimize.curve_fit(fitting_f, X, Y, [0,-1])
	print 'line fit coefficients', coefficients_p
	x_line = numpy.arange(math.floor(min(X)), math.ceil(max(X)), 0.1)
	pylab.plot(x_line, fitting_f(x_line, *coefficients_p), 'k-')
	if o.lineparams is not None:
		pylab.plot(x_line, fitting_f(x_line, *o.lineparams), 'g-')
	
	# main plotting
	import random; random.seed(12)
	for cellsource in xarr:
		pylab.plot(xarr[cellsource], yarr[cellsource], 'o', label=cellsource, fillstyle='none', markeredgecolor=dr_tools.randomcolour(), markersize=5, markeredgewidth=2)
	pylab.legend(loc='lower right')
	pylab.ylabel('biallelic fraction')
	pylab.xlabel('polyA+ RNA molecules, log2')
	pylab.savefig(o.pdfout)
	
	
            y = 1 / (1 + 2.718281**(params[1] * x + params[0]))
            yvalues.append(y)
        return numpy.array(yvalues)

    X, Y = [], []
    for cellsource in xarr:
        X.extend(xarr[cellsource])
        Y.extend(yarr[cellsource])
        print cellsource, 'n=', len(xarr[cellsource])
    print 'spearman', stats.spearmanr(X, Y)
    coefficients_p, pcov = optimize.curve_fit(fitting_f, X, Y, [0, -1])
    print 'line fit coefficients', coefficients_p
    x_line = numpy.arange(1, 9, 0.1)
    pylab.plot(x_line, fitting_f(x_line, *coefficients_p), 'k-')

    import random
    random.seed(100)
    for cellsource in xarr:
        pylab.plot(xarr[cellsource],
                   yarr[cellsource],
                   'o',
                   label=cellsource,
                   fillstyle='none',
                   markeredgecolor=dr_tools.randomcolour(),
                   markersize=5,
                   markeredgewidth=2)
    pylab.legend(loc='lower right')
    pylab.ylabel('biallelic fraction (>=20 rpkm)')
    pylab.xlabel('mRNA amount, log2 (1(nonlog)=same as ERCC amount)')
    pylab.savefig('ERCCsum_vs_biallelic_nonDia_min20rpkm_v6.pdf')
	raise Exception


if '__main__' == __name__:
	parser = argparse.ArgumentParser()
	parser.add_argument('-i', '--csv_in', nargs='*', action='append', required=True)
	parser.add_argument('-m', '--marker', '--channel', default='CD57')
	parser.add_argument('-o', '--plot_out', default='distribution.pdf')
	parser.add_argument('--base_folder_in', default='')
	parser.add_argument('--xlim', type=float, default=3)
	parser.add_argument('--ylim', type=float)
	parser.add_argument('--step', type=float, default=0.01)
	parser.add_argument('--noncumulative', action='store_true')
	o = parser.parse_args()
	
	random.seed(80)
	
	for files_in, bg_shade in zip(o.csv_in, shades_gen()):
		for csv_in in files_in:
			df = pandas.read_csv(os.path.join(o.base_folder_in, csv_in))
			values = list(df[o.marker])
			
			xarr, yarr = dr_tools.bin(values, start=o.step, end=o.xlim, step=o.step, cumulative=0 if o.noncumulative else -1, fractions=True)
			pyplot.plot(xarr, [y*100 for y in yarr], '-', color=dr_tools.mixcolours([bg_shade, dr_tools.randomcolour()], [0.5, 0.5]))
	
	pyplot.xlabel('Signal' if o.noncumulative else 'Cutoff')
	pyplot.ylabel('% of cells in bin'  if o.noncumulative else '% of cells above cutoff')
	if o.ylim is not None: pyplot.ylim(0, o.ylim)
	pyplot.title(o.marker)
	pyplot.savefig(o.plot_out)
def shades_gen():
	for c in ['#ff0000', '#0000ff', '#00ff00']:
		yield c
	while True:
		yield dr_tools.randomcolour()
                        time_since_baseline_per_sample[patient][visit]
                        for visit in xarr
                    ]
            except KeyError, ValueError:
                if not o.quite: print patient
                continue

        if o.interpolate_points is not None and not o.show_splines:
            f_splines = interpolation(xarr, yarr)
            xarr, yarr = o.interpolate_points, f_splines(o.interpolate_points)

        #for a, b, c in slidingwindow(yarr, 3):
        #	if (b-a)*(c-b) > 0: direction_continuation.append(1)
        #	elif (b-a)*(c-b) < 0: direction_continuation.append(0)
        if o.colour_patients is None:
            c = dr_tools.randomcolour()
        elif o.colour_patients[0].endswith('.txt'):
            for p in dr_tools.splitlines(o.colour_patients[0]):
                if p[0] == patient:
                    c = {'1': '#3465a4', '0': '#73d216', '2': '#d9b98e'}[p[1]]
                    break
            else:
                c = '#ff8888'
            #c = dr_tools.mixcolours([c, dr_tools.randomcolour()], [0.8, 0.2])
        else:
            c = '#5555ff' if patient in o.colour_patients else '#ccffcc'
            c = dr_tools.mixcolours([c, dr_tools.randomcolour()], [0.67, 0.33])
        if bins:
            for b in bins:
                b.add1within(xarr, yarr, patient)
        else: