File size: 26,091 Bytes
31d5c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
"""
Differential Analysis Module
=============================

Differential flux analysis between metabolic domains/groups.
"""

import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import logging
from scipy import stats
from typing import Optional, List
from streamlit_option_menu import option_menu
import spmetatme.plotting as pl
import io
from datetime import datetime

logger = logging.getLogger(__name__)


def display_plot_with_download(fig, plot_name: str = "plot"):
    """
    Display a matplotlib figure with a PDF download button on top right.
    
    Parameters
    ----------
    fig : matplotlib.figure.Figure
        The matplotlib figure to display and download
    plot_name : str
        Name for the downloaded file (without extension)
    """
    # Create layout with download button on top right
    col_space, col_download = st.columns([5.5, 0.5], gap="small")
    
    with col_download:
        # Generate PDF file
        pdf_buffer = io.BytesIO()
        fig.savefig(pdf_buffer, format='pdf', dpi=300, bbox_inches='tight')
        file_data = pdf_buffer.getvalue()
        
        st.download_button(
            label="πŸ“₯",
            data=file_data,
            file_name=f"{plot_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf",
            mime="application/pdf",
            key=f"download_{plot_name}_{id(fig)}",
            help="Download as PDF",
            use_container_width=False
        )
    
    # Display the plot
    st.pyplot(fig)


def render():
    """Render differential analysis UI with sidebar menu."""
    # Check if we have flux data
    if st.session_state.metabolic_adata is None:
        st.warning("⚠️ No flux data available")
        st.markdown("""
        Please:
        1. **For spatial data**: Complete preprocessing and run flux analysis
        2. **For pre-computed fluxes**: Upload your flux data in the Upload Data tab
        """)
        return
    
    metabolic_adata = st.session_state.metabolic_adata
    
    # Initialize selected differential page
    if 'selected_diff_page' not in st.session_state:
        st.session_state.selected_diff_page = "Differential Reactions"
    
    # Define differential analysis options
    diff_options = [
        "Differential Reactions",
        "Pathway Selection",
        "Differential Pathways",
        "Pathways by Variance"
    ]
    
    diff_icons = [
        "table",
        "fire",
        "diagram-3",
        "graph-up"
    ]
    
    # Get the current index
    try:
        current_index = diff_options.index(st.session_state.selected_diff_page)
    except ValueError:
        current_index = 0
        st.session_state.selected_diff_page = "Differential Reactions"
    
    # Sidebar menu for differential analysis selection
    with st.sidebar:
        selected_diff = option_menu(
            menu_title="Differential Analysis",
            options=diff_options,
            icons=diff_icons,
            default_index=current_index,
            orientation="vertical",
            styles={
                "container": {"padding": "0!important", "background-color": "#ffffff"},
                "icon": {"color": "#1a73e8", "font-size": "18px"},
                "nav-link": {
                    "font-size": "12px",
                    "text-align": "left",
                    "margin": "0px",
                    "padding": "12px 15px",
                    "--hover-color": "#e3f2fd",
                    "color": "#333333"
                },
                "nav-link-selected": {
                    "background-color": "#1a73e8",
                    "color": "#ffffff",
                    "font-weight": "600"
                }
            },
            key="diff_option_menu"
        )
        
        # Only rerun if selection changed
        if selected_diff != st.session_state.selected_diff_page:
            st.session_state.selected_diff_page = selected_diff
            st.rerun()
        
        st.markdown("---")
        
        # Back to home button in sidebar
        if st.button("🏠 Back to Home", use_container_width=True, key="back_to_home_diff_sidebar"):
            st.session_state.adata = None
            st.session_state.metabolic_adata = None
            st.session_state.data_type = None
            st.session_state.preprocessing_done = False
            st.session_state.flux_analysis_done = False
            st.session_state.selected_diff_page = None
            st.rerun()
        
        st.markdown("---")
        
        # Info section in sidebar
        st.markdown("""
        <div style='background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%); padding: 1rem; border-radius: 8px; font-size: 0.85rem; line-height: 1.6; border-left: 3px solid #1a73e8;'>
            <strong style='color: #1a73e8;'>πŸ“Š Differential Analysis</strong><br>
            Identify metabolically distinct regions and enriched reactions across domains.
        </div>
        """, unsafe_allow_html=True)
    
    # Main content area
    st.markdown("## πŸ“‰ Differential Metabolic Flux Analysis")
    
    st.markdown("""
    Identify metabolic reactions and pathways with significant differences between
    spatial domains and metabolic phenotypes.
    """)
    
    st.markdown("---")
    
    # Render selected differential analysis page
    if st.session_state.selected_diff_page == "Differential Reactions":
        render_differential_reactions(metabolic_adata)
    
    elif st.session_state.selected_diff_page == "Pathway Selection":
        render_pathway_selection(metabolic_adata)
    
    elif st.session_state.selected_diff_page == "Differential Pathways":
        render_differential_pathways(metabolic_adata)
    
    elif st.session_state.selected_diff_page == "Pathways by Variance":
        render_pathways_by_variance(metabolic_adata)


