Feedback

type to search

How do I automate the generation of 3D images of molecules?

Asked by [ Editor ]

Suppose I'm in the business of visualizing hundreds of molecules for a PDF file or web page. How can go about it in an automated way? The ideal, of course, is a short command-line tool that does the batch job with no interaction. Something for Python or another scripted language would also do.

All input files are in XYZ format and contain no more than 20 atoms. The output images need to be readable in both large (about 800x800) and thumbnail (about 75x75) sizes.

Imported from: http://blueobelisk.stackexchange.com/questions/38

NN comments
baoilleach
-

What do you mean by rendering? Depiction, or specifically a rendering tool? Because in the latter case, bitmaps are produced not vector images…

kml
-

Good point, Noel. I just mean visualization (depiction), whether it is raster images or vector doesn’t matter that much, as long as they look good in both sizes.

baoilleach
-

How about changing the question so to “How do I automate 3D depiction of molecules?”

baoilleach
-

Or even “How do I automate generation of 3D images of molecules”. This is my last suggestion (I promise!!) :–)

or Cancel

4 answers

3

chem-bla-ics [ Admin ]

I can suggest to have a look at Jmol. There is likely a page about your exact problem too, but only found this page about creating animations... but since the last step of that is to concatenate the images of each individual file, just skip that one to get a script for your purpose. There are a few examples, and the below comes close, I think:

for (var i=0; i<36; i=i+1)
  load "file" + i + ".xyz"
  color background [xD2DFEF]
  write image 200 200 @{"img" + ("0000" + i)[-3][0] + ".gif"}
  /* 200 and 200 are width and height */
end for

I have not tested it, but I think you just save the above as, for example, script.jmol and then use the jmol application to run the script:

jmol script.jmol

(This might actually be:

java -jar Jmol.jar script.jmol

I think, if you do not have a shell script wrapping this second option.). Have a look at the -n option to have Jmol not fire up a GUI.

I hope this helps you on your way, and will try it next week myself, and tune this answer.

NN comments
baoilleach
-

And Jmol can export as Povray too, right?

kml
-

I didn’t consider Jmol before, because I’m not a big fan of Java, but I surprised how nicely it works… I even managed to rotate and zoom easily in the script. Also, by adding the -n option I can suppress the GUI.

kml
-

I did not consider Jmol before, as I’m not a big fan of Java. I am surprised how nicely it works… even managed to rotate/zoom easily in the script. Also, by adding the -n option I can suppress the GUI.

chem-bla-ics
-

Ah, indeed… yes, the option to suppress the GUI is something really useful, and I forgot to mention that.

or Cancel
2

andrew dalke [ Editor ]

There are so many different ways to do that.

  • RasMol with the -nodisplay flag and -xyz to load the .xyz file, then some RasMol script commands to generate the raster image output. This also lets you generate stereo images.

  • VMD has similar options to run without a display and to execute Tcl and Python code

  • PyMol almost certainly has a scripting mode, but I haven't used it enough to find out

The latter two of these also supports POV-Ray output, as well as another of others, if that interests you. PyMol has better 2D depiction than VMD, which doesn't show bond types. (Although an XYZ file doesn't have bond types, that I recall.)

Pretty much pick a viewer with a scriptable interface and a batch mode (which is most of them) and there you go.

Now, getting an interesting view is harder. You'll have to figure out how you want to orient the structures, and scale things so it's consistent or appropriate across your input.

NN comments
kml
-

Yes, I had to deal with scaling and similar issues in my script. I did fine with Jmol, but would choose something else if I were writing it from scratch again.

or Cancel
1

steve wathen [ Editor ]

Bioclipse might be another option. It is script driven and uses Jchempaint and Jmol to visualize molecules in 2D and 3D. You could write a script to open the sdf file, load each molecule into either jchempaint or jmol and then save an image. This would give you the option of either a 2D (using jchempaint) or 3D (using Jmol) image.

or Cancel
1

baoilleach [ Admin ]

You could convert the XYZ files to Povray format using Open Babel, and run them all through Povray.

For a sample image generated with Open Babel 2.3 please see the docs.

NN comments
kml
-

Could someone provide an example (or link) for how to do this?

steve wathen
-

I just tried this with OpenBabel on Ubuntu. From the command line typing:

 babel ethylene.xyz ethylene.pov

creates a new file called ethylene.pov

Under linux, povray runs from the command line, so

 povray ethylene.pov

runs povray and generates a png file called ethylene.png

For some reason when I did this, the png file was completely black even though the povray code looks ok to me.

baoilleach
-

Are you missing a light source? (If you figure it out, let us know and we can patch OpenBabel)

steve wathen
-

I took another look – the pov file generated by OpenBabel does not include a camera or light source. In addition, the output when I run Povray says that there are zero objects in the scene – no wonder I don’t see anything.

kml
-

I had the same problem, so I would be grateful for updates on this!

baoilleach
-

Can I suggest you send an email to openbabel mailing list?

steve wathen
-

I have sent an email to the openbabel-discuss list about this.

OpenBabel leaves a few things out of the pov file. You will need to add a camera, a light source and probably define a background color for the molecule to be seen.

In addition, you will need to include the actual molecule to be rendered as the last line of the file. The pov file defines atoms and bonds, usually relative to the name of the input file, e.g. filename_atoms, but it does not finish with the actual object to be rendered. Add filename as the last line.

or Cancel

Your answer

You need to join Blue Obelisk eXchange to complete this action, click here to do so.