Modification Probabilities¶
Per-read modification scoring. Three algorithms share an EM-based calibration;
compute_sequential_modification_probabilities drives the hierarchical V1→V2→V3
pipeline.
compute_sequential_modification_probabilities
¶
compute_sequential_modification_probabilities(
contig_result: ContigResult,
*,
shrinkage_window: int = 15,
kappa_high: float = 0.5,
kappa_medium: float = 2.0,
kappa_low: float = 5.0,
mixture_max_iter: int = 100,
mixture_pi_threshold: float = 0.05,
mixture_separation: float = 0.8,
hmm_min_positions: int = 3,
hmm_p_stay_per_base: float = 0.92,
run_hmm: bool = True,
hmm_params: HMMParams | None = None,
emission_source: str = "p_mod_knn",
consistent_fallback: bool = True,
knn_weighted: bool = False,
legacy_scoring: bool = False,
show_progress: bool = True
) -> ContigModificationResult
Run the full V1→V2→V3 hierarchical pipeline on a contig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contig_result
|
ContigResult
|
Output of the eventalign pipeline for one contig. |
required |
shrinkage_window
|
int
|
Half-window in positions (not bases) for local shrinkage. |
15
|
kappa_high
|
float
|
Shrinkage strengths for high/medium/low coverage positions. |
0.5
|
kappa_medium
|
float
|
Shrinkage strengths for high/medium/low coverage positions. |
0.5
|
kappa_low
|
float
|
Shrinkage strengths for high/medium/low coverage positions. |
0.5
|
mixture_max_iter
|
int
|
Max EM iterations for V2 mixture. |
100
|
mixture_pi_threshold
|
float
|
Null-gate threshold on mixing proportion. |
0.05
|
mixture_separation
|
float
|
Minimum effect-size separation for V2 gate. |
0.8
|
hmm_min_positions
|
int
|
Minimum trajectory length for HMM. |
3
|
hmm_p_stay_per_base
|
float
|
Per-base state persistence for HMM transitions. |
0.92
|
run_hmm
|
bool
|
If False, skip V3 entirely. |
True
|
hmm_params
|
HMMParams | None
|
Trained HMM parameters. When provided, overrides
|
None
|
emission_source
|
str
|
Which per-read score field to use as HMM emissions. Also gates whether V1 (empirical-Bayes null + shrinkage) and V2 (anchored mixture EM) actually run:
|
'p_mod_knn'
|
consistent_fallback
|
bool
|
If True (default, Fix A), set the short-trajectory fallback to
emission_source instead of V2 |
True
|
knn_weighted
|
bool
|
If True (Fix B), use distance-weighted kNN scoring instead of unweighted counting. |
False
|
Returns:
| Type | Description |
|---|---|
ContigModificationResult
|
|
Source code in baleen/eventalign/_hierarchical.py
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 | |
compute_modification_probabilities
¶
compute_modification_probabilities(
distance_matrix: NDArray[float64],
n_native: int,
n_ivt: int,
position: int = -1,
*,
algorithms: Optional[Sequence[AlgorithmName]] = None,
knn_k: Optional[int] = None,
mds_components: int = 2,
max_iter: int = 100,
pi_threshold: float = 0.05
) -> dict[AlgorithmName, ModificationProbabilities]
Run one or more algorithms on a distance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance_matrix
|
shape(n_native + n_ivt, n_native + n_ivt)
|
Symmetric DTW distance matrix. Rows/columns ordered: native reads first, then IVT reads. |
required |
n_native
|
int
|
Number of native and IVT reads. |
required |
n_ivt
|
int
|
Number of native and IVT reads. |
required |
position
|
int
|
Genomic position (for labelling only). |
-1
|
algorithms
|
sequence of AlgorithmName
|
Which algorithms to run. Defaults to all three. |
None
|
knn_k
|
int
|
k for kNN algorithm. |
None
|
mds_components
|
int
|
Embedding dimensionality for MDS+GMM. |
2
|
max_iter
|
int
|
Maximum EM iterations. |
100
|
pi_threshold
|
float
|
Null-gate threshold on mixing proportion. |
0.05
|
Returns:
| Type | Description |
|---|---|
dict mapping AlgorithmName → ModificationProbabilities
|
|
Source code in baleen/eventalign/_probability.py
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 | |
ModificationProbabilities
dataclass
¶
ModificationProbabilities(
algorithm: AlgorithmName,
position: int,
probabilities: NDArray[float64],
n_native: int,
n_ivt: int,
scores: NDArray[float64],
null_gate_active: bool,
mixing_proportion: float,
)
Per-read modification probabilities from a single algorithm.
probabilities
instance-attribute
¶
Shape (n_native + n_ivt,) — native reads first, then IVT.
scores
instance-attribute
¶
Raw scalar scores (before calibration). Same ordering as probabilities.
null_gate_active
instance-attribute
¶
True if the position was classified as unmodified (all probs set to 0).
mixing_proportion
instance-attribute
¶
Fitted mixing proportion π for the alternative component.
AlgorithmName
¶
Bases: str, Enum
Scoring algorithms¶
distance_to_ivt
¶
distance_to_ivt(
distance_matrix: NDArray[float64],
n_native: int,
n_ivt: int,
*,
max_iter: int = 100,
pi_threshold: float = 0.05
) -> ModificationProbabilities
Algorithm 1: Robust Distance-to-IVT Baseline.
Each read is scored by its median DTW distance to IVT controls, then calibrated via a Normal null + Normal alternative EM mixture.
Source code in baleen/eventalign/_probability.py
knn_ivt_purity
¶
knn_ivt_purity(
distance_matrix: NDArray[float64],
n_native: int,
n_ivt: int,
*,
k: Optional[int] = None,
lof_weight: float = 0.3,
max_iter: int = 100,
pi_threshold: float = 0.05
) -> ModificationProbabilities
Algorithm 3: kNN IVT-Purity Score with LOF blending.
Each read is scored by how few of its k nearest neighbors are IVT controls, blended with a Local Outlier Factor (LOF) anomaly score, then calibrated via a Beta null + Beta alternative EM mixture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lof_weight
|
float
|
Weight for LOF score in the blend (0 = pure kNN, 1 = pure LOF). Default 0.3 gives 70% kNN purity + 30% LOF anomaly signal. |
0.3
|
Source code in baleen/eventalign/_probability.py
mds_gmm
¶
mds_gmm(
distance_matrix: NDArray[float64],
n_native: int,
n_ivt: int,
*,
n_components: int = 2,
max_iter: int = 100,
pi_threshold: float = 0.05
) -> ModificationProbabilities
Algorithm 5: MDS + Anchored Gaussian Mixture.
Embed the full distance matrix into low-dimensional space via classical MDS, then fit an IVT-anchored null Gaussian and a native alternative Gaussian using EM.
Source code in baleen/eventalign/_probability.py
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 | |