How to use tskit - 10 common examples

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 / msprime / tskit_tests / test_highlevel.py View on Github external
def validate(self, ts):
        self.assertIsNone(ts.file_uuid)
        ts.dump(self.temp_file)
        other_ts = tskit.load(self.temp_file)
        self.assertIsNotNone(other_ts.file_uuid)
        self.assertTrue(len(other_ts.file_uuid), 36)
        uuid = other_ts.file_uuid
        other_ts = tskit.load(self.temp_file)
        self.assertEqual(other_ts.file_uuid, uuid)
        self.assertEqual(ts.tables, other_ts.tables)

        # Check that the UUID is well-formed.
        parsed = _uuid.UUID("{" + uuid + "}")
        self.assertEqual(str(parsed), uuid)

        # Save the same tree sequence to the file. We should get a different UUID.
        ts.dump(self.temp_file)
        other_ts = tskit.load(self.temp_file)
        self.assertIsNotNone(other_ts.file_uuid)
        self.assertNotEqual(other_ts.file_uuid, uuid)

        # Even saving a ts that has a UUID to another file changes the UUID
        old_uuid = other_ts.file_uuid
        other_ts.dump(self.temp_file)
github tskit-dev / tsinfer / tests / test_cli.py View on Github external
def verify_output(self, output_path):
        output_trees = tskit.load(output_path)
        self.assertEqual(output_trees.num_samples, self.input_ts.num_samples)
        self.assertEqual(output_trees.sequence_length, self.input_ts.sequence_length)
        self.assertEqual(output_trees.num_sites, self.input_ts.num_sites)
        self.assertGreater(output_trees.num_sites, 1)
        self.assertTrue(
            np.array_equal(
                output_trees.genotype_matrix(), self.input_ts.genotype_matrix()
            )
github tskit-dev / pyslim / tests / test_provenance.py View on Github external
def get_0_3_slim_examples(self):
        for filename in ['tests/examples/recipe_WF.v3.3.1.trees',
                         'tests/examples/recipe_nonWF.v3.3.1.trees']:
            with self.assertWarns(Warning):
                yield tskit.load(filename)
github tskit-dev / msprime / tskit_tests / test_file_format.py View on Github external
ts.dump(self.temp_file)
        self.assertTrue(os.path.exists(self.temp_file))
        self.assertGreater(os.path.getsize(self.temp_file), 0)
        self.verify_keys(ts)

        store = kastore.load(self.temp_file)
        # Check the basic root attributes
        format_name = store['format/name']
        self.assertTrue(np.array_equal(
            np.array(bytearray(b"tskit.trees"), dtype=np.int8), format_name))
        format_version = store['format/version']
        self.assertEqual(format_version[0], CURRENT_FILE_MAJOR)
        self.assertEqual(format_version[1], 0)
        self.assertEqual(ts.sequence_length, store['sequence_length'][0])
        # Load another copy from file so we can check the uuid.
        other_ts = tskit.load(self.temp_file)
        self.verify_uuid(other_ts, store["uuid"].tobytes().decode())

        tables = ts.tables

        self.assertTrue(np.array_equal(
            tables.individuals.flags, store["individuals/flags"]))
        self.assertTrue(np.array_equal(
            tables.individuals.location, store["individuals/location"]))
        self.assertTrue(np.array_equal(
            tables.individuals.location_offset, store["individuals/location_offset"]))
        self.assertTrue(np.array_equal(
            tables.individuals.metadata, store["individuals/metadata"]))
        self.assertTrue(np.array_equal(
            tables.individuals.metadata_offset, store["individuals/metadata_offset"]))

        self.assertTrue(np.array_equal(tables.nodes.flags, store["nodes/flags"]))
github tskit-dev / msprime / tskit_tests / test_tables.py View on Github external
def test_empty_tables(self):
        tables = tskit.TableCollection(1)
        tables.sort()
        self.assertEqual(tables.nodes.num_rows, 0)
        self.assertEqual(tables.edges.num_rows, 0)
        self.assertEqual(tables.sites.num_rows, 0)
        self.assertEqual(tables.mutations.num_rows, 0)
        self.assertEqual(tables.migrations.num_rows, 0)
github tskit-dev / msprime / tskit_tests / test_metadata.py View on Github external
def test_nodes(self):
        tables = tskit.TableCollection(sequence_length=1)
        metadata = ExampleMetadata(one="node1", two="node2")
        pickled = pickle.dumps(metadata)
        tables.nodes.add_row(time=0.125, metadata=pickled)
        ts = tables.tree_sequence()
        node = ts.node(0)
        self.assertEqual(node.time, 0.125)
        self.assertEqual(node.metadata, pickled)
        unpickled = pickle.loads(node.metadata)
        self.assertEqual(unpickled.one, metadata.one)
        self.assertEqual(unpickled.two, metadata.two)
github tskit-dev / msprime / tests / test_likelihood.py View on Github external
def test_gap_in_ancestral_material(self):
        tables = tskit.TableCollection(sequence_length=1)
        tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, population=0,
                             individual=-1, time=0)
        tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, population=0,
                             individual=-1, time=0)

        tables.edges.add_row(left=0, right=0.3, parent=2, child=0)
        tables.edges.add_row(left=0.3, right=1, parent=3, child=0)
        tables.nodes.add_row(flags=msprime.NODE_IS_RE_EVENT, population=0,
                             individual=-1, time=0.1)
        tables.nodes.add_row(flags=msprime.NODE_IS_RE_EVENT, population=0,
                             individual=-1, time=0.1)

        tables.edges.add_row(left=0.3, right=0.5, parent=4, child=3)
        tables.edges.add_row(left=0.5, right=1, parent=5, child=3)
        tables.nodes.add_row(flags=msprime.NODE_IS_RE_EVENT, population=0,
                             individual=-1, time=0.2)
github tskit-dev / msprime / tskit_tests / test_file_format.py View on Github external
def test_empty_file(self):
        tables = tskit.TableCollection(sequence_length=3)
        self.verify_round_trip(tables.tree_sequence(), 10)
github tskit-dev / msprime / tests / wright_fisher.py View on Github external
def run(self, ngens):
        L = 1
        if self.num_loci is not None:
            L = self.num_loci
        tables = tskit.TableCollection(sequence_length=L)
        tables.populations.add_row()
        if self.deep_history:
            # initial population
            init_ts = msprime.simulate(
                self.N, recombination_rate=1.0, length=L, random_seed=self.seed)
            init_tables = init_ts.dump_tables()
            flags = init_tables.nodes.flags
            if not self.initial_generation_samples:
                flags = np.zeros_like(init_tables.nodes.flags)
            tables.nodes.set_columns(
                time=init_tables.nodes.time + ngens,
                flags=flags)
            tables.edges.set_columns(
                left=init_tables.edges.left, right=init_tables.edges.right,
                parent=init_tables.edges.parent, child=init_tables.edges.child)
        else:
github tskit-dev / msprime / tskit_tests / test_tables.py View on Github external
def test_sequence_length(self):
        for sequence_length in [0, 1, 100.1234]:
            tables = tskit.TableCollection(sequence_length=sequence_length)
            self.assertEqual(tables.sequence_length, sequence_length)