def render_differential_reactions(metabolic_adata):
    """Render differential reactions analysis with tabs for different heatmap types."""
    st.markdown("### Differential Metabolic Reactions Analysis")
    
    st.markdown("""
    Analyze differentially enriched metabolic reactions across spatial domains
    using different visualization approaches.
    """)
    
    # Create tabs for different analysis types
    tab1, tab2, tab3 = st.tabs([
        "Pathway-Specific Reactions",
        "All Differential Reactions",
        "Pathways by Variance"
    ])
    
    # TAB 1: Pathway-Specific Reactions (plot_differential_reactions_by_pathway_heatmap)
    with tab1:
        st.markdown("#### Pathway-Specific Differential Analysis")
        
        if 'subsystems' not in metabolic_adata.var.columns:
            st.error("Pathway information (subsystems) not found in data")
        else:
            available_pathways = sorted(metabolic_adata.var['subsystems'].unique().tolist())
            
            # Controls
            col1, col2, col3 = st.columns(3)
            
            with col1:
                selected_pathway = st.selectbox(
                    "Select pathway:",
                    options=available_pathways,
                    key="tab1_pathway_dropdown"
                )
            
            with col2:
                top_n_pathway = st.slider(
                    "Top N reactions",
                    min_value=5,
                    max_value=50,
                    value=15,
                    step=1,
                    key="tab1_pathway_top_n"
                )
            
            with col3:
                row_cluster = st.checkbox("Cluster rows", value=True, key="tab1_row_cluster")
            
            try:
                with st.spinner(f"Analyzing {selected_pathway}..."):
                    # Generate heatmap
                    df_pathway = pl.plot_differential_reactions_by_pathway_heatmap(
                        metabolic_adata, 
                        selected_pathway, 
                        row_cluster=row_cluster, 
                        return_marker_df=True, 
                        save_path=None,
                        top_n=top_n_pathway
                    )
                    
                    fig = plt.gcf()
                    
                    # Two-column layout: Heatmap and Table
                    col_plot, col_table = st.columns([1, 1], gap="large")
                    
                    with col_plot:
                        display_plot_with_download(fig, f"{selected_pathway.replace(' ', '_')}_Heatmap")
                    
                    with col_table:
                        st.write("")  
                        st.markdown("##### Reactions Data")
                        if df_pathway is not None:
                            st.dataframe(df_pathway, use_container_width=True)
                            
                            # Download button
                            csv = df_pathway.to_csv(index=False)
                            st.download_button(
                                label="πŸ“₯ Download Table (CSV)",
                                data=csv,
                                file_name=f"pathway_{selected_pathway.replace(' ', '_')}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                                mime="text/csv",
                                key="tab1_download_table"
                            )
                        else:
                            st.info("No data available")
                    
            except Exception as e:
                st.error(f"Error: {str(e)}")
                logger.error(f"Tab1 error: {str(e)}", exc_info=True)
    
    # TAB 2: All Differential Reactions (plot_differential_reactions_heatmap)
    with tab2:
        st.markdown("#### All Differential Reactions Heatmap")
        
        # Controls
        col1, col2 = st.columns(2)
        
        with col1:
            top_n_reactions = st.slider(
                "Top N reactions to show",
                min_value=5,
                max_value=100,
                value=20,
                step=5,
                key="tab2_top_n_reactions"
            )
        
        with col2:
            st.write("")  # Spacer
        
        try:
            with st.spinner("Analyzing all differential reactions..."):
                # Generate heatmap
                df_reactions = pl.plot_differential_reactions_heatmap(
                    metabolic_adata, 
                    save_path=None,
                    top_n=top_n_reactions,
                    return_marker_df=True
                )
                
                fig = plt.gcf()
                                            
                # Two-column layout: Heatmap and Table
                col_plot, col_table = st.columns([1, 1], gap="large")
                
                with col_plot:
                    display_plot_with_download(fig, "Differential_Reactions_Heatmap")
                
                with col_table:
                    st.write("")  
                    st.markdown("##### Reactions Data")
                    if df_reactions is not None:
                        st.dataframe(df_reactions, use_container_width=True)
                        
                        # Download button
                        csv = df_reactions.to_csv(index=False)
                        st.download_button(
                            label="πŸ“₯ Download Table (CSV)",
                            data=csv,
                            file_name=f"differential_reactions_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                            mime="text/csv",
                            key="tab2_download_table"
                        )
                    else:
                        st.info("No data available")
                                        
        except Exception as e:
            st.error(f"Error: {str(e)}")
            logger.error(f"Tab2 error: {str(e)}", exc_info=True)
    
    # TAB 3: Pathways by Variance (plot_pathways_flux_heatmap)
    with tab3:
        st.markdown("#### Pathways by Variance")
        
        # Controls
        col1, col2, col3 = st.columns(3)
        
        with col1:
            top_n = st.slider(
                "Top N pathways",
                min_value=5,
                max_value=30,
                value=20,
                step=1,
                key="tab3_top_n"
            )
        
        with col2:
            sort_by = st.selectbox(
                "Sort by",
                options=["variance", "mean"],
                key="tab3_sort_by"
            )
        
        with col3:
            st.write("")  # Spacer
        
        
        try:
            with st.spinner(f"Analyzing top {top_n} pathways by {sort_by}..."):
                # Generate heatmap
                df_pathways_var = pl.plot_pathways_flux_heatmap(
                    metabolic_adata, 
                    group_key="domain", 
                    pathway_key="subsystems", 
                    top_n=top_n, 
                    sort_by=sort_by
                )
                
                fig = plt.gcf()
                                
                # Two-column layout: Heatmap and Table
                col_plot, col_table = st.columns([1, 1], gap="large")
                
                with col_plot:
                    display_plot_with_download(fig, f"Pathways_Variance_Top{top_n}")
                
                with col_table:
                    st.markdown("##### Pathways Data")
                    if df_pathways_var is not None:
                        st.dataframe(df_pathways_var, use_container_width=True)
                        
                        # Download button
                        csv = df_pathways_var.to_csv(index=False)
                        st.download_button(
                            label="πŸ“₯ Download Table (CSV)",
                            data=csv,
                            file_name=f"pathways_variance_top{top_n}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                            mime="text/csv",
                            key="tab3_download_table"
                        )
                    else:
                        st.info("No data available")
                
        except Exception as e:
            st.error(f"Error: {str(e)}")
            logger.error(f"Tab3 error: {str(e)}", exc_info=True)


