You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove C support for haplotypes and implement in Python #326
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)
forvarints.variants():
alleles=np.array(ord(allele) foralleleinvar.alleles) # Throw an error if allele isn't single charH[:, var.site.id] =alleles[var.genotypes]
forhinH:
# Do some numpy trickery like we use in VCF to change to a utf8 encoded stringyieldnumpy_trickery(h)
Then we can deal with missing data much more simply and can get rid of a bunch of repetitious C code.
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.
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:
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
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.
Activity
hyanwong commentedon Aug 23, 2019
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 ahaplotypes()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.
hyanwong commentedon Aug 31, 2019
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_stateandtables.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:
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
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 lengthsequence_lengthto be passed tohaplotypes(), 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.positionFinally (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 usingnp.modhere 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 likets.integer_site_positionswhich gives fast access to this.petrelharp commentedon Sep 2, 2019
Much of this seems like a different discussion, to be discussed in #146.