Drkcore

12 01 2010 chemoinformatics Python GASTON Tweet

GASTONの出力ファイルをsmilesに変換する

SDFからGASTON実行できるようにしたので、結果を解釈するためにsmilesに変換するコードを用意した。

import re
import openbabel as ob

def convert(gf):
    obc = ob.OBConversion()
    obc.SetOutFormat('smi')
    mol = ob.OBMol()

    for l in gf.split('\n'):
        if len(l) > 0 and l[0] == 'v':
            a = mol.NewAtom()
            atomic_num = int(l.split(' ')[2])
            a.SetAtomicNum(atomic_num)

        elif len(l) > 0 and l[0] == 'e':
            begin_atom_idx = int(l.split(' ')[1]) + 1
            end_atom_idx   = int(l.split(' ')[2]) + 1
            bond_order     = int(l.split(' ')[3])
            b = mol.AddBond(begin_atom_idx, end_atom_idx, bond_order)

        elif len(l) > 0 and l[0] == '#':
            title = l.split(' ')[1]
            mol.SetTitle(title)

    return obc.WriteString(mol)

if __name__ == '__main__':
    txt = open('gaston.out').read()
    p = re.compile('#.+?(?=(#|$))',re.S)
    m = p.finditer(txt)

    for ss in m:
        print convert(ss.group())[:-1]

実行結果

NC  11
NCC 11
NCCC    11
NCC(=C)C    11
NCCC=C  11
NCC(=C)C=C  11
NCCC=CC 11
NCC(=C)C=CC 11
NCC=C   11

頻度の高いフラグメントのSMILESと、その分子のIDが出力される


それにしてもpythonの正規表現はややこしいなぁと、みんなのpythonを読み返して復習

ProductName みんなのPython
柴田 淳
ソフトバンククリエイティブ / ¥ 2,940 ()


正規表現文字列 -> コンパイル -> 正規表現オブジェクト -> マッチ処理 -> マッチオブジェクト

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021