Exemple #1
0
    def testOBMolSeparatePreservesAtomOrder(self):
        """Originally Separate() preserved DFS order rather
        than atom order"""
        # First test
        smi = "C123.F3.Cl2.Br1"
        mol = pybel.readstring("smi", smi)
        atomicnums = [atom.OBAtom.GetAtomicNum() for atom in mol]
        mols = mol.OBMol.Separate()
        new_atomicnums = [
            atom.OBAtom.GetAtomicNum() for atom in pybel.Molecule(mols[0])
        ]
        for x, y in zip(atomicnums, new_atomicnums):
            self.assertEqual(x,
                             y)  # check that the atoms have not been permuted
        # Second test
        xyz = """6
examples/water_dimer.xyz
O          0.12908       -0.26336        0.64798
H          0.89795        0.28805        0.85518
H          0.10833       -0.20468       -0.33302
O          0.31020        0.07569       -2.07524
H          0.64083       -0.57862       -2.71449
H         -0.26065        0.64232       -2.62218
"""
        mol = pybel.readstring("xyz", xyz)
        mols = mol.OBMol.Separate()
        allatoms = pybel.Molecule(mols[0]).atoms + pybel.Molecule(
            mols[1]).atoms
        for idx, atom in enumerate(allatoms):
            xcoord = atom.OBAtom.GetX()
            orig_xcoord = mol.OBMol.GetAtom(idx + 1).GetX()
            self.assertEqual(xcoord, orig_xcoord)
Exemple #2
0
def copyMolecule(molecule):
    '''Takes a pybel molecule as input.
  Effectively returns a deep copy of the pybel molecule submitted'''
    #using copy.deepcopy didnt return a usable molecule
    newmolecule = pb.Molecule(pb.ob.OBMol(
        molecule.OBMol))  #converts pybel into OpenBabel and back
    return newmolecule
def getSmiles(mol):
    '''
    This function returns the SMILES string for a molecule
    '''
    a = BabelMolAdaptor(mol)
    pba = pb.Molecule(a.openbabel_mol)
    return pba.write("can").split()[0]
Exemple #4
0
def layout(p):
    """Calculate points positions for depiction of Pharmacophore p using OpenBabel."""
    if not isinstance(p, Pharmacophore):
        raise TypeError("Expected Pharmacophore object, got %s instead" %
                        type(p).__name__)

    positions = np.zeros((p.numnodes, 2))
    m = pybel.Molecule(ob.OBMol())
    for i in range(p.numnodes):
        m.OBMol.NewAtom()
    idx = p.numnodes + 1
    for i in range(p.numnodes):
        for j in range(i):
            if p.edges[i, j] > 0:
                tmp = int(math.ceil(p.edges[i, j])) - 1
                prev = i + 1

                # add invisible atoms to get right distance
                for k in range(tmp):
                    atom = m.OBMol.NewAtom(idx)
                    atom.SetHyb(1)
                    m.OBMol.AddBond(prev, idx, 1)
                    prev = idx
                    idx += 1
                m.OBMol.AddBond(prev, j + 1, 1)
    m.draw(show=False, update=True)

    for i in range(p.numnodes):
        positions[i][0] = m.atoms[i].coords[0]
        positions[i][1] = m.atoms[i].coords[1]
    return positions
Exemple #5
0
def average_properties(smiles):
    """
    Calculate all relevant properties for a given molecule averaged across conformers

    :param mol: input molecule smiles
    :type mol: openbabel.OBMol
    :return: dictionary of properties
    :rtype dict

    ..todo: remove reliance on pybel
    """
    mol = smiles_to_ob(smiles)
    mols = run_confab(mol)
    num_confs = mols.NumConformers()

    globs = np.empty(num_confs)
    # pbfs = np.empty(num_confs)
    for i in range(num_confs):
        mols.SetConformer(i)
        pymol = pybel.Molecule(mols)
        # calculate properties
        globs[i] = calc_glob(pymol)
        # pbfs[i] = calc_pbf(pymol)

    data = {
        'rotatable_bonds': rotatable_bonds(pymol),
        'globularity': np.mean(globs),
        'primary_amine': has_primary_amine(pymol),
        # 'pbf': np.mean(pbfs)
    }
    return data
Exemple #6
0
    def restore_site_properties(self, site_property="ff_map", filename=None):
        """
        Restore the site properties for the final packed molecule.

        Args:
            site_property (str):
            filename (str): path to the final packed molecule.

        Returns:
            Molecule
        """

        # only for pdb
        if not self.control_params["filetype"] == "pdb":
            raise

        filename = filename or self.control_params["output"]
        bma = BabelMolAdaptor.from_file(filename, "pdb")
        pbm = pb.Molecule(bma._obmol)

        assert len(pbm.residues) == sum([x["number"] for x in self.param_list])

        packed_mol = self.convert_obatoms_to_molecule(
            pbm.residues[0].atoms, residue_name=pbm.residues[0].name,
            site_property=site_property)

        for resid in pbm.residues[1:]:
            mol = self.convert_obatoms_to_molecule(
                resid.atoms, residue_name=resid.name, site_property=site_property)
            for site in mol:
                packed_mol.append(site.species_and_occu, site.coords,
                                  properties=site.properties)

        return packed_mol
Exemple #7
0
def json_to_pybel(data, center=True):
    """Converts python data structure to pybel.Molecule.

    This will infer bond data if not specified.

    Args:
        data: The loaded json data of a molecule, as a Python object
        center: (Optional) Centers the coordinates of the outputted molecule
    Returns:
        An instance of `pybel.Molecule`
    """
    obmol = ob.OBMol()
    obmol.BeginModify()
    for atom in data["atoms"]:
        obatom = obmol.NewAtom()
        obatom.SetAtomicNum(table.GetAtomicNum(str(atom["element"])))
        obatom.SetVector(*atom["location"])
        if "charge" in atom:
            obatom.SetPartialCharge(atom["charge"])
    # If there is no bond data, try to infer them
    if "bonds" not in data or not data["bonds"]:
        obmol.ConnectTheDots()
        obmol.PerceiveBondOrders()
    # Otherwise, use the bonds in the data set
    else:
        for bond in data["bonds"]:
            if "atoms" not in bond:
                continue
            obmol.AddBond(bond["atoms"][0] + 1, bond["atoms"][1] + 1,
                          bond["order"])
    obmol.EndModify()
    if center:
        obmol.Center()
    return pybel.Molecule(obmol)
Exemple #8
0
 def getFormula(self):
     """
     Return the molecular formula for the molecule.
     """
     import pybel
     mol = pybel.Molecule(self.toOBMol())
     return mol.formula
def test_primary_amine_benzenesulfonamide():
    mol = calc_props.smiles_to_ob("O=S(C1=CC=CC=C1)(N)=O")
    pymol = pybel.Molecule(mol)
    primary_amine_smarts = calc_props.FUNCTIONAL_GROUP_TO_SMARTS[
        "primary_amine"]
    assert_equals(calc_props.has_functional_group(pymol, primary_amine_smarts),
                  False)
Exemple #10
0
    def _write_input(self, input_dir="."):
        """
        Write the packmol input file to the input directory.

        Args:
            input_dir (string): path to the input directory
        """
        with open(os.path.join(input_dir, self.input_file), 'wt', encoding="utf-8") as inp:
            for k, v in six.iteritems(self.control_params):
                inp.write('{} {}\n'.format(k, self._format_param_val(v)))
            # write the structures of the constituent molecules to file and set
            # the molecule id and the corresponding filename in the packmol
            # input file.
            for idx, mol in enumerate(self.mols):
                a = BabelMolAdaptor(mol)
                pm = pb.Molecule(a.openbabel_mol)
                filename = os.path.join(
                    input_dir, '{}.{}'.format(
                        idx, self.control_params["filetype"])).encode("ascii")
                pm.write(self.control_params["filetype"], filename=filename,
                         overwrite=True)
                inp.write("\n")
                inp.write(
                    "structure {}.{}\n".format(
                        os.path.join(input_dir, str(idx)),
                        self.control_params["filetype"]))
                for k, v in six.iteritems(self.param_list[idx]):
                    inp.write('  {} {}\n'.format(k, self._format_param_val(v)))
                inp.write('end structure\n')
