Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change atom color ? #913

Closed
gVallverdu opened this issue Jul 2, 2020 · 15 comments
Closed

How to change atom color ? #913

gVallverdu opened this issue Jul 2, 2020 · 15 comments

Comments

@gVallverdu
Copy link

Hi

I would like to change the default color of one atom (element). I think there might be a kind of parameters of the view where this is defined but I do not found where.

I can add a sphere or something like that as a workaround but this does not change the color of the bond in a ball+stick representation for example.

Thanks

python -c 'import nglview; print(nglview.__version__)'
2.7.7
python -c 'import ipywidgets; print(ipywidgets.__version__)'
7.5.1
@hainm
Copy link
Collaborator

hainm commented Jul 3, 2020

@gVallverdu may be try to add your own color scheme?

#839

The selection language is here: http://nglviewer.org/ngl/api/manual/usage/selection-language.html

@gVallverdu
Copy link
Author

gVallverdu commented Jul 5, 2020

Hi
Looking at #839 Yes I can do something. However, I have some difficulties reading the doc here: http://nglviewer.org/ngl/api/globals.html

I am looking for the class from which the atom variable is an instance. Can you help me about this point ?

The following works but If I want to change the color of one atom I must redefine all element... That's why I would like to read the doc to find a way to get the default color maybe ?

cm = ColormakerRegistry
cm.add_scheme_func('my_sel','''
 this.atomColor = function (atom) {
     if (atom.element == "C") {
       return 0x34495e // C
     } else if (atom.element == "H") {
       return 0xecf0f1
     } else if (atom.element == "S") {
       return 0xf1c40f
     } else if (atom.element == "N") {
       return 0x2980b9
     }
 }
''')

image

Thanks

@hainm
Copy link
Collaborator

hainm commented Jul 5, 2020

@gVallverdu Oh, I think we are making thing complicated. See my example below, just use the selection language.
Screen Shot 2020-07-05 at 2 31 47 PM

@vhorvath
Copy link
Contributor

vhorvath commented Jul 5, 2020

@hainm, I keep getting mentions, but I am not in this thread. Did you mean to add @gVallverdu instead? :)

@hainm
Copy link
Collaborator

hainm commented Jul 5, 2020

hi @vhorvath I am terribly sorry. Yes, I meant @gVallverdu

@gVallverdu
Copy link
Author

@hainm yes this is simpler but the bond are not colored correctly

But for one specific atom or a molecule/fragment not bonded to the whole system it is ok.

image

@hainm
Copy link
Collaborator

hainm commented Jul 5, 2020

@arose any idea? thanks.

@dominiquesydow
Copy link
Contributor

Hi @hainm,

I found this issue while trying to figure out how to recolor all C atoms in an atom selection.
Do you have any new ideas how the coloring could be extended also to the bonds with non-C atoms?

Minimal code example:

view = nglview.show_pdbid("3w32")
view.clear_representations()
view.add_representation("hyperball", selection="ligand")
view.add_representation("hyperball", selection="ligand and _C", color="magenta")
view.center()
view

image

Grey bonds should optimally also be magenta.


If this is not easily possible, I might go for the alternative and define element colors using the ColormakerRegistry as proposed above:

cm = ColormakerRegistry
cm.add_scheme_func('my_sel','''
 this.atomColor = function (atom) {
     if (atom.element == "C") {
       return 0x34495e // C
     } else if (atom.element == "H") {
       return 0xecf0f1
     } else if (atom.element == "S") {
       return 0xf1c40f
     } else if (atom.element == "N") {
       return 0x2980b9
     }
 }
''')

Could you maybe link me to the code where the nglview-default element colors are defined (for elements other than C, H, S, N, P)? My keyword searches in https://github1s.com/nglviewer/nglview/ where unsuccessful unfortunately, sorry.

@hainm
Copy link
Collaborator

hainm commented May 10, 2021

Could you maybe link me to the code where the nglview-default element colors are defined (for elements other than C, H, S, N, P)? My keyword searches in https://github1s.com/nglviewer/nglview/ where unsuccessful unfortunately, sorry.

Hi @dominiquesydow: I am sorry that I don't know the answer.
Please search the code in NGL repo: https://github.com/nglviewer/ngl

And/or ask the usage of color in that repo since there are many experts there. Good luck.

@hainm
Copy link
Collaborator

hainm commented May 10, 2021

For the color, you can search to JS code here: https://github.com/nglviewer/ngl/tree/master/src/color

@dominiquesydow
Copy link
Contributor

@hainm, thanks a lot for the pointers!

@dominiquesydow
Copy link
Contributor

In case anyone ends up in this issue searching for

Could you maybe link me to the code where the nglview-default element colors are defined (for elements other than C, H, S, N, P)?

Here is a the list of element-color mappings:
https://github.com/nglviewer/ngl/blob/1ec4a8c3a6a7a33c0c92f6d4eac01efde8ec4240/src/color/element-colormaker.ts#L13

@gVallverdu
Copy link
Author

Following my question at the beginning, here is how I finally did. It is a simple loop over atoms. It is thus not so adapted in case of huge number of atoms. But, I haven't found any way to do it using another way.
https://github.com/gVallverdu/pychemcurv/blob/974aed913bdf2cfb9dd2598e536cf5332bba2dcd/pychemcurv/vis.py#L207

@hainm
Copy link
Collaborator

hainm commented May 11, 2021

Thank you all. I go ahead closing this issue. Do feel free to open others if needed.

@hainm hainm closed this as completed May 11, 2021
@hainm
Copy link
Collaborator

hainm commented May 11, 2021

By the way, I've added

dynophores: Dynamic pharmacophore modeling of molecular interactions
pychemcurv: Discrete and local curvature applied to chemistry and chemical reactivity

to here: https://github.com/nglviewer/nglview/blob/master/README.md#projects-integrating-nglview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants