File size: 6,937 Bytes
7ab1cf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# dashboard.py
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

import streamlit as st
import pandas as pd
import torch
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
from rdkit import Chem
from rdkit.Chem import Descriptors

# --------------------------------------------------
# Page config
# --------------------------------------------------
st.set_page_config(
    page_title="ICH-NOVA | Future-Grade Drug Discovery",
    layout="wide",
    initial_sidebar_state="expanded"
)

# --------------------------------------------------
# Dark futuristic theme
# --------------------------------------------------
st.markdown("""
<style>
body { background-color: #0e0e0e; color: #d0d0d0; }
h1, h2, h3 { color: #f0f0f0; }
.stButton>button, .stDownloadButton>button {
    background-color: #1a1a1a;
    color: #d0d0d0;
    border: 1px solid #555;
}
</style>
""", unsafe_allow_html=True)

st.title("🧬 ICH-NOVA")
st.subheader("Regulatory-aware, self-evolving AI drug discovery")
# --------------------------------------------------
# Safe loaders
# --------------------------------------------------
@st.cache_data
def load_embeddings():
    try:
        with open("data/processed/embeddings.pkl", "rb") as f:
            return pickle.load(f)
    except Exception:
        return {}

@st.cache_data
def load_results():
    try:
        return pd.read_csv("final_candidates_rl.csv")
    except Exception:
        return pd.DataFrame()

@st.cache_resource
def load_stability_model():
    from src.stability.stability_zone_iv import StabilityPredictor
    model = StabilityPredictor(input_dim=256, hidden_dim=128)
    model.load_state_dict(torch.load("models/stability_model.pt", map_location="cpu"))
    model.eval()
    return model

embeddings_dict = load_embeddings()
final_df = load_results()
stability_model = load_stability_model()
# --------------------------------------------------
# TOP: Molecule of Interest (Primary Input)
# --------------------------------------------------
st.markdown("### πŸ§ͺ Molecule of Interest β€” Instant Zone-IV Evaluation")

top_col1, top_col2 = st.columns([3, 1])

with top_col1:
    top_smiles = st.text_input(
        "Enter SMILES string",
        placeholder="e.g. CC(=O)OC1=CC=CC=C1C(=O)O"
    )

with top_col2:
    top_predict = st.button("Predict Stability")

if top_predict and top_smiles.strip():
    mol = Chem.MolFromSmiles(top_smiles)
    if mol:
        mw = Descriptors.MolWt(mol)
        emb = embeddings_dict.get(top_smiles, torch.randn(256)).unsqueeze(0)
        with torch.no_grad():
            stab = stability_model(emb).item()

        res1, res2 = st.columns(2)
        res1.metric("Zone-IV Shelf-Life (days)", f"{stab:.2f}")
        res2.metric("Molecular Weight", f"{mw:.2f}")

        st.success("Prediction completed successfully.")
    else:
        st.error("Invalid SMILES string. Please check your input.")

st.markdown("---")



# --------------------------------------------------
# Normalize dataframe (CRITICAL FIX)
# --------------------------------------------------
required_cols = [
    "iteration", "smiles", "binding_score",
    "synthesis_score", "stability_pred",
    "reward", "admet_pass"
]

for col in required_cols:
    if col not in final_df.columns:
        final_df[col] = None

# If admet_pass missing β†’ infer safely
final_df["admet_pass"] = final_df["admet_pass"].fillna(
    final_df["stability_pred"].notna()
)

# --------------------------------------------------
# ADMET summary
# --------------------------------------------------
st.subheader("πŸ§ͺ ADMET Filtering Summary")

total = len(final_df)
passed = int(final_df["admet_pass"].sum())
failed = total - passed

c1, c2, c3 = st.columns(3)
c1.metric("Total Molecules", total)
c2.metric("Passed ADMET", passed)
c3.metric("Rejected", failed)

# --------------------------------------------------
# Sidebar: Molecule explorer
# --------------------------------------------------
st.sidebar.header("πŸ§ͺ Molecule Explorer")
user_smiles = st.sidebar.text_input("Enter SMILES")

if st.sidebar.button("Predict"):
    if user_smiles.strip():
        mol = Chem.MolFromSmiles(user_smiles)
        if mol:
            mw = Descriptors.MolWt(mol)
            emb = embeddings_dict.get(user_smiles, torch.randn(256)).unsqueeze(0)
            with torch.no_grad():
                stab = stability_model(emb).item()
            st.sidebar.success(f"Zone-IV Shelf-life: {stab:.2f} days")
            st.sidebar.info(f"Molecular Weight: {mw:.2f}")
        else:
            st.sidebar.error("Invalid SMILES")

# --------------------------------------------------
# Molecule table
# --------------------------------------------------
st.header("Generated Molecules")
st.dataframe(
    final_df.sort_values("admet_pass", ascending=False),
    use_container_width=True
)

st.download_button(
    "πŸ“₯ Download CSV",
    final_df.to_csv(index=False),
    "ICH_NOVA_results.csv",
    "text/csv"
)

# --------------------------------------------------
# Visualizations (SAFE)
# --------------------------------------------------
st.header("Visual Analysis")

numeric_df = final_df.dropna(subset=["reward", "binding_score", "stability_pred"])

# Reward vs Iteration
if not numeric_df.empty:
    st.subheader("Average RL Reward per Iteration")
    fig, ax = plt.subplots()
    numeric_df.groupby("iteration")["reward"].mean().plot(ax=ax)
    ax.set_xlabel("Iteration")
    ax.set_ylabel("Reward")
    st.pyplot(fig)

    st.subheader("Binding vs Stability")
    fig, ax = plt.subplots()
    sns.scatterplot(
        data=numeric_df,
        x="stability_pred",
        y="binding_score",
        hue="iteration",
        ax=ax
    )
    st.pyplot(fig)

    st.subheader("Reward vs Stability")
    fig, ax = plt.subplots()
    sc = ax.scatter(
        numeric_df["stability_pred"],
        numeric_df["binding_score"],
        c=numeric_df["reward"],
        cmap="viridis"
    )
    plt.colorbar(sc, ax=ax)
    st.pyplot(fig)
else:
    st.warning("Not enough numeric data for plots yet.")

# --------------------------------------------------
# Ablation (optional & safe)
# --------------------------------------------------
st.header("Module Ablation Analysis")

try:
    abl = pd.read_csv("ablation_results.csv")
    num = abl.select_dtypes(include="number")
    if not num.empty:
        plot_df = num.melt(var_name="Module", value_name="Reward")
        fig, ax = plt.subplots()
        sns.boxplot(data=plot_df, x="Module", y="Reward", ax=ax)
        st.pyplot(fig)
    else:
        st.info("Ablation file contains no numeric data.")
except Exception:
    st.info("Ablation results not available.")

# --------------------------------------------------
# Footer
# --------------------------------------------------
st.markdown("---")
st.markdown(
    "<center style='color:#888'>ICH-NOVA | Self-evolving AI for real-world drug discovery</center>",
    unsafe_allow_html=True
)