def test_primary_amine_methylethyl_ketone_oxime():
    mol = calc_props.smiles_to_ob("CC/C(C)=N/O")
    pymol = pybel.Molecule(mol)
    primary_amine_smarts = calc_props.FUNCTIONAL_GROUP_TO_SMARTS[
        "primary_amine"]
    assert_equals(calc_props.has_functional_group(pymol, primary_amine_smarts),
                  False)
def test_primary_amine_1_benzylguanidine():
    mol = calc_props.smiles_to_ob("NC(NCC1=CC=CC=C1)=N")
    pymol = pybel.Molecule(mol)
    primary_amine_smarts = calc_props.FUNCTIONAL_GROUP_TO_SMARTS[
        "primary_amine"]
    assert_equals(calc_props.has_functional_group(pymol, primary_amine_smarts),
                  False)
Exemple #13
0
def main():
  commandLineParser = argparse.ArgumentParser(description="Docking Results Ranking to Three Dimensions: takes \
                                                           a table generated by DRranker.py and builds a file \
                                                           with the coordinates of the docked molecules")
  commandLineParser.add_argument("rankingFile", help="file generated by DRranker.py")
  commandLineParser.add_argument("outputFile", help="output filename")
  commandLineParser.add_argument("-r", "--lowest-ranking", dest="lowestRanking", default=1, type=int, help="Last ranking level to be processed. Default: 1")
  commandLineParser.add_argument("-f", "--output-format", dest="outputFormat", default="sdf", help="Format of the file to be written. It must be a openbabel supported output format. Deafult: sdf")
  options = commandLineParser.parse_args()

  (ranking,basenames) = processRanking(options.rankingFile, options.lowestRanking)

  for (basename,coordFile) in basenames:
    try:
      mols = pybel.readfile("sdf", coordFile)
    except:
      print("Can't open " + coordFile, file=sys.stderr)
      exit()
    
    for mol in mols:
      if (mol.title,basename) in ranking:
        ranking[(mol.title,basename)] = pybel.Molecule(pybel.ob.OBMol(mol.OBMol))
  
  out = pybel.Outputfile(filename=options.outputFile, format=options.outputFormat, overwrite=True)
  for (molname,basename) in list(ranking.keys()):
    mol = ranking[(molname,basename)]
    mol.title += "_" + basename
    out.write(mol)

  out.close()
def test_primary_amine_imine():
    mol = calc_props.smiles_to_ob("CC1=CC(=CC=C1)N=CC2=CC=CC=C2")
    pymol = pybel.Molecule(mol)
    primary_amine_smarts = calc_props.FUNCTIONAL_GROUP_TO_SMARTS[
        "primary_amine"]
    assert_equals(calc_props.has_functional_group(pymol, primary_amine_smarts),
                  False)
