File size: 17,323 Bytes
5263bd3
 
 
 
3b775b7
 
5263bd3
 
4a7c026
 
40fe6da
afbf1c6
8c49ca8
 
 
5263bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5263bd3
8c49ca8
 
 
870813f
3b775b7
870813f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c49ca8
3b775b7
8c49ca8
 
 
6c88c65
8c49ca8
 
 
 
 
 
 
3b775b7
8c49ca8
 
 
 
3b775b7
8c49ca8
3b775b7
8c49ca8
3b775b7
d192dd4
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
7e19501
3b775b7
7e19501
3b775b7
 
7e19501
3b775b7
 
 
 
 
 
7e19501
6c88c65
8c49ca8
 
3b775b7
7e19501
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e7de0c
3b775b7
 
8c49ca8
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e7de0c
 
3b775b7
 
 
8c49ca8
6c88c65
3b775b7
6c88c65
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c49ca8
 
3b775b7
8c49ca8
3b775b7
8c49ca8
3b775b7
 
 
 
 
 
 
 
 
 
8c49ca8
3b775b7
 
 
 
 
 
 
 
 
 
7e19501
 
 
 
3b775b7
 
 
 
 
 
 
 
 
d192dd4
3b775b7
0e7de0c
3b775b7
 
 
 
 
 
 
 
 
0e7de0c
3b775b7
6a3b036
7e19501
723da6d
3b775b7
 
 
 
 
9d48283
d192dd4
3b775b7
0d2d632
3b775b7
 
d192dd4
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
723da6d
5263bd3
8c49ca8
 
 
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d192dd4
3b775b7
0e7de0c
3b775b7
0e7de0c
3b775b7
 
 
 
 
 
 
 
 
 
 
 
 
 
0d2d632
 
723da6d
3b775b7
 
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import gradio as gr
import torch
import joblib
import numpy as np
import shap
import random
from itertools import product
import torch.nn as nn
import matplotlib.pyplot as plt
import io
from PIL import Image

###############################################################################
# Model Definition
###############################################################################
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 gradient-based feature importance, specifically for the 
        'human' class (index=1) by computing gradient of that probability wrt x.
        """
        x.requires_grad_(True)
        output = self.network(x)
        probs = torch.softmax(output, dim=1)
        
        # Probability of 'human' class (index=1)
        human_prob = probs[..., 1]
        if x.grad is not None:
            x.grad.zero_()
        human_prob.backward()
        importance = x.grad  # shape: (batch_size, n_features)
        
        return importance, float(human_prob)

###############################################################################
# Utility Functions
###############################################################################
def parse_fasta(text):
    """Parses text input in FASTA format into a list of (header, sequence)."""
    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 sequence_to_kmer_vector(sequence: str, k: int = 4) -> np.ndarray:
    """Convert a single nucleotide sequence to a 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  # normalize frequencies

    return vec

###############################################################################
# Additional Plots
###############################################################################
def create_probability_bar_plot(prob_human, prob_nonhuman):
    """
    Simple bar plot comparing human vs. non-human probabilities.
    """
    labels = ["Non-human", "Human"]
    probs = [prob_nonhuman, prob_human]
    colors = ["red", "green"]

    fig, ax = plt.subplots(figsize=(6, 4))
    ax.bar(labels, probs, color=colors, alpha=0.7)
    ax.set_ylim(0, 1)
    for i, v in enumerate(probs):
        ax.text(i, v+0.02, f"{v:.3f}", ha='center', color='black', fontsize=11)

    ax.set_title("Predicted Probabilities")
    ax.set_ylabel("Probability")
    plt.tight_layout()
    return fig

