čtvrtek 26. února 2009

BKChem enters Trophees du Libre

I am pround to announce that BKChem has entered the competition for important free software awards - Trophees du Libre.

pondělí 23. února 2009

Find 9 differences

Can you find nine differences in the following two pictures?




This is the difference between double bonds for which the second line is drawn using only 2D coordinates of the end atoms and for which the complete 3D geometry is used.
Because chemical drawings, unlike real molecules, are often flat, there was never much need for me to implement the better drawing method, especially when there are always many more important things to do. However this omission cropped up again when I rotated benzene in 3D during testing of new BKChem code that allows 3D rotation of molecular fragment around a particular bond. Because I had this feature on my to-do list for a long time anyway, I finally decided to dust off my basic knowledge of analytic geometry and implement this feature.
You can see the result on the pictures above.
Note: the pictures were created using OASA 0.12.7 and 0.13.0 respectively and a similar code is used in BKChem 0.13.0.

pondělí 16. února 2009

How to embed PDF in LaTeX documents

In the previous entry to this blog, I have shown that OASA can create pictures of molecules in PDF. However a picture of a molecule as a separate PDF file is not very useful.
Today, I will show a snippet of LaTeX code that can be used to embed the exported PDF file inside a PDF document produced by pdflatex. (Please note that this post is not intended to be an in-depth tutorial, its main purpose it to inform that the possibility to use PDFs this way even exists.)
The whole LaTeX source looks like this:

\documentclass[a4paper,12pt]{article}
\usepackage{graphics}
\usepackage[left=1.5cm,right=2cm,top=2cm]{geometry}

\begin{document}

\begin{center}
\begin{large}Exam - \bf{1A}\end{large}
\end{center}

Write down SMILES for the following molecule:

\includegraphics{test}

Name the following molecule:

\includegraphics{test2}

\end{document}

The most important commands are the following two - \usepackage{graphics} and \includegraphics{test} (please note that the name of the file to embed is used without the extension .pdf).
After running pdflatex on this code you will get a PDF file with the two images embedded inside (provided the two images exist in the same directory). The result looks like this, you can download the source files of this example here.

pondělí 9. února 2009

PDF and SVG support added to OASA

In the newest versions of OASA, I have added the possibility to create images not only in PNG, but also in SVG and PDF.
This functionality comes for free as a courtesy of the Cairo library and gives the user the possibility to create output much more suitable for printing.
Here is a small example code showing how to create a PDF export:

from oasa import smiles, cairo_out
mol = smiles.text_to_mol( "Oc1ccc(N)cc1Cl")
mol.normalize_bond_length( 30)
mol.remove_unimportant_hydrogens()
cairo_out.mol_to_cairo( mol, "example.pdf", format="pdf")

The output looks like this (of course, this is rendered to PNG, the PDF can be found here):
(Because SMILES was used as input, OASA generates the 2D structure for you.)
For SVG, just replace "pdf" with "svg" in the example above.
The cairo_out.mol_to_cairo function has many keyword arguments that allows for changing of almost every aspect of the output. For example, the following adds hydrogen symbols to hetero-atoms and changes the line width for bonds.

cairo_out.mol_to_cairo( mol, "example.svg", format="svg",
show_hydrogens_on_hetero=True,
line_width=2)

The output now looks like this (again in PNG, the SVG can be found here):



One big advantage of the SVG generated by Cairo is that all text is converted to curves. This means that the output will look the same on every machine, regardless of installed fonts.