Exemple #15
0
 def testStereo(self):
     data = [
         ("FC[C@@](Br)(Cl)I", [((2, 3, 4, 5, 6), None, "C[C@@](Br)(Cl)I"),
                               ((2, 3, 4, 5), None, "CC(Br)Cl"),
                               ((1, 2, 3, 4, 5, 6), (4, ), "FCC(Br)Cl.I")]),
         ("[C@@H](Br)(Cl)I", [((1, 2, 3), None, "C(Br)Cl"),
                              ((1, 2, 3, 4), (2, ), "C(Br)Cl.I")]),
         ("C[C@@H]1CO1", [
             ((2, 3, 4), None, "C1CO1"),
         ]),
         ("F/C=C/I", [((1, 2, 3, 4), None, "F/C=C/I"),
                      ((1, 2, 3), None, "FC=C"),
                      ((1, 2, 3, 4), (0, ), "F.C=CI"),
                      ((1, 2, 3, 4), (1, ), "FC.CI")]),
     ]
     for smi, d in data:
         mol = pybel.readstring("smi", smi)
         for a, b, ans in d:
             nmol = ob.OBMol()
             bv = self.createBitVec(7, a)
             bondbv = None if b is None else self.createBitVec(5, b)
             ok = mol.OBMol.CopySubstructure(nmol, bv, bondbv)
             self.assertTrue(ok)
             if "@" not in ans and "/" not in ans:
                 self.assertFalse(nmol.GetData(ob.StereoData))
             self.assertEqual(
                 pybel.Molecule(nmol).write("smi").rstrip(), ans)
Exemple #16
0
def get_zmatrix_template(parser):
    """Make a zmatrix from the original geometry.

    Parameters
    ----------
    parser : object
        Parser object from vetee.

    Returns
    -------
    zmatrix : str
        The zmatrix (gzmat format) for the parser
        molecule. Will be used to combine with
        dihedral angles.

    """
    # from vetee
    obmol = openbabel.OBMol()
    # add coordinates for each atom
    for atom in parser.coords:
        obatom = openbabel.OBAtom()
        atomicnum = vetee.gaussian_options.periodic_table(atom[0])
        obatom.SetAtomicNum(atomicnum)
        obatom.SetVector(atom[1], atom[2], atom[3])
        obmol.AddAtom(obatom)
    # set charge, multiplicity, and comments (title)
    obmol.SetTotalCharge(parser.charge)
    obmol.SetTotalSpinMultiplicity(parser.multip)
    if parser.comments is not None:
        obmol.SetTitle(parser.comments)
    # convert the obmol to a pybel Molecule
    pybelmol = pybel.Molecule(obmol)
    # generate a zmatrix string using the obmol input
    zmatrix = pybelmol.write("gzmat")
    return zmatrix
Exemple #17
0
def loadcdxml(cdxml):
    import pybel
    obconv = openbabel.OBConversion()  # ob Class
    obmol = openbabel.OBMol()  # ob Class
    obconv.SetInFormat('cdxml')  # ob Method to set cdxml
    obconv.ReadFile(obmol, cdxml)  # ob Method to reaad cdxml into a OBMol()
    obmol.NumAtoms()
    idx_list = []
    atno_list = []
    for idx in range(obmol.NumAtoms()):
        if obmol.GetAtom(idx + 1).GetAtomicNum() == 26:
            idx_list.append(idx)
            atno_list.append(obmol.GetAtom(idx + 1).GetAtomicNum())
            obmol.GetAtom(idx + 1).SetAtomicNum(14)

    pymol = pybel.Molecule(obmol)
    pymol.make3D()
    pymol.localopt()
    for i in range(len(idx_list)):
        idx = idx_list[i]
        atno = atno_list[i]
        obmol.GetAtom(idx + 1).SetAtomicNum(atno)

    fname = re.sub(r'.cdxml', '.xyz', cdxml)  # file name for the new xyz
    obconv.WriteFile(obmol, fname)

    return fname
