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
@DomNelson and I were just discussing the possibility of adding support for multiple chromosomes to tskit. One possibility which seems like it might be a reasonably smooth path forward is the following:
Add a chromosome table with an ID, name, length and metadata.
Add a chromosome column to the edge table. Within a chromosome, coordinates must be from 0 to the chromosome's length.
Either deprecate the sequence_length property (probably best), or make it equal to the sum of the length of all chromosomes.
For things like trees() we could add an optional chromosome argument. If the tree sequence contains multiple chromosomes which would raise an error if it's not specified. For tree sequences with a single chromosome, things would continue to work as now.
I have implemented discrete chromosomes in fwdpy11. Length refers to the
total genome length, and the recombination functions work to ensure correct
recording of transmitted edges. In practice, keeping the individual
chromosome lengths as a global variable in a python script has been
sufficient for any downstream analysis. I'm in favor of not adding more
tables, although I do see the utility for considering "real" data.
One question: if you add chromosome labels, and edges on chromosome i are
in [0,i-1), then do you plan to simplify separately by chromosome or change
how edges are sorted to include the chromosome label?
I've let edges cross "chromosome" boundaries, which occurs at 1 minus the
crossover rates between chromosomes. The nice feature of this approach is
that none of the sorting and simplifying code needed touching.
SLiM allows simulation of multiple chromosomes, in practice, by letting the user define a recombination map with a recombination rate of 0.5 between specific pairs of bases that constitute the end of one chromosome and the start of the next. With this method you can simulate as many chromosomes as you want and it works quite nicely, but it's not terribly aesthetically pleasing. We may put a layer on top of this machinery that hides this implementation detail from the user and instead lets them formally define multiple chromosomes; but I imagine it will keep working the same way under the hood, more or less. So the upshot is: like fwdpy, we're already doing this and haven't seen any particular need for a new chromosome table, etc. But if those features were to appear in tskit, we could certainly adapt to using them.
My main question is: would this just be essentially semantic information, or would some real, substantial benefit accrue as a result? If it's just semantics, then so far SLiM users don't seem to care, and I would lean toward "why complicate our lives further?" But if there were a substantial benefit, then that's another matter. Apropos of @molpopgen's question about simplification, for example: if knowing about chromosome structure would allow a 5-chromosome model to simplify much faster, or with a much lower peak memory usage, than a 1-chromosome model of the same total genome length, that would suddenly look very interesting indeed, I think.
I'd probably populate the tables when "dumping" to tskit's format. But I'm
practice, we keep everything in the fwdpp format as the metadata are much
smaller.
I may be thinking sloppily, but I'm writing from a courthouse awaiting a
jury call, but simplifying separately may "get weird", unless you track
separate node tables per chromosome? If you fully simplify chromosome
zero, and then chromosome 1 has a node specific to it and more recent than
most ancient node on chr0, you have a book keeping violation. If you build
the "global" node table to avoid this, then you've basically run the entire
algorithm, right?
On Tue, Apr 16, 2019, 1:56 PM Ben Haller ***@***.***> wrote:
SLiM allows simulation of multiple chromosomes, in practice, by letting
the user define a recombination map with a recombination rate of 0.5
between specific pairs of bases that constitute the end of one chromosome
and the start of the next. With this method you can simulate as many
chromosomes as you want and it works quite nicely, but it's not terribly
aesthetically pleasing. We may put a layer on top of this machinery that
hides this implementation detail from the user and instead lets them
formally define multiple chromosomes; but I imagine it will keep working
the same way under the hood, more or less. So the upshot is: like fwdpy,
we're already doing this and haven't seen any particular need for a new
chromosome table, etc. But if those features were to appear in tskit, we
could certainly adapt to using them.
My main question is: would this just be essentially semantic information,
or would some real, substantial benefit accrue as a result? If it's just
semantics, then so far SLiM users don't seem to care, and I would lean
toward "why complicate our lives further?" But if there were a substantial
benefit, then that's another matter. Apropos of @molpopgen
<https://github.com/molpopgen>'s question about simplification, for
example: if knowing about chromosome structure would allow a 5-chromosome
model to simplify much faster, or with a much lower peak memory usage, than
a 1-chromosome model of the same total genome length, that would suddenly
look very interesting indeed, I think.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#176 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGHnH94UubwWDbz9_XM_Km-PzCQtDUiMks5vhjjCgaJpZM4czoSO>
.
A trivial comment, but as per #29 I would like to deprecate the word "length" to refer to genome length, and replace it with "span", or something less easily confused with branch lengths. So I would vote for deprecating sequence_length (and if you go down the route of a new chromosome table, I would have ID, name, span and metadata as the columns).
I like the original idea proposed by @jeromekelleher, and don't see it as problematic to create a new table, after all, if @molpopgen recommends...
keeping the individual
chromosome lengths as a global variable in a python script
...then that's essentially the same as a small table, but the table idea has the advantage of keeping names and other metadata associated with the chromosomes. The burden would be in the extra column in the edge table.
Also, there is a major evolutionary distinction between the autosomes and the X chromosome, which makes me think that simply keeping track of boundaries along a continuous span of all chromosomes concatenated together might not be a very good idea. As far as I know, all organisms (including bacteria) have genomes organised into chromosomes, so the structure would be pretty general - although it might be worth a flag in the chromosomes table to keep track of whether the chromosome in question is circular.
Much down the line, I wonder if it would ever be possible for sites to move between chromosomes via translocation (but this is a graph-genomes question, and way off-topic)
I like the original idea proposed by @jeromekelleher, and don't see it as problematic to create a new table, after all, if @molpopgen recommends...
keeping the individual
chromosome lengths as a global variable in a python script
Kind of -- it is really just the genetic map. It is needed to parameterize the forward simulation anyways.
...then that's essentially the same as a small table, but the table idea has the advantage of keeping names and other metadata associated with the chromosomes. The burden would be in the extra column in the edge table.
The burden is that the existing algorithm breaks. If an individual inherits the following two edges, which are (chrom, left, right) tuples, and L_i is the length of chromosome i:
0 L_0 - x L_0
1 0 y,
then the edges are "sorted", but not in a way that is supported by the current code. Further, the notion of scanning with respect to the "current left position" breaks, and must be replaced by the tuple (chrom, left), with comparison operators written for nested ordering ,etc..
So, if chromosome tables are going to be optional, then you end up with two code paths for sorting and simplification. Alternately, you need to manually add a dummy chromosome id for the case where no chromosome table is present.
Also, there is a major evolutionary distinction between the autosomes and the X chromosome, which makes me think that simply keeping track of boundaries along a continuous span of all chromosomes concatenated together might not be a very good idea.
I've mocked this up in Python for sex chromosomes. It isn't that hard--there is some extra external book-keeping, but that's it. The bigger problem is downstream, running a "variant iterator" over an individual that is heterogametic.
As far as I know, all organisms (including bacteria) have genomes organised into chromosomes, so the structure would be pretty general - although it might be worth a flag in the chromosomes table to keep track of whether the chromosome in question is circular.
Circular chromosomes should be easy for forward sims--it is the responsibility of the simulator to generate proper edges when a GC event spans the "0" point of a circle.
Much down the line, I wonder if it would ever be possible for sites to move between chromosomes via translocation (but this is a graph-genomes question, and way off-topic)
One question: if you add chromosome labels, and edges on chromosome i are in [0,i-1), then do you plan to simplify separately by chromosome or change how edges are sorted to include the chromosome label?
I haven't thought this through in detail, but I imagine that we have another int32_t column on the edge table, which is -1 by default (== no chromosome), similar to the population and individual references in the Node table. We would then add this chromosome ID as the first key in the sorting order for edges, so that all edges for a given chromosome are adjacent. If there are no chromosomes, then nothing changes. The site table would also need an extra chromosome column.
In terms of simplification then, each chromosome would be simplified separately, but with respect to the same set of nodes. I'm not certain of this, but I think this should lead to some performance gains/memory reduction over having all the chromosomes in the same coordinate space.
I completely agree with you here @bhaller and @molpopgen that there's no point in doing this unless there's real gains in expressivity (i.e, being able to encode real biology that otherwise very awkward/impossible) or performance gains (e.g., being able to split up simplify). This came out of discussions with @DomNelson, who is interested in these whole-genome simulations, which are currently pretty ugly in msprime.
@hyanwong, can you think of examples in tsinfer where we'd want to be able to do this? For example, could we infer a recent ancestor across multiple chromosomes, in principle?
From a forward simulation perspective, an alternative that seems more
natural is to keep an array of table collections, and simplify each
independently. Further, I then know immediately how to parallelize the
simplification of each chromosome, and it would be lock-free.
This doesn't solve the problem for a coalescent simulation, though.
@hyanwong, can you think of examples in tsinfer where we'd want to be able to do this? For example, could we infer a recent ancestor across multiple chromosomes, in principle?
Certainly I think that we want to be able to restrict recent events such that multiple discontiguous regions (on different chromosomes) pass through a single individual ancestor. Essentially this would appear as "trapped material" if we treat all chromosomes as one long concatenated sequence. But I don't see how this is solved by creating a chromosomes table?
From a forward simulation perspective, an alternative that seems more natural is to keep an array of table collections, and simplify each independently. Further, I then know immediately how to parallelize the simplification of each chromosome, and it would be lock-free.
It's definitely attractive (and where we started when talking about this), but we'd have a lot of duplication for, say, Individuals and it would get tricky to keep the different table collection in sync. Since the whole point is to allow us to track where different chromosomes go through the same individual, I think it would cause problems...
Certainly I think that we want to be able to restrict recent events such that multiple discontiguous regions (on different chromosomes) pass through a single individual ancestor. Essentially this would appear as "trapped material" if we treat all chromosomes as one long concatenated sequence. But I don't see how this is solved by creating a chromosomes table?
Well, I guess it's not so much 'solving' as giving a framework where this can be expressed naturally and the results interpreted in a user-friendly way. Certainly it seems more user-friendly to have something like
fortreeints.trees(chromosome=22):
forsiteintree.sites():
# Do something with site which has coordinate == standard reference
vs
fortreeints.trees():
forsiteintree.sites():
ifchrs[22].start<=site.position<chrs[22].end:
pos=site.pos-chrs[22].start# Pos should now be mapped into standard coordinates
Plus, the first is clearly going to be much more efficient.
Hmm, last point was wrong. The alternative would be:
fortreeints.trees(start=chrs[22].start, end=chrs[22].end):
forsiteintree.sites():
pos=site.pos-chrs[22].start# Pos should now be mapped into standard coordinates
so the efficiency point doesn't matter. Still, seems easier/less error prone than having to subtract offsets from coordinates, doesn't it?
From a forward simulation perspective, an alternative that seems more natural is to keep an array of table collections, and simplify each independently. Further, I then know immediately how to parallelize the simplification of each chromosome, and it would be lock-free.
It's definitely attractive (and where we started when talking about this), but we'd have a lot of duplication for, say, Individuals and it would get tricky to keep the different table collection in sync. Since the whole point is to allow us to track where different chromosomes go through the same individual, I think it would cause problems...
I do individuals rather differently, so there is less overhead. For me, a bigger problem with this approach is how to deal with mutations, etc., as this would break existing data models.
I do like the idea of simplifying in parallel, though!
I haven't thought this through in detail, but I imagine that we have another int32_t column on the edge table, which is -1 by default (== no chromosome), similar to the population and individual references in the Node table. We would then add this chromosome ID as the first key in the sorting order for edges, so that all edges for a given chromosome are adjacent. If there are no chromosomes, then nothing changes. The site table would also need an extra chromosome column.
In terms of simplification then, each chromosome would be simplified separately, but with respect to the same set of nodes. I'm not certain of this, but I think this should lead to some performance gains/memory reduction over having all the chromosomes in the same coordinate space.
In essence, this changes the definition of a position from a double to a tuple/struct, which is relatively easy to accommodate.
I do like the idea of simplifying in parallel, though!
Nothing stopping you doing it within fwdpp! Whichever way we end up encoding things for the canonical representation, internally it could make a lot of sense to have an array of table collections and do a bit of bookkeeping at export time.
I suggest this issue should now be about documenting the method that SLiM is now using for multiple chromosomes, which is multiple tree sequences in one folder.
I'd certainly like to see tskit and SLiM agree on the same approach to multiple chromosome support. The term that Peter and I agreed on for "multiple tree sequences in one folder" (particularly when they represent separate chromosomes) is a "trees archive". Note that SLiM also has some top-level metadata about the chromosomes within a trees archive (some required and some optional); tskit might or might not want to adopt that. It is potentially useful for each .trees file to be annotated with information saying "this file represents such-and-such chromosome, within this set of chromosomes that are represented here"; but it is also a bit of a pain to set up that metadata (unless SLiM does it for you), and I don't know whether tskit would have any particular use for that metadata; I guess no, unless there are plans for tskit to actually do something with the fact that a set of tree sequences represent chromosomes in a trees archive. Is tskit doesn't need to know that, then setting up that metadata can be a problem for SLiM and pyslim.
We should document how to run statistics and e.g. PCAs over multiple chromosomes. One way would simply be to concatenate them all together and run the stats on that. Another would be to run stats separately and then merge them together in a weighed manner. I guess the weighting would be relatively easy, but maybe we want code or even a method, so that people don't make mistakes?
If we wanted to weight PCAs, can we just combine the weighted eigenvalues? I'm not sure, TBH, so recommendations would be useful even for tskit experts.
We should document how to run statistics and e.g. PCAs over multiple chromosomes. One way would simply be to concatenate them all together and run the stats on that. Another would be to run stats separately and then merge them together in a weighed manner. I guess the weighting would be relatively easy, but maybe we want code or even a method, so that people don't make mistakes?
I would find this immediately useful, in fact; I'm about to need to do this.
Great. Perhaps you will have some suggestions as to how we make this easy for people?
You can use the new ts.concatenate() command as a test if you are in python, only there's a bug if there are internal samples, that @petrelharp and I have to fix: #3183 (comment)
Great. Perhaps you will have some suggestions as to how we make this easy for people?
You can use the new ts.concatenate() command as a test if you are in python, only there's a bug if there are internal samples, that @petrelharp and I have to fix.
I fear I am out of my depth, which is why a tutorial would be great. :-> But the use case I'm thinking of will just require calculating diversity, using that tskit function, and (I think?) doing a weighted average across chromosomes, so I think it is on the trivial end of what you're talking about. (Still a bit challenging for me since I don't really know Python, but I'll figure it out! :->) Re: PCA and such, I have absolutely no idea, sorry. :->
dirname="/Users/yan/Downloads/Yan_trees_archive"chroms= [tszip.load(os.path.join(dirname, filename)) forfilenameinos.listdir(dirname) iffilename.endswith(".trees")]
# First way: weighted averagesequence_lengths= [ts.sequence_lengthfortsinchroms]
genome_diversity1=np.average([ts.diversity() fortsinchroms], weights=sequence_lengths)
# Second way: concatenate them all together, changing the genome position values in all but the first tsbig_ts=chroms[0].concatenate(*chroms[1:]) # requires tskit >= 0.6.4genome_diversity=big_ts.diversity()
(Untested: could you maybe post a tiny example of a folder of tree sequences from a trivial multi-chrom SLiM simulation, so I can test they give the same results?)
(Untested: could you maybe post a tiny example of a folder of tree sequences from a trivial multi-chrom SLiM simulation, so I can test they give the same results?)
It produces a two-chromosome simulation where the first chromosome is all neutral and the second chromosome has a coding region in the middle that gets deleterious mutations as well as neutral mutations; here's a screenshot from SLiMgui:
It writes out a trees archive at the end. Here's a zip of it:
I wonder whether your analysis code will work with this directly, or whether – because these are SLiM mutations, with weird metadata and weird derived states – you'll need to preprocess the mutations with pyslim first? I'm not sure about that, but of course @petrelharp will know. :->
Regarding weighting by chromosome length - this is needed in case where we span normalise. For some statistics, normalising by default makes sense, say for diversity. For ratio statistics, say Fst, we must be careful as we usually don't want a weighted average of ratios (ratio is a derived statistic, if that's the right term), but we can calculate overall/genome non-normalised primary statistic values across chromosomes and then normalise. For some statistics, we might not need/want to normalise. So, it might depend on the statistic and how they are used, on how we want to combine across chromosomes. @petrelharp is likely the best reference here for these details. I am happy to contribute to tutorial (as much as I can) - we have had discusssed in our lab on how to combine results from multi chromosomes several times. Below are some discussions we had on our lab Slack:
Average (or variance, ...) of a statistic across chromosomes is useful to look at variation between chromosomes, but shows average (or variance, ...) statistic across chromosomes, and not the total/sum? statistic across the whole genome. The same principle applies also when looking at regions of a chromosome - average across regions of a chromosome does not give the chromosome statistic.
All the primary statistics defined here are sums across locations in the genome: something is computed for each position, and these values are added up across all positions in each window. Whether the total span of the window is then taken into account is determined by the option span_normalise: if it is True (the default), the sum for each window is converted into an average, by dividing by the window's span (i.e. the length of genome that it covers). Otherwise, the sum itself is returned. The default is span_normalise=True, because this makes the values comparable across windows of different sizes.
So, imagine that we would have two chromosomes (Chr1 and Chr2) stitched together into one long chromosome (Chr1+2). Then calculating a statistic in a non-span-normalised-way across this long chromosome would give us a sum of that statistic across all sites (or branches) over this long chromosome. If we would want to span-normalise this statistic, we would then have to divide by the span of both chromosomes.
One strategy to get the single value is then to calculate non-span-normalised stat per chromosome, sum them up across chromosomes, then divide by the total span of all chromosomes. This should not be hard. The other way around would be to get span-normalised stats per chromosome and then to add them up we would have to multiply each of these by (chromosome-span / genome-span). I attach photo of this.
It would be neat to demonstrate these concepts (of average and variance of a statistic per tree/region/chromosome, and total per genome) with the above example from @bhaller! Maybe adding one more chromosome would make it more interesting (to calculate variance). I don't know well enough how to work with SLiM tree sequences, but can help with analysis side of things - what a user would want to look at in such a case @hyanwong
I could make one of the chromosomes a sex chromosome, too, but then life gets complicated due to null haplosomes and vacant nodes, so maybe not. I don't really understand the conversation you guys are having (paying only superficial attention to it), but I'm happy to further customize this model if it's useful to you. :->
Well, my code above works fine (adjusted for a typo), with some rounding error. Here it is, slightly recoded to do the span division at the end.
# First way: weighted averagetot_seq_len=sum([ts.sequence_lengthfortsinchroms])
genome_diversity1=np.average([ts.diversity(span_normalise=False) fortsinchroms]) /tot_seq_len# Second way: concatenate them all together, changing the genome position values in all but the first tsbig_ts=chroms[0].concatenate(*chroms[1:]) # requires tskit >= 0.6.4genome_diversity2=big_ts.diversity()
print(genome_diversity1, genome_diversity2)
> 0.00021951808964519923 0.00021933149416082805
Note, however, that some stats (the ones that don't take span_normalise, like Tajimas_D) can't be averaged across chromosomes in this way, so the second way is what will work. I deliberately changed to use span_normalise=False in the code above to avoid giving the wrong answer in these cases.
Note that discussion and development have moved to https://github.com/hyanwong/tsgroup. The main sticking point here is how to deal with cases where the chromosomes within an individual are not phased wrt each other, which is not something that we discuss above. The requirement deal with this means that adding e.g. and extra column to edges, even if we didn't care about breaking the tskit-1.0 model, won't fly. So we are definitely left with the idea of having multiple tree sequences with some shared tables, as is currently implemented in SLiM.
Activity
molpopgen commentedon Apr 16, 2019
molpopgen commentedon Apr 16, 2019
bhaller commentedon Apr 16, 2019
SLiM allows simulation of multiple chromosomes, in practice, by letting the user define a recombination map with a recombination rate of 0.5 between specific pairs of bases that constitute the end of one chromosome and the start of the next. With this method you can simulate as many chromosomes as you want and it works quite nicely, but it's not terribly aesthetically pleasing. We may put a layer on top of this machinery that hides this implementation detail from the user and instead lets them formally define multiple chromosomes; but I imagine it will keep working the same way under the hood, more or less. So the upshot is: like fwdpy, we're already doing this and haven't seen any particular need for a new chromosome table, etc. But if those features were to appear in tskit, we could certainly adapt to using them.
My main question is: would this just be essentially semantic information, or would some real, substantial benefit accrue as a result? If it's just semantics, then so far SLiM users don't seem to care, and I would lean toward "why complicate our lives further?" But if there were a substantial benefit, then that's another matter. Apropos of @molpopgen's question about simplification, for example: if knowing about chromosome structure would allow a 5-chromosome model to simplify much faster, or with a much lower peak memory usage, than a 1-chromosome model of the same total genome length, that would suddenly look very interesting indeed, I think.
molpopgen commentedon Apr 16, 2019
hyanwong commentedon Apr 17, 2019
A trivial comment, but as per #29 I would like to deprecate the word "length" to refer to genome length, and replace it with "span", or something less easily confused with branch lengths. So I would vote for deprecating
sequence_length(and if you go down the route of a newchromosometable, I would have ID, name, span and metadata as the columns).I like the original idea proposed by @jeromekelleher, and don't see it as problematic to create a new table, after all, if @molpopgen recommends...
...then that's essentially the same as a small table, but the table idea has the advantage of keeping names and other metadata associated with the chromosomes. The burden would be in the extra column in the edge table.
Also, there is a major evolutionary distinction between the autosomes and the X chromosome, which makes me think that simply keeping track of boundaries along a continuous span of all chromosomes concatenated together might not be a very good idea. As far as I know, all organisms (including bacteria) have genomes organised into chromosomes, so the structure would be pretty general - although it might be worth a flag in the chromosomes table to keep track of whether the chromosome in question is circular.
Much down the line, I wonder if it would ever be possible for sites to move between chromosomes via translocation (but this is a graph-genomes question, and way off-topic)
molpopgen commentedon Apr 17, 2019
Kind of -- it is really just the genetic map. It is needed to parameterize the forward simulation anyways.
The burden is that the existing algorithm breaks. If an individual inherits the following two edges, which are (chrom, left, right) tuples, and L_i is the length of chromosome i:
0 L_0 - x L_0
1 0 y,
then the edges are "sorted", but not in a way that is supported by the current code. Further, the notion of scanning with respect to the "current left position" breaks, and must be replaced by the tuple (chrom, left), with comparison operators written for nested ordering ,etc..
So, if chromosome tables are going to be optional, then you end up with two code paths for sorting and simplification. Alternately, you need to manually add a dummy chromosome id for the case where no chromosome table is present.
I've mocked this up in Python for sex chromosomes. It isn't that hard--there is some extra external book-keeping, but that's it. The bigger problem is downstream, running a "variant iterator" over an individual that is heterogametic.
As far as I know, all organisms (including bacteria) have genomes organised into chromosomes, so the structure would be pretty general - although it might be worth a flag in the chromosomes table to keep track of whether the chromosome in question is circular.
Circular chromosomes should be easy for forward sims--it is the responsibility of the simulator to generate proper edges when a GC event spans the "0" point of a circle.
jeromekelleher commentedon Apr 17, 2019
I haven't thought this through in detail, but I imagine that we have another
int32_tcolumn on the edge table, which is-1by default (== no chromosome), similar to thepopulationandindividualreferences in the Node table. We would then add thischromosomeID as the first key in the sorting order for edges, so that all edges for a given chromosome are adjacent. If there are no chromosomes, then nothing changes. The site table would also need an extrachromosomecolumn.In terms of simplification then, each chromosome would be simplified separately, but with respect to the same set of nodes. I'm not certain of this, but I think this should lead to some performance gains/memory reduction over having all the chromosomes in the same coordinate space.
I completely agree with you here @bhaller and @molpopgen that there's no point in doing this unless there's real gains in expressivity (i.e, being able to encode real biology that otherwise very awkward/impossible) or performance gains (e.g., being able to split up simplify). This came out of discussions with @DomNelson, who is interested in these whole-genome simulations, which are currently pretty ugly in msprime.
@hyanwong, can you think of examples in tsinfer where we'd want to be able to do this? For example, could we infer a recent ancestor across multiple chromosomes, in principle?
molpopgen commentedon Apr 17, 2019
hyanwong commentedon Apr 17, 2019
Certainly I think that we want to be able to restrict recent events such that multiple discontiguous regions (on different chromosomes) pass through a single individual ancestor. Essentially this would appear as "trapped material" if we treat all chromosomes as one long concatenated sequence. But I don't see how this is solved by creating a chromosomes table?
jeromekelleher commentedon Apr 17, 2019
It's definitely attractive (and where we started when talking about this), but we'd have a lot of duplication for, say, Individuals and it would get tricky to keep the different table collection in sync. Since the whole point is to allow us to track where different chromosomes go through the same individual, I think it would cause problems...
jeromekelleher commentedon Apr 17, 2019
Well, I guess it's not so much 'solving' as giving a framework where this can be expressed naturally and the results interpreted in a user-friendly way. Certainly it seems more user-friendly to have something like
vs
Plus, the first is clearly going to be much more efficient.
jeromekelleher commentedon Apr 17, 2019
Hmm, last point was wrong. The alternative would be:
so the efficiency point doesn't matter. Still, seems easier/less error prone than having to subtract offsets from coordinates, doesn't it?
molpopgen commentedon Apr 17, 2019
I do individuals rather differently, so there is less overhead. For me, a bigger problem with this approach is how to deal with mutations, etc., as this would break existing data models.
I do like the idea of simplifying in parallel, though!
molpopgen commentedon Apr 17, 2019
In essence, this changes the definition of a position from a double to a tuple/struct, which is relatively easy to accommodate.
jeromekelleher commentedon Apr 17, 2019
Nothing stopping you doing it within fwdpp! Whichever way we end up encoding things for the canonical representation, internally it could make a lot of sense to have an array of table collections and do a bit of bookkeeping at export time.
19 remaining items
[-]Multiple chromosomes[/-][+]Document multiple chromosomes[/+]bhaller commentedon Jun 5, 2025
I'd certainly like to see tskit and SLiM agree on the same approach to multiple chromosome support. The term that Peter and I agreed on for "multiple tree sequences in one folder" (particularly when they represent separate chromosomes) is a "trees archive". Note that SLiM also has some top-level metadata about the chromosomes within a trees archive (some required and some optional); tskit might or might not want to adopt that. It is potentially useful for each .trees file to be annotated with information saying "this file represents such-and-such chromosome, within this set of chromosomes that are represented here"; but it is also a bit of a pain to set up that metadata (unless SLiM does it for you), and I don't know whether tskit would have any particular use for that metadata; I guess no, unless there are plans for tskit to actually do something with the fact that a set of tree sequences represent chromosomes in a trees archive. Is tskit doesn't need to know that, then setting up that metadata can be a problem for SLiM and pyslim.
hyanwong commentedon Jun 10, 2025
We should document how to run statistics and e.g. PCAs over multiple chromosomes. One way would simply be to concatenate them all together and run the stats on that. Another would be to run stats separately and then merge them together in a weighed manner. I guess the weighting would be relatively easy, but maybe we want code or even a method, so that people don't make mistakes?
If we wanted to weight PCAs, can we just combine the weighted eigenvalues? I'm not sure, TBH, so recommendations would be useful even for tskit experts.
bhaller commentedon Jun 10, 2025
I would find this immediately useful, in fact; I'm about to need to do this.
hyanwong commentedon Jun 10, 2025
Great. Perhaps you will have some suggestions as to how we make this easy for people?
You can use the new
ts.concatenate()command as a test if you are in python, only there's a bug if there are internal samples, that @petrelharp and I have to fix: #3183 (comment)bhaller commentedon Jun 10, 2025
I fear I am out of my depth, which is why a tutorial would be great. :-> But the use case I'm thinking of will just require calculating diversity, using that tskit function, and (I think?) doing a weighted average across chromosomes, so I think it is on the trivial end of what you're talking about. (Still a bit challenging for me since I don't really know Python, but I'll figure it out! :->) Re: PCA and such, I have absolutely no idea, sorry. :->
hyanwong commentedon Jun 10, 2025
Well, the two ways are equivalent to:
(Untested: could you maybe post a tiny example of a folder of tree sequences from a trivial multi-chrom SLiM simulation, so I can test they give the same results?)
bhaller commentedon Jun 10, 2025
Sure! Here's a pretty simple SLiM script:
It produces a two-chromosome simulation where the first chromosome is all neutral and the second chromosome has a coding region in the middle that gets deleterious mutations as well as neutral mutations; here's a screenshot from SLiMgui:
It writes out a trees archive at the end. Here's a zip of it:
Yan_trees_archive.zip
I wonder whether your analysis code will work with this directly, or whether – because these are SLiM mutations, with weird metadata and weird derived states – you'll need to preprocess the mutations with pyslim first? I'm not sure about that, but of course @petrelharp will know. :->
gregorgorjanc commentedon Jun 10, 2025
Regarding weighting by chromosome length - this is needed in case where we span normalise. For some statistics, normalising by default makes sense, say for diversity. For ratio statistics, say Fst, we must be careful as we usually don't want a weighted average of ratios (ratio is a derived statistic, if that's the right term), but we can calculate overall/genome non-normalised primary statistic values across chromosomes and then normalise. For some statistics, we might not need/want to normalise. So, it might depend on the statistic and how they are used, on how we want to combine across chromosomes. @petrelharp is likely the best reference here for these details. I am happy to contribute to tutorial (as much as I can) - we have had discusssed in our lab on how to combine results from multi chromosomes several times. Below are some discussions we had on our lab Slack:
Average (or variance, ...) of a statistic across chromosomes is useful to look at variation between chromosomes, but shows average (or variance, ...) statistic across chromosomes, and not the total/sum? statistic across the whole genome. The same principle applies also when looking at regions of a chromosome - average across regions of a chromosome does not give the chromosome statistic.
Today we touched upon what does one do with statistics calculated per chromosome if we want just "one value somehow aggregated" across chromosomes. The relevant point in tskit documentation is this https://tskit.dev/tskit/docs/stable/stats.html#span-normalise - this does not mention anything about different chromosomes, but note this (copied from the docs):
All the primary statistics defined here are sums across locations in the genome: something is computed for each position, and these values are added up across all positions in each window. Whether the total span of the window is then taken into account is determined by the option span_normalise: if it is True (the default), the sum for each window is converted into an average, by dividing by the window's span (i.e. the length of genome that it covers). Otherwise, the sum itself is returned. The default is span_normalise=True, because this makes the values comparable across windows of different sizes.
So, imagine that we would have two chromosomes (Chr1 and Chr2) stitched together into one long chromosome (Chr1+2). Then calculating a statistic in a non-span-normalised-way across this long chromosome would give us a sum of that statistic across all sites (or branches) over this long chromosome. If we would want to span-normalise this statistic, we would then have to divide by the span of both chromosomes.
One strategy to get the single value is then to calculate non-span-normalised stat per chromosome, sum them up across chromosomes, then divide by the total span of all chromosomes. This should not be hard. The other way around would be to get span-normalised stats per chromosome and then to add them up we would have to multiply each of these by (chromosome-span / genome-span). I attach photo of this.
gregorgorjanc commentedon Jun 10, 2025
gregorgorjanc commentedon Jun 10, 2025
It would be neat to demonstrate these concepts (of average and variance of a statistic per tree/region/chromosome, and total per genome) with the above example from @bhaller! Maybe adding one more chromosome would make it more interesting (to calculate variance). I don't know well enough how to work with SLiM tree sequences, but can help with analysis side of things - what a user would want to look at in such a case @hyanwong
bhaller commentedon Jun 10, 2025
OK, sure; here's a new model with a third chromosome:
Note each chromosome has different length, mutation rate, and recombination rate, just to make them different. Here's a new snapshot from SLiMgui:
And here's the new zipped trees archive:
Yan_trees_archive.zip
I could make one of the chromosomes a sex chromosome, too, but then life gets complicated due to null haplosomes and vacant nodes, so maybe not. I don't really understand the conversation you guys are having (paying only superficial attention to it), but I'm happy to further customize this model if it's useful to you. :->
hyanwong commentedon Jun 10, 2025
Well, my code above works fine (adjusted for a typo), with some rounding error. Here it is, slightly recoded to do the span division at the end.
Note, however, that some stats (the ones that don't take span_normalise, like Tajimas_D) can't be averaged across chromosomes in this way, so the second way is what will work. I deliberately changed to use
span_normalise=Falsein the code above to avoid giving the wrong answer in these cases.hyanwong commentedon Jul 8, 2026
Note that discussion and development have moved to https://github.com/hyanwong/tsgroup. The main sticking point here is how to deal with cases where the chromosomes within an individual are not phased wrt each other, which is not something that we discuss above. The requirement deal with this means that adding e.g. and extra column to edges, even if we didn't care about breaking the tskit-1.0 model, won't fly. So we are definitely left with the idea of having multiple tree sequences with some shared tables, as is currently implemented in SLiM.