def test1Basics(self): fs = [ FreeChemicalFeature('Aromatic', 'Foo', Point3D(0, 0, 0)), FreeChemicalFeature('Acceptor', 'Foo', Point3D(2, 0, 0)), FreeChemicalFeature('Acceptor', 'Foo', Point3D(2.1, 0, 0)) ] aFmp = FeatMaps.FeatMapParams() aFmp.radius = 2.0 bFmp = FeatMaps.FeatMapParams() bFmp.radius = 1.0 fmps = {'Aromatic': aFmp, 'Acceptor': bFmp} fmap = FeatMaps.FeatMap(params=fmps) fmap.AddFeature(fs[0], 1.0) fmap.AddFeature(fs[1], 1.0) fmap.AddFeature(fs[2], 2.0) fmap.AddFeature(fs[2], 2.0) self.assertTrue(fmap.GetNumFeatures() == 4) self.assertTrue(len(fmap.GetFeatures()) == 4) fmap.DropFeature(3) self.assertTrue(fmap.GetNumFeatures() == 3) self.assertTrue(len(fmap.GetFeatures()) == 3) f = fmap.GetFeature(0) self.assertTrue(f.GetFamily() == 'Aromatic') self.assertTrue(feq(f.weight, 1.0)) f = fmap.GetFeature(1) self.assertTrue(f.GetFamily() == 'Acceptor') self.assertTrue(feq(f.weight, 1.0)) f = fmap.GetFeature(2) self.assertTrue(f.GetFamily() == 'Acceptor') self.assertTrue(feq(f.weight, 2.0))
def test4FeatFeatScoreBox(self): aFmp = FeatMaps.FeatMapParams() aFmp.radius = 2.0 aFmp.featProfile = FeatMaps.FeatMapParams.FeatProfile.Box fmps = {'Aromatic': aFmp} fmap = FeatMaps.FeatMap(params=fmps) fs = [FreeChemicalFeature('Aromatic', 'Foo', Point3D(0, 0, 0))] fmap.AddFeature(fs[0], 1.1) self.assertTrue(len(fmap.GetFeatures()) == 1) sc = fmap.GetFeatFeatScore( fmap.GetFeature(0), FreeChemicalFeature('Aromatic', '', Point3D(1, 0, 0))) self.assertTrue(feq(sc, 1.1)) sc = fmap.GetFeatFeatScore( fmap.GetFeature(0), FreeChemicalFeature('Aromatic', '', Point3D(1.5, 0, 0))) self.assertTrue(feq(sc, 1.1)) sc = fmap.GetFeatFeatScore( fmap.GetFeature(0), FreeChemicalFeature('Aromatic', '', Point3D(0, 0, 0))) self.assertTrue(feq(sc, 1.1)) sc = fmap.GetFeatFeatScore( fmap.GetFeature(0), FreeChemicalFeature('Aromatic', '', Point3D(2.1, 0, 0))) self.assertTrue(feq(sc, 0))
def test5ScoreFeats(self): aFmp = FeatMaps.FeatMapParams() bFmp = FeatMaps.FeatMapParams() fmps = {'Aromatic': aFmp, 'Acceptor': bFmp} fmap = FeatMaps.FeatMap(params=fmps) fs = [ FreeChemicalFeature('Aromatic', 'Foo', Point3D(0, 0, 0)), FreeChemicalFeature('Acceptor', 'Foo', Point3D(2, 0, 0)), FreeChemicalFeature('Acceptor', 'Foo', Point3D(2.1, 0, 0)) ] fmap.AddFeature(fs[0], 1.1) fmap.AddFeature(fs[1], 1.1) fmap.AddFeature(fs[2], 2.1) l1 = fmap._loopOverMatchingFeats( FreeChemicalFeature('Aromatic', '', Point3D(0, 0, 0))) l1 = list(l1) self.assertTrue(len(l1) == 1) self.assertTrue(l1[0][0] == 0) self.assertTrue(l1[0][1].GetFamily() == 'Aromatic') l1 = fmap._loopOverMatchingFeats( FreeChemicalFeature('Acceptor', '', Point3D(0, 0, 0))) l1 = list(l1) self.assertTrue(len(l1) == 2) self.assertTrue(l1[0][0] == 1) self.assertTrue(l1[0][1].GetFamily() == 'Acceptor') self.assertTrue(l1[1][0] == 2) self.assertTrue(l1[1][1].GetFamily() == 'Acceptor')
def test2FeatFeatScoreGauss(self): aFmp = FeatMaps.FeatMapParams() aFmp.radius = 2.0 fmps = {'Aromatic':aFmp} fmap = FeatMaps.FeatMap(params=fmps) fs = [FreeChemicalFeature('Aromatic','Foo',Point3D(0,0,0))] fmap.AddFeature(fs[0],1.0) self.failUnless(len(fmap.GetFeatures())==1) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(1,0,0))) self.failUnless(feq(sc,math.exp(-1))) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(1.5,0,0))) self.failUnless(feq(sc,math.exp(-2.25))) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(0,0,0))) self.failUnless(feq(sc,1.0)) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(2.1,0,0))) self.failUnless(feq(sc,0)) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Acceptor','',Point3D(1,0,0))) self.failUnless(feq(sc,0)) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Acceptor','',Point3D(1,0,0)), typeMatch=False) self.failUnless(feq(sc,math.exp(-1))) self.failUnlessRaises(IndexError,lambda: fmap.GetFeatFeatScore(fmap.GetFeature(1),FreeChemicalFeature('Aromatic','',Point3D(0,0,0))))
def get_FeatureMapScore(small_m, large_m, score_mode=FeatMaps.FeatMapScoreMode.All): featLists = [] for m in [small_m, large_m]: rawFeats = fdef.GetFeaturesForMol(m) # filter that list down to only include the ones we're intereted in featLists.append([f for f in rawFeats if f.GetFamily() in keep]) fms = [FeatMaps.FeatMap(feats=x, weights=[1] * len(x), params=fmParams) for x in featLists] fms[0].scoreMode = score_mode fm_score = fms[0].ScoreFeats(featLists[1]) / min(fms[0].GetNumFeatures(), len(featLists[1])) return fm_score
def test7ScoreFeats(self): aFmp = FeatMaps.FeatMapParams() aFmp.radius = 2.0 aFmp.featProfile = FeatMaps.FeatMapParams.FeatProfile.Box bFmp = FeatMaps.FeatMapParams() bFmp.radius = 1.0 bFmp.featProfile = FeatMaps.FeatMapParams.FeatProfile.Box fmps = {'Aromatic': aFmp, 'Acceptor': bFmp} fmap = FeatMaps.FeatMap(params=fmps) fs = [ FreeChemicalFeature('Aromatic', 'Foo', Point3D(0, 0, 0)), ] fmap.AddFeature(fs[0], 1.1) fs = [ FreeChemicalFeature('Aromatic', '', Point3D(0, 1, 0)), FreeChemicalFeature('Acceptor', '', Point3D(1.5, 0, 0)), ] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, 1.1)) msv = [-1] * 1 fsv = [-1] * 2 fsfmi = [-1] * 2 sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 1.1)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], []]) fmap.scoreMode = FeatMaps.FeatMapScoreMode.Closest sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 1.1)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], []]) fmap.scoreMode = FeatMaps.FeatMapScoreMode.Best sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 1.1)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], []])
def CombineFeatMaps(fm1, fm2, mergeMetric=MergeMetric.NoMerge, mergeTol=1.5, dirMergeMode=DirMergeMode.NoMerge): """ the parameters will be taken from fm1 """ res = FeatMaps.FeatMap(params=fm1.params) __copyAll(res, fm1, fm2) if mergeMetric != MergeMetric.NoMerge: MergeFeatPoints(res, mergeMetric=mergeMetric, mergeTol=mergeTol) return res
def get_FeatureMapScore(ref_mol, query_mol): featLists = [] for m in [ref_mol, query_mol]: rawFeats = fdef.GetFeaturesForMol(m) # filter that list down to only include the ones we're interested in featLists.append([f for f in rawFeats if f.GetFamily() in keep]) fms = [ FeatMaps.FeatMap(feats=x, weights=[1] * len(x), params=fmParams) for x in featLists ] #utils.log("Calc:", str(fms[0].ScoreFeats(featLists[1])), "/ float(min(", str(fms[0].GetNumFeatures()), str(len(featLists[1])), "))") fm_score = fms[0].ScoreFeats(featLists[1]) / float( min(fms[0].GetNumFeatures(), len(featLists[1]))) return fm_score
def get_FeatureMapScore(small_feats, large_feats, tani=False, score_mode=FeatMaps.FeatMapScoreMode.All): """ Generate the feature map score. :param small_feats: :param large_feats: :param tani: :return: """ featLists = [] for rawFeats in [small_feats, large_feats]: # filter that list down to only include the ones we're interested in featLists.append(rawFeats) fms = [ FeatMaps.FeatMap(feats=x, weights=[1] * len(x), params=fmParams) for x in featLists ] # set the score mode fms[0].scoreMode = score_mode try: if tani: c = fms[0].ScoreFeats(featLists[1]) A = fms[0].GetNumFeatures() B = len(featLists[1]) if B != fms[1].GetNumFeatures(): utils.log("Why isn't B equal to number of features...?!") tani_score = float(c) / (A + B - c) return tani_score else: fm_score = fms[0].ScoreFeats(featLists[1]) / min( fms[0].GetNumFeatures(), len(featLists[1])) return fm_score except ZeroDivisionError: utils.log("ZeroDivisionError") return 0 if tani: tani_score = float(c) / (A + B - c) return tani_score else: fm_score = fms[0].ScoreFeats(featLists[1]) / min( fms[0].GetNumFeatures(), len(featLists[1])) return fm_score
def get_fm_score(self, small_m=None, large_m=None, score_mode=FeatMaps.FeatMapScoreMode.All): ''' Get the feature-map score for a small molecule vs. a large molecule :param small_m: rdkit mol object for the small molecule :param large_m: rdkit mol object for the large molecule :param score_mode: default = featuremaps score, defined in init :return: the value of the feature map score ''' if not small_m and not self.reference: raise Exception( 'Reference molecule not set! Please specify or provide to init' ) if not large_m and not self.target_s: raise Exception( 'Target molecule(s) not set! Please specify or provide to init' ) if not small_m: small_m = self.reference if not large_m: large_m = self.target_s feat_lists = [] for m in [small_m, large_m]: rawFeats = self.fdef.GetFeaturesForMol(m) # filter that list down to only include the ones we're interested in feat_lists.append( [f for f in rawFeats if f.GetFamily() in self.keep]) fms = [ FeatMaps.FeatMap(feats=x, weights=[1] * len(x), params=self.fmParams) for x in feat_lists ] fms[0].scoreMode = score_mode fm_score = fms[0].ScoreFeats(feat_lists[1]) / min( fms[0].GetNumFeatures(), len(feat_lists[1])) return fm_score
def Parse(self, featMap=None): if featMap is None: featMap = FeatMaps.FeatMap() l = self._NextLine().strip() while l: splitL = l.split('=') if len(splitL) == 1: keyword = splitL[0].strip().lower() if keyword == 'beginpoints': pts = self.ParseFeatPointBlock() for pt in pts: featMap.AddFeatPoint(pt) elif keyword == 'beginparams': featMap.params = self.ParseParamBlock() else: raise FeatMapParseError( 'Unrecognized keyword %s on line %d' % (keyword, self._lineNum)) else: keyword = splitL[0].strip().lower() val = splitL[1].strip() if keyword == 'scoremode': try: featMap.scoreMode = getattr(FeatMaps.FeatMapScoreMode, val) except AttributeError: raise FeatMapParseError( 'ScoreMode %s not recognized on line %d' % (val, self._lineNum)) elif keyword == 'dirscoremode': try: featMap.dirScoreMode = getattr( FeatMaps.FeatDirScoreMode, val) except AttributeError: raise FeatMapParseError( 'DirScoreMode %s not recognized on line %d' % (val, self._lineNum)) else: raise FeatMapParseError( 'Unrecognized keyword %s on line %d' % (keyword, self._lineNum)) l = self._NextLine().strip() return featMap
def test3FeatFeatScoreTriangle(self): aFmp = FeatMaps.FeatMapParams() aFmp.width = 2.0 aFmp.radius = 3.0 aFmp.featProfile=FeatMaps.FeatMapParams.FeatProfile.Triangle fmps = {'Aromatic':aFmp} fmap = FeatMaps.FeatMap(params=fmps) fs = [FreeChemicalFeature('Aromatic','Foo',Point3D(0,0,0))] fmap.AddFeature(fs[0],1.0) self.failUnless(len(fmap.GetFeatures())==1) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(1,0,0))) self.failUnless(feq(sc,0.5)) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(1.5,0,0))) self.failUnless(feq(sc,0.25)) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(0,0,0))) self.failUnless(feq(sc,1.0)) sc = fmap.GetFeatFeatScore(fmap.GetFeature(0),FreeChemicalFeature('Aromatic','',Point3D(2.1,0,0))) self.failUnless(feq(sc,0))
def test8ScoreFeatDirs(self): aFmp = FeatMaps.FeatMapParams() aFmp.radius = 2.0 aFmp.featProfile = FeatMaps.FeatMapParams.FeatProfile.Box fmps = {'Acceptor': aFmp} fmap = FeatMaps.FeatMap(params=fmps) fmap.dirScoreMode = FeatMaps.FeatDirScoreMode.DotFullRange fs = [ FreeChemicalFeature('Acceptor', 'Foo', Point3D(0, 0, 0)), ] fs[0].featDirs = [Point3D(0, 1, 0)] fmap.AddFeature(fs[0], 1.1) fs = [ FreeChemicalFeature('Acceptor', '', Point3D(1.0, 0, 0)), ] fs[0].featDirs = [Point3D(0, 1, 0)] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, 1.1)) fs[0].featDirs = [Point3D(0, -1, 0)] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, -1.1)) fs[0].featDirs = [Point3D(0, 0, 1)] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, 0.0)) fmap.dirScoreMode = FeatMaps.FeatDirScoreMode.DotPosRange fs[0].featDirs = [Point3D(0, -1, 0)] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, 0)) fs[0].featDirs = [Point3D(0, 1, 0)] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, 1.1))
def create_feature_map(mol): feats = get_raw_features(mol) return FeatMaps.FeatMap(feats=feats, weights=[1] * len(feats), params=fmaps.params)
def test6ScoreFeats(self): aFmp = FeatMaps.FeatMapParams() aFmp.radius = 2.0 bFmp = FeatMaps.FeatMapParams() bFmp.radius = 1.0 # use box scoring to make math easier... the other scoring # functions are tested above aFmp.featProfile = FeatMaps.FeatMapParams.FeatProfile.Box bFmp.featProfile = FeatMaps.FeatMapParams.FeatProfile.Box fmps = {'Aromatic': aFmp, 'Acceptor': bFmp} fmap = FeatMaps.FeatMap(params=fmps) fs = [ FreeChemicalFeature('Aromatic', 'Foo', Point3D(0, 0, 0)), FreeChemicalFeature('Acceptor', 'Foo', Point3D(2, 0, 0)), FreeChemicalFeature('Acceptor', 'Foo', Point3D(2.1, 0, 0)) ] fmap.AddFeature(fs[0], 1.1) fmap.AddFeature(fs[1], 1.1) fmap.AddFeature(fs[2], 2.1) fs = [ FreeChemicalFeature('Aromatic', '', Point3D(0, 1, 0)), FreeChemicalFeature('Acceptor', '', Point3D(1.5, 0, 0)), ] sc = fmap.ScoreFeats(fs) self.assertTrue(feq(sc, 4.3)) msv = [-1] * 3 fsv = [-1] * 2 fsfmi = [None] * 2 sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 4.3)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], [1, 2]]) # make sure we reset the vectors internally: sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 4.3)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], [1, 2]]) fmap.scoreMode = FeatMaps.FeatMapScoreMode.Closest sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 2.1)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], [1]]) fmap.scoreMode = FeatMaps.FeatMapScoreMode.Best sc = fmap.ScoreFeats(fs, mapScoreVect=msv, featsScoreVect=fsv, featsToFeatMapIdx=fsfmi) self.assertTrue(feq(sc, 3.2)) self.assertTrue(feq(sum(msv), sc)) self.assertTrue(feq(sum(fsv), sc)) self.assertTrue(fsfmi == [[0], [2]])
def getFeatureMap(mol): feats = getRawFeatures(mol) return FeatMaps.FeatMap(feats=feats, weights=[1] * len(feats), params=fmParams)