Exemple #1
0
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
Exemple #2
0
    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
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
    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('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 = ''
        self.acemd.setup(inputdir, outputdir, overwrite=True)
Exemple #6
0
    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)
Exemple #7
0
    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')
Exemple #8
0
    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)
Exemple #9
0
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
Exemple #10
0
    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
Exemple #11
0
    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()
Exemple #12
0
    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)
Exemple #13
0
    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
Exemple #14
0
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, []
Exemple #15
0
    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)
Exemple #16
0
    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)
Exemple #17
0
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))
Exemple #18
0
    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)
Exemple #19
0
    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)
Exemple #20
0
def _filterPDBPSF(sim, outfolder, filtsel):
    try:
        mol = Molecule(sim.molfile)
    except IOError as e:
        raise NameError('simFilter: ' + e.strerror + ' Cannot create filtered.pdb due to problematic pdb: ' + sim.molfile)

    if not path.isfile(path.join(outfolder, 'filtered.pdb')):
        mol.write(path.join(outfolder, 'filtered.pdb'), filtsel)
    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)
    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)
Exemple #23
0
    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)
Exemple #24
0
    def __init__(self, sims, protsel, dih=None, sincos=True):
        self._protsel = protsel
        self._sincos = sincos
        self._dih = dih  # TODO: Calculate the dihedral
        self._pc_dih = None

        (single, molfile) = _singleMolfile(sims)
        if single:
            mol = Molecule(molfile)
            self._pc_dih = self._dihedralPrecalc(mol, mol.atomselect(protsel))
Exemple #25
0
def _filterPDBPSF(sim, outfolder, filtsel):
    try:
        mol = Molecule(sim.molfile)
    except IOError as e:
        raise NameError('simFilter: {}. Cannot create filtered.pdb due to problematic pdb: {}'.format(e, sim.molfile))

    if not path.isfile(path.join(outfolder, 'filtered.pdb')):
        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)
        mol.write(path.join(outfolder, 'filtered.pdb'), filtsel)
Exemple #26
0
    def __init__(self, sims, protsel, dih=None, sincos=True):
        self._protsel = protsel
        self._sincos = sincos
        self._dih = dih  # TODO: Calculate the dihedral
        self._pc_dih = None

        (single, molfile) = _singleMolfile(sims)
        if single:
            mol = Molecule(molfile)
            self._pc_dih = self._dihedralPrecalc(mol, mol.atomselect(protsel))
Exemple #27
0
    def listDihedrals(filename):
        # This routine gets the names of the atoms in the soft dihedrals
        # It's so horrible, since we have to jump through hoops to
        # set up the input for gen_soft_list
        ll1 = []
        ll2 = []
        env = Parameterization._preflight_test(None)
        mol = Molecule(filename)
        mol = Parameterization._rename_mol(mol)
        mol.bonds = mol._guessBonds()
        # make a tempdir
        with tempfile.TemporaryDirectory() as td:
            # td=tempfile.mkdtemp()
            # print(td)
            pwd = os.getcwd()
            os.chdir(td)
            f = open("mol.prm", "w")
            f.close()
            mol.write("mol.pdb")
            mol.write("mol-opt.xyz")
            mol.write("mol.xpsf", type="psf")
            for charge in range(-1, 2):
                subprocess.check_output([
                    env['BIN_MATCH'], "-charge",
                    str(charge), "-forcefield", "top_all36_cgenff_new",
                    "mol.pdb"
                ],
                                        stderr=subprocess.STDOUT,
                                        shell=False,
                                        stdin=None)
            subprocess.check_output(
                [env['BIN_GEN_XPSF'], "mol.rtf", "mol.xpsf", "MOL"],
                stderr=subprocess.STDOUT,
                shell=False,
                stdin=None)
            subprocess.check_output([env['BIN_GEN_SOFT_LIST']],
                                    stderr=subprocess.STDOUT,
                                    shell=False,
                                    stdin=None)

            f = open("soft-dih-list.txt", "r")
            ff = f.readlines()
            for l in ff:
                ss = []
                tt = []
                for m in l.split():
                    tt.append(int(m))
                    ss.append(mol.name[int(m) - 1].strip().upper())
                ll1.append(tt)
                ll2.append(ss)

            f.close()
            os.chdir(pwd)

        return ll1, ll2
