# [Chimera-users] User-defined graphics in chimera: NMR tensor plotting

Tom Goddard goddard at sonic.net
Mon Aug 31 16:17:43 PDT 2020

```Hi Albert,

Not sure if you are talking about using a modified version that spherical Python code in Chimera or using it in ChimeraX.  I'll assume ChimeraX.

http://plato.cgl.ucsf.edu/trac/chimera/wiki/SphericalHarmonics <http://plato.cgl.ucsf.edu/trac/chimera/wiki/SphericalHarmonics>

It is much easier to shift and rotate the vertices than your approach.  Python numpy arrays make this very convenient.  After the line of code that makes the numpy array of vertices

va = array(vertices, float32)

you could shift it to a new origin with just

va += (32.5, 13, -24.1)

Rotation is also easy.

from chimerax.geometry import rotation
r = rotation(axis = (0,1,0), angle = 45)
va = r * va
na = r * na

Here I rotate the vertices and the normal vectors about 0,0,0.  Then I could apply the translation after that.  You might want to do a rotation given by a 3 by 3 rotation matrix and the translation in one step, like this

from chimerax.geometry import Place
t = Place(matrix = ((0,1,0, 32.5),
(-1,0,0, 13),
(0,0,1, -24.1)))
t.transform_points(va, in_place = True)
t.transform_vectors(na, in_place = True)

Here I used a 3 by 4 matrix where the first 3 columns were the rotation and the 4th column is the translation.  I'll update the example code to show this.  These geometric transforms are described in the ChimeraX Python programming documentation:

https://www.cgl.ucsf.edu/chimerax/docs/devel/bundles/geometry/src/geometry.html

As for what went wrong with you np.array([x0,y0,z0]).T approach -- that transpose operation put the vertices in a non-standard memory order -- you created a 3 by N array and then did a transpose to get an N by 3 array but numpy is smart and does not allocate a new array and instead just adjusts the strides and uses the original array.  My guess is the calculate_surface_normals() code has a bug in that it does not handle non-standard orders for the vertex array.  So the normals were completely wrong and that effects the lighting.

In the ChimeraX Python shell (menu Tools / General / Shell) the variable "session" is the ChimeraX session.

Tom

> On Aug 31, 2020, at 2:43 PM, Albert Smith-Penzel <Albert.Smith-Penzel at medizin.uni-leipzig.de> wrote:
>
> Dear all,
>
> Thanks for such quick replies and helpful answers. I had two follow-up questions on the approach that Tom suggested in ChimeraX.
>
> The first is maybe a glitch, or more likely something I don’t fully understand in python/chimera.
>
> In the code Tom links to (http://plato.cgl.ucsf.edu/trac/chimera/wiki/SphericalHarmonics <http://plato.cgl.ucsf.edu/trac/chimera/wiki/SphericalHarmonics>), a list of vertices (x,y,z tuples) is created. Suppose I want to shift the center of the spherical harmonic and/or rotate it. One way to do this is to extract x,y,z from the vertices list and modify them, then put them back in the list. My first step is to switch the tuple to a list in the code (switch xyz = (r*sp*ct, r*sp*st, r*cp) to xyz = [r*sp*ct, r*sp*st, r*cp] ). Then I extract the elements, do some editing, and then put them back into a list of vertices.
>
> x0,y0,z0=np.array(vertices).T
> #Some editing of x0,y0,z0 - although editing is not required to produce the strange effects.
> vertices=np.array([[x,y,z] for x,y,z in zip(x0,y0,z0)])
> #vertices=np.array([x0,y0,z0]).T
>
> Finally, I need to reconstruct the vertices list. The first approach (currently uncommented) works just fine. The second approach (commented) doesn’t create any errors, but the lighting is very strange, in that there are alternating dark and light areas on the surface of the harmonic. I would have claimed that the two approaches should produce practically identical numpy arrays, but clearly that’s not the case.
>
> Anyway, it’s not particularly important for me, since I can work around it, but it does take some time to figure out, because one would think the latter approach should also work.
>
> The second question is: is it possible to get the “session” object into an interactive python session? Usually, I work interactively with python, and have a number of functions that create various scripts and input files for chimera, and then launch chimera from a shell, calling some script on startup. While this works, it would be really nice if one could manipulate an existing chimera session (maybe a bit off of the original topic now).
>
> In any case, the new graphics with the tensors come out very nicely- thanks so much for your help.
>
> Cheers,
> Albert
>
>
>
>> On 28. Aug 2020, at 20:58, Tom Goddard <goddard at sonic.net <mailto:goddard at sonic.net>> wrote:
>>
>> WARNUNG: Diese E-Mail kam von außerhalb der Organisation. Klicken Sie nicht auf Links oder öffnen Sie keine Anhänge, es sei denn, Sie kennen den Absender und wissen, dass der Inhalt sicher ist.
>>
>> Hi Albert,
>>
>>   You need finer phi and theta steps to get a smoother appearance.  I found that 10 degree steps gives chunky appearance, while 3 degree steps givs smooth appearance.  Chimera does not have code to make an arbitrary surface smoother.
>>
>>   Maybe you are interested in making these surfaces all in Python instead of using BILD.  Here is how it is done in our newer ChimeraX program.
>>
>> http://plato.cgl.ucsf.edu/trac/chimera/wiki/SphericalHarmonics <http://plato.cgl.ucsf.edu/trac/chimera/wiki/SphericalHarmonics>
>>
>>   Tom
>>
>> <harmonics.png>
>>
>>> On Aug 28, 2020, at 1:54 AM, Albert Smith-Penzel <Albert.Smith-Penzel at medizin.uni-leipzig.de <mailto:Albert.Smith-Penzel at medizin.uni-leipzig.de>> wrote:
>>>
>>> Hello,
>>>
>>> I have a question about improving user defined graphics in Chimera.
>>>
>>> For background (maybe not necessary for answering):
>>> I’m working with nuclear magnetic resonance and MD simulation, and so I want to plot dynamically averaged spin-spin (H–C) dipole tensors, obtained from MD, on top of the corresponding H–C bonds. I’ve been relatively successful, but while one has a very high-resolution picture of the molecule itself, the tensors themselves are visibly triangulated.
>>>
>>> How I get this plot:
>>> Each tensor is expressed first in spherical coordinate (theta, phi, r). In python, I run Delauney from scipy.spatial on theta and phi to get a list of vertices that can be used for triangulation of the tensor. Then, for each tensor, I convert theta, phi, and r into cartesian coordinates, shift the center so that the tensor sits on top of the correct bond. Finally, I create a .bild file where I write out each triangle using the coordinates and vertices. Tensors have negative and positive values (red and blue in the plot), and so I only take triangles for which r for all vertices is positive, and then in a second step, take only triangles where all vertices are negative. This all gets written to the .bild file, and then opened in chimera on top of the molecule (see picture).
>>>
>>> The question:
>>> The result is certainly acceptable, however, there is clearly a quality difference in the molecule rendering vs. the tensor rendering (obviously, because the tensors are just made of triangles that I’ve created in python). Is there a better way to bring the surface data describing the tensors into chimera that would then allow usage of some sort of smoothing within chimera for a nicer image? (And possibly with a relatively simple data format….)
>>>
>>>
>>> Cheers,
>>> Albert Smith
>>>
>>> <example_triangulation.png>
>>> _______________________________________________
>>> Chimera-users mailing list: Chimera-users at cgl.ucsf.edu <mailto:Chimera-users at cgl.ucsf.edu>
>>> Manage subscription: https://plato.cgl.ucsf.edu/mailman/listinfo/chimera-users <https://plato.cgl.ucsf.edu/mailman/listinfo/chimera-users>
>>
>
> _______________________________________________
> Chimera-users mailing list: Chimera-users at cgl.ucsf.edu
> Manage subscription: https://plato.cgl.ucsf.edu/mailman/listinfo/chimera-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://plato.cgl.ucsf.edu/pipermail/chimera-users/attachments/20200831/4e7fb1bb/attachment-0001.html>
```