Initialization from a table collection

The steps are:

  • Add rows to a table
  • Sort the table
  • Index the table
  • Create the tree sequence

For brevity, we skip careful pattern matching of return values and instead just unwrap them.

Adding rows

    use tskit::prelude::*;
    use tskit::TableCollection;
    use tskit::TableSortOptions;
    use tskit::TreeFlags;
    use tskit::TreeSequenceFlags;

    let mut tables = TableCollection::new(1000.).unwrap();
    tables
        .add_node(0, 2.0, PopulationId::NULL, IndividualId::NULL)
        .unwrap();
    tables
        .add_node(0, 1.0, PopulationId::NULL, IndividualId::NULL)
        .unwrap();
    tables
        .add_node(
            TSK_NODE_IS_SAMPLE,
            0.0,
            PopulationId::NULL,
            IndividualId::NULL,
        )
        .unwrap();
    tables
        .add_node(
            TSK_NODE_IS_SAMPLE,
            0.0,
            PopulationId::NULL,
            IndividualId::NULL,
        )
        .unwrap();
    tables
        .add_node(
            TSK_NODE_IS_SAMPLE,
            0.0,
            PopulationId::NULL,
            IndividualId::NULL,
        )
        .unwrap();
    tables
        .add_node(
            TSK_NODE_IS_SAMPLE,
            0.0,
            PopulationId::NULL,
            IndividualId::NULL,
        )
        .unwrap();
    tables.add_edge(500., 1000., 0, 1).unwrap();
    tables.add_edge(0., 500., 0, 2).unwrap();
    tables.add_edge(0., 1000., 0, 3).unwrap();
    tables.add_edge(500., 1000., 1, 2).unwrap();
    tables.add_edge(0., 1000., 1, 4).unwrap();
    tables.add_edge(0., 1000., 1, 5).unwrap();

Sorting

    tables.full_sort(TableSortOptions::default()).unwrap();

See the API docs for the details of sorting. The behavior of this function can be confusing at first. Only tables with strict sorting requirements are affected.

Indexing

    tables.build_index().unwrap();

Create the tree sequence

    let treeseq = tables.tree_sequence(TreeSequenceFlags::default()).unwrap();

Notes:

  • We could have skipped tables.build_index() and passed TreeSquenceFlags::BUILD_INDEXES instead of the default flags.
  • Creating a tree sequence from a table collection takes ownership of the table data and consumes the table collection variable. Any further attempts to manipulate the table collection variable will result in a compiler error.