Skip to content

Commit 3bb9f3e

Browse files
authored
Merge pull request #296 from hyanwong/statgen
Finish the "intro to popgen tutorial"
2 parents b75a6d1 + 7a6f3e5 commit 3bb9f3e

2 files changed

Lines changed: 162 additions & 38 deletions

File tree

popgen.md

Lines changed: 157 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ plt.show()
115115
```
116116

117117
Genomic data in tree sequence format can be generated via the widely-used
118-
[msprime](https://tskit.dev/software/msprime.html) simulator. Here we simulate 20
119-
kilobases of genome sequence at the start of human chromosome 1 under this model,
118+
[msprime](https://tskit.dev/software/msprime.html) simulator. Here we simulate 1
119+
megabase of genome sequence at the start of human chromosome 1 under this model,
120120
together with its evolutionary history. We generate 16 diploid genomes: 4 from each of
121121
the populations in the model. The DNA sequences and their ancestry are stored in a
122122
succinct tree sequence named `ts`:
123123

124124
```{code-cell}
125-
contig = species.get_contig("chr1", mutation_rate=model.mutation_rate, right=20_000)
125+
contig = species.get_contig("chr1", mutation_rate=model.mutation_rate, right=1_000_000)
126126
samples = {"AFR": 4, "EUR": 4, "ASIA": 4, "ADMIX": 4} # 16 diploid samples
127127
engine = stdpopsim.get_engine("msprime")
128128
ts = engine.simulate(model, contig, samples, seed=9).trim() # trim to first 20kb simulated
@@ -135,24 +135,44 @@ along the genome:
135135

136136
```{code-cell}
137137
for v in ts.variants():
138-
display(v)
139-
if v.site.id >= 2: # Only show site 0, 1, and 2, for brevity
138+
print(
139+
f"Variable site {v.site.id} at position {v.site.position} has allele frequencies",
140+
{state: f"{freq:.1%}" for state, freq in v.frequencies().items()}
141+
)
142+
if v.site.id > 4:
143+
print("...")
140144
break
141145
```
142146

143-
Or we can display the {meth}`~TreeSequence.haplotypes` (i.e. the variable sites) for
144-
each sample
147+
Or we can efficiently grab the genotypes for each sampled genome
145148

146149
```{code-cell}
147-
samples = ts.samples()
148-
for sample_id, h in zip(samples, ts.haplotypes(samples=samples)):
149-
pop = ts.node(sample_id).population
150-
print(f"Sample {sample_id:<2} ({ts.population(pop).metadata['name']:^5}): {h}")
150+
print("Sample ---> ", " ".join([f"{u:>2}" for p in ts.populations() for u in ts.samples(population=p.id)]))
151+
print("Population |", "".join([f"{p.metadata['name']:^{3*(len(ts.samples(population=p.id)))-1}}|" for p in ts.populations()]))
152+
print("__________ |", "".join(["_" * (3 * len(ts.samples(population=p.id)) - 1) + "|" for p in ts.populations()]))
153+
print(" Position")
154+
for v in ts.variants():
155+
print(f"{int(v.site.position):>10} | ", " ".join(v.states()))
156+
if v.site.id >= 30: # Only show the first 30 sites, for brevity
157+
break
158+
```
159+
160+
It is also possible to grab the [haplotypes](https://tskit.dev/tskit/docs/stable/python-api.html#tskit.TreeSequence.haplotypes)
161+
for specific samples (although this is slightly less efficient)
162+
163+
```{code-cell}
164+
pop_id = 0
165+
samples = ts.samples(population=pop_id)
166+
167+
for sample_id, haplotype in zip(samples, ts.haplotypes(samples=samples)):
168+
h = ".".join(list(haplotype)) # Add a dot between letters, to clarify they are not adjacent
169+
print(f"Sample {sample_id:<2} ({ts.population(pop_id).metadata['name']:^5}): {h}")
151170
```
152171

153-
From the tree sequence it is easy to obtain the
172+
You can easily obtain the
154173
{meth}`TreeSequence.allele_frequency_spectrum` for the entire region (or for
155-
{ref}`windowed regions<sec_tskit_getting_started_compute_statistics_windowing>`)
174+
{ref}`windowed regions<sec_tskit_getting_started_compute_statistics_windowing>`)
175+
directly from the tree sequence (i.e. without needing to reconstruct genotypes)
156176

