def get_response_content(fs): # define some fixed values N_diploid = 6 N_hap = 2 * N_diploid plot_density = 8 # define some mutation rates theta_values = [0.001, 0.01, 0.1, 1.0] # define some selection coefficients to plot Ns_low = 0.0 Ns_high = 3.0 Ns_values = np.linspace(Ns_low, Ns_high, 3 * plot_density + 1) # get the values for each h Nr_values = (0, 5) arr_0 = get_plot_array(N_diploid, Nr_values[0], theta_values, Ns_values) arr_1 = get_plot_array(N_diploid, Nr_values[1], theta_values, Ns_values) ylab = '"expected returns to AB"' # define x and y plot limits xlim = (Ns_low, Ns_high) ylim = (np.min((arr_0, arr_1)), np.max((arr_0, arr_1))) ylogstr = '""' # http://sphaerula.com/legacy/R/multiplePlotFigure.html out = StringIO() print >> out, mk_call_str("par", mfrow="c(1,2)", oma="c(0,0,2,0)") print >> out, get_plot("left", Nr_values[0], arr_0, theta_values, Ns_values, xlim, ylim, ylogstr, ylab) print >> out, get_plot("right", Nr_values[1], arr_1, theta_values, Ns_values, xlim, ylim, ylogstr, '""') print >> out, mk_call_str("title", '"expected number of returns to AB, 2N=%s"' % N_hap, outer="TRUE") script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table(script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_plot( N_hap, theta, arr, Nr_values, Ns_values, xlim, ylim, ylogstr, ylab): colors = ['blue', 'red'] out = StringIO() print >> out, 'Ns.values <- c', str(tuple(Ns_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, mk_call_str('plot', 'Ns.values', 'ha', type='"p"', xlab='"Ns"', ylab=ylab, log=ylogstr, main='"theta=%s ; 2N=%s"' % (theta, N_hap), xlim='c' + str(xlim), ylim='c' + str(ylim), col='"%s"' % colors[0], ) print >> out, mk_call_str( 'points', 'Ns.values', 'hb', col='"%s"' % colors[1]) print >> out, mk_call_str( 'legend', '"topright"', 'c' + str(tuple('%s' % x for x in Nr_values)), title='"Nr"', lty='c' + str(tuple([1]*2)), lwd='c' + str(tuple([2.5]*2)), col='c' + str(tuple(colors)), ) return out.getvalue().rstrip()
def get_ggplot(): out = StringIO() print >> out, mk_call_str('require', '"ggplot2"') print >> out, mk_call_str( 'ggplot', 'my.table', mk_call_str('aes', x='log.probability.ratio', y='mutual.information')), print >> out, '+', print >> out, 'geom_line()' return out.getvalue()
def get_ggplot(): out = StringIO() print >> out, mk_call_str('require', '"reshape"') print >> out, mk_call_str('require', '"ggplot2"') print >> out, 'my.table.long <-', print >> out, mk_call_str('melt', 'my.table', id='"t"') print >> out, 'ggplot(data=my.table.long,' print >> out, mk_call_str('aes', x='t', y='value', colour='variable') print >> out, ') + geom_line()', return out.getvalue()
def get_response_content(fs): # define some fixed values N_diploid = 10 N_hap = 2 * N_diploid #Nr = fs.Nr plot_density = 2 # define some mutation rates theta_values = [0.001, 0.01, 0.1, 1.0] # define some selection coefficients to plot Ns_low = 0.0 Ns_high = 3.0 Ns_values = np.linspace(Ns_low, Ns_high, 3 * plot_density + 1) # get the values for each h Nr_values = (0, 5) arr_0 = get_plot_array(N_diploid, Nr_values[0], theta_values, Ns_values) arr_1 = get_plot_array(N_diploid, Nr_values[1], theta_values, Ns_values) if fs.scale_to_2N_200: arr_0 = (200 / float(N_hap)) * np.array(arr_0) arr_1 = (200 / float(N_hap)) * np.array(arr_1) ylab = '"generations * theta * (200 / 2N)"' else: ylab = '"generations * theta"' # define x and y plot limits xlim = (Ns_low, Ns_high) ylim = (np.min((arr_0, arr_1)), np.max((arr_0, arr_1))) if fs.ylogscale: ylogstr = '"y"' else: ylogstr = '""' # http://sphaerula.com/legacy/R/multiplePlotFigure.html out = StringIO() print >> out, mk_call_str( 'par', mfrow='c(1,2)', oma='c(0,0,2,0)', ) print >> out, get_plot('left', Nr_values[0], arr_0, theta_values, Ns_values, xlim, ylim, ylogstr, ylab) print >> out, get_plot('right', Nr_values[1], arr_1, theta_values, Ns_values, xlim, ylim, ylogstr, '""') print >> out, mk_call_str( 'title', '"mean hitting time, 2N=%s"' % N_hap, outer='TRUE', ) script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_ggplot(): out = StringIO() print >> out, mk_call_str('require', '"reshape"') print >> out, mk_call_str('require', '"ggplot2"') print >> out, 'ggplot(data=my.table,' print >> out, mk_call_str( 'aes', x='generation', y='number.of.mutants', fill='log.prob') print >> out, ') + geom_tile() +', print >> out, mk_call_str( 'scale_fill_continuous', breaks='c(-6, -5, -4, -3, -2, -1, 0)', limits='c(-6, 0)') return out.getvalue().rstrip()
def get_ggplot(): out = StringIO() print >> out, mk_call_str('require', '"ggplot2"') print >> out, mk_call_str( 'ggplot', 'my.table', mk_call_str( 'aes', x='log.probability.ratio', y='fisher.information')), print >> out, '+', print >> out, 'geom_line()' return out.getvalue()
def get_ggplot(granularity): cap = math.log(granularity) out = StringIO() print >> out, mk_call_str('require', '"reshape"') print >> out, mk_call_str('require', '"ggplot2"') print >> out, 'ggplot(data=my.table,' print >> out, mk_call_str( 'aes', x='generation', y='allele.frequency', fill='log.density') print >> out, ') + geom_tile() +', print >> out, mk_call_str( 'scale_fill_continuous', breaks='c(-6, -5, -4, -3, -2, -1, 0, %s)' % cap, limits='c(-6, %s)' % cap, ) return out.getvalue().rstrip()
def get_plot(side, Nr, arr, theta_values, Ns_values, xlim, ylim, ylogstr, ylab): """ @param side: 'left' or 'right' """ colors = ['blue', 'pink', 'green', 'red'] out = StringIO() print >> out, 'Ns.values <- c', str(tuple(Ns_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, mk_call_str( 'plot', 'Ns.values', 'ha', type='"l"', xlab='"Ns"', ylab=ylab, log=ylogstr, main='"Nr=%s"' % Nr, xlim='c' + str(xlim), ylim='c' + str(ylim), col='"%s"' % colors[0], ) print >> out, mk_call_str('lines', 'Ns.values', 'hb', col='"%s"' % colors[1]) print >> out, mk_call_str('lines', 'Ns.values', 'hc', col='"%s"' % colors[2]) print >> out, mk_call_str('lines', 'Ns.values', 'hd', col='"%s"' % colors[3]) if side == 'left': print >> out, mk_call_str( 'legend', '"topleft"', 'c' + str(tuple('%s' % x for x in theta_values)), title='"theta"', lty='c' + str(tuple([1] * 4)), lwd='c' + str(tuple([2.5] * 4)), col='c' + str(tuple(colors)), ) return out.getvalue().rstrip()
def get_response_content(fs): # define the initial frequency p = 0.1 # define some selection coefficients to plot h_low = 0.2 h_high = 0.8 h_values = np.linspace(h_low, h_high, 6*10 + 1) # define some dominance coefficients hs_values = [-0.2, -0.05, 0, 0.05, 0.2] colors = ['blue', 'green', 'black', 'orange', 'red'] # get the values for each h arr = [] for hs in hs_values: v = [kimura.get_fixation_probability_chen(p, hs/h, h) for h in h_values] arr.append(v) # # define the r script out = StringIO() print >> out, 'h.values <- c', str(tuple(h_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, 'he <- c', str(tuple(arr[4])) print >> out, mk_call_str('plot', 'h.values', 'ha', type='"l"', xlab='"h"', ylab='"probability of eventual fixation"', main=( '"fixation probabilities for various h*s ; ' 's = 2*N*sigma ; p0 = %s"' % p), ylim='c(0, 0.2)', col='"%s"' % colors[0], ) print >> out, mk_call_str('lines', 'h.values', 'hb', col='"%s"' % colors[1]) print >> out, mk_call_str('lines', 'h.values', 'hc', col='"%s"' % colors[2]) print >> out, mk_call_str('lines', 'h.values', 'hd', col='"%s"' % colors[3]) print >> out, mk_call_str('lines', 'h.values', 'he', col='"%s"' % colors[4]) script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_ggplot(granularity): cap = math.log(granularity) out = StringIO() print >> out, mk_call_str('require', '"reshape"') print >> out, mk_call_str('require', '"ggplot2"') print >> out, 'ggplot(data=my.table,' print >> out, mk_call_str('aes', x='generation', y='allele.frequency', fill='log.density') print >> out, ') + geom_tile() +', print >> out, mk_call_str( 'scale_fill_continuous', breaks='c(-6, -5, -4, -3, -2, -1, 0, %s)' % cap, limits='c(-6, %s)' % cap, ) return out.getvalue().rstrip()
def get_response_content(fs): # define the initial frequency p = 0.8 # define some selection coefficients to plot low = -5 high = 5 s_values = np.linspace(low, high, (high - low) * 20 + 1) # define some dominance coefficients h_values = [2, 3, 5, 7] # get the values for each h arr = [] for h in h_values: v = [kimura.get_fixation_probability_chen(p, s, h) for s in s_values] arr.append(v) # # define the r script out = StringIO() print >> out, 'title.string <- "my title"' print >> out, 's.values <- c', str(tuple(s_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, mk_call_str( 'plot', 's.values', 'ha', type='"l"', xlab='"selection coefficient (s)"', ylab='"probability of eventual fixation"', main='"fixation probabilities for various h ; p0 = 0.8"', ylim='c(0.5,1)', ) print >> out, mk_call_str('lines', 's.values', 'hb', col='"red"') print >> out, mk_call_str('lines', 's.values', 'hc', col='"green"') print >> out, mk_call_str('lines', 's.values', 'hd', col='"blue"') script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): # define the initial frequency p = 0.8 # define some selection coefficients to plot low = -5 high = 5 s_values = np.linspace(low, high, (high - low) * 20 + 1) # define some dominance coefficients h_values = [2, 3, 5, 7] # get the values for each h arr = [] for h in h_values: v = [kimura.get_fixation_probability_chen(p, s, h) for s in s_values] arr.append(v) # # define the r script out = StringIO() print >> out, 'title.string <- "my title"' print >> out, "s.values <- c", str(tuple(s_values)) print >> out, "ha <- c", str(tuple(arr[0])) print >> out, "hb <- c", str(tuple(arr[1])) print >> out, "hc <- c", str(tuple(arr[2])) print >> out, "hd <- c", str(tuple(arr[3])) print >> out, mk_call_str( "plot", "s.values", "ha", type='"l"', xlab='"selection coefficient (s)"', ylab='"probability of eventual fixation"', main='"fixation probabilities for various h ; p0 = 0.8"', ylim="c(0.5,1)", ) print >> out, mk_call_str("lines", "s.values", "hb", col='"red"') print >> out, mk_call_str("lines", "s.values", "hc", col='"green"') print >> out, mk_call_str("lines", "s.values", "hd", col='"blue"') script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table(script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_plainplot(): out = StringIO() print >> out, mk_call_str( 'plot', 'my.table$t', 'my.table$mutual.info.mutsel', type='"n"', xlab='"time"', ylab='""', ylim=mk_call_str('c', '0', '1.0'), main='"MI (black) and MI analog (red) over time"') print >> out, mk_call_str( 'lines', 'my.table$t', 'my.table$mutual.info.mut', col='"black"') print >> out, mk_call_str( 'lines', 'my.table$t', 'my.table$mutual.info.analog.mut', col='"red"') print >> out, mk_call_str( 'lines', 'my.table$t', 'my.table$mutual.info.mutsel', col='"black"') print >> out, mk_call_str( 'lines', 'my.table$t', 'my.table$mutual.info.analog.mutsel', col='"red"') return out.getvalue()
def get_response_content(fs): # define the initial frequency p = 0.1 # define some selection coefficients to plot low = -20 high = 20 s_values = np.linspace(low, high, (high-low)*10 + 1) # define some dominance coefficients h_values = [-3, 0, 1, 2] # get the values for each h arr = [] for h in h_values: v = [kimura.get_fixation_probability_chen(p, s, h) for s in s_values] arr.append(v) # # define the r script out = StringIO() print >> out, 's.values <- c', str(tuple(s_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, mk_call_str('plot', 's.values', 'ha', type='"l"', xlab='"selection coefficient (s)"', ylab='"probability of eventual fixation"', main='"fixation probabilities for various h ; p0 = 0.1"', ylim='c(0,1)', ) print >> out, mk_call_str('lines', 's.values', 'hb', col='"red"') print >> out, mk_call_str('lines', 's.values', 'hc', col='"green"') print >> out, mk_call_str('lines', 's.values', 'hd', col='"blue"') script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_plainplot(): out = StringIO() print >> out, mk_call_str( 'plot', 'my.table$t', 'my.table$mutual.info.mutsel', type='"n"', xlab='"time"', ylab='""', ylim=mk_call_str('c', '0', '1.0'), main='"MI (black) and MI analog (red) over time"') print >> out, mk_call_str('lines', 'my.table$t', 'my.table$mutual.info.mut', col='"black"') print >> out, mk_call_str('lines', 'my.table$t', 'my.table$mutual.info.analog.mut', col='"red"') print >> out, mk_call_str('lines', 'my.table$t', 'my.table$mutual.info.mutsel', col='"black"') print >> out, mk_call_str('lines', 'my.table$t', 'my.table$mutual.info.analog.mutsel', col='"red"') return out.getvalue()
def get_plot( side, Nr, arr, theta_values, Ns_values, xlim, ylim, ylogstr, ylab): """ @param side: 'left' or 'right' """ colors = ['blue', 'pink', 'green', 'red'] out = StringIO() print >> out, 'Ns.values <- c', str(tuple(Ns_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, mk_call_str('plot', 'Ns.values', 'ha', type='"l"', xlab='"Ns"', ylab=ylab, log=ylogstr, main='"Nr=%s"' % Nr, xlim='c' + str(xlim), ylim='c' + str(ylim), col='"%s"' % colors[0], ) print >> out, mk_call_str( 'lines', 'Ns.values', 'hb', col='"%s"' % colors[1]) print >> out, mk_call_str( 'lines', 'Ns.values', 'hc', col='"%s"' % colors[2]) print >> out, mk_call_str( 'lines', 'Ns.values', 'hd', col='"%s"' % colors[3]) if side == 'left': print >> out, mk_call_str( 'legend', '"topleft"', 'c' + str(tuple('%s' % x for x in theta_values)), title='"theta"', lty='c' + str(tuple([1]*4)), lwd='c' + str(tuple([2.5]*4)), col='c' + str(tuple(colors)), ) return out.getvalue().rstrip()
def get_plot(side, Nr, arr, theta_values, Ns_values, xlim, ylim, ylogstr, ylab): """ @param side: 'left' or 'right' """ colors = ["blue", "pink", "green", "red"] out = StringIO() print >> out, "Ns.values <- c", str(tuple(Ns_values)) print >> out, "ha <- c", str(tuple(arr[0])) print >> out, "hb <- c", str(tuple(arr[1])) print >> out, "hc <- c", str(tuple(arr[2])) print >> out, "hd <- c", str(tuple(arr[3])) print >> out, mk_call_str( "plot", "Ns.values", "ha", type='"l"', xlab='"Ns"', ylab=ylab, log=ylogstr, main='"Nr=%s"' % Nr, xlim="c" + str(xlim), ylim="c" + str(ylim), col='"%s"' % colors[0], ) print >> out, mk_call_str("lines", "Ns.values", "hb", col='"%s"' % colors[1]) print >> out, mk_call_str("lines", "Ns.values", "hc", col='"%s"' % colors[2]) print >> out, mk_call_str("lines", "Ns.values", "hd", col='"%s"' % colors[3]) if side == "left": print >> out, mk_call_str( "legend", '"topleft"', "c" + str(tuple("%s" % x for x in theta_values)), title='"theta"', lty="c" + str(tuple([1] * 4)), lwd="c" + str(tuple([2.5] * 4)), col="c" + str(tuple(colors)), ) return out.getvalue().rstrip()
def get_ggplot(): out = StringIO() print >> out, mk_call_str('require', '"reshape"') print >> out, mk_call_str('require', '"ggplot2"') print >> out, 'my.table.long <-', print >> out, mk_call_str('melt', 'my.table', id='"z"') print >> out, 'ggplot(data=my.table.long,' print >> out, mk_call_str('aes', x='z', y='value', colour='variable') print >> out, ') + geom_line()', print >> out, '+', print >> out, mk_call_str( 'xlim', mk_call_str('min', 'my.table.long$z'), mk_call_str('max', 'my.table.long$z'), ), print >> out, '+', print >> out, mk_call_str('ylim', '0', mk_call_str('max', 'my.table.long$value')) return out.getvalue()
def get_ggplot(): out = StringIO() print >> out, mk_call_str('require', '"reshape"') print >> out, mk_call_str('require', '"ggplot2"') print >> out, 'my.table.long <-', print >> out, mk_call_str('melt', 'my.table', id='"z"') print >> out, 'ggplot(data=my.table.long,' print >> out, mk_call_str('aes', x='z', y='value', colour='variable') print >> out, ') + geom_line()', print >> out, '+', print >> out, mk_call_str( 'xlim', mk_call_str('min', 'my.table.long$z'), mk_call_str('max', 'my.table.long$z'), ), print >> out, '+', print >> out, mk_call_str( 'ylim', '0', mk_call_str('max', 'my.table.long$value')) return out.getvalue()
def get_response_content(fs): N_diploid = fs.N_diploid N = N_diploid * 2 k = 2 gamma = fs.gamma # define the fitnesses and the selection value f0 = 1.0 f1 = 1.0 - gamma / N s = 1 - f1 / f0 if f1 <= 0: raise ValueError('the extreme selection caused a non-positive fitness') # get a wright fisher transition matrix P = np.exp(wfengine.create_genic_diallelic(N_diploid, s)) """ # condition on no fixation for i in range(N): P[i] /= 1 - P[i, N] # remove the fixed state from the transition matrix P = P[:N, :N] """ # add mutations P[0, 0] = 0 P[0, 1] = 1 P[N, N] = 0 P[N, 1] = 1 # compute the stationary distribution v = MatrixUtil.get_stationary_distribution(P) # get the distribution over dimorphic states h = v[1:-1] h /= np.sum(h) # look at continuous approximations w = np.zeros(N+1) for i in range(1, N): x = i / float(N) #x0 = i / float(N) #x1 = (i + 1) / float(N) #value = sojourn_definite(x0, x1, gamma) value = sojourn_kernel(x, gamma) w[i] = value w = w[1:-1] w /= np.sum(w) # get the array for the R plot arr = [h.tolist(), w.tolist()] # define the r script out = StringIO() print >> out, 'title.string <- "allele 1 vs allele 2"' print >> out, 'mdat <-', RUtil.matrix_to_R_string(arr) print >> out, mk_call_str( 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"exact discrete distribution"', '"continuous approximation"', #'"two-allele large N limit"', #'"two-allele"', #'"four-allele without mutational bias"', #'"four-allele with mutational bias (kappa_{1,2}=2)"', #'"four-allele with mutational bias, large N limit"', ), 'args.legend = list(x="topright", bty="n")', 'names.arg = 1:%s' % (N-1), main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', #'"red"', #'"white"', '"black"', #'"gray"', '"red"', ), beside='TRUE', border='NA', ) #print >> out, 'box()' script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): # N_diploid = 10 N_haploid = 2 * N_diploid t1_20_exact = get_t1_exact(N_diploid) t1_20_approx = get_t1_approx(N_diploid) proportions_20 = np.linspace(0.0, 1.0, 2 * N_diploid + 1)[1:] # N_diploid = 5 N_haploid = 2 * N_diploid t1_10_exact = get_t1_exact(N_diploid) t1_10_approx = get_t1_approx(N_diploid) proportions_10 = np.linspace(0.0, 1.0, 2 * N_diploid + 1)[1:] # # define the r script out = StringIO() print >> out, 'title.string <- "my title"' print >> out, 't1.20.approx <- c', str(tuple(t1_20_approx)) print >> out, 't1.20.exact <- c', str(tuple(t1_20_exact)) print >> out, 'p.20 <- c', str(tuple(proportions_20.tolist())) print >> out, mk_call_str('plot', 'p.20', 't1.20.approx', col='"red"', type='"l"', ylim='c(0,45)', xaxp='c(0,1,10)') print >> out, mk_call_str('points', 'p.20', 't1.20.exact', col='"blue"') # print >> out, 't1.10.approx <- c', str(tuple(t1_10_approx)) print >> out, 't1.10.exact <- c', str(tuple(t1_10_exact)) print >> out, 'p.10 <- c', str(tuple(proportions_10.tolist())) print >> out, mk_call_str('lines', 'p.10', 't1.10.approx', col='"red"') print >> out, mk_call_str('points', 'p.10', 't1.10.exact', col='"blue"') # """ 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"exact discrete distribution"', '"continuous approximation"', #'"two-allele large N limit"', #'"two-allele"', #'"four-allele without mutational bias"', #'"four-allele with mutational bias (kappa_{1,2}=2)"', #'"four-allele with mutational bias, large N limit"', ), 'args.legend = list(x="topright", bty="n")', 'names.arg = 1:%s' % (N-1), main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', #'"red"', #'"white"', '"black"', #'"gray"', '"red"', ), beside='TRUE', border='NA', ) #print >> out, 'box()' """ script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): N_small = 10 N_big_diploid = fs.N_big_diploid N_big_haploid = N_big_diploid * 2 if N_big_haploid < N_small: raise ValueError('use a larger diploid population size') if fs.with_replacement: f_subsample = StatsUtil.subsample_pmf_with_replacement elif fs.without_replacement: f_subsample = StatsUtil.subsample_pmf_without_replacement else: raise ValueError('subsampling option error') k = 4 gamma = fs.gamma_1 params_list = [ (0.008, 1, 1, fs.gamma_0, fs.gamma_1, fs.gamma_2), (0.008, 2, 1, fs.gamma_0, fs.gamma_1, fs.gamma_2)] allele_histograms = np.zeros((2, N_big_haploid + 1)) for i, params in enumerate(params_list): mutation, selection = kaizeng.params_to_mutation_fitness( N_big_haploid, params) P = kaizeng.get_transition_matrix( N_big_diploid, k, mutation, selection) v = MatrixUtil.get_stationary_distribution(P) for state_index, counts in enumerate(kaizeng.gen_states( N_big_haploid, k)): if counts[0] and counts[1]: allele_histograms[i, counts[0]] += v[state_index] # Define the r table. # There are nine columns each corresponding to an allele frequency. # There are three rows each corresponding to a configuration. arr = [] # Use the two allele approximation # from mcvean and charlesworth 1999 referred to by zeng 2011. # I'm not sure if I am using the right equation. g0 = fs.gamma_0 g1 = fs.gamma_1 """ s_0 = -gamma_0 / float(N_big) s_1 = -gamma_1 / float(N_big) hist = np.zeros(N_small+1) for i in range(1, N_small): x = i / float(N_small) hist[i] = math.exp(1*N_big*(s_0 - s_1)*x) / (x*(1-x)) h = hist[1:-1] h /= np.sum(h) arr.append(h.tolist()) """ arr.append(diallelic_approximation(N_small, g0, g1).tolist()) # Use the exact two allele distribution. # Well, it is exact if I understand the right scaling # of the population size and fitnesses. f0 = 1.0 f1 = 1.0 - gamma / N_big_haploid #f0 = 1.0 + gamma / N #f1 = 1.0 #f0 = 1.0 + 1.5 / (4*N) #f1 = 1.0 - 1.5 / (4*N) h = get_two_allele_distribution( N_big_haploid, N_small, f0, f1, f_subsample) arr.append(h.tolist()) # Get frequencies for the other two configurations for hist in allele_histograms: # Get probabilities conditional on dimorphism. hist[0] = 0 hist[-1] = 0 hist /= np.sum(hist) # Get the subsampled pmf. distn = f_subsample(hist, N_small) MatrixUtil.assert_distribution(distn) # Get probabiities conditional on dimorphism of the sample. distn[0] = 0 distn[-1] = 0 distn /= np.sum(distn) # Add to the table of densities. arr.append(distn[1:-1].tolist()) # Get a large population approximation # when there is mutational bias. params = (0.008, 2, 1, fs.gamma_0, fs.gamma_1, fs.gamma_2) mutation, fitness = kaizeng.params_to_mutation_fitness( N_big_haploid, params) gammas = np.array([fs.gamma_0, fs.gamma_1, fs.gamma_2, 0]) h = kaizeng.get_large_population_approximation(N_small, k, gammas, mutation) arr.append(h.tolist()) # define the r script out = StringIO() print >> out, 'title.string <- "allele 1 vs allele 2"' print >> out, 'mdat <-', RUtil.matrix_to_R_string(arr) print >> out, mk_call_str( 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"two-allele large N limit"', '"two-allele"', '"four-allele without mutational bias"', '"four-allele with mutational bias (kappa_{1,2}=2)"', '"four-allele with mutational bias, large N limit"', ), 'args.legend = list(x="topleft", bty="n")', 'names.arg = c(1,2,3,4,5,6,7,8,9)', main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', '"red"', '"white"', '"black"', '"gray"', '"blue"', ), beside='TRUE', ) #print >> out, 'box()' script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): # define the initial frequency p = 0.1 # define some selection coefficients to plot h_low = 0.2 h_high = 0.8 h_values = np.linspace(h_low, h_high, 6*10 + 1) # define some dominance coefficients hs_values = [-0.2, -0.1, -0.04, 0, 0.04, 0.1, 0.2] #colors = ['blue', 'green', 'black', 'orange', 'red'] colors = ['darkviolet', 'blue', 'green', 'black', 'yellow', 'orange', 'red'] # get the values for each h arr = [] xaxis = [] for hs in hs_values: s_values = hs / h_values v = [kimura.get_fixation_probability_chen(p, hs/h, h) for h in h_values] xaxis.append(s_values) arr.append(v) # # define the r script out = StringIO() print >> out, 'h.values <- c', str(tuple(h_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, 'he <- c', str(tuple(arr[4])) print >> out, 'hf <- c', str(tuple(arr[5])) print >> out, 'hg <- c', str(tuple(arr[6])) print >> out, 'ha.x <- c', str(tuple(xaxis[0])) print >> out, 'hb.x <- c', str(tuple(xaxis[1])) print >> out, 'hc.x <- c', str(tuple(xaxis[2])) print >> out, 'hd.x <- c', str(tuple(xaxis[3])) print >> out, 'he.x <- c', str(tuple(xaxis[4])) print >> out, 'hf.x <- c', str(tuple(xaxis[5])) print >> out, 'hg.x <- c', str(tuple(xaxis[6])) print >> out, mk_call_str('plot', 'ha.x', 'ha', type='"l"', xlab='"selection coefficient (s)"', ylab='"probability of eventual fixation"', main=( '"fixation probabilities for various h*s ; ' 's = 2*N*sigma ; p0 = %s"' % p), xlim='c(-1, 1)', ylim='c(0.05, 0.15)', col='"%s"' % colors[0], ) print >> out, mk_call_str('lines', 'hb.x', 'hb', col='"%s"' % colors[1]) print >> out, mk_call_str('lines', 'hc.x', 'hc', col='"%s"' % colors[2]) print >> out, mk_call_str('lines', 'hd.x', 'hd', col='"%s"' % colors[3]) print >> out, mk_call_str('lines', 'he.x', 'he', col='"%s"' % colors[4]) print >> out, mk_call_str('lines', 'hf.x', 'hf', col='"%s"' % colors[5]) print >> out, mk_call_str('lines', 'hg.x', 'hg', col='"%s"' % colors[6]) print >> out, mk_call_str( 'legend', '"topleft"', 'c' + str(rev('hs = %s' % x for x in hs_values)), lty='c' + str(rev([1]*7)), lwd='c' + str(rev([2.5]*7)), col='c' + str(rev(colors)), ) script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): N_diploid = 5 N_haploid = N_diploid * 2 k = 4 gamma = 1.5 params_list = [ (0.008, 1, 1, 0, gamma, 1), (0.008, 2, 1, 0, gamma, 1)] allele_histograms = np.zeros((2, N_haploid+1)) for i, params in enumerate(params_list): mutation, fitnesses = kaizeng.params_to_mutation_fitness( N_haploid, params) P = kaizeng.get_transition_matrix( N_diploid, k, mutation, fitnesses) v = MatrixUtil.get_stationary_distribution(P) for state_index, counts in enumerate(kaizeng.gen_states(N_haploid, k)): if counts[0] and counts[1]: allele_histograms[i, counts[0]] += v[state_index] # Define the r table. # There are nine columns each corresponding to an allele frequency. # There are three rows each corresponding to a configuration. arr = [] # Use the exact two allele distribution. # Well, it is exact if I understand the right scaling # of the population size and fitnesses. f0 = 1.0 f1 = 1.0 - gamma / N_haploid #f0 = 1.0 + gamma / N #f1 = 1.0 #f0 = 1.0 + 1.5 / (4*N) #f1 = 1.0 - 1.5 / (4*N) h = get_two_allele_distribution(N_diploid, f0, f1) arr.append(h.tolist()) # Use the two allele approximation # from mcvean and charlesworth 1999 referred to by zeng 2011. # I'm not sure if I am using the right equation. """ gamma_0 = 0 gamma_1 = 1.5 s_0 = -gamma_0 / float(N) s_1 = -gamma_1 / float(N) hist = np.zeros(N+1) for i in range(1, N): x = i / float(N) hist[i] = math.exp(1*N*(s_0 - s_1)*x) / (x*(1-x)) h = hist[1:-1] h /= np.sum(h) arr.append(h.tolist()) """ # Get frequencies for the other two configurations for hist in allele_histograms: h = hist[1:-1] h /= np.sum(h) arr.append(h.tolist()) # define the r script out = StringIO() print >> out, 'title.string <- "allele 1 vs allele 2, gamma = 1.5"' print >> out, 'mdat <-', RUtil.matrix_to_R_string(arr) print >> out, mk_call_str( 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"two-allele"', '"four-allele without mutational bias"', '"four-allele with mutational bias kappa_{1,2}=2"', ), 'args.legend = list(x="topleft", bty="n")', 'names.arg = c(1,2,3,4,5,6,7,8,9)', main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', #'"red"', '"white"', '"black"', '"gray"', ), beside='TRUE', ) #print >> out, 'box()' script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): N_diploid = 10 N_mutants = 1 N_haploid = N_diploid * 2 p = N_mutants / float(N_haploid) t1_exact = [] t1_approx = [] Nes_values = range(1, 9) for Nes in Nes_values: s = Nes / float(N_diploid) t1_exact.append(get_t1_exact(N_mutants, N_diploid, s)) t1_approx.append(get_t1_approx(p, N_diploid, s)) # define the r script out = StringIO() print >> out, 'title.string <- "my title"' print >> out, 'Nes <- c', str(tuple(Nes_values)) print >> out, 't1 <- c', str(tuple(t1_exact)) print >> out, 't1.approx <- c', str(tuple(t1_approx)) print >> out, mk_call_str('plot', 'Nes', 't1', #col='"red"', type='"l"', xaxp='c(0,1,10)', ylim='c(0,%s)' % (N_diploid*4)) print >> out, mk_call_str('lines', 'Nes', 't1.approx', col='"red"') #print >> out, mk_call_str('points', 'p.20', 't1.20.exact', #col='"blue"') # """ print >> out, 't1.10.approx <- c', str(tuple(t1_10_approx)) print >> out, 't1.10.exact <- c', str(tuple(t1_10_exact)) print >> out, 'p.10 <- c', str(tuple(proportions_10.tolist())) print >> out, mk_call_str('lines', 'p.10', 't1.10.approx', col='"red"') print >> out, mk_call_str('points', 'p.10', 't1.10.exact', col='"blue"') """ # """ 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"exact discrete distribution"', '"continuous approximation"', #'"two-allele large N limit"', #'"two-allele"', #'"four-allele without mutational bias"', #'"four-allele with mutational bias (kappa_{1,2}=2)"', #'"four-allele with mutational bias, large N limit"', ), 'args.legend = list(x="topright", bty="n")', 'names.arg = 1:%s' % (N-1), main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', #'"red"', #'"white"', '"black"', #'"gray"', '"red"', ), beside='TRUE', border='NA', ) #print >> out, 'box()' """ script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): # define the initial frequency p = 0.1 # define some selection coefficients to plot h_low = 0.2 h_high = 0.8 h_values = np.linspace(h_low, h_high, 6 * 10 + 1) # define some dominance coefficients hs_values = [-0.2, -0.05, 0, 0.05, 0.2] colors = ['blue', 'green', 'black', 'orange', 'red'] # get the values for each h arr = [] for hs in hs_values: v = [ kimura.get_fixation_probability_chen(p, hs / h, h) for h in h_values ] arr.append(v) # # define the r script out = StringIO() print >> out, 'h.values <- c', str(tuple(h_values)) print >> out, 'ha <- c', str(tuple(arr[0])) print >> out, 'hb <- c', str(tuple(arr[1])) print >> out, 'hc <- c', str(tuple(arr[2])) print >> out, 'hd <- c', str(tuple(arr[3])) print >> out, 'he <- c', str(tuple(arr[4])) print >> out, mk_call_str( 'plot', 'h.values', 'ha', type='"l"', xlab='"h"', ylab='"probability of eventual fixation"', main=('"fixation probabilities for various h*s ; ' 's = 2*N*sigma ; p0 = %s"' % p), ylim='c(0, 0.2)', col='"%s"' % colors[0], ) print >> out, mk_call_str('lines', 'h.values', 'hb', col='"%s"' % colors[1]) print >> out, mk_call_str('lines', 'h.values', 'hc', col='"%s"' % colors[2]) print >> out, mk_call_str('lines', 'h.values', 'hd', col='"%s"' % colors[3]) print >> out, mk_call_str('lines', 'h.values', 'he', col='"%s"' % colors[4]) script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): N_diploid = fs.N_diploid N = N_diploid * 2 k = 2 gamma = fs.gamma # define the fitnesses and the selection value f0 = 1.0 f1 = 1.0 - gamma / N s = 1 - f1 / f0 if f1 <= 0: raise ValueError('the extreme selection caused a non-positive fitness') # get a wright fisher transition matrix P = np.exp(wfengine.create_genic_diallelic(N_diploid, s)) """ # condition on no fixation for i in range(N): P[i] /= 1 - P[i, N] # remove the fixed state from the transition matrix P = P[:N, :N] """ # add mutations P[0, 0] = 0 P[0, 1] = 1 P[N, N] = 0 P[N, 1] = 1 # compute the stationary distribution v = MatrixUtil.get_stationary_distribution(P) # get the distribution over dimorphic states h = v[1:-1] h /= np.sum(h) # look at continuous approximations w = np.zeros(N + 1) for i in range(1, N): x = i / float(N) #x0 = i / float(N) #x1 = (i + 1) / float(N) #value = sojourn_definite(x0, x1, gamma) value = sojourn_kernel(x, gamma) w[i] = value w = w[1:-1] w /= np.sum(w) # get the array for the R plot arr = [h.tolist(), w.tolist()] # define the r script out = StringIO() print >> out, 'title.string <- "allele 1 vs allele 2"' print >> out, 'mdat <-', RUtil.matrix_to_R_string(arr) print >> out, mk_call_str( 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"exact discrete distribution"', '"continuous approximation"', #'"two-allele large N limit"', #'"two-allele"', #'"four-allele without mutational bias"', #'"four-allele with mutational bias (kappa_{1,2}=2)"', #'"four-allele with mutational bias, large N limit"', ), 'args.legend = list(x="topright", bty="n")', 'names.arg = 1:%s' % (N - 1), main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', #'"red"', #'"white"', '"black"', #'"gray"', '"red"', ), beside='TRUE', border='NA', ) #print >> out, 'box()' script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): # define the initial frequency p = 0.01 density = 2 # dominance if fs.h_dense: h_low = 0.2 h_high = 0.8 h_values = np.linspace(h_low, h_high, 6 * density + 1) elif fs.h_sparse: h_low = 0 h_high = 1 h_values = np.linspace(h_low, h_high, 2 + 1) # selection s_low = -1.0 s_high = 1.0 s_values = fs.s_scale * np.linspace(s_low, s_high, 20 * density + 1) # hs product hs_values = np.outer(h_values, s_values) # compute all values in the grid f_values = np.zeros((len(h_values), len(s_values))) for i, h in enumerate(h_values): for j, s in enumerate(s_values): f_values[i, j] = kimura.get_fixation_probability_chen(p, s, h) if fs.x_is_log_pfix: f_values = np.log(f_values / p) # define the r script out = StringIO() if fs.infer_s: ylab_string = '"selection coefficient (s)"' ylim_low = np.min(s_values) ylim_high = np.max(s_values) elif fs.infer_hs: ylab_string = '"product of dominance and selection (hs)"' ylim_low = np.min(hs_values) ylim_high = np.max(hs_values) if fs.x_is_pfix: xlab_string = '"probability of eventual fixation"' elif fs.x_is_log_pfix: xlab_string = '"log of ratios of probability of eventual fixation"' xlim_low = np.min(f_values) xlim_high = np.max(f_values) colors = ['orange', 'green', 'blue'] if fs.h_dense: main_string = ('"s = 2*N*sigma ; ' '%s < h < %s ; ' 'p0 = %s"' % (h_low, h_high, p)) elif fs.h_sparse: main_string = ('"s = 2*N*sigma ; ' 'p0 = %s"' % p) print >> out, mk_call_str( 'plot', 'c(0,0)', 'c(0,0)', type='"n"', xlab=xlab_string, ylab=ylab_string, main=main_string, xlim='c(%s, %s)' % (xlim_low, xlim_high), ylim='c(%s, %s)' % (ylim_low, ylim_high), ) if fs.h_dense: for h, s_row in zip(h_values, f_values): if fs.infer_s: y_vect = s_values elif fs.infer_hs: y_vect = h * s_values print >> out, mk_call_str( 'lines', 'c' + str(tuple(s_row)), 'c' + str(tuple(y_vect)), ) for s, h_col in zip(s_values, f_values.T): if fs.infer_s: y_vect = s * np.ones_like(h_col) elif fs.infer_hs: y_vect = s * h_values print >> out, mk_call_str( 'lines', 'c' + str(tuple(h_col)), 'c' + str(tuple(y_vect)), ) elif fs.h_sparse: for i, (h, s_row) in enumerate(zip(h_values, f_values)): if fs.infer_s: y_vect = s_values elif fs.infer_hs: y_vect = h * s_values print >> out, mk_call_str( 'lines', 'c' + str(tuple(s_row)), 'c' + str(tuple(y_vect)), col='"%s"' % colors[i], ) if fs.h_sparse: print >> out, mk_call_str( 'legend', '"topleft"', 'c' + str(tuple('h = %s' % x for x in h_values)), lty='c' + str(tuple([1] * len(h_values))), lwd='c' + str(tuple([2.5] * len(h_values))), col='c' + str(tuple(colors)), ) script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): N_small = 10 N_big_diploid = fs.N_big_diploid N_big_haploid = N_big_diploid * 2 if N_big_haploid < N_small: raise ValueError('use a larger diploid population size') if fs.with_replacement: f_subsample = StatsUtil.subsample_pmf_with_replacement elif fs.without_replacement: f_subsample = StatsUtil.subsample_pmf_without_replacement else: raise ValueError('subsampling option error') k = 4 gamma = fs.gamma_1 params_list = [(0.008, 1, 1, fs.gamma_0, fs.gamma_1, fs.gamma_2), (0.008, 2, 1, fs.gamma_0, fs.gamma_1, fs.gamma_2)] allele_histograms = np.zeros((2, N_big_haploid + 1)) for i, params in enumerate(params_list): mutation, selection = kaizeng.params_to_mutation_fitness( N_big_haploid, params) P = kaizeng.get_transition_matrix(N_big_diploid, k, mutation, selection) v = MatrixUtil.get_stationary_distribution(P) for state_index, counts in enumerate( kaizeng.gen_states(N_big_haploid, k)): if counts[0] and counts[1]: allele_histograms[i, counts[0]] += v[state_index] # Define the r table. # There are nine columns each corresponding to an allele frequency. # There are three rows each corresponding to a configuration. arr = [] # Use the two allele approximation # from mcvean and charlesworth 1999 referred to by zeng 2011. # I'm not sure if I am using the right equation. g0 = fs.gamma_0 g1 = fs.gamma_1 """ s_0 = -gamma_0 / float(N_big) s_1 = -gamma_1 / float(N_big) hist = np.zeros(N_small+1) for i in range(1, N_small): x = i / float(N_small) hist[i] = math.exp(1*N_big*(s_0 - s_1)*x) / (x*(1-x)) h = hist[1:-1] h /= np.sum(h) arr.append(h.tolist()) """ arr.append(diallelic_approximation(N_small, g0, g1).tolist()) # Use the exact two allele distribution. # Well, it is exact if I understand the right scaling # of the population size and fitnesses. f0 = 1.0 f1 = 1.0 - gamma / N_big_haploid #f0 = 1.0 + gamma / N #f1 = 1.0 #f0 = 1.0 + 1.5 / (4*N) #f1 = 1.0 - 1.5 / (4*N) h = get_two_allele_distribution(N_big_haploid, N_small, f0, f1, f_subsample) arr.append(h.tolist()) # Get frequencies for the other two configurations for hist in allele_histograms: # Get probabilities conditional on dimorphism. hist[0] = 0 hist[-1] = 0 hist /= np.sum(hist) # Get the subsampled pmf. distn = f_subsample(hist, N_small) MatrixUtil.assert_distribution(distn) # Get probabiities conditional on dimorphism of the sample. distn[0] = 0 distn[-1] = 0 distn /= np.sum(distn) # Add to the table of densities. arr.append(distn[1:-1].tolist()) # Get a large population approximation # when there is mutational bias. params = (0.008, 2, 1, fs.gamma_0, fs.gamma_1, fs.gamma_2) mutation, fitness = kaizeng.params_to_mutation_fitness( N_big_haploid, params) gammas = np.array([fs.gamma_0, fs.gamma_1, fs.gamma_2, 0]) h = kaizeng.get_large_population_approximation(N_small, k, gammas, mutation) arr.append(h.tolist()) # define the r script out = StringIO() print >> out, 'title.string <- "allele 1 vs allele 2"' print >> out, 'mdat <-', RUtil.matrix_to_R_string(arr) print >> out, mk_call_str( 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"two-allele large N limit"', '"two-allele"', '"four-allele without mutational bias"', '"four-allele with mutational bias (kappa_{1,2}=2)"', '"four-allele with mutational bias, large N limit"', ), 'args.legend = list(x="topleft", bty="n")', 'names.arg = c(1,2,3,4,5,6,7,8,9)', main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', '"red"', '"white"', '"black"', '"gray"', '"blue"', ), beside='TRUE', ) #print >> out, 'box()' script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data
def get_response_content(fs): N_diploid = 5 N_haploid = N_diploid * 2 k = 4 gamma = 1.5 params_list = [(0.008, 1, 1, 0, gamma, 1), (0.008, 2, 1, 0, gamma, 1)] allele_histograms = np.zeros((2, N_haploid + 1)) for i, params in enumerate(params_list): mutation, fitnesses = kaizeng.params_to_mutation_fitness( N_haploid, params) P = kaizeng.get_transition_matrix(N_diploid, k, mutation, fitnesses) v = MatrixUtil.get_stationary_distribution(P) for state_index, counts in enumerate(kaizeng.gen_states(N_haploid, k)): if counts[0] and counts[1]: allele_histograms[i, counts[0]] += v[state_index] # Define the r table. # There are nine columns each corresponding to an allele frequency. # There are three rows each corresponding to a configuration. arr = [] # Use the exact two allele distribution. # Well, it is exact if I understand the right scaling # of the population size and fitnesses. f0 = 1.0 f1 = 1.0 - gamma / N_haploid #f0 = 1.0 + gamma / N #f1 = 1.0 #f0 = 1.0 + 1.5 / (4*N) #f1 = 1.0 - 1.5 / (4*N) h = get_two_allele_distribution(N_diploid, f0, f1) arr.append(h.tolist()) # Use the two allele approximation # from mcvean and charlesworth 1999 referred to by zeng 2011. # I'm not sure if I am using the right equation. """ gamma_0 = 0 gamma_1 = 1.5 s_0 = -gamma_0 / float(N) s_1 = -gamma_1 / float(N) hist = np.zeros(N+1) for i in range(1, N): x = i / float(N) hist[i] = math.exp(1*N*(s_0 - s_1)*x) / (x*(1-x)) h = hist[1:-1] h /= np.sum(h) arr.append(h.tolist()) """ # Get frequencies for the other two configurations for hist in allele_histograms: h = hist[1:-1] h /= np.sum(h) arr.append(h.tolist()) # define the r script out = StringIO() print >> out, 'title.string <- "allele 1 vs allele 2, gamma = 1.5"' print >> out, 'mdat <-', RUtil.matrix_to_R_string(arr) print >> out, mk_call_str( 'barplot', 'mdat', 'legend.text=' + mk_call_str( 'c', '"two-allele"', '"four-allele without mutational bias"', '"four-allele with mutational bias kappa_{1,2}=2"', ), 'args.legend = list(x="topleft", bty="n")', 'names.arg = c(1,2,3,4,5,6,7,8,9)', main='title.string', xlab='"frequency of allele 1"', ylab='"frequency"', col=mk_call_str( 'c', #'"red"', '"white"', '"black"', '"gray"', ), beside='TRUE', ) #print >> out, 'box()' script = out.getvalue().rstrip() # create the R plot image device_name = Form.g_imageformat_to_r_function[fs.imageformat] retcode, r_out, r_err, image_data = RUtil.run_plotter_no_table( script, device_name) if retcode: raise RUtil.RError(r_err) return image_data