Example #1
0
    def test_atom_map(self):
        """Test get atom map"""
        from openeye import oechem
        tagged_smiles = '[H:5][C:1]#[N+:4][C:3]([H:9])([H:10])[C:2]([H:6])([H:7])[H:8]'
        mol_1 = openeye.smiles_to_oemol('CC[N+]#C')
        inf = get_fn('ethylmethylidyneamonium.mol2')
        ifs = oechem.oemolistream(inf)
        mol_2 = oechem.OEMol()
        oechem.OEReadMolecule(ifs, mol_2)

        atom_map = utils.get_atom_map(tagged_smiles, mol_1)

        for i, mapping in enumerate(atom_map):
            atom_1 = mol_1.GetAtom(oechem.OEHasAtomIdx(atom_map[mapping]))
            atom_1.SetAtomicNum(i+1)
            atom_2 = mol_2.GetAtom(oechem.OEHasAtomIdx(mapping-1))
            atom_2.SetAtomicNum(i+1)
            self.assertEqual(oechem.OECreateCanSmiString(mol_1), oechem.OECreateCanSmiString(mol_2))

        # Test aromatic molecule
        tagged_smiles = '[H:10][c:4]1[c:3]([c:2]([c:1]([c:6]([c:5]1[H:11])[H:12])[C:7]([H:13])([H:14])[H:15])[H:8])[H:9]'
        mol_1 = openeye.smiles_to_oemol('Cc1ccccc1')
        inf = get_fn('toluene.mol2')
        ifs = oechem.oemolistream(inf)
        mol_2 = oechem.OEMol()
        oechem.OEReadMolecule(ifs, mol_2)

        atom_map = utils.get_atom_map(tagged_smiles, mol_1)
        for i, mapping in enumerate(atom_map):
            atom_1 = mol_1.GetAtom(oechem.OEHasAtomIdx(atom_map[mapping]))
            atom_1.SetAtomicNum(i+1)
            atom_2 = mol_2.GetAtom(oechem.OEHasAtomIdx(mapping-1))
            atom_2.SetAtomicNum(i+1)
            self.assertEqual(oechem.OECreateCanSmiString(mol_1), oechem.OECreateCanSmiString(mol_2))
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 main(argv=sys.argv):
    if len(argv) != 2:
        oechem.OEThrow.Usage("%s <infile (oeb file prefix)>" % argv[0])

    ifs = oechem.oemolistream()
    if not ifs.open(argv[1]+'.oeb'):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1]+'.oeb' )
    
    ofsff = oechem.oemolostream()
    ofsff.SetFlavor( oechem.OEFormat_MOL2, oechem.OEOFlavor_MOL2_Forcefield )
    if not ofsff.open(argv[1]+'_ff.mol2'):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[1]+'_ff.mol2')

    ofsTri = oechem.oemolostream()
    ofsTri.SetFlavor( oechem.OEFormat_MOL2, oechem.OEOFlavor_MOL2_Forcefield )
    if not ofsTri.open(argv[1]+'_tripos.mol2'):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[1]+'_tripos.mol2')

    for mol in ifs.GetOEMols():
        oechem.OETriposAtomNames(mol)
        oechem.OEWriteConstMolecule(ofsff, mol)
        oechem.OETriposAtomTypeNames(mol)
        oechem.OEWriteConstMolecule(ofsTri, mol)

    ifs.close()
    ofsff.close()
    ofsTri.close()
def read_molecule(filename):
    ifs = oechem.oemolistream()
    ifs.open(filename)
    molecule = oechem.OEMol()
    oechem.OEReadMolecule(ifs, molecule)
    ifs.close()
    return molecule
Example #5
0
def check_boxes(forcefield, description="", chargeMethod=None, verbose=False):
    """Test creation of System from boxes of mixed solvents.
    """
    # Read monomers
    mols = list()
    monomers = ['cyclohexane', 'ethanol', 'propane', 'methane', 'butanol']
    from openeye import oechem
    mol = oechem.OEGraphMol()
    for monomer in monomers:
        filename = get_data_filename(os.path.join('systems', 'monomers', monomer + '.sdf'))
        ifs = oechem.oemolistream(filename)
        while oechem.OEReadMolecule(ifs, mol):
            oechem.OETriposAtomNames(mol)
            mols.append( oechem.OEGraphMol(mol) )
    if verbose: print('%d reference molecules loaded' % len(mols))

    # Read systems.
    boxes = ['cyclohexane_ethanol_0.4_0.6.pdb', 'propane_methane_butanol_0.2_0.3_0.5.pdb']
    from simtk.openmm.app import PDBFile
    for box in boxes:
        filename = get_data_filename(os.path.join('systems', 'packmol_boxes', box))
        pdbfile = PDBFile(filename)
        f = partial(check_system_creation_from_topology, forcefield, pdbfile.topology, mols, pdbfile.positions, chargeMethod=chargeMethod, verbose=verbose)
        f.description = 'Test creation of System object from %s %s' % (box, description)
        yield f
Example #6
0
def convert_mdtraj_to_oemol(traj: md.Trajectory) -> oechem.OEMol:
    """
    This method converts an mdtraj Trajectory to an OEMol via saving as a PDBfile
    and reading in with OpenEye. Although this seems hacky, it seems less error-prone
    than trying to manually construct the OEMol.

    Parameters
    ----------
    mdtraj: md.Trajectory
        The trajectory to turn into an OEMol
    
    Returns
    -------
    mol : oechem.OEMol
        The trajectory represented as an OEMol
    """
    #create a temporary file with a PDB suffix and save with MDTraj
    pdb_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdb")
    traj.save(pdb_file.name)
    pdb_file.close()
    
    #Now use the openeye oemolistream to read in this file as an OEMol:
    ifs = oechem.oemolistream()
    ifs.open(pdb_file.name)
    ifs.SetFormat(oechem.OEFormat_PDB)
    
    mol = oechem.OEMol()
    oechem.OEReadMolecule(ifs, mol)
    
    #close the stream and delete the temporary pdb file
    ifs.close()
    os.unlink(pdb_file.name)

    return mol
Example #7
0
    def testConvertFromOEChem(self):
        testfiles = [
            'tests/files/jandor.sdf',
            'tests/files/test_UID_corrected.mol2',
            'tests/files/fused.sdf',
            'tests/files/3RYZ.pdb',
            'tests/files/1DUF.pdb',
            ]

        for testfile in testfiles:
            with oechem.oemolistream()(testfile) as ifs:
                mol = oechem.OEMol()
                with TimedLogMessage("OEReadMolecule"):
                    assert oechem.OEReadMolecule(ifs, mol)

                if 'pdb' in testfile:
                    oechem.OEAddExplicitHydrogens(mol)

                with TimedLogMessage("ConvertFromOEChem"):
                    system = msys.ConvertFromOEChem(mol)

                if 'pdb' in testfile:
                    assert len(system.residues) > 1
                    if '1DUF' in testfile:
                        assert len(system.chains) > 1

                with TimedLogMessage("ConvertToOEChem"):
                    new_oemol = msys.ConvertToOEChem(system)

                with TimedLogMessage("OEMolToSmiles"):
                    oechem.OEMolToSmiles(new_oemol)

                assert new_oemol.GetTitle() == mol.GetTitle()

                assert oechem.OEMolToSmiles(new_oemol) == oechem.OEMolToSmiles(mol)
def read_molecules(filename):
    """Read a file into an OpenEye molecule (or list of molecules).

    Parameters
    ----------
    filename : str
        The name of the file to read (e.g. mol2, sdf)

    Returns
    -------
    molecule : openeye.oechem.OEMol
        The OEMol molecule read, or a list of molecules if multiple molecules are read.
        If no molecules are read, None is returned.

    """

    ifs = oechem.oemolistream(filename)
    molecules = list()
    for mol in ifs.GetOEMols():
        mol_copy = oechem.OEMol(mol)
        molecules.append(mol_copy)
    ifs.close()

    if len(molecules) == 0:
        return None
    elif len(molecules) == 1:
        return molecules[0]
    else:
        return molecules
Example #9
0
def test_smirff_energies_vs_parmatfrosst(verbose=False):
    """Test evaluation of energies from parm@frosst ffxml files versus energies of equivalent systems."""

    from openeye import oechem
    prefix = 'AlkEthOH_'
    molecules = [ 'r118', 'r12', 'c1161', 'r0', 'c100', 'c38', 'c1266' ]

    # Loop over molecules, load OEMols and prep for comparison/do comparison
    for molnm in molecules:
        f_prefix = os.path.join('molecules', prefix+molnm )
        mol2file = get_data_filename( f_prefix+'.mol2')
        prmtop = get_data_filename( f_prefix+'.top')
        crd = get_data_filename( f_prefix+'.crd')
        # Load special parm@frosst with parm99/parm@frosst bugs re-added for testing
        forcefield = ForceField( get_data_filename('forcefield/Frosst_AlkEtOH_parmAtFrosst.ffxml') )

        # Load OEMol
        mol = oechem.OEGraphMol()
        ifs = oechem.oemolistream(mol2file)
        flavor = oechem.OEIFlavor_Generic_Default | oechem.OEIFlavor_MOL2_Default | oechem.OEIFlavor_MOL2_Forcefield
        ifs.SetFlavor( oechem.OEFormat_MOL2, flavor)
        oechem.OEReadMolecule(ifs, mol )
        oechem.OETriposAtomNames(mol)

        # Do comparison
        results = compare_molecule_energies( prmtop, crd, forcefield, mol, verbose = verbose )
