Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
-
import flowio
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
-
import io
|
5 |
import pandas as pd
|
|
|
|
|
6 |
|
7 |
-
# Function to parse FCS file and extract data and metadata
|
8 |
def parse_fcs(file):
|
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 |
-
except Exception as e:
|
49 |
-
return f"Error: {e}"
|
50 |
-
|
51 |
-
# Function to dynamically update parameter options after file upload
|
52 |
-
def get_parameters(file):
|
53 |
-
if not file:
|
54 |
-
# Return empty dropdown updates if no file is uploaded
|
55 |
-
return gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
|
56 |
-
|
57 |
-
try:
|
58 |
-
_, columns = parse_fcs(file)
|
59 |
-
return gr.Dropdown.update(choices=columns), gr.Dropdown.update(choices=columns)
|
60 |
-
except Exception as e:
|
61 |
-
# Log the error and return empty dropdowns
|
62 |
-
print(f"Error parsing FCS file: {e}")
|
63 |
-
return gr.Dropdown.update(choices=[], value=None), gr.Dropdown.update(choices=[], value=None)
|
64 |
-
|
65 |
-
# Create Gradio interface
|
66 |
-
with gr.Blocks() as app:
|
67 |
-
gr.Markdown("# Flow Cytometry Dot Plot Viewer")
|
68 |
-
|
69 |
-
with gr.Row():
|
70 |
-
fcs_file = gr.File(label="Upload FCS File", file_types=[".fcs"])
|
71 |
-
|
72 |
-
with gr.Row():
|
73 |
-
x_param = gr.Dropdown(label="X Parameter", choices=[], interactive=True)
|
74 |
-
y_param = gr.Dropdown(label="Y Parameter", choices=[], interactive=True)
|
75 |
-
|
76 |
-
plot_btn = gr.Button("Generate Plot")
|
77 |
-
plot_output = gr.Image(type="filepath", label="Dot Plot")
|
78 |
-
|
79 |
-
# Update parameter dropdowns on file upload
|
80 |
-
fcs_file.change(get_parameters, inputs=[fcs_file], outputs=[x_param, y_param])
|
81 |
-
|
82 |
-
# Generate plot on button click
|
83 |
-
plot_btn.click(plot_dot, inputs=[fcs_file, x_param, y_param], outputs=plot_output)
|
84 |
-
|
85 |
-
# Run the app
|
86 |
-
app.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import fcsparser
|
5 |
|
|
|
6 |
def parse_fcs(file):
|
7 |
+
# Parse the FCS file
|
8 |
+
meta, data = fcsparser.parse(file.name)
|
9 |
+
return pd.DataFrame(data)
|
10 |
+
|
11 |
+
def plot_fcs(data, x_axis, y_axis):
|
12 |
+
# Plot the data
|
13 |
+
plt.figure(figsize=(10, 6))
|
14 |
+
plt.scatter(data[x_axis], data[y_axis], alpha=0.5)
|
15 |
+
plt.xlabel(x_axis)
|
16 |
+
plt.ylabel(y_axis)
|
17 |
+
plt.title(f"{x_axis} vs {y_axis}")
|
18 |
+
return plt
|
19 |
+
|
20 |
+
def process_fcs(file):
|
21 |
+
# Parse the FCS file and get the DataFrame
|
22 |
+
df = parse_fcs(file)
|
23 |
+
|
24 |
+
# Get the column names for x and y axis choices
|
25 |
+
columns = df.columns.tolist()
|
26 |
+
|
27 |
+
# Create a Gradio interface for selecting x and y axis
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
with gr.Row():
|
30 |
+
x_axis = gr.Dropdown(choices=columns, label="Select X-axis")
|
31 |
+
y_axis = gr.Dropdown(choices=columns, label="Select Y-axis")
|
32 |
+
plot = gr.Plot()
|
33 |
+
|
34 |
+
# Update the plot when the dropdowns change
|
35 |
+
x_axis.change(plot_fcs, inputs=[gr.State(df), x_axis, y_axis], outputs=plot)
|
36 |
+
y_axis.change(plot_fcs, inputs=[gr.State(df), x_axis, y_axis], outputs=plot)
|
37 |
+
|
38 |
+
return demo
|
39 |
+
|
40 |
+
# Gradio interface for uploading the FCS file
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
file = gr.File(label="Upload FCS File", file_types=[".fcs"])
|
43 |
+
output = gr.Interface(fn=process_fcs, inputs=file, outputs="component", live=True)
|
44 |
+
|
45 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|