Exemple #18
0
def insert_solvents(coll):
    names = """THF
    monoglyme
    Dimethylacetamide
    propylene carbonate
    ethylene carbonate
    dimethylcarbonate
    DMSO
    ACN
    Diethylcarbonate
    propyl glyme
    ethyl glyme
    ethyl diglyme
    diglyme
    Butyldiglyme
    tetraglyme
    Pentaethylene glycol diethyl ether
    Tetraethylene glycol diethyl ether
    Tetraethylene glycol dibutyl ether
    Butyldiglyme"""

    for n in names.split("\n"):
        response = requests.get(
            "http://cactus.nci.nih.gov/chemical/structure/{}/file?format=xyz".
            format(n))
        if response.status_code == 200:
            xyz = XYZ.from_string(response.text)
            clean_mol = xyz.molecule
            bb = BabelMolAdaptor(clean_mol)
            pbmol = pb.Molecule(bb.openbabel_mol)
            smiles = pbmol.write("smi").split()[0]
            can = pbmol.write("can").split()[0]
            inchi = pbmol.write("inchi")
            svg = pbmol.write("svg")

            d = {"molecule": clean_mol.as_dict()}
            comp = clean_mol.composition
            d["pretty_formula"] = comp.reduced_formula
            d["formula"] = comp.formula
            d["composition"] = comp.as_dict()
            d["elements"] = list(comp.as_dict().keys())
            d["nelements"] = len(comp)
            d["charge"] = clean_mol.charge
            d["spin_multiplicity"] = clean_mol.spin_multiplicity
            d["smiles"] = smiles
            d["can"] = can
            d["inchi"] = inchi
            # d["names"] = get_nih_names(smiles)
            d["svg"] = svg
            d["xyz"] = str(xyz)
            d["tags"] = ["Solvents"]
            coll.update(
                {
                    "inchi": inchi,
                    "charge": clean_mol.charge,
                    "spin_multiplicity": clean_mol.spin_multiplicity
                }, {"$set": d},
                upsert=True)
        else:
            print("{} not found.\n".format(n))
Exemple #19
0
def canonicalize(lig):
    """Get the canonical atom order for the ligand."""
    atomorder = None
    # Get canonical atom order

    lig = pybel.ob.OBMol(lig.OBMol)
    for bond in pybel.ob.OBMolBondIter(lig):
        if bond.GetBondOrder() != 1:
            bond.SetBondOrder(1)
    lig.DeleteData(pybel.ob.StereoData)
    lig = pybel.Molecule(lig)
    testcan = lig.write(format='can')
    try:
        pybel.readstring('can', testcan)
        reference = pybel.readstring('can', testcan)
    except IOError:
        testcan, reference = '', ''
    if testcan != '':
        reference.removeh()
        isomorphs = get_isomorphisms(
            reference,
            lig)  # isomorphs now holds all isomorphisms within the molecule
        if not len(isomorphs) == 0:
            smi_dict = {}
            smi_to_can = isomorphs[0]
            for x in smi_to_can:
                smi_dict[int(x[1]) + 1] = int(x[0]) + 1
            atomorder = [smi_dict[x + 1] for x in range(len(lig.atoms))]
        else:
            atomorder = None
    return atomorder
Exemple #20
0
 def singleTorsion(self, torsionLevel):
     if torsionLevel == len(self.torsionList_):
         self.printCurrentTorsion()
         mymol = pybel.Molecule(self.myOBMol_)
         if self.checkGeometry(mymol):
             #basename='c'+str(self.conformer_).zfill(self.spacePerConformer_)
             basename = 'conf' + str(self.conformer_)
             fullbasename = self.outDir_ + basename
             outname = fullbasename + '.xyz'
             self.baseNames_.append(basename)
             self.xyzNames_.append(outname)
             mymol.write('xyz', outname, overwrite=True)
             self.conformer_ += 1
         return
     else:
         torsion = self.torsionList_[torsionLevel]
         IDs = torsion.IDs
         angles = torsion.angles
         atoms = [self.myOBMol_.GetAtom(IDs[i]) for i in range(0, 4)]
         for angle in angles:
             targetAngle = math.radians(angle)
             self.myOBMol_.SetTorsion(atoms[0], atoms[1], atoms[2],
                                      atoms[3], targetAngle)
             realAngle = self.myOBMol_.GetTorsion(IDs[0], IDs[1], IDs[2],
                                                  IDs[3])
             self.currentTorsion_[torsionLevel] = realAngle
             self.currentTargetTorsion_[torsionLevel] = angle
             self.singleTorsion(torsionLevel + 1)
