Skip to content

Add compact_mutations #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/fwdpp_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,69 @@ fn remove_fixations_from_extant_genomes(
}
}

fn compact_mutations(pop: &mut DiploidPopulation) {
let new_indexes = {
let mut temp = vec![];
let mut i = 0_u32;
temp.resize_with(pop.mutations.len(), || {
let j = i;
i += 1;
j
});
let twon = 2 * (pop.individuals.len() as u32);
let (mut left, _): (Vec<u32>, Vec<u32>) = temp.into_iter().partition(|&index| {
let mcount = *unsafe { pop.mutation_counts.get_unchecked(index as usize) };
mcount > 0 && mcount < twon
});
left.sort_unstable_by_key(|&m| {
unsafe { pop.mutations.get_unchecked(m as usize) }.position()
});
left
};
let reindex = {
let mut temp = vec![u32::MAX; pop.mutations.len()];
for i in 0..new_indexes.len() {
temp[new_indexes[i] as usize] = i as u32;
}
temp
};

// remap the genomes
for g in &mut pop.haplotypes {
if g.count > 0 {
for m in &mut g.mutations {
*m = reindex[*m as usize];
// assert!((*m as usize) < new_indexes.len());
}
}
}

let mut mutations = vec![];
std::mem::swap(&mut mutations, &mut pop.mutations);
// convert all mutations into options
let mut mutations = mutations.into_iter().map(Some).collect::<Vec<_>>();
let mut mutation_counts = vec![];
for i in new_indexes {
mutation_counts.push(pop.mutation_counts[i as usize]);
pop.mutations.push(mutations[i as usize].take().unwrap());
}
std::mem::swap(&mut mutation_counts, &mut pop.mutation_counts);
// assert_eq!(pop.mutation_counts.len(), pop.mutations.len());
//assert!(pop
// .mutations
// .windows(2)
// .all(|w| w[0].position() <= w[1].position()));
//for g in &pop.haplotypes {
// if g.count > 0 {
// assert!(g
// .mutations
// .windows(2)
// .all(|w| pop.mutations[w[0] as usize].position()
// <= pop.mutations[w[1] as usize].position()));
// }
//}
}

#[inline(never)]
fn update_genomes(
genomes: (usize, usize),
Expand Down Expand Up @@ -353,6 +416,11 @@ pub fn evolve_pop(params: SimParams, genetic_map: GeneticMap) -> Option<DiploidP
}
queue = pop.mutation_recycling();
}

if generation % 100 == 0 {
compact_mutations(&mut pop);
queue = vec![];
}
}
Some(pop)
}