Herng Yi Cheng
  • Home
  • Publications
  • CV
  • Outreach
  • Art
    • Origami >
      • Origami Design Gallery
      • Origami Publications
      • Origami Research and Applications
      • Modular Polyhedra from Waterbomb Base Units
    • Performance
  • Software
    • Featured Project
  • Blog
    • Featured Post
  • Links

Hmol (Software)

7/22/2014

1 Comment

 
Back in 2010 I tinkered around with 3D graphics, and I learned how to use OpenGL, a 3D graphics engine, with the only language I knew well, Haskell. Eager to try it out, I embarked on a project to build from scratch a molecular graphics engine, because cool images of molecules always impressed me. Below are some snapshots from the program Hmol that I wrote:
Hmol is publicly available via Github under the GNU GPLv3 License (details in the Licensing section at the bottom).
I distinctly remember finishing much of the development during my exam period—no classes and no homework, so lots of spare time!

User Info

A quick introduction to the main features, and how to try the program out.

User Interface Features

  1. Rotate and zoom into molecules
  2. Save and open molecule data files
  3. Basic numerical data display (e.g. Molar mass)
  4. Different viewing options: Ball-and-stick/Space-filling/skeletal, color filters that visualize data, etc.
  5. Display parts of molecules as color-coded "packages" (convex hulls) for conceptual organization (e.g. highlight nucleic acids in DNA or sugar units in starch)
Picture
Opening molecule data files
Picture
Glucose, as seen through a mass density color filter. Brighter means denser.

Pre-built Molecules

  1. Crystals: Diamond, Salt, etc.
  2.  Nucleic Acids: Adenine, Uracil, DNA fragments like the TATA Box, etc.
  3. Organic Chemicals: Hydrocarbons, Benzene, EDTA, etc.
  4. Proteins: Every amino acid and an alpha-chain
  5. Sugars: Glucose, Starch, Cellulose
  6. ... and many more

Setting Up

Download all the Hmol source files from Github and install the following:
  • Glade 3.6.7 with GTK+
  • Glasgow Haskell Compiler, GHC 6.10.3 (yes, I know this is quite old)
  • gtk2hs 0.10.1
As well as the following packages using cabal, which comes with GHC:
  • openglraw-1.1.0.1
  • gluraw-1.1.0.0
  • opengl-ghc2.4.0.1
Finally, run the main command of Displayer.hs within GHCi.

Viewing Options

Atoms and Bonds

Ball-&-stick, space-filling and skeletal models for atoms and bonds between them are common representations of molecules for display. The skeletal model is convenient for complicated molecules with many atoms.
Ethyldiamine      C2H4(NH2)2
Picture
Ball-&-stick
Picture
Space-filling
Picture
Skeletal

Visibility

The bonds are usually divided into two colors in the ratio of the respective atomic radii. If the continuous bond coloring option is chosen, the bond is colored in a smooth gradient. The "lone pairs" and "empty orbitals" can be either shown or hidden. Some molecules have precomputed convex hulls which can be displayed, lumping some atoms into a conceptual unit inside a "tight package".
Alanine, one  of the 20 amino acids that combine into proteins in our body
Picture
Continuous bond coloring
Picture
Showing "lone pairs"
Picture
Showing convex hull

Data in Color

The distribution of mass, charge and electronegativity can be visualized to help understand the composition of the molecule. Each type of data is displayed as a color filter: for instance, activating the mass density filter colors each atom blue, with denser atoms in brighter blue. The other two filters operate similarly in green and red respectively, so one can discern different types of data even with overlapping colors. For example, with all three filters, a white atom has very high mass density, charge density and electronegativity.
Ethylenediaminetetraacetic acid (EDTA)
Picture
No filter
Picture
Mass
Picture
Charge
Picture
Electronegativity
Picture
All filters
Picture
Charge & Electronegativity
Picture
Mass & Electronegativity
Picture
Mass & Charge
Densities are calculated by dividing by the cube of the atomic radius.

A little bit of chemistry here: four of EDTA's oxygen atoms carry a negative charge: they show as black under the charge filter because they have a "low charge". Together with the nitrogen atoms (blue when no filter), these six atoms "capture" metal ions ("M" in the diagram on the right) and prevent them from reacting with other compounds.

This property makes EDTA useful for dissolving metal oxides and reducing water hardness in laundry (metal ions make water hard), among other applications.
Picture
EDTA binding with a metal ion M

Behind the Code

Hare are some of the theory and algorithms relating to chemistry and mathematics that were necessary to build this program. Refer to the source code from Github. I'll write at length in another post about the programming techniques used (mainly type classes and monads).

1  Chemistry

