Ejemplo n.º 1
0
def preliminaryAnalysis(obj, top):
    aligner1 = align.AlignTraj(obj, obj, verbose=True, in_memory=True)
    cAlpha = obj.select_atoms("name CA")

    #computing Radius of gyration
    print("Computing Radius of gyration along the trajectory:")
    gyro_list = list()
    for ts in obj.trajectory:
        gyro_list.append(obj.atoms.radius_of_gyration())
    s_gyro = pd.Series(gyro_list)

    #computing rmsd with first frame as reference
    print("Computing c-alphas RMSD with the first frame as reference:")
    rmsd1 = RMSD(cAlpha, verbose=True).run()
    rmsd_df = pd.DataFrame(rmsd1.rmsd)

    #computind rmsf
    print("Computing c-alphas RMSF:")
    ref_coordinates = obj.trajectory.timeseries(asel=cAlpha).mean(axis=1)
    ref = Merge(cAlpha).load_new(ref_coordinates[:, None, :], order="afc")
    re_align = align.AlignTraj(obj, ref, select="name CA").run()
    # need to write the trj to disk (issue 15)?
    with Writer("rmsfit.xtc", n_atoms=obj.atoms.n_atoms) as w:
        for ts in obj.trajectory:
            w.write(obj.atoms)
    #creating the fitted trj
    rmsfObj = Universe("rmsfit.xtc", top)
    #backboneRef = rmsfObj.select_atoms("backbone")
    rmsf = RMSF(cAlpha, verbose=True).run()
    rmsf_df = pd.DataFrame(rmsf.rmsf, index=cAlpha.resnums)

    return s_gyro, rmsd_df, rmsf_df
def RMSFcalculation(topology, trajectory, threshold):
    u = mda.Universe("%s" % trajectory,
                     in_memory=True)  # Initialization in MDAnalysis
    ref = mda.Universe("%s" %
                       topology)  # The backbone file has 3 atoms per residue
    prealigner = align.AlignTraj(
        u, ref, select="protein and name CA", in_memory=True).run(
        )  # Fit to the best frame to get a better average structure
    protein = u.select_atoms("protein")
    reference_coordinates = u.trajectory.timeseries(asel=protein).mean(axis=1)
    #print ("The atomic coordinates are\n%s" % reference_coordinates)
    reference = mda.Merge(protein).load_new(reference_coordinates[:, None, :],
                                            order="afc")
    aligner = align.AlignTraj(u,
                              reference,
                              select="protein and name CA",
                              in_memory=True).run()
    calphas = protein.select_atoms("name CA")
    rmsfer = RMSF(calphas, verbose=True).run()
    # rmsf_data = numpy.zeros(rmsfer.rmsf.size)
    resnums = calphas.resnums
    rmsf_data = rmsfer.rmsf
    plot(resnums, rmsf_data, threshold)
    selection_residue = []
    index = 1
    for rmsf_pointer in rmsf_data:
        if rmsf_pointer >= threshold:
            selection_residue.append(index)
        index += 1

    return selection_residue, rmsf_data
Ejemplo n.º 3
0
    def test_AlignTraj_custom_weights(self, universe, reference, tmpdir):
        weights = np.zeros(universe.atoms.n_atoms)
        ca = universe.select_atoms('name CA')
        weights[ca.indices] = 1

        outfile = str(tmpdir.join('align_test.dcd'))

        x = align.AlignTraj(universe, reference,
                            filename=outfile, select='name CA').run()
        x_weights = align.AlignTraj(universe, reference,
                                    filename=outfile, weights=weights).run()

        assert_array_almost_equal(x.rmsd, x_weights.rmsd)
Ejemplo n.º 4
0
    def test_AlignTraj_outfile_default_exists(self, universe, reference, tmpdir):
        reference.trajectory[-1]
        outfile = str(tmpdir.join('align_test.dcd'))
        align.AlignTraj(universe, reference, filename=outfile).run()
        fitted = mda.Universe(PSF, outfile)

        # ensure default file exists
        with mda.Writer(str(tmpdir.join('rmsfit_align_test.dcd')),
                        n_atoms=fitted.atoms.n_atoms) as w:
            w.write(fitted.atoms)

        align.AlignTraj(fitted, reference)
        # we are careful now. The default does nothing
        with pytest.raises(IOError):
            align.AlignTraj(fitted, reference, force=False)
Ejemplo n.º 5
0
    def test_AlignTraj_custom_weights(self):
        weights = np.zeros(self.universe.atoms.n_atoms)
        ca = self.universe.atoms.CA
        weights[ca.indices] = 1

        x = align.AlignTraj(self.universe,
                            self.reference,
                            filename=self.outfile,
                            select='name CA').run()
        x_weights = align.AlignTraj(self.universe,
                                    self.reference,
                                    filename=self.outfile,
                                    weights=weights).run()

        assert_array_almost_equal(x.rmsd, x_weights.rmsd)
Ejemplo n.º 6
0
def align_universe(u: mda.Universe,
                   align_type: AlignType,
                   sel: str = STANDARD_SELECTION):

    align.AlignTraj(u, u, select=sel, in_memory=True).run()

    return u
Ejemplo n.º 7
0
def extract_aligned_coords(struc_a, xtc_a, struc_b, xtc_b):
    """
    Writes out combined atomgroup density for both input simulations.
    

    Parameters
    ----------
    struc_a : str
        File name for the reference file (PDB or GRO format).
    xtc_a : str
        File name for the trajectory (xtc format).
    struc_b : str
        File name for the reference file (PDB or GRO format).
    xtc_b : str
        File name for the trajectory (xtc format).

    """
    if not os.path.exists('dens/'):
        os.makedirs('dens/')

    # # Before we extract the water densities, we need to first align the trajectories
    # # so that we can featurize water sites in both ensembles using the same coordinates
    condition_a = mda.Universe(struc_a, xtc_a)
    condition_b = mda.Universe(struc_b, xtc_b)

    align_xtc_name = 'dens/' + struc_a.split('/')[-1][:-4] + 'aligned.xtc'

    #align condition a to condition b
    align.AlignTraj(
        condition_a,  # trajectory to align
        condition_b,  # reference
        select='name CA',  # selection of atoms to align
        filename=align_xtc_name,  # file to write the trajectory to
        match_atoms=True,  # whether to match atoms based on mass
    ).run()
Ejemplo n.º 8
0
def align_with_mdanalysis(X_FT, smpl):

    import MDAnalysis as mda
    from MDAnalysis.analysis import align
    from MDAnalysis.analysis.rms import rmsd

    trj = mda.Universe(smpl.model.modelName+'.pdb', X_FT)
    ref = mda.Universe(smpl.model.modelName+'.pdb')
    # trj = mda.Universe('/Users/zofia/github/DFM/alanine.xyz', X_FT)
    # print(trj.trajectory)
    # ref = mda.Universe('/Users/zofia/github/DFM/alanine.xyz')#, X_FT[0,:,:])


    alignment = align.AlignTraj(trj, ref)#, filename='rmsfit.dcd')
    alignment.run()
    X_aligned = np.zeros(X_FT.shape)
    ci=0
    for ts in trj.trajectory:
        X_aligned[ci] = trj.trajectory.ts.positions
        ci=ci+1

    #X_aligned = (trj.trajectory.positions)
    #print(X_aligned.shape)
    #print(alignment)
    return X_aligned
Ejemplo n.º 9
0
def test_mean(pca_aligned, u):
    v = mda.Universe(PSF, DCD, in_memory=True)
    a = align.AlignTraj(v, v, select=SELECTION).run()
    coords = v.trajectory.timeseries(v.select_atoms(SELECTION), order='fac')
    assert_almost_equal(pca_aligned.mean,
                        coords.mean(axis=0).ravel(),
                        decimal=5)
Ejemplo n.º 10
0
def calcDensity(cutoff, **kwargs):
    """Retrive the density in Molar of density_sel"""

    for k, v in kwargs.items():
        exec(k + '=v')

    for co in cutoff:
        filenames = str(
            glob.glob(results_dir +
                      'structures/NAC_frames-{}-{}-{}-{}-*.gro'.format(
                          results_dir, prot, mol, c, co)))
        for f in filenames:
            xtc = f[:-4] + '.xtc'
            fit = f + '-fit.xtc'

            ref = mda.Universe(f)
            mobile = mda.Universe(f, xtc)
            alignment = align.AlignTraj(mobile, ref, filename=fit)
            alignment.run()

            fit_u = mda.Universe(filename + '.gro', fit)
            density = density_from_Universe(
                fit_u, delta=0.5
            )  #, atomselection=density_sel, update_selection=True)
            density.convert_density('Molar')
            density.export(filename + '-density_Molar.dx', type=double)
