def setup_yank_calculation(receptor_file_name, ligand_file_name, setup_directory_name, solvate=False): # Cleanup setup directory if os.path.exists(setup_directory_name): shutil.rmtree(setup_directory_name) os.makedirs(setup_directory_name) # Read ligand and receptor molecule ifs_mol2 = oechem.oemolistream() ifs_mol2.open(ligand_file_name) ligand_oemol = oechem.OEGraphMol() oechem.OEReadMolecule(ifs_mol2, ligand_oemol) ifs_mol2.close() ifs_mol2 = oechem.oemolistream() ifs_mol2.open(receptor_file_name) receptor_oemol = oechem.OEGraphMol() oechem.OEReadMolecule(ifs_mol2, receptor_oemol) ifs_mol2.close() # Push ligand close to receptor pull_close(receptor_oemol, ligand_oemol, MIN_DISTANCE, MAX_DISTANCE) # Add residue name 'MOL' residue = oechem.OEResidue() residue.SetName('MOL') for atom in ligand_oemol.GetAtoms(): oechem.OEAtomSetResidue(atom, residue) # Parametrize ligand with working_directory(setup_directory_name): # Save translated ligand ofs = oechem.oemolostream() ofs.open('ligand.mol2') oechem.OEWriteMolecule(ofs, ligand_oemol) ofs.close() # Parametrize ligand print "Parameterizing ligand with GAFF..." run_command( 'antechamber -fi mol2 -i ligand.mol2 -fo mol2 -o ligand.gaff.mol2') run_command( 'parmchk -i ligand.gaff.mol2 -o ligand.gaff.frcmod -f mol2') # Copy receptor so that leap will know the PDB file name shutil.copyfile(receptor_file_name, 'receptor.pdb') # Create AMBER prmtop/inpcrd files. print "Creating AMBER prmtop/inpcrd files..." cmd = 'tleap -f {!s} > setup.leap.out' if solvate: run_command(cmd.format(LEAP_IN_EXPLICIT)) else: run_command(cmd.format(LEAP_IN_IMPLICIT))
def PrintResidues(mol): """list alternate location code and occupancy by group and residue""" if not oechem.OEHasResidues(mol): oechem.OEPerceiveResidues(mol, oechem.OEPreserveResInfo_All) alf = oechem.OEAltLocationFactory(mol) print("%s - %d alternate location groups:" % (mol.GetTitle(), alf.GetGroupCount())) for grp in alf.GetGroups(): print("%d) %d alternate locations" % (grp.GetGroupID() + 1, grp.GetLocationCount()), end=" ") prev = oechem.OEResidue() prevCodes = "" sumOcc = [] atNum = [] for atom in alf.GetAltAtoms(grp): res = oechem.OEAtomGetResidue(atom) if not oechem.OESameResidue(res, prev): for i, code in enumerate(prevCodes): print("%c(%.0f%c) " % (code, (sumOcc[i] * 100.0) / atNum[i], "%"), end=" ") print() prevCodes = "" sumOcc = [] atNum = [] print("\t%s%d%c chain '%c': " % (res.GetName(), res.GetResidueNumber(), res.GetInsertCode(), res.GetChainID()), end=" ") prev = res code = res.GetAlternateLocation() whichCode = prevCodes.find(code) if whichCode < 0: prevCodes += code sumOcc.append(res.GetOccupancy()) atNum.append(1) else: sumOcc[whichCode] += res.GetOccupancy() atNum[whichCode] += 1 for i, code in enumerate(prevCodes): print("%c(%.0f%c) " % (code, (sumOcc[i] * 100.0) / atNum[i], "%"), end=" ") print()
def CalcResCounts(mol): if not oechem.OEHasResidues(mol): oechem.OEPerceiveResidues(mol, oechem.OEPreserveResInfo_All) resCt = 0 hetCt = 0 prevRes = oechem.OEResidue() for atom in mol.GetAtoms(): thisRes = oechem.OEAtomGetResidue(atom) if not oechem.OESameResidue(prevRes, thisRes): resCt += 1 if thisRes.IsHetAtom(): hetCt += 1 prevRes = thisRes print("Molecule: %s" % mol.GetTitle()) print("Residues: %d (%d hets)" % (resCt, hetCt))
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") ims = oechem.oemolistream() ims.SetFlavor(oechem.OEFormat_PDB, oechem.OEIFlavor_PDB_ALTLOC) inputFile = itf.GetString("-in") if not ims.open(inputFile): oechem.OEThrow.Fatal("Unable to open %s for reading." % inputFile) if not oechem.OEIs3DFormat(ims.GetFormat()): oechem.OEThrow.Fatal("%s is not in a 3D format." % inputFile) mol = oechem.OEGraphMol() if not oechem.OEReadMolecule(ims, mol): oechem.OEThrow.Fatal("Unable to read %s." % inputFile) if not oechem.OEHasResidues(mol): oechem.OEPerceiveResidues(mol, oechem.OEPreserveResInfo_All) alf = oechem.OEAltLocationFactory(mol) if alf.GetGroupCount() != 0: alf.MakePrimaryAltMol(mol) # in our example, we will select the first histidine selectedResidue = oechem.OEResidue() for atom in mol.GetAtoms(): res = oechem.OEAtomGetResidue(atom) if oechem.OEGetResidueIndex(res) == oechem.OEResidueIndex_HIS: selectedResidue = res break # @ <SNIPPET-PLACE-HYDROGENS-PRED> # given predicate IsSameResidue, residue selectedResidue and molecule mol... opt = oechem.OEPlaceHydrogensOptions() opt.SetNoFlipPredicate(IsSameResidue(selectedResidue)) if oechem.OEPlaceHydrogens(mol, opt): # selectedResidue will not be flipped... # @ </SNIPPET-PLACE-HYDROGENS-PRED> print("success") ims.close()
def openmmTop_to_oemol(topology, positions, verbose=False): """ This function converts an OpenMM topology in an OEMol Parameters: ----------- topology : OpenMM Topology The OpenMM topology positions : OpenMM Quantity The molecule atom positions associated with the topology Return: ------- oe_mol : OEMol The generated OEMol molecule """ # Create an empty OEMol oe_mol = oechem.OEMol() # Mapping dictionary between openmm atoms and oe atoms openmm_atom_to_oe_atom = {} # Python set used to identify atoms that are not in protein residues keep = set(proteinResidues).union(dnaResidues).union(rnaResidues) for chain in topology.chains(): for res in chain.residues(): # Create an OEResidue oe_res = oechem.OEResidue() # Set OEResidue name oe_res.SetName(res.name) # If the atom is not a protein atom then set its heteroatom # flag to True if res.name not in keep: oe_res.SetFragmentNumber(chain.index + 1) oe_res.SetHetAtom(True) # Set OEResidue Chain ID oe_res.SetChainID(chain.id) # res_idx = int(res.id) - chain.index * len(chain._residues) # Set OEResidue number oe_res.SetResidueNumber(int(res.id)) for openmm_at in res.atoms(): # Create an OEAtom based on the atomic number oe_atom = oe_mol.NewAtom(openmm_at.element._atomic_number) # Set atom name oe_atom.SetName(openmm_at.name) # Set Symbol oe_atom.SetType(openmm_at.element.symbol) # Set Atom index oe_res.SetSerialNumber(openmm_at.index + 1) # Commit the changes oechem.OEAtomSetResidue(oe_atom, oe_res) # Update the dictionary OpenMM to OE openmm_atom_to_oe_atom[openmm_at] = oe_atom if topology.getNumAtoms() != oe_mol.NumAtoms(): raise ValueError( "OpenMM topology and OEMol number of atoms mismatching: " "OpenMM = {} vs OEMol = {}".format(topology.getNumAtoms(), oe_mol.NumAtoms())) # Count the number of bonds in the openmm topology omm_bond_count = 0 # Create the bonds for omm_bond in topology.bonds(): omm_bond_count += 1 at0 = omm_bond[0] at1 = omm_bond[1] oe_bond_order = omm_bond.order # If bond order info are not present set the bond order temporary to one if not omm_bond.order: oe_bond_order = 1 # OE atoms oe_atom0 = openmm_atom_to_oe_atom[at0] oe_atom1 = openmm_atom_to_oe_atom[at1] # Create the bond oe_bond = oe_mol.NewBond(oe_atom0, oe_atom1, oe_bond_order) if omm_bond.type: if omm_bond.type == 'Aromatic': oe_atom0.SetAromatic(True) oe_atom1.SetAromatic(True) oe_bond.SetAromatic(True) oe_bond.SetType("Aromatic") elif omm_bond.type in ["Single", "Double", "Triple", "Amide"]: oe_bond.SetType(omm_bond.type) else: oe_bond.SetType("") if omm_bond_count != oe_mol.NumBonds(): raise ValueError( "OpenMM topology and OEMol number of bonds mismatching: " "OpenMM = {} vs OEMol = {}".format(omm_bond_count, oe_mol.NumBonds())) # Set the OEMol positions pos = positions.in_units_of(unit.angstrom) / unit.angstrom pos = list(itertools.chain.from_iterable(pos)) oe_mol.SetCoords(pos) oechem.OESetDimensionFromCoords(oe_mol) return oe_mol