File size: 2,240 Bytes
c6379ae
9be415c
f305b38
 
c6379ae
692e28b
53cac92
 
 
 
 
 
 
 
 
 
 
 
f305b38
 
53cac92
 
 
 
 
 
 
 
 
 
f305b38
 
53cac92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f305b38
53cac92
 
 
f305b38
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import fcsparser

def parse_fcs(file):
    try:
        # Parse the FCS file
        meta, data = fcsparser.parse(file.name)
        df = pd.DataFrame(data)
        
        # Check if the DataFrame is empty
        if df.empty:
            raise ValueError("The parsed DataFrame is empty. Please check the .fcs file.")
        
        return df
    except Exception as e:
        raise gr.Error(f"Error parsing .fcs file: {str(e)}")

def plot_fcs(data, x_axis, y_axis):
    try:
        # Plot the data
        plt.figure(figsize=(10, 6))
        plt.scatter(data[x_axis], data[y_axis], alpha=0.5)
        plt.xlabel(x_axis)
        plt.ylabel(y_axis)
        plt.title(f"{x_axis} vs {y_axis}")
        return plt.gcf()  # Return the current figure
    except Exception as e:
        raise gr.Error(f"Error plotting data: {str(e)}")

def process_fcs(file):
    try:
        # Parse the FCS file and get the DataFrame
        df = parse_fcs(file)
        
        # Get the column names for x and y axis choices
        columns = df.columns.tolist()
        
        # Check if columns are available
        if not columns:
            raise ValueError("No columns found in the DataFrame.")
        
        # Create a Gradio interface for selecting x and y axis
        with gr.Blocks() as demo:
            with gr.Row():
                x_axis = gr.Dropdown(choices=columns, label="Select X-axis")
                y_axis = gr.Dropdown(choices=columns, label="Select Y-axis")
            plot = gr.Plot()
            
            # Update the plot when the dropdowns change
            x_axis.change(lambda x, y, df: plot_fcs(df, x, y), inputs=[x_axis, y_axis, gr.State(df)], outputs=plot)
            y_axis.change(lambda x, y, df: plot_fcs(df, x, y), inputs=[x_axis, y_axis, gr.State(df)], outputs=plot)
        
        return demo
    except Exception as e:
        raise gr.Error(f"Error processing .fcs file: {str(e)}")

# Gradio interface for uploading the FCS file
with gr.Blocks() as demo:
    file = gr.File(label="Upload FCS File", file_types=[".fcs"])
    output = gr.Interface(fn=process_fcs, inputs=file, outputs="component", live=True)

demo.launch()