File size: 32,185 Bytes
9916f6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884be71
9916f6b
 
 
 
 
 
35e900e
 
884be71
9916f6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35e900e
884be71
35e900e
9916f6b
 
 
 
 
 
 
 
600e193
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# app.py
# AI-Powered Drug Discovery Pipeline Streamlit Application
# This script integrates four phases of drug discovery into a single, interactive web app.

import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import requests
import io
from PIL import Image
import base64

# RDKit and BioPython imports
from rdkit import Chem
from rdkit.Chem import Draw, AllChem, Descriptors
from Bio import SeqIO

# Scikit-learn for ML models
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# 3D Visualization
import py3Dmol

# Suppress warnings for cleaner output
import warnings
warnings.filterwarnings('ignore')

# --- Page Configuration ---
st.set_page_config(
    page_title="AI Drug Discovery Pipeline",
    page_icon="πŸ”¬",
    layout="wide",
    initial_sidebar_state="collapsed", # Sidebar is removed, but this is good practice
)

# Custom CSS for a professional, minimalist look
def apply_custom_styling():
    st.markdown(
        """
        <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
        
        html, body, [class*="st-"] {
            font-family: 'Roboto', sans-serif;
        }

        .stApp {
            background-color: rgb(28, 28, 28);
            color: white;
        }

        /* Tab styles */
        .stTabs [data-baseweb="tab-list"] {
            gap: 24px;
        }

        .stTabs [data-baseweb="tab"] {
            height: 50px;
            white-space: pre-wrap;
            background: none;
            border-radius: 0px;
            border-bottom: 2px solid #333;
            padding: 10px 4px;
            color: #AAA;
        }
        
        .stTabs [data-baseweb="tab"]:hover {
            background: #222;
            color: #FFF;
        }

        .stTabs [aria-selected="true"] {
            border-bottom: 2px solid #00A0FF; /* Highlight color for active tab */
            color: #FFF;
        }
        
        /* Button styles */
        .stButton>button {
            border-color: #00A0FF;
            color: #00A0FF;
        }
        
        .stButton>button:hover {
            border-color: #FFF;
            color: #FFF;
            background-color: #00A0FF;
        }

        </style>
        """,
        unsafe_allow_html=True
    )

apply_custom_styling()


# --- 2. Core Functions from All Phases ---
# These functions are adapted from the provided Python scripts.

# ===== Phase 1 Functions =====

@st.cache_data(show_spinner="Fetching PDB structure...")
def fetch_pdb_structure(pdb_id: str):
    """
    Fetches a PDB file and returns its content.
    """
    log = ""
    try:
        url = f"https://files.rcsb.org/download/{pdb_id}.pdb"
        response = requests.get(url, timeout=20)
        if response.status_code == 200:
            log += f"βœ… Successfully fetched PDB data for {pdb_id}.\n"
            return response.text, log
        else:
            log += f"⚠️ Failed to fetch PDB file for {pdb_id} (Status: {response.status_code}). Please check the PDB ID and try again.\n"
            return None, log
    except Exception as e:
        log += f"❌ An error occurred while fetching PDB data: {e}\n"
        return None, log

@st.cache_data(show_spinner="Fetching FASTA sequence...")
def fetch_fasta_sequence(protein_id: str):
    """
    Fetches a protein's FASTA sequence from NCBI.
    """
    log = ""
    try:
        url = f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=protein&id={protein_id}&rettype=fasta&retmode=text"
        response = requests.get(url, timeout=20)
        if response.status_code == 200:
            parsed_fasta = SeqIO.read(io.StringIO(response.text), "fasta")
            log += f"βœ… Successfully fetched FASTA sequence for {protein_id}.\n\n"
            log += f"--- Protein Sequence Information ---\n"
            log += f"ID: {parsed_fasta.id}\n"
            log += f"Description: {parsed_fasta.description}\n"
            log += f"Sequence Length: {len(parsed_fasta.seq)}\n"
            log += f"Sequence Preview: {parsed_fasta.seq[:60]}...\n"
            return log
        else:
            log += f"⚠️ Failed to fetch FASTA file (Status: {response.status_code}).\n"
            return log
    except Exception as e:
        log += f"❌ An error occurred while fetching FASTA data: {e}\n"
        return log