Example #10
0
def _construct_test_values(fp_func = None, num_bits=4096):
    from openeye.oechem import oemolistream
    from openeye.oegraphsim import OEFingerPrint, OEMakePathFP
    fp = OEFingerPrint()
    ifs = oemolistream()
    assert ifs.open(PUBCHEM_SDF)
    hex_data = []
    if fp_func is None:
        fp_func = OEMakePathFP

    def _convert_to_chemfp_order(s):
        # The FPS format allows either case but I prefer lowercase
        s = s.lower()
        # OpenEye orders hex values on nibbles. Chemfp orders on bytes.
        return "".join( (s[i+1]+s[i]) for i in range(0, len(s), 2))

    for mol in ifs.GetOEGraphMols():
        fp_func(fp, mol)
        # Set the byte values given the bit offsets
        bytes = [0] * (num_bits//8)
        i = fp.FirstBit()
        while i >= 0:
            bytes[i//8] |= 1<<(i%8)
            i = fp.NextBit(i)
        as_hex = "".join("%02x" % i for i in bytes)
        assert len(as_hex) == 2*(num_bits//8), len(as_hex)
        # Double-check that it matches the (reordered) ToHexString()
        oe_hex = fp.ToHexString()[:-1]
        assert as_hex == _convert_to_chemfp_order(oe_hex), (
            as_hex, _convert_to_chemfp_order(oe_hex))
        
        hex_data.append("%s\t%s" % (as_hex, mol.GetTitle()))
    return hex_data
Example #11
0
def read_oe_molecule(file_path):
    from openeye import oechem
    molecule = oechem.OEGraphMol()
    ifs = oechem.oemolistream()
    ifs.open(file_path)
    oechem.OEReadMolecule(ifs, molecule)
    ifs.close()
    return molecule
Example #12
0
    def testConvertFromOEChemFailures(self):
        mol = oechem.OEMol()

        # SMILES has no coordinates or hydrogens
        assert oechem.OESmilesToMol(mol, 'C1NCC[NH2+]C1')
        self.assertRaises(ValueError, msys.ConvertFromOEChem, mol)

        # This is a 2D SDF file
        with oechem.oemolistream()('tests/files/methotrexate.sdf') as ifs:
            assert oechem.OEReadMolecule(ifs, mol)
        self.assertRaises(ValueError, msys.ConvertFromOEChem, mol)

        # Suppressing hydrogens should cause ConvertFromOEChem to fail
        with oechem.oemolistream()('tests/files/fused.sdf') as ifs:
            assert oechem.OEReadMolecule(ifs, mol)
        assert oechem.OESuppressHydrogens(mol)
        self.assertRaises(ValueError, msys.ConvertFromOEChem, mol)
Example #13
0
def generate_fragments(inputf, output_dir, pdf=False, combinatorial=True, MAX_ROTORS=2, strict_stereo=True, remove_map=True):
    """
    This function generates fragment SMILES files sorted by rotatable bonds from an input molecule file.
    The output .smi files are written out to `output_dir` and named `nrotor_n.smi` where n corresponds to the number
    of rotatable bonds for all fragments in the file.
    Parameters
    ----------
    inputf: str
        absolute path to input molecule file
    output_dir: str
        absolute path to output directory
    pdf: bool
        If true, visualization of the fragments will be written to pdf files. The pdf will be writtten in the directory
        where this function is run from.
    combinatorial: bool
        If true, find all connected fragments from fragments and add all new fragments that have less than MAX_ROTORS
    MAX_ROTORS: int
        rotor threshold for combinatorial

    """
    ifs = oechem.oemolistream()
    smiles_unique = set()

    mol = oechem.OEMol()
    if ifs.open(inputf):
        while oechem.OEReadMolecule(ifs, mol):
            openeye.normalize_molecule(mol)
            logger().info('fragmenting {}...'.format(mol.GetTitle()))
            if remove_map:
                # Remove tags from smiles. This is done to make it easier to find duplicate fragments
                for a in mol.GetAtoms():
                    a.SetMapIdx(0)
            frags = _generate_fragments(mol, strict_stereo=strict_stereo)
            if not frags:
                logger().warn('Skipping {}, SMILES: {}'.format(mol.GetTitle(), oechem.OECreateSmiString(mol)))
                continue
            charged = frags[0]
            frags = frags[-1]
            if combinatorial:
                smiles = smiles_with_combined(frags, charged, MAX_ROTORS=MAX_ROTORS)
            else:
                smiles = frag_to_smiles(frags, charged)

            smiles_unique.update(list(smiles.keys()))
            if pdf:
                oname = '{}.pdf'.format(mol.GetTitle())
                ToPdf(charged, oname, frags)
            del charged, frags

    # Generate oedatabase for all fragments
    split_fname = inputf.split('.')
    base = split_fname[-2].split('/')[-1]
    ofname = base + '_frags'
    utils.to_smi(list(smiles_unique), output_dir, ofname)
    ofname_ext = ofname + '.smi'
    oedb_name = os.path.join(output_dir, ofname_ext)
    utils.create_oedatabase_idxfile(oedb_name)
    _sort_by_rotbond(oedb_name, outdir=output_dir)
Example #14
0
def load_database(database, mol2_directory, verbose=False):
    """
    This function prepares the database that will be use in sampling.

    Arguments
    ---------
    database : dict
        an unpickled version of the FreeSolv database
    mol2_directory : String
        the path to the FreeSolv mol2 files containing geometry and charges
    verbose : Boolean, optional
        verbosity

    Returns
    -------
    database : dict
        An updated version of the database dict containing OEMols
    """

    start_time = time.time()
    if verbose:
        print ("Reading all molecules in dataset. Will use charges and coordinates from dataset.")
    for cid in database.keys():
        entry = database[cid]

        # Store temperature
        # TODO: Get this from database?
        entry["temperature"] = 300.0 * units.kelvin

        # Extract relevant entry data from database.
        smiles = entry["smiles"]
        iupac_name = entry["iupac"]
        experimental_DeltaG = entry["expt"] * units.kilocalories_per_mole
        experimental_dDeltaG = entry["d_expt"] * units.kilocalories_per_mole

        # Read molecule.
        molecule = openeye.oechem.OEMol()

        # Load the mol2 file.
        tripos_mol2_filename = os.path.join(mol2_directory, cid + ".mol2")
        omolstream = oechem.oemolistream(tripos_mol2_filename)
        oechem.OEReadMolecule(omolstream, molecule)
        omolstream.close()
        molecule.SetTitle(iupac_name)
        molecule.SetData("cid", cid)

        # Add explicit hydrogens.
        oechem.OEAddExplicitHydrogens(molecule)

        # Store molecule.
        entry["molecule"] = oechem.OEMol(molecule)

        if verbose:
            print "%d molecules read" % len(database.keys())
            end_time = time.time()
            elapsed_time = end_time - start_time
            print "%.3f s elapsed" % elapsed_time
    return database
Example #15
0
def check_AlkEtOH(forcefield, description="", chargeMethod=None, verbose=False):
    """Test creation of System from AlkEtOH small molecules.
    """
    from openeye import oechem
    ifs = oechem.oemolistream(get_data_filename('molecules/AlkEtOH-tripos.mol2.gz'))
    mol = oechem.OEGraphMol()
    while oechem.OEReadMolecule(ifs, mol):
        args = { 'verbose' : verbose, 'chargeMethod' : chargeMethod }
        f = partial(check_system_creation_from_molecule, forcefield, mol, **args)
        f.description ='Testing creation of system object from small molecules (%s) %s' % (mol.GetTitle(), description)
        yield f
Example #16
0
def ProtonateLig_openeye(file_in,file_out,protonate=True,type_in=None,type_out=None):
    """
    Function to protonate small molecule using OpenEye's python libraries. Requires OpenEye licence.
    More information found https://docs.eyesopen.com/toolkits/python/quickstart-python/index.html

    Also doubles as a file conversion script. Files types can be specified or detected via the file extension. File types
    supported by OpenEye listed here: https://docs.eyesopen.com/toolkits/python/oechemtk/molreadwrite.html

    Parameters
    ----------
    file_in: str
        Name of the input file containing the coordinates of the molecule. One molecule expected.
    file_out: str
        Name of the output file.
    type_in: str
        File type of input file. Typically the file extension.
    type_out: str
        File type of the output file. Typically the file extension.

    Returns
    -------
    The output is written to a file
    """
    try:
        import openeye.oechem as oechem

        if type_in == None:
            type_in = file_in.split('.')[-1]
        if type_out == None:
            type_out = file_out.split('.')[-1]

        mol = oechem.OEGraphMol()           # initialising molecule object
        ifs = oechem.oemolistream()         # initialising input stream for reading in data
        ofs = oechem.oemolostream()         # initialising the OUTPUT stream for writing data

        ifs.SetFormat(eval('oechem.OEFormat_' + type_in.upper()))
        ofs.SetFormat(eval('oechem.OEFormat_' + type_out.upper()))

        ifs.open(file_in)
        if oechem.OEReadMolecule(ifs,mol):  # this function automatically returns True or False, to help spot for errors.
            pass
        else:
            print "Problem loading molecule!"

        mol = oechem.OEMol(mol)
        oechem.OEAssignAromaticFlags(mol, oechem.OEAroModelOpenEye)

        if protonate==True: oechem.OEAddExplicitHydrogens(mol)

        ofs.open(file_out)
        oechem.OEWriteMolecule(ofs,mol)

    except ImportError:
        print 'OpenEye python bindings not found'
Example #17
0
    def test_tagged_smiles(self):
        """Test index-tagges smiles"""
        from openeye import oechem
        inf = get_fn('ethylmethylidyneamonium.mol2')
        ifs = oechem.oemolistream(inf)
        inp_mol = oechem.OEMol()
        oechem.OEReadMolecule(ifs, inp_mol)

        tagged_smiles = utils.create_mapped_smiles(inp_mol)

        # Tags should always be the same as mol2 molecule ordering
        self.assertEqual(tagged_smiles, '[H:5][C:1]#[N+:4][C:3]([H:9])([H:10])[C:2]([H:6])([H:7])[H:8]')
def generate_forcefield(molecule_file, outfile):

    ifs = oechem.oemolistream()
    ifs.open(molecule_file)

    # get the list of molecules
    mol_list = [normalize_molecule(oechem.OEMol(mol)) for mol in ifs.GetOEMols()]

    # TODO: HORRIBLE HACK ; WILL DIE AT > 999 RESIDUES!
    for idx, mol in enumerate(mol_list):
        mol.SetTitle("%03d" % idx)

    ffxml = forcefield_generators.generateForceFieldFromMolecules(mol_list, normalize=False)

    with open(outfile, 'w') as output_file:
        output_file.write(ffxml)
def generate_ligand_topologies_and_positions(ligand_filename):
    """
    Generate the topologies and positions for ligand-only systems

    Parameters
    ----------
    ligand_filename : str
        The name of the file containing the ligands in any OpenEye supported format

    Returns
    -------
    ligand_topologies : dict of str: md.Topology
        A dictionary of the ligand topologies generated from the file indexed by SMILES strings
    ligand_positions_dict : dict of str: unit.Quantity array
        A dictionary of the corresponding positions, indexed by SMILES strings
    """
    ifs = oechem.oemolistream()
    ifs.open(ligand_filename)

    # get the list of molecules
    mol_list = [oechem.OEMol(mol) for mol in ifs.GetOEMols()]

    for idx, mol in enumerate(mol_list):
        mol.SetTitle("MOL{}".format(idx))
        oechem.OETriposAtomNames(mol)

    mol_dict = {oechem.OEMolToSmiles(mol) : mol for mol in mol_list}

    ligand_topology_dict = {smiles : forcefield_generators.generateTopologyFromOEMol(mol) for smiles, mol in mol_dict.items()}

    ligand_topologies = {}
    ligand_positions_dict = {}

    for smiles, ligand_topology in ligand_topology_dict.items():
        ligand_md_topology = md.Topology.from_openmm(ligand_topology)

        ligand_topologies[smiles] = ligand_md_topology

        ligand_positions = extractPositionsFromOEMOL(mol_dict[smiles])

        ligand_positions_dict[smiles] = ligand_positions

    return ligand_topologies, ligand_positions_dict
Example #20
0
    def testConvertAtoms(self):
        mol = msys.Load('tests/files/jandor.sdf')
        omol = msys.ConvertToOEChem(mol.atoms)
        assert omol.GetDimension() == 3
        new = msys.ConvertFromOEChem(omol)
        assert mol.positions.tolist() == new.positions.tolist()
        assert [a.atomic_number for a in mol.atoms] == [a.atomic_number for a in new.atoms]

        ifs = oechem.oemolistream()
        assert ifs.open('tests/files/jandor.sdf')
        oechem_mol = oechem.OEGraphMol()
        assert oechem.OEReadMolecule(ifs, oechem_mol)
        assert oechem.OEMolToSmiles(omol) == oechem.OEMolToSmiles(oechem_mol)

        # testing disconnected components
        atoms = mol.select('withinbonds 1 of hydrogen')
        omol = msys.ConvertToOEChem(atoms)
        new = msys.ConvertFromOEChem(omol)
        assert [a.pos.tolist() for a in atoms] == new.positions.tolist()
        assert [a.atomic_number for a in atoms] == [a.atomic_number for a in new.atoms]
def main(argv=sys.argv):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <infile (forcefield types)> <outfile (Tripos types)>" % argv[0])

    ifs = oechem.oemolistream()
    flavor = oechem.OEIFlavor_Generic_Default | oechem.OEIFlavor_MOL2_Default | oechem.OEIFlavor_MOL2_Forcefield
    ifs.SetFlavor(oechem.OEFormat_MOL2, flavor)
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])

    ofs = oechem.oemolostream()
    if not ofs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[2])

    for mol in ifs.GetOEMols():
        oechem.OETriposAtomNames(mol)
        oechem.OEWriteConstMolecule(ofs, mol)

    ifs.close()
    ofs.close()
