# 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 simu = sim.Simulator(sim.Population(50, loci=[10], ploidy=1), rep=3) simu.evolve(gen = 5) simu.dvars(0).gen simu.evolve( initOps=[sim.InitGenotype(freq=[0.5, 0.5])], matingScheme=sim.RandomSelection(), postOps=[ sim.Stat(alleleFreq=5), sim.IfElse('alleleNum[5][0] == 0', sim.PyEval(r"'Allele 0 is lost in rep %d at gen %d\n' % (rep, gen)")), sim.IfElse('alleleNum[5][0] == 50', sim.PyEval(r"'Allele 0 is fixed in rep %d at gen %d\n' % (rep, gen)")), sim.TerminateIf('len(alleleNum[5]) == 1'), ], ) [simu.dvars(x).gen for x in range(3)]
# 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 import time pop = sim.Population(1000, loci=10) pop.dvars().init_time = time.time() pop.dvars().last_time = time.time() exec('import time', pop.vars(), pop.vars()) pop.evolve( initOps=sim.InitSex(), matingScheme=sim.RandomMating(), postOps=[ sim.IfElse('time.time() - last_time > 5', [ sim.PyEval(r'"Gen: %d\n" % gen'), sim.PyExec('last_time = time.time()') ]), sim.TerminateIf('time.time() - init_time > 20') ] )
# (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 simu = sim.Simulator(sim.Population(100, loci=1), rep=5) simu.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]) ], matingScheme = sim.RandomMating(), postOps=[ sim.Stat(alleleFreq=0), sim.TerminateIf('len(alleleFreq[0]) == 1') ] )
import simuOpt simuOpt.setOptions(quiet=True, alleleType='long') import simuPOP as sim pop = sim.Population(size=1000, loci=[1]) simu = sim.Simulator(pop, 5) simu.evolve( initOps=[ sim.InitSex(), sim.PyExec('introGen=[]') ], preOps=[ sim.Stat(alleleFreq=0), sim.IfElse('alleleFreq[0][1] == 0', ifOps=[ sim.PointMutator(loci=0, allele=1, inds=0), sim.PyExec('introGen.append(gen)') ]), sim.TerminateIf('alleleFreq[0][1] >= 0.05') ], matingScheme=sim.RandomMating() ) # number of attempts print([len(x.dvars().introGen) for x in simu.populations()]) # age of mutant print([x.dvars().gen - x.dvars().introGen[-1] for x in simu.populations()])
sim.PyExec("Surviving = {'larvae': [], 'adults': [], 'smurfs': []}") ], preOps = [ sim.InfoExec("luck = random.random()"), sim.InfoExec("smurf = 1 if ((ind.smurf == 1) or (model == 'two_phases' and ind.age > ind.t0 and ind.luck <= 1.0 - math.exp(-ind.a * ind.age + ind.a * ind.t0 - ind.a / 2.0))) else 0", exposeInd='ind'), sim.DiscardIf(aging_model(args.model)), sim.InfoExec("age += 1") ], matingScheme = sim.CloneMating(subPops = sim.ALL_AVAIL, subPopSize = demo), postOps = [ sim.Stat(popSize=True, subPops=[(0,0), (0,1), (0,2)]), sim.PyExec("Surviving['larvae'].append(subPopSize[0])"), sim.PyExec("Surviving['adults'].append(subPopSize[1])"), sim.PyExec("Surviving['smurfs'].append(subPopSize[2])"), # sim.PyEval(r'"{:d}\t{:d}\t{:d}\t{:d}\n".format(gen, subPopSize[0], subPopSize[1], subPopSize[2])', step=1), sim.TerminateIf('popSize == 0') ], gen=args.G ) args.output.write("#Gen\t" + "\t".join(['Larvae\tAdults\tSmurfs' for a in range(args.replicates)]) + "\n") for day in range(args.G): line = "{:3d}".format(day + 1) for rep in range(args.replicates): for kind in ['larvae', 'adults', 'smurfs']: try: line += "\t{:.4f}".format(simu.vars(rep)['Surviving'][kind][day] / args.N) except IndexError: line += "\t0.0000" args.output.write(line + "\n")
# the user's guide (http://simupop.sourceforge.net/manual) for a detailed # description of this example. # import simuPOP as sim import simuPOP.demography as demo model = demo.MultiStageModel([ demo.InstantChangeModel( N0=1000, ops=[ sim.Stat(alleleFreq=sim.ALL_AVAIL, numOfSegSites=sim.ALL_AVAIL), # terminate if the average allele frequency of segregating sites # are more than 0.1 sim.TerminateIf( 'sum([x[1] for x in alleleFreq.values() if ' 'x[1] != 0])/(1 if numOfSegSites==0 else numOfSegSites) > 0.1') ]), demo.ExponentialGrowthModel(N0=[0.5, 0.5], r=0.01, NT=[2000, 5000]) ]) pop = sim.Population(size=model.init_size, loci=100) pop.evolve( initOps=sim.InitSex(), preOps=sim.SNPMutator(u=0.001, v=0.001), matingScheme=sim.RandomMating(subPopSize=model), postOps=[ sim.Stat(alleleFreq=sim.ALL_AVAIL, numOfSegSites=sim.ALL_AVAIL, popSize=True, step=50),
N = 100 p = 0.4 pop = sim.Population(2 * N, ploidy=1, loci=1) # Use a simulator to simulate 500 populations simultaneously. simu = sim.Simulator(pop, rep=500) gens = simu.evolve( initOps=sim.InitGenotype(prop=[p, 1 - p]), # A RandomSelection mating scheme choose parents randomly regardless # of sex and copy parental genotype to offspring directly. matingScheme=sim.RandomSelection(), postOps=[ # calculate allele frequency at locus 0 sim.Stat(alleleFreq=0), # and terminate the evolution of a population if it has no # allele 0 or 1 at locus 0. sim.TerminateIf('alleleFreq[0][0] in (0, 1)'), ], ) # find out populations with or without allele 1 gen_lost = [] gen_fixed = [] for gen, pop in zip(gens, simu.populations()): if pop.dvars().alleleFreq[0][0] == 0: gen_lost.append(gen) else: gen_fixed.append(gen) print('''\nMean persistence time: %.2f (expected: %.2f) Lost pops: %d (expected: %.1f), Mean persistence time: %.2f (expected: %.2f) Fixed pops: %d (expected: %.1f), Mean persistence time: %.2f (expected: %.2f)'''\ % (float(sum(gens)) / len(gens), -4*N*((1-p)*log(1-p) + p*log(p)),
# 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 simu = sim.Simulator(sim.Population(size=100, loci=1), rep=10) simu.evolve(initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]), ], matingScheme=sim.RandomMating(), postOps=[ sim.Stat(alleleFreq=0), sim.TerminateIf('len(alleleFreq[0]) == 1', stopAll=True) ])
def MutationSelection(N=1000, generations=10000, X_loci=100, A_loci=0, AgingModel='two_phases', seed=2001, reps=1, InitMutFreq=0.001, aging_a1=0.003, aging_a2=0.05, aging_b=-0.019, aging_k=0.1911, MutRate=0.001, StatsStep=100, OutPopPrefix='z1', PrintFreqs=False, debug=False): '''Creates and evolves a population to reach mutation-selection balance.''' if debug: sim.turnOnDebug('DBG_ALL') else: sim.turnOffDebug('DBG_ALL') sim.setRNG('mt19937', seed) pop = sim.Population(N, loci=[X_loci, A_loci], ploidy=2, chromTypes=[sim.CHROMOSOME_X, sim.AUTOSOME], infoFields=[ 'age', 'a', 'b', 'smurf', 'ind_id', 'father_id', 'mother_id', 'luck', 't0', 'fitness' ]) pop.setVirtualSplitter( sim.CombinedSplitter( splitters=[ sim.ProductSplitter(splitters=[ sim.InfoSplitter(field='age', cutoff=9), sim.InfoSplitter(field='smurf', values=[0, 1]) ]), sim.SexSplitter(), sim.InfoSplitter(field='age', values=0) ], vspMap=[(0), (2), (1, 3), (4), (5), (6)], names=['larvae', 'adults', 'smurfs', 'males', 'females', 'zero'])) pop.dvars().k = aging_k pop.dvars().N = N pop.dvars().seed = seed pop.dvars().X_loci = X_loci pop.dvars().A_loci = A_loci pop.dvars().AgingModel = AgingModel exec("import random\nrandom.seed(seed)", pop.vars(), pop.vars()) exec("import math", pop.vars(), pop.vars()) simu = sim.Simulator(pop, rep=reps) simu.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[1 - InitMutFreq, InitMutFreq]), sim.InitInfo([0], infoFields='age'), sim.InitInfo([aging_a1], infoFields='a'), sim.InitInfo([aging_b], infoFields='b'), sim.InitInfo(lambda: random.random(), infoFields='luck'), sim.InfoExec('t0 = -ind.b / ind.a', exposeInd='ind'), sim.InfoExec( 'smurf = 1.0 if AgingModel == "two_phases" and (ind.smurf == 1 or (ind.age > ind.t0 and ind.luck < 1.0 - math.exp(-ind.a * ind.age + ind.a * ind.t0 - ind.a / 2.0))) else 0.0', exposeInd='ind'), sim.IdTagger(), sim.PyExec('XFreqChange={}'), sim.PyExec('AFreqChange={}') ], preOps=[ sim.InfoExec('luck = random.random()'), sim.InfoExec( 'smurf = 1.0 if AgingModel == "two_phases" and (ind.smurf == 1 or (ind.age > ind.t0 and ind.luck < 1.0 - math.exp(-ind.a * ind.age + ind.a * ind.t0 - ind.a / 2.0))) else 0.0', exposeInd='ind'), sim.DiscardIf(natural_death(AgingModel)), sim.InfoExec('age += 1'), sim.PySelector(func=fitness_func1) ], matingScheme=sim.HeteroMating([ sim.CloneMating(subPops=[(0, 0), (0, 1), (0, 2)], weight=-1), sim.RandomMating(ops=[ sim.IdTagger(), sim.PedigreeTagger(), sim.InfoExec('smurf = 0.0'), sexSpecificRecombinator( rates=[0.75 / X_loci for x in range(X_loci)] + [2.07 / A_loci for x in range(A_loci)], maleRates=0.0), sim.PyQuanTrait(loci=sim.ALL_AVAIL, func=TweakAdditiveRecessive( aging_a1, aging_a2, aging_b, X_loci), infoFields=['a', 'b']) ], weight=1, subPops=[(0, 1)], numOffspring=1) ], subPopSize=demo), postOps=[ sim.SNPMutator(u=MutRate, subPops=[(0, 5)]), sim.Stat(alleleFreq=sim.ALL_AVAIL, step=StatsStep), sim.IfElse( 'X_loci > 0', ifOps=[ sim.PyExec( 'XFreqChange[gen] = [alleleFreq[x][1] for x in range(X_loci)]' ) ], elseOps=[sim.PyExec('XFreqChange[gen] = []')], step=StatsStep), sim.IfElse( 'A_loci > 0', ifOps=[ sim.PyExec( 'AFreqChange[gen] = [alleleFreq[a][1] for a in range(X_loci, pop.totNumLoci())]', exposePop='pop') ], elseOps=[sim.PyExec('AFreqChange[gen] = []')], step=StatsStep), sim.IfElse( PrintFreqs, ifOps=[ sim.PyEval( r"str(rep) + '\t' + str(gen) + '\t' + '\t'.join(map('{0:.4f}'.format, XFreqChange[gen])) + '\t\t' + '\t'.join(map('{0:.4f}'.format, AFreqChange[gen])) + '\n'" ) ], step=StatsStep), sim.TerminateIf( 'sum([alleleFreq[x][0] * alleleFreq[x][1] for x in range(X_loci + A_loci)]) == 0' ) ], gen=generations) i = 0 for pop in simu.populations(): pop.save('{}_{}.pop'.format(OutPopPrefix, i)) i += 1