Skip to content

Remove C support for haplotypes and implement in Python #326

Closed

Description

@jeromekelleher

There's no real point in having the C haplotype generator any more when we can replace it with some numpy code that'll be more-or-less as fast. It'll look something like:

H = np.empty((ts.num_samples, ts.num_sites), dtype=np.int8)

for var in ts.variants():
    alleles = np.array(ord(allele) for allele in var.alleles) # Throw an error if allele isn't single char
    H[:, var.site.id] = alleles[var.genotypes]
for h in H:
    # Do some numpy trickery like we use in VCF to change to a utf8 encoded string
    yield numpy_trickery(h)

Then we can deal with missing data much more simply and can get rid of a bunch of repetitious C code.

Activity

  1. added this to the Python 0.2.1 milestone on Aug 22, 2019
  2. hyanwong commented on Aug 23, 2019

    @hyanwong
    Member

    This might be obvious, but for "real data", we might well want haplotypes() to return full sequences, including non-variable sites. Otherwise we may get what looks like a contiguous DNA sequence out of a haplotypes() method, but which is actually a pasting together of only the variable sites. I've stupidly just been bitten by this.

    At the moment I'm doing this in simulations by adding all sites into the tree sequence, including non-variable ones. But perhaps we (a) want to warn when producing a haplotype string with integer site positions that are not incrementing by 1 unit each site and/or (b) have a haplotype-rendering method that takes a reference sequence and fills in the missing sites with data from the reference (or perhaps puts a "." at missing integer sites)

    Obviously these points are only really valid for tree sequences with integerized site positions.

  3. hyanwong commented on Aug 31, 2019

    @hyanwong
    Member

    When outputting haplotypes, I think we need to distinguish between (a) tree sequences that using the (msprime default) infinite sites mutation model and (b) tree sequences that have sites at integer positions (and hence probably use finite mutation models ). As I see it, there are two possible output formats:

    • Each haplotype is output as a 1D numpy array. This makes sense in both (a) and (b): we might want to (efficiently if possible) iterate over samples, producing "haplotypes" that are essentially the columns of the genotype_matrix() (i.e. an array of ints).

    • Each haplotype is output as a string (i.e. a concatenated series of the characters in tables.sites.ancestral_state and tables.mutations.derived_state), in something like FASTA format. This is trickier. Firstly, we probably want to make each allele of the same number of characters (or, as we do at present, restrict this sort of output to only having 1-character alleles). Secondly, we will usually want to indicate that there are gaps between the sites: this is definitely the case for (a) and may be the case for (b) if we do not have all the integer sites in the sites table.

    The first case is uncontroversial. We probably only need a function that does this if there is a more efficient way of doing it than iterating column-wise over the genotype_matrix.

    The second case needs some thought. I am inclined to use the A2M format, which is the subset of FASTA used in SAMtools. This gives us the - character to represent a missing state used as an "alignment column", and a period (.) to "represent insertion positions between alignment columns" - as the docs state, "The ... periods ... are only for human readability, and may be omitted."

    So how about the following:

    1. For tree sequences with non-integer site positions, we separate each allele by a single dot when outputting a haplotype string. This is easy to strip out, but makes the point that these are not necessarily evenly spaced sites

    2. We have a flag for the haplotypes method which is only relevant when we detect a tree sequence with sites at integerized positions (see below). We use this to change between the following alternative behaviours:
      i. As with non-integer sites, we simply separate each site with a single dot
      ii. We place a dot for each integer position which is not in sites.position - this can lead to strings of many dots in a row.
      iii. Like ii. but Instead of a dot we place a "reference sequence" letter (possibly in lowercase) for each integer position which is not in sites.position. It would be relatively easy to require a reference string of length sequence_length to be passed to haplotypes(), but this might be considered by some here as library overreach.
      iv. We throw an error if we don't have every integer position from (0 .. 1-sequence_length) in sites.position

    Finally (and slightly off-topic), I wonder if there is any sensible way that we should be able to flag up a tree sequence as having integer vs non-integer site positions, other than (somewhat tediously) checking np.all(np.mod(tables.sites.position[:], 1)==0)) (NB: I don't know if using np.mod here opens us up to rounding errors, but I think not). It might be useful to check this on creation of the TS, and have a boolean property like ts.integer_site_positions which gives fast access to this.

  4. petrelharp commented on Sep 2, 2019

    @petrelharp
    Contributor

    Much of this seems like a different discussion, to be discussed in #146.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions