也许回答有点晚了,但这些方法对我有用
如果你想要位(0和1):
from rdkit.Chem import AllChem
from rdkit.Chem import DataStructs
mol = Chem.MolFromSmiles('c1cccnc1C')
fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2, nBits=1024)
array = np.zeros((0, ), dtype=np.int8)
DataStructs.ConvertToNumpyArray(fp, array)
回到指纹:
bitstring = "".join(array.astype(str))
fp2 = DataStructs.cDataStructs.CreateFromBitString(bitstring)
assert list(fp.GetOnBits()) == list(fp2.GetOnBits())
如果你想要计数:
fp3 = AllChem.GetHashedMorganFingerprint(mol, 2, nBits=1024)
array = np.zeros((0,), dtype=np.int8)
DataStructs.ConvertToNumpyArray(fp3, array)
print(array.nonzero())
输出:
(array([ 19, 33, 64, 131, 175, 179, 356, 378, 428, 448, 698, 707, 726,
842, 849, 889]),)
回到指纹(不确定这是最好的方法):
def numpy_2_fp(array):
fp = DataStructs.cDataStructs.UIntSparseIntVect(len(array))
for ix, value in enumerate(array):
fp[ix] = int(value)
return fp
fp4 = numpy_2_fp(array)
assert fp3.GetNonzeroElements() == fp4.GetNonzeroElements()