Skip to content

Commit

Permalink
architectural shift for WF
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaller committed Oct 16, 2024
1 parent 361e2db commit 724bfdc
Show file tree
Hide file tree
Showing 18 changed files with 3,181 additions and 1,566 deletions.
36 changes: 21 additions & 15 deletions QtSLiM/QtSLiMGraphView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,33 +2434,39 @@ size_t QtSLiMGraphView::tallyGUIMutationReferences(slim_objectid_t subpop_id, in

if (subpop) // tally only within our chosen subpop
{
int haplosome_count_per_individual = subpop->HaplosomeCountPerIndividual();

for (Individual *ind : subpop->parent_individuals_)
{
for (Haplosome *haplosome : ind->haplosomes_)
{
if (!haplosome->IsNull())
Haplosome **haplosomes = ind->haplosomes_;
for (int haplosome_index = 0; haplosome_index < haplosome_count_per_individual; haplosome_index++)
{
int mutrun_count = haplosome->mutrun_count_;
Haplosome *haplosome = haplosomes[haplosome_index];

for (int run_index = 0; run_index < mutrun_count; ++run_index)
if (!haplosome->IsNull())
{
const MutationRun *mutrun = haplosome->mutruns_[run_index];
const MutationIndex *haplosome_iter = mutrun->begin_pointer_const();
const MutationIndex *haplosome_end_iter = mutrun->end_pointer_const();
int mutrun_count = haplosome->mutrun_count_;

for (; haplosome_iter != haplosome_end_iter; ++haplosome_iter)
for (int run_index = 0; run_index < mutrun_count; ++run_index)
{
const Mutation *mutation = mut_block_ptr + *haplosome_iter;
const MutationRun *mutrun = haplosome->mutruns_[run_index];
const MutationIndex *haplosome_iter = mutrun->begin_pointer_const();
const MutationIndex *haplosome_end_iter = mutrun->end_pointer_const();

if (mutation->mutation_type_ptr_->mutation_type_index_ == muttype_index)
(mutation->gui_scratch_reference_count_)++;
for (; haplosome_iter != haplosome_end_iter; ++haplosome_iter)
{
const Mutation *mutation = mut_block_ptr + *haplosome_iter;

if (mutation->mutation_type_ptr_->mutation_type_index_ == muttype_index)
(mutation->gui_scratch_reference_count_)++;
}
}

subpop_total_haplosome_count++;
}

subpop_total_haplosome_count++;
}
}
}
}

return subpop_total_haplosome_count;
Expand Down
14 changes: 13 additions & 1 deletion QtSLiM/QtSLiMHaplotypeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,23 @@ QtSLiMHaplotypeManager::QtSLiMHaplotypeManager(QObject *p_parent, ClusteringMeth
subpopCount = static_cast<int>(selected_subpops.size());

// Fetch haplosomes and figure out what we're going to plot; note that we plot only non-null haplosomes
int haplosome_count_per_individual = graphSpecies->HaplosomeCountPerIndividual();

for (Subpopulation *subpop : selected_subpops)
{
for (Individual *ind : subpop->parent_individuals_)
for (Haplosome *haplosome : ind->haplosomes_)
{
Haplosome **ind_haplosomes = ind->haplosomes_;

for (int haplosome_index = 0; haplosome_index < haplosome_count_per_individual; haplosome_index++)
{
Haplosome *haplosome = ind_haplosomes[haplosome_index];

if (!haplosome->IsNull())
haplosomes.emplace_back(haplosome);
}
}
}

// If a sample is requested, select that now; sampleSize <= 0 means no sampling
if ((sampleSize > 0) && (haplosomes.size() > sampleSize))
Expand Down
35 changes: 20 additions & 15 deletions SLiMgui/GraphView_MutationFrequencyTrajectory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ - (void)fetchDataForFinishedTick
// subpopulation that is being displayed in this graph, and tally into gui_scratch_reference_count only
// BCH 4/21/2023: This could use mutrun use counts to run faster...
//
int haplosome_count_per_individual = displaySpecies->HaplosomeCountPerIndividual();
int subpop_total_haplosome_count = 0;

Mutation *mut_block_ptr = gSLiM_Mutation_Block;
Expand All @@ -318,31 +319,35 @@ - (void)fetchDataForFinishedTick

for (Individual *ind : subpop->parent_individuals_)
{
for (Haplosome *haplosome : ind->haplosomes_)
{
if (!haplosome->IsNull())
Haplosome **haplosomes = ind->haplosomes_;
for (int haplosome_index = 0; haplosome_index < haplosome_count_per_individual; haplosome_index++)
{
int mutrun_count = haplosome->mutrun_count_;
Haplosome *haplosome = haplosomes[haplosome_index];

for (int run_index = 0; run_index < mutrun_count; ++run_index)
if (!haplosome->IsNull())
{
const MutationRun *mutrun = haplosome->mutruns_[run_index];
const MutationIndex *haplosome_iter = mutrun->begin_pointer_const();
const MutationIndex *haplosome_end_iter = mutrun->end_pointer_const();
int mutrun_count = haplosome->mutrun_count_;

for (; haplosome_iter != haplosome_end_iter; ++haplosome_iter)
for (int run_index = 0; run_index < mutrun_count; ++run_index)
{
const Mutation *mutation = mut_block_ptr + *haplosome_iter;
const MutationRun *mutrun = haplosome->mutruns_[run_index];
const MutationIndex *haplosome_iter = mutrun->begin_pointer_const();
const MutationIndex *haplosome_end_iter = mutrun->end_pointer_const();

if (mutation->mutation_type_ptr_->mutation_type_index_ == _selectedMutationTypeIndex)
(mutation->gui_scratch_reference_count_)++;
for (; haplosome_iter != haplosome_end_iter; ++haplosome_iter)
{
const Mutation *mutation = mut_block_ptr + *haplosome_iter;

if (mutation->mutation_type_ptr_->mutation_type_index_ == _selectedMutationTypeIndex)
(mutation->gui_scratch_reference_count_)++;
}
}

subpop_total_haplosome_count++;
}

subpop_total_haplosome_count++;
}
}
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions SLiMgui/SLiMHaplotypeManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,23 @@ - (instancetype)initWithClusteringMethod:(SLiMHaplotypeClusteringMethod)clusteri
[self setSubpopCount:(int)selected_subpops.size()];

// Fetch haplosomes and figure out what we're going to plot; note that we plot only non-null haplosomes
int haplosome_count_per_individual = displaySpecies->HaplosomeCountPerIndividual();

#warning we need a focal chromosome here; we can't align non-homologous haplosomes
for (Subpopulation *subpop : selected_subpops)
{
for (Individual *ind : subpop->parent_individuals_)
for (Haplosome *haplosome : ind->haplosomes_)
if (!haplosome->IsNull())
haplosomes.emplace_back(haplosome);
{
Haplosome **ind_haplosomes = ind->haplosomes_;

for (int haplosome_index = 0; haplosome_index < haplosome_count_per_individual; haplosome_index++)
{
Haplosome *haplosome = ind_haplosomes[haplosome_index];

if (!haplosome->IsNull())
haplosomes.emplace_back(haplosome);
}
}
}

// If a sample is requested, select that now; sampleSize <= 0 means no sampling
Expand Down
4 changes: 3 additions & 1 deletion core/community.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2440,8 +2440,8 @@ void Community::AllSpecies_PurgeRemovedObjects(void)
// with PurgeRemovedSubpopulations() only in Population::SwapGenerations().
for (Species *species : all_species_)
{
species->EmptyGraveyard(); // needs to be done first; uses subpopulation references
species->population_.PurgeRemovedSubpopulations();
species->EmptyGraveyard();
}
}

Expand Down Expand Up @@ -2695,6 +2695,7 @@ bool Community::_RunOneTickWF(void)
DeregisterScheduledScriptBlocks();

// Maintain our mutation run experiments; we want this overhead to appear within the stage 6 profile
// FIXME wait, why should this overhead appear in the fitness recalculation step??
for (Species *species : all_species_)
if (species->Active())
species->FinishMutationRunExperimentTimings();
Expand Down Expand Up @@ -3092,6 +3093,7 @@ bool Community::_RunOneTickNonWF(void)
DeregisterScheduledScriptBlocks();

// Maintain our mutation run experiments; we want this overhead to appear within the stage 6 profile
// FIXME wait, why should this overhead appear in late() events??
for (Species *species : all_species_)
if (species->Active())
species->FinishMutationRunExperimentTimings();
Expand Down
61 changes: 50 additions & 11 deletions core/haplosome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ void Haplosome::MakeNull(void)
}
}

