Skip to content

zombi2.genomes

Level 2: genomes evolving along the species tree. The level has three resolutions — family ⊂ ordered ⊂ nucleotide — and one entry point each; more detail costs more compute, so the resolution is a dial.

zombi2.genomes.simulate_genomes_family

simulate_genomes_family(tree, *, duplication=0.0, transfer=0.0, loss=0.0, origination=0.0, transfer_to='uniform', replacement=False, self_transfer=False, initial_families=100, families=None, max_family_size=10, joint=False, seed=None, parallel=False, stream_to=None, outputs=None, progress=False, **retired) -> 'FamilyGenomesResult | StreamedRun'

Evolve a multiset of gene families along a species tree by duplication, transfer, loss, and origination.

tree is the complete species tree (a Tree, or a SpeciesResult whose complete_tree is used). Genomes evolve on every lineage, extant and extinct alike, so the true gene-tree history is complete and a transfer can arrive "from the dead"; the observed genomes are the extant tips.

Rates (each a scope(base) × modifiers spec): duplication/transfer/loss default per copy, origination per lineage. When a transfer fires it moves a copy from a uniformly-chosen donor copy to a recipient lineage alive at that instant, chosen by transfer_to"uniform" (any other contemporaneous lineage), "distance" / Distance(decay=) (closer relatives likelier), Clades({...}, Between({...})) (weighted by the donor's and recipient's named clade, so transfer can run between two clades — see below), or Recipients().weighted_by(driver, mapping) (weighted by an evolved value; see below). replacement=True overwrites a homologous copy in the recipient (additive fallback if it has none); self_transfer=True lets a lineage donate to itself. The root starts with initial_families families of one copy each, recorded as originations at the origin. families=[family("toxin")] additionally declares named families — each gets a normal (integer) family id, but its name is remembered in result.family_names so you can track a specific family (result.has_family(node, "toxin")); this is the handle a joint scaled_by("genomes:toxin", …) reads. Deterministic given seed.

A family with rates of its own. families=[family("IS1", transfer=PerCopy(1.5), loss=0.02)] declares a named family and gives it its own rates, so one family can behave nothing like the rest of the genome — a mobile element that transfers constantly, a core gene that is almost never lost. Whatever a family leaves out falls back to the run's rate for that event, so families=[family("toxin")] is a family declared for its name alone and is exactly families=[family("toxin")]. family() also takes origin= and module=, which are origins= and modules= said on the family itself. Origination takes no per-family value: when it is read the family does not exist yet to have one.

