Skip to content

Parsimony algorithm. - #125

Merged
jeromekelleher merged 8 commits into
tskit-dev:masterfrom
jeromekelleher:sankoff
Jul 17, 2019
Merged

jeromekelleher merged 8 commits into
tskit-dev:masterfrom
jeromekelleher:sankoff

Conversation

@jeromekelleher

Copy link
Copy Markdown
Member

Closes #99

WIP.

@codecov

codecov Bot commented Feb 15, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #125 into master will decrease coverage by 0.83%.
The diff coverage is 93.8%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #125      +/-   ##
==========================================
- Coverage   86.47%   85.64%   -0.84%     
==========================================
  Files          18       19       +1     
  Lines        9812    13735    +3923     
  Branches     1777     2665     +888     
==========================================
+ Hits         8485    11763    +3278     
- Misses        788     1022     +234     
- Partials      539      950     +411
Flag Coverage Δ
#c_tests 86.56% <95.15%> (+0.09%) ⬆️
#python_c_tests 89.9% <91.8%> (?)
#python_tests 99.16% <100%> (ø) ⬆️
Impacted Files Coverage Δ
python/tskit/trees.py 98.57% <100%> (+0.01%) ⬆️
c/tskit/core.c 82.47% <100%> (+0.28%) ⬆️
c/tskit/convert.c 77.2% <100%> (ø) ⬆️
python/_tskitmodule.c 83.22% <88.88%> (ø)
c/tskit/trees.c 90.38% <93.45%> (+0.15%) ⬆️
c/tskit/genotypes.c 87.5% <97.22%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d03ba17...2ad37b0. Read the comment docs.

@jeromekelleher

Copy link
Copy Markdown
Member Author

I think this is ready for merging, so it would be good to get some review. The documentation on the new reconstruct method should explain what this is for, so I won't repeat it here.

I'm not entirely sure the interface is right (is "reconstruct" the right word?), so any thoughts on this would be appreciated.

The implementation is fast: finding a parsimonious explanation for an arbitrary set of genotypes for a tree with 100K samples takes < 0.01 seconds.

Pinging @hyanwong and @petrelharp for thoughts/review.

@jeromekelleher

Copy link
Copy Markdown
Member Author

TODO: this needs to handle missing data.

@hyanwong

Copy link
Copy Markdown
Member

finding a parsimonious explanation for an arbitrary set of genotypes for a tree with 100K samples takes < 0.01 seconds.

Wow. That's seriously impressive. I will try to look through this, but may have more questions than answers.

@jeromekelleher

Copy link
Copy Markdown
Member Author

Just thought of an issue here with not having a consistent mapping of genotype value to base pair. It doesn't matter for Fitch, but for Sankoff we need a transition matrix where we need a consistent mapping from 0 -> "A", 1 -> "C" etc for the matrix to make sense. Options are:

  1. Insist on a single cost matrix and make it the users responsibility of knowing how the alleles are encoded, with the correct cost matrix.
  2. Pass a list of m cost matrices, one per site, which can be of different sizes. This allows to have different matrices per site, but is a bit cumbersome. I guess we could have a higher level description of the matrix, using the list of m allele lists to translate?

@hyanwong

hyanwong commented Mar 1, 2019

Copy link
Copy Markdown
Member

Presumably most sites will have SNP-type variants, but some will be indels etc, which could be accounted for by having different cost matrices.

I assume by 2. you mean having a list of matrices, with each site containing an index into the list? In other words, it's a bit like an additional tskit table, the "cost" table, which gives both the allele encoding and the cost of transition.

@jeromekelleher

Copy link
Copy Markdown
Member Author

Presumably most sites will have SNP-type variants, but some will be indels etc, which could be accounted for by having different cost matrices.

Yes, exactly; it would be nice to have this generality.

I assume by 2. you mean having a list of matrices, with each site containing an index into the list? In other words, it's a bit like an additional tskit table, the "cost" table, which gives both the allele encoding and the cost of transition.

Yes, that's a good way of looking at it.

@jeromekelleher jeromekelleher changed the title Sankoff parsimony algorithm. Parsimony algorithm. Mar 7, 2019
@petrelharp

Copy link
Copy Markdown
Contributor

I may not be totally following here, but: you are wondering what about when the entries in the genotype matrix aren't consistently coded? This seems overly accomodating, and easy to deal with on the user's side. If all mutations are ACGT, then it is not hard to readjust the genotype matrix to be consistent, if this has not been done already. In fact, this seems easier than trying to wrangle the list of per-site cost matrices. If the user has more than one type of mutation, they could separate out the genotype sub-matrices corresponding to different cost matrices and pass those in.

@petrelharp

Copy link
Copy Markdown
Contributor

I'm not entirely sure the interface is right (is "reconstruct" the right word?), so any thoughts on this would be appreciated.

