Py.Cafe

jhsmit/

ipymolstar-click-interaction

ipymolstar Click Interaction

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
This example shows to how use click interactions
"""

from string import Template

import solara
from ipymolstar import PDBeMolstar

url_template = Template(
    "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/CID/$cid/record/SDF?record_type=3d&response_type=save&response_basename=Conformer3D_COMPOUND_CID_$cid"
)


AA_LUT = {
    "ARG": 6322,
    "HIS": 6274,
    "LYS": 5962,
    "ASP": 5960,
    "GLU": 33032,
    "SER": 5951,
    "THR": 6288,
    "ASN": 6267,
    "GLN": 5961,
    "CYS": 5862,
    "SEC": 6326983,
    "GLY": 750,
    "PRO": 145742,
    "ALA": 5950,
    "VAL": 6287,
    "ILE": 6306,
    "LEU": 6106,
    "MET": 6137,
    "PHE": 6140,
    "TYR": 6057,
    "TRP": 6305,
}

custom_data_initial = dict(
    url=url_template.substitute(cid=AA_LUT["TRP"]),
    format="sdf",
    binary=False,
)


molecule_id = "1QYN"

# %%
s = """empty
  -ISIS-  

  0  0  0  0  0  0  0  0  0  0999 V2000
M  END
$$$$
"""

b = s.encode()
empty_data = {
    "data": b,
    "format": "sdf",
    "binary": True,
}

# %%


@solara.component
def Page():
    solara.Title("ipymolstar - Click Events")
    click_event = solara.use_reactive(None)
    custom_data = solara.use_reactive(empty_data)
    molecule_id = solara.use_reactive("1QYN")
    amino_acid = solara.use_reactive("")

    def on_click(event):
        click_event.set(event)
        aa_tla = event["comp_id"]

        if aa_tla in AA_LUT:
            amino_acid.set(aa_tla)
            custom_data.set(
                dict(
                    url=url_template.substitute(cid=AA_LUT[aa_tla]),
                    format="sdf",
                    binary=False,
                )
            )
        else:
            amino_acid.set("")
            custom_data.set(empty_data)

    with solara.Sidebar():
        solara.InputText(
            label="Molecule ID", value=molecule_id, on_value=molecule_id.set
        )

    with solara.Columns():
        with solara.Card(f"Protein: {molecule_id.value}"):
            PDBeMolstar.element(  # type: ignore
                molecule_id=molecule_id.value.lower(),
                hide_controls_icon=True,
                hide_expand_icon=True,
                hide_settings_icon=True,
                hide_selection_icon=False,
                select_interaction=False,  # dont show local interactions on click
                click_focus=False,  # dont zoom on click
                on_click_event=on_click,
            )

        with solara.Card(f"Amino Acid: {amino_acid.value}"):
            PDBeMolstar.element(  # type: ignore
                molecule_id="",
                custom_data=custom_data.value,
                hide_controls_icon=True,
                hide_expand_icon=True,
                hide_settings_icon=True,
                hide_selection_icon=False,
                click_focus=False,
            )