Exemple #28
0
    def setUpClass(self):
        from htmd.molecule.molecule import Molecule
        from os import path
        self.mol = Molecule(
            path.join(home(), 'data', 'metricdistance', 'filtered.pdb'))
        self.mol_skipped = self.mol.copy()

        self.mol.read(path.join(home(), 'data', 'metricdistance', 'traj.xtc'))
        self.mol_skipped.read(path.join(home(), 'data', 'metricdistance',
                                        'traj.xtc'),
                              skip=10)
Exemple #29
0
def _loadMolecules(lipids, files):
    from htmd.rotationmatrix import rotationMatrix
    # Create Molecules
    for l in lipids:
        randidx = np.random.randint(len(files[l.resname]))
        mol = Molecule(files[l.resname][randidx])
        mol.filter('not water', _logger=False)
        if l.xyz[2] < 0:
            mol.rotateBy(rotationMatrix([1, 0, 0], np.deg2rad(180)))  # Rotate the lower leaflet lipids upside down
        l.mol = mol
        l.rot = np.random.random() * 360 - 180  # Random starting rotation
Exemple #30
0
def _filterPDBPSF(sim, outfolder, filtsel):
    try:
        mol = Molecule(sim.molfile)
    except IOError as e:
        raise NameError(
            'simFilter: ' + e.strerror +
            ' Cannot create filtered.pdb due to problematic pdb: ' +
            sim.molfile)

    if not path.isfile(path.join(outfolder, 'filtered.pdb')):
        mol.write(path.join(outfolder, 'filtered.pdb'), filtsel)
Exemple #31
0
    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()

        from htmd.units import convert
        numsteps = convert(self.timeunits, 'timesteps', self.runtime, timestep=self.acemd.timestep)
        self.acemd.temperature = self.temperature
        self.acemd.thermostattemp = self.temperature
        self.acemd.run = str(numsteps)

        if self.constraintsteps is None:
            constrsteps = int(numsteps / 2)
        else:
            constrsteps = int(self.constraintsteps)

        # Adding the default restraints of the equilibration
        restraints = list()
        restraints.append(AtomRestraint('protein and noh and not name CA', 0, [(0.1, 0), (0, constrsteps)]))
        restraints.append(AtomRestraint('protein and name CA', 0, [(1, 0), (0, constrsteps)]))
        if self.restraints is not None:
            restraints += self.restraints
        self.acemd.restraints = restraints

        if self.acemd.celldimension is None and self.acemd.extendedsystem is None:
            inmol = Molecule(os.path.join(inputdir, self.acemd.coordinates))
            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)
Exemple #32
0
def tileMembrane(memb, xmin, ymin, xmax, ymax, buffer=1.5):
    """ Tile a membrane in the X and Y dimensions to reach a specific size.

    Parameters
    ----------
    memb
    xmin
    ymin
    xmax
    ymax
    buffer

    Returns
    -------
    megamemb :
        A big membrane Molecule
    """
    from htmd.progress.progress import ProgressBar
    memb = memb.copy()
    memb.resid = sequenceID(memb.resid)

    minmemb = np.min(memb.get('coords', 'water'), axis=0).flatten()

    size = np.max(memb.get('coords', 'water'), axis=0) - np.min(memb.get('coords', 'water'), axis=0)
    size = size.flatten()
    xreps = int(np.ceil((xmax - xmin) / size[0]))
    yreps = int(np.ceil((ymax - ymin) / size[1]))

    logger.info('Replicating Membrane {}x{}'.format(xreps, yreps))

    from htmd.molecule.molecule import Molecule
    megamemb = Molecule()
    bar = ProgressBar(xreps * yreps, description='Replicating Membrane')
    k = 0
    for x in range(xreps):
        for y in range(yreps):
            tmpmemb = memb.copy()
            xpos = xmin + x * (size[0] + buffer)
            ypos = ymin + y * (size[1] + buffer)

            tmpmemb.moveBy([-float(minmemb[0]) + xpos, -float(minmemb[1]) + ypos, 0])
            tmpmemb.remove('same resid as (x > {} or y > {})'.format(xmax, ymax), _logger=False)
            tmpmemb.set('segid', 'M{}'.format(k))

            megamemb.append(tmpmemb)
            k += 1
            bar.progress()
    bar.stop()
    # Membranes don't tile perfectly. Need to remove waters that clash with lipids of other tiles
    # Some clashes will still occur between periodic images however
    megamemb.remove('same fragment as water and within 1.5 of not water', _logger=False)
    return megamemb
Exemple #33
0
def opm(pdb):
    """Download a molecule from the OPM.

    Removes DUM atoms.

    Parameters
    ----------
    pdb: str
        The 4-letter PDB code

    Returns
    -------
    mol: Molecule
        The oriented molecule

    thickness: float
        The bilayer thickness (both layers)

    Examples
    --------
    >>> mol, thickness = opm("1z98")
    >>> mol.numAtoms
    7902
    >>> thickness
    28.2

    """

    from htmd.molecule.support import string_to_tempfile
    from htmd.molecule.molecule import Molecule
    # http://opm.phar.umich.edu/pdb/1z98.pdb
    r = requests.get("http://opm.phar.umich.edu/pdb/{:s}.pdb".format(
        pdb.lower()))

    if r.status_code != 200:
        raise NameError('PDB code not found in the OPM database')

    tempfile = string_to_tempfile(r.content.decode('ascii'), "pdb")
    mol = Molecule(tempfile)
    mol.filter("not resname DUM")

    # Assuming the half-thickness is the last word in the first line
    # REMARK      1/2 of bilayer thickness:   14.1
    f = open(tempfile)
    h = f.readline()
    f.close()
    os.unlink(tempfile)

    hs = h.split()
    thickness = 2.0 * float(hs[-1])

    return mol, thickness
Exemple #34
0
    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')
Exemple #35
0
def opm(pdb):
    """Download a molecule from the OPM.

    Removes DUM atoms.

    Parameters
    ----------
    pdb: str
        The 4-letter PDB code

    Returns
    -------
    mol: Molecule
        The oriented molecule

    thickness: float
        The bilayer thickness (both layers)

    Examples
    --------
    >>> mol, thickness = opm("1z98")
    >>> mol.numAtoms
    7902
    >>> thickness
    28.2

    """

    from htmd.molecule.support import string_to_tempfile
    from htmd.molecule.molecule import Molecule
    # http://opm.phar.umich.edu/pdb/1z98.pdb
    r = requests.get("http://opm.phar.umich.edu/pdb/{:s}.pdb".format(pdb.lower()))

    if r.status_code != 200:
        raise NameError('PDB code not found in the OPM database')

    tempfile = string_to_tempfile(r.content.decode('ascii'), "pdb")
    mol = Molecule(tempfile)
    mol.filter("not resname DUM")

    # Assuming the half-thickness is the last word in the first line
    # REMARK      1/2 of bilayer thickness:   14.1
    f = open(tempfile)
    h = f.readline()
    f.close()
    os.unlink(tempfile)

    hs = h.split()
    thickness = 2.0 * float(hs[-1])

    return mol, thickness
Exemple #36
0
    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')
Exemple #37
0
    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)
Exemple #38
0
    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))
Exemple #39
0
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
Exemple #40
0
    def testWithoutProteinPrepare(self):
        from htmd.builder.solvate import solvate
        # Test without proteinPrepare
        pdbids = ['3PTB']
        # pdbids = ['3PTB', '1A25', '1GZM', '1U5U']
        for pid in pdbids:
            np.random.seed(1)
            mol = Molecule(pid)
            mol.filter('protein')
            smol = solvate(mol)
            ffs = defaultFf()
            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)
Exemple #41
0
    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)
Exemple #42
0
    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)
Exemple #43
0
    def __init__(self, sims, refmol, trajalnstr, refalnstr, atomsel, centerstr):
        self._refmol = refmol
        self._refalnsel = self._refmol.atomselect(refalnstr)
        self._trajalnsel = trajalnstr
        self._centersel = centerstr
        self._atomsel = atomsel
        self._pc_trajalnsel = None  # pc = Pre-calculated
        self._pc_atomsel = None
        self._pc_centersel = None

        (single, molfile) = _singleMolfile(sims)
        if single:
            mol = Molecule(molfile)
            self._pc_trajalnsel = mol.atomselect(trajalnstr)
            self._pc_atomsel = mol.atomselect(atomsel)
            self._pc_centersel = mol.atomselect(centerstr)
Exemple #44
0
    def __init__(self, sims, refmol, trajalnstr, refalnstr, atomsel, centerstr):
        self._refmol = refmol
        self._refalnsel = self._refmol.atomselect(refalnstr)
        self._trajalnsel = trajalnstr
        self._centersel = centerstr
        self._atomsel = atomsel
        self._pc_trajalnsel = None  # pc = Pre-calculated
        self._pc_atomsel = None
        self._pc_centersel = None

        (single, molfile) = _singleMolfile(sims)
        if single:
            mol = Molecule(molfile)
            self._pc_trajalnsel = mol.atomselect(trajalnstr)
            self._pc_atomsel = mol.atomselect(atomsel)
            self._pc_centersel = mol.atomselect(centerstr)
