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 _singleMolfile(sims): from htmd.molecule.molecule import mol_equal from htmd.util import ensurelist if isinstance(sims, Molecule): return False, [] elif isinstance(sims, np.ndarray): molfiles = [] for s in sims: molfiles.append(tuple(ensurelist(s.molfile))) uqmolfiles = list(set(molfiles)) if len(uqmolfiles) == 0: raise RuntimeError('No molfiles found in simlist') elif len(uqmolfiles) == 1: return True, uqmolfiles[0] elif len( uqmolfiles ) > 1: # If more than one molfile load them and see if they are different Molecules ref = Molecule(uqmolfiles[0], _logger=False) for i in range(1, len(uqmolfiles)): mol = Molecule(uqmolfiles[i], _logger=False) if not mol_equal(ref, mol, exceptFields=['coords']): return False, [] return True, uqmolfiles[0] return False, []
def setUp(self): molFile = os.path.join(home('test-qm'), 'H2-0.74.mol2') self.h2_074 = Molecule(molFile) molFile = os.path.join(home('test-qm'), 'H2-1.00.mol2') self.h2_100 = Molecule(molFile) molFile = os.path.join(home('test-qm'), 'H2O2-90.mol2') self.h2o2_90 = Molecule(molFile)
def write(self, inputdir, outputdir): """ Writes the production protocol and files into a folder. Parameters ---------- inputdir : str Path to a directory containing the files produced by a equilibration process. outputdir : str Directory where to write the production setup files. """ self._findFiles(inputdir) self._amberFixes() from htmd.units import convert numsteps = convert(self.timeunits, 'timesteps', self.runtime, timestep=self.acemd.timestep) self.acemd.temperature = str(self.temperature) self.acemd.langevintemp = str(self.temperature) if self.fb_k > 0: #use TCL only for flatbottom mol = Molecule(os.path.join(inputdir, self.acemd.coordinates)) self.acemd.tclforces = 'on' tcl = list(self.acemd.TCL) tcl[0] = tcl[0].format(NUMSTEPS=numsteps, KCONST=self.fb_k, REFINDEX=' '.join(map(str, mol.get('index', self.fb_reference))), SELINDEX=' '.join(map(str, mol.get('index', self.fb_selection))), BOX=' '.join(map(str, self.fb_box))) self.acemd.TCL = tcl[0] + tcl[1] else: self.acemd.TCL = 'set numsteps {}\n'.format(numsteps) if self.useconstraints: # Turn on constraints self.acemd.constraints = 'on' self.acemd.constraintscaling = '1.0' else: if len(self.constraints) != 0: logger.warning('You have setup constraints to {} but constraints are turned off. ' 'If you want to use constraints, define useconstraints=True'.format(self.constraints)) self.acemd.setup(inputdir, outputdir, overwrite=True) # Adding constraints if self.useconstraints: inmol = Molecule(os.path.join(inputdir, self.acemd.coordinates)) inmol.set('occupancy', 0) inmol.set('beta', 0) if len(self.constraints) == 0: raise RuntimeError('You have set the production to use constraints (useconstraints=True), but have not ' 'defined any constraints (constraints={}).') else: for sel in self.constraints: inmol.set('beta', self.constraints[sel], sel) outfile = os.path.join(outputdir, self.acemd.coordinates) inmol.write(outfile) self.acemd.consref = self.acemd.coordinates
def test_maximalSubstructureAlignment(self): from htmd.home import home from htmd.molecule.molecule import Molecule path = home(dataDir='test-molecule-graphalignment') ref_lig = Molecule(os.path.join(path, 'ref_lig.pdb')) lig2align = Molecule(os.path.join(path, 'lig2align.pdb')) lig_aligned = maximalSubstructureAlignment(ref_lig, lig2align) lig_reference = Molecule(os.path.join(path, 'lig_aligned.pdb')) self.assertTrue( np.allclose(lig_aligned.coords, lig_reference.coords, rtol=1e-4), 'maximalSubstructureAlignment produced different coords')
def test_build(self): from htmd.molecule.molecule import Molecule from htmd.builder.solvate import solvate from htmd.home import home from htmd.util import tempname, assertSameAsReferenceDir import os import numpy as np # Use pre-prepared files so we can tell whether the error is in prepare or in build # Inputs are reference outputs of proteinprepare. preparedInputDir = home(dataDir='test-proteinprepare') pdbids = ['3PTB', '1A25', '1GZM', '1U5U'] for pdb in pdbids: with self.subTest(pdb=pdb): print('Building {}'.format(pdb)) inFile = os.path.join(preparedInputDir, pdb, "{}-prepared.pdb".format(pdb)) mol = Molecule(inFile) mol.filter( 'protein') # Fix for bad proteinPrepare hydrogen placing np.random.seed(1) # Needed for ions smol = solvate(mol) topos = ['top/top_all36_prot.rtf', 'top/top_water_ions.rtf'] params = [ 'par/par_all36_prot_mod.prm', 'par/par_water_ions.prm' ] tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir) compareDir = home( dataDir=os.path.join('test-charmm-build', pdb)) assertSameAsReferenceDir(compareDir, tmpdir)
def test_disulfideWithInsertion(self): from htmd.molecule.molecule import Molecule from htmd.builder.solvate import solvate from htmd.home import home from htmd.util import tempname, assertSameAsReferenceDir import os import numpy as np # Use pre-prepared files so we can tell whether the error is in prepare or in build # Inputs are reference outputs of proteinprepare. preparedInputDir = home(dataDir='test-proteinprepare') pdb = '3PTB' print('Building {}'.format(pdb)) inFile = os.path.join(preparedInputDir, pdb, "{}-prepared.pdb".format(pdb)) mol = Molecule(inFile) mol.filter('protein') # Fix for bad proteinPrepare hydrogen placing np.random.seed(1) # Needed for ions smol = solvate(mol) topos = ['top/top_all36_prot.rtf', 'top/top_water_ions.rtf'] params = ['par/par_all36_prot_mod.prm', 'par/par_water_ions.prm'] smol.insertion[ smol.resid == 42] = 'A' # Adding an insertion to test that disulfide bonds with insertions work tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir) compareDir = home( dataDir=os.path.join('test-charmm-build', '3PTB_insertion')) assertSameAsReferenceDir(compareDir, tmpdir)
def _fb_potential2restraints(self, inputdir): from htmd.molecule.molecule import Molecule restraints = list() fb_box = np.array(self.fb_box) # convert fb_box to width width = list( np.concatenate( np.diff(np.array([fb_box[::2], fb_box[1::2]]), axis=0))) # If fb_box is not symmetrical if not np.all(fb_box[::2] == -fb_box[1::2]): # convert fb_box and fb_reference to fbcentre and width mol = Molecule(os.path.join(inputdir, self.acemd.structure)) mol.read(os.path.join(inputdir, self.acemd.coordinates)) fb_refcentre = mol.get( 'coords', sel=self.fb_reference).mean(axis=0).squeeze() fbcentre = list( np.around( np.mean(np.array([fb_box[::2], fb_box[1::2]]), axis=0) + fb_refcentre, 3)) restraints.append( GroupRestraint(self.fb_selection, width, [(self.fb_k, 0)], fbcentre=fbcentre)) else: restraints.append( GroupRestraint(self.fb_selection, width, [(self.fb_k, 0)], fbcentresel=self.fb_reference)) return restraints
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 _filterTopology(sim, outfolder, filtsel): try: from htmd.molecule.molecule import Molecule mol = Molecule(sim.molfile) except IOError as e: raise RuntimeError( 'simFilter: {}. Cannot read topology file {}'.format( e, sim.molfile)) ext = os.path.splitext(sim.molfile)[1][1:] filttopo = path.join(outfolder, 'filtered.{}'.format(ext)) filtpdb = path.join(outfolder, 'filtered.pdb') if not path.isfile(filttopo): try: mol.write(filttopo, filtsel) except Exception as e: logger.warning('Was not able to write {} due to error: {}'.format( filttopo, e)) if mol.coords.size == 0: # If we read for example psf or prmtop which have no coords, just add 0s everywhere mol.coords = np.zeros((mol.numAtoms, 3, 1), dtype=np.float32) try: mol.write(filtpdb, filtsel) except Exception as e: logger.warning('Was not able to write {} due to error: {}'.format( filtpdb, e))
def _run(self, molName): self.molName = molName logger.info('Molecule: {}'.format(self.molName)) molFile = os.path.join(home('test-charge'), self.molName + '.mol2') self.mol = Molecule(molFile) self.new_mols = {} self.extras = {} self.new_mols['Gasteiger'] = fitGasteigerCharges(self.mol) try: self.new_mols['AM1-BCC'] = fitChargesWithAntechamber(self.mol, type='bcc') except: pass qm = Psi4() qm.theory = 'B3LYP' qm.basis = '6-311++G**' workDir = os.path.join('tmp', self.molName) os.makedirs(workDir, exist_ok=True) for factor in [-10, -5, -4, -3, -2, -1]: logger.info('Factor: {}'.format(factor)) key = 'ESP b {}'.format(factor) np.random.seed(20181114) # Make ESP grid generation deterministic self.new_mols[key], self.extras[key] = fitESPCharges(self.mol, qm, workDir, restraint_factor=10**factor)
def write(self, inputdir, outputdir): """ Writes the production protocol and files into a folder. Parameters ---------- inputdir : str Path to a directory containing the files produced by a equilibration process. outputdir : str Directory where to write the production setup files. """ self._findFiles(inputdir) self._amberFixes() from htmd.units import convert numsteps = convert(self.timeunits, 'timesteps', self.runtime, timestep=self.acemd.timestep) self.acemd.temperature = str(self.temperature) self.acemd.langevintemp = str(self.temperature) if self.fb_k > 0: #use TCL only for flatbottom mol = Molecule(os.path.join(inputdir, self.acemd.coordinates)) self.acemd.tclforces = 'on' tcl = list(self.acemd.TCL) tcl[0] = tcl[0].format(NUMSTEPS=numsteps, KCONST=self.fb_k, REFINDEX=' '.join(map(str, mol.get('index', self.fb_reference))), SELINDEX=' '.join(map(str, mol.get('index', self.fb_selection))), BOX=' '.join(map(str, self.fb_box))) self.acemd.TCL = tcl[0] + tcl[1] else: self.acemd.TCL = 'set numsteps {}\n'.format(numsteps) self.acemd.setup(inputdir, outputdir, overwrite=True)
def getParameters(self, outdir="./", outname="mol"): if not self.completed: raise ValueError("Parameterization is not complete") # look for the right files in the output directory d = os.path.join(self.directory, "999-results") rtf = os.path.join(d, "mol.rtf") prm = os.path.join(d, "mol.prm") xyz = os.path.join(d, "mol.xyz") # pdb = os.path.join(d, "mol.pdb") rtf_tmp = outdir + outname + ".rtf" prm_tmp = outdir + outname + ".prm" pdb_tmp = outdir + outname + ".pdb" # xyz_tmp = tempfile.mkstemp(suffix=".xyz") # os.close(rtf_tmp[0]) # os.close(prm_tmp[0]) # os.close(pdb_tmp[0]) # os.close(xyz_tmp[0]) shutil.copyfile(rtf, rtf_tmp) shutil.copyfile(prm, prm_tmp) # shutil.copyfile(xyz, xyz_tmp[1]) # The Output minimised structure is in XYZ format # Need to turn it into a PDB with correct atom naming mol = Molecule(xyz) Parameterization._rename_mol( mol) # Canonicalise atom and reside naming mol.write(pdb_tmp) return
def _writeInputsFunction(i, f, epoch, inputpath): regex = re.compile('(e\d+s\d+)_') frameNum = f.frame piece = f.piece if f.sim.parent is None: currSim = f.sim else: currSim = f.sim.parent traj = currSim.trajectory[piece] if currSim.input is None: raise NameError( 'Could not find input folder in simulation lists. Cannot create new simulations.' ) wuName = _simName(traj) res = regex.search(wuName) if res: # If we are running on top of adaptive, use the first name part for the next sim name wuName = res.group(1) # create new job directory newName = 'e' + str(epoch) + 's' + str(i + 1) + '_' + wuName + 'p' + str( piece) + 'f' + str(frameNum) newDir = path.join(inputpath, newName, '') # copy previous input directory including input files copytree(currSim.input, newDir, symlinks=False, ignore=ignore_patterns(_TRAJ_EXTENSIONS_COOR)) # overwrite input file with new one. frameNum + 1 as catdcd does 1 based indexing mol = Molecule() mol.read(traj) mol.frame = frameNum mol.write(path.join(newDir, 'input.coor'))
def write(self, inputdir=None, outputdir=None): """ Writes the production protocol and files into a folder. Parameters ---------- inputdir : str Path to a directory containing the files produced by a equilibration process. outputdir : str Directory where to write the production setup files. """ if inputdir is not None: self.inputdir = inputdir if outputdir is not None: self.outputdir = outputdir self.acemd.temperature = self.temperature self.acemd.langevintemp = self.temperature if self.k > 0: #use TCL only for flatbottom mol = Molecule(os.path.join(self.inputdir, self.acemd.coordinates)) self.acemd.tclforces = 'on' TCL = self._TCL TCL = TCL.replace('KCONST', str(self.k)) TCL = TCL.replace( 'REFINDEX', ' '.join(map(str, mol.get('index', self.reference)))) TCL = TCL.replace( 'SELINDEX', ' '.join(map(str, mol.get('index', self.selection)))) TCL = TCL.replace('BOX', ' '.join(map(str, self.box))) self.acemd.TCL = TCL self.acemd.setup(self.inputdir, self.outputdir, overwrite=True)
def write(self, inputdir, outputdir): """ Writes the production protocol and files into a folder. Parameters ---------- inputdir : str Path to a directory containing the files produced by a equilibration process. outputdir : str Directory where to write the production setup files. """ self._findFiles(inputdir) self._amberFixes() self.acemd.temperature = str(self.temperature) self.acemd.langevintemp = str(self.temperature) if self.k > 0: #use TCL only for flatbottom mol = Molecule(os.path.join(inputdir, self.acemd.coordinates)) self.acemd.tclforces = 'on' TCL = self._TCL TCL = TCL.replace('NUMSTEPS', str(self.numsteps)) TCL = TCL.replace('KCONST', str(self.k)) TCL = TCL.replace('REFINDEX', ' '.join(map(str, mol.get('index', self.reference)))) TCL = TCL.replace('SELINDEX', ' '.join(map(str, mol.get('index', self.selection)))) TCL = TCL.replace('BOX', ' '.join(map(str, self.box))) self.acemd.TCL = TCL else: self.acemd.TCL = 'set numsteps {}\n'.format(self.numsteps) self.acemd.setup(inputdir, outputdir, overwrite=True)
def _filterTopology(sim, outfolder, filtsel): from htmd.util import ensurelist try: from htmd.molecule.molecule import Molecule mol = Molecule(sim.molfile) except IOError as e: raise RuntimeError( 'simFilter: {}. Cannot read topology file {}'.format( e, sim.molfile)) if mol.coords.size == 0: # If we read for example psf or prmtop which have no coords, just add 0s everywhere mol.coords = np.zeros((mol.numAtoms, 3, 1), dtype=np.float32) extensions = [ 'pdb', ] # Adding pdb to make sure it's always written for m in ensurelist(sim.molfile): extensions.append(os.path.splitext(m)[1][1:]) for ext in list(set(extensions)): filttopo = path.join(outfolder, 'filtered.{}'.format(ext)) if not path.isfile(filttopo): try: mol.write(filttopo, filtsel) except Exception as e: logger.warning( 'Filtering was not able to write {} due to error: {}'. format(filttopo, e))
def _prep_and_run(self, mol, rtf, prm, outdir, solvated): from htmd.builder.solvate import solvate from htmd.apps.acemdlocal import AcemdLocal # Do a simple solvation then run for 50ns ionize = True mol = Molecule(mol) mol.center() mol.set("segid", "L") d = maxDistance(mol, 'all') + 6 if solvated: mol = solvate(mol, minmax=[[-d, -d, -d], [d, d, d]]) if not solvated: ionize = False build_dir = os.path.join(outdir, "build") equil_dir = os.path.join(outdir, "equil") rtfs = ['top/top_water_ions.rtf', rtf] prms = ['par/par_water_ions.prm', prm] charmm.build(mol, topo=rtfs, param=prms, outdir=build_dir, ionize=ionize) md = Equilibration() md.runtime = 50 md.timeunits = 'ns' md.temperature = 300 md.write(build_dir, equil_dir) mdx = AcemdLocal() mdx.submit(equil_dir) mdx.wait()
def test_customDisulfideBonds(self): from htmd.builder.solvate import solvate # Test without proteinPrepare pdbids = [ '1GZM', ] for pid in pdbids: np.random.seed(1) mol = Molecule(pid) mol.filter('protein') smol = solvate(mol) ffs = defaultFf() disu = [['segid 1 and resid 110', 'segid 1 and resid 187'], ['segid 0 and resid 110', 'segid 0 and resid 187']] tmpdir = os.path.join(self.testDir, 'withoutProtPrep', pid) _ = build(smol, ff=ffs, outdir=tmpdir, disulfide=disu) refdir = home(dataDir=join('test-amber-build', 'nopp', pid)) TestAmberBuild._compareResultFolders(refdir, tmpdir, pid) np.random.seed(1) tmpdir = os.path.join(self.testDir, 'withoutProtPrep', pid) _ = build(smol, ff=ffs, outdir=tmpdir) refdir = home(dataDir=join('test-amber-build', 'nopp', pid)) TestAmberBuild._compareResultFolders(refdir, tmpdir, pid)
def _processSim(sim, projectionlist, uqmol, skip): pieces = sim.trajectory try: if uqmol is not None: mol = uqmol.copy() else: mol = Molecule(sim.molfile) logger.debug(pieces[0]) mol.read(pieces, skip=skip) #Gianni testing #_highfreqFilter(mol,10) data = [] for p in projectionlist: pj = _project(p, mol) if pj.ndim == 1: pj = np.atleast_2d(pj).T data.append(pj) data = np.hstack(data) if data.dtype == np.float64: data = data.astype(np.float32) except Exception as e: logger.warning('Error in simulation with id: ' + str(sim.simid) + '. "' + e.__str__() + '"') return None, None, None, True return data, _calcRef(pieces, mol.fileloc), mol.fstep, False
def test_equilibration(self): from htmd.home import home import filecmp from htmd.util import tempname from glob import glob from htmd.molecule.molecule import Molecule tmpdir = tempname() pdbid = '3PTB' builddir = home(dataDir=os.path.join('test-acemd', pdbid, 'build')) equil = Acemd('equilibration') mol = Molecule(os.path.join(builddir, 'structure.pdb')) celldim = mol.coords.max(axis=0) - mol.coords.min(axis=0) equil.celldimension = ' '.join( ['{:3.1f}'.format(val) for val in celldim.squeeze()]) equil.run = '2000' equil.trajectoryfreq = 200 equil.temperature = 300 equil.write(builddir, tmpdir) # Compare with reference refdir = home(dataDir=os.path.join('test-acemd', pdbid, 'equil')) files = [os.path.basename(f) for f in glob(os.path.join(refdir, '*'))] match, mismatch, error = filecmp.cmpfiles(refdir, tmpdir, files, shallow=False) if len(mismatch) != 0 or len(error) != 0 or len(match) != len(files): raise RuntimeError( 'Different results produced by Acemd equilibration for ' 'test {} between {} and {} in files {}.'.format( pdbid, refdir, tmpdir, mismatch))
def _createLipids(lipidratio, area, lipiddb, files, leaflet=None): lipiddb = lipiddb.to_dict(orient='index') lipidnames = list(lipidratio.keys()) ratiosAPL = np.array( [lipidratio[lipn] * lipiddb[lipn]['APL'] for lipn in lipidnames]) # Calculate the total areas per lipid type areaspl = area * (ratiosAPL / ratiosAPL.sum()) # Calculate the counts from the total areas counts = np.round(areaspl / np.array([lipiddb[lipn]['APL'] for lipn in lipidnames])).astype(int) lipids = [] for i in range(len(lipidnames)): resname = lipidnames[i] rings = _detectRings(Molecule(files[resname][0])) for k in range(counts[i]): if leaflet == 'upper': xyz = np.array( [np.nan, np.nan, lipiddb[resname]['Thickness'] / 2]) elif leaflet == 'lower': xyz = np.array( [np.nan, np.nan, -lipiddb[resname]['Thickness'] / 2]) lipids.append( _Lipid(resname=resname, headname=lipiddb[resname]['Head'], rings=rings, area=lipiddb[resname]['APL'], xyz=xyz)) return lipids
def calc_kinetics(self, source=None): """Calculates kinetics rates for the model Calculates all kinetics parameters starting from one source state to all other possible sinks Parameters ---------- source : int | optional Macrostate to be used as a source state. Returns ------- pandas.DataFrame Dataframe containing value for mfpton, mfptoff, kon, koff, kdeq and g0eq from a source macro to each other macrostate """ import pandas as pd import numpy as np from moleculekit.molecule import Molecule from htmd.kinetics import Kinetics columns = ['path', 'mfpton', 'mfptoff', 'kon', 'koff', 'kdeq', 'g0eq'] kin_summary = pd.DataFrame(columns=columns) #Will store strings of scientific notations (to be displayed in html) str_kin_summary = pd.DataFrame(columns=columns) if (self.bulk_split and not source): source = self.model.macronum - 1 if not self.concentration: try: from glob import glob gen_folder = glob(f"{self.input_folder}/generators/*/")[0] tmp_mol = Molecule(f"{gen_folder}/structure.pdb") tmp_mol.read(f"{gen_folder}/structure.psf") self.concentration = 55.55 / np.sum(tmp_mol.resname == "TIP3") / 3 except: self.concentration = 0 if self.concentration: for i in range(self.model.macronum): for j in range(self.model.macronum): kin = Kinetics(self.model, self.temperature, concentration=self.concentration, source=i, sink=j) kin_rates = kin.getRates() source = kin.source row = kin_rates.__dict__.copy() row['path'] = f'{source}-->{i}' kin_summary = kin_summary.append(row, ignore_index=True) #Creating values with string of scientific notation str_row = { col: "{:.2E}".format(row[col]) for col in columns if col is not 'path' } str_row['path'] = f'{i}-->{j}' str_kin_summary = str_kin_summary.append(str_row, ignore_index=True) str_kin_summary.to_json(f'{self.out_folder}/kin.json', orient='split', index=False) return kin_summary
def handle_model(self): """Creates a model is model is not set. Loads a model from a string. Or assign a model to self.model.out_folder Calling this function results in self.model to be and htmd.model.Model class """ from htmd.model import Model from htmd.molecule.molecule import Molecule if not self.model: from IDP_htmd.IDP_analysis import analyze_folder print("Creating new analysis") self.write_parameters() self.model = analyze_folder(self.input_folder, self.out_folder, self.skip, self.metrics, self.cluster, self.tica, self.ticadim, self.ticalag, self.modellag, self.modelunits, self.macronum, self.bulk_split, self.fes, self.rg_analysis, self.save_model, self.data_fstep) if isinstance(self.model, str): try: print("Loading model") model = Model() model.load(self.model) self.model = model except: print("Could not load the model") return if isinstance(self.model, Model): print("Model loaded") self.mol = Molecule(self.model.data.simlist[0].molfile)
def write(self, inputdir, outputdir): """ Write the equilibration protocol Writes the equilibration protocol and files into a folder for execution using files inside the inputdir directory Parameters ---------- inputdir : str Path to a directory containing the files produced by a build process. outputdir : str Directory where to write the equilibration setup files. Examples -------- >>> md = Equilibration() >>> md.write('./build','./equil') """ self._findFiles(inputdir) self._amberFixes() pdbfile = os.path.join(inputdir, self.acemd.coordinates) inmol = Molecule(pdbfile) self.acemd.TCL = self.acemd.TCL.replace('NUMSTEPS', str(self.numsteps)) self.acemd.TCL = self.acemd.TCL.replace('TEMPERATURE', str(self.temperature)) self.acemd.TCL = self.acemd.TCL.replace('KCONST', str(self.k)) self.acemd.TCL = self.acemd.TCL.replace('REFINDEX', ' '.join(map(str, inmol.get('index', self.reference)))) self.acemd.TCL = self.acemd.TCL.replace('SELINDEX', ' '.join(map(str, inmol.get('index', self.selection)))) self.acemd.TCL = self.acemd.TCL.replace('BOX', ' '.join(map(str, self.box))) if self.nvtsteps is None: self.acemd.TCL = self.acemd.TCL.replace('NVTSTEPS', str(500)) else: self.acemd.TCL = self.acemd.TCL.replace('NVTSTEPS', str(self.nvtsteps)) if self.constraintsteps is None: self.acemd.TCL = self.acemd.TCL.replace('CONSTRAINTSTEPS', str(self.numsteps / 2)) else: self.acemd.TCL = self.acemd.TCL.replace('CONSTRAINTSTEPS', str(self.constraintsteps)) if self.acemd.celldimension is None and self.acemd.extendedsystem is None: coords = inmol.get('coords', sel='water') if coords.size == 0: # It's a vacuum simulation coords = inmol.get('coords', sel='all') dim = np.max(coords, axis=0) - np.min(coords, axis=0) dim = dim + 12. else: dim = np.max(coords, axis=0) - np.min(coords, axis=0) self.acemd.celldimension = '{} {} {}'.format(dim[0], dim[1], dim[2]) if self.useconstantratio: self.acemd.useconstantratio = 'on' self.acemd.setup(inputdir, outputdir, overwrite=True) # Adding constraints inmol.set('occupancy', 0) inmol.set('beta', 0) for sel in self.constraints: inmol.set('beta', self.constraints[sel], sel) outfile = os.path.join(outputdir, self.acemd.coordinates) inmol.write(outfile)
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 _writeInputs(self, simsframes, epoch=None): if epoch is None: epoch = self._getEpoch() + 1 test = glob(path.join(self.inputpath, 'e' + str(epoch) + '*')) if len(test) != 0: raise NameError('Input dirs of epoch ' + str(epoch) + ' already exists.') if path.exists( path.join(self.inputpath, 'e' + str(epoch) + '_writeinputs.log')): raise NameError('Epoch logfile already exists. Cant overwrite it.') fid = open( path.join(self.inputpath, 'e' + str(epoch) + '_writeinputs.log'), 'w') regex = re.compile('(e\d+s\d+)_') for i, f in enumerate(simsframes): frameNum = f.frame piece = f.piece #print(frameNum) if f.sim.parent is None: currSim = f.sim else: currSim = f.sim.parent traj = currSim.trajectory[piece] if currSim.input is None: raise NameError( 'Could not find input folder in simulation lists. Cannot create new simulations.' ) wuName = _simName(traj) res = regex.search(wuName) if res: # If we are running on top of adaptive, use the first name part for the next sim name wuName = res.group(1) # create new job directory newName = 'e' + str(epoch) + 's' + str( i + 1) + '_' + wuName + 'p' + str(piece) + 'f' + str(frameNum) newDir = path.join(self.inputpath, newName, '') # copy previous input directory including input files copytree(currSim.input, newDir, symlinks=False, ignore=ignore_patterns('*.dcd', '*.xtc', '*.coor')) # overwrite input file with new one. frameNum + 1 as catdcd does 1 based indexing mol = Molecule() mol.read(traj) mol.frame = frameNum mol.write(path.join(newDir, 'input.coor')) # write nextInput file fid.write('# {0} \n{1} {2}\n'.format(newName, traj, frameNum)) fid.close()
def _writeInputsFunction(i, f, epoch, inputpath, coorname): regex = re.compile('(e\d+s\d+)_') frameNum = f.frame piece = f.piece if f.sim.parent is None: currSim = f.sim else: currSim = f.sim.parent traj = currSim.trajectory[piece] if currSim.input is None: raise NameError( 'Could not find input folder in simulation lists. Cannot create new simulations.' ) wuName = _simName(traj) res = regex.search(wuName) if res: # If we are running on top of adaptive, use the first name part for the next sim name wuName = res.group(1) # create new job directory newName = 'e' + str(epoch) + 's' + str(i + 1) + '_' + wuName + 'p' + str( piece) + 'f' + str(frameNum) newDir = path.join(inputpath, newName, '') # copy previous input directory including input files copytree(currSim.input, newDir, symlinks=False, ignore=ignore_patterns('*.coor', '*.rst', '*.out', *_IGNORE_EXTENSIONS)) # overwrite input file with new one. frameNum + 1 as catdcd does 1 based indexing from htmd.molecule.readers import _MDTRAJ_TRAJECTORY_EXTS import os if os.path.splitext(traj)[1].split( '.')[1].lower() in _MDTRAJ_TRAJECTORY_EXTS: # MDtraj trajectory. Unfortunately we need to read the topology to read the trajectory. mol = Molecule(currSim.molfile) else: mol = Molecule() mol.read(traj) mol.dropFrames( keep=frameNum) # Making sure only specific frame to write is kept mol.write(path.join(newDir, coorname))
def _prepare_points(self): """Load structures and compute the location of the points of the 3D image to be generated. """ protein = Molecule(str(self.protein.path)) ligand = Molecule(str(self.ligand.path)) protein.filter( 'not (water or name CO or name NI or name CU or name NA)') center = np.mean(ligand.get('coords'), axis=0) ligand.moveBy(-center) protein.moveBy(-center) center = np.mean(ligand.get('coords'), axis=0) self.protein.structure = protein self.ligand.structure = ligand self.points = grid_around(center, self.size, spacing=24 / (self.size - 1)).reshape( (-1, 3))
def __init__(self, sims, sel, simple): self._pc_sel = None self._sel = sel self._simple = simple (single, molfile) = _singleMolfile(sims) if single: mol = Molecule(molfile) self._pc_sel = mol.atomselect(sel)