Ejemplo n.º 11
0
def align_coordinates(ref,
                      pdb,
                      trj_list,
                      out_name,
                      sel_string='all',
                      start_frame=0):
    """
    Aligns selected coordinates from a trajectory file.

    Parameters
    ----------
	ref : str
	    File name for reference topology.
	    Can read all MDAnalysis-compatible topology formats.
	pdb : str
	    File name for reference PDB file.
	trj_list : list of str
	    File names for the input trajectory.
	    Can read all MDAnalysis-compatible trajectory formats.
	out_name : str
	    Core of the file names for the output files
	start_frame : int, optional
	    First frame to read from the trajectory. 
    """
    # Read the reference+PDB files and align selected parts.
    u = mda.Universe(ref, pdb)
    for trj in trj_list:
        mobile = mda.Universe(ref, trj)
        #mobile.trajectory = mobile.trajectory[start_frame:]
        alignment = align.AlignTraj(mobile,
                                    u,
                                    select=sel_string,
                                    filename=f'{out_name}.xtc')
        alignment.run()
Ejemplo n.º 12
0
def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs):
    u = mda.Universe(*ref_files, in_memory=True)
    prealigner = align.AlignTraj(u, ref, select=select, **kwargs).run()
    ag = u.select_atoms(select)
    reference_coordinates = u.trajectory.timeseries(asel=ag).mean(axis=1)
    rmsd = sum(prealigner.rmsd / len(u.trajectory))
    return reference_coordinates, rmsd
Ejemplo n.º 13
0
 def test_AlignTraj_partial_fit(self):
     # fitting on a partial selection should still write the whole topology
     align.AlignTraj(self.universe,
                     self.reference,
                     select='resid 1-20',
                     filename=self.outfile,
                     weights='mass').run()
     mda.Universe(PSF, self.outfile)
Ejemplo n.º 14
0
 def test_AlignTraj_outfile_default(self, universe, reference, tmpdir):
     with tmpdir.as_cwd():
         reference.trajectory[-1]
         x = align.AlignTraj(universe, reference)
         try:
             assert os.path.basename(x.filename) == 'rmsfit_adk_dims.dcd'
         finally:
             x._writer.close()
Ejemplo n.º 15
0
 def test_AlignTraj_partial_fit(self, universe, reference, tmpdir):
     outfile = str(tmpdir.join('align_test.dcd'))
     # fitting on a partial selection should still write the whole topology
     align.AlignTraj(universe,
                     reference,
                     select='resid 1-20',
                     filename=outfile,
                     weights='mass').run()
     mda.Universe(PSF, outfile)
Ejemplo n.º 16
0
    def affinity_propagation(self, preference=-6.0):
        '''Performing Affinity Propagation clustering of AMOEBA-run Trp-Cage folding trajectory:-

           Default parameter values from MDAnalysis - damping=0.9, max_iter=500, convergence_iter=50
           Preference reduced to -10 from -1 to reflect local homogenity within the trajectory'''

        print("Performing Affinity Propagation with input preference = %f" %
              preference)
        clust = encore.cluster(self.univ_cut,
                               method=encore.AffinityPropagation(
                                   preference=preference, verbose=True))
        centroids = [cluster.centroid * self.ncut for cluster in clust]
        ids = [cluster.id for cluster in clust]

        print(
            "Clustering complete! - %d clusters formed with average size = %d frames"
            % (len(ids), np.average([cluster.size for cluster in clust])))

        coords_centroids = np.zeros((len(centroids), 304, 3), dtype=np.float64)
        coords_centroids[:, :, :] = [
            self.coords_protein[centroids[i], :, :]
            for i in np.arange(0, len(centroids))
        ]

        protein = self.univ2.select_atoms("protein")

        univ_centroids = mda.Merge(protein)
        univ_centroids.load_new(coords_centroids, format=MemoryReader)

        ref = mda.Universe("folded.pdb")

        nframes = len(univ_centroids.trajectory)

        alignment = align.AlignTraj(univ_centroids,
                                    ref,
                                    select='protein and name CA',
                                    in_memory=True,
                                    verbose=True)
        alignment.run()

        idvscenter = {
            'Cluster ID': [ids[i] for i in range(0, len(ids))],
            'Centroid Time (ps)':
            [centroids[i] * 10 for i in range(0, len(centroids))],
            'Cluster Size': [cluster.size for cluster in clust]
        }

        idtable = pd.DataFrame(data=idvscenter)

        #Visualisation representation set for Trp-Cage - will expand to general proteins later
        view = nglview.show_mdanalysis(univ_centroids)
        view.add_cartoon(selection="protein")
        view.add_licorice('TRP')
        view.add_licorice('PRO')
        view.center(selection='protein', duration=nframes)
        view.player.parameters = dict(frame=True)
        return view, idtable, univ_centroids
