fitness = PlusMinusFitness(args.selection_coef) pop = sim.Population(size=[args.popsize] * npops, loci=[args.nloci, 2], lociPos=locus_position + [0, args.length], infoFields=['ind_id', 'fitness', 'migrate_to']) id_tagger = sim.IdTagger() id_tagger.apply(pop) # record recombinations rc = RecombCollector(first_gen=pop.indInfo("ind_id"), ancestor_age=args.ancestor_age, length=2 * args.length, locus_position=locus_position + [args.length, 2 * args.length]) migr_mat = [[0, args.m, 0], [args.m, 0, args.M], [0, args.M, 0]] pop.evolve(initOps=[ sim.InitSex(), ] + init_geno, preOps=[ sim.PyOperator(lambda pop: rc.increment_time() or True), sim.Migrator(rate=migr_mat, mode=sim.BY_PROBABILITY), sim.SNPMutator(u=args.sel_mut_rate, v=args.sel_mut_rate), sim.PyMlSelector(fitness.left, subPops=fitness.left_subpops, output=">>" + selloci_file),
size=[args.popsize]*npops, loci=[args.nloci], lociPos=locus_position, infoFields=['ind_id','fitness','migrate_to']) id_tagger = sim.IdTagger() id_tagger.apply(pop) # set up recomb collector first_gen = pop.indInfo("ind_id") init_ts = msprime.simulate(2*len(first_gen), length=max(locus_position)) haploid_labels = [(k,p) for k in first_gen for p in (0,1)] node_ids = {x:j for x, j in zip(haploid_labels, init_ts.samples())} rc = RecombCollector(ts=init_ts, node_ids=node_ids, locus_position=locus_position) if min(args.gridheight,args.gridwidth)==1: migr_rates=migrSteppingStoneRates( args.migr, n=max(args.gridwidth,args.gridheight), circular=False) else: migr_rates=migr2DSteppingStoneRates( args.migr, m=args.gridwidth, n=args.gridheight, diagonal=False, circular=False) pop.evolve( initOps=[ sim.InitSex(), ]+init_geno, preOps=[ sim.PyOperator(lambda pop: rc.increment_time() or True), sim.Migrator(
id_tagger = sim.IdTagger() id_tagger.apply(pop) # set up recomb collector # NB: we have to simulate an initial tree sequence first_gen = pop.indInfo("ind_id") init_ts = msprime.simulate(2 * len(first_gen), Ne=args.popsize, recombination_rate=args.recomb_rate / 2.0, length=max(locus_position)) haploid_labels = [(k, p) for k in first_gen for p in (0, 1)] node_ids = {x: j for x, j in zip(haploid_labels, init_ts.samples())} rc = RecombCollector(ts=init_ts, node_ids=node_ids, locus_position=locus_position, benchmark=True, mode='binary') pre_ops = [sim.PyOperator(lambda pop: rc.increment_time() or True)] mating_ops = [id_tagger] logfile.write(time.strftime('%X %x %Z') + "\n") logfile.write("Started simulating! Generations:\n") post_ops = [sim.PyEval(r'" %d\n" % (gen)', step=100, output=logfile)] nselloci = int(args.sel_mut_rate / (args.mut_rate + args.sel_mut_rate)) selected_loci = random.sample(locus_position, nselloci) neutral_loci = list(set(locus_position) - set(selected_loci)) if args.record_neutral: pre_ops += [ sim.SNPMutator(u=args.mut_rate, v=args.mut_rate, loci=neutral_loci)
# initially polymorphic alleles init_freqs=[[k/100,1-k/100,0,0] for k in range(1,11)] locus_classes=[min(len(init_freqs)-1,math.floor(random.expovariate(1))) for k in range(nloci)] init_classes=[list(filter(lambda k: locus_classes[k]==x,range(nloci))) for x in range(len(init_freqs))] logfile.write("Locus positions:\n") logfile.write(str(locus_position)+"\n") logfile.write("----------\n") logfile.flush() init_geno=[sim.InitGenotype(freq=init_freqs[k],loci=init_classes[k]) for k in range(len(init_freqs))] # record recombinations rc = RecombCollector( nsamples=nsamples, generations=generations, N=popsize*npops, ancestor_age=ancestor_age, length=length, locus_position=locus_position) id_tagger = sim.IdTagger() id_tagger.apply(pop) # record recombinations rc = RecombCollector( first_gen=pop.indInfo("ind_id"), ancestor_age=args.ancestor_age, length=args.length, locus_position=locus_position) ### # modified from http://simupop.sourceforge.net/manual_svn/build/userGuide_ch5_sec9.html class FixedFitness: def __init__(self,s): self.s = s
min(len(init_freqs) - 1, math.floor(random.expovariate(1))) for k in range(args.nselloci) ] init_classes = [ list(filter(lambda k: locus_classes[k] == x, range(args.nselloci))) for x in range(len(init_freqs)) ] init_geno = [ sim.InitGenotype(freq=init_freqs[k], loci=init_classes[k]) for k in range(len(init_freqs)) ] # record recombinations rc = RecombCollector(nsamples=args.nsamples, generations=args.generations, N=args.popsize, ancestor_age=args.ancestor_age, length=args.nselloci, locus_position=locus_position) ### # random selection coefficients: # modified from http://simupop.sourceforge.net/manual_svn/build/userGuide_ch5_sec9.html class GammaDistributedFitness: def __init__(self, alpha, beta): # mean is alpha/beta self.coefMap = {} self.alpha = alpha self.beta = beta