File size: 9,384 Bytes
5263bd3
 
 
 
 
 
4a7c026
 
40fe6da
5263bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdd8a58
 
6c88c65
cdd8a58
 
40fe6da
cdd8a58
6c88c65
40fe6da
6c88c65
 
40fe6da
 
 
 
5263bd3
9d48283
 
870813f
9d48283
 
 
870813f
 
 
9d48283
 
 
 
 
 
 
5263bd3
870813f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c88c65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63d967d
 
4a7c026
870813f
723da6d
 
 
 
 
 
4a7c026
723da6d
6a3b036
cdd8a58
 
 
723da6d
 
6a3b036
9d48283
 
723da6d
 
 
4a7c026
723da6d
4a7c026
 
 
723da6d
 
4a7c026
 
 
 
 
 
6c88c65
17c9ecb
 
 
4a7c026
6c88c65
 
 
4a7c026
6c88c65
4a7c026
 
 
6c88c65
 
 
 
 
 
 
b0fba50
6c88c65
 
 
 
 
 
 
b0fba50
6c88c65
4a7c026
 
6c88c65
4a7c026
6c88c65
5263bd3
 
6c88c65
cdd8a58
2897f12
4a7c026
 
 
6c88c65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a7c026
723da6d
4a7c026
723da6d
4a7c026
5263bd3
 
870813f
723da6d
6c88c65
 
 
 
870813f
5263bd3
 
723da6d
5bf9386
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import gradio as gr
import torch
import joblib
import numpy as np
from itertools import product
import torch.nn as nn
import matplotlib.pyplot as plt
import io
from PIL import Image

class VirusClassifier(nn.Module):
    def __init__(self, input_shape: int):
        super(VirusClassifier, self).__init__()
        self.network = nn.Sequential(
            nn.Linear(input_shape, 64),
            nn.GELU(),
            nn.BatchNorm1d(64),
            nn.Dropout(0.3),
            nn.Linear(64, 32),
            nn.GELU(),
            nn.BatchNorm1d(32),
            nn.Dropout(0.3),
            nn.Linear(32, 32),
            nn.GELU(),
            nn.Linear(32, 2)
        )

    def forward(self, x):
        return self.network(x)
    
    def get_feature_importance(self, x):
        """Calculate feature importance using gradient-based method"""
        x.requires_grad_(True)
        output = self.network(x)
        probs = torch.softmax(output, dim=1)
        
        # Get importance for human class (index 1)
        human_prob = probs[..., 1]
        if x.grad is not None:
            x.grad.zero_()
        human_prob.backward()
        importance = x.grad
        
        return importance, float(human_prob)

def sequence_to_kmer_vector(sequence: str, k: int = 4) -> np.ndarray:
    """Convert sequence to k-mer frequency vector"""
    kmers = [''.join(p) for p in product("ACGT", repeat=k)]
    kmer_dict = {km: i for i, km in enumerate(kmers)}
    vec = np.zeros(len(kmers), dtype=np.float32)
    
    for i in range(len(sequence) - k + 1):
        kmer = sequence[i:i+k]
        if kmer in kmer_dict:
            vec[kmer_dict[kmer]] += 1

    total_kmers = len(sequence) - k + 1
    if total_kmers > 0:
        vec = vec / total_kmers

    return vec

def parse_fasta(text):
    sequences = []
    current_header = None
    current_sequence = []
    
    for line in text.split('\n'):
        line = line.strip()
        if not line:
            continue
        if line.startswith('>'):
            if current_header:
                sequences.append((current_header, ''.join(current_sequence)))
            current_header = line[1:]
            current_sequence = []
        else:
            current_sequence.append(line.upper())
    if current_header:
        sequences.append((current_header, ''.join(current_sequence)))
    return sequences

def create_visualization(important_kmers, human_prob, title):
    """Create a comprehensive visualization of k-mer impacts"""
    fig = plt.figure(figsize=(15, 10))
    
    # Create grid for subplots
    gs = plt.GridSpec(2, 1, height_ratios=[1.5, 1], hspace=0.3)
    
    # 1. Probability Step Plot
    ax1 = plt.subplot(gs[0])
    current_prob = 0.5
    steps = [('Start', current_prob, 0)]
    
    for kmer in important_kmers:
        change = kmer['impact'] * (-1 if kmer['direction'] == 'non-human' else 1)
        current_prob += change
        steps.append((kmer['kmer'], current_prob, change))
    
    x = range(len(steps))
    y = [step[1] for step in steps]
    
    # Plot steps
    ax1.step(x, y, 'b-', where='post', label='Probability', linewidth=2)
    ax1.plot(x, y, 'b.', markersize=10)
    
    # Add reference line
    ax1.axhline(y=0.5, color='r', linestyle='--', label='Neutral (0.5)')
    
    # Customize plot
    ax1.grid(True, linestyle='--', alpha=0.7)
    ax1.set_ylim(0, 1)
    ax1.set_ylabel('Human Probability')
    ax1.set_title(f'K-mer Contributions to Prediction (final prob: {human_prob:.3f})')
    
    # Add labels for each point
    for i, (kmer, prob, change) in enumerate(steps):
        # Add k-mer label
        ax1.annotate(kmer, 
                    (i, prob),
                    xytext=(0, 10 if i % 2 == 0 else -20),
                    textcoords='offset points',
                    ha='center',
                    rotation=45)
        
        # Add change value
        if i > 0:
            change_text = f'{change:+.3f}'
            color = 'green' if change > 0 else 'red'
            ax1.annotate(change_text,
                       (i, prob),
                       xytext=(0, -20 if i % 2 == 0 else 10),
                       textcoords='offset points',
                       ha='center',
                       color=color)
    
    ax1.legend()
    
    # 2. K-mer Frequency and Sigma Plot
    ax2 = plt.subplot(gs[1])
    
    # Prepare data
    kmers = [k['kmer'] for k in important_kmers]
    frequencies = [k['occurrence'] for k in important_kmers]
    sigmas = [k['sigma'] for k in important_kmers]
    colors = ['g' if k['direction'] == 'human' else 'r' for k in important_kmers]
    
    # Create bar plot for frequencies
    x = np.arange(len(kmers))
    width = 0.35
    
    ax2.bar(x - width/2, frequencies, width, label='Frequency (%)', color=colors, alpha=0.6)
    ax2_twin = ax2.twinx()
    ax2_twin.bar(x + width/2, sigmas, width, label='σ from mean', color=[c if s > 0 else 'gray' for c, s in zip(colors, sigmas)], alpha=0.3)
    
    # Customize plot
    ax2.set_xticks(x)
    ax2.set_xticklabels(kmers, rotation=45)
    ax2.set_ylabel('Frequency (%)')
    ax2_twin.set_ylabel('Standard Deviations (σ) from Mean')
    ax2.set_title('K-mer Frequencies and Statistical Significance')
    
    # Add legends
    lines1, labels1 = ax2.get_legend_handles_labels()
    lines2, labels2 = ax2_twin.get_legend_handles_labels()
    ax2.legend(lines1 + lines2, labels1 + labels2, loc='upper right')
    
    plt.tight_layout()
    return fig

