def testUpdateDataFile(self): """Test case - update data file """ self.lfh.write("\nStarting %s %s\n" % (self.__class__.__name__, sys._getframe().f_code.co_name)) try: # Create a initial data file -- # myDataList=[] curContainer=DataContainer("myblock") aCat=DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append([9,2,3,4,5,6,7]) aCat.append([10,2,3,4,5,6,7]) aCat.append([11,2,3,4,5,6,7]) aCat.append([12,2,3,4,5,6,7]) #self.lfh.write("Assigned data category state-----------------\n") #aCat.dumpIt(fh=self.lfh) tmpf = tempname(suffix='.cif') curContainer.append(aCat) myDataList.append(curContainer) ofh = open(tmpf, "w") pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() # # # Read and update the data - # myDataList=[] ifh = open(tmpf, "r") pRd=PdbxReader(ifh) pRd.read(myDataList) ifh.close() # myBlock=myDataList[0] myBlock.printIt() myCat=myBlock.getObj('pdbx_seqtool_mapping_ref') myCat.printIt() for iRow in range(0,myCat.getRowCount()): myCat.setValue('some value', 'ref_mon_id',iRow) myCat.setValue(100, 'ref_mon_num',iRow) ofh = open(tempname(suffix='.cif'), "w") pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() # except: traceback.print_exc(file=self.lfh) self.fail()
def MDTRAJwrite(mol, filename): try: import mdtraj as md from htmd.util import tempname ext = os.path.splitext(filename)[1][1:] if ext == 'gz': pieces = filename.split('.') ext = '{}.{}'.format(pieces[-2], pieces[-1]) if ext in _MDTRAJ_TOPOLOGY_SAVERS: tmppdb = tempname(suffix='.pdb') mol.write(tmppdb) traj = md.load(tmppdb) os.remove(tmppdb) elif ext in _MDTRAJ_TRAJECTORY_SAVERS: tmppdb = tempname(suffix='.pdb') tmpxtc = tempname(suffix='.xtc') mol.write(tmppdb) mol.write(tmpxtc) traj = md.load(tmpxtc, top=tmppdb) os.remove(tmppdb) os.remove(tmpxtc) else: raise ValueError('Unknown file type for file {}'.format(filename)) # traj.xyz = np.swapaxes(np.swapaxes(self.coords, 1, 2), 0, 1) / 10 # traj.time = self.time # traj.unitcell_lengths = self.box.T / 10 traj.save(filename) except Exception as e: raise ValueError('MDtraj reader failed for file {} with error "{}"'.format(filename, e))
def MDTRAJwrite(mol, filename): try: import mdtraj as md from htmd.util import tempname ext = os.path.splitext(filename)[1][1:] if ext == 'gz': pieces = filename.split('.') ext = '{}.{}'.format(pieces[-2], pieces[-1]) if ext in _MDTRAJ_TOPOLOGY_SAVERS: tmppdb = tempname(suffix='.pdb') mol.write(tmppdb) traj = md.load(tmppdb) os.remove(tmppdb) elif ext in _MDTRAJ_TRAJECTORY_SAVERS: tmppdb = tempname(suffix='.pdb') tmpxtc = tempname(suffix='.xtc') mol.write(tmppdb) mol.write(tmpxtc) traj = md.load(tmpxtc, top=tmppdb) os.remove(tmppdb) os.remove(tmpxtc) else: raise ValueError('Unknown file type for file {}'.format(filename)) # traj.xyz = np.swapaxes(np.swapaxes(self.coords, 1, 2), 0, 1) / 10 # traj.time = self.time # traj.unitcell_lengths = self.box.T / 10 traj.save(filename) except Exception as e: raise ValueError( 'MDtraj reader failed for file {} with error "{}"'.format( filename, e))
def testUpdateDataFile(self): """Test case - write data file """ self.lfh.write("\nStarting %s %s\n" % (self.__class__.__name__, sys._getframe().f_code.co_name)) try: # Create a initial data file -- # tmpf = tempname(suffix='.cif') myDataList=[] ofh = open(tmpf, "w") curContainer=DataContainer("myblock") aCat=DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) curContainer.append(aCat) myDataList.append(curContainer) pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() # # Read and update the data - # myDataList=[] ifh = open(tmpf, "r") pRd=PdbxReader(ifh) pRd.read(myDataList) ifh.close() # myBlock=myDataList[0] myBlock.printIt() myCat=myBlock.getObj('pdbx_seqtool_mapping_ref') myCat.printIt() for iRow in range(0,myCat.getRowCount()): myCat.setValue('some value', 'ref_mon_id',iRow) myCat.setValue(100, 'ref_mon_num',iRow) ofh = open(tempname(suffix='.cif'), "w") pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() except: traceback.print_exc(file=sys.stderr) self.fail()
def test_customDisulfideBonds(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 = '1GZM' 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'] disu = [['segid 1 and resid 110', 'segid 1 and resid 187'], ['segid 0 and resid 110', 'segid 0 and resid 187']] tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir, disulfide=disu) compareDir = home(dataDir=os.path.join('test-charmm-build', pdb)) assertSameAsReferenceDir(compareDir, tmpdir) # TODO: Remove this after deprecation from htmd.builder.builder import DisulfideBridge np.random.seed(1) # Needed for ions disu = [ DisulfideBridge('1', 110, '1', 187), DisulfideBridge('0', 110, '0', 187) ] tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir, disulfide=disu) compareDir = home(dataDir=os.path.join('test-charmm-build', pdb)) assertSameAsReferenceDir(compareDir, tmpdir)
def test_h2o2_dihed_fix(self): refDir = os.path.join(self.dataDir, 'h2o2_dihed_fix') self._test( refDir, tempname(), 'parameterize -m input.mol2 -f GAFF2 --no-min --no-esp --no-geomopt' )
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 test_acemd3(self): from htmd.util import tempname from htmd.home import home from glob import glob import os pd = Production() pd.runtime = 4 pd.timeunits = "ns" pd.temperature = 300 pd.useconstraints = True pd.constraints = { "protein and name CA": 1, "protein and noh and not name CA": 0.1, } pd.fb_reference = "protein and name CA" pd.fb_selection = "resname MOL and noh" pd.fb_box = [-21, 21, -19, 19, 29, 30] pd.fb_k = 5 tmpdir = tempname() pd.write( home(dataDir=os.path.join("test-protocols", "equilibration", "acemd3", "protLig", "postrun")), tmpdir, ) # Compare with reference refdir = home(dataDir=os.path.join("test-protocols", "production", "acemd3", "protLig", "prerun")) files = [os.path.basename(f) for f in glob(os.path.join(refdir, "*"))] self._compareResultFolders(refdir, tmpdir, "protLig")
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_acemd3(self): from htmd.util import tempname from htmd.home import home from glob import glob import os eq = Equilibration(_version=3) eq.runtime = 4 eq.timeunits = 'ns' eq.temperature = 300 # keep protein on the same place during all equilibration from htmd.units import convert eq.constraintsteps = convert(eq.timeunits, 'timesteps', eq.runtime, timestep=eq.acemd.timestep) eq.constraints = { 'protein and name CA': 1, 'protein and noh and not name CA': 0.1 } eq.fb_reference = 'protein and name CA' eq.fb_selection = 'resname MOL and noh' eq.fb_box = [-21, 21, -19, 19, 29, 30] eq.fb_k = 5 tmpdir = tempname() eq.write( home(dataDir=os.path.join('test-protocols', 'build', 'protLig')), tmpdir) # Compare with reference refdir = home(dataDir=os.path.join('test-protocols', 'equilibration', 'acemd3', 'protLig', 'prerun')) files = [os.path.basename(f) for f in glob(os.path.join(refdir, '*'))] self._compareResultFolders(refdir, tmpdir, 'protLig')
def setUp(self): if os.environ.get('TRAVIS_OS_NAME') == 'osx': self.skipTest('Mac does not work!') self.maxDiff = None self.dataDir = home(dataDir='test-param') self.testDir = os.environ.get('TESTDIR', tempname())
def drawIsoSurface(values3d, resolution=1., plot_center=None, viewer=None): from htmd.vmdviewer import getCurrentViewer from htmd.molecule.util import writeVoxels # plot_center should be - molecule.get_center() + 12 if len(values3d.shape) != 3: raise ValueError("Your provided a box of {} dimensions." "\nThis only works with dimension of 3".format( len(values3d.shape))) from htmd.util import tempname if viewer is None: viewer = getCurrentViewer() mincoor = np.zeros(3, dtype=np.float64) maxcoor = np.array(values3d.shape, dtype=np.float64) rescoor = np.array([resolution] * 3) # Adjust the plotting center if plot_center is None: plot_center = maxcoor / 2. else: plot_center = np.array(plot_center) mincoor -= (plot_center + 0.5 ) # TODO: Fix so it will work in case resolution != 1. maxcoor -= (plot_center + 0.5) outf = tempname(suffix='.cube') writeVoxels(values3d, outf, mincoor, maxcoor, rescoor) viewer.send( 'mol new {} type cube first 0 last -1 step 1 waitfor 1 volsets {{0 }}'. format(outf)) viewer.send('mol modstyle 0 top Isosurface 0.75 0 2 0 1 1')
def test_customDisulfideBonds(self): from moleculekit.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 = "1GZM" 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.prm", "par/par_water_ions.prm"] disu = [ ["segid 1 and resid 110", "segid 1 and resid 187"], ["segid 0 and resid 110", "segid 0 and resid 187"], ] tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir, disulfide=disu) compareDir = home(dataDir=os.path.join("test-charmm-build", pdb)) assertSameAsReferenceDir(compareDir, tmpdir)
def test_build(self): from moleculekit.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.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 drawIsoSurface(values3d, resolution=1., plot_center=None, viewer=None): from htmd.vmdviewer import getCurrentViewer from htmd.molecule.util import writeVoxels # plot_center should be - molecule.get_center() + 12 if len(values3d.shape) != 3: raise ValueError("Your provided a box of {} dimensions." "\nThis only works with dimension of 3".format(len(values3d.shape))) from htmd.util import tempname if viewer is None: viewer = getCurrentViewer() mincoor = np.zeros(3, dtype=np.float64) maxcoor = np.array(values3d.shape, dtype=np.float64) rescoor = np.array([resolution] * 3) # Adjust the plotting center if plot_center is None: plot_center = maxcoor / 2. else: plot_center = np.array(plot_center) mincoor -= (plot_center + 0.5) # TODO: Fix so it will work in case resolution != 1. maxcoor -= (plot_center + 0.5) outf = tempname(suffix='.cube') writeVoxels(values3d, outf, mincoor, maxcoor, rescoor) viewer.send('mol new {} type cube first 0 last -1 step 1 waitfor 1 volsets {{0 }}'.format(outf)) viewer.send('mol modstyle 0 top Isosurface 0.75 0 2 0 1 1')
def test_run_water(self): from htmd.util import tempname from htmd.home import home from glob import glob from subprocess import check_output import subprocess import shutil import os acemd3exe = shutil.which('acemd3', mode=os.X_OK) if not acemd3exe: raise NameError('Could not find acemd3, or no execute permissions are given') for system in ['amber-equil-completed', 'charmm-equil-completed']: pd = Production(_version=3) pd.runtime = 5 pd.timeunits = 'steps' pd.temperature = 300 pd.constraints = {} # Set these down for tiny box size of water pd.acemd.cutoff = 3 pd.acemd.switchdistance = 2 ###### tmpdir = tempname() pd.write(home(dataDir=os.path.join('test-acemd', 'tiny-water', system)), tmpdir) try: res = check_output(['acemd3', '--platform', 'CPU', os.getenv('ACE3ARG')], cwd=tmpdir) except subprocess.CalledProcessError as exc: assert False, f'Failed to run due to error: {exc}\n\n ---> Error log:\n\n{exc.output.decode("ascii")}' res = res.decode('utf-8').strip() print(res) assert res.endswith('Completed simulation!'), 'Failed at system ' + system
def test_benzamidine_cgenff(self): refDir = os.path.join(self.dataDir, 'benzamidine_cgenff') self._test( refDir, tempname(), 'parameterize -m input.mol2 --charge 1 --forcefield CGENFF --no-min --no-esp --no-torsions' )
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 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_h2o2_outdir(self): refDir = os.path.join(self.dataDir, 'h2o2_outdir') self._test( refDir, tempname(), 'parameterize -m input.mol2 -f GAFF2 --no-min --no-esp --no-torsions -o dir' )
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 __init__(self, arr, vecMin, vecRes, color=8, isovalue=0.5): """ Displays an isosurface in VMD The function returns an instance of VMDGraphicsObject. To delete it, use the delete() method. Parameters ---------- arr: np.ndarray 3D array with volumetric data. vecMin: np.ndarray 3D vector denoting the minimal corner of the grid vecRes: np.ndarray 3D vector denoting the resolution of the grid in each dimension color: str The color to be used for the isosurface """ super().__init__(arr) from htmd.util import tempname import numpy as np import os filename = tempname(suffix='.cube') outFile = open(filename, 'w') # conversion to gaussian units L = 1 / 0.52917725 gauss_bin = vecRes * L # minCorner = 0.5*L*(vecMin - vecMax + vecRes) minCorner = L * (vecMin + 0.5 * vecRes) ngrid = arr.shape # write header outFile.write("CUBE FILE\n") outFile.write("OUTER LOOP: X, MIDDLE LOOP: Y, INNER LOOP: Z\n") outFile.write("%5d %12.6f %12.6f %12.6f\n" % (1, minCorner[0], minCorner[1], minCorner[2])) outFile.write("%5d %12.6f %12.6f %12.6f\n" % (ngrid[0], gauss_bin[0], 0, 0)) outFile.write("%5d %12.6f %12.6f %12.6f\n" % (ngrid[1], 0, gauss_bin[1], 0)) outFile.write("%5d %12.6f %12.6f %12.6f\n" % (ngrid[2], 0, 0, gauss_bin[2])) outFile.write("%5d %12.6f %12.6f %12.6f %12.6f\n" % (1, 0, minCorner[0], minCorner[1], minCorner[2])) # main loop cont = 0 for i in range(ngrid[0]): for j in range(ngrid[1]): for k in range(ngrid[2]): outFile.write("%13.5g" % arr[i][j][k]) if np.mod(cont, 6) == 5: outFile.write("\n") cont += 1 outFile.close() vmd = getCurrentViewer() vmd.send('mol new {} type cube first 0 last -1 step 1 waitfor 1 volsets {{0 }}'.format(filename)) vmd.send('mol modstyle top top Isosurface {} 0 2 0 2 1'.format(isovalue)) vmd.send('mol modcolor top top ColorID {}'.format(color)) vmd.send('set htmd_graphics_mol({:d}) [molinfo top]'.format(self.n)) os.unlink(filename)
def test_acemd3(self): from htmd.util import tempname from htmd.home import home from htmd.units import convert from glob import glob import os from htmd.mdengine.acemd.acemd import GroupRestraint eq = Equilibration() eq.runtime = 4 eq.timeunits = "ns" eq.temperature = 300 eq.restraintsteps = convert( eq.timeunits, "timesteps", eq.runtime, timestep=eq.acemd.timestep ) ligres = GroupRestraint( "resname MOL and noh", [42, 38, 1], [(5, 0)], fbcentre=[-0.178, -0.178, 29.195], ) eq.restraints = eq.defaultEquilRestraints(1000000) + [ligres] tmpdir = tempname() eq.write( home(dataDir=os.path.join("test-protocols", "build", "protLig")), tmpdir ) # Compare with reference refdir = home( dataDir=os.path.join( "test-protocols", "equilibration", "acemd3", "protLig", "prerun" ) ) files = [os.path.basename(f) for f in glob(os.path.join(refdir, "*"))] self._compareResultFolders(refdir, tmpdir, "protLig")
def testWriteDataFile(self): """Test case - write data file """ self.lfh.write("\nStarting %s %s\n" % (self.__class__.__name__, sys._getframe().f_code.co_name)) try: # myDataList=[] ofh = open(tempname(suffix='.cif'), "w") curContainer=DataContainer("myblock") aCat=DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) curContainer.append(aCat) myDataList.append(curContainer) pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() except: traceback.print_exc(file=sys.stderr) self.fail()
def test_production(self): from htmd.home import home import filecmp from htmd.util import tempname from glob import glob tmpdir = tempname() pdbid = '3PTB' prod = Acemd('production') prod.run = '2000' prod.trajectoryfreq = 200 prod.temperature = 300 prod.write( home(dataDir=os.path.join('test-acemd', pdbid, 'equil_out')), tmpdir) print(tmpdir) # Compare with reference refdir = home(dataDir=os.path.join('test-acemd', pdbid, 'prod')) 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 production for ' 'test {} between {} and {} in files {}.'.format( pdbid, refdir, tmpdir, mismatch))
def test_equilibration(self): from htmd.home import home import filecmp from htmd.util import tempname from glob import glob from moleculekit.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.boxsize = " ".join( ["{:3.1f}".format(val) for val in celldim.squeeze()]) equil.run = "2000" equil.trajectoryperiod = 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 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 test_production(self): from htmd.home import home import filecmp from htmd.util import tempname from glob import glob tmpdir = tempname() pdbid = "3PTB" prod = Acemd("production") prod.run = "2000" prod.trajectoryperiod = 200 prod.temperature = 300 prod.write( home(dataDir=os.path.join("test-acemd", pdbid, "equil_out")), tmpdir) print(tmpdir) # Compare with reference refdir = home(dataDir=os.path.join("test-acemd", pdbid, "prod")) 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 production for " "test {} between {} and {} in files {}.".format( pdbid, refdir, tmpdir, mismatch))
def test_h2o2_dihed_opt_restart(self): refDir = os.path.join(self.dataDir, 'h2o2_dihed_opt_restart') resDir = tempname() shutil.copytree(os.path.join(refDir, 'dihedral-opt'), os.path.join(resDir, 'dihedral-opt')) self._test(refDir, resDir, 'parameterize -m input.mol2 -f GAFF2 --no-min --no-esp')
def test_h2o2_esp_restart(self): refDir = os.path.join(self.dataDir, 'h2o2_esp_restart') resDir = tempname() shutil.copytree(os.path.join(refDir, 'esp'), os.path.join(resDir, 'esp')) self._test( refDir, resDir, 'parameterize -m input.mol2 -f GAFF2 --no-min --no-torsions')
def setUpClass(self): from htmd.util import tempname from htmd.home import home from moleculekit.projections.metricdistance import MetricDistance import shutil import os tmpdir = tempname() shutil.copytree(home(dataDir='adaptive'), tmpdir) os.chdir(tmpdir)
def test_glycol_dihed_fix_restart_2(self): refDir = os.path.join(self.dataDir, 'glycol_dihed_fix') resDir = tempname() shutil.copytree(os.path.join(refDir, 'dihedral-single-point'), os.path.join(resDir, 'dihedral-single-point')) self._test( refDir, resDir, 'parameterize input.mol2 -d O1-C1-C2-O2 C1-C2-O2-H6 --no-min --no-esp --no-dihed-opt' )
def test_benzamidine_rtf_prm(self): refDir = os.path.join(self.dataDir, 'benzamidine_rtf_prm') resDir = tempname() os.makedirs(resDir) shutil.copy(os.path.join(refDir, 'input.rtf'), resDir) shutil.copy(os.path.join(refDir, 'input.prm'), resDir) self._test( refDir, resDir, 'parameterize -m input.mol2 --charge 1 --forcefield CGENFF --rtf input.rtf --prm input.prm --no-min ' '--no-esp --no-torsions')
def setUp(self): if os.environ.get('TRAVIS_OS_NAME') == 'osx': self.skipTest('Mac does not work!') self.maxDiff = None self.dataDir = home(dataDir='test-param') if os.environ.get('TESTDIR') is not None: self.testDir = os.environ['TESTDIR'] else: self.testDir = tempname() print('Test dir: {}'.format(self.testDir))
def test_benzamidine_esp_freeze_restart(self): refDir = os.path.join(self.dataDir, 'benzamidine_esp_freeze_restart') resDir = tempname() shutil.copytree(os.path.join(refDir, 'minimize'), os.path.join(resDir, 'minimize')) shutil.copytree(os.path.join(refDir, 'esp'), os.path.join(resDir, 'esp')) self._test( refDir, resDir, 'parameterize -m input.mol2 --charge 1 --basis 6-31g-star --no-torsions --freeze-charge N2' )
def fixParameters(parameterfile): from htmd.util import tempname tmpfile = tempname(suffix='.prm') with open(parameterfile, 'r') as f: lines = f.readlines() with open(tmpfile, 'w') as f: for l in lines: l = l.replace('!MASS', 'MASS') l = l.replace('!ATOMS', 'ATOMS') f.write(l) return tmpfile
def test_benzamidine_full_restart(self): refDir = os.path.join(self.dataDir, 'benzamidine_full_restart') resDir = tempname() shutil.copytree(os.path.join(refDir, 'minimize'), os.path.join(resDir, 'minimize')) shutil.copytree(os.path.join(refDir, 'esp'), os.path.join(resDir, 'esp')) shutil.copytree(os.path.join(refDir, 'dihedral-opt'), os.path.join(resDir, 'dihedral-opt')) self._test(refDir, resDir, 'parameterize -m input.mol2 --charge 1 --basis 6-31g-star')
def testSimpleInitialization(self): """Test case - Simple initialization of a data category and data block """ self.lfh.write("\nStarting %s %s\n" % (self.__class__.__name__, sys._getframe().f_code.co_name)) try: # fn = tempname(suffix='.cif') attributeNameList=['aOne','aTwo','aThree','aFour','aFive','aSix','aSeven','aEight','aNine','aTen'] rowList=[[1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10] ] nameCat='myCategory' # # curContainer=DataContainer("myblock") aCat=DataCategory(nameCat,attributeNameList,rowList) aCat.printIt() curContainer.append(aCat) curContainer.printIt() # myContainerList=[] myContainerList.append(curContainer) ofh = open(fn, "w") pdbxW=PdbxWriter(ofh) pdbxW.write(myContainerList) ofh.close() myContainerList=[] ifh = open(fn, "r") pRd=PdbxReader(ifh) pRd.read(myContainerList) ifh.close() for container in myContainerList: for objName in container.getObjNameList(): name,aList,rList=container.getObj(objName).get() self.lfh.write("Recovered data category %s\n" % name) self.lfh.write("Attribute list %r\n" % repr(aList)) self.lfh.write("Row list %r\n" % repr(rList)) except: traceback.print_exc(file=self.lfh) self.fail()
def test_customDisulfideBonds(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 = '1GZM' 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'] disu = [['segid 1 and resid 110', 'segid 1 and resid 187'], ['segid 0 and resid 110', 'segid 0 and resid 187']] tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir, disulfide=disu) compareDir = home(dataDir=os.path.join('test-charmm-build', pdb)) assertSameAsReferenceDir(compareDir, tmpdir) # TODO: Remove this after deprecation from htmd.builder.builder import DisulfideBridge np.random.seed(1) # Needed for ions disu = [DisulfideBridge('1', 110, '1', 187), DisulfideBridge('0', 110, '0', 187)] tmpdir = tempname() _ = build(smol, topo=topos, param=params, outdir=tmpdir, disulfide=disu) compareDir = home(dataDir=os.path.join('test-charmm-build', pdb)) assertSameAsReferenceDir(compareDir, tmpdir)
def test_production(self): from htmd.home import home import filecmp from htmd.util import tempname from glob import glob tmpdir = tempname() pdbid = '3PTB' prod = Acemd('production') prod.run = '2000' prod.trajectoryfreq = 200 prod.temperature = 300 prod.write(home(dataDir=os.path.join('test-acemd', pdbid, 'equil_out')), tmpdir) print(tmpdir) # Compare with reference refdir = home(dataDir=os.path.join('test-acemd', pdbid, 'prod')) 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 production for ' 'test {} between {} and {} in files {}.'.format(pdbid, refdir, tmpdir, mismatch))
def test_glycol_dihed_fix_restart_2(self): refDir = os.path.join(self.dataDir, 'glycol_dihed_fix') resDir = tempname() shutil.copytree(os.path.join(refDir, 'dihedral-single-point'), os.path.join(resDir, 'dihedral-single-point')) self._test(refDir, resDir, 'parameterize input.mol2 -d O1-C1-C2-O2 C1-C2-O2-H6 --no-min --no-esp --no-dihed-opt')
for i in range(len(St)): mst[i] = macro_ofcluster[St[i]] return mst '''def _macroP(C, macro_ofmicro): macronum = np.max(macro_ofmicro) + 1 macroC = np.zeros((macronum, macronum)) for m1 in range(macronum): sourcemicros = np.where(macro_ofmicro == m1)[0] for m2 in range(macronum): sinkmicros = np.where(macro_ofmicro == m2)[0] ixgrid = np.ix_(sourcemicros, sinkmicros) macroC[m1, m2] = np.sum(C[ixgrid].flatten()) from msmtools.estimation import transition_matrix return transition_matrix(macroC, reversible=True)''' if __name__ == '__main__': from htmd.util import tempname from htmd.home import home from os.path import join testfolder = home(dataDir='model') model = Model(file=join(testfolder, 'model.dat')) tmpsave = tempname(suffix='.dat') model.save(tmpsave)
def _prepareStream(filename): from htmd.util import tempname tmpout = tempname() os.makedirs(tmpout) return split(filename, tmpout)
def setUp(self): from htmd.util import tempname self.testDir = os.environ.get('TESTDIR', tempname()) print('Running tests in {}'.format(self.testDir))
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) if __name__ == "__main__": import htmd.home from htmd.util import tempname md = Production() md.temperature = 300 md.fb_reference = 'protein and name CA' md.fb_selection = 'segname L and noh' md.acemd.extendedsystem = None # use different data md.acemd.binindex = None # use different data md.fb_box = [-20, 20, -20, 20, 43, 45] md.fb_k = 5 md.write(htmd.home.home() +'/data/equilibrate', tempname()) md.fb_k = 0 md.write(htmd.home.home() +'/data/equilibrate', tempname())
def dock(protein, ligand, center=None, extent=None, numposes=20, babelexe='babel', vinaexe=None): """ Molecular docking, using Vina If centre and extent are not provided, docking will be performed over the whole protein Parameters ---------- protein : :class:`Molecule <htmd.molecule.molecule.Molecule>` object Molecule object representing the receptor ligand : :class:`Molecule <htmd.molecule.molecule.Molecule>` object Molecule object representing the ligand to dock center : list 3-vec centre of of the search bounding box (optional) extent : list 3-vec linear extent of the search bounding box (optional) numposes : int Number of poses to return. Vina cannot return more than 20 poses. babelexe : str Path to babel executable. vinaexe : str Path to AutoDock Vina executable. Returns ------- poses Molecule object representing the N<10 best poses scores 3x num_poses matrix containing kcal, rmsd lb, rmsd ub Examples -------- >>> poses, scoring = dock(protein, ligand) >>> poses, scoring = dock(protein, ligand, center=[ 10., 5., 12. ], extent=[ 15., 15., 15. ] ) """ if np.size(protein.coords, 2) != 1 or np.size(ligand.coords, 2) != 1: raise NameError('Protein and ligand Molecules should be single frames') buffer = 10. # Angstrom buffer around protein for whole-protein docking c_min = np.min(protein.coords, 0).reshape((1, 3))[0] c_max = np.max(protein.coords, 0).reshape((1, 3))[0] if center is None: center = (buffer + (c_max + c_min)) / 2 if extent is None: extent = (c_max - c_min) + buffer # babel -i pdb protein.pdb -o pdbqt protein.pdbqt -xr # babel -i pdb ligand.pdb -o pdbqt ligand.pdbqt -xhn # vina --ligand ligand.pdbqt --receptor protein.pdbqt --center_x 0. --center_y 0. --center_z 0. --size_x 60. --size_y 60. --size_z 60 --exhaustiveness 10 # babel -m -i pdbqt ligand_out.pdbqt -o pdb out_.pdb -xhn protein_pdb = tempname(suffix=".pdb") ligand_pdb = tempname(suffix=".pdb") output_pdb = tempname(suffix="_.pdb") output_prefix = path.splitext(output_pdb)[0] protein_pdbqt = tempname(suffix=".pdbqt") ligand_pdbqt = tempname(suffix=".pdbqt") output_pdbqt = tempname(suffix=".pdbqt") protein.write(protein_pdb) ligand.write(ligand_pdb) try: if vinaexe is None: import platform suffix = '' if platform.system() == "Windows": suffix = '.exe' vinaexe = '{}-vina{}'.format(platform.system(), suffix) vinaexe = shutil.which(vinaexe, mode=os.X_OK) if not vinaexe: raise NameError('Could not find vina, or no execute permissions are given') except: raise NameError('Could not find vina, or no execute permissions are given') try: babelexe = shutil.which(babelexe, mode=os.X_OK) if babelexe is None: raise NameError('Could not find babel, or no execute permissions are given') except: raise NameError('Could not find babel, or no execute permissions are given') call([babelexe, '-i', 'pdb', protein_pdb, '-o', 'pdbqt', protein_pdbqt, '-xr']) call([babelexe, '-i', 'pdb', ligand_pdb, '-o', 'pdbqt', ligand_pdbqt, '-xhn']) if not path.isfile(ligand_pdbqt): raise NameError('Ligand could not be converted to PDBQT') if not path.isfile(protein_pdbqt): raise NameError('Protein could not be converted to PDBQT') call([vinaexe, '--receptor', protein_pdbqt, '--ligand', ligand_pdbqt, '--out', output_pdbqt, '--center_x', str(center[0]), '--center_y', str(center[1]), '--center_z', str(center[2]), '--size_x', str(extent[0]), '--size_y', str(extent[1]), '--size_z', str(extent[2]), '--num_modes', str(numposes)]) call([babelexe, '-m', '-i', 'pdbqt', output_pdbqt, '-o', 'pdb', output_pdb, '-xhn']) scoring = [] coords = [] outfiles = natsorted(glob('{}*.pdb'.format(output_prefix))) for outf in outfiles: # First get the scoring scoring.append(_parseScoring(outf)) next_pose = Molecule(outf) os.remove(outf) c = next_pose.coords co = c.copy() natoms = len(ligand.name) # Order atoms back to original order for idx_i in range(natoms): for idx_j in range(natoms): if ligand.name[idx_i] == next_pose.name[idx_j]: co[idx_i, :, :] = c[idx_j, :, :] coords.append(co) poses = [] for i, c in enumerate(coords): l = ligand.copy() l.viewname = 'Pose {}'.format(i) l.coords = c poses.append(l) os.remove(protein_pdb) os.remove(ligand_pdb) os.remove(protein_pdbqt) os.remove(ligand_pdbqt) os.remove(output_pdbqt) return poses, np.array(scoring)
def loopModeller( mol, segid, seq, startresid, movstart=None, movend=None, modellerexe="/shared/sdoerr/Software/modeller-9.16/build/bin/mod9.16", ): """ Uses the Modeller software to predict missing loops in a Molecule. Parameters ---------- mol : :class:`Molecule <htmd.molecule.molecule.Molecule>` object A Molecule object. segid : str The name of the segment containing the gap. seq : str The sequence of residues to be added by the loop modeller. startresid : int The resid of the residue before the gap. movstart : int The resid after which the residues will be allowed to move in modeller. movend : int The resid before which the residues will be allowed to move in modeller. modellerexe : str The path to the modeller executable. Returns ------- newmol : :class:`Molecule <htmd.molecule.molecule.Molecule>` object A new Molecule object containing the protein with the modelled loop. Examples -------- >>> newmol = loopModeller(mol, 'B', 'MLSGSK', 263) """ if movstart is None: movstart = startresid if movend is None: movend = movstart + len(seq) + 1 segatm = mol.atomselect("segid {}".format(segid)) segres = np.unique(mol.get("resid", sel=segatm)) chain = np.unique(mol.get("chain", sel=segatm)) if len(chain) != 1: raise RuntimeError("More than one chain detected in segment {}".format(segid)) pos = np.where(segres == startresid)[0][0] if (segres[pos + 1] - segres[pos]) != (len(seq) + 1): raise RuntimeError( "Sequence is {} characters long while sequence gap ({}-{}) is {} long. Cannot insert loop.".format( len(seq), segres[pos], segres[pos + 1], (segres[pos + 1] - segres[pos]) ) ) chain = chain[0] segresidstart = np.min(segres) segresidend = np.max(segres) currseq = mol.sequence()[segid] minuses = ["-"] * len(seq) minuses = "".join(minuses) inspos = np.where(segres == startresid)[0] + 1 gapseq = currseq[:inspos] + minuses + currseq[inspos:] + "*" fullseq = currseq[:inspos] + seq + currseq[inspos:] + "*" # Get the sequence of the 1qg8 PDB file, and write to an alignment file pdbfile = tempname(suffix=".pdb") mol.write(pdbfile) alifile = tempname(suffix=".ali") f = open(alifile, "w") f.write(">P1;prot\nstructure:{}:{}:{}:{}:{}::::\n".format(pdbfile, segresidstart, chain, segresidend, chain)) f.write(gapseq) f.write("\n>P1;prot_fill\nsequence:::::::::\n") f.write(fullseq) f.close() script = """ from modeller import * from modeller.automodel import * # Load the automodel class log.verbose() env = environ() class MyModel(automodel): def select_atoms(self): return selection(self.residue_range('{}', '{}')) a = MyModel(env, alnfile = '{}', knowns = 'prot', sequence = 'prot_fill') a.starting_model= 1 a.ending_model = 1 a.make() """.format( movstart + 1 - segresidstart, movend - segresidstart, alifile ) pyfile = tempname(suffix=".py") f = open(pyfile, "w") f.write(script) f.close() print(pdbfile) print(alifile) print(pyfile) call("{} {}".format(modellerexe, pyfile), shell=True) newmol = Molecule("./prot_fill.B99990001.pdb") return newmol
bind = {} for i, elt in enumerate(list(set(b))): bind[elt] = i return np.array([bind.get(itm, -1) for itm in a]) # None can be replaced by any other "not in b" value if __name__ == '__main__': from htmd import * from htmd.util import tempname from htmd.home import home from os.path import join testfolder = home(dataDir='adaptive') sims = simlist(glob(join(testfolder, 'data', '*', '')), glob(join(testfolder, 'input', '*', 'structure.pdb'))) fsims = simfilter(sims, tempname(), 'not water') metr = Metric(fsims) metr.set(MetricDistance('protein and resid 10 and name CA', 'resname BEN and noh', metric='contacts', groupsel1='residue', threshold=4)) data1 = metr.project() metr.set(MetricDihedral()) data2 = metr.project() # Testing combining of metrics data1.combine(data2) # Testing dimensions assert np.array_equal(data1.map.shape, (897, 3)), 'combine not working correct' assert np.array_equal(data1.dat[0].shape, (6, 897)), 'combine not working correct' assert np.array_equal(np.where(data1.map.type == 'contact')[0], [0, 1, 2, 3, 4, 5, 6, 7, 8]), 'combine not working correct'
def dock(protein, ligand, center=None, extent=None, numposes=20, babelexe='obabel', vinaexe=None): """ Molecular docking, using Vina If centre and extent are not provided, docking will be performed over the whole protein Parameters ---------- protein : :class:`Molecule <htmd.molecule.molecule.Molecule>` object Molecule object representing the receptor ligand : :class:`Molecule <htmd.molecule.molecule.Molecule>` object Molecule object representing the ligand to dock center : list 3-vec centre of of the search bounding box (optional) extent : list 3-vec linear extent of the search bounding box (optional) numposes : int Number of poses to return. Vina cannot return more than 20 poses. babelexe : str Path to babel executable. vinaexe : str Path to AutoDock Vina executable. Returns ------- poses Molecule object representing the N<10 best poses scores 3x num_poses matrix containing kcal, rmsd lb, rmsd ub Examples -------- >>> poses, scoring = dock(protein, ligand) >>> poses, scoring = dock(protein, ligand, center=[ 10., 5., 12. ], extent=[ 15., 15., 15. ] ) """ if np.size(protein.coords, 2) != 1 or np.size(ligand.coords, 2) != 1: raise NameError('Protein and ligand Molecules should be single frames') buffer = 10. # Angstrom buffer around protein for whole-protein docking c_min = np.min(protein.coords, 0).reshape((1, 3))[0] c_max = np.max(protein.coords, 0).reshape((1, 3))[0] if center is None: center = (buffer + (c_max + c_min)) / 2 if extent is None: extent = (c_max - c_min) + buffer # babel -i pdb protein.pdb -o pdbqt protein.pdbqt -xr # babel -i pdb ligand.pdb -o pdbqt ligand.pdbqt -xhn # vina --ligand ligand.pdbqt --receptor protein.pdbqt --center_x 0. --center_y 0. --center_z 0. --size_x 60. --size_y 60. --size_z 60 --exhaustiveness 10 # babel -m -i pdbqt ligand_out.pdbqt -o pdb out_.pdb -xhn protein_pdb = tempname(suffix=".pdb") ligand_mol2 = tempname(suffix=".mol2") output_pdb = tempname(suffix="_.pdb") output_prefix = path.splitext(output_pdb)[0] protein_pdbqt = tempname(suffix=".pdbqt") ligand_pdbqt = tempname(suffix=".pdbqt") output_pdbqt = tempname(suffix=".pdbqt") protein.write(protein_pdb) lig2 = ligand.copy() lig2.atomtype = lig2.element # babel does not understand mol2 atomtypes and requires elements instead lig2.write(ligand_mol2) # Dirty hack to remove the 'END' line from the PDBs since babel hates it with open(protein_pdb, 'r') as f: lines = f.readlines() with open(protein_pdb, 'w') as f: f.writelines(lines[:-1]) # End of dirty hack try: if vinaexe is None: import platform suffix = '' if platform.system() == "Windows": suffix = '.exe' vinaexe = '{}-vina{}'.format(platform.system(), suffix) vinaexe = shutil.which(vinaexe, mode=os.X_OK) if not vinaexe: raise NameError('Could not find vina, or no execute permissions are given') except: raise NameError('Could not find vina, or no execute permissions are given') try: babelexe = shutil.which(babelexe, mode=os.X_OK) if babelexe is None: raise NameError('Could not find babel, or no execute permissions are given') except: raise NameError('Could not find babel, or no execute permissions are given') call([babelexe, '-i', 'pdb', protein_pdb, '-o', 'pdbqt', '-O', protein_pdbqt, '-xr']) if np.all(ligand.charge != 0): logger.info('Charges detected in ligand and will be used for docking.') call([babelexe, '-i', 'mol2', ligand_mol2, '-o', 'pdbqt', '-O', ligand_pdbqt, '-xn', '-xh']) else: logger.info('Charges were not defined for all atoms. Will guess charges anew using gasteiger method.') call([babelexe, '-i', 'mol2', ligand_mol2, '-o', 'pdbqt', '-O', ligand_pdbqt, '-xn', '-xh', '--partialcharge', 'gasteiger']) if not path.isfile(ligand_pdbqt): raise NameError('Ligand could not be converted to PDBQT') if not path.isfile(protein_pdbqt): raise NameError('Protein could not be converted to PDBQT') call([vinaexe, '--receptor', protein_pdbqt, '--ligand', ligand_pdbqt, '--out', output_pdbqt, '--center_x', str(center[0]), '--center_y', str(center[1]), '--center_z', str(center[2]), '--size_x', str(extent[0]), '--size_y', str(extent[1]), '--size_z', str(extent[2]), '--num_modes', str(numposes)]) call([babelexe, '-m', '-i', 'pdbqt', output_pdbqt, '-o', 'pdb', '-O', output_pdb, '-xhn']) from natsort import natsorted outfiles = natsorted(glob('{}*.pdb'.format(output_prefix))) scoring = [] poses = [] for i, ligf in enumerate(outfiles): scoring.append(_parseScoring(ligf)) l = Molecule(ligf) l.viewname = 'Pose {}'.format(i) poses.append(l) os.remove(protein_pdb) os.remove(ligand_mol2) os.remove(protein_pdbqt) os.remove(ligand_pdbqt) os.remove(output_pdbqt) return poses, np.array(scoring)
def setUp(self): self.lfh=sys.stderr self.verbose=False self.pathPdbxDataFile = os.path.join(home(dataDir='molecule-readers'), "1kip.cif") self.pathOutputFile = tempname(suffix='.cif')
from htmd.home import home from htmd.molecule.molecule import Molecule, mol_equal from htmd.util import tempname import numpy as np import os testfolder = home(dataDir='metricdistance') mol = Molecule(os.path.join(testfolder, 'filtered.pdb')) mol.coords = np.tile(mol.coords, (1, 1, 2)) mol.filter('protein and resid 1 to 20') mol.boxangles = np.ones((3, 2), dtype=np.float32) * 90 mol.box = np.ones((3, 2), dtype=np.float32) * 15 mol.step = np.arange(2) mol.time = np.arange(2) * 1E5 for ext in _WRITERS: tmp = tempname(suffix='.'+ext) mol.write(tmp) print('Can write {} files'.format(ext)) # from difflib import Differ # d = Differ() # # with open(tmp, 'rb') as f1, open(a, 'rb') as f2: # res = d.compare([x.decode('utf8') for x in f1.readlines()], [x.decode('utf8') for x in f2.readlines()]) # # try: # next(res) # except StopIteration: # print('Same files') # else:
def dock(protein, ligand, center=None, extent=None): """ Molecular docking, using Vina If centre and extent are not provided, docking will be performed over the whole protein Parameters ---------- protein Molecule object representing the receptor ligand Molecule object representing the ligand to dock center 3-vec centre of of the search bounding box (optional) extent 3-vec linear extent of the search bounding box (optional) Returns ------- poses Molecule object representing the N<10 best poses scores 3x num_poses matrix containing kcal, rmsd lb, rmsd ub Examples -------- >>> poses = dock(protein, ligand) >>> poses = dock(protein, ligand, center=[ 10., 5., 12. ], extent=[ 15., 15., 15. ] ) """ if np.size(protein.coords, 2) != 1 or np.size(ligand.coords, 2) != 1: raise NameError('Protein and ligand Molecules should be single frames') buffer = 10. # Angstrom buffer around protein for whole-protein docking c_min = np.min(protein.coords, 0).reshape((1, 3))[0] c_max = np.max(protein.coords, 0).reshape((1, 3))[0] if center is None: center = (buffer + (c_max + c_min)) / 2 if extent is None: extent = (c_max - c_min) + buffer # babel -i pdb protein.pdb -o pdbqt protein.pdbqt -xr # babel -i pdb ligand.pdb -o pdbqt ligand.pdbqt -xhn # vina --ligand ligand.pdbqt --receptor protein.pdbqt --center_x 0. --center_y 0. --center_z 0. --size_x 60. --size_y 60. --size_z 60 --exhaustiveness 10 # babel -m -i pdbqt ligand_out.pdbqt -o pdb out_.pdb -xhn protein_pdb = tempname(suffix=".pdb") ligand_pdb = tempname(suffix=".pdb") output_pdb = tempname(suffix="_.pdb") output_prefix = path.splitext(output_pdb)[0] protein_pdbqt = tempname(suffix=".pdbqt") ligand_pdbqt = tempname(suffix=".pdbqt") output_pdbqt = tempname(suffix=".pdbqt") protein.write(protein_pdb) ligand.write(ligand_pdb) try: import platform suffix="" if platform.system() == "Windows": suffix=".exe" vinaexe = shutil.which(platform.system() + "-vina" + suffix, mode=os.X_OK ) if not vinaexe: raise NameError('Could not find vina, or no execute permissions are given') except: raise NameError('Could not find vina, or no execute permissions are given') try: babelexe = shutil.which('htmd_babel', mode=os.X_OK) except: raise NameError('Could not find babel, or no execute permissions are given') #babelexe = path.join(home(), 'bin', 'htmd_babel') #vinaexe = path.join(home(), 'bin', 'htmd_vina') #if not os.access(babelexe, os.X_OK): # raise NameError('Could not find ' + babelexe + ' or no execute permissions are given') #if not os.access(vinaexe, os.X_OK): # raise NameError('Could not find ' + vinaexe + ' or no execute permissions are given') #from IPython.core.debugger import Tracer #Tracer()() call([babelexe, '-i', 'pdb', protein_pdb, '-o', 'pdbqt', protein_pdbqt, '-xr']) call([babelexe, '-i', 'pdb', ligand_pdb, '-o', 'pdbqt', ligand_pdbqt, '-xhn']) if not path.isfile(ligand_pdbqt): raise NameError('Ligand could not be converted to PDBQT') if not path.isfile(protein_pdbqt): raise NameError('Protein could not be converted to PDBQT') call([vinaexe, '--receptor', protein_pdbqt, '--ligand', ligand_pdbqt, '--out', output_pdbqt, '--center_x', str(center[0]), '--center_y', str(center[1]), '--center_z', str(center[2]), '--size_x', str(extent[0]), '--size_y', str(extent[1]), '--size_z', str(extent[2])]) call([babelexe, '-m', '-i', 'pdbqt', output_pdbqt, '-o', 'pdb', output_pdb, '-xhn']) scoring = np.zeros((0,3)) coords = [] idx = 1 name = '{}{}.pdb'.format(output_prefix, idx) while path.isfile(name): # First get the scoring kcal = float(check_output('grep "VINA RESULT" ' + name + ' | awk \'{print $4}\'', shell=True).decode('ascii').strip()) rmsdlb = float(check_output('grep "VINA RESULT" ' + name + ' | awk \'{print $5}\'', shell=True).decode('ascii').strip()) rmsdub = float(check_output('grep "VINA RESULT" ' + name + ' | awk \'{print $6}\'', shell=True).decode('ascii').strip()) scoring = np.append(scoring, np.array([[float(kcal), float(rmsdlb), float(rmsdub)]]), axis=0) next_pose = Molecule(name) os.remove(name) c = next_pose.coords co = c.copy() natoms = len(ligand.name) for idx_i in range(natoms): for idx_j in range(natoms): if ligand.name[idx_i] == next_pose.name[idx_j]: co[idx_i, :, :] = c[idx_j, :, :] '''if idx == 1: coords = co else: coords = np.append(coords, co, axis=2)''' coords.append(co) idx += 1 name = '{}{}.pdb'.format(output_prefix, idx) poses = [] for i, c in enumerate(coords): l = ligand.copy() l.viewname = 'Pose {}'.format(i) l.coords = c poses.append(l) os.remove(protein_pdb) os.remove(ligand_pdb) os.remove(protein_pdbqt) os.remove(ligand_pdbqt) os.remove(output_pdbqt) return poses, scoring
def test_acemd2(self): from htmd.home import home from htmd.util import tempname import filecmp from glob import glob tmpdir = tempname() acemd = Acemd2() acemd.temperature = 350 acemd.restart = 'on' acemd.restartfreq = 25 acemd.outputname = 'output' acemd.xtcfile = 'myout.xtc' acemd.xtcfreq = 500 acemd.timestep = 4 acemd.rigidbonds = 'all' acemd.hydrogenscale = 0.3 acemd.switching = 'on' acemd.switchdist = 9 acemd.cutoff = 5 acemd.exclude = 'scaled1-4' acemd.scaling14 = 5.6 acemd.langevin = 'on' acemd.langevintemp = 300 acemd.langevindamping = 0.8 acemd.pme = 'on' acemd.pmegridspacing = 5 acemd.fullelectfrequency = 3 acemd.energyfreq = 10 acemd.constraints = 'on' acemd.consref = '5dhfr_cube.pdb' acemd.constraintscaling = 3.2 acemd.berendsenpressure = 'on' acemd.berendsenpressuretarget = 14 acemd.berendsenpressurerelaxationtime = 2 acemd.tclforces = 'on' acemd.minimize = 150 acemd.run = 555 acemd.celldimension = [3, 56, 2] acemd.useconstantratio = 'on' acemd.amber = 'off' acemd.dielectric = 16 acemd.pairlistdist = 9 acemd.TCL = 'on' acemd.bincoordinates = None acemd.binvelocities = None acemd.binindex = None acemd.structure = '5dhfr_cube.psf' acemd.parameters = 'par_all22_prot.inp' acemd.extendedsystem = None acemd.coordinates = '5dhfr_cube.pdb' acemd.velocities = None acemd.parmfile = None acemd.coordinates = '5dhfr_cube.pdb' acemd.setup(home(dataDir='dhfr/'), tmpdir, overwrite=True) # Compare with reference refdir = home(dataDir='test-acemd-v2') 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.write between {} and {} ' 'in files {}.'.format(refdir, tmpdir, mismatch)) import shutil shutil.rmtree(tmpdir)
for f in deletefiles: os.remove(f) # Test ACEMD version 2 pd = Production(_version=2) pd.runtime = 4 pd.timeunits = 'ns' pd.temperature = 300 pd.useconstraints = True pd.constraints = {'protein and name CA': 1, 'protein and noh and not name CA': 0.1} pd.fb_reference = 'protein and name CA' pd.fb_selection = 'resname MOL and noh' pd.fb_box = [-21, 21, -19, 19, 29, 30] pd.fb_k = 5 tmpdir = tempname() pd.write(home(dataDir=os.path.join('test-protocols', 'equilibration', 'acemd2', 'protLig', 'postrun')), tmpdir) # Compare with reference refdir = home(dataDir=os.path.join('test-protocols', 'production', 'acemd2', 'protLig', 'prerun')) files = [os.path.basename(f) for f in glob(os.path.join(refdir, '*'))] _compareResultFolders(refdir, tmpdir, 'protLig') # Test new ACEMD version 3 pd = Production(_version=3) pd.runtime = 4 pd.timeunits = 'ns' pd.temperature = 300 pd.useconstraints = True pd.constraints = {'protein and name CA': 1, 'protein and noh and not name CA': 0.1}
def buildMembrane(xysize, ratioupper, ratiolower, waterbuff=20, equilibrate=True, outdir='./buildmemb/', lipidf=None): """ Construct a membrane containing arbitrary lipids and ratios of them. Parameters ---------- xysize : list A list containing the size in x and y dimensions of the membrane in Angstroms ratioupper : list of lists A list containing sublists indicating the molecule name and the ratio of that molecule for the upper layer ratiolower : list of lists Same as ratioupper but for the lower layer waterbuff : float The z-dimension size of the water box above and below the membrane equilibrate : bool If True it equilibrates the membrane outdir : str A folder in which to store the psf and pdb files lipidf : str The path to the starting lipid conformations Returns ------- mol : :class:`Molecule <htmd.molecule.molecule.Molecule` The resulting membrane including surrounding waters Examples -------- >>> lipidratioupper = [['popc', 10], ['chl1', 1]] >>> lipidratiolower = [['popc', 8], ['chl1', 2]] >>> width = [50, 100] >>> res = buildMembrane(width, lipidratioupper, lipidratiolower) """ from htmd.membranebuilder.ringpenetration import resolveRingPenetrations from htmd.builder.solvate import solvate from htmd.builder.charmm import build from htmd.util import tempname from htmd.molecule.molecule import Molecule from htmd.home import home import os import pandas as pd if lipidf is None: lipidf = os.path.join(home(), 'membranebuilder', 'lipids') lipiddb = pd.read_csv(os.path.join(home(), 'membranebuilder', 'lipiddb.csv'), index_col='Name') uqlip = np.unique([x[0] for x in ratioupper] + [x[0] for x in ratiolower]) files = _locateLipidFiles(lipidf, uqlip) area = np.prod(xysize) lipids = _createLipids(ratioupper, area, lipiddb, files, leaflet='upper') lipids += _createLipids(ratiolower, area, lipiddb, files, leaflet='lower') _setPositionsLJSim(xysize, [l for l in lipids if l.xyz[2] > 0]) _setPositionsLJSim(xysize, [l for l in lipids if l.xyz[2] < 0]) _findNeighbours(lipids, xysize) _loadMolecules(lipids, files) resolveRingPenetrations(lipids, xysize) memb = _createMembraneMolecule(lipids) # from globalminimization import minimize # newpos, newrot = minimize(lipids, xysize + [100], stepxy=0.5, steprot=50, contactthresh=1) # for i in range(len(lipids)): # lipids[i].xyz[:2] = newpos[i] # lipids[i].rot = newrot[i] # # resolveRingPenetrations(lipids, xysize) # endmemb = _createMembraneMolecule(lipids) minc = memb.get('coords', 'name P').min(axis=0) - 5 maxc = memb.get('coords', 'name P').max(axis=0) + 5 mm = [[0, 0, maxc[2] - 2], [xysize[0], xysize[1], maxc[2] + waterbuff]] smemb = solvate(memb, minmax=mm) mm = [[0, 0, minc[2] - waterbuff], [xysize[0], xysize[1], minc[2] + 2]] smemb = solvate(smemb, minmax=mm) smemb.moveBy([0, 0, -smemb.coords[:, 2, 0].min()]) if outdir is None: outdir = tempname() print('Outdir ', outdir) res = build(smemb, ionize=False, stream=['str/lipid/toppar_all36_lipid_cholesterol_model_1.str'], outdir=outdir) if equilibrate: from htmd.membranebuilder.simulate_openmm import equilibrateSystem from shutil import copy, move outpdb = tempname(suffix='.pdb') charmmf = os.path.join(home(), 'membranebuilder', 'charmm-toppar') equilibrateSystem(os.path.join(outdir, 'structure.pdb'), os.path.join(outdir, 'structure.psf'), outpdb, charmmfolder=charmmf) res = Molecule(outpdb) res.center() move(os.path.join(outdir, 'structure.pdb'), os.path.join(outdir, 'starting_structure.pdb')) copy(outpdb, os.path.join(outdir, 'structure.pdb')) return res
md.run() # Cleaning up inputodel = glob(path.join(home(), 'data', 'adaptive', 'input', 'e2*')) for i in inputodel: shutil.rmtree(i, ignore_errors=True, acemd='/shared/acemd/bin/acemd') os.remove(path.join(home(), 'data', 'adaptive', 'input', 'e2_writeinputs.log'))''' import htmd import os import shutil from htmd.queues.localqueue import LocalGPUQueue from htmd.simlist import Frame, simlist from htmd.util import tempname filedir = htmd.home.home()+'/data/adaptive/' sims = simlist(glob(os.path.join(filedir, 'data', '*', '')), glob(os.path.join(filedir, 'input', '*', '')), glob(os.path.join(filedir, 'input', '*', ''))) outf = tempname() os.makedirs(outf) f = Frame(sims[0], 0, 5) _writeInputsFunction(1, f, 2, outf, 'input.coor') mol = Molecule(sims[0]) mol.read(os.path.join(outf, 'e2s2_e1s1p0f5', 'input.coor')) shutil.rmtree(outf)
def loopModeller(mol, segid, seq, startresid, movstart=None, movend=None, modellerexe='mod9.18'): """ Uses the Modeller software to predict missing loops in a Molecule. Parameters ---------- mol : :class:`Molecule <htmd.molecule.molecule.Molecule>` object A Molecule object. segid : str The name of the segment containing the gap. seq : str The sequence of residues to be added by the loop modeller. startresid : int The resid of the residue before the gap. movstart : int The resid after which the residues will be allowed to move in modeller. movend : int The resid before which the residues will be allowed to move in modeller. modellerexe : str The path to the modeller executable. Returns ------- newmol : :class:`Molecule <htmd.molecule.molecule.Molecule>` object A new Molecule object containing the protein with the modelled loop. Examples -------- >>> mol = Molecule('1qg8') >>> mol2 = loopModeller(mol, '0', 'ENR', 133) """ if shutil.which(modellerexe) is None: raise NameError('Could not find a Modeller executable called `{}` in the PATH. This might indicate a wrong path' ' to the executable or a missing installation. To install modeller use `conda install -c ' 'salilab modeller` and follow the instructions. To provide the correct path change the ' '`modellerexe` argument'.format(modellerexe)) if movstart is None: movstart = startresid if movend is None: movend = movstart + len(seq) + 1 segatm = mol.segid == segid segres = np.unique(mol.resid[segatm]) chain = np.unique(mol.chain[segatm]) if len(chain) != 1: raise RuntimeError('More than one chain detected in segment {}'.format(segid)) pos = np.where(segres == startresid)[0][0] if (segres[pos+1] - segres[pos]) != (len(seq) + 1): raise RuntimeError('Sequence is {} characters long while sequence gap ({}-{}) is {} long. Cannot insert loop.'.format( len(seq), segres[pos], segres[pos+1], (segres[pos+1] - segres[pos]))) chain = chain[0] segresidstart = np.min(segres) segresidend = np.max(segres) currseq = mol.sequence()[segid] minuses = ''.join(['-'] * len(seq)) inspos = np.where(segres == startresid)[0][0] + 1 gapseq = currseq[:inspos] + minuses + currseq[inspos:] + '*' fullseq = currseq[:inspos] + seq + currseq[inspos:] + '*' # Get the sequence of the 1qg8 PDB file, and write to an alignment file pdbfile = tempname(suffix='.pdb') mol.write(pdbfile) alifile = tempname(suffix='.ali') f = open(alifile, 'w') f.write('>P1;prot\nstructure:{}:{}:{}:{}:{}::::\n'.format(pdbfile, segresidstart, chain, segresidend, chain)) f.write(gapseq) f.write('\n>P1;prot_fill\nsequence:::::::::\n') f.write(fullseq) f.close() script = ''' from modeller import * from modeller.automodel import * # Load the automodel class log.verbose() env = environ() class MyModel(automodel): def select_atoms(self): return selection(self.residue_range('{}', '{}')) a = MyModel(env, alnfile = '{}', knowns = 'prot', sequence = 'prot_fill') a.starting_model= 1 a.ending_model = 1 a.make() '''.format(movstart+1-segresidstart, movend-segresidstart, alifile) pyfile = tempname(suffix='.py') f = open(pyfile, 'w') f.write(script) f.close() print(pdbfile) print(alifile) print(pyfile) call('{} {}'.format(modellerexe, pyfile), shell=True) newmol = Molecule('./prot_fill.B99990001.pdb') print('You can ignore the `import site` error (https://salilab.org/modeller/release.html#issues).' ' The results should have been returned.') return newmol