def visualize_protein_3d(pdb_data: str, title="Protein 3D Structure"):
    """
    Generates an interactive 3D protein visualization using py3Dmol.
    """
    if not pdb_data:
        return None, "Cannot generate 3D view: No PDB data provided."
    try:
        viewer = py3Dmol.view(width='100%', height=600)
        # MODIFIED: Changed background color
        viewer.setBackgroundColor('#1C1C1C') 
        viewer.addModel(pdb_data, "pdb")
        viewer.setStyle({'cartoon': {'color': 'spectrum', 'thickness': 0.8}})
        viewer.addSurface(py3Dmol.VDW, {'opacity': 0.3, 'color': 'lightblue'})
        viewer.zoomTo()
        html = viewer._make_html()
        log = f"βœ… Generated 3D visualization for {title}."
        return html, log
    except Exception as e:
        return None, f"❌ 3D visualization error: {e}"

def create_sample_molecules():
    """
    Returns a list of sample SMILES strings for initial analysis.
    """
    return [
        "CC(=O)N[C@@H]1[C@@H](N)C=C(C(=O)O)O[C@H]1[C@H](O)[C@H](O)CO",
        "CC(C)C[C@H](NC(=O)C)C(=O)N[C@@H]1[C@@H](O)C=C(C(=O)O)O[C@H]1[C@H](O)[C@H](O)CO",
        "CC(C)CCCCCCCCCCCCCCCCCCCCCCCCCCCCC(=O)O",
        "CCO",
    ]

def calculate_molecular_properties(smiles_list: list):
    """
    Calculates key physicochemical properties for a list of molecules using RDKit.
    """
    properties = []
    log = ""
    for i, smiles in enumerate(smiles_list):
        mol = Chem.MolFromSmiles(smiles)
        if mol:
            props = {
                'Molecule': f'Compound_{i+1}',
                'SMILES': smiles,
                'MW': Descriptors.MolWt(mol),
                'LogP': Descriptors.MolLogP(mol),
                'HBD': Descriptors.NumHDonors(mol),
                'HBA': Descriptors.NumHAcceptors(mol),
                'TPSA': Descriptors.TPSA(mol),
                'RotBonds': Descriptors.NumRotatableBonds(mol),
            }
            properties.append(props)
        else:
            log += f"⚠️ Invalid SMILES string skipped: {smiles}\n"
    
    df = pd.DataFrame(properties).round(2)
    log += f"βœ… Calculated properties for {len(df)} valid molecules.\n"
    return df, log

def assess_drug_likeness(df: pd.DataFrame):
    """
    Assesses drug-likeness based on Lipinski's Rule of Five.
    """
    if df.empty:
        return pd.DataFrame(), "Cannot assess drug-likeness: No properties data."
    df_copy = df.copy()
    df_copy['MW_OK'] = df_copy['MW'] <= 500
    df_copy['LogP_OK'] = df_copy['LogP'] <= 5
    df_copy['HBD_OK'] = df_copy['HBD'] <= 5
    df_copy['HBA_OK'] = df_copy['HBA'] <= 10
    df_copy['Lipinski_Violations'] = (~df_copy[['MW_OK', 'LogP_OK', 'HBD_OK', 'HBA_OK']]).sum(axis=1)
    df_copy['Drug_Like'] = df_copy['Lipinski_Violations'] <= 1
    log = "βœ… Assessed drug-likeness using Lipinski's Rule of Five.\n"
    return df_copy, log

