def degradation_experiment(): """Determine whether linear or pairwise models are more resistant to degradation""" L = 10 N = 50 Ne = 5 nu = Ne - 1 sigma = 1 mu = -10 matrix = sample_matrix(L, sigma) code = sample_code(L, sigma) li_motif = sample_motif_cftp(matrix, mu, Ne, N) pw_motif = sample_pw_motif_mh(code, N, Ne, mu, iterations=100000)[-1] def li_log_fitness(motif): eps = [score_seq(matrix, site) for site in motif] return sum(-nu * log((1 + exp(ep - mu))) for ep in eps) def pw_log_fitness(motif): eps = map(lambda x: -log(x), pw_prob_sites(motif, code)) return sum(log(1 / (1 + exp(ep - mu))**nu) for ep in eps) li_base_fit = li_log_fitness(li_motif) li_mut_fits = [li_log_fitness(mutate_motif(li_motif)) for i in range(100)] pw_base_fit = pw_log_fitness(pw_motif) pw_mut_fits = [pw_log_fitness(mutate_motif(pw_motif)) for i in range(100)]
def prop((code, motif)): code_p = [d.copy() for d in code] i = random.randrange(len(code)) b1, b2 = random.choice("ACGT"), random.choice("ACGT") code_p[i][(b1, b2)] += random.gauss(0, sigma) motif_p = mutate_motif(motif) return (code_p, motif_p)
def propose(matrix,motif,motif_prob=0.5): """Return a candidate (S,R) system by mutating either the motif or the matrix""" if random.random() < motif_prob: return matrix,mutate_motif(motif) else: return mutate_matrix(matrix),motif
def match_ic_mi(N, L, des_ic, des_mi, iterations=50000, take_stock=None, eta=0.01, alpha=1, beta=0): if take_stock is None: take_stock = int((N * L) * log(N * L)) x = random_motif(L, N) xs = [None] * iterations ics = [0.0] * iterations mis = [0.0] * iterations alphas = [0.0] * iterations betas = [0.0] * iterations ic = motif_ic(x) mi = total_motif_mi(x) accepts = 0 for i in xrange(iterations): # if i == iterations/2: # eta *= 0.1 xp = mutate_motif(x) icp = motif_ic(xp) mip = total_motif_mi(xp) log_y = (alpha * ic + beta * mi) log_yp = (alpha * icp + beta * mip) if log(random.random()) < log_yp - log_y: accepts += 1 x = xp ic = icp mi = mip ics[i] = (ic) mis[i] = (mi) xs[i] = (x) #print sum(site.count("A") for site in x) alphas[i] = (alpha) betas[i] = (beta) if i > 0 and i % take_stock == 0: if i < iterations / 10: mean_ic = mean(ics[i - take_stock:i]) mean_mi = mean(mis[i - take_stock:i]) alpha += eta * (des_ic - mean_ic) * exp( -i / (10 * float(iterations))) beta += eta * (des_mi - mean_mi) * exp( -i / (10 * float(iterations))) else: mean_ic = mean(ics[i - take_stock:i]) mean_mi = mean(mis[i - take_stock:i]) alpha = poly1d(polyfit(ics[:i], alphas[:i], 1))(des_ic) beta = poly1d(polyfit(mis[:i], betas[:i], 1))(des_mi) fmt_string = ( "mean ic: % 1.2f, mean mi: % 1.2f, alpha: % 1.2f, beta: % 1.2f" % (mean_ic, mean_mi, alpha, beta)) print i, "AR:", accepts / (i + 1.0), fmt_string return xs, ics, mis, alphas, betas
def mutate((tf,motif)): L = len(tf) if random.random() < 0.5: tfp = tf[:] i = random.randrange(L) tfp[i] = 1 - tf[i] return (tfp,motif) else: return tf,mutate_motif(motif)
def degrade_motif(L,n): motif = ["A"*L for i in range(n)] #motif = random_motif(L,n) column_ics = [] total_ics = [] for iteration in trange(n*L): col_ic = motif_ic(motif,correct=False) column_ics.append(col_ic) total_ics.append(col_ic + pairwise_motif_mi(motif)) motif = mutate_motif(motif) plt.plot(total_ics,column_ics) plt.plot([0,2*L],[0,2*L],linestyle='--',color='b') plt.xlabel("Total IC") plt.ylabel("Column IC") plt.title("n=%s, L=%s" % (n,L))
def flux_prob(motif, a, b, trials=10000): """determine probability of mutation pushing motif out of interval [a,b]""" ic = motif_ic(motif) lesser = 0 same = 0 greater = 0 for i in trange(trials): icp = motif_ic(mutate_motif(motif)) if icp < a: lesser += 1 elif a <= icp < b: same += 1 else: greater += 1 return lesser, same, greater
def propose_motif(matrix,motif): return matrix,mutate_motif(motif)