File size: 9,860 Bytes
5263bd3
 
 
 
f1d4be6
5263bd3
4a7c026
 
40fe6da
a6886ca
5263bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5edb58
f1d4be6
7e92f7c
870813f
 
 
 
f1d4be6
870813f
 
 
 
 
 
 
 
 
 
 
 
 
 
a6886ca
7e92f7c
a6886ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef80028
a6886ca
7e92f7c
 
a6886ca
f1d4be6
 
7e92f7c
ef80028
7e92f7c
 
ef80028
7e92f7c
ef80028
7e92f7c
ef80028
7e92f7c
 
 
 
 
 
 
ef80028
 
a6886ca
7e92f7c
 
 
 
f1d4be6
ef80028
 
 
 
7e19501
7e92f7c
f1d4be6
ef80028
 
7e92f7c
ef80028
7e92f7c
f1d4be6
7e92f7c
8c49ca8
7e92f7c
a6886ca
7e92f7c
 
a6886ca
7e92f7c
 
3b775b7
7e92f7c
 
 
 
 
 
 
a6886ca
7e92f7c
 
ef80028
7e92f7c
 
 
555d484
7e92f7c
 
555d484
7e92f7c
 
 
ef80028
7e92f7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5edb58
7e92f7c
555d484
f1d4be6
 
ef80028
7e92f7c
ef80028
f1d4be6
 
ef80028
f1d4be6
0681a74
 
f1d4be6
7e92f7c
ef80028
7e92f7c
f1d4be6
 
 
ef80028
7e92f7c
ef80028
f1d4be6
 
7e92f7c
ef80028
723da6d
ef80028
555d484
a6886ca
b5edb58
ef80028
f1d4be6
ef80028
 
 
 
f1d4be6
7e92f7c
 
ef80028
7e92f7c
ef80028
 
7e92f7c
 
 
 
ef80028
a6886ca
7e92f7c
 
 
 
 
 
ef80028
 
f1d4be6
 
ef80028
f1d4be6
ef80028
f1d4be6
ef80028
 
7e92f7c
ef80028
 
 
 
 
 
f1d4be6
 
ef80028
 
 
7e92f7c
ef80028
 
 
 
 
 
8f84058
7e92f7c
ef80028
 
 
 
 
 
 
 
7e92f7c
ef80028
 
 
 
 
 
 
 
7e92f7c
 
ef80028
 
 
 
7e92f7c
ef80028
 
 
7e92f7c
 
 
 
 
 
 
ef80028
0d2d632
723da6d
ef80028
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
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 parse_fasta(text):
    """Parse FASTA formatted text into a list of (header, sequence)."""
    sequences = []
    current_header = None
    current_sequence = []
    
    for line in text.strip().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 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

    return vec

def calculate_shap_values(model, x_tensor):
    """
    Calculate SHAP values using a simple ablation approach.
    Returns shap values and model prediction.
    """
    model.eval()
    with torch.no_grad():
        # Get baseline prediction
        baseline_output = model(x_tensor)
        baseline_probs = torch.softmax(baseline_output, dim=1)
        baseline_prob = baseline_probs[0, 1].item()  # Probability of human class
        
        # Calculate impact of zeroing each feature
        shap_values = []
        x_zeroed = x_tensor.clone()
        for i in range(x_tensor.shape[1]):
            x_zeroed[0, i] = 0
            output = model(x_zeroed)
            probs = torch.softmax(output, dim=1)
            prob = probs[0, 1].item()
            impact = baseline_prob - prob  # How much removing the feature changed the prediction
            shap_values.append(impact)
            x_zeroed[0, i] = x_tensor[0, i]  # Restore the original value
            
    return np.array(shap_values), baseline_prob

def create_importance_bar_plot(shap_values, kmers, top_k=10):
    """Create a bar plot of the most important k-mers."""
    plt.rcParams.update({'font.size': 10})
    plt.figure(figsize=(10, 6))
    
    # Sort by absolute importance
    indices = np.argsort(np.abs(shap_values))[-top_k:]
    values = shap_values[indices]
    features = [kmers[i] for i in indices]
    
    colors = ['#ff9999' if v > 0 else '#99ccff' for v in values]
    
    plt.barh(range(len(values)), values, color=colors)
    plt.yticks(range(len(values)), features)
    plt.xlabel('SHAP value (impact on model output)')
    plt.title(f'Top {top_k} Most Influential k-mers')
    plt.gca().invert_yaxis()  # Most important at top
    
    return plt.gcf()