The file Chemistry.hs contains the chemical data such as the elements up to period 4, atomic radii (Van Der Waals radii), molar masses, color designations, orbital types and geometries (based on VSEPR theory), hybridization and electronegativities.

hmol can handle single, double and triple bonds between atoms, as well as "intermolecular forces" (represented as "dotted bonds" in the snapshots of DNA). 

Convenient subroutines build macromolecules out of atoms and smaller molecules:
  • alkane (from Molecule.hs) constructs an alkane of any desired length.
  • polymerize (from MoleculeBuilder.hs) chains some copies of a specified molecule together by connecting specified bonds end-to-end.
  • crystallize2D and crystallize3D (from MoleculeBuilder.hs) lays out an 2D or 3D array of a chosen molecule and connects the specified bonds to form a sheet or crystal.
  • transcribeToSingleHelix and transcribeToDoubleHelix (from NucleicAcids.hs) synthesizes a DNA fragment from a given nucleic acid sequence (the "Human Telomeric Repeat" pictured at the very top comes from the sequence "TTAGGG".)
  • proteinAlphaHelix (from Proteins.hs) chains a given sequence of amino acids into an alpha helix.
A strand of cellulose: a polymer from glucose units, it provides structural strength in plant tissue.
Several strands link side-by-side to confer additional strength.
Picture
I cannot vouch for the scientific accuracy of Hmol, because it was made with my high school knowledge of chemistry! The program gives a a very simplistic representation of molecules. It doesn't draw orbitals with the exotic shapes as predicted by MO theory, nor does it portray aromaticity at all.

2  Special Algorithms

Algorithms of a more mathematical flavor were sometimes necessary.

Forcing bond angles

 The positions of the orbitals were usually based on usual geometries—four orbitals would be displayed with a tetrahedral geometry. But sometimes strange bond angles are necessary, such as for proline, an amino acid with an unusual pentagonal carbon ring. This forces the bond angle within the pentagon to be 108° instead of the approximately 109.5° bond angle of tetrahedral carbon atoms.
Picture
Proline
The warpAtom subroutine from Algorithms.hs fixes some orbitals of an atom in a specified configuration, then allows the remaining orbitals to roam around, "pushed" by electromagnetic repulsion from the fixed configurations. Mathematically, the repulsion between two orbitals is taken to be inverse proportional to the square of the bond angle between them. After 1000 rounds of electromagnetic jostling, the orbitals are assumed to have settled in their equilibrium positions. Is this a form of gradient descent? At any rate, the results look okay...

Convex hull of spheres

To compute the convex hulls of a set of atoms, normal convex hull algorithms couldn't be used because I needed the hull of spheres that could have different radii. I basically adapted the gift wrapping algorithm by considering planes that are externally tangent to three spheres at a time. The set of triangular faces is then augmented with the spheres themselves and conical frusta between adjacent spheres to render a package with a smooth surface.
Cyclohexane, "boat" configuration
Picture
Picture

3  Programming

The user interface was conveniently built using Glade, interfaced with Haskell via Gtk2Hs, and the 3D rendering was done using HOpenGL. Blood and sweat were shed to get HOpenGL to display what even you see in these pictures, and I'm sure that is far from the full potential of HOpenGL.

I used some Haskell tricks to make programming easier, but they take long to explain so I'll save them for another post.

Licensing

Hmol is licensed under the GNU General Public License Version 3. Redistribution of this work, with or without modification, is permitted if Cheng Herng Yi is attributed as the original author of this work, but not in any way that suggests that Cheng Herng Yi endorses you or your use of the work.
Picture
Copyright © 2010 Cheng Herng Yi

Enquiries about Hmol

Submit

Creative Commons License
Graphics from Hmol in this article by Cheng Herng Yi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Attribution must include the Hmol program and a link to its Github repository, or if impossible, a link to this article.
1 Comment
Taya link
12/6/2020 05:08:33 pm

Nice blog yoou have

Reply



Leave a Reply.

    Archives

    December 2020
    February 2020
    January 2019
    December 2018
    May 2018
    December 2017
    August 2017
    July 2017
    May 2016
    October 2015
    December 2014
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    February 2014
    January 2014
    September 2013
    June 2013
    May 2013
    March 2013
    December 2012

    RSS Feed

    Categories

    All
    Advocacy
    Art
    Design
    Elementary
    Exhibition
    Mathematics
    MIT
    Musing
    Origami
    Programming
    Puzzle
    Research
    Science
    Teaching
    Theater
    Writing

  • Home
  • Publications
  • CV
  • Outreach
  • Art
    • Origami >
      • Origami Design Gallery
      • Origami Publications
      • Origami Research and Applications
      • Modular Polyhedra from Waterbomb Base Units
    • Performance
  • Software
    • Featured Project
  • Blog
    • Featured Post
  • Links