def plot_properties_dashboard(df: pd.DataFrame):
    """
    Creates a 2x2 dashboard of molecular property visualizations.
    """
    if df.empty:
        return None, "Cannot plot: No analysis data."
    
    plt.style.use('dark_background')
    fig, axes = plt.subplots(2, 2, figsize=(12, 10))
    fig.suptitle("Molecular Properties Analysis", fontsize=16)

    fig.patch.set_facecolor('none')
    for ax_row in axes:
        for ax in ax_row:
            ax.set_facecolor('none')

    axes[0,0].scatter(df['MW'], df['LogP'], c=df['Drug_Like'].map({True: 'green', False: 'red'}), s=80, alpha=0.7)
    axes[0,0].set_title('Molecular Weight vs LogP')
    axes[0,0].set_xlabel('Molecular Weight (Da)')
    axes[0,0].set_ylabel('LogP')
    axes[0,0].axvline(500, color='r', linestyle='--', alpha=0.6, label='MW < 500')
    axes[0,0].axhline(5, color='r', linestyle='--', alpha=0.6, label='LogP < 5')
    axes[0,0].legend()

    axes[0,1].scatter(df['HBD'], df['HBA'], c=df['Drug_Like'].map({True: 'green', False: 'red'}), s=80, alpha=0.7)
    axes[0,1].set_title('Hydrogen Bonding Properties')
    axes[0,1].set_xlabel('Hydrogen Bond Donors')
    axes[0,1].set_ylabel('Hydrogen Bond Acceptors')
    axes[0,1].axvline(5, color='r', linestyle='--', alpha=0.6, label='HBD < 5')
    axes[0,1].axhline(10, color='r', linestyle='--', alpha=0.6, label='HBA < 10')
    axes[0,1].legend()

    axes[1,0].scatter(df['TPSA'], df['RotBonds'], c=df['Drug_Like'].map({True: 'green', False: 'red'}), s=80, alpha=0.7)
    axes[1,0].set_title('TPSA vs Flexibility')
    axes[1,0].set_xlabel('Topological Polar Surface Area (Γ…Β²)')
    axes[1,0].set_ylabel('Rotatable Bonds')

    drug_like_counts = df['Drug_Like'].value_counts()
    labels = ['Drug-like' if i else 'Non-drug-like' for i in drug_like_counts.index]
    colors = ['green' if i else 'red' for i in drug_like_counts.index]
    axes[1,1].pie(drug_like_counts.values, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
    axes[1,1].set_title('Drug-likeness Distribution')
    
    plt.tight_layout(rect=[0, 0, 1, 0.96])
    return fig, "βœ… Generated properties dashboard."

# ===== Phase 2 Functions =====
def get_phase2_molecules():
    return {
        'Oseltamivir (Tamiflu)': "CCC(CC)O[C@H]1[C@H]([C@@H]([C@H](C=C1C(=O)OCC)N)N)NC(=O)C",
        'Zanamivir (Relenza)': "C[C@H](N)C(=O)N[C@H]1[C@@H](O)C=C(O[C@H]1[C@@H](O)[C@H](O)CO)C(O)=O",
        'Aspirin': "CC(=O)OC1=CC=CC=C1C(=O)O",
        'Ibuprofen': "CC(C)CC1=CC=C(C=C1)C(C)C(=O)O",
    }

def simulate_virtual_screening(smiles_dict: dict):
    np.random.seed(42)
    scores = np.random.uniform(2.0, 9.8, len(smiles_dict))
    results = [{'Molecule': name, 'SMILES': smiles, 'Predicted_Binding_Affinity': round(score, 2)} for (name, smiles), score in zip(smiles_dict.items(), scores)]
    df = pd.DataFrame(results).sort_values('Predicted_Binding_Affinity', ascending=False).reset_index(drop=True)
    df['Ranking'] = df.index + 1
    return df, f"βœ… Simulated virtual screening for {len(df)} molecules.\n"

def predict_admet_properties(smiles_dict: dict):
    admet_data = []
    log = ""
    for i, (name, smiles) in enumerate(smiles_dict.items()):
        mol = Chem.MolFromSmiles(smiles)
        if not mol: continue
        mw, logp, hbd, hba = Descriptors.MolWt(mol), Descriptors.MolLogP(mol), Descriptors.NumHDonors(mol), Descriptors.NumHAcceptors(mol)
        np.random.seed(42 + i)
        admet_data.append({'Molecule': name, 'MW': round(mw, 2), 'LogP': round(logp, 2), 'HBD': hbd, 'HBA': hba,
                           'Solubility (logS)': round(np.random.uniform(-4, -1), 2),
                           'Toxicity Risk': round(np.random.uniform(0.05, 0.4), 3),
                           'Lipinski Violations': sum([mw > 500, logp > 5, hbd > 5, hba > 10])})
    df = pd.DataFrame(admet_data)
    log += f"βœ… Predicted ADMET properties for {len(df)} molecules.\n"
    return df, log

def visualize_molecule_2d_3d(smiles: str, name: str):
    """Generates a side-by-side 2D SVG and 3D py3Dmol HTML view for a single molecule."""
    log = ""
    try:
        mol = Chem.MolFromSmiles(smiles)
        if not mol: return f"<p>Invalid SMILES for {name}</p>", f"❌ Invalid SMILES for {name}"
        
        drawer = Draw.rdMolDraw2D.MolDraw2DSVG(400, 300)
        drawer.DrawMolecule(mol)
        drawer.FinishDrawing()
        svg_2d = drawer.GetDrawingText().replace('svg:', '')

        mol_3d = Chem.AddHs(mol)
        AllChem.EmbedMolecule(mol_3d, randomSeed=42)
        AllChem.MMFFOptimizeMolecule(mol_3d)
        sdf_data = Chem.MolToMolBlock(mol_3d)

        viewer = py3Dmol.view(width=400, height=300)
        # MODIFIED: Changed background color
        viewer.setBackgroundColor('#1C1C1C')
        viewer.addModel(sdf_data, "sdf")
        viewer.setStyle({'stick': {}, 'sphere': {'scale': 0.25}})
        viewer.zoomTo()
        html_3d = viewer._make_html()

        combined_html = f"""
        <div style="display: flex; flex-direction: row; align-items: center; justify-content: space-around; border: 1px solid #444; border-radius: 10px; padding: 10px; margin-bottom: 10px; background-color: #2b2b2b;">
            <div style="text-align: center;">
                <h4 style="color: white; font-family: 'Roboto', sans-serif;">{name} (2D Structure)</h4>
                {svg_2d}
            </div>
            <div style="text-align: center;">
                <h4 style="color: white; font-family: 'Roboto', sans-serif;">{name} (3D Interactive)</h4>
                {html_3d}
            </div>
        </div>
        """
        log += f"βœ… Generated 2D/3D view for {name}.\n"
        return combined_html, log
    except Exception as e:
        return f"<p>Error visualizing {name}: {e}</p>", f"❌ Error visualizing {name}: {e}"
        
def visualize_protein_ligand_interaction(pdb_data: str, pdb_id: str, ligand_resn='G39'):
    """Visualizes a protein-ligand binding site using py3Dmol."""
    if not pdb_data: return None, "Cannot generate view: No PDB data provided."
    try:
        viewer = py3Dmol.view(width='100%', height=700)
        # MODIFIED: Changed background color
        viewer.setBackgroundColor('#1C1C1C')
        viewer.addModel(pdb_data, "pdb")
        viewer.setStyle({'cartoon': {'color': 'spectrum', 'thickness': 0.8}})
        viewer.addSurface(py3Dmol.VDW, {'opacity': 0.2, 'color': 'lightblue'})
        viewer.addStyle({'resn': ligand_resn}, {'stick': {'colorscheme': 'greenCarbon', 'radius': 0.3}, 'sphere': {'scale': 0.4, 'colorscheme': 'greenCarbon'}})
        viewer.addStyle({'within': {'distance': 4, 'sel': {'resn': ligand_resn}}}, {'stick': {'colorscheme': 'orangeCarbon', 'radius': 0.2}})
        viewer.zoomTo({'resn': ligand_resn})
        html = viewer._make_html()
        log = (f"βœ… Generated protein-ligand interaction view for PDB {pdb_id}.\n"
               f"🟒 Green: Ligand ({ligand_resn})\n"
               f"🟠 Orange: Residues within 4Γ… of ligand\n")
        return html, log
    except Exception as e:
        return None, f"❌ Protein-ligand visualization error: {e}"

# ===== Phase 3 Functions =====
def get_phase3_molecules():
    return {
        'Oseltamivir': 'CCC(CC)O[C@H]1[C@H]([C@@H]([C@H](C=C1C(=O)OCC)N)N)NC(=O)C',
        'Aspirin': 'CC(=O)OC1=CC=CC=C1C(=O)O',
        'Remdesivir': 'CCC(CC)COC(=O)[C@@H](C)N[P@](=O)(OC[C@@H]1O[C@](C#N)([C@H]([C@@H]1O)O)C2=CC=C3N2N=CN=C3N)OC4=CC=CC=C4',
        'Penicillin G': 'CC1([C@@H](N2[C@H](S1)[C@@H](C2=O)NC(=O)CC3=CC=CC=C3)C(=O)O)C'
    }

def calculate_comprehensive_properties(smiles_dict: dict):
    analysis = []
    log = ""
    for name, smiles in smiles_dict.items():
        mol = Chem.MolFromSmiles(smiles)
        if not mol: continue
        mw, logp, hbd, hba = Descriptors.MolWt(mol), Descriptors.MolLogP(mol), Descriptors.NumHDonors(mol), Descriptors.NumHAcceptors(mol)
        violations = sum([mw > 500, logp > 5, hbd > 5, hba > 10])
        analysis.append({'Compound': name, 'Molecular_Weight': mw, 'LogP': logp, 'HBD': hbd, 'HBA': hba,
                         'TPSA': Descriptors.TPSA(mol), 'Rotatable_Bonds': Descriptors.NumRotatableBonds(mol),
                         'Aromatic_Rings': Descriptors.NumAromaticRings(mol),
                         'Lipinski_Violations': violations, 'Drug_Like': 'Yes' if violations <= 1 else 'No'})
    df = pd.DataFrame(analysis).round(2)
    log += f"βœ… Calculated comprehensive properties for {len(df)} compounds.\n"
    return df, log

def predict_toxicity(properties_df: pd.DataFrame):
    if properties_df.empty: return pd.DataFrame(), "Cannot predict toxicity: No properties data."
    np.random.seed(42)
    n_compounds = 500
    training_data = pd.DataFrame({'molecular_weight': np.random.normal(400, 100, n_compounds),
                                  'logp': np.random.normal(2.5, 1.5, n_compounds),
                                  'tpsa': np.random.normal(80, 30, n_compounds),
                                  'rotatable_bonds': np.random.randint(0, 15, n_compounds),
                                  'aromatic_rings': np.random.randint(0, 5, n_compounds)})
    toxicity_score = ((training_data['molecular_weight'] > 550) * 0.4 + (abs(training_data['logp']) > 4.5) * 0.4 + np.random.random(n_compounds) * 0.2)
    training_data['toxic'] = (toxicity_score > 0.5).astype(int)
    features = ['molecular_weight', 'logp', 'tpsa', 'rotatable_bonds', 'aromatic_rings']
    rf_model = RandomForestClassifier(n_estimators=50, random_state=42)
    rf_model.fit(training_data[features], training_data['toxic'])
    X_pred = properties_df[['Molecular_Weight', 'LogP', 'TPSA', 'Rotatable_Bonds', 'Aromatic_Rings']]
    X_pred.columns = features
    toxicity_prob = rf_model.predict_proba(X_pred)[:, 1]
    results_df = properties_df[['Compound']].copy()
    results_df['Toxicity_Probability'] = np.round(toxicity_prob, 3)
    results_df['Predicted_Risk'] = ["🟒 LOW" if p < 0.3 else "🟑 MODERATE" if p < 0.7 else "πŸ”΄ HIGH" for p in toxicity_prob]
    return results_df, "βœ… Predicted toxicity using a pre-trained simulation model.\n"

# ===== Phase 4 Functions =====
def get_regulatory_summary():
    summary = {'Component': ['Data Governance', 'Model Architecture', 'Model Validation', 'Interpretability'],
               'Description': ['Data sourced from ChEMBL, PDB, GISAID. Bias assessed via geographic distribution analysis.',
                               'Graph Convolutional Network (Target ID), Random Forest (ADMET), K-Means (Patient Stratification).',
                               'ADMET Model validated with AUC-ROC > 0.85 on an independent test set.',
                               'SHAP used for patient stratification model outputs.']}
    return pd.DataFrame(summary), "βœ… Generated AI/ML documentation summary."

def simulate_rwd_analysis(adverse_event_text):
    np.random.seed(42)
    base_events = list(np.random.choice(['headache', 'nausea', 'fatigue', 'dizziness', 'rash', 'fever'], 100, p=[0.25, 0.2, 0.15, 0.15, 0.15, 0.1]))
    user_events = [e.strip().lower() for e in adverse_event_text.split(',') if e.strip()]
    all_events = base_events + user_events
    event_counts = pd.Series(all_events).value_counts()
    log = f"βœ… Analyzed {len(all_events)} simulated adverse event reports.\n"
    
    plt.style.use('dark_background')
    fig_bar, ax_bar = plt.subplots(figsize=(10, 6))
    
    fig_bar.patch.set_facecolor('none')
    ax_bar.set_facecolor('none')
    
    sns.barplot(x=event_counts.values, y=event_counts.index, palette='viridis', ax=ax_bar, orient='h')
    ax_bar.set_title('Simulated Adverse Event Frequencies')
    ax_bar.set_xlabel('Number of Reports')
    ax_bar.set_ylabel('Adverse Event')
    
    plt.tight_layout()

    return event_counts.reset_index().rename(columns={'index': 'Event', 0: 'Count'}), fig_bar, log

def get_ethical_framework():
    framework = {'Pillar': ['1. Beneficence & Non-Maleficence', '2. Justice & Fairness', '3. Transparency & Explainability', '4. Accountability & Governance'],
                 'Description': ['AI should help patients and do no harm. Requires rigorous validation and safety monitoring.',
                                 'AI must not create or worsen health disparities. Requires bias detection and mitigation.',
                                 'Clinical decisions influenced by AI must be understandable. Requires interpretable models.',
                                 'Clear lines of responsibility for AI systems must be established. Requires human oversight.']}
    return pd.DataFrame(framework), "βœ… Generated ethical framework summary."


# --- 3. Streamlit Interface Definition ---

st.title("πŸ”¬ AI-Powered Drug Discovery Pipeline")
st.markdown("""
Welcome to the AI Drug Discovery Pipeline Demonstrator. This application integrates the four major phases of drug development,
showcasing how AI and computational tools can accelerate the process from target identification to post-market surveillance.
Navigate through the tabs below to explore each phase.
""")

# Initialize session state for logs and results
if 'log_p1' not in st.session_state: st.session_state.log_p1 = "Phase 1 logs will appear here."
if 'results_p1' not in st.session_state: st.session_state.results_p1 = {}
if 'log_p2' not in st.session_state: st.session_state.log_p2 = "Phase 2 logs will appear here."
if 'results_p2' not in st.session_state: st.session_state.results_p2 = {}
if 'log_p3' not in st.session_state: st.session_state.log_p3 = "Phase 3 logs will appear here."
if 'results_p3' not in st.session_state: st.session_state.results_p3 = {}
if 'log_p4' not in st.session_state: st.session_state.log_p4 = "Phase 4 logs will appear here."
if 'results_p4' not in st.session_state: st.session_state.results_p4 = {}

tab1, tab2, tab3, tab4 = st.tabs([
    "Phase 1: Discovery & Target ID",
    "Phase 2: Lead Generation & Optimization",
    "Phase 3: Preclinical Development",
    "Phase 4: Implementation & Post-Market"
])

# ===== TAB 1: DISCOVERY & TARGET IDENTIFICATION =====
with tab1:
    st.header("🧬 Step 1: Target Identification and Initial Analysis")
    st.markdown("Fetch protein data from public databases and perform a high-level analysis of potential drug-like molecules.")
    
    with st.form(key="phase1_form"):
        st.subheader("Analysis Controls")
        col1, col2 = st.columns(2)
        with col1:
            pdb_id_input = st.text_input("Enter PDB ID", value="3B7E", key="p1_pdb")
            protein_id_input = st.text_input("Enter Protein ID (for FASTA)", value="ACF54602.1", key="p1_protein")
        with col2:
            smiles_input_p1 = st.text_area("Enter SMILES strings (one per line)", value="\n".join(create_sample_molecules()), height=150, key="p1_smiles")
        
        run_phase1_btn = st.form_submit_button("πŸš€ Run Phase 1 Analysis", use_container_width=True)

    if run_phase1_btn:
        full_log = "--- Starting Phase 1 Analysis ---\n"
        pdb_data, log_pdb_fetch = fetch_pdb_structure(pdb_id_input)
        full_log += log_pdb_fetch
        fasta_log = fetch_fasta_sequence(protein_id_input)
        full_log += fasta_log
        protein_view_html, log_3d_viz = visualize_protein_3d(pdb_data, pdb_id_input)
        full_log += log_3d_viz
        smiles_list = [s.strip() for s in smiles_input_p1.split('\n') if s.strip()]
        props_df, log_props = calculate_molecular_properties(smiles_list)
        full_log += log_props
        analysis_df, log_lipinski = assess_drug_likeness(props_df)
        full_log += log_lipinski
        props_plot, log_plot = plot_properties_dashboard(analysis_df)
        full_log += log_plot
        full_log += "\n--- Phase 1 Analysis Complete ---"
        st.session_state.log_p1 = full_log
        
        lipinski_cols = ['Molecule', 'MW', 'LogP', 'HBD', 'HBA', 'Lipinski_Violations', 'Drug_Like']
        lipinski_subset_df = analysis_df[lipinski_cols] if not analysis_df.empty else pd.DataFrame(columns=lipinski_cols)

        st.session_state.results_p1 = {
            'protein_view_html': protein_view_html,
            'fasta_log': fasta_log,
            'lipinski_subset_df': lipinski_subset_df,
            'props_df': props_df,
            'props_plot': props_plot
        }

    st.text_area("Status & Logs", st.session_state.log_p1, height=200, key="log_p1_area")
    
    if st.session_state.results_p1:
        res1 = st.session_state.results_p1
        p1_tabs = st.tabs(["Protein Information", "Molecule Analysis", "Analysis Plots"])
        with p1_tabs[0]:
            st.subheader("Protein 3D Structure (Interactive)")
            st.components.v1.html(res1.get('protein_view_html', '<p>No data</p>'), height=600, scrolling=False)
            st.subheader("FASTA Sequence Information")
            st.text_area("", res1.get('fasta_log', 'No data'), height=200, key="fasta_info_area")
        with p1_tabs[1]:
            st.subheader("Drug-Likeness Assessment (Lipinski's Rule of Five)")
            st.dataframe(res1.get('lipinski_subset_df', pd.DataFrame()), use_container_width=True)
            st.subheader("Calculated Molecular Properties")
            st.dataframe(res1.get('props_df', pd.DataFrame()), use_container_width=True)
        with p1_tabs[2]:
            st.subheader("Molecular Properties Dashboard")
            if res1.get('props_plot'):
                st.pyplot(res1['props_plot'])

# ===== TAB 2: LEAD GENERATION & OPTIMIZATION =====
with tab2:
    st.header("πŸ’Š Step 2: Virtual Screening and ADMET Prediction")
    st.markdown("Screen candidate molecules against the target, predict their ADMET properties, and visualize the top candidates.")
    
    with st.form(key="phase2_form"):
        st.subheader("Analysis Controls")
        col1, col2 = st.columns(2)
        with col1:
            phase2_pdb_id_input = st.text_input("Enter PDB ID for Interaction View", value="3B7E", key="p2_pdb")
        with col2:
            phase2_ligand_resn = st.text_input("Ligand Residue Name (in PDB)", value="G39", key="p2_ligand")
        run_phase2_btn = st.form_submit_button("πŸš€ Run Phase 2 Analysis", use_container_width=True)
        
    if run_phase2_btn:
        full_log = "--- Starting Phase 2 Analysis ---\n"
        smiles_dict = get_phase2_molecules()
        screening_df, log_screening = simulate_virtual_screening(smiles_dict)
        full_log += log_screening
        admet_df, log_admet = predict_admet_properties(smiles_dict)
        full_log += log_admet
        combined_viz_html = ""
        for name, smiles in smiles_dict.items():
            html_block, log_mol_viz = visualize_molecule_2d_3d(smiles, name)
            combined_viz_html += html_block
            full_log += log_mol_viz
        pdb_data, log_pdb_fetch_2 = fetch_pdb_structure(phase2_pdb_id_input)
        full_log += log_pdb_fetch_2
        interaction_html, log_interaction = visualize_protein_ligand_interaction(pdb_data, phase2_pdb_id_input, phase2_ligand_resn)
        full_log += log_interaction
        full_log += "\n--- Phase 2 Analysis Complete ---"
        st.session_state.log_p2 = full_log
        st.session_state.results_p2 = {
            'screening_df': screening_df,
            'admet_df': admet_df,
            'combined_viz_html': combined_viz_html,
            'interaction_html': interaction_html
        }
        
    st.text_area("Status & Logs", st.session_state.log_p2, height=200, key="log_p2_area")

    if st.session_state.results_p2:
        res2 = st.session_state.results_p2
        p2_tabs = st.tabs(["Virtual Screening & ADMET", "Molecule Visualization (2D & 3D)", "Protein-Ligand Interaction"])
        with p2_tabs[0]:
            col1, col2 = st.columns(2)
            with col1:
                st.subheader("Virtual Screening Results (Simulated)")
                st.dataframe(res2.get('screening_df', pd.DataFrame()), use_container_width=True)
            with col2:
                st.subheader("ADMET Properties Prediction")
                st.dataframe(res2.get('admet_df', pd.DataFrame()), use_container_width=True)
        with p2_tabs[1]:
            st.subheader("Interactive 2D and 3D views of candidate molecules.")
            st.components.v1.html(res2.get('combined_viz_html', '<p>No data</p>'), height=700, scrolling=True)
        with p2_tabs[2]:
            st.subheader("Detailed view of the top candidate binding to the protein.")
            st.components.v1.html(res2.get('interaction_html', '<p>No data</p>'), height=700, scrolling=False)


# ===== TAB 3: PRECLINICAL DEVELOPMENT =====
with tab3:
    st.header("πŸ§ͺ Step 3: In-Depth Candidate Analysis and Toxicity Prediction")
    st.markdown("Perform a comprehensive analysis of the most promising lead compounds and use a simulated AI model to predict toxicity risk.")
    
    with st.form(key="phase3_form"):
        st.subheader("Analysis Controls")
        run_phase3_btn = st.form_submit_button("πŸš€ Run Phase 3 Analysis", use_container_width=True)

    if run_phase3_btn:
        full_log = "--- Starting Phase 3 Analysis ---\n"
        smiles_dict = get_phase3_molecules()
        comp_props_df, log_comp_props = calculate_comprehensive_properties(smiles_dict)
        full_log += log_comp_props
        tox_df, log_tox = predict_toxicity(comp_props_df)
        full_log += log_tox
        combined_viz_html = ""
        for name, smiles in smiles_dict.items():
            html_block, log_mol_viz_p3 = visualize_molecule_2d_3d(smiles, name)
            combined_viz_html += html_block
            full_log += log_mol_viz_p3
        full_log += "\n--- Phase 3 Analysis Complete ---"
        st.session_state.log_p3 = full_log
        st.session_state.results_p3 = {
            'comp_props_df': comp_props_df,
            'tox_df': tox_df,
            'combined_viz_html': combined_viz_html
        }

    st.text_area("Status & Logs", st.session_state.log_p3, height=200, key="log_p3_area")

    if st.session_state.results_p3:
        res3 = st.session_state.results_p3
        p3_tabs = st.tabs(["Comprehensive Properties & Toxicity", "Molecule Visualization (3D Gallery)"])
        with p3_tabs[0]:
            st.subheader("Comprehensive Molecular Properties & AI-Powered Toxicity Prediction (Simulated)")
            col1, col2 = st.columns(2)
            with col1:
                st.dataframe(res3.get('comp_props_df', pd.DataFrame()), use_container_width=True)
            with col2:
                st.dataframe(res3.get('tox_df', pd.DataFrame()), use_container_width=True)
        with p3_tabs[1]:
            st.subheader("Interactive 3D gallery of the compounds under analysis.")
            st.components.v1.html(res3.get('combined_viz_html', '<p>No data</p>'), height=1000, scrolling=True)


# ===== TAB 4: POST-MARKET SURVEILLANCE =====
with tab4:
    st.header("πŸ“ˆ Step 4: Regulatory Submission and Pharmacovigilance")
    st.markdown("Explore summaries of the documentation needed for regulatory approval and simulate how AI can monitor real-world data for adverse events.")
    
    with st.form(key="phase4_form"):
        st.subheader("Analysis Controls")
        rwd_input = st.text_area("Enter new adverse events (comma-separated)", value="severe allergic reaction, joint pain, severe allergic reaction", height=100, key="p4_rwd")
        run_phase4_btn = st.form_submit_button("πŸš€ Run Phase 4 Analysis", use_container_width=True)

    if run_phase4_btn:
        full_log = "--- Starting Phase 4 Analysis ---\n"
        reg_df, log_reg = get_regulatory_summary()
        full_log += log_reg
        eth_df, log_eth = get_ethical_framework()
        full_log += log_eth
        
        rwd_df, plot_bar, log_rwd = simulate_rwd_analysis(rwd_input)
        full_log += log_rwd
        full_log += "\n--- Phase 4 Analysis Complete ---"
        st.session_state.log_p4 = full_log
        
        st.session_state.results_p4 = {
            'rwd_df': rwd_df,
            'plot_bar': plot_bar,
            'reg_df': reg_df,
            'eth_df': eth_df
        }

    st.text_area("Status & Logs", st.session_state.log_p4, height=200, key="log_p4_area")
    
    if st.session_state.results_p4:
        res4 = st.session_state.results_p4
        p4_tabs = st.tabs(["Pharmacovigilance Analysis", "Regulatory & Ethical Frameworks"])
        with p4_tabs[0]:
            st.subheader("Simulated Adverse Event Analysis")
            st.pyplot(res4['plot_bar'])
            st.dataframe(res4['rwd_df'], use_container_width=True)
            
        with p4_tabs[1]:
            col1, col2 = st.columns(2)
            with col1:
                st.subheader("AI/ML Documentation Summary for Submission")
                st.dataframe(res4.get('reg_df', pd.DataFrame()), use_container_width=True)
            with col2:
                st.subheader("Ethical Framework for AI in Healthcare")
                st.dataframe(res4.get('eth_df', pd.DataFrame()), use_container_width=True)