bkchem-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bkchem-user] Using BKChem as a library for generation of 2D structu


From: Beda Kosata
Subject: Re: [Bkchem-user] Using BKChem as a library for generation of 2D structure images
Date: Thu, 27 Mar 2008 20:49:41 +0100
User-agent: Thunderbird 2.0.0.9 (X11/20071031)

Noel O'Boyle wrote:
> 
> Hmm...it seems that it's necessary to start the Application. Do you
> have any plans to separate some of the functionality into a library
> that could be accessed without starting the App? For example, I simply
> want to (1) build a molecule (using the information in the connection
> table of a pybel molecule), (2) generate 2D layout, (3) create an
> image (preferably stored in a string, rather than in a file).

The problem here is that the started App lets you measure exact (or
almost exact) text size, thus enabling much nicer output.
Also there is the whole machinery of the App behind you, so you can
relatively easily work with color, fonts, etc.
But its true that it is slow and ineffective.

> 
> It would be great to have this as a library, not just for Pybel.
> According to today's write up on Depth First, BKChem is one of only 4
> available structure diagram generators...and the only one in Python
> (see 
> http://depth-first.com/articles/2008/03/26/five-open-tools-for-2d-structure-layout-aka-structure-diagram-generation)
> 

Interesting. The 2D coordinate generation is done in the OASA library I
mentioned. This particular part of the library was written some 3 or 4
years ago when I was doing a postdoc in Germany and had plenty of free
time in the evenings. I did not touch the code since and it has some
weak points (such as possible overlaps of atoms in highly branched
structures), etc.

This brings us nicely to the second option I mentioned - using OASA (the
library that is behind BKChem). I use it on http://inchi.info to
generate pictures in the converter. It is not able to run without
X-windows installed, because it uses cairo for picture generation and
cairo does not work without X installed. However, you don't need X
running. I use it for PNGs now, but cairo is capable of producing PDF's,
EPS's and SVG's as well.
I attach two  small scripts that demonstrate what you can do. Just copy
it to the bkchem directory where the oasa library resides and run it. As
you can see the OASA api is not that different from Pybel and it would
be almost no effort to create a bridge between the two (as I intent to
do in neat future :)
The only problem here is that I did not put that much effort into the
cairo output backend, so the result is not that nice. Also there is not
much support for configuration of display options for atoms, fancy
things like sub- and superscripts, etc. (In BKChem you can say "I want
this atom rendered with symbol on and hydrogens on, I also want each
nitrogen atom painted in green"; you cannot do this in OASA (without
some hacking)).
It all depends on what you need.

> My other question is how stable is the BKChem API?
> 

Well, it depends. Some parts of the API are pretty stable, but I don't
hesitate when I need to change something when I need to. I try to
document changes in commit logs, but not very thoroughly because I am
the only one working on BKChem.
Maybe it is better to give an example - there are some batch scripts
that I have created some two years ago that work even with recent
versions. On the other hand there are a few that I had to tweak slightly
because they used things that changed slightly over the time.

Best regards

        Beda
import oasa

mol = oasa.smiles.text_to_mol( "c1ccncc1")
# this is to get around a bug that is not fixed in the release version of oasa
# z coordinate is not set and mol.normalize_bond_length tries to operate with it
for a in mol.atoms:
  a.z = 0
# // end of bug
mol.normalize_bond_length( bond_length=30)
oasa.cairo_out.mol_to_png( mol, "out.png")
import oasa

mol = oasa.molecule()
for symbol in ["C","N","C","C"]:
  a = mol.create_vertex()
  a.symbol = symbol
  mol.add_vertex( a)
e = mol.create_edge()
e.order = 1
mol.add_edge( 0, 1, e)
mol.add_edge( 1, 2) # default edge is created
mol.add_edge( 1, 3)
oasa.coords_generator.calculate_coords( mol, bond_length=30)
oasa.cairo_out.mol_to_png( mol, "out.png")

reply via email to

[Prev in Thread] Current Thread [Next in Thread]