Ejemplo n.º 17
0
def f(u, selection, align_with='protein'):
    align.AlignTraj(u, u, select=align_with, in_memory=True).run()

    sele = u.select_atoms(selection)
    rmsfer = RMSF(sele, verbose=True).run()

    x = [(a.name, a.id) for a in rmsfer.atomgroup.atoms]
    df = pd.DataFrame(x, columns=('Name', 'ID'))
    df['RMSF'] = rmsfer.rmsf
    return df.set_index(['Name', 'ID'])
Ejemplo n.º 18
0
 def test_AlignTraj_outfile_default(self, universe, reference):
     # NOTE: Remove the line os.remove() with release 1.0,
     #       when the default behavior of AlignTraj changes.
     with tempdir.in_tempdir():
         reference.trajectory[-1]
         x = align.AlignTraj(universe, reference)
         try:
             assert os.path.basename(x.filename) == 'rmsfit_adk_dims.dcd'
         finally:
             x._writer.close()
             os.remove(x.filename)
Ejemplo n.º 19
0
    def test_ensemble_superimposition_to_reference_non_weighted(self):
        aligned_ensemble1 = mda.Universe(PSF, DCD)
        align.AlignTraj(aligned_ensemble1,
                        aligned_ensemble1,
                        select="name CA",
                        in_memory=True).run()
        aligned_ensemble2 = mda.Universe(PSF, DCD)
        align.AlignTraj(aligned_ensemble2,
                        aligned_ensemble2,
                        select="name *",
                        in_memory=True).run()

        rmsfs1 = rms.RMSF(aligned_ensemble1.select_atoms('name *'))
        rmsfs1.run()

        rmsfs2 = rms.RMSF(aligned_ensemble2.select_atoms('name *'))
        rmsfs2.run()

        assert sum(rmsfs1.rmsf) > sum(rmsfs2.rmsf), "Ensemble aligned on all " \
                                                    "atoms should have lower full-atom RMSF than ensemble aligned on only CAs."
Ejemplo n.º 20
0
    def test_AlignTraj_in_memory(self):
        self.reference.trajectory[-1]
        x = align.AlignTraj(self.universe,
                            self.reference,
                            filename=self.outfile,
                            in_memory=True).run()
        assert_almost_equal(x.rmsd[0], 6.9290, decimal=3)
        assert_almost_equal(x.rmsd[-1], 5.2797e-07, decimal=3)

        # check in memory trajectory
        self._assert_rmsd(self.universe, 0, 6.929083044751061)
        self._assert_rmsd(self.universe, -1, 0.0)
Ejemplo n.º 21
0
    def test_AlignTraj_in_memory(self, universe, reference, tmpdir):
        outfile = str(tmpdir.join('align_test.dcd'))
        reference.trajectory[-1]
        x = align.AlignTraj(universe, reference, filename=outfile,
                            in_memory=True).run()
        assert x.filename is None
        assert_almost_equal(x.rmsd[0], 6.9290, decimal=3)
        assert_almost_equal(x.rmsd[-1], 5.2797e-07, decimal=3)

        # check in memory trajectory
        self._assert_rmsd(reference, universe, 0, 6.929083044751061)
        self._assert_rmsd(reference, universe, -1, 0.0)
