Add metadata to individuals #2021
-
How can I add metadata info to individuals in my tree sequence ? I do something like this: ts1=msprime.sim_ancestry( ... ) but metadata is still empty |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @andrei-barysenka 👋 The TreeSequence object is read-only, and any modifications that you make to the Python objects you get back will either fail, or be transitory (as you have here with the Individual objects). If you want the changes to "stick" you need to work with the underlying TableCollection. For your example: import msprime
ts = msprime.sim_ancestry(3, random_seed=2)
tables = ts.dump_tables()
tables.individuals.clear()
for individual in ts.individuals():
individual.metadata = b"META"
tables.individuals.append(individual)
print(tables.individuals)
ts2 = tables.tree_sequence()
for i in ts2.individuals():
print(i) However, you probably want to use a schema if you're adding metadata. This is the simplest way: import tskit
tables = ts.dump_tables()
tables.individuals.clear()
tables.individuals.metadata_schema = tskit.MetadataSchema.permissive_json()
for individual in ts.individuals():
individual.metadata = {"key": "value"}
tables.individuals.append(individual)
print(tables.individuals)
ts2 = tables.tree_sequence()
for i in ts2.individuals():
print(i) You might find these tutorials useful: |
Beta Was this translation helpful? Give feedback.
Hi @andrei-barysenka 👋
The TreeSequence object is read-only, and any modifications that you make to the Python objects you get back will either fail, or be transitory (as you have here with the Individual objects). If you want the changes to "stick" you need to work with the underlying TableCollection. For your example:
However, you probably want to use a schema if you're…