def draw_fingerprint_substructures(mol,
                                   radius,
                                   from_atoms=None,
                                   im_size_x=250,
                                   im_size_y=250):
    """ Draws the fingerprint substructures of a molecule for a specified radius. """

    # Check if the input molecule is given in SMILES or in the RDKit Mol format.
    if isinstance(mol, str):
        try:
            # Generate the RDKit Mol object from the input SMILES string.
            mol = AllChem.MolFromSmiles(mol)
            # Sanitize the molecule.
            AllChem.SanitizeMol(mol)
        except:
            print(
                "Unable to sanitize generated molecule. Check the validity of the input SMILES string."
            )

    # Generate the full-length fingerprint of the molecule and collect the info about the active bits.
    bit_info = {}
    fp = rdMolDescriptors.GetMorganFingerprintAsBitVect(mol,
                                                        radius=radius,
                                                        fromAtoms=from_atoms,
                                                        bitInfo=bit_info)
    on_bits = [(mol, x, bit_info) for x in fp.GetOnBits()]

    # Create the drawer object only for active bits.
    drawer = Draw.DrawMorganBits(on_bits,
                                 molsPerRow=3,
                                 subImgSize=(im_size_x, im_size_y),
                                 legends=[str(x) for x in fp.GetOnBits()])
    # Modify the .svg string.
    svg = drawer.replace("svg:", "")

    # Return the final Image object.
    return Image.open(io.BytesIO(svg2png(svg)))
taxol = taxol[0]
type(taxol) ### pubchempy.Compound
taxol = Chem.MolFromSmiles(taxol.canonical_smiles)
type(taxol) ### rdkit.Chem.rdchem.Mol

### Morganフィンガープリント
bitI_morgan = {}
fp_morgan = AllChem.GetMorganFingerprintAsBitVect(taxol, 2, bitInfo=bitI_morgan)


### RDKitフィンガープリント
bitI_rdkit = {}
fp_rdkit = Chem.RDKFingerprint(taxol, bitInfo=bitI_rdkit)

print(fp_morgan.GetNumBits(),fp_morgan.GetNumOnBits()) ### 2048 86
print(len(bitI_morgan)) ### 86
 
print(len(fp_rdkit), len(bitI_rdkit.keys())) ### (2048, 1444)
 
for key in list(bitI_morgan.keys())[:5]:
    print(bitI_morgan[key])
    
    
### Morgan可視化   
morgan_turples = ((taxol, bit, bitI_morgan) for bit in list(bitI_morgan.keys())[:12])
Draw.DrawMorganBits(morgan_turples, molsPerRow=4, legends=['bit: '+str(x) for x in list(bitI_morgan.keys())[:12]])

### RDKit可視化
rdkit_turples = ((taxol, bit, bitI_rdkit) for bit in list(bitI_rdkit.keys())[:12])
Draw.DrawRDKitBits(rdkit_turples, molsPerRow=4, legends=['bit: '+str(x) for x in list(bitI_rdkit.keys())[:12]])
def displayingsinrow(imgs, col=4):
    plt.figure(figsize=(20, 20))
    columns = col
    for i, image in enumerate(imgs):
        ax = plt.subplot(len(imgs) / columns, columns, i)
        ax.set_axis_off()
        plt.imshow(image)


displayingsinrow(imgs)

bi_tuple = [(mol, bit, bi) for bit in list(bi.keys())]
img = Draw.DrawMorganBits(
    bi_tuple,
    molsPerRow=4,
    subImgSize=(250, 250),
    legends=list(
        map(str, list(bi.keys()))
    )
)
# 存储为图片
with open('/drug_development/studyRdkit/st_rdcit/img/mol26.svg', 'w+') as outf:
    outf.write(img)
# 从上图我们可以看到对摩根指纹可视化的时候,不仅有片段结构,而且对原子用不同颜色进行了标注
# 1. 蓝色:说明该原子是中心原子
# 2. 黄色:说明该原子是芳香原子
# 3. 灰色:说明该原子是脂肪烃原子

# # 3.8 可视化拓扑指纹中的bit
# 拓扑指纹也称为RDKit指纹,其调用函数Chem.RDKFingerprint(mol)
mol = Chem.MolFromSmiles('c1cccnc1C')
rdkbi = {}