def predict(file_obj):
    if file_obj is None:
        return "Please upload a FASTA file", None
    
    try:
        if isinstance(file_obj, str):
            text = file_obj
        else:
            text = file_obj.decode('utf-8')
    except Exception as e:
        return f"Error reading file: {str(e)}", None

    k = 4
    kmers = [''.join(p) for p in product("ACGT", repeat=k)]
    kmer_dict = {km: i for i, km in enumerate(kmers)}
    
    try:
        device = 'cuda' if torch.cuda.is_available() else 'cpu'
        model = VirusClassifier(256).to(device)
        state_dict = torch.load('model.pt', map_location=device)
        model.load_state_dict(state_dict)
        scaler = joblib.load('scaler.pkl')
        model.eval()
    except Exception as e:
        return f"Error loading model: {str(e)}", None

    results_text = ""
    plot_image = None
    
    try:
        sequences = parse_fasta(text)
        header, seq = sequences[0]
        
        raw_freq_vector = sequence_to_kmer_vector(seq)
        kmer_vector = scaler.transform(raw_freq_vector.reshape(1, -1))
        X_tensor = torch.FloatTensor(kmer_vector).to(device)
        
        # Get model predictions
        with torch.no_grad():
            output = model(X_tensor)
            probs = torch.softmax(output, dim=1)
        
        # Get feature importance
        importance, _ = model.get_feature_importance(X_tensor)
        kmer_importance = importance[0].cpu().numpy()
        
        # Get top k-mers
        top_k = 10
        top_indices = np.argsort(np.abs(kmer_importance))[-top_k:][::-1]
        
        important_kmers = []
        for idx in top_indices:
            kmer = list(kmer_dict.keys())[list(kmer_dict.values()).index(idx)]
            imp = float(abs(kmer_importance[idx]))
            direction = 'human' if kmer_importance[idx] > 0 else 'non-human'
            freq = float(raw_freq_vector[idx] * 100)  # Convert to percentage
            sigma = float(kmer_vector[0][idx])
            
            important_kmers.append({
                'kmer': kmer,
                'impact': imp,
                'direction': direction,
                'occurrence': freq,
                'sigma': sigma
            })
        
        # Generate text results
        pred_class = 1 if probs[0][1] > probs[0][0] else 0
        pred_label = 'human' if pred_class == 1 else 'non-human'
        human_prob = float(probs[0][1])
        
        results_text = f"""Sequence: {header}
Prediction: {pred_label}
Confidence: {float(max(probs[0])):0.4f}
Human probability: {human_prob:0.4f}
Non-human probability: {float(probs[0][0]):0.4f}
Most influential k-mers (ranked by importance):"""
        
        for kmer in important_kmers:
            results_text += f"\n  {kmer['kmer']}: "
            results_text += f"pushes toward {kmer['direction']} (impact={kmer['impact']:.4f}), "
            results_text += f"occurrence={kmer['occurrence']:.2f}% of sequence "
            results_text += f"(appears {abs(kmer['sigma']):.2f}σ "
            results_text += "more" if kmer['sigma'] > 0 else "less"
            results_text += " than average)"
        
        # Create visualization
        fig = create_visualization(important_kmers, human_prob, header)
        
        # Save plot
        buf = io.BytesIO()
        fig.savefig(buf, format='png', bbox_inches='tight', dpi=300)
        buf.seek(0)
        plot_image = Image.open(buf)
        plt.close(fig)
        
    except Exception as e:
        return f"Error processing sequences: {str(e)}", None

    return results_text, plot_image

iface = gr.Interface(
    fn=predict,
    inputs=gr.File(label="Upload FASTA file", type="binary"),
    outputs=[
        gr.Textbox(label="Results"),
        gr.Image(label="K-mer Analysis Visualization")
    ],
    title="Virus Host Classifier"
)

if __name__ == "__main__":
    iface.launch(share=True)