Py.Cafe

jhsmit/

protein-annotate-and-share

Interactive Protein Data Visualization with Solara

DocsPricing
  • app.py
  • pycafe_linkgen.py
  • requirements.txt
  • requirements_view.txt
  • viewer.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from pathlib import Path

import altair as alt
import ipywidgets
import pandas as pd
import pyperclip
import solara
from cmap import Catalog, Colormap
from pycafe_linkgen import make_link
from viewer import AxisProperties, ColorTransform, ProteinView

DEFAULT_CMAP = "tol:rainbow_PuRd"
VMIN_DEFAULT = 0.0
VMAX_DEFAULT = 1.0
HIGHLIGHT_COLOR = "#e933f8"
MISSING_DATA_COLOR = "#8c8c8c"
CMAP_OPTIONS = list(Catalog().namespaced_keys())

# TODO color pickers formatting


# VMIN_DEFAULT =

# column selection
# test button
# -> seca data
# revert vmin vmax defaults

# DONE

import numpy as np

N = 200
my_data = pd.DataFrame(
    {
        "resi": np.arange(N),
        "data1": np.sin(np.arange(N) / 10.0),
        "data2": np.random.rand(N),
    }
)

R_DEFAULT = "resi"
V_DEFAULT = "data1"


# etst
# %%
# TESTING TODO REMOVE
empty_frame = pd.DataFrame()
empty_frame = my_data


@solara.component
def Page():
    solara.Style(
        """
        .vega-embed {
        overflow: visible;
        width: 100% !important;
        }"""
    )

    dark_effective = solara.lab.use_dark_effective()
    dark_previous = solara.use_previous(dark_effective)

    if dark_previous != dark_effective:
        if dark_effective:
            alt.themes.enable("dark")
        else:
            alt.themes.enable("default")

    title = solara.use_reactive("My annotated protein view")
    description = solara.use_reactive("")
    pdb_id = solara.use_reactive("1QYN")
    data = solara.use_reactive(empty_frame)
    warning_text = solara.use_reactive("")

    residue_column = solara.use_reactive(R_DEFAULT)
    color_column = solara.use_reactive(V_DEFAULT)

    label = solara.use_reactive("value")
    unit = solara.use_reactive("au")

    highlight_color = solara.use_reactive(HIGHLIGHT_COLOR)
    missing_data_color = solara.use_reactive(MISSING_DATA_COLOR)
    autoscale_y = solara.use_reactive(True)

    cmap_name = solara.use_reactive(DEFAULT_CMAP)
    reverse = solara.use_reactive(False)
    full_cmap_name = cmap_name.value + "_r" if reverse.value else cmap_name.value
    cmap = Colormap(full_cmap_name)

    vmin = solara.use_reactive(VMIN_DEFAULT)
    vmax = solara.use_reactive(VMAX_DEFAULT)
    diverging = solara.use_reactive(False)

    png_bytes = cmap._repr_png_(height=24)

    def on_file(value: solara.components.file_drop.FileInfo):
        df = pd.read_csv(value["file_obj"])
        if len(df.columns) < 2:
            warning_text.set(f"Expected at least 2 columns, got {len(df.columns)}")
            data.set(pd.DataFrame())
            return

        warning_text.set("")
        data.set(df)
        residue_column.set(df.columns[0])
        color_column.set(df.columns[1])

    colors = ColorTransform(
        name=full_cmap_name,
        vmin=vmin.value,
        vmax=vmax.value,
        missing_data_color=missing_data_color.value,
        highlight_color=highlight_color.value,
    )

    axis_props = AxisProperties(
        label=label.value,
        unit=unit.value,
        autoscale_y=autoscale_y.value,
    )

    data_view = data.value.rename(
        columns={residue_column.value: "residue_number", color_column.value: "value"}
    )[["residue_number", "value"]]

    ProteinView(
        title.value,
        molecule_id=pdb_id.value,
        data=data_view,
        colors=colors,
        axis_properties=axis_props,
        dark_effective=dark_effective,
        description=description.value,
    ).key(f"ProteinView-{dark_effective}")  # redraw the app on theme change

    with solara.Sidebar():
        with solara.Card("Settings"):
            solara.InputText(label="Title", value=title)
            solara.InputText(label="PDB ID", value=pdb_id)
            solara.FileDrop(
                label="Drop .csv file with residue/color data", on_file=on_file
            )

            if warning_text.value:
                solara.Warning(warning_text.value)

            if not data.value.empty:
                with solara.Row():
                    solara.Select(
                        label="Residue Column",
                        value=residue_column,
                        values=list(data.value.columns),
                    )
                    solara.Select(
                        label="Color Column",
                        value=color_column,
                        values=list(data.value.columns),
                    )

            with solara.Row():
                solara.InputText(label="Label", value=label)
                solara.InputText(label="Unit", value=unit)

            ipywidgets.ColorPicker.element(  # type: ignore
                description="Highlight Color",
                concise=True,
                value=highlight_color.value,
                on_value=highlight_color.set,
            )

            ipywidgets.ColorPicker.element(  # type: ignore
                description="Missing data color",
                concise=True,
                value=missing_data_color.value,
                on_value=missing_data_color.set,
            )

            solara.Text(highlight_color.value)

            with solara.Row():
                # solara.v.ColorPicker()

                solara.Checkbox(label="Autoscale Y", value=autoscale_y)

            with solara.Row():
                solara.v.Autocomplete(
                    v_model=cmap_name.value,
                    on_v_model=cmap_name.set,
                    items=CMAP_OPTIONS,
                )
                solara.Checkbox(label="Reverse", value=reverse)

            with solara.Row():

                def set_vmin(value: float):
                    if diverging.value:
                        vmin.set(value)
                        vmax.set(-value)
                    else:
                        vmin.set(value)

                solara.InputFloat(
                    label="vmin",
                    value=vmin.value,
                    on_value=set_vmin,
                )
                solara.InputFloat(
                    label="vmax",
                    value=vmax,
                    disabled=diverging.value,
                )
                with solara.Tooltip("Sets normalization range symmetric around zero"):
                    solara.Checkbox(label="Diverging", value=diverging)

            solara.Image(png_bytes, width="100%")
            solara.InputTextArea(
                label="Description", value=description, continuous_update=True
            )
            solara.Div(style={"height": "10px"})

            def to_cafe():
                url = make_link(
                    title=title.value,
                    data=data_view,
                    molecule_id=pdb_id.value,
                    colors=colors,
                    axis_properties=axis_props,
                    description=description.value,
                )
                print(url)
                pyperclip.copy(url)

            solara.Button("Copy PyCafé link", block=True, on_click=to_cafe)


# %%