Exemple #21
0
 def optg_c(self, steps=60, ff="mmff94", optimizer='cg'):
     """
     Opt geometry by ff with constrained torsions
     ==============================================
     defaut ff: mmff94, mmff94s seems to be
                problematic, sometimes it
                leads to weird geometries
     steps: ff steps for constraint opt
     """
     m = self.m  #copy.copy( self.m )
     # Define constraints
     c = ob.OBFFConstraints()
     #c.AddDistanceConstraint(1, 8, 10)        # Angstroms
     #c.AddAngleConstraint(1, 2, 3, 120.0)     # Degrees
     # constrain torsion only
     torsions = self.get_all_torsions()
     for torsion in torsions:
         i, j, k, l = torsion
         ang = torsions[torsion]
         c.AddTorsionConstraint(i, j, k, l, ang)  # Degrees
     # Setup the force field with the constraints
     obff = ob.OBForceField.FindForceField(ff)
     obff.Setup(m, c)
     obff.SetConstraints(c)
     obff.ConjugateGradients(steps)
     obff.GetCoordinates(m)
     self.m = m
     self.M = pb.Molecule(m)
     self.coords = get_coords(m)
Exemple #22
0
 def optg(self, cparam=[False, 'slow'], ff="MMFF94", steps=2500):
     """ geoemtry optimization
     ================
     cparam: conformer parameters to be used
             cparam = [False] -- no conformer generation
             cparam = [True, algo, num_conf, N] -- gen confs and `N ff steps for each conf
     """
     # ff opt
     m = self.m
     obff = ob.OBForceField.FindForceField(ff)
     obff.Setup(m)
     #obff.SteepestDescent(1200, 1.0e-4) # a quick cleanup before searching
     obff.ConjugateGradients(600, 1.0e-3)  # fast & coarse
     if cparam[0]:
         if cparam[1] in ['slow']:
             obff.FastRotorSearch(True)
         elif algo in ['slowest']:
             nc, n = cparam[1:]
             obff.WeightedRotorSearch(nc, n)
         else:
             print('unknow algo')
             raise
     obff.ConjugateGradients(steps, 1.0e-6)  # fast & coarse
     #obff.SteepestDescent(500, 1.0e-6) # slower &
     obff.GetCoordinates(m)
     self.m = m
     self.M = pb.Molecule(m)
     self.coords = get_coords(m)
def test_primary_amine_glycine():
    mol = calc_props.smiles_to_ob("C(C(=O)O)N")
    pymol = pybel.Molecule(mol)
    primary_amine_smarts = calc_props.FUNCTIONAL_GROUP_TO_SMARTS[
        "primary_amine"]
    assert_equals(calc_props.has_functional_group(pymol, primary_amine_smarts),
                  True)
Exemple #24
0
 def testAtomMapsAfterCopying(self):
     """Copying a molecule should copy the atom maps"""
     smi = "C[CH2:2]O[Cl:6]"
     obmol = pybel.readstring("smi", smi).OBMol
     copy = pybel.ob.OBMol(obmol)
     copysmi = pybel.Molecule(copy).write("smi", opt={"a": True})
     self.assertEqual(copysmi.rstrip(), smi)
