File size: 25,417 Bytes
e834a48
6963cf4
c85a5b0
160210a
3a463dd
 
 
c85a5b0
 
0dde248
c85a5b0
6963cf4
 
 
 
 
 
 
 
 
 
 
160210a
 
6963cf4
 
 
 
 
4499595
3e01b86
 
 
 
a6b7cf0
 
aae512c
 
 
6963cf4
c85a5b0
 
 
 
3a463dd
c85a5b0
 
17ad0e5
 
 
 
3a463dd
 
 
 
 
17ad0e5
1f960e0
17ad0e5
3a463dd
 
17ad0e5
3a463dd
 
 
 
 
 
17ad0e5
 
 
 
 
3a463dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09b66ec
3a463dd
09b66ec
3a463dd
 
 
4499595
09b66ec
3a463dd
09b66ec
3a463dd
09b66ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805e870
09b66ec
 
 
3a463dd
09b66ec
 
 
 
3a463dd
 
 
17ad0e5
 
 
 
 
 
 
 
 
 
 
b5108ae
17ad0e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5576cdd
ac18eb6
 
17ad0e5
3a463dd
 
 
 
 
 
 
 
 
 
 
 
17ad0e5
 
3a463dd
 
17ad0e5
a28eeb5
3a463dd
 
 
b081fc7
5ff1286
 
 
b5108ae
5ff1286
17ad0e5
 
fd6cc24
b5108ae
17ad0e5
b5108ae
3a463dd
b5108ae
 
 
17ad0e5
b5108ae
09b66ec
d2ee732
 
 
 
 
 
 
 
09b66ec
d2ee732
 
 
 
 
 
 
 
 
 
17ad0e5
3a463dd
d2ee732
17ad0e5
 
 
 
 
 
09b66ec
 
 
 
17ad0e5
d2ee732
17ad0e5
 
c85a5b0
4499595
a6b7cf0
17ad0e5
 
 
 
c85a5b0
fd6cc24
17ad0e5
 
 
c85a5b0
 
fd6cc24
3a463dd
0c6c0c1
 
 
 
 
 
c85a5b0
3a463dd
 
 
 
c85a5b0
3a463dd
c85a5b0
3a463dd
 
09b66ec
 
 
3a463dd
0c6c0c1
c85a5b0
8bd6bbb
09b66ec
 
 
 
 
d2ee732
09b66ec
 
 
 
 
 
 
0c6c0c1
09b66ec
 
 
 
 
 
3a463dd
0c6c0c1
c85a5b0
09b66ec
 
 
 
 
 
0c6c0c1
09b66ec
 
3a463dd
 
 
09b66ec
3a463dd
09b66ec
 
 
 
 
 
 
3a463dd
c85a5b0
3a463dd
c85a5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd6cc24
c85a5b0
 
 
 
160210a
 
c85a5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06bab06
 
 
 
 
 
 
 
 
 
 
 
 
64f6421
3a463dd
09b66ec
 
 
 
 
 
 
 
 
b081fc7
09b66ec
06bab06
 
 
 
 
 
 
 
 
 
 
 
8bd6bbb
01ff8b6
b081fc7
 
06bab06
c85a5b0
b5108ae
 
 
 
 
 
 
 
c85a5b0
09b66ec
d2ee732
 
 
17ad0e5
 
 
09b66ec
 
 
3a463dd
c85a5b0
b5108ae
 
 
 
 
17ad0e5
b5108ae
 
 
5dbe94b
 
09b66ec
5dbe94b
 
17ad0e5
5dbe94b
17ad0e5
09b66ec
06bab06
 
09b66ec
 
 
b5108ae
5dbe94b
17ad0e5
 
 
 
 
 
5dbe94b
b5108ae
17ad0e5
 
b5108ae
 
17ad0e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09b66ec
 
 
06bab06
09b66ec
06bab06
 
09b66ec
 
 
06bab06
 
09b66ec
 
 
 
 
 
 
31865d7
 
09b66ec
 
 
 
 
 
3a463dd
09b66ec
b5108ae
 
17ad0e5
b5108ae
 
17ad0e5
b5108ae
17ad0e5
 
 
3a463dd
 
 
09b66ec
 
3a463dd
 
 
1f960e0
 
 
fd6cc24
 
4eaf58b
 
1f960e0
c85a5b0
1f960e0
 
889b770
70d9713
 
 
 
 
 
 
 
 
 
 
 
 
c8b39c2
 
 
 
 
 
 
 
70d9713
 
5ed7396
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
from datetime import datetime
import gradio as gr
import requests
from Bio.PDB import PDBParser, MMCIFParser, PDBIO, Select
from Bio.PDB.Polypeptide import is_aa
from Bio.SeqUtils import seq1
from typing import Optional, Tuple
import numpy as np
import os
from gradio_molecule3d import Molecule3D

from model_loader import load_model

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader

import re
import pandas as pd
import copy

import transformers
from transformers import AutoTokenizer, DataCollatorForTokenClassification

from datasets import Dataset

from scipy.special import expit

# Load model and move to device
#checkpoint = 'ThorbenF/prot_t5_xl_uniref50'
#checkpoint = 'ThorbenF/prot_t5_xl_uniref50_cryptic'
#checkpoint = 'ThorbenF/prot_t5_xl_uniref50_database'
checkpoint = 'ThorbenF/prot_t5_xl_uniref50_full'
max_length = 1500
model, tokenizer = load_model(checkpoint, max_length)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
model.eval()

def normalize_scores(scores):
    min_score = np.min(scores)
    max_score = np.max(scores)
    return (scores - min_score) / (max_score - min_score) if max_score > min_score else scores

def read_mol(pdb_path):
    """Read PDB file and return its content as a string"""
    with open(pdb_path, 'r') as f:
        return f.read()

def fetch_structure(pdb_id: str, output_dir: str = ".") -> str:
    """
    Fetch the structure file for a given PDB ID. Prioritizes CIF files.
    If a structure file already exists locally, it uses that.
    """
    file_path = download_structure(pdb_id, output_dir)
    return file_path

def download_structure(pdb_id: str, output_dir: str) -> str:
    """
    Attempt to download the structure file in CIF or PDB format.
    Returns the path to the downloaded file.
    """
    for ext in ['.cif', '.pdb']:
        file_path = os.path.join(output_dir, f"{pdb_id}{ext}")
        if os.path.exists(file_path):
            return file_path
        url = f"https://files.rcsb.org/download/{pdb_id}{ext}"
        response = requests.get(url, timeout=10)
        if response.status_code == 200:
            with open(file_path, 'wb') as f:
                f.write(response.content)
            return file_path
    return None

def convert_cif_to_pdb(cif_path: str, output_dir: str = ".") -> str:
    """
    Convert a CIF file to PDB format using BioPython and return the PDB file path.
    """
    pdb_path = os.path.join(output_dir, os.path.basename(cif_path).replace('.cif', '.pdb'))
    parser = MMCIFParser(QUIET=True)
    structure = parser.get_structure('protein', cif_path)
    io = PDBIO()
    io.set_structure(structure)
    io.save(pdb_path)
    return pdb_path

def fetch_pdb(pdb_id):
    pdb_path = fetch_structure(pdb_id)
    _, ext = os.path.splitext(pdb_path)
    if ext == '.cif':
        pdb_path = convert_cif_to_pdb(pdb_path)
    return pdb_path

def create_chain_specific_pdb(input_pdb: str, chain_id: str, residue_scores: list, protein_residues: list) -> str:
    """
    Create a PDB file with only the selected chain and residues, replacing B-factor with prediction scores
    """
    parser = PDBParser(QUIET=True)
    structure = parser.get_structure('protein', input_pdb)
    
    output_pdb = f"{os.path.splitext(input_pdb)[0]}_{chain_id}_predictions_scores.pdb"
    
    # Create scores dictionary for easy lookup
    scores_dict = {resi: score for resi, score in residue_scores}

    # Create a custom Select class
    class ResidueSelector(Select):
        def __init__(self, chain_id, selected_residues, scores_dict):
            self.chain_id = chain_id
            self.selected_residues = selected_residues
            self.scores_dict = scores_dict
        
        def accept_chain(self, chain):
            return chain.id == self.chain_id
        
        def accept_residue(self, residue):
            return residue.id[1] in self.selected_residues

        def accept_atom(self, atom):
            if atom.parent.id[1] in self.scores_dict:
                atom.bfactor = np.absolute(1-self.scores_dict[atom.parent.id[1]]) * 100
            return True

    # Prepare output PDB with selected chain and residues, modified B-factors
    io = PDBIO()
    selector = ResidueSelector(chain_id, [res.id[1] for res in protein_residues], scores_dict)
    
    io.set_structure(structure[0])
    io.save(output_pdb, selector)
    
    return output_pdb

def generate_pymol_commands(pdb_id, segment, residues_by_bracket, current_time, score_type):
    """Generate PyMOL commands based on score type"""
    pymol_commands = f"Prediction for PDB: {pdb_id}, Chain: {segment}\nDate: {current_time}\nScore Type: {score_type}\n\n"
    
    pymol_commands += f"""
    # PyMOL Visualization Commands
    fetch {pdb_id}, protein
    hide everything, all
    show cartoon, chain {segment}
    color white, chain {segment}
    """
    
    # Define colors for each score bracket
    bracket_colors = {
        "0.0-0.2": "white",
        "0.2-0.4": "lightorange",
        "0.4-0.6": "yelloworange",
        "0.6-0.8": "orange",
        "0.8-1.0": "red"
    }
    
    # Add PyMOL commands for each score bracket
    for bracket, residues in residues_by_bracket.items():
        if residues:  # Only add commands if there are residues in this bracket
            color = bracket_colors[bracket]
            resi_list = '+'.join(map(str, residues))
            pymol_commands += f"""
    select bracket_{bracket.replace('.', '').replace('-', '_')}, resi {resi_list} and chain {segment}
    show sticks, bracket_{bracket.replace('.', '').replace('-', '_')}
    color {color}, bracket_{bracket.replace('.', '').replace('-', '_')}
    """
    return pymol_commands

def generate_results_text(pdb_id, segment, residues_by_bracket, protein_residues, sequence, scores, current_time, score_type):
    """Generate results text based on score type"""
    result_str = f"Prediction for PDB: {pdb_id}, Chain: {segment}\nDate: {current_time}\nScore Type: {score_type}\n\n"
    result_str += "Residues by Score Brackets:\n\n"
    
    # Add residues for each bracket
    for bracket, residues in residues_by_bracket.items():
        result_str += f"Bracket {bracket}:\n"
        result_str += f"Columns: Residue Name, Residue Number, One-letter Code, {score_type} Score\n"
        result_str += "\n".join([
            f"{res.resname} {res.id[1]} {sequence[i]} {scores[i]:.2f}" 
            for i, res in enumerate(protein_residues) if res.id[1] in residues
        ])
        result_str += "\n\n"
    
    return result_str




