Skip to content

Node labels in to_nexus are ambiguous #1803

Closed

Description

@jeromekelleher

As @jeetsukumaran points out our default node labels in to_nexus are ambiguous as they are in the form "tsk_{node.id}_{node.flags}", and these would be interpreted as "tsk {node.id} {node.flags}" by conformant nexus parsers.

Since it's unlikely many people are using the to_nexus functionality at the moment (given that it's broken! #1785 ) I think we're free to change things around a bit.

The idea of including the ID and flags in the label is so that samples can be identified, but maybe the flags are extraneous for most applications (the samples are nearly always be the leaf nodes), and we would be better off keeping things simple and just including the ID. The idea of the tsk prefix is that some things get confused by integer names, but maybe that's extraneous as well. So, perhaps the best default behaviour would be to simply use the bare ID values?

As we're updating the method, I think it's worthwhile adding (or at least planning for) custom node labels as well, via a mapping like we use in the current "newick" method. (Which we're planning on deprecating, #1671 )

Any thoughts here @jeetsukumaran? Great to get your input here so that we can at least lay the foundations for getting good interoperability with the classical phylo formats.

Activity

  1. added
    bug
    Python API
    on Oct 17, 2021
  2. jeetsukumaran commented on Oct 17, 2021

    @jeetsukumaran
    Contributor

    As I noted in the thread discussing #1801, just leaving the underscores in as-is unquoted, will work fine (for most folks, most of the time) even when interoperating with other nexus-compliant programs. The semantics will differ, but it is syntactically correct, and ambiguity/confusion in interpretation of the labels only will arise when mixing outputs from programs with different assumptions (and that output data that uses the internally-normalized labels). These latter cases throw some obvious errors and can be handled.

    Another option is just to swap in a valid token that is not subject to any ambiguity and can be emitted/parsed without quoting. I would suggest using a caret ("^"; so "tsk^{node.id}^{node.flags}"). This seems to be handled by most programs without issue, and is preserved through round-tripping. The period (".") also mostly works, but at least as of a few years ago, there were some programs in the wild that, for some strange idiosyncratic reason, stripped periods from labels.

    What I ended up doing in dendropy is just provide a very rich interface for client code to decide what their labels are going to look like. Lots of true/false options, and all can be overridden by a client-supplied function object passed to the writer that will take a node as an argument and return the string rendering.

    The other big thing is to whether or not the trees should be seen as rooted vs unrooted. Typically, a tree string is prepended with "[&R]" to indicate a rooted tree vs "[&U]" an unrooted tree. This is a fairly standard convention and widely -supported. The problem is when such a token is not present, should the trees be considered unrooted (even if there is a basal bifurcation) or rooted? Here there is no consensus. Many and maybe most bioinformatics programs assume rooted trees, but PAUP* (which for me is the reference standard for the Nexus foramt) assumes unrooted trees (and so does DendroPy).

    Maybe we can think about how much tskit wants to invest in data parsing/writing. My suggestion is that maybe tskit can emit some basically compliant nexus/newick for now, that works for most folks most of the time. If there is an interest in extending its data writing/parsing capabilities and there is no objection to introducing dendropy as a dependency, then in the longer term, then we can bring in dendropy to do all the heavy-lifting in that regard. All these internals will be hidden to the client code, of course, though we can expose the DendroPy writer/parser API in the "to_nexus()" method signature -- this will allow for very flexible and highly customizable output (and input) in a range of formats.

  3. jeromekelleher commented on Oct 18, 2021

    @jeromekelleher
    MemberAuthor

    Maybe we can think about how much tskit wants to invest in data parsing/writing. My suggestion is that maybe tskit can emit some basically compliant nexus/newick for now, that works for most folks most of the time.

    I agree, that seems like the most sensible path. I wonder if we should just sidestep this separator issue entirely though by changing the (default) labels for nodes to "{node.id}"? I doubt there's much value in the "tsk_" prefix and "_{node.flags}" suffix in practise. Would having bare integer labels cause any downstream issues do you think?

    I've opened #1812 to track adding a "node_labels" argument which would allow users specify node labels themselves, if they wanted something specific. Also see my suggestion for a quote_labels argument.

    The other big thing is to whether or not the trees should be seen as rooted vs unrooted.
    Trees are always rooted in tskit as far as I know, since there's a time associated with each node and therefore always an unambiguously "deepest node". So I think adding the [&R] prefix sounds like a good idea.

    Maybe we can think about how much tskit wants to invest in data parsing/writing.

    We made a policy decision a while back that there would be very little code within tskit itself for reading-in data in other formats. There's a number of reasons for this, but mainly the reasoning is that because no other formats use the succinct tree sequence data structure internally, there's never a 1-1 mapping, and so there's an "inference" process where we need derive a tree sequence. We created the https://github.com/tskit-dev/tsconvert repo for this, as a place to keep code that manages to do this inference. It's not a formal package though and I doubt we'll every fully support it as such. It's just too hard a problem.

    In terms out output, I think we're happy to provide conversion utilities within tskit once the target format is plain text, and the process of conversion is relatively straightforward (or the format sufficiently popular, like VCF). We do want to keep our dependency list as small as possible, so adding a new dependency is something we'd weigh up carefully. Having dendopy would solve a lot of problems though, and it's nice and lightweight. There is a slight packaging annoyance which would make our lives a little difficult, in that tskit's conda package is on conda-forge while dendropy's is on bioconda so our conda package couldn't depend on it.

    What other formats do you think would be useful for tskit to support?

  4. jeetsukumaran commented on Oct 18, 2021

    @jeetsukumaran
    Contributor

    (1) Given the policy of very limited data ingest, I think it makes sense to similarly target a minimal output, and leave it to 3rd party tools to do the conversion to the full diversity of formats. This minimal output should be capable of expressing all data and metadata unambiguously. For character data, I presume you've got that covered between fasta and vcf. For the phylogenetics community the fasta is sufficient as well. For tree data, I think newick nexus is fine. Given all that, maybe just focus on getting the fasta/nexus/newick to where you are happy would be a good objective.

    With limited parsing/output format as a design principle, this can be done without dendropy as a dependency. (Incidentally, I was not aware that dendropy was even bundled with bioconda! I personally maintain it as standalone, with primary deployment is through pip/github as well as pypi. One of the explicit design goals is no third-party dependencies for ultra-straightforward installation).

    (2) Bare integer labels are syntactically correct and generally will be interpreted correctly. I would avoid them though -- if there is a TRANSLATE block, then there might be some confusion whether we are looking at the numerical index of a taxon or the label of the taxon. (To be clear: the Nexus parser has no ambiguity: it will resolve to the taxon label as a priority if there is a direct match, and only if not fall back to the numerical index interpretation. But users might get confused). Fortunately, all this can eaily be avoided by simply prepending a string or character to the number (e.g., "tsk42","t42", etc.).

    (3) As things stand, the three things that need to be added to the nexus/newick output:

    (3.1) some convention for default labeling -- this should be simple to avoid dealing with quotes etc. If there is no need for separators because the label is simply the node id (e.g., "tsk42"), then that's fine. Otherwise use "^" (e.g., "tsk^42^341").
    (3.2) some convention for handling quotes in the case of user-supplied labels (#1812)
    (3.3) inclusion of the rooted/unrooted token

    For (3.1) -- the default labeling -- it would be really nice if there is complete/unambiguous identity of labels for the same data outputted in fasta, newick, nexus, etc. formats. So, for example, a third-party program can read a fasta file for the mutations can work together with the tree file for the genealogy. (This is a place where the quoted vs unquoted underscores matters, as an underscore in a fasta label will be interpreted as an underscore, quoted or not). So maybe the labeling should be abstracted out into some library-wide function or variable or configuration that is called whenever a label is needed. And maybe even formally documented as the convention? This way it can not only should be shared across formats, but client code/programs know what to expect and can plan how to handle/manipulate the data, and would be useful as well as in terms of internal expectations for tests.

  5. jeromekelleher commented on Oct 18, 2021

    @jeromekelleher
    MemberAuthor

    These are great points, thanks @jeetsukumaran! I agree with everything you're saying, especially the behaviour of labels across the phylo-formats should be consistent so that data exported like this will "line up" correctly.

    I like "tsk42" as the convention. The only downside I think is that our VCF output uses ``tsk_42". However, these usually correspond to diploid individuals rather then individual sample nodes, so I think having it's fine getting rid of the underscore here, since there is a good argument for it. I think the existing "flags" suffix will just confuse people and is of very limited use.

  6. added 4 commits that reference this issue on Oct 22, 2021
    5af7a04
    b4a4b2a
    712417a
    66c7eb6
  7. added 3 commits that reference this issue on Nov 2, 2021
    ec4840f
    8e9308e
    8386079
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

    Python APIbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions