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

Coloring based on confidence #1004

Closed
emehinovic72 opened this issue Nov 8, 2021 · 10 comments
Closed

Coloring based on confidence #1004

emehinovic72 opened this issue Nov 8, 2021 · 10 comments

Comments

@emehinovic72
Copy link

Is there a way to set the color scheme to reflect on PLDDT confidence for those proteins structures outputted by Alphafold?

@appassionate
Copy link

appassionate commented Nov 9, 2021

Hi, I have similar problems on it. I tried to applied a array with (n_frames, n_atoms) shape to reflect a "condition" per frame.
I tried to set a "color_scheme" to a Widget class, however "_ColormakerRegistry" seems not work in a initialized widget object. Maybe, I want to know more about "_remote_call" to control those embedded JS...
Also, if there is any a method to let "widget" get an extra data for this kind representations?
Any ideas will be inspired me for i am just a greenhand. : )

@emehinovic72
Copy link
Author

view.add_cartoon('protein',color_scheme='bfactor') is close, but the color scale needs to be adjusted 

@hainm
Copy link
Collaborator

hainm commented Nov 11, 2021

view.add_cartoon('protein',color_scheme='bfactor') is close, but the color scale needs to be adjusted 

@emehinovic72 Maybe try customizing the color scheme yourself? #839

@hainm
Copy link
Collaborator

hainm commented Nov 11, 2021

Hi, I have similar problems on it. I tried to applied a array with (n_frames, n_atoms) shape to reflect a "condition" per frame. I tried to set a "color_scheme" to a Widget class, however "_ColormakerRegistry" seems not work in a initialized widget object. Maybe, I want to know more about "_remote_call" to control those embedded JS... Also, if there is any a method to let "widget" get an extra data for this kind representations? Any ideas will be inspired me for i am just a greenhand. : )

hi @appassionate I don't clearly understand your issue, but if you want to update the color based on frame changed, you can play with this code:

from time import sleep

def on_change(change):
    frame = change.new
    if frame % 2:
        color = 'red'
    else:
        color = 'blue'
    view.update_cartoon(color=color)
    sleep(0.1) # wait for the color update
view.observe(on_change, names=['frame'])

@emehinovic72
Copy link
Author

view.add_cartoon('protein',color_scheme='bfactor') is close, but the color scale needs to be adjusted 

@emehinovic72 Maybe try customizing the color scheme yourself? #839

Would you be able to provide an example(s) for color customization by bfactor?

@appassionate
Copy link

appassionate commented Nov 16, 2021

Hi, I have similar problems on it. I tried to applied a array with (n_frames, n_atoms) shape to reflect a "condition" per frame. I tried to set a "color_scheme" to a Widget class, however "_ColormakerRegistry" seems not work in a initialized widget object. Maybe, I want to know more about "_remote_call" to control those embedded JS... Also, if there is any a method to let "widget" get an extra data for this kind representations? Any ideas will be inspired me for i am just a greenhand. : )

hi @appassionate I don't clearly understand your issue, but if you want to update the color based on frame changed, you can play with this code:

from time import sleep

def on_change(change):
    frame = change.new
    if frame % 2:
        color = 'red'
    else:
        color = 'blue'
    view.update_cartoon(color=color)
    sleep(0.1) # wait for the color update
view.observe(on_change, names=['frame'])

Thanks! But "update_cartoon" seems not work in my work.. maybe I even have not used "add_cartoon" in my viewer.
Actually, I followed class "NGLDisplay" in ase. It represented atoms by using "add_spacefill", however "update_spacefill"
seems changed all color in spacefill representation instead of"changing a selected color of one of atoms " during my attempt... :(
In my naive try: I want to create "spacefill" for every atoms, it will cost a lot times...

n_frames = 100
atom_num = len(u.atoms)
value_array = np.random.randint(0,3,(n_frames,atom_num))
color_list = ['red', 'blue', 'yellow']

for i in range(n_frames):
    viewer.view.remove_spacefill()
    for j in range(atom_num):
        _color = color_list[value_array[i][j]]
        viewer.view.add_spacefill(selection=f'@{j}',radiusScale=2,color=f'{_color}')
    sleep(1)

@hainm
Copy link
Collaborator

hainm commented Nov 18, 2021

Thanks! But "update_cartoon" seems not work in my work.. maybe I even have not used "add_cartoon" in my viewer.

hi @appassionate That's a copy and paste code for you to adapt it. You need to write the on_change function by yourself.

In my naive try: I want to create "spacefill" for every atoms, it will cost a lot times...

where does the bottleneck come from?

@hainm
Copy link
Collaborator

hainm commented Nov 18, 2021

view.add_cartoon('protein',color_scheme='bfactor') is close, but the color scale needs to be adjusted 

@emehinovic72 Maybe try customizing the color scheme yourself? #839

Would you be able to provide an example(s) for color customization by bfactor?

I am not sure "how". But here is the JS code: https://github.com/nglviewer/ngl/blob/master/src/color/bfactor-colormaker.ts

In this PR, I showed an example of how to write your own JS code to customize your color. (I admit I am more familiar with Python): #839 (See Function section)

@appassionate
Copy link

appassionate commented Nov 19, 2021

Thanks! But "update_cartoon" seems not work in my work.. maybe I even have not used "add_cartoon" in my viewer.

hi @appassionate That's a copy and paste code for you to adapt it. You need to write the on_change function by yourself.

In my naive try: I want to create "spacefill" for every atoms, it will cost a lot times...

where does the bottleneck come from?

Many thanks for your reply.. I believe I have learned about "add_cartoon" will be a "protein representation" which is not related to my "atom representation".
I think "add_spacefill"(a kind of add_representation? ) for every atoms will lead to a big burden for "rendering engine"(I guess it exists in JS codes somewhere). One frame for atoms will be trouble let alone the rendering of each frame... It is a bad try....
So, I continued to try "update_spacefill" by color, but I could not select atoms exactly, but to have selcected all atoms in a spacefill... I guess it is a "generated spacefill object" that cant be changed anymore by "remote_call" method, or is there any keyargs for changing a generated spacefill by atom index?

I believe it will be easier to set a ColorScheme to represent a kind of property such as "bfactors". However, the property value in my task is not in trajectory files but a numpy array... It seems that there is less api between JS and python.... it will be a complexed task compared with my imaged "update_spacefill" way.
maybe there are some things i should do....

  1. find some way like nglview.adaptor.structure using to load my value, like "def get_coordinates" and find or develop an api to communicate with JS code.
  2. just like atoms.serial in JS code, we may have "atom.so_called_value" for every frame as a streaming reading.
  3. a registered colorscheme to let "render engine" know to give balls or other topology object colors.

Anyway, thanks for your help. :p

@hainm
Copy link
Collaborator

hainm commented Jan 11, 2022

Cheers.

@hainm hainm closed this as completed Jan 11, 2022
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

3 participants