Example #22
0
def to_oemol(filename, title=True, verbose=True):
    """Create OEMol from file. If more than one mol in file, return list of OEMols.

    Parameters
    ----------
    filename: str
        absolute path to
    title: str, title
        title for molecule. If None, IUPAC name will be given as title.

    Returns
    -------
    mollist: list
        list of OEMol for multiple molecules. OEMol if file only has one molecule.
    """

    if not os.path.exists(filename):
        raise Exception("File {} not found".format(filename))
    if verbose:
        logger().info("Loading molecules from {}".format(filename))

    ifs = oechem.oemolistream(filename)
    #moldb = oechem.OEMolDatabase(ifs)
    mollist = []

    molecule = oechem.OECreateOEGraphMol()
    while oechem.OEReadMolecule(ifs, molecule):
        molecule_copy = oechem.OEMol(molecule)
        if title:
            title = molecule_copy.GetTitle()
            if verbose:
                logger().infor("Reading molecule {}".format(title))

        mollist.append(normalize_molecule(molecule_copy, title))

    if len(mollist) <= 1:
        mollist = mollist[0]

    ifs.close()

    return mollist
Example #23
0
    def __init__(self, prefix: str, ref_file: os.PathLike, **kwargs):
        """
        :param prefix: Prefix to identify scoring function instance (e.g., Risperidone)
        :param ref_file: Path to reference file to overlay query to (.pdb)
        :param kwargs:
        """

        self.prefix = prefix.replace(" ", "_")
        self.rocs_metrics = ROCS.return_metrics
        self.ref_file = ref_file
        self.refmol = oechem.OEMol()
        ifs = oechem.oemolistream(self.ref_file)  # Set up input file stream
        oechem.OEReadMolecule(ifs, self.refmol)  # Read ifs to empty mol object
        self.fitmol = None
        self.rocs_results = None
        self.best_overlay = None

        omegaOpts = oeomega.OEOmegaOptions()
        omegaOpts.SetStrictStereo(False)
        omegaOpts.SetMaxSearchTime(1.0)
        self.omega = oeomega.OEOmega(omegaOpts)
def main(argv=[__name__]):
    if len(argv) != 2 and len(argv) != 3:
        oechem.OEThrow.Usage("%s [-H] <mol-infile>" % argv[0])

    hideAtoms = False
    filename = argv[1]
    if len(argv) == 3:
        if argv[1] != "-H":
            oechem.OEThrow.Fatal("Bad flag: %s (expecting -H)" % argv[1])
        hideAtoms = True
        filename = argv[2]

    ifs = oechem.oemolistream()
    if not ifs.open(filename):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % filename)

    # need this flavor to read alt loc atoms
    ifs.SetFlavor(oechem.OEFormat_PDB, oechem.OEIFlavor_PDB_ALTLOC)

    for mol in ifs.GetOEGraphMols():
        PrintLocations(mol, hideAtoms)
def read_csv_molecules(filename):
    """Read molecules from the specified path

    Parameters
    ----------
    filename : str
        File from which molecules are to be read

    Returns
    -------
    molecules : list of openeye.oechem.OEMol
        The read molecules
    """

    from openeye import oechem
    mol = oechem.OEMol()
    molecules = list()
    with oechem.oemolistream(filename) as ifs:
        while oechem.OEReadCSVFile(ifs, mol):
            molecules.append(oechem.OEMol(mol))
    return molecules
Example #26
0
def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <reffile> <fitfile>" % argv[0])

    reffs = oechem.oemolistream(argv[1])
    refmol = oechem.OEMol()
    oechem.OEReadMolecule(reffs, refmol)
    print("Ref. Title:", refmol.GetTitle(), "Num Confs:", refmol.NumConfs())

    fitfs = oechem.oemolithread(sys.argv[2])

    thrds = []
    for i in range(oechem.OEGetNumProcessors()):
        thrd = MultiCoreOverlay(refmol, fitfs)
        thrd.start()
        thrds.append(thrd)

    for thrd in thrds:
        thrd.join()

    return 0
Example #27
0
def main(argv=[__name__]):
    if len(argv) != 2:
        oechem.OEThrow.Usage("%s <molfile>" % argv[0])

    ifs = oechem.oemolistream()
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])
    mol = oechem.OEGraphMol()
    oechem.OEReadMolecule(ifs, mol)
    oechem.OEAssignBondiVdWRadii(mol)
    oechem.OEMMFFAtomTypes(mol)
    oechem.OEMMFF94PartialCharges(mol)

    epsin = 1.0
    zap = oezap.OEZap()
    zap.SetInnerDielectric(epsin)
    zap.SetMolecule(mol)

    grid = oegrid.OEScalarGrid()
    if zap.CalcPotentialGrid(grid):
        oegrid.OEWriteGrid("zap.grd", grid)
