nglviewer / nglview

Jupyter widget to interactively view molecular structures and trajectories
http://nglviewer.org/nglview/latest/
Other
819 stars 133 forks source link

How to set colors of atoms by index ? #892

Closed gVallverdu closed 4 years ago

gVallverdu commented 4 years ago

Hello

I have a molecule that I load from a PDB file and I would like to set the color of atoms from their index.

Thank you for your help

python -c 'import nglview; print(nglview.__version__)'
2.7.1
python -c 'import ipywidgets; print(ipywidgets.__version__)'
7.5.1
hainm commented 4 years ago

may be view.add_licorice(color='atomindex')?

Or do you want customized colors?

gVallverdu commented 4 years ago

I did this but it does not work

idx_C1 = np.array([  0,   1,   2,   5,   6,   7,   8,   9,  11,  13,  14,  23,  24,
    28,  37,  38,  39,  40,  45,  46,  47,  50,  51,  52,  53,  54,
    56,  58,  59,  68,  69,  73,  83,  84,  85,  86,  91,  92,  93,
    96,  97,  98,  99, 100, 102, 104, 105, 114, 115, 119, 129, 130,
    131, 132])
idx_C2 = np.array([  3,   4,  12,  15,  22,  27,  33,  34,  48,  49,  57,  60,  67,
    72,  79,  80,  94,  95, 103, 106, 113, 118, 125, 126])
idx_C3 = np.array([ 10,  29,  32,  55,  74,  77,  78, 101, 120, 123, 124, 137])

view = nv.show_file("D3-2.pdb")
view.add_representation('licorice', selection=idx_C1, color='red')
view.add_representation('licorice', selection=idx_C2, color='blue')
view.add_representation('licorice', selection=idx_C3, color='green')
view

where idx_Ci are numpy array. I tried list but it does not work.

And yes, what I would like to do is to be able to give the color according to a give quantity (charge, spin ...)

Thanks

the pdb file is the following :

