Skip to content

Commit 1f0d8c8

Browse files
authored
doc: start book sections on tree sequences (#381)
1 parent a13f791 commit 1f0d8c8

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

book/src/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
- [Accessing table columns](./table_collection_column_access.md)
99
- [Accessing table rows](./table_collection_row_access.md)
1010
- [Validating table collection contents](./table_collection_validation.md)
11+
12+
- [Working with tree sequences](./working_with_tree_sequences.md)
13+
- [Initialization from a table collection](./tree_sequence_from_table_collection.md)
14+
- [Iterating over trees](./tree_sequence_iterate_trees.md)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Initialization from a table collection
2+
3+
The steps are:
4+
5+
* Add rows to a table
6+
* Sort the table
7+
* Index the table
8+
* Create the tree sequence
9+
10+
For brevity, we skip careful pattern matching of return values
11+
and instead just unwrap them.
12+
13+
### Adding rows
14+
15+
```rust, noplaygound, ignore
16+
{{#include ../../tests/book_trees.rs:build_tables}}
17+
```
18+
19+
### Sorting
20+
21+
```rust, noplaygound, ignore
22+
{{#include ../../tests/book_trees.rs:sort_tables}}
23+
```
24+
25+
See the [API docs](https://docs.rs) for the details of sorting.
26+
The behavior of this function can be confusing at first.
27+
Only tables with strict sorting requirements are affected.
28+
29+
### Indexing
30+
31+
```rust, noplaygound, ignore
32+
{{#include ../../tests/book_trees.rs:index_tables}}
33+
```
34+
35+
### Create the tree sequence
36+
37+
```rust, noplaygound, ignore
38+
{{#include ../../tests/book_trees.rs:create_tree_sequence}}
39+
```
40+
41+
Notes:
42+
43+
* We could have skipped `tables.build_index()` and passed `TreeSquenceFlags::BUILD_INDEXES` instead of the default flags.
44+
* Creating a tree sequence from a table collection takes ownership of the table data and consumes the table collection variable.
45+
Any further attempts to manipulate the table collection variable will result in a compiler error.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Iterating over trees
2+
3+
To iterate over trees left-to-right:
4+
5+
```rust, noplaygound, ignore
6+
{{#include ../../tests/book_trees.rs:iterate_trees}}
7+
```
8+
9+
This API depends on traits most easily brought into scope via the crate prelude:
10+
11+
```rust, noplayground, ignore
12+
use tskit::prelude::*;
13+
```
14+
15+
A `next_back()` function allows iteration to the next tree left of the current tree.
16+
We currently do not have an API expressing "build me an iterator starting from the rightmost tree".
17+
Such an thing is certainly doable.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Working with tree sequences <img align="right" width="73" height="45" src="https://raw.githubusercontent.com/tskit-dev/administrative/main/logos/svg/tskit-rust/Tskit_rust_logo.eps.svg">
2+
3+
The next several sections provide examples based on a tree sequence with two trees.
4+
5+
The first tree is:
6+
7+
0
8+
+++
9+
| | 1
10+
| | +++
11+
2 3 4 5
12+
13+
The second tree is:
14+
15+
0
16+
+-+-+
17+
1 |
18+
+-+-+ |
19+
2 4 5 3
20+
21+

tests/book_trees.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#[test]
2+
fn initialize_from_table_collection() {
3+
// ANCHOR: build_tables
4+
use tskit::prelude::*;
5+
use tskit::TableCollection;
6+
use tskit::TableSortOptions;
7+
use tskit::TreeFlags;
8+
use tskit::TreeSequenceFlags;
9+
10+
let mut tables = TableCollection::new(1000.).unwrap();
11+
tables
12+
.add_node(0, 2.0, PopulationId::NULL, IndividualId::NULL)
13+
.unwrap();
14+
tables
15+
.add_node(0, 1.0, PopulationId::NULL, IndividualId::NULL)
16+
.unwrap();
17+
tables
18+
.add_node(
19+
TSK_NODE_IS_SAMPLE,
20+
0.0,
21+
PopulationId::NULL,
22+
IndividualId::NULL,
23+
)
24+
.unwrap();
25+
tables
26+
.add_node(
27+
TSK_NODE_IS_SAMPLE,
28+
0.0,
29+
PopulationId::NULL,
30+
IndividualId::NULL,
31+
)
32+
.unwrap();
33+
tables
34+
.add_node(
35+
TSK_NODE_IS_SAMPLE,
36+
0.0,
37+
PopulationId::NULL,
38+
IndividualId::NULL,
39+
)
40+
.unwrap();
41+
tables
42+
.add_node(
43+
TSK_NODE_IS_SAMPLE,
44+
0.0,
45+
PopulationId::NULL,
46+
IndividualId::NULL,
47+
)
48+
.unwrap();
49+
tables.add_edge(500., 1000., 0, 1).unwrap();
50+
tables.add_edge(0., 500., 0, 2).unwrap();
51+
tables.add_edge(0., 1000., 0, 3).unwrap();
52+
tables.add_edge(500., 1000., 1, 2).unwrap();
53+
tables.add_edge(0., 1000., 1, 4).unwrap();
54+
tables.add_edge(0., 1000., 1, 5).unwrap();
55+
// ANCHOR_END: build_tables
56+
57+
// ANCHOR: sort_tables
58+
tables.full_sort(TableSortOptions::default()).unwrap();
59+
// ANCHOR_END: sort_tables
60+
61+
// ANCHOR: index_tables
62+
tables.build_index().unwrap();
63+
// ANCHOR_END: index_tables
64+
65+
// ANCHOR: create_tree_sequence
66+
let treeseq = tables.tree_sequence(TreeSequenceFlags::default()).unwrap();
67+
// ANCHOR_END: create_tree_sequence
68+
69+
// ANCHOR: iterate_trees
70+
let mut tree_iterator = treeseq.tree_iterator(TreeFlags::default()).unwrap();
71+
72+
while let Some(_tree) = tree_iterator.next() {
73+
// _tree is a tskit::Tree
74+
}
75+
// ANCHOR_END: iterate_trees
76+
}

0 commit comments

Comments
 (0)