Skip to content

Commit 7b3d70f

Browse files
committed
fix #560, add new recipe 14.16 to explore linkage disequilibrium
1 parent 43d5e27 commit 7b3d70f

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

QtSLiM/recipes.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<file>recipes/Recipe 14.13 - Modeling biallelic loci with a mutation() callback II.txt</file>
119119
<file>recipes/Recipe 14.14 - Modeling biallelic loci in script.txt</file>
120120
<file>recipes/Recipe 14.15 - Using runs of homozygosity (ROH) to track inbreeding.txt</file>
121+
<file>recipes/Recipe 14.16 - Visualizing linkage disequilibrium.txt</file>
121122
<file>recipes/Recipe 15.1 - A minimal nonWF model.txt</file>
122123
<file>recipes/Recipe 15.2 - Age structure (a life table model).txt</file>
123124
<file>recipes/Recipe 15.4 - Monogamous mating and variation in litter size.txt</file>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Keywords: linkage disequilibrium, LD, custom plotting, recombination, calcLD
2+
3+
initialize()
4+
{
5+
setSeed(2180149919968428688);
6+
defineConstant("L", 1e7);
7+
initializeMutationRate(1e-7);
8+
initializeMutationType("m1", 0.5, "f", 0.0);
9+
initializeMutationType("m2", 1.0, "f", 0.1).color="red"; // used for MUT1
10+
initializeGenomicElementType("g1", m1, 1.0);
11+
initializeGenomicElement(g1, 0, L-1);
12+
initializeRecombinationRate(1e-8);
13+
}
14+
1 early()
15+
{
16+
sim.addSubpop("p1", 1000);
17+
}
18+
5000 late()
19+
{
20+
// create the MUT1 mutation that we will track over time
21+
mut1 = sample(p1.haplosomes, 1).addNewDrawnMutation(m2, asInteger(L/2));
22+
defineGlobal("MUT1", mut1);
23+
}
24+
25+
5000:10000 late()
26+
{
27+
allMutsMAF = sim.mutations[sim.mutationFrequencies(p1) >= 0.10];
28+
muts = sortBy(allMutsMAF, "position");
29+
30+
if (size(muts) == 0)
31+
return;
32+
if (!MUT1.isSegregating)
33+
{
34+
catn("The focal mutation fixed or was lost.");
35+
sim.simulationFinished();
36+
return;
37+
}
38+
39+
ld = calcLD_D(MUT1, muts) * 10; // scale up to make it more visible
40+
r = calcLD_Rsquared(MUT1, muts, squared=F);
41+
r2 = calcLD_Rsquared(MUT1, muts);
42+
p = muts.position;
43+
44+
plot = slimgui.createPlot("LD versus R2", xrange=c(0,L-1), yrange=c(-1,1),
45+
xlab="tick", ylab="metric", width=800, height=300);
46+
plot.abline(v=MUT1.position, color="red", lwd=2);
47+
plot.points(p, ld, symbol=16, color="cornflowerblue", size=0.5, alpha=0.1);
48+
plot.points(p, r, symbol=16, color="chartreuse3", size=0.5, alpha=0.1);
49+
plot.points(p, r2, symbol=16, color="black", size=0.5, alpha=0.1);
50+
51+
f = rep(1/201, 201); // running average filter, 201 mutations wide
52+
plot.lines(p, filter(ld, f, outside=T), color="cornflowerblue", lwd=2);
53+
plot.lines(p, filter(r, f, outside=T), color="chartreuse3", lwd=2);
54+
plot.lines(p, filter(r2, f, outside=T), color="black", lwd=2);
55+
56+
plot.addLegend("topRight");
57+
plot.legendPointEntry("R2", symbol=16, color="black", size=0.5);
58+
plot.legendPointEntry("R", symbol=16, color="chartreuse3", size=0.5);
59+
plot.legendPointEntry("LD*10", symbol=16, color="cornflowerblue", size=0.5);
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Keywords: linkage disequilibrium, LD, custom plotting, recombination, calcLD
2+
3+
initialize()
4+
{
5+
setSeed(2180149919968428688);
6+
defineConstant("L", 1e7);
7+
initializeMutationRate(1e-7);
8+
initializeMutationType("m1", 0.5, "f", 0.0);
9+
initializeMutationType("m2", 1.0, "f", 0.1).color="red"; // used for MUT1
10+
initializeGenomicElementType("g1", m1, 1.0);
11+
initializeGenomicElement(g1, 0, L-1);
12+
initializeRecombinationRate(1e-8);
13+
}
14+
1 early()
15+
{
16+
sim.addSubpop("p1", 1000);
17+
}
18+
5000 late()
19+
{
20+
// create the MUT1 mutation that we will track over time
21+
mut1 = sample(p1.haplosomes, 1).addNewDrawnMutation(m2, asInteger(L/2));
22+
defineGlobal("MUT1", mut1);
23+
}
24+
25+
5000:10000 late()
26+
{
27+
allMutsMAF = sim.mutations[sim.mutationFrequencies(p1) >= 0.10];
28+
muts = sortBy(allMutsMAF, "position");
29+
30+
if (size(muts) == 0)
31+
return;
32+
if (!MUT1.isSegregating)
33+
{
34+
catn("The focal mutation fixed or was lost.");
35+
sim.simulationFinished();
36+
return;
37+
}
38+
39+
ld = calcLD_D(MUT1, muts) * 10; // scale up to make it more visible
40+
r = calcLD_Rsquared(MUT1, muts, squared=F);
41+
r2 = calcLD_Rsquared(MUT1, muts);
42+
p = muts.position;
43+
44+
plot = slimgui.createPlot("LD versus R2", xrange=c(0,L-1), yrange=c(-1,1),
45+
xlab="tick", ylab="metric", width=800, height=300);
46+
plot.abline(v=MUT1.position, color="red", lwd=2);
47+
plot.points(p, ld, symbol=16, color="cornflowerblue", size=0.5, alpha=0.1);
48+
plot.points(p, r, symbol=16, color="chartreuse3", size=0.5, alpha=0.1);
49+
plot.points(p, r2, symbol=16, color="black", size=0.5, alpha=0.1);
50+
51+
f = rep(1/201, 201); // running average filter, 201 mutations wide
52+
plot.lines(p, filter(ld, f, outside=T), color="cornflowerblue", lwd=2);
53+
plot.lines(p, filter(r, f, outside=T), color="chartreuse3", lwd=2);
54+
plot.lines(p, filter(r2, f, outside=T), color="black", lwd=2);
55+
56+
plot.addLegend("topRight");
57+
plot.legendPointEntry("R2", symbol=16, color="black", size=0.5);
58+
plot.legendPointEntry("R", symbol=16, color="chartreuse3", size=0.5);
59+
plot.legendPointEntry("LD*10", symbol=16, color="cornflowerblue", size=0.5);
60+
}

VERSIONS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ development head (in the master branch):
4949
fix #526, add a calcDxy() popgen utility function to SLiM that calculates Nei's Dxy genetic divergence statistic, contributed by Vitor Sudbrack
5050
fix #529, extend calcFST() to support multiple chromosomes
5151
extend subsetMutations() to support subsetting mutations belonging to more than one chromosome
52+
fix #560, add recipe 14.16 "Visualizing linkage disequilibrium" to demonstrate the new calcLD_D() and calcLD_Rsquared() functions
5253

5354

5455
version 5.0 (Eidos version 4.0):

0 commit comments

Comments
 (0)