def test_water256_periodic(self):
        nonbondedMethod = app.PME
        expected_energy = -2270.88890
        pdb = app.PDBFile("pdb_files/water256_integration_test.pdb")
        forcefield = app.ForceField("../mbpol.xml")

        nonbondedCutoff = 0.9 * unit.nanometer

        if (nonbondedMethod == app.PME):
            boxDimension = 19.3996888399961804 / 10.
            boxsize = [boxDimension, boxDimension, boxDimension]
            pdb.topology.setUnitCellDimensions(boxsize)

        system = forcefield.createSystem(pdb.topology,
                                         nonbondedMethod=nonbondedMethod,
                                         nonbondedCutoff=nonbondedCutoff)

        integrator = mm.LangevinIntegrator(0.0, 0.1, 0.01)
        platform = mm.Platform.getPlatformByName('Reference')
        simulation = app.Simulation(pdb.topology, system, integrator, platform)
        simulation.context.setPositions(pdb.positions)
        simulation.context.computeVirtualSites()
        state = simulation.context.getState(getForces=True,
                                            getEnergy=True,
                                            getPositions=True)
        potential_energy = state.getPotentialEnergy()
        potential_energy.in_units_of(unit.kilocalorie_per_mole)

        self.assertTrue(
            abs(
                potential_energy.in_units_of(unit.kilocalorie_per_mole)._value
                - expected_energy) < 20)
Esempio n. 2
0
    def build_engine(self):
        """Builds the OpenMMEngine."""

        from simtk.openmm import app
        import simtk.openmm as mm
        from simtk import unit
        kinase = self.kinase

        #### SET UP THE OPENMM STUFF #######################################
        # taken from builder.openmm.org
        forcefield = app.ForceField('amber99sbildn.xml', 'amber99_obc.xml')

        system = forcefield.createSystem(app.PDBFile(kinase['pdb']).topology,
                                         nonbondedMethod=app.NoCutoff,
                                         constraints=app.HBonds)

        integrator = mm.LangevinIntegrator(300 * unit.kelvin,
                                           1.0 / unit.picoseconds,
                                           2.0 * unit.femtoseconds)
        integrator.setConstraintTolerance(0.00001)

        #### OPENPATHSAMPLING-SPECIFIC SETUP ###############################
        options = {'nsteps_per_frame': 50, 'n_frames_max': 1500}
        template = omm_eng.tools.snapshot_from_pdb(kinase['pdb'])

        engine = omm_eng.Engine(template=template,
                                system=system,
                                integrator=integrator,
                                options=options)

        return engine
Esempio n. 3
0
def clean_protein(
    prot: protein.Protein,
    checks: bool = True):
  """Adds missing atoms to Protein instance.

  Args:
    prot: A `protein.Protein` instance.
    checks: A `bool` specifying whether to add additional checks to the cleaning
      process.

  Returns:
    pdb_string: A string of the cleaned protein.
  """
  _check_atom_mask_is_ideal(prot)

  # Clean pdb.
  prot_pdb_string = protein.to_pdb(prot)
  pdb_file = io.StringIO(prot_pdb_string)
  alterations_info = {}
  fixed_pdb = cleanup.fix_pdb(pdb_file, alterations_info)
  fixed_pdb_file = io.StringIO(fixed_pdb)
  pdb_structure = PdbStructure(fixed_pdb_file)
  cleanup.clean_structure(pdb_structure, alterations_info)

  logger.info("alterations info: %s", alterations_info)

  # Write pdb file of cleaned structure.
  as_file = openmm_app.PDBFile(pdb_structure)
  pdb_string = _get_pdb_string(as_file.getTopology(), as_file.getPositions())
  if checks:
    _check_cleaned_atoms(pdb_string, prot_pdb_string)
  return pdb_string
Esempio n. 4
0
 def __init__(self, conf):
     self.diag = []
     self.off_diag = []
     self.V = []
     for d in conf["diag"]:
         pdbname, xmlname = d["topology"], d["parameter"]
         self.V.append(d["V"])
         ff = app.ForceField(xmlname)
         pdb = app.PDBFile(pdbname)
         system = ff.createSystem(pdb.topology,
                                  nonbondedMethod=app.NoCutoff,
                                  polarization='mutual',
                                  mutualInducedTargetEpsilon=0.00001,
                                  removeCMMotion=False)
         integrator = mm.LangevinIntegrator(195 * unit.kelvin,
                                            1 / unit.picosecond,
                                            0.0005 * unit.picoseconds)
         if "platform" in conf:
             context = mm.Context(
                 system, integrator,
                 mm.Platform.getPlatformByName(conf["platform"].upper()))
         else:
             context = mm.Context(system, integrator)
         self.diag.append(context)
     for offd in conf["off_diag"]:
         self.off_diag.append(offd)
     self.emat = np.zeros((len(self.diag), len(self.diag)))
Esempio n. 5
0
def build_protein_system(host_pdbfile):

    host_ff = app.ForceField("amber99sbildn.xml", "tip3p.xml")
    host_pdb = app.PDBFile(host_pdbfile)

    modeller = app.Modeller(host_pdb.topology, host_pdb.positions)
    host_coords = strip_units(host_pdb.positions)

    padding = 1.0
    box_lengths = np.amax(host_coords, axis=0) - np.amin(host_coords, axis=0)
    box_lengths = box_lengths.value_in_unit_system(unit.md_unit_system)

    box_lengths = box_lengths + padding
    box = np.eye(3, dtype=np.float64) * box_lengths

    modeller.addSolvent(host_ff,
                        boxSize=np.diag(box) * unit.nanometers,
                        neutralize=False)
    solvated_host_coords = strip_units(modeller.positions)

    nha = host_coords.shape[0]
    nwa = solvated_host_coords.shape[0] - nha

    print("building a protein system with", nha, "protein atoms and", nwa,
          "water atoms")
    solvated_host_system = host_ff.createSystem(modeller.topology,
                                                nonbondedMethod=app.NoCutoff,
                                                constraints=None,
                                                rigidWater=False)

    return solvated_host_system, solvated_host_coords, nwa, nha, box, modeller.topology
Esempio n. 6
0
def convert_spce_to_cs(file, saveas="newpdb.pdb"):
    """Extract and rename oxygens from all-atom water structure"""

    import os
    import numpy as np

    import simtk.unit as unit
    import simtk.openmm as omm
    import simtk.openmm.app as app
    file = "equil_spc.pdb"
    saveas = "justO_spc.pdb"
    app.element.solvent = app.element.Element(201, "Solvent", "Sv",
                                              18 * unit.amu)

    pdb = app.PDBFile(file)
    O_idxs = np.array(
        [atm.index for atm in pdb.topology.atoms() if atm.name == "O"])

    xyz = np.array(pdb.positions / unit.angstrom)[O_idxs]
    positions = unit.Quantity(xyz, unit.angstrom)

    n_slv = len(O_idxs)

    topology = app.Topology()
    chain = topology.addChain()
    for i in range(n_slv):
        res = topology.addResidue("SLV", chain)
        topology.addAtom("CS", app.element.get_by_symbol("Sv"), res)

    box_edge = 5.96256971 * unit.nanometer
    topology.setUnitCellDimensions(box_edge * omm.Vec3(1, 1, 1))

    with open(saveas, "w") as fout:
        pdb.writeFile(topology, positions, file=fout)
Esempio n. 7
0
 def test_chemical_environments_matches_OE(self):
     """Test Topology.chemical_environment_matches"""
     from simtk.openmm import app
     toolkit_wrapper = OpenEyeToolkitWrapper()
     pdbfile = app.PDBFile(
         get_data_file_path(
             'systems/packmol_boxes/cyclohexane_ethanol_0.4_0.6.pdb'))
     # toolkit_wrapper = RDKitToolkitWrapper()
     molecules = [
         Molecule.from_file(get_data_file_path(name))
         for name in ('molecules/ethanol.mol2',
                      'molecules/cyclohexane.mol2')
     ]
     topology = Topology.from_openmm(pdbfile.topology,
                                     unique_molecules=molecules)
     # Test for substructure match
     matches = topology.chemical_environment_matches(
         "[C:1]-[C:2]-[O:3]", toolkit_registry=toolkit_wrapper)
     assert len(matches) == 143
     assert matches[0].topology_atom_indices == (1728, 1729, 1730)
     # Test for whole-molecule match
     matches = topology.chemical_environment_matches(
         "[H][C:1]([H])([H])-[C:2]([H])([H])-[O:3][H]",
         toolkit_registry=toolkit_wrapper)
     assert len(
         matches
     ) == 1716  # 143 * 12 (there are 12 possible hydrogen mappings)
     assert matches[0].topology_atom_indices == (1728, 1729, 1730)
     # Search for a substructure that isn't there
     matches = topology.chemical_environment_matches(
         "[C][C:1]-[C:2]-[O:3]", toolkit_registry=toolkit_wrapper)
     assert len(matches) == 0
Esempio n. 8
0
def test_residues():
    pdb = app.PDBFile(get_test_file_path("ALA_GLY/ALA_GLY.pdb"))
    traj = md.load(get_test_file_path("ALA_GLY/ALA_GLY.pdb"))
    mol = Molecule(get_test_file_path("ALA_GLY/ALA_GLY.sdf"),
                   file_format="sdf")

    top = OFFBioTop.from_openmm(pdb.topology, unique_molecules=[mol])
    top.mdtop = traj.top

    assert top.n_topology_atoms == 29
    assert top.mdtop.n_residues == 4
    assert [r.name for r in top.mdtop.residues] == ["ACE", "ALA", "GLY", "NME"]

    ff = ForceField("openff-1.3.0.offxml")
    off_sys = Interchange.from_smirnoff(ff, top)

    # Assign positions and box vectors in order to run MM
    off_sys.positions = pdb.positions
    off_sys.box = [4.8, 4.8, 4.8]

    # Just ensure that a single-point energy can be obtained without error
    get_openmm_energies(off_sys)

    assert len(top.mdtop.select("resname ALA")) == 10
    assert [*off_sys.topology.mdtop.residues][-1].n_atoms == 6
Esempio n. 9
0
    def addExtraMolecules_PDB(self, system, extra_input_pdb, input_path=None):
        
        if input_path == None:
            
            input_path=self.def_input_struct
            
        total_mols=[]
        
        for idx, pdb_e in enumerate(extra_input_pdb, 1):
            
            
            path_pdb=f'{self.def_input_struct}/{pdb_e}'

            if not os.path.exists(path_pdb):
                
                print(f'\tFile {path_pdb} not found!')
            else:
                print(f'\tAdding extra PDB file {idx} to pre-system: {path_pdb}')
                extra_pdb = app.PDBFile(path_pdb)
                       
            try:
                system.add(extra_pdb.topology, extra_pdb.positions)
                print(f'\t{extra_pdb.topology}')
            
                total_mols.append((idx, path_pdb))  
            
            except:
                
                print(f'\tMolecule {idx} ({extra_pdb}) could not be added by openMM')

        for mol in total_mols:
            
            print(f'\tAdded {mol[0]}: {mol[1]}')  
  
        return system, total_mols