def create_frequency_sigma_plot(important_kmers, title):
    """
    Creates a bar plot of the top k-mers (by importance) showing 
    frequency (%) and σ from mean.
    """
    # Sort by absolute impact
    sorted_kmers = sorted(important_kmers, key=lambda x: x['impact'], reverse=True)
    kmers = [k["kmer"] for k in sorted_kmers]
    frequencies = [k["occurrence"] for k in sorted_kmers]  # in %
    sigmas = [k["sigma"] for k in sorted_kmers]
    directions = [k["direction"] for k in sorted_kmers]
    
    x = np.arange(len(kmers))
    width = 0.4

    fig, ax_bar = plt.subplots(figsize=(10, 5))

    # Bar for frequency
    bars_freq = ax_bar.bar(
        x - width/2, frequencies, width, alpha=0.7,
        color=["green" if d=="human" else "red" for d in directions],
        label="Frequency (%)"
    )
    ax_bar.set_ylabel("Frequency (%)")
    ax_bar.set_ylim(0, max(frequencies) * 1.2 if len(frequencies) > 0 else 1)

    # Twin axis for σ
    ax_bar_twin = ax_bar.twinx()
    bars_sigma = ax_bar_twin.bar(
        x + width/2, sigmas, width, alpha=0.5, color="gray", label="σ from Mean"
    )
    ax_bar_twin.set_ylabel("Standard Deviations (σ)")

    ax_bar.set_title(f"Frequency & σ from Mean for Top k-mers — {title}")
    ax_bar.set_xticks(x)
    ax_bar.set_xticklabels(kmers, rotation=45, ha='right')

    # Combined legend
    lines1, labels1 = ax_bar.get_legend_handles_labels()
    lines2, labels2 = ax_bar_twin.get_legend_handles_labels()
    ax_bar.legend(lines1 + lines2, labels1 + labels2, loc="upper right")

    plt.tight_layout()
    return fig

def create_importance_bar_plot(important_kmers, title):
    """
    Create a simple bar chart showing the absolute gradient magnitude 
    for the top k-mers, sorted descending.
    """
    sorted_kmers = sorted(important_kmers, key=lambda x: x['impact'], reverse=True)
    kmers = [k['kmer'] for k in sorted_kmers]
    impacts = [k['impact'] for k in sorted_kmers]
    directions = [k["direction"] for k in sorted_kmers]

    x = np.arange(len(kmers))

    fig, ax = plt.subplots(figsize=(10, 5))
    bar_colors = ["green" if d=="human" else "red" for d in directions]

    ax.bar(x, impacts, color=bar_colors, alpha=0.7, edgecolor='black')
    ax.set_xticks(x)
    ax.set_xticklabels(kmers, rotation=45, ha='right')
    ax.set_title(f"Absolute Feature Importance (Top k-mers) — {title}")
    ax.set_ylabel("Gradient Magnitude")
    ax.grid(axis="y", alpha=0.3)

    plt.tight_layout()
    return fig

###############################################################################
# SHAP Beeswarm
###############################################################################
def create_shap_beeswarm_plot(
    model, 
    input_vector: np.ndarray, 
    background_data: np.ndarray, 
    feature_names: list
):
    """
    Creates a SHAP beeswarm plot using KernelExplainer for the given model and data.
    
    Parameters
    ----------
    model : nn.Module
        Trained PyTorch model (binary classifier).
    input_vector : np.ndarray
        The 1-sample input (or multiple samples) we want SHAP values for.
    background_data : np.ndarray
        Background samples for KernelExplainer. Should have shape (N, #features).
    feature_names : list
        Names for each feature (k-mers).
    
    Returns
    -------
    fig : matplotlib Figure
        Beeswarm plot figure.
    """

    # We'll define a prediction function that shap can call
    # The model outputs logits for shape [N, 2]
    # We want the raw outputs for each class. SHAP will handle the link function if needed.
    def predict_fn(data):
        """
        data: shape (N, #features)
        returns: shape (N, 2) for 2-class logits
        """
        with torch.no_grad():
            x = torch.FloatTensor(data)
            logits = model(x)
            return logits.detach().cpu().numpy()

    # Create KernelExplainer
    explainer = shap.KernelExplainer(
        model=predict_fn,
        data=background_data
    )

    # Compute SHAP values
    # For a 2-class model, shap_values is a list of length 2 => [class0 array, class1 array]
    # Each array is shape (N, #features).
    shap_values = explainer.shap_values(input_vector)

    # We’ll produce a beeswarm for the 'human' class (class index=1).
    # If we have only 1 sample, the beeswarm won't be too interesting, but let's do it anyway.
    class_idx = 1  # 'human'
    
    # If we only have one sample, place it in an array for shap summary plotting:
    # We can do shap_values[class_idx].shape => (1, #features) for a single sample
    # Beeswarm typically expects multiple samples. We'll plot anyway.
    shap.plots.beeswarm(
        shap_values[class_idx],
        feature_names=feature_names,
        show=False
    )

    fig = plt.gcf()
    fig.set_size_inches(8, 6)
    plt.title("SHAP Beeswarm Plot (Class: Human)")

    plt.tight_layout()
    return fig

