Skip to content

move reference sequence to top-level metadata #180

Closed

Description

@petrelharp

Currently we store the reference sequence as a separate "column" in the kastore object. This requires us to open up the kastore separately, but more importantly, it requires us to do some gymnastics in pyslim to maintain the reference sequence in the tree sequence. In particular, we need to write a wrapper for any msprime method that modifies a tree sequence that does that operation and then adds back on the reference sequence.

But, now that we've got top-level metadata, we can just put the reference sequence there, and it'll travel with the tree sequence for free. This will remove a lot of responsibility from pyslim, and a lot of potential sources of confusion.

I think the only thing that affects this is whether it should be a standard tskit metadata entry, and hence ts.metadata["reference_sequence"], or whether it should be under the SLiM metadata (and hence ts.metadata["SLiM"]["reference_sequence"]. I'd vote for the former.

(Note: I've asked for input here on the tskit slack.)

Activity

  1. bhaller commented on May 5, 2021

    @bhaller
    Contributor

    Yes, it seems to make sense to shift this, although it'll be another file version hassle; I guess it's worth it.

    The former proposal – ts.metadata["reference_sequence"] – makes sense to me. The only thing I'd add is: probably it would be useful to be able to provide the reference sequence instead as a filename or a URL, and have it read in from that if needed. We don't want to get into that now, but we ought to make sure that that will fit into the scheme we design now, I suppose.

  2. petrelharp commented on Jun 7, 2021

    @petrelharp
    CollaboratorAuthor

    Over on the tskit slack, @jeromekelleher said:

    (1) seems like a good short term solution to me. Right now we're not using the reference sequence in tskit, so it's (by our definition) metadata. When we do, we can think about how to do (3) properly. (By properly, I mean hook up with existing infrastructure so that we don't need to have a copy of GRCh38 in every tsinfer'd tree sequence, but we fetch on demand)
    

    ... so, let's do this!

    probably it would be useful to be able to provide the reference sequence instead as a filename or a URL, and have it read in from that if needed.

    How about making ts.metadata["reference_sequence"] an object, and we'll just stick in ts.metadata["reference_sequence"]["sequence"] = CCGTAGTCCT...; and that'd leave room in the future for putting in e.g. ts.metadata["reference_sequence"]["url"] = XXX? (Or we just stick the nucleotides straight in to ts.metadata["reference_sequence"] and don't prematurely generalize? Do you see SLiM wanting to put in a URL or hash or something?)

  3. jeromekelleher commented on Jun 7, 2021

    @jeromekelleher

    I think it's probably better to have ts.metadata["reference_sequence"] = "ACGT..." rather than making it an object. I think if we do have the fancy on-demand fetching and caching stuff for reference sequences then it'll be done as data rather than metadata - that is, we use it internally in the library. The idea of metadata is that it's supposed to be opaque to the tskit library, but if start adding custom semantics for certain keys, then that doesn't hold true any more.

    So, ultimately, I think we'll have something like ts.reference_sequence which will have some useful behaviour like automatically fetching and caching the reference for a given (say) human build and contig (we could have some fallback behaviour that checks the metadata for the existence of the reference_sequence and do something appropriate if it exists).

  4. bhaller commented on Jun 7, 2021

    @bhaller
    Contributor

    I'm inclined not to touch this stuff until we are ready to move it into tskit data, rather than metadata. If I understand correctly, tskit is not yet ready/interested in doing that, but that is the long-term vision. So, for the time being, "if it ain't broke don't fix it" would be my guiding principle. :-> Changing it now when we know it's going to change again in future just causes file-version thrash for no reason, which is annoying and increases the likelihood of a bug in the read/write code. So I'm inclined to close this issue, or at least mark it "will not fix for the foreseeable future" – but simpler to just close it and open a new issue when there's work we actually want to do now. Thoughts?

  5. petrelharp commented on Jun 7, 2021

    @petrelharp
    CollaboratorAuthor

    So, for the time being, "if it ain't broke don't fix it" would be my guiding principle. :->

    Agree, but it is kinda broken at the moment, because currently it's way too easy for users to lose the reference sequence - to avoid this, pyslim's SlimTreeSequence would need to wrap all the tree sequence transformation methods to stick back on the reference sequence. In fact, many of our examples lose the reference sequence - any time we wrap something in SlimTreeSequence( ), like for instance SlimTreeSequence(msprime.mutate(ts, ....)), we have lost it. Moving it to metadata will be much easier than dealing with this, and much less confusing for the users. I don't see an alternative, besides just giving up on the idea of maintaining the reference sequence. It's annoying to have to bump the file version, but we're wanting to do that anyohw (to adjust the population metadata).

  6. jeromekelleher commented on Jun 7, 2021

    @jeromekelleher

    I agree with @petrelharp here - sure it's not great doing something that you know will change in the future, but we totally don't have the time to do TreeSequence.reference_sequence in tskit right now and realistically probably won't for something like a year.

  7. bhaller commented on Jun 7, 2021

    @bhaller
    Contributor

    I see. OK, that provides a pretty strong justification for this. Hmm. Can we put it in its final resting place now, and just have tskit not actually do anything with it yet? Or do we really have to move it to metadata now, and then move it again later?

  8. jeromekelleher commented on Jun 7, 2021

    @jeromekelleher

    Tskit only reads and write out columns that it recognises I'm afraid, so metadata is the only option without changes to tskit.

    (That's not to say that I think silently dropping unknown columns is the correct behaviour, but it would be significant work to rectify this.)

  9. bhaller commented on Jun 7, 2021

    @bhaller
    Contributor

    OK, I guess I'm convinced. I guess we put it into the kastore, back in the day, because we didn't have the spiffy metadata stuff yet, at that time? Well, so, where do we go from here? One part is that pyslim gets extended to be able to read the sequence from either site, and moves it to metadata if it finds it in the kastore, thereby converting old files to an internal format that will maintain the reference sequence across transformations. And then the other part is that SLiM gets modified to be able to read the sequence from either place, and to always write it out to metadata. And I guess even if both can read the sequence from either place, we still need to bump the file version, because .trees files that are written out with the sequence in the new place will not be (correctly) readable by older versions of SLiM/pyslim. Given that, I guess all of these things ought to happen simultaneously. @petrelharp, do you plan to do the work on the SLiM side, or am I doing that? And what's your timeframe for this? It will roll in SLiM 3.7, but at present I have no target date set for that, and I don't want to set one until I really have to; the goal is to roll stuff into it on an ongoing basis until I'm ready to release SLiM 4, ideally. So it will just be in GitHub until then, which ought to be fine, right?

  10. petrelharp commented on Jun 8, 2021

    @petrelharp
    CollaboratorAuthor

    I guess we put it into the kastore, back in the day, because we didn't have the spiffy metadata stuff yet, at that time?

    Right!

    Well, so, where do we go from here?

    What you say sounds right - should be straightforward: we'll bump the file version, and then if the file version is older than the new one we'll read the reference sequence from the old place, more or less just like we deal with other old file versions. And we can bundle this with the change to population metadata. I'll discuss timelines elsewhere.

  11. added
    trees
    on Jun 23, 2021
  12. bhaller commented on Oct 24, 2021

    @bhaller
    Contributor

    Note to self: when this gets fixed, revise the metadata description in the SLiM manual.

  13. bhaller commented on Oct 24, 2021

    @bhaller
    Contributor

    Working on this now. It occurs to me to confirm: tskit, pyslim, etc. will be happy to have a string that is, say, 3e9 characters long stuffed into metadata as a single JSON string? This is really where we want to put it now, given the potential size of the data? Anyhow, if I don't see a reply today I'll probably just finish this and commit it, on the assumption that you guys have really thought through the implications of this change. :->

  14. bhaller commented on Oct 24, 2021

    @bhaller
    Contributor

    @petrelharp I guess this new key, ts.metadata["reference_sequence"], needs to be added to the schema as an optional string key. How do you generate those rather horrendous schema strings, with all the escaping etc.? I will try to hand-modify it, but yuck. Here's the beautiful thing I came up with, let me know if you see a problem:

    "{\"$schema\":\"http://json-schema.org/schema#\",\"codec\":\"json\",\"examples\":[{\"SLiM\":{\"file_version\":\"0.7\",\"generation\":123,\"model_type\":\"WF\",\"nucleotide_based\":false,\"separate_sexes\":true,\"spatial_dimensionality\":\"xy\",\"spatial_periodicity\":\"x\"}}],\"properties\":{\"reference_sequence\":{\"description\":\"The reference (i.e., ancestral) sequence, for nucleotide-based models.\",\"type\":\"string\"},\"SLiM\":{\"description\":\"Top-level metadata for a SLiM tree sequence, file format version 0.7\",\"properties\":{\"file_version\":{\"description\":\"The SLiM 'file format version' of this tree sequence.\",\"type\":\"string\"},\"generation\":{\"description\":\"The 'SLiM generation' counter when this tree sequence was recorded.\",\"type\":\"integer\"},\"model_type\":{\"description\":\"The model type used for the last part of this simulation (WF or nonWF).\",\"enum\":[\"WF\",\"nonWF\"],\"type\":\"string\"},\"nucleotide_based\":{\"description\":\"Whether the simulation was nucleotide-based.\",\"type\":\"boolean\"},\"separate_sexes\":{\"description\":\"Whether the simulation had separate sexes.\",\"type\":\"boolean\"},\"spatial_dimensionality\":{\"description\":\"The spatial dimensionality of the simulation.\",\"enum\":[\"\",\"x\",\"xy\",\"xyz\"],\"type\":\"string\"},\"spatial_periodicity\":{\"description\":\"The spatial periodicity of the simulation.\",\"enum\":[\"\",\"x\",\"y\",\"z\",\"xy\",\"xz\",\"yz\",\"xyz\"],\"type\":\"string\"},\"stage\":{\"description\":\"The stage of the SLiM life cycle when this tree sequence was recorded.\",\"type\":\"string\"}},\"required\":[\"model_type\",\"generation\",\"file_version\",\"spatial_dimensionality\",\"spatial_periodicity\",\"separate_sexes\",\"nucleotide_based\"],\"type\":\"object\"}},\"required\":[\"SLiM\"],\"type\":\"object\"}"

  15. bhaller commented on Nov 17, 2021

    @bhaller
    Contributor

    tskit is adding support for keeping the reference sequence in kastore at reference_sequence/data , as I understand it (see tskit-dev/tskit#146), which is where we used to keep it, so I'm backing out this fix. I will not change the file version number for this reversal; file version 0.7 was added earlier in the SLiM 3.7 development cycle for the addition of the parents column info in the individuals table, and then this fix was added in to version 0.7, and now I'm going to back it out of 0.7. Since 0.7 has not gone public yet in a SLiM release, these changes will affect only people on the GitHub head, who ought to be prepared for such flux. Note that I'm not going to back out the diffs exactly; some restructuring was done for this that remains a good idea, I think.

  16. bhaller commented on Nov 18, 2021

    @bhaller
    Contributor

    OK, it has been pointed out by @benjeffery that of course the fix here should use the new tskit APIs to set the reference sequence prior to saving, rather than just pushing it into the kastore. So, reopening. Once tskit C API 1.0 has been released and merged into SLiM, we will shift over to the new APIs provided for this.

  17. bhaller commented on Nov 30, 2021

    @bhaller
    Contributor

    Now waiting for tskit-dev/tskit#1970 to be resolved to proceed with this.

  18. bhaller commented on Dec 8, 2021

    @bhaller
    Contributor

    This was fixed in #247.

  19. added 2 commits that reference this issue on Jul 20, 2024
    1d16bfe
    02e15c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    prioritytrees

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions