kedimestan commited on
Commit
f305b38
·
verified ·
1 Parent(s): 5748e11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -82
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
- # Use flowio to parse the FCS file
10
- fcs_data = flowio.FlowData(file.name)
11
-
12
- # Extract channel names and data
13
- channel_names = [channel['PnN'] for channel in fcs_data.channels]
14
- data = pd.DataFrame(fcs_data.events, columns=channel_names)
15
-
16
- return data, channel_names
17
-
18
- # Function to plot the data based on selected x and y parameters
19
- def plot_dot(file, x_param, y_param):
20
- if not file:
21
- return "Please upload an FCS file."
22
-
23
- try:
24
- data, columns = parse_fcs(file)
25
-
26
- if x_param not in columns or y_param not in columns:
27
- return "Invalid parameters selected. Please choose valid parameters from the dropdown."
28
-
29
- # Create the plot
30
- plt.figure(figsize=(8, 6))
31
- plt.scatter(data[x_param], data[y_param], s=1, alpha=0.5, color="blue")
32
- plt.xlabel(x_param)
33
- plt.ylabel(y_param)
34
- plt.title("Flow Cytometry Dot Plot")
35
- plt.grid(True)
36
-
37
- # Save plot to a BytesIO object
38
- buf = io.BytesIO()
39
- plt.savefig(buf, format="png")
40
- buf.seek(0)
41
- plt.close()
42
-
43
- # Save the plot as a temporary file for display
44
- temp_file = "/tmp/dot_plot.png"
45
- with open(temp_file, "wb") as f:
46
- f.write(buf.getbuffer())
47
- return temp_file
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()