Ejemplo n.º 22
0
    def test_AlignTraj_weighted(self, universe, reference, tmpdir):
        outfile = str(tmpdir.join('align_test.dcd'))
        x = align.AlignTraj(universe, reference,
                            filename=outfile, weights='mass').run()
        fitted = mda.Universe(PSF, outfile)
        assert_almost_equal(x.rmsd[0], 0, decimal=3)
        assert_almost_equal(x.rmsd[-1], 6.9033, decimal=3)

        self._assert_rmsd(reference, fitted, 0, 0.0,
                          weights=universe.atoms.masses)
        self._assert_rmsd(reference, fitted, -1, 6.929083032629219,
                          weights=universe.atoms.masses)
Ejemplo n.º 23
0
    def test_AlignTraj(self, universe, reference, tmpdir):
        reference.trajectory[-1]
        outfile = str(tmpdir.join('align_test.dcd'))
        x = align.AlignTraj(universe, reference, filename=outfile).run()
        fitted = mda.Universe(PSF, outfile)

        assert_almost_equal(x.rmsd[0], 6.9290, decimal=3)
        assert_almost_equal(x.rmsd[-1], 5.2797e-07, decimal=3)

        # RMSD against the reference frame
        # calculated on Mac OS X x86 with MDA 0.7.2 r689
        # VMD: 6.9378711
        self._assert_rmsd(reference, fitted, 0, 6.929083044751061)
        self._assert_rmsd(reference, fitted, -1, 0.0)
Ejemplo n.º 24
0
    def test_AlignTraj_custom_mass_weights(self):
        x = align.AlignTraj(self.universe,
                            self.reference,
                            filename=self.outfile,
                            weights=self.reference.atoms.masses).run()
        fitted = mda.Universe(PSF, self.outfile)
        assert_almost_equal(x.rmsd[0], 0, decimal=3)
        assert_almost_equal(x.rmsd[-1], 6.9033, decimal=3)

        self._assert_rmsd(fitted, 0, 0.0, weights=self.universe.atoms.masses)
        self._assert_rmsd(fitted,
                          -1,
                          6.929083032629219,
                          weights=self.universe.atoms.masses)
Ejemplo n.º 25
0
def pairwise_rmsds(universes: List[mda.Universe]):
    """Compute pairwise RMSDs for position snapshots."""
    size = len(universes)
    rmsd_matrix = np.zeros(shape=(size, size))
    for i, universe_i in enumerate(universes):
        for j, universe_j in enumerate(universes):
            if i == j:
                rmsd = 0
            else:
                align.AlignTraj(universe_i,
                                universe_i,
                                select=STANDARD_SELECTION,
                                in_memory=True)

                align.AlignTraj(universe_i,
                                universe_j,
                                select=STANDARD_SELECTION,
                                in_memory=True)

                pos_i = extract_positions(universe_i)
                pos_j = extract_positions(universe_j)

                rmsd = rms.rmsd(
                    pos_i.mean(axis=1)[:, None, :],
                    pos_j.mean(axis=1)[:, None, :],
                )

                # rmsd = rms.rmsd(
                #     pos_i,
                #     pos_j
                # )

            rmsd_matrix[i, j] = rmsd
            rmsd_matrix[j, i] = rmsd

    return rmsd_matrix
Ejemplo n.º 26
0
    def test_ensemble_superimposition():
        aligned_ensemble1 = mda.Universe(PSF, DCD)
        align.AlignTraj(aligned_ensemble1,
                        aligned_ensemble1,
                        select="name CA",
                        in_memory=True).run()
        aligned_ensemble2 = mda.Universe(PSF, DCD)
        align.AlignTraj(aligned_ensemble2,
                        aligned_ensemble2,
                        select="name *",
                        in_memory=True).run()

        rmsfs1 = rms.RMSF(aligned_ensemble1.select_atoms('name *'))
        rmsfs1.run()

        rmsfs2 = rms.RMSF(aligned_ensemble2.select_atoms('name *'))
        rmsfs2.run()

        assert_equal(
            sum(rmsfs1.rmsf) > sum(rmsfs2.rmsf),
            True,
            err_msg=
            "Ensemble aligned on all atoms should have lower full-atom RMSF "
            "than ensemble aligned on only CAs.")