Example #28
0
def main(argv=[__name__]):
    itf = oechem.OEInterface(InterfaceData)
    oemolprop.OEConfigureFilterParams(itf)

    if not oechem.OEParseCommandLine(itf, argv):
        oechem.OEThrow.Fatal("Unable to interpret command line!")

    iname = itf.GetString("-in")
    oname = itf.GetString("-out")

    ifs = oechem.oemolistream()
    if not ifs.open(iname):
        oechem.OEThrow.Fatal("Cannot open input file!")

    ftype = oemolprop.OEGetFilterType(itf)
    filt = oemolprop.OEFilter(ftype)

    ver = itf.GetInt("-verbose")
    oechem.OEThrow.SetLevel(ver)

    oemolprop.OEWritePropertyDataToCSV(oname, ifs, filt)
Example #29
0
def main(args):
    if len(args) != 3:
        oechem.OEThrow.Usage("%s <input> <output>" % args[0])

    ifs = oechem.oemolistream()
    if not ifs.open(args[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % args[1])

    ofs = oechem.oemolostream()
    if not ofs.open(args[2]):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % args[2])

    mol = oechem.OEMol()
    while oechem.OEReadMolecule(ifs, mol):
        oechem.OEAddExplicitHydrogens(mol)

        mmff = oeff.OEMMFF()
        if not mmff.PrepMol(mol) or not mmff.Setup(mol):
            oechem.OEThrow.Warning("Unable to process molecule: title = '%s'" %
                                   mol.GetTitle())
            oechem.OEWriteMolecule(ofs, mol)
            continue

        vecCoords = oechem.OEDoubleArray(3 * mol.GetMaxAtomIdx())
        for conf in mol.GetConfs():
            oechem.OEThrow.Info("Molecule: %s Conformer: %d" %
                                (mol.GetTitle(), conf.GetIdx() + 1))
            conf.GetCoords(vecCoords)

            energy = mmff(vecCoords)
            oechem.OEThrow.Info("Initial energy: %d kcal/mol" % energy)

            optimizer = oeff.OEBFGSOpt()
            energy = optimizer(mmff, vecCoords, vecCoords)
            oechem.OEThrow.Info("Optimized energy: %d kcal/mol" % energy)
            conf.SetCoords(vecCoords)

        oechem.OEWriteMolecule(ofs, mol)

    return 0
Example #30
0
def check_boxes(forcefield, description="", chargeMethod=None, verbose=False):
    """Test creation of System from boxes of mixed solvents.
    """
    # Read monomers
    mols = list()
    monomers = [
        'water', 'cyclohexane', 'ethanol', 'propane', 'methane', 'butanol'
    ]
    from openeye import oechem
    mol = oechem.OEGraphMol()
    for monomer in monomers:
        filename = get_data_filename(
            os.path.join('systems', 'monomers', monomer + '.sdf'))
        ifs = oechem.oemolistream(filename)
        while oechem.OEReadMolecule(ifs, mol):
            oechem.OETriposAtomNames(mol)
            mols.append(oechem.OEGraphMol(mol))
    if verbose: print('%d reference molecules loaded' % len(mols))

    # Read systems.
    boxes = [
        'ethanol_water.pdb', 'cyclohexane_water.pdb',
        'cyclohexane_ethanol_0.4_0.6.pdb',
        'propane_methane_butanol_0.2_0.3_0.5.pdb'
    ]
    from simtk.openmm.app import PDBFile
    for box in boxes:
        filename = get_data_filename(
            os.path.join('systems', 'packmol_boxes', box))
        pdbfile = PDBFile(filename)
        f = partial(check_system_creation_from_topology,
                    forcefield,
                    pdbfile.topology,
                    mols,
                    pdbfile.positions,
                    chargeMethod=chargeMethod,
                    verbose=verbose)
        f.description = 'Test creation of System object from %s %s' % (
            box, description)
        yield f
Example #31
0
def test_molecular_atom_mapping():
    """
    Test the creation of atom maps between pairs of molecules from the JACS benchmark set.

    """
    from openeye import oechem
    from perses.rjmc.topology_proposal import SmallMoleculeSetProposalEngine
    from perses.tests.utils import createOEMolFromSMILES
    from perses.tests.utils import render_atom_mapping
    from itertools import combinations

    # Test mappings for JACS dataset ligands
    for dataset_name in ['CDK2']: #, 'p38', 'Tyk2', 'Thrombin', 'PTP1B', 'MCL1', 'Jnk1', 'Bace']:
        # Read molecules
        dataset_path = 'data/schrodinger-jacs-datasets/%s_ligands.sdf' % dataset_name
        mol2_filename = resource_filename('perses', dataset_path)
        ifs = oechem.oemolistream(mol2_filename)
        molecules = list()
        for mol in ifs.GetOEGraphMols():
            molecules.append(oechem.OEGraphMol(mol))

        # Build atom map for some transformations.
        #for (molecule1, molecule2) in combinations(molecules, 2): # too slow
        molecule1 = molecules[0]
        for i, molecule2 in enumerate(molecules[1:]):
            new_to_old_atom_map = SmallMoleculeSetProposalEngine._get_mol_atom_map(molecule1, molecule2)
            # Make sure we aren't mapping hydrogens onto anything else
            atoms1 = [atom for atom in molecule1.GetAtoms()]
            atoms2 = [atom for atom in molecule2.GetAtoms()]
            #for (index2, index1) in new_to_old_atom_map.items():
            #    atom1, atom2 = atoms1[index1], atoms2[index2]
            #    if (atom1.GetAtomicNum()==1) != (atom2.GetAtomicNum()==1):
            filename = 'mapping-error-%d.png' % i
            render_atom_mapping(filename, molecule1, molecule2, new_to_old_atom_map)
            #msg = 'Atom atomic number %d is being mapped to atomic number %d\n' % (atom1.GetAtomicNum(), atom2.GetAtomicNum())
            msg = 'molecule 1 : %s\n' % oechem.OECreateIsoSmiString(molecule1)
            msg += 'molecule 2 : %s\n' % oechem.OECreateIsoSmiString(molecule2)
            msg += 'Wrote atom mapping to %s for inspection; please check this.' % filename
            msg += str(new_to_old_atom_map)
            print(msg)
Example #32
0
    def test_mapped_xyz(self):
        """Test writing out mapped xyz"""
        from openeye import oechem, oeomega
        tagged_smiles = '[H:10][c:4]1[c:3]([c:2]([c:1]([c:6]([c:5]1[H:11])[H:12])[C:7]([H:13])([H:14])[H:15])[H:8])[H:9]'
        mol_1 = openeye.smiles_to_oemol('Cc1ccccc1')
        inf = get_fn('toluene.mol2')
        ifs = oechem.oemolistream(inf)
        mol_2 = oechem.OEMol()
        oechem.OEReadMolecule(ifs, mol_2)

        atom_map = utils.get_atom_map(tagged_smiles, mol_1)
        for i, mapping in enumerate(atom_map):
            atom_1 = mol_1.GetAtom(oechem.OEHasAtomIdx(atom_map[mapping]))
            atom_1.SetAtomicNum(i + 1)
            atom_2 = mol_2.GetAtom(oechem.OEHasAtomIdx(mapping - 1))
            atom_2.SetAtomicNum(i + 1)

        xyz_1 = utils.to_mapped_xyz(mol_1, atom_map)
        # molecule generated from mol2 should be in the right order.
        atom_map_mol2 = {
            1: 0,
            2: 1,
            3: 2,
            4: 3,
            5: 4,
            6: 5,
            7: 6,
            8: 7,
            9: 8,
            10: 9,
            11: 10,
            12: 11,
            13: 12,
            14: 13,
            15: 14
        }
        xyz_2 = utils.to_mapped_xyz(mol_2, atom_map_mol2)

        for ele1, ele2 in zip(xyz_1.split('\n')[:-1], xyz_2.split('\n')[:-1]):
            self.assertEqual(ele1.split(' ')[2], ele2.split(' ')[2])
def test_merge_system():
    """Test merging of a system created from AMBER and another created from SMIRNOFF."""
    from .utils import create_system_from_amber, get_amber_file_path, get_alkethoh_file_path

    # Create System from AMBER
    prmtop_filename, inpcrd_filename = get_amber_file_path(
        'cyclohexane_ethanol_0.4_0.6')
    system0, topology0, positions0 = create_system_from_amber(
        prmtop_filename, inpcrd_filename)

    # TODO:
    from openeye import oechem
    # Load simple OEMol
    alkethoh_mol2_filepath = get_alkethoh_file_path('AlkEthOH_c100')[0]
    ifs = oechem.oemolistream(alkethoh_mol2_filepath)
    mol = oechem.OEMol()
    flavor = oechem.OEIFlavor_Generic_Default | oechem.OEIFlavor_MOL2_Default | oechem.OEIFlavor_MOL2_Forcefield
    ifs.SetFlavor(oechem.OEFormat_MOL2, flavor)
    oechem.OEReadMolecule(ifs, mol)
    oechem.OETriposAtomNames(mol)

    # Load forcefield file.
    AlkEthOH_offxml_filename = utils.get_data_file_path(
        'test_forcefields/Frosst_AlkEthOH.offxml')
    forcefield = ForceField(AlkEthOH_offxml_filename)

    # Create OpenMM System and Topology.
    off_mol = Molecule.from_openeye(mol, allow_undefined_stereo=True)
    off_top = Topology.from_molecules([off_mol])
    system1 = forcefield.create_openmm_system(off_top)
    topology1 = structure.generateTopologyFromOEMol(mol)
    positions1 = structure.extractPositionsFromOEMol(mol)

    structure.merge_system(topology0,
                           topology1,
                           system0,
                           system1,
                           positions0,
                           positions1,
                           verbose=True)