def process_pdb(pdb_id_or_file, segment, score_type='normalized'):
    # Determine if input is a PDB ID or file path
    if pdb_id_or_file.endswith('.pdb'):
        pdb_path = pdb_id_or_file
        pdb_id = os.path.splitext(os.path.basename(pdb_path))[0]
    else:
        pdb_id = pdb_id_or_file
        pdb_path = fetch_pdb(pdb_id)
    
    # Determine the file format and choose the appropriate parser
    _, ext = os.path.splitext(pdb_path)
    parser = MMCIFParser(QUIET=True) if ext == '.cif' else PDBParser(QUIET=True)
    
    # Parse the structure file
    structure = parser.get_structure('protein', pdb_path)
    
    # Extract the specified chain
    chain = structure[0][segment]
    
    protein_residues = [res for res in chain if is_aa(res)]
    sequence = "".join(seq1(res.resname) for res in protein_residues)
    sequence_id = [res.id[1] for res in protein_residues]

    input_ids = tokenizer(" ".join(sequence), return_tensors="pt").input_ids.to(device)
    with torch.no_grad():
        outputs = model(input_ids).logits.detach().cpu().numpy().squeeze()

    # Calculate scores and normalize them
    raw_scores = expit(outputs[:, 1] - outputs[:, 0])
    normalized_scores = normalize_scores(raw_scores)
    
    # Choose which scores to use based on score_type
    display_scores = normalized_scores if score_type == 'normalized' else raw_scores
    
    # Zip residues with scores to track the residue ID and score
    residue_scores = [(resi, score) for resi, score in zip(sequence_id, display_scores)]
    
    # Also save both score types for later use
    raw_residue_scores = [(resi, score) for resi, score in zip(sequence_id, raw_scores)]
    norm_residue_scores = [(resi, score) for resi, score in zip(sequence_id, normalized_scores)]
    
    # Define the score brackets
    score_brackets = {
        "0.0-0.2": (0.0, 0.2),
        "0.2-0.4": (0.2, 0.4),
        "0.4-0.6": (0.4, 0.6),
        "0.6-0.8": (0.6, 0.8),
        "0.8-1.0": (0.8, 1.0)
    }
    
    # Initialize a dictionary to store residues by bracket
    residues_by_bracket = {bracket: [] for bracket in score_brackets}
    
    # Categorize residues into brackets
    for resi, score in residue_scores:
        for bracket, (lower, upper) in score_brackets.items():
            if lower <= score < upper:
                residues_by_bracket[bracket].append(resi)
                break
    
    # Generate timestamp
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    # Generate result text and PyMOL commands based on score type
    display_score_type = "Normalized" if score_type == 'normalized' else "Raw"
    result_str = generate_results_text(pdb_id, segment, residues_by_bracket, protein_residues, sequence, 
                                      display_scores, current_time, display_score_type)
    pymol_commands = generate_pymol_commands(pdb_id, segment, residues_by_bracket, current_time, display_score_type)
    
    # Create chain-specific PDB with scores in B-factor
    scored_pdb = create_chain_specific_pdb(pdb_path, segment, residue_scores, protein_residues)

    # Molecule visualization with updated script with color mapping
    mol_vis = molecule(pdb_path, residue_scores, segment)
    
    # Create prediction file
    prediction_file = f"{pdb_id}_{display_score_type.lower()}_binding_site_residues.txt"
    with open(prediction_file, "w") as f:
        f.write(result_str)
    
    scored_pdb_name = f"{pdb_id}_{segment}_{display_score_type.lower()}_predictions_scores.pdb"
    os.rename(scored_pdb, scored_pdb_name)
    
    return pymol_commands, mol_vis, [prediction_file, scored_pdb_name], raw_residue_scores, norm_residue_scores, pdb_id, segment

