def _swap_at_locations(mother, father, locations): """ Swap elements at the given (start, stop) locations in locations. Locations is given by basepair locations, rather than centimorgans or list index. """ locations = tuple(locations) flat_locations = np.fromiter(chain.from_iterable(locations), count=len(locations) * 2, dtype=np.uint32) new_mother = new_sequence(mother, flat_locations) new_father = new_sequence(father, flat_locations) for start, stop in locations: mother_start_i = bisect_left(new_mother.starts, start) mother_stop_i = bisect_left(new_mother.starts, stop, mother_start_i) father_start_i = bisect_left(new_father.starts, start) father_stop_i = bisect_left(new_father.starts, stop, father_start_i) temp_mother = new_mother.starts[mother_start_i:mother_stop_i] temp_father = new_father.starts[father_start_i:father_stop_i] new_mother.starts[mother_start_i:mother_stop_i], \ new_father.starts[father_start_i:father_stop_i] = (temp_father, temp_mother) temp_mother = new_mother.founder[mother_start_i:mother_stop_i] temp_father = new_father.founder[father_start_i:father_stop_i] new_mother.founder[mother_start_i:mother_stop_i], \ new_father.founder[father_start_i:father_stop_i] = (temp_father, temp_mother) return_mother = Diploid(np.array(new_mother.starts, dtype=np.uint32), mother.end, np.array(new_mother.founder, dtype=np.uint32)) return_father = Diploid(np.array(new_father.starts, dtype=np.uint32), father.end, np.array(new_father.founder, dtype=np.uint32)) return (return_mother, return_father)
def generate(self): starts = np.fromiter( (self._chrom_start_offset[chrom] for chrom in CHROMOSOME_ORDER), dtype=np.uint32, count=NUM_CHROMS) mother_founder = np.empty(NUM_CHROMS, dtype=np.uint32) mother_founder.fill(self._genome_id) father_founder = np.empty(NUM_CHROMS, dtype=np.uint32) father_founder.fill(self._genome_id + 1) mother = Diploid(starts, self._total_length, mother_founder) # XXX: Can the start array be shared across some individuals? father = Diploid(np.array(starts, dtype=np.uint32), self._total_length, father_founder) self._genome_id += 2 return RecombGenome(mother, father)
def next_genmo(self): size = len(self._inds) i_dying = random.randrange(size) f = [x.calculate_ind_sc() / size for x in self._inds] parent = random.choice(self._inds, 2, False, f) father = parent[0] mother = parent[1] next_p = father.replicate_error().make_zygote() next_m = mother.replicate_error().make_zygote() self._inds[i_dying] = Diploid(next_p, next_m) return self
def __init__(self, popsize, replength, mut_allelerate=0.0, i=None, s=0.0): mutantrate = mut_allelerate**2 heterorate = 2 * mut_allelerate * (1 - mut_allelerate) num_mutant = int(popsize * mutantrate) num_hetero = int(popsize * heterorate) num_notwild = num_mutant + num_hetero mutant_inds = [ Diploid(Tandem_repeat(x, replength, i, s)) for x in range(num_mutant) ] het_inds = [ Diploid(Tandem_repeat(x, replength), Tandem_repeat(x, replength, i, s)) for x in range(num_mutant, num_notwild) ] wild_inds = [ Diploid(Tandem_repeat(x, replength)) for x in range(num_notwild, popsize) ] self._inds = mutant_inds + het_inds + wild_inds
def next_genwf(self): next_generation = [] size = len(self._inds) for x in range(size): f = [x.calculate_ind_sc() / size for x in self._inds] parent = random.choice(self._inds, 2, False, f) father = parent[0] mother = parent[1] next_p = father.replicate_error().make_zygote() next_m = mother.replicate_error().make_zygote() next_generation.append(Diploid(next_p, next_m)) self._inds = next_generation return self