157177
```{code-cell}
158178
afs = ts.allele_frequency_spectrum()
@@ -163,16 +183,16 @@ plt.show()
163183

164184
Similarly `tskit` allows fast and easy
165185
{ref}`calculation of statistics<sec_tutorial_stats>` along the genome. Here is
166-
a plot of windowed $F_{st}$ between Africans and admixed Americans over this short
186+
a plot of windowed $F_{st}$ between Africans and admixed Americans over this
167187
region of chromosome:
168188

169189
```{code-cell}
170190
# Define the samples between which Fst will be calculated
171191
pop_id = {p.metadata["name"]: p.id for p in ts.populations()}
172192
sample_sets=[ts.samples(pop_id["AFR"]), ts.samples(pop_id["ADMIX"])]
173193
174-
# Do the windowed calculation, using windows of 2 kilobases
175-
windows = list(range(0, int(ts.sequence_length + 1), 2_000))
194+
# Do the windowed calculation, using windows of 10 kilobases
195+
windows = list(range(0, int(ts.sequence_length + 1), 10_000))
176196
F_st = ts.Fst(sample_sets, windows=windows)
177197
178198
# Plot
@@ -185,7 +205,8 @@ plt.show()
185205
Extracting the genetic tree at a specific genomic location is easy using `tskit`, which
186206
also provides methods to {ref}`plot<sec_tskit_viz>` these trees. Here we
187207
grab the tree at position 10kb, and colour the different populations by
188-
different colours, as described in the {ref}`viz tutorial<sec_tskit_viz_styling>`:
208+
grab the tree at position 10kb, and colour the samples according to their population,
209+
as described in the {ref}`viz tutorial<sec_tskit_viz_styling>`:
189210

190211
```{code-cell}
191212
tree = ts.at(10_000)
@@ -214,7 +235,26 @@ tree.draw_svg(
214235
y_axis=True,
215236
y_ticks=range(0, 30_000, 10_000)
216237
)
238+
```
217239

240+
Or we can plot a principal components analysis of the genome, which should reflect
241+
geographical distinctiveness:
242+
243+
```{code-cell}
244+
from matplotlib.patches import Patch
245+
246+
# Run the Principal Components Analysis (PCA)
247+
pca_obj = ts.pca(num_components=2)
248+
249+
# Plot the PCA "factors"
250+
col_list = [colours[pop.metadata["name"]] for pop in ts.populations()]
251+
sample_pop_ids = ts.nodes_population[ts.samples()]
252+
plt.scatter(*pca_obj.factors.T, c=[col_list[p] for p in sample_pop_ids], edgecolors= "black")
253+
plt.xlabel("PCA 1")
254+
plt.ylabel("PCA 2")
255+
plt.legend(handles=[
256+
Patch(color=col_list[pop.id], label=pop.metadata["name"]) for pop in ts.populations()
257+
]);
218258
```
219259