"Reconstruct" makes me think you're reconstructing the tree maybe. I think the more usual terminology for this is "mapping" the mutations on the tree? Maybe the method should be called .map_mutations?

@petrelharp

Copy link
Copy Markdown
Contributor

For the future: it would be natural to make this a method of a tree sequence, and give also a vector of positions, so that this happens for each site on the tree for its site.

@jeromekelleher

Copy link
Copy Markdown
Member Author

Thanks @petrelharp, I like map_mutations, lets go with that.

@jeromekelleher

Copy link
Copy Markdown
Member Author

This is ready to go I think. Review would be appreciated here @petrelharp and @hyanwong!

@jeromekelleher

Copy link
Copy Markdown
Member Author

I'd like to get this one squared away soon, if possible. @hyanwong, would you mind taking a look through please and see if you agree with the high-level API and docs? This will be very useful in tsinfer, as we can get rid of the slow and crappy code for dealing with non-inference sites. Also, it has the first steps for dealing with missing data (missing genotype is -1, and genotypes are now signed).

@hyanwong

Copy link
Copy Markdown
Member

Right - will make this a priority today & tomorrow. Have to go for 2 hrs now but will be on this when I return.

@petrelharp

Copy link
Copy Markdown
Contributor

alert @bhaller vargen will be changing:

Change genotypes from unsigned to signed to accomodate missing data
  (see :issue:`144` for discussion). This only affects users of the
  ``tsk_vargen_t`` class.

@bhaller

bhaller commented Jul 15, 2019

Copy link
Copy Markdown

alert @bhaller vargen will be changing:

Change genotypes from unsigned to signed to accomodate missing data
  (see :issue:`144` for discussion). This only affects users of the
  ``tsk_vargen_t`` class.

Thanks for the heads-up. Whenever things are in a state where it makes sense to pull over the new code from tskit into SLiM, I can coordinate with you on that; up to you. (Well, until August 5th or so, and then I'll be busy until mid-September.)

Comment thread docs/tutorial.rst Resolved
Comment thread docs/tutorial.rst Resolved
Comment thread python/tskit/trees.py Resolved
Comment thread docs/tutorial.rst Resolved
Comment thread python/_tskitmodule.c Resolved
Comment thread python/requirements/development.txt Resolved
Comment thread python/tskit/trees.py Resolved
Comment thread docs/examples.py Resolved
Comment thread docs/examples.py Resolved
Comment thread docs/examples.py Resolved
Comment thread python/tskit/trees.py Resolved
Comment thread c/tskit/trees.c Resolved
@petrelharp

Copy link
Copy Markdown
Contributor

Looks great!

@jeromekelleher

jeromekelleher commented Jul 16, 2019 •

Copy link
Copy Markdown
Member Author

Thanks @petrelharp! Quick question: after discussing this with Yan, I think it's better if we change the API to

def map_mutations(self, genotypes, alleles):
      # do stuff
      return ancestral_state, mutations

Where alleles is a list of strings, and we now return the ancestral state as a string and return a list actual mutation objects rather than StateTransitions. So, we map exactly to the site/mutation data model, rather than the slightly different integer encodings, which seems confusing to me now.

An advantage of this is that the cost_matrix we want to pass in then can either be a 2D numpy array mapping to the integer encodings of the alleles, OR a dictionary mappying allele pairs to costs. So, we could have:

alleles = ["A", "T"]
costs = {("A", "T"): 0.1, ("T", "A"): 0.2)}
# or 
costs = [
    [0, 0.1],
    [0.2, 0]]

This would be nice and general I think, and should be reasonably intuitive in either direction.

Any objections?

@petrelharp

Copy link
Copy Markdown
Contributor

That sounds much nicer! I'm not sure that specifying a dictionary mapping of pairs is actually useful, though, unless you're thinking of cases where some transitions are not possible.

@jeromekelleher

Copy link
Copy Markdown
Member Author

That sounds much nicer! I'm not sure that specifying a dictionary mapping of pairs is actually useful, though, unless you're thinking of cases where some transitions are not possible.

Great, I'll do that and merge. The advantage of the dictionary mapping pairs that the encoding 0->"A", 1->"C" etc can change from site to site (as it does with tskit's output). So, we can specify a single cost mapping for all sites without having to worry about permuting the numpy matrix each site. Anyway, we don't have to worry about that for now - the door is open to both approaches.

@hyanwong

Copy link
Copy Markdown
Member

So, we can specify a single cost mapping for all sites without having to worry about permuting the numpy matrix each site.

This is a big advantage, in my book.

@jeromekelleher
jeromekelleher merged commit 70ce4c2 into tskit-dev:master Jul 17, 2019
@jeromekelleher
jeromekelleher deleted the sankoff branch July 17, 2019 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Method to position mutations to explain genotypes

4 participants