def testProteinLigand(self): from htmd.builder.solvate import solvate from htmd.parameterization.fftype import fftype, FFTypeMethod from htmd.parameterization.writers import writeFRCMOD # Test protein ligand building with parametrized ligand refdir = home(dataDir=join('test-amber-build', 'protLig')) tmpdir = os.path.join(self.testDir, 'protLig') os.makedirs(tmpdir) mol = Molecule(join(refdir, '3ptb_mod.pdb')) lig = Molecule(join(refdir, 'benzamidine.pdb'), guess=('bonds', 'angles', 'dihedrals')) prm, lig = fftype(lig, method=FFTypeMethod.GAFF2) writeFRCMOD(lig, prm, join(tmpdir, 'mol.frcmod')) lig.segid[:] = 'L' # params = newmol = Molecule() newmol.append(lig) newmol.append(mol) smol = solvate(newmol) params = defaultParam() + [join(tmpdir, 'mol.frcmod'),] _ = build(smol, outdir=tmpdir, param=params, ionize=False) refdir = home(dataDir=join('test-amber-build', 'protLig', 'results')) TestAmberBuild._compareResultFolders(refdir, tmpdir, '3PTB')
def testProteinLigand(self): from htmd.builder.solvate import solvate from htmd.parameterization.fftype import fftype, FFTypeMethod from htmd.parameterization.writers import writeFRCMOD # Test protein ligand building with parametrized ligand refdir = home(dataDir=join('test-amber-build', 'protLig')) tmpdir = os.path.join(self.testDir, 'protLig') os.makedirs(tmpdir) mol = Molecule(join(refdir, '3ptb_mod.pdb')) lig = Molecule(join(refdir, 'benzamidine.pdb'), guess=('bonds', 'angles', 'dihedrals')) prm, lig = fftype(lig, method=FFTypeMethod.GAFF2) writeFRCMOD(lig, prm, join(tmpdir, 'mol.frcmod')) lig.segid[:] = 'L' # params = newmol = Molecule() newmol.append(lig) newmol.append(mol) smol = solvate(newmol) params = defaultParam() + [ join(tmpdir, 'mol.frcmod'), ] _ = build(smol, outdir=tmpdir, param=params, ionize=False) refdir = home(dataDir=join('test-amber-build', 'protLig', 'results')) TestAmberBuild._compareResultFolders(refdir, tmpdir, '3PTB')
def _viewStatesNGL(self, states, statetype, protein, ligand, mols, numsamples): if states is None: states = range(self.macronum) if isinstance(states, int): states = [states] if mols is None: mols = self.getStates(states, statetype, numsamples=min(numsamples, 15)) colors = [0, 1, 3, 4, 5, 6, 7, 9] if protein is None and ligand is None: raise NameError('Please provide either the "protein" or "ligand" parameter for viewStates.') if protein: mol = Molecule() if ligand: mol = mols[0].copy() mol.remove(ligand, _logger=False) mol.coords = np.atleast_3d(mol.coords[:, :, 0]) mol.reps.add(sel='protein', style='NewCartoon', color='Secondary Structure') for i, s in enumerate(states): if protein: mol.reps.add(sel='segid ST{}'.format(s), style='NewCartoon', color='Index') if ligand: mol.reps.add(sel='segid ST{}'.format(s), style='Licorice', color=colors[np.mod(i, len(colors))]) mols[i].filter(ligand, _logger=False) mols[i].set('segid', 'ST{}'.format(s)) tmpcoo = mols[i].coords for j in range(mols[i].numFrames): mols[i].coords = np.atleast_3d(tmpcoo[:, :, j]) mol.append(mols[i]) w = mol.view(viewer='ngl') self._nglButtons(w, statetype, states) return w
def tileMembrane(memb, xmin, ymin, xmax, ymax, buffer=1.5): """ Tile a membrane in the X and Y dimensions to reach a specific size. Parameters ---------- memb xmin ymin xmax ymax buffer Returns ------- megamemb : A big membrane Molecule """ from htmd.progress.progress import ProgressBar memb = memb.copy() memb.resid = sequenceID(memb.resid) minmemb = np.min(memb.get('coords', 'water'), axis=0).flatten() size = np.max(memb.get('coords', 'water'), axis=0) - np.min(memb.get('coords', 'water'), axis=0) size = size.flatten() xreps = int(np.ceil((xmax - xmin) / size[0])) yreps = int(np.ceil((ymax - ymin) / size[1])) logger.info('Replicating Membrane {}x{}'.format(xreps, yreps)) from htmd.molecule.molecule import Molecule megamemb = Molecule() bar = ProgressBar(xreps * yreps, description='Replicating Membrane') k = 0 for x in range(xreps): for y in range(yreps): tmpmemb = memb.copy() xpos = xmin + x * (size[0] + buffer) ypos = ymin + y * (size[1] + buffer) tmpmemb.moveBy([-float(minmemb[0]) + xpos, -float(minmemb[1]) + ypos, 0]) tmpmemb.remove('same resid as (x > {} or y > {})'.format(xmax, ymax), _logger=False) tmpmemb.set('segid', 'M{}'.format(k)) megamemb.append(tmpmemb) k += 1 bar.progress() bar.stop() # Membranes don't tile perfectly. Need to remove waters that clash with lipids of other tiles # Some clashes will still occur between periodic images however megamemb.remove('same fragment as water and within 1.5 of not water', _logger=False) return megamemb
def protein_optimization(complex_path): """Optimize protein inside complex file and rewrite complex. complex_path : pathlib.Path Path to complex file """ complex = Molecule(str(complex_path)) prot = complex.copy() prot.filter("protein") lig = complex.copy() lig.filter(constants.ligand_selector) #prot = proteinPrepare(prot, pH=7.0) mol = Molecule(name="complex") mol.append(prot) mol.append(lig) mol.write(str(complex_path))
def viewCrystalPacking(mol, hexagonal=False, style_display='NewCartoon'): """ Views the crystal packing of a protein Parameters ---------- pdbfile : str Path to the pdb file which to read. Can also be a 4-letter PDB ID """ if mol.crystalinfo is None or 'numcopies' not in mol.crystalinfo: raise RuntimeError('No crystallography data found in Molecule.') ci = mol.crystalinfo alpha, beta, gamma, a, b, c = ci['alpha'], ci['beta'], ci['gamma'], ci['a'], ci['b'], ci['c'] alpha = np.deg2rad(float(alpha)) beta = np.deg2rad(float(beta)) gamma = np.deg2rad(float(gamma)) caux = (np.cos(alpha) - np.cos(beta) * np.cos(gamma)) / np.sin(gamma) axes = np.array([[a, 0, 0], [b * np.cos(gamma), b * np.sin(gamma), 0], [c * np.cos(beta), c * caux, c * np.sqrt(1 - np.cos(beta) ** 2 - caux ** 2)]]) size = np.array([axes[0][0], axes[1][1], axes[2][2]]) molunit = Molecule() viewer = VMD() _draw_cell(axes, ci['sGroup'], viewer, hexagonal=hexagonal) # Creates copies of the molecule and places them correctly inside the complete Unit Cell hexagonal_molunit = None for i in range(ci['numcopies']): molecule = mol.copy() # apply SMTRY (Crystal Symmetry) operations molecule.rotateBy(ci['rotations'][i]) molecule.moveBy(ci['translations'][i]) # apply translation to inside of same Unit Cell. _place_crystal(molecule, size, [alpha, beta, gamma], axes) # pack copies to target Unit Cell molunit.append(molecule) if ci['sGroup'][0] == 'H' and hexagonal: hexagonal_molunit = Molecule() _build_hexagon(molunit, hexagonal_molunit) if hexagonal_molunit is not None: hexagonal_molunit.view(style=style_display, viewerhandle=viewer) else: molunit.view(style=style_display, viewerhandle=viewer)
def _viewStatesNGL(self, states, statetype, protein, ligand, mols, numsamples): if states is None: states = range(self.macronum) if isinstance(states, int): states = [states] if mols is None: mols = self.getStates(states, statetype, numsamples=min(numsamples, 15)) colors = [0, 1, 3, 4, 5, 6, 7, 9] if protein is None and ligand is None: raise NameError( 'Please provide either the "protein" or "ligand" parameter for viewStates.' ) if protein: mol = Molecule() if ligand: mol = mols[0].copy() mol.remove(ligand, _logger=False) mol.coords = np.atleast_3d(mol.coords[:, :, 0]) mol.reps.add(sel='protein', style='NewCartoon', color='Secondary Structure') for i, s in enumerate(states): if protein: mol.reps.add(sel='segid ST{}'.format(s), style='NewCartoon', color='Index') if ligand: mol.reps.add(sel='segid ST{}'.format(s), style='Licorice', color=colors[np.mod(i, len(colors))]) mols[i].filter(ligand, _logger=False) mols[i].guessBonds() mols[i].set('segid', 'ST{}'.format(s)) tmpcoo = mols[i].coords for j in range(mols[i].numFrames): mols[i].coords = np.atleast_3d(tmpcoo[:, :, j]) mol.append(mols[i]) w = mol.view(viewer='ngl') self._nglButtons(w, statetype, states) return w
def _viewStatesNGL(self, states, statetype, protein, ligand, mols, numsamples, gui=False): from htmd.builder.builder import sequenceID if states is None: states = range(self.macronum) if isinstance(states, int): states = [states] if mols is None: mols = self.getStates(states, statetype, numsamples=min(numsamples, 15)) colors = [0, 1, 3, 4, 5, 6, 7, 9] hexcolors = {0: '#0000ff', 1: '#ff0000', 2: '#333333', 3: '#ff6600', 4: '#ffff00', 5: '#4c4d00', 6: '#b2b2cc', 7: '#33cc33', 8: '#ffffff', 9: '#ff3399', 10: '#33ccff'} if protein is None and ligand is None: raise NameError('Please provide either the "protein" or "ligand" parameter for viewStates.') k = 0 from nglview import NGLWidget, HTMDTrajectory view = NGLWidget(gui=gui) ref = mols[0].copy() for i, s in enumerate(states): if protein: mol = Molecule() if ligand: mol = ref.copy() mol.remove(ligand, _logger=False) mol.coords = np.atleast_3d(mol.coords[:, :, 0]) mols[i].filter(ligand, _logger=False) mols[i].set('chain', '{}'.format(s)) tmpcoo = mols[i].coords for j in range(mols[i].numFrames): mols[i].coords = np.atleast_3d(tmpcoo[:, :, j]) if ligand: mols[i].set('segid', sequenceID(mols[i].resid)+k) k = int(mols[i].segid[-1]) mol.append(mols[i]) view.add_trajectory(HTMDTrajectory(mol)) # Setting up representations if ligand: view[i].add_cartoon('protein', color='sstruc') view[i].add_hyperball(':{}'.format(s), color=hexcolors[np.mod(i, len(hexcolors))]) if protein: view[i].add_cartoon('protein', color='residueindex') self._nglButtons(view, statetype, states) return view
def _viewStatesNGL(self, states, statetype, protein, ligand, mols, numsamples, gui=False): from htmd.molecule.util import sequenceID if states is None: states = range(self.macronum) if isinstance(states, int): states = [states] if mols is None: mols = self.getStates(states, statetype, numsamples=min(numsamples, 15)) colors = [0, 1, 3, 4, 5, 6, 7, 9] hexcolors = {0: '#0000ff', 1: '#ff0000', 2: '#333333', 3: '#ff6600', 4: '#ffff00', 5: '#4c4d00', 6: '#b2b2cc', 7: '#33cc33', 8: '#ffffff', 9: '#ff3399', 10: '#33ccff'} if protein is None and ligand is None: raise NameError('Please provide either the "protein" or "ligand" parameter for viewStates.') k = 0 from nglview import NGLWidget, HTMDTrajectory view = NGLWidget(gui=gui) ref = mols[0].copy() for i, s in enumerate(states): if protein: mol = Molecule() if ligand: mol = ref.copy() mol.remove(ligand, _logger=False) mol.dropFrames(keep=0) mols[i].filter(ligand, _logger=False) mols[i].set('chain', '{}'.format(s)) tmpcoo = mols[i].coords for j in range(mols[i].numFrames): mols[i].coords = np.atleast_3d(tmpcoo[:, :, j]) if ligand: mols[i].set('segid', sequenceID(mols[i].resid)+k) k = int(mols[i].segid[-1]) mol.append(mols[i]) view.add_trajectory(HTMDTrajectory(mol)) # Setting up representations if ligand: view[i].add_cartoon('protein', color='sstruc') view[i].add_hyperball(':{}'.format(s), color=hexcolors[np.mod(i, len(hexcolors))]) if protein: view[i].add_cartoon('protein', color='residueindex') self._nglButtons(view, statetype, states) return view
def tileMembrane(memb, xmin, ymin, xmax, ymax): """ Tile the membrane in the X and Y dimensions to reach a specific size. Returns ------- megamemb : A big membrane Molecule """ from htmd.progress.progress import ProgressBar memb = memb.copy() memb.resid = sequenceID(memb.resid) minmemb = np.min(memb.get('coords', 'water'), axis=0).flatten() size = np.max(memb.get('coords', 'water'), axis=0) - np.min( memb.get('coords', 'water'), axis=0) size = size.flatten() xreps = int(np.ceil((xmax - xmin) / size[0])) yreps = int(np.ceil((ymax - ymin) / size[1])) logger.info('Replicating Membrane {}x{}'.format(xreps, yreps)) from htmd.molecule.molecule import Molecule megamemb = Molecule() bar = ProgressBar(xreps * yreps, description='Replicating Membrane') k = 0 for x in range(xreps): for y in range(yreps): tmpmemb = memb.copy() xpos = xmin + x * size[0] ypos = ymin + y * size[1] tmpmemb.moveBy( [-float(minmemb[0]) + xpos, -float(minmemb[1]) + ypos, 0]) sel = 'same resid as (x > {} or y > {})'.format(xmax, ymax) tmpmemb.remove(sel, _logger=False) tmpmemb.set('segid', 'M{}'.format(k)) megamemb.append(tmpmemb) k += 1 bar.progress() bar.stop() return megamemb
def tileMembrane(memb, xmin, ymin, xmax, ymax): """ Tile the membrane in the X and Y dimensions to reach a specific size. Returns ------- megamemb : A big membrane Molecule """ from htmd.progress.progress import ProgressBar memb = memb.copy() memb.resid = sequenceID(memb.resid) minmemb = np.min(memb.get('coords', 'water'), axis=0).flatten() size = np.max(memb.get('coords', 'water'), axis=0) - np.min(memb.get('coords', 'water'), axis=0) size = size.flatten() xreps = int(np.ceil((xmax - xmin) / size[0])) yreps = int(np.ceil((ymax - ymin) / size[1])) logger.info('Replicating Membrane {}x{}'.format(xreps, yreps)) from htmd.molecule.molecule import Molecule megamemb = Molecule() bar = ProgressBar(xreps * yreps, description='Replicating Membrane') k = 0 for x in range(xreps): for y in range(yreps): tmpmemb = memb.copy() xpos = xmin + x * size[0] ypos = ymin + y * size[1] tmpmemb.moveBy([-float(minmemb[0]) + xpos, -float(minmemb[1]) + ypos, 0]) sel = 'same resid as (x > {} or y > {})'.format(xmax, ymax) tmpmemb.remove(sel, _logger=False) tmpmemb.set('segid', 'M{}'.format(k)) megamemb.append(tmpmemb) k += 1 bar.progress() bar.stop() return megamemb
def tileMembrane(memb, xmin, ymin, xmax, ymax, buffer=1.5): """ Tile a membrane in the X and Y dimensions to reach a specific size. Parameters ---------- memb : :class:`Molecule <htmd.molecule.molecule.Molecule>` object The membrane to be tiled xmin : float Minimum x coordinate ymin : float Minimum y coordinate xmax : float Maximum x coordinate ymax : float Maximum y coordinate buffer : float Buffer distance between tiles Returns ------- megamemb : A big membrane Molecule """ from tqdm import tqdm memb = memb.copy() memb.resid = sequenceID((memb.resid, memb.insertion, memb.chain, memb.segid)) minmemb = np.min(memb.get('coords', 'water'), axis=0).flatten() size = np.max(memb.get('coords', 'water'), axis=0) - np.min(memb.get('coords', 'water'), axis=0) size = size.flatten() xreps = int(np.ceil((xmax - xmin) / size[0])) yreps = int(np.ceil((ymax - ymin) / size[1])) logger.info('Replicating Membrane {}x{}'.format(xreps, yreps)) from htmd.molecule.molecule import Molecule megamemb = Molecule() bar = tqdm(total=xreps * yreps, desc='Replicating Membrane') k = 0 for x in range(xreps): for y in range(yreps): tmpmemb = memb.copy() xpos = xmin + x * (size[0] + buffer) ypos = ymin + y * (size[1] + buffer) tmpmemb.moveBy([-float(minmemb[0]) + xpos, -float(minmemb[1]) + ypos, 0]) tmpmemb.remove('same resid as (x > {} or y > {})'.format(xmax, ymax), _logger=False) if tmpmemb.numAtoms == 0: continue tmpmemb.set('segid', 'M{}'.format(k), sel='not water') tmpmemb.set('segid', 'MW{}'.format(k), sel='water') megamemb.append(tmpmemb) k += 1 bar.update(1) bar.close() # Membranes don't tile perfectly. Need to remove waters that clash with lipids of other tiles # Some clashes will still occur between periodic images however megamemb.remove('same resid as water and within 1.5 of not water', _logger=False) return megamemb
def sampleRegion(self, point=None, radius=None, limits=None, nsamples=20, singlemol=False): """ Samples conformations from a region in the projected space. Parameters ---------- point : list or np.ndarray A point in the projected space. Undefined dimensions should have None value. radius : float The radius in around the point in which to sample conformations. limits : np.ndarray A (2, ndim) dimensional array containing the min (1st row) and max (2nd row) limits for each dimension. None values will be interpreted as no limit in that dimension, or min/max value. nsamples : int The number of conformations to sample. singlemol : bool If True it will return all samples within a single Molecule instead of a list of Molecules. Returns ------- absFrames : list A list of the absolute frame indexes sampled relFrames : list of tuples A list of (trajNum, frameNum) tuples sampled mols : Molecule or list of Molecules The conformations stored in a Molecule or a list of Molecules Examples -------- >>> # Working with 4 dimensional data for example >>> abs, rel, mols = data.sampleRegion(point=(0.5, 3, None, None), radius=0.1) # Point undefined in dim 3, 4 >>> minlims = [-1, None, None, 4] # No min limit for 2, 3 dim >>> maxlims = [2, 3, None, 7] # No max limit for 3 dim >>> abs, rel, mols = data.sampleRegion(limits=np.array([minlims, maxlims])) """ from scipy.spatial.distance import cdist datconcat = np.concatenate(self.dat) numdim = datconcat.shape[1] if point is not None: if radius is None: raise RuntimeError('You must define a radius with a point.') point = np.array(point) if len(point) != numdim: raise RuntimeError( 'Argument `point` should be same dimensionality as your data ({} dimensions)' .format(numdim)) keepdim = np.array([p is not None for p in point]) dists = cdist(datconcat[:, keepdim], [point[keepdim]]) confs = np.where(dists < radius)[0] elif limits is not None: if limits.shape != (2, numdim): raise RuntimeError( 'Argument `limits` should be of shape (2, {})'.format( numdim)) mask = np.ones(datconcat.shape[0], dtype=bool) for i in range(numdim): if limits[0, i] is not None: mask &= datconcat[:, i] > limits[0, i] if limits[1, i] is not None: mask &= datconcat[:, i] < limits[1, i] confs = np.where(mask)[0] if len(confs) > nsamples: confs = np.random.choice(confs, nsamples, replace=False) sims = self.abs2sim(confs) from htmd.molecule.molecule import Molecule if singlemol: mol = Molecule(sims[0]) for i in range(1, len(sims)): m = Molecule(sims[i]) mol.appendFrames(m) else: mol = [] for s in sims: mol.append(Molecule(s)) return confs, self.abs2rel(confs), mol
bmol = build(smol, ff=ffs, outdir=tmpdir) refdir = home(dataDir=join('test-amber-build-nopp', pid)) _compareResultFolders(refdir, tmpdir, pid) shutil.rmtree(tmpdir) # Test protein ligand building with parametrized ligand mol = Molecule('3ptb') mol.filter('protein') mol.renumberResidues() lig = Molecule( join(home(dataDir='test-param'), 'h2o2_gaff2', 'parameters', 'GAFF2', 'B3LYP-cc-pVDZ-vacuum', 'mol.mol2')) lig.segid[:] = 'L' newmol = Molecule() newmol.append(lig) newmol.append(mol) smol = solvate(newmol) tmpdir = tempname() bmol = build(newmol, outdir=tmpdir, ionize=False) shutil.rmtree(tmpdir) # # Test protein-ligand building # folder = home(dataDir='building-protein-ligand') # prot = Molecule(os.path.join(folder, 'trypsin.pdb')) # prot.filter('protein') # prot.renumberResidues() # prot = autoSegment2(prot) # prot = proteinPrepare(prot) # prot1 = prot # prot2 = prot.copy()
def sampleRegion(self, point=None, radius=None, limits=None, nsamples=20, singlemol=False): """ Samples conformations from a region in the projected space. Parameters ---------- point : list or np.ndarray A point in the projected space. Undefined dimensions should have None value. radius : float The radius in around the point in which to sample conformations. limits : np.ndarray A (2, ndim) dimensional array containing the min (1st row) and max (2nd row) limits for each dimension. None values will be interpreted as no limit in that dimension, or min/max value. nsamples : int The number of conformations to sample. singlemol : bool If True it will return all samples within a single Molecule instead of a list of Molecules. Returns ------- absFrames : list A list of the absolute frame indexes sampled relFrames : list of tuples A list of (trajNum, frameNum) tuples sampled mols : Molecule or list of Molecules The conformations stored in a Molecule or a list of Molecules Examples -------- >>> # Working with 4 dimensional data for example >>> abs, rel, mols = data.sampleRegion(point=(0.5, 3, None, None), radius=0.1) # Point undefined in dim 3, 4 >>> minlims = [-1, None, None, 4] # No min limit for 2, 3 dim >>> maxlims = [2, 3, None, 7] # No max limit for 3 dim >>> abs, rel, mols = data.sampleRegion(limits=np.array([minlims, maxlims])) """ from scipy.spatial.distance import cdist datconcat = np.concatenate(self.dat) numdim = datconcat.shape[1] if point is not None: if radius is None: raise RuntimeError('You must define a radius with a point.') point = np.array(point) if len(point) != numdim: raise RuntimeError( 'Argument `point` should be same dimensionality as your data ({} dimensions)'.format(numdim)) keepdim = np.array([p is not None for p in point]) dists = cdist(datconcat[:, keepdim], [point[keepdim]]) confs = np.where(dists < radius)[0] elif limits is not None: if limits.shape != (2, numdim): raise RuntimeError('Argument `limits` should be of shape (2, {})'.format(numdim)) mask = np.ones(datconcat.shape[0], dtype=bool) for i in range(numdim): if limits[0, i] is not None: mask &= datconcat[:, i] > limits[0, i] if limits[1, i] is not None: mask &= datconcat[:, i] < limits[1, i] confs = np.where(mask)[0] if len(confs) > nsamples: confs = np.random.choice(confs, nsamples, replace=False) sims = self.abs2sim(confs) from htmd.molecule.molecule import Molecule if singlemol: mol = Molecule(sims[0]) for i in range(1, len(sims)): m = Molecule(sims[i]) mol.appendFrames(m) else: mol = [] for s in sims: mol.append(Molecule(s)) return confs, self.abs2rel(confs), mol