Macs in Chemistry

Insanely great science

 

An Applescript droplet to generate Augmented Reality files from JMol

Augmented reality is finding new applications in science, in particular the ability to enhance publications or lecture notes, and viewers can set up a free account with Augment to provide easy access.

A number of applications can be used to create the files required for 3D display

All work fine but can require command line access to generate the required files and some file manipulation. There is a tutorial here.

I was asked recently if it might be possible to generate an AppleScript droplet that you could simply drop a chemical structure file onto to generate the desired files needed for the Augment, and this is an ideal use case for a droplet.

Droplets are specialised AppleScript applets which are designed to process items dragged onto them. Their icon contains a downward pointing arrow indicating their status as a droplet.

This script uses Jmol to generate the Wavefront .obj and .mtl files. Jmol can be downloaded from sourceforge https://sourceforge.net/projects/jmol/files/Jmol/ and downloads to yield a folder named in this format Jmol-xx.xx.xx. This is a bit of a pain because the script requires the exact path to the executable.

Jmol is a free, open source molecule viewer for students, educators, and researchers in chemistry, biochemistry, physics, and materials science. It is cross-platform, running on Windows, Mac OS X, and Linux/Unix systems. The JSmol JmolApplet is a web browser JavaScript application that can be integrated easily into web pages. Though Java-based for development and management, JSmol is completely JavaScript. The Jmol application is a stand-alone Java application (Jmol.jar) that runs on the desktop. It has all the capabilities of the JSmol applet but runs approximately 3-6 times faster. In addition, it can be run in "headless" mode (JmolData.jar) in order to carry out tasks that need to be replicated. Jmol.jar can be integrated into any other Java program, providing file loading, structure processing, viewing, and output capabilities.

The command to create the files needed for

java -jar /Applications/jmol-14.29.17/JmolData.jar -j 'write OBJ  /Users/username/Desktop/SampleFiles/carbocation.obj'  /Users/username/Desktop/SampleFiles/carbocation.xyz

This actually creates two files /carbocation.obj and /carbocation.mtl which is the material file that defines the surface

Whilst you can open and view these files in an application like Meshlab you need to compress these two files into a single Archive.zip file for uploading to the Augment site. We can use another command line application to generate the zip file.

man zip

ZIP(1L) 

NAME
       zip - package and compress (archive) files

SYNOPSIS
       zip  [-aABcdDeEfFghjklLmoqrRSTuvVwXyz!@$] [--longoption ...]  [-b path]      [-n suffixes] [-t date] [-tt date] [zipfile [file ...]]  [-xi list]

The AppleScript

Droplets are written in a specific manner using the on open handler. This droplet will process only files which have been dragged onto its icon. It will not process folders, alias files, or files within folders.

The first step gets the list of files that have been dropped onto the icon, we then get all the file information for each file, you could use this to limit the number of filetypes that can be processed. We then need to generate the paths to the source file and the output files, in this script the output files are created in the same folder as the input files. These paths will be Mac paths so we need to convert Mac file paths (colon delimited) to unix paths (slash delimited).

set unix_path to quoted form of POSIX path of new_path

We can now construct the Jmol command, remember you need to include the full path to the JmolData.jar, in this case the Jmol folder is in the Applications folder, if you have installed it elsewhere or have a different version this will need to be updated.

set myscript to "java -jar /Applications/jmol-14.29.17/JmolData.jar -j 'write OBJ  " & unix_path & "' " & input_file

The next step is to use the do shell script to run the Jmol command.

This will create the .obj and .mtl files, we next use the zip command to compress the two files into a single archive.

Jmol2AR

If you drag and drop chemical structure files onto the droplet you should end up with results like this, with a new .obj, .mtl and .zip file created for each of the structure files. Jmol can read a variety of different chemistry file formats described here with examples https://sourceforge.net/p/jmol/code/HEAD/tree/trunk/Jmol-datafiles/.

aufiles

The instructions on how to upload to Augment are available here.

You can download the droplet here http://macinchem.org/applescript/generateARmodel_files/Jmol2AR.zip.

Nick Greeves has tweeted an example of its use here and a demo page here.

Last Updated 9 August 2018