如果你的环境还没准备好, 可以官方网站看如何配置环境:http://openbabel.org/wiki/Install_Python_bindings
1. 通过使用OBMol, OBAtom和OBBond来创建原子和键
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() //输出原子个数
outMDL = obConversion.WriteString(mol)
3. 计算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) //计算相似度值
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
周锐 阅读(3708)
评论(0) 编辑 收藏 所属分类:
Chemistry 、
Java 、
Openbabel