File size: 13,204 Bytes
e42f219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import yaml
import gradio as gr
import pandas as pd
from functools import partial
from nist_cda_dashboard.component import ComponentHelper
from nist_cda_dashboard.utils import DownloadHelper


with open("./config.yml", 'r') as file:
    config = yaml.safe_load(file)

component_helper = ComponentHelper(file_qc_results=pd.read_csv(config["file_path"]["file_qc_results"]),
                                   dataset_qc_results=pd.read_csv(
                                       config["file_path"]["dataset_qc_results"]),
                                   analyzed_gating_results=pd.read_csv(
                                       config["file_path"]["analyzed_gating_results"]),
                                   analyzed_gating_results_wCDAResults=pd.read_csv(
                                       config["file_path"]["analyzed_gating_results_wCDAResults"]),
                                   config=config)

custom_css = """
#plot_wScrollBar {
    overflow-x: auto !important;
    text-align: center !important;
}

#plot_wScrollBar > .js-plotly-plot,
#plot_wScrollBar > .plotly,
#plot_wScrollBar > div:first-child { 
    display: inline-block !important;
    text-align: left !important;
}
"""

with gr.Blocks(css=custom_css) as demo:
    with gr.Sidebar(position="left", width=700):
        gr.Markdown("# STEP 1: Filter Dataset")
        clear_all_models_button, select_all_models_button, model_filter, \
            clear_all_datasets_button, select_all_datasets_button, dataset_filter, \
            main_apply_filters_button = component_helper.creator.main_filter_components()

    clear_all_models_button.click(fn=partial(component_helper.updater.clean_all_choices),
                                  outputs=model_filter)
    select_all_models_button.click(fn=partial(component_helper.updater.select_all_choices, "model_filter"),
                                   outputs=model_filter)

    clear_all_datasets_button.click(fn=partial(component_helper.updater.clean_all_choices),
                                    outputs=dataset_filter)
    select_all_datasets_button.click(fn=partial(component_helper.updater.select_all_choices, "dataset_filter"),
                                     outputs=dataset_filter)

    model_filter.change(fn=component_helper.updater.update_dataset_filter,
                        inputs=[model_filter,
                                dataset_filter],
                        outputs=dataset_filter)

    gr.Markdown("# STEP 2: Choose QC or Analysis Tab")
    with gr.Tab(label="Quality Check"):
        gr.Markdown("# STEP 3: Filter File")
        clear_all_sop_exps_qc_tab_button, select_all_sop_exps_qc_tab_button, sop_exp_qc_tab_filter, \
            clear_all_materials_button, select_all_materials_button, material_filter, \
            clear_all_issues_button, select_all_issues_button, issue_filter, \
            qc_tab_apply_filters_button = component_helper.creator.qc_tab_filter_components()

        clear_all_sop_exps_qc_tab_button.click(fn=partial(component_helper.updater.clean_all_choices),
                                               outputs=sop_exp_qc_tab_filter)
        select_all_sop_exps_qc_tab_button.click(fn=partial(component_helper.updater.select_all_choices, "sop_exp_qc_tab_filter"),
                                                outputs=sop_exp_qc_tab_filter)

        clear_all_materials_button.click(fn=partial(component_helper.updater.clean_all_choices),
                                         outputs=material_filter)
        select_all_materials_button.click(fn=partial(component_helper.updater.select_all_choices, "material_filter"),
                                          outputs=material_filter)

        clear_all_issues_button.click(fn=partial(component_helper.updater.clean_all_choices),
                                      outputs=issue_filter)
        select_all_issues_button.click(fn=partial(component_helper.updater.select_all_choices, "issue_filter"),
                                       outputs=issue_filter)

        sop_exp_qc_tab_filter.change(fn=component_helper.updater.update_material_filter,
                                     inputs=[sop_exp_qc_tab_filter,
                                             material_filter],
                                     outputs=material_filter)

        gr.Markdown("<br><br>")
        gr.Markdown("# STEP 4: Results")
        qc_tab_dataset_qc_status_filter = component_helper.creator.qc_tab_dataset_qc_status_filter_component()

        with gr.Row():
            gr.Markdown("## File QC Results Table")
            download_file_qc_button = gr.Button(
                "Download File QC Results")

        file_qc_status_filter, file_qc_table, file_qc_table_no_file_msg = component_helper.creator.file_qc_result_components()

        download_file_qc_button_hidden = gr.DownloadButton(
            visible=False, elem_id="download_file_qc_button_hidden")
        download_file_qc_button.click(fn=(lambda df: DownloadHelper.df2csv(df, "file_qc_table")),
                                      inputs=[file_qc_table],
                                      outputs=[download_file_qc_button_hidden]). \
            then(fn=None,
                 inputs=None,
                 outputs=None,
                 js="() => document.querySelector('#download_file_qc_button_hidden').click()")

        gr.Markdown("<br><br>")
        with gr.Row():
            gr.Markdown("## Dataset QC Results Table")
            download_dataset_qc_button = gr.Button(
                "Download Dataset QC Results")
        dataset_qc_table, dataset_qc_table_no_dataset_msg = component_helper.creator.dataset_qc_result_components()

        download_dataset_qc_button_hidden = gr.DownloadButton(
            visible=False, elem_id="download_dataset_qc_button_hidden")
        download_dataset_qc_button.click(fn=(lambda df: DownloadHelper.df2csv(df, "dataset_qc_table")),
                                         inputs=[dataset_qc_table],
                                         outputs=[download_dataset_qc_button_hidden]). \
            then(fn=None,
                 inputs=None,
                 outputs=None,
                 js="() => document.querySelector('#download_dataset_qc_button_hidden').click()")

        gr.Markdown("<br><br>")
        with gr.Row():
            gr.Markdown("## QC Results Visualization")
            download_qc_fig_button = gr.Button(
                "Download QC Visualization Figure")
        qc_fig, qc_fig_no_file_msg = component_helper.creator.qc_result_visual_components()

        download_qc_fig_button_hidden = gr.DownloadButton(
            visible=False, elem_id="download_qc_fig_button_hidden")
        download_qc_fig_button.click(fn=(lambda fig: DownloadHelper.fig2png(fig, "qc_visual_fig")),
                                     inputs=[qc_fig],
                                     outputs=[download_qc_fig_button_hidden]). \
            then(fn=None,
                 inputs=None,
                 outputs=None,
                 js="() => document.querySelector('#download_qc_fig_button_hidden').click()")

        gr.on(fn=component_helper.updater.update_file_qc_table,
              triggers=[main_apply_filters_button.click, qc_tab_apply_filters_button.click,
                        qc_tab_dataset_qc_status_filter.change, file_qc_status_filter.change],
              inputs=[dataset_filter, sop_exp_qc_tab_filter, material_filter,
                      issue_filter, qc_tab_dataset_qc_status_filter, file_qc_status_filter],
              outputs=[file_qc_table, file_qc_table_no_file_msg,
                       dataset_qc_table, dataset_qc_table_no_dataset_msg])

        gr.on(fn=component_helper.updater.update_qc_fig,
              triggers=[main_apply_filters_button.click, qc_tab_apply_filters_button.click,
                        qc_tab_dataset_qc_status_filter.change],
              inputs=[dataset_filter, sop_exp_qc_tab_filter, material_filter,
                      issue_filter, qc_tab_dataset_qc_status_filter],
              outputs=[qc_fig, qc_fig_no_file_msg])

    with gr.Tab(label="Gating Result Analysis"):
        gr.Markdown("# STEP 3: Filter File")
        sample_filter, sop_exp_analysis_tab_filter, compensation_control_filter, \
            gating_control_filter, pop_pheno_parent_filter, clear_gating_tab_filters_button, analysis_tab_apply_filters_button = component_helper.creator.analysis_tab_filter_components()

        gr.Markdown("<br><br>")
        gr.Markdown("# STEP 4: Results")
        gr.Markdown("## Analysis Visualization")
        analysis_tab_dataset_qc_status_filter, analyzed_result_filter = component_helper.creator.analyzed_result_filter_component()

        with gr.Tab(label="Single Result  Barplot"):
            download_barplot_fig_button = gr.Button("Download Barplot Figure")
            analysis_barplot_fig, barplot_not_reportable_msg = component_helper.creator.analysis_barplot_components()

            download_barplot_fig_button_hidden = gr.DownloadButton(
                visible=False, elem_id="download_barplot_fig_button_hidden")
            download_barplot_fig_button.click(fn=(lambda fig: DownloadHelper.fig2png(fig, "analysis_barplot_fig")),
                                              inputs=[analysis_barplot_fig],
                                              outputs=[download_barplot_fig_button_hidden]). \
                then(fn=None,
                    inputs=None,
                    outputs=None,
                     js="() => document.querySelector('#download_barplot_fig_button_hidden').click()")

        with gr.Tab(label="Multiple Results Heatmap"):
            download_heatmap_fig_button = gr.Button(
                "Download Heatmap Figure")
            compared_protocol_filter, include_CDA_results_checkbox, analysis_heatmap_exp_info_table, analysis_heatmap_exp_comparison_table, \
                analysis_heatmap_fig, heatmap_not_reportable_msg = component_helper.creator.analysis_heatmap_components()

            download_heatmap_fig_button_hidden = gr.DownloadButton(
                visible=False, elem_id="download_heatmap_fig_button_hidden")
            download_heatmap_fig_button.click(fn=(lambda fig: DownloadHelper.fig2png(fig, "analysis_heatmap_fig")),
                                              inputs=[analysis_heatmap_fig],
                                              outputs=[download_heatmap_fig_button_hidden]). \
                then(fn=None,
                     inputs=None,
                     outputs=None,
                     js="() => document.querySelector('#download_heatmap_fig_button_hidden').click()")

        gr.on(fn=component_helper.updater.update_barplot_fig,
              triggers=[main_apply_filters_button.click, analysis_tab_apply_filters_button.click,
                        analysis_tab_dataset_qc_status_filter.change, analyzed_result_filter.change],
              inputs=[dataset_filter, analysis_tab_dataset_qc_status_filter,
                      sample_filter, sop_exp_analysis_tab_filter, compensation_control_filter,
                      gating_control_filter, pop_pheno_parent_filter, analyzed_result_filter],
              outputs=[analysis_barplot_fig, barplot_not_reportable_msg])

        gr.on(fn=component_helper.updater.update_heatmap_fig,
              triggers=[main_apply_filters_button.click, analysis_tab_apply_filters_button.click,
                        analysis_tab_dataset_qc_status_filter.change, analyzed_result_filter.change,
                        compared_protocol_filter.change, include_CDA_results_checkbox.change],
              inputs=[dataset_filter, analysis_tab_dataset_qc_status_filter,
                      sample_filter, sop_exp_analysis_tab_filter, compensation_control_filter,
                      gating_control_filter, pop_pheno_parent_filter,
                      analyzed_result_filter, compared_protocol_filter, include_CDA_results_checkbox],
              outputs=[analysis_heatmap_exp_info_table, analysis_heatmap_exp_comparison_table,
                       analysis_heatmap_fig, heatmap_not_reportable_msg])

        gr.on(fn=component_helper.updater.update_analysis_tab_filters,
              triggers=[sample_filter.change,
                        sop_exp_analysis_tab_filter.change, compensation_control_filter.change,
                        gating_control_filter.change, pop_pheno_parent_filter.change],
              inputs=[sample_filter,
                      sop_exp_analysis_tab_filter, compensation_control_filter,
                      gating_control_filter, pop_pheno_parent_filter],
              outputs=[sample_filter,
                       sop_exp_analysis_tab_filter, compensation_control_filter,
                       gating_control_filter, pop_pheno_parent_filter])

        clear_gating_tab_filters_button.click(fn=component_helper.updater.clear_analysis_tab_filters,
                                              outputs=[sample_filter,
                                                       sop_exp_analysis_tab_filter, compensation_control_filter,
                                                       gating_control_filter, pop_pheno_parent_filter])

demo.launch(inbrowser=True)