def __init__(self, universe, selectionstring, cutoff=15.0, pbc=False, sparse=None): """Initialize from a *universe* or pdb file. :Arguments: *universe* :class:`MDAnalysis.Universe` or a PDB file name. *selection* :class:`MDAnalysis.core.AtomGroup.AtomGroup` or a :meth:`MDAnalysis.Universe.select_atoms` selection string for atoms that define the lipid head groups, e.g. universe.atoms.PO4 or "name PO4" or "name P*" :Keywords: *cutoff* head group-defining atoms within a distance of *cutoff* Angstroms are deemed to be in the same leaflet [15.0] *pbc* take periodic boundary conditions into account (only works for orthorhombic boxes) [``False``] *sparse* ``None``: use fastest possible routine; ``True``: use slow sparse matrix implementation (for large systems); ``False``: use fast :func:`~MDAnalysis.analysis.distances.distance_array` implementation [``None``]. """ universe = MDAnalysis.as_Universe(universe) self.universe = universe self.selectionstring = selectionstring if type(self.selectionstring) == MDAnalysis.core.AtomGroup.AtomGroup: self.selection = self.selectionstring else: self.selection = universe.select_atoms(self.selectionstring) self.pbc = pbc self.sparse = sparse self._init_graph(cutoff)
def __init__(self, *args, **kwargs): """Calculate native contacts within a group or between two groups. :Arguments: *topology* psf or pdb file *trajectory* dcd or xtc/trr file *universe* instead of a topology/trajectory combination, one can also supply a :class:`MDAnalysis.Universe` :Keywords: *selection* selection string that determines which distances are calculated; if this is a tuple or list with two entries then distances are calculated between these two different groups ["name CA or name B*"] *refgroup* reference group, either a single :class:`~MDAnalysis.core.AtomGroup.AtomGroup` (if there is only a single *selection*) or a list of two such groups. The reference contacts are directly computed from *refgroup* and hence the atoms in the reference group(s) must be equivalent to the ones produced by the *selection* on the input trajectory. *radius* contacts are deemed any atoms within radius [8.0 A] *outfile* name of the output file; with the gz or bz2 suffix, a compressed file is written. The average <q> is written to a second, gzipped file that has the same name with 'array' included. E.g. for the default name "q1.dat.gz" the <q> file will be "q1.array.gz". The format is the matrix in column-row format, i.e. selection 1 residues are the columns and selection 2 residues are rows. The file can be read with :func:`np.loadtxt`. ["q1.dat.gz"] The function calculates the percentage of native contacts q1 along a trajectory. "Contacts" are defined as the number of atoms within *radius* of a given other atom. *q1* is the fraction of contacts relative to the reference state 1 (typically the starting conformation of the trajectory). The timeseries is written to a file *outfile* and is also accessible as the attribute :attr:`ContactAnalysis1.timeseries`. """ # XX or should I use as input # sel = (group1, group2), ref = (refgroup1, refgroup2) # and get the universe from sel? # Currently it's a odd hybrid. # # Enhancements: # - select contact pairs to write out as a timecourse # - make this selection based on qavg from os.path import splitext self.selection_strings = self._return_tuple2(kwargs.pop("selection", "name CA or name B*"), "selection") self.references = self._return_tuple2(kwargs.pop("refgroup", None), "refgroup") self.radius = kwargs.pop("radius", 8.0) self.targetdir = kwargs.pop("targetdir", os.path.curdir) self.output = kwargs.pop("outfile", "q1.dat.gz") self.outarray = splitext(splitext(self.output)[0])[0] + ".array.gz" self.force = kwargs.pop("force", False) self.timeseries = None # final result self.filenames = args self.universe = MDAnalysis.as_Universe(*args, **kwargs) self.selections = [self.universe.select_atoms(s) for s in self.selection_strings] # sanity checkes for x in self.references: if x is None: raise ValueError("a reference AtomGroup must be supplied") for ref, sel, s in izip(self.references, self.selections, self.selection_strings): if ref.atoms.n_atoms != sel.atoms.n_atoms: raise ValueError( "selection=%r: Number of atoms differ between " "reference (%d) and trajectory (%d)" % (s, ref.atoms.n_atoms, sel.atoms.n_atoms) ) # compute reference contacts dref = MDAnalysis.lib.distances.distance_array( self.references[0].coordinates(), self.references[1].coordinates() ) self.qref = self.qarray(dref) self.nref = self.qref.sum() # setup arrays for the trajectory self.d = np.zeros_like(dref) self.q = self.qarray(self.d) self._qtmp = np.zeros_like(self.q) # pre-allocated array self.qavg = np.zeros(shape=self.q.shape, dtype=np.float64)
def reference(self): return mda.Universe(PSF, CRD)
def u(self): top = mmtf.parse(MMTF) with mock.patch('mmtf.fetch') as mock_fetch: mock_fetch.return_value = top return mda.fetch_mmtf('173D') # string is irrelevant
from __future__ import division, print_function import MDAnalysis import numpy as np import matplotlib as mpl from matplotlib import pyplot as plt from IPython import embed import cPickle as pickle univ = MDAnalysis.Universe('top.tpr', 'prot.gro') univ.add_TopologyAttr('tempfactor') #rg = univ.residues[1:-1] rg = univ.residues ag = rg.atoms charges = np.abs(rg.atoms.charges) width = 0.01 bb = np.arange(0,1+width,width) hist, bb = np.histogram(charges, bins=bb) plt.bar(bb[:-1], hist, width=width, align='edge') ymax = 116 xcoords = [0.15, 0.20, 0.25, 0.30, 0.35] for xc in xcoords: plt.axvline(x=xc, ymax=ymax, linestyle='--', color='k') plt.ylim(0,ymax) plt.show()
def universe(self): return mda.Universe(*self.ref_files)
def universe(): return mda.Universe(PSF, DCD)
def __init__(self, pdb, delta=1.0, atomselection='resname HOH and name O', metadata=None, padding=1.0, sigma=None): """Construct the density from psf and pdb and the atomselection. Parameters ---------- pdb : str PDB file or :class:`MDAnalysis.Universe`; atomselection : str selection string (MDAnalysis syntax) for the species to be analyzed delta : float bin size for the density grid in Angstroem (same in x,y,z) [1.0] metadata : dict dictionary of additional data to be saved with the object padding : float increase histogram dimensions by padding (on top of initial box size) sigma : float width (in Angstrom) of the gaussians that are used to build up the density; if ``None`` (the default) then uses B-factors from pdb Notes ----- For assigning X-ray waters to MD densities one might have to use a sigma of about 0.5 A to obtain a well-defined and resolved x-ray water density that can be easily matched to a broader density distribution. Examples -------- The following creates the density with the B-factors from the pdb file:: DC = BfactorDensityCreator(pdb, delta=1.0, atomselection="name HOH", padding=2, sigma=None) density = DC.Density() See Also -------- :func:`density_from_PDB` for a convenience function """ u = MDAnalysis.as_Universe(pdb) group = u.select_atoms(atomselection) coord = group.positions logger.info("Selected {0:d} atoms ({1!s}) out of {2:d} total.".format(coord.shape[0], atomselection, len(u.atoms))) smin = np.min(coord, axis=0) - padding smax = np.max(coord, axis=0) + padding BINS = fixedwidth_bins(delta, smin, smax) arange = list(zip(BINS['min'], BINS['max'])) bins = BINS['Nbins'] # get edges by doing a fake run grid, self.edges = np.histogramdd(np.zeros((1, 3)), bins=bins, range=arange, normed=False) self.delta = np.diag([(e[-1] - e[0]) / (len(e) - 1) for e in self.edges]) self.midpoints = [0.5 * (e[:-1] + e[1:]) for e in self.edges] self.origin = [m[0] for m in self.midpoints] n_frames = 1 if sigma is None: # histogram individually, and smear out at the same time # with the appropriate B-factor if np.any(group.bfactors == 0.0): wmsg = "Some B-factors are Zero (will be skipped)." logger.warning(wmsg) warnings.warn(wmsg, category=MissingDataWarning) rmsf = Bfactor2RMSF(group.bfactors) grid *= 0.0 # reset grid self.g = self._smear_rmsf(coord, grid, self.edges, rmsf) else: # histogram 'delta functions' grid, self.edges = np.histogramdd(coord, bins=bins, range=arange, normed=False) logger.info("Histogrammed {0:6d} atoms from pdb.".format(len(group.atoms))) # just a convolution of the density with a Gaussian self.g = self._smear_sigma(grid, sigma) try: metadata['pdb'] = pdb except TypeError: metadata = {'pdb': pdb} metadata['atomselection'] = atomselection metadata['n_frames'] = n_frames metadata['sigma'] = sigma self.metadata = metadata logger.info("Histogram completed (initial density in Angstrom**-3)")
def convert(self): w = mda.Writer(self.newname, self.frames.n_atoms) for ts in self.frames: w.write(ts) w.close()
import MDAnalysis as mda import matplotlib.pyplot as plt from MDAnalysis.analysis import align from MDAnalysis.analysis import rms import pandas as pd x = mda.Universe('AT3_3UIM_model1.B99990004_filt.pdb') a = mda.Universe('outII.pdb') align.AlignTraj(a, x, select='backbone or (resid 8 and name CB and resname ALA)', filename='aligned_AT3.II.pdb', match_atoms=True, dt=16).run() R = rms.RMSD(a, x, select='backbone or (resid 8 and name CB and resname ALA)') R.run() df = pd.DataFrame(R.rmsd, columns=[ 'Frames', 'Time (ns)', 'backbone or (resid 8 and name CB and resname ALA)' ]) df.to_csv('rmsd_AT3.II.csv', index=False) fig = df.plot(x='Frames', y=['backbone or (resid 8 and name CB and resname ALA)'], kind='hist', figsize=(20, 16), fontsize=60, color=['#781C6DFF'], edgecolor="white", legend=False) fig.set_xlabel(r'RMSD ($\AA$)') for item in ([fig.title, fig.xaxis.label, fig.yaxis.label] +
from MDAnalysis.analysis.align import rotation_matrix from MDAnalysis.analysis.psa import PSAnalysis import pandas as pd import scipy.cluster.hierarchy as hier from scipy.spatial.distance import directed_hausdorff import matplotlib.pyplot as plt from pylab import * from matplotlib import collections as mc import glob import os import shutil # Endpoint structures target = mda.Universe("paper_methods/top/init.psf", "paper_methods/EX_10/targ.crd") initial = mda.Universe("paper_methods/top/init.psf", "paper_methods/EX_10/init.crd") # RESIDs adjusted ****(confirm selection rigorously!)**** mhp1_bun_resids = "(resid 32:64 or resid 67:102 or resid 203:300)" # Reference structure candidate = 'IF_OCC_2jlo_avg_candidate.pdb' ref_IF_OCC_avg = mda.Universe(candidate) # Selections
import MDAnalysis import MDAnalysis.analysis.rms import matplotlib.pyplot as plt u = MDAnalysis.Universe('XDATCAR.pdb', permissive=True) ref = MDAnalysis.Universe('XDATCAR.pdb', permissive=True) # reference (with the default ref_frame=0) ref.trajectory[0] #use first frame as reference R = MDAnalysis.analysis.rms.RMSD(u, ref, select="all", # superimpose on whole backbone of all atoms # align based on all atoms groupselections=["type H","type O"], filename="rmsd_all.dat",center=True)#, # CORE timestep=0.0005 #0.5fs from fs to ps as Reader has no dt information, set to 1.0 ps R.run() rmsd = R.rmsd.T # transpose makes it easier for plotting time = rmsd[1]*timestep fig = plt.figure(figsize=(5,4)) ax = fig.add_subplot(111) ax.plot(time, rmsd[2], 'k-', label="all") ax.plot(time, rmsd[3], 'r--', label="type H") ax.plot(time, rmsd[4], 'b--', label="type O") ax.legend(loc="best") ax.set_xlabel("time (ps)") ax.set_ylabel(r"RMSD ($\AA$)") fig.savefig("rmsd_md_analysis.png")
def per_core_work(topology_file_path, trajectory_file_path, list_square_vertex_arrays_this_core, MDA_selection, start_frame, end_frame, reconstruction_index_list, maximum_delta_magnitude): """Run the analysis on one core. The code to perform on a given core given the list of square vertices assigned to it. """ # obtain the relevant coordinates for particles of interest universe_object = MDAnalysis.Universe(topology_file_path, trajectory_file_path) list_previous_frame_centroids = [] list_previous_frame_indices = [] #define some utility functions for trajectory iteration: def produce_list_indices_point_in_polygon_this_frame(vertex_coord_list): list_indices_point_in_polygon = [] for square_vertices in vertex_coord_list: path_object = matplotlib.path.Path(square_vertices) index_list_in_polygon = np.where( path_object.contains_points( relevant_particle_coordinate_array_xy)) list_indices_point_in_polygon.append(index_list_in_polygon) return list_indices_point_in_polygon def produce_list_centroids_this_frame(list_indices_in_polygon): list_centroids_this_frame = [] for indices in list_indices_in_polygon: if not indices[ 0].size > 0: # if there are no particles of interest in this particular square list_centroids_this_frame.append('empty') else: current_coordinate_array_in_square = relevant_particle_coordinate_array_xy[ indices] current_square_indices_centroid = np.average( current_coordinate_array_in_square, axis=0) list_centroids_this_frame.append( current_square_indices_centroid) return list_centroids_this_frame # a list of numpy xy centroid arrays for this frame for ts in universe_object.trajectory: if ts.frame < start_frame: # don't start until first specified frame continue relevant_particle_coordinate_array_xy = universe_object.select_atoms( MDA_selection).coordinates()[..., :-1] # only 2D / xy coords for now #I will need a list of indices for relevant particles falling within each square in THIS frame: list_indices_in_squares_this_frame = produce_list_indices_point_in_polygon_this_frame( list_square_vertex_arrays_this_core) #likewise, I will need a list of centroids of particles in each square (same order as above list): list_centroids_in_squares_this_frame = produce_list_centroids_this_frame( list_indices_in_squares_this_frame) if list_previous_frame_indices: # if the previous frame had indices in at least one square I will need to use # those indices to generate the updates to the corresponding centroids in this frame: list_centroids_this_frame_using_indices_from_last_frame = produce_list_centroids_this_frame( list_previous_frame_indices) #I need to write a velocity of zero if there are any 'empty' squares in either frame: xy_deltas_to_write = [] for square_1_centroid, square_2_centroid in zip( list_centroids_this_frame_using_indices_from_last_frame, list_previous_frame_centroids): if square_1_centroid == 'empty' or square_2_centroid == 'empty': xy_deltas_to_write.append([0, 0]) else: xy_deltas_to_write.append( np.subtract(square_1_centroid, square_2_centroid).tolist()) #xy_deltas_to_write = np.subtract(np.array( # list_centroids_this_frame_using_indices_from_last_frame),np.array(list_previous_frame_centroids)) xy_deltas_to_write = np.array(xy_deltas_to_write) #now filter the array to only contain distances in the range [-8,8] as a placeholder for dealing with PBC # issues (Matthieu seemed to use a limit of 8 as well); xy_deltas_to_write = np.clip(xy_deltas_to_write, -maximum_delta_magnitude, maximum_delta_magnitude) #with the xy and dx,dy values calculated I need to set the values from this frame to previous frame # values in anticipation of the next frame: list_previous_frame_centroids = list_centroids_in_squares_this_frame[:] list_previous_frame_indices = list_indices_in_squares_this_frame[:] else: # either no points in squares or after the first frame I'll just reset the 'previous' values so they # can be used when consecutive frames have proper values list_previous_frame_centroids = list_centroids_in_squares_this_frame[:] list_previous_frame_indices = list_indices_in_squares_this_frame[:] if ts.frame > end_frame: break # stop here return list(zip(reconstruction_index_list, xy_deltas_to_write.tolist()))
def __init__(self, pdb, delta=1.0, atomselection='resname HOH and name O', metadata=None, padding=1.0, sigma=None): """Construct the density from psf and pdb and the atomselection. DC = BfactorDensityCreator(pdb, delta=<delta>, atomselection=<MDAnalysis selection>, metadata=<dict>, padding=2, sigma=None) density = DC.Density() :Arguments: pdb PDB file or :class:`MDAnalysis.Universe`; a PDB is read with the simpl PDB reader. If the Bio.PDB reader is required, either set the *permissive_pdb_reader* flag to ``False`` in :data:`MDAnalysis.core.flags` or supply a Universe that was created with the `permissive` = ``False`` keyword. atomselection selection string (MDAnalysis syntax) for the species to be analyzed delta bin size for the density grid in Angstroem (same in x,y,z) [1.0] metadata dictionary of additional data to be saved with the object padding increase histogram dimensions by padding (on top of initial box size) sigma width (in Angstrom) of the gaussians that are used to build up the density; if None then uses B-factors from pdb For assigning X-ray waters to MD densities one might have to use a sigma of about 0.5 A to obtain a well-defined and resolved x-ray water density that can be easily matched to a broader density distribution. """ u = MDAnalysis.as_Universe(pdb) group = u.select_atoms(atomselection) coord = group.coordinates() logger.info("Selected {0:d} atoms ({1!s}) out of {2:d} total.".format(coord.shape[0], atomselection, len(u.atoms))) smin = np.min(coord, axis=0) - padding smax = np.max(coord, axis=0) + padding BINS = fixedwidth_bins(delta, smin, smax) arange = zip(BINS['min'], BINS['max']) bins = BINS['Nbins'] # get edges by doing a fake run grid, self.edges = np.histogramdd(np.zeros((1, 3)), bins=bins, range=arange, normed=False) self.delta = np.diag(map(lambda e: (e[-1] - e[0]) / (len(e) - 1), self.edges)) self.midpoints = map(lambda e: 0.5 * (e[:-1] + e[1:]), self.edges) self.origin = map(lambda m: m[0], self.midpoints) n_frames = 1 if sigma is None: # histogram individually, and smear out at the same time # with the appropriate B-factor if np.any(group.bfactors == 0.0): wmsg = "Some B-factors are Zero (will be skipped)." logger.warn(wmsg) warnings.warn(wmsg, category=MissingDataWarning) rmsf = Bfactor2RMSF(group.bfactors) grid *= 0.0 # reset grid self.g = self._smear_rmsf(coord, grid, self.edges, rmsf) else: # histogram 'delta functions' grid, self.edges = np.histogramdd(coord, bins=bins, range=arange, normed=False) logger.info("Histogrammed {0:6d} atoms from pdb.".format(len(group.atoms))) # just a convolution of the density with a Gaussian self.g = self._smear_sigma(grid, sigma) try: metadata['pdb'] = pdb except TypeError: metadata = {'pdb': pdb} metadata['atomselection'] = atomselection metadata['n_frames'] = n_frames metadata['sigma'] = sigma self.metadata = metadata logger.info("Histogram completed (initial density in Angstrom**-3)")
def reference(): return mda.Universe(PSF, DCD)
def __init__(self, *args, **kwargs): """Create a density grid from a trajectory. density_from_trajectory(PSF, DCD, delta=1.0, atomselection='name OH2', ...) --> density or density_from_trajectory(PDB, XTC, delta=1.0, atomselection='name OH2', ...) --> density :Arguments: psf/pdb/gro topology file dcd/xtc/trr/pdb trajectory; if reading a single PDB file it is sufficient to just provide it once as a single argument :Keywords: mode 'solvent', 'bulk' or 'all' ('all' does both 'solvent' and \bulk' at the same time and thus :meth:`DensityCreator.Density`` returns a list of densities; this saves time!) ['all'] atomselection selection string (MDAnalysis syntax) for the species to be analyzed ["name OH2"] delta approximate bin size for the density grid in Angstroem (same in x,y,z) (It is slightly adjusted when the box length is not an integer multiple of delta.) [1.0] metadata dictionary of additional data to be saved with the object padding increase histogram dimensions by padding (on top of initial box size) in Angstroem [2.0] soluteselection MDAnalysis selection for the solute, e.g. "protein" [``None``] cutoff With *cutoff*, select '<atomsel> NOT WITHIN <cutoff> OF <soluteselection>' (Special routines that are faster than the standard AROUND selection) [0] verbosity: int level of chattiness; 0 is silent, 3 is verbose [3] :Returns: :class:`hop.sitemap.Density` :TODO: * Should be able to also set skip and start/stop for data collection. .. Note:: * In order to calculate the bulk density, use atomselection='name OH2',soluteselection='protein and not name H*',cutoff=3.5 This will select water oxygens not within 3.5 A of the protein heavy atoms. Alternatively, use the VMD-based :func:`density_from_volmap` function. * The histogramming grid is determined by the initial frames min and max. * metadata will be populated with psf, dcd, and a few other items. This allows more compact downstream processing. """ _kwargs = self.defaults.copy() _kwargs.update(kwargs) kwargs = _kwargs # workaround for python 2.5 *args,**kwargs only allowed: universe_kwargs = {"permissive": kwargs.pop("permissive", False)} self.universe = MDAnalysis.as_Universe(*args, **universe_kwargs) self.mode = kwargs.pop("mode", "all") # 'all' runs modes[1:] if not self.mode in self.modes: errmsg = "DensityCreator: mode must be one of %r, not %r" % (self.modes, self.mode) logger.fatal(errmsg) raise ValueError(errmsg) if self.mode == "all": modes = self.modes[1:] else: modes = [self.mode] self.collectors = [] min_coords = [] max_coords = [] for mode in modes: modeargs = kwargs.copy() if mode == "solvent": modeargs["soluteselection"] = None modeargs["cutoff"] = 0 c = DensityCollector(mode, self.universe, **modeargs) self.collectors.append(c) min_coords.append(c.min_coordinates()) # with default padding from modeargs max_coords.append(c.max_coordinates()) # determine maximum bounding box from initial positions of solvent # (add generous padding... probably more than my default 2 A) smin = numpy.sort(min_coords, axis=0)[0] # the three smallest values smax = numpy.sort(max_coords, axis=0)[-1] # the three largest values for c in self.collectors: c.init_histogram(smin=smin, smax=smax) # also guarantees compatible grid self.densities = {} # densities will be stored with mode as key
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)
help="name of the rotamer library [%default]") parser.add_option("--plotname", dest="plotname", metavar="FILENAME", default=None, help="plot the histogram to FILENAME (the extensions determines the format) " "By default <outputFile>.pdf.") parser.add_option("--no-plot", action="store_false", dest="with_plot", default=True, help="suppress producing plots with --plotname") parser.add_option("--useNOelectron", action="store_true", dest="useNOelectron", help="Set this flag, if the geometic midpoints of N1 and O1 atoms should be " "used for distances measurements.") parser.add_option("--no-useNOelectron", action="store_false", dest="useNOelectron", help="Set this flag, if N1 atoms should be used for distances measurements.") options, args = parser.parse_args() MDAnalysis.start_logging() logger.info(rotcon.get_info_string()) logger.info(rotcon.get_license_string()) logger.info(rotcon.get_citation_string()) # load the reference protein structure try: proteinStructure = MDAnalysis.Universe(*args) except: logger.critical("protein structure and/or trajectory not correctly specified") raise if options.residues is None or len(options.residues) != 2: raise ValueError("Provide residue ids in --residues R1 R2") logger.info("Loading trajectory data as Universe({0})".format(*args))
For the CA CA bonds CA CA 1 0.14000 392459.2 ; 7,(1986),230; BENZENE,PHE,TRP,TYR ; This is the amber aa - between aromatic AA CA S 1 0.17500 189953.6 ; Au_cluster_ff ; This is the parameters for the amber aa - between aromatic AA and Sulfer """ ## Module imports import numpy as np import MDAnalysis from MDAnalysis.analysis.distances import distance_array universe = MDAnalysis.Universe( "hydrophobic_CG.gro") ## Reading in the .gro file5 # Ligand attached P5 atoms P5Atoms = universe.select_atoms("name P5") P5ID = P5Atoms.atoms.ids # ST atoms STAtoms = universe.select_atoms("name ST") STID = STAtoms.atoms.ids # BEN ligands PETAtoms = universe.select_atoms("resname PET") PETID = PETAtoms.atoms.ids P5AtomsPositionArray = P5Atoms.positions # Core atoms STAtomsPositionArray = STAtoms.positions # Sulfur atoms