Exemple #45
0
    def testWithoutProteinPrepare(self):
        from htmd.builder.solvate import solvate
        # Test without proteinPrepare
        pdbids = ['3PTB']
        # pdbids = ['3PTB', '1A25', '1GZM', '1U5U']
        for pid in pdbids:
            np.random.seed(1)
            mol = Molecule(pid)
            mol.filter('protein')
            smol = solvate(mol)
            ffs = defaultFf()
            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)
Exemple #46
0
    def _analyse(self, mol, pdb, traj):
        m = Molecule(pdb)
        m.read(traj)
        torsions = Parameterization.listDihedrals(mol)
        # print(torsions)
        for i in range(len(torsions[0])):
            # For each torsion, measure
            title = torsions[1][i][0]
            title = title + "-" + torsions[1][i][1]
            title = title + "-" + torsions[1][i][2]
            title = title + "-" + torsions[1][i][3]

            (r, theta) = self._measure_torsion(torsions[0][i], m.coords)

            self._plot_scatter(r, theta, title)
            self._plot_hist(theta, title)
Exemple #47
0
def equilibrateSystem(pdbfile, psffile, outpdb, numsteps=30000, minimplatform='CPU', equilplatform='CUDA', device=0,
                      temp=300, minimize=500000, minimizetol=100, charmmfolder=None):
    pdb = Molecule(pdbfile)
    watcoo = pdb.get('coords', 'water')
    celld = unit.Quantity((watcoo.max(axis=0) - watcoo.min(axis=0)).squeeze(), unit=unit.angstrom)

    psf = app.CharmmPsfFile(psffile)
    pdb = app.PDBFile(pdbfile)
    psf.setBox(celld[0], celld[1], celld[2])

    params = readCharmmParameters(charmmfolder, defaultCharmmFiles)

    system = psf.createSystem(params, nonbondedMethod=app.PME,
                              nonbondedCutoff=1*unit.nanometer,
                              constraints=app.HBonds)
    system.addForce(mm.MonteCarloBarostat(1*unit.atmospheres, temp*unit.kelvin, 25))

    platform, prop = getPlatform(minimplatform, device)
    integrator = mm.LangevinIntegrator(temp*unit.kelvin, 1/unit.picosecond, 0.002*unit.picoseconds)
    simulation = app.Simulation(psf.topology, system, integrator, platform, prop)
    simulation.context.setPositions(pdb.positions)

    # Perform minimization
    printEnergies(simulation, 'Energy before minimization')
    simulation.minimizeEnergy(tolerance=minimizetol*unit.kilojoule/unit.mole, maxIterations=minimize)
    printEnergies(simulation, 'Energy after minimization')

    # Copy coordinates from miminization context to equilibration context
    state = simulation.context.getState(getPositions=True)
    pos = state.getPositions()

    # Set up the equilibration simulation
    platform, prop = getPlatform(equilplatform, device)
    integrator = mm.LangevinIntegrator(temp*unit.kelvin, 1/unit.picosecond, 0.002*unit.picoseconds)
    simulation = app.Simulation(psf.topology, system, integrator, platform, prop)
    simulation.context.setPositions(pos)

    # Generate initial velocities
    simulation.context.setVelocitiesToTemperature(temp)

    from htmd.membranebuilder.pdbreporter import PDBReporter
    from htmd.membranebuilder.dcdreporter import DCDReporter
    simulation.reporters.append(PDBReporter(outpdb, numsteps, enforcePeriodicBox=False))
    simulation.reporters.append(app.StateDataReporter(stdout, int(numsteps/10), step=True,
        potentialEnergy=True, kineticEnergy=True, totalEnergy=True, temperature=True,
        progress=True, speed=True, remainingTime=True, totalSteps=numsteps, separator='\t'))
    simulation.step(numsteps)
