Repository navigation
Closed
Description
Hi there,
I'm running into a bug when attempting to add additional metadata to my nodes. The tree sequence was generated in SLiM, so it has the normal metadata associated with that.
When I try to add another property ("node_tag", a string), I get the following error:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 636, in __init__
codec_instance = codec_cls(schema)
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 582, in __init__
decoder = StructCodec.make_decode(schema)
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 326, in make_decode
return StructCodec.make_object_or_null_decode(sub_schema)
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 380, in make_object_or_null_decode
sub_decoders = {
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 381, in <dictcomp>
key: StructCodec.make_decode(prop)
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 328, in make_decode
return {
File "/Users/shyamie/miniconda3/envs/slim-env/lib/python3.9/site-packages/tskit/metadata.py", line 402, in make_string_decode
f = "<" + sub_schema["binaryFormat"]
KeyError: 'binaryFormat'
To give you a reproducible example, my code follows. I have also attached an example tree sequence.
import tskit
# Read in tree
tree = tskit.load("inf-2_V1_to_H33_on_day_45.trees")
# Add metadata
metadata_schema = tree.tables.nodes.metadata_schema.schema
# if node_tag is not already initialized, add it
if "node_tag" not in metadata_schema.get("properties").keys():
metadata_schema["properties"]["node_tag"] = {"description":"Additional metadata specifying exactly where in the Ross-Macdonald sequence this node was generated.", "type":"string"}
tree_tables = tree.dump_tables()
tree_tables.nodes.metadata_schema = tskit.MetadataSchema(metadata_schema)
Any ideas how to fix this? Thanks in advance!
Metadata
Metadata
Assignees
Labels
No labels
Activity
ShyamieG commentedon Sep 22, 2023
Sorry, re-opening this. I thought I had solved the issue.
I determined that I should specify a 'binaryFormat' for my new property. I tried 's' and 'p' (based on reading https://docs.python.org/3/library/struct.html#format-characters)
However, when I try to access the node metadata now I get the following error:
Ultimately, I'm trying to create a property that will allow me to store a string of flexible length.
petrelharp commentedon Sep 22, 2023
Hah, I've been writing this at the same time as you.
... in response to your previous comment: yes, that's right but I think you may still run into some issues (which you did). Here's the explanation:
There's two ways that metadata can be encoded, roughly: (a) as json-text, or (b) directly packing bytes in binary. The thing that controls which is done is the
codecproperty of the metadata schema; and SLiM uses (b), so in the metadata schema,codec="struct". For this codec, you need to tell it how to pack those bytes in, as you discovered.However, if you change the metadata schema, you'll no longer be able to read existing metadata, since it's in binary. (This is less of a problem for the
jsoncodec, since what's stored is just a text string.) So to modify the metadata you'll need to do some unpacking-and-repacking. Here's an example where I add anint:(Now I'll read your next comment...)
petrelharp commentedon Sep 22, 2023
Ah-ha - I was afraid that you wanted to store a string of variable length. That's not currently supported by the
structcodec (I think?), basically for efficiency reasons. The solution is to switch to thejsoncodec.However, it's important to know that if you change the metadata format you won't be able to read this back into SLiM. If you do want to do that, an alternative is to just stick the information in toplevel metadata, as a vector of length equal to number of nodes, for instance.
petrelharp commentedon Sep 22, 2023
Here's an example of switching to
json:petrelharp commentedon Sep 22, 2023
As you can tell, improving documentation and error messages for metadata is on the TODO list.
petrelharp commentedon Sep 22, 2023
Also note that using the
jsoncodec will be slower and take up more space than thestructcodec. (I'm not sure how much, though!) But if you're storing strings of variable length, that's pretty much unavoidable.ShyamieG commentedon Sep 22, 2023
Thank you so much! Once again, your explanations have really helped clear up my confusion 😂 I should have put more effort into trying to understand the different codecs/binary formats before messing around with the metadata.
I think I'll modify my approach to encode all additional metadata as integers to keep things efficient/compatible with SLiM.
ShyamieG commentedon Sep 22, 2023
Hi again,
I got everything working so that I have two new properties in my metadata (integers, to avoid having to change the codec). However, when I try to read the resulting tree back into SLiM, I get a Simulation Runtime Error:
It seems to be complaining about the node metadata length... but I thought SLiM would be flexible about that? This is my tree.
Edited to add that I'm still using SLiM 3, if that matters
Edited again to say that I am getting a similar error with SLiM 4 :(
petrelharp commentedon Sep 25, 2023
That's always appreciated, but on the contrary, you've put in a lot of work here and this thread will be useful for others, I think - no worries.
Ah, sorry, I should have been more clear above. SLiM is not flexible about metadata length. The reason is that the binary encoding that it uses is hardcoded: SLiM could in principle read and use the metadata schema, but that would be a whole can of worms, so it's not done.
So - if you want to read the information back into SLiM, I would not store the extra information in node metadata, but instead store it in top level metadata. Here's a real simple example where I just (a) in SLiM, store in top-level metadata some information about each node (the sum of their mutations' effects), and (b) associate it with the corresponding nodes in python (by making a dictionary with keys equal to the tskit-node-ids).
First, SLiM:
Then, python:
So then for instance, tskit-node 10 represents the genome with SLiM ID 9010:
I've been meaning to write something like this for the pyslim documentation.
ShyamieG commentedon Sep 25, 2023
Okay, this makes a lot of sense - thank you!
Do you know if there is a way to access this top-level metadata within SLIM itself? For example, if I were to load the tree sequence you created above by a different SLiM script, how could I access the node_ids and node_fitnesses elements you defined?
ShyamieG commentedon Sep 26, 2023
I've been playing around with this more, and it seems like it won't work as I had hoped. I'm trying to find some way to add metadata (ideally to the nodes themselves, but at least to the top-level metadata) such that it can be tracked across multiple SLiM simulations that are chained together.
I'm able to add stuff to the "user_metadata" slot when I output the tree sequence from my SLiM simulation, and I can access it with tskit, but this information is not preserved if I then read that tree sequence back into SLiM (using readFromPopulationFile) and output the tree sequence some generations later.
Is what I am attempting possible, or do I need to re-think this approach altogether?
petrelharp commentedon Sep 27, 2023
Hm - you're right, that SLiM doesn't allow access to the extra, user-added metadata when it reads in a tree sequence. That would be a nice thing, though, to enable the sort of thing you're trying to do - perhaps
readFromPopulationFile( )could return a dictionary containing anything inSLiM/user_metadataof the tree sequence? If that seems like a good and useful idea, we should perhaps open an issue over at SLiM?Lacking that, though, the only way I can think of to do what you're trying to do is to write out the information to a text file and read it into SLiM that way. @bhaller - any other suggestions?
ShyamieG commentedon Sep 27, 2023
Yes, that would work. I can open an issue over there and link to this discussion.
I was hoping to avoid having to write out a text file for each output tree, but it seems that will the best way to go about this for the time being.
Thanks again for your responses, your input has been extremely helpful!
bhaller commentedon Sep 27, 2023
The
treeSeqMetadata()function is there for exactly this purpose. Simply call it to get your metadata dictionary back.petrelharp commentedon Sep 27, 2023
Perhaps I'll close this issue, and open a new one in SLiM or pyslim as appropriate, if there's further things to discuss? Feel free to re-open though @ShyamieG .