Esempio n. 10
0
    def openmm_system(self):
        """Initialise the OpenMM system we will use to evaluate the energies."""

        # Load the initial coords into the system and initialise
        pdb = app.PDBFile(self.pdb)
        forcefield = app.ForceField(self.xml)
        modeller = app.Modeller(pdb.topology, pdb.positions)  # set the initial positions from the pdb
        self.system = forcefield.createSystem(modeller.topology, nonbondedMethod=app.NoCutoff, constraints=None)

        # Check what combination rule we should be using from the xml
        xmlstr = open(self.xml).read()
        # check if we have opls combination rules if the xml is present
        try:
            self.combination = ET.fromstring(xmlstr).find('NonbondedForce').attrib['combination']
        except AttributeError:
            pass
        except KeyError:
            pass

        if self.combination == 'opls':
            print('OPLS combination rules found in xml file')
            self.opls_lj()

        temperature = constants.STP * unit.kelvin
        integrator = mm.LangevinIntegrator(temperature, 5 / unit.picoseconds, 0.001 * unit.picoseconds)

        self.simulation = app.Simulation(modeller.topology, self.system, integrator)
        self.simulation.context.setPositions(modeller.positions)
Esempio n. 11
0
 def test_chemical_environments_matches_RDK(self):
     """Test Topology.chemical_environment_matches"""
     from simtk.openmm import app
     toolkit_wrapper = RDKitToolkitWrapper()
     pdbfile = app.PDBFile(
         get_data_file_path(
             'systems/packmol_boxes/cyclohexane_ethanol_0.4_0.6.pdb'))
     # toolkit_wrapper = RDKitToolkitWrapper()
     #molecules = [Molecule.from_file(get_data_file_path(name)) for name in ('molecules/ethanol.mol2',
     #                                                                      'molecules/cyclohexane.mol2')]
     molecules = []
     molecules.append(Molecule.from_smiles('CCO'))
     molecules.append(Molecule.from_smiles('C1CCCCC1'))
     topology = Topology.from_openmm(pdbfile.topology,
                                     unique_molecules=molecules)
     # Count CCO matches
     matches = topology.chemical_environment_matches(
         "[C:1]-[C:2]-[O:3]", toolkit_registry=toolkit_wrapper)
     assert len(matches) == 143
     assert tuple(i.topology_atom_index
                  for i in matches[0]) == (1728, 1729, 1730)
     matches = topology.chemical_environment_matches(
         "[H][C:1]([H])([H])-[C:2]([H])([H])-[O:3][H]",
         toolkit_registry=toolkit_wrapper)
     assert len(
         matches
     ) == 1716  # 143 * 12 (there are 12 possible hydrogen mappings)
     assert tuple(i.topology_atom_index
                  for i in matches[0]) == (1728, 1729, 1730)
     # Search for a substructure that isn't there
     matches = topology.chemical_environment_matches(
         "[C][C:1]-[C:2]-[O:3]", toolkit_registry=toolkit_wrapper)
     assert len(matches) == 0
def create_vdata_txt(pdb_fnm, data):
    # check gradient
    gradient = data['gradient']
    if np.abs(gradient).max() > 1e-3:
        print("Warning! Max gradient greater than 1e-3")
    # get the openmm list of masses for the molecule, to be consistent with ForceBalance
    pdb = app.PDBFile(pdb_fnm)
    mass_array = np.array([
        a.element.mass.value_in_unit(unit.dalton)
        for a in pdb.topology.atoms()
    ])
    # compute mass-weighted hessian
    invert_sqrt_mass_array_repeat = 1.0 / np.sqrt(
        mass_array.repeat(3))  # repeat for x, y, z coords
    hessian = data['hessian']
    mass_weighted_hessian = hessian * invert_sqrt_mass_array_repeat[:, np.
                                                                    newaxis] * invert_sqrt_mass_array_repeat[
                                                                        np.
                                                                        newaxis, :]
    mass_weighted_hessian *= 937583.07  # convert units from Eh / bohr^2 * dalton to 10^24 s^-2
    # perform normal mode analysis
    freqs, normal_modes = compute_normal_modes(mass_weighted_hessian)
    # write vdata.txt
    with open('vdata.txt', 'w') as outfile:
        outfile.write(data['molecule'].to_string('xyz') + '\n')
        for freq, normol_mode in zip(freqs, normal_modes):
            outfile.write(f'{freq}\n')
            for nx, ny, nz in normol_mode:
                outfile.write(f'{nx:13.4f} {ny:13.4f} {nz:13.4f}\n')
            outfile.write('\n')
Esempio n. 13
0
    def get_mixture_tesystem(packmol_boxname, monomers):
        """Retrieve test system information for specified packmol mixed-component box and monomers.

        Parameters
        ----------
        packmol_boxname : str
            Prefix of PDB file to be retrieved from systems/packmolboxes in test data directory
        monomers : list of str
            List of monomer names within packmol box

        Returns
        -------
        topology : openforcefield.topology.Topology
            Topology for the packmol box
        positions : simtk.unit.Quantity with dimension (nparticles,3) with units compatible with angstroms
            Positions corresponding to particles in ``topology``
        """
        pdbfile = app.PDBFile(get_packmol_pdb_file_path(packmol_boxname))
        molecules = [
            Molecule.from_file(get_monomer_mol2_file_path(name))
            for name in monomers
        ]
        topology = Topology.from_openmm(pdbfile.topology,
                                        unique_molecules=molecules)
        positions = pdbfile.positions
        return topology, positions
Esempio n. 14
0
    def production(self):  

        if os.path.exists(self.production_dcd_filename) and os.path.exists(self.production_data_filename):
            return

        prmtop = app.AmberPrmtopFile(self.prmtop_filename)
        pdb = app.PDBFile(self.equil_pdb_filename)

        system = prmtop.createSystem(nonbondedMethod=app.PME, nonbondedCutoff=CUTOFF, constraints=app.HBonds)

        integrator = mm.LangevinIntegrator(self.temperature, FRICTION, TIMESTEP)
        system.addForce(mm.MonteCarloBarostat(PRESSURE, self.temperature, BAROSTAT_FREQUENCY))

        simulation = app.Simulation(prmtop.topology, system, integrator)
        
        simulation.context.setPositions(pdb.positions)
        simulation.context.setPeriodicBoxVectors(*pdb.topology.getPeriodicBoxVectors())
        simulation.context.setVelocitiesToTemperature(self.temperature)
        
        print('Production.')
        
        simulation.reporters.append(app.DCDReporter(self.production_dcd_filename, OUTPUT_FREQUENCY))
        simulation.reporters.append(app.StateDataReporter(self.production_data_filename, OUTPUT_DATA_FREQUENCY, step=True, potentialEnergy=True, temperature=True, density=True))

        converged = False
        while not converged:
            simulation.step(N_STEPS)
            d = pd.read_csv(self.production_data_filename, names=["step", "U", "Temperature", "Density"], skiprows=1)
            density_ts = np.array(d.Density)
            [t0, g, Neff] = ts.detectEquilibration(density_ts, nskip=1000)
            density_ts = density_ts[t0:]
            density_mean_stderr = density_ts.std() / np.sqrt(Neff)
            if density_mean_stderr < STD_ERROR_TOLERANCE:
                converged = True
def makeLib(file_path, residue_name, connect0=None, connect1=None, charges='bcc', atom_type='gaff', force_field='leaprc.ff12SB', parameterized=False):
	name, extension = file_path.split('/')[-1].split(".")
	lib_path = '/'.join(file_path.split('/')[:-1]+[residue_name])
	tleap_input = """
	source %s
	source leaprc.gaff
	%s = loadmol2 %s.mol2
	loadamberparams %s.frcmod"""%(force_field,
		 residue_name, name, 
		 lib_path) 
        if connect0 and connect1:
        	tleap_input += """
		set %s head %s.1.%s
		set %s tail %s.1.%s
		set %s.1 connect0 %s.head
		set %s.1 connect1 %s.tail"""%(residue_name, residue_name, connect0, 
						residue_name, residue_name, connect1, 
						residue_name, residue_name, 
						residue_name, residue_name)
	tleap_input +="""
	check %s
        saveoff %s %s.lib
	savepdb %s %s_tmp.pdb
	quit
	"""%(residue_name, residue_name, lib_path, residue_name, lib_path)
	if parameterized:
		tleap_input = """
		source leaprc.gaff
		source %s
		%s = loadpdb %s.pdb
		saveoff %s %s.lib
		savepdb %s %s_tmp.pdb
		quit
		"""%(force_field, residue_name, name, residue_name, lib_path,
			residue_name, lib_path)
	#Write LEaP infile
	with open("%s.in"%name,"w") as fil:
		fil.write(tleap_input)
	if not parameterized:
		#Execute
		subprocess.call("antechamber -i %s -fi %s -o %s.mol2 -fo mol2 -c %s -rn %s -at %s"%(file_path,
																					   extension, 
																					   name, 
																					   charges, 
																					   residue_name, 
																					   atom_type), 
						shell=True)
		subprocess.call("parmchk -i %s.mol2 -f mol2 -o %s.frcmod"%(name, lib_path), shell=True)
	subprocess.call("tleap -f %s.in"%name, shell=True)
	PDB = app.PDBFile(lib_path + "_tmp.pdb")
	length = sum([1 for atom in PDB.topology.atoms()])
	#Cleanup
	if not parameterized:
		os.remove(name+".mol2")
	#os.remove(name+".frcmod")
	os.remove("%s.in"%name)
	os.remove("%s_tmp.pdb"%(lib_path))
	os.remove("leap.log")
	return length
Esempio n. 16
0
    def __init__(self, true_value=None, initial_value=None, n_increments=18, rj=True, sample_phase=False,
                 continuous=False):
        self._param = CharmmParameterSet(get_fun('toy.str'))
        self._struct = CharmmPsfFile(get_fun('toy.psf'))
        self._pdb = app.PDBFile(get_fun('toy.pdb'))
        self._topology = md.load_psf(get_fun('toy.psf'))
        self.synthetic_energy = units.Quantity()
        self._positions = units.Quantity()
        self._platform = mm.Platform.getPlatformByName('Reference')

        # Replace ('CG331', 'CG321', 'CG321', 'CG331') torsion with true_value
        self._dih_type = ('CG331', 'CG321', 'CG321', 'CG331')
        original_torsion = self._param.dihedral_types[self._dih_type]
        if true_value is not None:
            if type(true_value) == DihedralTypeList:
                dih_tlist = true_value
            elif type(true_value) == DihedralType:
                dih_tlist = DihedralTypeList()
                dih_tlist.append(true_value)
        else:
            dih_tlist = self._randomize_dih_param(return_dih=True)
        self.true_value = copy.deepcopy(dih_tlist)
        self._param.dihedral_types[self._dih_type] = dih_tlist

        # parametrize toy
        self._struct.load_parameters(self._param, copy_parameters=False)
        self._struct.positions = self._pdb.positions

        # generate synthetic torsion scan
        self._torsion_scan(n_increments=n_increments)

        # initialize parameter
        if initial_value is not None:
            if type(initial_value) == DihedralTypeList:
                dih_tlist = initial_value
            if type(initial_value) == DihedralType:
                dih_tlist = DihedralTypeList()
                dih_tlist.append(initial_value)
            elif initial_value == 'cgenff':
                dih_tlist = original_torsion
        else:
            dih_tlist = self._randomize_dih_param(return_dih=True)

        self.initial_value = copy.deepcopy(dih_tlist)
        self._param.dihedral_types[self._dih_type] = dih_tlist

        # create torsionfit.TorsionScanSet
        torsions = np.zeros((len(self._positions), 4))
        torsions[:] = [1, 2, 3, 4]
        direction = None
        steps = None
        self.scan_set = ScanSet.QMDataBase(positions=self._positions.value_in_unit(units.nanometers),
                                           topology=self._topology, structure=self._struct, torsions=torsions,
                                           steps=steps, directions=direction,
                                           qm_energies=self.synthetic_energy.value_in_unit(units.kilojoules_per_mole))

        self.model = model.TorsionFitModel(param=self._param, frags=self.scan_set, platform=self._platform,
                                           param_to_opt=[self._dih_type], rj=rj, continuous_phase=continuous,
                                           sample_phase=sample_phase)
