Skip to content

Commit

Permalink
set up two patches landscape, progress on #44
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPannetier committed Jun 21, 2024
1 parent 7ea78c0 commit 060d1f5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Community.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ void Community::writePairwiseFSTFile(Species* pSpecies, const int yr, const int

void Community::outNeutralGenetics(Species* pSpecies, int rep, int yr, int gen, bool fstat, bool perLocus, bool pairwise) {

const int nAlleles = (int)(pSpecies->getSpTrait(NEUTRAL)->getMutationParameters().find(MAX)->second);
const int maxNbNeutralAlleles = (int)(pSpecies->getSpTrait(NEUTRAL)->getMutationParameters().find(MAX)->second);
const int nLoci = (int)pSpecies->getNPositionsForTrait(NEUTRAL);
const set<int> patchList = pSpecies->getSamplePatches();
int nInds = 0;
Expand All @@ -1869,19 +1869,19 @@ void Community::outNeutralGenetics(Species* pSpecies, int rep, int yr, int gen,
if (fstat) {
pNeutralStatistics->calculateHo(patchList, nInds, nLoci, pSpecies, pLandscape);
pNeutralStatistics->calcAllelicDiversityMetrics(patchList, nInds, pSpecies, pLandscape);
pNeutralStatistics->calculateFstatWC(patchList, nInds, nLoci, nAlleles, pSpecies, pLandscape);
pNeutralStatistics->calculateFstatWC(patchList, nInds, nLoci, maxNbNeutralAlleles, pSpecies, pLandscape);
writeWCFstatFile(rep, yr, gen);
}

if (perLocus) {
pNeutralStatistics->calcPerLocusMeanSquaresFst(patchList, nInds, nLoci, nAlleles, pSpecies, pLandscape);
pNeutralStatistics->calcPerLocusMeanSquaresFst(patchList, nInds, nLoci, maxNbNeutralAlleles, pSpecies, pLandscape);
pNeutralStatistics->calculatePerLocusHo(patchList, nInds, nLoci, pSpecies, pLandscape);
writeWCPerLocusFstatFile(pSpecies, yr, gen, nAlleles, nLoci, patchList);
writeWCPerLocusFstatFile(pSpecies, yr, gen, maxNbNeutralAlleles, nLoci, patchList);
}

if (pairwise) {
pNeutralStatistics->calcPairwiseWeightedFst(patchList, nInds, nLoci, pSpecies, pLandscape);
writePairwiseFSTFile(pSpecies, yr, gen, nAlleles, nLoci, patchList);
writePairwiseFSTFile(pSpecies, yr, gen, maxNbNeutralAlleles, nLoci, patchList);
}
}

Expand Down
6 changes: 3 additions & 3 deletions NeutralStatsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ NeutralStatsManager::NeutralStatsManager(const int& nbSampledPatches, const int
void NeutralStatsManager::updateAllNeutralTables(Species* pSpecies, Landscape* pLandscape, set<int> const& patchList) {

const int nLoci = pSpecies->getNPositionsForTrait(NEUTRAL);
const int nAlleles = (int)pSpecies->getSpTrait(NEUTRAL)->getMutationParameters().find(MAX)->second;
const int maxNbNeutralAlleles = (int)pSpecies->getSpTrait(NEUTRAL)->getMutationParameters().find(MAX)->second;
const int ploidy = pSpecies->isDiploid() ? 2 : 1;

// Create / Update community-level NEUTRAL counts table
Expand All @@ -48,7 +48,7 @@ void NeutralStatsManager::updateAllNeutralTables(Species* pSpecies, Landscape* p
}
else { // populate the tables with default values
for (int thisLocus = 0; thisLocus < nLoci; thisLocus++) {
NeutralCountsTable newNeutralTbl = NeutralCountsTable(nAlleles);
NeutralCountsTable newNeutralTbl = NeutralCountsTable(maxNbNeutralAlleles);
commNeutralCountTables.push_back(newNeutralTbl);
}
}
Expand All @@ -67,7 +67,7 @@ void NeutralStatsManager::updateAllNeutralTables(Species* pSpecies, Landscape* p
}
// Add population-level counts to community-level counts
for (int thisLocus = 0; thisLocus < nLoci; thisLocus++) {
for (int allele = 0; allele < nAlleles; allele++) {
for (int allele = 0; allele < maxNbNeutralAlleles; allele++) {

if (pPop != 0) {
patchAlleleCount = pPop->getAlleleTally(thisLocus, allele);
Expand Down
2 changes: 1 addition & 1 deletion NeutralStatsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct PatchMatrix
struct NeutralCountsTable {

public:
NeutralCountsTable(int nAllele) : alleleTallies(nAllele), alleleFrequencies(nAllele), alleleHeterozygoteTallies(nAllele) {};
NeutralCountsTable(int maxNbNeutralAlleles) : alleleTallies(maxNbNeutralAlleles), alleleFrequencies(maxNbNeutralAlleles), alleleHeterozygoteTallies(maxNbNeutralAlleles) {};

void reset() {
fill(alleleTallies.begin(), alleleTallies.end(), 0); fill(alleleFrequencies.begin(), alleleFrequencies.end(), 0);
Expand Down
57 changes: 57 additions & 0 deletions unit_tests/testNeutralStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,64 @@ void testNeutralStats() {
// - per-locus Fst
// - pairwise per-patch Fst
{
// Create two-patches landscape
Landscape* pLandscape = new Landscape;
const int nbPatches = 2;
vector<Patch*> patches(nbPatches);
set<int> patchList;
for (int i = 0; i < nbPatches; i++) {
patches[i] = pLandscape->newPatch(i);
Cell* pCell = new Cell(0, 0, (intptr)patches[i], 0);
patches[i]->addCell(pCell, 0, 0);
patchList.insert(patches[i]->getPatchNum());
}
const string indSampling = "all";
const set<int> stgToSample = { 1 };

// Create genetic structure
const int genomeSz = 4;
Species* pSpecies = new Species();
pSpecies->setDemogr(createDefltDiploidDemogrParams());
pSpecies->setGeneticParameters(
set<int>{genomeSz - 1}, // single chromosome
genomeSz,
0.0, // no recombination
patchList,
indSampling,
stgToSample,
1
);
const set<int> genePositions = { 0, 1, 3 }; // arbitrary
const int nbLoci = genePositions.size();
const bool isDiploid{ true };
const float maxAlleleVal = 255; // highly unlikely that same value sampled twice
SpeciesTrait* spTr = createTestNeutralSpTrait(maxAlleleVal, genePositions, isDiploid);
pSpecies->addTrait(TraitType::NEUTRAL, *spTr);

// Initialise populations
const int nbIndsPerPop = 2;
for (auto p : patches) {
Population* pPop = new Population(pSpecies, p, nbIndsPerPop, 1);
pPop->sampleIndsWithoutReplacement(indSampling, stgToSample);
}

// Calculate heterozygosity
auto pNeutralStatistics = make_unique<NeutralStatsManager>(nbPatches, nbLoci);
pNeutralStatistics->updateAllNeutralTables(
pSpecies,
pLandscape,
patchList
);
const int maxNbNeutralAlleles = static_cast<int>(maxAlleleVal);
pNeutralStatistics->calculateFstatWC(
patchList,
nbIndsPerPop,
nbLoci,
maxNbNeutralAlleles,
pSpecies,
pLandscape
);
assert(pNeutralStatistics->getFstWC() == 1.0);
}
}

Expand Down

0 comments on commit 060d1f5

Please sign in to comment.