데이터 과학

RDkit 사용법 본문

생명정보학 & 화학정보학/RDkit와 SMILES 형식

RDkit 사용법

티에스윤 2023. 10. 15. 12:57

RDkit은 파이썬 라이브러리를 활용하는 분자식 표현 프로그램입니다. 

오픈소스이며 가장 많이 사용되고 있는 프로그램이며, 코랩에서 사용할 수 있기에 라이브러리만 설치하면 누구나 쉽게 사용할 수 있습니다. 

 

https://rdkit.org/

 

RDKit

 

rdkit.org

파이썬 라이브러리를 사용하면 간단하게 사용할 수 있습니다. 파일형식은 SMILES를 사용하기에 SMILES 형식에 대한 이해를 해야 합니다. 

 

 

 

코랩에서 다음과 같이 실행하면 됩니다. 

!pip install rdkit
 
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Descriptors
from rdkit.Chem import AllChem
from rdkit import DataStructs
import numpy as np

 

 

IPythonConsole.ipython_useSVG=True
def mol_with_atom_index(mol):
    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(atom.GetIdx())
    return mol
# Test in a kinase inhibitor
mol = Chem.MolFromSmiles("C1CC2=C3C(=CC=C2)C(=CN3C1)[C@H]4[C@@H](C(=O)NC4=O)C5=CNC6=CC=CC=C65")
# Default
mol
 
 

 

SMILES 형식을 입력하면 그에 대한 분자식이 나타납니다. 

 

mol_with_atom_index(mol)

 

 

 
IPythonConsole.drawOptions.addAtomIndices = True
IPythonConsole.molSize = 300,300
mol = Chem.MolFromSmiles("C1CC2=C3C(=CC=C2)C(=CN3C1)[C@H]4[C@@H](C(=O)NC4=O)C5=CNC6=CC=CC=C65")
mol
 

 

 

옵션에 따라 나오는 결과가 다릅니다. 

 

 

 

Highlight a Substructure in a Molecule

 

 

m = Chem.MolFromSmiles('c1cc(C(=O)O)c(OC(=O)C)cc1')
substructure = Chem.MolFromSmarts('C(=O)O')
print(m.GetSubstructMatches(substructure))
m
 
 

 

SMILES 형식에서 위치를 찾아 substructure 변수에 하이라이트 영역을 만들어 주면 그 영역에 대한 번호를 지정해서 색을 칠하게 해 줍니다. 

 

m.__sssAtoms = [0,1,2,6,11,12]
m
 

 

 

하이라이트 위치를 바꿔 줄 수도 있습니다. 

 

 

Highlight Entire Molecule

 

from rdkit.Chem.Draw import rdMolDraw2D
import io
from PIL import Image
 
mol = Chem.MolFromSmiles('CC(C)CN1C(=O)COC2=C1C=CC(=C2)NC(=O)/C=C/C3=CC=CC=C3')
rgba_color = (0.0, 0.0, 1.0, 0.1) # transparent blue

atoms = []
for a in mol.GetAtoms():
    atoms.append(a.GetIdx())

bonds = []
for bond in mol.GetBonds():
    aid1 = atoms[bond.GetBeginAtomIdx()]
    aid2 = atoms[bond.GetEndAtomIdx()]
    bonds.append(mol.GetBondBetweenAtoms(aid1,aid2).GetIdx())

drawer = rdMolDraw2D.MolDraw2DCairo(350,300)
drawer.drawOptions().fillHighlights=True
drawer.drawOptions().setHighlightColour((rgba_color))
drawer.drawOptions().highlightBondWidthMultiplier=20
drawer.drawOptions().clearBackground = False
rdMolDraw2D.PrepareAndDrawMolecule(drawer, mol, highlightAtoms=atoms, highlightBonds=bonds)
bio = io.BytesIO(drawer.GetDrawingText())
Image.open(bio)
 
 
 
 
 

 

 

Neutralizing Molecules

 

이 예제는 배열을 활용한 예제로 여러가지 분자식을 표현할 수 있습니다. 

아래 링크에 원소스가 있습니다. 

 

https://baoilleach.blogspot.com/2019/12/no-charge-simple-approach-to.html

 

 

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
 
