1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Google Colabで分子の構造式を描画し保存する

Posted at

Pythonを使って分子構造を描画する

PythonはRDkitを使って分子の構造式を描画することができます。今回はGoogle Colab上で描画して保存します。

やり方と結果

分子の構造式を描画するにあたって、smiles表記の分子を準備してください。分子のsmilesへの変換はここなどを参照。今回は6種類の分子[2-メチルブタン, プロピレン, アセトン, ベンゼン, m-キシレン, 安息香酸]を描画してみます。得られる分子の構造式は以下です。

molecular_structures.png

実装

描画

以下を実行するとGoogle Colab上に描画されます。

!pip install rdkit

from rdkit import Chem
import numpy as np

# 描画する化合物のSMILES
smiles_list = [
    'CC(C)CC',  # 2-メチルブタン
    'CC=C',  # プロピレン
    'CC(=O)C',  # アセトン
    'c1ccccc1',    # ベンゼン
    'Cc1cccc(C)c1',  # m-キシレン
    'c1ccc(c(c1)C(=O)O)',  # 安息香酸
]

mols = [Chem.MolFromSmiles(smiles) for smiles in smiles_list]
Chem.Draw.MolsToGridImage(mols, molsPerRow=3, subImgSize=(300,300), useSVG=True)

保存

以下実行するとpng形式、svg形式で保存されます。

# pngで保存
ncols = 3
nrows = int(np.ceil(len(mols) / ncols))
w, h = 300, 300

drawer = Chem.Draw.rdMolDraw2D.MolDraw2DCairo(ncols*w, nrows*h, w, h)
drawer.DrawMolecules(mols)
drawer.FinishDrawing()
png_bytes = drawer.GetDrawingText()

with open('molecular_structures.png', 'wb') as f:
    f.write(png_bytes)

# svgで保存
img_s = Chem.Draw.MolsToGridImage(mols, molsPerRow=3, subImgSize=(300,300), useSVG=True)

with open(f'molecular_structures.svg', mode='w') as f:
    f.write(img_s.data)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?