⽣命情報実験a(情報系課題) - github pages...準備 rdkitをインストール...
TRANSCRIPT
機械学習の流れ
学習
学習アルゴリズム
前処理
トレーニングデータ
テストデータ
ラベル
⽣データ+
評価
予測モデル
ラベル
• 特徴抽出• スケーリング• 特徴選択• 次元削減• サンプリング
• モデル選択• 交差検証• 性能指標• ハイパーパラメータ最適化
準備︓RDkitをインストール
• RDKitとは︖– コンピュータで化合物情報を扱うために⽤いられるオープンソースのライブラリ
– ⼤部分はC++で記述されているため⾮常に⾼速で、Pythonからも利⽤可能
– 本課題では、化合物構造の表⽰や特徴抽出に利⽤する。• Google ColaboratoryでRDkitを使えるようにするために以下を実⾏する。!curl -Lo rdkit_installer.py https://git.io/fxiPZimport rdkit_installer%time rdkit_installer.install()
題材︓Tox21 Data Challenge 2014
• ⽶国NIH主導の下で開催された計算毒性学のデータコンペティション
• 核内受容体(7種)およびストレス応答タンパク質(5種)と低分⼦化合物(およそ1万種)の結合評価データ
• 本課題では、アンドロゲン受容体 (AR) に関するデータを⽤いて結合予測モデルを構築する。
前準備︓データのダウンロード
• Tox21データをダウンロードする。
• ARに関するデータのみを選択する。
!curl -Lo tox21_10k_all.sdf.zip https://bit.ly/2T6beHP!unzip tox21_10k_all.sdf.zip
from rdkit import Chem
sup = Chem.SDMolSupplier('tox21_10k_data_all.sdf')train = [mol for mol in sup
if mol is not None and 'NR-AR' in mol.GetPropsAsDict()]
⋯
⋯
化合物構造情報のフォーマット
データ形式︓
sdfファイル形式
座標
原⼦数 結合数
化合物の(2次)構造式
RDkitによる化合物構造式の表⽰
• 最初の化合物の構造を表⽰してみる。from rdkit.Chem import Draw
display(Draw.MolToImage(train[0]))
出⼒︓
前処理︓特徴抽出
• Morgan Fingerprint(後述)による特徴抽出from rdkit.Chem import AllChemfrom rdkit import DataStructs
X = [ AllChem.GetMorganFingerprintAsBitVect(m, 2) for m in train]X = [ np.asarray(x) for x in X]X = np.vstack(X)
y = [m.GetIntProp('NR-AR') for m in train]y = np.array(y)
print(X.shape, y.shape)
X: (9357, 2048) y: (9357,)
出⼒︓
Morgan Fingerprint
• Extended Connectivity Fingerprint (ECFP)
[https://docs.chemaxon.com/display/docs/Extended+Connectivity+Fingerprint+ECFP]
Morgan Fingerprint
• Extended Connectivity Fingerprint (ECFP)
[https://chembioinfo.wordpress.com/2011/10/30/revisiting-molecular-hashed-fingerprints/]
前処理︓データの分割
• トレーニングデータとテストデータに分割する。from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1, stratify=y)
print('Labels counts in y:', np.bincount(y))print('Labels counts in y_train:', np.bincount(y_train))print('Labels counts in y_test:', np.bincount(y_test))
Labels counts in y: [8977 380] Labels counts in y_train: [6732 285] Labels counts in y_test: [2245 95]
出⼒︓
学習︓トレーニングデータによる予測モデル学習
• パーセプトロンと呼ばれる学習アルゴリズムにより学習
• 他に使⽤可能なアルゴリズム(⼀部)– k最近傍法 (sklearn.neighbors.KNeighborsClassifier)– ロジスティック回帰 (sklearn.linear_model.LogisticRegression)– サポートベクタマシン (sklearn.svm.SVC)– ランダムフォレスト (sklearn.ensemble.RandomForestClassifier)
from sklearn.linear_model import Perceptron
ppn = Perceptron(max_iter=40, tol=1e-3, eta0=0.1, random_state=1234)ppn.fit(X_train, y_train)
評価︓テストデータによる精度評価
• テストデータにおける精度を計算する。from sklearn.metrics import accuracy_score, f1_score
y_test_pred = ppn.predict(X_test)print("Acc:", accuracy_score(y_test , y_test_pred)) print("F:", f1_score(y_test, y_test_pred))
Acc: 0.9666666666666667 F: 0.5617977528089888
出⼒︓
⼆値分類の予測精度
• Accuracy
• Positive Predictive Value (Precision), Sensitivity (Recall)
• F値
予測positive negative
正解
positive true positive(TP)
false negative (FN)
negative false positive(FP)
true negative (TN)
Acc =TP + TN
TP + TN + FP + FN
F =2⇥ PPV ⇥ Sen
PPV + Sen
PPV =TP
TP + FP, Sen =
TP
TP + FN
混同⾏列︓
positiveとnegativeの数に偏りがあると困る
この⼆つはトレードオフの関係にある
評価︓予測モデルの可視化
• 主成分分析を⽤いて次元削減し、散布図にプロットする。
• plot_decision_resionsはnotebookからコピーする。
from sklearn.decomposition import PCA
pca = PCA(n_components=2)X_combined = np.vstack((X_train, X_test))y_combined = np.hstack((y_train, y_test))X_transformed = pca.fit_transform(X_combined)test_idx = range(X_train.shape[0], X_combined.shape[0])plot_decision_regions(X_transformed, y_combined, ppn,
test_idx=test_idx, pca=pca)plt.xlabel('PC1 ({:.4f})'.format(pca.explained_variance_ratio_[0]))plt.ylabel('PC2 ({:.4f})'.format(pca.explained_variance_ratio_[1]))plt.legend(loc='upper left')plt.tight_layout()
新規予測︓候補化合物8種の結合予測
• 新しい化合物が結合するかを予測する。!curl -Lo compound_list.sdf https://git.io/JvhPK
sup = Chem.SDMolSupplier('compound_list.sdf')unknowns = [mol for mol in sup if mol is not None]cids = [mol.GetProp('PUBCHEM_COMPOUND_CID') for mol in unknowns]Draw.MolsToGridImage(unknowns, legends=cids, molsPerRow=4)
出⼒︓
新規予測︓候補化合物8種の結合予測
• 新しい化合物が結合するかを予測する。
• ここで出⼒された機械学習による予測がAutodockによるドッキング解析の結果とどのように異なるかを確認する。
X_unknowns = [ AllChem.GetMorganFingerprintAsBitVect(m, 2)for m in unknowns]
X_unknowns = [ np.asarray(x) for x in X_unknowns]X_unknowns = np.vstack(X_unknowns)y_unknowns_pred = ppn.predict(X_unknowns)for cid, pred in zip(cids, y_unknowns_pred):print(cid, pred)