# list of SMILES
smiList = ['CC(CNC[O-])[N+]([O-])=O',
       'C[N+](C)(C)CCC([O-])=O',
       '[O-]C1=CC=[N+]([O-])C=C1',
       '[O-]CCCN=[N+]=[N-]',
       'C[NH+](C)CC[S-]',
       'CP([O-])(=O)OC[NH3+]']

# Create RDKit molecular objects
mols = [Chem.MolFromSmiles(m) for m in smiList]

# display
Draw.MolsToGridImage(mols,molsPerRow=3,subImgSize=(200,200))
 
 

 

 

SMILES 표기를 리스트에 입력해서 표현하는 결과입니다. 

 

def neutralize_atoms(mol):

    pattern = Chem.MolFromSmarts("[+1!h0!$([*]~[-1,-2,-3,-4]),-1!$([*]~[+1,+2,+3,+4])]")
    at_matches = mol.GetSubstructMatches(pattern)
    at_matches_list = [y[0] for y in at_matches]
    if len(at_matches_list) > 0:
        for at_idx in at_matches_list:
            atom = mol.GetAtomWithIdx(at_idx)
            chg = atom.GetFormalCharge()
            hcount = atom.GetTotalNumHs()
            atom.SetFormalCharge(0)
            atom.SetNumExplicitHs(hcount - chg)
            atom.UpdatePropertyCache()
    return mol
 
 
for mol in mols:
    neutralize_atoms(mol)
    print(Chem.MolToSmiles(mol))

Draw.MolsToGridImage(mols,molsPerRow=3, subImgSize=(200,200))

 

 

 

 

 

 

from rdkit.Chem.MolStandardize import rdMolStandardize
un = rdMolStandardize.Uncharger()
mols2 = [Chem.MolFromSmiles(m) for m in smiList]

for mol2 in mols2:
    un.uncharge(mol2)
    print(Chem.MolToSmiles(mol2))
 
 
Draw.MolsToGridImage(mols2,molsPerRow=3,subImgSize=(200,200))
 
 
 

rdMolStandardize.Uncharger에 대한 결과를 비교해 봅시다. 

 

 

 

지금까지 작성한 예제입니다. 

 

RDKit.ipynb
0.65MB

 

 

위 예제는 cookbook에 있는 예제들입니다. 한번씩 읽어보면서 실습하고 SMILES 형식을 이용해서 구조식을 만들어 보는 것이 화학식을 학습하는데 많은 도움이 될 것입니다.

 

 

RDkit cookbook 입니다. 

 

https://www.rdkit.org/docs/Cookbook.html

 

RDKit Cookbook — The RDKit 2023.09.1 documentation

If you have suggestions for how to improve the Cookbook and/or examples you would like included, please contribute directly in the source document (the .rst file). Alternatively, you can also send Cookbook revisions and addition requests to the mailing lis

www.rdkit.org

 

 

https://www.rdkit.org/docs/GettingStartedInPython.html

 

Getting Started with the RDKit in Python — The RDKit 2023.09.1 documentation

Acceptor [$([O,S;H1;v2;!$(*-*=[O,N,P,S])]),$([O,S;H0;v2]),$([O,S;-]),$([N;v3;!$(N-*=[O,N,P,S])]),n&H0&+0,$([o,s;+0;!$([o,s]:n);!$([o,s]:c:n)])] Basic [#7;+,$([N;H2&+0][$([C,a]);!$([C,a](=O))]),$([N;H1&+0]([$([C,a]);!$([C,a](=O))])[$([C,a]);!$([C,a](=O))]),

www.rdkit.org

 

https://molecularmodelingbasics.blogspot.com/2016/05/a-brief-introduction-to-smiles-and-inchi.html

 

A brief introduction to SMILES and InChI

SMILES and InChI are two notations that that can be used to compactly describe molecules.  As molecules get complicated SMILES and InC...

molecularmodelingbasics.blogspot.com

 

 

https://buildmedia.readthedocs.org/media/pdf/rdkit/release_2017_03_1/rdkit.pdf

 

 

'생명정보학 & 화학정보학 > RDkit와 SMILES 형식' 카테고리의 다른 글

SMILES 데이터 만드는 방법  (0) 2024.01.14
Colab 데이터 open 방법  (0) 2024.01.11
Pubchem과 Chembl 사용법  (4) 2023.10.14
SMILES 형식  (0) 2023.10.14