def molecule(input_pdb, residue_scores=None, segment='A'):
    # Read PDB file content
    mol = read_mol(input_pdb)
    
    # Prepare high-scoring residues script if scores are provided
    high_score_script = ""
    if residue_scores is not None:
        # Filter residues based on their scores
        class1_score_residues = [resi for resi, score in residue_scores if 0.0 < score <= 0.2]
        class2_score_residues = [resi for resi, score in residue_scores if 0.2 < score <= 0.4]
        class3_score_residues = [resi for resi, score in residue_scores if 0.4 < score <= 0.6]
        class4_score_residues = [resi for resi, score in residue_scores if 0.6 < score <= 0.8]
        class5_score_residues = [resi for resi, score in residue_scores if 0.8 < score <= 1.0]
        
        high_score_script = """
        // Load the original model and apply white cartoon style
        let chainModel = viewer.addModel(pdb, "pdb");
        chainModel.setStyle({}, {});
        chainModel.setStyle(
            {"chain": "%s"}, 
            {"cartoon": {"color": "white"}}
        );

        // Create a new model for high-scoring residues and apply red sticks style
        let class1Model = viewer.addModel(pdb, "pdb");
        class1Model.setStyle({}, {});
        class1Model.setStyle(
            {"chain": "%s", "resi": [%s]}, 
            {"stick": {"color": "0xFFFFFF", "opacity": 0.5}}
        );

        // Create a new model for high-scoring residues and apply red sticks style
        let class2Model = viewer.addModel(pdb, "pdb");
        class2Model.setStyle({}, {});
        class2Model.setStyle(
            {"chain": "%s", "resi": [%s]}, 
            {"stick": {"color": "0xFFD580", "opacity": 0.7}}
        );

        // Create a new model for high-scoring residues and apply red sticks style
        let class3Model = viewer.addModel(pdb, "pdb");
        class3Model.setStyle({}, {});
        class3Model.setStyle(
            {"chain": "%s", "resi": [%s]}, 
            {"stick": {"color": "0xFFA500", "opacity": 1}}
        );

        // Create a new model for high-scoring residues and apply red sticks style
        let class4Model = viewer.addModel(pdb, "pdb");
        class4Model.setStyle({}, {});
        class4Model.setStyle(
            {"chain": "%s", "resi": [%s]}, 
            {"stick": {"color": "0xFF4500", "opacity": 1}}
        );

        // Create a new model for high-scoring residues and apply red sticks style
        let class5Model = viewer.addModel(pdb, "pdb");
        class5Model.setStyle({}, {});
        class5Model.setStyle(
            {"chain": "%s", "resi": [%s]}, 
            {"stick": {"color": "0xFF0000", "alpha": 1}}
        );

        """ % (
            segment,
            segment,
            ", ".join(str(resi) for resi in class1_score_residues),
            segment,
            ", ".join(str(resi) for resi in class2_score_residues),
            segment,
            ", ".join(str(resi) for resi in class3_score_residues),
            segment,
            ", ".join(str(resi) for resi in class4_score_residues),
            segment,
            ", ".join(str(resi) for resi in class5_score_residues)
        )
    
    # Generate the full HTML content
    html_content = f"""
    <!DOCTYPE html>
    <html>
    <head>    
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <style>
        .mol-container {{
            width: 100%;
            height: 700px;
            position: relative;
        }}
        </style>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
        <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
    </head>
    <body>
        <div id="container" class="mol-container"></div>
        <script>
            let pdb = `{mol}`; // Use template literal to properly escape PDB content
            $(document).ready(function () {{
                let element = $("#container");
                let config = {{ backgroundColor: "white" }};
                let viewer = $3Dmol.createViewer(element, config);
                
                {high_score_script}
                
                // Add hover functionality
                viewer.setHoverable(
                    {{}}, 
                    true, 
                    function(atom, viewer, event, container) {{
                        if (!atom.label) {{
                            atom.label = viewer.addLabel(
                                atom.resn + ":" +atom.resi + ":" + atom.atom, 
                                {{
                                    position: atom, 
                                    backgroundColor: 'mintcream', 
                                    fontColor: 'black',
                                    fontSize: 18,
                                    padding: 4
                                }}
                            );
                        }}
                    }},
                    function(atom, viewer) {{
                        if (atom.label) {{
                            viewer.removeLabel(atom.label);
                            delete atom.label;
                        }}
                    }}
                );
                
                viewer.zoomTo();
                viewer.render();
                viewer.zoom(0.8, 2000);
            }});
        </script>
    </body>
    </html>
    """
    
    # Return the HTML content within an iframe safely encoded for special characters
    return f'<iframe width="100%" height="700" srcdoc="{html_content.replace(chr(34), "&quot;").replace(chr(39), "&#39;")}"></iframe>'