Exemple #25
0
def mol_to_pybel(mdtmol):
    """ Translate a moldesign molecule object into a pybel molecule object.

    Note:
        The focus is on translating topology and biomolecular structure -
        we don't translate any metadata.

    Args:
        mdtmol (moldesign.Molecule): molecule to translate

    Returns:
        pybel.Molecule: translated molecule
    """
    import openbabel as ob
    import pybel as pb

    obmol = ob.OBMol()
    obmol.BeginModify()
    atommap = {}
    resmap = {}
    for atom in mdtmol.atoms:
        obatom = obmol.NewAtom()
        obatom.SetAtomicNum(atom.atnum)
        atommap[atom] = obatom
        pos = atom.position.value_in(u.angstrom)
        obatom.SetVector(*pos)

        if atom.residue and atom.residue not in resmap:
            obres = obmol.NewResidue()
            resmap[atom.residue] = obres
            obres.SetChain(
                str(atom.chain.pdbname)[0] if atom.chain.pdbname else 'Z')
            obres.SetName(
                str(atom.residue.pdbname) if atom.residue.pdbname else 'UNL')
            obres.SetNum(
                str(atom.residue.pdbindex) if atom.residue.pdbindex else 0)
        else:
            obres = resmap[atom.residue]

        obres.AddAtom(obatom)
        obres.SetHetAtom(obatom, not atom.residue.is_standard_residue)
        obres.SetAtomID(obatom, str(atom.name))
        obres.SetSerialNum(
            obatom, mdt.utils.if_not_none(atom.pdbindex, atom.index + 1))

    for atom in mdtmol.bond_graph:
        a1 = atommap[atom]
        for nbr, order in mdtmol.bond_graph[atom].items():
            a2 = atommap[nbr]
            if a1.GetIdx() > a2.GetIdx():
                obmol.AddBond(a1.GetIdx(), a2.GetIdx(), order)
    obmol.EndModify()
    pbmol = pb.Molecule(obmol)

    for atom in atommap:
        idx = atommap[atom].GetIdx()
        obatom = obmol.GetAtom(idx)
        obatom.SetFormalCharge(int(atom.formal_charge.value_in(u.q_e)))
    return pbmol
Exemple #26
0
def cell_ffopt(ff, mol, frozenats):
    ### FORCE FIELD OPTIMIZATION ##
    # INPUT
    #   - ff: force field to use, available MMFF94, UFF< Ghemical, GAFF
    #   - mol: mol3D to be ff optimized
    #   - connected: indices of connection atoms to metal
    #   - constopt: flag for constrained optimization
    # OUTPUT
    #   - mol: force field optimized mol3D
    metals = range(21, 31) + range(39, 49) + range(72, 81)
    ### check requested force field
    ffav = 'mmff94, uff, ghemical, gaff, mmff94s'  # force fields
    if ff.lower() not in ffav:
        print 'Requested force field not available. Defaulting to MMFF94'
        ff = 'mmff94'
    ### convert mol3D to OBmol via xyz file, because AFTER/END option have coordinates
    backup_mol = mol3D()
    backup_mol.copymol3D(mol)
    #   print('bck ' + str(backup_mol.getAtom(0).coords()))
    #   print('mol_ibf ' + str(mol.getAtom(0).coords()))

    mol.writexyz('tmp.xyz')
    mol.OBmol = mol.getOBmol('tmp.xyz', 'xyzf')
    os.remove('tmp.xyz')
    ### initialize constraints
    constr = pybel.ob.OBFFConstraints()
    ### openbabel indexing starts at 1 ### !!!
    # convert metals to carbons for FF
    indmtls = []
    mtlsnums = []
    for iiat, atom in enumerate(mol.OBmol.atoms):
        if atom.atomicnum in metals:
            indmtls.append(iiat)
            mtlsnums.append(atom.atomicnum)
            atom.OBAtom.SetAtomicNum(19)
    for cat in frozenats:
        constr.AddAtomConstraint(cat + 1)  # indexing babel
    ### set up forcefield
    forcefield = pybel.ob.OBForceField.FindForceField(ff)
    obmol = mol.OBmol.OBMol
    forcefield.Setup(obmol, constr)
    ## force field optimize structure
    forcefield.ConjugateGradients(2500)
    forcefield.GetCoordinates(obmol)
    mol.OBmol = pybel.Molecule(obmol)

    # reset atomic number to metal
    for i, iiat in enumerate(indmtls):
        mol.OBmol.atoms[iiat].OBAtom.SetAtomicNum(mtlsnums[i])
    mol.convert2mol3D()

    en = forcefield.Energy()
    #   print(str(mol.OBmol.atoms[1].OBAtom.GetVector().GetZ()))
    #    print(str(forcefield.Validate()))
    # print('mol_af ' + str(mol.getAtom(0).coords()))

    #  print('ff delta = ' + str(backup_mol.rmsd(mol)))
    del forcefield, constr, obmol
    return mol, en
