Repository navigation
Add metadata to individuals #2021
Answered
by
jeromekelleher
andrei-barysenka
asked this question in
Q&A
|
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 |
Answered by
jeromekelleher
Mar 21, 2022
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: |
0 replies
Answer selected by
andrei-barysenka
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to 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:
However, you probably want to use a schema if you're…