###############################################################################
# Prediction Function
###############################################################################
def predict(file_obj):
    """
    Main function for Gradio:
      1. Reads the uploaded FASTA file or text.
      2. Loads the model and scaler.
      3. Generates predictions, probabilities, and top k-mers.
      4. Creates multiple outputs:
         - Text summary (Markdown)
         - Probability Bar Plot
         - SHAP Beeswarm Plot
         - Frequency & σ Plot
         - Absolute Feature Importance Bar Plot
    """
    # 0. Basic file read
    if file_obj is None:
        return (
            "Please upload a FASTA file.",
            None,
            None,
            None,
            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,
            None,
            None,
            None
        )

    # 1. Parse FASTA
    sequences = parse_fasta(text)
    if len(sequences) == 0:
        return (
            "No valid FASTA sequences found. Please check your input.",
            None,
            None,
            None,
            None
        )
    header, seq = sequences[0]  # We'll classify only the first sequence

    # 2. Prepare model, scaler, and input
    k = 4
    device = "cuda" if torch.cuda.is_available() else "cpu"
    try:
        raw_freq_vector = sequence_to_kmer_vector(seq, k=k)

        # Load model & scaler
        model = VirusClassifier(input_shape=4**k).to(device)
        state_dict = torch.load("model.pt", map_location=device)
        model.load_state_dict(state_dict)
        scaler = joblib.load("scaler.pkl")
        model.eval()

        scaled_vector = scaler.transform(raw_freq_vector.reshape(1, -1))
        X_tensor = torch.FloatTensor(scaled_vector).to(device)

        # 3. Predict
        with torch.no_grad():
            logits = model(X_tensor)
            probs = torch.softmax(logits, dim=1)
        human_prob = float(probs[0][1])
        non_human_prob = float(probs[0][0])
        pred_label = "human" if human_prob >= non_human_prob else "non-human"
        confidence = float(max(probs[0]))

        # 4. Gradient-based feature importance
        importance, hum_prob_grad = model.get_feature_importance(X_tensor)
        importances = importance[0].cpu().numpy()  # shape: (#features,)
        abs_importances = np.abs(importances)

        # 5. Gather k-mer strings
        kmers_list = [''.join(p) for p in product("ACGT", repeat=k)]
        # top 10 by absolute importance
        top_k = 10
        top_idxs = np.argsort(abs_importances)[-top_k:][::-1]
        important_kmers = []
        for idx in top_idxs:
            direction = "human" if importances[idx] > 0 else "non-human"
            freq_percent = float(raw_freq_vector[idx] * 100.0)
            sigma_val = float(scaled_vector[0][idx])  # scaled / standardized val
            important_kmers.append({
                'kmer': kmers_list[idx],
                'idx': idx,
                'impact': abs_importances[idx],
                'direction': direction,
                'occurrence': freq_percent,
                'sigma': sigma_val
            })

        # 6. Generate text summary
        text_summary = (
            f"**Sequence Header**: {header}\n\n"
            f"**Predicted Label**: {pred_label}\n"
            f"**Confidence**: {confidence:.4f}\n\n"
            f"**Human Probability**: {human_prob:.4f}\n"
            f"**Non-human Probability**: {non_human_prob:.4f}\n\n"
            "### Most Influential k-mers:\n"
        )
        for km in important_kmers:
            direction_text = f"(pushes toward {km['direction']})"
            freq_text = f"{km['occurrence']:.2f}%"
            sigma_text = (
                f"{abs(km['sigma']):.2f}σ "
                + ("above" if km['sigma'] > 0 else "below")
                + " mean"
            )
            text_summary += (
                f"- **{km['kmer']}**: impact={km['impact']:.4f}, {direction_text}, "
                f"occurrence={freq_text}, ({sigma_text})\n"
            )

        # 7. Probability Bar Plot
        fig_prob = create_probability_bar_plot(human_prob, non_human_prob)
        buf_prob = io.BytesIO()
        fig_prob.savefig(buf_prob, format='png', bbox_inches='tight', dpi=120)
        buf_prob.seek(0)
        prob_img = Image.open(buf_prob)
        plt.close(fig_prob)

        # 8. SHAP Beeswarm Plot
        # We need some background data for KernelExplainer. Let's create a small random sample
        # or sample from the scaled_vector itself in a repeated manner. Real usage: choose a valid background set.
        background_size = 5  # keep small for speed
        # We'll pick random sequences from normal(0,1) or from scaled_vector repeated
        background_data = []
        for _ in range(background_size):
            # Option A: random small variations around scaled_vector
            # new_sample = scaled_vector[0] + np.random.normal(0, 0.5, size=scaled_vector.shape[1])
            # Option B: just clone the same scaled vector multiple times
            new_sample = scaled_vector[0]
            background_data.append(new_sample)
        background_data = np.stack(background_data, axis=0)  # shape (5, #features)

        fig_bee = create_shap_beeswarm_plot(
            model=model,
            input_vector=scaled_vector,      # our single sample
            background_data=background_data, # background for KernelExplainer
            feature_names=kmers_list
        )
        buf_bee = io.BytesIO()
        fig_bee.savefig(buf_bee, format='png', bbox_inches='tight', dpi=120)
        buf_bee.seek(0)
        bee_img = Image.open(buf_bee)
        plt.close(fig_bee)

        # 9. Frequency & σ Plot
        fig_freq = create_frequency_sigma_plot(important_kmers, header)
        buf_freq = io.BytesIO()
        fig_freq.savefig(buf_freq, format='png', bbox_inches='tight', dpi=120)
        buf_freq.seek(0)
        freq_img = Image.open(buf_freq)
        plt.close(fig_freq)

        # 10. Absolute Feature Importance Bar Plot
        fig_imp = create_importance_bar_plot(important_kmers, header)
        buf_imp = io.BytesIO()
        fig_imp.savefig(buf_imp, format='png', bbox_inches='tight', dpi=120)
        buf_imp.seek(0)
        imp_img = Image.open(buf_imp)
        plt.close(fig_imp)

        return text_summary, prob_img, bee_img, freq_img, imp_img
    
    except Exception as e:
        return (
            f"Error during prediction or visualization: {str(e)}",
            None,
            None,
            None,
            None
        )


