Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- scripts/visualize.py +84 -0
scripts/visualize.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt # type: ignore
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
def create_design_plot(fasta_file_path):
|
| 6 |
+
"""
|
| 7 |
+
Parses the MPNN fasta file and returns a Matplotlib figure.
|
| 8 |
+
"""
|
| 9 |
+
data = []
|
| 10 |
+
with open(fasta_file_path, 'r') as f:
|
| 11 |
+
content = f.read()
|
| 12 |
+
|
| 13 |
+
# Extract score (local score, not global_score) and recovery from headers
|
| 14 |
+
# Example: >T=0.1, sample=1, score=0.7880, global_score=1.3897, seq_recovery=0.4295
|
| 15 |
+
# We want to capture the local "score=" value, not "global_score="
|
| 16 |
+
pattern = r"score=([\d\.]+)(?:,\s+global_score=[\d\.]+)?,\s+seq_recovery=([\d\.]+)"
|
| 17 |
+
|
| 18 |
+
for line in content.split('\n'):
|
| 19 |
+
if line.startswith(">") and "score=" in line and "seq_recovery" in line:
|
| 20 |
+
# Make sure we're not matching global_score by checking the pattern order
|
| 21 |
+
match = re.search(pattern, line)
|
| 22 |
+
if match:
|
| 23 |
+
# Verify we got the local score (should be before global_score if present)
|
| 24 |
+
score_val = float(match.group(1))
|
| 25 |
+
recovery_val = float(match.group(2))
|
| 26 |
+
data.append({
|
| 27 |
+
"Score": score_val,
|
| 28 |
+
"Recovery": recovery_val
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
df = pd.DataFrame(data)
|
| 32 |
+
|
| 33 |
+
# Create the Plot
|
| 34 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 35 |
+
ax.scatter(df['Score'], df['Recovery'], color='black', alpha=0.6, s=80, edgecolors='white')
|
| 36 |
+
|
| 37 |
+
# Highlight the Lead Candidate (Lowest Score) with a gold star
|
| 38 |
+
best = df.loc[df['Score'].idxmin()]
|
| 39 |
+
ax.scatter(best['Score'], best['Recovery'], marker='*', color='gold', s=400, edgecolors='black', linewidths=1.5, label="Lead Candidate", zorder=10)
|
| 40 |
+
|
| 41 |
+
ax.set_title("BroteinShake: Design Evolution (N=20)", fontsize=14)
|
| 42 |
+
ax.set_xlabel("ProteinMPNN Score (Lower = More Stable)", fontsize=10)
|
| 43 |
+
ax.set_ylabel("Sequence Recovery (%)", fontsize=10)
|
| 44 |
+
ax.legend()
|
| 45 |
+
ax.grid(True, linestyle='--', alpha=0.5)
|
| 46 |
+
|
| 47 |
+
return fig
|
| 48 |
+
|
| 49 |
+
def create_protein_viewer(pdb_file_path):
|
| 50 |
+
"""
|
| 51 |
+
Generates HTML/JS for a 3D protein viewer using 3Dmol.js.
|
| 52 |
+
"""
|
| 53 |
+
with open(pdb_file_path, 'r') as f:
|
| 54 |
+
pdb_content = f.read().replace('\n', '\\n')
|
| 55 |
+
|
| 56 |
+
html_content = f"""
|
| 57 |
+
<div id="container-3d" style="height: 500px; width: 100%; position: relative;"></div>
|
| 58 |
+
<script>
|
| 59 |
+
(function() {{
|
| 60 |
+
// Wait for 3Dmol to be loaded (from head)
|
| 61 |
+
function initViewer() {{
|
| 62 |
+
if (typeof $3Dmol === 'undefined') {{
|
| 63 |
+
setTimeout(initViewer, 100);
|
| 64 |
+
return;
|
| 65 |
+
}}
|
| 66 |
+
let element = document.getElementById('container-3d');
|
| 67 |
+
if (!element) return;
|
| 68 |
+
let config = {{ backgroundColor: 'white' }};
|
| 69 |
+
let viewer = $3Dmol.createViewer(element, config);
|
| 70 |
+
let pdbData = `{pdb_content}`;
|
| 71 |
+
viewer.addModel(pdbData, "pdb");
|
| 72 |
+
viewer.setStyle({{}}, {{cartoon: {{color: 'spectrum'}}}});
|
| 73 |
+
viewer.zoomTo();
|
| 74 |
+
viewer.render();
|
| 75 |
+
}}
|
| 76 |
+
if (document.readyState === 'loading') {{
|
| 77 |
+
document.addEventListener('DOMContentLoaded', initViewer);
|
| 78 |
+
}} else {{
|
| 79 |
+
initViewer();
|
| 80 |
+
}}
|
| 81 |
+
}})();
|
| 82 |
+
</script>
|
| 83 |
+
"""
|
| 84 |
+
return html_content
|