def main_no_rot(): plt.close("all") table_path = "C:\\Users\\brouw\\Desktop\\Data\\" table_file = "180509" measurements = ba.build_measurements(table_path + table_file + ".xlsx", p) for measurement in measurements: print("Processing measurement... " + str(measurement)) f_pull, z_pull, f_release, z_release, title = ba.read_analyze( measurement, p) f_wlc = np.logspace(np.log10(0.15), np.log10(int(np.max(f_pull))), 1000) wlc, _ = func.WLC(f_wlc, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.tick_params(direction='in', top=True, right=True) plt.ylim(-1, 65) plt.xlim(500, 2300) plt.scatter(1000 * z_pull, f_pull, color='darkgreen', label="Pull", s=10, zorder=25, facecolors='none') plt.scatter(1000 * z_release, f_release, color='lightgrey', s=10, zorder=15, label='Release', facecolors='none') plt.plot(wlc, f_wlc, '--', color="black", label="WLC", zorder=10000) plt.plot([], [], ' ', label="Drift: " + str(p['drift'])) # quick and very dirty plt.legend(loc=2, frameon=False) plt.title(title) if p['save'] == True: new_path = table_path + table_file + "\\Selected figures (no rot)\\" if not os.path.exists(new_path): os.makedirs(new_path) plt.savefig(new_path + title) # plt.show() plt.close() return
def main_assemble(): plt.close("all") plt.rcParams.update({'font.size': 28}) measurements = meas.measurements() for measurement in measurements: f_pull, z_pull, f_release, z_release, title = hmf.read_analyze_compare( measurement, p) wlc, _ = func.WLC(f_pull, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) plt.scatter(1000 * z_pull, f_pull, label=title, s=10, zorder=25) # plt.scatter(1000 * z_release, f_release, color='lightgrey', s=10, zorder=15, facecolors='none') plt.plot(wlc, f_pull, '--', color="black", label="WLC") plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.ylim(-1, 52) plt.xlim(0, 1350) # plt.yscale('log') # plt.ylim(10 ** -1, 10 ** 2) plt.legend(loc=2, frameon=False) plt.tick_params(direction='in', axis="both", bottom="on", top="on", left="on", right="on") # placement of figure mngr = plt.get_current_fig_manager() mngr.window.setGeometry(0, 56, 3840, 2004) # laptop # mngr.window.setGeometry(0, -1024, 1920, 984) # work PC # HMfB - reference # f_pull, z_pull, f_release, z_release, title = hmf.read_analyze_compare(['171201', '012', ' 5', 'HMfB'], p) # f_pull, z_pull, f_release, z_release, title = hmf.read_analyze_compare(['171201', '007', '18', 'HMfB'], p) # plt.scatter(1000 * z_pull, f_pull, label=title, s=20, zorder=100, color='black') plt.title(title[-4:] + " assembly") plt.show() return
import numpy as np import matplotlib.pyplot as plt import functions as func L_bp = 1000 P_nm = 50 S_pN = 1000 z0_nm = 0 k_pN_nm = 0.01 L_nm = L_bp * 0.34 f = np.linspace(0.1, 10, 100) z_wlc, w_wlc = func.WLC(f, L_bp=L_bp, P_nm=P_nm, S_pN=S_pN, z0_nm=z0_nm) z_fjc, w_fjc = func.FJC(f, b=2 * P_nm, k_pN_nm=k_pN_nm, L_nm=L_nm, z0_nm=0) f_z_wlc = np.full((len(f)), max(f)) f_z_fjc = np.full((len(f)), max(f)) # initialize plots fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, sharey=True) ''' W = f dz''' # # WLC -- W = f dz # ax1.plot(f, z_wlc, color='black') # ax1.fill_between(f, 0, z_wlc, color='red') # ax1.set_title('WLC -- W = f dz') # ax1.set_ylabel('Extension (nm)') # # # FJC -- W = f dz # ax2.plot(f, z_fjc, color='black')
def main_fit_no_constant(): plt.close("all") measurements = meas.measurements() f_pull, z_pull, f_release, z_release, title = hmf.read_analyze( measurements, p) wlc, _ = func.WLC(f_pull, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) Fmax_pN = np.max(f_pull) f = np.logspace(np.log10(0.02), np.log10(int(Fmax_pN)), 1000) n_prot = (p['L_bp'] // p['L1_bp']) # plot the states Z, Zi, Gi = hmf.hmf_WLC(f, pars=p) barrier = hmf.barrier(p) plt.rcParams.update({'font.size': 14}) # select high-force data select_f = f_pull[np.where((f_pull > 5) & (f_pull < 50))] select_z = z_pull[np.where((f_pull > 5) & (f_pull < 50))] select_z *= 1000 if p['two_transitions']: if p['fit_G1']: # fit the Boltzmann distribution to find 'G2' and 'constant' popt, pcov = curve_fit( lambda f, G1, G2: hmf.hmf_boltzmann_fit(f, p, G1, G2, 0), select_f, select_z, p0=[p['G1_kT'], p['G2_kT']], bounds=(0.01, [10, 100])) print("G1 = " + str(popt[0]) + " kT") print("G2 = " + str(popt[1]) + " kT") print("constant = 0") p['G1_kT'] = popt[0] p['G2_kT'] = popt[1] p['constant'] = 0 else: # fit the Boltzmann distribution to find 'G2' and 'constant' popt, pcov = curve_fit( lambda f, G2: hmf.hmf_boltzmann_fit(f, p, p['G1_kT'], G2, 0), select_f, select_z, p0=p['G2_kT'], bounds=(0.01, 100)) print("G1 = " + str(p['G1_kT']) + " kT") print("G2 = " + str(popt[0]) + " kT") print("constant = 0") p['G2_kT'] = popt[0] p['constant'] = 0 # plot the Boltzmann distribution Z = hmf.hmf_boltzmann_fit(f, p, G1=p['G1_kT'], G2=p['G2_kT'], constant=p['constant']) plt.plot(Z, f, color='k', linewidth=3, zorder=120, label="Stat.Mech.Model fit") else: # fit the Boltzmann distribution to find 'G2', 'G3' and 'constant' if p['fit_G1']: print("Starting fit procedure...") popt, pcov = curve_fit( lambda f, G1, G2, G3: hmf.hmf_boltzmann_fit_three( f, p, G1, G2, G3, 0), select_f, select_z, p0=[p['G1_kT'], p['G2_kT'], p['G3_kT']], bounds=(0.01, [10, 100, 100])) print("Done!") print("G1 = " + str(popt[0]) + " kT") print("G2 = " + str(popt[1]) + " kT") print("G3 = " + str(popt[2]) + " kT") print("constant = 0") p['G1_kT'] = popt[0] p['G2_kT'] = popt[1] p['G3_kT'] = popt[2] p['constant'] = 0 else: popt, pcov = curve_fit( lambda f, G2, G3: hmf.hmf_boltzmann_fit_three( f, p, p['G1_kT'], G2, G3, 0), select_f, select_z, p0=[p['G2_kT'], p['G3_kT']], bounds=(0.01, [100, 100])) print("G1 = " + str(p['G1_kT']) + " kT") print("G2 = " + str(popt[0]) + " kT") print("G3 = " + str(popt[1]) + " kT") print("constant = 0") p['G2_kT'] = popt[0] p['G3_kT'] = popt[1] p['constant'] = 0 # plot the Boltzmann distribution Z = hmf.hmf_boltzmann_fit_three(f, p, G1=p['G1_kT'], G2=p['G2_kT'], G3=p['G3_kT'], constant=p['constant']) plt.plot(Z, f, color='k', linewidth=3, zorder=120, label="Stat.Mech.Model fit") # plot the grid colors = iter(cm.rainbow(np.linspace(0, 1, len(Zi)))) for n, z in enumerate(Zi): plt.plot(z, f, color=next(colors), linewidth=2, zorder=int(-n)) plt.plot(Zi[0], f, '--', color='lightgrey', linewidth=4, zorder=1000, label='state 0') plt.plot(Zi[barrier], f, '--', color='grey', linewidth=4, zorder=1000, label='state 1') plt.plot(wlc, f_pull, '--', color="black", linewidth=4, zorder=10000, label='state 2') if p['two_transitions'] == False: plt.plot(Zi[barrier + n_prot - p['dimer_beads']], f, '--', color='dimgrey', linewidth=4, zorder=1000, label='state 1.5') # plot the data if title[-4:] == "HMfA": pull_color = 'navy' release_color = 'lightblue' else: pull_color = 'darkgreen' release_color = 'lightgrey' plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.ylim(-0.5, 5) plt.xlim(0, 1000) plt.ylim(-1, 52) plt.xlim(0, 1350) plt.yscale('log') plt.ylim(10**-1, 10**2) plt.scatter(1000 * z_pull, f_pull, color=pull_color, label="Pull", s=200, zorder=25, facecolors='none') plt.scatter(1000 * z_release, f_release, color=release_color, s=200, zorder=15, label='Release', facecolors='none') plt.legend(loc=2, frameon=False) plt.tick_params(direction='in', axis="both", bottom="on", top="on", left="on", right="on") plt.title(title[-4:]) # placement of figure mngr = plt.get_current_fig_manager() mngr.window.setGeometry(0, 56, 3840, 2004) # laptop mngr.window.setGeometry(0, -1024, 1920, 984) # work PC plt.show() return
def main(): plt.close("all") measurements = meas.measurements() f_pull, z_pull, f_release, z_release, title = hmf.read_analyze( measurements, p) wlc, _ = func.WLC(f_pull, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) Fmax_pN = np.max(f_pull) f = np.logspace(np.log10(0.02), np.log10(int(Fmax_pN)), 1000) n_prot = (p['L_bp'] // p['L1_bp']) # plot the states Z, Zi, Gi = hmf.hmf_WLC(f, pars=p) barrier = hmf.barrier(p) skip = 1 Gi = Gi[::skip] Zi = Zi[::skip] plt.rcParams.update({'font.size': 14}) ''' Force Extension plot ''' # plot the grid colors = iter(cm.rainbow(np.linspace(0, 1, len(Zi)))) for n, z in enumerate(Zi): plt.plot(z, f, color=next(colors), linewidth=2, zorder=int(-n)) plt.plot(Zi[0], f, '--', color='lightgrey', linewidth=2, zorder=1000, label='state 0') plt.plot(Zi[barrier], f, '--', color='grey', linewidth=2, zorder=1000, label='state 1') # if p['two_transitions'] == False: # plt.plot(Zi[barrier + n_prot - p['dimer_beads']], f, '--', color='dimgrey', linewidth=4, zorder=1000, # label='state 1.5') plt.plot(wlc, f_pull, '--', color="black", linewidth=2, zorder=10000, label='state 2') # plt.plot(Zi[121-45], f, '--', color='red', linewidth=2, zorder=1000, label = "Kulic") # Kulic Requirement # plot the Boltzmann distribution # Z_b = hmf.hmf_boltzmann_fit_three_fix(f, p, p['G1_kT'], p['G2_kT'], p['constant'], p['constant_wlc']) # plt.plot(Z_b, f, color='k', linewidth=3, zorder=120, label="Stat.Mech.Model fit") # plot the data if title[-4:] == "HMfA": pull_color = 'navy' release_color = 'lightblue' else: pull_color = 'darkgreen' release_color = 'lightgrey' plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.tick_params(direction='in', top=True, right=True) plt.ylim(-0.5, 5) plt.xlim(0, 1000) plt.ylim(-1, 52) plt.xlim(0, 1350) plt.yscale('log') plt.ylim(10**-1, 10**2) plt.scatter(1000 * z_pull, f_pull, color=pull_color, label="Pull", s=100, zorder=25, facecolors='none') plt.scatter(1000 * z_release, f_release, color=release_color, s=100, zorder=15, label='Release', facecolors='none') # plt.plot(wlc, f_pull, '--', color="black", label="WLC", zorder=10000) plt.legend(loc=2, frameon=False) plt.tick_params(direction='in', axis="both", bottom="on", top="on", left="on", right="on") # plt.title(title[-4:] + ', G2 = ' + str(p['G2_kT']) + ' kT, G3 = ' + str(p['G3_kT']) + ' kT') # plt.title(title[-4:]) save = p['save'] # placement of figure mngr = plt.get_current_fig_manager() mngr.window.setGeometry(0, 56, 3840, 2004) # laptop # mngr.window.setGeometry(0, -1024, 1920, 984) # work PC # if save: # # save figure # plt.rcParams.update({'font.size': 14}) # plt.savefig('C:\\Brouwer\\HMf analysis\\Fits\\' + str(title) + '__' + time.strftime("%H%M%S") + ".png", dpi=300) # # save parameters # with open('C:\\Brouwer\\HMf analysis\\Fits\\' + str(title) + '__' + time.strftime("%H%M%S") + '.json', # 'w') as ff: # ff.write(json.dumps(p)) plt.show() # plt.close() ''' Energy Plot ''' sequence = False if sequence: for F_ref in range(0, 151): F_ref /= 10 plt.title(' F = ' + str(F_ref) + ' pN', ha=u'left') colors = iter(cm.rainbow(np.linspace(0, 1, len(Zi)))) for g, z in zip(Gi, Zi): g_tot = g - F_ref * z / kT plt.plot(z, g_tot, color=next(colors), linewidth=2) # ax[1].plot(z_ref, w_ref, color = 'k', linewidth = 2) plt.ylim(-3000, 3000) plt.xlim(0, 1350) plt.ylabel('G (kT)') plt.xlabel('z (nm)') plt.tick_params(direction='in', top=True, right=True) plt.savefig("C:\\Users\\brouw\\Desktop\\Figs\\Free Energy @ " + str(F_ref) + " pN.png") plt.tight_layout() plt.close() # plt.show() else: F_ref = 30 plt.title(' F = ' + str(F_ref) + ' pN', ha=u'left') colors = iter(cm.rainbow(np.linspace(0, 1, len(Zi)))) for g, z in zip(Gi, Zi): g_tot = g - F_ref * z / kT plt.plot(z, g_tot, color=next(colors), linewidth=2) plt.ylim(-7000, -6000) plt.xlim(0, 1350) plt.ylabel('G (kT)') plt.xlabel('z (nm)') plt.tick_params(direction='in', top=True, right=True) # plt.tight_layout() # plt.show() plt.close() ''' # plotting the difference between states diff_FJC = [] diff_WLC = [] diff_tot = [] for z_fjc, z_wlc, z_tot in zip(Z_FJC, Z_WLC, Zi): diff_FJC.append(max(z_fjc)) diff_WLC.append(max(z_wlc)) diff_tot.append(max(z_tot)) diff_FJC = [j - i for i, j in zip(diff_FJC[:-1], diff_FJC[1:])] diff_WLC = [j - i for i, j in zip(diff_WLC[:-1], diff_WLC[1:])] diff_tot = [j - i for i, j in zip(diff_tot[:-1], diff_tot[1:])] x = np.arange(len(diff_WLC)) plt.scatter(x, diff_FJC, label = "FJC") plt.scatter(x, diff_WLC, label = "WLC") plt.scatter(x, diff_tot, marker='x', s = 115, label = "Total") plt.ylabel("Max distance between states (nm)") plt.xlabel("States") plt.legend() plt.show() ''' return
def main_compare(): plt.close("all") plt.rcParams.update({'font.size': 45}) plt.rc('axes', linewidth=4) plt.figure(figsize=(17.5, 22)) plt.rcParams['xtick.major.size'] = 20 plt.rcParams['xtick.major.width'] = 4 plt.rcParams['xtick.minor.size'] = 10 plt.rcParams['xtick.minor.width'] = 2 plt.rcParams['ytick.major.size'] = 20 plt.rcParams['ytick.major.width'] = 4 plt.rcParams['ytick.minor.size'] = 10 plt.rcParams['ytick.minor.width'] = 2 plt.tick_params(direction='in', top=True, right=True) p = default_pars() measurements = meas.measurements() for n, measurement in enumerate(measurements): if p['load_pars'] == True: par_dir = "C:\\Brouwer\\HMf analysis\\Fits\\" par_file = str(measurement[0]) + "_" + str( measurement[1]) + "_" + str(measurement[2]) + "_" + str( measurement[3]) + ".json" # open parameters with open(par_dir + par_file, 'r') as read_file: loaded_parameters = json.loads(read_file.read()) p = loaded_parameters print(measurement) print(p) f_pull, z_pull, f_release, z_release, title = hmf.read_analyze_compare( measurement, p) wlc, _ = func.WLC(f_pull, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) if title[-4:] == "HMfA": pull_color = 'red' release_color = 'grey' else: pull_color = 'forestgreen' release_color = 'grey' plt.scatter(1000 * z_pull, f_pull, color=pull_color, label=title[-4:], s=500, linewidth=2, zorder=25, alpha=0.7, facecolors='none') plt.scatter(1000 * z_release, f_release, color=release_color, s=500, linewidth=2, zorder=15, alpha=0.7, facecolors='none') Fmax_pN = np.max(f_pull) f = np.logspace(np.log10(0.02), np.log10(int(Fmax_pN)), 1000) # plot the Boltzmann distribution Z_b = hmf.hmf_boltzmann_fit(f, p, p['G1_kT'], p['G2_kT'], p['constant']) # if n == 0: # plt.plot(Z_b, f, color='black', linewidth=6, zorder=120, label="Stat.Mech.Model fit") # else: # plt.plot(Z_b, f, color='black', linewidth=6, zorder=120) plt.plot(Z_b, f, color='black', linewidth=10, zorder=10000) # plot the states Z, Zi, Gi = hmf.hmf_WLC(f, pars=p) barrier = hmf.barrier(p) # plot the grid # colors = iter(cm.rainbow(np.linspace(0, 1, len(Zi)))) # for n, z in enumerate(Zi): # plt.plot(z, f, color=next(colors), linewidth=2, zorder=int(-n)) # if p['two_transitions'] == False: # plt.plot(Zi[barrier + n_prot - p['dimer_beads']], f, '--', color='dimgrey', linewidth=4, zorder=1000, # label='state 1.5') # plt.plot(Zi[0], f, '--', color='lightgrey', linewidth=5, zorder=1000, label='state 0') # plt.plot(Zi[barrier], f, '--', color='grey', linewidth=5, zorder=1000, label='state 1') plt.plot(wlc, f_pull, '--', color="black", linewidth=5, zorder=10000, label='WLC') plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.ylim(-1, 52) plt.xlim(0, 1500) plt.yscale('log') plt.ylim(10**-1, 10**2) plt.xticks(range(0, 1501, 500)) plt.legend(loc=2, frameon=False) plt.tick_params(direction='in', axis="both", bottom="on", top="on", left="on", right="on") # placement of figure # mngr = plt.get_current_fig_manager() # mngr.window.setGeometry(0, 56, 3840, 2004) # laptop # mngr.window.setGeometry(0, -1024, 1920, 984) # work PC # plt.title("HMfA / HMfB comparison") plt.savefig('C:\\Brouwer\\HMf analysis\\Figure 2A - HMfA-HMfB comparison', dpi=600) plt.savefig( 'C:\\Brouwer\\HMf analysis\\Figure 2A - HMfA-HMfB comparison.pdf') plt.show() return
def main_fit_three(): plt.close("all") measurements = meas.measurements() f_pull, z_pull, f_release, z_release, title = hmf.read_analyze( measurements, p) wlc, _ = func.WLC(f_pull, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) Fmax_pN = np.max(f_pull) f = np.logspace(np.log10(0.02), np.log10(int(Fmax_pN)), 1000) n_prot = (p['L_bp'] // p['L1_bp']) # plot the states Z, Zi, Gi = hmf.hmf_WLC(f, pars=p) barrier = hmf.barrier(p) plt.rcParams.update({'font.size': 28}) # select high-force data lower_bound = 0.5 higher_bound = 50 select_f = f_pull[np.where((f_pull > lower_bound) & (f_pull < higher_bound))] select_z = z_pull[np.where((f_pull > lower_bound) & (f_pull < higher_bound))] select_z *= 1000 if p['two_transitions']: if p['fit_G1']: if p['fit_G1_only']: # fit the Boltzmann distribution to find 'G2' and 'constant' popt, pcov = curve_fit(lambda f, G1: hmf.hmf_boltzmann_fit( f, p, G1, p['G2_kT'], p['constant']), select_f, select_z, p0=p['G1_kT'], bounds=(0.01, 10)) p['G1_kT'] = popt[0] p['fitted_G1_var'] = pcov[0][0] print("G1 = " + str(round(p['G1_kT'], 2)) + " +/- " + str(round(p['fitted_G1_var'], 2)) + " kT") print("G2 = " + str(p['G2_kT']) + " kT") print("constant = " + str(p['constant'])) else: # fit the Boltzmann distribution to find 'G2' and 'constant' popt, pcov = curve_fit( lambda f, G1, G2, constant: hmf.hmf_boltzmann_fit( f, p, G1, G2, constant), select_f, select_z, p0=[p['G1_kT'], p['G2_kT'], p['constant']], bounds=(0.01, [10, 100, 1])) p['G1_kT'] = popt[0] p['G2_kT'] = popt[1] p['constant'] = popt[2] p['fitted_G1_var'] = pcov[0][0] p['fitted_G2_var'] = pcov[1][1] p['fitted_constant_var'] = pcov[2][2] print("G1 = " + str(round(p['G1_kT'], 2)) + " +/- " + str(round(p['fitted_G1_var'], 2)) + " kT") print("G2 = " + str(round(p['G2_kT'], 2)) + " +/- " + str(round(p['fitted_G2_var'], 2)) + " kT") print("constant = " + str(round(p['constant'], 2)) + " +/- " + str(round(p['fitted_constant_var'], 2))) else: # fit the Boltzmann distribution to find 'G2' and 'constant' popt, pcov = curve_fit( lambda f, G2, constant: hmf.hmf_boltzmann_fit( f, p, p['G1_kT'], G2, constant), select_f, select_z, p0=[p['G2_kT'], p['constant']], bounds=(0.01, [100, 1])) p['G2_kT'] = popt[0] p['constant'] = popt[1] p['fitted_G2_var'] = pcov[0][0] p['fitted_constant_var'] = pcov[1][1] print("G1 = " + str(p['G1_kT']) + " kT") print("G2 = " + str(round(p['G2_kT'], 2)) + " +/- " + str(round(p['fitted_G2_var'], 2)) + " kT") print("constant = " + str(round(p['constant'], 2)) + " +/- " + str(round(p['fitted_constant_var'], 2))) # plot the Boltzmann distribution Z = hmf.hmf_boltzmann_fit(f, p, G1=p['G1_kT'], G2=p['G2_kT'], constant=p['constant']) plt.plot(Z, f, color='k', linewidth=3, zorder=120, label="Stat.Mech.Model fit") else: # fit the Boltzmann distribution to find 'G2', 'G3' and 'constant' if p['fit_G1']: print("Starting fit procedure...") t1 = time.time() popt, pcov = curve_fit( lambda f, G1, G2, constant, constant_wlc: hmf.hmf_boltzmann_fit_three_fix( f, p, G1, G2, constant, constant_wlc), select_f, select_z, p0=[p['G1_kT'], p['G2_kT'], p['constant'], p['constant_wlc']], bounds=(0.01, [10, 100, 1, 1])) t2 = time.time() t_tot = t2 - t1 print("Done! Elapsed time: " + str(round(t_tot, 2)) + " seconds") p['G1_kT'] = popt[0] p['G2_kT'] = popt[1] p['constant'] = popt[2] p['constant_wlc'] = popt[3] p['fitted_G1_var'] = pcov[0][0] p['fitted_G2_var'] = pcov[1][1] p['fitted_constant_var'] = pcov[2][2] p['fitted_constant_wlc_var'] = pcov[3][3] print("G1 = " + str(round(p['G1_kT'], 2)) + " +/- " + str(round(p['fitted_G1_var'], 2)) + " kT") print("G2 = " + str(round(p['G2_kT'], 2)) + " +/- " + str(round(p['fitted_G2_var'], 2)) + " kT") print("constant = " + str(round(p['constant'], 2)) + " +/- " + str(round(p['fitted_constant_var'], 2))) print("constant_wlc = " + str(round(p['constant_wlc'], 2)) + " +/- " + str(round(p['fitted_constant_wlc_var'], 2))) else: # ''' print("Starting fit procedure...") t1 = time.time() popt, pcov = curve_fit( lambda f, G2, constant_wlc: hmf.hmf_boltzmann_fit_three_fix( f, p, p['G1_kT'], G2, p['constant'], constant_wlc), select_f, select_z, p0=[p['G2_kT'], p['constant_wlc']], bounds=(0.001, [100, 1])) t2 = time.time() t_tot = t2 - t1 print("Done! Elapsed time: " + str(round(t_tot, 2)) + " seconds") p['G2_kT'] = popt[0] p['constant_wlc'] = popt[1] p['fitted_G2_var'] = pcov[0][0] p['fitted_constant_wlc_var'] = pcov[1][1] print("G1 = " + str(p['G1_kT']) + " kT") print("G2 = " + str(round(p['G2_kT'], 2)) + " +/- " + str(round(p['fitted_G2_var'], 2)) + " kT") print("constant = " + str(round(p['constant'], 2))) print("constant_wlc = " + str(round(p['constant_wlc'], 2)) + " +/- " + str(round(p['fitted_constant_wlc_var'], 2))) ''' print("Starting fit procedure...") t1 = time.time() popt, pcov = curve_fit( lambda f, G2 : hmf.hmf_boltzmann_fit_three_fix(f, p, p['G1_kT'], G2, p['constant'], p['constant_wlc']), select_f, select_z, p0=p['G2_kT'], bounds=(0.001, 100)) t2 = time.time() t_tot = t2 - t1 print("Done! Elapsed time: " + str(round(t_tot,2)) + " seconds") p['G2_kT'] = popt[0] p['fitted_G2_var'] = pcov[0][0] print("G1 = " + str(p['G1_kT']) + " kT") print("G2 = " + str(round(p['G2_kT'], 2)) + " +/- " + str(round(p['fitted_G2_var'], 2)) + " kT") print("constant = " + str(round(p['constant'], 2))) print("constant_wlc = " + str(round(p['constant_wlc'], 2))) ''' # plot the Boltzmann distribution Z = hmf.hmf_boltzmann_fit_three_fix(f, p, G1=p['G1_kT'], G2=p['G2_kT'], constant_fjc=p['constant'], constant_wlc=p['constant_wlc']) plt.plot(Z, f, color='k', linewidth=3, zorder=120, label="Stat.Mech.Model fit") # plot the grid colors = iter(cm.rainbow(np.linspace(0, 1, len(Zi)))) for n, z in enumerate(Zi): plt.plot(z, f, color=next(colors), linewidth=2, zorder=int(-n)) plt.plot(Zi[0], f, '--', color='lightgrey', linewidth=4, zorder=1000, label='state 0') plt.plot(Zi[barrier], f, '--', color='grey', linewidth=4, zorder=1000, label='state 1') plt.plot(wlc, f_pull, '--', color="black", linewidth=4, zorder=10000, label='state 2') if p['two_transitions'] == False: plt.plot(Zi[barrier + n_prot - p['dimer_beads']], f, '--', color='dimgrey', linewidth=4, zorder=1000, label='state 1.5') # plot the data if title[-4:] == "HMfA": pull_color = 'navy' release_color = 'lightblue' else: pull_color = 'darkgreen' release_color = 'lightgrey' plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.ylim(-0.5, 5) plt.xlim(0, 1000) plt.ylim(-1, 52) plt.xlim(0, 1350) plt.yscale('log') plt.ylim(10**-1, 10**2) plt.scatter(1000 * z_pull, f_pull, color=pull_color, label="Pull", s=200, zorder=25, facecolors='none') plt.scatter(1000 * z_release, f_release, color=release_color, s=200, zorder=15, label='Release', facecolors='none') plt.legend(loc=2, frameon=False) plt.tick_params(direction='in', axis="both", bottom="on", top="on", left="on", right="on") plt.title(title[-4:]) save = p['save'] if save: # save figure plt.savefig('C:\\Brouwer\\HMf analysis\\Fits\\' + str(title) + ".png") # save parameters with open('C:\\Brouwer\\HMf analysis\\Fits\\' + str(title) + '.json', 'w') as ff: ff.write(json.dumps(p)) # placement of figure # mngr = plt.get_current_fig_manager() # mngr.window.setGeometry(0, 56, 3840, 2004) # laptop # mngr.window.setGeometry(0, -1024, 1920, 984) # work PC plt.show() return
def batch_analysis(beads, data_lines, headers, time, diff_magnet, force, title, file_name, analysis_path): # constants kBT = 4.114 # (pN nm) - Boltzmann factor Lc = 1500 # contour length (bp) p = 50 # persistence length (nm) S = 1000 # stretch modulus (pN) L = Lc * 0.34 # contour length (nm) x0 = 0 # offset (nm) for bead in range(0, beads): print("Processing bead " + str(bead) + " of " + str(beads)) # load the data Z = [] for x in data_lines: Z.append(float(x.split()[headers.index('Z' + str(bead) + ' (um)')])) # calculate drift for the individual bead slope = func.calc_drift_self(data_lines, headers, time, bead) # correcting drift Z_drift = [] for n, t in enumerate(time): Z_drift.append(Z[n] - (slope / 1000) * t) Z = np.array(Z_drift) # split the data in pull/release-curve f_pull = [] f_release = [] z_pull = [] z_release = [] time_pull = [] time_release = [] trigger = [] # from what data point does the pulling trace start # if the differential of the magnet is positive -> pull, else -> release ('factor' since 0 does not work) for n, i in enumerate(diff_magnet): factor = max(diff_magnet / 1000) if i < -factor: trigger.append(n) f_pull.append(force[n]) z_pull.append(Z[n]) time_pull.append(time[n]) if i > factor: f_release.append(force[n]) z_release.append(Z[n]) time_release.append(time[n]) # wlc for reference wlc = [] for f in f_pull: wlc.append(func.WLC(f, p, L, S, x0)) # select data select_f = [] select_z = [] for n, f in enumerate(f_pull): if 20 < f < 30: select_f.append(f) select_z.append(Z[n + min(trigger)]) # initial guesses x_init = 1 # fit the WLC in fashion (x,y) - only fit offset, fix everything else popt, pcov = curve_fit(lambda f, x0: func.WLC(f, p, L, S, x0), select_f, select_z, p0=(x_init)) std = np.sqrt(np.diag(pcov)) # returns the standard deviation x_fit = popt[0] z_pull -= x_fit # subtract fitted offset from data z_release -= x_fit # subtract fitted offset from data select_z -= x_fit a = np.percentile(z_pull, 1) dZ = "{0:.3f}".format(a - np.percentile(z_pull, 99)) # plotting + saving # marker_size = 10 # # plt.subplot(2, 1, 1) # # plt.title(str(title) + " / " + str(file_name) + " / bead " + str(bead) + " (dZ = " + str(dZ) + " nm)") # plt.scatter(time_pull, z_pull-a, facecolor='None', edgecolors="darkgreen", s=marker_size) # plt.scatter(time_release, z_release-a, facecolor='None', edgecolors="darkgrey", s=marker_size) # plt.ylim(0, 0.75) # plt.xlabel("Time (s)") # plt.ylabel("Extension ($\mu$m)") # # plt.subplot(2, 2, 3) # # plt.plot(wlc, f_pull, color='black', zorder=100) # plt.scatter(z_pull, f_pull, facecolor='None', edgecolors="darkgreen", s = marker_size) # plt.ylabel("Force (pN)") # plt.xlabel("Extension ($\mu$m)") # plt.xlim(0, 0.75) # # plt.subplot(2, 2, 4) # # # plt.plot(wlc, f_pull, color='black', zorder=100) # plt.scatter(z_release, f_release, facecolor='None', edgecolors="darkgrey", s = marker_size) # plt.ylabel("Force (pN)") # plt.xlabel("Extension ($\mu$m)") # plt.xlim(0, 0.75) # # plt.savefig(analysis_path + "dZ_" + str(dZ) + "_" + str(title) + "_" + str(file_name) + "_bead" + str(bead) + '_subplot.png', dpi=300, bbox_inches='tight') # # plt.close() return
z_pull = z[np.where((diff_force > factor))] z_release = z[np.where((diff_force < factor))] time_pull = time[np.where(diff_force > factor)] time_release = time[np.where((diff_force < factor))] z_fit_pull = z_fit[np.where((diff_force > factor))] T1 = T1[np.where((diff_force > factor))] T2 = T2[np.where((diff_force > factor))] T3 = T3[np.where((diff_force > factor))] transitions = np.stack((T1, T2, T3)) # all transitions in a 3D array if measurement[1] == "197x15": # wlc f_wlc = np.linspace(0.1,35, 1000) z_wlc, _ = func.WLC(f_wlc, L_bp=4985, S_pN=900) ax1 = fig.add_subplot(1, 2, 2) ax1.scatter(z_pull, f_pull, label=measurement[1], color='blue', s=100, zorder=25, facecolors='none', alpha=0.5) for t in range(len(np.transpose(transitions[0]))): plt.plot(np.transpose(transitions[2])[t], f_pull, '--', color='lightgrey') # transition 3 # ax1.scatter(z_release, f_release, color='lightgrey', s=30, zorder=15, facecolors='none', alpha=0.5) ax1.plot(z_fit_pull, f_pull, color='black', linewidth=3, zorder=1000) ax1.plot(z_wlc/1000, f_wlc, color='black', linewidth=3, zorder=10) # ax1.legend(loc=2, frameon=False) ax1.set_xlim(-0.1, 1.75) ax1.set_ylim(-1, 35) ax1.tick_params(direction='in', length=6, width=3, top=True, right=True) ax1.xaxis.set_ticks(np.arange(0, 2, 0.5)) ax1.yaxis.set_ticks(np.arange(0, 40, 10))
def main_fitfiles_single(): fitfile_path = "C:\\Users\\brouw\\Desktop\\Data\\" fitfiles = [] os.chdir(fitfile_path) for file in glob.glob("*.fit"): fitfiles.append(file) ass_fit_pars = [] ass_fit_errors = [] for fitfile in fitfiles: print("Processing fitfile... " + str(fitfile)) title = fitfile[:6] f_pull, f_release, z_pull, z_release, z_fit_pull, transitions = ba.read_fitfiles( fitfile_path, fitfile, p) f_wlc = np.logspace(np.log10(0.15), np.log10(int(np.max(f_pull))), 1000) wlc, _ = func.WLC(f_wlc, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) # read pars from logfile logfile = fitfile[:-3] + "log" fit_pars, fit_errors, table = ba.read_logfile(fitfile_path, logfile) ass_fit_pars.append(fit_pars) ass_fit_errors.append(fit_errors) # print pars in figure report = str(table[0]) + '\n' + str(table[1]) + '\n' + str( table[2]) + '\n' + str(table[3]) + '\n' + str( table[4]) + '\n' + str(table[5]) plt.annotate(report, xy=(0, 0.75), xytext=(12, -12), va='top', xycoords='axes fraction', textcoords='offset points') # plot transitions for t in range(len(np.transpose(transitions[0]))): # plt.plot(np.transpose(transitions[0])[t],f_trans,'--',color='lightgrey') # transition 1 # plt.plot(np.transpose(transitions[1])[t],f_trans,'--',color='lightgrey') # transition 2 plt.plot(np.transpose(transitions[2])[t], f_pull, '--', color='lightgrey') # transition 3 plt.ylabel('F (pN)') plt.xlabel('z (nm)') plt.tick_params(direction='in', top=True, right=True) plt.ylim(-1, 60) plt.xlim(0, 1.8) plt.scatter(z_pull, f_pull, color='darkgreen', label="Pull", s=10, zorder=25, facecolors='none') plt.scatter(z_release, f_release, color='lightgrey', s=10, zorder=15, label='Release', facecolors='none') plt.plot(wlc / 1000, f_wlc, '--', color="black", label="WLC", zorder=100) plt.plot(z_fit_pull, f_pull, color='black', linewidth=2, label="Stat. Mech. Model fit", zorder=1000) plt.legend(loc=2, frameon=False) plt.title(fitfile[:-4] + " - " + p['NRL_str']) # TODO fix the offset in title if p['save'] == True: new_path = fitfile_path + "\\" + fitfile[:6] + "\\Fitfile figures (single)\\" if not os.path.exists(new_path): os.makedirs(new_path) plt.savefig(new_path + fitfile[:-4]) # plt.show() plt.close() # assemble parameters into histogram if p['save'] == True: ba.plot_hist(ass_fit_pars, ass_fit_errors, title, new_path, p, show_plot=False) return
def main(): plt.close("all") table_path = "C:\\Users\\brouw\\Desktop\\Data\\" table_file = "180612_168" measurements = ba.build_measurements(table_path, table_file + ".txt", p) drift_arr = [] z_offset_arr = [] for measurement in measurements: print("Processing measurement... " + str(measurement)) f_pull, z_pull, f_release, z_release, title, drift, z_offset = ba.read_analyze( measurement, p) f_wlc = np.logspace(np.log10(0.15), np.log10(int(np.max(f_pull))), 1000) wlc, _ = func.WLC(f_wlc, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) drift_FE = p['drift'] drift_arr.append(drift) z_offset_arr.append(z_offset) twist_pos, twist_neg, z_pos, z_neg, lnd_pos, lnd_neg = ba.read_analyze_rot( measurement, p) drift_rot = p['drift'] fig = plt.figure(figsize=(15, 5)) # rotation ax1 = fig.add_subplot(1, 2, 1) ax1.set_ylabel('z (nm)') ax1.set_xlabel('$\sigma$') ax1.tick_params(direction='in', top=True, right=True) ax1.set_xlim(-0.025, 0.025) ax1.set_ylim(-500, 500) # twist on x-axis # ax1.scatter(twist_pos, 1000*z_pos, color='darkgreen', label="Forward twisting", s=10, zorder=25, facecolors='none') # ax1.scatter(twist_neg, 1000*z_neg, color='lightgrey', label='Backward twisting', s=10, zorder=15, facecolors='none') # LND on x-axis ax1.scatter(lnd_pos, 1000 * z_pos, color='darkgreen', label="Forward twisting", s=10, zorder=25, facecolors='none') ax1.scatter(lnd_neg, 1000 * z_neg, color='lightgrey', label='Backward twisting', s=10, zorder=15, facecolors='none') ax1.plot([], [], ' ', label="Drift: " + str(drift_rot)) # quick and very dirty ax1.legend(loc=2, frameon=False) ax1.set_title("Twist @ 1 pN") # force-extension ax2 = fig.add_subplot(1, 2, 2) ax2.set_ylabel('F (pN)') ax2.set_xlabel('z (nm)') ax2.tick_params(direction='in', top=True, right=True) ax2.set_ylim(-1, 65) ax2.set_xlim(0, 2000) ax2.plot([], [], ' ', label="Drift: " + str(drift_FE)) # quick and very dirty ax2.scatter(1000 * z_pull, f_pull, color='darkgreen', label="Pull", s=10, zorder=25, facecolors='none') ax2.scatter(1000 * z_release, f_release, color='lightgrey', s=10, zorder=15, label='Release', facecolors='none') ax2.plot(wlc, f_wlc, '--', color="black", label="WLC", zorder=10000) ax2.legend(loc=2, frameon=False) ax2.set_title("Force Extension") fig.suptitle(title) if p['save'] == True: new_path = table_path + table_file[: 6] + "\\Selected figures" + table_file[ 6:] + "\\" if not os.path.exists(new_path): os.makedirs(new_path) fig.savefig(new_path + title) # fig.show() plt.close() if p['save'] == True: measurements = np.transpose(measurements) measurements = np.append(measurements, np.full((len(z_offset_arr)), p['L_bp'])) # append length measurements = np.append(measurements, np.full((len(z_offset_arr)), p['NRL'])) # append NRL measurements = np.append(measurements, np.full((len(z_offset_arr)), p['repeats'])) # append repeats measurements = np.append(np.transpose(measurements), z_offset_arr) # append z-offset measurements = np.transpose( np.append(measurements, drift_arr).reshape( (9, -1))) # append drift headers = [ "date", "data_", "bead", "type", "length (bp)", "NRL", "repeats", "z-offset", "drift" ] measurements = np.append(headers, measurements).reshape((-1, 9)) with open(new_path + table_file + "_list_of_measurements.txt", "w+") as my_csv: csvWriter = csv.writer(my_csv, delimiter='\t') csvWriter.writerows(measurements) # TODO fix why pycharm closes figures return
def main_fitfiles(): fitfile_path = "C:\\Users\\brouw\\Desktop\\Data\\" fitfiles = [] os.chdir(fitfile_path) for file in glob.glob("*.fit"): fitfiles.append(file) ass_fit_pars = [] ass_fit_errors = [] for fitfile in fitfiles: print("Processing fitfile... " + str(fitfile)) f_pull, f_release, z_pull, z_release, z_fit_pull, transitions = ba.read_fitfiles( fitfile_path, fitfile, p) f_wlc = np.logspace(np.log10(0.15), np.log10(int(np.max(f_pull))), 1000) wlc, _ = func.WLC(f_wlc, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) # read pars from logfile logfile = fitfile[:-3] + "log" fit_pars, fit_errors, table = ba.read_logfile(fitfile_path, logfile) ass_fit_pars.append(fit_pars) ass_fit_errors.append(fit_errors) fig = plt.figure(figsize=(30, 10)) plt.rcParams.update({'font.size': 20}) plt.rc('axes', linewidth=3) # number of nucleosomes ax0 = fig.add_subplot(1, 2, 1) ax0.set_ylabel('F (pN)') ax0.set_xlabel('z (nm)') ax0.tick_params(direction='in', top=True, right=True) ax0.set_ylim(0, 6) ax0.set_xlim(0.25, 1.25) ax0.set_title("Zoom in") ax0.scatter(z_pull, f_pull, color='darkgreen', label="Pull", s=30, zorder=25, facecolors='none') ax0.scatter(z_release, f_release, color='lightgrey', s=30, zorder=15, label='Release', facecolors='none') ax0.plot(wlc / 1000, f_wlc, '--', color="black", label="WLC", zorder=100) ax0.plot(z_fit_pull, f_pull, color='black', linewidth=3, label="Stat. Mech. Model fit", zorder=1000) ax0.legend(loc=2, frameon=False) # number of tetrasomes ax1 = fig.add_subplot(1, 2, 2) ax1.set_ylabel('F (pN)') ax1.set_xlabel('z (nm)') ax1.tick_params(direction='in', top=True, right=True) ax1.set_ylim(-1, 60) ax1.set_xlim(0, 1.8) ax1.set_title("Zoom out") # print pars in figure report = str(table[0]) + '\n' + str(table[1]) + '\n' + str( table[2]) + '\n' + str(table[3]) + '\n' + str( table[4]) + '\n' + str(table[5]) ax1.annotate(report, xy=(0, 0.75), xytext=(12, -12), va='top', xycoords='axes fraction', textcoords='offset points') # plot transitions for t in range(len(np.transpose(transitions[0]))): # ax1.plot(np.transpose(transitions[0])[t],f_trans,'--',color='lightgrey') # transition 1 # ax1.plot(np.transpose(transitions[1])[t],f_trans,'--',color='lightgrey') # transition 2 ax1.plot(np.transpose(transitions[2])[t], f_pull, '--', color='lightgrey') # transition 3 ax1.scatter(z_pull, f_pull, color='darkgreen', label="Pull", s=30, zorder=25, facecolors='none') ax1.scatter(z_release, f_release, color='lightgrey', s=30, zorder=15, label='Release', facecolors='none') ax1.plot(wlc / 1000, f_wlc, '--', color="black", label="WLC", zorder=100) ax1.plot(z_fit_pull, f_pull, color='black', linewidth=3, label="Stat. Mech. Model fit", zorder=1000) ax1.legend(loc=2, frameon=False) fig.suptitle(fitfile[:-4] + " - " + p['NRL_str']) if p['save'] == True: new_path = fitfile_path + "\\Fitfile figures\\" if not os.path.exists(new_path): os.makedirs(new_path) plt.savefig(new_path + fitfile[:-4]) # plt.show() plt.close() # assemble parameters into histogram if p['save'] == True: ba.plot_hist(ass_fit_pars, ass_fit_errors, new_path, p, show_plot=False) return
plt.close("all") data_path = "C:\\Users\\tbrouwer\\Desktop\\Data\\181115\\" datx_file = "181115_197_WT" measurements = ba.build_measurements(data_path, datx_file + ".datx", p) drift_arr = [] z_offset_arr = [] print("n = " + str(len(measurements))) for measurement in measurements: print("Processing measurement... " + str(measurement)) f_pull, z_pull, f_release, z_release, title, drift, z_offset = ba.read_analyze( measurement, p, data_path) f_wlc = np.logspace(np.log10(0.15), np.log10(int(np.max(f_pull))), 1000) wlc, _ = func.WLC(f_wlc, L_bp=p['L_bp'], P_nm=p['P_nm'], S_pN=p['S_pN']) drift_FE = p['drift'] drift_arr.append(drift) z_offset_arr.append(z_offset) twist_pos, twist_neg, z_pos, z_neg, lnd_pos, lnd_neg = ba.read_analyze_rot( measurement, p, data_path) drift_rot = p['drift'] fig = plt.figure(figsize=(15, 5)) # rotation ax1 = fig.add_subplot(1, 2, 1) ax1.set_ylabel('z (nm)') ax1.set_xlabel('$\sigma$')