File size: 2,212 Bytes
c6379ae
692e28b
 
 
c6379ae
692e28b
 
 
 
c6379ae
692e28b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import fcsparser
import matplotlib.pyplot as plt
import io

# Function to parse FCS file and extract data and metadata
def parse_fcs(file):
    meta, data = fcsparser.parse(file.name, reformat_meta=True)
    return data, list(data.columns)

# Function to plot the data based on selected x and y parameters
def plot_dot(file, x_param, y_param):
    if not file:
        return "Please upload an FCS file."

    try:
        data, columns = parse_fcs(file)

        if x_param not in columns or y_param not in columns:
            return "Invalid parameters selected. Please choose valid parameters from the dropdown."

        # Create the plot
        plt.figure(figsize=(8, 6))
        plt.scatter(data[x_param], data[y_param], s=1, alpha=0.5, color="blue")
        plt.xlabel(x_param)
        plt.ylabel(y_param)
        plt.title("Flow Cytometry Dot Plot")
        plt.grid(True)

        # Save plot to a BytesIO object
        buf = io.BytesIO()
        plt.savefig(buf, format="png")
        buf.seek(0)
        plt.close()
        return buf
    except Exception as e:
        return f"Error: {e}"

# Function to dynamically update parameter options after file upload
def get_parameters(file):
    if not file:
        return [], []

    try:
        _, columns = parse_fcs(file)
        return gr.Dropdown.update(choices=columns), gr.Dropdown.update(choices=columns)
    except Exception as e:
        return [f"Error: {e}"], [f"Error: {e}"]

# Create Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# Flow Cytometry Dot Plot Viewer")

    with gr.Row():
        fcs_file = gr.File(label="Upload FCS File")

    with gr.Row():
        x_param = gr.Dropdown(label="X Parameter", choices=[], interactive=True)
        y_param = gr.Dropdown(label="Y Parameter", choices=[], interactive=True)

    plot_btn = gr.Button("Generate Plot")
    plot_output = gr.Image(type="file", label="Dot Plot")

    # Update parameter dropdowns on file upload
    fcs_file.change(get_parameters, inputs=[fcs_file], outputs=[x_param, y_param])

    # Generate plot on button click
    plot_btn.click(plot_dot, inputs=[fcs_file, x_param, y_param], outputs=plot_output)

# Run the app
app.launch()