Create Schema openbabel; Grant All On Schema openbabel to public; Create Or Replace Function openbabel.valid(smi Text) Returns Boolean As $EOPY$ import openbabel try: obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInFormat("smi") return obc.ReadString(mol, smi) return True except: return False $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.cansmiles(smi Text) Returns Text As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInAndOutFormats("smi", "can") if obc.ReadString(mol, smi): mol.SetTitle("") return obc.WriteString(mol,1) else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.smiles_to_symbols(smi Text) Returns Text[] As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInAndOutFormats("smi", "mol") if obc.ReadString(mol, smi): tbl = openbabel.OBElementTable() return "{" + ",".join((tbl.GetSymbol(a.GetAtomicNum()) for a in openbabel.OBMolAtomIter(mol))) + "}" else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.smiles_to_bonds(smi Text) Returns Integer[] As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInAndOutFormats("smi", "mol") if obc.ReadString(mol, smi): bonds = [] for b in openbabel.OBMolBondIter(mol): if b.IsAromatic(): bo = 4 else: bo = b.GetBO() bonds.append("{%d,%d,%d}" % (b.GetBeginAtomIdx(), b.GetEndAtomIdx(), bo)) return "{" + ",".join(bonds) + "}" else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.smiles_to_molfile(smi Text) Returns Text As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInAndOutFormats("smi", "mol") if obc.ReadString(mol, smi): return obc.WriteString(mol) else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.molfile_to_smiles(molfil Text) Returns Text As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInAndOutFormats("sdf", "smi") if obc.ReadString(mol, molfil): mol.SetTitle("") return obc.WriteString(mol,1) else: raise ValueError("Error in input molfile") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.matches(smi Text, sma Text) Returns Boolean As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInFormat("smi") if obc.ReadString(mol, smi): pat = openbabel.OBSmartsPattern() if pat.Init(sma): return pat.Match(mol) else: raise ValueError("Error in input smarts") return None else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.count_matches(smi Text, sma Text) Returns Integer As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInFormat("smi") if obc.ReadString(mol, smi): pat = openbabel.OBSmartsPattern() if pat.Init(sma): if pat.Match(mol): return len(pat.GetUMapList()) else: return 0 else: raise ValueError("Error in input smarts") return None else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.list_matches(smi Text, sma Text, imatch Integer, istart Integer) Returns Integer[] As $EOPY$ import openbabel obc = openbabel.OBConversion() mol = openbabel.OBMol() obc.SetInFormat("smi") if obc.ReadString(mol, smi): pat = openbabel.OBSmartsPattern() if pat.Init(sma): if pat.Match(mol): i = 0 all_matches = list() for p in pat.GetUMapList(): i += 1 pgarray = '{' + ','.join([str(a-1+istart) for a in p]) + '}' if i == imatch: return pgarray all_matches.append(pgarray) return '{' + ','.join(all_matches) + '}' else: return None else: raise ValueError("Error in input smarts") return None else: raise ValueError("Error in input smiles") return None $EOPY$ Language plpythonu Immutable; Create Or Replace Function openbabel.list_matches(Text, Text) Returns Integer[] As $EOSQL$ Select openbabel.list_matches($1, $2, 1, 0); $EOSQL$ Language SQL Immutable; Create Or Replace Function openbabel.list_matches(Text, Text, Integer) Returns Integer[] As $EOSQL$ Select openbabel.list_matches($1, $2, $3, 0); $EOSQL$ Language SQL Immutable; Create Or Replace Function openbabel.fp(smi Text) Returns Bit As $EOPY$ import pybel mol = pybel.readstring("smi", smi) fp = mol.calcfp() fpx = ['0' for i in range(1024)] for b in fp.bits: fpx[b-1] = '1' return "".join(fpx) $EOPY$ Language plpythonu Immutable; Create or Replace FUNCTION openbabel.contains(Bit, Bit) Returns Boolean As $EOSQL$ Select $2 = ($1 & $2); $EOSQL$ Language SQL Immutable; Comment On FUNCTION frowns.contains(bit, bit) Is 'does first bit string contain all the bits of second';