def pyScorePair(at1, at2, dist, atomCodes=None): """ Returns a score for an individual atom pair. >>> m = Chem.MolFromSmiles('CCCCC') >>> c1 = Utils.GetAtomCode(m.GetAtomWithIdx(0)) >>> c2 = Utils.GetAtomCode(m.GetAtomWithIdx(1)) >>> c3 = Utils.GetAtomCode(m.GetAtomWithIdx(2)) >>> t = 1 | min(c1,c2)<<numPathBits | max(c1,c2)<<(rdMolDescriptors.AtomPairsParameters.codeSize+numPathBits) >>> pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(1),1)==t 1 >>> pyScorePair(m.GetAtomWithIdx(1),m.GetAtomWithIdx(0),1)==t 1 >>> t = 2 | min(c1,c3)<<numPathBits | max(c1,c3)<<(rdMolDescriptors.AtomPairsParameters.codeSize+numPathBits) >>> pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(2),2)==t 1 >>> pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(2),2, ... atomCodes=(Utils.GetAtomCode(m.GetAtomWithIdx(0)),Utils.GetAtomCode(m.GetAtomWithIdx(2))))==t 1 """ if not atomCodes: code1 = Utils.GetAtomCode(at1) code2 = Utils.GetAtomCode(at2) else: code1, code2 = atomCodes accum = int(dist) % _maxPathLen accum |= min(code1, code2) << numPathBits accum |= max(code1, code2) << ( rdMolDescriptors.AtomPairsParameters.codeSize + numPathBits) return accum
def ExplainPairScore(score): """ >>> m = Chem.MolFromSmiles('C=CC') >>> score = pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(1),1) >>> ExplainPairScore(score) (('C', 1, 1), 1, ('C', 2, 1)) >>> score = pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(2),2) >>> ExplainPairScore(score) (('C', 1, 0), 2, ('C', 1, 1)) >>> score = pyScorePair(m.GetAtomWithIdx(1),m.GetAtomWithIdx(2),1) >>> ExplainPairScore(score) (('C', 1, 0), 1, ('C', 2, 1)) >>> score = pyScorePair(m.GetAtomWithIdx(2),m.GetAtomWithIdx(1),1) >>> ExplainPairScore(score) (('C', 1, 0), 1, ('C', 2, 1)) """ codeMask = (1 << rdMolDescriptors.AtomPairsParameters.codeSize) - 1 pathMask = (1 << numPathBits) - 1 dist = score & pathMask score = score >> numPathBits code1 = score & codeMask score = score >> rdMolDescriptors.AtomPairsParameters.codeSize code2 = score & codeMask res = Utils.ExplainAtomCode(code1), dist, Utils.ExplainAtomCode(code2) return res
def testGithub334(self): m1 = Chem.MolFromSmiles('N#C') self.assertEqual(Utils.NumPiElectrons(m1.GetAtomWithIdx(0)), 2) self.assertEqual(Utils.NumPiElectrons(m1.GetAtomWithIdx(1)), 2) m1 = Chem.MolFromSmiles('N#[CH]') self.assertEqual(Utils.NumPiElectrons(m1.GetAtomWithIdx(0)), 2) self.assertEqual(Utils.NumPiElectrons(m1.GetAtomWithIdx(1)), 2)
def ExplainPairScore(score, includeChirality=False): """ >>> from rdkit import Chem >>> m = Chem.MolFromSmiles('C=CC') >>> score = pyScorePair(m.GetAtomWithIdx(0), m.GetAtomWithIdx(1), 1) >>> ExplainPairScore(score) (('C', 1, 1), 1, ('C', 2, 1)) >>> score = pyScorePair(m.GetAtomWithIdx(0), m.GetAtomWithIdx(2), 2) >>> ExplainPairScore(score) (('C', 1, 0), 2, ('C', 1, 1)) >>> score = pyScorePair(m.GetAtomWithIdx(1), m.GetAtomWithIdx(2), 1) >>> ExplainPairScore(score) (('C', 1, 0), 1, ('C', 2, 1)) >>> score = pyScorePair(m.GetAtomWithIdx(2), m.GetAtomWithIdx(1), 1) >>> ExplainPairScore(score) (('C', 1, 0), 1, ('C', 2, 1)) We can optionally deal with chirality too >>> m = Chem.MolFromSmiles('C[C@H](F)Cl') >>> score = pyScorePair(m.GetAtomWithIdx(0), m.GetAtomWithIdx(1), 1) >>> ExplainPairScore(score) (('C', 1, 0), 1, ('C', 3, 0)) >>> score = pyScorePair(m.GetAtomWithIdx(0), m.GetAtomWithIdx(1), 1, includeChirality=True) >>> ExplainPairScore(score, includeChirality=True) (('C', 1, 0, ''), 1, ('C', 3, 0, 'R')) >>> m = Chem.MolFromSmiles('F[C@@H](Cl)[C@H](F)Cl') >>> score = pyScorePair(m.GetAtomWithIdx(1), m.GetAtomWithIdx(3), 1, includeChirality=True) >>> ExplainPairScore(score, includeChirality=True) (('C', 3, 0, 'R'), 1, ('C', 3, 0, 'S')) """ codeSize = rdMolDescriptors.AtomPairsParameters.codeSize if includeChirality: codeSize += rdMolDescriptors.AtomPairsParameters.numChiralBits codeMask = (1 << codeSize) - 1 pathMask = (1 << numPathBits) - 1 dist = score & pathMask score = score >> numPathBits code1 = score & codeMask score = score >> codeSize code2 = score & codeMask return (Utils.ExplainAtomCode(code1, includeChirality=includeChirality), dist, Utils.ExplainAtomCode(code2, includeChirality=includeChirality))
def pyScorePath(mol, path, size, atomCodes=None): """ Returns a score for an individual path. >>> from rdkit import Chem >>> m = Chem.MolFromSmiles('CCCCC') >>> c1 = Utils.GetAtomCode(m.GetAtomWithIdx(0),1) >>> c2 = Utils.GetAtomCode(m.GetAtomWithIdx(1),2) >>> c3 = Utils.GetAtomCode(m.GetAtomWithIdx(2),2) >>> c4 = Utils.GetAtomCode(m.GetAtomWithIdx(3),1) >>> t = c1 | (c2 << rdMolDescriptors.AtomPairsParameters.codeSize) | (c3 << (rdMolDescriptors.AtomPairsParameters.codeSize*2)) | (c4 << (rdMolDescriptors.AtomPairsParameters.codeSize*3)) >>> pyScorePath(m,(0,1,2,3),4)==t 1 The scores are path direction independent: >>> pyScorePath(m,(3,2,1,0),4)==t 1 >>> m = Chem.MolFromSmiles('C=CC(=O)O') >>> c1 = Utils.GetAtomCode(m.GetAtomWithIdx(0),1) >>> c2 = Utils.GetAtomCode(m.GetAtomWithIdx(1),2) >>> c3 = Utils.GetAtomCode(m.GetAtomWithIdx(2),2) >>> c4 = Utils.GetAtomCode(m.GetAtomWithIdx(4),1) >>> t = c1 | (c2 << rdMolDescriptors.AtomPairsParameters.codeSize) | (c3 << (rdMolDescriptors.AtomPairsParameters.codeSize*2)) | (c4 << (rdMolDescriptors.AtomPairsParameters.codeSize*3)) >>> pyScorePath(m,(0,1,2,4),4)==t 1 """ codes = [None] * size for i in range(size): if i == 0 or i == (size - 1): sub = 1 else: sub = 2 if not atomCodes: codes[i] = Utils.GetAtomCode(mol.GetAtomWithIdx(path[i]), sub) else: base = atomCodes[path[i]] codes[i] = base - sub # "canonicalize" the code vector: beg = 0 end = len(codes) - 1 while (beg < end): if codes[beg] > codes[end]: codes.reverse() break elif codes[beg] == codes[end]: beg += 1 end -= 1 else: break accum = 0 for i in range(size): accum |= codes[i] << (rdMolDescriptors.AtomPairsParameters.codeSize * i) return accum
def ExplainPathScore(score, size=4): """ >>> from rdkit import Chem >>> m = Chem.MolFromSmiles('C=CC') >>> score=pyScorePath(m, (0, 1, 2), 3) >>> ExplainPathScore(score, 3) (('C', 1, 0), ('C', 2, 1), ('C', 1, 1)) Again, it's order independent: >>> score = pyScorePath(m, (2, 1, 0), 3) >>> ExplainPathScore(score, 3) (('C', 1, 0), ('C', 2, 1), ('C', 1, 1)) >>> m = Chem.MolFromSmiles('C=CO') >>> score=pyScorePath(m, (0, 1, 2), 3) >>> ExplainPathScore(score, 3) (('C', 1, 1), ('C', 2, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('OC=CO') >>> score=pyScorePath(m, (0, 1, 2, 3), 4) >>> ExplainPathScore(score, 4) (('O', 1, 0), ('C', 2, 1), ('C', 2, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('CC=CO') >>> score=pyScorePath(m, (0, 1, 2, 3), 4) >>> ExplainPathScore(score, 4) (('C', 1, 0), ('C', 2, 1), ('C', 2, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('C=CC(=O)O') >>> score=pyScorePath(m, (0, 1, 2, 3), 4) >>> ExplainPathScore(score, 4) (('C', 1, 1), ('C', 2, 1), ('C', 3, 1), ('O', 1, 1)) >>> score=pyScorePath(m, (0, 1, 2, 4), 4) >>> ExplainPathScore(score, 4) (('C', 1, 1), ('C', 2, 1), ('C', 3, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('OOOO') >>> score=pyScorePath(m, (0, 1, 2), 3) >>> ExplainPathScore(score, 3) (('O', 1, 0), ('O', 2, 0), ('O', 2, 0)) >>> score=pyScorePath(m, (0, 1, 2, 3), 4) >>> ExplainPathScore(score, 4) (('O', 1, 0), ('O', 2, 0), ('O', 2, 0), ('O', 1, 0)) """ codeSize: int = rdMolDescriptors.AtomPairsParameters.codeSize codeMask = (1 << codeSize) - 1 res = [None] * size for i in range(size): if i == 0 or i == size - 1: sub = 1 else: sub = 2 code = score & codeMask score = score >> codeSize symb, nBranch, nPi = Utils.ExplainAtomCode(code) res[i] = (symb, nBranch + sub, nPi) return tuple(res)
def ExplainPathScore(score, size=4): """ >>> m = Chem.MolFromSmiles('C=CC') >>> score=pyScorePath(m,(0,1,2),3) >>> ExplainPathScore(score,3) (('C', 1, 0), ('C', 2, 1), ('C', 1, 1)) Again, it's order independent: >>> score=pyScorePath(m,(2,1,0),3) >>> ExplainPathScore(score,3) (('C', 1, 0), ('C', 2, 1), ('C', 1, 1)) >>> m = Chem.MolFromSmiles('C=CO') >>> score=pyScorePath(m,(0,1,2),3) >>> ExplainPathScore(score,3) (('C', 1, 1), ('C', 2, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('OC=CO') >>> score=pyScorePath(m,(0,1,2,3),4) >>> ExplainPathScore(score,4) (('O', 1, 0), ('C', 2, 1), ('C', 2, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('CC=CO') >>> score=pyScorePath(m,(0,1,2,3),4) >>> ExplainPathScore(score,4) (('C', 1, 0), ('C', 2, 1), ('C', 2, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('C=CC(=O)O') >>> score=pyScorePath(m,(0,1,2,3),4) >>> ExplainPathScore(score,4) (('C', 1, 1), ('C', 2, 1), ('C', 3, 1), ('O', 1, 1)) >>> score=pyScorePath(m,(0,1,2,4),4) >>> ExplainPathScore(score,4) (('C', 1, 1), ('C', 2, 1), ('C', 3, 1), ('O', 1, 0)) >>> m = Chem.MolFromSmiles('OOOO') >>> score=pyScorePath(m,(0,1,2),3) >>> ExplainPathScore(score,3) (('O', 1, 0), ('O', 2, 0), ('O', 2, 0)) >>> score=pyScorePath(m,(0,1,2,3),4) >>> ExplainPathScore(score,4) (('O', 1, 0), ('O', 2, 0), ('O', 2, 0), ('O', 1, 0)) """ codeMask = (1 << rdMolDescriptors.AtomPairsParameters.codeSize) - 1 res = [None] * size #print '>>>>>>>>>>>',score,size,codeMask for i in range(size): if i == 0 or i == (size - 1): sub = 1 else: sub = 2 code = score & codeMask #print i,code,score score = score >> rdMolDescriptors.AtomPairsParameters.codeSize symb, nBranch, nPi = Utils.ExplainAtomCode(code) expl = symb, nBranch + sub, nPi res[i] = expl return tuple(res)