File size: 3,544 Bytes
c6379ae
6a1993f
9be415c
6a1993f
 
 
e760057
6a1993f
 
 
f305b38
6a1993f
 
 
 
 
 
 
 
602a264
6a1993f
 
 
 
 
 
e760057
6a1993f
 
 
 
 
b13bccc
e760057
6a1993f
f305b38
6a1993f
 
 
ee16303
6a1993f
 
 
 
ee16303
6a1993f
 
 
e760057
6a1993f
 
 
 
 
 
9239ab3
6a1993f
 
e760057
6a1993f
 
 
e760057
6a1993f
 
 
e760057
6a1993f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9239ab3
f305b38
b13bccc
6a1993f
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
import gradio as gr
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from fcsparser import parse
from matplotlib.path import Path

def parse_fcs(file):
    _, data = parse(file.name, reformat_meta=True)
    return data

def update_params(data):
    if data is None: 
        return [gr.Dropdown.update(choices=[])] * 2
    return [gr.Dropdown.update(choices=data.columns.tolist())] * 2

def create_plot(data, x_param, y_param):
    if data is None or not x_param or not y_param:
        return None
    
    fig = go.Figure(data=go.Scattergl(
        x=data[x_param],
        y=data[y_param],
        mode='markers',
        marker=dict(size=2, opacity=0.5)
    ))
    
    fig.update_layout(
        title=f"{x_param} vs {y_param}",
        dragmode='drawclosedpath',
        width=800,
        height=600
    )
    
    return fig

def apply_gate(data, x_param, y_param, fig):
    if data is None or fig is None:
        return "Önce veri yükleyin ve grafik oluşturun", None
    
    # Plotly şekil verisini çıkar
    shapes = fig.get('layout', {}).get('shapes', [])
    if not shapes:
        return "Lütfen önce polygon çizin", None
    
    # Son çizilen şekli al
    last_shape = shapes[-1]
    path = last_shape.get('path', '')
    
    # Path verisini işle (M=move, L=line, Z=close)
    vertices = []
    for part in path.split(' '):
        if part.startswith('M') or part.startswith('L'):
            x, y = part[1:].split(',')
            vertices.append((float(x), float(y)))
    
    if len(vertices) < 3:
        return "Geçerli bir polygon çizilmedi", None
    
    # Matplotlib Path oluştur
    polygon = Path(vertices)
    points = np.vstack([data[x_param], data[y_param]]).T
    
    # İçerde kalan noktaları bul
    mask = polygon.contains_points(points)
    gated_data = data[mask]
    
    # Sonuçları hazırla
    stats = f"""
    Toplam Hücre: {len(data):,}
    Geçitlenen Hücre: {len(gated_data):,} (%{len(gated_data)/len(data)*100:.1f})
    İlk 10 Hücre İndeksi: {gated_data.index[:10].tolist()}
    """
    
    return stats, gated_data

with gr.Blocks(title="FCS Analiz Uygulaması") as demo:
    gr.Markdown("## FCS Dosya Analizi ve Hücre Geçitleme")
    
    with gr.Row():
        with gr.Column():
            file_input = gr.File(label="FCS Dosyası Yükle", type="file")
            x_param = gr.Dropdown(label="X Parametresi")
            y_param = gr.Dropdown(label="Y Parametresi")
            plot_btn = gr.Button("Grafik Oluştur")
            
        with gr.Column():
            plot_output = gr.Plot(label="Dağılım Grafiği")
            gate_btn = gr.Button("Geçitlemeyi Uygula")
            result_output = gr.Textbox(label="Sonuçlar")
            data_output = gr.DataFrame(label="Geçitlenen Veriler", visible=False)

    # State management
    data_state = gr.State()
    fig_state = gr.State()

    # Event handlers
    file_input.change(
        fn=parse_fcs,
        inputs=file_input,
        outputs=data_state
    ).then(
        fn=update_params,
        inputs=data_state,
        outputs=[x_param, y_param]
    )

    plot_btn.click(
        fn=create_plot,
        inputs=[data_state, x_param, y_param],
        outputs=[plot_output]
    ).then(
        lambda fig: fig,
        inputs=plot_output,
        outputs=fig_state
    )

    gate_btn.click(
        fn=apply_gate,
        inputs=[data_state, x_param, y_param, fig_state],
        outputs=[result_output, data_output]
    )

if __name__ == "__main__":
    demo.launch()