void Haplosome::ReinitializeHaplosomeToMutruns(int32_t p_mutrun_count, slim_position_t p_mutrun_length, const std::vector<MutationRun *> &p_runs)
void Haplosome::ReinitializeHaplosomeToMutruns(Individual *individual, int32_t p_mutrun_count, slim_position_t p_mutrun_length, const std::vector<MutationRun *> &p_runs)
{
individual_ = individual;

if (p_mutrun_count)
{
if (mutrun_count_ == 0)
Expand Down Expand Up @@ -295,8 +297,13 @@ void Haplosome::ReinitializeHaplosomeToMutruns(int32_t p_mutrun_count, slim_posi
}
}

void Haplosome::ReinitializeHaplosomeNullptr(int32_t p_mutrun_count, slim_position_t p_mutrun_length)
void Haplosome::ReinitializeHaplosomeNullptr(Individual *individual, int32_t p_mutrun_count, slim_position_t p_mutrun_length)
{
// BCH 10/15/2024: The name of this method states that the reinitialized haplosome will be cleared to nullptr,
// but that is no longer the case since it is no longer necessary. Instead, unless the debugging flag
// SLIM_CLEAR_HAPLOSOMES is set, we just clear to garbage; we provide the allocation structure only.
individual_ = individual;

if (p_mutrun_count)
{
if (mutrun_count_ == 0)
Expand All @@ -308,10 +315,18 @@ void Haplosome::ReinitializeHaplosomeNullptr(int32_t p_mutrun_count, slim_positi
if (mutrun_count_ <= SLIM_HAPLOSOME_MUTRUN_BUFSIZE)
{
mutruns_ = run_buffer_;
#if SLIM_CLEAR_HAPLOSOMES
EIDOS_BZERO(run_buffer_, SLIM_HAPLOSOME_MUTRUN_BUFSIZE * sizeof(const MutationRun *));
#endif
}
else
{
#if SLIM_CLEAR_HAPLOSOMES
mutruns_ = (const MutationRun **)calloc(mutrun_count_, sizeof(const MutationRun *));
#else
mutruns_ = (const MutationRun **)malloc(mutrun_count_ * sizeof(const MutationRun *));
#endif
}
}
else if (mutrun_count_ != p_mutrun_count)
{
Expand All @@ -325,18 +340,28 @@ void Haplosome::ReinitializeHaplosomeNullptr(int32_t p_mutrun_count, slim_positi
if (mutrun_count_ <= SLIM_HAPLOSOME_MUTRUN_BUFSIZE)
{
mutruns_ = run_buffer_;
#if SLIM_CLEAR_HAPLOSOMES
EIDOS_BZERO(run_buffer_, SLIM_HAPLOSOME_MUTRUN_BUFSIZE * sizeof(const MutationRun *));
#endif
}
else
{
#if SLIM_CLEAR_HAPLOSOMES
mutruns_ = (const MutationRun **)calloc(mutrun_count_, sizeof(const MutationRun *));
#else
mutruns_ = (const MutationRun **)malloc(mutrun_count_ * sizeof(const MutationRun *));
#endif
}
}
else
{
#if SLIM_CLEAR_HAPLOSOMES
// the number of mutruns has not changed; need to zero out
if (p_mutrun_count <= SLIM_HAPLOSOME_MUTRUN_BUFSIZE)
EIDOS_BZERO(run_buffer_, SLIM_HAPLOSOME_MUTRUN_BUFSIZE * sizeof(const MutationRun *)); // much faster because optimized at compile time
else
EIDOS_BZERO(mutruns_, p_mutrun_count * sizeof(const MutationRun *));
#endif
}

// we leave the new mutruns_ buffer filled with nullptr
Expand Down Expand Up @@ -445,6 +470,7 @@ EidosValue_SP Haplosome::GetProperty(EidosGlobalStringID p_property_id)
// constants
case gID_chromosome:
{
// We reach our chromosome through our individual; note this prevents standalone haplosome objects
Chromosome *chromosome = individual_->subpopulation_->species_.Chromosomes()[chromosome_index_];

return EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_Object(chromosome, gSLiM_Chromosome_Class));
Expand Down Expand Up @@ -568,6 +594,9 @@ void Haplosome::SetProperty(EidosGlobalStringID p_property_id, const EidosValue
{
case gID_tag: // ACCELERATED
{
// FIXME MULTICHROM if the empty haplosome is shared by multiple clients, its tag value will be shared also!
// need to split off a given haplosome from the shared haplosome object if its tag is set!
// This seems tricky, and is maybe a reason not to use a shared haplosome object in the first place!
slim_usertag_t value = SLiMCastToUsertagTypeOrRaise(p_value.IntAtIndex_NOCAST(0, nullptr));

tag_value_ = value;
Expand Down Expand Up @@ -4139,14 +4168,22 @@ EidosValue_SP Haplosome_Class::ExecuteMethod_removeMutations(EidosGlobalStringID
// just work automatically.
if (recording_tree_sequence_mutations)
{
int haplosome_count_per_individual = species->HaplosomeCountPerIndividual();

// Mark all non-null haplosomes in the simulation that are not among the target haplosomes
for (auto subpop_pair : species->population_.subpops_)
{
Subpopulation *subpop = subpop_pair.second;

for (Individual *ind : subpop->parent_individuals_)
for (Haplosome *haplosome : ind->haplosomes_)
haplosome->scratch_ = (haplosome->IsNull() ? 0 : 1);
{
for (int haplosome_index = 0; haplosome_index < haplosome_count_per_individual; haplosome_index++)
{
Haplosome *haplosome = ind->haplosomes_[haplosome_index];

haplosome->scratch_ = (haplosome->IsNull() ? 0 : 1);
}
}
}

for (int haplosome_index = 0; haplosome_index < target_size; ++haplosome_index)
Expand Down Expand Up @@ -4174,16 +4211,18 @@ EidosValue_SP Haplosome_Class::ExecuteMethod_removeMutations(EidosGlobalStringID

for (Individual *ind : subpop->parent_individuals_)
{
for (Haplosome *haplosome : ind->haplosomes_)
{
if (haplosome->scratch_ == 1)
for (int haplosome_index = 0; haplosome_index < haplosome_count_per_individual; haplosome_index++)
{
for (slim_position_t position : unique_positions)
species->RecordNewDerivedState(haplosome, position, *haplosome->derived_mutation_ids_at_position(position));
haplosome->scratch_ = 0;
Haplosome *haplosome = ind->haplosomes_[haplosome_index];

if (haplosome->scratch_ == 1)
{
for (slim_position_t position : unique_positions)
species->RecordNewDerivedState(haplosome, position, *haplosome->derived_mutation_ids_at_position(position));
haplosome->scratch_ = 0;
}
}
}
}
}
}
}
Expand Down
Loading

0 comments on commit 724bfdc

Please sign in to comment.