Esempio n. 17
0
def main():
  print("Reading the PSF file");

  # Read the PSF file
  psf = app.CharmmPsfFile('g1_25mm.psf');

  boxsize = 5 # Boxsize in nm
  psf.setBox(boxsize*nanometer, boxsize*nanometer, boxsize*nanometer);

  print("Reading the pdb file")
  pdb = app.PDBFile('g1_25mm.pdb');

  # Load the parameter set
  lib = 'toppar/'
  #params = app.CharmmParameterSet('toppar/top_all36_cgenff.rtf','toppar/par_all36_cgenff.prm','toppar/cgenff3.0.1/top_all36_cgenff.rtf','toppar/cgenff3.0.1/par_all36_cgenff.prm','g1_new.str','toppar_water_ions.str')
  params = app.CharmmParameterSet('toppar/cgenff3.0.1/top_all36_cgenff.rtf','toppar/cgenff3.0.1/par_all36_cgenff.prm','g1_new.str','toppar_water_ions.str')


  #platform = openmm.Platform.getPlatformByName('CUDA');
  platform = openmm.Platform.getPlatformByName('Reference');

  # Creating the system
  system = psf.createSystem(params, 
                            nonbondedMethod=app.PME, 
                            nonbondedCutoff=1.2*nanometer, 
                            switchDistance=1.0*nanometer, 
                            ewaldErrorTolerance= 0.0001, 
                            constraints=app.HBonds);

  # Thermostat @ 298 K
  system.addForce(openmm.AndersenThermostat(298*kelvin, 1/picosecond)) 
  # adding the barostat for now
  system.addForce(openmm.MonteCarloBarostat(1*bar, 298*kelvin));

  integrator = openmm.VerletIntegrator(0.001*picoseconds)

  simulation = app.Simulation(psf.topology, system, integrator, platform)
  simulation.context.setPositions(pdb.getPositions())
  simulation.minimizeEnergy(maxIterations = 500)

  #nsavcrd = 10000 # save coordinates every 10 ps
  #nstep  = 2000000 # write dcd files every 2 ps
  #nprint = 2000 # report every 2 ps
  nsavcrd = 10 # save coordinates every 10 ps
  nstep  = 200 # write dcd files every 2 ps
  nprint = 20 # report every 2 ps
  firstDcdStep = nsavcrd ;


  # Reporters
  dcd = app.DCDReporter('g1_new.dcd', nsavcrd)
  dcd._dcd = app.DCDFile(dcd._out, simulation.topology, simulation.integrator.getStepSize(), firstDcdStep, nsavcrd)
  simulation.reporters.append(dcd)

  simulation.reporters.append(app.StateDataReporter('g1_new.out', nprint, step=True, kineticEnergy=True, potentialEnergy=True, totalEnergy=True, temperature=True, volume=True, speed=True))
  simulation.step(nstep)
  simulation.reporters.pop()
  simulation.reporters.pop()
  dcd._out.close()
Esempio n. 18
0
def simulate(pdbcode, pdb_filename):
    from simtk.openmm import app
    import simtk.openmm as mm
    from simtk import unit
    from sys import stdout

    # Load the PDB file.
    pdb = app.PDBFile(pdb_filename)

    # Set up implicit solvent forcefield.
    #forcefield = app.ForceField('amber99sbildn.xml')
    forcefield = app.ForceField('amber10.xml')

    # Create the system.
    system = forcefield.createSystem(pdb.topology,
                                     nonbondedMethod=app.NoCutoff,
                                     constraints=app.HBonds)

    # Create an integrator.
    integrator = mm.LangevinIntegrator(300 * unit.kelvin,
                                       91.0 / unit.picoseconds,
                                       1.0 * unit.femtoseconds)

    # Create a context.
    context = mm.Context(system, integrator)
    context.setPositions(pdb.positions)

    # Check to make sure energy is finite.
    state = context.getState(getEnergy=True)
    potential = state.getPotentialEnergy() / unit.kilocalories_per_mole
    if numpy.isnan(potential):
        raise Exception("Initial energy for %s is NaN." % pdbcode)

    # Minimize.
    tolerance = 1.0 * unit.kilocalories_per_mole / unit.angstroms
    maxIterations = 50
    mm.LocalEnergyMinimizer.minimize(context, tolerance, maxIterations)

    # Check to make sure energy is finite.
    state = context.getState(getEnergy=True)
    potential = state.getPotentialEnergy() / unit.kilocalories_per_mole
    if numpy.isnan(potential):
        raise Exception("Energy for %s is NaN after minimization." % pdbcode)

    # Simulate.
    nsteps = 500
    integrator.step(nsteps)

    # Check to make sure energy is finite.
    state = context.getState(getEnergy=True)
    potential = state.getPotentialEnergy() / unit.kilocalories_per_mole
    if numpy.isnan(potential):
        raise Exception("Energy for %s is NaN after simulation." % pdbcode)

    del context, integrator

    print("Simulation completed: potential = %.3f kcal/mol" % potential)

    return
