Python-Ref > Cheminformatics > OpenBabel > Hello OpenBabel
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Hello OpenBabel

First program using Pybel and OpenBabel.
OpenBabel is a toolkit for conversion and analysis of chemical structures. It contains bindings to Python which come in two flavours - openbabel and pybel modules. While the former is a straight port of OpenBabel functionality to Python, the latter provides a more convenient API for common task, built around openbabel.
The following two examples show very basic examples of using both.
Expand/Shrink
Zdroj: (babel1-1.py)
  1   import openbabel
  2   
  3   conv = openbabel.OBConversion()
  4   conv.SetInAndOutFormats("smi", "inchi")
  5    
  6   mol = openbabel.OBMol()
  7   conv.ReadString(mol, "c1ccccc1")
  8   
  9   print mol
 10   print mol.GetMolWt() # automatically adds hydrogens
 11   print mol.GetMolWt( False) # *do not* automatically adds hydrogens
 12   print conv.WriteString( mol)
stdout:
<openbabel.OBMol; proxy of <Swig Object of type 'OpenBabel::OBMol *' at 0x874800> >
78.11184
72.0642
InChI=1/C6H6/c1-2-4-6-5-3-1/h1-6H

Doba běhu: 75.5 ms
Expand/Shrink
Zdroj: (babel1-2.py)
  1   import pybel 
  2   
  3   mol = pybel.readstring( "smi", "c1ccccc1")
  4   print mol
  5   print mol.molwt
  6   print mol.write( "inchi")
stdout:
c1ccccc1	

78.11184
InChI=1/C6H6/c1-2-4-6-5-3-1/h1-6H

Doba běhu: 87.2 ms
In the following sections we will examine openbabels and pybels capabilities in more detail.