def render_pathway_selection(metabolic_adata):
    """Render interactive pathway selection with dropdown for differential analysis."""
    st.markdown("### Pathway-Specific Differential Analysis")
    
    st.markdown("""
    Select any metabolic pathway to investigate differential enrichment of reactions
    within that pathway across spatial metabolic domains.
    """)
    
    # Get all available pathways
    if 'subsystems' not in metabolic_adata.var.columns:
        st.error("Pathway information (subsystems) not found in data")
        return
    
    available_pathways = sorted(metabolic_adata.var['subsystems'].unique().tolist())
    
    # Pathway selection
    col1, col2 = st.columns(2)
    
    with col1:
        selected_pathway = st.selectbox(
            "Select pathway to analyze:",
            options=available_pathways,
            key="pathway_dropdown"
        )
    
    with col2:
        top_n_pathway = st.slider(
            "Top N reactions to display",
            min_value=5,
            max_value=50,
            value=15,
            step=1,
            key="pathway_top_n"
        )
    
    # Analysis options
    col1, col2, col3 = st.columns(3)
    
    with col1:
        row_cluster = st.checkbox("Cluster rows", value=True, key="pathway_row_cluster")
    
    with col2:
        show_table = st.checkbox("Show data table", value=True, key="pathway_show_table")
    
    with col3:
        show_stats = st.checkbox("Show statistics", value=True, key="pathway_show_stats")
    
    if st.button(f"πŸ“Š Analyze {selected_pathway}", key="pathway_analyze_btn"):
        try:
            with st.spinner(f"Analyzing {selected_pathway}..."):
                
                # Generate the heatmap
                df_pathway = pl.plot_differential_reactions_by_pathway_heatmap(
                    metabolic_adata, 
                    selected_pathway, 
                    row_cluster=row_cluster, 
                    return_marker_df=True, 
                    save_path=None,
                    top_n=top_n_pathway
                )
                
                # Get the current figure
                fig = plt.gcf()
                
                st.success(f"βœ“ {selected_pathway} analysis completed!")
                
                # Display with download option
                display_plot_with_download(fig, f"Pathway_{selected_pathway.replace(' ', '_')}_Heatmap")
                
                st.markdown("---")
                
                # Display statistics if requested
                if show_stats:
                    col1, col2, col3 = st.columns(3)
                    
                    with col1:
                        reactions_in_pathway = len(df_pathway) if df_pathway is not None else 0
                        st.metric("Reactions in Pathway", reactions_in_pathway)
                    
                    with col2:
                        if 'domain' in metabolic_adata.obs.columns:
                            n_domains = metabolic_adata.obs['domain'].nunique()
                            st.metric("Number of Domains", n_domains)
                    
                    with col3:
                        st.metric("Spatial Spots", metabolic_adata.n_obs)
                    
                    st.markdown("---")
                
                # Show data table if requested
                if show_table and df_pathway is not None:
                    st.markdown(f"#### {selected_pathway} - Reactions Data")
                    st.dataframe(df_pathway, use_container_width=True)
                    
                    # Download button for table
                    csv = df_pathway.to_csv(index=False)
                    st.download_button(
                        label="πŸ“₯ Download Table (CSV)",
                        data=csv,
                        file_name=f"pathway_{selected_pathway.replace(' ', '_')}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                        mime="text/csv",
                        key="download_pathway_table"
                    )
                
                st.info(f"πŸ’‘ Tip: This heatmap shows the {top_n_pathway} most differential reactions in the {selected_pathway} pathway")
                
        except Exception as e:
            st.error(f"Error analyzing {selected_pathway}: {str(e)}")
            logger.error(f"Pathway selection error for {selected_pathway}: {str(e)}", exc_info=True)