def master():
    mol = oechem.OEMol()

    ifs = oechem.oemolistream(input_smiles_file)
    ifs.SetConfTest(oechem.OEAbsCanonicalConfTest())

    for pos, mol in enumerate(ifs.GetOEMols()):
        smiles = oechem.OEMol(mol)
        ligand_name = smiles.GetTitle()

        status = MPI.Status()
        comm.recv(source=MPI.ANY_SOURCE, tag=WORKTAG, status=status)
        rank_from = status.Get_source()
        data = (pos, smiles, ligand_name)
        comm.send(data, dest=rank_from, tag=WORKTAG)

        if args.v == 1 and pos % 1000 == 0:
            print("sent", pos, "jobs", flush=True)

    for i in range(1, world_size):
        comm.send([], dest=i, tag=DIETAG)
    comm.Barrier()
Example #35
0
def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <protein> <ligands>" % argv[0])

    protein = oechem.OEMol()

    ifs = oechem.oemolistream()
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])
    oechem.OEReadMolecule(ifs, protein)

    oechem.OEAssignBondiVdWRadii(protein)
    oechem.OEMMFFAtomTypes(protein)
    oechem.OEMMFF94PartialCharges(protein)
    print("protein:   " + protein.GetTitle())

    epsin = 1.0
    bind = oezap.OEBind()
    bind.GetZap().SetInnerDielectric(epsin)
    bind.SetProtein(protein)
    results = oezap.OEBindResults()

    if not ifs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[2])
    ifs.SetConfTest(oechem.OEIsomericConfTest())

    ligand = oechem.OEMol()
    while oechem.OEReadMolecule(ifs, ligand):
        oechem.OEAssignBondiVdWRadii(ligand)
        oechem.OEMMFFAtomTypes(ligand)
        oechem.OEMMFF94PartialCharges(ligand)
        print("ligand:  %s has %d conformers" %
              (ligand.GetTitle(), ligand.NumConfs()))

        for conf in ligand.GetConfs():
            bind.Bind(conf, results)
            print(" conf# %d   be = %f" %
                  (conf.GetIdx(), results.GetBindingEnergy()))
    return 0
Example #36
0
def main(argv=[__name__]):

    itf = oechem.OEInterface(InterfaceData, argv)

    ifname = itf.GetString("-in")
    ofname = itf.GetString("-ringdict")

    ifs = oechem.oemolistream()
    if not ifs.open(ifname):
        oechem.OEThrow.Fatal("Unable to open %s for reading!" % ifname)

    if not oechem.OEIs2DFormat(ifs.GetFormat()):
        oechem.OEThrow.Fatal("Invalid input file format for 2D coordinates!")

    ofs = oechem.oemolostream()
    if not ofs.open(ofname):
        oechem.OEThrow.Fatal("Unable to open %s for writing!" % ofname)

    if ofs.GetFormat() != oechem.OEFormat_OEB:
        oechem.OEThrow.Fatal("Output file has to be OEB format!")

    opts = oechem.OE2DRingDictionaryCreatorOptions()
    opts.SetRetainExistingBuiltInTemplates(itf.GetBool('-retain-built-in'))

    ringdict = oechem.OE2DRingDictionary(opts)

    dots = oechem.OEDots(10000, 100, "molecules")

    for mol in ifs.GetOEGraphMols():
        dots.Update()
        ringdict.AddRings(mol)

    nrrings = ringdict.NumRings()
    oechem.OEThrow.Info("%d ring template(s) have been extracted!" % nrrings)

    if nrrings != 0:
        oechem.OEWrite2DRingDictionary(ofname, ringdict)

    return 0
def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <mol-infile> <mol-outfile>" % argv[0])

    ifs = oechem.oemolistream()
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])

    ofs = oechem.oemolostream()
    if not ofs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[2])

    mol = oechem.OEGraphMol()
    while oechem.OEReadMolecule(ifs, mol):
        if oequacpac.OESetNeutralpHModel(mol):
            oechem.OEWriteMolecule(ofs, mol)
        else:
            msg = ("Unable to set a neutral pH model for molecule: %s" %
                   mol.GetTile())
            oechem.OEThrow.Warning(msg)

    return 0
def main(argv=[__name__]):
    if len(argv) != 3:
        oechem.OEThrow.Usage("%s <protein> <ligand>" % argv[0])

    ifs = oechem.oemolistream()
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])
    protein = oechem.OEGraphMol()
    oechem.OEReadMolecule(ifs, protein)

    if not ifs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[2])
    ligand = oechem.OEGraphMol()
    oechem.OEReadMolecule(ifs, ligand)

    oechem.OEAssignBondiVdWRadii(protein)
    oechem.OEMMFFAtomTypes(protein)
    oechem.OEMMFF94PartialCharges(protein)

    oechem.OEAssignBondiVdWRadii(ligand)
    oechem.OEMMFFAtomTypes(ligand)
    oechem.OEMMFF94PartialCharges(ligand)

    cmplx = oechem.OEGraphMol(protein)
    oechem.OEAddMols(cmplx, ligand)

    epsin = 1.0
    spacing = 0.5
    zap = oezap.OEZap()
    zap.SetInnerDielectric(epsin)
    zap.SetGridSpacing(spacing)

    PrintHeader(protein.GetTitle(), ligand.GetTitle())

    CalcBindingEnergy(zap, protein, ligand, cmplx)
    zap.SetFocusTarget(ligand)
    CalcBindingEnergy(zap, protein, ligand, cmplx)

    return 0
Example #39
0
def read_pdb_file(pdb_file):
    print(f'Reading receptor from {pdb_file}...')

    from openeye import oechem
    ifs = oechem.oemolistream()
    ifs.SetFlavor(oechem.OEFormat_PDB,
                  oechem.OEIFlavor_PDB_Default | oechem.OEIFlavor_PDB_DATA
                  | oechem.OEIFlavor_PDB_ALTLOC)  # noqa

    if not ifs.open(pdb_file):
        oechem.OEThrow.Fatal("Unable to open %s for reading." % pdb_file)

    temp_mol = oechem.OEGraphMol()
    if not oechem.OEReadMolecule(ifs, temp_mol):
        oechem.OEThrow.Fatal("Unable to read molecule from %s." % pdb_file)
    ifs.close()

    fact = oechem.OEAltLocationFactory(temp_mol)
    mol = oechem.OEGraphMol()
    mol.Clear()
    fact.MakePrimaryAltMol(mol)
    return (mol)
Example #40
0
def GetUrlSDF2SMI(url, fout, ntries=20, poll_wait=10):
    '''Get PubChem SDF.GZ, convert to SMILES using the SD tag PUBCHEM_OPENEYE_CAN_SMILES
or PUBCHEM_OPENEYE_ISO_SMILES, and PUBCHEM_COMPOUND_CID or PUBCHEM_COMPOUND_SID for name.'''
    import openeye.oechem as oechem

    def HandleOEErrors(oeerrs, nowarn):
        errstr = oeerrs.str()
        for line in errstr.split('\n'):
            if not line.rstrip(): continue
            if re.search('Warning', line, re.I) and nowarn: continue
            sys.stderr.write("%s\n" % line)
        oeerrs.clear()

    fout_tmp = tempfile.NamedTemporaryFile(prefix='pubchem_ftp_',
                                           suffix='.sdf.gz',
                                           delete=False)
    GetUrl(url, fout_tmp, ntries, poll_wait)
    fpath_tmp = fout_tmp.name
    logging.debug('fpath_tmp = %s' % fpath_tmp)
    fout_tmp.close()

    ims = oechem.oemolistream(fpath_tmp)
    ims.SetFormat(oechem.OEFormat_SDF)
    ims.Setgz(True)

    mol = oechem.OEGraphMol()
    nbytes = 0
    oeerrs = oechem.oeosstream()
    oechem.OEThrow.SetOutputStream(oeerrs)
    while oechem.OEReadMolecule(ims, mol):
        cid = oechem.OEGetSDData(mol, 'PUBCHEM_COMPOUND_CID')
        cansmi = oechem.OEGetSDData(mol, 'PUBCHEM_OPENEYE_CAN_SMILES')
        isosmi = oechem.OEGetSDData(mol, 'PUBCHEM_OPENEYE_ISO_SMILES')
        buff = ("%s %s\n" % (isosmi, cid))
        fout.write(buff)
        nbytes += len(buff)
        HandleOEErrors(oeerrs, True)
    os.remove(fpath_tmp)
    return nbytes
