Skip to content

Commit

Permalink
progress on test for #35
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPannetier committed Jul 3, 2024
1 parent e340dd7 commit 89eaf9d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
12 changes: 9 additions & 3 deletions Individual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ void Individual::inherit(Species* pSpecies, const Individual* mother) {
set<unsigned int> recomPositions; //not used here cos haploid but need it for inherit function, not ideal
int startingChromosome = 0;

const auto& mumTraits = getTraitTypes();
const auto& motherTraits = getTraitTypes();

for (auto const& trait : mumTraits)
for (auto const& trait : motherTraits)
{
const auto motherTrait = mother->getTrait(trait);
auto newTrait = motherTrait->clone(); // shallow copy, pointer to species trait initialised and empty sequence
Expand Down Expand Up @@ -1591,11 +1591,17 @@ Cell* Individual::getCurrCell() const {
return pCurrCell;
}


void Individual::setInitAngle(const float angle) {
auto pCRW = dynamic_cast<crwData*>(pTrfrData.get());
pCRW->prevdrn = angle;
}

// Force mutations to trigger for all traits
void Individual::triggerMutations() {
for (auto const& [trType, indTrait] : spTraitTable) {
indTrait->mutate();
}
}

#endif // RSDEBUG

1 change: 1 addition & 0 deletions Individual.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class Individual {
void insertIndDispTrait(TraitType trType, DispersalTrait tr) {
spTraitTable.insert(make_pair(trType, make_unique<DispersalTrait>(tr)));
};
void triggerMutations();
#endif

private:
Expand Down
7 changes: 6 additions & 1 deletion SpeciesTrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ SpeciesTrait::SpeciesTrait(
switch (paramType)
{
case MIN: case MAX: case MEAN:
if (!isValidTraitVal(paramVal))
if (
(trType == NEUTRAL || trType == GENETIC_LOAD || trType == GENETIC_LOAD1 ||
trType == GENETIC_LOAD2 || trType == GENETIC_LOAD3 || trType == GENETIC_LOAD4 || trType == GENETIC_LOAD5)
&& !isValidTraitVal(paramVal)
// dispersal traits are cumulative so no value is invalid
)
throw logic_error("Invalid parameter value: mutation parameter " + to_string(paramType) + " must have a valid value for trait" + to_string(traitType) + ".");
break;
case SD: case SHAPE: case SCALE:
Expand Down
83 changes: 83 additions & 0 deletions unit_tests/testIndividual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,89 @@ void testIndividual() {
assert(countRecombineTogetherAB > countRecombineTogetherAC);
assert(35 < countRecombineTogetherCD && countRecombineTogetherCD < 65);
}

// Traits can be sex-specific
{
Patch* pPatch = new Patch(0, 0);
Cell* pCell = new Cell(0, 0, (intptr)pPatch, 0);

const int genomeSz = 6;
Species* pSpecies = new Species();
pSpecies->setGeneticParameters(
set<int>{genomeSz - 1}, // one chromosome
genomeSz,
0.0, // no recombination
set<int>{}, "none", set<int>{}, 0 // no output so no sampling
);
emigRules emig;
emig.indVar = true;
emig.sexDep = true;
emig.densDep = false;
pSpecies->setEmigRules(emig);

// Create species trait
//// Shared params
const map<GenParamType, float> initParams{
// all values are 1
pair<GenParamType, float>{GenParamType::MIN, 1.0},
pair<GenParamType, float>{GenParamType::MAX, 1.0}
};
const map<GenParamType, float> mutationParams{
// reduction of emigration probability
pair<GenParamType, float>{GenParamType::MIN, -0.5},
pair<GenParamType, float>{GenParamType::MAX, -0.25}
};
const bool isDiploid{ true };

//// Sex-specific params
const set<int> maleGenePositions = { 0, 2, 4 };
const set<int> femaleGenePositions = { 1, 3, 5 };
const float maleMutationRate = 0.0;
const float femaleMutationRate = 1.0;

SpeciesTrait* spTrM = new SpeciesTrait(
TraitType::E_D0_M,
sex_t::MAL,
maleGenePositions,
ExpressionType::AVERAGE,
// Set all initial alleles values to 1
DistributionType::UNIFORM, initParams,
DistributionType::NONE, initParams, // no dominance, params are ignored
true, // isInherited
maleMutationRate, // does not mutate
DistributionType::UNIFORM, mutationParams, // not used
isDiploid ? 2 : 1
);

pSpecies->addTrait(TraitType::E_D0_M, *spTrM);

SpeciesTrait* spTrF = new SpeciesTrait(
TraitType::E_D0_F,
sex_t::FEM,
femaleGenePositions,
ExpressionType::AVERAGE,
// Set all initial alleles values to 1
DistributionType::UNIFORM, initParams,
DistributionType::NONE, initParams, // no dominance, params are ignored
true, // isInherited
femaleMutationRate, // does not mutate
DistributionType::UNIFORM, mutationParams, // not used
isDiploid ? 2 : 1
);
pSpecies->addTrait(TraitType::E_D0_F, *spTrF);

Individual indFemale = Individual(pCell, pPatch, 0, 0, 0, 0.0, false, 0);
Individual indMale = Individual(pCell, pPatch, 0, 0, 0, 1.0, false, 0);
indFemale.setUpGenes(pSpecies, 1.0);
indMale.setUpGenes(pSpecies, 1.0);
indFemale.triggerMutations();
indMale.triggerMutations();

emigTraits femaleEmig = indFemale.getIndEmigTraits();
emigTraits maleEmig = indMale.getIndEmigTraits();
assert(femaleEmig.d0 != 1.0);
assert(maleEmig.d0 == 1.0);
}
}

#endif //RSDEBUG

0 comments on commit 89eaf9d

Please sign in to comment.