Esempio n. 19
0
def _check_cleaned_atoms(pdb_cleaned_string: str, pdb_ref_string: str):
  """Checks that no atom positions have been altered by cleaning."""
  cleaned = openmm_app.PDBFile(io.StringIO(pdb_cleaned_string))
  reference = openmm_app.PDBFile(io.StringIO(pdb_ref_string))

  cl_xyz = np.array(cleaned.getPositions().value_in_unit(LENGTH))
  ref_xyz = np.array(reference.getPositions().value_in_unit(LENGTH))

  for ref_res, cl_res in zip(reference.topology.residues(),
                             cleaned.topology.residues()):
    assert ref_res.name == cl_res.name
    for rat in ref_res.atoms():
      for cat in cl_res.atoms():
        if cat.name == rat.name:
          if not np.array_equal(cl_xyz[cat.index], ref_xyz[rat.index]):
            raise ValueError(f"Coordinates of cleaned atom {cat} do not match "
                             f"coordinates of reference atom {rat}.")
Esempio n. 20
0
    def _execute(self, directory, available_resources):

        from openforcefield.topology import Molecule, Topology

        pdb_file = app.PDBFile(self.coordinate_file_path)

        force_field_source = ForceFieldSource.from_json(self.force_field_path)

        if not isinstance(force_field_source, SmirnoffForceFieldSource):
            raise ValueError(
                "Only SMIRNOFF force fields are supported by this protocol.")

        force_field = force_field_source.to_force_field()

        unique_molecules = []
        charged_molecules = []

        if self.apply_known_charges:
            charged_molecules = self._generate_known_charged_molecules()

        # Load in any additional, user specified charged molecules.
        for charged_molecule_path in self.charged_molecule_paths:

            charged_molecule = Molecule.from_file(charged_molecule_path,
                                                  "MOL2")
            charged_molecules.append(charged_molecule)

        for component in self.substance.components:

            molecule = Molecule.from_smiles(smiles=component.smiles)

            if molecule is None:
                raise ValueError(
                    f"{component} could not be converted to a Molecule")

            unique_molecules.append(molecule)

        topology = Topology.from_openmm(pdb_file.topology,
                                        unique_molecules=unique_molecules)

        if len(charged_molecules) > 0:
            system = force_field.create_openmm_system(
                topology, charge_from_molecules=charged_molecules)
        else:
            system = force_field.create_openmm_system(topology)

        if system is None:

            raise RuntimeError(
                "Failed to create a system from the specified topology and molecules."
            )

        system_xml = openmm.XmlSerializer.serialize(system)
        self.system_path = os.path.join(directory, "system.xml")

        with open(self.system_path, "w") as file:
            file.write(system_xml)
Esempio n. 21
0
def load_amoeba(hydrogenMass=1.0):
    pdb = app.PDBFile("./sandbox/iamoeba.pdb")
    forcefield = app.ForceField('iamoeba.xml')
    system = forcefield.createSystem(pdb.topology,
                                     nonbondedMethod=app.PME,
                                     nonbondedCutoff=1.0 * u.nanometers,
                                     hydrogenMass=hydrogenMass * u.amu,
                                     rigidWater=False)
    return system, pdb.positions
    def equilibrate(self, ff_name, water_name):

        input_pdb_filename = self.get_initial_pdb_filename(ff_name, water_name)
        equil_pdb_filename = self.get_equil_pdb_filename(ff_name, water_name)
        equil_dcd_filename = self.get_equil_dcd_filename(ff_name, water_name)
        equil_protein_pdb_filename = self.get_equil_protein_pdb_filename(
            ff_name, water_name)

        utils.make_path(equil_pdb_filename)

        if os.path.exists(equil_pdb_filename):
            return

        ff = app.ForceField('%s.xml' % ff_name, '%s.xml' % water_name)
        pdb = app.PDBFile(input_pdb_filename)
        modeller = app.Modeller(pdb.topology, pdb.positions)
        modeller.addSolvent(ff,
                            model=water_mapping[water_name],
                            padding=self.padding,
                            ionicStrength=self.ionic_strength)
        topology = modeller.getTopology()
        positions = modeller.getPositions()

        system = ff.createSystem(topology,
                                 nonbondedMethod=app.PME,
                                 nonbondedCutoff=self.cutoff,
                                 constraints=app.HBonds)
        integrator = mm.LangevinIntegrator(self.temperature,
                                           self.equil_friction,
                                           self.equil_timestep)
        system.addForce(
            mm.MonteCarloBarostat(self.pressure, self.temperature,
                                  self.barostat_frequency))

        simulation = app.Simulation(topology, system, integrator)
        simulation.context.setPositions(positions)

        print('Minimizing.')
        simulation.minimizeEnergy()

        simulation.context.setVelocitiesToTemperature(self.temperature)
        print('Equilibrating.')

        simulation.reporters.append(
            app.PDBReporter(equil_pdb_filename, self.n_equil_steps - 1))
        simulation.reporters.append(
            app.DCDReporter(equil_dcd_filename, self.equil_output_frequency))
        simulation.step(self.n_equil_steps)
        del simulation
        del system
        traj = md.load(equil_dcd_filename, top=equil_pdb_filename)[-1]
        traj.save(equil_pdb_filename)

        top, bonds = traj.top.to_dataframe()
        atom_indices = top.index[top.chainID == 0].values
        traj.restrict_atoms(atom_indices)
        traj.save(equil_protein_pdb_filename)