def visualize_sequence_impacts(sequence, kmers, shap_values, base_prob):
    """
    Create a SHAP-style visualization of sequence impacts.
    Shows each k-mer's contribution in context.
    """
    k = 4  # k-mer size
    kmer_dict = {km: i for i, km in enumerate(kmers)}
    
    # Find all k-mers and their impacts
    kmer_impacts = []
    for i in range(len(sequence) - k + 1):
        kmer = sequence[i:i+k]
        if kmer in kmer_dict:
            impact = shap_values[kmer_dict[kmer]]
            kmer_impacts.append((i, kmer, impact))
    
    # Sort by absolute impact
    kmer_impacts.sort(key=lambda x: abs(x[2]), reverse=True)
    
    # Create the plot
    fig = plt.figure(figsize=(20, max(10, len(kmer_impacts[:30])*0.3)))
    ax = plt.gca()
    
    # Add title and base value
    plt.text(0.01, 1.02, f"base value = {base_prob:.3f}", transform=ax.transAxes, fontsize=12)
    
    # Plot k-mers
    y_position = 1
    sequence_length = len(sequence)
    
    for pos, kmer, impact in kmer_impacts[:30]:  # Show top 30 most impactful k-mers
        # Show sequence with highlighted k-mer
        pre_sequence = sequence[:pos]
        post_sequence = sequence[pos+k:]
        
        # Choose color based on impact
        color = '#ffcccb' if impact > 0 else '#cce0ff'  # Light red or light blue
        arrow = '↑' if impact > 0 else '↓'
        
        # Calculate text positions
        plt.text(0.01, y_position, pre_sequence, fontsize=10)
        plt.text(0.01 + len(pre_sequence)/(sequence_length*1.5), y_position, 
                kmer, fontsize=10, bbox=dict(facecolor=color, alpha=0.3, pad=2))
        plt.text(0.01 + (len(pre_sequence) + len(kmer))/(sequence_length*1.5), 
                y_position, post_sequence, fontsize=10)
        
        # Add impact value
        plt.text(0.8, y_position, f"{arrow} {impact:+.3f}", fontsize=10)
        
        y_position -= 0.03
    
    plt.axis('off')
    plt.tight_layout()
    return fig

def predict(file_obj, top_kmers=10, fasta_text=""):
    """Main prediction function for Gradio interface."""
    # Handle input
    if fasta_text.strip():
        text = fasta_text.strip()
    elif file_obj is not None:
        try:
            with open(file_obj, 'r') as f:
                text = f.read()
        except Exception as e:
            return f"Error reading file: {str(e)}", None, None
    else:
        return "Please provide a FASTA sequence.", None, None

    # Parse FASTA
    sequences = parse_fasta(text)
    if not sequences:
        return "No valid FASTA sequences found.", None, None
    
    header, seq = sequences[0]

    # Load model and process sequence
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    try:
        model = VirusClassifier(256).to(device)
        model.load_state_dict(torch.load('model.pt', map_location=device, weights_only=True))
        scaler = joblib.load('scaler.pkl')
    except Exception as e:
        return f"Error loading model: {str(e)}", None, None

    # Generate features
    freq_vector = sequence_to_kmer_vector(seq)
    scaled_vector = scaler.transform(freq_vector.reshape(1, -1))
    x_tensor = torch.FloatTensor(scaled_vector).to(device)

    # Calculate SHAP values and get prediction
    shap_values, prob_human = calculate_shap_values(model, x_tensor)
    
    # Generate result text
    results = [
        f"Sequence: {header}",
        f"Prediction: {'Human' if prob_human > 0.5 else 'Non-human'} Origin",
        f"Confidence: {max(prob_human, 1-prob_human):.3f}",
        f"Human Probability: {prob_human:.3f}",
        "\nTop Contributing k-mers:"
    ]

    # Get k-mers for visualization
    kmers = [''.join(p) for p in product("ACGT", repeat=4)]
    
    # Create visualizations
    importance_plot = create_importance_bar_plot(shap_values, kmers, top_kmers)
    sequence_plot = visualize_sequence_impacts(seq, kmers, shap_values, prob_human)
    
    # Convert plots to images
    def fig_to_image(fig):
        buf = io.BytesIO()
        fig.savefig(buf, format='png', bbox_inches='tight', dpi=150)
        buf.seek(0)
        img = Image.open(buf)
        plt.close(fig)
        return img

    return "\n".join(results), fig_to_image(importance_plot), fig_to_image(sequence_plot)

# Create Gradio interface
css = """
.gradio-container {
    font-family: 'IBM Plex Sans', sans-serif;
}
"""

with gr.Blocks(css=css) as iface:
    gr.Markdown("""
    # Virus Host Classifier
    Predicts whether a viral sequence is of human or non-human origin using k-mer analysis.
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            file_input = gr.File(
                label="Upload FASTA file",
                file_types=[".fasta", ".fa", ".txt"],
                type="filepath"
            )
            text_input = gr.Textbox(
                label="Or paste FASTA sequence",
                placeholder=">sequence_name\nACGTACGT...",
                lines=5
            )
            top_k = gr.Slider(
                minimum=5,
                maximum=30,
                value=10,
                step=1,
                label="Number of top k-mers to display"
            )
            submit_btn = gr.Button("Analyze Sequence", variant="primary")
            
        with gr.Column(scale=2):
            results = gr.Textbox(label="Analysis Results", lines=10)
            kmer_plot = gr.Image(label="K-mer Importance Plot")
            shap_plot = gr.Image(label="Sequence Impact Visualization (SHAP-style)")
    
    submit_btn.click(
        predict,
        inputs=[file_input, top_k, text_input],
        outputs=[results, kmer_plot, shap_plot]
    )
    
    gr.Markdown("""
    ### Visualization Guide
    - **K-mer Importance Plot**: Shows the most influential k-mers and their SHAP values
    - **Sequence Impact Visualization**: Shows the sequence with highlighted k-mers:
      - Red highlights = pushing toward human origin
      - Blue highlights = pushing toward non-human origin
      - Arrows (↑/↓) show impact direction
      - Values show impact magnitude
    """)

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