genetic_presets Module
Built-in genetic presets and preset application helpers.
Overview
The genetic_presets module provides reusable preset abstractions and concrete
implementations (for example, gene drives) that can inject gamete modifiers,
zygote modifiers, and fitness patches into populations.
Complete Module Reference
natal.genetic_presets
Genetic presets for mutations, gene drives, and allele conversions.
This module provides a framework for defining reusable genetic modifications including: - Gene drives (e.g., CRISPR/Cas9 homing drives) - General mutations (point mutations, insertions, deletions) - Allele conversion rules and fitness effects - Complex genetic constructions
Each preset can modify population genetics through three mechanisms: - gamete_modifier: modifies gamete frequencies during gametogenesis - zygote_modifier: modifies embryo genotypes after fertilization - fitness_patch: declarative specification of viability/fecundity/sexual selection effects
GeneticPreset
Bases: ABC
Abstract base for genetic modification presets including gene drives, mutations, and allele conversions.
A preset bundles gamete modifiers, zygote modifiers, and fitness effects that form a cohesive genetic system. This can include: - Gene drives (e.g., CRISPR/Cas9 homing drives) - General mutations (point mutations, insertions, deletions) - Complex allele conversion systems
Presets should implement
- gamete_modifier(): returns GameteModifier callable or None
- zygote_modifier(): returns ZygoteModifier callable or None
- fitness_patch(): returns declarative fitness configuration dict or None
All methods are optional (can return None). At least one method should be implemented for the preset to have any effect.
Examples:
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable preset name. |
hook_id |
Optional[int]
|
Optional identifier used when registering modifiers. |
Initialize the preset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Optional human-readable name for the preset. |
''
|
species
|
Optional[Species]
|
Optional species bound at construction time. If provided, applying this preset to a population with a different species will raise an error. |
None
|
Source code in src/natal/genetic_presets.py
bind_species
Bind this preset instance to a concrete species.
This enables delayed species injection: users can construct presets without passing species, and binding happens automatically when the preset is applied to a population.
Source code in src/natal/genetic_presets.py
gamete_modifier
abstractmethod
Return a gamete modifier or None.
The modifier should return:
Dict[(sex_idx, genotype_idx) -> Dict[compressed_hg_glab_idx -> freq]]
where compressed_hg_glab_idx is an integer index into the compressed haploid genotype space.
Source code in src/natal/genetic_presets.py
zygote_modifier
abstractmethod
Return a zygote modifier or None.
The modifier should return:
Dict[(c1, c2) -> (idx_modified | Genotype | Dict[idx -> prob])]
where c1, c2 are compressed coordinate pairs representing the parental diploid genotypes.
Source code in src/natal/genetic_presets.py
fitness_patch
Return declarative fitness patch.
Returns:
| Type | Description |
|---|---|
Optional[PresetFitnessPatch]
|
Fitness patch from custom function if set, otherwise None. |
Optional[PresetFitnessPatch]
|
Subclasses should override this method for built-in behavior. |
Source code in src/natal/genetic_presets.py
with_fitness_patch
Set a custom fitness patch function and return self for chaining.
This allows dynamic modification of fitness effects at runtime without subclassing, using a fluent interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patch_func
|
Callable[[], Optional[PresetFitnessPatch]]
|
Callable that returns a PresetFitnessPatch or None. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining. |
Example
preset = (HomingDrive(...) ... .with_fitness_patch(lambda: { ... 'viability_allele': {'Drive': (0.8, 'dominant')} ... })) population.apply_preset(preset)
Also works with complex custom logic
def conditional_patch(): ... if some_condition: ... return {'fecundity_allele': {'Mut': (0.5, 'recessive')}} ... return None
preset = HomingDrive(...).with_fitness_patch(conditional_patch)
Note
This overrides any fitness patch defined in subclasses. To preserve subclass behavior while adding modifications, subclass and call super().fitness_patch() instead.
Source code in src/natal/genetic_presets.py
clear_fitness_patch
Remove any custom fitness patch, restoring default behavior.
Returns:
| Type | Description |
|---|---|
GeneticPreset
|
Self for method chaining. |
apply
Register this preset onto a population (DEPRECATED).
.. deprecated:: Use population.apply_preset(preset) instead. This method is kept for backwards compatibility and may be removed in future versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
BasePopulation[Any]
|
The BasePopulation instance to modify. |
required |
See Also
:meth:natal.base_population.BasePopulation.apply_preset - Preferred modern API
Source code in src/natal/genetic_presets.py
HomingDrive
HomingDrive(name: str, drive_allele: _AlleleSpecifier, target_allele: _AlleleSpecifier, resistance_allele: Optional[_AlleleSpecifier] = None, functional_resistance_allele: Optional[_AlleleSpecifier] = None, cas9_allele: Optional[_AlleleSpecifier] = None, drive_conversion_rate: _SexSpecificRates = 0.5, late_germline_resistance_formation_rate: _SexSpecificRates = 0.0, embryo_resistance_formation_rate: _SexSpecificRates = 0.0, functional_resistance_ratio: float = 0.0, fecundity_scaling: _FecundityScalingConfig = 1.0, viability_scaling: _ViabilityScalingConfig = 1.0, sexual_selection_scaling: _SexualSelectionScalingConfig = 1.0, zygote_viability_scaling: _ZygoteViabilityScalingConfig = 1.0, viability_mode: _AlleleScalingMode = 'multiplicative', fecundity_mode: _AlleleScalingMode = 'multiplicative', sexual_selection_mode: _AlleleScalingMode = 'multiplicative', zygote_viability_mode: _AlleleScalingMode = 'multiplicative', cas9_deposition_glab: Optional[str] = None, species: Optional[Species] = None, use_paternal_deposition: bool = False)
Bases: GeneticPreset
Homing-based gene drive (e.g., CRISPR/Cas9 homing drives).
This preset implements a homing gene drive that spreads through homology-directed repair (HDR) converting wild-type alleles into drive alleles in heterozygotes. It can also generate resistance alleles through non-homologous end joining (NHEJ).
Key features include drive conversion in heterozygotes, germline/embryo resistance formation, optional parental Cas9 deposition, and sex-specific rate control.
The drive operates through a sequential cascade: 1. Homing conversion (WT -> Drive) 2. Resistance formation in remaining WT alleles 3. Optional functional resistance split
Attributes:
| Name | Type | Description |
|---|---|---|
drive_conversion_rate |
Tuple[float, float]
|
Female/male homing rates. |
late_germline_resistance_formation_rate |
Tuple[float, float]
|
Female/male late germline resistance rates. |
embryo_resistance_formation_rate |
Tuple[float, float]
|
Female/male embryo resistance rates. |
Examples:
drive = HomingDrive( name="MyDrive", drive_allele="Drive", target_allele="WT", resistance_allele="Resistance", drive_conversion_rate=0.95, late_germline_resistance_formation_rate=0.03 ) population.apply_preset(drive)
Initialize a homing-based gene drive (e.g., CRISPR/Cas9 homing drives).
This drive spreads via homology-directed repair (HDR) converting wild-type alleles into drive alleles in heterozygotes. It can also generate resistance alleles through non-homologous end joining (NHEJ).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the gene drive. |
required |
drive_allele
|
str or Gene
|
The allele carrying the drive cassette. |
required |
target_allele
|
str or Gene
|
The wild-type allele targeted by the drive. |
required |
resistance_allele
|
str or Gene
|
The non-functional resistance allele formed by NHEJ. |
None
|
functional_resistance_allele
|
str or Gene
|
The functional resistance allele formed by in-frame NHEJ. If not provided, assume no functional resistance. |
None
|
cas9_allele
|
str or Gene
|
The allele carrying Cas9 for cleavage, used when modeling a split drive where Cas9 is separate from the drive locus. |
None
|
drive_conversion_rate
|
float or dict
|
Probability of drive conversion caused by Cas9 cleavage and homology-directed repair in heterozygotes. Can be a single float (applies to both sexes), a dict with sex keys, or a tuple (female_rate, male_rate) for sex-specific rates. |
0.5
|
late_germline_resistance_formation_rate
|
float or dict
|
Probability of resistance formation after drive conversion in the germline. Can be a single float (applies to both sexes), a dict with sex keys, or a tuple (female_rate, male_rate) for sex-specific rates. |
0.0
|
embryo_resistance_formation_rate
|
float or dict
|
Probability of resistance formation in embryos due to maternal/paternal Cas9 deposition. Can be a single float, dict, or tuple. |
0.0
|
functional_resistance_ratio
|
float
|
Proportion of resistance alleles that are functional (in-frame mutations). Range: 0.0 (all non-functional) to 1.0 (all functional). |
0.0
|
fecundity_scaling
|
float or dict
|
Fitness multiplier for drive carriers affecting fecundity. Applied multiplicatively based on allele copy number. |
1.0
|
viability_scaling
|
float or dict
|
Fitness multiplier for drive carriers affecting viability. Applied multiplicatively based on allele copy number. |
1.0
|
sexual_selection_scaling
|
float or tuple
|
Fitness multiplier affecting sexual selection. Can be a single float or tuple (default_selection, carrier_selection). |
1.0
|
zygote_viability_scaling
|
float or dict
|
Fitness multiplier affecting survival of zygotes before competition takes place. Applied multiplicatively based on allele copy number. |
1.0
|
viability_mode
|
str
|
Scaling mode: "multiplicative", "dominant", "recessive", or "custom". If "custom", scaling values must be tuples (het_val, hom_val). |
'multiplicative'
|
fecundity_mode
|
str
|
Scaling mode: "multiplicative", "dominant", "recessive", or "custom". If "custom", scaling values must be tuples (het_val, hom_val). |
'multiplicative'
|
sexual_selection_mode
|
str
|
Scaling mode for scalar sexual_selection_scaling. Note: if sexual_selection_scaling is a tuple, mode is ignored. |
'multiplicative'
|
zygote_viability_mode
|
str
|
Scaling mode: "multiplicative", "dominant", "recessive", or "custom". If "custom", scaling values must be tuples (het_val, hom_val). |
'multiplicative'
|
cas9_deposition_glab
|
str
|
Gamete label for Cas9 deposition tracking. Used for maternal/paternal effect modeling. |
None
|
species
|
Species
|
Species to bind at construction time. If None, will be bound when applied to population. |
None
|
use_paternal_deposition
|
bool
|
Whether to enable paternal Cas9 deposition. If True, fathers can deposit Cas9 in embryos. |
False
|
Examples:
>>> drive = HomingDrive(
... name="MyDrive",
... drive_allele="Drive",
... target_allele="WT",
... resistance_allele="R2",
... drive_conversion_rate=0.95,
... late_germline_resistance_formation_rate=0.03
... )
>>> population.apply_preset(drive)
Source code in src/natal/genetic_presets.py
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 | |
fitness_patch
Return declarative fitness patch for homing drive scaling configs.
Source code in src/natal/genetic_presets.py
gamete_modifier
Implement homing in heterozygous parents, germline resistance, and Cas9 deposition.
In heterozygotes (drive/wild-type), gametes are biased towards drive.
Source code in src/natal/genetic_presets.py
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 | |
zygote_modifier
Implement embryo resistance.
Cleavage in the embryo (due to deposited Cas9 or zygotic expression) converts wild-type alleles into resistance alleles.
Source code in src/natal/genetic_presets.py
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 | |
ToxinAntidoteDrive
ToxinAntidoteDrive(name: str, drive_allele: _AlleleSpecifier, target_allele: _AlleleSpecifier, disrupted_allele: _AlleleSpecifier, conversion_rate: _SexSpecificRates = 0.8, embryo_disruption_rate: _SexSpecificRates = 0.0, viability_scaling: _ViabilityScalingConfig = 1.0, fecundity_scaling: _FecundityScalingConfig = 1.0, sexual_selection_scaling: Optional[_SexualSelectionScalingConfig] = None, zygote_viability_scaling: _ZygoteViabilityScalingConfig = 0.0, viability_mode: _AlleleScalingMode = 'recessive', fecundity_mode: _AlleleScalingMode = 'recessive', sexual_selection_mode: _AlleleScalingMode = 'recessive', zygote_viability_mode: _AlleleScalingMode = 'recessive', cas9_deposition_glab: Optional[str] = None, species: Optional[Species] = None, use_paternal_deposition: bool = False)
Bases: GeneticPreset
Toxin-Antidote gene drive (e.g., TARE, TADE).
This preset implements a toxin-antidote gene drive system where a "drive" allele disrupts a "target" allele into a "disrupted" version. The "disrupted" allele typically carries a high fitness cost (the toxin effect), while the "drive" allele itself often provides a functional rescue (the antidote).
Key features include germline disruption, embryo disruption, and configurable fitness costs for the disrupted allele.
In a typical TARE (Toxin-Antidote Recessive Embryo lethality) configuration, the disrupted allele is set to be recessive lethal (viability_scaling=0.0, viability_mode="recessive").
Attributes:
| Name | Type | Description |
|---|---|---|
conversion_rate |
Tuple[float, float]
|
Female/male germline disruption rates. |
embryo_disruption_rate |
Tuple[float, float]
|
Female/male embryo disruption rates. |
viability_mode |
_AlleleScalingMode
|
Scaling mode for viability effects. |
fecundity_mode |
_AlleleScalingMode
|
Scaling mode for fecundity effects. |
Initialize a toxin-antidote gene drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the gene drive. |
required |
drive_allele
|
_AlleleSpecifier
|
The allele carrying the antidote and disruption machinery. |
required |
target_allele
|
_AlleleSpecifier
|
The wild-type allele targeted for disruption. |
required |
disrupted_allele
|
_AlleleSpecifier
|
The resulting non-functional/disrupted allele. |
required |
conversion_rate
|
_SexSpecificRates
|
Probability of target disruption in the germline. |
0.8
|
embryo_disruption_rate
|
_SexSpecificRates
|
Probability of target disruption in embryos. |
0.0
|
viability_scaling
|
_ViabilityScalingConfig
|
Fitness multiplier for the disrupted allele. |
1.0
|
fecundity_scaling
|
_FecundityScalingConfig
|
Fecundity multiplier for the disrupted allele. |
1.0
|
sexual_selection_scaling
|
Optional[_SexualSelectionScalingConfig]
|
Optional sexual-selection multiplier for the disrupted allele.
Supports a scalar copy-number effect or a tuple
|
None
|
zygote_viability_scaling
|
_ZygoteViabilityScalingConfig
|
Zygote viability scaling configuration for the disrupted allele. Applied during reproduction stage before survival; represents probability that a zygote survives to become an individual. |
0.0
|
viability_mode
|
_AlleleScalingMode
|
Scaling mode for viability (default "recessive"). |
'recessive'
|
fecundity_mode
|
_AlleleScalingMode
|
Scaling mode for fecundity (default "recessive"). |
'recessive'
|
sexual_selection_mode
|
_AlleleScalingMode
|
Scaling mode for scalar sexual-selection values.
Ignored when |
'recessive'
|
zygote_viability_mode
|
_AlleleScalingMode
|
Scaling mode for zygote viability (default "multiplicative"). |
'recessive'
|
cas9_deposition_glab
|
Optional[str]
|
Gamete label for Cas9 deposition tracking. |
None
|
species
|
Optional[Species]
|
Optional species to bind at construction. |
None
|
use_paternal_deposition
|
bool
|
Whether to enable paternal Cas9 deposition. |
False
|
Source code in src/natal/genetic_presets.py
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 | |
fitness_patch
Return declarative fitness patch for the disrupted allele.
Source code in src/natal/genetic_presets.py
gamete_modifier
Implement target disruption in the germline of drive carriers.
Source code in src/natal/genetic_presets.py
zygote_modifier
Implement target disruption in embryos.
Source code in src/natal/genetic_presets.py
apply_preset_to_population
Apply a genetic preset to a population by registering its modifiers and fitness effects.
This function handles the mechanical application of a preset to a population, including: 1. Species binding and validation 2. Registration of gamete modifiers 3. Registration of zygote modifiers 4. Application of fitness patches
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
BasePopulation[Any]
|
The BasePopulation instance to modify. |
required |
preset
|
GeneticPreset
|
The GeneticPreset instance to apply. |
required |
Note
This is typically called through the modern API:
population.apply_preset(preset)
The legacy API preset.apply(population) is deprecated but still supported.
Raises:
| Type | Description |
|---|---|
ValueError
|
If preset is bound to a different species than the population |
RuntimeError
|
If preset has no bound species |