Example #41
0
def smi2indivSdf(wdir, smiles):
    """
    From a file containing smiles strings, generate omega conformers,
       resolve steric clashes, do a quick MM opt, and write SDF output
       as individual files.

    Parameters
    ----------
    wdir: str - working directory containing .smi file
    smiles: str - name of the smiles file. E.g. "name.smi"

    """
    os.chdir(wdir)

    ### Read in smiles file.
    ifs = oechem.oemolistream()
    if not ifs.open(smiles):
        oechem.OEThrow.Warning("Unable to open %s for reading" % smiles)


    ### For each molecule: label atoms, generate 1 conf, write output.
    for i, smimol in enumerate(ifs.GetOEMols()):
        oechem.OETriposAtomNames(smimol)
        mol = GenerateConfs(smimol)

        ### Open output file to write molecules.
        sdfout = smiles.split('.')[0] + '-%d.sdf' %(i)
        ofs = oechem.oemolostream()
        if os.path.exists(sdfout):
            #sys.exit("Output .sdf file already exists. Exiting.\n")
            print("Output .sdf file already exists. Exiting.\n")
            return
        if not ofs.open(sdfout):
            oechem.OEThrow.Fatal("Unable to open %s for writing" % sdfout)
        oechem.OEWriteConstMolecule(ofs, mol)
        ofs.close()

    ### Close files.
    ifs.close()
def load_molecules(filename):
    """
    Read molecules from the specified file

    Parameters
    ----------
    filename : str
        The file name from which to read molecules in a format that OpenEye supports

    Returns
    -------
    target_molecules : list of OEMol
        The list of molecules read (without conformers if none were provided in file)
    """
    from openeye import oechem
    target_molecules = list()
    with oechem.oemolistream(target_molecules_filename) as ifs:
        for mol in ifs.GetOEGraphMols():
            # Store a copy
            target_molecules.append(oechem.OEGraphMol(mol))

    return target_molecules
Example #43
0
    def __init__(self, pdb_file, xmax, ymax, zmax, xmin, ymin, zmin):
        self.receptor = oechem.OEGraphMol()
        self.scorers = {
            'Shapegauss': oedocking.OEScore(oedocking.OEScoreType_Shapegauss),
            'Chemscore': oedocking.OEScore(oedocking.OEScoreType_Chemscore),
            'Chemgauss': oedocking.OEScore(oedocking.OEScoreType_Chemgauss),
            'Chemgauss3': oedocking.OEScore(oedocking.OEScoreType_Chemgauss3),
            'Chemgauss4': oedocking.OEScore(oedocking.OEScoreType_Chemgauss4),
        }

        proteinStructure = oechem.OEGraphMol()
        ifs = oechem.oemolistream(pdb_file)
        ifs.SetFormat(oechem.OEFormat_PDB)
        oechem.OEReadMolecule(ifs, proteinStructure)

        box = oedocking.OEBox(xmax, ymax, zmax, xmin, ymin, zmin)

        receptor = oechem.OEGraphMol()
        s = oedocking.OEMakeReceptor(receptor, proteinStructure, box)
        for _, score in self.scorers.items():
            assert (s != False)
            score.Initialize(receptor)
def ParameterizeOE(path):
    """
    Reads in the PDB from 'RunDocking' and outputs 'charged.mol2' of the ligand
    Then runs antechamber to convert this to coordinate (.inpcrd) and 
    parameter (.prmtop) files.
    """
    from openeye import oechem, oeomega, oequacpac
    mol = oechem.OEMol()
    ifs = oechem.oemolistream()
    if ifs.open(f'{path}/lig.pdb'):
        oechem.OEReadMolecule(ifs, mol)
        ifs.close()
    if not oequacpac.OEAssignCharges(mol, oequacpac.OEAM1BCCCharges()):
        raise (RuntimeError("OEAssignCharges failed."))
    ofs = oechem.oemolostream()
    if ofs.open(f'{path}/charged.mol2'):
        oechem.OEWriteMolecule(ofs, mol)

    import subprocess
    with working_directory(path):
        subprocess.check_output(
            f'antechamber -i lig.pdb -fi pdb -o lig.mol2 -fo mol2 -pf y -an y -a charged.mol2 -fa mol2 -ao crg',
            shell=True)
        subprocess.check_output(f'parmchk2 -i lig.mol2 -f mol2 -o lig.frcmod',
                                shell=True)
        # Wrap tleap
        with open(f'leap.in', 'w+') as leap:
            leap.write("source leaprc.protein.ff14SBonlysc\n")
            leap.write("source leaprc.gaff\n")
            leap.write("set default PBRadii mbondi3\n")
            leap.write("rec = loadPDB apo.pdb # May need full filepath?\n")
            leap.write("saveAmberParm rec apo.prmtop apo.inpcrd\n")
            leap.write("lig = loadmol2 lig.mol2\n")
            leap.write("loadAmberParams lig.frcmod\n")
            leap.write("com = combine {rec lig}\n")
            leap.write("saveAmberParm lig lig.prmtop lig.inpcrd\n")
            leap.write("saveAmberParm com com.prmtop com.inpcrd\n")
            leap.write("quit\n")
        subprocess.check_output(f'tleap -f leap.in', shell=True)
Example #45
0
    def __init__(self, ipf, keepAlts=F, verbose=T):
        self.ipf = ipf
        self.keepAlts = keepAlts
        self.verbose = verbose

        flavor = oechem.OEIFlavor_PDB_Default
        ims = oechem.oemolistream()
        ims.SetFlavor(oechem.OEFormat_PDB, flavor)

        if not ims.open(self.ipf):
            oechem.OEThrow.Fatal("Unable to open %s for reading." % self.ipf)

        if not oechem.OEIs3DFormat(ims.GetFormat()):
            oechem.OEThrow.Fatal("%s is not in a 3D format." % self.ipf)

        iftp = oechem.OEGetFileType(oechem.OEGetFileExtension(self.ipf))
        if (iftp == oechem.OEFormat_PDB) and not self.keepAlts:
            oechem.OEThrow.Verbose(
                "Default processing of alt locations (keep just 'A' and ' ').")

        inmol = oechem.OEGraphMol()
        if not oechem.OEReadMolecule(ims, inmol):
            oechem.OEThrow.Fatal("Unable to read %s." % self.ipf)

        ims.close()

        if (inmol.NumAtoms() == 0):
            oechem.OEThrow.Fatal("Input molecule %s contains no atoms." %
                                 self.ipf)

        if inmol.GetTitle() == "":
            inmol.SetTitle(ipf[:-4])

        oechem.OEThrow.Verbose("Processing %s." % inmol.GetTitle())
        if not oechem.OEHasResidues(inmol):
            oechem.OEPerceiveResidues(inmol, oechem.OEPreserveResInfo_All)

        self.inmol = inmol
        self.mol = inmol.CreateCopy()
Example #46
0
def main(argv=sys.argv):
    if len(argv) != 3:
        oechem.OEThrow.Usage(
            "%s <infile (forcefield types)> <outfile (Tripos types)>" %
            argv[0])

    ifs = oechem.oemolistream()
    flavor = oechem.OEIFlavor_Generic_Default | oechem.OEIFlavor_MOL2_Default | oechem.OEIFlavor_MOL2_Forcefield
    ifs.SetFlavor(oechem.OEFormat_MOL2, flavor)
    if not ifs.open(argv[1]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1])

    ofs = oechem.oemolostream()
    if not ofs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[2])

    for mol in ifs.GetOEMols():
        oechem.OETriposAtomNames(mol)
        oechem.OEWriteConstMolecule(ofs, mol)

    ifs.close()
    ofs.close()
Example #47
0
def read_oe_molecule(file_path, conformer_idx=None):
    from openeye import oechem

    # Open input file stream
    ifs = oechem.oemolistream()
    if not ifs.open(file_path):
        oechem.OEThrow.Fatal('Unable to open {}'.format(file_path))

    # Read all conformations
    for mol in ifs.GetOEMols():
        try:
            molecule.NewConf(mol)
        except UnboundLocalError:
            molecule = oechem.OEMol(mol)

    # Select conformation of interest
    if conformer_idx is not None:
        if molecule.NumConfs() <= conformer_idx:
            raise ValueError('conformer_idx {} out of range'.format(conformer_idx))
        molecule = oechem.OEGraphMol(molecule.GetConf(oechem.OEHasConfIdx(conformer_idx)))

    return molecule
Example #48
0
def confs2psi(insdf, method, basis, spe=False, memory=None):
    """
    Parameters
    ----------
    insdf:  string - PATH+name of SDF file
    method: string - method. E.g. "mp2"
    basis:  string - basis set. E.g. "def2-sv(p)"
    spe:    boolean. True for single point energy calcns, False for geom opt.
            default option is False.
    memory: string - memory specification. Psi4 default is 256 Mb. E.g. "1.5 Gb"

    """
    wdir = os.getcwd()

    ### Read in .sdf file and distinguish each molecule's conformers
    ifs = oechem.oemolistream()
    ifs.SetConfTest( oechem.OEAbsoluteConfTest() )
    if not ifs.open(insdf):
        oechem.OEThrow.Warning("Unable to open %s for reading" % insdf)
        return

    ### For each molecule: for each conf, generate input
    for mol in ifs.GetOEMols():
        print(mol.GetTitle(), mol.NumConfs())
        if not mol.GetTitle():
            sys.exit("ERROR: OEMol must have title assigned! Exiting.")
        for i, conf in enumerate( mol.GetConfs()):
            # change into subdirectory ./mol/conf/
            subdir = os.path.join(wdir,"%s/%s" % (mol.GetTitle(), i+1))
            if not os.path.isdir(subdir):
                os.makedirs(subdir)
            if os.path.exists(os.path.join(subdir,'input.dat')):
                print("Input file (\"input.dat\") already exists. Skipping.\n")
                continue
            label = mol.GetTitle()+'_'+str(i+1)
            ofile = open(os.path.join(subdir,'input.dat'), 'w')
            ofile.write(make_psi_input( conf, label, method, basis, spe, memory))
            ofile.close()
    ifs.close()
Example #49
0
    def test_success(self):
        print('Testing cube:', self.cube.name)
        # File name
        fname = ommutils.get_data_filename('examples', 'data/Bace_protein.pdb')

        # Read OEMol molecule
        mol = oechem.OEMol()

        with oechem.oemolistream(fname) as ifs:
            oechem.OEReadMolecule(ifs, mol)

        atom_i = mol.GetMaxAtomIdx()

        # Process the molecules
        self.cube.process(mol, self.cube.intake.name)
        # Assert that one molecule was emitted on the success port
        self.assertEqual(self.runner.outputs['success'].qsize(), 1)
        # Assert that zero molecules were emitted on the failure port
        self.assertEqual(self.runner.outputs['failure'].qsize(), 0)

        outmol = self.runner.outputs["success"].get()

        atom_f = outmol.GetMaxAtomIdx()

        self.assertEquals(atom_i, atom_f)

        # Check Total number of atoms
        prot, lig, wat, other = utils.split(outmol)

        # Check protein, ligand, excipients and water number of atoms
        npa = prot.GetMaxAtomIdx()
        nla = lig.GetMaxAtomIdx()
        noa = other.GetMaxAtomIdx()
        nwa = wat.GetMaxAtomIdx()

        self.assertEquals(npa, 6044)
        self.assertEquals(nla, 0)
        self.assertEquals(noa, 14)
        self.assertEquals(nwa, 57)
Example #50
0
def main(argv=[__name__]):
    if len(argv) != 4:
        oechem.OEThrow.Usage("%s <csvfile> <infile> <outsdfile>" % argv[0])
    try:
        csvfile = open(argv[1])
    except Exception:
        oechem.OEThrow.Fatal("Unable to open %s csv for reading" % argv[1])

    ifs = oechem.oemolistream()
    if not ifs.open(argv[2]):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[2])

    ofs = oechem.oemolostream()
    if not ofs.open(argv[3]):
        oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[3])

    if ofs.GetFormat() not in [oechem.OEFormat_SDF, oechem.OEFormat_OEB]:
        oechem.OEThrow.Fatal("Only works for sdf or oeb output files")

    CSV2SDF(csvfile, ifs, ofs)

    csvfile.close()
Example #51
0
    def test_generat_torsions(self):
        """ Tests finding torsion to drive """
        from openeye import oechem
        infile = get_fn('butane.pdb')
        ifs = oechem.oemolistream(infile)
        inp_mol = oechem.OEMol()
        oechem.OEReadMolecule(ifs, inp_mol)
        outfile_path = tempfile.mkdtemp()[1]
        qmscan.generate_torsions(inp_mol=inp_mol, output_path=outfile_path, interval=30, tar=False)
        input_files = []
        pattern = '*.pdb'
        for path, subdir, files in os.walk(outfile_path):
            for name in files:
                if fnmatch(name, pattern):
                    input_files.append(os.path.join(path, name))

        contents = open(input_files[0]).read()
        pdb = get_fn('butane_10_7_4_3_0.pdb')
        compare_contents = open(pdb).read()
        self.assertEqual(contents, compare_contents )

        shutil.rmtree(outfile_path)
Example #52
0
    def deserializeOe(self, oeS):
        """Reconstruct an OE molecule from the input string serialization (OE binary).

        The deserialized molecule is used to initialize the internal OE molecule
        within this object.

        Returns:
            list:  OE GraphMol list
        """
        molList = []
        try:
            ims = oechem.oemolistream()
            ims.SetFormat(oechem.OEFormat_OEB)
            ims.openstring(oeS)
            for mol in ims.GetOEGraphMols():
                logger.debug("SMILES %s", oechem.OECreateCanSmiString(mol))
                logger.debug("title  %s", mol.GetTitle())
                logger.debug("atoms  %d", mol.NumAtoms())
                molList.append(oechem.OEGraphMol(mol))
        except Exception as e:
            logger.exception("Failing with %s", str(e))
        return molList
Example #53
0
    def request_structure(pdb_id, file_format):

        # request structure
        if file_format == 'mmcif':
            url = f'https://files.rcsb.org/download/{pdb_id}.cif'
        elif file_format == 'pdb':
            url = f'https://files.rcsb.org/download/{pdb_id}.pdb'
        else:
            print(f'Unsupported file format: {file_format}. Returning None')
            return None
        response = requests.get(url)

        # store structure in temporary file
        with tempfile.NamedTemporaryFile(suffix='.' +
                                         file_format) as temp_file:
            temp_file.write(response.content)

            # read structure from temporary file
            with oechem.oemolistream() as ifs:
                if file_format == 'mmcif':
                    ifs.SetFlavor(oechem.OEFormat_MMCIF,
                                  oechem.OEIFlavor_MMCIF_Default)
                elif file_format == 'pdb':
                    ifs.SetFlavor(
                        oechem.OEFormat_PDB, oechem.OEIFlavor_PDB_Default
                        | oechem.OEIFlavor_PDB_DATA
                        | oechem.OEIFlavor_PDB_ALTLOC)
                else:
                    print(
                        f'Unsupported file format: {file_format}. Returning None'
                    )
                    return None

                ifs.open(temp_file.name)
                mol = oechem.OEGraphMol()
                if not oechem.OEReadMolecule(ifs, mol):
                    mol = None

        return mol
Example #54
0
    def read_file (filename):
        """
        Reads a .mol2 file and returns title of molecule , base molecule object 
and `OEMol' objects.
        @type  filename: C{str}                               
        @param filename: Name of the structure file
        """
        ret = []
        istream = oechem.oemolistream()
        istream.open(filename)                                
                                                              
        molecule = oechem.OEMol()                             
                                                              
        oechem.OEReadMolecule(istream, molecule)              
                                                              
        istream.close()                                       
        struc = OeStruc(molecule)                             
        for i in range(1, len(struc.atom) + 1) :              
            struc.atom_prop[i]["orig_index"] = i        
        ret.append(struc)                                     
                                                                      
        return ret
Example #55
0
def test_change_parameters(verbose=False):
    """Test modification of forcefield parameters."""
    from openeye import oechem
    # Load simple OEMol
    ifs = oechem.oemolistream(get_data_filename('molecules/AlkEthOH_c100.mol2'))
    mol = oechem.OEMol()
    flavor = oechem.OEIFlavor_Generic_Default | oechem.OEIFlavor_MOL2_Default | oechem.OEIFlavor_MOL2_Forcefield
    ifs.SetFlavor( oechem.OEFormat_MOL2, flavor)
    oechem.OEReadMolecule(ifs, mol )
    oechem.OETriposAtomNames(mol)

    # Load forcefield file
    ffxml = get_data_filename('forcefield/Frosst_AlkEtOH.ffxml')
    ff = ForceField(ffxml)

    from smarty.forcefield import generateTopologyFromOEMol
    topology = generateTopologyFromOEMol(mol)
    # Create initial system
    system = ff.createSystem(topology, [mol], verbose=verbose)
    # Get initial energy before parameter modification
    positions = positions_from_oemol(mol)
    old_energy=get_energy(system, positions)

    # Get params for an angle
    params = ff.getParameter(smirks='[a,A:1]-[#6X4:2]-[a,A:3]')
    # Modify params
    params['k']='0.0'
    ff.setParameter(params, smirks='[a,A:1]-[#6X4:2]-[a,A:3]')
    # Write params
    ff.writeFile( tempfile.TemporaryFile(suffix='.ffxml') )
    # Make sure params changed energy! (Test whether they get rebuilt on system creation)
    system=ff.createSystem(topology, [mol], verbose=verbose)
    energy=get_energy(system, positions)
    if verbose:
        print("New energy/old energy:", energy, old_energy)
    if np.abs(energy-old_energy)<0.1:
        raise Exception("Error: Parameter modification did not change energy.")
Example #56
0
    def test_mapped_xyz(self):
        """Test writing out mapped xyz"""
        from openeye import oechem, oeomega
        tagged_smiles = '[H:10][c:4]1[c:3]([c:2]([c:1]([c:6]([c:5]1[H:11])[H:12])[C:7]([H:13])([H:14])[H:15])[H:8])[H:9]'
        mol_1 = openeye.smiles_to_oemol('Cc1ccccc1')
        inf = get_fn('toluene.mol2')
        ifs = oechem.oemolistream(inf)
        mol_2 = oechem.OEMol()
        oechem.OEReadMolecule(ifs, mol_2)

        atom_map = utils.get_atom_map(tagged_smiles, mol_1)
        for i, mapping in enumerate(atom_map):
            atom_1 = mol_1.GetAtom(oechem.OEHasAtomIdx(atom_map[mapping]))
            atom_1.SetAtomicNum(i+1)
            atom_2 = mol_2.GetAtom(oechem.OEHasAtomIdx(mapping-1))
            atom_2.SetAtomicNum(i+1)

        xyz_1 = utils.to_mapped_xyz(mol_1, atom_map)
        # molecule generated from mol2 should be in the right order.
        atom_map_mol2 = {1:0, 2:1, 3:2, 4:3, 5:4, 6:5, 7:6, 8:7, 9:8, 10:9, 11:10, 12:11, 13:12, 14:13, 15:14}
        xyz_2 = utils.to_mapped_xyz(mol_2, atom_map_mol2)

        for ele1, ele2 in zip(xyz_1.split('\n')[:-1], xyz_2.split('\n')[:-1]):
            self.assertEqual(ele1.split(' ')[2], ele2.split(' ')[2])