Ejemplo n.º 27
0
def get_rmsf(t, label=None):
    sel = 'protein and not name H*'
    prot = t.select_atoms(sel)
    average_coordinates = t.trajectory.timeseries(asel=prot).mean(axis=1)
    # make a reference structure (need to reshape into a 1-frame "trajectory")
    reference = MDAnalysis.Merge(prot).load_new(average_coordinates[:,
                                                                    None, :],
                                                order="afc")
    aligner = align.AlignTraj(t, reference, select=sel, in_memory=True).run()
    print('\tDone Aligning... calculating RMSF')
    rmsfer = RMSF(prot).run()
    ret = pd.DataFrame(zip(prot.resids, prot.resnames, rmsfer.rmsf),
                       columns=('resid', 'resname', 'rmsf'))
    if label != None:
        ret['label'] = label
    return ret
Ejemplo n.º 28
0
    def test_AlignTraj_weights_deprecated(self):
        with warnings.catch_warnings(record=True) as warn:
            warnings.simplefilter('always')
            x = align.AlignTraj(self.universe,
                                self.reference,
                                filename=self.outfile,
                                mass_weighted=True).run()
        assert_equal(len(warn), 1)
        fitted = mda.Universe(PSF, self.outfile)
        assert_almost_equal(x.rmsd[0], 0, decimal=3)
        assert_almost_equal(x.rmsd[-1], 6.9033, decimal=3)

        self._assert_rmsd(fitted, 0, 0.0, weights=self.universe.atoms.masses)
        self._assert_rmsd(fitted,
                          -1,
                          6.929083032629219,
                          weights=self.universe.atoms.masses)
Ejemplo n.º 29
0
def to_mda_traj(pos_traj,
                frame_traj,
                all_steps,
                ncp_bp_frames,
                bp_num_map,
                mute=True,
                alignTrj=False):
    #gen topology in pdb
    pdb_string = ''
    bp_frames, octamers = gen_fiber(pos_traj[0], frame_traj[0], all_steps,
                                    ncp_bp_frames, bp_num_map)
    num = 1
    pdb_string = pdb_string + 'MODEL     1\n'
    for row in bp_frames[:, 3, :3] / 10:
        pdb_string = pdb_string + "ATOM  %5d  N   DNA A   1    %8.3f%8.3f%8.3f\n" % (
            num, row[0], row[1], row[2])
        num += 1
    for row in octamers / 10:
        pdb_string = pdb_string + "ATOM  %5d  O   NUC B   1    %8.3f%8.3f%8.3f\n" % (
            num, row[0], row[1], row[2])
        num += 1
    pdb_string = pdb_string + "ENDMDL\n"

    #gen trajectory np array
    coords = np.zeros((len(pos_traj), bp_frames.shape[0] + len(octamers), 3))
    for frame, (positions, frames) in tqdm(enumerate(zip(pos_traj,
                                                         frame_traj)),
                                           total=len(pos_traj),
                                           disable=mute):
        bp_frames, octamers = gen_fiber(positions, frames, all_steps,
                                        ncp_bp_frames, bp_num_map)
        coords[frame] = np.vstack([bp_frames[:, 3, :3], octamers])

    u = mda.Universe(StringIO(pdb_string),
                     coords / 10.0,
                     topology_format='pdb',
                     format=MemoryReader,
                     order='fac')

    if alignTrj:
        alignment = align.AlignTraj(u, u, in_memory=True)
        alignment.run()
    return (u)
Ejemplo n.º 30
0
def test_rmsf_xtc(run):
    """Align multiple times + RMSF"""

    u = mda.Universe(TPR, XTC)

    u2 = u.copy()

    average = align.AverageStructure(u2, u2, select='protein and name CA',
                                     ref_frame=0).run()

    ref = average.universe

    aligner = align.AlignTraj(u2, ref,
                              select='protein and name CA',
                              in_memory=True).run()

    c_alphas = u2.select_atoms('protein and name CA')
    R = rms.RMSF(c_alphas).run()

    assert 1 == 1