Repository navigation
Feat:/simulate after local mrca #2396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jeromekelleher
merged 4 commits into
tskit-dev:main
from
hossam26644:feat-simulate-after-local-mrca
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1129,6 +1129,7 @@ However, if `coalescing_segments_only=False`, then the edges recorded | |
| would be from `m` to `p` over the entire segment `[a, b)`, and from `n` to `p` over the entire segment `[c,d)`. | ||
| The nodes `m` and `n` coalesce (in `p`) on only the overlapping segment `[c,b)`, and so the node `p` will be a unary node in the flanking regions: above `m` on the segment `[a,c)` and above `n` on the segment `[b,d)`. | ||
|
|
||
|
|
||
| (sec_additional_nodes_ca)= | ||
|
|
||
| ### Common ancestor events | ||
|
|
@@ -1480,6 +1481,38 @@ Migrations nodes are also recorded in the ARG using the | |
| `NodeType.MIGRANT` flag. See the {ref}`sec_api_node_flags` | ||
| section for more details. | ||
|
|
||
| (sec_ancestry_stop_at_local_mrca)= | ||
|
|
||
| ### Simulating ancestral to local MRCAs | ||
|
|
||
| By default, msprime stops simulating local trees when a local most recent | ||
| common ancestor (MRCA) is found. This is because events that occur above the | ||
| common ancestor are shared across all samples, and we are usually interested in | ||
| differences between samples. However, it is sometime useful to simulate ancestry | ||
| across the full span of the region until **all** local MRCAs have been reached. | ||
| To do this, we set the parameter `stop_at_local_mrca` of | ||
| {func}`.sim_ancestry` to `False`. | ||
|
|
||
| ```{code-cell} | ||
| ts = msprime.sim_ancestry( | ||
| 2, recombination_rate=0.1, sequence_length=10, random_seed=75, | ||
| stop_at_local_mrca=False, record_full_arg=True) | ||
| ts.draw_svg(time_scale='rank') | ||
| ``` | ||
|
|
||
| In this example we've continued the simulation *after* we've found roots for the local | ||
| trees, and we can see, for example, that node 14 is still the ancestor of 11 | ||
| in the rightmost tree. This would be not recorded with the default ``stop_at_local_mrca=False`` | ||
| because 11 is the root of that tree. | ||
|
|
||
| The stopping condition remains the same---we keep simulating until an MRCA has been | ||
| found at *all* local trees. The interpretation of the the final "root" nodes | ||
| across the trees is a little subtle, and follows the same logic as used when | ||
| {ref}`sec_ancestry_end_time`. All of the trees have a root node with time | ||
| equal to the oldest MRCA, and the nodes correspond to different lineages present | ||
| in the simulation. | ||
|
|
||
|
|
||
| ## Manipulating simulation time | ||
|
|
||
| (sec_ancestry_end_time)= | ||
|
|
@@ -1493,7 +1526,7 @@ for two diploid samples: | |
|
|
||
| ```{code-cell} | ||
| ts = msprime.sim_ancestry(2, sequence_length=10, recombination_rate=0.1, random_seed=42) | ||
| SVG(ts.draw_svg(y_axis=True, y_ticks=[0, 1, 2, 3, 4, 5])) | ||
| SVG(ts.draw_svg(y_axis=True, y_ticks=[0, 1, 2, 3, 4])) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be in a separate commit or is fine here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, good point, this should be a separate commit. |
||
| ``` | ||
|
|
||
| Sometimes we would like to stop the simulation early, **before** complete | ||
|
|
@@ -2780,6 +2813,39 @@ Because the input pedigree fully describes the simulation many features such as | |
| combined with other {ref}`ancestry models<sec_ancestry_models>`. | ||
| ::: | ||
|
|
||
| (sec_ancestry_models_fixed_pedigree_tracing)= | ||
|
|
||
| #### Tracing ancestry through a pedigree | ||
|
|
||
| The previous example generated trees that are embedded within the pedigree, | ||
| but doesn't tell us directly the exact path that each sample's genetic | ||
| material took through the ancestors. To trace these paths exactly, we can | ||
| use a combination of the ``additional_nodes``, | ||
| ``coalescing_segments_only`` | ||
| (see {ref}`sec_ancestry_additional_nodes`), | ||
| and ``stop_at_local_mrca`` (see {ref}`sec_ancestry_stop_at_local_mrca`). | ||
|
|
||
| ```{code-cell} | ||
| ped_ts = msprime.sim_ancestry( | ||
| initial_state=pedigree, | ||
| model="fixed_pedigree", | ||
| random_seed=41, | ||
| recombination_rate=0.001, | ||
| coalescing_segments_only=False, | ||
| additional_nodes=( | ||
| msprime.NodeType.COMMON_ANCESTOR | | ||
| msprime.NodeType.PASS_THROUGH), | ||
| stop_at_local_mrca=True | ||
| ) | ||
| node_labels = {node.id: f"{node.individual}({node.id})" for node in ped_ts.nodes()} | ||
| SVG(ped_ts.draw_svg(y_axis=True, node_labels=node_labels, size=(600,200))) | ||
| ``` | ||
|
|
||
| We can now see that the ancestor of node 15 is 20, which we would have to | ||
| infer in the previous example. | ||
|
|
||
|
|
||
|
|
||
| #### Censoring pedigree information | ||
|
|
||
| :::{warning} | ||
|
|
@@ -2883,6 +2949,7 @@ when calculating population sizes. Thus, the pedigree must be seen as | |
| entirely **external** to the population model simulated during recapitation. | ||
| ::: | ||
|
|
||
|
|
||
| (sec_ancestry_models_fixed_pedigree_demography)= | ||
|
|
||
| #### Pedigrees and demography | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why wasdtwf excluded from verify overlaps? why is it included in the new version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question - it was incorrectly omitted before, and it means that we didn't pick up the basic logic error in the multi-merger case (subtracting min_overlap, instead of len(h)). We also weren't testing this code path at all.