Ejemplo n.º 1
0
def atom_count(str_data, in_format):
    mol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    conv.ReadString(mol, str_data)

    return mol.NumAtoms()
Ejemplo n.º 2
0
def convert_str(str_data, in_format, out_format):
    mol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    conv.SetOutFormat(out_format)
    conv.ReadString(mol, str_data)

    return (conv.WriteString(mol), conv.GetOutFormat().GetMIMEType())
Ejemplo n.º 3
0
def cjson_to_ob_molecule(cjson):
    cjson_str = json.dumps(cjson)
    sdf_str = avo_convert_str(cjson_str, 'cjson', 'sdf')
    conv = OBConversion()
    conv.SetInFormat('sdf')
    conv.SetOutFormat('sdf')
    mol = OBMol()
    conv.ReadString(mol, sdf_str)
    return mol
Ejemplo n.º 4
0
 def __init__(self, smile):
     self._smile = smile.strip()
     conv = OBConversion()
     if not conv.SetInAndOutFormats('smi', 'inchi'):
         raise 'Problem with openbabel'
     mol = OBMol()
     if not conv.ReadString(mol, self._smile):
         raise TypeError, "No such smile: %s" % self._smile
     self._inchi = conv.WriteString(mol).strip()
Ejemplo n.º 5
0
def LoadMolFromSmiles(smiles):
    """Returns an OBMol construcetd from an SMILES code"""
    smiles = sorted(smiles.split("."), key=len)[-1] ## Strip salts
    mol=OBMol()
    loader=OBConversion()
    loader.SetInAndOutFormats("smi","smi")
    if not loader.ReadString(mol, smiles):
        return None
    mol.smilesCode=smiles
    return mol
Ejemplo n.º 6
0
def to_inchi(str_data, in_format):
    mol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    # Hackish for now, convert to xyz first...
    conv.SetOutFormat('xyz')
    conv.ReadString(mol, str_data)
    xyz = conv.WriteString(mol)

    # Now convert to inchi and inchikey.
    mol = OBMol()
    conv.SetInFormat('xyz')
    conv.ReadString(mol, xyz)

    conv.SetOutFormat('inchi')
    inchi = conv.WriteString(mol).rstrip()
    conv.SetOptions("K", conv.OUTOPTIONS)
    inchikey = conv.WriteString(mol).rstrip()

    return (inchi, inchikey)
Ejemplo n.º 7
0
def to_inchi(str_data, in_format):
    mol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    conv.ReadString(mol, str_data)

    conv.SetOutFormat('inchi')
    inchi = conv.WriteString(mol).rstrip()
    conv.SetOptions('K', conv.OUTOPTIONS)
    inchikey = conv.WriteString(mol).rstrip()

    return (inchi, inchikey)
Ejemplo n.º 8
0
def get_formula(str_data, in_format):
    # Inchi must start with 'InChI='
    if in_format == 'inchi' and not str_data.startswith('InChI='):
        str_data = 'InChI=' + str_data
        validate_start_of_inchi(str_data)
    # Get the molecule using the "Hill Order" - i. e., C first, then H,
    # and then alphabetical.
    mol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    conv.ReadString(mol, str_data)

    return mol.GetFormula()
Ejemplo n.º 9
0
def convert_str(str_data,
                in_format,
                out_format,
                gen3d=False,
                add_hydrogens=False,
                perceive_bonds=False,
                out_options=None):

    # Make sure that the start of InChI is valid before passing it to
    # Open Babel, or Open Babel will crash the server.
    if in_format.lower() == 'inchi':
        validate_start_of_inchi(str_data)

    if out_options is None:
        out_options = {}

    obMol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    conv.SetOutFormat(out_format)
    conv.ReadString(obMol, str_data)

    if add_hydrogens:
        obMol.AddHydrogens()

    if gen3d:
        # Generate 3D coordinates for the input
        mol = pybel.Molecule(obMol)
        mol.make3D()

    if perceive_bonds:
        obMol.ConnectTheDots()
        obMol.PerceiveBondOrders()

    for option, value in out_options.items():
        conv.AddOption(option, conv.OUTOPTIONS, value)

    return (conv.WriteString(obMol), conv.GetOutFormat().GetMIMEType())
Ejemplo n.º 10
0
def properties(str_data, in_format, add_hydrogens=False):
    # Returns a dict with the atom count, formula, heavy atom count,
    # mass, and spaced formula.
    if in_format == 'inchi' and not str_data.startswith('InChI='):
        # Inchi must start with 'InChI='
        str_data = 'InChI=' + str_data
        validate_start_of_inchi(str_data)
    mol = OBMol()
    conv = OBConversion()
    conv.SetInFormat(in_format)
    conv.ReadString(mol, str_data)

    if add_hydrogens:
        mol.AddHydrogens()

    props = {}
    props['atomCount'] = mol.NumAtoms()
    props['formula'] = mol.GetFormula()
    props['heavyAtomCount'] = mol.NumHvyAtoms()
    props['mass'] = mol.GetMolWt()
    props['spacedFormula'] = mol.GetSpacedFormula()

    return props
Ejemplo n.º 11
0
kegg = Kegg.getInstance()
cids = kegg.get_all_cids()
print 'Found %d cids!' % len(cids)

smiles = open('smiles.txt', 'a')
nums = open('nums.txt', 'a')

if len(argv) < 2:
    i = 0
else:
    i = int(argv[1])

conv = OBConversion()
if not conv.SetInAndOutFormats('inchi', 'smi'):
    raise 'Problem with openbabel'

for j, cid in enumerate(cids[i:]):
    try:
        nums.write(str(j + i) + '\n')
        inchi = kegg.cid2inchi(cid)
        mol = OBMol()
        if not conv.ReadString(mol, inchi):
            print cid
        smiles.write(conv.WriteString(mol))
        #smiles.write(kegg.cid2smiles(cid) + '\n')
    except:
        print cid

smiles.close()
nums.close()