File size: 64,386 Bytes
0444618 | 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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 | """
BioAgent: AI-Powered Bioinformatics Demo
Deployable on HuggingFace Spaces with Claude Scientific Skills integration
"""
import os
import streamlit as st
import anthropic
import requests
import json
import time
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from functools import lru_cache
from typing import Optional
import numpy as np
from io import StringIO
import re
from dataclasses import dataclass
from typing import Tuple
# ============================================================================
# PAGE CONFIG & STYLING
# ============================================================================
st.set_page_config(
page_title="BioAgent | AI Bioinformatics",
page_icon="π§¬",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for a distinctive scientific aesthetic
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&family=Source+Sans+3:wght@300;400;600;700&display=swap');
:root {
--bg-primary: #0a0e17;
--bg-secondary: #111827;
--accent-cyan: #06b6d4;
--accent-emerald: #10b981;
--accent-violet: #8b5cf6;
--text-primary: #f1f5f9;
--text-muted: #94a3b8;
--border-color: #1e293b;
}
.stApp {
background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
}
.main-header {
font-family: 'Source Sans 3', sans-serif;
font-size: 2.5rem;
font-weight: 700;
background: linear-gradient(90deg, var(--accent-cyan), var(--accent-emerald));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 0.5rem;
}
.sub-header {
font-family: 'Source Sans 3', sans-serif;
color: var(--text-muted);
font-size: 1.1rem;
font-weight: 300;
letter-spacing: 0.5px;
}
.module-card {
background: rgba(17, 24, 39, 0.8);
border: 1px solid var(--border-color);
border-radius: 12px;
padding: 1.5rem;
margin: 1rem 0;
backdrop-filter: blur(10px);
}
.metric-box {
background: linear-gradient(135deg, rgba(6, 182, 212, 0.1), rgba(16, 185, 129, 0.1));
border: 1px solid rgba(6, 182, 212, 0.3);
border-radius: 8px;
padding: 1rem;
text-align: center;
}
.code-output {
font-family: 'JetBrains Mono', monospace;
background: #0d1117;
border: 1px solid #30363d;
border-radius: 8px;
padding: 1rem;
font-size: 0.85rem;
overflow-x: auto;
}
.skill-badge {
display: inline-block;
background: rgba(139, 92, 246, 0.2);
border: 1px solid rgba(139, 92, 246, 0.5);
color: #a78bfa;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-size: 0.75rem;
font-family: 'JetBrains Mono', monospace;
margin-right: 0.5rem;
margin-bottom: 0.5rem;
}
.status-running {
color: var(--accent-cyan);
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
/* Sidebar styling */
[data-testid="stSidebar"] {
background: rgba(10, 14, 23, 0.95);
border-right: 1px solid var(--border-color);
}
/* Input fields */
.stTextInput input, .stTextArea textarea, .stSelectbox select {
background: var(--bg-secondary) !important;
border: 1px solid var(--border-color) !important;
color: var(--text-primary) !important;
font-family: 'Source Sans 3', sans-serif !important;
}
/* Buttons */
.stButton > button {
background: linear-gradient(90deg, var(--accent-cyan), var(--accent-emerald));
color: var(--bg-primary);
font-weight: 600;
border: none;
border-radius: 8px;
padding: 0.5rem 2rem;
font-family: 'Source Sans 3', sans-serif;
transition: transform 0.2s, box-shadow 0.2s;
}
.stButton > button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 20px rgba(6, 182, 212, 0.4);
}
/* Expander */
.streamlit-expanderHeader {
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: 8px;
}
/* Tabs */
.stTabs [data-baseweb="tab-list"] {
gap: 8px;
background: transparent;
}
.stTabs [data-baseweb="tab"] {
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: 8px 8px 0 0;
color: var(--text-muted);
font-family: 'Source Sans 3', sans-serif;
}
.stTabs [aria-selected="true"] {
background: linear-gradient(90deg, rgba(6, 182, 212, 0.2), rgba(16, 185, 129, 0.2));
border-color: var(--accent-cyan);
color: var(--text-primary);
}
</style>
""", unsafe_allow_html=True)
# ============================================================================
# SKILL MANAGEMENT
# ============================================================================
SKILL_BASE_URL = "https://raw.githubusercontent.com/K-Dense-AI/claude-scientific-skills/main"
SKILL_REGISTRY = {
"biopython": {
"path": "scientific-packages/biopython",
"description": "Sequence analysis, alignment, parsing biological data",
"icon": "π§¬"
},
"rdkit": {
"path": "scientific-packages/rdkit",
"description": "Molecular properties, SMILES, fingerprints, drug discovery",
"icon": "π"
},
"scanpy": {
"path": "scientific-packages/scanpy",
"description": "Single-cell RNA-seq analysis, clustering, visualization",
"icon": "π¬"
},
"pubmed": {
"path": "scientific-databases/pubmed",
"description": "Literature search, citation retrieval",
"icon": "π"
},
"uniprot": {
"path": "scientific-databases/uniprot",
"description": "Protein sequences, annotations, functional data",
"icon": "π·"
},
"chembl": {
"path": "scientific-databases/chembl",
"description": "Bioactive molecules, drug targets, bioassays",
"icon": "βοΈ"
},
"pubchem": {
"path": "scientific-databases/pubchem",
"description": "Chemical structures, properties, bioactivities",
"icon": "π§ͺ"
}
}
@lru_cache(maxsize=20)
def fetch_skill(skill_key: str) -> str:
"""Fetch a SKILL.md from K-Dense GitHub repo with caching"""
if skill_key not in SKILL_REGISTRY:
return ""
skill_path = SKILL_REGISTRY[skill_key]["path"]
url = f"{SKILL_BASE_URL}/{skill_path}/SKILL.md"
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
return response.text
except requests.RequestException:
pass
return ""
def get_combined_skills(skill_keys: list[str]) -> str:
"""Combine multiple skills into a single context block"""
skills_content = []
for key in skill_keys:
content = fetch_skill(key)
if content:
skills_content.append(f"### {SKILL_REGISTRY[key]['icon']} {key.upper()} SKILL\n\n{content}")
return "\n\n---\n\n".join(skills_content)
# ============================================================================
# API INTEGRATIONS (Real Data)
# ============================================================================
class PubChemAPI:
"""Real PubChem API integration"""
BASE_URL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug"
@staticmethod
def search_compound(query: str, max_results: int = 5) -> list[dict]:
"""Search for compounds by name"""
try:
# Get CIDs
url = f"{PubChemAPI.BASE_URL}/compound/name/{query}/cids/JSON"
response = requests.get(url, timeout=10)
if response.status_code != 200:
return []
cids = response.json().get("IdentifierList", {}).get("CID", [])[:max_results]
compounds = []
for cid in cids:
# Get properties
prop_url = f"{PubChemAPI.BASE_URL}/compound/cid/{cid}/property/MolecularFormula,MolecularWeight,XLogP,TPSA,HBondDonorCount,HBondAcceptorCount/JSON"
prop_response = requests.get(prop_url, timeout=10)
if prop_response.status_code == 200:
props = prop_response.json().get("PropertyTable", {}).get("Properties", [{}])[0]
def safe_float(val, default=0.0):
"""Safely convert to float, handling None and strings"""
if val is None:
return default
try:
return float(val)
except (ValueError, TypeError):
return default
def safe_int(val, default=0):
"""Safely convert to int, handling None and strings"""
if val is None:
return default
try:
return int(val)
except (ValueError, TypeError):
return default
compounds.append({
"cid": cid,
"formula": props.get("MolecularFormula") or "N/A",
"mw": safe_float(props.get("MolecularWeight")),
"logp": safe_float(props.get("XLogP")),
"tpsa": safe_float(props.get("TPSA")),
"hbd": safe_int(props.get("HBondDonorCount")),
"hba": safe_int(props.get("HBondAcceptorCount")),
"image_url": f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/{cid}/PNG"
})
return compounds
except Exception as e:
st.error(f"PubChem API error: {e}")
return []
@staticmethod
def get_structure_image(cid: int) -> str:
"""Get 2D structure image URL"""
return f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/{cid}/PNG?image_size=300x300"
class PubMedAPI:
"""Real PubMed/NCBI E-utilities integration"""
BASE_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils"
@staticmethod
def search_literature(query: str, max_results: int = 10) -> list[dict]:
"""Search PubMed for articles"""
try:
# Search
search_url = f"{PubMedAPI.BASE_URL}/esearch.fcgi"
search_params = {
"db": "pubmed",
"term": query,
"retmax": max_results,
"retmode": "json",
"sort": "relevance"
}
search_response = requests.get(search_url, params=search_params, timeout=10)
if search_response.status_code != 200:
return []
pmids = search_response.json().get("esearchresult", {}).get("idlist", [])
if not pmids:
return []
# Fetch details
fetch_url = f"{PubMedAPI.BASE_URL}/esummary.fcgi"
fetch_params = {
"db": "pubmed",
"id": ",".join(pmids),
"retmode": "json"
}
fetch_response = requests.get(fetch_url, params=fetch_params, timeout=10)
if fetch_response.status_code != 200:
return []
results = fetch_response.json().get("result", {})
articles = []
for pmid in pmids:
if pmid in results:
article = results[pmid]
articles.append({
"pmid": pmid,
"title": article.get("title", "N/A"),
"authors": ", ".join([a.get("name", "") for a in article.get("authors", [])[:3]]),
"journal": article.get("source", "N/A"),
"pubdate": article.get("pubdate", "N/A"),
"url": f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/"
})
return articles
except Exception as e:
st.error(f"PubMed API error: {e}")
return []
class UniProtAPI:
"""Real UniProt API integration"""
BASE_URL = "https://rest.uniprot.org/uniprotkb"
@staticmethod
def search_protein(query: str, max_results: int = 5) -> list[dict]:
"""Search UniProt for proteins"""
try:
url = f"{UniProtAPI.BASE_URL}/search"
params = {
"query": query,
"format": "json",
"size": max_results,
"fields": "accession,id,protein_name,organism_name,length,sequence"
}
response = requests.get(url, params=params, timeout=15)
if response.status_code != 200:
return []
results = response.json().get("results", [])
proteins = []
for r in results:
protein_name = r.get("proteinDescription", {}).get("recommendedName", {}).get("fullName", {}).get("value", "N/A")
proteins.append({
"accession": r.get("primaryAccession", "N/A"),
"entry_name": r.get("uniProtkbId", "N/A"),
"protein_name": protein_name,
"organism": r.get("organism", {}).get("scientificName", "N/A"),
"length": r.get("sequence", {}).get("length", 0),
"sequence": r.get("sequence", {}).get("value", "")[:100] + "..."
})
return proteins
except Exception as e:
st.error(f"UniProt API error: {e}")
return []
# ============================================================================
# CLAUDE INTEGRATION
# ============================================================================
def call_claude_with_skills(
client: anthropic.Anthropic,
user_prompt: str,
skill_keys: list[str],
task_context: str = "",
model: str = "claude-sonnet-4-20250514"
) -> str:
"""Call Claude API with scientific skills context injected"""
skills_content = get_combined_skills(skill_keys)
system_prompt = f"""You are BioAgent, an expert AI bioinformatics assistant created by Excelra.
You help researchers with drug discovery, multi-omics analysis, and precision medicine workflows.
{f"TASK CONTEXT: {task_context}" if task_context else ""}
SCIENTIFIC SKILLS REFERENCE:
Use the following skill documentation to inform your responses. Follow the patterns,
best practices, and code examples described in these skills.
<skills>
{skills_content}
</skills>
RESPONSE GUIDELINES:
1. Be precise and scientifically accurate
2. When providing code, use the libraries and patterns from the skills above
3. Explain your reasoning step-by-step
4. Cite relevant databases or literature when applicable
5. Format output clearly with appropriate sections
6. If generating analysis code, make it executable and well-commented
"""
if client is None:
return "Error: No API client configured. Please enter your Anthropic API key."
try:
response = client.messages.create(
model=model,
max_tokens=4096,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}]
)
return response.content[0].text
except anthropic.APIError as e:
return f"API Error: {str(e)}"
except Exception as e:
return f"Error: {str(e)}"
# ============================================================================
# MODULE: DRUG DISCOVERY
# ============================================================================
def render_drug_discovery_module(client: anthropic.Anthropic):
"""Drug Discovery Pipeline Module"""
st.markdown("### π Drug Discovery Pipeline")
st.markdown("Search compounds, analyze molecular properties, and get AI-powered insights.")
# Show active skills
st.markdown(
'<span class="skill-badge">rdkit</span>'
'<span class="skill-badge">pubchem</span>'
'<span class="skill-badge">chembl</span>',
unsafe_allow_html=True
)
col1, col2 = st.columns([2, 1])
with col1:
compound_query = st.text_input(
"Search Compound",
placeholder="e.g., aspirin, imatinib, remdesivir",
key="drug_search"
)
analysis_type = st.selectbox(
"Analysis Type",
["Drug-likeness Assessment", "ADMET Prediction", "Target Identification", "Lead Optimization"]
)
with col2:
st.markdown("**Quick Examples:**")
st.caption("Click to copy, then paste:")
st.code("aspirin", language=None)
st.code("imatinib", language=None)
st.code("metformin", language=None)
if st.button("π Analyze Compound", key="analyze_drug") and compound_query:
with st.status("Running Drug Discovery Pipeline...", expanded=True) as status:
# Step 1: Fetch from PubChem
st.write("π‘ Querying PubChem database...")
compounds = PubChemAPI.search_compound(compound_query, max_results=3)
time.sleep(0.5)
if not compounds:
st.error(f"No compounds found for '{compound_query}'")
return
st.write(f"β Found {len(compounds)} compound(s)")
# Step 2: Display results
st.write("π§ͺ Analyzing molecular properties...")
time.sleep(0.3)
status.update(label="Pipeline complete!", state="complete", expanded=True)
# Results display
tabs = st.tabs(["π Properties", "πΌοΈ Structures", "π€ AI Analysis"])
with tabs[0]:
# Properties table
df = pd.DataFrame(compounds)
df.columns = ["CID", "Formula", "MW (g/mol)", "LogP", "TPSA (Ε²)", "HBD", "HBA", "Image URL"]
# Lipinski's Rule of Five assessment
st.markdown("#### Lipinski's Rule of Five")
for comp in compounds:
violations = 0
checks = []
if comp["mw"] <= 500:
checks.append("β
MW β€ 500")
else:
checks.append("β MW > 500")
violations += 1
if comp["logp"] <= 5:
checks.append("β
LogP β€ 5")
else:
checks.append("β LogP > 5")
violations += 1
if comp["hbd"] <= 5:
checks.append("β
HBD β€ 5")
else:
checks.append("β HBD > 5")
violations += 1
if comp["hba"] <= 10:
checks.append("β
HBA β€ 10")
else:
checks.append("β HBA > 10")
violations += 1
with st.expander(f"CID {comp['cid']} - {comp['formula']} ({violations} violations)"):
col1, col2 = st.columns(2)
with col1:
for check in checks:
st.markdown(check)
with col2:
st.metric("Molecular Weight", f"{comp['mw']:.2f}")
st.metric("LogP", f"{comp['logp']:.2f}")
st.dataframe(df.drop(columns=["Image URL"]), width='stretch')
with tabs[1]:
# Structure images
cols = st.columns(min(len(compounds), 3))
for i, comp in enumerate(compounds):
with cols[i % 3]:
st.image(comp["image_url"], caption=f"CID: {comp['cid']}")
with tabs[2]:
# AI Analysis
with st.spinner("π€ Generating AI analysis..."):
prompt = f"""Analyze the compound "{compound_query}" for {analysis_type.lower()}.
Available data from PubChem:
{json.dumps(compounds[0], indent=2)}
Provide:
1. Brief compound overview
2. Key molecular features relevant to {analysis_type.lower()}
3. Potential concerns or advantages
4. Recommended next steps for drug development
Be concise but thorough. Use scientific terminology appropriately."""
analysis = call_claude_with_skills(
client,
prompt,
["rdkit", "pubchem", "chembl"],
task_context=f"Drug discovery analysis for {compound_query}"
)
st.markdown(analysis)
# ============================================================================
# MODULE: SEQUENCE ANALYSIS
# ============================================================================
def render_sequence_analysis_module(client: anthropic.Anthropic):
"""Sequence Analysis Module"""
st.markdown("### 𧬠Sequence Analysis")
st.markdown("Analyze protein/DNA sequences with AI-powered insights.")
st.markdown(
'<span class="skill-badge">biopython</span>'
'<span class="skill-badge">uniprot</span>',
unsafe_allow_html=True
)
input_type = st.radio(
"Input Type",
["Search UniProt", "Paste Sequence"],
horizontal=True
)
if input_type == "Search UniProt":
# Initialize session state for search results and analysis
if "uniprot_results" not in st.session_state:
st.session_state.uniprot_results = []
if "protein_analysis" not in st.session_state:
st.session_state.protein_analysis = {}
col1, col2 = st.columns([3, 1])
with col1:
protein_query = st.text_input(
"Search Protein",
placeholder="e.g., BRCA1, insulin, p53 human",
key="protein_search"
)
with col2:
st.markdown("**Examples:**")
st.caption("Click to copy, then paste:")
st.code("p53 human", language=None)
st.code("EGFR human", language=None)
if st.button("π Search UniProt") and protein_query:
with st.spinner("Searching UniProt..."):
results = UniProtAPI.search_protein(protein_query)
st.session_state.uniprot_results = results
st.session_state.protein_analysis = {} # Clear previous analyses
st.session_state.uniprot_searched = True # Track that search was performed
# Display results from session state (persists across reruns)
proteins = st.session_state.uniprot_results
if proteins:
st.markdown("#### Search Results")
# Let user select which protein to analyze
protein_options = {f"{p['entry_name']} - {p['protein_name'][:40]}": p for p in proteins}
selected_name = st.selectbox(
"Select protein to analyze:",
options=list(protein_options.keys()),
key="protein_selector"
)
selected_protein = protein_options[selected_name]
accession = selected_protein['accession']
# Show protein details
st.markdown(f"**Accession:** {accession} | **Organism:** {selected_protein['organism']} | **Length:** {selected_protein['length']} aa")
with st.expander("View sequence"):
st.code(selected_protein['sequence'], language=None)
# Analyze button - outside expander for reliability
if accession in st.session_state.protein_analysis:
st.markdown("---")
st.markdown("#### π€ AI Analysis")
st.markdown(st.session_state.protein_analysis[accession])
else:
if st.button(f"π¬ Analyze {accession}", key="analyze_protein_btn"):
with st.spinner("Running sequence analysis with Claude..."):
prompt = f"""Analyze this protein:
Name: {selected_protein['protein_name']}
Organism: {selected_protein['organism']}
Length: {selected_protein['length']} amino acids
Sequence (truncated): {selected_protein['sequence']}
Provide:
1. Functional overview of this protein
2. Key domains/motifs likely present
3. Disease associations (if known)
4. Potential drug target considerations
Use your knowledge of protein biology and the BioPython skill for analysis approaches."""
analysis = call_claude_with_skills(
client,
prompt,
["biopython", "uniprot"],
task_context="Protein sequence analysis"
)
if analysis:
st.session_state.protein_analysis[accession] = analysis
st.rerun()
else:
st.error("Failed to generate analysis. Check API key.")
elif st.session_state.get("uniprot_searched", False):
st.warning("No proteins found. Try a different search term.")
else: # Paste Sequence
sequence = st.text_area(
"Paste Sequence (FASTA or raw)",
height=150,
placeholder=">sequence_name\nMKTVRQERLKSIVRILERSKEPVSGAQLA..."
)
analysis_options = st.multiselect(
"Analysis Options",
["Basic Statistics", "Composition Analysis", "Hydrophobicity Profile", "Secondary Structure Prediction"],
default=["Basic Statistics"]
)
if st.button("𧬠Analyze Sequence") and sequence:
# Parse sequence
clean_seq = "".join([line for line in sequence.split("\n") if not line.startswith(">")])
clean_seq = re.sub(r'[^A-Za-z]', '', clean_seq).upper()
if len(clean_seq) < 10:
st.error("Sequence too short. Please enter at least 10 residues.")
return
with st.spinner("Analyzing sequence..."):
# Basic calculations
seq_len = len(clean_seq)
# Amino acid composition
aa_counts = {aa: clean_seq.count(aa) for aa in "ACDEFGHIKLMNPQRSTVWY"}
aa_freq = {aa: count/seq_len*100 for aa, count in aa_counts.items()}
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Length", f"{seq_len} aa")
with col2:
# Approximate MW
avg_aa_mw = 110 # Da
st.metric("Est. MW", f"{seq_len * avg_aa_mw / 1000:.1f} kDa")
with col3:
# Basic pI estimate (very rough)
acidic = clean_seq.count('D') + clean_seq.count('E')
basic = clean_seq.count('K') + clean_seq.count('R') + clean_seq.count('H')
if basic > acidic:
pi_est = "Basic (>7)"
elif acidic > basic:
pi_est = "Acidic (<7)"
else:
pi_est = "~Neutral"
st.metric("Est. pI", pi_est)
if "Composition Analysis" in analysis_options:
st.markdown("#### Amino Acid Composition")
fig = px.bar(
x=list(aa_freq.keys()),
y=list(aa_freq.values()),
labels={"x": "Amino Acid", "y": "Frequency (%)"},
color=list(aa_freq.values()),
color_continuous_scale="Viridis"
)
fig.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)"
)
st.plotly_chart(fig, width='stretch')
# AI Analysis
prompt = f"""Analyze this protein sequence:
Sequence length: {seq_len} amino acids
Sequence: {clean_seq[:200]}{'...' if len(clean_seq) > 200 else ''}
Amino acid composition highlights:
- Most frequent: {max(aa_freq, key=aa_freq.get)} ({aa_freq[max(aa_freq, key=aa_freq.get)]:.1f}%)
- Charged residues (D,E,K,R): {sum(aa_freq[aa] for aa in 'DEKR'):.1f}%
- Hydrophobic (A,V,I,L,M,F,W): {sum(aa_freq[aa] for aa in 'AVILMFW'):.1f}%
Requested analyses: {', '.join(analysis_options)}
Provide insights on:
1. Likely protein type/family based on composition
2. Notable sequence features
3. Predicted structural characteristics
4. Suggestions for experimental validation"""
analysis = call_claude_with_skills(
client,
prompt,
["biopython", "uniprot"],
task_context="Sequence analysis"
)
st.markdown("#### AI Analysis")
st.markdown(analysis)
# ============================================================================
# MODULE: LITERATURE SEARCH
# ============================================================================
def render_literature_module(client: anthropic.Anthropic):
"""Literature Search Module"""
st.markdown("### π Literature Intelligence")
st.markdown("Search PubMed and get AI-synthesized insights from scientific literature.")
st.markdown(
'<span class="skill-badge">pubmed</span>',
unsafe_allow_html=True
)
col1, col2 = st.columns([3, 1])
with col1:
search_query = st.text_input(
"Search Query",
placeholder="e.g., CRISPR cancer therapy, single-cell RNA-seq tumor",
key="lit_search"
)
with col2:
max_results = st.slider("Max Results", 5, 20, 10)
search_type = st.radio(
"Search Mode",
["Standard Search", "AI-Powered Synthesis"],
horizontal=True
)
if st.button("π Search Literature") and search_query:
with st.spinner("Searching PubMed..."):
articles = PubMedAPI.search_literature(search_query, max_results)
if not articles:
st.warning("No articles found. Try different search terms.")
return
st.success(f"Found {len(articles)} articles")
if search_type == "Standard Search":
for article in articles:
with st.expander(f"π {article['title'][:80]}..."):
st.markdown(f"**Authors:** {article['authors']}")
st.markdown(f"**Journal:** {article['journal']}")
st.markdown(f"**Date:** {article['pubdate']}")
st.markdown(f"**PMID:** [{article['pmid']}]({article['url']})")
else: # AI Synthesis
st.markdown("#### π Article Results")
# Show articles in compact format
df = pd.DataFrame(articles)
df = df[["pmid", "title", "journal", "pubdate"]]
st.dataframe(df, width='stretch', hide_index=True)
st.markdown("#### π€ AI Synthesis")
with st.spinner("Synthesizing findings..."):
articles_text = "\n\n".join([
f"Title: {a['title']}\nAuthors: {a['authors']}\nJournal: {a['journal']} ({a['pubdate']})"
for a in articles
])
prompt = f"""Based on these PubMed search results for "{search_query}":
{articles_text}
Provide a synthesis that includes:
1. **Key Themes**: What are the main research directions evident from these papers?
2. **Recent Advances**: What new findings or methods appear most significant?
3. **Research Gaps**: Based on the titles, what areas might need more investigation?
4. **Suggested Reading**: Which 2-3 papers seem most foundational for someone new to this topic?
Be concise and scientifically rigorous. Note that you only have titles/metadata, not full texts."""
synthesis = call_claude_with_skills(
client,
prompt,
["pubmed"],
task_context=f"Literature synthesis for: {search_query}"
)
st.markdown(synthesis)
# ============================================================================
# MODULE: SINGLE-CELL ANALYSIS (Demo)
# ============================================================================
def render_single_cell_module(client: anthropic.Anthropic):
"""Single-Cell Analysis Demo Module"""
st.markdown("### π¬ Single-Cell Analysis Demo")
st.markdown("Interactive demonstration of single-cell RNA-seq analysis concepts.")
st.markdown(
'<span class="skill-badge">scanpy</span>',
unsafe_allow_html=True
)
st.info("π‘ This module demonstrates single-cell analysis concepts using simulated data. "
"For real analysis, connect your own datasets.")
# Generate demo data
np.random.seed(42)
n_cells = 500
n_genes = 50
# Simulate 4 cell clusters
cluster_centers = [
np.array([2, 2]),
np.array([-2, 2]),
np.array([2, -2]),
np.array([-2, -2])
]
cell_types = ["T cells", "B cells", "Monocytes", "NK cells"]
umap_coords = []
cell_labels = []
for i, center in enumerate(cluster_centers):
n_cluster = n_cells // 4
coords = np.random.randn(n_cluster, 2) * 0.5 + center
umap_coords.extend(coords)
cell_labels.extend([cell_types[i]] * n_cluster)
umap_coords = np.array(umap_coords)
# Create DataFrame
df = pd.DataFrame({
"UMAP1": umap_coords[:, 0],
"UMAP2": umap_coords[:, 1],
"Cell Type": cell_labels,
"n_genes": np.random.randint(500, 3000, len(cell_labels)),
"n_counts": np.random.randint(1000, 10000, len(cell_labels))
})
# Visualization options
color_by = st.selectbox(
"Color by",
["Cell Type", "n_genes", "n_counts"]
)
# Create plot
if color_by == "Cell Type":
fig = px.scatter(
df,
x="UMAP1",
y="UMAP2",
color="Cell Type",
color_discrete_sequence=px.colors.qualitative.Set2,
hover_data=["n_genes", "n_counts"],
title="UMAP Projection - Cell Type Clusters"
)
else:
fig = px.scatter(
df,
x="UMAP1",
y="UMAP2",
color=color_by,
color_continuous_scale="Viridis",
hover_data=["Cell Type", "n_genes", "n_counts"],
title=f"UMAP Projection - {color_by}"
)
fig.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
height=500
)
fig.update_traces(marker=dict(size=6, opacity=0.7))
st.plotly_chart(fig, width='stretch')
# Cluster statistics
st.markdown("#### Cluster Statistics")
stats = df.groupby("Cell Type").agg({
"n_genes": ["mean", "std"],
"n_counts": ["mean", "std"],
"UMAP1": "count"
}).round(1)
stats.columns = ["Avg Genes", "Std Genes", "Avg Counts", "Std Counts", "N Cells"]
st.dataframe(stats, width='stretch')
# AI interpretation
if st.button("π€ Get AI Interpretation"):
with st.spinner("Generating interpretation..."):
prompt = f"""Interpret this single-cell RNA-seq analysis result:
Dataset: {n_cells} cells, {n_genes} variable genes
Clusters identified: {', '.join(cell_types)}
Cluster statistics:
{stats.to_string()}
Provide:
1. Quality assessment of this dataset
2. Biological interpretation of the cell type distribution
3. Suggested downstream analyses
4. Potential validation experiments
This is simulated demonstration data, but analyze as if it were real."""
interpretation = call_claude_with_skills(
client,
prompt,
["scanpy"],
task_context="Single-cell RNA-seq analysis interpretation"
)
st.markdown(interpretation)
# ============================================================================
# MODULE: PATIENT STRATIFICATION (Multi-Omics VAE)
# ============================================================================
class MultiOmicsStratificationEngine:
"""
Multi-Omics Patient Stratification Engine
Based on UCB Multi-Omics Stratification Agent architecture.
Features:
- Simulated multi-omics data with causal relationships
- Gated encoders with attention mechanism
- UMAP visualization of patient clusters
- Survival analysis simulation
- Biomarker discovery
"""
def __init__(self, n_samples: int = 300, n_genes: int = 500, seed: int = 2025):
self.n_samples = n_samples
self.n_genes = n_genes
self.seed = seed
self.data = None
self.results = None
def simulate_data(self) -> dict:
"""Simulate multi-omics data with biological relationships"""
np.random.seed(self.seed)
# Patient subtypes: 0=Non-Responder, 1=Responder, 2=High-Risk
subtypes = np.random.choice([0, 1, 2], self.n_samples, p=[0.4, 0.4, 0.2])
# CNV: mostly diploid (0), some gains (+1) and losses (-1)
cnv = np.random.choice([-1, 0, 1], size=(self.n_samples, self.n_genes), p=[0.05, 0.9, 0.05])
# Mutations: sparse binary with subtype-specific patterns
mutations = np.zeros((self.n_samples, self.n_genes))
for i in range(self.n_samples):
base_muts = np.random.random(self.n_genes) < 0.02
if subtypes[i] == 0: # Non-responder: resistance mutations
base_muts[0:50] = np.random.random(50) < 0.3
elif subtypes[i] == 1: # Responder: sensitivity mutations
base_muts[50:100] = np.random.random(50) < 0.3
else: # High-risk: inflammatory signature
base_muts[100:150] = np.random.random(50) < 0.25
mutations[i] = base_muts.astype(int)
# Gene Expression: baseline + CNV effect + subtype signature + noise
gex = np.random.randn(self.n_samples, self.n_genes) * 0.5
gex += cnv * 0.8 # CNV affects expression
# Subtype-specific expression signatures
for i in range(self.n_samples):
if subtypes[i] == 0:
gex[i, 0:50] += 1.5 # Resistance genes up
gex[i, 200:250] -= 1.0 # Immune genes down
elif subtypes[i] == 1:
gex[i, 50:100] += 1.2 # Sensitivity pathway
gex[i, 200:250] += 1.5 # Immune activation
else:
gex[i, 100:150] += 2.0 # Inflammatory genes
gex[i, 150:200] += 1.0 # Cytokine storm signature
# Standardize GEX
gex = (gex - gex.mean(axis=0)) / (gex.std(axis=0) + 1e-8)
# Patient metadata
patient_ids = [f"PT{i:04d}" for i in range(self.n_samples)]
ages = np.random.normal(55, 12, self.n_samples).astype(int)
ages = np.clip(ages, 25, 85)
self.data = {
'gex': gex,
'mutations': mutations,
'cnv': cnv,
'subtypes': subtypes,
'patient_ids': patient_ids,
'ages': ages,
'n_samples': self.n_samples,
'n_genes': self.n_genes
}
return self.data
def run_stratification(self) -> dict:
"""
Run patient stratification using PCA-based approach.
(Simplified from full VAE for demo speed - real implementation uses PyTorch VAE)
"""
if self.data is None:
self.simulate_data()
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
# Combine modalities with learned weights (simulated attention)
# In full model, this comes from attention mechanism
gex_weight = 0.55
mut_weight = 0.30
cnv_weight = 0.15
# Create combined feature matrix
combined = np.hstack([
self.data['gex'] * gex_weight,
self.data['mutations'] * mut_weight,
self.data['cnv'] * cnv_weight
])
# PCA for latent space (VAE substitute for demo)
pca = PCA(n_components=32)
latent = pca.fit_transform(combined)
# UMAP for visualization
from sklearn.manifold import TSNE
# Using t-SNE as fallback if UMAP not available
try:
import umap.umap_ as umap_lib
reducer = umap_lib.UMAP(n_neighbors=15, min_dist=0.1, random_state=42)
embedding = reducer.fit_transform(latent)
except ImportError:
tsne = TSNE(n_components=2, random_state=42, perplexity=30)
embedding = tsne.fit_transform(latent)
# Train classifier
X_train, X_test, y_train, y_test, idx_train, idx_test = train_test_split(
latent, self.data['subtypes'], np.arange(self.n_samples),
test_size=0.2, stratify=self.data['subtypes'], random_state=42
)
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
# Predictions
predictions = clf.predict(latent)
probabilities = clf.predict_proba(latent)
test_predictions = clf.predict(X_test)
test_accuracy = (test_predictions == y_test).mean()
# Biomarker discovery: feature importance from GEX
gex_importance = np.abs(pca.components_[:, :self.n_genes]).mean(axis=0)
top_biomarkers = np.argsort(gex_importance)[-20:]
# Simulate survival times
survival_times, events = self._simulate_survival(predictions)
self.results = {
'embedding': embedding,
'latent': latent,
'predictions': predictions,
'probabilities': probabilities,
'true_labels': self.data['subtypes'],
'test_accuracy': test_accuracy,
'attention_weights': np.array([gex_weight, mut_weight, cnv_weight]),
'top_biomarkers': top_biomarkers,
'biomarker_importance': gex_importance,
'survival_times': survival_times,
'survival_events': events,
'idx_test': idx_test
}
return self.results
def _simulate_survival(self, subtypes: np.ndarray) -> tuple:
"""Simulate survival times based on patient subtypes"""
np.random.seed(self.seed + 1)
times = []
events = []
for s in subtypes:
if s == 0: # Non-responder
t = np.random.exponential(6)
elif s == 1: # Responder
t = np.random.exponential(24)
else: # High-risk
t = np.random.exponential(10)
# Censor at 24 months
if t > 24:
times.append(24)
events.append(0)
else:
times.append(t)
events.append(1)
return np.array(times), np.array(events)
def render_patient_stratification_module(client: anthropic.Anthropic):
"""Patient Stratification Module - Multi-Omics Analysis"""
st.markdown("### π― Patient Stratification")
st.markdown("Multi-omics integration for clinical trial patient selection and risk stratification.")
st.markdown(
'<span class="skill-badge">scanpy</span>'
'<span class="skill-badge">biopython</span>'
'<span class="skill-badge">pytorch</span>',
unsafe_allow_html=True
)
# Configuration
st.markdown("#### βοΈ Configuration")
col1, col2, col3 = st.columns(3)
with col1:
n_patients = st.slider("Number of Patients", 100, 500, 300, step=50)
with col2:
n_genes = st.slider("Number of Genes", 200, 1000, 500, step=100)
with col3:
seed = st.number_input("Random Seed", value=2025, min_value=1)
data_source = st.radio(
"Data Source",
["π² Simulated Demo Data", "π Upload Real Data (Coming Soon)"],
horizontal=True
)
if "Upload" in data_source:
st.info("π€ File upload for real multi-omics data will be available in production. "
"Supported formats: CSV (GEX matrix), VCF (mutations), SEG (CNV).")
return
if st.button("π Run Stratification Pipeline", type="primary"):
# Initialize engine
engine = MultiOmicsStratificationEngine(n_samples=n_patients, n_genes=n_genes, seed=seed)
with st.status("Running Multi-Omics Stratification Pipeline...", expanded=True) as status:
st.write("𧬠Simulating multi-omics data...")
data = engine.simulate_data()
time.sleep(0.3)
st.write(f" β Generated {n_patients} patients Γ {n_genes} genes")
st.write(f" β Modalities: GEX, Mutations, CNV")
st.write("π§ Running stratification model...")
results = engine.run_stratification()
time.sleep(0.3)
st.write(f" β Test accuracy: {results['test_accuracy']:.1%}")
st.write("π Generating visualizations...")
time.sleep(0.2)
status.update(label="β
Pipeline Complete!", state="complete")
# Store in session state
st.session_state['strat_engine'] = engine
st.session_state['strat_results'] = results
st.session_state['strat_data'] = data
# Display results if available
if 'strat_results' in st.session_state:
results = st.session_state['strat_results']
data = st.session_state['strat_data']
engine = st.session_state['strat_engine']
# Metrics row
st.markdown("---")
st.markdown("#### π Key Metrics")
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Patients", data['n_samples'])
with col2:
st.metric("Test Accuracy", f"{results['test_accuracy']:.1%}")
with col3:
n_responders = (results['predictions'] == 1).sum()
st.metric("Predicted Responders", f"{n_responders} ({n_responders/data['n_samples']:.0%})")
with col4:
n_highrisk = (results['predictions'] == 2).sum()
st.metric("High-Risk Flagged", n_highrisk)
# Tabs for visualizations
tabs = st.tabs([
"πΊοΈ Patient Map",
"βοΈ Modality Attention",
"π Survival Analysis",
"𧬠Biomarkers",
"π€ AI Insights"
])
# Tab 1: Patient Stratification Map (UMAP)
with tabs[0]:
st.markdown("##### Patient Stratification Map")
st.caption("UMAP projection of learned patient representations")
labels = ['Non-Responder', 'Responder', 'High-Risk']
colors = ['#808080', '#2ecc71', '#e74c3c']
df_umap = pd.DataFrame({
'UMAP1': results['embedding'][:, 0],
'UMAP2': results['embedding'][:, 1],
'True Label': [labels[s] for s in data['subtypes']],
'Predicted': [labels[p] for p in results['predictions']],
'Patient ID': data['patient_ids'],
'Age': data['ages'],
'Responder Prob': results['probabilities'][:, 1]
})
color_by = st.radio("Color by:", ["Predicted", "True Label", "Responder Prob"], horizontal=True)
if color_by == "Responder Prob":
fig = px.scatter(
df_umap, x='UMAP1', y='UMAP2',
color='Responder Prob',
color_continuous_scale='RdYlGn',
hover_data=['Patient ID', 'Age', 'True Label', 'Predicted'],
title='Patient Stratification Map'
)
else:
fig = px.scatter(
df_umap, x='UMAP1', y='UMAP2',
color=color_by,
color_discrete_map={
'Non-Responder': '#808080',
'Responder': '#2ecc71',
'High-Risk': '#e74c3c'
},
hover_data=['Patient ID', 'Age', 'Responder Prob'],
title='Patient Stratification Map'
)
fig.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
height=500
)
fig.update_traces(marker=dict(size=8, opacity=0.7, line=dict(width=0.5, color='white')))
st.plotly_chart(fig, width='stretch')
# Confusion matrix
st.markdown("##### Classification Performance")
from sklearn.metrics import confusion_matrix, classification_report
cm = confusion_matrix(data['subtypes'], results['predictions'])
fig_cm = px.imshow(
cm,
labels=dict(x="Predicted", y="True", color="Count"),
x=labels,
y=labels,
color_continuous_scale='Blues',
text_auto=True
)
fig_cm.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
height=350
)
st.plotly_chart(fig_cm, width='stretch')
# Tab 2: Modality Attention
with tabs[1]:
st.markdown("##### Modality Importance")
st.caption("Which omics data layer contributed most to stratification decisions?")
attn = results['attention_weights']
fig_attn = go.Figure(data=[
go.Bar(
x=['Gene Expression', 'Mutations', 'CNV'],
y=attn,
marker_color=['#4cc9f0', '#f72585', '#7209b7'],
text=[f'{v:.0%}' for v in attn],
textposition='outside'
)
])
fig_attn.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
yaxis_title="Attention Weight",
yaxis_range=[0, 1],
height=400,
title="AI Attention: Which Data Drove the Decision?"
)
st.plotly_chart(fig_attn, width='stretch')
st.markdown(f"""
**Interpretation:**
- **Gene Expression** ({attn[0]:.0%}): Primary driver - captures pathway activity and treatment response signatures
- **Mutations** ({attn[1]:.0%}): Key for identifying resistance/sensitivity markers
- **CNV** ({attn[2]:.0%}): Supporting signal for dosage-sensitive genes
""")
# Tab 3: Survival Analysis
with tabs[2]:
st.markdown("##### Simulated Clinical Trial: Survival Analysis")
st.caption("Kaplan-Meier curves showing progression-free survival by predicted group")
fig_surv = go.Figure()
for group_idx, (group_name, color) in enumerate(zip(labels, colors)):
mask = results['predictions'] == group_idx
g_times = results['survival_times'][mask]
g_events = results['survival_events'][mask]
# Sort by time
sort_idx = np.argsort(g_times)
g_times = g_times[sort_idx]
g_events = g_events[sort_idx]
# Kaplan-Meier calculation
survival_prob = 1.0
probs_list = [1.0]
ts = [0.0]
n_at_risk = len(g_times)
for i, t in enumerate(g_times):
if g_events[i] == 1:
survival_prob *= (1 - 1/max(n_at_risk, 1))
probs_list.append(survival_prob)
ts.append(t)
n_at_risk -= 1
fig_surv.add_trace(go.Scatter(
x=ts, y=probs_list,
mode='lines',
name=f'{group_name} (n={mask.sum()})',
line=dict(color=color, width=2, shape='hv')
))
fig_surv.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
xaxis_title="Time (months)",
yaxis_title="Progression-Free Survival",
yaxis_range=[0, 1.05],
xaxis_range=[0, 25],
height=450,
title="Kaplan-Meier Survival Curves by Predicted Subtype",
legend=dict(x=0.7, y=0.95)
)
st.plotly_chart(fig_surv, width='stretch')
# Median survival times
st.markdown("**Median Progression-Free Survival:**")
col1, col2, col3 = st.columns(3)
for i, (col, label, color) in enumerate(zip([col1, col2, col3], labels, colors)):
mask = results['predictions'] == i
median_pfs = np.median(results['survival_times'][mask])
with col:
st.metric(label, f"{median_pfs:.1f} months")
# Tab 4: Biomarkers
with tabs[3]:
st.markdown("##### Biomarker Discovery")
st.caption("Genes most predictive of treatment response")
top_genes = results['top_biomarkers'][-15:]
importance = results['biomarker_importance'][top_genes]
fig_bio = go.Figure(data=[
go.Bar(
y=[f"Gene_{g:04d}" for g in top_genes],
x=importance,
orientation='h',
marker_color='#2ecc71'
)
])
fig_bio.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
xaxis_title="Importance Score",
height=450,
title="Top 15 Predictive Biomarkers"
)
st.plotly_chart(fig_bio, width='stretch')
# Gene distribution by subtype
st.markdown("##### Top Biomarker Expression by Subtype")
top_gene = top_genes[-1]
df_gene = pd.DataFrame({
'Expression': data['gex'][:, top_gene],
'Subtype': [labels[s] for s in data['subtypes']]
})
fig_box = px.box(
df_gene, x='Subtype', y='Expression',
color='Subtype',
color_discrete_map={
'Non-Responder': '#808080',
'Responder': '#2ecc71',
'High-Risk': '#e74c3c'
},
title=f"Gene_{top_gene:04d} Expression by Subtype"
)
fig_box.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
showlegend=False,
height=350
)
st.plotly_chart(fig_box, width='stretch')
# Tab 5: AI Insights
with tabs[4]:
st.markdown("##### π€ Clinical Insights")
if st.button("Generate Analysis", key="ai_strat"):
with st.spinner("Analyzing the stratification results..."):
# Build context
subtype_counts = pd.Series(results['predictions']).value_counts().sort_index()
top_genes = results['top_biomarkers']
prompt = f"""Analyze these patient stratification results from a multi-omics clinical trial analysis:
**Dataset Summary:**
- Total patients: {data['n_samples']}
- Features: {data['n_genes']} genes across 3 modalities (GEX, Mutations, CNV)
- Test accuracy: {results['test_accuracy']:.1%}
**Stratification Results:**
- Non-Responders: {subtype_counts.get(0, 0)} patients ({subtype_counts.get(0, 0)/data['n_samples']:.0%})
- Responders: {subtype_counts.get(1, 0)} patients ({subtype_counts.get(1, 0)/data['n_samples']:.0%})
- High-Risk: {subtype_counts.get(2, 0)} patients ({subtype_counts.get(2, 0)/data['n_samples']:.0%})
**Modality Importance:**
- Gene Expression: {results['attention_weights'][0]:.0%}
- Mutations: {results['attention_weights'][1]:.0%}
- CNV: {results['attention_weights'][2]:.0%}
**Survival Analysis:**
- Responders show significantly longer progression-free survival
- High-risk patients flagged for potential adverse events
**Top Biomarker Genes:** Gene_{top_genes[-1]:04d}, Gene_{top_genes[-2]:04d}, Gene_{top_genes[-3]:04d}
Provide clinical insights covering:
1. **Patient Selection**: Recommendations for trial enrollment criteria
2. **Risk Stratification**: How to use these results for patient monitoring
3. **Biomarker Validation**: Next steps for validating discovered markers
4. **Trial Design**: How this stratification could improve trial efficiency
5. **Regulatory Considerations**: Key points for FDA submission
Be specific and actionable. This is for a precision medicine clinical trial context."""
analysis = call_claude_with_skills(
client,
prompt,
["scanpy", "biopython"],
task_context="Multi-omics patient stratification for clinical trials"
)
st.markdown(analysis)
else:
st.info("Click 'Generate AI Analysis' to get Claude's interpretation of the stratification results.")
# ============================================================================
# MAIN APPLICATION
# ============================================================================
def main():
# Header
st.markdown('<h1 class="main-header">𧬠BioAgent</h1>', unsafe_allow_html=True)
st.markdown('<p class="sub-header">AI-Powered Bioinformatics β’ Drug Discovery β’ Multi-Omics Analysis</p>', unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.markdown("## βοΈ Configuration")
# Try to get API key from environment variable (HuggingFace Spaces Docker)
# or from Streamlit secrets, or let user input manually
api_key_from_env = os.environ.get("ANTHROPIC_API_KEY")
api_key_from_secrets = None
try:
api_key_from_secrets = st.secrets.get("ANTHROPIC_API_KEY")
except Exception:
pass # No secrets file exists
if api_key_from_env:
api_key = api_key_from_env
st.success("β API key loaded from environment")
elif api_key_from_secrets:
api_key = api_key_from_secrets
st.success("β API key loaded from secrets")
else:
api_key = st.text_input(
"Anthropic API Key",
type="password",
help="Enter your Anthropic API key to enable AI features"
)
if api_key:
st.success("β API key configured")
if api_key:
client = anthropic.Anthropic(api_key=api_key)
else:
st.warning("β οΈ Enter API key for AI features")
client = None
st.markdown("---")
st.markdown("## π§ͺ Analysis Modules")
module = st.radio(
"Select Module",
[
"π Drug Discovery",
"𧬠Sequence Analysis",
"π― Patient Stratification",
"π Literature Search",
"π¬ Single-Cell Demo"
],
label_visibility="collapsed"
)
st.markdown("---")
st.markdown("## π About")
st.markdown("""
**BioAgent** demonstrates Agentic
bioinformatics workflows using:
- Scientific Skills
- Multi-Omics Data Integration
- Real-time database APIs
- Interactive visualizations
- Anthropic
Developed by Aravind [Risentia].""")
st.markdown("---")
# Skills status
with st.expander("π§ Loaded Skills"):
for key, info in SKILL_REGISTRY.items():
st.markdown(f"{info['icon']} **{key}**")
st.caption(info['description'])
# Main content
if not client:
st.markdown("""
<div class="module-card">
<h3>π Welcome to BioAgent</h3>
<p>Enter your Anthropic API key in the sidebar to enable AI-powered analysis features.</p>
<p>You can still explore the data integrations (PubChem, PubMed, UniProt) without an API key.</p>
</div>
""", unsafe_allow_html=True)
# Show demo even without API key
st.markdown("### π― Quick Demo: PubChem Search")
demo_compound = st.text_input("Try searching a compound:", value="caffeine")
if st.button("Search PubChem"):
with st.spinner("Fetching from PubChem..."):
compounds = PubChemAPI.search_compound(demo_compound)
if compounds:
for c in compounds:
col1, col2 = st.columns([1, 2])
with col1:
st.image(c["image_url"])
with col2:
st.markdown(f"**CID:** {c['cid']}")
st.markdown(f"**Formula:** {c['formula']}")
st.markdown(f"**MW:** {c['mw']:.2f} g/mol")
st.markdown(f"**LogP:** {c['logp']:.2f}")
return
# Render selected module
if "Drug Discovery" in module:
render_drug_discovery_module(client)
elif "Sequence Analysis" in module:
render_sequence_analysis_module(client)
elif "Patient Stratification" in module:
render_patient_stratification_module(client)
elif "Literature" in module:
render_literature_module(client)
elif "Single-Cell" in module:
render_single_cell_module(client)
if __name__ == "__main__":
main()
|