def concentration_vs_percentage_bound(kd, concentration_A, concentration_B_range, n_steps = 100, print_for = []): if type(kd) == float or type(kd) == int: kd = [kd] if type(concentration_A) == float or type(concentration_A) == int: concentration_A = [concentration_A] step = (concentration_B_range[1] - concentration_B_range[0])/(n_steps-1) if concentration_B_range[0] == 0: start = step else: start = concentration_B_range[0] stop = concentration_B_range[1] + step/2 concentration_B = numpy.arange(start, stop, step) xy = [0,0] plt.figure() for i in range(len(kd)): for j in range(len(concentration_A)): r = numpy.zeros(len(concentration_B)) for k in range(len(concentration_B)): a = 1 b = -(concentration_A[j] + concentration_B[k] + kd[i]) c = concentration_A[j] * concentration_B[k] xy[0], xy[1] = M.square_formula(a, b, c) for m in range(2): if xy[m] > concentration_A[j] or xy[m] > concentration_B[k]: pass else: r[k] = 100*xy[m]/concentration_B[k] if len(print_for) != 0: print("Kd: " + str(kd[i]) + " M, [A]: " + str(1000*concentration_A[j]) + " mM") for k in range(len(print_for)): index = numpy.where(concentration_B >= print_for[k]) print(" [B]: " + str(numpy.round(1000*concentration_B[index[0][0]],1)) + " mM: " + str(numpy.round(r[index[0][0]],1)) + "%") c = ["b", "g", "r"] plt.plot(1000*concentration_B, r, label = "kd: " + str(1000*kd[i]) + " mM, [A]: " + str(1000*concentration_A[j]) + " mM")#, color = c[j]) plt.title("Percentage bound as a function of concentration and Kd\nA+B <=> AB") plt.ylim(80,100) plt.xlim(1000*start, 1000*stop) plt.xlabel("[B] (mM)") plt.ylabel("Percentage bound (%)") plt.legend(loc="lower left") plt.grid(b=True, which="both") plt.show()
def protein_ligand_kinetics(kd, cA, cX, X_steps = 100, print_for_X = [], A_name = "A", X_name = "X", flag_plot = True, y_range = [0,0], verbose = True): """ For the equilibrium A + X <-> AX, with dissociation constant kd and with concentrations for A and X, calculate the percentage of how much of X is bound to A. In general, A is the protein at a known concentration and X is a range of ligand concentrations. The plot can be used to find the best concentration of the ligand for a given protein concentration. Once the ligand is added in a known concentration, the option print_for_X can be used to give the precise percentage bound. All values are in M (molar). The values are printed and plotted in micro molar. Inputs: - kd (int or list): dissociation constant - cA (int or list): concentration of A - cX (int or list): concentration of variable on X-axis - X_steps: number of steps of ligand - cX = int or float: only this concentration will be used. X_steps will be ignored - cX = [a, b, c] and X_steps =< 1: only the concentrations a, b and c will be used - cX = [a, b, c] and X_steps > 1: the concentration of X will be between cX[0] and cX[-1] in X_steps. - print_for_X: print for these concentrations of X - A_name, X_name: give the names, they will be used on the plot. This minimizes confusion. - flag_plot: If True, it will make a plot. If cX is an integer, it will not make a plot. - y_range: the plot will show the percentages in this range. Set to [0,0] to use the automatic range. """ if verbose: print("This routine calculates the percentage of " + X_name + " that is bound to " + A_name + ".") X_letter = X_name[0] A_letter = A_name[0] if type(kd) == float or type(kd) == int or type(kd) == numpy.float64: kd = [kd] if type(cA) == float or type(cA) == int or type(cA) == numpy.float64: cA = [cA] if type(cX) == float or type(cX) == int or type(cX) == numpy.float64: cX = [cX] flag_plot = False elif type(cX) == list and X_steps <= 1: pass elif type(cX) == list and X_steps > 1: step = (cX[-1] - cX[0])/(X_steps-1) if cX[0] == 0: start = step else: start = cX[0] stop = cX[-1] + step/2 cX = numpy.arange(start, stop, step) xy = [0,0] r = numpy.zeros( (len(kd),len(cA),len(cX)) ) for i in range(len(kd)): for j in range(len(cA)): for k in range(len(cX)): a = 1 b = -(cA[j] + cX[k] + kd[i]) c = cA[j] * cX[k] xy[0], xy[1] = M.square_formula(a, b, c) for m in range(2): if xy[m] > cA[j] or xy[m] > cX[k]: pass else: # percentage! r[i, j, k] = 100*xy[m]/cX[k] if len(print_for_X) != 0: for i in range(len(kd)): for j in range(len(cA)): temp = value_to_string(kd[i], "M") kd_str = str(temp[0]) + " " + temp[1] temp = value_to_string(cA[j], "M") cA_str = str(temp[0]) + " " + temp[1] print("Kd: " + kd_str + ", [" + A_letter + "]: " + cA_str) for k in range(len(print_for_X)): index = numpy.where(cX >= print_for_X[k]) temp = value_to_string(cX[index[0][0]], "M", flag_round = True) cX_str = str(temp[0]) + " " + temp[1] print(" [" + X_letter + "]: " + cX_str + ": " + str(numpy.round(r[i, j, index[0][0]],1)) + "%") if flag_plot: plt.figure() x_plot_prop = value_to_string(cX[-1], "M") for i in range(len(kd)): for j in range(len(cA)): temp = value_to_string(kd[i], "M") kd_str = str(temp[0]) + " " + temp[1] temp = value_to_string(cA[j], "M") cA_str = str(temp[0]) + " " + temp[1] plt.plot(x_plot_prop[2]*cX, r[i,j,:], label = "kd: " + kd_str + ", [" + A_letter + "]: " + cA_str) plt.title("Percentage " + X_letter + " bound to " + A_letter + " a function of concentration and Kd\n" + A_letter + "+" + X_letter + " <=> " + A_letter + X_letter) plt.ylabel("Percentage bound (%)") if y_range != [0,0]: plt.ylim(y_range[0], y_range[1]) plt.xlim(x_plot_prop[2]*cX[0], x_plot_prop[2]*cX[-1]) plt.xlabel("[" + X_letter + "] (" + x_plot_prop[1] + ")") plt.legend(loc=0)#"lower right") plt.grid(b=True, which="both") plt.show() return r, kd, cA, cX
def fit_correlation(path, mess_array, scan_array, mess_date, maxtau, A_start = [3, 10000, 1, 10], flag_write = False, flag_plot = True, debug = False): """ croc.LaserCorrelation.fit_correlation This method imports the correlation data and fits it two a double exponential. CHANGELOG: 20120223/RB: collected some scripts into this function INPUT: - path (string): the path to the correlation data. Note that this is the same as the path_output in the import_and_process function. - mess_array (array): the base file names, for example 'correlation_T300' - scan_array (array): array with the file-endings, for example ['_NR1_1.bin', '_NR1_2.bin', ...] - mess_date (int): the day of the measurement yyyymmdd - pixel (int): the pixel that should be used - maxtau (int): to determine the correct name for the file of the correlation data - A_start (array, 4 elements): starting guess for the fit of the correlation data - flag_write (BOOL, False): If True, will write a file with the fitting data - flag_plot (BOOL, True): If False, it will not plot the result. - debug (BOOL, False): If true, it will only calculate 1 correlation, not all of them. OUTPUT: - a plot of the correlation data using both methods and the corresponding fits. - a text file with the fitting data """ if debug: i_len = 1 j_len = 1 else: i_len = len(mess_array) j_len = len(scan_array) string = "" for i in range(i_len): for j in range(j_len): if flag_plot: plt.figure() for k in range(2): if k == 0: sort = "fft" else: sort = "jan_" + str(maxtau) c = Resources.IOMethods.import_data_correlation(path, mess_array, i, scan_array, j, mess_date, sort = sort) c = c[:1000] x = numpy.arange(len(c)) A = A_start A_final = M.fit(x, c, E.double_exp, A) if flag_plot: plt.plot(c, ".") plt.plot(E.double_exp(A_final, x)) plt.title(mess_array[i] + scan_array[j]) s_m = mess_array[i] s_s = scan_array[j] s_p = sort s_a = str(numpy.round(A_final[0],2)) s_t1 = str(numpy.round(A_final[1],1)) s_b = str(numpy.round(A_final[2],2)) s_t2 = str(numpy.round(A_final[3],1)) temp_string = "{0:5} {1:6} {2:8} {3:5} {4:7} {5:5} {6:7}".format(s_m, s_s, s_p, s_a, s_t1, s_b, s_t2) print(temp_string) string += temp_string if flag_plot: plt.show() if flag_write: FILE = open(path + "corr_fitting.txt", "w") FILE.write(string) FILE.close()