A written rate is that family's rate, in the rate's own units. The run's rate does not reach it, and neither does a varying_among('families', …) draw meant to vary the run's rate among families (SPEC §5's argument for set_by, one level down). Two things are refused for now: a family's rate carrying a verb of its own, and a family's rate beside a driven run rate; both arrive with the joint step. So does the per-family engine — parallel= and stream_to= build one set of rates for the whole run, so they refuse a family that writes its own.

A family placed by hand. origins=[("n5", 0.4)] originates a family on lineage n5 at time 0.4 — the same event the origination rate produces, at a point you choose rather than one that is drawn. It adds to the run: whatever initial_families and origination give you is still there, so origins with both of those at 0 is a tree carrying exactly the families you placed, and origins beside them is an ordinary genome with one family planted where you want it. The time is the run's own clock and must fall inside that lineage's life (None puts it at the branch's start); a placed family is an ordinary family from that instant on, so it duplicates, transfers and is lost like any other, and gets its gene tree the same way. The ids come straight after the initial and named ones, in the order you wrote the origins, on either engine — so the family you placed is initial_families + len(family_names). See resolve_origins.

Conditioning (a trait drives a rate). Any of the four rates may be driven by another levelloss = PerCopy(0.25).scaled_by("trait_events.tsv", {"aquatic": 3.0, "terrestrial": 1.0}) scales each lineage's loss by the habitat on that branch, read from a driver file grown first (traits.simulate_discrete(...).write(dir, outputs=("events",)), which writes trait_events.tsv). A driven rate is then per-lineage: it is summed over the living lineages (each with its own copy count and driver value), the affected lineage is drawn weighted by its rate, and the Gillespie steps at every mid-branch switch of the driver (SPEC §2). For transfer the affected lineage is the donor, so a driven transfer says how often a lineage donates.

Conditioning (a trait drives who receives). transfer_to = Recipients().weighted_by(driver, mapping) is the other half, and a different model: the mapping's numbers are per-candidate weights, not rate multipliers, so they leave the total amount of transfer alone and only redistribute it (SPEC §5, a weight, not a rate). Candidate lineage k gets weight mapping(driver value on k now) and receives with probability w_k / Σw — five candidates at weight 1 and five at weight 2 send two thirds of transfers to the weight-2 group. Weight 0 means "cannot receive"; when every candidate weighs 0 the transfer does not happen (see _do_transfer()). The two driven arguments are independent and may be used together or apart.

parallel opts into a separate engine that evolves the families concurrently, one per worker process — the families are independent (a transfer roams a copy across lineages but never mixes two families), so it enumerates every family's origination first and then evolves each on its own. It is worker-count invariant (each family draws from its own stream spawned off seed) but gives a different-though-equally-valid draw than the serial default for a given seed. False (default) runs the serial loop; True uses every core; an int sets the worker count. A driven rate or a weighted transfer_to runs on the per-family engine too — conditioning does not couple families, so nothing here forces a fallback. The gain is real but modest (a merge over the whole event log stays serial), so a handful of workers is the sweet spot; unlike the sequences level it does not scale far. Because it spawns processes, a calling script must guard its entry with if __name__ == "__main__": (the zombi2 CLI already does).

stream_to=DIR takes the same engine to the many-families regime: each family is written straight to disk as it finishes — no whole-run merge, no run held in memory (a run that fills gigabytes in memory streams in tens of megabytes) — and a light StreamedRun handle comes back instead of a FamilyGenomesResult. outputs= picks which files, as FamilyGenomesResult.write() takes them minus summary (a streamed run writes no genome_summary.json); the default is all six. It is the per-family engine, and outputs without stream_to is an error.

Source code in zombi2/genomes/family.py
 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
@without_cyclic_gc
def simulate_genomes_family(tree, *, duplication=0.0, transfer=0.0, loss=0.0, origination=0.0,
                            transfer_to="uniform", replacement=False, self_transfer=False,
                            initial_families=100, families=None, max_family_size=10, joint=False,
                            seed=None, parallel=False, stream_to=None, outputs=None,
                            progress=False, **retired) -> "FamilyGenomesResult | StreamedRun":
    """Evolve a multiset of gene families along a species tree by duplication, transfer, loss, and
    origination.

    ``tree`` is the **complete** species tree (a `Tree`, or a
    `SpeciesResult` whose ``complete_tree`` is used). Genomes evolve on
    **every** lineage, extant and extinct alike, so the true gene-tree history is complete and a
    transfer can arrive "from the dead"; the observed genomes are the extant tips.

    Rates (each a ``scope(base) × modifiers`` spec): ``duplication``/``transfer``/``loss`` default
    **per copy**, ``origination`` **per lineage**. When a transfer fires it moves a copy from a
    uniformly-chosen donor copy to a recipient lineage alive at that instant, chosen by
    ``transfer_to`` — ``"uniform"`` (any other contemporaneous lineage), ``"distance"`` /
    ``Distance(decay=)`` (closer relatives likelier), ``Clades({...}, Between({...}))`` (weighted by
    the donor's and recipient's **named clade**, so transfer can run *between* two clades — see below),
    or ``Recipients().weighted_by(driver, mapping)`` (weighted by an evolved value; see below). ``replacement=True``
    overwrites a homologous
    copy in the recipient (additive fallback if it has none); ``self_transfer=True`` lets a lineage
    donate to itself. The root starts with ``initial_families`` families of one copy each, recorded
    as originations at the origin. ``families=[family("toxin")]`` additionally declares **named** families —
    each gets a normal (integer) family id, but its name is remembered in ``result.family_names`` so
    you can track a specific family (``result.has_family(node, "toxin")``); this is the handle a joint
    ``scaled_by("genomes:toxin", …)`` reads. Deterministic given ``seed``.

    **A family with rates of its own.** ``families=[family("IS1", transfer=PerCopy(1.5), loss=0.02)]``
    declares a named family and gives it its own rates, so one family can behave nothing like the rest
    of the genome — a mobile element that transfers constantly, a core gene that is almost never lost.
    Whatever a family leaves out falls back to the run's rate for that event, so
    ``families=[family("toxin")]`` is a family declared for its name alone and is exactly
    ``families=[family("toxin")]``. `family()` also takes ``origin=`` and ``module=``, which are
    ``origins=`` and ``modules=`` said on the family itself. Origination takes no per-family value:
    when it is read the family does not exist yet to have one.

    A written rate **is** that family's rate, in the rate's own units. The run's rate does not reach
    it, and neither does a ``varying_among('families', …)`` draw meant to vary the run's rate among
    families (SPEC §5's argument for ``set_by``, one level down). Two things are refused for now: a
    family's rate carrying a verb of its own, and a family's rate beside a *driven* run rate; both
    arrive with the joint step. So does the per-family engine — ``parallel=`` and ``stream_to=``
    build one set of rates for the whole run, so they refuse a family that writes its own.

    **A family placed by hand.** ``origins=[("n5", 0.4)]`` originates a family on lineage ``n5`` at
    time ``0.4`` — the same event the ``origination`` rate produces, at a point you choose rather
    than one that is drawn. It **adds to** the run: whatever ``initial_families`` and ``origination``
    give you is still there, so ``origins`` with both of those at 0 is a tree carrying exactly the
    families you placed, and ``origins`` beside them is an ordinary genome with one family planted
    where you want it. The time is the run's own clock and must fall inside that lineage's life
    (``None`` puts it at the branch's start); a placed family is an ordinary family from that instant
    on, so it duplicates, transfers and is lost like any other, and gets its gene tree the same way.
    The ids come straight after the initial and named ones, in the order you wrote the origins, on
    either engine — so the family you placed is ``initial_families + len(family_names)``. See
    `resolve_origins`.

    **Conditioning (a trait drives a rate).** Any of the four rates may be *driven by another level* —
    ``loss = PerCopy(0.25).scaled_by("trait_events.tsv", {"aquatic": 3.0, "terrestrial": 1.0})`` scales each
    lineage's loss by the habitat on that branch, read from a driver file grown first
    (``traits.simulate_discrete(...).write(dir, outputs=("events",))``, which writes
    ``trait_events.tsv``). A driven rate is then *per-lineage*: it is summed over the living lineages (each with its own copy count and driver
    value), the affected lineage is drawn weighted by its rate, and the Gillespie steps at every
    mid-branch switch of the driver (SPEC §2). For ``transfer`` the affected
    lineage is the **donor**, so a driven ``transfer`` says how often a lineage *donates*.

    **Conditioning (a trait drives who receives).** ``transfer_to =
    Recipients().weighted_by(driver, mapping)`` is
    the other half, and a different model: the mapping's numbers are per-candidate **weights**, not
    rate multipliers, so they leave the total amount of transfer alone and only redistribute it
    (SPEC §5, a weight, not a rate). Candidate lineage ``k`` gets weight ``mapping(driver value on k now)``
    and receives with probability ``w_k / Σw`` — five candidates at weight 1 and five at weight 2 send
    two thirds of transfers to the weight-2 group. Weight 0 means "cannot receive"; when every
    candidate weighs 0 the transfer does not happen (see `_do_transfer()`). The two driven arguments are
    independent and may be used together or apart.

    ``parallel`` opts into a **separate** engine that evolves the families concurrently, one per worker
    process — the families are independent (a transfer roams a copy across lineages but never mixes two
    families), so it enumerates every family's origination first and then evolves each on its own. It is
    worker-count invariant (each family draws from its own stream spawned off ``seed``) but gives a
    different-though-equally-valid draw than the serial default for a given seed. ``False`` (default)
    runs the serial loop; ``True`` uses every core; an ``int`` sets the worker count. A **driven** rate
    or a weighted ``transfer_to`` runs on the per-family engine too — conditioning does not couple
    families, so nothing here forces a fallback. The gain is real but modest (a merge over the whole event log stays
    serial), so a handful of workers is the sweet spot; unlike the sequences level it does not scale far.
    Because it spawns processes, a calling script must guard its entry with ``if __name__ ==
    "__main__":`` (the ``zombi2`` CLI already does).

    ``stream_to=DIR`` takes the same engine to the many-families regime: each family is written straight
    to disk as it finishes — no whole-run merge, no run held in memory (a run that fills gigabytes in
    memory streams in tens of megabytes) — and a light `StreamedRun` handle comes
    back instead of a ``FamilyGenomesResult``. ``outputs=`` picks which files, as
    `FamilyGenomesResult.write()` takes them minus ``summary`` (a streamed run writes no
    ``genome_summary.json``); the default is all six. It is the per-family engine, and
    ``outputs`` without ``stream_to`` is an error.
    """
    tree = as_tree(tree, level="genomes")
    dup = as_rate(duplication, default_scope=PerCopy)
    tra = as_rate(transfer, default_scope=PerCopy)
    los = as_rate(loss, default_scope=PerCopy)
    org = as_rate(origination, default_scope=PerLineage)
    # A scope says *per what* a rate is counted, and the three copy-consuming events implement two
    # answers. **Per copy** (the default) puts every copy independently at risk, so a bigger genome
    # turns over faster. **Per lineage** is a fixed budget: the lineage duplicates or loses at its
    # rate whatever its genome size — and then the total and the pick both have to change, or the
    # rate would say one thing and the picking another. Origination is per lineage alone, because it
    # is the rate at which families are CREATED: per copy it would be base × 0 in an empty genome, a
    # silent no-op. A driver is read per lineage on all four events; on transfer the driven
    # lineage is the DONOR (who receives is the separate transfer_to choice, below).
    for label, rate, legal in (("duplication", dup, (PerCopy, PerLineage)),
                               ("transfer", tra, (PerCopy, PerLineage)),
                               ("loss", los, (PerCopy, PerLineage)),
                               ("origination", org, (PerLineage,))):
        # `rate.scope` holds the scope CLASS, not an instance — a scope constructor returns the rate
        # itself — so this is an identity test against the legal set rather than an isinstance one.
        assert rate.scope is not None            # as_rate fills the level's default where none was written
        if rate.scope not in legal:
            raise ValueError(
                f"{label} has a {rate.scope.__name__} scope, but the family genome engine "
                f"takes {' or '.join(s.__name__ for s in legal)} for {label}."
            )
        for m in rate.modifiers:
            if m.reads == (DRAWN, "families") and label == "origination":
                raise ValueError(
                    "origination carries a per-family draw, but origination is the rate at which families are "
                    "CREATED — when it is read there is no family yet to have drawn a factor for. "
                    "Write varying_among('families', …) on duplication, transfer or loss; writing one "
                    "such object on several of them gives a family-wide tempo, since one object is "
                    "one draw.")
            if isinstance(m, Driven):
                check_not_a_kernel(m.mapping, label=label)
            if is_implemented(m, IMPLEMENTED_MODIFIERS, "genomes.family"):
                continue
            raise ValueError(
                f"{label} carries {describe(m)}, which the family genome engine does not "
                f"support. It takes changing_at (skyline), scaled_by (a conditioned or joint driver), "
                f"set_by (a driver that replaces the base) and varying_among('families', …) "
                f"(per-family heterogeneity). Clade drift is not implemented yet."
            )
    for label, rate in (("duplication", dup), ("transfer", tra), ("loss", los),
                        ("origination", org)):
        rate.check_one_base(label)
    # A per-family draw under a per-lineage scope is refused because what it should MEAN is a
    # modelling decision nobody has taken, not because it is hard. Under PerCopy a family's
    # multiplier scales each copy's own rate, so a genome full of fast families turns over faster —
    # the multiplier moves the total. Under PerLineage the total is fixed by definition, so the same
    # multiplier could only decide *which* copy the event takes, normalised within the lineage. Those
    # are two different models, and running one while the user wrote the other is exactly the silent
    # mismatch this engine refuses everywhere else.
    # The check is over the whole RUN, not per rate, and getting that wrong was a real bug: a
    # per-family draw anywhere makes the engine take its per-family path for **every** gene rate,
    # summing each one over the live copies — so a `PerLineage` rate elsewhere in the same run was
    # silently counted per copy while nothing on the page said so. One draw on duplication was
    # enough to turn a `PerLineage` loss back into a `PerCopy` one.
    per_lineage_here = [lbl for lbl, r in (("duplication", dup), ("transfer", tra), ("loss", los))
                        if r.scope is PerLineage]
    drawn_here = [lbl for lbl, r in (("duplication", dup), ("transfer", tra), ("loss", los))
                  if any(m.reads == (DRAWN, "families") for m in r.modifiers)]
    if per_lineage_here and drawn_here:
        raise ValueError(
            f"{', '.join(per_lineage_here)} is PerLineage while {', '.join(drawn_here)} carries a "
            f"per-family draw, and the two cannot share a run. Under PerCopy a family's multiplier "
            f"scales each copy's rate, so it changes the lineage's total; under PerLineage the total "
            f"is fixed whatever the genome holds, so the multiplier could only choose which copy is "
            f"taken. Those are different models and the choice is not made yet — write PerCopy "
            f"throughout for the first, or drop the per-family draw for the second.")
    # the choice (SPEC §5), validated in the one place all three resolutions share: the mapping's
    # numbers are weights over the candidate recipients, never a rate multiplier
    transfer_to = resolve_transfer_to(transfer_to)
    if isinstance(initial_families, bool) or not isinstance(initial_families, int) or initial_families < 0:
        raise ValueError(f"initial_families must be a non-negative integer, got {initial_families!r}")
    check_no_retired_keywords(retired, where="simulate_genomes_family")
    # every named family, from the one list that declares them
    declared, module_map, planted_named = resolve_families(families, tree)
    family_names = [f.name for f in declared]
    # what each family sets for itself; empty unless some family writes a rate, and then the engine
    # takes the path that sums those beside the run's (see `_family_weights`)
    fam_own = resolve_family_rates(declared, {"duplication": dup, "transfer": tra, "loss": los})
    any_written = bool(fam_own)
    if any_written:
        for key, rate in (("duplication", dup), ("transfer", tra), ("loss", los)):
            if key not in fam_own:
                continue
            assert rate.scope is not None        # `as_rate` filled the level's default above
            if rate.scope is not PerCopy:
                raise ValueError(
                    f"a family writes its own {key}, but the run's {key} is {rate.scope.__name__}. "
                    f"The two are summed over the same copies, so both are counted per copy — write "
                    f"PerCopy for the run's {key}, or drop the family's.")
            # a driven run rate composes: the driver scales the run's rate, and a family that states
            # its own has no run rate in it to scale (see `_driven_weights`)

    # A family's copies in one genome are capped. Growth compounds — a duplication rate above the
    # loss rate multiplies without bound — so a run needs a ceiling somewhere. An int is that number
    # of copies; a float (the default, 10.0) is that multiple of the lineages in the complete tree,
    # so the bound travels with the size of the run. Refusing an event on a condition that depends
    # only on the current state is Poisson thinning, so what is kept is exactly the process whose
    # duplication rate is zero for a family already at its quota — a declared ceiling, not a
    # truncated run. ``None`` removes it.
    cap = resolve_max_family_size(max_family_size)

    # conditioning: a rate written with scaled_by reads a driver per lineage. Resolve each driver once into
    # a DriverTrajectory (value + next-switch lookups, keyed by the shared species node id) — from a
    # file (a str driver) or an object handed over in memory (a trait result, or a genome's presence /
    # completion). No driven rate ⇒ this is empty
    # and the loop stays byte-identical to an undriven run.
    dup_mods, los_mods = _driven_mods(dup), _driven_mods(los)
    org_mods, tra_mods = _driven_mods(org), _driven_mods(tra)
    all_mods = (*dup_mods, *los_mods, *org_mods, *tra_mods)
    # Two kinds of driver, told apart by what the driver *is* (SPEC §5). A finished one — a file, a
    # grown result — was produced before this run started, and the run is conditioned. A **live**
    # name is gene content this run is itself producing, and the run is joint: there is no order to
    # simulate the two in, because they are the same run. Only the first can be resolved to a
    # trajectory; the second is read off the live genome as the loop goes.
    live_mods = [m for m in all_mods if names_a_live_level(m.driver)]
    file_mods = [m for m in all_mods if not names_a_live_level(m.driver)]
    live_keys = resolve_live_drivers(live_mods, set(family_names), joint=joint)
    # driver key → its Driven (deduped, so a driver shared across rates resolves once);
    # the modifier rather than the driver itself, because the driver's step rides on the modifier
    by_key: dict[object, "Driven"] = {}
    for m in file_mods:
        by_key.setdefault(m.key, m)
    resolved = {}
    if by_key:
        resolved = {key: resolve_driver(m.driver, tree, step=m.step, level="genomes.family")
                    for key, m in by_key.items()}
        # a mapping whose states never occur in the driver leaves every lineage at the default factor,
        # so the rate is never driven and the run is secretly the undriven model — refuse it here,
        # naming the driver, rather than let it pass as a driven run
        for m in file_mods:
            label = m.driver if isinstance(m.driver, str) else f"<{type(m.driver).__name__}>"
            check_mapping_fires(m.mapping, resolved[m.key].states(), driver_label=label)
    # `trajs` is the drivers that move a RATE: they alone make the loop per-lineage and set the
    # Gillespie horizon. It is built BEFORE transfer_to is prepared, and that order is
    # load-bearing — a driven transfer_to changes no rate, so its trajectory must not end up here
    # adding horizon breakpoints (see prepare_transfer_to). `resolved` is passed along as the driver
    # cache, so a driver shared between a rate and transfer_to is loaded once.
    trajs = dict(resolved)
    group_of, to_traj = prepare_transfer_to(tree, transfer_to, resolved, level="genomes.family")

    # Parallel is a *separate* engine (opt-in): families are independent, so it evolves them one per
    # process (SPEC-style — serial by default). `stream_to` takes the same engine one step further —
    # each family is written straight to disk and a light StreamedRun handle comes back, so a run of a
    # million families never has to fit in memory (`outputs` picks which files, as `.write` does). The
    # drivers and clade painting above are resolved once, before the split, and handed to whichever
    # engine runs — they are shared validation and shared input, not one engine's business. A
    # configuration neither engine covers still returns None there, so this serial reference loop runs
    # unchanged (decision A); a streamed run raises instead, being unable to fall back without pulling
    # the whole thing into memory.
    if outputs is not None and stream_to is None:
        raise ValueError(
            "outputs applies to a streamed run (stream_to=DIR), which writes the files itself; for an "
            "in-memory run choose them when you call result.write(outputs=...).")
    seed = resolve_seed(seed)     # drawn if none was given, so either engine below records it
    if (parallel or stream_to is not None) and live_keys:
        # The one thing that engine's whole design rests on: a family's history depends on no other
        # family, so each can be evolved alone. A rate reading the genome's own content is exactly
        # that dependence, so this is a refusal rather than a fallback.
        raise ValueError(
            "a joint genome run cannot use the per-family engine (parallel= / stream_to=), which "
            "evolves each family in its own process because families do not affect each other. A "
            "rate reading live gene content is that effect. Drop parallel / stream_to.")
    if (parallel or stream_to is not None) and planted_named:
        # Pass 1 of that engine enumerates every family's origination up front, seeding the declared
        # ones at the root; a family that arrives partway down is not in that enumeration, and
        # threading it through would renumber the families the serial engine mints. Stated rather
        # than silently dropped.
        raise ValueError(
            "family(origin=...) does not run on the per-family engine (parallel= / stream_to=), "
            "which enumerates every family's origination before it starts. Drop parallel / "
            "stream_to, or let the origination rate place the family.")
    if (parallel or stream_to is not None) and any_written:
        # not a fallback: that engine evolves one family per process against a context built once for
        # the whole run, so a per-family rate has nowhere to live in it. Saying so beats silently
        # running every family at the run's rate.
        raise ValueError(
            "a family writing its own rate cannot run on the per-family engine (parallel= / "
            "stream_to=), which builds one set of rates for the whole run and evolves each family "
            "against it. Drop parallel / stream_to, or give every family the run's rate.")
    if parallel or stream_to is not None:
        from ._perfamily import run_parallel_family
        result = run_parallel_family(
            tree, dup=dup, tra=tra, los=los, org=org, transfer_to=transfer_to,
            replacement=replacement, self_transfer=self_transfer, initial_families=initial_families,
            family_names=family_names, placed=[], modules=module_map, cap=cap,
            seed=seed, parallel=parallel,
            progress=progress, stream_to=stream_to, outputs=outputs,
            trajs=trajs, to_traj=to_traj, group_of=group_of,
            driven={"duplication": bool(dup_mods), "transfer": bool(tra_mods),
                    "loss": bool(los_mods), "origination": bool(org_mods)})
        if result is not None:
            return result

    rng, seed = stream("genomes", seed)     # the genomes level's own stream
    copy_counter = 0
    family_counter = 0

    def new_copy(family: int) -> GeneCopy:
        nonlocal copy_counter
        c = GeneCopy(copy_counter, family)
        copy_counter += 1
        return c

    # Per-family multipliers, drawn once when a family is created and then fixed for its whole life.
    # Whether a family's rates move together is decided by what was written: one `Random` object read
    # by two rates is one draw for both, two objects are two draws. Empty unless some rate carries
    # one, and then the engine takes its weighted path; a run carrying none draws nothing here.
    fam_by = {"duplication": tuple(m for m, _ in dup.carried_modifiers(unit="families")),
              "transfer": tuple(m for m, _ in tra.carried_modifiers(unit="families")),
              "loss": tuple(m for m, _ in los.carried_modifiers(unit="families"))}
    any_family = any(fam_by.values()) or any_written
    # A rate carrying nothing per family holds 1.0 for every family, so all such rates share one
    # empty table rather than each filling its own — which is what lets _FamilyWeights sum them once.
    # Sharing is off once some family writes a rate: a family may write its loss and not its
    # duplication, so the two tables then hold different numbers for the same family.
    no_variation: dict[int, float] = {}
    fam_mult: dict[str, dict[int, float]] = {
        key: ({} if (mods or any_written) else no_variation) for key, mods in fam_by.items()}
    #: per event, the families that set a rate of their own — their own per-copy rate, and 0.0 for
    #: every other family, which contributes through `fam_mult` instead. `None` when nobody does,
    #: and then every expression below is the one it always was (see `_family_weights`).
    fam_fixed: "dict[str, dict[int, float]] | None" = (
        {key: {} for key in fam_by} if any_written else None)
    #: that table for one event, or ``None`` — what a copy pick reads, against the per-lineage *sums*
    #: of the same table that `own_sums` reads. Two different shapes of the same information: a rate
    #: per family here, and per lineage the total over its live copies there.
    own_rates = (lambda key: fam_fixed[key]) if fam_fixed is not None else (lambda key: None)

    def new_family(declared_at: "int | None" = None) -> int:
        """Mint a family id. ``declared_at`` is its index in ``declared`` for a named family, which is
        how it finds the rates it wrote; an anonymous family passes nothing and runs at the run's."""
        nonlocal family_counter
        f = family_counter
        family_counter += 1
        if any_family:
            # one draw per distinct modifier *object* for this family, shared across its rates: the
            # same `Random` object written on duplication and on loss means one number, so a fast
            # family is fast at both. Two separately built ones are two draws even with the same law.
            shared: dict[int, float] = {}
            for key, mods in fam_by.items():
                own = None if declared_at is None else fam_own.get(key, {}).get(declared_at)
                if own is not None:
                    # this family's rate IS the number it wrote, so the run's rate does not reach it
                    # and neither does a draw meant to vary the run's rate among families
                    assert fam_fixed is not None     # a written rate is exactly when it was built
                    fam_mult[key][f] = 0.0
                    fam_fixed[key][f] = own
                    continue
                fam_mult[key][f] = math.prod(values_at_birth(mods, rng, shared))
                if fam_fixed is not None:
                    fam_fixed[key][f] = 0.0
        return f

    # Which of the three copy-consuming rates is a fixed per-lineage budget rather than a per-copy
    # risk. Read once: it decides both how the total is counted and how the affected lineage is
    # picked, and those two must never disagree.
    dup_per_lineage = dup.scope is PerLineage
    los_per_lineage = los.scope is PerLineage
    tra_per_lineage = tra.scope is PerLineage
    any_per_lineage = dup_per_lineage or los_per_lineage or tra_per_lineage

    depth = mean_root_to_tip(tree)  # timescale for Distance weighting (unused by "uniform")
    schedule = sorted((tree.nodes[i].end_time, i) for i in tree.nodes)  # (end_time, node_id)

    root = tree.nodes[tree.root]
    t = root.birth_time
    alive: list[int] = []
    gen: list[list[GeneCopy]] = []
    pos: dict[int, int] = {}
    genomes: dict[int, tuple[GeneCopy, ...]] = {}
    events: list[GeneEdge] = []
    enter(alive, gen, pos, root.id, [])
    for _ in range(initial_families):  # lay down the origin's genome as originations at t = root.birth_time
        _originate(gen[0], root, t, events, new_copy, new_family)
    named: dict[str, int] = {}  # a minted id per declared name (so GeneCopy.family stays an int)
    named_plants: list[tuple[float, int, int]] = []
    for i, spec in enumerate(declared):
        fid = new_family(i)
        named[spec.name] = fid
        if i in planted_named:
            # a declared family given an `origin` is planted there rather than seeded at the origin,
            # which is what `origins=` does for an anonymous one — the same event, with a name on it
            t_p, lineage = planted_named[i]
            named_plants.append((t_p, lineage, fid))
            continue
        c = new_copy(fid)
        gen[0].append(c)
        events.append(GeneEdge(t, "origination", root.id, fid, c.id))
    # A planted family is not seeded into a genome: it arrives at its own time, in the loop below.
    plants = sorted(named_plants)
    plant_i = 0                                                              # walked in time order
    total_copies = len(gen[0])
    initial_genome = tuple(gen[0])   # the run's starting genome: a snapshot before the stem runs

    any_driven = bool(trajs) or bool(live_keys)

    def live_value(src: str, k: int):
        """What a live driver reads on lineage ``k`` **right now** — the joint half of the driver
        mechanism (SPEC §2). A finished driver answers from a trajectory built before the run; this
        one answers from the genome the run is building.

        It needs no horizon breakpoint, and that is what makes the race exact rather than thinned:
        gene content changes only when a genome event fires, and an event ends the current step, so
        every rate is already constant between two events."""
        if src == LIVE_COUNT:
            return len(gen[k])
        return "present" if counts.holds(k, named[src.split(":", 1)[1]]) else "absent"
    # the per-family weight sums, carried across events rather than rebuilt each time (see the class).
    # The families that wrote their own rate ride in the same structure under a suffixed key, because
    # summing a table over a lineage's copies is the same work whichever kind of number is in it.
    _tables = dict(fam_mult)
    if fam_fixed is not None:
        _tables.update({key + _FIXED: m for key, m in fam_fixed.items()})
    weights = _FamilyWeights(_tables, gen) if any_family else None
    counts = _FamilyCounts(gen)      # the family cap's question, answered without walking a genome

    # four bare numbers — the per-copy trio and a per-lineage origination, no modifier on any rate,
    # no family writing its own — is the common run, and it needs none of the loop's context
    # machinery: each total is scope(base) exactly, resolved here once. A rate whose base is None
    # carries a set_by, which is a modifier, so `plain` is False and its 0.0 is never read.
    plain = (not (dup.modifiers or los.modifiers or org.modifiers or tra.modifiers)
             and fam_fixed is None and not any_per_lineage
             and dup.scope is PerCopy and los.scope is PerCopy and tra.scope is PerCopy
             and org.scope is PerLineage)
    dup_base, los_base = dup.base or 0.0, los.base or 0.0
    tra_base, org_base = tra.base or 0.0, org.base or 0.0

    # the species tree's schedule is the run's spine: one entry per speciation/extinction, so how
    # far through it we are is how far through the tree the genomes have got
    bar = progress_bar(len(schedule), "genomes", unit="branch", enabled=progress)
    si = 0
    while si < len(schedule):
        bar.to(si)
        n = total_copies
        if live_keys and n > MAX_LIVE_COPIES:
            raise RuntimeError(
                f"the run passed {MAX_LIVE_COPIES} live gene copies at time {t:.3g} and is still "
                f"growing — a rate reading the genome's own content is feeding itself. Lower the "
                f"rates, flatten the mapping the driver is read through, or set a max_family_size.")
        k_alive = len(alive)
        can_xfer = n > 0 and (k_alive >= 2 or self_transfer)  # a recipient must be able to exist
        next_species = schedule[si][0]  # the tree's own next event: who is alive changes only here
        # a family placed by `origins=` originates at a fixed instant, so it joins the horizon like
        # any other breakpoint: the waiting time can never step over it
        next_plant = plants[plant_i][0] if plant_i < len(plants) else math.inf
        w_dup = w_los = w_org = w_tra = None
        if plain:
            # no modifier on any rate: each total is scope(base) exactly — the per-copy trio times
            # the live copies, origination times the living lineages — none of them ever changes on
            # its own (next_change is inf), and nothing below reads a context or a weight.
            r_dup = dup_base * n
            r_los = los_base * n
            r_org = org_base * k_alive
            r_tra = tra_base * n if can_xfer else 0.0
            horizon = min(next_species, next_plant)
        else:
            ctx = {"copies": n, "lineages": k_alive, "time": t}
            # A copy-consuming event counted *per lineage* is counted per lineage that HOLDS a copy:
            # an empty genome offers nothing to duplicate or lose, so it must not contribute its
            # share of the total and then be picked with no victim inside it. Origination keeps
            # `ctx` — an empty genome can still gain a family. Computed only when some rate needs
            # it, so the per-copy path does exactly the work it did before.
            if any_per_lineage:
                n_hosts = sum(1 for g in gen if g)
                host_ctx = {"copies": n, "lineages": n_hosts, "time": t}
            else:
                n_hosts, host_ctx = 0, ctx
            # a driven rate is per-lineage: sum its effective rate over the living lineages (each
            # read with its own copy count and its branch's driver value), keeping the weights for
            # the affected-lineage pick — the species_tree._grow shape. An undriven rate stays
            # pooled (one .effective, uniform pick), so a run with no driver is byte-identical to
            # before. For transfer the affected lineage is the donor, so a driven transfer weights
            # who donates. A run carrying BOTH a driver and a per-family draw multiplies the two —
            # the driver's factor is the lineage's, the multipliers are its contents' — which is
            # what `_driven_weights` does with `fam_sums`.
            fw = None
            if any_driven:  # each lineage's driver values, read before the weights that multiply them in
                drivers = [{**{key: trajs[key].value(alive[k], t) for key in trajs},
                            **{src: live_value(src, k) for src in live_keys}} for k in range(k_alive)]
            if any_family:
                # A per-copy rate pools over copies, so with per-family multipliers the total is the
                # unit rate times the sum of those multipliers over the live copies — and the copy has
                # to be drawn with the same weights, or the rates would say one thing and the picking
                # another. Summed per lineage, so the existing weighted-lineage pick can be reused.
                assert weights is not None       # `any_family` is exactly when it was built
                fw = weights.current(gen)
                unit = {"duplication": dup.effective(copies=1, lineages=1, time=t),
                        "loss": los.effective(copies=1, lineages=1, time=t),
                        "transfer": tra.effective(copies=1, lineages=1, time=t) if can_xfer else 0.0}
                own_sums = (lambda key: fw[key + _FIXED]) if fam_fixed is not None else (lambda key: None)

                def unit_at(key, k, _rates={"duplication": dup, "loss": los, "transfer": tra}):
                    """The run's unit rate as lineage ``k`` reads it. Identical to ``unit[key]`` unless
                    the rate is driven, and then it is the number `_driven_weights` used for that
                    lineage — which the copy pick has to use too, or the totals and the pick disagree
                    about how a written family rate compares with the run's."""
                    if not any_driven:
                        return unit[key]
                    return _rates[key].effective(copies=1, lineages=1, time=t, drivers=drivers[k])
                w_dup = _family_weights(unit["duplication"], fw["duplication"], own_sums("duplication"))
                w_los = _family_weights(unit["loss"], fw["loss"], own_sums("loss"))
                if can_xfer:
                    w_tra = _family_weights(unit["transfer"], fw["transfer"], own_sums("transfer"))
            if any_driven:
                if dup_mods:
                    w_dup = _driven_weights(dup, gen, k_alive, t, drivers,
                                            fw["duplication"] if fw is not None else None,
                                            own_sums("duplication") if fw is not None else None)
                if los_mods:
                    w_los = _driven_weights(los, gen, k_alive, t, drivers,
                                            fw["loss"] if fw is not None else None,
                                            own_sums("loss") if fw is not None else None)
                if org_mods:
                    # origination can never carry a per-family draw (refused above: when it is read
                    # there is no family yet), so it needs no fam_sums branch
                    w_org = [org.effective(copies=len(gen[k]), lineages=1, time=t, drivers=drivers[k])
                             for k in range(k_alive)]
                if tra_mods and can_xfer:
                    w_tra = _driven_weights(tra, gen, k_alive, t, drivers,
                                            fw["transfer"] if fw is not None else None,
                                            own_sums("transfer") if fw is not None else None)
            r_dup = sum(w_dup) if w_dup is not None else (
                dup.effective(**(host_ctx if dup_per_lineage else ctx)) if n else 0.0)
            r_los = sum(w_los) if w_los is not None else (
                los.effective(**(host_ctx if los_per_lineage else ctx)) if n else 0.0)
            r_org = sum(w_org) if w_org is not None else org.effective(**ctx)
            r_tra = sum(w_tra) if w_tra is not None else (
                tra.effective(**(host_ctx if tra_per_lineage else ctx)) if can_xfer else 0.0)
            horizon = min(next_species, next_plant, dup.next_change(t), los.next_change(t),
                          org.next_change(t), tra.next_change(t))
            if any_driven:  # a driven rate also changes when the driver switches mid-branch — step there
                driver_next = min((trajs[key].next_change(alive[k], t) for key in trajs
                                   for k in range(k_alive)), default=math.inf)
                horizon = min(horizon, driver_next)
        total = r_dup + r_los + r_org + r_tra

        if total > 0.0:
            t_ev = t + float(rng.exponential(1.0 / total))
            if t_ev < horizon:  # a genome event fires before the alive set or the rate changes
                t = t_ev
                r = float(rng.random()) * total
                if r < r_dup:
                    if w_dup is not None:  # weighted lineage, then a copy within it
                        k = weighted_index(rng, w_dup, r_dup)
                        j = (_pick_copy_by_family(rng, gen[k], fam_mult["duplication"],
                                                  own_rates("duplication"), unit_at("duplication", k))
                             if any_family else int(rng.integers(len(gen[k]))))
                    elif dup_per_lineage:  # every occupied genome equally likely, then a copy in it
                        k = _pick_host(rng, gen, n_hosts)
                        j = int(rng.integers(len(gen[k])))
                    else:
                        k, j = _pick_copy(rng, gen, n)
                    fam = gen[k][j].family
                    if not counts.at_cap(k, fam, cap):
                        _duplicate(gen[k], j, tree.nodes[alive[k]], t, events, new_copy)
                        counts.added(k, fam)
                        total_copies += 1
                        if weights is not None:
                            weights.touched(k)
                elif r < r_dup + r_los:
                    if w_los is not None:
                        k = weighted_index(rng, w_los, r_los)
                        j = (_pick_copy_by_family(rng, gen[k], fam_mult["loss"],
                                                  own_rates("loss"), unit_at("loss", k))
                             if any_family else int(rng.integers(len(gen[k]))))
                    elif los_per_lineage:
                        k = _pick_host(rng, gen, n_hosts)
                        j = int(rng.integers(len(gen[k])))
                    else:
                        k, j = _pick_copy(rng, gen, n)
                    counts.removed(k, gen[k][j].family)      # before the copy leaves the genome
                    _lose_at(gen[k], j, tree.nodes[alive[k]], t, events)
                    total_copies -= 1
                    if weights is not None:
                        weights.touched(k)
                elif r < r_dup + r_los + r_org:
                    k = (weighted_index(rng, w_org, r_org) if w_org is not None
                         else int(rng.integers(k_alive)))  # origination is per lineage
                    _originate(gen[k], tree.nodes[alive[k]], t, events, new_copy, new_family)
                    counts.added(k, gen[k][-1].family)       # the copy _originate just appended
                    total_copies += 1
                    if weights is not None:
                        weights.touched(k)
                else:
                    if w_tra is not None:  # driven: weighted DONOR lineage, then a uniform copy in it
                        kd = weighted_index(rng, w_tra, r_tra)
                        if not gen[kd]:    # only via weighted_index's r == total float guard: a
                            # zero-weight lineage has no copies to donate, so take the heaviest instead
                            kd = max(range(k_alive), key=lambda k: w_tra[k])
                        jd = (_pick_copy_by_family(rng, gen[kd], fam_mult["transfer"],
                                                   own_rates("transfer"), unit_at("transfer", kd))
                              if any_family else int(rng.integers(len(gen[kd]))))
                    elif tra_per_lineage:  # every occupied genome donates equally often
                        kd = _pick_host(rng, gen, n_hosts)
                        jd = int(rng.integers(len(gen[kd])))
                    else:
                        kd, jd = _pick_copy(rng, gen, n)
                    delta, kr = _do_transfer(rng, tree, alive, gen, counts, kd, jd, t, events,
                                             new_copy, transfer_to, replacement, self_transfer,
                                             depth, to_traj, cap, group_of)
                    total_copies += delta
                    if weights is not None and kr is not None:
                        weights.touched(kr)   # only the recipient's composition changed (see there)
                continue

        if horizon == next_species:  # advance to the tree's next event(s); process the whole tie-batch
            t = next_species
            while si < len(schedule) and schedule[si][0] == t:
                i = schedule[si][1]
                k_out = pos[i]
                g = gen[k_out]
                genomes[i] = tuple(g)  # finalise this lineage (extant, extinct, or unsampled)
                total_copies -= len(g)
                retire(alive, gen, pos, k_out)
                inherited = counts.retired(k_out)   # what the daughters below inherit, if any
                if weights is not None:
                    weights.retired(k_out)
                node = tree.nodes[i]
                if node.children:  # a speciation: each gene re-ids into each daughter
                    per_daughter = []
                    for c in node.children:
                        child_genome, rows = [], []
                        for old in g:  # ZOMBI1: the gene ends here and continues under a fresh id
                            nc = new_copy(old.family)
                            child_genome.append(nc)
                            rows.append(GeneEdge(t, "speciation", c, old.family, nc.id, parent=old.id))
                        per_daughter.append(rows)
                        enter(alive, gen, pos, c, child_genome)
                        counts.entered_like(inherited)   # a re-id of the parent: same families
                        if weights is not None:
                            weights.entered(child_genome)
                        total_copies += len(child_genome)
                    # the ids are minted daughter by daughter (which is what fixes them), but a gene's
                    # two rows are recorded together: one gene ending is one event, and the log writes
                    # it as one row with both daughters in it.
                    for pair in zip(*per_daughter):
                        events.extend(pair)
                si += 1
        elif plant_i < len(plants) and horizon == next_plant:
            # a placed family arrives. The lineage is live by construction (its time was checked
            # against that branch's own life), and a tie with the tree's schedule falls to the
            # branch above: the daughters have entered by the time this runs.
            t = horizon
            while plant_i < len(plants) and plants[plant_i][0] == t:
                _, lineage, fam = plants[plant_i]
                k = pos[lineage]
                c = new_copy(fam)
                gen[k].append(c)
                events.append(GeneEdge(t, "origination", lineage, fam, c.id))
                counts.added(k, fam)
                total_copies += 1
                if weights is not None:
                    weights.touched(k)
                plant_i += 1
        else:
            t = horizon  # a skyline breakpoint: advance and re-evaluate the (now changed) rate

    bar.close()
    return FamilyGenomesResult(tree, genomes, events, seed, named, module_map, initial_genome,
                               cap)

zombi2.genomes.simulate_genomes_ordered

simulate_genomes_ordered(tree, *, duplication=0.0, transfer=0.0, loss=0.0, origination=0.0, inversion=0.0, transposition=0.0, translocation=0.0, chromosomes=1, topology='circular', fission=0.0, fusion=0.0, chromosome_origination=0.0, chromosome_loss=0.0, duplication_extent=None, loss_extent=None, transfer_extent=None, inversion_extent=None, transposition_extent=None, translocation_extent=None, inversion_probability=0.0, transfer_to='uniform', replacement=False, self_transfer=False, initial_families=100, families=None, joint=False, max_family_size=10, seed=None, progress=False, **retired) -> OrderedGenomesResult

Evolve ordered genomes — genes with a position and an orientation, on chromosomes — along a species tree, by the D/T/L/O core plus segmental rearrangements and the chromosome events.

Every gene-level event acts on an extent — a run of consecutive genes (the ZOMBI1 model): duplication copies the run in tandem, loss removes it, transfer sends it to a contemporaneous recipient as a block, inversion reverses it (flipping strands), transposition relocates it elsewhere on the same chromosome, and translocation moves it to a different chromosome. The run's extent is drawn per event from <event>_extent (a distribution, default Geometric(mean=1) — usually a single gene; dial the mean up for larger blocks). origination is the exception: a family is born once, a single gene, no extent. transposition and translocation land the moved block inverted with probability inversion_probability.

Where a run stops is set by the chromosome's topology. A run goes rightwards from the gene it starts at. On a "circular" chromosome there are no ends, so a run that reaches the last gene continues from the first, and only the whole chromosome bounds it; on a "linear" one the run stops at the last gene. So on a circular chromosome every gene is covered by segmental events at the same rate, and the nominal mean extent is the realised one.

Scopes follow the cross-level grammar, which counts an event per the thing it acts on: the gene-level events — duplication/transfer/loss and the rearrangements inversion/transposition/translocation — are per copy, since each acts on a run of genes that starts at one of them; the chromosome rates fission/fusion/chromosome_loss are per chromosome; and the two events that make something from nothing, origination/chromosome_origination, are per lineage. The run starts with chromosomes chromosomes of the given topology, across which the initial_families founding genes are dealt round-robin; families=[family("toxin")] additionally declares named families (remembered in result.family_names for result.has_family(node, "toxin")), as in the family core; replacement / self_transfer behave as in the family core. So does transfer_to, which chooses who receives"uniform", "distance" / Distance(decay=) (closer relatives likelier), Clades({...}, Between({...})) (weight by the donor's and recipient's named clade) or Recipients().weighted_by(driver, mapping) (weight by another level; see below). What moves is a block of genes rather than a single copy, and the block arrives whole, so the rule chooses the recipient lineage exactly as it does at the family resolution.

The chromosome events change chromosome number: fission (split), fusion (merge, between two chromosomes of the same topology — the reticulation; a ring and a molecule with two ends cannot become one molecule, so a genome of one of each never fuses), chromosome_origination (a de-novo replicon), chromosome_loss (a whole chromosome and its genes die; never the genome's last). Chromosomes carry identity — re-minted at every event that reshapes them — so chromosome_events is the true reticulating chromosome genealogy, rooted at the initial and de-novo originations. Deterministic given seed.

A family placed by hand. origins=[("n5", 0.4)] originates a family on lineage n5 at time 0.4 — the ordinary origination event, at a point you choose rather than one that is drawn, and here too the founding gene lands on a uniformly-chosen chromosome and position. It adds to whatever initial_families and origination already give you. See resolve_origins.

Conditioning (a trait drives a rate). Any rate here may be driven by another levelinversion = PerCopy(0.3).scaled_by(habitat, {"host": 4.0, "free": 1.0}) scales each lineage's inversion rate by the habitat on that branch, read from a trait grown first (the finished TraitsResult, or the trait_events.tsv it wrote). A driven rate is then per lineage: it is summed over the living lineages, each read with its own gene count, chromosome count and driver value; the lineage an event lands on is drawn with those same weights, and the gene inside it uniformly, because the gene count is already in the weight. The Gillespie steps at every mid-branch switch of the driver rather than averaging over a branch (SPEC §2). For transfer the driven lineage is the donor, so a driven transfer says how often a lineage donates.

Conditioning (a trait drives who receives). transfer_to = Recipients().weighted_by(driver, mapping) is the other half, and a different model: the mapping's numbers are per-candidate weights, not rate multipliers, so they leave the total amount of transfer alone and only redistribute it (SPEC §5, a weight, not a rate). Candidate lineage k gets weight mapping(driver value on k now) and receives with probability w_k / Σw. Weight 0 means "cannot receive"; when every candidate weighs 0 the transfer does not happen at all, and the donor's chromosome is left untouched. A Between({...}) mapping reads the donor's value too, so transfer can be steered between guilds; Clades({...}, Between({...})) is the same steering by named clade, read off the tree instead of a driver. Because a weight is not a rate, a driven transfer_to adds no Gillespie breakpoint and composes freely with a driven transfer rate.

Conditioning (a trait drives an extent). An extent takes the same modifiers a rate does (SPEC §6) — inversion_extent = Extent(4).scaled_by(habitat, {"host": 3.0, "free": 1.0}) makes a host-restricted lineage invert longer runs of genes, which is a different statement from raising its inversion rate. An extent's modifier is read at the instant an event fires, so it changes how much a run takes and never how often one starts, and it adds no Gillespie breakpoint.

a per-family draw and a driven rate cannot be set in the same run: one weights lineages by a driver and the other weights the segment by what it covers.

Source code in zombi2/genomes/ordered.py
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
@without_cyclic_gc
def simulate_genomes_ordered(tree, *, duplication=0.0, transfer=0.0, loss=0.0, origination=0.0,
                             inversion=0.0, transposition=0.0, translocation=0.0,
                             chromosomes=1, topology="circular",
                             fission=0.0, fusion=0.0, chromosome_origination=0.0, chromosome_loss=0.0,
                             duplication_extent=None, loss_extent=None, transfer_extent=None,
                             inversion_extent=None, transposition_extent=None,
                             translocation_extent=None, inversion_probability=0.0,
                             transfer_to="uniform", replacement=False, self_transfer=False,
                             initial_families=100, families=None, joint=False,
                             max_family_size=10, seed=None,
                             progress=False, **retired) -> OrderedGenomesResult:
    """Evolve ordered genomes — genes with a position and an orientation, on chromosomes — along a
    species tree, by the D/T/L/O core plus segmental rearrangements and the chromosome events.

    **Every gene-level event acts on an *extent*** — a run of consecutive genes (the ZOMBI1 model):
    ``duplication`` copies the run in tandem, ``loss`` removes it, ``transfer`` sends it to a
    contemporaneous recipient as a block, ``inversion`` reverses it (flipping strands), ``transposition``
    relocates it elsewhere on the same chromosome, and ``translocation`` moves it to a different
    chromosome. The run's **extent** is drawn per event from ``<event>_extent`` (a distribution,
    default ``Geometric(mean=1)`` — usually a single gene; dial the mean up for larger blocks).
    ``origination`` is the exception: a family is born once, a single gene, no extent.
    ``transposition`` and ``translocation`` land the moved block inverted with probability
    ``inversion_probability``.

    **Where a run stops is set by the chromosome's ``topology``.** A run goes rightwards from the gene
    it starts at. On a ``"circular"`` chromosome there are no ends, so a run that reaches the last
    gene continues from the first, and only the whole chromosome bounds it; on a ``"linear"`` one the
    run stops at the last gene. So on a circular chromosome every gene is covered by segmental events
    at the same rate, and the nominal mean extent is the realised one.

    Scopes follow the cross-level grammar, which counts an event per the thing it acts on: the
    gene-level events — ``duplication``/``transfer``/``loss`` and the rearrangements
    ``inversion``/``transposition``/``translocation`` — are **per copy**, since each acts on a run of
    genes that starts at one of them; the chromosome rates ``fission``/``fusion``/``chromosome_loss``
    are **per chromosome**; and the two events that make something from nothing,
    ``origination``/``chromosome_origination``, are **per lineage**. The
    run starts with ``chromosomes`` chromosomes of the given ``topology``, across which the
    ``initial_families`` founding genes are dealt **round-robin**; ``families=[family("toxin")]`` additionally
    declares **named** families (remembered in ``result.family_names`` for ``result.has_family(node,
    "toxin")``), as in the family core; ``replacement`` / ``self_transfer`` behave as in the family
    core. So does ``transfer_to``, which **chooses who receives** — ``"uniform"``,
    ``"distance"`` / ``Distance(decay=)`` (closer relatives likelier), ``Clades({...}, Between({...}))``
    (weight by the donor's and recipient's named clade) or ``Recipients().weighted_by(driver, mapping)`` (weight by
    another level; see below). What moves is a block of genes rather than a single copy, and the block
    arrives whole, so the rule chooses the recipient lineage exactly as it does at the family
    resolution.

    The **chromosome events** change chromosome *number*: ``fission`` (split), ``fusion`` (merge,
    between two chromosomes of the **same topology** — the reticulation; a ring and a molecule with
    two ends cannot become one molecule, so a genome of one of each never fuses),
    ``chromosome_origination`` (a de-novo replicon), ``chromosome_loss`` (a whole
    chromosome and its genes die; never the genome's last). Chromosomes carry identity — re-minted at
    every event that reshapes them — so ``chromosome_events`` is the true reticulating chromosome
    genealogy, rooted at the initial and de-novo originations. Deterministic given ``seed``.

    **A family placed by hand.** ``origins=[("n5", 0.4)]`` originates a family on lineage ``n5`` at
    time ``0.4`` — the ordinary origination event, at a point you choose rather than one that is
    drawn, and here too the founding gene lands on a uniformly-chosen chromosome and position. It
    adds to whatever ``initial_families`` and ``origination`` already give you. See
    `resolve_origins`.

    **Conditioning (a trait drives a rate).** Any rate here may be *driven by another level* —
    ``inversion = PerCopy(0.3).scaled_by(habitat, {"host": 4.0, "free": 1.0})`` scales each lineage's
    inversion rate by the habitat on that branch, read from a trait grown first (the finished
    ``TraitsResult``, or the ``trait_events.tsv`` it wrote). A driven rate is then *per lineage*: it is
    summed over the living lineages, each read with its own gene count, chromosome count and driver
    value; the lineage an event lands on is drawn with those same weights, and the gene inside it
    uniformly, because the gene count is already in the weight. The Gillespie steps at **every**
    mid-branch switch of the driver rather than averaging over a branch (SPEC §2). For ``transfer``
    the driven lineage is the **donor**, so a driven ``transfer`` says how often a lineage *donates*.

    **Conditioning (a trait drives who receives).** ``transfer_to =
    Recipients().weighted_by(driver, mapping)`` is
    the other half, and a different model: the mapping's numbers are per-candidate **weights**, not
    rate multipliers, so they leave the total amount of transfer alone and only redistribute it
    (SPEC §5, a weight, not a rate). Candidate lineage ``k`` gets weight ``mapping(driver value on k now)``
    and receives with probability ``w_k / Σw``. Weight 0 means "cannot receive"; when every candidate
    weighs 0 the transfer does not happen at all, and the donor's chromosome is left untouched. A
    ``Between({...})`` mapping reads the **donor's** value too, so transfer can be steered between
    guilds; ``Clades({...}, Between({...}))`` is the same steering by named clade, read off the tree
    instead of a driver. Because a weight is not a rate, a driven ``transfer_to`` adds no Gillespie
    breakpoint and composes freely with a driven ``transfer`` rate.

    **Conditioning (a trait drives an extent).** An extent takes the same modifiers a rate does
    (SPEC §6) — ``inversion_extent = Extent(4).scaled_by(habitat, {"host": 3.0, "free": 1.0})`` makes a
    host-restricted lineage invert *longer runs of genes*, which is a different statement from raising
    its inversion rate. An extent's modifier is read at the instant an event fires, so it changes how
    much a run takes and never how often one starts, and it adds no Gillespie breakpoint.

    a per-family draw and a driven rate cannot be set in the same run: one weights lineages by a driver and
    the other weights the segment by what it covers.
    """
    tree = as_tree(tree, level="genomes")
    labels = _topologies(chromosomes, topology)
    n_initial_chrom = chromosomes
    # this slice implements each event's default scope and the four verbs IMPLEMENTED_MODIFIERS
    # declares: changing_at (skyline), scaled_by (a conditioned/joint driver, per lineage), set_by (a
    # driver that replaces the base) and a per-family draw —
    # the last with the weight on the SEGMENT rather than on its starting gene (SPEC §6, and
    # _pick_run_by_family). A clade-drift modifier is a later slice, so reject it
    # rather than silently mis-scale (see the family engine for the reasoning).
    _rates: dict[str, Rate] = {}
    for label, spec, want in (("duplication", duplication, PerCopy), ("transfer", transfer, PerCopy),
                              ("loss", loss, PerCopy), ("origination", origination, PerLineage),
                              ("inversion", inversion, PerCopy),
                              ("transposition", transposition, PerCopy),
                              ("translocation", translocation, PerCopy),
                              ("fission", fission, PerChromosome), ("fusion", fusion, PerChromosome),
                              ("chromosome_origination", chromosome_origination, PerLineage),
                              ("chromosome_loss", chromosome_loss, PerChromosome)):
        rate = as_rate(spec, default_scope=want)
        # An event that acts on **genes** takes either answer to *per what?*: per copy (the default —
        # each gene independently at risk, so a bigger genome turns over faster) or per lineage (a
        # fixed budget, the same however much the genome holds). The chromosome rates and origination
        # keep one scope each: origination creates families, so per copy it would be base × 0 in an
        # empty genome, and the chromosome rates are not implemented per lineage.
        legal = (want, PerLineage) if want is PerCopy else (want,)
        # `rate.scope` holds the scope CLASS, not an instance — a scope constructor returns the rate
        # itself — so this is an identity test against the legal set rather than an isinstance one.
        assert rate.scope is not None            # as_rate fills the level's default where none was written
        if rate.scope not in legal:
            raise ValueError(
                f"{label} has a {rate.scope.__name__} scope, but the ordered genome engine "
                f"takes {' or '.join(s.__name__ for s in legal)} for {label}."
            )
        for m in rate.modifiers:
            if m.reads == (DRAWN, "families") and label == "origination":
                raise ValueError(
                    "origination carries a per-family draw, but origination is the rate at which families are "
                    "CREATED — when it is read there is no family yet to have drawn a factor for. "
                    "Write varying_among('families', …) on duplication, transfer, loss, inversion, "
                    "transposition or translocation; writing one such object on several of them gives "
                    "a family-wide tempo, since one object is one draw.")
            if m.reads == (DRAWN, "families") and rate.scope is not PerCopy:
                raise ValueError(
                    f"{label} carries a per-family draw on a {rate.scope.__name__} scope. A per-family "
                    f"weight has to reach the genes an event covers, so it applies to the per-copy "
                    f"gene events only — not to the chromosome events, which act on whole replicons.")
            if isinstance(m, Driven):
                check_not_a_kernel(m.mapping, label=label)
            if not is_implemented(m, IMPLEMENTED_MODIFIERS, "genomes.ordered"):
                raise ValueError(
                    f"{label} carries {describe(m)}, which the ordered genome engine does not "
                    f"support. It takes changing_at (skyline), scaled_by (a conditioned or joint "
                    f"driver), set_by (a driver that replaces the base) and varying_among('families', "
                    f"…) (per-family heterogeneity, weighted on the segment an event covers). Clade "
                    f"drift is not implemented yet."
                )
        _rates[label] = rate
    # the eleven rates keep short names in the Gillespie loop below; the dict is what the driver
    # resolution and the per-lineage weights walk, so neither has to name all eleven again
    dup, tra, los, org = (_rates["duplication"], _rates["transfer"], _rates["loss"],
                          _rates["origination"])
    inv, trp, trl = _rates["inversion"], _rates["transposition"], _rates["translocation"]
    fis, fus = _rates["fission"], _rates["fusion"]
    cor, clo = _rates["chromosome_origination"], _rates["chromosome_loss"]
    for label, r in _rates.items():
        r.check_one_base(label)
    # Over the whole RUN, not per rate, and getting that wrong was a real bug: a per-family draw
    # anywhere makes the engine take its per-family path for **every** gene rate, summing each one
    # over the live genes — so a `PerLineage` rate elsewhere in the same run had its total counted
    # per copy while its acting lineage was still drawn uniformly among occupied genomes. The total
    # and the pick then said different things, which is the one failure this engine must not have.
    _GENE_EVENTS = ("duplication", "transfer", "loss", "inversion", "transposition", "translocation")
    per_lineage_here = [lbl for lbl in _GENE_EVENTS if _rates[lbl].scope is PerLineage]
    drawn_here = [lbl for lbl in _GENE_EVENTS
                  if any(m.reads == (DRAWN, "families") for m in _rates[lbl].modifiers)]
    if per_lineage_here and drawn_here:
        raise ValueError(
            f"{', '.join(per_lineage_here)} is PerLineage while {', '.join(drawn_here)} carries a "
            f"per-family draw, and the two cannot share a run. Under PerCopy a family's multiplier "
            f"scales each gene's rate, so it changes the lineage's total; under PerLineage the total "
            f"is fixed whatever the genome holds, so the multiplier could only choose which segment "
            f"is taken. Those are different models and the choice is not made yet — write PerCopy "
            f"throughout for the first, or drop the per-family draw for the second.")
    if any(m.reads == (DRAWN, "families") for r in _rates.values() for m in r.modifiers) and \
            any(isinstance(m, Driven) for r in _rates.values() for m in r.modifiers):
        raise ValueError(
            "a per-family draw and a driver on the same run is not wired at the ordered resolution: "
            "a driver weights the lineage, and here a per-family draw has to weight the SEGMENT an "
            "event covers rather than the gene it started from, so the two are not one "
            "multiplication. The family resolution runs the pair — there a family's multiplier is "
            "the copy's, and the weight is simply the product. Use it, or use one of the two here.")
    # per-event extent distributions (segment size in genes); a bare number is the mean, None a single gene
    def _ext_spec(spec, label):
        """One event's extent (SPEC §6): ``base × modifiers``, no scope, in **genes** here. An extent
        takes the modifiers a rate takes at this resolution minus a per-family draw (see
        `IMPLEMENTED_EXTENT_MODIFIERS`), and they scale the size drawn — ``changing_at`` in time,
        ``scaled_by`` on the lineage the event lands on."""
        e = as_extent(spec)
        rate_slot = label.removesuffix("_extent")
        for m in e.modifiers:
            if isinstance(m, Driven):
                check_not_a_kernel(m.mapping, label=label)
            if m.reads == (DRAWN, "families"):
                raise ValueError(
                    f"{label} carries a per-family draw, which an extent cannot mean: the size is drawn before "
                    f"the run's genes are known, and a run covers several families, so there is no "
                    f"one family to draw a factor for. Put it on {rate_slot}, where it weights "
                    f"the segment by what it covers.")
            if not is_implemented(m, IMPLEMENTED_EXTENT_MODIFIERS, "genomes.ordered"):
                raise ValueError(
                    f"{label} carries {describe(m)}, which the ordered genome engine does not "
                    f"support on an extent — it takes "
                    f"{', '.join(cell_name(w) for w in IMPLEMENTED_EXTENT_MODIFIERS)}.")
        return e

    dup_ext, los_ext, tra_ext = (_ext_spec(duplication_extent, "duplication_extent"),
                                 _ext_spec(loss_extent, "loss_extent"),
                                 _ext_spec(transfer_extent, "transfer_extent"))
    inv_ext, trp_ext, trl_ext = (_ext_spec(inversion_extent, "inversion_extent"),
                                 _ext_spec(transposition_extent, "transposition_extent"),
                                 _ext_spec(translocation_extent, "translocation_extent"))
    _extents: dict[str, Extent] = {
        "duplication_extent": dup_ext, "loss_extent": los_ext, "transfer_extent": tra_ext,
        "inversion_extent": inv_ext, "transposition_extent": trp_ext,
        "translocation_extent": trl_ext}
    if not 0.0 <= inversion_probability <= 1.0:
        raise ValueError(f"inversion_probability must be in [0, 1], got {inversion_probability!r}")
    # the choice (SPEC §5), validated in the one place all three resolutions share: the mapping's
    # numbers are weights over the candidate recipients, never a rate multiplier
    transfer_to = resolve_transfer_to(transfer_to)
    if isinstance(initial_families, bool) or not isinstance(initial_families, int) or initial_families < 0:
        raise ValueError(f"initial_families must be a non-negative integer, got {initial_families!r}")
    check_no_retired_keywords(retired, where="simulate_genomes_ordered")
    # the family resolution's own resolver, so the two engines cannot disagree about what a
    # declaration means
    declared, module_map, planted_named = resolve_families(families, tree)
    family_names = [f.name for f in declared]
    if any(f.written() for f in declared):
        raise ValueError(
            "a family writing its own rate is implemented at the family resolution and not yet here. "
            "This engine carries a segment's extent as well as its rate, and what a per-family extent "
            "means is not decided. Declare the family without rates, or run at "
            "resolution='family'.")
    if joint:
        raise ValueError(
            "joint=True — a rate reading the genome's own live content — is implemented at the "
            "family resolution and not yet here. Run at resolution='family' for it.")

    # The growth guard, as at the family resolution: duplication compounds, so a run whose rate sits
    # above its loss rate — or a family that drew a high a per-family draw factor — multiplies without bound
    # unless something stops it. A segment may carry several families, and several copies of one, so
    # the run is refused when it would take *any* of them past the quota (see _run_over_cap).
    cap = resolve_max_family_size(max_family_size)

    # Conditioning: a rate written with scaled_by reads a driver **per lineage**, so its rate stops being
    # one number for the whole live set and becomes one per lineage. Same machinery as the other two
    # resolutions — each driver resolves once into a DriverTrajectory keyed by the shared species node
    # id, from a file or an in-memory trait result. With no driven rate and no driven extent this is
    # empty and the loop stays exactly the pooled one, so an undriven run is untouched.
    driven = {label: [m for m in r.modifiers if isinstance(m, Driven)]
              for label, r in _rates.items()}
    ext_driven = {label: [m for m in e.modifiers if isinstance(m, Driven)]
                  for label, e in _extents.items()}
    by_key: dict = {}                   # driver key → its Driven (deduped: one driver resolves once)
    for mods in (*driven.values(), *ext_driven.values()):
        for m in mods:
            by_key.setdefault(m.key, m)
    resolved: dict = {}
    if by_key:
        resolved = {key: resolve_driver(m.driver, tree, step=m.step, level="genomes.ordered")
                    for key, m in by_key.items()}
        # a mapping whose states never occur leaves every lineage on the default factor, so the run
        # would secretly be the undriven model — refuse it here, naming the driver
        for mods in (*driven.values(), *ext_driven.values()):
            for m in mods:
                src = m.driver if isinstance(m.driver, str) else f"<{type(m.driver).__name__}>"
                check_mapping_fires(m.mapping, resolved[m.key].states(), driver_label=src)
    # Only a driver on a **rate** makes the loop per-lineage and adds a Gillespie breakpoint. A driver
    # on an **extent** is read at the instant an event fires — it changes how much that event takes,
    # never how often one happens — so it deliberately stays out of `trajs`: no per-lineage rate
    # weights, no extra horizon steps. (SPEC §6.)
    _rate_keys = {m.key for mods in driven.values() for m in mods}
    trajs = {key: traj for key, traj in resolved.items() if key in _rate_keys}
    any_driven = bool(trajs)
    any_ext_driven = any(ext_driven.values())
    # The transfer_to slot is prepared **after** `trajs` is fixed, for the same reason: a driven
    # transfer_to is a weight, not a rate, so its trajectory must not join `trajs` and start adding
    # horizon breakpoints. `resolved` doubles as the driver cache, so a trait that drives both a rate
    # and who receives is loaded once and read from one trajectory.
    group_of, to_traj = prepare_transfer_to(tree, transfer_to, resolved, level="genomes.ordered")

    rng, seed = stream("genomes", seed)     # own stream, and a drawn seed if none was given
    copy_counter = 0
    family_counter = 0
    chrom_counter = 0

    def new_gene(family: int, strand: int) -> Gene:
        nonlocal copy_counter
        g = Gene(copy_counter, family, strand)
        copy_counter += 1
        return g

    # Per-family multipliers, drawn once when a family is created and fixed for its whole life,
    # exactly as at the family resolution: one `Random` object read by two rates is one draw for
    # both, two objects are two draws. What differs here is where the weight lands — on the run an
    # event covers, not on the gene it started from (SPEC §6). Empty unless some rate carries one.
    fam_by = {"duplication": tuple(m for m, _ in dup.carried_modifiers(unit="families")),
              "transfer": tuple(m for m, _ in tra.carried_modifiers(unit="families")),
              "loss": tuple(m for m, _ in los.carried_modifiers(unit="families")),
              "inversion": tuple(m for m, _ in inv.carried_modifiers(unit="families")),
              "transposition": tuple(m for m, _ in trp.carried_modifiers(unit="families")),
              "translocation": tuple(m for m, _ in trl.carried_modifiers(unit="families"))}
    any_family = any(fam_by.values())
    fam_mult: dict[str, dict[int, float]] = {key: {} for key in fam_by}

    # Which gene-level rates are a fixed per-lineage budget rather than a per-gene risk. Read once:
    # it decides both how the total is counted and how the acting lineage is picked, and those two
    # must never disagree.
    per_lineage = {label: _rates[label].scope is PerLineage
                   for label in ("duplication", "transfer", "loss",
                                 "inversion", "transposition", "translocation")}
    any_per_lineage = any(per_lineage.values())

    def new_family() -> int:
        nonlocal family_counter
        f = family_counter
        family_counter += 1
        if any_family:
            # one draw per distinct modifier object for this family, shared across its rates (see
            # `values_at_birth`): one object written on two rates is one number.
            shared: dict[int, float] = {}
            for key, mods in fam_by.items():
                fam_mult[key][f] = math.prod(values_at_birth(mods, rng, shared))
        return f

    def new_chromosome() -> int:
        nonlocal chrom_counter
        cid = chrom_counter
        chrom_counter += 1
        return cid

    depth = mean_root_to_tip(tree)  # timescale for Distance weighting (unused by "uniform")
    schedule = sorted((tree.nodes[i].end_time, i) for i in tree.nodes)  # (end_time, node_id)

    root = tree.nodes[tree.root]
    t = root.birth_time
    alive: list[int] = []
    gen: list[list[Chromosome]] = []
    pos: dict[int, int] = {}
    genomes: dict[int, tuple[Chromosome, ...]] = {}
    events: list[GeneEdge] = []
    event_positions: list[EventPosition] = []
    rearrangements: list[Inversion | Transposition | Translocation] = []
    chromosome_events: list[ChromosomeEvent] = []

    initial_chroms = []
    for label in labels:  # lay down the initial karyotype; each initial chromosome is a network root
        cid = new_chromosome()
        initial_chroms.append(Chromosome(cid, label, []))
        # `initial`, not `origination`: a replicon the run *starts* with is not something it did, so
        # counting `origination` in the log gives the de-novo replicons alone
        chromosome_events.append(ChromosomeEvent(t, "initial", root.id, (), (cid,)))
    # the origin's initial genome is logged like any other origination — each founding gene appended in turn —
    # so the position table is total over gene-content events and a replay of the root branch can
    # start from an empty karyotype (every other branch starts from its parent's gene_order rows)
    for i in range(initial_families):  # deal the founding genes round-robin across the chromosomes
        fam = new_family()
        chrom = initial_chroms[i % n_initial_chrom]
        _live(chrom).append(new_gene(fam, +1))
        events.append(GeneEdge(t, "origination", root.id, fam, chrom.genes[-1].id))
        event_positions.append(EventPosition(t, "origination", root.id, chrom.id,
                                             len(chrom.genes) - 1, 1, family=fam))
    named: dict[str, int] = {}  # a minted id per declared name, dealt round-robin after the anonymous ones
    named_plants: list[tuple[float, int, int]] = []
    for j, name in enumerate(family_names):
        fam = new_family()
        named[name] = fam
        if j in planted_named:
            # given an `origin`, so it arrives there rather than at the tree's origin — the same
            # event, at a point chosen instead of drawn
            t_p, lineage = planted_named[j]
            named_plants.append((t_p, lineage, fam))
            continue
        chrom = initial_chroms[(initial_families + j) % n_initial_chrom]
        _live(chrom).append(new_gene(fam, +1))
        events.append(GeneEdge(t, "origination", root.id, fam, chrom.genes[-1].id))
        event_positions.append(EventPosition(t, "origination", root.id, chrom.id,
                                             len(chrom.genes) - 1, 1, family=fam))
    # the run's starting genome: a deep snapshot, so the live genome's events never reach it
    # the ids of the families `origins=` places: minted here, straight after the initial and named
    # ones and in the order they were written, so the same origins name the same families at either
    # resolution. Each is planted at its own time, in the loop below.
    plants = sorted(named_plants)
    plant_i = 0
    initial_genome = tuple(Chromosome(c.id, c.topology, list(c.genes)) for c in initial_chroms)
    enter(alive, gen, pos, root.id, initial_chroms)
    # a family given an `origin` is not in the root genome — it arrives later, in the loop
    total_copies = initial_families + len(family_names) - len(named_plants)
    total_chromosomes = n_initial_chrom

    # eleven bare numbers on their default scopes — no modifier on any rate, none on any extent,
    # no per-lineage budget — is the common run, and it needs none of the loop's context machinery:
    # each total is scope(base) exactly, resolved here once. A rate whose base is None carries a
    # set_by, which is a modifier, so `plain` is False and its 0.0 is never read.
    plain = (not any(r.modifiers for r in _rates.values()) and not any_per_lineage
             and not any(e.has_modifiers for e in (dup_ext, los_ext, tra_ext, inv_ext, trp_ext,
                                                   trl_ext)))
    dup_base, los_base, tra_base = dup.base or 0.0, los.base or 0.0, tra.base or 0.0
    org_base, inv_base, trp_base = org.base or 0.0, inv.base or 0.0, trp.base or 0.0
    trl_base, fis_base, fus_base = trl.base or 0.0, fis.base or 0.0, fus.base or 0.0
    cor_base, clo_base = cor.base or 0.0, clo.base or 0.0
    no_weights: dict = {}    # what `w` is when nothing is driven: read by .get, never written
    if plain:
        def _ext_ctx(k):
            # nothing in this run carries a modifier (`plain` pinned every extent to
            # has_modifiers=False), so an extent's sample() reads none of this: an empty context
            # is the same multiplication by 1.0, without rebuilding the loop's context per event.
            return {}

    bar = progress_bar(len(schedule), "genomes", unit="branch", enabled=progress)
    si = 0
    while si < len(schedule):
        bar.to(si)
        n = total_copies
        k_alive = len(alive)
        c = total_chromosomes
        can_xfer = n > 0 and (k_alive >= 2 or self_transfer)
        next_species = schedule[si][0]
        # a family placed by `origins=` originates at a fixed instant, so it joins the horizon like
        # any other breakpoint: the waiting time can never step over it
        next_plant = plants[plant_i][0] if plant_i < len(plants) else math.inf
        if plain:
            # no modifier on any rate or extent: each total is scope(base) exactly — a gene rate
            # times the live genes, a chromosome rate times the standing chromosomes, the two
            # originations per living lineage — none of them ever changes on its own (next_change
            # is inf), and nothing below reads a context or a weight.
            w = no_weights
            fw = None
            r_dup = dup_base * n
            r_los = los_base * n
            r_tra = tra_base * n if can_xfer else 0.0
            r_inv = inv_base * n
            r_trp = trp_base * n
            r_trl = trl_base * n
            r_org = org_base * k_alive
            r_fis = fis_base * c
            r_fus = fus_base * c
            r_cor = cor_base * k_alive
            r_clo = clo_base * c
            horizon = min(next_species, next_plant)
        else:
            ctx = {"copies": n, "lineages": k_alive, "chromosomes": total_chromosomes, "time": t}
            # A gene-level event counted PER LINEAGE is counted per lineage that HOLDS a gene: an
            # empty genome offers nothing to act on, so it must not take a share of the total and
            # then be picked with no victim inside it. Built only when some rate needs it, so the
            # per-copy path does exactly the work it did before.
            if any_per_lineage:
                gene_hosts = [k for k in range(k_alive) if _genome_size(gen[k])]
                gene_ctx = {**ctx, "lineages": len(gene_hosts)}
            else:
                gene_hosts, gene_ctx = None, ctx
            # A driven rate differs from lineage to lineage, so it is summed **over the living
            # lineages**, each read with its own driver value, its own gene count and its own
            # chromosome count — and the weights are kept, because the affected lineage must then
            # be drawn with them too. The gene count sits inside the weight, which is what makes a
            # driven per-copy rate a two-stage pick (a lineage, then a gene in it) rather than the
            # one-stage lineage draw a per-lineage rate takes. A per-family draw and a Driven
            # cannot both be set, so `w` and `fw` never coexist.
            w = {}
            if any_driven:
                drivers = [{key: trajs[key].value(alive[k], t) for key in trajs} for k in range(k_alive)]
                for label, rate in _rates.items():
                    if driven[label]:
                        w[label] = [rate.effective(copies=_genome_size(gen[k]), lineages=1,
                                                   chromosomes=len(gen[k]), time=t, drivers=drivers[k])
                                    for k in range(k_alive)]

            def _r(label, pooled, live=True):
                """The total for one event class: summed per-lineage when driven, pooled when not."""
                if not live:
                    return 0.0
                return sum(w[label]) if label in w else pooled

            # A per-copy rate pools over genes, so with per-family weights the total is the unit rate
            # times those weights summed over the live genes — and the run must then be drawn with the
            # same weights, or the rate would say one thing and the picking another. Summed per lineage,
            # so the lineage pick can reuse them. On a circular chromosome ``Σ_s mean_w(s, m)`` is exactly
            # this sum for every run size, which is why no per-size term appears here (SPEC §6).
            fw = None
            if any_family:
                fw = {key: [sum(mult[g.family] for chrom in gen[k] for g in chrom.genes)
                            for k in range(k_alive)]
                      for key, mult in fam_mult.items()}
                one = {"copies": 1, "lineages": 1, "chromosomes": 1, "time": t}
                r_dup = dup.effective(**one) * sum(fw["duplication"]) if n else 0.0
                r_los = los.effective(**one) * sum(fw["loss"]) if n else 0.0
                r_tra = tra.effective(**one) * sum(fw["transfer"]) if can_xfer else 0.0
                r_inv = inv.effective(**one) * sum(fw["inversion"]) if n else 0.0
                r_trp = trp.effective(**one) * sum(fw["transposition"]) if n else 0.0
                r_trl = trl.effective(**one) * sum(fw["translocation"]) if n else 0.0
            else:
                # each gene-level rate is read in the context its own scope asks for: `gene_ctx` counts
                # only the occupied genomes, which is what a per-lineage budget is counted over
                def _gc(label):
                    return gene_ctx if per_lineage[label] else ctx

                r_dup = _r("duplication", dup.effective(**_gc("duplication")) if n else 0.0, live=bool(n))
                r_los = _r("loss", los.effective(**_gc("loss")) if n else 0.0, live=bool(n))
                r_tra = _r("transfer", tra.effective(**_gc("transfer")) if can_xfer else 0.0,
                           live=can_xfer)
                r_inv = _r("inversion", inv.effective(**_gc("inversion")) if n else 0.0, live=bool(n))
                r_trp = _r("transposition", trp.effective(**_gc("transposition")) if n else 0.0,
                           live=bool(n))
                r_trl = _r("translocation", trl.effective(**_gc("translocation")) if n else 0.0,
                           live=bool(n))
            r_org = _r("origination", org.effective(**ctx))                 # per lineage
            r_fis = _r("fission", fis.effective(**ctx) if c else 0.0, live=bool(c))  # per chromosome
            r_fus = _r("fusion", fus.effective(**ctx) if c else 0.0, live=bool(c))
            r_cor = _r("chromosome_origination", cor.effective(**ctx))      # per lineage (de-novo replicon)
            r_clo = _r("chromosome_loss", clo.effective(**ctx) if c else 0.0, live=bool(c))
            horizon = min(next_species, next_plant,
                          dup.next_change(t), los.next_change(t), org.next_change(t),
                          tra.next_change(t), inv.next_change(t), trp.next_change(t), trl.next_change(t),
                          fis.next_change(t), fus.next_change(t), cor.next_change(t), clo.next_change(t))
            if any_driven:  # a driven rate also changes when its driver switches mid-branch — step there
                horizon = min(horizon, min((trajs[key].next_change(alive[k], t) for key in trajs
                                            for k in range(k_alive)), default=math.inf))
            def _ext_ctx(k):
                """The context an extent is sampled in, on the lineage the event landed on.

                It cannot be built before the lineage is drawn, because a driven extent is read on
                the **acting** lineage at the instant the event fires — which is also why an extent
                adds no Gillespie breakpoint and never enters the horizon above (SPEC §6). With no
                driven extent this is the same context the rates were read in.

                The rest of `ctx` — the gene, lineage and chromosome counts — goes with it, because
                `Modifier.implemented_for` promises this engine supplies them and a modifier of
                your own is admitted onto an extent by the same gate that admits it onto a rate.
                Handing an extent a thinner context meant one gate certifying two different
                contracts: a modifier written the documented way read zeros, and one with a
                required keyword died mid-run."""
                # `ctx` was snapshotted at the top of the loop, before `t` advanced to the firing
                # instant, so `time` has to be taken fresh: an extent's own breakpoints are kept out
                # of the horizon, so a schedule's breakpoint routinely falls inside a stretch, and
                # reading the stale `t` would size the event on the wrong side of it.
                if not any_ext_driven:
                    return {**ctx, "time": t}
                return {**ctx, "time": t,
                        "drivers": {key: resolved[key].value(alive[k], t) for key in resolved}}
        total = (r_dup + r_los + r_org + r_tra + r_inv + r_trp + r_trl
                 + r_fis + r_fus + r_cor + r_clo)

        if total > 0.0:
            t_ev = t + float(rng.exponential(1.0 / total))
            if t_ev < horizon:  # a genome event fires before the alive set or a rate changes
                t = t_ev
                r = float(rng.random()) * total
                b_los = r_dup + r_los                    # cumulative bounds, in the firing order below
                b_org = b_los + r_org
                b_tra = b_org + r_tra
                b_inv = b_tra + r_inv
                b_trp = b_inv + r_trp
                b_trl = b_trp + r_trl
                b_fis = b_trl + r_fis
                b_fus = b_fis + r_fus
                b_cor = b_fus + r_cor                    # ... and the remainder (to total) is clo
                if r < r_dup:                            # every gene-level event acts on an extent
                    picked = _pick_event_run(rng, gen, n, fw, fam_mult, "duplication", dup_ext,
                                             _ext_ctx, w.get("duplication"),
                                             gene_hosts if per_lineage["duplication"] else None)
                    if picked is not None:
                        k, ci, j, m = picked
                        if not _run_over_cap(gen[k], gen[k][ci], j, m, cap):
                            total_copies += _duplicate(gen[k][ci], j, m, tree.nodes[alive[k]], t,
                                                       events, event_positions, new_gene)
                elif r < b_los:
                    picked = _pick_event_run(rng, gen, n, fw, fam_mult, "loss", los_ext,
                                             _ext_ctx, w.get("loss"),
                                             gene_hosts if per_lineage["loss"] else None)
                    if picked is not None:
                        k, ci, j, m = picked
                        total_copies -= _lose_at(gen[k][ci], j, m, tree.nodes[alive[k]], t, events,
                                                 event_positions)
                elif r < b_org:
                    # origination is per lineage: a uniform lineage, or one drawn by its own rate
                    # when that rate is driven (the same weights the total was summed with)
                    k = (weighted_index(rng, w["origination"], r_org) if "origination" in w
                         else int(rng.integers(k_alive)))
                    _originate(gen[k], tree.nodes[alive[k]], t, events, event_positions, new_gene,
                               new_family, rng)
                    total_copies += 1
                elif r < b_tra:
                    picked = _pick_event_run(rng, gen, n, fw, fam_mult, "transfer", tra_ext,
                                             _ext_ctx, w.get("transfer"),
                                             gene_hosts if per_lineage["transfer"] else None)
                    if picked is not None:                # driven: the weighted lineage is the DONOR
                        kd, cdi, jd, m = picked
                        total_copies += _do_transfer(rng, tree, alive, gen, kd, cdi, jd, m, t, events,
                                                     event_positions, new_gene, transfer_to,
                                                     replacement, self_transfer, depth, cap,
                                                     to_traj, group_of)
                elif r < b_inv:
                    picked = _pick_event_run(rng, gen, n, fw, fam_mult, "inversion", inv_ext,
                                             _ext_ctx, w.get("inversion"),
                                             gene_hosts if per_lineage["inversion"] else None)
                    if picked is not None:                # the run starts at a gene, so: per copy
                        k, ci, i0, m = picked
                        _invert(gen[k][ci], i0, m, tree.nodes[alive[k]], t, rearrangements)
                elif r < b_trp:
                    picked = _pick_event_run(rng, gen, n, fw, fam_mult, "transposition", trp_ext,
                                             _ext_ctx, w.get("transposition"),
                                             gene_hosts if per_lineage["transposition"] else None)
                    if picked is not None:
                        k, ci, i0, m = picked
                        _transpose(gen[k][ci], i0, m, tree.nodes[alive[k]], t, rearrangements, rng,
                                   inversion_probability)
                elif r < b_trl:
                    picked = _pick_event_run(rng, gen, n, fw, fam_mult, "translocation", trl_ext,
                                             _ext_ctx, w.get("translocation"),
                                             gene_hosts if per_lineage["translocation"] else None)
                    if picked is not None:
                        k, ci, j, m = picked
                        _translocate(gen[k], ci, j, m, tree.nodes[alive[k]], t, rearrangements, rng,
                                     inversion_probability)
                elif r < b_fis:
                    picked = _pick_chromosome(rng, gen, c, w.get("fission"))
                    if picked is not None:
                        k, ci = picked
                        dc, dg = _fission(gen[k], ci, tree.nodes[alive[k]], t, chromosome_events,
                                          new_chromosome, rng)
                        total_chromosomes += dc
                        total_copies += dg
                elif r < b_fus:
                    picked = _pick_chromosome(rng, gen, c, w.get("fusion"))
                    if picked is not None:
                        k, ci = picked
                        dc, dg = _fusion(gen[k], ci, tree.nodes[alive[k]], t, chromosome_events,
                                         new_chromosome, rng)
                        total_chromosomes += dc
                        total_copies += dg
                elif r < b_cor:
                    # chromosome origination is per lineage, uniform or driven, exactly as origination
                    k = (weighted_index(rng, w["chromosome_origination"], r_cor)
                         if "chromosome_origination" in w else int(rng.integers(k_alive)))
                    dc, dg = _chromosome_originate(gen[k], tree.nodes[alive[k]], t, chromosome_events,
                                                   new_chromosome)
                    total_chromosomes += dc
                    total_copies += dg
                else:
                    picked = _pick_chromosome(rng, gen, c, w.get("chromosome_loss"))
                    if picked is not None:
                        k, ci = picked
                        dc, dg = _chromosome_lose(gen[k], ci, tree.nodes[alive[k]], t, events,
                                                  event_positions, chromosome_events)
                        total_chromosomes += dc
                        total_copies += dg
                continue

        if horizon == next_species:  # advance to the tree's next event(s); process the whole tie-batch
            t = next_species
            while si < len(schedule) and schedule[si][0] == t:
                i = schedule[si][1]
                g = gen[pos[i]]
                genomes[i] = tuple(Chromosome(c.id, c.topology, tuple(c.genes)) for c in g)  # freeze
                total_copies -= sum(len(c.genes) for c in g)
                total_chromosomes -= len(g)
                retire(alive, gen, pos, pos[i])
                node = tree.nodes[i]
                if node.children:  # a speciation: re-mint every chromosome and gene id
                    child_genomes: dict[int, list[Chromosome]] = {c: [] for c in node.children}
                    for pchrom in g:
                        dcids = []
                        per_daughter: list[list[GeneEdge]] = []
                        for c in node.children:
                            dcid = new_chromosome()
                            dcids.append(dcid)
                            dgenes, edges = [], []
                            for old in pchrom.genes:  # ZOMBI1: the gene ends and continues, fresh id
                                ng = new_gene(old.family, old.strand)
                                dgenes.append(ng)
                                edges.append(GeneEdge(t, "speciation", c, old.family, ng.id,
                                                   parent=old.id))
                            per_daughter.append(edges)
                            child_genomes[c].append(Chromosome(dcid, pchrom.topology, dgenes))
                        # the ids are minted daughter by daughter (which is what fixes them), but a
                        # gene's two edges are recorded together: one gene ending is one event, and
                        # the log writes it as one row naming both daughters
                        for gene_edges in zip(*per_daughter):
                            events.extend(gene_edges)
                        chromosome_events.append(
                            ChromosomeEvent(t, "speciation", node.id, (pchrom.id,), tuple(dcids)))
                    for c in node.children:
                        cg = child_genomes[c]
                        enter(alive, gen, pos, c, cg)
                        total_copies += sum(len(ch.genes) for ch in cg)
                        total_chromosomes += len(cg)
                si += 1
        elif plant_i < len(plants) and horizon == next_plant:
            # a placed family arrives — the ordinary origination event, at a time and on a lineage
            # that were chosen rather than drawn. The lineage is live by construction (its time was
            # checked against that branch's own life), and a tie with the tree's schedule falls to
            # the branch above, so the daughters have entered by the time this runs.
            t = horizon
            while plant_i < len(plants) and plants[plant_i][0] == t:
                _, lineage, fam = plants[plant_i]
                _originate(gen[pos[lineage]], tree.nodes[lineage], t, events, event_positions,
                           new_gene, new_family, rng, family=fam)
                total_copies += 1
                plant_i += 1
        else:
            t = horizon  # a skyline breakpoint: advance and re-evaluate the (now changed) rate

    bar.close()
    return OrderedGenomesResult(tree, genomes, events, rearrangements, chromosome_events, seed,
                                named, module_map, event_positions, initial_genome)

zombi2.genomes.simulate_genomes_nucleotide

simulate_genomes_nucleotide(tree, *, inversion=0.0, inversion_extent=50.0, translocation=0.0, translocation_extent=50.0, transposition=0.0, transposition_extent=50.0, inversion_probability=0.0, loss=0.0, loss_extent=50.0, deletion=0.0, deletion_extent=5.0, insertion=0.0, insertion_extent=5.0, duplication=0.0, duplication_extent=50.0, transfer=0.0, transfer_extent=50.0, transfer_to='uniform', self_transfer=False, origination=0.0, origination_extent=50.0, fission=0.0, fusion=0.0, chromosome_origination=0.0, chromosome_loss=0.0, chromosomes=1, root_length=1000, topology='circular', genes=0, gene_length=100, gff=None, fasta=None, modules=None, trim_overlaps=False, seed=None, progress=False) -> NucleotideGenomesResult

Evolve a nucleotide genome along a species tree by inversion, translocation, transposition, loss, duplication, transfer, origination, and the chromosome events. The run starts from a karyotypechromosomes replicons, each its own source: an int N gives N equal replicons of root_length/topology, or pass a list of (length, topology) for heterogeneous sizes and shapes. Each lineage inherits a copy of its parent's karyotype at speciation, with every chromosome re-minted (the chromosome network), and evolves:

  • inversion (per lineage) reverses a geometric-length (mean inversion_extent) arc of a length-weighted chromosome.
  • translocation (per lineage) moves a geometric-length (mean translocation_extent) arc to a different chromosome; transposition (per lineage, mean transposition_extent) moves one within its chromosome. Both land inverted with probability inversion_probability, keep source coordinates, and are rearrangements, not edges.
  • loss (per lineage) deletes a geometric-length (mean loss_extent) arc — an ancestry-changing event (a death), recorded in events. Never empties a chromosome.
  • deletion (per lineage, mean deletion_extent) removes an arc as an indel: the same material goes, but no copy lineage ends, so it is recorded in deletions and not in the genealogy, and its breakpoints do not cut the root partition. The pair divides like this — loss changes what a lineage has (a copy dies, a gene can go whole), deletion changes how much of a surviving copy it carries. In practice that is a difference of scale: hundreds to thousands of base pairs against ones to tens.
  • insertion (per lineage, mean insertion_extent) lays down a run of novel spacer at a legal position — the twin of deletion, and origination without the gene. Novel DNA descends from nothing, so it arrives on a fresh source under a fresh copy lineage and is recorded in events as a root; the one breakpoint it makes to open a gap for itself is indel-made, so the block it landed inside is not cut in two. The pair divides the same way: origination brings a new gene family into the run, insertion brings sequence.
  • duplication (per lineage) copies a geometric-length (mean duplication_extent) arc in tandem — an ancestry-changing birth, recorded in events.
  • transfer (per lineage) copies a geometric-length (mean transfer_extent) arc into a contemporaneous recipient (transfer_to: "uniform", "distance" / a Distance, Clades({...}, Between({...})) or Recipients().weighted_by(driver, mapping) — see below; self_transfer allows the donor itself) — a horizontal birth, additive (the donor keeps its copy). This is what needs the global timeline.
  • origination (per lineage) lays down a new gene on a fresh source (geometric length, mean origination_extent) — a birth of a wholly new family, indivisible from birth.
  • fission (per chromosome) splits a chromosome in two (a bifurcation); fusion (per chromosome) merges two chromosomes of the same topology (the reticulation). chromosome_origination (per lineage) adds a de-novo circular replicon (a plasmid, a network root) carrying one new gene; chromosome_loss (per chromosome) kills a whole chromosome (its material dies as a loss; never the last one) — a network leaf. All record a chromosome-network edge.

A chromosome never exists without a gene. A replicon is born with one, and any event that would strip a chromosome of its last gene — a loss, a translocation carrying it away, a fission splitting off a geneless half — simply does not happen. (Vacuous when no genes are declared.)

The engine runs a global-timeline Gillespie: all lineages alive at once evolve along one clock (every segmental event is per lineage — the rate says how often a lineage does it, the extent how much it touches, so a bigger genome does not get proportionally more events; the chromosome rates are per chromosome), so a transfer couples two contemporaries. With loss, the strong invariant weakens: every node carries a subset of the initial sequence (each ancestral position at most once, monotonically down every path); origination further adds fresh sources beyond the root. Deterministic given seed. (Transfer is always additive.)

Driving out of the genome. A gene declared by a GFF is named, and a named gene answers is it in this lineage, right now?result.presence("dnaA"), the driver the other two resolutions already hand out. modules={"flagellum": ["flgA", "flgB"]} groups declared genes, so result.completion("flagellum") gives the fraction of the group a lineage carries; a module changes nothing about how the genome evolves.

Conditioning (a trait drives who receives). transfer_to = Recipients().weighted_by(driver, mapping) weights the candidate recipients by another level, and the numbers are weights, not rate multipliers: they are normalised across the candidates, so they leave the total amount of transfer alone and only redistribute it (SPEC §5, a weight, not a rate). Weight 0 means "cannot receive"; when every candidate weighs 0 the transfer does not happen. A Between({...}) mapping reads the donor's value too, so transfer can be steered between guilds rather than merely into one, and Clades({...}, Between({...})) is the same steering by named clade, read off the tree instead of a driver. Since a transfer here is additive, steering changes only which lineage the arc lands on — never how much DNA moves, and never anything about the donor.

Source code in zombi2/genomes/nucleotide.py
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
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
def simulate_genomes_nucleotide(tree, *, inversion=0.0, inversion_extent=50.0, translocation=0.0,
                                translocation_extent=50.0, transposition=0.0, transposition_extent=50.0,
                                inversion_probability=0.0, loss=0.0, loss_extent=50.0,
                                deletion=0.0, deletion_extent=5.0,
                                insertion=0.0, insertion_extent=5.0, duplication=0.0,
                                duplication_extent=50.0, transfer=0.0, transfer_extent=50.0,
                                transfer_to="uniform", self_transfer=False, origination=0.0,
                                origination_extent=50.0, fission=0.0, fusion=0.0,
                                chromosome_origination=0.0, chromosome_loss=0.0, chromosomes=1,
                                root_length=1000, topology="circular", genes=0, gene_length=100,
                                gff=None, fasta=None, modules=None, trim_overlaps=False, seed=None,
                                progress=False) -> NucleotideGenomesResult:
    """Evolve a nucleotide genome along a species tree by inversion, translocation, transposition,
    **loss**, **duplication**, **transfer**, **origination**, and the chromosome events.
    The run starts from a **karyotype** — ``chromosomes`` replicons, each its own source: an int
    ``N`` gives ``N`` equal replicons of ``root_length``/``topology``, or pass a list of ``(length,
    topology)`` for heterogeneous **sizes and shapes**. Each lineage inherits a copy of its parent's
    karyotype at speciation, with **every chromosome re-minted** (the chromosome network), and evolves:

    - ``inversion`` (**per lineage**) reverses a geometric-length (mean ``inversion_extent``) arc
      of a length-weighted chromosome.
    - ``translocation`` (**per lineage**) moves a geometric-length (mean ``translocation_extent``)
      arc to a **different** chromosome; ``transposition`` (**per lineage**, mean
      ``transposition_extent``) moves one **within** its chromosome. Both land inverted with
      probability ``inversion_probability``, keep source coordinates, and are rearrangements, not edges.
    - ``loss`` (**per lineage**) deletes a geometric-length (mean ``loss_extent``) arc — an
      ancestry-**changing** event (a death), recorded in ``events``. Never empties a chromosome.
    - ``deletion`` (**per lineage**, mean ``deletion_extent``) removes an arc as an **indel**: the
      same material goes, but no copy lineage ends, so it is recorded in ``deletions`` and not in the
      genealogy, and its breakpoints do not cut the root partition. The pair divides like this —
      ``loss`` changes what a lineage *has* (a copy dies, a gene can go whole), ``deletion`` changes
      how much of a surviving copy it *carries*. In practice that is a difference of scale: hundreds
      to thousands of base pairs against ones to tens.
    - ``insertion`` (**per lineage**, mean ``insertion_extent``) lays down a run of **novel spacer**
      at a legal position — the twin of ``deletion``, and ``origination`` without the gene. Novel DNA
      descends from nothing, so it arrives on a fresh source under a fresh copy lineage and is
      recorded in ``events`` as a root; the one breakpoint it makes to open a gap for itself is
      indel-made, so the block it landed inside is not cut in two. The pair divides the same way:
      ``origination`` brings a new **gene family** into the run, ``insertion`` brings **sequence**.
    - ``duplication`` (**per lineage**) copies a geometric-length (mean ``duplication_extent``) arc
      in tandem — an ancestry-**changing** *birth*, recorded in ``events``.
    - ``transfer`` (**per lineage**) copies a geometric-length (mean ``transfer_extent``) arc into a
      **contemporaneous recipient** (``transfer_to``: ``"uniform"``, ``"distance"`` / a `Distance`,
      ``Clades({...}, Between({...}))`` or ``Recipients().weighted_by(driver, mapping)`` — see below;
      ``self_transfer`` allows the donor itself) — a horizontal *birth*, additive (the donor keeps its
      copy). This is what needs the global timeline.
    - ``origination`` (**per lineage**) lays down a **new gene** on a fresh source (geometric length,
      mean ``origination_extent``) — a *birth* of a wholly new family, indivisible from birth.
    - ``fission`` (**per chromosome**) splits a chromosome in two (a **bifurcation**); ``fusion``
      (**per chromosome**) merges two chromosomes of the same topology (the **reticulation**).
      ``chromosome_origination`` (**per lineage**) adds a de-novo circular replicon (a plasmid, a
      network **root**) **carrying one new gene**; ``chromosome_loss`` (**per chromosome**) kills a
      whole chromosome (its material dies as a loss; never the last one) — a network **leaf**. All
      record a chromosome-network edge.

    **A chromosome never exists without a gene.** A replicon is born with one, and any event that
    would strip a chromosome of its last gene — a loss, a translocation carrying it away, a fission
    splitting off a geneless half — simply does not happen. (Vacuous when no genes are declared.)

    The engine runs a **global-timeline** Gillespie: all lineages alive at once evolve along one clock
    (every segmental event is **per lineage** — the rate says how often a lineage does it, the extent how
    much it touches, so a bigger genome does not get proportionally more events; the chromosome rates
    are per chromosome), so a transfer couples two contemporaries. With loss, the strong invariant
    weakens: every node carries a **subset** of the initial sequence (each ancestral position at most
    once, monotonically down every path); origination further adds fresh sources beyond the root.
    Deterministic given ``seed``. (Transfer is always additive.)

    **Driving out of the genome.** A gene declared by a GFF is named, and a named gene answers *is it
    in this lineage, right now?* — ``result.presence("dnaA")``, the driver the other two resolutions
    already hand out. ``modules={"flagellum": ["flgA", "flgB"]}`` groups declared genes, so
    ``result.completion("flagellum")`` gives the fraction of the group a lineage carries; a module
    changes nothing about how the genome evolves.

    **Conditioning (a trait drives who receives).** ``transfer_to =
    Recipients().weighted_by(driver, mapping)``
    weights the candidate recipients by another level, and the numbers are **weights**, not rate
    multipliers: they are normalised across the candidates, so they leave the total amount of transfer
    alone and only redistribute it (SPEC §5, a weight, not a rate). Weight 0 means "cannot receive"; when
    every candidate weighs 0 the transfer does not happen. A ``Between({...})`` mapping reads the
    **donor's** value too, so transfer can be steered between guilds rather than merely into one, and
    ``Clades({...}, Between({...}))`` is the same steering by named clade, read off the tree instead
    of a driver. Since a transfer here is additive, steering changes only which lineage the arc lands
    on — never how much DNA moves, and never anything about the donor."""
    tree = as_tree(tree, level="genomes")
    # Every rate takes the written form (SPEC §5). The scopes here are **per lineage** for the gene
    # events — the rate says how often a lineage does the event and the extent says how much DNA it
    # touches, so the number reads the same whatever the genome's size — and **per chromosome** for the
    # chromosome rates. A bare number therefore stays a bare number, and the scope is stated rather
    # than hardcoded, so it can be seen and (later) overridden.
    _scoped = (("inversion", inversion, PerLineage), ("translocation", translocation, PerLineage),
               ("transposition", transposition, PerLineage), ("loss", loss, PerLineage),
               ("deletion", deletion, PerLineage), ("insertion", insertion, PerLineage),
               ("duplication", duplication, PerLineage), ("transfer", transfer, PerLineage),
               ("origination", origination, PerLineage), ("fission", fission, PerChromosome),
               ("fusion", fusion, PerChromosome),
               ("chromosome_origination", chromosome_origination, PerLineage),
               ("chromosome_loss", chromosome_loss, PerChromosome))
    _rates: dict[str, Rate] = {}
    for label, spec, want in _scoped:
        if isinstance(spec, (int, float)) and not isinstance(spec, bool) and spec < 0:
            raise ValueError(f"{label} must be >= 0, got {spec}")
        rate = as_rate(spec, default_scope=want)
        # `rate.scope` holds the scope CLASS, not an instance — a scope constructor returns the rate
        # itself — so this is an identity test rather than an isinstance one.
        assert rate.scope is not None            # as_rate fills the level's default where none was written
        if rate.scope is not want:
            raise ValueError(
                f"{label} has a {rate.scope.__name__} scope, but the nucleotide engine reads {label} "
                f"as {want.__name__} and cannot read it any other way. Write {want.__name__}(...), "
                f"or drop the scope and let the level fill in its own.")
        for m in rate.modifiers:
            if isinstance(m, Driven):
                check_not_a_kernel(m.mapping, label=label)
            if not is_implemented(m, IMPLEMENTED_MODIFIERS, "genomes.nucleotide"):
                raise ValueError(
                    f"{label} carries {describe(m)}, which the nucleotide genome engine does not "
                    f"support. It takes {', '.join(cell_name(w) for w in IMPLEMENTED_MODIFIERS)}.")
        _rates[label] = rate
    def _as_bp_extent(spec, label, any_base=False):
        """An extent in base pairs (SPEC §6): ``base × modifiers``, no scope. A bare number *is* the
        mean, so ``500`` reads the same here as anywhere else.

        The base must be `Geometric` for a **segmental** event — that engine draws each arc's far
        end **directly from the genome's legal breakpoints** rather than drawing a size and clamping
        it, so an arbitrary shape would have to be re-weighted over that set instead of sampled.
        Refusing beats quietly approximating: the extent reaches the mutator as a *mean*, so a shape
        it cannot express would be accepted and then silently sampled as a geometric anyway.

        An **indel** takes any shape (``any_base``), because its cut set is unrestricted — every
        position is legal for one — so there is nothing left to re-weight against and its size can be
        drawn outright. That is what makes ``Fixed(1)`` mean one nucleotide rather than a geometric of
        mean one, and what puts a power law, the shape indel lengths actually take, within reach. The modifiers are the ones this resolution
        supports on a rate, and they scale the mean: an extent's modifier is read when an event fires,
        so it changes how much that event takes without touching any rate."""
        if isinstance(spec, (int, float)) and not isinstance(spec, bool) and spec < 1:
            raise ValueError(f"{label} must be >= 1 bp, got {spec}")
        e = as_extent(spec)
        if not any_base and not isinstance(e.base, Geometric):
            raise ValueError(
                f"{label} has a {type(e.base).__name__} base, but the nucleotide engine takes a "
                f"geometric extent only — it draws each arc's far end directly from the legal "
                f"breakpoints, so another shape would have to be re-weighted over that set rather "
                f"than drawn. Pass a number (the mean in bp) or Geometric(mean=...).")
        for m in e.modifiers:
            if isinstance(m, Driven):
                check_not_a_kernel(m.mapping, label=label)
            if not is_implemented(m, IMPLEMENTED_MODIFIERS, "genomes.nucleotide"):
                raise ValueError(
                    f"{label} carries {describe(m)}, which the nucleotide genome engine does not "
                    f"support — an extent takes the same modifiers a rate does here "
                    f"({', '.join(cell_name(w) for w in IMPLEMENTED_MODIFIERS)}).")
        return e

    inversion_extent = _as_bp_extent(inversion_extent, "inversion_extent")
    translocation_extent = _as_bp_extent(translocation_extent, "translocation_extent")
    transposition_extent = _as_bp_extent(transposition_extent, "transposition_extent")
    loss_extent = _as_bp_extent(loss_extent, "loss_extent")
    deletion_extent = _as_bp_extent(deletion_extent, "deletion_extent", any_base=True)
    insertion_extent = _as_bp_extent(insertion_extent, "insertion_extent", any_base=True)
    duplication_extent = _as_bp_extent(duplication_extent, "duplication_extent")
    transfer_extent = _as_bp_extent(transfer_extent, "transfer_extent")
    origination_extent = _as_bp_extent(origination_extent, "origination_extent")
    _extents = {"inversion_extent": inversion_extent, "translocation_extent": translocation_extent,
                "transposition_extent": transposition_extent, "loss_extent": loss_extent,
                "deletion_extent": deletion_extent, "insertion_extent": insertion_extent,
                "duplication_extent": duplication_extent, "transfer_extent": transfer_extent,
                "origination_extent": origination_extent}
    if not 0.0 <= inversion_probability <= 1.0:
        raise ValueError(f"inversion_probability must be in [0, 1], got {inversion_probability}")
    # who receives, validated in the one place all three resolutions share (SPEC §5): the mapping's
    # numbers are weights over the candidate recipients, never a rate multiplier
    transfer_to = resolve_transfer_to(transfer_to)
    if isinstance(genes, bool) or not isinstance(genes, int) or genes < 0:
        raise ValueError(f"genes must be a non-negative integer, got {genes!r}")
    if genes and (isinstance(gene_length, bool) or not isinstance(gene_length, int) or gene_length < 1):
        raise ValueError(f"gene_length must be a positive integer, got {gene_length!r}")
    initial_sequence: dict[int, str] = {}               # {source: initial DNA}, empty unless a FASTA is given
    if gff is not None:                              # declared from a GFF: exact coordinates and names
        if genes:
            raise ValueError("pass either gff= or genes=, not both — a GFF already declares the genes")
        lengths, gff_genes = read_gff(gff, trim_overlaps=trim_overlaps)
        seqids = sorted(lengths)                     # a deterministic replicon order
        by_seqid: dict[str, list] = {sq: [] for sq in seqids}
        for gene in gff_genes:
            by_seqid[gene.seqid].append((gene.start, gene.end, gene.strand, gene.name))
        specs = [(_valid_length(lengths[sq]), topology) for sq in seqids]
        layouts = [by_seqid[sq] for sq in seqids]
        if fasta is not None:                        # the initial DNA, one record per replicon, by seqid
            seqs = read_fasta(fasta)
            if set(seqs) != set(seqids):
                raise ValueError(
                    f"the FASTA's records {sorted(seqs)} do not match the GFF's replicons "
                    f"{seqids} — every ##sequence-region needs exactly one > record, same id")
            for i, sq in enumerate(seqids):
                if len(seqs[sq]) != lengths[sq]:
                    raise ValueError(
                        f"replicon {sq!r} is {lengths[sq]} bp in the GFF but {len(seqs[sq])} bp in "
                        "the FASTA — the sequence must be exactly as long as its sequence-region")
                initial_sequence[i] = seqs[sq]
    else:
        if fasta is not None:
            raise ValueError("fasta= needs gff=: the FASTA's records are matched to the GFF's "
                             "replicons by id, so there is nothing to lay down without one")
        specs = _replicon_specs(chromosomes, root_length, topology)
        for _length, _top in specs:                  # the genes must fit; they need not leave a gap
            if genes and genes * gene_length > _length:
                raise ValueError(f"{genes} genes of {gene_length} bp do not fit in a {_length} bp "
                                 f"replicon")
        layouts = [_even_gene_intervals(length, genes, gene_length) for (length, _t) in specs]
    # The one check that is about the run rather than about one argument: an extent too small to
    # cover a gene leaves the gene-level counters at ~0 however high the rates. Here, where the
    # layout is known and nothing has been drawn yet, and on the DECLARED genes only — a de-novo gene
    # arrives later at `origination_extent` and would drag the shortest one down to nothing.
    _warn_if_extents_cannot_reach_a_gene(specs, layouts, _rates, _extents)
    # Conditioning: a rate written with scaled_by reads a driver **per lineage**, so the rates stop being
    # one number for the whole live set and become one per lineage. Same machinery as the family
    # resolution — each driver resolves once into a DriverTrajectory keyed by the shared species node
    # id, from a file or an in-memory trait result. With no driven rate this is empty and the loop
    # stays exactly the pooled one, so an undriven run is untouched.
    driven = {label: [m for m in r.modifiers if isinstance(m, Driven)] for label, r in _rates.items()}
    ext_driven = {label: [m for m in e.modifiers if isinstance(m, Driven)]
                  for label, e in _extents.items()}
    by_key: dict[object, "Driven"] = {}
    for mods in (*driven.values(), *ext_driven.values()):
        for m in mods:
            by_key.setdefault(m.key, m)
    resolved = {}
    if by_key:
        resolved = {key: resolve_driver(m.driver, tree, step=m.step, level="genomes.nucleotide")
                    for key, m in by_key.items()}
        # a mapping whose states never occur leaves every lineage on the default factor, so the run
        # would secretly be the undriven model — refuse it here, naming the driver
        for mods in (*driven.values(), *ext_driven.values()):
            for m in mods:
                label = m.driver if isinstance(m.driver, str) else f"<{type(m.driver).__name__}>"
                check_mapping_fires(m.mapping, resolved[m.key].states(), driver_label=label)
    # Only a driver on a **rate** makes the loop per-lineage and adds a Gillespie breakpoint. A driver
    # on an **extent** is read at the instant an event fires — it changes how much that event takes,
    # never how often one happens — so it deliberately stays out of `trajs`: no per-lineage rate
    # weights, no extra horizon steps. (SPEC §6.)
    _rate_keys = {m.key for mods in driven.values() for m in mods}
    trajs = {k: v for k, v in resolved.items() if k in _rate_keys}
    any_driven = bool(trajs)
    # The transfer_to choice is prepared **after** `trajs` is fixed, for the same reason: a driven
    # transfer_to is a weight, not a rate, so its trajectory must not join `trajs` and start adding
    # horizon breakpoints. `resolved` doubles as the driver cache, so a trait that drives both a rate
    # and who receives is loaded once and read from one trajectory.
    group_of, to_traj = prepare_transfer_to(tree, transfer_to, resolved, level="genomes.nucleotide")

    # by keyword rather than by position: eleven rates and seven extents in one call is exactly the
    # shape where inserting a twelfth silently shifts everything after it
    rates = _Rates(**{k: _rates[k] for k in _rates},
                   inversion_extent=inversion_extent, translocation_extent=translocation_extent,
                   transposition_extent=transposition_extent, loss_extent=loss_extent,
                   deletion_extent=deletion_extent, insertion_extent=insertion_extent,
                   duplication_extent=duplication_extent,
                   transfer_extent=transfer_extent, origination_extent=origination_extent,
                   inversion_probability=inversion_probability)
    depth = mean_root_to_tip(tree)                       # timescale for Distance weighting

    rng, seed = stream("genomes", seed)     # own stream, and a drawn seed if none was given
    chrom_counter = 0
    copy_counter = 0
    source_counter = len(specs)                          # de-novo sources continue past the initial sources

    def new_chrom_id() -> int:
        nonlocal chrom_counter
        cid = chrom_counter
        chrom_counter += 1
        return cid

    def new_copy() -> int:
        nonlocal copy_counter
        copy_counter += 1
        return copy_counter                             # copy ids start at 1 (0 = the unset sentinel)

    family_counter = 0

    def new_family() -> int:
        nonlocal family_counter
        family_counter += 1
        return family_counter                           # gene family ids start at 1 (0 = intergene)

    def new_source() -> int:
        nonlocal source_counter
        src = source_counter
        source_counter += 1
        return src

    genomes: dict[int, NucleotideGenome] = {}
    events: list[Origination | Loss | Duplication | Transfer | Speciation] = []
    deletions: list[Deletion] = []                      # the indel log: extent-changing, no genealogy
    rearrangements: list[Inversion | Translocation | Transposition] = []
    chromosome_events: list[ChromosomeEvent] = []
    root = tree.nodes[tree.root]
    schedule = sorted((tree.nodes[i].end_time, i) for i in tree.nodes)   # (end_time, node) in time order

    initial_chroms = []
    gene_spans: dict[int, tuple[int, int, int]] = {}
    gene_names: dict[str, int] = {}
    gene_strands: dict[int, int] = {}
    for source, ((length, top), intervals) in enumerate(zip(specs, layouts)):  # one source per replicon
        cid = new_chrom_id()
        cp = new_copy()                                 # ...and one initial copy lineage per replicon
        initial_chroms.append(Chromosome(cid, top, _initial_blocks(source, length, cp, intervals, new_family,
                                                             gene_spans, gene_names,
                                                             gene_strands), gene_spans))
        # `initial`, as the copy lineage below is: a replicon the run *starts* with is not something
        # it did, so counting `origination` in either log gives the de-novo ones alone
        chromosome_events.append(ChromosomeEvent(root.birth_time, "initial", root.id, (), (cid,)))
        events.append(Origination(root.birth_time, root.id, cid, cp, source, 0, length, kind="initial"))

    # a module groups declared genes, so it can only be checked once the layout has named them — the
    # same validation the other two resolutions run against `family_names`, here against the GFF's
    module_map = resolve_modules(modules, gene_names)

    # the run's starting genome: a deep snapshot, so the live genome's events never reach it
    initial_genome = NucleotideGenome(
        [Chromosome(c.id, c.topology, [Block(b.source, b.start, b.end, b.strand, b.copy, b.gene)
                                       for b in c.blocks]) for c in initial_chroms])

    t = root.birth_time
    alive: list[int] = []                               # the live-lineage set (species._grow shape)
    gen: list[NucleotideGenome] = []
    pos: dict[int, int] = {}
    enter(alive, gen, pos, root.id, NucleotideGenome(initial_chroms))
    total_length = sum(c.length for c in initial_chroms)
    total_chromosomes = len(initial_chroms)

    # thirteen bare numbers on their stated scopes — no modifier on any rate, none on any extent —
    # is the common run, and it needs none of the loop's context machinery: each total is
    # scope(base) exactly, resolved here once. A rate whose base is None carries a set_by, which is
    # a modifier, so `plain` is False and its 0.0 is never read.
    plain = (not any(r.modifiers for r in _rates.values())
             and not any(e.has_modifiers for e in _extents.values()))
    _b = {label: (rate.base or 0.0) for label, rate in _rates.items()}
    inv_b, trl_b, trp_b = _b["inversion"], _b["translocation"], _b["transposition"]
    los_b, del_b, ins_b = _b["loss"], _b["deletion"], _b["insertion"]
    dup_b, tra_b, org_b = _b["duplication"], _b["transfer"], _b["origination"]
    fis_b, fus_b = _b["fission"], _b["fusion"]
    cor_b, clo_b = _b["chromosome_origination"], _b["chromosome_loss"]
    no_weights: dict = {}    # what `w` is when nothing is driven: read by .get, never written

    bar = progress_bar(len(schedule), "genomes", unit="branch", enabled=progress)
    si = 0
    while si < len(schedule):
        bar.to(si)
        length, count, nlin = total_length, total_chromosomes, len(alive)
        can_xfer = nlin >= 2 or self_transfer
        next_species = schedule[si][0]
        if plain:
            # no modifier on any rate or extent: each total is scope(base) exactly — a gene event
            # per living lineage, a chromosome event per standing chromosome — none of them ever
            # changes on its own (next_change is inf), and nothing below reads a context or a
            # weight.
            w = no_weights
            r_inv = inv_b * nlin
            r_trl = trl_b * nlin
            r_trp = trp_b * nlin
            r_los = los_b * nlin
            r_del = del_b * nlin
            r_ins = ins_b * nlin
            r_dup = dup_b * nlin
            r_tra = tra_b * nlin if can_xfer else 0.0
            r_org = org_b * nlin
            r_fis = fis_b * count
            r_fus = fus_b * count
            r_cor = cor_b * nlin
            r_clo = clo_b * count
            horizon = next_species
        else:
            # Each rate carries its own scope, so the count it is "per" comes from the context rather
            # than from a multiplication written here. The gene events are PER LINEAGE: the rate says
            # how often a lineage does the event and the extent says how much DNA it touches, so a
            # bigger genome does NOT get more events (that would double-count size and explode).
            ctx = {"copies": 0, "lineages": nlin, "chromosomes": count, "time": t}
            # A driven rate differs from lineage to lineage, so it is summed **over the living
            # lineages**, each read with its own driver value and its own chromosome count — and the
            # weights are kept, because the affected lineage must then be drawn with them too. An
            # undriven rate stays pooled (one .effective, uniform pick), so a run with no driver is
            # byte-identical to before.
            w = {}
            if any_driven:
                drivers = [{key: trajs[key].value(alive[k], t) for key in trajs} for k in range(nlin)]
                for label, rate in _rates.items():
                    if driven[label]:
                        w[label] = [rate.effective(copies=0, lineages=1,
                                                   chromosomes=len(gen[k].chromosomes), time=t,
                                                   drivers=drivers[k]) for k in range(nlin)]

            def _r(label, pooled, live=True):
                """The total for one event class: summed per-lineage when driven, pooled when not."""
                if not live:
                    return 0.0
                return sum(w[label]) if label in w else pooled

            r_inv = _r("inversion", rates.inversion.effective(**ctx))
            r_trl = _r("translocation", rates.translocation.effective(**ctx))
            r_trp = _r("transposition", rates.transposition.effective(**ctx))
            r_los = _r("loss", rates.loss.effective(**ctx))
            r_del = _r("deletion", rates.deletion.effective(**ctx))
            r_ins = _r("insertion", rates.insertion.effective(**ctx))
            r_dup = _r("duplication", rates.duplication.effective(**ctx))
            r_tra = _r("transfer", rates.transfer.effective(**ctx), live=can_xfer)
            r_org = _r("origination", rates.origination.effective(**ctx))
            r_fis = _r("fission", rates.fission.effective(**ctx))
            r_fus = _r("fusion", rates.fusion.effective(**ctx))
            r_cor = _r("chromosome_origination", rates.chromosome_origination.effective(**ctx))
            r_clo = _r("chromosome_loss", rates.chromosome_loss.effective(**ctx))
            # a skyline steps at a known time, so the race runs only to the next of those or the next
            # species event — whichever comes first — and the rates are re-read on the other side.
            horizon = min(next_species, rates.inversion.next_change(t), rates.translocation.next_change(t),
                          rates.transposition.next_change(t), rates.loss.next_change(t),
                          rates.deletion.next_change(t), rates.insertion.next_change(t),
                          rates.duplication.next_change(t), rates.transfer.next_change(t),
                          rates.origination.next_change(t), rates.fission.next_change(t),
                          rates.fusion.next_change(t), rates.chromosome_origination.next_change(t),
                          rates.chromosome_loss.next_change(t))
            if any_driven:  # a driven rate also changes when its driver switches mid-branch — step there
                horizon = min(horizon, min((trajs[key].next_change(alive[k], t) for key in trajs
                                            for k in range(nlin)), default=math.inf))
        total = (r_inv + r_trl + r_trp + r_los + r_del + r_ins + r_dup + r_tra + r_org + r_fis
                 + r_fus + r_cor + r_clo)

        def _ext(label, k):
            """An extent's mean in bp for this event: the base mean, scaled by its modifiers read on
            the acting lineage. Undriven is the common case and costs nothing — the point of reading it
            here rather than in the rate loop is that an extent changes no rate, so it never had to be
            raced to."""
            e = _extents[label]
            if not e.has_modifiers:
                return e.base.mean()
            # the same `ctx` the rates were read in, not a thinner one: an extent's modifiers pass
            # the gate that admits a rate's, so they are promised the same context (see `_ext_ctx`
            # in the ordered engine, which had the same hole).
            # `time` fresh, not `ctx`'s: `ctx` predates `t = t_ev`, and an extent is read at the
            # instant the event fires (see `_ext_ctx` in the ordered engine, same hole).
            return e.mean(**{**ctx, "time": t},
                          drivers={key: resolved[key].value(alive[k], t) for key in resolved})

        def _ext_draw(label, k):
            """One **drawn** size in bp, for an indel. The segmental events are parameterised by the
            mean (`_ext`) because they sample a far end out of the legal cut set; an indel has no
            such restriction, so it draws its size outright and any shape works —
            `Extent.sample()` scales the draw rather than the distribution's parameter, so the base
            still means what it says. At least 1 bp: a zero-length indel is not an event."""
            e = _extents[label]
            if not e.has_modifiers:
                return max(1, int(e.base.sample(rng)))
            return max(1, int(e.sample(rng, **{**ctx, "time": t},
                                       drivers={key: resolved[key].value(alive[k], t)
                                                for key in resolved})))

        def _pick(label, fallback=None):
            """The affected lineage: drawn by its own effective rate where that rate is driven — the
            same weights the total was summed with — and otherwise by the rate's own undriven rule,
            which is uniform for a per-lineage rate and by chromosome count for a per-chromosome one."""
            ws = w.get(label)
            if ws:
                return weighted_index(rng, ws, sum(ws))
            return fallback() if fallback is not None else int(rng.integers(nlin))
        if total > 0.0:
            t_ev = t + float(rng.exponential(1.0 / total))
            if t_ev < horizon:                          # a genome event fires before the horizon
                t = t_ev
                r = float(rng.random()) * total
                b_trl = r_inv + r_trl
                b_trp = b_trl + r_trp
                b_los = b_trp + r_los
                # at deletion=0 this collapses onto b_los, so the ladder is numerically what it was
                # and no run written before indels existed moves
                b_del = b_los + r_del
                b_ins = b_del + r_ins
                b_dup = b_ins + r_dup
                b_tra = b_dup + r_tra
                b_org = b_tra + r_org
                b_fis = b_org + r_fis
                b_fus = b_fis + r_fus
                b_cor = b_fus + r_cor
                if r < r_inv:
                    k = _pick("inversion")
                    _do_inversion(gen[k], alive[k], t, _ext("inversion_extent", k), rng, rearrangements)
                elif r < b_trl:
                    k = _pick("translocation")
                    _do_translocation(gen[k], alive[k], t, _ext("translocation_extent", k),
                                      rates.inversion_probability, rng, rearrangements)
                elif r < b_trp:
                    k = _pick("transposition")
                    _do_transposition(gen[k], alive[k], t, _ext("transposition_extent", k),
                                      rates.inversion_probability, rng, rearrangements)
                elif r < b_los:
                    k = _pick("loss")
                    total_length += _do_loss(gen[k], alive[k], t, _ext("loss_extent", k), rng, events)
                elif r < b_del:
                    k = _pick("deletion")
                    total_length += _do_deletion(gen[k], alive[k], t, _ext_draw("deletion_extent", k),
                                                 rng, deletions)
                elif r < b_ins:
                    k = _pick("insertion")
                    total_length += _do_insertion(gen[k], alive[k], t, _ext_draw("insertion_extent", k),
                                                  rng, events, new_source, new_copy)
                elif r < b_dup:
                    k = _pick("duplication")
                    total_length += _do_duplication(gen[k], alive[k], t, _ext("duplication_extent", k),
                                                    rng, events, new_copy)
                elif r < b_tra:
                    kd = _pick("transfer")
                    total_length += _do_transfer(rng, tree, alive, gen, kd, t, _ext("transfer_extent", kd),
                                                 transfer_to, self_transfer, depth, events, new_copy,
                                                 to_traj, group_of)
                elif r < b_org:
                    k = _pick("origination")            # per lineage; weighted when driven
                    total_length += _do_origination(gen[k], alive[k], t, _ext("origination_extent", k),
                                                    rng, events, new_source, new_copy,
                                                    new_family, gene_spans, gene_strands)
                elif r < b_fis:
                    k = _pick("fission", lambda: _pick_lineage_by_chromosomes(rng, gen, count))
                    total_chromosomes += _do_fission(gen[k], alive[k], t, rng, chromosome_events,
                                                     new_chrom_id)
                elif r < b_fus:
                    k = _pick("fusion", lambda: _pick_lineage_by_chromosomes(rng, gen, count))
                    total_chromosomes += _do_fusion(gen[k], alive[k], t, rng, chromosome_events,
                                                    new_chrom_id)
                elif r < b_cor:
                    k = _pick("chromosome_origination")  # per lineage; weighted when driven
                    dc, dl = _do_chromosome_origination(
                        gen[k], alive[k], t, _ext("origination_extent", k), rng, events,
                        chromosome_events, new_chrom_id, new_source, new_copy, new_family,
                        gene_spans, gene_strands)
                    total_chromosomes += dc
                    total_length += dl
                else:
                    k = _pick("chromosome_loss", lambda: _pick_lineage_by_chromosomes(rng, gen, count))
                    dc, dl = _do_chromosome_loss(gen[k], alive[k], t, rng, events, chromosome_events)
                    total_chromosomes += dc
                    total_length += dl
                continue

        t = horizon
        if horizon < next_species:                      # a rate stepped, not a species event: re-read
            continue                                    # the rates on the other side and race again
        while si < len(schedule) and schedule[si][0] == t:   # process the whole tie-batch
            i = schedule[si][1]
            g = gen[pos[i]]
            genomes[i] = g                              # freeze: the lineage retires, never mutated again
            total_length -= g.length
            total_chromosomes -= len(g.chromosomes)
            retire(alive, gen, pos, pos[i])
            node = tree.nodes[i]
            if node.children:              # a speciation: re-mint into the daughters
                for c, cg in _speciate(node, g, new_chrom_id, new_copy, events,
                                       chromosome_events).items():
                    enter(alive, gen, pos, c, cg)
                    total_length += cg.length
                    total_chromosomes += len(cg.chromosomes)
            si += 1
    bar.close()
    return NucleotideGenomesResult(tree, genomes, events, rearrangements, chromosome_events, seed,
                                   gene_spans, gene_names, module_map,
                                   gene_strands=gene_strands, initial_genome=initial_genome,
                                   initial_sequence=initial_sequence, deletions=deletions)

Results

One result type per resolution, each carrying the true history behind the dataset: the gene trees, the event log, and the genomes themselves.

zombi2.genomes.FamilyGenomesResult dataclass

FamilyGenomesResult(complete_tree: Tree, node_genomes: dict[int, tuple[GeneCopy, ...]], edges: list[GeneEdge], seed: int | None, family_names: dict[str, int] = dict(), modules: dict[str, tuple[str, ...]] = dict(), initial_genome: tuple[GeneCopy, ...] = (), max_family_size: int | None = None)

What simulate_genomes_family returns: the complete_tree it ran on, the final node_genomes at every node (extant and extinct), the events log (the compact source of truth), and the seed. The observed dataset is the extant tips, genomes. The phyletic profiles are derived from those tips on access, and write materialises the chosen outputs to disk.

genomes property

genomes: dict[str, tuple[GeneCopy, ...]]

The observed dataset — the genome at each extant tip, keyed by the tip name the tree writes: n5.

Keyed by name because the only thing anyone does with this is join it to the tree, or to another level grown on that tree, and both name their tips. genomes.tsv on disk is keyed by name too, so what you get in Python and what you get from a file are the same dataset — they used to share no keys at all, and nothing said so. This is the trait level's TraitsResult.values for gene content.

node_genomes is the run's own record: every node, extant and extinct and internal alike, keyed by node id. Use that one to join against complete_tree.nodes or the event log.

profiles cached property

profiles: Profiles

The phyletic profiles — each gene family's copy count in each extant species — derived from the observed genomes (the classic comparative-genomics matrix). See profiles.

events cached property

events: list[Event]

The genome events — one per row of genome_events.tsv, the same objects the writer formats.

edges is the finer record this is grouped from: one entry per gene-tree edge, so a duplication is two of them and a transfer likewise. That is the shape a gene tree is built out of, and it used to be what this attribute returned — which meant counting duplications in Python gave twice the file's number, and a filter on kind == "transfer" matched everything here and nothing there. One word, one meaning: an event is what the log has a row for.

gene_trees cached property

gene_trees: dict[int, GeneTree]

{family id: GeneTree} — each family's true genealogy inside the complete tree, derived from the event log. Each GeneTree exposes .complete and .extant. See gene_trees.

family_counts

family_counts(node_id: int) -> collections.Counter

A multiset view of one node's genome: family id → copy count.

Source code in zombi2/genomes/family.py
def family_counts(self, node_id: int) -> collections.Counter:
    """A multiset view of one node's genome: ``family id → copy count``."""
    return collections.Counter(c.family for c in self.node_genomes[node_id])

completion

completion(name: str)

A module's completion as a conditioning driverModuleCompletion, a number in [0, 1]: the fraction of the module's families a lineage carries.

Read it with a Curve, the way any continuous driver is read; a threshold goes there rather than here (lambda f: 8.0 if f > 0.8 else 1.0).

Source code in zombi2/genomes/family.py
def completion(self, name: str):
    """A module's completion as a **conditioning driver** — `ModuleCompletion`, a number in
    ``[0, 1]``: the fraction of the module's families a lineage carries.

    Read it with a `Curve`, the way any continuous driver is read; a threshold goes there rather
    than here (``lambda f: 8.0 if f > 0.8 else 1.0``)."""
    from .presence import ModuleCompletion
    if name not in self.modules:
        raise KeyError(f"no module {name!r}; declared modules are {sorted(self.modules)}")
    return ModuleCompletion(self, name)

presence

presence(name: str)

The named family's presence as a conditioning driverGenePresence.

has_family answers for one node; this answers for every lineage at every instant, which is what a driven rate needs::

switch=PerLineage(0.1).scaled_by(g.presence("tox"), {"present": 5.0, "absent": 1.0})
Source code in zombi2/genomes/family.py
def presence(self, name: str):
    """The named family's presence as a **conditioning driver** — `GenePresence`.

    ``has_family`` answers for one node; this answers for every lineage at every instant, which
    is what a driven rate needs::

        switch=PerLineage(0.1).scaled_by(g.presence("tox"), {"present": 5.0, "absent": 1.0})
    """
    from .presence import GenePresence
    if name not in self.family_names:
        raise KeyError(f"no named family {name!r}; declared families are "
                       f"{sorted(self.family_names)}")
    return GenePresence(self, name)

has_family

has_family(node_id: int, name: str) -> bool

Whether the named family name (declared via families=) is present — has ≥ 1 copy — in the genome at node_id. The presence a joint scaled_by("genomes:<name>", …) reads as its driver.

Source code in zombi2/genomes/family.py
def has_family(self, node_id: int, name: str) -> bool:
    """Whether the named family ``name`` (declared via ``families=``) is present — has ≥ 1 copy — in
    the genome at ``node_id``. The presence a joint ``scaled_by("genomes:<name>", …)`` reads as its driver."""
    if name not in self.family_names:
        raise KeyError(f"no named family {name!r}; declared families are {sorted(self.family_names)}")
    fid = self.family_names[name]
    return any(c.family == fid for c in self.node_genomes[node_id])

summary

summary() -> dict

What this run produced, as a plain dict — the payload of genome_summary.json.

Every count here is one per event, which is also what genome_events.tsv is now one row of. They are counted from the GeneEdge objects, and those are one per gene-tree edge: a duplication, a transfer and a speciation each end one gene and start two, so counting edges inflates them exactly 2×. A duplication's two edges share the parent gene they descend from, and a gene ends at exactly one event, so distinct parents are the events.

loss counts every gene that died, which under replacement is more than the log's loss rows: a copy displaced by an arriving transfer has no row of its own — it is the second parent of that transfer_replacing row, because its death and the transfer are one event. It is a loss of the gene tree all the same, and that is what this counts.

The family counts are the other thing nobody could reconcile: gene_trees/ holds a file pair per family that ever existed, while the run's summary line counts the ones that survived, so "96 gene families" sat next to 213 files with nothing to explain the gap. Both numbers are here, named.

origination counts only the families that arose during the run: the initial genome is logged as origination at the root's own start time, so a bare count of that kind is de-novo arrivals plus initial_families, which is a number nobody asked for. They are separate here.

And the cap, which was invisible. When max_family_size binds it discards duplications and arriving transfers, so realised rates fall below the declared ones — so this reports which families are sitting at it, because that is the signal a reader can act on.

empty_genomes is the other end of the same story. There is no floor at this resolution: loss is counted per gene copy and the last copy is a copy like any other, so a high loss rate can strip a lineage of every gene it has. That is a real outcome of the model, not a failure — but it is invisible in the outputs, because a genome with no genes writes no row in profiles.tsv and leaves no gene tree for a sequence to run down. This is the number that says it happened, before a reader wonders why the matrix is short. (The ordered and nucleotide resolutions do have a floor, but it is a statement about what a chromosome is — a loss never takes a chromosome below its last gene — not a bound on genome size.)

Source code in zombi2/genomes/family.py
def summary(self) -> dict:
    """What this run produced, as a plain dict — the payload of ``genome_summary.json``.

    **Every count here is one per event**, which is also what ``genome_events.tsv`` is now one row
    of. They are counted from the `GeneEdge` objects, and those are one per gene-tree *edge*: a
    duplication, a transfer and a speciation each end one gene and start two, so counting edges
    inflates them exactly 2×. A duplication's two edges share the ``parent`` gene they descend
    from, and a gene ends at exactly one event, so distinct parents *are* the events.

    ``loss`` counts every gene that died, which under ``replacement`` is more than the log's
    ``loss`` rows: a copy displaced by an arriving transfer has no row of its own — it is the
    second parent of that ``transfer_replacing`` row, because its death and the transfer are one
    event. It is a loss of the gene tree all the same, and that is what this counts.

    The family counts are the other thing nobody could reconcile: ``gene_trees/`` holds a file pair
    per family that ever existed, while the run's summary line counts the ones that survived, so
    "96 gene families" sat next to 213 files with nothing to explain the gap. Both numbers are
    here, named.

    ``origination`` counts only the families that arose *during* the run: the initial genome is
    logged as origination at the root's own start time, so a bare count of that kind is de-novo
    arrivals plus ``initial_families``, which is a number nobody asked for. They are separate here.

    And the cap, which was invisible. When ``max_family_size`` binds it discards duplications and
    arriving transfers, so realised rates fall below the declared ones — so this reports which
    families are sitting at it, because that is the signal a reader can act on.

    ``empty_genomes`` is the other end of the same story. There is **no floor** at this
    resolution: loss is counted per gene copy and the last copy is a copy like any other, so a
    high loss rate can strip a lineage of every gene it has. That is a real outcome of the model,
    not a failure — but it is invisible in the outputs, because a genome with no genes writes no
    row in ``profiles.tsv`` and leaves no gene tree for a sequence to run down. This is the
    number that says it happened, before a reader wonders why the matrix is short. (The ordered
    and nucleotide resolutions do have a floor, but it is a statement about what a chromosome is
    — a loss never takes a chromosome below its last gene — not a bound on genome size.)"""
    t0 = self.complete_tree.nodes[self.complete_tree.root].birth_time
    counted = event_counts(self.edges, t0)    # shared with the ordered and nucleotide summaries

    extant = list(self.complete_tree.extant_leaves())
    born = {e.family for e in self.edges}
    surviving = {c.family for i in extant for c in self.node_genomes.get(i, ())}
    genes_per_genome = [len(self.node_genomes.get(i, ())) for i in extant]
    cells = [collections.Counter(c.family for c in self.node_genomes.get(i, ())) for i in extant]
    copies = [n for cell in cells for n in cell.values()]

    cap = self.max_family_size
    at_cap = sorted({fam for cell in cells for fam, n in cell.items()
                     if cap is not None and n >= cap})
    return {
        "level": "genomes",
        "seed": self.seed,
        "resolution": "family",
        # one number per EVENT — the same thing a row of genome_events.tsv is
        "events": counted,
        "families": {"born": len(born), "surviving": len(surviving),
                     "died_out": len(born) - len(surviving),
                     "named": len(self.family_names)},
        "extant_genomes": len(extant),
        "empty_genomes": sum(1 for i in extant if not self.node_genomes.get(i, ())),
        "genes_per_genome": _stats(genes_per_genome),
        "copies_per_family_per_genome": _stats(copies),
        # the cap made visible. `families_at_cap` is what to look at: a family sitting at the
        # ceiling had events discarded, so its realised rates are below the ones you declared.
        "family_size_cap": {
            "cap": cap,
            "families_at_cap": len(at_cap),
            "cells_at_cap": sum(1 for cell in cells for n in cell.values()
                                if cap is not None and n >= cap),
            "family_ids_at_cap": at_cap},
    }

write

write(directory, outputs=('events', 'profiles', 'genomes', 'initial_genome', 'gene_trees', 'species_tree', 'summary'), *, flat: bool = False) -> None

Materialise chosen outputs to directory (created if needed):

  • "events"genome_events.tsv, the event log (the source of truth).
  • "profiles"profiles.tsv, the family × extant-species copy-count matrix.
  • "genomes"genomes.tsv, every node's gene content, one row per gene copy — ancestors included, where profiles.tsv counts only the extant tips.
  • "initial_genome"initial_genome.tsv, the genome the run started with. Its own file, not a row in genomes.tsv, because it belongs to no node: it sits at the start of the root branch, and every lineage in that table is a node at the end of one.
  • "gene_trees"gene_tree_fam<family>_{complete,extant}.nwk under gene_trees/, each family's true genealogy. A family with no surviving copy writes no _extant file.

  • "species_tree"species_complete.nwk, the tree the run evolved along. Written because a directory of gene trees with no species tree is not a dataset anyone can use: every one of these outputs is indexed by that tree's node labels, and the truth a gene tree is compared against is the species tree it grew inside. A run written from Python used to leave it out entirely, so the quickstart handed back gene trees with nothing to compare them to and said nothing about it. The gene trees are two files per family, so they get a subdirectory rather than burying the tables above; flat=True writes everything into directory instead.

Source code in zombi2/genomes/family.py
def write(self, directory, outputs=("events", "profiles", "genomes", "initial_genome",
                                    "gene_trees", "species_tree", "summary"), *,
          flat: bool = False) -> None:
    """Materialise chosen ``outputs`` to ``directory`` (created if needed):

    - ``"events"`` → ``genome_events.tsv``, the event log (the source of truth).
    - ``"profiles"`` → ``profiles.tsv``, the family × extant-species copy-count matrix.
    - ``"genomes"`` → ``genomes.tsv``, every node's gene content, one row per gene copy —
      **ancestors included**, where ``profiles.tsv`` counts only the extant tips.
    - ``"initial_genome"`` → ``initial_genome.tsv``, the genome the run started with. Its own
      file, not a row in ``genomes.tsv``, because it belongs to no node: it sits at the start of
      the root branch, and every ``lineage`` in that table is a node at the end of one.
    - ``"gene_trees"`` → ``gene_tree_fam<family>_{complete,extant}.nwk`` under ``gene_trees/``,
      each family's true genealogy. A family with no surviving copy writes no ``_extant`` file.

    - ``"species_tree"`` → ``species_complete.nwk``, the tree the run evolved along. Written
      because a directory of gene trees with no species tree is not a dataset anyone can use:
      every one of these outputs is *indexed by* that tree's node labels, and the truth a gene
      tree is compared against is the species tree it grew inside. A run written from Python
      used to leave it out entirely, so the quickstart handed back gene trees with nothing to
      compare them to and said nothing about it.
    The gene trees are two files per family, so they get a subdirectory rather than burying the
    tables above; ``flat=True`` writes everything into ``directory`` instead.
    """
    # An unknown token used to write nothing and exit clean — silent data loss you discover
    # three pipeline steps later, when the next tool has no input. The other levels have always
    # raised; these two did not.
    if unknown := [o for o in outputs if o not in self.OUTPUTS]:
        raise ValueError(f"unknown write outputs {unknown}; choose from {list(self.OUTPUTS)}")
    d = pathlib.Path(directory)
    d.mkdir(parents=True, exist_ok=True)
    # a run's directory describes that run: clear the per-unit directories this write is
    # about to fill, so nothing from a previous run survives inside them (see fresh_dirs)
    fresh_dirs(d, ("gene_trees",), flat)
    names = self.complete_tree.labels()      # e<id> for a lineage that died; n<id> for the rest
    if "events" in outputs:
        (d / "genome_events.tsv").write_text(events_tsv(self.edges, names), encoding="utf-8")
    if "profiles" in outputs:
        (d / "profiles.tsv").write_text(self.profiles.to_tsv(), encoding="utf-8")
    if "genomes" in outputs:
        (d / "genomes.tsv").write_text(self._genomes_tsv(), encoding="utf-8")
    if "initial_genome" in outputs:
        (d / "initial_genome.tsv").write_text(self._initial_genome_tsv(), encoding="utf-8")
    if "gene_trees" in outputs:
        write_gene_trees(self.gene_trees, grouped_dir(d, "gene_trees", flat), names)
    if "species_tree" in outputs:
        (d / "species_complete.nwk").write_text(self.complete_tree.to_newick() + "\n",
                                                encoding="utf-8")
    if "summary" in outputs:
        write_summary(d / "genome_summary.json", self.summary())

zombi2.genomes.OrderedGenomesResult dataclass

OrderedGenomesResult(complete_tree: Tree, node_genomes: dict[int, tuple[Chromosome, ...]], edges: list[GeneEdge], rearrangements: list[Inversion | Transposition | Translocation], chromosome_events: list[ChromosomeEvent], seed: int | None, family_names: dict[str, int] = dict(), modules: dict[str, tuple[str, ...]] = dict(), event_positions: list[EventPosition] = list(), initial_genome: tuple[Chromosome, ...] = ())

What simulate_genomes_ordered() returns: the complete_tree it ran on, the final genomes at every node as tuples of Chromosome\ s, the shared gene-genealogy events log, the rearrangements (inversions, transpositions and translocations) and chromosome_events (the chromosome genealogy) logs, and the seed. The observed genomes are the extant tips; profiles and gene_trees are derived from the (position-blind) genealogy exactly as for the family core; gene_order reads a node's layout, and write materialises the chosen outputs.

genomes property

genomes: dict[str, tuple[Chromosome, ...]]

The observed dataset — the genome at each extant tip, keyed by the tip name the tree writes: n5.

Keyed by name because the only thing anyone does with this is join it to the tree, or to another level grown on that tree, and both name their tips. The written output is keyed by name too, so what you get in Python and what you get from a file are the same dataset. This is the trait level's TraitsResult.values for gene content.

node_genomes is the run's own record: every node, extant and extinct and internal alike, keyed by node id. Use that one to join against complete_tree.nodes or the event log.

profiles cached property

profiles: Profiles

The phyletic profiles — each gene family's copy count in each extant species — derived from the observed genomes, flattening across chromosomes (position does not enter). See profiles.

events cached property

events: list[Event]

The genome events — one per row of genome_events.tsv, the same objects the writer formats.

edges is the finer record this is grouped from: one entry per gene-tree edge, so a duplication is two of them and a transfer likewise. That is the shape a gene tree is built out of, and it used to be what this attribute returned — which meant counting duplications in Python gave twice the file's number, and a filter on kind == "transfer" matched everything here and nothing there. One word, one meaning: an event is what the log has a row for.

gene_trees cached property

gene_trees: dict[int, GeneTree]

{family id: GeneTree} — each family's true genealogy inside the complete tree, derived from the (position-blind) event log exactly as for the family core. See gene_trees.

family_counts

family_counts(node_id: int) -> collections.Counter

A multiset view of one node's genome: family id → copy count (across all chromosomes).

Source code in zombi2/genomes/ordered.py
def family_counts(self, node_id: int) -> collections.Counter:
    """A multiset view of one node's genome: ``family id → copy count`` (across all chromosomes)."""
    return collections.Counter(g.family for chrom in self.node_genomes[node_id] for g in chrom.genes)

completion

completion(name: str)

A module's completion as a conditioning driverModuleCompletion, a number in [0, 1]: the fraction of the module's families a lineage carries.

Read it with a Curve, the way any continuous driver is read; a threshold goes there rather than here (lambda f: 8.0 if f > 0.8 else 1.0).

Source code in zombi2/genomes/ordered.py
def completion(self, name: str):
    """A module's completion as a **conditioning driver** — `ModuleCompletion`, a number in
    ``[0, 1]``: the fraction of the module's families a lineage carries.

    Read it with a `Curve`, the way any continuous driver is read; a threshold goes there rather
    than here (``lambda f: 8.0 if f > 0.8 else 1.0``)."""
    from .presence import ModuleCompletion
    if name not in self.modules:
        raise KeyError(f"no module {name!r}; declared modules are {sorted(self.modules)}")
    return ModuleCompletion(self, name)

presence

presence(name: str)

The named family's presence as a conditioning driverGenePresence.

has_family answers for one node; this answers for every lineage at every instant, which is what a driven rate needs::

switch=PerLineage(0.1).scaled_by(g.presence("tox"), {"present": 5.0, "absent": 1.0})
Source code in zombi2/genomes/ordered.py
def presence(self, name: str):
    """The named family's presence as a **conditioning driver** — `GenePresence`.

    ``has_family`` answers for one node; this answers for every lineage at every instant, which
    is what a driven rate needs::

        switch=PerLineage(0.1).scaled_by(g.presence("tox"), {"present": 5.0, "absent": 1.0})
    """
    from .presence import GenePresence
    if name not in self.family_names:
        raise KeyError(f"no named family {name!r}; declared families are "
                       f"{sorted(self.family_names)}")
    return GenePresence(self, name)

has_family

has_family(node_id: int, name: str) -> bool

Whether the named family name (declared via family_names=) has ≥ 1 copy in the genome at node_id (across all chromosomes).

Source code in zombi2/genomes/ordered.py
def has_family(self, node_id: int, name: str) -> bool:
    """Whether the named family ``name`` (declared via ``family_names=``) has ≥ 1 copy in the genome at
    ``node_id`` (across all chromosomes)."""
    if name not in self.family_names:
        raise KeyError(f"no named family {name!r}; declared families are {sorted(self.family_names)}")
    fid = self.family_names[name]
    return any(g.family == fid for chrom in self.node_genomes[node_id] for g in chrom.genes)

gene_order

gene_order(node_id: int) -> list[tuple[int, int, int, int, int]]

One node's layout as (chromosome, position, strand, family, gene id) rows, chromosome by chromosome and left to right within each — the ordered analogue of family_counts.

Source code in zombi2/genomes/ordered.py
def gene_order(self, node_id: int) -> list[tuple[int, int, int, int, int]]:
    """One node's layout as ``(chromosome, position, strand, family, gene id)`` rows, chromosome
    by chromosome and left to right within each — the ordered analogue of ``family_counts``."""
    return [(chrom.id, pos, g.strand, g.family, g.id)
            for chrom in self.node_genomes[node_id] for pos, g in enumerate(chrom.genes)]

write

write(directory, outputs=('events', 'profiles', 'gene_order', 'initial_genome', 'gene_trees', 'chromosome_events', 'species_tree', 'summary'), *, flat: bool = False) -> None

Materialise chosen outputs to directory (created if needed):

  • "events"two tables, because a run does two different things to a genome. genome_events.tsv is the gene genealogy — one row per event, in the format every resolution writes — with where each event happened beside it. rearrangement_events.tsv is the ancestry-neutral rearrangements: an inversion, a transposition or a translocation begins and ends no gene lineage, so it has no parents and no children and nothing to say in those columns. The two used to be one table, which meant nine columns empty on every rearrangement row and six on every genealogy row. Together with gene_order they are enough to replay the run.
  • "profiles"profiles.tsv, the family × extant-species copy-count matrix.
  • "gene_order"gene_order.tsv, every node's layout (one row per gene), ancestors included — so a branch's rearrangements can be replayed from its parent's genome.
  • "initial_genome"initial_genome.tsv, the layout the run started with. Its own file, not a row in gene_order.tsv, because it belongs to no node: it sits at the start of the root branch, and every lineage in that table is a node at the end of one.
  • "chromosome_events"chromosome_events.tsv, the chromosome genealogy edges. The one log kept apart: it is a network over chromosome ids, with list-valued parents and children, joined on a different key from everything above.
  • "gene_trees"gene_tree_fam<family>_{complete,extant}.nwk under gene_trees/, each family's true genealogy — unchanged from the family resolution, position being orthogonal to it.

The gene trees are two files per family, so they get a subdirectory rather than burying the tables above; flat=True writes everything into directory instead.

Source code in zombi2/genomes/ordered.py
def write(self, directory, outputs=("events", "profiles", "gene_order", "initial_genome",
                                    "gene_trees", "chromosome_events", "species_tree",
                                    "summary"), *,
          flat: bool = False) -> None:
    """Materialise chosen ``outputs`` to ``directory`` (created if needed):

    - ``"events"`` → **two** tables, because a run does two different things to a genome.
      ``genome_events.tsv`` is the gene genealogy — one row per event, in the format every
      resolution writes — with **where** each event happened beside it.
      ``rearrangement_events.tsv`` is the ancestry-neutral rearrangements: an inversion, a
      transposition or a translocation begins and ends no gene lineage, so it has no parents and
      no children and nothing to say in those columns. The two used to be one table, which meant
      nine columns empty on every rearrangement row and six on every genealogy row. Together with
      ``gene_order`` they are enough to replay the run.
    - ``"profiles"`` → ``profiles.tsv``, the family × extant-species copy-count matrix.
    - ``"gene_order"`` → ``gene_order.tsv``, every node's layout (one row per gene), ancestors
      included — so a branch's rearrangements can be replayed from its parent's genome.
    - ``"initial_genome"`` → ``initial_genome.tsv``, the layout the run started with. Its own
      file, not a row in ``gene_order.tsv``, because it belongs to no node: it sits at the start
      of the root branch, and every ``lineage`` in that table is a node at the end of one.
    - ``"chromosome_events"`` → ``chromosome_events.tsv``, the chromosome genealogy edges. The
      one log kept apart: it is a network over chromosome **ids**, with list-valued parents and
      children, joined on a different key from everything above.
    - ``"gene_trees"`` → ``gene_tree_fam<family>_{complete,extant}.nwk`` under ``gene_trees/``,
      each family's true genealogy — unchanged from the family resolution, position being
      orthogonal to it.

    The gene trees are two files per family, so they get a subdirectory rather than burying the
    tables above; ``flat=True`` writes everything into ``directory`` instead.
    """
    # An unknown token used to write nothing and exit clean — silent data loss you discover
    # three pipeline steps later, when the next tool has no input. The other levels have always
    # raised; these two did not.
    if unknown := [o for o in outputs if o not in self.OUTPUTS]:
        raise ValueError(f"unknown write outputs {unknown}; choose from {list(self.OUTPUTS)}")
    d = pathlib.Path(directory)
    d.mkdir(parents=True, exist_ok=True)
    # a run's directory describes that run: clear the per-unit directories this write is
    # about to fill, so nothing from a previous run survives inside them (see fresh_dirs)
    fresh_dirs(d, ("gene_trees",), flat)
    names = self.complete_tree.labels()   # e<id> for a lineage that died; n<id> for the rest
    if "events" in outputs:
        (d / "genome_events.tsv").write_text(
            _events_tsv(self.edges, self.event_positions, names), encoding="utf-8")
        (d / "rearrangement_events.tsv").write_text(
            rearrangement_events_tsv(self.rearrangements, names), encoding="utf-8")
    if "profiles" in outputs:
        (d / "profiles.tsv").write_text(self.profiles.to_tsv(), encoding="utf-8")
    if "gene_order" in outputs:
        (d / "gene_order.tsv").write_text(self._gene_order_tsv(names), encoding="utf-8")
    if "initial_genome" in outputs:
        (d / "initial_genome.tsv").write_text(self._initial_genome_tsv(), encoding="utf-8")
    if "chromosome_events" in outputs:
        (d / "chromosome_events.tsv").write_text(
            chromosome_events_tsv(self.chromosome_events, self.complete_tree, names),
            encoding="utf-8")
    if "gene_trees" in outputs:
        write_gene_trees(self.gene_trees, grouped_dir(d, "gene_trees", flat),
                         self.complete_tree.labels())
    if "species_tree" in outputs:            # the tree everything here is indexed by: without
        (d / "species_complete.nwk").write_text(   # it a directory of gene trees is not a dataset
            self.complete_tree.to_newick() + "\n", encoding="utf-8")
    if "summary" in outputs:
        write_summary(d / "genome_summary.json", self.summary())

summary

summary() -> dict

What this run produced, as a plain dict — the payload of genome_summary.json.

The corrected event counts are the reason this exists. genome_events.tsv's loss rows undercount real losses whenever replacement is on, because a copy displaced by an arriving transfer has no row of its own; the migration guide names that as the change most likely to hand a returning user a plausible wrong number, and points them here. This file used to be written only at the family resolution — so the advice was sound and the remedy was absent at the two resolutions where the gap is larger (64% at ordered, measured).

event_counts is shared with the other two resolutions, so the three cannot drift. The rest is what this resolution has and the family core does not: where the genes sit, and what moved them.

Source code in zombi2/genomes/ordered.py
def summary(self) -> dict:
    """What this run produced, as a plain dict — the payload of ``genome_summary.json``.

    The **corrected event counts** are the reason this exists. ``genome_events.tsv``'s ``loss``
    rows undercount real losses whenever ``replacement`` is on, because a copy displaced by an
    arriving transfer has no row of its own; the migration guide names that as the change most
    likely to hand a returning user a plausible wrong number, and points them here. This file used
    to be written only at the family resolution — so the advice was sound and the remedy was
    absent at the two resolutions where the gap is *larger* (64% at ordered, measured).

    `event_counts` is shared with the other two resolutions, so the three cannot drift. The rest
    is what this resolution has and the family core does not: where the genes sit, and what moved
    them."""
    t0 = self.complete_tree.nodes[self.complete_tree.root].birth_time
    extant = list(self.complete_tree.extant_leaves())
    born = {e.family for e in self.edges}
    surviving = {g.family for i in extant for c in self.node_genomes.get(i, ()) for g in c.genes}
    genes_per_genome = [sum(len(c.genes) for c in self.node_genomes.get(i, ())) for i in extant]
    chrom_per_genome = [len(self.node_genomes.get(i, ())) for i in extant]
    rearrangements = collections.Counter(type(r).__name__.lower() for r in self.rearrangements)
    chromosome = collections.Counter(e.kind for e in self.chromosome_events)
    return {
        "level": "genomes",
        "seed": self.seed,
        "resolution": "ordered",
        "events": event_counts(self.edges, t0),
        "families": {"born": len(born), "surviving": len(surviving),
                     "died_out": len(born) - len(surviving),
                     "named": len(self.family_names)},
        "extant_genomes": len(extant),
        "empty_genomes": sum(1 for i in extant
                             if not any(c.genes for c in self.node_genomes.get(i, ()))),
        "genes_per_genome": _stats(genes_per_genome),
        "chromosomes_per_genome": _stats(chrom_per_genome),
        # this resolution's own two records: what moved genes without changing their ancestry,
        # and what happened to the replicons carrying them
        "rearrangements": {k: rearrangements.get(k, 0)
                           for k in ("inversion", "transposition", "translocation")},
        "chromosome_events": dict(sorted(chromosome.items())),
    }

zombi2.genomes.NucleotideGenomesResult dataclass

NucleotideGenomesResult(complete_tree: Tree, node_genomes: dict[int, NucleotideGenome], events: list[Origination | Loss | Duplication | Transfer | Speciation], rearrangements: list[Inversion | Translocation | Transposition], chromosome_events: list[ChromosomeEvent], seed: int | None, gene_spans: dict[int, tuple[int, int, int]] = dict(), gene_names: dict[str, int] = dict(), modules: dict[str, tuple[str, ...]] = dict(), gene_strands: dict[int, int] = dict(), initial_genome: NucleotideGenome = (lambda: NucleotideGenome([]))(), initial_sequence: dict[int, str] = dict(), deletions: list[Deletion] = list())

What simulate_genomes_nucleotide() returns: the complete_tree it ran on, the final nucleotide genomes (karyotypes) at every node, the copy-lineage genealogy events (origination, loss, duplication, transfer, speciation — carrying the copy ids the gene-tree recovery reads), the ancestry-neutral rearrangements (inversion, translocation, transposition), the chromosome_events (the chromosome network), and the seed. mosaic / trace_back / ancestry read a node's genome.

genomes property

genomes: dict[str, NucleotideGenome]

The observed dataset — the genome at each extant tip, keyed by the tip name the tree writes: n5.

Keyed by name because the only thing anyone does with this is join it to the tree, or to another level grown on that tree, and both name their tips. The written output is keyed by name too, so what you get in Python and what you get from a file are the same dataset. This is the trait level's TraitsResult.values for gene content.

node_genomes is the run's own record: every node, extant and extinct and internal alike, keyed by node id. Use that one to join against complete_tree.nodes or the event log.

indels property

indels: int

How many indels this run fired — deletions plus insertions. Both leave a lineage carrying part of a root block rather than all of it, which is the thing assembly() cannot yet put back together, so they are counted together and asked about in one place.

root_blocks property

root_blocks: list[tuple[int, int, int]]

The recovered root partition: (source, start, end) for each maximal never-cut interval that some node still carries — one per block_trees entry (by index).

Cut at the breakpoints of every node's genome, not only the extant leaves', which is what lets any node be reconstructed (assembly()) rather than the survivors alone.

block_trees property

block_trees: dict[int, GeneTree]

{root-block index: GeneTree} — a tree for every recovered root block, spacer as well as genes, keyed by its index in root_blocks.

gene_trees covers the declared genes; this covers the whole genome. A block never splits, so its size is fixed and its genealogy is in the event log just as a gene's is — the recovery is the same one, pointed at every block instead of a chosen few. That is what makes an ancestral genome reconstructable at any node rather than only at the loci you declared.

A gene's tree here has the same topology and branch lengths as its gene_trees one, but not the same g<id> leaf labels: segment ids are handed out as the recovery walks its targets, and walking every block numbers them differently from walking three. Use one accessor or the other within a piece of analysis — they are the same genealogy under different names.

gene_trees property

gene_trees: dict[int, GeneTree]

{family: GeneTree} — the recovered gene trees.

With genes declared, one tree per gene, keyed by its gene family id (see gene_spans); the intergenic root-blocks keep their block ancestry in the log but are not built into trees. With no genes declared the whole genome is one big intergene, so every recovered root-block is a family in its own right and the key is its index in root_blocks. Every node votes on the partition, so a gene surviving only in lineages that died still gets a tree — a complete one, with no extant tree to go with it. Only a gene lost from every node has no root-block and no tree.

genealogy property

genealogy: list[GeneEdge]

The run's genealogy as GeneEdgethe same table the family and ordered resolutions write, and what lands in genome_events.tsv.

A nucleotide run's own record, events, is interval-shaped: a copy lineage covers an extent, an event covers a sub-extent, and a duplication there mints a child without ending the parent (a split is not a birth). That is the right model for sequence, and the wrong shape for a gene tree. This is the translation onto the root-block partition, where a copy either covers a block in full or does not touch it — so a duplication is a bifurcation, writes two rows sharing a parent, and the ids are gene ids: the ones the gene trees, the alignments and the homology tables use. It is what the recovery already builds to derive gene_trees; writing it costs nothing extra.

describe

describe(node_id: int) -> str

One node's genome written out block by block, for reading by eye.

mosaic is the same information as data — (source, start, end, strand) per block — and this is it as text, one line per block, each labelled with the gene it is or intergene:

n2, chromosome 2
  [   0, 600) +  600 bp  intergene
  [ 600,1000) +  400 bp  gene 1

A block is a stretch of DNA with one unbroken ancestry, written as the interval it came from on the initial sequence and the strand it is read on, so a gene keeps its coordinates wherever it turns up and whichever way it points. A gene is always one block, because nothing may cut one; a run of intergene lines is spacer that events have cut apart.

Genes declared with a name (from a GFF) are named; the rest are numbered by family.

Source code in zombi2/genomes/nucleotide.py
def describe(self, node_id: int) -> str:
    """One node's genome written out block by block, for reading by eye.

    `mosaic` is the same information as data — ``(source, start, end, strand)`` per block — and
    this is it as text, one line per block, each labelled with the gene it is or ``intergene``:

        n2, chromosome 2
          [   0, 600) +  600 bp  intergene
          [ 600,1000) +  400 bp  gene 1

    A block is a stretch of DNA with one unbroken ancestry, written as the interval it came from
    on the **initial** sequence and the strand it is read on, so a gene keeps its coordinates
    wherever it turns up and whichever way it points. A gene is always one block, because
    nothing may cut one; a run of intergene lines is spacer that events have cut apart.

    Genes declared with a name (from a GFF) are named; the rest are numbered by family.
    """
    by_span = {span: fam for fam, span in self.gene_spans.items()}
    named = {fam: name for name, fam in self.gene_names.items()}
    # `node_label` and not an f-string: a lineage that died is `e5`, and the one place that
    # spelling is decided is the tree's own helper
    node = self.complete_tree.nodes.get(node_id)
    name = node_label(node_id, node.fate if node is not None else None)
    out = []
    for chromosome, blocks in self.mosaic(node_id).items():
        out.append(f"{name}, chromosome {chromosome}")
        for source, start, end, strand in blocks:
            fam = by_span.get((source, start, end))
            what = "intergene" if fam is None else named.get(fam, f"gene {fam}")
            out.append(f"  [{start:4d},{end:4d}) {'+' if strand == 1 else '−'} "
                       f"{end - start:4d} bp  {what}")
    return "\n".join(out)

completion

completion(name: str)

A module's completion as a conditioning driverModuleCompletion, a number in [0, 1]: the fraction of the module's genes a lineage carries.

Read it with a Curve, the way any continuous driver is read; a threshold goes there rather than here (lambda f: 8.0 if f > 0.8 else 1.0).

Source code in zombi2/genomes/nucleotide.py
def completion(self, name: str):
    """A module's completion as a **conditioning driver** — `ModuleCompletion`, a number in
    ``[0, 1]``: the fraction of the module's genes a lineage carries.

    Read it with a `Curve`, the way any continuous driver is read; a threshold goes there rather
    than here (``lambda f: 8.0 if f > 0.8 else 1.0``)."""
    from .presence import ModuleCompletion
    if name not in self.modules:
        raise KeyError(f"no module {name!r}; declared modules are {sorted(self.modules)}")
    return ModuleCompletion(self, name)

presence

presence(name: str)

The named gene's presence as a conditioning driverGenePresence, the same reader the other two resolutions hand out, read off the gene's own recovered tree::

switch=PerLineage(0.1).scaled_by(g.presence("dnaA"), {"present": 5.0, "absent": 1.0})

A gene is named here by the GFF that declared it (its ID / Name); the evenly-spaced genes= layout lays its genes down unnamed.

Source code in zombi2/genomes/nucleotide.py
def presence(self, name: str):
    """The named gene's presence as a **conditioning driver** — `GenePresence`, the same reader
    the other two resolutions hand out, read off the gene's own recovered tree::

        switch=PerLineage(0.1).scaled_by(g.presence("dnaA"), {"present": 5.0, "absent": 1.0})

    A gene is named here by the GFF that declared it (its ``ID`` / ``Name``); the evenly-spaced
    ``genes=`` layout lays its genes down unnamed."""
    from .presence import GenePresence
    if name not in self.gene_names:
        raise KeyError(
            f"no named gene {name!r}; declared genes are {sorted(self.gene_names)}. A gene is "
            f"named by the GFF that declares it (its ID / Name attribute) — the evenly-spaced "
            f"genes= layout has no names to give.")
    return GenePresence(self, name)

block_of

block_of(family: int) -> int

The index in root_blocks of the block a declared gene family occupies — the join between the two numbering schemes this resolution has.

They are both plain ints over overlapping ranges, so mixing them up is silent: gene_spans and gene_trees are keyed by gene family id, while root_blocks, block_trees and everything a sequence run produces here are keyed by block index (every block evolves, and spacer has no family). block_of is how you get from a gene to its sequences::

r.alignments[g.block_of(g.gene_names["dnaA"])]     # that gene's alignment

Raises KeyError for a family that was never declared, and LookupError for one declared but surviving nowhere at all — it has no recovered block, so there is nothing to point at. The reverse lookup is one line: {span: fam for fam, span in g.gene_spans.items()} read at root_blocks[i].

Source code in zombi2/genomes/nucleotide.py
def block_of(self, family: int) -> int:
    """The index in `root_blocks` of the block a declared **gene family** occupies — the join
    between the two numbering schemes this resolution has.

    They are both plain ints over overlapping ranges, so mixing them up is silent: ``gene_spans``
    and ``gene_trees`` are keyed by **gene family id**, while ``root_blocks``, ``block_trees`` and
    everything a sequence run produces here are keyed by **block index** (every block evolves, and
    spacer has no family). ``block_of`` is how you get from a gene to its sequences::

        r.alignments[g.block_of(g.gene_names["dnaA"])]     # that gene's alignment

    Raises ``KeyError`` for a family that was never declared, and ``LookupError`` for one declared
    but surviving nowhere at all — it has no recovered block, so there is nothing to point at.
    The reverse lookup is one line: ``{span: fam for fam, span in g.gene_spans.items()}`` read at
    ``root_blocks[i]``."""
    span = self.gene_spans[family]                    # KeyError: never declared
    try:
        return self.root_blocks.index(span)
    except ValueError:
        raise LookupError(
            f"gene family {family} spans {span} but has no recovered root block — no node in the "
            "tree still carries it, so nothing was reconstructed for it. gene_trees leaves such a "
            "family out for the same reason.") from None

assembly

assembly(node_id: int) -> dict[int, list[tuple[int, int, int, int, int]]]

How this node's genome is built out of the recovered root blocks: {chromosome id: [(block, gene, strand, lo, hi), …]} in physical order, where block indexes root_blocks, gene is the gene id that block's tree gives this node's copy (the g<id> label in block_trees), strand is +1 read forward or -1 reverse-complemented, and [lo, hi) is the half-open sub-range of that block this node carries, offset from the block's own start.

The sub-range is what an indel makes necessary and is the whole of the presence bookkeeping: a deletion leaves a lineage holding a block minus a stretch of its middle, and an insertion opens a gap inside one, so a piece is a part of a block rather than all of it. Without indels every piece is the whole block — lo is 0 and hi its length — so the shape says the same thing it always did, at one extra pair of numbers.

To reconstruct a genome: take [lo, hi) of each piece's block sequence, flip the -1\ s, and concatenate. The sequence level does exactly that; nothing here knows about letters. Every node works — an extinct leaf and the root as readily as a surviving tip — which is what makes the whole history recoverable rather than only its leaves. initial_assembly() does the same for the genome the run started with.

Every node votes on where the partition is cut (see _root_block_partition()), so a node's every event breakpoint is in it and no piece ever straddles two blocks. What a block is cut into is one piece per root block it spans — and on a reversed block those come out in descending coordinate order, since physical order runs down the source. Only an indel breakpoint, deliberately absent from the partition, makes a piece narrower than the block it indexes.

Source code in zombi2/genomes/nucleotide.py
def assembly(self, node_id: int) -> dict[int, list[tuple[int, int, int, int, int]]]:
    """How this node's genome is built out of the recovered root blocks:
    ``{chromosome id: [(block, gene, strand, lo, hi), …]}`` in **physical order**, where ``block``
    indexes `root_blocks`, ``gene`` is the gene id that block's tree gives this node's copy (the
    ``g<id>`` label in `block_trees`), ``strand`` is ``+1`` read forward or ``-1``
    reverse-complemented, and ``[lo, hi)`` is the **half-open sub-range of that block** this node
    carries, offset from the block's own start.

    The sub-range is what an indel makes necessary and is the whole of the presence bookkeeping:
    a deletion leaves a lineage holding a block minus a stretch of its middle, and an insertion
    opens a gap inside one, so a piece is a *part* of a block rather than all of it. Without
    indels every piece is the whole block — ``lo`` is 0 and ``hi`` its length — so the shape says
    the same thing it always did, at one extra pair of numbers.

    To reconstruct a genome: take ``[lo, hi)`` of each piece's block sequence, flip the
    ``-1``\\ s, and concatenate. The sequence level does exactly that; nothing here knows about
    letters. **Every** node works — an extinct leaf and the root as readily as a surviving tip —
    which is what makes the whole history recoverable rather than only its leaves.
    `initial_assembly()` does the same for the genome the run started with.

    Every node votes on where the partition is cut (see `_root_block_partition()`), so a node's
    every *event* breakpoint is in it and no piece ever straddles two blocks. What a block is cut
    into is one piece per root block it spans — and on a reversed block those come out in
    descending coordinate order, since physical order runs *down* the source. Only an indel
    breakpoint, deliberately absent from the partition, makes a piece narrower than the block it
    indexes."""
    tips = self._recover_blocks()[2]
    blocks = self.root_blocks
    what = node_label(node_id)
    out: dict[int, list[tuple[int, int, int, int, int]]] = {}
    for cid, pieces in self._pieces(self.node_genomes[node_id], what).items():
        named: list[tuple[int, int, int, int, int]] = []
        for (i, copy, strand, lo, hi) in pieces:
            gene = tips.get((i, copy), _MISSING)
            if gene is _MISSING or gene is None:
                raise AssertionError(                            # a guard — see the class docstring
                    f"{what} carries {blocks[i]} under copy lineage {copy}, but that block's "
                    + ("genealogy has no such copy" if gene is _MISSING
                       else "genealogy ends that copy in a loss")
                    + " — the event log and the genomes disagree")
            named.append((i, gene, strand, lo, hi))
        out[cid] = named
    return out

initial_assembly

initial_assembly() -> dict[int, list[tuple[int, int, int, int]]]

assembly() for initial_genome: {chromosome id: [(block, strand, lo, hi), …]}.

No gene id here, unlike assembly(), and that is the honest shape rather than a saving. The initial genome sits at the start of the root branch, before any event, so each of its blocks has exactly one sequence — the founding draw the sequence level records as founding[block] — and there is no copy to disambiguate. A gene id would in fact be wrong here: the one assembly() gives is the last gene a copy held, and for an initial copy that is at the far end of the stem. A loss on the stem can even end it, which is the same thing said louder.

Source code in zombi2/genomes/nucleotide.py
def initial_assembly(self) -> dict[int, list[tuple[int, int, int, int]]]:
    """`assembly()` for `initial_genome`: ``{chromosome id: [(block, strand, lo, hi), …]}``.

    No gene id here, unlike `assembly()`, and that is the honest shape rather than a saving.
    The initial genome sits at the **start** of the root branch, before any event, so each of its
    blocks has exactly one sequence — the founding draw the sequence level records as
    ``founding[block]`` — and there is no copy to disambiguate. A gene id would in fact be *wrong*
    here: the one `assembly()` gives is the **last** gene a copy held, and for an initial copy
    that is at the far end of the stem. A loss on the stem can even end it, which is the same
    thing said louder."""
    return {cid: [(i, strand, lo, hi) for (i, _copy, strand, lo, hi) in pieces]
            for cid, pieces in self._pieces(self.initial_genome, "the initial genome").items()}

write

write(directory, outputs=('events', 'genes', 'blocks', 'initial_genome', 'initial_sequence', 'gene_trees', 'chromosome_events', 'gff', 'bed', 'species_tree', 'summary'), *, flat: bool = False) -> None

Materialise chosen outputs to directory (created if needed):

  • "events"three tables, because a nucleotide run records three different things. genome_events.tsv is the genealogy (genealogy) in the format every resolution writes, so one reader serves them all: one row per event, its participants named n<species>_g<copy>. block_events.tsv is this resolution's own record — the copy-lineage log over ancestral intervals, one row per interval an event touched, so an event spanning several blocks writes several rows sharing a time and kind. rearrangement_events.tsv is the ancestry-neutral rearrangements, which begin and end no lineage and so have nothing to put in parents and children. The last two are what read_nucleotide_genomes() replays.
  • "blocks"blocks.tsv, every node's genome as its block mosaic (ancestors included, as for the ordered resolution's gene_order). The one big file here: blocks are not kept maximal during a run, so a rearrangement-heavy genome carries far more of them than it has distinct ancestral runs, and this grows with their number × every node.
  • "genes"genes.tsv, the declared genes and where they sit in root coordinates. Header-only for a run that declared none.
  • "initial_genome"initial_genome.tsv, the block mosaic the run started with. Its own file, not a row in blocks.tsv, because it belongs to no node: it sits at the start of the root branch, and every lineage in that table is a node at the end of one.
  • "chromosome_events"chromosome_events.tsv, the chromosome network's edges. The one log kept apart: it is a network over chromosome ids, with list-valued parents and children, joined on a different key from everything above.
  • "gene_trees"gene_tree_fam<family>_{complete,extant}.nwk, one recovered genealogy per family some node still carries; the _extant file only where the family has a surviving copy.
  • "initial_sequence"initial_sequence.fasta, the initial DNA the run was given (fasta=), one >source<n> record per replicon. Written only when a FASTA was supplied — it is what lets a separate zombi2 sequences run found its blocks from the real sequence.
  • "gff"genome_<lineage>.gff under gff/, that genome's genes, in its own coordinates: the annotation to read beside the sequence level's genome_<lineage>.fasta.
  • "bed"genome_<lineage>.bed under bed/, that genome's blocks — every piece, spacer included, named by the ancestral interval it descends from. The ancestry as a browser track.

Both name their sequences <lineage>_chr<c>, exactly as the FASTA records are named, so a genome and its annotation join without renaming anything. Written for every node and for the initial genome, so there are two files per genome.

gene_trees, gff and bed are one file per family or per node — thousands, on a real genome times a real tree — so each gets a subdirectory rather than burying the tables above; flat=True writes everything into directory instead.

Source code in zombi2/genomes/nucleotide.py
def write(self, directory, outputs=("events", "genes", "blocks", "initial_genome",
                                    "initial_sequence", "gene_trees", "chromosome_events",
                                    "gff", "bed", "species_tree", "summary"), *,
          flat: bool = False) -> None:
    """Materialise chosen ``outputs`` to ``directory`` (created if needed):

    - ``"events"`` → **three** tables, because a nucleotide run records three different things.
      ``genome_events.tsv`` is the genealogy (`genealogy`) in the format *every* resolution
      writes, so one reader serves them all: one row per event, its participants named
      ``n<species>_g<copy>``. ``block_events.tsv`` is this resolution's own record — the
      copy-lineage log over **ancestral intervals**, one row per interval an event touched, so an
      event spanning several blocks writes several rows sharing a ``time`` and ``kind``.
      ``rearrangement_events.tsv`` is the ancestry-neutral rearrangements, which begin and end no
      lineage and so have nothing to put in ``parents`` and ``children``. The last two are what
      `read_nucleotide_genomes()` replays.
    - ``"blocks"`` → ``blocks.tsv``, every node's genome as its block mosaic (ancestors
      included, as for the ordered resolution's ``gene_order``). The one big file here: blocks
      are not kept maximal during a run, so a rearrangement-heavy genome carries far more of
      them than it has distinct ancestral runs, and this grows with their number × every node.
    - ``"genes"`` → ``genes.tsv``, the declared genes and where they sit in root coordinates.
      Header-only for a run that declared none.
    - ``"initial_genome"`` → ``initial_genome.tsv``, the block mosaic the run started with. Its
      own file, not a row in ``blocks.tsv``, because it belongs to no node: it sits at the start
      of the root branch, and every ``lineage`` in that table is a node at the end of one.
    - ``"chromosome_events"`` → ``chromosome_events.tsv``, the chromosome network's edges. The
      one log kept apart: it is a network over chromosome **ids**, with list-valued parents and
      children, joined on a different key from everything above.
    - ``"gene_trees"`` → ``gene_tree_fam<family>_{complete,extant}.nwk``, one recovered
      genealogy per family some node still carries; the ``_extant`` file only where the family
      has a surviving copy.
    - ``"initial_sequence"`` → ``initial_sequence.fasta``, the initial DNA the run was given (``fasta=``),
      one ``>source<n>`` record per replicon. Written only when a FASTA was supplied — it is what
      lets a separate ``zombi2 sequences`` run found its blocks from the real sequence.
    - ``"gff"`` → ``genome_<lineage>.gff`` under ``gff/``, that genome's **genes**, in its own
      coordinates: the annotation to read beside the sequence level's ``genome_<lineage>.fasta``.
    - ``"bed"`` → ``genome_<lineage>.bed`` under ``bed/``, that genome's **blocks** — every piece,
      spacer included, named by the ancestral interval it descends from. The ancestry as a
      browser track.

    Both name their sequences ``<lineage>_chr<c>``, exactly as the FASTA records are named, so a
    genome and its annotation join without renaming anything. Written for every node and for the
    initial genome, so there are two files per genome.

    ``gene_trees``, ``gff`` and ``bed`` are one file per family or per node — thousands, on a real
    genome times a real tree — so each gets a subdirectory rather than burying the tables above;
    ``flat=True`` writes everything into ``directory`` instead.
    """
    # An unknown token used to write nothing and exit clean — silent data loss you discover
    # three pipeline steps later, when the next tool has no input. The other levels have always
    # raised; these two did not.
    if unknown := [o for o in outputs if o not in self.OUTPUTS]:
        raise ValueError(f"unknown write outputs {unknown}; choose from {list(self.OUTPUTS)}")
    d = pathlib.Path(directory)
    d.mkdir(parents=True, exist_ok=True)
    # a run's directory describes that run: clear the per-unit directories this write is
    # about to fill, so nothing from a previous run survives inside them (see fresh_dirs)
    fresh_dirs(d, ("gene_trees", "gff", "bed"), flat)
    names = self.complete_tree.labels()   # e<id> for a lineage that died; n<id> for the rest
    if "events" in outputs:
        # Three tables, because they describe three things. `genome_events.tsv` is the genealogy
        # in the one format every resolution writes, so one reader serves them all;
        # `block_events.tsv` is this resolution's own interval record, which has no counterpart
        # elsewhere; `rearrangement_events.tsv` is what moved without ending anything. The first
        # two used to share a name, which made a nucleotide log look readable to the family reader
        # while meaning something else in the same columns; the third used to be rows inside the
        # second, with its copy and interval columns empty on every one of them.
        (d / "genome_events.tsv").write_text(events_tsv(self.genealogy, names), encoding="utf-8")
        (d / "block_events.tsv").write_text(
            _nucleotide_events_tsv([*self.events, *self.deletions], self.complete_tree, names),
            encoding="utf-8")
        (d / "rearrangement_events.tsv").write_text(
            rearrangement_events_tsv(self.rearrangements, names), encoding="utf-8")
    if "blocks" in outputs:
        (d / "blocks.tsv").write_text(self._blocks_tsv(names), encoding="utf-8")
    if "genes" in outputs:
        (d / "genes.tsv").write_text(self._genes_tsv(), encoding="utf-8")
    if "initial_genome" in outputs:
        (d / "initial_genome.tsv").write_text(self._initial_genome_tsv(), encoding="utf-8")
    if "chromosome_events" in outputs:
        (d / "chromosome_events.tsv").write_text(
            chromosome_events_tsv(self.chromosome_events, self.complete_tree, names),
            encoding="utf-8")
    if "gene_trees" in outputs:
        write_gene_trees(self.gene_trees, grouped_dir(d, "gene_trees", flat), names)
    if "species_tree" in outputs:            # the tree everything here is indexed by: without
        (d / "species_complete.nwk").write_text(   # it a directory of gene trees is not a dataset
            self.complete_tree.to_newick() + "\n", encoding="utf-8")
    if "initial_sequence" in outputs and self.initial_sequence:
        (d / "initial_sequence.fasta").write_text(
            "".join(f">source{src}\n{self.initial_sequence[src]}\n"
                    for src in sorted(self.initial_sequence)))
    for token, ext, render in (("gff", "gff", self._gff), ("bed", "bed", self._bed)):
        if token in outputs:
            into = grouped_dir(d, token, flat)
            for label, genome in self._every_genome(names):
                (into / f"genome_{label}.{ext}").write_text(render(label, genome), encoding="utf-8")
    if "summary" in outputs:
        write_summary(d / "genome_summary.json", self.summary())

summary

summary() -> dict

What this run produced, as a plain dict — the payload of genome_summary.json.

The same correction as the other two resolutions: a GeneEdge is one gene-tree edge, so a duplication, a transfer and a speciation each end one gene and start two, and counting edges inflates them exactly 2×; this is the corrected count. (A transfer here is always additive, with no replacement option, so no displaced copy to fold in.) event_counts is shared with them, reading genealogy — the GeneEdge translation every resolution writes — so the three agree by construction rather than by three separate implementations agreeing by luck.

The unit here is the base pair, so there are no phyletic profiles and no per-family copy counts to report; what this resolution has instead is how much sequence there is and how it is divided.

Source code in zombi2/genomes/nucleotide.py
def summary(self) -> dict:
    """What this run produced, as a plain dict — the payload of ``genome_summary.json``.

    The same correction as the other two resolutions: a `GeneEdge` is one gene-tree *edge*, so a
    duplication, a transfer and a speciation each end one gene and start two, and counting edges
    inflates them exactly 2×; this is the corrected count. (A transfer here is always additive,
    with no ``replacement`` option, so no displaced copy to fold in.) `event_counts` is shared
    with them, reading `genealogy` — the `GeneEdge` translation every resolution writes — so the
    three agree by construction rather than by three separate implementations agreeing by luck.

    The unit here is the base pair, so there are no phyletic profiles and no per-family copy
    counts to report; what this resolution has instead is how much sequence there is and how it is
    divided."""
    t0 = self.complete_tree.nodes[self.complete_tree.root].birth_time
    extant = list(self.complete_tree.extant_leaves())
    lengths = [self.node_genomes[i].length for i in extant if i in self.node_genomes]
    chrom_per_genome = [len(self.node_genomes[i].chromosomes) for i in extant if i in self.node_genomes]
    rearrangements = collections.Counter(type(r).__name__.lower() for r in self.rearrangements)
    chromosome = collections.Counter(e.kind for e in self.chromosome_events)
    # this resolution's own log, counted one number per EVENT — a row of `block_events.tsv` is
    # one ancestral *interval*, so an event spanning several blocks writes several rows. An
    # `Origination` splits the way `event_counts` splits it: the genome the run started with is
    # `initial`, and only a de-novo arrival is an `origination`.
    blocks = collections.Counter(
        (e.kind if isinstance(e, Origination) else type(e).__name__.lower())
        for e in self.events)
    return {
        "level": "genomes",
        "seed": self.seed,
        "resolution": "nucleotide",
        "events": event_counts(self.genealogy, t0),
        "extant_genomes": len(extant),
        # every gene the run holds, not only the ones declared at the start: `origination`
        # and `chromosome_origination` both mint genes into `gene_spans` as the run goes,
        # so a run that declared five can end with seven. It was called declared_genes.
        "genes": len(self.gene_spans),
        "base_pairs_per_genome": _stats(lengths),
        "chromosomes_per_genome": _stats(chrom_per_genome),
        # The same six kinds as `events`, so the two read side by side — and they must, because
        # an event here takes an arc of DNA while `events` counts gene-tree branchings, and
        # neither bounds the other: an arc covering three genes is three gene events, and an arc
        # covering none is zero. That is the whole answer to "why is duplication 0 when
        # block_events.tsv has thirteen rows" — see `_warn_if_extents_cannot_reach_a_gene`.
        "block_events": {k: blocks.get(k, 0)
                         for k in ("initial", "origination", "insertion", "duplication",
                                   "transfer", "loss", "speciation")},
        "rearrangements": {k: rearrangements.get(k, 0)
                           for k in ("inversion", "transposition", "translocation")},
        # the indel log, counted one number per EVENT like block_events above. It is neither a
        # genealogy event nor a rearrangement: no copy ends or begins, and material does go.
        "deletions": len(self.deletions),
        "base_pairs_deleted": sum(end - beg for e in self.deletions
                                  for (_cp, _src, beg, end) in e.deleted),
        "chromosome_events": dict(sorted(chromosome.items())),
    }

zombi2.genomes.StreamedRun dataclass

StreamedRun(directory: str, seed: 'int | None', n_families: int, n_events: int, outputs: tuple)

A genome run written straight to disk, family by family — what stream_to= returns, for a scale where a whole FamilyGenomesResult would not fit in memory. Thin by design: the outputs are the files and the disk is the handoff (the sequences level reads them back), so this carries where they are and how big the run was, not the run itself.

path

path(output: str) -> str

The path of a written top-level file — e.g. path("events")…/genome_events.tsv. Gene trees are not a single file; they live one pair per family under gene_trees/.

Source code in zombi2/genomes/_perfamily.py
def path(self, output: str) -> str:
    """The path of a written top-level file — e.g. ``path("events")`` → ``…/genome_events.tsv``.
    Gene trees are not a single file; they live one pair per family under ``gene_trees/``."""
    if output not in _STREAM_FILENAMES:
        raise KeyError(f"{output!r} is not a top-level streamed file (gene trees are under "
                       f"gene_trees/); files are {sorted(_STREAM_FILENAMES)}")
    return os.path.join(self.directory, _STREAM_FILENAMES[output])

Reading a run back

A written run is a genome run too: read_run reopens one from its directory, and the sequence level accepts a directory or a StreamedRun wherever it accepts a result.

zombi2.genomes.read_run

read_run(directory) -> FamilyGenomesResult

Reopen a genome run written to directory — the run object, from its files.

Takes a run directory in either layout (out/ with a genomes/ inside it, or a flat=True directory), or a StreamedRun handle. The event log is the source of truth, so gene_trees, events and the whole genealogy come back exactly; genomes and initial_genome come from their own tables when the run wrote them, and are empty otherwise.

This is what simulate_sequences calls when handed a path, so the two-command CLI pipeline and a two-step Python one are the same pipeline::

st = genomes.simulate_genomes_family(sp, ..., stream_to="run/")
sequences.simulate_sequences(st, model=hky85(), length=1000, seed=1)   # reads it back

The seed of a reopened run is the seed recorded on the handle when there is one, and None from a bare directory: the files are the run, and a number that did not produce them would be a lie. It is in the run's own run.zombi2 / genomes.log either way.

Source code in zombi2/genomes/read.py
def read_run(directory) -> FamilyGenomesResult:
    """Reopen a genome run written to ``directory`` — the run object, from its files.

    Takes a run directory in either layout (``out/`` with a ``genomes/`` inside it, or a ``flat=True``
    directory), or a `StreamedRun` handle. The event log is the source of truth, so ``gene_trees``,
    ``events`` and the whole genealogy come back exactly; ``genomes`` and ``initial_genome`` come from
    their own tables when the run wrote them, and are empty otherwise.

    This is what ``simulate_sequences`` calls when handed a path, so the two-command CLI pipeline and
    a two-step Python one are the same pipeline::

        st = genomes.simulate_genomes_family(sp, ..., stream_to="run/")
        sequences.simulate_sequences(st, model=hky85(), length=1000, seed=1)   # reads it back

    The ``seed`` of a reopened run is the seed recorded on the handle when there is one, and ``None``
    from a bare directory: the files are the run, and a number that did not produce them would be a
    lie. It is in the run's own ``run.zombi2`` / ``genomes.log`` either way.
    """
    seed = getattr(directory, "seed", None)                 # a StreamedRun carries its own
    directory = getattr(directory, "directory", directory)
    directory = os.fspath(directory)
    handoff = _resolve(directory)
    if os.path.exists(os.path.join(handoff, "blocks.tsv")):
        raise NotImplementedError(
            f"{handoff} is a nucleotide genome run (it has blocks.tsv), which reads back through "
            f"zombi2.genomes.nucleotide.read_nucleotide_genomes(directory, tree) — it needs the "
            f"blocks as well as the events, so it takes the tree explicitly.")
    tree = _tree(handoff, directory)
    with open(os.path.join(handoff, "genome_events.tsv"), encoding="utf-8") as f:
        edges = edges_from_tsv(f.read())
    initial: tuple[GeneCopy, ...] = ()
    initial_path = os.path.join(handoff, "initial_genome.tsv")
    if os.path.exists(initial_path):
        with open(initial_path, encoding="utf-8") as f:
            rows = [line.rstrip("\n").split("\t") for line in f if line.strip()]
        initial = tuple(GeneCopy(gene_from_label(copy), int(family))
                        for family, copy in rows if family != "family")
    return FamilyGenomesResult(complete_tree=tree, node_genomes=_genomes(handoff, tree), edges=edges,
                               seed=seed, initial_genome=initial)

Gene trees

zombi2.genomes.GeneTree dataclass

GeneTree(family: int, complete: GeneNode, origination: float)

One gene family's true genealogy. complete is the whole tree (lost and extinct-species lineages included); extant is it pruned to the genes surviving at the extant tips (degree-two nodes suppressed), or None if the family left no extant gene. to_newick serialises either.

origination is when the family was founded — the exact time of its origination event, or the root lineage's start for a family declared by initial_families. A GeneNode records when it ended, so this is the one time the tree cannot derive: it is where the root's branch begins.

to_newick

to_newick(which: str = 'extant', *, annotate: bool = True, names: 'dict[int, str] | None' = None, labels: str = 'events') -> str | None

Newick of the "extant" (default) or "complete" tree; None if it is empty. Leaves are n<species>_g<copy> — the copy and the branch it sits on, the same name the alignment FASTA records and the homology tables use, so a tip needs no translation to say which genome it came from. With annotate internal nodes carry <kind>_n<species>; branch lengths are time differences.

names is the run's node names (Tree.labels()), which is how a tree writes e<species> for a gene sitting on a lineage that died. The extant tree needs them too, which is not obvious: its leaves are all in living species, but a transfer node sits on the donor's branch, and a transfer out of a lineage that later died survives into the extant tree whenever the copy that moved has extant descendants. Without names that node was written n<species> for a species the extant tree does not contain and the complete tree calls e<species> — a label that resolved in neither file, on exactly the transfers whose donor is invisible.

labels picks what internal nodes are called. "events" (default) is the annotated form above. "copies" names every internal node the way the leaves are named, n<species>_g<copy> — the key the alignment and ancestral tables use — so a per-copy quantity (an ancestral sequence, a composition) can be attached to every branch of the tree, not only its tips. Event kinds are then not in the labels; they remain on the GeneNode objects.

The root carries one too, running from origination to where the root gene ended — the stem of the family, real time in which that founding gene existed. On the extant tree the root may be a node whose ancestors were suppressed; its branch still starts at origination and so absorbs them, exactly as the species tree's extant root absorbs its own.

Source code in zombi2/genomes/gene_trees.py
def to_newick(self, which: str = "extant", *, annotate: bool = True,
              names: "dict[int, str] | None" = None,
              labels: str = "events") -> str | None:
    """Newick of the ``"extant"`` (default) or ``"complete"`` tree; ``None`` if it is empty.
    Leaves are ``n<species>_g<copy>`` — the copy and the branch it sits on, the same name the
    alignment FASTA records and the homology tables use, so a tip needs no translation to say
    which genome it came from. With ``annotate`` internal nodes carry ``<kind>_n<species>``;
    branch lengths are time differences.

    ``names`` is the run's node names (`Tree.labels()`), which is how a tree writes
    ``e<species>`` for a gene sitting on a lineage that died. The **extant** tree needs them too,
    which is not obvious: its *leaves* are all in living species, but a transfer node sits on the
    **donor's** branch, and a transfer out of a lineage that later died survives into the extant
    tree whenever the copy that moved has extant descendants. Without ``names`` that node was
    written ``n<species>`` for a species the extant tree does not contain and the complete tree
    calls ``e<species>`` — a label that resolved in neither file, on exactly the transfers whose
    donor is invisible.

    ``labels`` picks what internal nodes are called. ``"events"`` (default) is the
    annotated form above. ``"copies"`` names every internal node the way the leaves are
    named, ``n<species>_g<copy>`` — the key the alignment and ancestral tables use — so a
    per-copy quantity (an ancestral sequence, a composition) can be attached to every
    branch of the tree, not only its tips. Event kinds are then not in the labels; they
    remain on the `GeneNode` objects.

    The root carries one too, running from ``origination`` to where the root gene ended — the
    stem of the family, real time in which that founding gene existed. On the extant tree the
    root may be a node whose ancestors were suppressed; its branch still starts at ``origination``
    and so absorbs them, exactly as the species tree's extant root absorbs its own."""
    if labels not in ("events", "copies"):
        raise ValueError(f"labels must be 'events' or 'copies', not {labels!r}")
    root = self.extant if which == "extant" else self.complete
    if root is None:
        return None
    return _to_newick(root, annotate, self.origination, names, labels) + ";"

zombi2.genomes.GeneCopy dataclass

GeneCopy(id: int, family: int)

One gene copy: a member of family family, identified by a globally-unique id. Its birth/death times and parentage live in the event log (the source of truth); the copy carries only what a genome snapshot needs to be self-describing — who it is and which family it is in. A genome may hold several copies sharing a family (that family's copy count).

Who receives a transfer

transfer_to chooses who receives a horizontal transfer. It redistributes the transfers without changing how many happen. A weight of 0 means "cannot receive", and when every candidate weighs 0 the transfer does not fire. The numbers are weights, not rate multipliers, so a driven rule is written from its own entry point, transfer_to = Recipients().weighted_by(driver, mapping), with no base in front of it — a rate written there is an error.

zombi2.genomes.Distance dataclass

Distance(decay: float = 1.0)

A transfer_to weighting by relatedness: a recipient at patristic distance d from the donor gets weight exp(-decay × d / depth), where depth is the tree's mean root-to-tip time — so decay is scale-free (in units of tree depth), meaning the same across trees of different absolute timescales. transfer_to="distance" is Distance(decay=1.0).

zombi2.genomes.Clades dataclass

Clades(groups: dict, between: object)

A transfer_to weighting by named clades — the topological, donor-conditioned sibling of Distance. Each group is a clade of the species tree, and a Between kernel weights a candidate recipient by the pair (donor's clade, recipient's clade), so a transfer can be steered to run between two clades rather than within them — which the per-recipient weight of a Driven cannot express::

transfer_to = Clades({"A": ["n12", "n27"], "B": 40},
                     Between({("A", "B"): 1.0, ("B", "A"): 1.0}, default=0.0))

A clade is named either by a set of tips (a list — the clade is the subtree below their MRCA) or by a single node id (an int, or an "n<id>" label — the clade is that node's whole subtree). Groups must be disjoint; a lineage in none of them is in the implicit group "rest", usable as a kernel key. Membership is read from the tree (a clade is a fact about the tree, not another level), so this is a topological rule like "distance", resolved once per run — not a weighted_by reading another level, and needing no driver file.

zombi2.genomes.Between

Between(per_pair, default: float = 1.0)

A weight over ordered (donor-group, recipient-group) pairs — the 2-D kernel of the transfer choice of who receives (SPEC §5), the donor-conditioned sibling of Table::

Between({("A", "B"): 1.0, ("B", "A"): 1.0}, default=0.0)   # A↔B only, nothing else receives
Between({("A", "B"): 3.0})                                 # A→B 3× baseline, every other pair 1×

A Table weights a candidate recipient by that candidate's state alone; a Between weights it by the pair — the donor's group and the recipient's — which is what lets a transfer be steered to run between two groups rather than within them. It is therefore not a Mapping (a Mapping.multiplier reads one value): its weight() reads two, and the engine passes both. It is used in transfer_to — on its own as the kernel of a Clades rule (groups from the tree), or as the mapping of a Recipients().weighted_by(...) (groups from a trait). It is not a rate multiplier: a rate has no donor to condition on, so a Between on a rate is refused.

Keys are (from_group, to_group) pairs matched by string form, exactly like Table's states, so an integer-labelled group still finds its entry. default (1.0) is the weight for any pair not named — default=0.0 gives the "only the flows I name can happen" idiom, reusing the rule that a weight of 0 means the donor cannot send to that recipient group; when every candidate weighs 0 the transfer has nowhere to land and does not fire.

Source code in zombi2/params/mapping.py
def __init__(self, per_pair, default: float = 1.0) -> None:
    if not isinstance(per_pair, dict) or not per_pair:
        raise ValueError(
            f"Between needs a non-empty {{(from_group, to_group): weight}} dict, got {per_pair!r}")
    table: dict[tuple[str, str], float] = {}
    for pair, weight in per_pair.items():
        if not (isinstance(pair, tuple) and len(pair) == 2):
            raise ValueError(
                f"Between keys are (from_group, to_group) pairs, got {pair!r} — write "
                f"Between({{('A', 'B'): 1.0}}), the donor group first, the recipient group second")
        key = (str(pair[0]), str(pair[1]))  # groups matched by string form, like Table's states
        if key in table:
            raise ValueError(
                f"Between pairs collide as strings: {pair!r} and an earlier key both map to {key!r}")
        table[key] = _check_factor(weight, f"Between weight for {pair!r}")
    self.per_pair = table
    self.default = _check_factor(default, "Between default")

weight

weight(from_group: object, to_group: object) -> float

The weight for a transfer from a from_group donor to a to_group recipient — the named pair's weight, or default if the pair is unnamed.

Source code in zombi2/params/mapping.py
def weight(self, from_group: object, to_group: object) -> float:
    """The weight for a transfer from a ``from_group`` donor to a ``to_group`` recipient — the
    named pair's weight, or `default` if the pair is unnamed."""
    return self.per_pair.get((str(from_group), str(to_group)), self.default)

groups

groups() -> set

Every group named on either side of a pair — what a fires-check tests against the groups that actually occur, so a kernel naming only absent groups (a typo) can be caught.

Source code in zombi2/params/mapping.py
def groups(self) -> set:
    """Every group named on either side of a pair — what a fires-check tests against the groups
    that actually occur, so a kernel naming only absent groups (a typo) can be caught."""
    return {g for pair in self.per_pair for g in pair}