TITLE diastereomer_D3-2.xyz
MODEL    1
ATOM      1 C    MOL            -6.230  -5.450   2.403
ATOM      2 C    MOL            -5.968  -4.183   1.851
ATOM      3 C    MOL            -4.680  -3.821   1.499
ATOM      4 C    MOL            -3.583  -4.701   1.673
ATOM      5 C    MOL            -3.848  -5.945   2.328
ATOM      6 C    MOL            -5.178  -6.307   2.648
ATOM      7 C    MOL            -3.661  -3.235  -1.325
ATOM      8 C    MOL            -2.204  -4.359   1.325
ATOM      9 C    MOL            -1.811  -3.262   0.473
ATOM     10 C    MOL            -2.698  -2.578  -0.475
ATOM     11 C    MOL            -0.487  -2.759   0.544
ATOM     12 C    MOL             0.491  -3.495   1.270
ATOM     13 C    MOL             0.171  -4.687   1.862
ATOM     14 C    MOL            -1.171  -5.140   1.916
ATOM     15 C    MOL            -1.478  -6.366   2.588
ATOM     16 C    MOL            -2.763  -6.786   2.732
ATOM     17 H    MOL            -2.989  -7.732   3.218
ATOM     18 H    MOL            -0.651  -6.960   2.971
ATOM     19 H    MOL            -7.246  -5.736   2.662
ATOM     20 H    MOL            -5.351  -7.270   3.123
ATOM     21 H    MOL             1.497  -3.106   1.356
ATOM     22 H    MOL             0.938  -5.268   2.368
ATOM     23 C    MOL            -5.870  -4.387  -2.731
ATOM     24 C    MOL            -5.788  -3.037  -2.585
ATOM     25 C    MOL            -4.678  -2.432  -1.915
ATOM     26 H    MOL            -6.729  -4.844  -3.216
ATOM     27 H    MOL            -6.572  -2.387  -2.967
ATOM     28 C    MOL            -4.578  -1.019  -1.860
ATOM     29 C    MOL            -3.505  -0.409  -1.270
ATOM     30 C    MOL            -2.545  -1.169  -0.545
ATOM     31 H    MOL            -5.334  -0.424  -2.367
ATOM     32 H    MOL            -3.382   0.663  -1.357
ATOM     33 C    MOL            -0.194  -1.406   0.063
ATOM     34 C    MOL            -3.645  -4.655  -1.673
ATOM     35 C    MOL            -4.783  -5.225  -2.328
ATOM     36 H    MOL            -6.779  -3.475   1.706
ATOM     37 H    MOL            -4.511  -2.829   1.105
ATOM     38 C    MOL            -4.798  -6.603  -2.649
ATOM     39 C    MOL            -2.518  -5.496  -1.498
ATOM     40 C    MOL            -2.544  -6.833  -1.850
ATOM     41 C    MOL            -3.704  -7.406  -2.403
ATOM     42 H    MOL            -5.686  -7.013  -3.124
ATOM     43 H    MOL            -1.600  -5.083  -1.103
ATOM     44 H    MOL            -1.655  -7.440  -1.704
ATOM     45 H    MOL            -3.724  -8.461  -2.663
ATOM     46 C    MOL             7.838  -2.664   2.405
ATOM     47 C    MOL             6.609  -3.072   1.855
ATOM     48 C    MOL             5.652  -2.139   1.502
ATOM     49 C    MOL             5.865  -0.748   1.672
ATOM     50 C    MOL             7.076  -0.354   2.324
ATOM     51 C    MOL             8.055  -1.324   2.646
ATOM     52 C    MOL             4.634  -1.553  -1.324
ATOM     53 C    MOL             4.879   0.273   1.323
ATOM     54 C    MOL             3.732   0.063   0.472
ATOM     55 C    MOL             3.583  -1.048  -0.475
ATOM     56 C    MOL             2.634   0.958   0.542
ATOM     57 C    MOL             2.782   2.175   1.266
ATOM     58 C    MOL             3.975   2.495   1.856
ATOM     59 C    MOL             5.039   1.560   1.911
ATOM     60 C    MOL             6.255   1.910   2.580
ATOM     61 C    MOL             7.262   1.007   2.725
ATOM     62 H    MOL             8.195   1.286   3.208
ATOM     63 H    MOL             6.356   2.923   2.960
ATOM     64 H    MOL             8.594  -3.401   2.666
ATOM     65 H    MOL             8.976  -0.991   3.120
ATOM     66 H    MOL             1.942   2.851   1.353
ATOM     67 H    MOL             4.095   3.450   2.361
ATOM     68 C    MOL             6.739  -2.891  -2.725
ATOM     69 C    MOL             5.530  -3.495  -2.581
ATOM     70 C    MOL             4.449  -2.836  -1.913
ATOM     71 H    MOL             7.566  -3.406  -3.209
ATOM     72 H    MOL             5.360  -4.499  -2.963
ATOM     73 C    MOL             3.175  -3.457  -1.859
ATOM     74 C    MOL             2.110  -2.832  -1.270
ATOM     75 C    MOL             2.287  -1.620  -0.546
ATOM     76 H    MOL             3.039  -4.410  -2.364
ATOM     77 H    MOL             1.121  -3.263  -1.358
ATOM     78 C    MOL             1.316   0.534   0.062
ATOM     79 C    MOL             1.128  -0.863  -0.066
ATOM     80 C    MOL             5.856  -0.829  -1.672
ATOM     81 C    MOL             6.920  -1.529  -2.324
ATOM     82 H    MOL             6.402  -4.129   1.713
ATOM     83 H    MOL             4.707  -2.490   1.111
ATOM     84 C    MOL             8.121  -0.853  -2.644
ATOM     85 C    MOL             6.018   0.569  -1.501
ATOM     86 C    MOL             7.188   1.215  -1.852
ATOM     87 C    MOL             8.267   0.497  -2.402
ATOM     88 H    MOL             8.921  -1.417  -3.117
ATOM     89 H    MOL             5.200   1.157  -1.111
ATOM     90 H    MOL             7.268   2.290  -1.710
ATOM     91 H    MOL             9.190   1.007  -2.661
ATOM     92 C    MOL            -4.570   6.898  -2.406
ATOM     93 C    MOL            -4.651   5.608  -1.851
ATOM     94 C    MOL            -3.504   4.920  -1.498
ATOM     95 C    MOL            -2.214   5.479  -1.674
ATOM     96 C    MOL            -2.142   6.748  -2.331
ATOM     97 C    MOL            -3.329   7.447  -2.652
ATOM     98 C    MOL            -2.672   4.088   1.327
ATOM     99 C    MOL            -0.974   4.786  -1.325
ATOM    100 C    MOL            -0.884   3.625  -0.474
ATOM    101 C    MOL            -1.919   3.199   0.476
ATOM    102 C    MOL             0.260   2.789  -0.546
ATOM    103 C    MOL             1.397   3.241  -1.272
ATOM    104 C    MOL             1.403   4.475  -1.863
ATOM    105 C    MOL             0.228   5.267  -1.917
ATOM    106 C    MOL             0.256   6.530  -2.589
ATOM    107 C    MOL            -0.873   7.273  -2.735
ATOM    108 H    MOL            -0.843   8.245  -3.221
ATOM    109 H    MOL             1.210   6.886  -2.972
ATOM    110 H    MOL            -5.475   7.441  -2.666
ATOM    111 H    MOL            -3.243   8.421  -3.129
ATOM    112 H    MOL             2.264   2.600  -1.359
ATOM    113 H    MOL             2.296   4.834  -2.370
ATOM    114 C    MOL            -4.496   5.784   2.735
ATOM    115 C    MOL            -4.773   4.461   2.592
ATOM    116 C    MOL            -3.863   3.583   1.920
ATOM    117 H    MOL            -5.202   6.453   3.221
ATOM    118 H    MOL            -5.700   4.041   2.976
ATOM    119 C    MOL            -4.140   2.193   1.868
ATOM    120 C    MOL            -3.268   1.321   1.276
ATOM    121 C    MOL            -2.144   1.801   0.548
ATOM    122 H    MOL            -5.025   1.819   2.378
ATOM    123 H    MOL            -3.432   0.255   1.365
ATOM    124 C    MOL             0.184   1.407  -0.065
ATOM    125 C    MOL            -1.118   0.871   0.064
ATOM    126 C    MOL            -2.281   5.454   1.673
ATOM    127 C    MOL            -3.227   6.305   2.329
ATOM    128 H    MOL            -5.620   5.139  -1.705
ATOM    129 H    MOL            -3.603   3.919  -1.103
ATOM    130 C    MOL            -2.876   7.638   2.649
ATOM    131 C    MOL            -0.972   5.967   1.497
ATOM    132 C    MOL            -0.642   7.263   1.848
ATOM    133 C    MOL            -1.609   8.123   2.401
ATOM    134 H    MOL            -3.624   8.269   3.124
ATOM    135 H    MOL            -0.196   5.325   1.103
ATOM    136 H    MOL             0.376   7.613   1.701
ATOM    137 H    MOL            -1.349   9.146   2.660
ATOM    138 C    MOL            -1.309  -0.545  -0.065
TER
ENDMDL
hainm commented 4 years ago