Exemple #27
0
def json_to_pybel(data):
    """Converts a python data structure to pybel.Molecule.

    The data structure is a plain python object of the form:

    ```
    {
        "atoms": [{"location": [0, 0, 0], "element": "H", "label": "H1",
                   "charge": 0}, ...],
        "bonds": [{"source": 0, "target": 0, "order": 1}, ...],
        "unitcell": [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    }
    ```

    It is referred to as "json" because this data structure is intended to
    be read from and written to json files or databases.

    As RASPA makes no use of bond information, this field is ignored.

    The labels are stripped and replaced with "MOF_{element}", in accordance
    with the CrystalGenerator forcefield notation. Therefore, labels are also
    ignored.

    Args:
        data: the molecule, as a python object
    Returns:
        An instance of `pybel.Molecule`
    """
    if not PYBEL_LOADED:
        raise ImportError("Open Babel not installed.")

    table = pybel.ob.OBElementTable()

    if "building_blocks" in data:
        data["atoms"] = [a for bb in data["building_blocks"]
                         for a in bb["atoms"]]
    obmol = pybel.ob.OBMol()
    obmol.BeginModify()
    for atom in data["atoms"]:
        obatom = obmol.NewAtom()
        obatom.SetAtomicNum(table.GetAtomicNum(str(atom["element"])))
        obatom.SetVector(*atom["location"])

    uc = pybel.ob.OBUnitCell()
    uc.SetData(*(pybel.ob.vector3(*v) for v in data["unitcell"]))
    uc.SetSpaceGroup("P1")
    obmol.CloneData(uc)

    obmol.EndModify()

    mol = pybel.Molecule(obmol)

    # Add partial charges
    if "charge" in data["atoms"][0]:
        mol.OBMol.SetPartialChargesPerceived()
        for atom, pyatom in zip(data["atoms"], mol.atoms):
            pyatom.OBAtom.SetPartialCharge(atom["charge"])

    return mol
def test_rb_basic():
    # DNM
    mol = calc_props.smiles_to_ob(
        "CC(C1=CC(C(C)=CC(N2C)=O)=C2C3=C1N4CO3)=CC4=O")
    pymol = pybel.Molecule(mol)
    assert_equals(calc_props.rotatable_bonds(pymol), 0)

    # Ribocil C
    mol = calc_props.smiles_to_ob(
        "C1CC(CN(C1)CC2=CN(C=N2)C3=NC=CC=N3)C4=NC(=O)C=C(N4)C5=CC=CS5")
    pymol = pybel.Molecule(mol)
    assert_equals(calc_props.rotatable_bonds(pymol), 5)

    # Triphenylphosphine
    mol = calc_props.smiles_to_ob("C1(P(C2=CC=CC=C2)C3=CC=CC=C3)=CC=CC=C1")
    pymol = pybel.Molecule(mol)
    assert_equals(calc_props.rotatable_bonds(pymol), 3)
Exemple #29
0
def mol_from_pymatgen(mol: Molecule):
    """
    Args:
        mol(Molecule)
    """
    mol = pybel.Molecule(BabelMolAdaptor(mol).openbabel_mol)
    mol.make3D()
    return mol
Exemple #30
0
 def Clone(self):
     tmp = Molecule()
     tmp.title = self.title
     tmp.obmol = openbabel.OBMol(self.obmol)
     tmp.pybel_mol = pybel.Molecule(tmp.obmol)
     tmp.smiles = self.smiles
     tmp.inchi = self.inchi
     return tmp