化學資訊學入門:使用 Pandas 輔助化學數據處理

從 RDKit 到 Pandas 的實作

RDKit 是一個強大的 Python 函式庫,可以處理各種化學資訊的開源函式庫。RDKit 中的 Chem.PandasTools 支援多種便利功能,可以將 RDKit 分子轉換為 Pandas DataFrame 處理。

先導入需要用到的一些 library :

from rdkit import rdBase, Chem, DataStructs
from rdkit.Chem import AllChem, Draw, Descriptors, PandasTools
import pandas as pd
import xlsxwriter

準備數據

這次我們使用的資料集「Platinum dataset」是德國漢堡大學透過高精度 X- ray 測定得到的化合物分子構造。相關論文:

High-Quality Dataset of Protein-Bound Ligand Conformations and Its Application to Benchmarking Conformer Ensemble Generators

(J. Chem. Inf. Model. 57, 3, 529–539.)

Benchmarking Commercial Conformer Ensemble Generators

(J. Chem. Inf. Model. 57, 11, 2719–2728.)

使用 PandasTools 讀取 sdf 檔並儲存成DataFrame形式:

df = PandasTools.LoadSDF('C:/Users/.../platinum_dataset_2017_01.sdf')
df.head()

輸出結果如下:

檢索部分分子結構

之後我們可以檢索數據集中所有分子式中所存在的苯環並進行定位:

benzene_ring = Chem.MolFromSmiles('c1ccccc1')
df[df.ROMol >= benzene_ring].head()

輸出結果如下:

需要注意的地方是,當分子式中含有多個苯環的時候,只會標記其中的一個。另外,我們也可以用這個方法去檢索一些比較特殊的部分分子構造。

擷取並加入 Bemis-Murcko 框架

Bemis・Murcko 定義的分子框架指是和連結構造 (linker) 相接的環狀構造。

可以使用 Bemis-Murcko 框架來提取分子框架 (moleculer framwork) 以及分子框架的smiles。

PandasTools.AddMoleculeColumnToFrame(df, molCol='Murcko_Mol', smilesCol='Murcko_SMILES')
df.head(10)

輸出結果如下:

最後

我們可以將 Pandas 編輯完的 DataFrame 儲存到 excel

PandasTools.SaveXlsxFromFrame(df, 'C:/Users/.../sample.xls', molCol='Murcko_Mol', size=(150,150))

此外,如果要將資訊儲存到原來的 sdf 檔案裡,可以使用下列指令

WriteSDF(dataframe,output_file,idName=None,properties=None)

這次簡單介紹了 RDKit 和 Pandas 一起使用的例子,除此之外 RDKit 還有很多方便及強大的功能。下次有機會再做介紹!

(感謝@yweilin753@uentinnlea共同編輯)

--

--

Chemistry with data magic

I am working on improving material developments by creating machine learning analytical tools for chemical data to accelerate the material discovery.