I am not sure about your system. It works fine like below:

Screen Shot 2020-04-01 at 5 02 52 PM
hainm commented 4 years ago

@gVallverdu I can reproduce your issue with your code + file.

gVallverdu commented 4 years ago

I also reproduce your code and it works. I created the pdb file by hands from an xyz file. Maybe I miss something ?

hainm commented 4 years ago

ah, you did not clear the default representation (for some reasons), this default color overlaps with your new ones.

Screen Shot 2020-04-01 at 5 11 51 PM

Try this:

import nglview as nv
import numpy as np

idx_C1 = np.array([  0,   1,   2,   5,   6,   7,   8,   9,  11,  13,  14,  23,  24,
    28,  37,  38,  39,  40,  45,  46,  47,  50,  51,  52,  53,  54,
    56,  58,  59,  68,  69,  73,  83,  84,  85,  86,  91,  92,  93,
    96,  97,  98,  99, 100, 102, 104, 105, 114, 115, 119, 129, 130,
    131, 132])
idx_C2 = np.array([  3,   4,  12,  15,  22,  27,  33,  34,  48,  49,  57,  60,  67,
    72,  79,  80,  94,  95, 103, 106, 113, 118, 125, 126])
idx_C3 = np.array([ 10,  29,  32,  55,  74,  77,  78, 101, 120, 123, 124, 137])

view = nv.show_file("D3-2.pdb", default=False)
view.center()
view.add_representation('line', selection=idx_C1, color='red')
view.add_representation('line', selection=idx_C2, color='blue')
view.add_representation('line', selection=idx_C3, color='green')
view
gVallverdu commented 4 years ago

Ok, it works, but it is quite strange. Sometimes I have to execute the cell several times to get the widget to render the view.

image

Here is the code I used:

import nglview as nv
import numpy as np

idx_C1 = np.array([  0,   1,   2,   5,   6,   7,   8,   9,  11,  13,  14,  23,  24,
    28,  37,  38,  39,  40,  45,  46,  47,  50,  51,  52,  53,  54,
    56,  58,  59,  68,  69,  73,  83,  84,  85,  86,  91,  92,  93,
    96,  97,  98,  99, 100, 102, 104, 105, 114, 115, 119, 129, 130,
    131, 132])
idx_C2 = np.array([  3,   4,  12,  15,  22,  27,  33,  34,  48,  49,  57,  60,  67,
    72,  79,  80,  94,  95, 103, 106, 113, 118, 125, 126])
idx_C3 = np.array([ 10,  29,  32,  55,  74,  77,  78, 101, 120, 123, 124, 137])

view = nv.show_file("Angewandte/diastereomer_D3-2.pdb", default=False)
view.center()
view.add_representation("ball+stick")
view.add_representation('ball+stick', selection=idx_C1, color='red')
view.add_representation('ball+stick', selection=idx_C2, color='blue')
view.add_representation('ball+stick', selection=idx_C3, color='green')

view

Going back to the beginning of the question, what I would like to do now is to give a color to each atom according to an atomic quantity (charge, spin ...). I can make a loop on this add_representation function and set the color but this is not really clean. Does it exist a kind of mapping function ?

If not, I could try something and maybe try to contribute ...

hainm commented 4 years ago

Ok, it works, but it is quite strange. Sometimes I have to execute the cell several times to get the widget to render the view.

yeah, it might be related to the bug fixed in master branch: https://github.com/arose/nglview/pull/882

hainm commented 4 years ago

Going back to the beginning of the question, what I would like to do now is to give a color to each atom according to an atomic quantity (charge, spin ...). I can make a loop on this add_representation function and set the color but this is not really clean. Does it exist a kind of mapping function ?

Try to use ColormakerRegistry to see if that helps: https://github.com/arose/nglview/pull/839

gVallverdu commented 4 years ago

Where can I find the **kwargs of a function such as add_ball_and_stick for example ? Or the available options ?

hainm commented 4 years ago

it's NGL's parameters.

http://nglviewer.org/ngl/api/manual/molecular-representations.html#ball-stick (nglview convert camel to snake).