220260
## Population genetic inference
@@ -230,13 +270,12 @@ The genomic region encoded in this tree sequence has been cut down to
230270
span positions 108Mb-110Mb of human chromosome 2, which spans the
231271
[EDAR](https://en.wikipedia.org/wiki/Ectodysplasin_A_receptor) gene.
232272

233-
Note that tree sequence files are usually imported using {func}`load`,
234-
but because this file has been additionally compressed, we load it via
235-
{func}`tszip:tszip.decompress`:
273+
Note that we are using {func}`tszip:tszip.load` to load the file, as this
274+
utility can also read and write compressed tree sequences in `.tsz` format.
236275

237276
```{code-cell}
238277
import tszip
239-
ts = tszip.decompress("data/unified_genealogy_2q_108Mb-110Mb.tsz")
278+
ts = tszip.load("data/unified_genealogy_2q_108Mb-110Mb.tsz")
240279
241280
# The ts encompasses a region on chr 2 with an interesting SNP (rs3827760) in the EDAR gene
242281
edar_gene_bounds = [108_894_471, 108_989_220] # In Mb from the start of chromosome 2
@@ -256,10 +295,11 @@ import pandas as pd
256295
print(ts.num_populations, "populations defined in the tree sequence:")
257296
258297
pop_names_regions = [
259-
[p.metadata.get("name"), p.metadata.get("region")]
298+
[p.metadata.get("name"), p.metadata.get("region"), len(ts.samples(population=p.id))]
260299
for p in ts.populations()
261300
]
262-
display(pd.DataFrame(pop_names_regions, columns=["population name", "region"]))
301+
with pd.option_context('display.max_rows', 100):
302+
display(pd.DataFrame(pop_names_regions, columns=["name", "region", "# genomes"]))
263303
```
264304

265305
You can see that there are multiple African and East asian populations, grouped by
@@ -321,16 +361,100 @@ using tree sequences is simply that they allow these sorts of analysis to
321361
### Topological analysis
322362

323363
As this inferred tree sequence stores (an estimate of) the underlying
324-
genealogy, we can also derive statistics based on genealogical relationships. For
325-
example, this tree sequence also contains a sample genome based on an ancient
326-
genome, a [Denisovan](https://en.wikipedia.org/wiki/Denisovan) individual. We can
327-
look at the closeness of relationship between samples from the different geographical
328-
regions and the Denisovan:
329-
330-
:::{todo}
331-
Show an example of looking at topological relationships between the Denisovan and
332-
various East Asian groups, using the {ref}`sec_counting_topologies` functionality.
333-
:::
364+
genealogy, we can also derive statistics based on genealogical relationships. You
365+
may have noticed that this tree sequence also contains a sample genome based on an ancient
366+
genome, a [Denisovan](https://en.wikipedia.org/wiki/Denisovan) individual. We'll first
367+
simplify the tree sequence to focus on only the Denisovan plus
368+
a common East Asian and a common African population:
369+
370+
```{code-cell}
371+
# Focus on Han, San, and Denisovan
372+
focal = {
373+
"Han": ts.samples(population=6),
374+
"San": ts.samples(population=17),
375+
"Denisovan": ts.samples(population=66),
376+
}
377+
378+
for name, nodes in focal.items(): # Sanity check that we got the right IDs
379+
assert ts.population(ts.node(nodes[0]).population).metadata["name"] == name
380+
381+
# Simplify to just those samples ...
382+
all_focal_samples = [u for samples in focal.values() for u in samples]
383+
simplified_ts = ts.simplify(all_focal_samples, filter_sites=False)
384+
385+
# ... and find the tree around the rs3827760 SNP
386+
focal_site = simplified_ts.site(focal_variant.site.id)
387+
tree = simplified_ts.at(focal_site.position)
388+
```
389+
390+
With this smaller number of samples, we can easily plot the tree
391+
at the "rs3827760" SNP:
392+
393+
```{code-cell}
394+
:"tags": ["hide-input"]
395+
# Make some nice labels, colours, and legend
396+
mutation_labels = {m.id: focal_site.metadata.get("ID") for m in focal_site.mutations}
397+
colours = dict(San="yellow", Han="green", Denisovan="magenta")
398+
styles = [
399+
f".leaf.p{pop.id} > .sym {{fill: {colours[pop.metadata['name']]}; stroke: grey}}"
400+
for pop in simplified_ts.populations()
401+
]
402+
legend = '<rect width="125" height="75" x="100" y="30" fill="transparent" stroke="grey" />'
403+
legend += '<text x="120" y="45" font-weight="bold">Populations</text>'
404+
# Create the legend lines, one for each population. Setting classes that match those
405+
# used for normal nodes means that styled colours are auto automatically picked-up.
406+
legend += "".join([
407+
f'<g transform="translate(105, {60 + 15*p.id})" class="leaf p{p.id}">' # an SVG group
408+
f'<rect width="6" height="6" class="sym" />' # Square symbol
409+
f'<text x="10" y="7">{p.metadata["name"]}' # Label
410+
f'{(" (" + p.metadata["region"].replace("_", " ").title() + ")") if "region" in p.metadata else ""}</text></g>'
411+
for p in simplified_ts.populations()
412+
])
413+
414+
tree.draw_svg(
415+
size=(1000, 400),
416+
style="".join(styles),
417+
node_labels={},
418+
mutation_labels=mutation_labels,
419+
preamble=legend,
420+
title=f"Tree of human chromosome 2 at position {int(focal_variant.site.position)}",
421+
y_axis=True,
422+
y_ticks=range(0, 50_000, 10_000),
423+
)
424+
```
425+
426+
You can see that the pair of magenta Denisovan genomes in this region tend to be
427+
more closely associated with the East Asian genomes. We can assess that by counting
428+
all the 3-tip topologies in the tree that contain one genome from each population:
429+
430+
```{code-cell}
431+
topology_counter = tree.count_topologies()
432+
embedded_topologies = topology_counter[range(simplified_ts.num_populations)]
433+
```
434+
435+
```{code-cell}
436+
:"tags": ["hide-input"]
437+
# All the following code is simply to plot the embedded_topologies nicely
438+
all_trees = list(tskit.all_trees(simplified_ts.num_populations))
439+
last = len(all_trees) - 1
440+
svgs = ""
441+
style = "".join(styles) + ".sample text.lab {baseline-shift: super; font-size: 0.7em;}"
442+
style = style.replace(".leaf.p", ".leaf.n") # Hack to map node IDs to population colours
443+
params = {
444+
"size": (160, 150),
445+
"node_labels": {pop.id: pop.metadata["name"] for pop in simplified_ts.populations()}
446+
}
447+
for i, t in enumerate(all_trees):
448+
rank = t.rank()
449+
count = embedded_topologies[rank]
450+
params["title"] = f"{count} trees"
451+
if i != last:
452+
svgs += t.draw_svg(root_svg_attributes={'x': (last - i) * 150}, **params)
453+
else:
454+
# Plot the last svg and stack the previous ones to the right
455+
display(t.draw_svg(preamble=svgs, canvas_size=(1000, 150), style=style, **params))
456+
```
457+
334458

335459
See {ref}`sec_counting_topologies` for an introduction to topological methods in
336460
`tskit`.

requirements-CI.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
git+https://github.com/tskit-dev/tsconvert@4ed3c0f9bbbb79c7c394b352c58f5386cfef2b90
22
demes==0.2.3
3-
demesdraw==0.4.0
4-
jupyter-book==1.0.2
3+
demesdraw==0.4.1
4+
jupyter-book==1.0.3
55
jupyter-cache==0.6.1
6-
msprime==1.3.2
6+
msprime==1.4.0
77
networkx==3.3
88
numpy==2.2.6
99
pandas==2.3.3
@@ -12,6 +12,6 @@ scikit-allel==1.3.13
1212
stdpopsim==0.3.0
1313
tqdm==4.66.3
1414
tskit==1.0.0
15-
tskit_arg_visualizer==0.1.1
16-
tszip==0.2.4
15+
tskit_arg_visualizer==0.1.2
16+
tszip==0.3
1717
jsonschema==4.18.6 # Pinned due to 4.19 "AttributeError module jsonschema has no attribute _validators"

0 commit comments

Comments
 (0)