def expand_by_selfing(self, pop, recombination_rates): """ Specific for plant populations capable of selfing. Creates an F2 subpopulations generation by selfing the individuals of 'pop'. Works on a population with one or more subpopulations. :param pop: """ # self.odd_to_even(pop) num_sub_pops = pop.numSubPop() progeny_per_individual = int(self.operating_population_size / 2) return pop.evolve( preOps=[ sim.MergeSubPops(), sim.PyEval(r'"Generation: %d\n" % gen'), sim.SplitSubPops(sizes=[1] * num_sub_pops, randomize=False), ], matingScheme=sim.SelfMating( subPopSize=[progeny_per_individual] * num_sub_pops, numOffspring=progeny_per_individual, ops=[ sim.Recombinator(rates=recombination_rates), sim.IdTagger(), sim.PedigreeTagger() ], ), gen=1, )
def generate_f_one(self, pop: sim.Population): """ A very basic implementation of the F_1 cross between pairs of individuals. Relies on pre-formatting of desired mating pairs into an ordered list. [1, 3, 5, 11, 12, 9, 22, 2] The mating pattern would be: 1x3, 5x11, 12x9, 22x2. Rearranging the order of the indices would change the mating pairs. :param pop: :type pop: :return: :rtype: """ pop.dvars().generations[1] = 'F_1' pop.dvars().gen = 1 pairs_of_founders = int(pop.popSize() / 2) self.odd_to_even(pop) print("Creating the F_one population from selected founders.") return pop.evolve( preOps=[ sim.PyEval(r'"Generation: %d\n" % gen'), operators.CalcTripletFrequencies(), sim.PyExec('triplet_freq[gen]=tripletFreq'), sim.SplitSubPops(sizes=[2] * pairs_of_founders, randomize=False), ], matingScheme=sim.RandomMating(subPopSize=[1] * pairs_of_founders, ops=[sim.Recombinator(rates=0.01), sim.IdTagger(), sim.PedigreeTagger()]), gen=1, )
def _mate_and_merge(self, pop: sim.Population): starting_gen = pop.vars()['gen'] print("Initiating recombinatorial convergence at generation: %d" % pop.dvars().gen) while pop.numSubPop() > 1: pop.vars()['generations'][pop.vars()['gen']] = 'IG'+str(pop.vars()['gen'] - starting_gen) self.pop_halver(pop) self.odd_to_even(pop) self.pairwise_merge_protocol(pop) sub_pop_sizes = list(pop.subPopSizes()) pop.evolve( preOps=[ sim.MergeSubPops(), sim.PyEval(r'"Generation: %d\n" % gen'), operators.CalcTripletFreq(), sim.PyExec('triplet_freq[gen]=tripletFreq'), sim.SplitSubPops(sizes=sub_pop_sizes, randomize=False), ], matingScheme=sim.RandomMating(ops=[sim.Recombinator(rates=0.01), sim.IdTagger(), sim.PedigreeTagger()]), gen=1, )
def _generate_f_two(self, pop: sim.Population) -> sim.Population: """ Creates an F2 subpopulations generation by selfing the individuals of 'pop'. Works on a population with one or more subpopulations. """ pop.vars()['generations'][2] = 'F_2' self.odd_to_even(pop) num_sub_pops = pop.numSubPop() progeny_per_individual = int(self.selected_population_size/2) print("Creating the F_two population.") return pop.evolve( preOps=[ sim.MergeSubPops(), sim.PyEval(r'"Generation: %d\n" % gen'), operators.CalcTripletFreq(), sim.PyExec('triplet_freq[gen]=tripletFreq'), sim.SplitSubPops(sizes=[1]*num_sub_pops, randomize=False), ], matingScheme=sim.SelfMating(subPopSize=[progeny_per_individual] * num_sub_pops, numOffspring=progeny_per_individual, ops=[sim.Recombinator(rates=0.01), sim.IdTagger(), sim.PedigreeTagger()], ), gen=1, )
vec_env = [uniform(-1, 1), uniform(-1, 1)] #Creating initial population of size popsize and "caryotype" vecloc pop = sim.Population(size=[popsize], loci=vecloc, infoFields=['migrate_to', 'fitness', 'env'], lociNames=allele_naming(numchrom, numloc)) #--------------------------Main evolving process------------------------ pop.evolve( #Initializing sex and genotype initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]), ], preOps=[ #Splitting each population into two at 'atgen' generations sim.SplitSubPops(proportions=[0.5, 0.5], at=atgen), #Calling function 'migration' for individuals migration #(Operator Migrator does not allow for varying number of subpopulations) sim.PyOperator(migration, begin=atgen[0]), #Selection process #Create new environmental values for new daughter populations #Only takes place when fission takes place (atgen) sim.PyOperator(env_update, at=atgen), #Set environmental value (env infoField) for each individual in the population #Takes place at each generation after the first fission sim.PyOperator(env_set, begin=atgen[1]), #Selection occures at selected loci according to env information field sim.PySelector(fit_env, loci=locisel, begin=atgen[1]), ], #Mating at random (pangamy) matingScheme=sim.RandomMating(
print('Simulation number ' + str(k)) #Creating initial population of size popsize and "caryotype" vecloc pop = sim.Population(size=[popsize], loci=vecloc, infoFields=['migrate_to', 'fitness', 'env'], lociNames=allele_naming(numchrom, numloc)) #--------------------------Main evolving process------------------------ pop.evolve( #Initializing sex and genotype initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]), ], preOps=[ #Fisson of population into 16 at 'atgen' generation sim.SplitSubPops(proportions=[0.0625] * 16, at=atgen), #Migration using a simple stepping stone model sim.Migrator(rate=migrSteppingStoneRates(m, 16, circular=False), begin=atgen), #Selection process #Set environmental value (env infoField) for each individual in the population #Takes place at each generation after the first fission sim.PyOperator(env_set, begin=atgen), #Selection occures at selected loci according to env information field sim.PySelector(fit_env, loci=locisel, begin=atgen), ], #Mating at random (pangamy) matingScheme=sim.RandomMating( #Fixed population size (fixed at 'popsize') subPopSize=demo, #Recombination
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # This script is an example in the simuPOP user's guide. Please refer to # the user's guide (http://simupop.sourceforge.net/manual) for a detailed # description of this example. # import simuPOP as sim def demo(gen, pop): if gen < 2: return 1000 + 100 * gen else: return [x + 50 * gen for x in pop.subPopSizes()] pop = sim.Population(1000) pop.evolve(preOps=[ sim.SplitSubPops(subPops=0, proportions=[.5] * 2, at=2), sim.Stat(popSize=True), sim.PyEval(r'"Gen %d:\t%s\n" % (gen, subPopSize)') ], matingScheme=sim.RandomSelection(subPopSize=demo), gen=4)
def simulate(NA, N1, N2, Tbeforesplit, Taftersplit, r2loci, numLoci, K_sel, Mu_sel, S_sel, L_neut, Mu_neut, Nsample1, Nsample2): pop = Population(size=NA, ploidy=2, loci=[L_neut + 1], infoFields='fitness') def getfitness(geno): # returns fitness of genotype geno at the overdominant locus with selection coeff S_sel # geno is (A1 A2) if geno[0] == geno[1]: return 1 - S_sel # homozygote else: return 1 # heterozygote if useRPy: plotter = VarPlotter( 'alleleFreq[0][0],alleleFreq[0][1],alleleFreq[0][2],alleleFreq[0][3],alleleFreq[0][4]', ylim=[0, 1], ylab='allele frequency', update=Tbeforesplit + Taftersplit - 1, saveAs='slocus.png') else: plotter = NoneOp() g = pop.evolve( initOps=[ InitSex(), #initially put 5 alleles at the selected locus with equal frequencies InitGenotype(loci=0, freq=[.1] * 10) # InitGenotype(loci=0, freq=[0.01, 0.1, 0.4, 0.2, 0.29]) ], preOps=[ #Resize the ancestral population at the time immediatly before the split sim.ResizeSubPops([0], sizes=[N1 + N2], at=Tbeforesplit - 1), # split ancestral population in 3 subpopulations only works if NA>N1+N2 sim.SplitSubPops(subPops=0, sizes=[N1, N2], at=Tbeforesplit), # apply overdominant selection by invoking function getfitness PySelector(loci=0, func=getfitness), ], matingScheme=RandomMating(ops=[ #apply recombination between the selected locus and the neutral locus at rate r2loci Recombinator(rates=r2loci, loci=0), ]), postOps=[ # apply mutation to the selected locus according to K allele model KAlleleMutator(k=K_sel, rates=[Mu_sel], loci=[0]), # apply mutation to the neutral sequence SNPMutator(u=Mu_neut, v=0, loci=range(1, L_neut)), #Computes the frequency of each allele at selected locus at the last generation Stat(alleleFreq=0, step=1), #output to the screen the frequency of the 4 first alleles at the selected locus PyEval( r'"%.0f\t %.3f\t %.3f\t %.3f\t %.3f\t %.3f\n" % (gen, alleleFreq[0][0], alleleFreq[0][1], alleleFreq[0][2], alleleFreq[0][3], alleleFreq[0][4])', step=100), plotter, ], # sets the last generation = Tbeforesplit+Taftersplit gen=Tbeforesplit + Taftersplit) #draw two random samples from species 1 and 2 with size Nsample1 and Nsample2 sample = drawRandomSample(pop, sizes=[Nsample1, Nsample2]) #write to file the content of the two random samples sim.utils.saveCSV(sample, filename='output.txt') return g
def replicate_recurrent_drift(self, multi_pop, meta_sample_library, qtl, allele_effects, recombination_rates): """ :param multi_pop: :param meta_pop_sample_library: :param qtl: :param allele_effects: :param recombination_rates: :return: """ for pop in multi_pop.populations(): pop.dvars().gen = 0 sizes = [self.individuals_per_breeding_subpop] \ * self.number_of_breeding_subpops + \ [self.number_of_nonbreeding_individuals] offspring_pops = [self.offspring_per_breeding_subpop] \ * self.number_of_breeding_subpops + [0] assert len(sizes) == len(offspring_pops), "Number of parental " \ "subpopulations must equal " \ "the number of offspring " \ "subpopulations" sampling_generations = [ i for i in range(2, self.generations_of_drift, 2) ] pc = breed.HalfSibBulkBalanceChooser( self.individuals_per_breeding_subpop, self.offspring_per_female) multi_pop.evolve( initOps=[ sim.InitInfo(0, infoFields=['generation']), sim.InfoExec('replicate=rep'), operators.GenoAdditiveArray(qtl, allele_effects), operators.CalculateErrorVariance(self.heritability), operators.PhenotypeCalculator( self.proportion_of_individuals_saved), operators.ReplicateMetaPopulation(meta_sample_library, self.meta_pop_sample_sizes), sim.PyEval(r'"Initial: Sampled %d individuals from generation ' r'%d Replicate: %d.\n" % (ss, gen_sampled_from, ' r'rep)'), ], preOps=[ sim.PyEval(r'"Generation: %d\n" % gen'), operators.GenoAdditiveArray(qtl, allele_effects, begin=1), sim.InfoExec('generation=gen'), sim.InfoExec('replicate=rep'), operators.PhenotypeCalculator( self.proportion_of_individuals_saved, begin=1), operators.ReplicateMetaPopulation(meta_sample_library, self.meta_pop_sample_sizes, at=sampling_generations), sim.SplitSubPops(sizes=sizes, randomize=False), ], matingScheme=sim.HomoMating( sim.PyParentsChooser(pc.recursive_pairwise_parent_chooser), sim.OffspringGenerator(ops=[ sim.IdTagger(), sim.PedigreeTagger(), sim.Recombinator(rates=recombination_rates) ], numOffspring=1), subPopSize=offspring_pops, subPops=list(range(1, self.number_of_breeding_subpops, 1))), postOps=[ sim.MergeSubPops(), operators.DiscardRandomOffspring( self.number_of_offspring_discarded), ], finalOps=[ sim.InfoExec('generation=gen'), sim.InfoExec('replicate=rep'), operators.GenoAdditiveArray(qtl, allele_effects), operators.PhenotypeCalculator( self.proportion_of_individuals_saved), operators.ReplicateMetaPopulation(meta_sample_library, self.meta_pop_sample_sizes), sim.PyEval( r'"Final: Sampled %d individuals from generation %d\n" ' r'% (ss, gen_sampled_from)'), ], gen=self.generations_of_drift)
def recurrent_drift(self, pop, meta_pop, qtl, aes, recombination_rates): """ Sets up and runs recurrent selection for a number of generations for a single replicate population. Samples individuals at specified intervals to make a ``meta_pop``. :param pop: Population which undergoes selection. :param meta_pop: Population into which sampled individuals are deposited :param qtl: List of loci to which allele effects have been assigned :param aes: Dictionary of allele effects """ pop.dvars().gen = 0 meta_pop.dvars().gen = 0 sizes = [self.individuals_per_breeding_subpop] \ * self.number_of_breeding_subpops + \ [self.number_of_nonbreeding_individuals] offspring_pops = [self.offspring_per_breeding_subpop] \ * self.number_of_breeding_subpops + [0] assert len(sizes) == len(offspring_pops), "Number of parental " \ "subpopulations must equal " \ "the number of offspring " \ "subpopulations" sampling_generations = [ i for i in range(2, self.generations_of_drift, 2) ] pc = breed.HalfSibBulkBalanceChooser( self.individuals_per_breeding_subpop, self.offspring_per_female) pop.evolve( initOps=[ sim.InitInfo(0, infoFields=['generation']), operators.GenoAdditiveArray(qtl, aes), operators.CalculateErrorVariance(self.heritability), operators.PhenotypeCalculator( self.proportion_of_individuals_saved), operators.MetaPopulation(meta_pop, self.meta_pop_sample_sizes), sim.PyEval(r'"Initial: Sampled %d individuals from generation ' r'%d Replicate: %d.\n" % (ss, gen_sampled_from, ' r'rep)'), ], preOps=[ sim.PyEval(r'"Generation: %d\n" % gen'), operators.GenoAdditiveArray(qtl, aes, begin=1), sim.InfoExec('generation=gen'), operators.PhenotypeCalculator( self.proportion_of_individuals_saved, begin=1), operators.MetaPopulation(meta_pop, self.meta_pop_sample_sizes, at=sampling_generations), sim.SplitSubPops(sizes=sizes, randomize=True), ], matingScheme=sim.HomoMating( sim.PyParentsChooser(pc.recursive_pairwise_parent_chooser), sim.OffspringGenerator(ops=[ sim.IdTagger(), sim.PedigreeTagger(), sim.Recombinator(rates=recombination_rates) ], numOffspring=1), subPopSize=offspring_pops, subPops=list(range(1, self.number_of_breeding_subpops, 1))), postOps=[ sim.MergeSubPops(), operators.DiscardRandomOffspring( self.number_of_offspring_discarded), ], finalOps=[ sim.InfoExec('generation=gen'), operators.GenoAdditive(qtl, aes), operators.PhenotypeCalculator( self.proportion_of_individuals_saved), operators.MetaPopulation(meta_pop, self.meta_pop_sample_sizes), sim.PyEval( r'"Final: Sampled %d individuals from generation %d\n" ' r'% (ss, gen_sampled_from)'), ], gen=self.generations_of_drift)
# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # This script is an example in the simuPOP user's guide. Please refer to # the user's guide (http://simupop.sourceforge.net/manual) for a detailed # description of this example. # import simuPOP as sim pop = sim.Population(1000) pop.evolve(preOps=[ sim.SplitSubPops(subPops=0, sizes=[300, 300, 400], at=2), sim.Stat(popSize=True), sim.PyEval(r'"Gen %d:\t%s\n" % (gen, subPopSize)') ], matingScheme=sim.RandomSelection(), gen=4)