Example #57
0
def mol_to_tagged_smiles(infile, outfile):
    """
    Generate .smi from input mol with index-tagged explicit hydrogen SMILES
    Parameters
    ----------
    infile: str
        input molecule file
    outfile: str
        output smi file. Must be smi or ism

    """
    ifs = oechem.oemolistream()
    if not ifs.open(infile):
        oechem.OEThrow.Fatal("Unable to open {} for reading".format(infile))

    ofs = oechem.oemolostream()
    if not ofs.open(outfile):
        oechem.OEThrow.Fatal("Unable to open {} for writing".format(outfile))
    if ofs.GetFormat() not in [oechem.OEFormat_ISM, oechem.OEFormat_SMI]:
        oechem.OEThrow.Fatal("Output format must be SMILES")

    for mol in ifs.GetOEMols():
        smiles = create_mapped_smiles(mol)
        oechem.OEWriteMolecule(ofs, mol)
def enumerate_conformations(name, smiles=None, pdbname=None):
    """Run Epik to get protonation states using PDB residue templates for naming.

    Parameters
    ----------
    name : str
       Common name of molecule (used to create subdirectory)
    smiles : str
       Isomeric SMILES string
    pdbname : str
       Three-letter PDB code (e.g. 'DB8')
    """
    # Create output subfolder
    output_basepath = os.path.join(output_dir, name)
    if not os.path.isdir(output_basepath):
        os.mkdir(output_basepath)
    output_basepath = os.path.join(output_basepath, name)

    if pdbname:
        # Make sure to only use one entry if there are mutliple
        if ' ' in pdbname:
            pdbnames = pdbname.split(' ')
            print("Splitting '%s' into first entry only: '%s'" % (pdbname, pdbnames[0]))
            pdbname = pdbnames[0]

        # Retrieve PDB (for atom names)
        url = 'http://ligand-expo.rcsb.org/reports/%s/%s/%s_model.pdb' % (pdbname[0], pdbname, pdbname)
        pdb_filename = output_basepath + '-input.pdb'
        retrieve_url(url, pdb_filename)
        pdb_molecule = read_molecule(pdb_filename)

        # Retrieve SDF (for everything else)
        url = 'http://ligand-expo.rcsb.org/reports/%s/%s/%s_model.sdf' % (pdbname[0], pdbname, pdbname)
        sdf_filename = output_basepath + '-input.sdf'
        retrieve_url(url, sdf_filename)
        sdf_molecule = read_molecule(sdf_filename)

        # Replace atom names in SDF
        for (sdf_atom, pdb_atom) in zip(sdf_molecule.GetAtoms(), pdb_molecule.GetAtoms()):
            sdf_atom.SetName(pdb_atom.GetName())
        # Assign Tripos atom types
        oechem.OETriposAtomTypeNames(sdf_molecule)
        oechem.OETriposBondTypeNames(sdf_molecule)

        oe_molecule = sdf_molecule

        # We already know the residue name
        residue_name = pdbname
    elif smiles:
        # Generate molecule geometry with OpenEye
        print("Generating molecule {}".format(name))
        oe_molecule = openeye.smiles_to_oemol(smiles)
        # Assign Tripos atom types
        oechem.OETriposAtomTypeNames(oe_molecule)
        oechem.OETriposBondTypeNames(oe_molecule)
        try:
            oe_molecule = openeye.get_charges(oe_molecule, keep_confs=1)
        except RuntimeError as e:
            traceback.print_exc()
            print("Skipping molecule " + name)
            return
        residue_name = re.sub('[^A-Za-z]+', '', name.upper())[:3]
    else:
        raise Exception('Must provide SMILES string or pdbname')

    # Save mol2 file, preserving atom names
    print("Running epik on molecule {}".format(name))
    mol2_file_path = output_basepath + '-input.mol2'
    write_mol2_preserving_atomnames(mol2_file_path, oe_molecule, residue_name)

    # Run epik on mol2 file
    mae_file_path = output_basepath + '-epik.mae'
    schrodinger.run_epik(mol2_file_path, mae_file_path, tautomerize=False,
                         max_structures=100, min_probability=np.exp(-MAX_ENERGY_PENALTY), ph=7.4)

    # Convert maestro file to sdf and mol2
    output_sdf_filename = output_basepath + '-epik.sdf'
    output_mol2_filename = output_basepath + '-epik.mol2'
    schrodinger.run_structconvert(mae_file_path, output_sdf_filename)
    schrodinger.run_structconvert(mae_file_path, output_mol2_filename)

    # Read SDF file.
    ifs_sdf = oechem.oemolistream()
    ifs_sdf.SetFormat(oechem.OEFormat_SDF)
    ifs_sdf.open(output_sdf_filename)
    sdf_molecule = oechem.OEGraphMol()

    # Read MOL2 file.
    ifs_mol2 = oechem.oemolistream()
    ifs_mol2.open(output_mol2_filename)
    mol2_molecule = oechem.OEMol()

    # Assign charges.
    charged_molecules = list()
    index = 0
    while oechem.OEReadMolecule(ifs_sdf, sdf_molecule):
        oechem.OEReadMolecule(ifs_mol2, mol2_molecule)

        index += 1
        print("Charging molecule %d" % (index))
        try:
            # Charge molecule.
            charged_molecule = openeye.get_charges(mol2_molecule, max_confs=800, strictStereo=False, normalize=True, keep_confs=None)
            # Assign Tripos types
            oechem.OETriposAtomTypeNames(charged_molecule)
            oechem.OETriposBondTypeNames(charged_molecule)
            # Store tags.
            oechem.OECopySDData(charged_molecule, sdf_molecule)
            # Store molecule
            charged_molecules.append(charged_molecule)
        except Exception as e:
            print(e)
            print("Skipping protomer/tautomer because of failed charging.")

    # Clean up
    ifs_sdf.close()
    ifs_mol2.close()

    # Write state penalites.
    outfile = open(output_basepath + '-state-penalties.out', 'w')
    for (index, charged_molecule) in enumerate(charged_molecules):
        # Get Epik data.
        epik_Ionization_Penalty = float(oechem.OEGetSDData(charged_molecule, "r_epik_Ionization_Penalty"))
        epik_Ionization_Penalty_Charging = float(oechem.OEGetSDData(charged_molecule, "r_epik_Ionization_Penalty_Charging"))
        epik_Ionization_Penalty_Neutral = float(oechem.OEGetSDData(charged_molecule, "r_epik_Ionization_Penalty_Neutral"))
        epik_State_Penalty = float(oechem.OEGetSDData(charged_molecule, "r_epik_State_Penalty"))
        epik_Tot_Q = int(oechem.OEGetSDData(charged_molecule, "i_epik_Tot_Q"))

        outfile.write('%16.8f\n' % epik_State_Penalty)
    outfile.close()

    # Write as PDB
    charged_pdb_filename = output_basepath + '-epik-charged.pdb'
    ofs = oechem.oemolostream(charged_pdb_filename)
    flavor = oechem.OEOFlavor_PDB_CurrentResidues | oechem.OEOFlavor_PDB_ELEMENT | oechem.OEOFlavor_PDB_BONDS | oechem.OEOFlavor_PDB_HETBONDS | oechem.OEOFlavor_PDB_BOTH
    ofs.SetFlavor(oechem.OEFormat_PDB, flavor)
    for (index, charged_molecule) in enumerate(charged_molecules):
        # Fix residue names
        for atom in charged_molecule.GetAtoms():
            residue = oechem.OEAtomGetResidue(atom)
            residue.SetName(residue_name)
            oechem.OEAtomSetResidue(atom, residue)

        #oechem.OEWritePDBFile(ofs, charged_molecule, flavor)
        oechem.OEWriteMolecule(ofs, charged_molecule)
    ofs.close()

    # Write molecules as mol2.
    charged_mol2_filename = output_basepath + '-epik-charged.mol2'
    write_mol2_preserving_atomnames(charged_mol2_filename, charged_molecules, residue_name)
#!/usr/bin/env python
"""
Generate ffxml files for ligands in a multi-mole mol2.
"""

import openeye
import openmoltools

mol2_filename = 'Imatinib-epik-charged.mol2'
ffxml_filename = 'Imatinib-epik-charged.ffxml'

# Read mol2 file containing protonation states and extract canonical isomeric SMILES from this.
print("Reading molecules")
from openeye import oechem
ifs = oechem.oemolistream(mol2_filename)
mol = oechem.OEMol()
molecules = list()
while oechem.OEReadMolecule(ifs, mol):
    molecules.append(oechem.OEMol(mol))

print("Generating forcefield parameters...")
from openmoltools.forcefield_generators import generateForceFieldFromMolecules
ffxml = generateForceFieldFromMolecules(molecules, ignoreFailures=False, generateUniqueNames=True)

print("Writing forcefield to '%s'..." % ffxml_filename)
outfile = open(ffxml_filename, 'w')
outfile.write(ffxml)
outfile.close()
def _read_mol2(mol2_filename):
    mol = oechem.OEMol()
    istream = oechem.oemolistream()
    istream.open(mol2_filename)
    oechem.OEReadMol2File(istream, mol)
    return mol