Skip to content

Commit

Permalink
weir and cockerham, weir & hill converge, fix #73
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPannetier committed Sep 2, 2024
1 parent 3409068 commit 43da58f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 2 additions & 3 deletions NeutralStatsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ void NeutralStatsManager::calcPerLocusMeanSquaresFst(set<int> const& patchList,
void NeutralStatsManager::calcPairwiseWeightedFst(set<int> const& patchList, const int nInds, const int nLoci, Species* pSpecies, Landscape* pLandscape) {

const int nAlleles = (int)pSpecies->getSpTrait(NEUTRAL)->getNbNeutralAlleles();
const int ploidy = pSpecies->isDiploid() ? 2 : 1;

// Needs to be in vector to iterate over, copy preserves order
vector<int> patchVect;
Expand All @@ -559,14 +558,14 @@ void NeutralStatsManager::calcPairwiseWeightedFst(set<int> const& patchList, con
double denominator = 0;
double sumWeights = 0;

totSize = nInds * ploidy;
totSize = nInds;

// Calculate weight (n_ic) terms
for (int i = 0; i < nPatches; ++i) {
const auto patch = pLandscape->findPatch(patchVect[i]);
const auto pPop = (Population*)patch->getPopn((intptr)pSpecies);
if (pPop != 0) {
popSizes[i] = pPop->sampleSize() * ploidy;
popSizes[i] = pPop->sampleSize();
} // else popSizes[i] remain default init value 0, safe
popWeights[i] = popSizes[i] - (popSizes[i] * popSizes[i] / totSize); // n_ic in Weir & Hill 2002
sumWeights += popWeights[i];
Expand Down
17 changes: 14 additions & 3 deletions unit_tests/testNeutralStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,12 @@ void testNeutralStats() {
assert(pNeutralStatistics->getPairwiseFst(0, 1) == 0.0);
}

double refDiploidFst; // for use in haploid test below
double refDiploidFst; // for use in further tests below

// In strictly homozygote samples:
// 1. Fis = 1 (maximum inbreeding)
// 2. The sign of the Fst depends on the ratio of variation within vs between populations
// 3. (if sample sizes are equal) The Weir&Cockerham 1984, Weir&Hill 2002 estimators yield the same values
{
// Case 1/2: variation within > between
// Within: frequencies of allele A and B are quite different
Expand Down Expand Up @@ -572,7 +573,17 @@ void testNeutralStats() {
assert(pNeutralStatistics->getFstWC() < 0.0);
assert(pNeutralStatistics->getFisWC() == 1.0);

refDiploidFst = pNeutralStatistics->getFstWC(); // for use in haploid test below
pNeutralStatistics->calcPairwiseWeightedFst(
patchList,
nbIndsPerPop * patchList.size(),
nbLoci,
pSpecies,
pLandscape
);
const double tol = 0.000001;
assert(abs(pNeutralStatistics->getWeightedFst() - pNeutralStatistics->getFstWC()) < tol);

refDiploidFst = pNeutralStatistics->getFstWC(); // for use in further tests below
}

// Case 2/2: variation within < between
Expand Down Expand Up @@ -847,7 +858,7 @@ void testNeutralStats() {
assert(calcError < 0.0001);
}

// THe Fst is a haploid sample equals that of a diploid sample if:
// The Fst is a haploid sample equals that of a diploid sample if:
// 1 - allelic frequencies are equal
// 2 - the diploid sample has no heterozygotes
// (reference diploid Fst has already been calculated above)
Expand Down

0 comments on commit 43da58f

Please sign in to comment.