如果你的環(huán)境還沒準(zhǔn)備好, 可以官方網(wǎng)站看如何配置環(huán)境:http://openbabel.org/wiki/Install_Python_bindings
1. 通過使用OBMol, OBAtom和OBBond來創(chuàng)建原子和鍵
import openbabel
mol = openbabel.OBMol()
print 'Should print 0 (atoms)'
print mol.NumAtoms()
a = mol.NewAtom()
a.SetAtomicNum(6) # carbon atom
a.SetVector(0.0, 1.0, 2.0) # coordinates
b = mol.NewAtom()
mol.AddBond(1, 2, 1) # atoms indexed from 1
print 'Should print 2 (atoms)'
print mol.NumAtoms()
print 'Should print 1 (bond)'
print mol.NumBonds()
mol.Clear();
2. 通過OBConversion來讀取分子, 并輸出不同格式文件或字符串值
import openbabel
obConversion = openbabel.OBConversion()
obConversion.SetInAndOutFormats("smi", "mdl") //讀取smiles值, 然后輸出mdl值
mol = openbabel.OBMol()
obConversion.ReadString(mol, "C1=CC=CS1")
print 'Should print 5 (atoms)'
print mol.NumAtoms()
mol.AddHydrogens()
print 'Should print 9 (atoms) after adding hydrogens'
print mol.NumAtoms() //輸出原子個(gè)數(shù)
outMDL = obConversion.WriteString(mol)
3. 計(jì)算fp值
import pybel
smiles = ['CCCC', 'CCCN']
mols = [pybel.readstring("smi", x) for x in smiles] # Create two molecules from the SMILES
fps = [x.calcfp() for x in mols] # Calculate their fingerprints
print fps[0].bits, fps[1].bits
print fps[0].fp[0]
mol2 = pybel.readstring('smi', 'c2ccc1ccccc1c2')
fp2 = mol2.calcfp("FP4")
print fp2
print fp2.bits
mol3 = pybel.readstring('smi', 'C1CCCCC1')
fp3 = mol3.calcfp()
print fp3.__or__(fp2) //計(jì)算相似度值
4. 讀取sdf文件
#encoding=utf-8
import pybel
for mymol in pybel.readfile("sdf", "structures_all.sdf"):
fp = mymol.calcfp("FP2")
print fp
5. 輸出txt文件和sdf文件
print mymol.write("smi") //'CCCC'
mymol.write("smi", "outputfile.txt")
largeSDfile = Outputfile("sdf", "multipleSD.sdf")
largeSDfile.write(mymol)
largeSDfile.write(myothermol)
largeSDfile.close()
posted on 2009-10-25 12:37
周銳 閱讀(3734)
評(píng)論(0) 編輯 收藏 所屬分類:
Chemistry 、
Java 、
Openbabel