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を読み返して復習
正規表現文字列 -> コンパイル -> 正規表現オブジェクト -> マッチ処理 -> マッチオブジェクト
SDFからGASTON用のファイルを作る
gSpanやGASTONの入力はラベル付きのvertexとedgeなので、openbabelでsdfを読み込んで、GASTONのinputに変換するものを作ってみた。
import openbabel as ob
def convert(sdf):
obc = ob.OBConversion()
obc.SetInAndOutFormats('sdf','smi')
mol = ob.OBMol()
next = obc.ReadFile(mol,sdf)
molnum = 0
while next:
# mol
print "t # %d" % molnum
# atom
for i,atom in enumerate(ob.OBMolAtomIter(mol)):
print "v %d %d " % (i,atom.GetAtomicNum())
# bond
for i,bond in enumerate(ob.OBMolBondIter(mol)):
print "e %d %d %d" % (bond.GetBeginAtomIdx()-1,bond.GetEndAtomIdx()-1,bond.GetBondOrder())
mol = ob.OBMol()
next = obc.Read(mol)
molnum += 1
return True
if __name__ == "__main__":
sdffile = 'pubchem_sample.sdf'
convert(sdffile)
SDFはpubchemから適当に選んだがCID: 16757835は外しといた。
./gaston 11 pubchem_data pubchem_out
GASTONを実行した出力の一部
# 14
t 163
v 0 6
v 1 6
v 2 1
e 0 1 2
e 1 2 1
# 12
t 164
v 0 6
v 1 6
v 2 1
v 3 1
e 0 1 2
e 0 3 1
e 1 2 1
アロマティックなボンドは3みたいに別のラベルを与える必要がある気はするが、、、
後は、GASTONの出力ファイルから構造を構築するものを用意すれば、頻出する部分構造を扱えるようになる。
みんなのPython

