def create_mp4_pov(directory, origin_frame=0, fps=5, reverse=True): filenames = sorted(glob.glob(directory + '\\*.npz')) r = np.append([10], (np.ones(8) * 4)) r = np.append(r, 3) r = np.append(r, 2) c = 'kbbggryrycy' image_files = [] j = 0 report_progress(len(filenames), title='create_pov_mp4', init=True) for file in filenames: j += 1 report_progress(j, title='create_pov_mp4') dna = HelixPose.from_file(file) origin_of = nMC.get_of(dna, origin_frame) tf = nMC.get_transformation(origin_of) coords = nMC.apply_transformation(dna.coords, tf) image_files.append( pov_dna(file, [coords], range_A=[1000, 1500], offset_A=[0, 0, 150], show=False)) create_mp4_images(image_files, fps=fps, delete=True, filename=directory + '.mp4', reverse=reverse) return
def get_stack_pars(dna, dyads): ofs = [] for dyad in dyads: ofs.append(nMC.get_of(dna, dyad)) params = [] for of1, of2 in zip(ofs, ofs[1:]): params.append(nMC.ofs2params(of1, of2)) return np.asarray(params)
def create_unfolded_fiber(pars, ref_of): dna, dyads, nucl = create_nuc_array(p=pars) linker_params = create_curved_linker(p=pars) dna = replace_linker(dna, dyads, linker_params) if ref_of is None: ref_of = nMC.get_of(dna, 0) i = pars['ref_frame'].value tf = nMC.get_transformation(nMC.get_of(dna, i), target=ref_of) coords = nMC.apply_transformation(dna.coords, tf) return coords, dna, tf
def insert_nucs(dna, dyads, pdb='1KX5'): ''' Fix basepair parameters for nucleosomes in DNA at dyad positions Parameters ---------- dna: HelixMC pose dyads : dyad positions in DNA (bp) Returns ------- dna : HelixMC pose nucl : NucleosomeMC pose ''' # # Get nucleosome nucl = nMC.NucPose() nucl.from_file(fileio.change_extension(pdb, '3DNA')) params = dna.params length = len(nucl.dna.params) new_dyads = [] for dyad in dyads: start = dyad - nucl.dyad end = start + length if (start >= 0 and (end < len(params))): params[start:end] = nucl.dna.params new_dyads.append(dyad) dna = HelixPose(params) return dna, nucl, np.asarray(new_dyads)
def get_fiber_frames(par): ''' Get the frame coordinates that define a stacked, folded fiber Returns ------- n_ofs : ndarray of (N, 4, 3) The coordinates of the nucleosome frames d_ofs : ndarray of (N, 4, 3) The coordinates of the dyad frames ''' r = par['diameter_A'].value / 2.0 - 65.0 nld = par['nld_A'].value rise = par['rise_A'].value coords = [] n_ofs = [] phi = np.sqrt(rise**2 - nld**2) / r if par['chirality'].value < 0: phi *= -1 for i in np.arange(par['n_nuc'].value + 1): coords.append( np.asarray([r * np.cos(i * phi), r * np.sin(i * phi), i * nld])) for o1, o2 in zip(coords, coords[1:]): f0 = par['face'].value * o1 * [-1, -1, 0] f0 /= np.linalg.norm(f0) f1 = o2 - o1 f1 /= np.linalg.norm(f1) f1 = np.cross(f0, f1) f2 = np.cross(f0, f1) n_ofs.append(nMC.join_o_f(o1, np.transpose([f0, f1, f2]))) return n_ofs
def plot_dna(pose, tf=None, color='blue', update=False, title='', range_nm=100, save=False, wait=0): global ax, fig, scale if tf is None: coords = pose.coords / 10.0 else: coords = nMC.apply_transformation(pose.coords, tf) / 10.0 if update: ax.clear() plt.title(title, loc='left') else: plt.close() fig = plt.figure(figsize=(5, 5)) ax = fig.add_subplot(111, projection='3d') scale = range_nm / 2 plt.title(title, loc='left') plt.tight_layout(pad=0.3, w_pad=0.3, h_pad=0.3) pointsize = 0.1 * np.sqrt(scale) ax.set_xlabel('x (nm)') ax.set_ylabel('y (nm)') ax.set_zlabel('z (nm)') ax.scatter(coords[:, 0], coords[:, 1], coords[:, 2], s=pointsize, c=color, alpha=0.25) ax.scatter(coords[0, 0], coords[0, 1], coords[0, 2], s=pointsize, c='red', alpha=0.55) ax.scatter([0], [0], [0], s=pointsize, c='k', alpha=0.55) plt.xlim((-scale, scale)) plt.ylim((-scale, scale)) ax.set_zlim(0, 2 * scale) plt.draw() plt.pause(0.05) if save: filename = get_filename(incr=True, sub=True, ext='jpg') plt.savefig(filename, dpi=600, format='jpg') time.sleep(wait) return
def score_wrapping(moving_bp, dna, dyads, nucl, fixed_wrap_params, g_wrap_kT): closest_dyad = dyads[np.abs(dyads - moving_bp).argmin()] start_bp = closest_dyad - nucl.dyad end_bp = start_bp + len(nucl.dna.params) if start_bp <= moving_bp < end_bp: wrap_params = nMC.get_wrap_params(dna, closest_dyad, nucl.fixed) G, _ = get_unwrap_energy(wrap_params, fixed_wrap_params, g_wrap_kT) return G else: return 0
def scan_fiber_param(fiber_pars, iter_param, iter_vals): setnr = 0 image_files = [] report_file = fileio.get_filename(incr=True) fileio.report_progress(len(iter_vals), title='scan_fiber_param', init=True) for i in iter_vals: fileio.report_progress(setnr, title='scan_fiber_param\n') filename = fileio.get_filename(incr=True, sub=True) fiber_pars[iter_param].value = i dna, dyads, nucl = create_nuc_array(p=fiber_pars) fiber_dna, dyads, w = create_folded_fiber(fiber_pars, nucl) fiber_pars['ref_frame'].value = dyads[0] ref_of = nMC.get_of(fiber_dna, fiber_pars['ref_frame'].value) try: out = minimize(residual, fiber_pars, args=(ref_of, fiber_dna.coords, w), method='nelder') report_fit(out) fiber_pars = out.params coords, dna, tf = create_unfolded_fiber(fiber_pars, ref_of) image_files.append( fileio.pov_dna(filename, [dna.coords], range_A=[400, 400], offset_A=[0, 0, 150], show=True)) fileio.write_xlsx(filename, str(setnr), fiber_pars, report_file=report_file) dna.write2disk(fileio.get_filename(sub=True, ext='npz')) except: print('Fit did not converge') setnr += 1 fileio.create_mp4_images(image_files, filename=report_file) return
def create_folded_fiber(par, nucl): dna, dyads, _ = create_nuc_array(p=par) params = np.zeros_like(dna.params) w = np.zeros(len(dna.coords)) n_ofs = get_fiber_frames(par) origin_of = np.concatenate(([np.zeros(3)], np.eye(3))) unwrapped = int(par['Unwrapped_bp']) length = len(nucl.dna.params) - 2 * unwrapped for n_of, dyad in zip(n_ofs, dyads): tf = nMC.get_transformation(nucl.of, target=n_of) start_bp = dyad - nucl.dyad + unwrapped w[start_bp:start_bp + length] = np.ones(length) params[start_bp:start_bp + length] = nucl.dna.params[unwrapped:unwrapped + length] params[start_bp - 1] = \ nMC.ofs2params(origin_of, nMC.apply_transformation(nMC.get_of(nucl.dna, unwrapped), tf)) params[start_bp + length] = \ nMC.ofs2params(nMC.apply_transformation(nMC.get_of(nucl.dna, len(nucl.dna.params) - unwrapped), tf), origin_of) fiber_dna = HelixPose(params) w = np.transpose(np.asarray([w, w, w])) return fiber_dna, dyads, w
def main(): # Define fiber params pars = Parameters() pars.add('F_pN', value=0) pars.add('z_nm', value=0) pars.add('L_bp', value=800) pars.add('n_nuc', value=4) pars.add('NRL', value=167) pars.add('P_nm', value=50) pars.add('dyad0_bp', value=0) pars.add('diameter_A', value=330) pars.add('rise_A', value=100) pars.add('nld_A', value=25) pars.add('chirality', value=-1) pars.add('Unwrapped_bp', value=30) pars.add('face', value=1) pars.add('g_wrap_kT', value=3) pars.add('g_stack_kT', value=25) pars.add('fiber_start', value=2) g_wrap_kT = pars['g_wrap_kT'].value g_stack_kT = pars['g_stack_kT'].value # Setup files and forces filename = fileio.get_filename(incr=True, root='3nucs') print(filename) n_step = 250 n_substeps = 10 fmax_pN = 10 fmin_pN = 0.1 # forces = np.linspace(fmin_pN, fmax_pN, n_step / 2) forces = np.logspace(np.log10(fmin_pN), np.log10(fmax_pN), n_step / 2) forces = np.append(forces, forces[::-1]) get_from_file = False if get_from_file: # Get from file file_in = 'E:\\Users\\noort\\data\\20180321\\data_003.xlsx' dataset = 0 pars, datafile = fileio.read_xlsx(file_in, dataset, pars=pars) dna, dyads, nucl = fMC.create_nuc_array(p=pars) dna = HelixPose.from_file(fileio.change_extension(datafile, 'npz')) else: # Initialize fiber pose dna, dyads, nucl = fMC.create_nuc_array(p=pars) pars['dyad0_bp'].value = dyads[0] fileio.plot_dna(dna, title='Initial conformation\n', range_nm=100, save=True) pars.pretty_print(columns=['value']) # Get stack and wrap parameters fixed_wrap_params = nMC.get_wrap_params(nucl.dna, nucl.dyad, nucl.fixed) fiber_dna, dyads, w = fMC.create_folded_fiber(pars, nucl) fixed_stack_params = fMC.get_stack_pars(fiber_dna, dyads)[0] fiber_start = pars['fiber_start'].value # Initialize random steps random_step = RandomStepSimple.load_gaussian_params('DNA_gau.npy') basepairs = np.asarray(xrange(pars['L_bp'] - 1)) accept = 0 all_coord = np.empty((n_step, 3)) current_step = 0 fileio.report_progress(n_step, title='RunMC3', init=True) for force in forces: fileio.report_progress(current_step + 1, title='RunMC3') scorefxn = ScoreTweezers(force) previous_bp = 0 for sub_step in xrange(n_substeps): for bp in basepairs: accept += MC_move(dna, bp, previous_bp, scorefxn, fixed_wrap_params, fixed_stack_params, dyads, nucl, random_step, g_wrap_kT, g_stack_kT, fiber_start) previous_bp = bp basepairs = basepairs[::-1] fileio.plot_dna(dna, update=True, title='F = {:.1f} pN\n'.format(force), save=True) pars['F_pN'].value = force pars['z_nm'].value = dna.coord_terminal[2] fileio.write_xlsx(fileio.get_filename(sub=True), str(current_step), pars, report_file=filename) dna.write2disk(fileio.get_filename(sub=True, ext='npz')) all_coord[current_step] = dna.coord_terminal current_step += 1 z = all_coord[:, 2] / 10 wlc = 1 - 0.5 * np.sqrt(0.1 * kT / (forces * pars['P_nm'])) grid = [] for i in xrange(1, pars['n_nuc'] + 1): grid.append(wlc * (pars['L_bp'] - 80 * i) / 3) grid.append(wlc * (pars['L_bp'] - (pars['n_nuc'] * 80 + i * (147 - 80))) / 3) wlc *= pars['L_bp'] / 3 selected = np.diff(np.append([-1], forces)) > 0 fileio.save_plot((forces, z, wlc, selected), filename=filename, ax_labels=['z (nm)', 'F (pN)'], grid=grid, transpose=True, xrange=[0, 1.1 * pars['L_bp'] / 3]) if n_step > 0: print('Accept rate = %.1f %%' % (100 * float(accept) / (n_step * n_substeps * (pars['L_bp'] - 1)))) try: fileio.create_mp4_pov(fileio.get_filename(sub=True, folder=True), origin_frame=0, reverse=False) except Exception, e: print(Exception, e)