Exemple #48
0
    def test_project_align(self):
        from htmd.molecule.molecule import Molecule
        from htmd.home import home
        from os import path
        mol = Molecule(path.join(home(), 'data', 'metricdistance', 'filtered.pdb'))
        mol.read(path.join(home(), 'data', 'metricdistance', 'traj.xtc'))
        ref = mol.copy()
        ref.coords = np.atleast_3d(ref.coords[:, :, 0])
        metr = MetricCoordinate('protein and name CA', ref)
        data = metr.project(mol)

        lastcoors = np.array([6.79283285, 5.55226946, 4.49387407, 2.94484425,
                              5.36937141, 3.18590879, 5.75874281, 5.48864174,
                              1.69625032, 1.58790839, 0.57877392, -2.66498065,
                              -3.70919156, -3.33702421, -5.38465405, -8.43286991,
                              -8.15859032, -7.85062265, -10.92551327, -13.70733166], dtype=np.float32)
        assert np.all(np.abs(data[-1, -20:] - lastcoors) < 0.001), 'Coordinates calculation is broken'
Exemple #49
0
    def test_project(self):
        from htmd.molecule.molecule import Molecule
        from htmd.home import home
        from os import path
        mol = Molecule(path.join(home(), 'data', 'metricdistance', 'filtered.pdb'))
        mol.read(path.join(home(), 'data', 'metricdistance', 'traj.xtc'))
        ref = mol.copy()
        ref.coords = np.atleast_3d(ref.coords[:, :, 0])
        metr = MetricCoordinate('protein and name CA')
        data = metr.project(mol)

        lastcoors = np.array([-24.77000237, -27.76000023, -30.44000244, -33.65000153,
                              -33.40999985, -36.32000351, -36.02000427, -36.38000107,
                              -39.61000061, -41.01000214, -43.80000305, -45.56000137,
                              -45.36000061, -47.13000488, -49.54000473, -50.6000061 ,
                              -50.11000061, -52.15999985, -55.1400032 , -55.73000336], dtype=np.float32)
        assert np.all(np.abs(data[-1, -20:] - lastcoors) < 0.001), 'Coordinates calculation is broken'
Exemple #50
0
def _processSimOld(obj, i, updList, uniqueMol, uqMol, skip, deleteSims, metrics, ref, fstep):
    pieces = updList[i].trajectory
    try:
        if uniqueMol:
            mol = uqMol.copy()
        else:
            mol = Molecule(updList[i].molfile)
        logger.debug(pieces[0])
        mol._readTraj(pieces, skip=skip)
    except Exception as e:
        logger.warning('Error in simulation with id: ' + str(updList[i].simid) + ' ' + e.__str__())
        #deleteSims[i] = True
        return None, None, None, True, i
    #fstep[i] = mol.fstep
    #metrics[i] = obj._processTraj(mol)
    #ref[i] = obj._calcRef(pieces, mol.fileloc)
    return obj._processTraj(mol), obj._calcRef(pieces, mol.fileloc), mol.fstep, False, i
Exemple #51
0
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

    mol = Molecule(currSim.molfile)  # Always read the mol file, otherwise it does not work if we need to save a PDB as coorname
    mol.read(traj)
    mol.dropFrames(keep=frameNum)  # Making sure only specific frame to write is kept
    mol.write(path.join(newDir, coorname))
Exemple #52
0
    def _viewStatesNGL(self, states, statetype, protein, ligand, mols, numsamples):
        if states is None:
            states = range(self.macronum)
        if isinstance(states, int):
            states = [states]
        if mols is None:
            mols = self.getStates(states, statetype, numsamples=min(numsamples, 15))
        colors = [0, 1, 3, 4, 5, 6, 7, 9]
        if protein is None and ligand is None:
            raise NameError('Please provide either the "protein" or "ligand" parameter for viewStates.')
        if protein:
            mol = Molecule()
        if ligand:
            mol = mols[0].copy()
            mol.remove(ligand, _logger=False)
            mol.coords = np.atleast_3d(mol.coords[:, :, 0])
            mol.reps.add(sel='protein', style='NewCartoon', color='Secondary Structure')
        for i, s in enumerate(states):
            if protein:
                mol.reps.add(sel='segid ST{}'.format(s), style='NewCartoon', color='Index')
            if ligand:
                mol.reps.add(sel='segid ST{}'.format(s), style='Licorice', color=colors[np.mod(i, len(colors))])
                mols[i].filter(ligand, _logger=False)

            mols[i].set('segid', 'ST{}'.format(s))
            tmpcoo = mols[i].coords
            for j in range(mols[i].numFrames):
                mols[i].coords = np.atleast_3d(tmpcoo[:, :, j])
                mol.append(mols[i])

        w = mol.view(viewer='ngl')
        self._nglButtons(w, statetype, states)
        return w