###############################################################################
# Gradio Interface
###############################################################################
with gr.Blocks(title="Advanced Virus Host Classifier with SHAP Beeswarm") as demo:
    gr.Markdown(
        """
        # Advanced Virus Host Classifier (SHAP Beeswarm Edition)

        **Upload a FASTA file** containing a single nucleotide sequence. 
        The model will predict whether this sequence is **human** or **non-human**, 
        provide a confidence score, and highlight the most influential k-mers. 
        We also produce a **SHAP beeswarm** plot for the features.
        
        ---
        **Note**: Beeswarm plots are usually most insightful with multiple samples. 
        Here, we demonstrate usage with a single sample plus a small synthetic background.
        """
    )
    
    with gr.Row():
        file_in = gr.File(label="Upload FASTA", type="binary")
        btn = gr.Button("Run Prediction")

    # We will create multiple tabs for our outputs
    with gr.Tabs():
        with gr.Tab("Prediction Results"):
            md_out = gr.Markdown()
        with gr.Tab("Probability Plot"):
            prob_out = gr.Image()
        with gr.Tab("SHAP Beeswarm Plot"):
            bee_out = gr.Image()
        with gr.Tab("Frequency & σ Plot"):
            freq_out = gr.Image()
        with gr.Tab("Importance Bar Plot"):
            imp_out = gr.Image()

    # Link the button
    btn.click(
        fn=predict,
        inputs=[file_in],
        outputs=[md_out, prob_out, bee_out, freq_out, imp_out]
    )

if __name__ == "__main__":
    # By default, share=False. You can set share=True for external access.
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)