15 May 2013

415. Briefly: making a polyhedral molecular figure in gdis

This post is mainly directed towards a particular PhD student, hence the specificity in terms of workflow.

The example I use, http://www.crystallography.net/information_card.php?cif=4308402, is random, however.

0. Install stuff
sudo apt-get install gdis openbabel wget

Also turns out that there's no povray in Debian anymore! Instead, compile it as shown here: http://verahill.blogspot.com.au/2013/05/413-povray-37-rc7-on-debian-wheezy.html

1. Get the CIF
wget http://www.crystallography.net/cif/4/30/84/4308402.cif

2. Open the cif
gdis 4308402.cif

3. Optional: Trim the content and save as xyz
Select parts to delete (e.g. counter-ions and solvent) by left-clicking and dragging, then delete by hitting the Del key. Hold right-click and drag the mouse to rotate.

It doesn't need to be perfect at this stage.

Save as xyz by going to Save.. and selecting XYZ as the format.

You can also edit the xyz by hand at this point to remove e.g. all sodium ions etc.

4. Open the XYZ file you just saved

5. Delete the remaining undesirable atoms.

6. Optional: If there are bonds missing
An atom needs to bond to six other atoms for gdis to render it as an octahedral polygon, so make sure that all the bonds are there.

Open Tools/Building/Editing. Click on Add Single Bonds.
This bit is a bit frustrating -- mark one atom, then mark another. You mark by double left-clicking (don't hold shift), which sounds easy enough, but actually managing to select an atom can be frustratingly difficult sometimes and zooming doesn't help for some reason.

Note: all bonds will be gone as soon as you close gdis...

7. Turn it into polyhedral representation
Go to View/Display Properties.Click on Polyhedral
8. Generate POV file
Click on the POVRay tab. Make sure to UNCHECK the "Delete intermediate files..." button. To save time, check 'Create files, then stop'.

Click on Render. Looks like nothing happened, but a dummy_0.pov file was written to the working directory.

If the rendered image doesn't 'fit', you might have to zoom out in gdis before hitting render.

9. Render the POV file.
povray +W1000 +H1000 +A0.01 dummy_0.pov

to generate a 1000x1000 png image with anti-aliasing (the lower the number following A, the 'nicer' the figure)
Final image


* Changing Colour
1. The permanent way: edit /usr/share/gdis/gdis.elements
gksu gedit /usr/share/gdis/gdis.elements

Find the element you want to change, e.g. Selenium:
341 %gdis_elem 342 symbol: Se 343 name: Selenium 344 number: 34 345 weight: 78.959999 346 cova: 1.220000 347 vdw: 2.000000 348 charge: 4.000000 349 colour: 65535 52860 59880 350 %gdis_end

Change the colour block -- it's a simple RGB (Red:Green:Blue) 16 bit formula which ranges from 0 to 65535. 655365 655365 65535 is white, 0 0 0 is black, and 0 20000 0 is a dark green.

Using octave you can automatically convert HTML RGB codes:
octave:1> rgb = @ (a) 257.*[hex2dec(a(1:2)), hex2dec(a(3:4)) ,hex2dec(a(5:6))] rgb = @(a) 257 .* [hex2dec(a (1:2)), hex2dec(a (3:4)), hex2dec(a (5:6))] octave:2> rgb('FFB00F') ans = 65535 45232 3855 octave:3>
Make your changes and save. Now open the XYZ file you want to work with and the colours should be 'right'.

2. The temporary way
If you've already generate a POV and/or you don't want to make all those single bonds again, you can edit the POV directly. It does take a bit of script-fu, but isn't unreasonably difficult:

A. First figure out what colours are actually used:
cat dummy_0.pov |grep -v '#'|grep RGB|uniq|sort
texture_list { RGB_2899 RGB_2899 RGB_2899 }} texture_list { RGB_32040 RGB_32040 RGB_32040 }} texture_list { RGB_32040 RGB_32040 RGB_32040 }} texture_list { RGB_32382 RGB_32382 RGB_32382 }} texture_list { RGB_32382 RGB_32382 RGB_32382 }} texture_list { RGB_3637 RGB_3637 RGB_3637 }} texture_list { RGB_3637 RGB_3637 RGB_3637 }}

We have four colour formulae: 2899, 32040, 32382 and 3637 (the spaces are important below).

B. Take a look at the colours:
cat dummy_0.pov |grep '#'|egrep 'RGB_2899 | RGB_32040 |RGB_32382 |RGB_3637 '
#declare RGB_2899 = texture{pigment{color rgb <0.064516,0.838710,0.612903> } finish { Phong_Shiny } } #declare RGB_3637 = texture{pigment{color rgb <0.096774,0.548387,0.677419> } finish { Phong_Shiny } } #declare RGB_32040 = texture{pigment{color rgb <1.000000,0.290323,0.258065> } finish { Phong_Shiny } } #declare RGB_32382 = texture{pigment{color rgb <1.000000,0.612903,0.967742> } finish { Phong_Shiny } }
Looking at the colours and comparing with gdis/elements I'd say that the elements are in this order: Cerium, Tungsten, Oxygen, Arsenic

C. Rename all instances of RGB_2899 to Cerium etc. Note that this can't be undone if you make a mistake.
sed -i 's/RGB_2899/Cerium/g' dummy_0.pov
sed -i 's/RGB_3637/Tungsten/g' dummy_0.pov
sed -i 's/RGB_32040/Oxygen/g' dummy_0.pov
sed -i 's/RGB_32382/Arsenic/g' dummy_0.pov

D. Change the colours by opening dummy_0.pov with e.g. vim, and editing the declare lines, e.g.
#declare Tungsten = texture{pigment{color rgb <1.0,1.0,0.0> } finish { Phong_Shiny } }

You can use the rgb script above to calculate the colour values from hex codes:
octave:3> [rgb('FFB00F')]./65535 ans = 1.000000 0.690196 0.058824

E. Then render:
I accidentally screwed up the sed step and couldn't be bothered to make all the bonds again so the polyhedra look awful.
* Transparent polyhedra
Note that this increases rendering times by orders of magnitude.

Anyway, it's simple to set up: just change from rgb to rgbf, e.g.
#declare Tungsten = texture{pigment{color rgb <1.0,1.0,0.0> } finish { Phong_Shiny } }
#declare Tungsten = texture{pigment{color rgbf <1.0,1.0,0.0,0.5> } finish { Phong_Shiny } }
With rgbf you have four values <a,b,c,d>, where the higher the value of d, the more transparent the object. d=0 means that it's completely opaque.

No comments:

Post a Comment