def test_fold(self): v = ob.vectorUnsignedInt([0x2A, 0x41]) self.assertEqual(len(v), 2) x = ob.OBFingerprint.FindFingerprint("FP2") x.Fold(v, 32) self.assertEqual(len(v), 1) self.assertEqual(v[0], (0x2A | 0x41)) v = ob.vectorUnsignedInt([0x01, 0x04, 0x20, 0x00]) self.assertEqual(len(v), 4) x.Fold(v, 64) self.assertEqual(len(v), 2) self.assertEqual(v[0], 0x21) self.assertEqual(v[1], 0x04)
def testOptions(self): mol = pybel.readstring("smi", "ICBr") bv = self.createBitVec(4, (1, 3)) ans = ["[I].[Br]", "I.Br", "I*.Br*"] ans_atomorder = [[1, 3], [1, 3], [1, 3, 2, 2]] ans_bondorder = [ [], [], [0, 1] ] for option in range(3): nmol = ob.OBMol() atomorder = ob.vectorUnsignedInt() bondorder = ob.vectorUnsignedInt() ok = mol.OBMol.CopySubstructure(nmol, bv, None, option, atomorder, bondorder) self.assertTrue(ok) self.assertEqual(pybel.Molecule(nmol).write("smi").rstrip(), ans[option]) self.assertEqual(ans_atomorder[option], list(atomorder)) self.assertEqual(ans_bondorder[option], list(bondorder))
def __init__(self, Global): """use Global dictionary supplied by plpython to buffer parsed smiles and smarts for quicker lookup when reused. """ # buffering smiles and smarts can eat up lots of memory, # but speeds things up considerably # certainly if an entire table can acutally fit # but also if this is the maximum size of a hit list, say from match if not Global.has_key("OB"): Global["OB"] = dict() self.GOB = Global["OB"] self.maxsmi = 1000 self.maxqsmi = 1000 if not self.GOB.has_key("mol"): self.GOB["mol"] = dict() self.mol = self.GOB["mol"] # pick a reasonable number of smarts patterns you expect to use often, # say 166 public keys or even 1000 fragment keys self.maxsma = 1000 if not self.GOB.has_key("pat"): self.GOB["pat"] = dict() self.pat = self.GOB["pat"] if not self.GOB.has_key("obc"): self.GOB["obc"] = openbabel.OBConversion() self.obc = self.GOB["obc"] self.obc.SetInFormat("smi") # if not self.GOB.has_key("fobc"): self.GOB["fobc"] = openbabel.OBConversion() # self.fobc = self.GOB["fobc"] # self.fobc.SetInAndOutFormats("sdf","can") # if not self.GOB.has_key("fmol"): self.GOB["fmol"] = openbabel.OBMol() # self.fmol = self.GOB["fmol"] if not self.GOB.has_key('fp'): self.GOB['fp'] = openbabel.vectorUnsignedInt() if not self.GOB.has_key('fpr'): self.GOB['fpr'] = openbabel.OBFingerprint.FindFingerprint('FP2') self.fp = self.GOB["fp"] self.fpr = self.GOB["fpr"] if not self.GOB.has_key("isomapper"): self.GOB["isomapper"] = dict() self.isomapper = self.GOB["isomapper"] self.isomap = openbabel.vpairUIntUInt()
def calc_MACCS(mol, fp=None, get_fingerprint=_ob_get_fingerprint["MACCS"][1], _pack_256 = struct.Struct("<" + "I"*8).pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_256(*fp)[:21]
def calc_FP4(mol, fp=None, get_fingerprint=_ob_get_fingerprint["FP4"][1], _pack_512 = struct.Struct("<" + "I"*16).pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_512(*fp)[:39]
def calc_FP3(mol, fp=None, get_fingerprint=_ob_get_fingerprint["FP3"][1], _pack_64 = struct.Struct("<II").pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_64(*fp)[:7]
def calc_FP2(mol, fp=None, get_fingerprint=_ob_get_fingerprint["FP2"][1], _pack_1024 = struct.Struct("<" + "I"*32).pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_1024(*fp)
def testOptions(self): mol = pybel.readstring("smi", "ICBr") bv = self.createBitVec(4, (1, 3)) ans = ["[I].[Br]", "I.Br", "I*.Br*"] ans_atomorder = [[1, 3], [1, 3], [1, 3, 2, 2]] ans_bondorder = [[], [], [0, 1]] for option in range(3): nmol = ob.OBMol() atomorder = ob.vectorUnsignedInt() bondorder = ob.vectorUnsignedInt() ok = mol.OBMol.CopySubstructure(nmol, bv, None, option, atomorder, bondorder) self.assertTrue(ok) self.assertEqual( pybel.Molecule(nmol).write("smi").rstrip(), ans[option]) self.assertEqual(ans_atomorder[option], list(atomorder)) self.assertEqual(ans_bondorder[option], list(bondorder))
def calc_MACCS(mol, fp=None, get_fingerprint=_ob_get_fingerprint["MACCS"][1], _pack_256=struct.Struct("<" + "I" * 8).pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_256(*fp)[:21]
def calc_FP4(mol, fp=None, get_fingerprint=_ob_get_fingerprint["FP4"][1], _pack_512=struct.Struct("<" + "I" * 16).pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_512(*fp)[:39]
def calc_FP2(mol, fp=None, get_fingerprint=_ob_get_fingerprint["FP2"][1], _pack_1024=struct.Struct("<" + "I" * 32).pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_1024(*fp)
def calc_FP3(mol, fp=None, get_fingerprint=_ob_get_fingerprint["FP3"][1], _pack_64=struct.Struct("<II").pack): if fp is None: fp = ob.vectorUnsignedInt() get_fingerprint(mol, fp) return _pack_64(*fp)[:7]
def test_get_set(self): v = ob.vectorUnsignedInt([1, 6]) # XXX Why does GetBit need an actual instance? x = ob.OBFingerprint.FindFingerprint("FP2") self.assertTrue(x.GetBit(v, 0)) for i in range(1, 32): self.assertFalse(x.GetBit(v, i), i) self.assertFalse(x.GetBit(v, 32)) self.assertTrue(x.GetBit(v, 33)) self.assertTrue(x.GetBit(v, 34)) self.assertFalse(x.GetBit(v, 35)) x.SetBit(v, 35) self.assertTrue(x.GetBit(v, 35))
def calcfp(self, fptype="FP2"): """Calculate a molecular fingerprint. Optional parameters: fptype -- the fingerprint type (default is "FP2"). See the fps variable for a list of of available fingerprint types. """ fp = ob.vectorUnsignedInt() try: fingerprinter = _fingerprinters[fptype] except KeyError: raise ValueError("%s is not a recognised Open Babel Fingerprint type" % fptype) fingerprinter.GetFingerprint(self.OBMol, fp) return Fingerprint(fp)
def calcfp(self, fptype=""): """Calculate a molecular fingerprint. Optional parameters: fptype -- the name of the Open Babel fingerprint type. If fptype is not specified, the default Open Babel fingerprint type is used. See the Open Babel library documentation for more details. """ fp = ob.vectorUnsignedInt() fingerprinter = ob.OBFingerprint.FindFingerprint(fptype) if fingerprinter is None: raise ValueError, "%s is not a recognised Open Babel Fingerprint type" % fptype fingerprinter.GetFingerprint(self.OBMol, fp) return Fingerprint(fp)
def calcfp(self, fptype="FP2"): """Calculate a molecular fingerprint. Optional parameters: fptype -- the name of the Open Babel fingerprint type. If fptype is not specified, the FP2 fingerprint type is used. See the Open Babel library documentation for more details. """ fp = ob.vectorUnsignedInt() try: fingerprinter = _fingerprinters[fptype] except KeyError: raise ValueError, "%s is not a recognised Open Babel Fingerprint type" % fptype fingerprinter.GetFingerprint(self.OBMol, fp) return Fingerprint(fp)
def test_fp_words(self): mol = parse_smiles("c1ccccc1O.C#N.[Ge].C1CCC1") def next_highest_power_of_two(n): i = 8 while i < n: i *= 2 return i for (name, nbits, v0, v1) in ( ("FP2", 1021, 0, 1), ("FP3", 55, 67108864, 1159170), ("FP4", 307, 2, 0), # TODO: change my MACCS.txt so it's correct # then rerun this test and change to the right answer ("MACCS", 166, 2097156, 256), ): fingerprinter = ob.OBFingerprint.FindFingerprint(name) v = ob.vectorUnsignedInt() fingerprinter.GetFingerprint(mol, v) size = next_highest_power_of_two(nbits)//32 # bits-per-int self.assertEqual(len(v), size) self.assertEqual(v[0], v0, (name, v[0], v0)) self.assertEqual(v[1], v1, (name, v[1], v1))
def symmetries(self): '''Find the atom symmetrys in the molecule. @Returns a tuple such as [[1,2,3], [4,5]] ''' # Get symmetry classes gs = ob.OBGraphSym(self.__obmol) sym_classes = ob.vectorUnsignedInt() gs.GetSymmetry(sym_classes) sym_list = [x for x in sym_classes] sym_maps = {} i = 0 for atom in ob.OBMolAtomIter(self.__obmol): sym_grp = sym_list[i] if sym_grp in sym_maps: sym_maps[sym_grp].append(atom.GetIdx()) else: sym_maps[sym_grp] = [atom.GetIdx()] i += 1 return [v for k, v in sym_maps.items()]
def test_tanimoto_size_mismatch(self): v1 = ob.vectorUnsignedInt([0x1, 0x6]) v2 = ob.vectorUnsignedInt([1, 2, 0]) x = ob.OBFingerprint.FindFingerprint("FP2") self.assertEqual(x.Tanimoto(v1, v2), -1.0)
def test_tanimoto(self): v1 = ob.vectorUnsignedInt([0x1, 0x6]) v2 = ob.vectorUnsignedInt([0x1, 0x7]) x = ob.OBFingerprint.FindFingerprint("FP2") self.assertEqual(x.Tanimoto(v1, v2), (1 + 2) / (1 + 3 + 0.0))
def test_tanimoto_with_no_set_bits(self): v1 = ob.vectorUnsignedInt([0, 0, 0, 0]) x = ob.OBFingerprint.FindFingerprint("FP2") # Again, this is an arbitrary decision by toolkit providers self.assertEqual(x.Tanimoto(v1, v1), 0.0)