Importing Molecules into Layar

Error message

  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_unhide_all() (line 479 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Undefined index: en in drutex_node_view() (line 81 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).

This is a description of the conversion process for importing molecular structures into [http://www.layar.com/ Layar]: converting the PDB files into the format Layar uses, importing these files into [http://www.hoppala-agency.com/ Hoppala] and placing them within a layer.

== Overview ==

Most molecular structures which are freely available exist as [http://en.wikipedia.org/wiki/Protein_Data_Bank_%28file_format%29 PDB] files; these files define atoms (with types and coordinates) and bonds between the atoms. In order to visualise these, they are typically converted into a 3d mesh - for example using [http://www.pymol.org/ pymol]. Unfortunately, Layar is very picky about the 3D formats it understands - it will only accept it's own format (l3d), which is based on a Wavefront .obj file and its associated material file (.mat).

It is hard to find a good process for converting PDB files into l3d. The following is a hack, which works OK for small molecules (and small numbers of them).

== Get the PDB ==

First stop, [http://pubchem.ncbi.nlm.nih.gov/ PubChem]. Because it is the middle of winter in Scotland, I'm low on [http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=5280795&loc=ec_rcs Vitamin D], so we will work with that. It seems that PDBs aren't as freely available as I thought, but we can get hold of an XML file from the PubChem page. [http://openbabel.org OpenBabel] to the rescue, and we can create a PDB from the XML file:

babel -isdf CID_5280795.sdf -opdb VitaminD.pdb

== Create the Mesh ==

This is the trickest part; pymol would seem like the obvious tool to use, as it creates beautiful 3D pictures of molecules. Unfortunately, although it can export .obj files, it doesn't export a materials file to go with it. This means that none of the atom colours are represented, which is a great shame. Instead, we use [http://www.blender.org/ Blender], an open source 3D modelling package.

Blender cannot import PDB files directly, but there are some scripts to do it. [http://www2.inf.fh-brs.de/~mgante2s/pdb_importer.html This] seems quite polished, but lacks options about the style of molecule to display. I've been using [http://blenderartists.org/forum/showthread.php?t=81711 this], which gives a bit more control.

If you open the script in blender and run it, you'll get a box like this:
Blender Import PDB dialog

Because this is going to be displayed on low powered mobile devices, we need a low polygon count. So I've reduced the quality of both the connections and the spheres, and turned off hydrogens. This gives a molecule like this:
Vitamin D Imported into blender

(it looks a bit dull because I've deleted the lights from my scene).

You need to perform adjustments at this stage as well. The molecule should be oriented as you would like it to appear, especially in terms of the vertical axis. Also, the scaling in Hoppala is a bit problematic at the moment, and values lower that 1:1 don't seem to work. So, the model wants to be shrunk by approximately 100-fold.

Now we can use File -> Export -> Wavefront to export the .obj file. It is *essential* to export as triangles, as the Layar software doesn't deal with anything else. Apparently, exporting as material groups can speed up rendering too:

Export from Blender

You should now have a .obj file, and an associated .mat file.

== Convert to Layar format ==

Layar uses its own format for representing 3D models. Called l3d, it is essentially a binary version of a .obj file and its associate materials (.mat) file. Fortunately, they also provide a tool for converting files into this format, the [http://layar.pbworks.com/w/page/32586555/3D-Model-Converter Layar Model Converter].

If all has gone well, when you open the .obj file with this tool, you'll see:
* on the overview page, the number of faces should be reasonable (<5k), and the model dimensions should be <1
* the materials page should have materials reflecting the colours in the model
* the preview should look sensible...

Finally, you should be able to try positioning the molecule on the open streetmap view in the tool. This is worth doing, since Hoppala doesn't give you much of a preview for scale or rotation. Also, if you were using an alternative setup, you could directly use the .json file which is generated.

You can now select Save As..., and it will create a .l3d file, which is the model and its materials, and a .json file which describes the positioning carried out.

== Import into Hoppala ==

Now, these files can be imported into Hoppala. Go into your layer in Hoppala, and click on "Add an Augment". This sets up a POI, which you can position on the map:

Adding an Augment

Now, you can setup the basic properties of the POI - title, name etc. I've added a picture to represent the molecule in general browsing, which might also be displayed if the user is far away from the molecule.

Setting basic properties

The more detailed model properties are where the l3d file can be uploaded; set the model type to 3D and add the model using the boxes provided. Here is where the .json file comes in useful: the Hoppala software allows for the lat/long coordinates of the POI to be set using the Google maps view, but the scale and rotation need to be set by hand. These values can be copied from the .json file if the model was set up as desired in the conversion tool.

Setting 3D Model properties

Press save, and the model is ready for display!

Project Type: