How to use the tskit.NULL function in tskit

To help you get started, we’ve selected a few tskit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tskit-dev / tsinfer / tests / test_formats.py View on Github external
def test_from_tree_sequence_with_metadata(self):
        ts = get_example_individuals_ts_with_metadata(5, 2, 10)
        # Remove individuals
        tables = ts.dump_tables()
        tables.individuals.clear()
        tables.nodes.individual = np.full(
            ts.num_nodes, tskit.NULL, dtype=tables.nodes.individual.dtype
        )
        ts_no_individuals = tables.tree_sequence()
        sd1 = formats.SampleData(sequence_length=ts.sequence_length)
        self.verify_data_round_trip(ts_no_individuals, sd1)
        sd2 = formats.SampleData.from_tree_sequence(ts_no_individuals)
        self.assertTrue(sd1.data_equal(sd2))
github tskit-dev / tsinfer / tests / test_inference.py View on Github external
parent = edges[0].parent
            original_node = len(t1.nodes) + j
            self.assertEqual(
                tables.nodes.flags[original_node], tsinfer.NODE_IS_SAMPLE_ANCESTOR
            )
            # Most of the time the parent is the original node. However, in
            # simple cases it can be somewhere up the tree above it.
            if parent != original_node:
                for tree in final_ts.trees():
                    u = parent
                    while u != tskit.NULL:
                        siblings = tree.children(u)
                        if original_node in siblings:
                            break
                        u = tree.parent(u)
                    self.assertNotEqual(u, tskit.NULL)
github tskit-dev / msprime / tests / test_demography.py View on Github external
def verify_incomplete_tree_sequence(self, n, max_time, ts):
        self.assertEqual(ts.num_samples, n)
        time = ts.tables.nodes.time
        for tree in ts.trees():
            # Every sample with time <= max_time will end on a path
            # with time == max_time
            for u in tree.samples():
                if time[u] <= max_time:
                    while tree.parent(u) != tskit.NULL:
                        u = tree.parent(u)
                    self.assertEqual(ts.node(u).time, max_time)
                else:
                    self.assertEqual(tree.parent(u), tskit.NULL)
        max_roots = max(tree.num_roots for tree in ts.trees())
        self.assertGreater(max_roots, 1)
github tskit-dev / pyslim / tests / test_tree_sequence.py View on Github external
def verify_has_parents(self, ts):
        right_answer = np.repeat(True, ts.num_individuals)
        node_indivs = ts.tables.nodes.individual
        parent_ids = [set() for _ in ts.individuals()]
        node_parent_ids = [set() for _ in ts.nodes()]
        for t in ts.trees():
            for i in ts.individuals():
                if len(i.nodes) != 2:
                    right_answer[i.id] = False
                for n in i.nodes:
                    pn = t.parent(n)
                    if pn == tskit.NULL:
                        right_answer[i.id] = False
                    else:
                        p = node_indivs[t.parent(n)]
                        if p == tskit.NULL:
                            right_answer[i.id] = False
                        else:
                            ptime = ts.individual_times[p]
                            if ts.slim_provenance.model_type == "WF":
                                if i.time + 1 != ptime:
                                    right_answer[i.id] = False
                            else:
                                pdeath = ptime - ts.individual_ages[p]
                                if i.time + 1 < pdeath:
                                    right_answer[i.id] = False
                            parent_ids[i.id].add(p)
                            node_parent_ids[n].add(p)
github tskit-dev / tsinfer / tsinfer / formats.py View on Github external
IDs also added.
        :rtype: tuple(int, list(int))
        """
        self._check_build_mode()
        if self._build_state == self.ADDING_POPULATIONS:
            self._populations_writer.flush()
            self._populations_writer = None
            self._build_state = self.ADDING_SAMPLES
        if self._build_state != self.ADDING_SAMPLES:
            raise ValueError("Cannot add individuals after adding sites")

        time = np.float64(time).item()
        if not np.isfinite(time):
            raise ValueError("time must be a single finite number")
        if population is None:
            population = tskit.NULL
        if population >= self.num_populations:
            raise ValueError("population ID out of bounds")
        if ploidy <= 0:
            raise ValueError("Ploidy must be at least 1")
        if samples_metadata is not None and len(samples_metadata) != ploidy:
            raise ValueError(
                "If you specify samples_metadata, it must be a list of length `ploidy`"
            )
        if samples_metadata is None:
            samples_metadata = [None] * ploidy
        if location is None:
            location = []
        location = np.array(location, dtype=np.float64)
        individual_id = self._individuals_writer.add(
            metadata=self._check_metadata(metadata), location=location, time=time
        )
github tskit-dev / tsinfer / tsinfer / eval_util.py View on Github external
# and the sample we choose is on the other root branch.
                if mutation_node == sample:
                    continue
                if {mutation_node, sample} == set(tree.children(tree.root)):
                    continue
                # If the input probability is very high we can still get fixations
                # though by placing a mutation over every sample.
                if rng.random() < probability:
                    # If sample is a descendent of the mutation node we
                    # change the state to 0, otherwise change state to 1.
                    u = sample
                    while u != mutation_node and u != tskit.NULL:
                        u = tree.parent(u)
                    derived_state = str(int(u == tskit.NULL))
                    parent = tskit.NULL
                    if u == tskit.NULL:
                        parent = len(tables.mutations) - 1
                    tables.mutations.add_row(
                        site=site.id,
                        node=sample,
                        parent=parent,
                        derived_state=derived_state,
                    )
    return tables.tree_sequence()
github tskit-dev / tskit / python / tskit / tables.py View on Github external
metadata=new_md,
            metadata_offset=new_md_offset)
        # We also need to adjust the mutations table, as it references into sites
        keep_mutations = keep_sites[self.mutations.site]
        new_ds, new_ds_offset = keep_with_offset(
            keep_mutations, self.mutations.derived_state,
            self.mutations.derived_state_offset)
        new_md, new_md_offset = keep_with_offset(
            keep_mutations, self.mutations.metadata, self.mutations.metadata_offset)
        # Site numbers will have changed
        site_map = np.cumsum(keep_sites, dtype=self.mutations.site.dtype) - 1
        # Mutation numbers will change, so the parent references need altering
        mutation_map = np.cumsum(keep_mutations, dtype=self.mutations.parent.dtype) - 1
        # Map parent == -1 to -1, and check this has worked (assumes tskit.NULL == -1)
        mutation_map = np.append(mutation_map, -1).astype(self.mutations.parent.dtype)
        assert mutation_map[tskit.NULL] == tskit.NULL
        self.mutations.set_columns(
            site=site_map[self.mutations.site[keep_mutations]],
            node=self.mutations.node[keep_mutations],
            derived_state=new_ds,
            derived_state_offset=new_ds_offset,
            parent=mutation_map[self.mutations.parent[keep_mutations]],
            metadata=new_md,
            metadata_offset=new_md_offset)
        if record_provenance:
            # TODO replace with a version of https://github.com/tskit-dev/tskit/pull/243
            parameters = {
                "command": "delete_sites",
                "TODO": "add parameters"
            }
            self.provenances.add_row(record=json.dumps(
                provenance.get_provenance_dict(parameters)))
github tskit-dev / tsinfer / tsinfer / eval_util.py View on Github external
"""
    Checks if the specified tree sequence has the required properties for an
    ancestors tree sequence.
    """
    # An empty tree sequence is always fine.
    if ts.num_nodes == 0:
        return
    tables = ts.tables
    if np.any(tables.nodes.time <= 0):
        raise ValueError("All nodes must have time > 0")

    for tree in ts.trees(sample_counts=False):
        # 0 must always be a root and have at least one child.
        if tree.parent(0) != tskit.NULL:
            raise ValueError("0 is not a root: non null parent")
        if tree.left_child(0) == tskit.NULL:
            raise ValueError("0 must have at least one child")
        for root in tree.roots:
            if root != 0:
                if tree.left_child(root) != tskit.NULL:
                    raise ValueError("All non empty subtrees must inherit from 0")
github tskit-dev / msprime / msprime / __init__.py View on Github external
#
# You should have received a copy of the GNU General Public License
# along with msprime.  If not, see .
#
"""
Msprime is a reimplementation of Hudson's classical ms simulator for
modern datasets.
"""
# flake8: NOQA
import tskit

# Compatibility layer for old code using the tskit API exported by msprime.
NULL_NODE = tskit.NULL
NULL_POPULATION = tskit.NULL
NULL_INDIVIDUAL = tskit.NULL
NULL_MUTATION = tskit.NULL

from tskit import (
    Individual, Node, Edge, Site, Mutation, Migration, Population,
    Variant, Edgeset, Provenance)
from tskit import Tree as SparseTree  # Rename SparseTree to Tree in tskit
from tskit import TreeSequence
from tskit import (
    IndividualTable, NodeTable, EdgeTable, SiteTable, MutationTable,
    MigrationTable, PopulationTable, ProvenanceTable, TableCollection)
from tskit import LdCalculator
from tskit import load, load_text
from tskit import (
    parse_nodes, parse_edges, parse_individuals, parse_sites, parse_mutations)
from tskit import pack_strings, pack_bytes, unpack_bytes, unpack_strings
from tskit import validate_provenance