How to use the msprime.NodeTable function in msprime

To help you get started, we’ve selected a few msprime 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 / tests / test_topology.py View on Github external
def test_zero_nodes(self):
        nodes = msprime.NodeTable()
        edges = msprime.EdgeTable()
        # Without a sequence length this should fail.
        self.assertRaises(
            _msprime.LibraryError, msprime.load_tables, nodes=nodes, edges=edges)
        ts = msprime.load_tables(nodes=nodes, edges=edges, sequence_length=1)
        self.assertEqual(ts.sequence_length, 1)
        self.assertEqual(ts.num_trees, 1)
        self.assertEqual(ts.num_nodes, 0)
        self.assertEqual(ts.num_edges, 0)
        t = next(ts.trees())
        self.assertEqual(t.index, 0)
        self.assertEqual(t.left_root, msprime.NULL_NODE)
        self.assertEqual(t.interval, (0, 1))
        self.assertEqual(t.roots, [])
        self.assertEqual(t.root, msprime.NULL_NODE)
        self.assertEqual(t.parent_dict, {})
github tskit-dev / msprime / tests / test_topology.py View on Github external
def test_one_node_one_sample(self):
        nodes = msprime.NodeTable()
        nodes.add_row(time=0, flags=msprime.NODE_IS_SAMPLE)
        edges = msprime.EdgeTable()
        # Without a sequence length this should fail.
        self.assertRaises(
            _msprime.LibraryError, msprime.load_tables, nodes=nodes, edges=edges)
        ts = msprime.load_tables(nodes=nodes, edges=edges, sequence_length=1)
        self.assertEqual(ts.sequence_length, 1)
        self.assertEqual(ts.num_trees, 1)
        self.assertEqual(ts.num_nodes, 1)
        self.assertEqual(ts.sample_size, 1)
        self.assertEqual(ts.num_edges, 0)
        t = next(ts.trees())
        self.assertEqual(t.index, 0)
        self.assertEqual(t.left_root, 0)
        self.assertEqual(t.interval, (0, 1))
        self.assertEqual(t.roots, [0])
github tskit-dev / msprime / tests / test_topology.py View on Github external
def test_one_node(self):
        nodes = msprime.NodeTable()
        edges = msprime.EdgeTable()
        nodes.add_row()
        ts = msprime.load_tables(nodes=nodes, edges=edges, sequence_length=1)
        self.assertEqual(ts.sequence_length, 1)
        self.assertEqual(ts.num_trees, 1)
        # Test the simple tree iterator.
        trees = list(tsutil.algorithm_T(ts))
        self.assertEqual(len(trees), 1)
        (left, right), parent = trees[0]
        self.assertEqual(left, 0)
        self.assertEqual(right, 1)
        self.assertEqual(parent, [-1])
github tskit-dev / msprime / simplify_work / test_simplify.py View on Github external
def print_tables(ts):
    nodes = msprime.NodeTable()
    edges = msprime.EdgesetTable()
    ts.dump_tables(nodes=nodes, edgesets=edges)
    print(nodes)
    print(edges)
github tskit-dev / msprime / simplify_work / paint_simplify.py View on Github external
def simplify(ts, samples):
    """
    Steps through records, painting each according to who descends from them.
    When two colors combine, they get a new color and we record a record
    in the simplified tables.
    """
    nodes = msprime.NodeTable()
    edgesets = msprime.EdgesetTable()
    # dict of old labels -> new labels
    node_colors = {}

    def add_colors(values, left, right, parent, time, empty=-1):
        """
        Return a new color if there is more than one nonempty one,
        storing these events as we go.
        """
        colors = list(set(values).difference([empty]))
        # print(colors)
        if len(colors) == 0:
            return empty
        elif len(colors) == 1:
            return colors[0]
        else:
github mcveanlab / treeseq-inference / src / plots.py View on Github external
n_used = None
        else:
            Ne_used = Ne
            n_used = sample_size
        if mut_seed is None:
            ts = msprime.simulate(
                n_used, Ne_used, recombination_map=recombination_map, mutation_rate=mutation_rate,
                random_seed=sim_seed, **kwargs)
        else:
            #run with no mutations (should give same result regardless of mutation rate)
            ts = msprime.simulate(
                n_used, Ne_used, recombination_map=recombination_map, mutation_rate=0,
                random_seed=sim_seed, **kwargs)
            #now add the mutations
            rng2 = msprime.RandomGenerator(mut_seed)
            nodes = msprime.NodeTable()
            edges = msprime.EdgeTable()
            sites = msprime.SiteTable()
            muts = msprime.MutationTable()
            ts.dump_tables(nodes=nodes, edges=edges)
            mutgen = msprime.MutationGenerator(rng2, mutation_rate)
            mutgen.generate(nodes, edges, sites, muts)
            msprime.sort_tables(nodes=nodes, edges=edges, sites=sites, mutations=muts)
            ts = msprime.load_tables(nodes=nodes, edges=edges, sites=sites, mutations=muts)

        logging.info(
            "Neutral simulation done; {} sites, {} trees".format(ts.num_sites, ts.num_trees))
        sim_fn = mk_sim_name(sample_size, Ne, length, recombination_rate, mutation_rate, seed, mut_seed, self.simulations_dir)
        logging.debug("writing {}.hdf5".format(sim_fn))
        ts.dump(sim_fn+".hdf5")

        # Make sure that there is *some* information in this simulation that can be used
github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
def finalise_tree_sequence(num_samples, store, position, length, tree_sequence_builder):

    site_position = np.zeros(store.num_sites + 1, dtype=np.float64)
    site_position[:store.num_sites] = position
    site_position[store.num_sites] = length
    # Make sure the first coordinate is 0
    site_position[0] = 0

    # Allocate the nodes table.
    num_nodes = tree_sequence_builder.num_nodes
    flags = np.zeros(num_nodes, dtype=np.uint32)
    time = np.zeros(num_nodes, dtype=np.float64)
    tree_sequence_builder.dump_nodes(flags, time)
    nodes = msprime.NodeTable()
    nodes.set_columns(flags=flags, time=time)
    del flags, time

    num_edgesets = tree_sequence_builder.num_edgesets
    num_children = tree_sequence_builder.num_children
    left = np.empty(num_edgesets, dtype=np.float64)
    right = np.empty(num_edgesets, dtype=np.float64)
    parent = np.empty(num_edgesets, dtype=np.int32)
    children = np.empty(num_children, dtype=np.int32)
    children_length = np.empty(num_edgesets, dtype=np.uint32)
    tree_sequence_builder.dump_edgesets(
        left, right, parent, children, children_length)
    # Translate left and right into positions.
    # TODO this should be done at the lower level where its safer.
    left = site_position[left.astype(np.uint32)]
    right = site_position[right.astype(np.uint32)]