コード例 #1
0
ファイル: simulations.py プロジェクト: champost/msprime
 def get_tree_sequence(self,
                       mutation_generator=None,
                       provenance_record=None):
     """
     Returns a TreeSequence representing the state of the simulation.
     """
     ll_recomb_map = self.recombination_map.get_ll_recombination_map()
     t = self.tables
     self.ll_sim.populate_tables(t.nodes.ll_table,
                                 t.edges.ll_table,
                                 t.migrations.ll_table,
                                 t.populations.ll_table,
                                 recombination_map=ll_recomb_map)
     if mutation_generator is not None:
         mutation_generator.generate(t.nodes.ll_table, t.edges.ll_table,
                                     t.sites.ll_table, t.mutations.ll_table)
     t.provenances.reset()
     if provenance_record is not None:
         t.provenances.add_row(provenance_record)
     ll_tree_sequence = _msprime.TreeSequence()
     ll_tree_sequence.load_tables(t.nodes.ll_table,
                                  t.edges.ll_table,
                                  t.migrations.ll_table,
                                  t.sites.ll_table,
                                  t.mutations.ll_table,
                                  t.provenances.ll_table,
                                  populations=t.populations.ll_table)
     return trees.TreeSequence(ll_tree_sequence)
コード例 #2
0
ファイル: test_hdf5.py プロジェクト: guzhongru/msprime
 def test_version_load_error(self):
     ts = msprime.simulate(10)
     for bad_version in [(0, 1), (0, 8), (2, 0)]:
         ts.dump(self.temp_file)
         hfile = h5py.File(self.temp_file, "r+")
         hfile.attrs['format_version'] = bad_version
         hfile.close()
         other_ts = _msprime.TreeSequence()
         self.assertRaises(_msprime.LibraryError, other_ts.load, self.temp_file)
コード例 #3
0
                              population=0),
    msprime.CoalescenceRecord(left=0.8521429346530099,
                              right=1.0,
                              node=5,
                              children=(0, 4),
                              time=0.7114579294844481,
                              population=0),
    msprime.CoalescenceRecord(left=0.0,
                              right=0.1912159270586483,
                              node=6,
                              children=(0, 4),
                              time=2.8161856234286375,
                              population=0)
]

ll_ts_0 = _msprime.TreeSequence()
ll_ts_0.load_records(records_0)
ts_0 = msprime.TreeSequence(ll_ts_0)

[x == y for x, y in zip(ts.records(), ts_0.records())]

[x == y for x, y in zip(ts.trees(), ts_0.trees())]

###
print("Adjust records")

# Round down the times:
#
# 1.0          6
# 0.7         / \                                                                     5
#            /   \                                                                   / \
コード例 #4
0
def _load_legacy_hdf5(root):
    if 'format_version' not in root.attrs:
        raise ValueError("HDF5 file not in msprime format")
    format_version = root.attrs['format_version']
    if format_version[0] != 2:
        raise ValueError("Only version 2.x are supported")

    # Get the coalescence records
    trees_group = root["trees"]
    left = np.array(trees_group["left"])
    right = np.array(trees_group["right"])
    node = np.array(trees_group["node"])
    children = np.array(trees_group["children"])
    population = np.array(trees_group["population"])
    time = np.array(trees_group["time"])
    num_records = len(left)
    records = num_records * [None]
    for j in range(num_records):
        records[j] = msprime.CoalescenceRecord(left=left[j],
                                               right=right[j],
                                               node=node[j],
                                               children=tuple(children[j]),
                                               time=time[j],
                                               population=population[j])

    # Get the samples (if present)
    samples = None
    if "samples" in root:
        samples_group = root["samples"]
        population = np.array(samples_group["population"])
        time = None
        if "time" in samples_group:
            time = np.array(samples_group["time"])
        sample_size = len(population)
        samples = sample_size * [None]
        for j in range(sample_size):
            t = 0
            if time is not None:
                t = time[j]
            samples[j] = msprime.Sample(population=population[j], time=t)

    # Get the mutations (if present)
    mutations = None
    if "mutations" in root:
        mutations_group = root["mutations"]
        position = np.array(mutations_group["position"])
        node = np.array(mutations_group["node"])
        num_mutations = len(node)
        mutations = num_mutations * [None]
        for j in range(num_mutations):
            mutations[j] = msprime.Mutation(position=position[j],
                                            node=node[j],
                                            index=j)

    ll_ts = _msprime.TreeSequence()
    if samples is None:
        ll_ts.load_records(records)
    else:
        ll_ts.load_records(records, samples)
    ll_ts.add_provenance_string(
        _get_provenance("generate_trees", trees_group.attrs))
    if mutations is not None:
        ll_ts.set_mutations(mutations)
        ll_ts.add_provenance_string(
            _get_provenance("generate_mutations", mutations_group.attrs))
    ll_ts.add_provenance_string(
        json.dumps(msprime.get_provenance_dict("upgrade", {})))
    return ll_ts