with gr.Blocks(css="""
    /* Customize Gradio button colors */
    #visualize-btn, #predict-btn {
        background-color: #FF7300; /* Deep orange */
        color: white;
        border-radius: 5px;
        padding: 10px;
        font-weight: bold;
    }
    #visualize-btn:hover, #predict-btn:hover {
        background-color: #CC5C00; /* Darkened orange on hover */
    }
""") as demo:
    gr.Markdown("# Protein Binding Site Prediction")
    
    # Mode selection
    mode = gr.Radio(
        choices=["PDB ID", "Upload File"],
        value="PDB ID",
        label="Input Mode",
        info="Choose whether to input a PDB ID or upload a PDB/CIF file."
    )

    # Input components based on mode
    pdb_input = gr.Textbox(value="2F6V", label="PDB ID", placeholder="Enter PDB ID here...")
    pdb_file = gr.File(label="Upload PDB/CIF File", visible=False)
    visualize_btn = gr.Button("Visualize Structure", elem_id="visualize-btn")

    molecule_output2 = Molecule3D(label="Protein Structure", reps=[
        {
            "model": 0,
            "style": "cartoon",
            "color": "whiteCarbon",
            "residue_range": "",
            "around": 0,
            "byres": False,
        }
    ])

    with gr.Row():
        segment_input = gr.Textbox(value="A", label="Chain ID (protein)", placeholder="Enter Chain ID here...",
        info="Choose in which chain to predict binding sites.")
        prediction_btn = gr.Button("Predict Binding Site", elem_id="predict-btn")

    # Add score type selector
    score_type = gr.Radio(
        choices=["Normalized Scores", "Raw Scores"],
        value="Normalized Scores",
        label="Score Visualization Type",
        info="Choose which score type to visualize"
    )

    molecule_output = gr.HTML(label="Protein Structure")
    explanation_vis = gr.Markdown("""
    Score dependent colorcoding:
    - 0.0-0.2: white  
    - 0.2–0.4: light orange  
    - 0.4–0.6: yellow orange
    - 0.6–0.8: orange
    - 0.8–1.0: red
    """)
    predictions_output = gr.Textbox(label="Visualize Prediction with PyMol")
    gr.Markdown("### Download:\n- List of predicted binding site residues\n- PDB with score in beta factor column")
    download_output = gr.File(label="Download Files", file_count="multiple")
    
    # Store these as state variables so we can switch between them
    raw_scores_state = gr.State(None)
    norm_scores_state = gr.State(None)
    last_pdb_path = gr.State(None)
    last_segment = gr.State(None)
    last_pdb_id = gr.State(None)
    
    def process_interface(mode, pdb_id, pdb_file, chain_id, score_type_val):
        selected_score_type = 'normalized' if score_type_val == "Normalized Scores" else 'raw'
        
        # First get the actual PDB file path
        if mode == "PDB ID":
            pdb_path = fetch_pdb(pdb_id)  # Get the actual file path
            
            pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_id_result, segment = process_pdb(pdb_path, chain_id, selected_score_type)
            # Store the actual file path, not just the PDB ID
            return pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id, pdb_id_result
        elif mode == "Upload File":
            _, ext = os.path.splitext(pdb_file.name)
            file_path = os.path.join('./', f"{_}{ext}")
            if ext == '.cif':
                pdb_path = convert_cif_to_pdb(file_path)
            else:
                pdb_path = file_path
            
            pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_id_result, segment = process_pdb(pdb_path, chain_id, selected_score_type)
            return pymol_cmd, mol_vis, files, raw_scores, norm_scores, pdb_path, chain_id, pdb_id_result
    
    def update_visualization_and_files(score_type_val, raw_scores, norm_scores, pdb_path, segment, pdb_id):
        if raw_scores is None or norm_scores is None or pdb_path is None or segment is None or pdb_id is None:
            return None, None, None
        
        # Choose scores based on radio button selection
        selected_score_type = 'normalized' if score_type_val == "Normalized Scores" else 'raw'
        selected_scores = norm_scores if selected_score_type == 'normalized' else raw_scores
        
        # Generate visualization with selected scores
        mol_vis = molecule(pdb_path, selected_scores, segment)
        
        # Generate PyMOL commands and downloadable files
        # Get structure for residue info
        _, ext = os.path.splitext(pdb_path)
        parser = MMCIFParser(QUIET=True) if ext == '.cif' else PDBParser(QUIET=True)
        structure = parser.get_structure('protein', pdb_path)
        chain = structure[0][segment]
        protein_residues = [res for res in chain if is_aa(res)]
        sequence = "".join(seq1(res.resname) for res in protein_residues)
        
        # Define score brackets
        score_brackets = {
            "0.0-0.2": (0.0, 0.2),
            "0.2-0.4": (0.2, 0.4),
            "0.4-0.6": (0.4, 0.6),
            "0.6-0.8": (0.6, 0.8),
            "0.8-1.0": (0.8, 1.0)
        }
        
        # Initialize a dictionary to store residues by bracket
        residues_by_bracket = {bracket: [] for bracket in score_brackets}
        
        # Categorize residues into brackets
        for resi, score in selected_scores:
            for bracket, (lower, upper) in score_brackets.items():
                if lower <= score < upper:
                    residues_by_bracket[bracket].append(resi)
                    break
        
        # Generate timestamp
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
        # Generate result text and PyMOL commands based on score type
        display_score_type = "Normalized" if selected_score_type == 'normalized' else "Raw"
        scores_array = [score for _, score in selected_scores]
        result_str = generate_results_text(pdb_id, segment, residues_by_bracket, protein_residues, sequence, 
                                           scores_array, current_time, display_score_type)
        pymol_commands = generate_pymol_commands(pdb_id, segment, residues_by_bracket, current_time, display_score_type)
        
        # Create chain-specific PDB with scores in B-factor
        scored_pdb = create_chain_specific_pdb(pdb_path, segment, selected_scores, protein_residues)
        
        # Create prediction file
        prediction_file = f"{pdb_id}_{display_score_type.lower()}_binding_site_residues.txt"
        with open(prediction_file, "w") as f:
            f.write(result_str)
        
        scored_pdb_name = f"{pdb_id}_{segment}_{display_score_type.lower()}_predictions_scores.pdb"
        os.rename(scored_pdb, scored_pdb_name)
        
        return mol_vis, pymol_commands, [prediction_file, scored_pdb_name]

    def fetch_interface(mode, pdb_id, pdb_file):
        if mode == "PDB ID":
            return fetch_pdb(pdb_id)
        elif mode == "Upload File":
            _, ext = os.path.splitext(pdb_file.name)
            file_path = os.path.join('./', f"{_}{ext}")
            if ext == '.cif':
                pdb_path = convert_cif_to_pdb(file_path)
            else:
                pdb_path= file_path
            return pdb_path

    def toggle_mode(selected_mode):
        if selected_mode == "PDB ID":
            return gr.update(visible=True), gr.update(visible=False)
        else:
            return gr.update(visible=False), gr.update(visible=True)

    
        
    mode.change(
        toggle_mode,
        inputs=[mode],
        outputs=[pdb_input, pdb_file]
    )

    prediction_btn.click(
        process_interface, 
        inputs=[mode, pdb_input, pdb_file, segment_input, score_type], 
        outputs=[predictions_output, molecule_output, download_output, 
                raw_scores_state, norm_scores_state, last_pdb_path, last_segment, last_pdb_id]
    )
    
    # Update visualization, PyMOL commands, and files when score type changes
    score_type.change(
        update_visualization_and_files,
        inputs=[score_type, raw_scores_state, norm_scores_state, last_pdb_path, last_segment, last_pdb_id],
        outputs=[molecule_output, predictions_output, download_output]
    )

    visualize_btn.click(
        fetch_interface, 
        inputs=[mode, pdb_input, pdb_file], 
        outputs=molecule_output2
    )

    gr.Markdown("## Examples")
    gr.Examples(
        examples=[
            ["7RPZ", "A"],
            ["2IWI", "B"],
            ["7LCJ", "R"],
            ["4OBE", "A"]
        ],
        inputs=[pdb_input, segment_input],
        outputs=[predictions_output, molecule_output, download_output]
    )

    def predict_utils(sequence):
        input_ids = tokenizer(" ".join(sequence), return_tensors="pt").input_ids.to(device)
        with torch.no_grad():
            outputs = model(input_ids).logits.detach().cpu().numpy().squeeze()
    
        raw_scores = expit(outputs[:, 1] - outputs[:, 0])
        normalized_scores = normalize_scores(raw_scores)
    
        return {
        "raw_scores": raw_scores.tolist(),
        "normalized_scores": normalized_scores.tolist()
    }

    dummy_input = gr.Textbox(visible=False)
    dummy_output = gr.Textbox(visible=False)
    
    dummy_btn = gr.Button("Predict Sequence", visible=False)
    dummy_btn.click(
        predict_utils,
        inputs=[dummy_input],
        outputs=[dummy_output]
    )

demo.launch(share=True)