-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_page.py
More file actions
328 lines (267 loc) Β· 13 KB
/
Copy pathmain_page.py
File metadata and controls
328 lines (267 loc) Β· 13 KB
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
import os
import io
import streamlit as st
import pandas as pd
import numpy as np
from tempfile import NamedTemporaryFile
import py3Dmol
from stmol import showmol
# Import our RAG pipeline
from rag_pipeline import initialize_rag_pipeline
# Assuming these exist in your project
from utils import dataframes, confidence
# Environment setup
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# Initialize session state for RAG pipeline
if 'rag_pipeline' not in st.session_state:
st.session_state.rag_pipeline = None
def gather_structure_data(temp_file_path, hl_ligand, hl_pocket, hl_chain, hl_resi_list):
"""Gather structural analysis data into a formatted string"""
structure_summary = "=== PROTEIN STRUCTURE ANALYSIS ===\n"
try:
# Basic info
structure_summary += f"β’ Analyzed Chain: {hl_chain}\n"
#structure_summary += f"β’ Highlighted Residues: {len(hl_resi_list)} residues\n"
# Ligand analysis
if hl_ligand:
try:
ligand_df = dataframes.get_resi_bfactor(temp_file_path, resi_name=["UNK", "LIG", "LG1"])
if not ligand_df.empty:
avg_confidence = ligand_df['Prediction Confidence'].mean()
min_confidence = ligand_df['Prediction Confidence'].min()
max_confidence = ligand_df['Prediction Confidence'].max()
structure_summary += f"\n=== LIGAND BINDING ANALYSIS ===\n"
structure_summary += f"β’ Average Prediction Confidence: {avg_confidence:.2f}\n"
structure_summary += f"β’ Confidence Range: {min_confidence:.2f} - {max_confidence:.2f}\n"
# Confidence interpretation
if avg_confidence >= 80:
reliability = "HIGH - 80% chance of accurate prediction"
elif avg_confidence >= 60:
reliability = "MODERATE - 50% chance of accurate prediction"
elif avg_confidence >= 0:
reliability = "LOW - 10% chance of accurate prediction"
else:
reliability = "UNDEFINED - requires validation"
structure_summary += f"β’ Reliability Assessment: {reliability}\n"
else:
structure_summary += "\n=== LIGAND BINDING ANALYSIS ===\n"
structure_summary += "β’ No ligand data available\n"
except Exception as e:
structure_summary += f"\n=== LIGAND BINDING ANALYSIS ===\n"
structure_summary += f"β’ Error analyzing ligand: {e}\n"
# Pocket analysis
if hl_pocket:
try:
pocket_resi_list, pocket_resname_list = confidence.select_pocket_residue(temp_file_path)
pocket_df = dataframes.get_resi_bfactor(temp_file_path, resi_list=pocket_resi_list)
if not pocket_df.empty:
avg_pocket_confidence = pocket_df["Prediction Confidence"].mean()
structure_summary += f"\n=== BINDING POCKET ANALYSIS ===\n"
structure_summary += f"β’ Average Pocket Confidence (pLDDT): {avg_pocket_confidence:.2f}\n"
structure_summary += f"β’ Number of pocket residues: {len(pocket_resi_list)}\n"
structure_summary += f"β’ Binding Residues: {','.join(pocket_resname_list)}\n"
else:
structure_summary += f"\n=== BINDING POCKET ANALYSIS ===\n"
structure_summary += "β’ No pocket data available\n"
except Exception as e:
structure_summary += f"\n=== BINDING POCKET ANALYSIS ===\n"
structure_summary += f"β’ Error analyzing pocket: {e}\n"
except Exception as e:
structure_summary += f"\nError in structure analysis: {e}\n"
return structure_summary
# Streamlit App
st.set_page_config(page_title="Protein Structure + Literature Analysis", layout="wide")
st.markdown("# Protein Structure + Literature Analysis")
st.markdown("*Analyze protein structures with integrated literature insights*")
# Initialize RAG pipeline
if st.session_state.rag_pipeline is None:
st.session_state.rag_pipeline = initialize_rag_pipeline()
rag_pipeline = st.session_state.rag_pipeline
# Sidebar for file uploads and settings
st.sidebar.title("π Upload Files")
# PDB file upload
pdb_file = st.sidebar.file_uploader("Choose a PDB file", type=['pdb'])
pdb_code = st.sidebar.text_input("Or enter PDB Code", value=None)
# PDF upload for RAG
st.sidebar.markdown("---")
st.sidebar.markdown("**π Literature Analysis (Optional)**")
if rag_pipeline is None:
st.sidebar.error("β οΈ RAG pipeline not initialized. Please provide OpenAI API key.")
else:
st.sidebar.success("β
RAG pipeline ready")
pdf_docs = st.sidebar.file_uploader(
"Upload PDF literature (max 10MB each)",
accept_multiple_files=True,
type=['pdf'],
help="Upload relevant research papers for contextual analysis"
)
# Process PDFs if uploaded
literature_available = False
if pdf_docs and rag_pipeline:
if st.sidebar.button("π Process Literature"):
with st.sidebar.spinner("Processing PDFs..."):
success, message = rag_pipeline.process_literature(pdf_docs)
if success:
st.sidebar.success(message)
literature_available = True
else:
st.sidebar.error(message)
# Check if literature is already available
if rag_pipeline and rag_pipeline.is_literature_available():
literature_available = True
st.sidebar.info("π Literature database ready")
# Visualization settings
st.sidebar.markdown("---")
st.sidebar.title("π§ View Settings")
surf_transp = st.sidebar.slider("Surface Transparency", 0.0, 1.0, 0.5)
hl_chain = st.sidebar.text_input("Highlight Chain", value="A")
hl_resi_list = st.sidebar.multiselect("Highlight Residues", options=list(range(1, 100)))
hl_pocket = st.sidebar.checkbox("Highlight Pocket", value=False)
hl_ligand = st.sidebar.checkbox("Highlight Ligand", value=False)
hl_color = st.sidebar.text_input("Highlight Color", value="red")
bb_color = st.sidebar.text_input("Backbone Color", value="orange")
lig_color = st.sidebar.text_input("Ligand Color", value="white")
# Main layout
col1, col2 = st.columns([2.5, 1.5])
with col1:
st.subheader("𧬠3D Structure Visualization")
# Setup 3D visualization
width, height = 700, 600
cartoon_radius, stick_radius = 0.2, 0.2
if pdb_file:
with NamedTemporaryFile(delete=False, suffix=".pdb") as temp_file:
temp_file.write(pdb_file.getvalue())
temp_file_path = temp_file.name
view = py3Dmol.view(width=width, height=height)
view.addModel(pdb_file.getvalue().decode("utf-8"))
view.zoomTo()
elif pdb_code:
view = py3Dmol.view(query=f"pdb:{pdb_code.lower()}", width=width, height=height)
temp_file_path = f"{pdb_code.lower()}.pdb" # This might need special handling
else:
# Default structure
view = py3Dmol.view(width=width, height=height)
pdb_file_path = "./showcase/8SLG_relaxed_plddt.pdb"
with io.open(pdb_file_path, mode="r", encoding="utf-8") as f:
pdb_content = f.read()
temp_file_path = pdb_file_path
view.addModel(pdb_content)
view.zoomTo()
# Apply styling
view.setStyle({"cartoon": {"style": "oval", "color": bb_color, "thickness": cartoon_radius}})
view.addSurface(py3Dmol.VDW, {"opacity": surf_transp, "color": bb_color}, {"hetflag": False})
view.addStyle({"elem": "C", "hetflag": True}, {"stick": {"color": lig_color, "radius": stick_radius}})
view.addStyle({"hetflag": True}, {"stick": {"radius": stick_radius}})
if hl_pocket:
view.addStyle({'within': {'distance': '5.5', 'sel': {'resn': 'UNK', "elem": "C"}}},
{'stick': {'colorscheme': 'white', "radius": stick_radius}})
for hl_resi in hl_resi_list:
view.addStyle({"chain": hl_chain, "resi": hl_resi, "elem": "C"},
{"stick": {'colorscheme': "white", "radius": stick_radius}})
showmol(view, height=height, width=width)
with col2:
st.subheader("π Confidence Metrics")
if 'temp_file_path' in locals():
try:
if hl_pocket:
pocket_resi_list, pocket_resname_list = confidence.select_pocket_residue(temp_file_path)
pocket_df = dataframes.get_resi_bfactor(temp_file_path, resi_list=pocket_resi_list)
st.write("**Pocket Residues**")
st.dataframe(pocket_df, height=200)
if hl_ligand:
ligand_df = dataframes.get_resi_bfactor(temp_file_path, resi_name=["UNK", "LIG", "LG1"])
st.write("**Ligand Confidence**")
st.dataframe(ligand_df, height=200)
except Exception as e:
st.error(f"Error loading confidence data: {e}")
else:
st.info("Load a PDB structure to see confidence metrics")
# Analysis Section
st.markdown("---")
# Two-column layout for analysis
analysis_col1, analysis_col2 = st.columns([1, 1])
with analysis_col1:
st.subheader("π€ AI-Powered Analysis")
# Generate analysis button
if st.button("π¬ Generate Structure Analysis", type="primary", use_container_width=True):
if not rag_pipeline:
st.error("RAG pipeline not available. Please check your OpenAI API key.")
elif 'temp_file_path' not in locals():
st.error("No protein structure loaded")
else:
with st.spinner("Analyzing structure and searching literature..."):
# Gather structural data
structure_data = gather_structure_data(
temp_file_path, hl_ligand, hl_pocket, hl_chain, hl_resi_list
)
# Generate analysis
if literature_available:
# Search literature for relevant context
literature_query = f"protein ligand binding confidence prediction accuracy validation {hl_chain}"
literature_context = rag_pipeline.search_literature(literature_query)
analysis = rag_pipeline.generate_analysis_with_gpt(
structure_data, literature_context, has_literature=True
)
st.success("β
Analysis generated with literature context")
else:
analysis = rag_pipeline.generate_analysis_with_gpt(
structure_data, has_literature=False
)
st.info("βΉοΈ Analysis generated without literature context")
# Display results
st.markdown("### π Analysis Report")
st.markdown(analysis)
with analysis_col2:
st.subheader("π¬ Interactive Q&A")
if literature_available and rag_pipeline:
st.success("π Literature-enhanced Q&A available")
user_question = st.text_input(
"Ask about your structure + literature:",
placeholder="e.g., What does literature say about this confidence score?"
)
if user_question and st.button("π Get Answer", use_container_width=True):
with st.spinner("Searching literature and generating answer..."):
# Get current structure context
if 'temp_file_path' in locals():
structure_context = gather_structure_data(
temp_file_path, hl_ligand, hl_pocket, hl_chain, hl_resi_list
)
else:
structure_context = "No structure currently loaded"
# Generate answer
answer = rag_pipeline.answer_question(user_question, structure_context)
st.markdown("### π‘ Answer")
st.markdown(answer)
elif rag_pipeline:
st.info("π Upload and process literature PDFs to enable Q&A")
# Basic structure-only Q&A
user_question = st.text_input(
"Ask about structure analysis:",
placeholder="e.g., What do these confidence scores mean?"
)
if user_question and st.button("π Get Answer", use_container_width=True):
with st.spinner("Generating answer..."):
if temp_file_path:
structure_context = gather_structure_data(
temp_file_path, hl_ligand, hl_pocket, hl_chain, hl_resi_list
)
# Generate structure-only answer
answer = rag_pipeline.generate_analysis_with_gpt(
f"Question: {user_question}\n\nContext: {structure_context}"
)
st.markdown("### π‘ Answer")
st.markdown(answer)
else:
st.error("No structure loaded for analysis")
else:
st.warning("β οΈ Q&A requires OpenAI API key")
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: #666;'>
<small>Built with Streamlit</small>
</div>
""",
unsafe_allow_html=True
)