def render_differential_pathways(metabolic_adata):
    """Render differential pathways heatmap (top N pathways)."""
    st.markdown("### Differential Pathways Heatmap")
    
    st.markdown("""
    This visualization shows metabolic pathways with the largest differences
    in mean flux between spatial domains. Each pathway is aggregated from its constituent reactions.
    """)
    
    # Options
    col1, col2 = st.columns(2)
    
    with col1:
        top_n_pathways = st.slider(
            "Number of top pathways to show",
            min_value=5,
            max_value=20,
            value=15,
            step=1,
            key="diff_pathway_top_n"
        )
    
    with col2:
        show_table = st.checkbox("Show data table", value=True, key="diff_pathway_show_table")
    
    if st.button("πŸ“Š Generate Differential Pathways Heatmap", key="diff_pathway_btn"):
        try:
            with st.spinner("Generating differential pathways heatmap..."):
                
                # Generate the heatmap
                fig = plt.figure(figsize=(14, 10))
                df_pathways = pl.plot_differential_pathways_heatmap(
                    metabolic_adata, 
                    save_path=None,
                    top_n=top_n_pathways
                )
                
                # Get the current figure
                fig = plt.gcf()
                
                st.success("βœ“ Differential pathways heatmap generated successfully!")
                
                # Display with download option
                display_plot_with_download(fig, "Differential_Pathways_Heatmap")
                
                st.markdown("---")
                
                # Display statistics
                col1, col2, col3 = st.columns(3)
                
                with col1:
                    st.metric("Top Pathways Shown", top_n_pathways)
                
                with col2:
                    if 'domain' in metabolic_adata.obs.columns:
                        n_domains = metabolic_adata.obs['domain'].nunique()
                        st.metric("Number of Domains", n_domains)
                
                with col3:
                    if 'subsystems' in metabolic_adata.var.columns:
                        n_pathways = metabolic_adata.var['subsystems'].nunique()
                        st.metric("Total Pathways", n_pathways)
                
                st.info("πŸ’‘ Tip: Pathways ranked by the sum of absolute flux differences across domains")
                
                # Show data table if requested
                if show_table and df_pathways is not None:
                    st.markdown("---")
                    st.markdown("#### Differential Pathways Data")
                    st.dataframe(df_pathways, use_container_width=True)
                    
                    # Download button for table
                    csv = df_pathways.to_csv(index=False)
                    st.download_button(
                        label="πŸ“₯ Download Table (CSV)",
                        data=csv,
                        file_name=f"differential_pathways_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                        mime="text/csv",
                        key="download_diff_pathways_table"
                    )
                
        except Exception as e:
            st.error(f"Error generating differential pathways heatmap: {str(e)}")
            logger.error(f"Differential pathways error: {str(e)}", exc_info=True)


def render_pathways_by_variance(metabolic_adata):
    """Render pathways ranked by variance (top N)."""
    st.markdown("### Pathways by Variance")
    
    st.markdown("""
    This visualization shows metabolic pathways with the highest variance
    in flux values across the tissue. High variance indicates heterogeneous metabolic activity
    and potential metabolic specialization across domains.
    """)
    
    # Options
    col1, col2, col3 = st.columns(3)
    
    with col1:
        top_n = st.slider(
            "Number of pathways to show",
            min_value=5,
            max_value=30,
            value=20,
            step=1,
            key="pathway_variance_n"
        )
    
    with col2:
        sort_by = st.selectbox(
            "Sort by",
            options=["variance", "mean"],
            key="pathway_sort_by"
        )
    
    with col3:
        show_table = st.checkbox("Show data table", value=True, key="pathway_var_show_table")
    
    if st.button("πŸ“Š Generate Pathways by Variance Heatmap", key="pathway_var_btn"):
        try:
            with st.spinner(f"Generating top {top_n} pathways by {sort_by} heatmap..."):
                
                # Generate the heatmap
                fig = plt.figure(figsize=(14, 10))
                df_pathways_var = pl.plot_pathways_flux_heatmap(
                    metabolic_adata, 
                    group_key="domain", 
                    pathway_key="subsystems", 
                    top_n=top_n, 
                    sort_by=sort_by
                )
                
                # Get the current figure
                fig = plt.gcf()
                
                st.success(f"βœ“ Pathways by {sort_by} heatmap generated successfully!")
                
                # Display with download option
                display_plot_with_download(fig, f"Pathways_Variance_Top{top_n}")
                
                st.markdown("---")
                
                # Display statistics
                col1, col2, col3 = st.columns(3)
                
                with col1:
                    st.metric("Top Pathways Shown", top_n)
                
                with col2:
                    st.metric("Sort Metric", sort_by.capitalize())
                
                with col3:
                    if 'domain' in metabolic_adata.obs.columns:
                        n_domains = metabolic_adata.obs['domain'].nunique()
                        st.metric("Number of Domains", n_domains)
                
                st.info(f"πŸ’‘ Tip: Shows {top_n} most variable pathways across spatial domains, highlighting metabolic hotspots")
                
                # Show data table if requested
                if show_table and df_pathways_var is not None:
                    st.markdown("---")
                    st.markdown(f"#### Top {top_n} Pathways by {sort_by.title()}")
                    st.dataframe(df_pathways_var, use_container_width=True)
                    
                    # Download button for table
                    csv = df_pathways_var.to_csv(index=False)
                    st.download_button(
                        label="πŸ“₯ Download Table (CSV)",
                        data=csv,
                        file_name=f"pathways_variance_top{top_n}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                        mime="text/csv",
                        key="download_pathways_var_table"
                    )
                
        except Exception as e:
            st.error(f"Error generating pathways by variance heatmap: {str(e)}")
            logger.error(f"Pathways by variance error: {str(e)}", exc_info=True)