Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import json
import pandas as pd
from typing import List, Dict
from shinywidgets import render_altair
from shiny import App, render, ui, reactive
from utils import data_utils
from utils import ms_plotting as ms_plot
from utils.data_utils import get_mass_across_datasets_table
from utils.ms_plotting import create_spectra_by_experiment_plot
from utils.ui_component_renderer_builder import spectra_panel_renderer, spectra_by_attribute_selector_renderer, \
data_table_panel_by_attribute_selector_renderer, data_table_panel_renderer
import polars as pl
import altair as alt
RENDER_PLOT_ATTRIBUTE_SELECTOR = 'render_plot_creator'
RENDER_DATA_TABLE_ATTRIBUTE_SELECTOR = 'render_dt_creator'
RENDER_VISUALIZATION = 'panel_render_trigger'
app_ui = ui.page_sidebar(ui.sidebar(ui.input_action_link(id='panel_creator_trigger', label='Plot Creator'),
ui.input_action_link(id='data_table_panel_attribute_selector_trigger',
label='Data Table Creator'),
ui.input_action_link(id='panel_render_trigger', label='Visualization Panels'), ),
ui.output_ui('stage'), title=ui.panel_title("MASSIVE"))
def server(input, output, session):
alt.data_transformers.enable('vegafusion')
r_spectra_attributes = reactive.value([])
r_dt_attributes = reactive.value([])
r_stage_render_flag = reactive.value('')
r_spectra_by_experiment_plot = reactive.value(None)
r_mz_report_dt = reactive.value(None)
@reactive.effect
@reactive.event(input.panel_creator_trigger)
def _():
r_stage_render_flag.set(RENDER_PLOT_ATTRIBUTE_SELECTOR)
@reactive.effect
@reactive.event(input.data_table_panel_attribute_selector_trigger)
def _():
r_stage_render_flag.set(RENDER_DATA_TABLE_ATTRIBUTE_SELECTOR)
@reactive.effect
@reactive.event(input.panel_render_trigger)
def _():
r_stage_render_flag.set(RENDER_VISUALIZATION)
@reactive.effect
@reactive.event(input.spectra_attribute_selector_btn)
def _():
selected_spectra_attributes = input.spectra_attribute_selector()
if not selected_spectra_attributes:
selected_spectra_attributes = []
r_spectra_attributes.set(selected_spectra_attributes)
@reactive.effect
@reactive.event(input.data_table_panel_attribute_selector_btn)
def _():
selected_dt_attributes = input.data_table_panel_attribute_selector()
if not selected_dt_attributes:
selected_dt_attributes = []
r_dt_attributes.set(selected_dt_attributes)
@render.ui
@reactive.event(r_stage_render_flag)
def stage():
action = r_stage_render_flag.get()
if action == RENDER_PLOT_ATTRIBUTE_SELECTOR:
return spectra_by_attribute_selector_renderer()
if action == RENDER_VISUALIZATION:
selected_spectra_attributes = r_spectra_attributes.get()
selected_dt_attributes = r_dt_attributes.get()
return ui.page_fluid(ui.panel_title('Stage'), spectra_panel_renderer(selected_spectra_attributes),
data_table_panel_renderer(selected_dt_attributes))
if action == RENDER_DATA_TABLE_ATTRIBUTE_SELECTOR:
return data_table_panel_by_attribute_selector_renderer()
pass
@reactive.effect
@reactive.event(input.spectra_attribute_selector_btn)
def _():
if 'Experiment' in r_spectra_attributes.get():
r_spectra_by_experiment_plot.set(create_spectra_by_experiment_plot(data_utils.get_dataset()))
@reactive.effect
@reactive.event(input.data_table_panel_attribute_selector_btn, input.mz_agg_intensity_cutoff)
def _():
# This function builds the aggregated m/z report across datasets
if 'mz_report_across_dataset' in r_dt_attributes.get():
intensity_cutoff = input.mz_agg_intensity_cutoff()
df: pl.DataFrame = get_mass_across_datasets_table(data_utils.get_dataset(by_experiment=False),
intensity_cutoff=intensity_cutoff)
r_mz_report_dt.set(df)
@render_altair
@reactive.event(r_spectra_by_experiment_plot)
def spectra_by_experiment_plot():
if 'Experiment' in r_spectra_attributes.get():
return r_spectra_by_experiment_plot.get()
return None
@render.data_frame
@reactive.event(r_mz_report_dt)
def mz_report_across_datasets():
if 'mz_report_across_dataset' in r_dt_attributes.get():
dt: pl.DataFrame = r_mz_report_dt.get()
return dt
return None
app = App(app_ui, server)