Esempio n. 23
0
    def convert_input(self):
        """
        Converts inputs to OpenMM readable topologies and positions.
        Currently supports pdb inputs as well as Amber and Gromacs input files.
        """

        if self.system_info_format == 'pdb':
            # instantiate OpenMM pdb object
            if type(self.system_info) is list:
                self.pdb = OM_app.PDBFile(self.system_info[0])
            elif type(self.system_info) is str:
                self.pdb = OM_app.PDBFile(self.system_info)
            # instantiate OpenMM forcefield object
            self.forcefield = OM_app.ForceField(self.ff, self.ff_water)
            self.topology = self.pdb.topology
            self.positions = self.pdb.positions
            self.PeriodicBoxVector = self.topology.getPeriodicBoxVectors()

        elif self.system_info_format in ['Amber', 'amber']:
            for fil in self.system_info:
                if fil.endswith('prmtop'):
                    self.forcefield = OM_app.AmberPrmtopFile(fil)
                    self.topology = self.forcefield.topology
                    self.use_pdb = False
                if fil.endswith('pdb'):
                    self.pdb = OM_app.PDBFile(fil)
                    self.forcefield = OM_app.ForceField(self.ff, self.ff_water)
                    self.topology = self.pdb.topology
                    self.use_pdb = True
                if fil.endswith('inpcrd'):
                    self.inpcrd = OM_app.AmberInpcrdFile(fil)
                    self.positions = self.inpcrd.positions
                    self.PeriodicBoxVector = self.inpcrd.boxVectors

        elif self.system_info_format in ['Gromacs', 'gromacs']:
            for fil in self.system_info:
                if 'gro' in fil:
                    self.pdb = OM_app.GromacsGroFile(fil)
            for fil in self.system_info:
                if 'top' in fil:
                    self.forcefield = OM_app.GromacsTopFile(
                        fil,
                        periodicBoxVectors=self.pdb.getPeriodicBoxVectors())
                    self.topology = self.forcefield.topology
Esempio n. 24
0
 def testLongTrajectory(self):
     """Test writing a trajectory that has more than 2^31 steps."""
     fname = tempfile.mktemp(suffix='.dcd')
     pdbfile = app.PDBFile('systems/alanine-dipeptide-implicit.pdb')
     natom = len(list(pdbfile.topology.atoms()))
     with open(fname, 'wb') as f:
         dcd = app.DCDFile(f, pdbfile.topology, 0.001, interval=1000000000)
         for i in range(5):
             dcd.writeModel([mm.Vec3(random(), random(), random()) for j in range(natom)]*unit.angstroms)
     os.remove(fname)
Esempio n. 25
0
 def write_header(self):
     """
     Confusingly this initializes writer as well because 
     """
     outfile = open(self.out_filepath, 'w')
     self.outfile = outfile
     cpdb = app.PDBFile(self.pdb_str)
     PDBFile.writeHeader(cpdb.topology, self.outfile)
     self.topology = cpdb.topology
     self.frame_idx = 0
Esempio n. 26
0
 def test_dcd(self):
     """ Test the DCD file """
     fname = tempfile.mktemp(suffix='.dcd')
     pdbfile = app.PDBFile('systems/alanine-dipeptide-implicit.pdb')
     natom = len(list(pdbfile.topology.atoms()))
     with open(fname, 'wb') as f:
         dcd = app.DCDFile(f, pdbfile.topology, 0.001)
         for i in range(5):
             dcd.writeModel([mm.Vec3(random(), random(), random()) for j in range(natom)]*unit.angstroms)
     os.remove(fname)
    def run(self,
            production_steps=200,
            start='ala2_1stFrame.pdb',
            production='ala2_production.pdb'):  #### ?!!!!!!!!!!!!!!!!
        #from __future__ import print_function
        from simtk.openmm import app
        import simtk.openmm as mm
        from simtk import unit
        from sys import stdout

        nonbondedCutoff = 1.0 * unit.nanometers
        timestep = 2.0 * unit.femtoseconds
        temperature = 300 * unit.kelvin
        #save_frequency = 100 #Every 100 steps, save the trajectory
        save_frequency = 10  #Every 1 steps, save the trajectory

        pdb = app.PDBFile(start)
        forcefield = app.ForceField('amber99sb.xml', 'tip3p.xml')
        ala2_model = app.Modeller(pdb.topology, pdb.positions)

        #Hydrogens will be constrained after equilibrating
        system = forcefield.createSystem(ala2_model.topology,
                                         nonbondedMethod=app.PME,
                                         nonbondedCutoff=nonbondedCutoff,
                                         constraints=app.HBonds,
                                         rigidWater=True,
                                         ewaldErrorTolerance=0.0005)

        system.addForce(mm.MonteCarloBarostat(
            1 * unit.bar, temperature,
            100))  #Apply Monte Carlo Pressure changes in 100 timesteps
        integrator = mm.LangevinIntegrator(temperature, 1.0 / unit.picoseconds,
                                           timestep)
        integrator.setConstraintTolerance(0.00001)

        platform = mm.Platform.getPlatformByName('CPU')
        simulation = app.Simulation(ala2_model.topology, system, integrator,
                                    platform)
        simulation.context.setPositions(ala2_model.positions)

        simulation.context.setVelocitiesToTemperature(temperature)

        simulation.reporters.append(app.PDBReporter(production,
                                                    save_frequency))
        #simulation.reporters.append(app.StateDataReporter('stateReporter_constantPressure.txt', 1000, step=True,
        #   totalEnergy=True, temperature=True, volume=True, progress=True, remainingTime=True,
        #    speed=True, totalSteps=production_steps, separator='\t'))

        print('Running Production...')
        simulation.step(production_steps)
        print('Done!')

        import mdtraj as md
        trj = md.load(production)
        return trj
def load_receptor_pdb(pdb_filename):
    """
    Load the receptor by PDB filename.

    Parameters
    ----------
    pdb_filename
    """
    f = open(pdb_filename, 'r')
    pdbfile = app.PDBFile(f)
    return pdbfile.topology, pdbfile.positions
Esempio n. 29
0
def make_xml_explicit_imidazole(
    pdb_filename="protons/examples/Ligand example/imidazole.pdb",
    ffxml_filename="protons/examples/Ligand example/imidazole.xml",
    outfile="imidazole-explicit",
):
    """Solvate an imidazole pdb file and minimize the system"""

    temperature = 300.0 * unit.kelvin
    pressure = 1.0 * unit.atmospheres
    outfile1 = open("{}.sys.xml".format(outfile), "w")
    outfile2 = open("{}.state.xml".format(outfile), "w")
    gaff = get_data("gaff.xml", "forcefields")
    pdb = app.PDBFile(pdb_filename)
    forcefield = app.ForceField(gaff, ffxml_filename, "amber99sbildn.xml",
                                "tip3p.xml")
    integrator = openmm.LangevinIntegrator(300 * unit.kelvin,
                                           1.0 / unit.picoseconds,
                                           2.0 * unit.femtoseconds)

    integrator.setConstraintTolerance(0.00001)

    modeller = app.Modeller(pdb.topology, pdb.positions)
    modeller.addSolvent(
        forcefield,
        boxSize=openmm.Vec3(3.5, 3.5, 3.5) * unit.nanometers,
        model="tip3p",
        ionicStrength=0.1 * unit.molar,
        positiveIon="Na+",
        negativeIon="Cl-",
    )
    system = forcefield.createSystem(
        modeller.topology,
        nonbondedMethod=app.PME,
        nonbondedCutoff=1.0 * unit.nanometers,
        constraints=app.HBonds,
        rigidWater=True,
        ewaldErrorTolerance=0.0005,
    )
    system.addForce(openmm.MonteCarloBarostat(pressure, temperature))

    simulation = app.Simulation(modeller.topology, system, integrator)
    simulation.context.setPositions(modeller.positions)

    simulation.minimizeEnergy()

    outfile1.write(openmm.XmlSerializer.serialize(simulation.system))
    positions = simulation.context.getState(getPositions=True)
    outfile2.write(openmm.XmlSerializer.serialize(positions))
    app.PDBFile.writeFile(
        simulation.topology,
        modeller.positions,
        open("imidazole-solvated-minimized.pdb", "w"),
    )
Esempio n. 30
0
def fix(pdbid, missing_residues, padding=PADDING, mutations=None):
    fixer = pdbfixer.PDBFixer(filename="%s.pdb" % pdbid)

    if mutations is not None:
        fixer.applyMutations(mutations[0], mutations[1])

    fixer.missingResidues = missing_residues
    fixer.findNonstandardResidues()
    fixer.replaceNonstandardResidues()
    fixer.removeHeterogens(True)
    fixer.findMissingAtoms()
    fixer.addMissingAtoms()
    fixer.addMissingHydrogens(7.0)
    numChains = len(list(fixer.topology.chains()))
    fixer.removeChains(range(1, numChains))
    file_handle = open("./pdbs/%s_fixed0.pdb" % pdbid, 'wb')
    app.PDBFile.writeFile(fixer.topology, fixer.positions, file_handle)
    file_handle.close()
    
    traj0 = md.load("./pdbs/%s_fixed0.pdb" % pdbid)
    traj = sort_atoms(traj0)
    filename1 = "./pdbs/%s_fixed1.pdb" % pdbid
    traj.save(filename1)
    
    filename = "./pdbs/%s_fixed.pdb" % pdbid
    # Need to sync the protonation state of one histidine
    
    cmd = """grep  -v 'HE1 HIS A 119' %s |grep  -v 'HE2 HIS A 119'|grep  -v 'HD1 HIS A 119'|grep  -v 'HD2 HIS A 119' > %s""" % (filename1, filename)
    os.system(cmd)
    #os.system("grep  -v 'HE2 HIS A 119' %s > %s""" % (filename1, filename))
    #os.system("grep  -v 'HD1 HIS A 119' %s > %s""" % (filename1, filename))
    #os.system("grep  -v 'HD2 HIS A 119' %s > %s""" % (filename1, filename))

    ff_name = "amber99sbildn"
    water_name = 'tip3p'

    which_forcefield = "%s.xml" % ff_name
    which_water = '%s.xml' % water_name

    out_pdb_filename = "./equil/equil.pdb"
    ff = app.ForceField(which_forcefield, which_water)
    
    pdb = app.PDBFile(filename)

    modeller = app.Modeller(pdb.topology, pdb.positions)
    
    variants = [None for i in range(161)]
    variants[118] = "HIE"
    
    modeller.addHydrogens(ff, variants=variants)
    modeller.addSolvent(ff, padding=padding)

    app.PDBFile.writeFile(modeller.topology, modeller.positions, open("./pdbs/%s_box.pdb" % pdbid, 'w'))