Exemple #53
0
    def testWithProteinPrepare(self):
        from htmd.builder.preparation import proteinPrepare
        from htmd.builder.solvate import solvate
        # Test with proteinPrepare
        pdbids = ['3PTB']
        # pdbids = ['3PTB', '1A25', '1GZM']  # '1U5U' out because it has AR0 (no parameters)
        for pid in pdbids:
            np.random.seed(1)
            mol = Molecule(pid)
            mol.filter('protein')
            mol = proteinPrepare(mol)
            mol.filter('protein')  # Fix for bad proteinPrepare hydrogen placing
            smol = solvate(mol)
            ffs = defaultFf()
            tmpdir = os.path.join(self.testDir, 'withProtPrep', pid)
            _ = build(smol, ff=ffs, outdir=tmpdir)

            refdir = home(dataDir=join('test-amber-build', 'pp', pid))
            TestAmberBuild._compareResultFolders(refdir, tmpdir, pid)
Exemple #54
0
    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
Exemple #55
0
def _pointsOnSphere(radius, numsamples=1000):
    pointcoords = np.zeros((numsamples, 3))
    for i in range(numsamples):
        point = Molecule()
        point.empty(1)
        point.moveBy([0, 0, radius])
        point.rotateBy(uniformRandomRotation())
        pointcoords[i, :] = np.squeeze(point.coords)

    return pointcoords
Exemple #56
0
def tileMembrane(memb, xmin, ymin, xmax, ymax):
    """ Tile the membrane in the X and Y dimensions to reach a specific size.
    Returns
    -------
    megamemb :
        A big membrane Molecule
    """
    from htmd.progress.progress import ProgressBar
    memb = memb.copy()
    memb.resid = sequenceID(memb.resid)

    minmemb = np.min(memb.get('coords', 'water'), axis=0).flatten()

    size = np.max(memb.get('coords', 'water'), axis=0) - np.min(memb.get('coords', 'water'), axis=0)
    size = size.flatten()
    xreps = int(np.ceil((xmax - xmin) / size[0]))
    yreps = int(np.ceil((ymax - ymin) / size[1]))

    logger.info('Replicating Membrane {}x{}'.format(xreps, yreps))

    from htmd.molecule.molecule import Molecule
    megamemb = Molecule()
    bar = ProgressBar(xreps * yreps, description='Replicating Membrane')
    k = 0
    for x in range(xreps):
        for y in range(yreps):
            tmpmemb = memb.copy()
            xpos = xmin + x * size[0]
            ypos = ymin + y * size[1]

            tmpmemb.moveBy([-float(minmemb[0]) + xpos, -float(minmemb[1]) + ypos, 0])
            sel = 'same resid as (x > {} or y > {})'.format(xmax, ymax)
            tmpmemb.remove(sel, _logger=False)
            tmpmemb.set('segid', 'M{}'.format(k))

            megamemb.append(tmpmemb)
            k += 1
            bar.progress()
    bar.stop()
    return megamemb
Exemple #57
0
def _filtSim(i, sims, outFolder, filterSel):
    name = _simName(sims[i].trajectory[0])
    directory = path.join(outFolder, name)
    if not path.exists(directory):
        makedirs(directory)

    logger.debug('Processing trajectory ' + name)

    fmolfile = path.join(outFolder, 'filtered.pdb')
    (traj, outtraj) = _renameSims(sims[i].trajectory, name, outFolder)
    if not traj:
        ftrajectory = _listXTCs(path.join(outFolder, name))
        return Sim(simid=sims[i].simid, parent=sims[i], input=None, trajectory=ftrajectory, molfile=fmolfile)

    try:
        mol = Molecule(sims[i].molfile)
    except:
        logger.warning('Error! Skipping simulation ' + name)
        return

    sel = mol.atomselect(filterSel)

    for j in range(0, len(traj)):
        try:
            mol.read(traj[j])
        except IOError as e:
            logger.warning(e.strerror + ', skipping trajectory')
            break

        mol.write(outtraj[j], sel)

    ftrajectory = _listXTCs(path.join(outFolder, name))
    #bar.progress()
    return Sim(simid=sims[i].simid, parent=sims[i], input=None, trajectory=ftrajectory, molfile=fmolfile)
Exemple #58
0
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))
Exemple #59
0
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._readTraj(pieces, skip=skip)

        data = []
        for p in projectionlist:
            pj=p.project(mol)
            if pj.ndim==1:
                pj=np.atleast_2d(pj).T
            data.append(pj)
        data = np.hstack(data)
    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