// Defining a function calcDxy and testing its relation with calcFST and calcPi
initialize() {
initializeMutationRate(1e-4);
initializeMutationType("m1", 0.5, "f", 0.0); //neutral
initializeGenomicElementType("g1", m1, 1.0);
m1.mutationStackPolicy = 'l'; // this is important for Dxy to be relevant
defineGlobal("L", 100); // genome size
initializeGenomicElement(g1, 0, L-1);
initializeRecombinationRate(1e-8);
}
1 late() { // create two isolated populations of 500 individuals
sim.addSubpop("p1", 500); sim.addSubpop("p2", 500);
}
2: late() {// outputting calculating at every generation
catn("Gen. "+sim.cycle+": -------");
// comparing FST with Dxy
fst = calcFST(p1.haplosomes, p2.haplosomes);
dxy = calcDxy(p1.haplosomes, p2.haplosomes);
catn("(1,2):"+fst+"|"+dxy);
// comparing calcPi with calcDxy for each subpopulation
pi = calcPi(p1.haplosomes);
dxy = calcDxy(p1.haplosomes, p1.haplosomes);
catn("( 1 ):"+pi+"|"+2*dxy/L); // dxy divided by chr size
pi = calcPi(p2.haplosomes);
dxy = calcDxy(p2.haplosomes, p2.haplosomes);
catn("( 2 ):"+pi+"|"+2*dxy/L); // dxy divided by chr size
}
2000000 late() { sim.simulationFinished(); }
// Function calcDxy
function (float$)calcDxy(object<Haplosome> haplosomes1, object<Haplosome> haplosomes2, [No<Mutation> muts = NULL], [Ni$ start = NULL], [Ni$ end = NULL]){
n1 = haplosomes1.length();
n2 = haplosomes2.length();
if (n1 == 0 | n2 == 0)
stop("ERROR (calcDxy): haplosomes1 and haplosomes2 must both be non-empty.");
species = haplosomes1[0].individual.subpopulation.species;
if (community.allSpecies.length() > 1)
{
if (any(c(haplosomes1, haplosomes2).individual.subpopulation.species != species))
stop("ERROR (calcDxy): all haplosomes must belong to the same species.");
if (!isNULL(muts))
if (any(muts.mutationType.species != species))
stop("ERROR (calcDxy): all mutations must belong to the same species as the haplosomes.");
}
chromosome = haplosomes1[0].chromosome;
if (species.chromosomes.length() > 1)
{
if (any(c(haplosomes1, haplosomes2).chromosome != chromosome))
stop("ERROR (calcDxy): all haplosomes must be associated with the same chromosome.");
if (!isNULL(muts))
if (any(muts.chromosome != chromosome))
stop("ERROR (calcDxy): all mutations must be associated with the same chromosome as the haplosomes.");
}
length = chromosome.length;
if (isNULL(muts))
muts = species.subsetMutations(chromosome=chromosome);
// handle windowing
if (!isNULL(start) & !isNULL(end))
{
if (start > end)
stop("ERROR (calcDxy): start must be less than or equal to end.");
if ((start < 0) | (end >= length))
stop("ERROR (calcDxy): start and end must be within the bounds of the focal chromosome");
length = end - start + 1;
mpos = muts.position;
muts = muts[(mpos >= start) & (mpos <= end)];
}
else if (isNULL(start) & isNULL(end))
{
start = 0;
end = chromosome.length - 1;
}
else if (!isNULL(start) | !isNULL(end))
{
stop("ERROR (calcDxy): start and end must both be NULL or both be non-NULL.");
}
// narrow down to the mutations that are actually present in the haplosomes and aren't fixed
p = c(haplosomes1, haplosomes2).mutationFrequenciesInHaplosomes(muts);
muts = muts[(p != 0.0) & (p != 1.0)];
if (size(muts) == 0) // if there are no mutations segregating in this genomic window, Dxy vanishes
return 0.0;
// check if two mutations cannot be in the same position of the same haplotype
stackGroup = muts[0].mutationType.mutationStackGroup;
if(any(muts.mutationType.mutationStackGroup != stackGroup) | muts[0].mutationType.mutationStackPolicy=='s')
stop("ERROR (calcDxy): all mutations must be from the same stack group with either last or first stackPolicy. If mutations can stack, one should probably use calcFST()");
// do the calculation for each mutation
dos1 = haplosomes1.mutationCountsInHaplosomes(muts);
dos2 = haplosomes2.mutationCountsInHaplosomes(muts);
// number of differences is: 1x0 + 0x1
diffs_per_site = dos1 * (n2 - dos2) + (n1 - dos1) * dos2;
diff = sum(diffs_per_site);
return diff / 2.0 / n1 / n2; //it may or may not be divided by "length"... original definition by Nei does not divide, to be consistent with other functions in SLiM it could be normalized
}
****--
SLiM forward genetic simulation: http://messerlab.org/slim/
---
You received this message because you are subscribed to the Google Groups "slim-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slim-discuss...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/slim-discuss/00f0b638-cdf9-4b23-ad1c-1b028101c33en%40googlegroups.com.