Skip to content

Finalise semantics of fasta output #353

Closed

Description

@jeromekelleher

Pending some issues being worked out about what do when we have floating point site positions and integer site positions (see #326, #350)

Activity

  1. added this to the Python 0.2.2 milestone on Sep 1, 2019
  2. hyanwong commented on Nov 19, 2019

    @hyanwong
    Member

    To address this issue, I do think it would be helpful to know what we plan to do about integer site positions. Do we have a consensus here? E.g. we could make a derived class, e.g. FiniteSitesTreeSequence as suggested by @petrelharp. If so we could store the site positions as true integers, or simply as floating point (and report them as integers). Alternatively, we could keep the same class, and have a flag in the metadata that tells us to interpret the existing floating point positions as integers. Other suggestions welcome too.

    I'm hoping to get @dianalir to have a go at thinking about this soon, so comments very welcome, @jeromekelleher

  3. jeromekelleher commented on Nov 20, 2019

    @jeromekelleher
    MemberAuthor

    Integer site positions aren't necessary, I think.

    I think the semantics are: if there is no reference sequence present (which there never is, right now; see #146 for discussion) we output the haplotypes as computed by the haplotypes() method. If there is a reference sequence, we use this to fill in the gaps between the variant sites.

    But for now, I think it's clear: we just output the haplotypes.

  4. hyanwong commented on Nov 20, 2019

    @hyanwong
    Member

    OK. For the record, I think that it is quite misleading to concatenate together non-adjacent sites as adjacent letters in a FASTA file. The FASTA convention is (AFAIK) for letters to represent adjacent bases, and for potential gaps (of any length) to be represented by a -. So my eventual preference would be to output a set of non-adjacent sites something like:

    A-T-T-C-G-... etc.

    I do think that would be the least misleading and most standards-conformant output. Of course, once we have integer sites (and/or reference sequences: I don't think they are mutually dependent), then in some circumstances we could omit the - character between sites.

    Perhaps I'm being too pernicketty, though?

  5. hyanwong commented on Nov 20, 2019

    @hyanwong
    Member

    (I forgot that I wrote up my thoughts on this in #326 (comment))

  6. jeromekelleher commented on Nov 20, 2019

    @jeromekelleher
    MemberAuthor

    With non-integer site positions then there's no real meaning to 'between' them. We can add the padding behaviour later as an option, but I think the only reasonable thing to do for now is just output the haplotypes. It's up to the user to put in intermediate sites if they want them.

  7. jeromekelleher commented on Nov 20, 2019

    @jeromekelleher
    MemberAuthor

    Hmmm, reading the fasta "specifications" available I'm coming around to @hyanwong's point of view. A "-" character does indicate a "gap of indeterminate length" in these, so it would be a good choice to (as Yan puts it) indicate that this is weird fasta data. I've definitely seen alignment programs that use "-" to indicate a gap of length exactly one though, so I wonder how well this is supported/interpreted.

    It might be a good exercise to see what various libraries do when they are fed with fastas containing these characters. For example, what would Pysam and BioPython make of a FASTA string "-A-T-"?

  8. hyanwong commented on Nov 20, 2019

    @hyanwong
    Member

    It is common to allow both . and - for these sort of gaps, and the A2M format uses these to distinguish - as character to represent a missing state (i.e. used as an "alignment column"), and a period (.) to "represent insertion positions between alignment columns". The A2M docs also state that "The ... periods ... are only for human readability, and may be omitted."

    So my suggestion is as follows (happy to change anything though)

    The haplotypes() function is given an optional extra parameter, e.g. pad_sites, which, if true, inserts a single dot (not a dash) between sequence letters if the site positions are non-integer (and possibly if they are integer but are missing some integer positions). However, this parameter defaults to False, so that our current behaviour is unchanged. Since haplotypes() doesn't claim to be FASTA format, this is fine.

    The write_fasta() function by default calls haplotypes(pad_sites=True), so that unlike haplotypes(), the default FASTA output does have dots between letters if the site positions are non-integer. We probably want the ability to turn this off, however - perhaps by having the function definition something like write_fasta(omit_site_padding=False).

    Either way, we still indicate samples that have missing data at a particular site with a -, in both haplotypes() and write_fasta(). There is an argument that users of haplotypes() might want to turn this off, if they only want a single sample haplotype, and don't care about aligning. This is a bit of an edge-case, however, and could be incorporated later if indeed there is any call for it.

  9. hyanwong commented on Nov 20, 2019

    @hyanwong
    Member

    p.s. I'm happy to implement my suggestion above (or whatever modification of it we agree upon), if I get the go ahead, @jeromekelleher. I can do this today, tomorrow, or Fri.

  10. jeromekelleher commented on Nov 20, 2019

    @jeromekelleher
    MemberAuthor

    Right, well this is looking like rather a lot more work than just quickly popping out a point release. I think we'll just have to undocument the fasta functionality for now, since it's controversial.

  11. hyanwong commented on Nov 21, 2019

    @hyanwong
    Member

    Yes, happy to work on this is there's agreement, with the understanding that it's pushed to 0.2.4. I think it's better to wait a little and get the format right, rather than end up having to support something that's not quite what we want.

  12. removed this from the Python 0.3.0 milestone on May 6, 2020
  13. 6 remaining items

  14. jeromekelleher commented on Dec 15, 2020

    @jeromekelleher
    MemberAuthor

    With msprime 1.0 @igronau though, tree sequences with non-integer positions are going to become much rarer as by default everything will be done on a discrete genome (like most people expect, I think). No other program generates tree sequences with floating point positions AFAIK, so I think the simplest and most robust thing to do is just not bother with continuous coordinates and wrestling with what the semantics should be.

    I'm happy to write a quick function to do what you're looking for on the current msprime output, if that would be helpful?

  15. igronau commented on Dec 15, 2020

    @igronau

    Yes @jeromekelleher , this would be very helpful.

  16. jeromekelleher commented on Dec 15, 2020

    @jeromekelleher
    MemberAuthor

    How's this @igronau?

    import msprime
    import numpy as np
    
    ts = msprime.simulate(10, length=20, mutation_rate=0.1)
    
    encoding = "ascii"
    S = np.zeros((ts.num_samples, int(ts.sequence_length)), dtype=np.int8)
    pad_value = ord("-".encode(encoding))
    S[:] = pad_value
    
    for variant in ts.variants():
        assert len(variant.alleles) == 2
        alleles = np.array(
            [ord(allele.encode(encoding)) for allele in variant.alleles],
            dtype=np.int8)
        coord = int(variant.site.position)
        if S[0, coord] == pad_value:
            # This sites hasn't been seen before, so initialise it to the
            # ancestral state
            S[:, coord] = alleles[0]
        # Update the site with any derived states
        derived_samples = variant.genotypes > 0
        S[derived_samples, coord] = alleles[variant.genotypes[derived_samples]]
    
    # print out the sequences
    for s in S:
        print(s.tobytes().decode(encoding))

    gives:

    -00-00-1110-01-11-01
    -11-11-1101-11-00-01
    -00-00-1110-01-11-01
    -00-00-1100-01-11-01
    -00-00-1100-01-11-01
    -11-11-1100-11-00-11
    -00-00-1110-01-11-01
    -11-11-1100-11-00-11
    -11-11-1101-11-00-01
    -11-11-1100-11-00-11
    

    It's easy enough to adapt to ACGT alleles, if you like. It's completely untested, so health warnings appy!

  17. igronau commented on Dec 15, 2020

    @igronau

    Looks great! Thanks, @jeromekelleher.

  18. jeromekelleher commented on Oct 18, 2021

    @jeromekelleher
    MemberAuthor

    I think we can probably resurrect this one now that discrete genome tree sequences are pretty common. My take is that we should

    • raise an exception if the tree sequence is not on a discrete genome
    • pad missing sites with some value.

    I guess we need to make some decision about whether these are zero-or-one based coordinates too. I guess the best thing to do is see if we can round-trip the data from some fasta readers and do whatever padding we need to get this. Probably we'll have to add an option though, since I guess our coordinates are inherently zero based (since we can have a position at 0).

  19. added 5 commits that reference this issue on Nov 5, 2021
    b00c873
    158abcf
    420c010
    f85954e
    2c2ea27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions