def prepare_sim(self, params): for view in self._views: for info in view.info_fields: self._info_fields.add(info) nloci = 1 + params['neutral_loci'] pop, init_ops, pre_ops, post_ops = \ self._create_single_pop(params['pop_size'], nloci) view_ops = [] for view in self._views: view.pop = pop view_ops.extend(view.view_ops) for view in self._views: post_ops.append(sp.PyOperator(func=_hook_view, param=view)) post_ops = view_ops + post_ops loci, genome_init = self._create_snp_genome( nloci, freq=params['snp_freq']) sim = sp.Simulator(pop, 1, True) if params['sel_type'] == 'hz_advantage': ms = sp.MapSelector(loci=0, fitness={ (0, 0): 1 - params['sel'], (0, 1): 1, (1, 1): 1 - params['sel']}) elif params['sel_type'] == 'recessive': ms = sp.MapSelector(loci=0, fitness={ (0, 0): 1 - params['sel'], (0, 1): 1 - params['sel'], (1, 1): 1}) else: # dominant ms = sp.MapSelector(loci=0, fitness={ (0, 0): 1 - params['sel'], (0, 1): 1, (1, 1): 1}) return {'sim': sim, 'pop': pop, 'init_ops': init_ops + genome_init, 'pre_ops': pre_ops, 'post_ops': post_ops, 'mating_scheme': sp.RandomMating( ops=[sp.MendelianGenoTransmitter(), ms])}
# 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(size=1000, loci=1, infoFields='fitness') s1 = .1 s2 = .2 pop.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[.2, .8]) ], preOps=sim.MapSelector(loci=0, fitness={(0,0):1-s1, (0,1):1, (1,1):1-s2}), matingScheme=sim.RandomMating(), postOps=[ sim.Stat(alleleFreq=0), sim.PyEval(r"'%.4f\n' % alleleFreq[0][0]", step=100) ], gen=301 )
# 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, loci=[5]*4, # one autosome, two sex chromosomes, and one mitochondrial chromosomes chromTypes=[sim.AUTOSOME, sim.CHROMOSOME_X, sim.CHROMOSOME_Y, sim.MITOCHONDRIAL], infoFields=['fitness']) pop.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.25]*4) ], preOps=[ sim.MapSelector(loci=17, fitness={(0,): 1, (1,): 1, (2,): 1, (3,): 0.4}) ], matingScheme=sim.RandomMating(ops= [ sim.Recombinator(rates=0.1), sim.MitochondrialGenoTransmitter(), ]), postOps=[ sim.Stat(alleleFreq=17, step=10), sim.PyEval(r'"%.2f %.2f %.2f %.2f\n" % (alleleNum[17][0],' 'alleleNum[17][1], alleleNum[17][2], alleleNum[17][3])', step=10), ], gen = 100 )
# 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(4000, loci=1, infoFields='fitness') simu = sim.Simulator(pop, rep=3) simu.evolve(initOps=[sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5])], preOps=sim.MapSelector(loci=0, fitness={ (0, 0): 1, (0, 1): 0.98, (1, 1): 0.97 }), matingScheme=sim.RandomMating(), postOps=[ sim.Stat(alleleFreq=0, step=10), sim.PyEval("'Gen:%3d ' % gen", reps=0, step=10), sim.PyEval(r"'%.3f\t' % alleleFreq[0][1]", step=10), sim.PyOutput('\n', reps=-1, step=10) ], gen=50)
import simuPOP as sim from simuPOP.plotter import VarPlotter pop = sim.Population(size=10000, loci=1, infoFields='fitness') simu = sim.Simulator(pop, rep=3) h = [0.5, 0.2, -0.5] s = [0.1, -0.1, 0.1] simu.evolve(initOps=[sim.InitSex(), sim.InitGenotype(freq=(0.5, 0.5))], preOps=[ sim.MapSelector(loci=0, fitness={ (0, 0): 1, (0, 1): 1 - h[x] * s[x], (1, 1): 1 - s[x] }, reps=x) for x in range(3) ], matingScheme=sim.RandomMating(), postOps=[ sim.Stat(alleleFreq=0), sim.PyEval(r'"%.3f\t" % alleleFreq[0][1]', step=50), sim.PyOutput('\n', reps=-1, step=50), VarPlotter( 'alleleFreq[0][0]', update=200, legend=['h=%.1f s=%.1f' % (x, y) for x, y in zip(h, s)], saveAs='Figures/selection.pdf', lines_lty_rep=[1, 2, 3], lines_col='black', lines_lwd=2, legend_x=120,
# Case 1: produce the given number of offspring checkNumOffspring(numOffspring=2) # Case 2: Use a Python function import random def func(gen): return random.randint(5, 8) checkNumOffspring(numOffspring=func) # Case 3: A geometric distribution checkNumOffspring(numOffspring=(sim.GEOMETRIC_DISTRIBUTION, 0.3)) # Case 4: A Possition distribution checkNumOffspring(numOffspring=(sim.POISSON_DISTRIBUTION, 1.6)) # Case 5: A Binomial distribution checkNumOffspring(numOffspring=(sim.BINOMIAL_DISTRIBUTION, 0.1, 10)) # Case 6: A uniform distribution checkNumOffspring(numOffspring=(sim.UNIFORM_DISTRIBUTION, 2, 6)) # Case 7: With selection on offspring checkNumOffspring(numOffspring=8, ops=[ sim.MapSelector(loci=0, fitness={ (0, 0): 1, (0, 1): 0.8, (1, 1): 0.5 }) ])
# # 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(size=10000, loci=2, infoFields='fitness') pop.evolve( initOps=[sim.InitSex(), sim.InitGenotype(freq=[.5, .5])], preOps=[ sim.MlSelector([ sim.MapSelector(loci=0, fitness={ (0, 0): 1, (0, 1): 1, (1, 1): .8 }), sim.MapSelector(loci=1, fitness={ (0, 0): 1, (0, 1): 0.9, (1, 1): .8 }), ], mode=sim.ADDITIVE, reps=0), sim.MapSelector(loci=0, fitness={ (0, 0): 1, (0, 1): 1,
def simu(w, m1, m2, psize, afr_size, fl): print(fl) if os.path.exists(fl): os.remove(fl) matingScheme = sim.HaplodiploidMating(sexMode=sex_func, subPopSize=get_sizes) fitness = { (0, ): 1.0, (1, ): 1.0, (0, 0): w, (0, 1): (1 + w) / 2, (1, 1): 1.0 } migrator = sim.BackwardMigrator(rate=[[0, m1, m2], [m1, 0, m2], [0, 0, 0]], begin=4, step=1) count_pop = {} sg = sample_genes() for i in range(len(sg)): (n_loci, gene_prop_az, gene_prop_tx) = sg[i] selector = sim.MlSelector( [sim.MapSelector(loci=x, fitness=fitness) for x in range(n_loci)], mode=sim.ADDITIVE, begin=4, step=1) pre_ops = [ migrator, selector, sim.InitGenotype(prop=(0.1, 0.9), subPops=[2]) ] post_ops = [ sim.ResizeSubPops(subPops=[2], sizes=[math.ceil(psize * afr_size)], propagate=True, at=g) for g in range(1, 11) ] pop = sim.Population( size=[psize, psize, math.ceil(afr_size * psize)], ploidy=2, loci=n_loci, subPopNames=['AZ', 'TX', 'AFR'], ancGen=-1, infoFields=['fitness', 'migrate_to', 'migrate_from']) # store all past generations pop.evolve(initOps=[ sim.InitSex(maleFreq=0.9), sim.InitGenotype(prop=(gene_prop_az, 1 - gene_prop_az), subPops=[0]), sim.InitGenotype(prop=(gene_prop_tx, 1 - gene_prop_tx), subPops=[1]), sim.InitGenotype(prop=(0.1, 0.9), subPops=[2]) ], matingScheme=matingScheme, preOps=[], postOps=[], gen=1) sim.stat(pop, alleleFreq=list(range(n_loci)), subPops=[0]) az1 = np.mean( np.array([pop.dvars().alleleFreq[loc][1] for loc in range(n_loci)])) sim.stat(pop, alleleFreq=list(range(n_loci)), subPops=[1]) tx1 = np.mean( np.array([pop.dvars().alleleFreq[loc][1] for loc in range(n_loci)])) cp = export(pop, fl, False) count_pop["AZ_early"] = cp["AZ"] count_pop["TX_early"] = cp["TX"] pop.evolve(initOps=[], matingScheme=matingScheme, preOps=pre_ops, postOps=post_ops, gen=10) sim.stat(pop, alleleFreq=list(range(n_loci)), subPops=[0]) az2 = np.mean( np.array([pop.dvars().alleleFreq[loc][1] for loc in range(n_loci)])) sim.stat(pop, alleleFreq=list(range(n_loci)), subPops=[1]) tx2 = np.mean( np.array([pop.dvars().alleleFreq[loc][1] for loc in range(n_loci)])) cp = export(pop, fl, True) count_pop["AZ_late"] = cp["AZ"] count_pop["TX_late"] = cp["TX"] print("%i) nloci: %i AZ : %.3f->%.3f TX : %.3f->%.3f" % (i, n_loci, az1, az2, tx1, tx2)) n = count_pop["AZ_early"] + count_pop["TX_early"] + count_pop[ "AZ_late"] + count_pop["TX_late"] with open(fl, 'r+') as f: lns = f.readlines() lns.insert(0, '30164 48394 29292\n') lns.insert(0, 'simuPOP_export %d %d\n' % (n, len(sg))) f.seek(0) # readlines consumes the iterator, so we need to start over f.writelines(lns)