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 color each residue differently #491

Closed
msultan opened this issue Sep 30, 2016 · 19 comments
Closed

how to color each residue differently #491

msultan opened this issue Sep 30, 2016 · 19 comments

Comments

@msultan
Copy link

msultan commented Sep 30, 2016

Hey guys,
Thanks a lot for developing this package. Its really awesome! I am trying to setup a view where I color each residue in my protein differently(similar to the default layout but with custom colors). I was trying to play with the view.add_repr method but i cant seem to get it to work.

import seaborn as sns

view = nglview.show_mdtraj(t1.superpose(t1[0]))
view.clear()
view.add_cartoon('protein', color=sns.cubehelix_palette(t1.n_residues))

Am I missing something very simple here?

@hainm
Copy link
Collaborator

hainm commented Sep 30, 2016

Hi

It is not implemented yet in nglview although NGL does has that. I will ping you when it's available. Cheers.

@msultan
Copy link
Author

msultan commented Sep 30, 2016

Thanks for the quick reply @hainm . And please do keep me in the loop.

@msultan msultan closed this as completed Sep 30, 2016
@hainm
Copy link
Collaborator

hainm commented Oct 1, 2016

Can you reopen this to remind us.

Hai

On Sep 30, 2016, at 7:53 PM, msultan [email protected] wrote:

Closed #491.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@msultan msultan reopened this Oct 1, 2016
@hainm hainm mentioned this issue Oct 1, 2016
@hainm
Copy link
Collaborator

hainm commented Oct 1, 2016

@msultan I have a quick demo here: #492 for your need.
Still need more time to think about API. It's good to try but you need to create a list of hex colors by yourself.

import nglview as nv

view = nv.demo()
view

# use hex values for now.
colors = ['0x0000FF', '0xFF0000'] * 1000 # for 2000 residues.
view._set_color_by_residue(colors, component_index=0, repr_index=0)

@hainm
Copy link
Collaborator

hainm commented Oct 1, 2016

from your example (updated)

import seaborn as sns

view = nglview.show_mdtraj(t1.superpose(t1[0]))
view.clear()
view

view.add_cartoon('protein')
colors = [c.replace('#', '0x') for c in sns.cubehelix_palette(t1.n_residues).as_hex()]
view._set_color_by_residue(colors)

@msultan
Copy link
Author

msultan commented Oct 1, 2016

@hainm this is awesome! Thanks a lot for putting this together so quickly. Its working perfectly.

@msultan
Copy link
Author

msultan commented Oct 1, 2016

Closing this now.

@msultan msultan closed this as completed Oct 1, 2016
@tonka-balonka
Copy link

Hi, I've successfully used the view._set_color_by_residue(colors) function before, but since I've updated nglview to 2.6.1, I get the following error: 'NGLWidget' object has no attribute '_set_color_by_residue'.
I've checked the source code and the function is missing from widget.py and I couldn't work out if there's an alternative that will allow me to custom color my residues. Any help is greatly appreciated as I really need this function 😄

@hainm
Copy link
Collaborator

hainm commented Jul 31, 2019

hi, sorry about that. Can you please adapt this function below?

    def _set_color_by_residue(self, colors, component_index=0, repr_index=0):
        self._remote_call('setColorByResidue',
                           target='Widget',
                           args=[colors, component_index, repr_index])
    if not hasattr(view, '_set_color_by_residue'):
        view._set_color_by_residue = _set_color_by_residue

@hainm
Copy link
Collaborator

hainm commented Jul 31, 2019

Otherwise, use can try to use the private (unfortunately) _ColorScheme class.
#755

@tonka-balonka
Copy link

Thanks a lot for the quick reply!
I've managed to sort it out with _ColorScheme.

@dwhswenson
Copy link

@hainm Any chance custom residue coloring might move into the API? It is really useful, and although I know I can hack something that works when I need to, I'm not keen on recommending something that my downstream users do something that isn't necessarily stable upstream.

I came across this issue, coincidentally having had the same problem as @tonka-balonka at about the same time -- I was trying custom coloring for the first time in a year. I wanted to color a protein by the fraction of time a drug molecule spends in contact with each residue -- you see how this could be neat?

@hainm
Copy link
Collaborator

hainm commented Aug 1, 2019

Hi, ok, this will be my top priority. Cheers.

@dwhswenson
Copy link

dwhswenson commented Aug 1, 2019

Thanks @hainm! I'll open a PR soon so you can see the specific use case

@hainm
Copy link
Collaborator

hainm commented Aug 2, 2019

hi all, please try v2.6.7 for public API for customize color scheme:

#839

@tonka-balonka
Copy link

Hi, I def agree with @dwhswenson that this is a really useful option to have and thank you for implementing it so quickly.
I rewrote my code and it works like a charm!

@shayanhoss
Copy link

Hi @hainm I am trying to create a custom color mapping by position of amino acid residue but not using a simple 3 color RGB color map. For each position I have a hex color code (color representation of values from -2 to +2). I have tried using 'ColormakerRegistry.add_selection_scheme' to generate a list with [Position, hexcode] but this mapping doesn't seem to work. Is there a way to do custom color mapping using hexcodes to reflect gradients ?

@hainm
Copy link
Collaborator

hainm commented Mar 20, 2024

@shayanhoss Sorry Shayan, I don't know the answer. I guess you have to google search or ask chatGPT then. good luck.

@shayanhoss
Copy link

Is there any color mapping function that takes continuous inputs rather than discrete?

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

5 participants