File size: 128,654 Bytes
9d901ad | 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 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 | """scPTR β Single-Cell Post-Transcriptional Regulatory Decomposition."""
from __future__ import annotations
import io
import os
import tempfile
import traceback
import warnings
import matplotlib
matplotlib.use("Agg")
import json
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scanpy as sc
import streamlit as st
warnings.filterwarnings("ignore")
import scptr # noqa: E402 β imported after matplotlib backend set
st.set_page_config(
page_title="scPTR",
page_icon=None,
layout="wide",
initial_sidebar_state="expanded",
)
# ββ CSS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.markdown("""
<style>
/* animations */
@keyframes fadeUp {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes slideIn {
from { opacity: 0; transform: translateX(-8px); }
to { opacity: 1; transform: translateX(0); }
}
@keyframes countUp {
from { opacity: 0; transform: scale(0.92); }
to { opacity: 1; transform: scale(1); }
}
@keyframes stepPulse {
0%, 100% { box-shadow: 0 0 0 0 rgba(43,87,151,0.3); }
50% { box-shadow: 0 0 0 4px rgba(43,87,151,0.1); }
}
/* base */
html, body, [data-testid="stAppViewContainer"] {
background: #f2f3f5;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
font-size: 14px;
color: #1a1a1a;
}
#MainMenu, footer, header,
[data-testid="stToolbar"],
[data-testid="stDecoration"],
[data-testid="stStatusWidget"] { display: none !important; }
.block-container {
padding: 2rem 2.5rem !important;
max-width: 1080px;
}
/* sidebar */
[data-testid="stSidebar"] {
background: #ffffff;
border-right: 1px solid #d0d0d0;
}
[data-testid="stSidebar"] > div:first-child { padding: 0 !important; }
/* sidebar brand */
.sb-brand {
padding: 1.25rem 1.5rem 1rem;
border-bottom: 1px solid #e8e8e8;
}
.sb-name {
font-size: 22px;
font-weight: 800;
letter-spacing: -0.03em;
color: #2b5797;
}
.sb-tag {
font-size: 11px;
color: #888;
line-height: 1.5;
margin-top: 0.1rem;
}
/* sidebar step list */
.sb-steps { padding: 0.5rem 0; }
.sb-step {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.55rem 1.5rem;
border-left: 3px solid transparent;
}
.sb-step.active {
background: #eef2f9;
border-left-color: #2b5797;
animation: slideIn 0.25s ease both;
}
.sb-step.done { opacity: 0.6; }
.sb-step-num {
width: 22px; height: 22px;
border: 1.5px solid #ccc;
border-radius: 50%;
display: flex; align-items: center; justify-content: center;
font-size: 10px; font-weight: 700; color: #888;
flex-shrink: 0;
}
.sb-step.active .sb-step-num {
background: #2b5797; border-color: #2b5797; color: #fff;
animation: stepPulse 2.5s ease infinite;
}
.sb-step.done .sb-step-num {
background: #e5f3ec; border-color: #2d7d4b; color: #2d7d4b;
}
.sb-step-label { font-size: 12px; color: #555; }
.sb-step.active .sb-step-label { font-weight: 700; color: #1a1a1a; }
/* data info */
.sb-info {
margin: 0.75rem 1rem;
padding: 0.7rem 1rem;
background: #f7f8fa;
border: 1px solid #e0e0e0;
font-size: 12px; color: #444; line-height: 1.8;
animation: fadeUp 0.3s ease;
}
.sb-info b { color: #1a1a1a; font-weight: 700; }
.sb-ok { color: #2d7d4b; font-size: 11px; }
/* page title */
.pt { font-size: 22px; font-weight: 700; letter-spacing: -0.02em;
color: #1a1a1a; margin-bottom: 0.2rem;
animation: fadeUp 0.3s ease both; }
.pd { font-size: 13px; color: #555; line-height: 1.65;
margin-bottom: 1.5rem; animation: fadeUp 0.4s ease both; }
/* section label */
.sl {
font-size: 11px; font-weight: 700; letter-spacing: 0.1em;
text-transform: uppercase; color: #888;
margin: 1.5rem 0 0.6rem; padding-bottom: 0.35rem;
border-bottom: 1px solid #ebebeb;
}
/* cards */
.card {
background: #fff; border: 1px solid #ddd;
padding: 1.4rem 1.75rem; margin-bottom: 1.25rem;
animation: fadeUp 0.4s ease both;
transition: border-color 0.15s, box-shadow 0.15s;
}
.card:hover { border-color: #b0bac8; box-shadow: 0 2px 6px rgba(0,0,0,0.06); }
.card-title {
font-size: 11px; font-weight: 700; letter-spacing: 0.08em;
text-transform: uppercase; color: #777;
margin-bottom: 0.6rem; padding-bottom: 0.4rem;
border-bottom: 1px solid #ebebeb;
}
/* metric grid */
.mg {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
gap: 1px; background: #d0d0d0;
border: 1px solid #d0d0d0;
margin-bottom: 1.5rem;
animation: fadeUp 0.4s ease both;
}
.mc { background: #fff; padding: 1rem 1.25rem;
animation: fadeUp 0.35s ease both; }
.mc:nth-child(1) { animation-delay: 0.03s; }
.mc:nth-child(2) { animation-delay: 0.07s; }
.mc:nth-child(3) { animation-delay: 0.10s; }
.mc:nth-child(4) { animation-delay: 0.13s; }
.mc:nth-child(5) { animation-delay: 0.16s; }
.mc:nth-child(6) { animation-delay: 0.19s; }
.mc-label {
font-size: 10px; color: #888;
text-transform: uppercase; letter-spacing: 0.08em;
margin-bottom: 0.25rem;
}
.mc-val {
font-size: 23px; font-weight: 600; color: #1a1a1a;
font-family: "SF Mono","Fira Code",Consolas,monospace;
font-variant-numeric: tabular-nums;
animation: countUp 0.4s ease both;
}
.mc-sub { font-size: 11px; color: #aaa; margin-top: 0.1rem; }
.mc.blue .mc-val { color: #2b5797; }
.mc.green .mc-val { color: #2d7d4b; }
/* tables */
.tbl { width: 100%; border-collapse: collapse; font-size: 13px;
animation: fadeUp 0.4s ease both; }
.tbl th {
background: #f7f8fa; border-bottom: 2px solid #d0d0d0;
padding: 0.5rem 0.75rem; text-align: left;
font-size: 10px; font-weight: 700; letter-spacing: 0.08em;
text-transform: uppercase; color: #666;
}
.tbl td {
padding: 0.42rem 0.75rem; border-bottom: 1px solid #ebebeb;
font-family: "SF Mono","Fira Code",Consolas,monospace; font-size: 12px;
}
.tbl tr:last-child td { border-bottom: none; }
.tbl tr:hover td { background: #fafbfc; }
.tbl td.txt { font-family: inherit; font-size: 13px; }
/* status boxes */
.ibox { background: #eef2f9; border-left: 3px solid #2b5797;
padding: 0.7rem 1rem; margin: 0.75rem 0;
font-size: 13px; color: #2a3a5a; line-height: 1.6;
animation: fadeUp 0.3s ease both;
transition: background 0.15s; }
.ibox:hover { background: #e5ecf6; }
.sbox { background: #eaf5ef; border-left: 3px solid #2d7d4b;
padding: 0.7rem 1rem; margin: 0.75rem 0;
font-size: 13px; color: #1a3d28; line-height: 1.6;
animation: fadeUp 0.3s ease both;
transition: background 0.15s; }
.sbox:hover { background: #dff0e8; }
.ebox { background: #fdf0f0; border-left: 3px solid #c0392b;
padding: 0.7rem 1rem; margin: 0.75rem 0;
font-size: 13px; color: #5a1a1a; line-height: 1.6;
animation: fadeUp 0.3s ease both;
transition: background 0.15s; }
.ebox:hover { background: #f9e5e5; }
.wbox { background: #fef9f0; border-left: 3px solid #d4860a;
padding: 0.7rem 1rem; margin: 0.75rem 0;
font-size: 13px; color: #4a3010; line-height: 1.6;
animation: fadeUp 0.3s ease both;
transition: background 0.15s; }
.wbox:hover { background: #fdf1e0; }
/* radio as horizontal nav */
[data-testid="stSidebar"] [data-testid="stRadio"] { margin: 0.5rem 0.75rem; }
[data-testid="stSidebar"] [data-testid="stRadio"] > label { display: none !important; }
[data-testid="stSidebar"] [data-testid="stRadio"] > div {
display: flex !important; gap: 0 !important; flex-direction: row !important;
}
[data-testid="stSidebar"] [data-testid="stRadio"] label {
flex: 1 !important; text-align: center !important;
padding: 0.4rem 0 !important; margin: 0 !important;
background: #f4f4f4 !important; border: 1px solid #d0d0d0 !important;
border-right: none !important;
font-size: 10px !important; font-weight: 700 !important;
text-transform: uppercase !important; letter-spacing: 0.07em !important;
color: #666 !important; cursor: pointer !important;
transition: background 0.15s !important;
}
[data-testid="stSidebar"] [data-testid="stRadio"] label:last-of-type {
border-right: 1px solid #d0d0d0 !important;
}
[data-testid="stSidebar"] [data-testid="stRadio"] label:has(input:checked) {
background: #2b5797 !important; color: #fff !important;
border-color: #2b5797 !important;
}
[data-testid="stSidebar"] [data-testid="stRadio"] [data-baseweb="radio"] > div:first-child {
display: none !important;
}
/* buttons */
.stButton > button {
background: #2b5797 !important; color: #fff !important;
border: none !important; border-radius: 0 !important;
padding: 0.48rem 1.3rem !important;
font-size: 11px !important; font-weight: 700 !important;
letter-spacing: 0.06em !important; text-transform: uppercase !important;
transition: background 0.15s !important;
}
.stButton > button:hover { background: #1e3f6e !important; }
.stButton > button:active { background: #163060 !important; }
.stButton > button:disabled { background: #b0bac8 !important; }
.stDownloadButton > button {
background: #fff !important; color: #2b5797 !important;
border: 1.5px solid #2b5797 !important; border-radius: 0 !important;
font-size: 11px !important; font-weight: 700 !important;
letter-spacing: 0.04em !important; text-transform: uppercase !important;
}
.stDownloadButton > button:hover { background: #eef2f9 !important; }
/* form labels */
[data-testid="stSelectbox"] label,
[data-testid="stSlider"] label,
[data-testid="stNumberInput"] label,
[data-testid="stFileUploader"] label,
[data-testid="stCheckbox"] label,
[data-testid="stRadio"] label,
[data-testid="stTextInput"] label,
[data-testid="stTextArea"] label {
font-size: 11px !important; font-weight: 700 !important;
text-transform: uppercase !important; letter-spacing: 0.07em !important;
color: #555 !important;
}
[data-testid="stFileUploader"] {
border: 1.5px dashed #bbb !important;
background: #fafafa !important;
}
/* sidebar jump buttons β secondary style (override primary) */
[data-testid="stSidebar"] .stButton > button {
background: transparent !important; color: #2b5797 !important;
border: 1px solid #c8d4e8 !important;
font-size: 10px !important; padding: 0.3rem 0.75rem !important;
}
[data-testid="stSidebar"] .stButton > button:hover {
background: #eef2f9 !important; border-color: #2b5797 !important;
}
/* tabs */
[data-testid="stTabs"] { overflow-x: auto; }
[data-testid="stTabs"] > div > div:first-child {
flex-wrap: nowrap; white-space: nowrap;
}
[data-testid="stTabs"] button {
border-radius: 0 !important; font-size: 11px !important;
font-weight: 700 !important; text-transform: uppercase !important;
letter-spacing: 0.07em !important; white-space: nowrap !important;
}
[data-testid="stTabs"] button[aria-selected="true"] {
color: #2b5797 !important;
border-bottom-color: #2b5797 !important;
}
/* dividers */
hr { border: none; border-top: 1px solid #e0e0e0; margin: 1.25rem 0; }
/* images */
.stImage img { border: 1px solid #d0d0d0; display: block; }
/* expander */
[data-testid="stExpander"] { border: 1px solid #ddd !important; border-radius: 0 !important; }
/* hero (home page) */
.hero {
background: #fff; border: 1px solid #ddd;
padding: 2.25rem 2rem 1.75rem;
margin-bottom: 1.5rem;
animation: fadeUp 0.4s ease both;
}
.hero-title {
font-size: 34px; font-weight: 800; letter-spacing: -0.04em;
color: #1a1a1a; line-height: 1.1; margin-bottom: 0.5rem;
}
.hero-title .accent { color: #2b5797; }
.hero-sub {
font-size: 14px; color: #555; line-height: 1.65;
max-width: 600px; margin-bottom: 1.25rem;
}
.hero-eq {
display: inline-block;
background: #f7f8fa; border: 1px solid #ddd;
padding: 0.65rem 1.25rem;
font-family: "SF Mono","Fira Code",Consolas,monospace;
font-size: 15px; color: #2b5797; letter-spacing: 0.03em;
}
/* pipeline steps (home) */
.pipe { display: flex; margin: 1rem 0 1.5rem; }
.pipe-step {
flex: 1; background: #fff; border: 1px solid #ddd; border-right: none;
padding: 0.75rem 0.9rem;
animation: fadeUp 0.4s ease both;
transition: background 0.15s, border-color 0.15s;
}
.pipe-step:hover { background: #f7f8fa; border-color: #b8c4d4; }
.pipe-step:nth-child(1) { animation-delay: 0.05s; }
.pipe-step:nth-child(2) { animation-delay: 0.10s; }
.pipe-step:nth-child(3) { animation-delay: 0.15s; }
.pipe-step:nth-child(4) { animation-delay: 0.20s; }
.pipe-step:nth-child(5) { animation-delay: 0.25s; }
.pipe-step:last-child { border-right: 1px solid #ddd; }
.pipe-step.done { border-top: 2px solid #2d7d4b; }
.pipe-step.done .pipe-n { color: #2d7d4b; }
.pipe-step.active-step { border-top: 2px solid #2b5797; }
.pipe-step.active-step .pipe-n { color: #2b5797; }
.pipe-n { font-size: 10px; font-weight: 700; color: #999;
text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 0.2rem; }
.pipe-name { font-size: 12px; font-weight: 600; color: #1a1a1a; margin-bottom: 0.15rem; }
.pipe-desc { font-size: 11px; color: #777; line-height: 1.4; }
/* docs */
.doc-sec {
background: #fff; border: 1px solid #ddd;
padding: 1.5rem 1.75rem; margin-bottom: 1.25rem;
animation: fadeUp 0.4s ease both;
}
.doc-h2 {
font-size: 15px; font-weight: 700; color: #1a1a1a;
margin-bottom: 0.75rem; padding-bottom: 0.5rem;
border-bottom: 2px solid #2b5797;
}
.doc-h3 {
font-size: 12px; font-weight: 700; letter-spacing: 0.07em;
text-transform: uppercase; color: #555;
margin: 1.2rem 0 0.5rem;
}
.doc-p { font-size: 13px; color: #333; line-height: 1.75; margin-bottom: 0.6rem; }
.doc-code {
background: #f7f8fa; border: 1px solid #e0e0e0;
padding: 0.75rem 1rem;
font-family: "SF Mono","Fira Code",Consolas,monospace;
font-size: 12px; color: #1a1a1a;
white-space: pre; overflow-x: auto;
margin: 0.5rem 0 1rem;
}
.param-name { font-family: monospace; font-size: 12px; color: #2b5797; font-weight: 600; }
.param-type { font-size: 11px; color: #888; font-style: italic; }
.param-desc { font-size: 12px; color: #444; line-height: 1.5; }
</style>
""", unsafe_allow_html=True)
# ββ session state βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
for k, v in {
"page": "home",
"step": 1,
"adata": None,
"dataset_name": None,
"preprocessed": False,
"estimated": False,
"states_done": False,
"velocity_done": False,
"network_done": False,
"_tmps": [],
}.items():
if k not in st.session_state:
st.session_state[k] = v
# ββ helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def go(step: int) -> None:
st.session_state.step = step
def nav(page: str) -> None:
st.session_state.page = page
def fig_png(fig: plt.Figure, dpi: int = 150) -> bytes:
buf = io.BytesIO()
fig.savefig(buf, format="png", dpi=dpi, bbox_inches="tight", facecolor="white")
buf.seek(0)
return buf.read()
def make_tmp(suffix: str = ".h5ad") -> str:
fd, path = tempfile.mkstemp(suffix=suffix)
os.close(fd)
st.session_state._tmps.append(path)
return path
def clean_tmps() -> None:
for p in st.session_state._tmps:
try:
os.unlink(p)
except Exception:
pass
st.session_state._tmps = []
def mg(*metrics) -> str:
"""metric_grid(*[(label, value, sub, cls?), ...])"""
cells = []
for m in metrics:
lbl, val, sub = m[0], m[1], m[2]
cls = m[3] if len(m) > 3 else ""
cells.append(
f'<div class="mc {cls}">'
f'<div class="mc-label">{lbl}</div>'
f'<div class="mc-val">{val}</div>'
f'<div class="mc-sub">{sub}</div>'
f'</div>'
)
return f'<div class="mg">{"".join(cells)}</div>'
def reset_downstream(from_step: int) -> None:
if from_step <= 1:
st.session_state.preprocessed = False
st.session_state.estimated = False
st.session_state.states_done = False
st.session_state.velocity_done = False
st.session_state.network_done = False
elif from_step <= 2:
st.session_state.estimated = False
st.session_state.states_done = False
st.session_state.velocity_done = False
st.session_state.network_done = False
elif from_step <= 3:
st.session_state.states_done = False
st.session_state.velocity_done = False
st.session_state.network_done = False
# ββ sidebar βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
STEPS = ["Load Data", "Preprocess", "Estimate Rates", "Discover PT States", "Results"]
with st.sidebar:
st.markdown(
'<div class="sb-brand">'
'<div class="sb-name">scPTR</div>'
'<div class="sb-tag">Single-Cell Post-Transcriptional<br>Regulatory Decomposition</div>'
'</div>',
unsafe_allow_html=True,
)
_page_map = {"Home": "home", "Analysis": "analysis", "Docs": "docs"}
_page_rev = {v: k for k, v in _page_map.items()}
_nav_choice = st.radio(
"nav",
list(_page_map.keys()),
index=list(_page_map.keys()).index(_page_rev.get(st.session_state.page, "Home")),
horizontal=True,
label_visibility="collapsed",
key="sidebar_nav",
)
if _page_map[_nav_choice] != st.session_state.page:
st.session_state.page = _page_map[_nav_choice]
st.rerun()
# Show data summary on home/docs pages too
if st.session_state.page != "analysis" and st.session_state.adata is not None:
_a = st.session_state.adata
_done_count = sum([
st.session_state.preprocessed,
st.session_state.estimated,
st.session_state.states_done,
])
st.markdown(
f'<div class="sb-info">'
f'<b>{st.session_state.dataset_name}</b><br>'
f'{_a.n_obs:,} cells Β· {_a.n_vars:,} genes<br>'
f'<span class="sb-ok">{_done_count}/3 steps complete</span>'
f'</div>',
unsafe_allow_html=True,
)
if st.button("Resume Analysis β", use_container_width=True):
nav("analysis"); st.rerun()
if st.session_state.page == "analysis":
st.markdown('<hr style="margin:0.75rem 0">', unsafe_allow_html=True)
st.markdown('<div class="sb-steps">', unsafe_allow_html=True)
step = st.session_state.step
done = {
1: st.session_state.adata is not None,
2: st.session_state.preprocessed,
3: st.session_state.estimated,
4: st.session_state.states_done,
}
for i, label in enumerate(STEPS, 1):
if i == step:
cls = "active"
elif done.get(i, False):
cls = "done"
else:
cls = ""
num = "β" if (done.get(i, False) and i != step) else str(i)
# Clickable for completed steps and step 5 (always accessible after states)
can_jump = done.get(i, False) and i != step
if can_jump:
# Show step as a compact link button
col_btn, = st.columns([1])
if st.button(f"β© {label}", key=f"jump_{i}", use_container_width=True):
go(i); st.rerun()
else:
st.markdown(
f'<div class="sb-step {cls}">'
f'<div class="sb-step-num">{num}</div>'
f'<div class="sb-step-label">{label}</div>'
f'</div>',
unsafe_allow_html=True,
)
st.markdown('</div>', unsafe_allow_html=True)
if st.session_state.adata is not None:
adata = st.session_state.adata
lines = [
f"<b>{st.session_state.dataset_name}</b>",
f"{adata.n_obs:,} cells Β· {adata.n_vars:,} genes",
]
if st.session_state.preprocessed:
lines.append('<span class="sb-ok">β preprocessed</span>')
if st.session_state.estimated:
lines.append('<span class="sb-ok">β rates estimated</span>')
if st.session_state.states_done:
n_s = adata.obs["pt_state"].nunique() if "pt_state" in adata.obs else "?"
lines.append(f'<span class="sb-ok">β {n_s} PT states</span>')
if st.session_state.network_done:
lines.append('<span class="sb-ok">β network inferred</span>')
st.markdown(
f'<div class="sb-info">{"<br>".join(lines)}</div>',
unsafe_allow_html=True,
)
# ββ page routing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
page = st.session_state.page
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# HOME PAGE
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if page == "home":
st.markdown(
'<div class="hero">'
'<div class="hero-title">Single-cell <span class="accent">post-transcriptional</span><br>regulatory decomposition</div>'
'<div class="hero-sub">'
'scPTR estimates per-cell, per-gene mRNA degradation rates from standard scRNA-seq '
'spliced/unspliced counts. It uses the degradation rate Ξ³ as a primary analytical '
'axis β complementary to RNA velocity β to discover expression-invisible cell states, '
'compute post-transcriptional velocity, and infer RNA-binding protein networks.'
'</div>'
'<div class="hero-eq">Ξ³<sub>ig</sub> = Ξ²<sub>g</sub> Β· u<sub>ig</sub> / s<sub>ig</sub></div>'
'</div>',
unsafe_allow_html=True,
)
home_c1, home_c2 = st.columns([1, 2])
with home_c1:
if st.button("Start Analysis β"):
nav("analysis"); st.rerun()
with home_c2:
if st.session_state.adata is not None:
_prog_items = [
("Load Data", True),
("Preprocess", st.session_state.preprocessed),
("Estimate Rates", st.session_state.estimated),
("PT States", st.session_state.states_done),
("Results", st.session_state.states_done),
]
_pct = sum(1 for _, v in _prog_items if v) / len(_prog_items) * 100
bars = "".join(
f'<div style="flex:1;height:4px;background:{"#2d7d4b" if v else "#e0e0e0"};margin-right:2px"></div>'
for _, v in _prog_items
)
st.markdown(
f'<div style="font-size:11px;color:#888;text-transform:uppercase;letter-spacing:0.06em;margin-bottom:0.3rem">'
f'Analysis progress Β· {_pct:.0f}%</div>'
f'<div style="display:flex;align-items:center;gap:0">{bars}</div>'
f'<div style="font-size:12px;color:#555;margin-top:0.4rem">'
f'{st.session_state.dataset_name} β '
f'{st.session_state.adata.n_obs:,} cells Β· {st.session_state.adata.n_vars:,} genes</div>',
unsafe_allow_html=True,
)
if st.button("Resume β"):
nav("analysis"); st.rerun()
# Pipeline β dynamic done/active state
_s = st.session_state
_pipe_states = [
_s.adata is not None,
_s.preprocessed,
_s.estimated,
_s.states_done,
_s.states_done,
]
_next_step = next((i for i, v in enumerate(_pipe_states) if not v), len(_pipe_states))
def _pipe_cls(i):
if _pipe_states[i]: return "done"
if i == _next_step: return "active-step"
return ""
def _pipe_icon(i):
return "β " if _pipe_states[i] else ""
st.markdown('<div class="sl">Analysis pipeline</div>', unsafe_allow_html=True)
st.markdown(
'<div class="pipe">'
f'<div class="pipe-step {_pipe_cls(0)}"><div class="pipe-n">{_pipe_icon(0)}01</div><div class="pipe-name">Load Data</div>'
'<div class="pipe-desc">Upload .h5ad or use built-in pancreas / dentate gyrus datasets</div></div>'
f'<div class="pipe-step {_pipe_cls(1)}"><div class="pipe-n">{_pipe_icon(1)}02</div><div class="pipe-name">Preprocess</div>'
'<div class="pipe-desc">Filter genes, normalize counts, kNN graph, Gaussian smoothing</div></div>'
f'<div class="pipe-step {_pipe_cls(2)}"><div class="pipe-n">{_pipe_icon(2)}03</div><div class="pipe-name">Estimate Rates</div>'
'<div class="pipe-desc">Ξ² via quantile regression on u/s portraits; Ξ³ = Ξ² Β· u / s per cell</div></div>'
f'<div class="pipe-step {_pipe_cls(3)}"><div class="pipe-n">{_pipe_icon(3)}04</div><div class="pipe-name">PT States</div>'
'<div class="pipe-desc">Leiden clustering in Ξ³-space; PT velocity; RBPβtarget networks</div></div>'
f'<div class="pipe-step {_pipe_cls(4)}"><div class="pipe-n">{_pipe_icon(4)}05</div><div class="pipe-name">Results</div>'
'<div class="pipe-desc">UMAP visualization, gene rankings, download outputs</div></div>'
'</div>',
unsafe_allow_html=True,
)
col1, col2 = st.columns(2, gap="large")
with col1:
st.markdown('<div class="sl">Validation</div>', unsafe_allow_html=True)
_g = 'background:#eaf5ef'
st.markdown(
'<table class="tbl">'
'<thead><tr><th>Validation</th><th>Result</th></tr></thead>'
'<tbody>'
f'<tr style="{_g}"><td class="txt">sci-fate metabolic labeling</td><td><b>Ο = β0.81</b></td></tr>'
f'<tr><td class="txt">10x developmental half-lives</td><td>Ο = β0.33 to β0.40</td></tr>'
f'<tr><td class="txt">vs. scVelo steady-state</td><td>β0.37 (scPTR: β0.40)</td></tr>'
f'<tr><td class="txt">vs. velVI</td><td>β0.28 (scPTR: β0.40)</td></tr>'
f'<tr style="{_g}"><td class="txt">miRNA target enrichment</td><td><b>59% of 215 families</b>, p = 4.7Γ10β»βΆβ΅</td></tr>'
f'<tr style="{_g}"><td class="txt">DepMap CRISPR essentiality</td><td>hub RBPs, <b>p = 6.4Γ10β»β΅</b></td></tr>'
f'<tr><td class="txt">Subsampling robustness</td><td>r > 0.97 at 20%</td></tr>'
'</tbody></table>',
unsafe_allow_html=True,
)
with col2:
st.markdown('<div class="sl">Key findings</div>', unsafe_allow_html=True)
st.markdown(
'<div class="card" style="margin-bottom:0.75rem">'
'<div class="card-title">Expression-invisible states</div>'
'<div style="font-size:13px;color:#333;line-height:1.65">'
'3/8 pancreatic and 6/11 hippocampal cell types harbor post-transcriptional '
'subpopulations undetectable by expression analysis. Confirmed by zero-permutation '
'control (ARI β 0).'
'</div></div>'
'<div class="card" style="margin-bottom:0.75rem">'
'<div class="card-title">Temporal precedence</div>'
'<div style="font-size:13px;color:#333;line-height:1.65">'
'Degradation-rate changes precede expression changes for 54% of pancreas '
'transition genes (p < 10β»β΅β·) and 78% in dentate gyrus (p = 9.9Γ10β»ΒΉΒ³).'
'</div></div>'
'<div class="card">'
'<div class="card-title">RBP networks</div>'
'<div style="font-size:13px;color:#333;line-height:1.65">'
'Library-size-corrected inference identifies essential hub regulators '
'(HNRNPA1, YBX1, ELAVL1/HuR). Neuroblastoma shows 66% stabilizing edges.'
'</div></div>',
unsafe_allow_html=True,
)
st.markdown('<div class="sl">Input requirements</div>', unsafe_allow_html=True)
st.markdown(
'<div class="ibox">'
'scPTR requires an AnnData file (<code>.h5ad</code>) with two count matrix layers: '
'<code>spliced</code> and <code>unspliced</code>. These can be generated with '
'<strong>STARsolo</strong>, <strong>Alevin-fry</strong>, <strong>kallisto|bustools</strong>, '
'or <strong>velocyto</strong>. Raw (un-normalized) counts are expected.'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="wbox" style="font-size:12px">'
'<b>Session data lives in browser memory.</b> Keep this tab open while running analysis. '
'Refreshing the page will reset all results. Download your AnnData file in step 5 to save progress.'
'</div>',
unsafe_allow_html=True,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ANALYSIS β STEP 1: LOAD DATA
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
elif page == "analysis" and st.session_state.step == 1:
st.markdown('<div class="pt">Load Data <span style="font-size:13px;font-weight:400;color:#aaa;letter-spacing:0">β step 1 of 5</span></div>', unsafe_allow_html=True)
st.markdown(
'<div class="pd">Upload an AnnData file (.h5ad) containing <code>spliced</code> and '
'<code>unspliced</code> count layers, or start with a built-in example dataset.</div>',
unsafe_allow_html=True,
)
st.markdown('<div class="sl">Quick start</div>', unsafe_allow_html=True)
qs_c1, qs_c2, qs_c3 = st.columns(3, gap="large")
with qs_c1:
st.markdown(
'<div class="card" style="animation-delay:0.05s">'
'<div class="card-title">Pancreas</div>'
'<div style="font-size:13px;color:#333;line-height:1.6;margin-bottom:0.75rem">'
'Mouse endocrinogenesis Β· 3,696 cells<br>'
'<span style="color:#888;font-size:12px">Bastidas-Ponce et al. 2019</span>'
'</div>'
'</div>',
unsafe_allow_html=True,
)
if st.button("Load Pancreas", key="qs_pancreas", use_container_width=True):
with st.spinner("Downloading pancreas datasetβ¦"):
try:
_adata = scptr.datasets.pancreas()
st.session_state.adata = _adata
st.session_state.dataset_name = "Pancreas"
reset_downstream(1)
go(2); st.rerun()
except Exception as e:
st.markdown(f'<div class="ebox">Error: {e}</div>', unsafe_allow_html=True)
with qs_c2:
st.markdown(
'<div class="card" style="animation-delay:0.10s">'
'<div class="card-title">Dentate Gyrus</div>'
'<div style="font-size:13px;color:#333;line-height:1.6;margin-bottom:0.75rem">'
'Mouse hippocampal neurogenesis Β· 2,930 cells<br>'
'<span style="color:#888;font-size:12px">Hochgerner et al. 2018</span>'
'</div>'
'</div>',
unsafe_allow_html=True,
)
if st.button("Load Dentate Gyrus", key="qs_dg", use_container_width=True):
with st.spinner("Downloading dentate gyrus datasetβ¦"):
try:
_adata = scptr.datasets.dentate_gyrus()
st.session_state.adata = _adata
st.session_state.dataset_name = "Dentate Gyrus"
reset_downstream(1)
go(2); st.rerun()
except Exception as e:
st.markdown(f'<div class="ebox">Error: {e}</div>', unsafe_allow_html=True)
with qs_c3:
st.markdown(
'<div class="card" style="animation-delay:0.15s">'
'<div class="card-title">Upload</div>'
'<div style="font-size:13px;color:#333;line-height:1.6;margin-bottom:0.75rem">'
'Your own .h5ad file<br>'
'<span style="color:#888;font-size:12px">Requires spliced + unspliced layers</span>'
'</div>'
'</div>',
unsafe_allow_html=True,
)
if st.button("Upload File β", key="qs_upload", use_container_width=True):
st.session_state["_show_upload"] = True
st.rerun()
st.markdown('<hr>', unsafe_allow_html=True)
col1, col2 = st.columns(2, gap="large")
with col1:
st.markdown('<div class="sl">Example datasets</div>', unsafe_allow_html=True)
example = st.selectbox(
"Dataset",
["β select β",
"Pancreas (mouse endocrinogenesis, 3,696 cells)",
"Dentate Gyrus (mouse hippocampal neurogenesis, 2,930 cells)"],
label_visibility="collapsed",
)
st.markdown(
'<div class="ibox" style="font-size:12px">'
'Both datasets include spliced/unspliced counts from scVelo. '
'Requires <code>pooch</code> β datasets are downloaded on first use (~30 MB each).'
'</div>',
unsafe_allow_html=True,
)
if st.button("Load Example Dataset", disabled=(example == "β select β")):
with st.spinner("Downloading and loading datasetβ¦"):
try:
if "Pancreas" in example:
adata = scptr.datasets.pancreas()
name = "Pancreas"
else:
adata = scptr.datasets.dentate_gyrus()
name = "Dentate Gyrus"
st.session_state.adata = adata
st.session_state.dataset_name = name
reset_downstream(1)
go(2)
st.rerun()
except Exception as e:
st.markdown(
f'<div class="ebox"><b>Error loading dataset:</b> {e}<br>'
f'Ensure <code>pip install "scptr[datasets]"</code> is installed.</div>',
unsafe_allow_html=True,
)
with col2:
if st.session_state.pop("_show_upload", False):
st.markdown('<div class="ibox">Use the file uploader below to load your .h5ad file.</div>', unsafe_allow_html=True)
st.markdown('<div class="sl">Upload your own</div>', unsafe_allow_html=True)
uploaded = st.file_uploader(
"H5AD file",
type=["h5ad"],
label_visibility="collapsed",
help="AnnData file with 'spliced' and 'unspliced' layers",
)
if uploaded is not None:
st.markdown(
f'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
f'File: <code>{uploaded.name}</code> ({uploaded.size / 1e6:.1f} MB)</div>',
unsafe_allow_html=True,
)
if st.button("Load File"):
tmp = make_tmp(".h5ad")
try:
with open(tmp, "wb") as f:
f.write(uploaded.read())
with st.spinner("Reading fileβ¦"):
adata = sc.read_h5ad(tmp)
missing = [l for l in ["spliced", "unspliced"] if l not in adata.layers]
if missing:
st.markdown(
f'<div class="ebox"><b>Missing layers:</b> {", ".join(missing)}<br>'
f'The file must contain <code>spliced</code> and <code>unspliced</code> '
f'count matrix layers. Use velocyto, STARsolo, or Alevin-fry to generate them.</div>',
unsafe_allow_html=True,
)
else:
st.session_state.adata = adata
st.session_state.dataset_name = uploaded.name.removesuffix(".h5ad")
reset_downstream(1)
go(2)
st.rerun()
except Exception as e:
st.markdown(
f'<div class="ebox"><b>Could not read file:</b> {e}</div>',
unsafe_allow_html=True,
)
st.markdown('<hr>', unsafe_allow_html=True)
st.markdown(
'<div class="card">'
'<div class="card-title">What scPTR needs</div>'
'<table class="tbl">'
'<thead><tr><th>Slot</th><th>Content</th><th>Required</th></tr></thead>'
'<tbody>'
'<tr><td>adata.layers["spliced"]</td><td class="txt">Spliced (mature) mRNA counts per cell Γ gene</td><td>β</td></tr>'
'<tr><td>adata.layers["unspliced"]</td><td class="txt">Unspliced (nascent) mRNA counts per cell Γ gene</td><td>β</td></tr>'
'<tr><td>adata.obs columns</td><td class="txt">Cell metadata (cell type, cluster, etc.) for visualization</td><td>optional</td></tr>'
'<tr><td>adata.obsm["X_umap"]</td><td class="txt">UMAP embedding (scPTR will compute one if absent)</td><td>optional</td></tr>'
'</tbody></table>'
'</div>',
unsafe_allow_html=True,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ANALYSIS β STEP 2: PREPROCESS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
elif page == "analysis" and st.session_state.step == 2:
adata = st.session_state.adata
st.markdown('<div class="pt">Preprocess <span style="font-size:13px;font-weight:400;color:#aaa;letter-spacing:0">β step 2 of 5</span></div>', unsafe_allow_html=True)
st.markdown(
'<div class="pd">Filter low-quality genes, normalize counts to library size, '
'build a cell neighborhood graph (kNN in PCA space), and apply Gaussian kernel '
'smoothing to the spliced and unspliced layers.</div>',
unsafe_allow_html=True,
)
# Data quality summary
_s_layer = adata.layers["spliced"]
_u_layer = adata.layers["unspliced"]
_s_sparsity = 1.0 - float(np.count_nonzero(_s_layer)) / _s_layer.size
_u_sparsity = 1.0 - float(np.count_nonzero(_u_layer)) / _u_layer.size
_has_obs = list(adata.obs.columns)[:5]
st.markdown(
mg(
("Cells", f"{adata.n_obs:,}", ""),
("Genes", f"{adata.n_vars:,}", "before filtering"),
("Spliced sparsity", f"{_s_sparsity:.1%}", "zeros in matrix"),
("Unspliced sparsity", f"{_u_sparsity:.1%}", "zeros in matrix"),
),
unsafe_allow_html=True,
)
_layer_ok = all(l in adata.layers for l in ["spliced", "unspliced"])
_extra_layers = [l for l in adata.layers if l not in ["spliced", "unspliced"]]
_layer_info = (
'<span style="color:#2d7d4b;font-weight:700">β spliced</span> '
'<span style="color:#2d7d4b;font-weight:700">β unspliced</span>'
)
if _extra_layers:
_layer_info += f' Β· also: {", ".join(_extra_layers[:4])}'
if _has_obs:
_layer_info += f'<br><span style="color:#888;font-size:11px">obs columns: {", ".join(_has_obs)}</span>'
st.markdown(
f'<div class="ibox" style="font-size:12px">'
f'Layers detected: {_layer_info}'
f'</div>',
unsafe_allow_html=True,
)
_rn = st.session_state.get("pp_reset_n", 0) # reset counter β changes keys to force re-init
col1, col2, col3 = st.columns(3, gap="large")
with col1:
st.markdown('<div class="sl">Gene filtering</div>', unsafe_allow_html=True)
min_unspliced = st.number_input(
"Min total unspliced counts per gene",
value=10, min_value=1, step=1,
key=f"pp_mu_{_rn}",
help="Genes with fewer total unspliced counts across all cells are removed.",
)
min_cells = st.number_input(
"Min cells with nonzero unspliced",
value=5, min_value=1, step=1,
key=f"pp_mc_{_rn}",
help="Genes expressed in fewer cells are removed.",
)
with col2:
st.markdown('<div class="sl">Cell filtering</div>', unsafe_allow_html=True)
min_cell_unspliced = st.number_input(
"Min unspliced counts per cell",
value=0, min_value=0, step=100,
key=f"pp_mcu_{_rn}",
help="Cells with fewer total unspliced counts are removed. 0 = no filter.",
)
min_cell_spliced = st.number_input(
"Min spliced counts per cell",
value=0, min_value=0, step=100,
key=f"pp_mcs_{_rn}",
help="Cells with fewer total spliced counts are removed. 0 = no filter.",
)
with col3:
st.markdown('<div class="sl">Neighborhood graph</div>', unsafe_allow_html=True)
n_neighbors = st.number_input(
"Neighbors (kNN graph)",
value=30, min_value=5, max_value=100, step=5,
key=f"pp_nn_{_rn}",
help="Number of nearest neighbors for the cell graph. Larger = smoother.",
)
n_pcs = st.number_input(
"PCA components",
value=30, min_value=10, max_value=100, step=5,
key=f"pp_np_{_rn}",
help="PCA dimensions used for neighbor search.",
)
with st.expander("Advanced: Smoothing options"):
bw_mode = st.radio(
"Smoothing bandwidth",
["Adaptive (median distance)", "Fixed value"],
horizontal=True,
)
fixed_bw = None
if bw_mode == "Fixed value":
fixed_bw = st.number_input("Bandwidth", value=1.0, min_value=0.05, step=0.05)
st.markdown(
'<div class="ibox" style="font-size:12px">'
'Gaussian kernel smoothing reduces noise in spliced/unspliced counts by averaging '
'over cell neighbors. Adaptive bandwidth uses the median distance to the k-th neighbor.'
'</div>',
unsafe_allow_html=True,
)
if st.session_state.preprocessed:
st.markdown(
'<div class="sbox">Preprocessing already complete. '
'Click below to re-run with new parameters.</div>',
unsafe_allow_html=True,
)
run_col, reset_col = st.columns([2, 1])
if "pp_reset_n" not in st.session_state:
st.session_state.pp_reset_n = 0
with reset_col:
if st.button("β» Reset to defaults", help="Restore all preprocessing parameters to defaults"):
st.session_state.pp_reset_n += 1
st.rerun()
with run_col:
_run_pp = st.button("Run Preprocessing", use_container_width=True)
if _run_pp:
adata_work = st.session_state.adata.copy()
try:
prog = st.progress(0, text="Filtering genesβ¦")
scptr.pp.filter_genes(
adata_work,
min_unspliced_counts=min_unspliced,
min_unspliced_cells=min_cells,
)
if min_cell_unspliced > 0 or min_cell_spliced > 0:
prog.progress(15, text="Filtering cellsβ¦")
scptr.pp.filter_cells(
adata_work,
min_unspliced_counts=min_cell_unspliced,
min_spliced_counts=min_cell_spliced,
)
prog.progress(25, text="Normalizing layersβ¦")
scptr.pp.normalize_layers(adata_work)
prog.progress(50, text="Building neighborhood graphβ¦")
scptr.pp.neighbors(adata_work, n_neighbors=n_neighbors, n_pcs=n_pcs)
prog.progress(75, text="Smoothing spliced/unsplicedβ¦")
if fixed_bw is not None:
scptr.pp.smooth_layers(adata_work, bandwidth=fixed_bw)
else:
scptr.pp.smooth_layers(adata_work)
prog.progress(100, text="Done.")
st.session_state.adata = adata_work
st.session_state.preprocessed = True
reset_downstream(2)
_cells_removed = adata.n_obs - adata_work.n_obs
_genes_removed = adata.n_vars - adata_work.n_vars
st.markdown(
mg(
("Cells retained", f"{adata_work.n_obs:,}", f"β{_cells_removed:,} filtered" if _cells_removed else "all kept", "green"),
("Genes retained", f"{adata_work.n_vars:,}", f"β{_genes_removed:,} filtered"),
("Neighbors", str(n_neighbors), "kNN graph"),
("PCA dims", str(n_pcs), "for graph"),
),
unsafe_allow_html=True,
)
st.markdown(
'<div class="sbox">'
'Preprocessing complete β genes filtered, layers normalized, kNN graph built, counts smoothed. '
'<b>Next:</b> estimate Ξ² (splicing rate) and Ξ³ (degradation rate). '
'Variance decomposition runs automatically with rate estimation.'
'</div>',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'<div class="ebox"><b>Preprocessing failed:</b> {e}<br>'
f'<span style="font-size:12px;opacity:0.8">Common causes: '
f'too few genes after filtering (lower thresholds in step 2) Β· '
f'missing spliced/unspliced layers Β· '
f'dataset too small for PCA (n_pcs may exceed n_cells or n_genes)</span>'
f'</div>',
unsafe_allow_html=True,
)
with st.expander("Traceback"):
st.code(traceback.format_exc())
st.markdown('<hr>', unsafe_allow_html=True)
c1, c2 = st.columns([1, 6])
with c1:
if st.button("β Back"):
go(1); st.rerun()
with c2:
if st.button("Next: Estimate Rates β", disabled=not st.session_state.preprocessed):
go(3); st.rerun()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ANALYSIS β STEP 3: ESTIMATE RATES
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
elif page == "analysis" and st.session_state.step == 3:
adata = st.session_state.adata
st.markdown('<div class="pt">Estimate Rates <span style="font-size:13px;font-weight:400;color:#aaa;letter-spacing:0">β step 3 of 5</span></div>', unsafe_allow_html=True)
st.markdown(
'<div class="pd">Estimate gene-specific splicing rates (Ξ²) from unspliced/spliced phase '
'portraits using quantile regression. Then compute per-cell, per-gene degradation rates '
'Ξ³<sub>ig</sub> = Ξ²<sub>g</sub> Β· u<sub>ig</sub> / s<sub>ig</sub> using the '
'kinetic steady-state model.</div>',
unsafe_allow_html=True,
)
col1, col2 = st.columns(2, gap="large")
with col1:
st.markdown('<div class="sl">Beta estimation (Ξ²)</div>', unsafe_allow_html=True)
quantile = st.slider(
"Phase portrait quantile",
0.80, 0.99, 0.95, 0.01,
help="Upper quantile of the u/s ratio distribution used to estimate Ξ². "
"Typical: 0.95 for 10x Chromium; 0.90β0.92 for Smart-seq (denser coverage). "
"Lower values = more conservative boundary.",
)
min_r2 = st.slider(
"Min RΒ² for beta fit",
0.0, 0.90, 0.0, 0.05,
help="Genes with a phase portrait RΒ² below this threshold are excluded.",
)
with col2:
st.markdown('<div class="sl">Gamma estimation (Ξ³)</div>', unsafe_allow_html=True)
min_spliced_thr = st.number_input(
"Min smoothed spliced (Ms) for Ξ³",
value=0.01, min_value=0.001, step=0.005, format="%.3f",
help="Cells with Ms < threshold for a given gene receive Ξ³ = 0 (prevents division by near-zero).",
)
clip_q = st.slider(
"Gamma clip quantile",
0.90, 1.00, 0.99, 0.01,
help="Per-gene clipping at this quantile to remove extreme outliers before analysis.",
)
if st.session_state.estimated:
st.markdown(
'<div class="sbox">Rate estimation already complete. Re-run to update with new parameters.</div>',
unsafe_allow_html=True,
)
if st.button("Estimate Ξ² and Ξ³"):
adata_work = st.session_state.adata
try:
prog = st.progress(0, text="Estimating splicing rates (Ξ²)β¦")
scptr.tl.estimate_beta(adata_work, quantile=quantile)
prog.progress(40, text="Estimating degradation rates (Ξ³)β¦")
scptr.tl.estimate_gamma(
adata_work,
clip_quantile=clip_q,
min_spliced=min_spliced_thr,
)
prog.progress(80, text="Variance decompositionβ¦")
scptr.tl.variance_decomposition(adata_work)
prog.progress(100, text="Done.")
st.session_state.adata = adata_work
st.session_state.estimated = True
reset_downstream(3)
beta = adata_work.var["beta"]
gamma = adata_work.layers["gamma"]
gamma_pos = gamma[gamma > 0]
inf_genes = int((np.median(gamma, axis=0) > 0).sum())
st.markdown(
mg(
("Median Ξ²", f"{np.median(beta):.3f}", "splicing rate"),
("Median Ξ³", f"{np.median(gamma_pos):.4f}", "positive cells", "blue"),
("Max Ξ³", f"{np.max(gamma):.2f}", "clipped"),
("Informative genes", f"{inf_genes:,}", "median Ξ³ > 0", "green"),
),
unsafe_allow_html=True,
)
# Phase portrait for highest-Ξ² gene
top_gene = beta.idxmax()
fig, axes = plt.subplots(1, 2, figsize=(9, 3.8))
# Phase portrait
ax = axes[0]
Ms = adata_work.layers["Ms"][:, adata_work.var_names.get_loc(top_gene)]
Mu = adata_work.layers["Mu"][:, adata_work.var_names.get_loc(top_gene)]
ax.scatter(Ms, Mu, s=5, alpha=0.35, color="#2b5797", linewidths=0)
bv = adata_work.var.loc[top_gene, "beta"]
x = np.linspace(0, np.percentile(Ms, 99), 100)
ax.plot(x, bv * x, color="#c0392b", lw=1.5, label=f"Ξ² = {bv:.3f}")
ax.set_xlabel("Ms (smoothed spliced)", fontsize=10)
ax.set_ylabel("Mu (smoothed unspliced)", fontsize=10)
ax.set_title(f"Phase portrait: {top_gene}", fontsize=10, fontweight="bold")
ax.legend(fontsize=9, frameon=False)
ax.spines[["top", "right"]].set_visible(False)
# Beta distribution
ax2 = axes[1]
ax2.hist(beta, bins=50, color="#2b5797", alpha=0.7, linewidth=0)
ax2.axvline(np.median(beta), color="#c0392b", lw=1.5,
label=f"median = {np.median(beta):.3f}")
ax2.set_xlabel("Ξ² (splicing rate)", fontsize=10)
ax2.set_ylabel("Gene count", fontsize=10)
ax2.set_title("Distribution of Ξ² across genes", fontsize=10, fontweight="bold")
ax2.legend(fontsize=9, frameon=False)
ax2.spines[["top", "right"]].set_visible(False)
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
# Ξ² quality summary
_beta_pos_pct = (beta > 0).mean() * 100
_gamma_pos_pct = (np.median(adata_work.layers["gamma"], axis=0) > 0).mean() * 100
_quality = "good" if _beta_pos_pct > 60 else "low"
_quality_color = "#2d7d4b" if _quality == "good" else "#d4860a"
st.markdown(
f'<div class="ibox" style="font-size:12px">'
f'<b>Data quality:</b> '
f'{_beta_pos_pct:.0f}% of genes have estimable Ξ² Β· '
f'{_gamma_pos_pct:.0f}% have estimable Ξ³ Β· '
f'<span style="color:{_quality_color};font-weight:700">{_quality.upper()}</span>'
f'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="sbox">'
'Ξ² and Ξ³ estimated, variance decomposition complete. '
'<b>Next:</b> cluster cells in Ξ³-space to discover PT states. '
'Or explore the phase portrait below for individual genes.'
'</div>',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'<div class="ebox"><b>Rate estimation failed:</b> {e}</div>',
unsafe_allow_html=True,
)
with st.expander("Traceback"):
st.code(traceback.format_exc())
# Phase portrait explorer (available once rates are estimated)
if st.session_state.estimated:
st.markdown('<div class="sl">Phase portrait explorer</div>', unsafe_allow_html=True)
st.markdown(
'<div class="ibox" style="font-size:12px">'
'Inspect the unspliced vs spliced phase portrait for any gene, colored by Ξ³. '
'The diagonal line shows the estimated Ξ² slope. Cells above = accelerating; below = decelerating.'
'</div>',
unsafe_allow_html=True,
)
adata_est = st.session_state.adata
pp_col1, pp_col2 = st.columns([3, 1])
with pp_col1:
pp_gene = st.selectbox(
"Gene",
sorted(adata_est.var_names.tolist()),
key="pp_gene_select",
help="Select a gene to visualize its unspliced vs spliced phase portrait.",
)
with pp_col2:
pp_cmap = st.selectbox("Color", ["viridis", "plasma", "RdBu_r", "coolwarm"], key="pp_cmap",
help="Colormap for Ξ³ values in the phase portrait scatter.")
if st.button("Plot Phase Portrait", key="pp_btn"):
try:
fig = scptr.pl.phase_portrait(
adata_est, genes=pp_gene, color_by="gamma",
cmap=pp_cmap, show=False,
)
if fig is not None:
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as e:
st.markdown(f'<div class="ebox">Phase portrait failed: {e}</div>', unsafe_allow_html=True)
st.markdown('<hr>', unsafe_allow_html=True)
c1, c2 = st.columns([1, 6])
with c1:
if st.button("β Back"):
go(2); st.rerun()
with c2:
if st.button("Next: Discover PT States β", disabled=not st.session_state.estimated):
go(4); st.rerun()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ANALYSIS β STEP 4: DISCOVER PT STATES
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
elif page == "analysis" and st.session_state.step == 4:
adata = st.session_state.adata
st.markdown('<div class="pt">Discover PT States <span style="font-size:13px;font-weight:400;color:#aaa;letter-spacing:0">β step 4 of 5</span></div>', unsafe_allow_html=True)
st.markdown(
'<div class="pd">Cluster cells in Ξ³-space (PCA β kNN β Leiden) to reveal '
'post-transcriptional states invisible to expression analysis. '
'Optionally compute PT velocity and infer RBPβtarget regulatory networks.</div>',
unsafe_allow_html=True,
)
tab1, tab2, tab3 = st.tabs(["PT STATES", "PT VELOCITY", "RBP NETWORKS"])
# ββ Tab 1: PT States ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab1:
col1, col2 = st.columns(2, gap="large")
with col1:
st.markdown('<div class="sl">Dimensionality reduction</div>', unsafe_allow_html=True)
n_pcs_g = st.number_input(
"PCA dims (Ξ³-space)",
value=20, min_value=5, max_value=50,
help="PCA components computed from the Ξ³ matrix before clustering.",
)
n_nbrs_g = st.number_input(
"Neighbors (Ξ³-space kNN)",
value=15, min_value=5, max_value=50,
help="Number of nearest neighbors in Ξ³-PCA space.",
)
with col2:
st.markdown('<div class="sl">Leiden clustering</div>', unsafe_allow_html=True)
resolution = st.slider(
"Resolution",
0.1, 2.0, 0.5, 0.05,
help="Higher resolution β more, smaller clusters. "
"Typical range: 0.3β0.8. Start at 0.5 and increase if clusters are too coarse.",
)
rand_seed = st.number_input("Random seed", value=42, step=1,
help="Random seed for reproducible clustering and UMAP.")
st.markdown(
'<div class="ibox">'
'PT state discovery performs PCA on the Ξ³ matrix, builds a kNN graph in Ξ³-PCA space, '
'then runs Leiden community detection. The resulting clusters represent groups of cells '
'with similar post-transcriptional programs β which may differ from their expression-based identity.'
'</div>',
unsafe_allow_html=True,
)
if st.session_state.states_done:
n_s = adata.obs["pt_state"].nunique() if "pt_state" in adata.obs else "?"
st.markdown(
f'<div class="sbox">PT states already computed ({n_s} states). '
f'Re-run to update with new parameters.</div>',
unsafe_allow_html=True,
)
if st.button("Find PT States"):
try:
with st.spinner("Clustering cells in Ξ³-spaceβ¦"):
scptr.tl.pt_states(
adata,
n_pcs=n_pcs_g,
n_neighbors=n_nbrs_g,
resolution=resolution,
random_state=rand_seed,
)
st.session_state.adata = adata
st.session_state.states_done = True
st.session_state.velocity_done = False
st.session_state.network_done = False
n_states = adata.obs["pt_state"].nunique()
state_sizes = adata.obs["pt_state"].value_counts().sort_index()
st.markdown(
mg(
("PT States", str(n_states), "Leiden clusters in Ξ³-space", "blue"),
("Largest state", f"{state_sizes.max():,}", "cells"),
("Smallest state", f"{state_sizes.min():,}", "cells"),
),
unsafe_allow_html=True,
)
# Side-by-side UMAP: Ξ³-space vs expression
fig, axes = plt.subplots(1, 2, figsize=(10, 4.2))
# Ξ³-space UMAP
coords_g = adata.obsm["X_gamma_umap"]
labels = adata.obs["pt_state"].values
unique_labels = sorted(set(labels), key=lambda x: int(x) if str(x).isdigit() else x)
cmap = plt.colormaps["tab20"].resampled(len(unique_labels))
label_map = {l: i for i, l in enumerate(unique_labels)}
c = [label_map[l] for l in labels]
sc_plot = axes[0].scatter(
coords_g[:, 0], coords_g[:, 1],
c=c, cmap=cmap, s=8, alpha=0.6, linewidths=0,
)
axes[0].set_title("PT states (Ξ³-space UMAP)", fontsize=10, fontweight="bold")
axes[0].axis("off")
# legend
for l in unique_labels:
axes[0].scatter([], [], color=cmap(label_map[l]), label=str(l), s=20)
axes[0].legend(fontsize=8, frameon=False, ncol=2, loc="upper right")
# Expression UMAP (if available)
if "X_umap" in adata.obsm:
coords_e = adata.obsm["X_umap"]
axes[1].scatter(
coords_e[:, 0], coords_e[:, 1],
c=c, cmap=cmap, s=8, alpha=0.6, linewidths=0,
)
axes[1].set_title("PT states (expression UMAP)", fontsize=10, fontweight="bold")
axes[1].axis("off")
else:
axes[1].text(0.5, 0.5, "No expression UMAP\n(add X_umap to adata.obsm)",
ha="center", va="center", transform=axes[1].transAxes,
fontsize=10, color="#888")
axes[1].axis("off")
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
# State size table
rows = "".join(
f"<tr><td>State {s}</td><td>{n:,}</td>"
f'<td>{n / len(adata.obs) * 100:.1f}%</td></tr>'
for s, n in state_sizes.items()
)
st.markdown(
f'<table class="tbl" style="max-width:340px">'
f'<thead><tr><th>PT State</th><th>Cells</th><th>%</th></tr></thead>'
f'<tbody>{rows}</tbody></table>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="sbox">PT state discovery complete. '
'Proceed to Results or explore PT Velocity and RBP Networks.</div>',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'<div class="ebox"><b>PT state discovery failed:</b> {e}</div>',
unsafe_allow_html=True,
)
with st.expander("Traceback"):
st.code(traceback.format_exc())
# ββ Tab 2: PT Velocity ββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab2:
if not st.session_state.states_done:
st.markdown(
'<div class="wbox">Run PT state discovery (Tab 1) before computing velocity.</div>',
unsafe_allow_html=True,
)
else:
st.markdown(
'<div class="ibox">'
'Post-transcriptional velocity computes, for each cell, the weighted mean '
'difference in Ξ³ from its neighbors: <code>v[i,g] = Ξ£ w_ij Β· (Ξ³[j,g] β Ξ³[i,g])</code>. '
'This captures the direction and magnitude of post-transcriptional change, '
'orthogonal to RNA velocity.'
'</div>',
unsafe_allow_html=True,
)
vel_c1, vel_c2 = st.columns(2, gap="large")
with vel_c1:
graph_choice = st.radio(
"Neighbor graph",
["Ξ³-space graph (from PT states)", "Expression-space graph"],
horizontal=False,
help="Which kNN graph to use when averaging neighbor Ξ³ values.",
)
use_graph = "gamma" if "Ξ³-space" in graph_choice else "expression"
with vel_c2:
viz_style = st.radio(
"Visualization style",
["Arrows (embedding)", "Streamlines"],
horizontal=False,
help="Arrows show per-cell velocity; streamlines show flow patterns.",
)
arrow_size = st.slider("Arrow size", 1.0, 8.0, 3.0, 0.5, key="vel_arrow",
help="Scale of velocity arrows on the embedding plot.")
if st.session_state.velocity_done:
st.markdown(
'<div class="sbox">PT velocity already computed. Re-run to update.</div>',
unsafe_allow_html=True,
)
if st.button("Compute PT Velocity"):
try:
with st.spinner("Computing PT velocityβ¦"):
scptr.tl.pt_velocity(adata, use_graph=use_graph)
st.session_state.adata = adata
st.session_state.velocity_done = True
fig, ax = plt.subplots(figsize=(6, 5))
try:
if "Stream" in viz_style:
scptr.pl.pt_velocity_stream(adata, ax=ax, show=False)
else:
scptr.pl.pt_velocity_embedding(adata, ax=ax, arrow_size=arrow_size, show=False)
except Exception:
coords = adata.obsm.get("X_gamma_umap", adata.obsm.get("X_umap"))
if coords is not None:
ax.scatter(coords[:, 0], coords[:, 1], s=6, alpha=0.4,
color="#2b5797", linewidths=0)
ax.set_title(f"Post-transcriptional velocity ({'streamlines' if 'Stream' in viz_style else 'arrows'})", fontsize=10)
ax.axis("off")
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
st.markdown(
'<div class="sbox">PT velocity computed and stored in '
'<code>adata.layers["pt_velocity"]</code>.</div>',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'<div class="ebox"><b>PT velocity failed:</b> {e}</div>',
unsafe_allow_html=True,
)
with st.expander("Traceback"):
st.code(traceback.format_exc())
# ββ Tab 3: RBP Networks βββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab3:
if not st.session_state.states_done:
st.markdown(
'<div class="wbox">Run PT state discovery (Tab 1) before inferring networks.</div>',
unsafe_allow_html=True,
)
else:
st.markdown(
'<div class="ibox">'
'RBPβtarget network inference regresses Ξ³ of each target gene on the '
'smoothed expression of regulator genes using elastic net regression. '
'Non-zero coefficients indicate putative regulatory relationships. '
'Runtime scales with number of genes β start with defaults for speed.'
'</div>',
unsafe_allow_html=True,
)
# Core settings
use_known_rbps = st.checkbox(
"Restrict regulators to known RBPs (recommended β faster, more interpretable)",
value=True,
help="Filter regulators to curated RNA-binding proteins only.",
)
rbp_organism = None
if use_known_rbps:
rbp_organism = st.selectbox(
"RBP organism",
["human", "mouse", None],
format_func=lambda x: x if x else "all species",
key="rbp_org",
)
with st.expander("Advanced options"):
net_c1, net_c2 = st.columns(2, gap="large")
with net_c1:
n_top = st.number_input(
"Top edges per target gene",
value=50, min_value=5, max_value=500,
help="Maximum number of regulatorβtarget edges to retain per gene.",
)
alpha = st.slider(
"Elastic net mixing (Ξ±)",
0.0, 1.0, 0.5, 0.05,
help="0 = ridge (all regulators retained), 1 = lasso (sparse selection). 0.5 = elastic net.",
)
with net_c2:
custom_regs = st.text_area(
"Additional regulators (optional)",
value="",
height=80,
help="Comma or newline-separated gene names. Appended to known RBPs if that option is checked.",
)
custom_tgts = st.text_area(
"Restrict to these targets (optional)",
value="",
height=80,
help="Leave empty to use all genes as targets.",
)
if st.session_state.network_done:
net = adata.uns.get("pt_network", pd.DataFrame())
st.markdown(
f'<div class="sbox">Network already inferred ({len(net):,} edges). '
f'Re-run to update.</div>',
unsafe_allow_html=True,
)
if st.button("Infer RBPβTarget Network"):
regs = None
tgts = None
# Build regulator list
if use_known_rbps:
try:
known = scptr.tl.list_known_rbps(organism=rbp_organism if use_known_rbps else None)
# Keep only those present in adata
known = [g for g in known if g in adata.var_names]
regs = known if known else None
except Exception:
regs = None
if custom_regs.strip():
extra = [g.strip() for g in custom_regs.replace(",", "\n").split("\n") if g.strip()]
regs = list(set((regs or []) + extra)) if regs else extra
if custom_tgts.strip():
tgts = [g.strip() for g in custom_tgts.replace(",", "\n").split("\n") if g.strip()]
if regs:
st.markdown(
f'<div class="ibox" style="font-size:12px">Using {len(regs):,} regulators.</div>',
unsafe_allow_html=True,
)
try:
with st.spinner("Running elastic net regression⦠(may take several minutes for large datasets)"):
scptr.tl.infer_network(
adata,
regulators=regs,
targets=tgts,
method="elasticnet",
alpha=alpha,
n_top=n_top,
)
st.session_state.adata = adata
st.session_state.network_done = True
net = adata.uns.get("pt_network", pd.DataFrame())
if len(net) == 0:
st.markdown(
'<div class="wbox">No significant edges found. '
'Try reducing Ξ± or increasing n_top.</div>',
unsafe_allow_html=True,
)
else:
n_destab = int((net["weight"] > 0).sum())
n_stab = int((net["weight"] < 0).sum())
st.markdown(
mg(
("Total edges", f"{len(net):,}", "regulatorβtarget pairs"),
("Destabilizing", f"{n_destab:,}", "weight > 0", "blue"),
("Stabilizing", f"{n_stab:,}", "weight < 0", "green"),
("Hub regulators", f"{net['regulator'].nunique():,}", "unique"),
),
unsafe_allow_html=True,
)
st.markdown(
'<div class="ibox" style="font-size:12px">'
'<b>Edge weights:</b> positive = regulator destabilizes target (promotes degradation); '
'negative = regulator stabilizes target (protects from degradation).'
'</div>',
unsafe_allow_html=True,
)
# Hub table with direction
hubs_dest = net[net["weight"] > 0].groupby("regulator").size()
hubs_stab = net[net["weight"] < 0].groupby("regulator").size()
hubs_all = net.groupby("regulator").size().sort_values(ascending=False).head(15)
rows = "".join(
f"<tr><td>{rbp}</td><td>{cnt:,}</td>"
f"<td>{hubs_dest.get(rbp, 0):,}</td>"
f"<td>{hubs_stab.get(rbp, 0):,}</td></tr>"
for rbp, cnt in hubs_all.items()
)
st.markdown(
f'<table class="tbl" style="max-width:500px">'
f'<thead><tr><th>Regulator</th><th>Total</th><th>Destab.</th><th>Stab.</th></tr></thead>'
f'<tbody>{rows}</tbody></table>',
unsafe_allow_html=True,
)
# Network plot
try:
fig, ax = plt.subplots(figsize=(7, 6))
scptr.pl.network_graph(adata, n_edges=60, ax=ax, show=False)
ax.set_title("Top regulatory edges", fontsize=10, fontweight="bold")
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as _ne:
st.markdown(
f'<div class="wbox">Network plot could not be rendered: {_ne}</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="sbox">Network inference complete. '
'Results stored in <code>adata.uns["pt_network"]</code>.</div>',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'<div class="ebox"><b>Network inference failed:</b> {e}</div>',
unsafe_allow_html=True,
)
with st.expander("Traceback"):
st.code(traceback.format_exc())
st.markdown('<hr>', unsafe_allow_html=True)
c1, c2 = st.columns([1, 6])
with c1:
if st.button("β Back"):
go(3); st.rerun()
with c2:
if st.button("View Results β", disabled=not st.session_state.states_done):
go(5); st.rerun()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ANALYSIS β STEP 5: RESULTS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
elif page == "analysis" and st.session_state.step == 5:
adata = st.session_state.adata
st.markdown('<div class="pt">Results <span style="font-size:13px;font-weight:400;color:#aaa;letter-spacing:0">β step 5 of 5</span></div>', unsafe_allow_html=True)
# Summary metrics
gamma = adata.layers.get("gamma")
gamma_pos = gamma[gamma > 0] if gamma is not None else np.array([])
n_states = adata.obs["pt_state"].nunique() if "pt_state" in adata.obs else 0
beta = adata.var.get("beta")
net = adata.uns.get("pt_network", pd.DataFrame())
st.markdown(
mg(
("Cells", f"{adata.n_obs:,}", ""),
("Genes", f"{adata.n_vars:,}", ""),
("PT States", str(n_states), "Ξ³-space Leiden", "blue"),
("Median Ξ²", f"{np.median(beta):.3f}" if beta is not None else "β", "splicing rate"),
("Median Ξ³", f"{np.median(gamma_pos):.4f}" if len(gamma_pos) else "β", "positive cells"),
("Network edges", f"{len(net):,}" if len(net) else "β", "RBPβtarget", "green"),
),
unsafe_allow_html=True,
)
# Analysis completion summary
_done_items = []
if beta is not None: _done_items.append("Ξ² estimated")
if gamma is not None: _done_items.append("Ξ³ estimated")
if "tf_score" in adata.var.columns: _done_items.append("variance decomposed")
if n_states > 0: _done_items.append(f"{n_states} PT states")
if st.session_state.velocity_done: _done_items.append("PT velocity")
if st.session_state.network_done: _done_items.append(f"{len(net):,} network edges")
if _done_items:
st.markdown(
'<div class="ibox" style="font-size:12px">'
f'<b>Analyses complete:</b> {" Β· ".join(_done_items)}'
'</div>',
unsafe_allow_html=True,
)
rtab1, rtab2, rtab3, rtab4, rtab5 = st.tabs(["VISUALIZATION", "VARIANCE DECOMP", "GENE RANKINGS", "GAMMA EXPLORER", "DOWNLOAD"])
# ββ Visualization βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with rtab1:
st.markdown(
'<div class="ibox">Choose a column to color by and click <b>Plot UMAP</b> to visualize cells. '
'Toggle <b>Ξ³-space UMAP</b> to compare expression vs. post-transcriptional organization.</div>',
unsafe_allow_html=True,
)
col_a, col_b = st.columns([2, 1])
with col_a:
color_opts = []
if "pt_state" in adata.obs.columns:
color_opts.append("pt_state")
color_opts += [c for c in adata.obs.columns if c != "pt_state"][:30]
viz_col = st.selectbox("Color by", color_opts, label_visibility="visible")
with col_b:
use_g_umap = st.checkbox(
"Ξ³-space UMAP",
value=True,
help="Use UMAP computed from Ξ³-space PCA (vs. expression UMAP)",
)
if st.button("Plot UMAP"):
basis = "X_gamma_umap" if (use_g_umap and "X_gamma_umap" in adata.obsm) else "X_umap"
if basis not in adata.obsm and "X_umap" not in adata.obsm:
with st.spinner("Computing expression UMAPβ¦"):
sc.tl.umap(adata)
basis = "X_umap"
elif basis not in adata.obsm:
basis = "X_umap"
fig, ax = plt.subplots(figsize=(6, 5))
sc.pl.embedding(
adata, basis=basis, color=viz_col, ax=ax, show=False,
frameon=False, size=14,
title=f"{viz_col} Β· {'Ξ³-space' if 'gamma' in basis else 'expression'} UMAP",
)
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
# PT state UMAP using native scptr function
if n_states > 0 and "X_gamma_umap" in adata.obsm:
st.markdown('<hr>', unsafe_allow_html=True)
if st.button("PT State UMAP (Ξ³-space)"):
try:
fig = scptr.pl.pt_umap(adata, show=False)
if fig is not None:
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as e:
st.markdown(f'<div class="ebox">PT UMAP failed: {e}</div>', unsafe_allow_html=True)
if gamma is not None and n_states > 0:
st.markdown('<hr>', unsafe_allow_html=True)
if st.button("Plot Gamma Heatmap"):
with st.spinner("Building heatmapβ¦"):
try:
fig = scptr.pl.gamma_heatmap(adata, groupby="pt_state", show=False)
if fig is not None:
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as e:
st.markdown(f'<div class="ebox">Heatmap failed: {e}</div>', unsafe_allow_html=True)
if "X_gamma_umap" in adata.obsm and "X_umap" in adata.obsm:
st.markdown('<hr>', unsafe_allow_html=True)
if st.button("Ξ³-space vs Expression Comparison"):
with st.spinner("Building side-by-side comparisonβ¦"):
try:
fig = scptr.pl.pt_comparison(adata, figsize=(12, 5), show=False)
if fig:
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as e:
# Fallback: manual side-by-side
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
for ax, basis, title in [
(axes[0], "X_umap", "Expression UMAP"),
(axes[1], "X_gamma_umap", "Ξ³-space UMAP"),
]:
coords = adata.obsm[basis]
if "pt_state" in adata.obs.columns:
labels = adata.obs["pt_state"].values
unique = sorted(set(labels), key=lambda x: int(x) if str(x).isdigit() else x)
cmap = plt.colormaps["tab20"].resampled(len(unique))
lmap = {l: i for i, l in enumerate(unique)}
c = [lmap[l] for l in labels]
ax.scatter(coords[:, 0], coords[:, 1], c=c, cmap=cmap,
s=8, alpha=0.6, linewidths=0)
else:
ax.scatter(coords[:, 0], coords[:, 1], s=8, alpha=0.5,
color="#2b5797", linewidths=0)
ax.set_title(title, fontsize=10, fontweight="bold")
ax.axis("off")
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
if st.session_state.velocity_done:
st.markdown('<hr>', unsafe_allow_html=True)
if st.button("Plot PT Velocity"):
try:
fig, ax = plt.subplots(figsize=(6, 5))
scptr.pl.pt_velocity_embedding(adata, ax=ax, show=False)
ax.set_title("Post-transcriptional velocity", fontsize=10)
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as e:
st.markdown(f'<div class="ebox">Velocity plot failed: {e}</div>', unsafe_allow_html=True)
# ββ Variance decomposition ββββββββββββββββββββββββββββββββββββββββββββββββ
with rtab2:
has_vd = "tf_score" in adata.var.columns and "ptf_score" in adata.var.columns
if not has_vd:
st.markdown(
'<div class="wbox">Variance decomposition not computed. '
'Return to step 3 and run Estimate Ξ² and Ξ³ (variance decomposition runs automatically).</div>',
unsafe_allow_html=True,
)
else:
st.markdown(
'<div class="ibox">'
'Each gene\'s variance is decomposed into a <b>transcriptional fraction</b> (TF, driven by changes in '
'unspliced/nascent RNA) and a <b>post-transcriptional fraction</b> (PTF, driven by changes in Ξ³). '
'Genes with high PTF score are primarily regulated post-transcriptionally.'
'</div>',
unsafe_allow_html=True,
)
tf = adata.var["tf_score"]
ptf = adata.var["ptf_score"]
# Aggregate stats
ptf_dom = (ptf > 0.5).sum()
tf_dom = (tf > 0.5).sum()
st.markdown(
mg(
("PTF-dominant genes", f"{ptf_dom:,}", "PTF score > 0.5", "blue"),
("TF-dominant genes", f"{tf_dom:,}", "TF score > 0.5"),
("Median PTF score", f"{ptf.median():.3f}", "across genes"),
),
unsafe_allow_html=True,
)
n_label = st.slider("Label top N genes", 5, 30, 10, 5, key="vd_label")
if st.button("Plot Variance Decomposition"):
try:
fig = scptr.pl.tf_ptf_scatter(adata, label_top=n_label, show=False)
if fig:
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
except Exception as e:
# Fallback: manual plot
fig, axes = plt.subplots(1, 2, figsize=(10, 4.5))
axes[0].hist(ptf, bins=50, color="#2b5797", alpha=0.75, linewidth=0)
axes[0].axvline(0.5, color="#c0392b", lw=1.5, ls="--", label="PTF=0.5")
axes[0].set_xlabel("PTF score", fontsize=10)
axes[0].set_ylabel("Gene count", fontsize=10)
axes[0].set_title("Distribution of PTF scores", fontsize=10, fontweight="bold")
axes[0].legend(fontsize=9, frameon=False)
axes[0].spines[["top","right"]].set_visible(False)
rank = np.argsort(ptf.values)
axes[1].scatter(range(len(ptf)), ptf.values[rank], s=4, alpha=0.5,
color="#2b5797", linewidths=0)
axes[1].axhline(0.5, color="#c0392b", lw=1.2, ls="--")
axes[1].set_xlabel("Gene rank", fontsize=10)
axes[1].set_ylabel("PTF score", fontsize=10)
axes[1].set_title("Ranked PTF scores", fontsize=10, fontweight="bold")
axes[1].spines[["top","right"]].set_visible(False)
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
# Top PTF genes table
st.markdown('<div class="sl">Top post-transcriptionally regulated genes</div>', unsafe_allow_html=True)
top_ptf = ptf.sort_values(ascending=False).head(30)
ptf_df = pd.DataFrame({
"Gene": top_ptf.index,
"PTF score": top_ptf.values.round(4),
"TF score": tf[top_ptf.index].values.round(4),
})
st.dataframe(ptf_df, use_container_width=True, hide_index=True, height=420)
# ββ Gene rankings βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with rtab3:
if n_states == 0:
st.markdown(
'<div class="wbox">No PT states found. Run <b>Discover PT States β PT STATES</b> tab first, '
'then return here to rank differentially degraded genes.</div>',
unsafe_allow_html=True,
)
else:
st.markdown(
'<div class="ibox">Identifies genes with significantly different Ξ³ (degradation rate) '
'between PT states β analogous to differential expression but in Ξ³-space.</div>',
unsafe_allow_html=True,
)
rank_method = st.selectbox(
"Statistical test",
["t-test", "wilcoxon"],
help="t-test is faster; Wilcoxon rank-sum is more robust to outliers.",
)
if st.button("Rank PT-Differential Genes"):
with st.spinner("Running differential Ξ³ testβ¦"):
try:
scptr.tl.rank_pt_genes(adata, method=rank_method)
st.session_state.adata = adata
except Exception as e:
st.markdown(f'<div class="ebox">Ranking failed: {e}</div>', unsafe_allow_html=True)
if "rank_pt_genes" in adata.uns:
names = adata.uns["rank_pt_genes"].get("names", {})
if names:
groups = list(names.keys())
show_groups = st.multiselect(
"Show states",
groups,
default=groups[:min(4, len(groups))],
)
if show_groups:
top_n_genes = st.slider("Genes per state", 5, 50, 15, 5, key="rank_n")
all_rows = []
for g in show_groups:
gene_list = list(names[g])[:top_n_genes]
for rank, gene in enumerate(gene_list, 1):
all_rows.append({"State": f"PT {g}", "Rank": rank, "Gene": gene})
if all_rows:
rank_df = pd.DataFrame(all_rows)
st.dataframe(rank_df, use_container_width=True, hide_index=True, height=380)
st.download_button(
"Download ranked genes (.csv)",
rank_df.to_csv(index=False).encode(),
file_name=f"ranked_genes_{st.session_state.dataset_name}.csv",
mime="text/csv",
)
if beta is not None:
st.markdown('<hr>', unsafe_allow_html=True)
st.markdown('<div class="sl">Genes by splicing rate (Ξ²)</div>', unsafe_allow_html=True)
top_n = st.slider("Show top N genes", 10, 100, 30, 10, key="top_beta_n")
top_beta = beta.sort_values(ascending=False).head(top_n)
beta_df = pd.DataFrame({"Gene": top_beta.index, "Ξ² (splicing rate)": top_beta.values.round(5)})
st.dataframe(beta_df, use_container_width=True, hide_index=True, height=350)
# ββ Gamma explorer ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with rtab4:
if gamma is None:
st.markdown('<div class="wbox">No Ξ³ data available.</div>', unsafe_allow_html=True)
else:
st.markdown(
'<div class="ibox">Explore the distribution of degradation rates for individual genes '
'or compare Ξ³ across PT states.</div>',
unsafe_allow_html=True,
)
ge_col1, ge_col2 = st.columns([2, 1])
with ge_col1:
gene_input = st.selectbox(
"Select gene",
sorted(adata.var_names.tolist()),
key="gamma_gene",
)
with ge_col2:
groupby_col = "pt_state" if "pt_state" in adata.obs.columns else None
if groupby_col is None:
st.markdown('<div style="font-size:12px;color:#888;margin-top:1.5rem">No PT states computed yet</div>', unsafe_allow_html=True)
if gene_input and st.button("Plot Ξ³ for selected gene"):
g_idx = adata.var_names.get_loc(gene_input)
g_vals = gamma[:, g_idx]
g_pos = g_vals[g_vals > 0]
# Try scptr.pl.gamma_violin first, fall back to manual
if groupby_col:
try:
fig = scptr.pl.gamma_violin(adata, genes=gene_input, groupby=groupby_col, show=False)
if fig:
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
raise StopIteration # skip fallback
except StopIteration:
pass
except Exception:
pass # fall through to manual
# Manual histogram + violin fallback
fig, axes = plt.subplots(1, 2 if groupby_col else 1, figsize=(9 if groupby_col else 5, 3.8))
ax0 = axes[0] if groupby_col else axes
ax0.hist(g_pos, bins=40, color="#2b5797", alpha=0.75, linewidth=0)
if len(g_pos):
ax0.axvline(np.median(g_pos), color="#c0392b", lw=1.5,
label=f"median = {np.median(g_pos):.4f}")
ax0.legend(fontsize=9, frameon=False)
ax0.set_xlabel(f"Ξ³ ({gene_input})", fontsize=10)
ax0.set_ylabel("Cell count", fontsize=10)
ax0.set_title(f"Ξ³ distribution: {gene_input}", fontsize=10, fontweight="bold")
ax0.spines[["top", "right"]].set_visible(False)
if groupby_col:
states = sorted(adata.obs[groupby_col].unique(),
key=lambda x: int(x) if str(x).isdigit() else x)
data_by_state = [g_vals[adata.obs[groupby_col] == s] for s in states]
vp = axes[1].violinplot(data_by_state, positions=range(len(states)),
showmedians=True, showextrema=False)
for body in vp["bodies"]:
body.set_facecolor("#2b5797"); body.set_alpha(0.6)
vp["cmedians"].set_color("#c0392b")
axes[1].set_xticks(range(len(states)))
axes[1].set_xticklabels([str(s) for s in states], fontsize=9)
axes[1].set_xlabel("PT State", fontsize=10)
axes[1].set_ylabel(f"Ξ³ ({gene_input})", fontsize=10)
axes[1].set_title("Ξ³ by PT state", fontsize=10, fontweight="bold")
axes[1].spines[["top", "right"]].set_visible(False)
plt.tight_layout()
st.image(fig_png(fig), use_container_width=True)
plt.close(fig)
st.markdown(
mg(
("Median Ξ³", f"{np.median(g_pos):.4f}" if len(g_pos) else "β", "positive cells"),
("Max Ξ³", f"{np.max(g_vals):.4f}", ""),
("Cells Ξ³ > 0", f"{int((g_vals > 0).sum()):,}", f"of {adata.n_obs:,}"),
),
unsafe_allow_html=True,
)
# ββ Download ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with rtab5:
st.markdown('<div class="sl">Export results</div>', unsafe_allow_html=True)
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("**Full AnnData**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'All layers (Ξ³, Ξ², Ms, Mu) plus PT state assignments and embeddings.</div>',
unsafe_allow_html=True,
)
if st.button("Prepare AnnData (.h5ad)"):
tmp = make_tmp(".h5ad")
with st.spinner("Writingβ¦"):
adata.write_h5ad(tmp)
with open(tmp, "rb") as f:
st.download_button(
"Download .h5ad",
f.read(),
file_name=f"scptr_{st.session_state.dataset_name}.h5ad",
mime="application/octet-stream",
)
with col2:
if gamma is not None:
st.markdown("**Gamma matrix**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'Cells Γ genes CSV of degradation rates Ξ³.</div>',
unsafe_allow_html=True,
)
if st.button("Prepare Ξ³ matrix (.csv)"):
with st.spinner("Building CSVβ¦"):
gamma_df = pd.DataFrame(
gamma,
index=adata.obs_names,
columns=adata.var_names,
)
csv_bytes = gamma_df.to_csv().encode()
st.download_button(
"Download gamma.csv",
csv_bytes,
file_name=f"gamma_{st.session_state.dataset_name}.csv",
mime="text/csv",
)
with col3:
st.markdown("**Cell metadata**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'PT state assignments per cell (CSV).</div>',
unsafe_allow_html=True,
)
obs_cols = [c for c in ["pt_state"] if c in adata.obs.columns]
if obs_cols:
obs_csv = adata.obs[obs_cols].copy().to_csv().encode()
st.download_button(
"Download metadata.csv",
obs_csv,
file_name=f"metadata_{st.session_state.dataset_name}.csv",
mime="text/csv",
)
else:
st.markdown(
'<div class="wbox" style="font-size:12px">Run <b>Discover PT States</b> first to populate cell metadata.</div>',
unsafe_allow_html=True,
)
st.markdown('<hr>', unsafe_allow_html=True)
col_d1, col_d2, col_d3 = st.columns(3)
with col_d1:
if beta is not None:
st.markdown("**Gene splicing rates (Ξ²)**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'Per-gene Ξ² from phase portrait quantile regression.</div>',
unsafe_allow_html=True,
)
beta_df = pd.DataFrame({"gene": beta.index, "beta": beta.values})
st.download_button(
"Download beta.csv",
beta_df.to_csv(index=False).encode(),
file_name=f"beta_{st.session_state.dataset_name}.csv",
mime="text/csv",
)
with col_d2:
if "tf_score" in adata.var.columns:
st.markdown("**Variance decomposition**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'TF and PTF score per gene (0β1).</div>',
unsafe_allow_html=True,
)
vd_df = pd.DataFrame({
"gene": adata.var_names,
"tf_score": adata.var["tf_score"].values,
"ptf_score": adata.var["ptf_score"].values,
})
st.download_button(
"Download variance_decomp.csv",
vd_df.to_csv(index=False).encode(),
file_name=f"variance_decomp_{st.session_state.dataset_name}.csv",
mime="text/csv",
)
with col_d3:
st.markdown("**Analysis parameters**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'Reproducibility: parameters logged by scPTR.</div>',
unsafe_allow_html=True,
)
params_log = adata.uns.get("scptr", {})
if params_log:
st.download_button(
"Download parameters.json",
json.dumps(params_log, indent=2, default=str).encode(),
file_name=f"parameters_{st.session_state.dataset_name}.json",
mime="application/json",
)
else:
st.markdown(
'<div class="wbox" style="font-size:12px">No logged parameters yet β run the full pipeline to populate.</div>',
unsafe_allow_html=True,
)
if len(net) > 0:
st.markdown('<hr>', unsafe_allow_html=True)
st.markdown("**RBPβtarget network**")
st.markdown(
'<div style="font-size:12px;color:#555;margin-bottom:0.5rem">'
'Columns: regulator, target, weight. Positive weight = destabilizing; negative = stabilizing.</div>',
unsafe_allow_html=True,
)
net_csv = net.to_csv(index=False).encode()
st.download_button(
"Download network.csv",
net_csv,
file_name=f"network_{st.session_state.dataset_name}.csv",
mime="text/csv",
)
st.markdown('<hr>', unsafe_allow_html=True)
if st.button("β Start Over"):
clean_tmps()
for k, v in {
"step": 1,
"adata": None,
"dataset_name": None,
"preprocessed": False,
"estimated": False,
"states_done": False,
"velocity_done": False,
"network_done": False,
}.items():
st.session_state[k] = v
nav("analysis"); st.rerun()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# DOCUMENTATION PAGE
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
elif page == "docs":
st.markdown('<div class="pt">Documentation</div>', unsafe_allow_html=True)
st.markdown(
'<div class="pd">Method details, parameter reference, and interpretation guide.</div>',
unsafe_allow_html=True,
)
dtab1, dtab2, dtab3, dtab4, dtab5, dtab6 = st.tabs(["METHOD", "PARAMETERS", "INTERPRETATION", "QUICK START", "TROUBLESHOOTING", "CITATION"])
# ββ Method ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with dtab1:
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">The kinetic model</div>'
'<div class="doc-p">'
'scPTR is based on the kinetic model of RNA metabolism where each gene g has a '
'transcription rate Ξ±, splicing rate Ξ², and degradation rate Ξ³. At steady state, '
'the rates satisfy:'
'</div>'
'<div class="doc-code">'
'du/dt = Ξ± β Ξ² Β· u = 0 β u* = Ξ±/Ξ²\n'
'ds/dt = Ξ² Β· u β Ξ³ Β· s = 0 β s* = Ξ±/Ξ³\n\n'
'Therefore: Ξ³_ig = Ξ²_g Β· u_ig / s_ig'
'</div>'
'<div class="doc-p">'
'This means Ξ³ is directly estimable from observed spliced and unspliced counts, '
'given an estimate of Ξ². Critically, Ξ³ can vary <em>per cell</em> because of '
'post-transcriptional regulatory programs (miRNA-mediated repression, RBP binding, '
'codon usage, etc.).'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Beta estimation</div>'
'<div class="doc-p">'
'The splicing rate Ξ²<sub>g</sub> is gene-specific and estimated from the phase portrait '
'(u vs. s plot) using quantile regression on the upper boundary of the u/s ratio. '
'This approach captures the slope of the kinetic upper boundary, which reflects the '
'maximum u/s ratio observed across cells β corresponding to minimal degradation.'
'</div>'
'<div class="doc-p">'
'Concretely, Ξ²Μ<sub>g</sub> = quantile(u<sub>ig</sub> / s<sub>ig</sub>, q=0.95) '
'clipped at the 99th percentile of positive values.'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Post-transcriptional state discovery</div>'
'<div class="doc-p">'
'To identify cell populations with distinct degradation programs, scPTR:'
'</div>'
'<ol style="font-size:13px;color:#333;line-height:2;margin:0 0 0.5rem 1.25rem">'
'<li>Computes PCA on the Ξ³ matrix (cells Γ genes)</li>'
'<li>Builds a kNN graph in Ξ³-PCA space</li>'
'<li>Runs Leiden community detection</li>'
'<li>Embeds in 2D using UMAP for visualization</li>'
'</ol>'
'<div class="doc-p">'
'These clusters can differ from expression-based clusters β a cell may be '
'transcriptionally similar to its neighbors but have a distinct degradation program '
'("expression-invisible" state).'
'</div>'
'<div class="doc-p">'
'<b>Zero-permutation control:</b> scPTR validates that PT states are not artifactual '
'by permuting Ξ³ values randomly across cells and recomputing clusters. The adjusted '
'Rand index (ARI) between real and permuted PT states should be near 0.'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">RBPβtarget network inference</div>'
'<div class="doc-p">'
'For each target gene, scPTR regresses its Ξ³ values across cells on the '
'smoothed expression of regulator genes (putative RBPs) using elastic net regression:'
'</div>'
'<div class="doc-code">Ξ³_target ~ Ξ£_r coef_r Β· expr_r + Ξ΅</div>'
'<div class="doc-p">'
'Non-zero coefficients indicate regulatory relationships. Positive coefficients imply '
'the regulator promotes degradation (destabilizing); negative implies protection '
'(stabilizing).'
'</div>'
'</div>',
unsafe_allow_html=True,
)
# ββ Parameters ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with dtab2:
def param_row(name, type_, default, desc):
return (
f'<tr>'
f'<td><span class="param-name">{name}</span></td>'
f'<td><span class="param-type">{type_}</span></td>'
f'<td>{default}</td>'
f'<td class="txt param-desc">{desc}</td>'
f'</tr>'
)
tables = [
("pp.filter_genes", [
("min_unspliced_counts", "int", "10", "Minimum total unspliced count across all cells."),
("min_unspliced_cells", "int", "5", "Minimum cells with nonzero unspliced expression."),
("min_spliced_counts", "int", "0", "Minimum total spliced count (rarely changed)."),
]),
("pp.neighbors", [
("n_neighbors", "int", "30", "Number of nearest neighbors for the cell graph."),
("n_pcs", "int", "30", "PCA components used to build the neighbor graph."),
]),
("pp.smooth_layers", [
("bandwidth", "float | None", "None", "Gaussian kernel bandwidth. None = adaptive (median kNN distance)."),
]),
("tl.estimate_beta", [
("quantile", "float", "0.95", "Upper quantile of u/s ratio used as the kinetic boundary."),
]),
("tl.estimate_gamma", [
("clip_quantile", "float", "0.99", "Per-gene clipping quantile to remove extreme Ξ³ values."),
("min_spliced", "float", "0.01", "Minimum smoothed spliced (Ms) for reliable Ξ³; below this Ξ³ = 0."),
("mode", "str", "'steady_state'", "'steady_state' or 'dynamic' (uses ds/dt from RNA velocity)."),
]),
("tl.pt_states", [
("resolution", "float", "0.5", "Leiden resolution. Higher β more, smaller clusters."),
("n_pcs", "int", "30", "PCA components computed from the Ξ³ matrix."),
("n_neighbors", "int", "30", "kNN neighbors in Ξ³-PCA space."),
("random_state", "int", "0", "Random seed for PCA, UMAP, and Leiden."),
]),
("tl.pt_velocity", [
("use_graph", "str", "'gamma'", "'gamma' = Ξ³-space kNN graph; 'expression' = expression kNN graph."),
]),
("tl.infer_network", [
("regulators", "list | None", "None", "Regulator gene names. None = all genes used as regulators."),
("targets", "list | None", "None", "Target gene names. None = all genes used as targets."),
("method", "str", "'elasticnet'", "Regression method. Currently only 'elasticnet'."),
("alpha", "float", "0.5", "Elastic net mixing: 0 = ridge, 1 = lasso, 0.5 = elastic net."),
("n_top", "int", "50", "Maximum edges to retain per target gene."),
]),
]
for fn_name, params in tables:
rows = "".join(param_row(*p) for p in params)
st.markdown(
f'<div class="doc-sec">'
f'<div class="doc-h2"><code>{fn_name}</code></div>'
f'<table class="tbl">'
f'<thead><tr><th>Parameter</th><th>Type</th><th>Default</th><th>Description</th></tr></thead>'
f'<tbody>{rows}</tbody>'
f'</table>'
f'</div>',
unsafe_allow_html=True,
)
# ββ Interpretation ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with dtab3:
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Interpreting Ξ³ values</div>'
'<div class="doc-p">'
'Ξ³ (gamma) represents the <b>mRNA degradation rate</b> β higher Ξ³ means faster '
'turnover. Key relationships:'
'</div>'
'<table class="tbl">'
'<thead><tr><th>Ξ³ value</th><th>Meaning</th></tr></thead>'
'<tbody>'
'<tr><td>Ξ³ = 0</td><td class="txt">Insufficient spliced counts; rate not estimable</td></tr>'
'<tr><td>Ξ³ β small (e.g., 0.001)</td><td class="txt">Slow degradation; stable mRNA</td></tr>'
'<tr><td>Ξ³ β large (e.g., 0.5+)</td><td class="txt">Rapid degradation; unstable mRNA</td></tr>'
'<tr><td>Ξ³ varies across cells</td><td class="txt">Post-transcriptional regulation; likely RBP or miRNA activity</td></tr>'
'</tbody></table>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Interpreting PT states</div>'
'<div class="doc-p">'
'A PT state is a cluster of cells with similar Ξ³ profiles. Biologically, this means:'
'</div>'
'<ul style="font-size:13px;color:#333;line-height:2;margin:0 0 0.5rem 1.25rem">'
'<li><b>Same PT state, same expression cluster</b>: transcriptional and post-transcriptional programs are aligned</li>'
'<li><b>Different PT states within one expression cluster</b>: expression-invisible state β cells look identical transcriptionally but differ in RNA stability</li>'
'<li><b>Same PT state, different expression clusters</b>: convergent post-transcriptional programs across distinct cell types</li>'
'</ul>'
'<div class="doc-p">'
'Use the <b>silhouette score</b> to assess separation: higher in Ξ³-space than '
'expression-space indicates a genuine post-transcriptional signature.'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Interpreting PT velocity</div>'
'<div class="doc-p">'
'PT velocity captures the <b>direction and magnitude of change in Ξ³</b> across the '
'Ξ³-space neighborhood graph. Unlike RNA velocity (which reflects nascent β mature RNA '
'flow), PT velocity is orthogonal β it reflects where in Ξ³-space a cell is headed.'
'</div>'
'<ul style="font-size:13px;color:#333;line-height:2;margin:0 0 0.5rem 1.25rem">'
'<li><b>Arrow direction</b>: predicted future Ξ³ profile of the cell</li>'
'<li><b>Arrow length</b>: magnitude of degradation-rate change (longer = faster transition)</li>'
'<li><b>Coherent arrows</b> in a region suggest a shared post-transcriptional program being gained or lost</li>'
'<li><b>Divergent arrows</b> suggest a decision point where cells adopt different degradation programs</li>'
'</ul>'
'<div class="doc-p">'
'PT velocity precedes expression changes for 54β78% of transition genes '
'(pancreas p < 10β»β΅β·, dentate gyrus p = 9.9Γ10β»ΒΉΒ³), making it a leading '
'indicator of cell-fate decisions.'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Interpreting network edges</div>'
'<div class="doc-p">'
'RNA-binding proteins (RBPs) are post-transcriptional regulators that bind mRNA to control '
'stability, splicing, and translation. scPTR infers RBPβtarget edges where RBP expression '
'predicts Ξ³ of the target, using library-size-corrected elastic net regression.'
'</div>'
'<div class="doc-p">'
'Each edge has a <b>weight</b> (regression coefficient):'
'</div>'
'<ul style="font-size:13px;color:#333;line-height:2;margin:0 0 0.5rem 1.25rem">'
'<li><b>Positive weight</b>: RBP expression correlates with higher Ξ³ β '
'the RBP <em>destabilizes</em> the target mRNA</li>'
'<li><b>Negative weight</b>: RBP expression correlates with lower Ξ³ β '
'the RBP <em>stabilizes</em> the target mRNA</li>'
'<li><b>|weight| magnitude</b>: filter by |weight| > 0.05 to retain high-confidence edges</li>'
'<li><b>Hub regulators</b> (many edges) are likely master regulators '
'β validated hubs include HNRNPA1, YBX1, ELAVL1/HuR, RBFOX1</li>'
'</ul>'
'<div class="doc-p">'
'To validate edges, cross-reference with CLIP-seq databases (e.g., ENCODE eCLIP, '
'ATtRACT) or miRNA target databases (TargetScan) for experimental support.'
'</div>'
'</div>',
unsafe_allow_html=True,
)
# ββ Quick Start βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with dtab4:
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Python quick start</div>'
'<div class="doc-code">'
'import scptr\n\n'
'# Load data (must have spliced + unspliced layers)\n'
'adata = scptr.read_h5ad("your_data.h5ad")\n\n'
'# Preprocessing\n'
'scptr.pp.filter_genes(adata, min_unspliced_counts=10, min_unspliced_cells=5)\n'
'scptr.pp.normalize_layers(adata)\n'
'scptr.pp.neighbors(adata, n_neighbors=30, n_pcs=30)\n'
'scptr.pp.smooth_layers(adata) # Gaussian kernel smoothing\n\n'
'# Rate estimation\n'
'scptr.tl.estimate_beta(adata, quantile=0.95)\n'
'scptr.tl.estimate_gamma(adata, clip_quantile=0.99, min_spliced=0.01)\n'
'scptr.tl.variance_decomposition(adata)\n\n'
'# PT state discovery\n'
'scptr.tl.pt_states(adata, resolution=0.5, n_pcs=20, n_neighbors=15)\n\n'
'# Optional: PT velocity\n'
'scptr.tl.pt_velocity(adata, use_graph="gamma")\n\n'
'# Optional: RBPβtarget network\n'
'net = scptr.tl.infer_network(adata, alpha=0.5, n_top=50)\n'
'# Results in adata.uns["pt_network"]\n\n'
'# Visualization\n'
'scptr.pl.pt_umap(adata) # Ξ³-space UMAP\n'
'scptr.pl.gamma_heatmap(adata, groupby="pt_state")\n'
'scptr.pl.network_graph(adata, n_edges=50)'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Installation</div>'
'<div class="doc-code">'
'pip install . # core package\n'
'pip install ".[datasets]" # built-in pancreas / dentate gyrus\n'
'pip install ".[deep]" # DeepPTR (PyTorch)\n'
'pip install ".[dev]" # pytest for testing'
'</div>'
'<div class="doc-h3">Requirements</div>'
'<div class="doc-p">'
'Python β₯ 3.9, anndata β₯ 0.8, scanpy β₯ 1.9, numpy β₯ 1.21, scipy β₯ 1.7, '
'scikit-learn β₯ 1.0, numba β₯ 0.55, matplotlib β₯ 3.5'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Data preparation</div>'
'<div class="doc-p">'
'scPTR requires <b>raw, un-normalized</b> spliced and unspliced counts '
'in an AnnData <code>.h5ad</code> file. Tools for generating these:'
'</div>'
'<table class="tbl">'
'<thead><tr><th>Tool</th><th>Notes</th></tr></thead>'
'<tbody>'
'<tr><td>STARsolo</td><td class="txt">Produces spliced/unspliced with <code>--soloFeatures SJ Gene Velocyto</code></td></tr>'
'<tr><td>Alevin-fry</td><td class="txt">Use <code>splici</code> index; outputs spliced + unspliced per cell</td></tr>'
'<tr><td>velocyto</td><td class="txt">Run on BAM files post-alignment; outputs loom β convert to h5ad</td></tr>'
'<tr><td>kallisto|bustools</td><td class="txt">Use kb-python with <code>--workflow lamanno</code></td></tr>'
'</tbody></table>'
'<div class="doc-h3">Input data requirements</div>'
'<div class="doc-p">'
'scPTR requires <b>raw (un-normalized) counts</b> in both layers. '
'Do <em>not</em> pre-normalize with Seurat, Scanpy, or other pipelines β '
'scPTR performs its own library-size normalization internally. '
'STARsolo and Alevin-fry output is already raw; velocyto loom files are also raw.'
'</div>'
'<div class="doc-h3">Verify your data has required layers</div>'
'<div class="doc-code">'
'import anndata as ad\n'
'adata = ad.read_h5ad("your_data.h5ad")\n'
'print(list(adata.layers.keys())) # must include "spliced" and "unspliced"\n'
'print(adata.shape) # (n_cells, n_genes)\n'
'# Check counts are raw (integers)\n'
'import numpy as np\n'
'print(np.allclose(adata.layers["spliced"] % 1, 0)) # True = raw counts'
'</div>'
'</div>',
unsafe_allow_html=True,
)
# ββ Troubleshooting βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with dtab5:
issues = [
("Missing spliced/unspliced layers",
"Error: 'spliced' not in adata.layers",
"Your file lacks the required layers. Re-run alignment with STARsolo "
"(<code>--soloFeatures Velocyto</code>), velocyto, or Alevin-fry with a splici index."),
("All Ξ³ values are zero",
"Gamma layer exists but all values are 0",
"This happens when Ms (smoothed spliced) is below the <code>min_spliced</code> threshold everywhere. "
"Try reducing Min smoothed spliced (Ms) to 0.001 in step 3, or check that normalization ran correctly."),
("Very few genes after filtering",
"n_vars drops to < 100 after preprocessing",
"Lower <em>Min total unspliced counts</em> or <em>Min cells with nonzero unspliced</em> in step 2. "
"Some datasets have sparse unspliced counts β try 5 and 3 respectively."),
("PT states = 1 (all cells in one cluster)",
"Only 1 Leiden cluster found",
"Increase the Leiden resolution (try 1.0β2.0). Also check that the Ξ³ matrix has variance β "
"if most values are 0, the clustering will be uninformative."),
("Network inference is slow",
"Elastic net taking > 10 minutes",
"Check 'Restrict regulators to known RBPs' to reduce the feature matrix from all genes "
"to ~200 curated RBPs. Also reduce <em>Top edges per target</em> to 20β30."),
("Dataset download fails",
"pooch.HTTPError or FileNotFoundError for example datasets",
"Ensure <code>pip install \"scptr[datasets]\"</code> is installed and you have an internet connection. "
"The pancreas dataset is ~30 MB and dentate gyrus ~25 MB."),
("Memory error on large dataset",
"MemoryError or kernel crash",
"The Ξ³ matrix (cells Γ genes) can be large. Try filtering more aggressively in step 2 "
"(higher min counts). For >20,000 cells, consider subsetting to highly variable genes first."),
("Phase portrait shows no clear line",
"Ξ² estimates are noisy / RΒ² is very low",
"This is normal for some genes. Raise the <em>Phase portrait quantile</em> to 0.97β0.99 "
"to better capture the kinetic upper boundary. Genes with very low counts will always have noisy portraits."),
]
for title, symptom, fix in issues:
st.markdown(
f'<div class="doc-sec">'
f'<div class="doc-h2">{title}</div>'
f'<div class="doc-h3">Symptom</div>'
f'<div class="doc-code">{symptom}</div>'
f'<div class="doc-h3">Fix</div>'
f'<div class="doc-p">{fix}</div>'
f'</div>',
unsafe_allow_html=True,
)
# ββ Citation ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with dtab6:
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Citing scPTR</div>'
'<div class="doc-p">'
'If you use scPTR in your research, please cite:'
'</div>'
'<div class="doc-code">'
'Bryan Cheng*, Austin Jin*\n'
'scPTR: Decomposing Post-Transcriptional Regulation at Single-Cell Resolution\n'
'(2026)'
'</div>'
'<div class="doc-h3">BibTeX</div>'
'<div class="doc-code">'
'@article{cheng2026scptr,\n'
' title = {scPTR: Decomposing Post-Transcriptional Regulation\n'
' at Single-Cell Resolution},\n'
' author = {Cheng, Bryan and Jin, Austin},\n'
' year = {2026},\n'
'}'
'</div>'
'</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="doc-sec">'
'<div class="doc-h2">Dependencies to cite</div>'
'<div class="doc-p">'
'scPTR builds on these foundational tools β please also cite them as appropriate:'
'</div>'
'<table class="tbl">'
'<thead><tr><th>Tool</th><th>Use in scPTR</th><th>Reference</th></tr></thead>'
'<tbody>'
'<tr><td>scanpy</td><td class="txt">PCA, neighbors, UMAP, Leiden clustering</td>'
'<td class="txt">Wolf et al., Genome Biology 2018</td></tr>'
'<tr><td>anndata</td><td class="txt">Core data structure</td>'
'<td class="txt">Virshup et al., Nat Methods 2024</td></tr>'
'<tr><td>scikit-learn</td><td class="txt">Elastic net regression (network inference)</td>'
'<td class="txt">Pedregosa et al., JMLR 2011</td></tr>'
'<tr><td>scVelo</td><td class="txt">Steady-state kinetic model inspiration</td>'
'<td class="txt">Bergen et al., Nat Biotechnol 2020</td></tr>'
'</tbody></table>'
'</div>',
unsafe_allow_html=True,
)
|