Skip to content

Commit

Permalink
Update return value of conditional models. (#1163)
Browse files Browse the repository at this point in the history
* mutation_index is now Optional[int]
* added fixation_index, which is Optional[int]
* Added tests of prune_selected = True
* Update vignettes
* Add auto-attribute docs for a population's
  fixation and fixation time properties

Closes #1160
  • Loading branch information
molpopgen authored Aug 16, 2023
1 parent 001f088 commit bfe0236
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 65 deletions.
8 changes: 8 additions & 0 deletions doc/misc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Breaking changes
PR {pr}`1161`.
Issue {issue}`934`.

Fixes

* {class}`fwdpy11.conditional_models.ConditionalModelOutput` now contains
fields allowing one to distinguish if the tracked mutation is still present
in {attr}`fwdpy11.DiploidPopulation.mutations` and/or {attr}`fwdpy11.DiploidPopulation.fixations`
PR {pr}`1163`.
Issue {issue}`1160`.

New features

* {func}`fwdpy11.DiploidPopulation.create_from_tskit` is now able to restore
Expand Down
2 changes: 2 additions & 0 deletions doc/pages/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ kernelspec:
.. autoattribute:: fwdpy11.DiploidPopulation.generation
.. autoattribute:: fwdpy11.DiploidPopulation.mutations
.. autoattribute:: fwdpy11.DiploidPopulation.fixations
.. autoattribute:: fwdpy11.DiploidPopulation.fixation_times
.. autoattribute:: fwdpy11.DiploidPopulation.haploid_genomes
.. autoattribute:: fwdpy11.DiploidPopulation.genetic_values
.. autoattribute:: fwdpy11.DiploidPopulation.ancient_sample_genetic_values
Expand Down
16 changes: 10 additions & 6 deletions doc/short_vignettes/trackmutationfates.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ output = fwdpy11.conditional_models.track_added_mutation(
When tracking deleterious variants, it is unlikely that they will be around at the end of the simulation:

```{code-cell} python
try:
if output.mutation_index is not None:
print(output.pop.mutations[output.mutation_index])
except IndexError as _:
print(f"mutation {output.mutation_index} is no longer in the population!")
elif output.fixation_index is not None:
print(output.pop.fixations[output.fixation_index])
else:
print("Our mutation is no longer in the population!")
```

### Recording all generations of the mutation's sojourn
Expand Down Expand Up @@ -131,10 +133,12 @@ Now, our mutation is present in nodes in our tree sequence.
Let's try to print it again:

```{code-cell} python
try:
if output.mutation_index is not None:
print(output.pop.mutations[output.mutation_index])
except IndexError as _:
output.mutation_index(f"mutation {output.mutation_index} is no longer in the population!")
elif output.fixation_index is not None:
print(output.pop.fixations[output.fixation_index])
else:
print("Our mutation is no longer in the population!")
```

Let's track this variant's frequency at each time point:
Expand Down
4 changes: 3 additions & 1 deletion fwdpy11/conditional_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ class ConditionalModelOutput:
"""The population with the added mutation"""
params: fwdpy11.ModelParams
"""The evolved model parameters"""
mutation_index: int
mutation_index: typing.Optional[int]
"""The index of the new mutation in pop.mutations"""
fixation_index: typing.Optional[int]
"""The index of the new mutation in pop.fixations"""
num_descendant_nodes: int
"""The number of alive nodes initially containing the new mutation"""

Expand Down
15 changes: 14 additions & 1 deletion fwdpy11/conditional_models/_track_added_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ def _track_added_mutation(
_sampling_policy,
)

tracked_mutation_key = pcopy.mutations[idx].key

internal_options = _InternalSweepEvolveOptions()

finished = False
Expand Down Expand Up @@ -422,9 +424,20 @@ def _track_added_mutation(

assert pop_to_return is not None

if idx < len(pop_to_return.mutations):
mutation_index = idx
else:
mutation_index = None
if mutation_index is not None:
assert pop_to_return.mutations[mutation_index].key == tracked_mutation_key
fixation_index = None
for i, f in enumerate(pop_to_return.fixations):
if f.key == tracked_mutation_key:
fixation_index = i
return ConditionalModelOutput(
pop=pop_to_return,
params=local_params,
mutation_index=idx,
mutation_index=mutation_index,
fixation_index=fixation_index,
num_descendant_nodes=ndescendants,
)
Loading

0 comments on commit bfe0236

Please sign in to comment.