Exemplo n.º 1
0
def main():

    # First, grab and interpret command line arguments
    parser = argparse.ArgumentParser(
        description="Command line arguments for Simulate")
    parser.add_argument("-v",
                        "--verbose",
                        help="Triggers verbose mode",
                        action="store_true")
    parser.add_argument("-d",
                        "--distribution",
                        default="normal",
                        choices=["normal", "gamma"],
                        help="Distribution option")
    parser.add_argument(
        "-p1",
        "--parameter1",
        default=3.0,
        type=float,
        help="Shape parameter (only used if distribution choice is gamma)")
    parser.add_argument(
        "-p2",
        "--parameter2",
        default=1.5,
        type=float,
        help="Scale parameter (only used if distribution choice is gamma)")
    parser.add_argument("-s",
                        "--size",
                        required=True,
                        type=int,
                        nargs="+",
                        help="Specify population size(s)")
    parser.add_argument("-l",
                        "--loci",
                        required=True,
                        type=int,
                        nargs="+",
                        help="Loci with effects")
    parser.add_argument("-n",
                        "--number",
                        required=True,
                        default=3000,
                        type=int,
                        help="Number of loci per population or sub-population")
    parser.add_argument("-e",
                        "--effect",
                        required=True,
                        type=float,
                        nargs="+",
                        help="Effect size(s) for the loci specified")
    parser.add_argument("-i",
                        "--heritability",
                        default=0.2,
                        type=restricted_float,
                        help="Heritability coefficient for population")
    parser.add_argument("-m",
                        "--mean",
                        default=2.0,
                        type=float,
                        nargs="+",
                        help="Mean(s) for population phenotype(s)")
    parser.add_argument("-g",
                        "--gen",
                        default=5,
                        type=int,
                        help="Number of generations for population to evolve")
    parser.add_argument("-r",
                        "--rrate",
                        default=0.0,
                        type=restricted_float,
                        help="Recombination rate for given population")
    parser.add_argument("-f",
                        "--filename",
                        default="my",
                        type=str,
                        help="Prefix for output file set")
    args = parser.parse_args()

    verbose = args.verbose
    if verbose:
        print "Verbose mode"
    distribution = args.distribution
    if verbose:
        print "Simulation will occur with " + distribution + " distribution"
    parameter1 = args.parameter1
    if verbose and distribution == "gamma":
        print "Gamma distrbution will occur with alpha parameter:", parameter1
    parameter2 = args.parameter2
    if verbose and distribution == "gamma":
        print "Gamma distribution will occur with beta parameter", parameter2
    individuals = args.size
    if verbose:
        print "Population size(s) set at", individuals
    loci = args.loci
    if verbose:
        print "Loci positions per individual set as", loci
    number = args.number
    if verbose:
        print "Number of loci per population set as", number
    global effects
    effects = args.effect
    if verbose:
        print "Effects for loci per individual are", effects
    heritability = args.heritability
    mean = args.mean
    if len(mean) == 1 and len(individuals) > 1:
        mean = numpy.array(mean)
        mean = numpy.repeat(mean, len(individuals), axis=0)
        mean = list(mean)
    if verbose:
        print "Population mean(s) set as", mean
    gen = args.gen
    if verbose:
        print "Number of generations to evolve set as", gen
    rrate = args.rrate
    if verbose:
        print "Recombination rate set as", rrate
    filename = args.filename
    if verbose:
        "File will be saved as", filename

    ## Start quantitative trait simulation via simuPOP
    if verbose:
        print "Creating population..."
    pop = sim.Population(size=individuals,
                         loci=int(number),
                         infoFields=["qtrait"])
    if verbose:
        print "Evolving population..."
    type(gen)
    pop.evolve(
        initOps=[sim.InitSex(),
                 sim.InitGenotype(prop=[0.7, 0.3])],
        matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=rrate)),
        postOps=[
            sim.PyQuanTrait(loci=loci,
                            func=additive_model,
                            infoFields=["qtrait"])
        ],
        gen=gen)

    if verbose:
        print "Coalescent process complete. Population evolved with", pop.numSubPop(
        ), "sub-populations."

    genotypes = list()
    for i in pop.individuals():
        genotypes.append(i.genotype())

    phenotypes = list()
    for i in pop.individuals():
        phenotypes.append(i.qtrait)

    # fun() obtains the heritability equation set to zero for various settings of sigma (standard deviation)
    def fun(sigma, h):
        x_exact = list()
        count = 0
        for i in phenotypes:
            current_mean = mean[pop.subPopIndPair(count)[0]]
            x_exact.append(current_mean + i)
            count += 1
        x_random = list()
        count = 0
        for each in phenotypes:
            current_mean = mean[pop.subPopIndPair(count)[0]]
            x_random.append(random.normalvariate(current_mean + each, sigma))
            count += 1
        r = pearsonr(x_exact, x_random)[0]
        return r - math.sqrt(h)

    if verbose:
        print "Building polynomial model for variance tuning..."

    # Polyfit fits a polynomial model in numpy to the values obtained from the fun() function
    points = list()
    for i in drange(0, max(effects) * 10, 0.001):
        points.append(i)
    y_points = list()
    for i in points:
        y_points.append(fun(i, heritability))
    z = numpy.polyfit(x=points, y=y_points, deg=3)
    p = numpy.poly1d(z)

    # Netwon's method finds the polynomial model's roots
    def newton(p):
        xn = 100
        p_d = p.deriv()
        count = 0
        while abs(p(xn)) > 0.01:
            if count > 1000:
                print "Unable to converge after 1000 iterations...\nPlease choose different settings."
                sys.exit()
            count += 1
            xn = xn - p(xn) / p_d(xn)
        if xn < 0.0:
            xn = 0.0
        if verbose:
            print "Estimated variance of phenotypes for specified heriability: ", xn
        return xn

    if verbose:
        print "Using Newton's method to find polynomial roots..."

    # Files are saved to the specified location
    estimated_variance = newton(p)
    new_phenotypes = list()
    count = 0
    for each in phenotypes:
        current_mean = mean[pop.subPopIndPair(count)[0]]
        if distribution == "normal":
            new_phenotypes.append(
                random.normalvariate(current_mean + each, estimated_variance))
        elif distribution == "gamma":
            new_phenotypes.append(
                random.gammavariate(
                    (current_mean + each) / parameter2,
                    numpy.sqrt(estimated_variance / parameter1)))
        count += 1

    f = open(filename + "_qtrait.txt", "w")
    f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
    f.close()

    numpy.savetxt(filename + "_kt_ote.txt",
                  numpy.column_stack((loci, numpy.array(effects))),
                  fmt='%i %10.7f')
    saveCSV(pop, filename + "_genomes.csv")

    # Call the convert.R script to convert the output into usable PLINK files
    # Will probably need to change this line to something more generalizable in the near future

    os.system("Rscript convert.R " + filename)
    print "\n\n"
Exemplo n.º 2
0
def main():

	## Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhs:n:l:e:f:i:m:", longopts=["verbose", "help", "size=",
		                                     "number=", "loci=", "effect=", "mean=", "filename=", "heritability="])

	except getopt.GetoptError as err:
		print(err)
		usage()
		sys.exit()

	verbose = False
	filename = "my"
	heritability = 0.2
	mean = 2.0
	print "\n"

	for o in opts:
		if o[0] in ("-v", "--verbose"):
			verbose = True
			print ("Verbose mode")
	for o in opts:
		if o[0] in ("-h", "--help"):
			usage()
			sys.exit()
		elif o[0] in ("-s", "--size"):
			individuals = o[1]
			if verbose:
				print "Population size is set at", individuals
		elif o[0] in ("-n", "--number"):
			number = o[1]
			if verbose:
				print "Number of loci per individual is set at", number
		elif o[0] in ("-l", "--loci"):
			global loci
			loci = o[1].split(",")
			loci = map(int, loci)
			if verbose:
				print "Loci positions per individual are:", loci
		elif o[0] in ("-e", "--effect"):
			global effects
			effects = o[1].split(",")
			effects = map(float, effects)
			if verbose:
				print "Effects for loci per individual are:", effects
		elif o[0] in ("-f", "--filename"):
			filename = o[1]
			if verbose:
				print "File will be saved as:", filename
		elif o[0] in ("-i", "--heritability"):
			heritability = float(o[1])
			if verbose:
				print "Heritability for simulation specified as:", heritability
		elif o[0] in ("-m", "--mean"):
			mean = float(o[1])
			if verbose:
				print "Population mean specified as:", mean


	## Start quantitative trait simulation
	if verbose:
		print "Creating population..."

	pop = sim.Population(size=int(individuals), loci=int(number), infoFields=["qtrait"])

	if verbose:
		print "Evolving population..."

	pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], matingScheme=sim.RandomMating(),
	           postOps=[sim.PyQuanTrait(loci=loci, func=additive_model, infoFields=["qtrait"])],
	           gen=5)

	if verbose:
		print "Coalescent process complete. Population evolved."

	genotypes = list()
	for i in pop.individuals():
		genotypes.append(i.genotype())
		#print i.genotype()

	phenotypes = list()
	for i in pop.individuals():
		phenotypes.append(i.qtrait)
		#print i.qtrait

	def fun(sigma, h):
		x_exact = phenotypes
		x_random = list()
		for each in phenotypes:
			x_random.append(random.normalvariate(each, sigma))
		r = pearsonr(x_exact, x_random)[0]
		return r - math.sqrt(h)

	#print fun(2.25, 0.25)

	if verbose:
		print "Building polynomial model for variance tuning..."

	points = list()
	for i in drange(0, max(effects)*10, 0.001):
		points.append(i)
	y_points = list()
	for i in points:
		y_points.append(fun(i, heritability))
	z = numpy.polyfit(x=points, y=y_points, deg=3)
	p = numpy.poly1d(z)

	def newton(p):
		xn = 100
		p_d = p.deriv()
		count = 0
		while abs(p(xn)) > 0.01:
			if count > 1000:
				print "Unable to converge after 1000 iterations...\nPlease choose different settings."
				usage()
				sys.exit()
			count += 1
			xn = xn - p(xn)/p_d(xn)
		if verbose:
			print "Estimated variance of phenotypes for specified heriability: ", xn
		return xn

	if verbose:
		print "Using Newton's method to find polynomial roots..."

	estimated_variance = newton(p)
	new_phenotypes = list()
	for each in phenotypes:
		new_phenotypes.append(random.normalvariate(mean + each, estimated_variance))

	f = open(filename + "_qtrait.txt", "w")
	f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
	f.close()

	saveCSV(pop, filename + "_genomes.csv")
	print "\n\n"
Exemplo n.º 3
0
# 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(10000, loci=1)
simu = sim.Simulator(pop, rep=3)
simu.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5])
    ],
    matingScheme=sim.RandomMating(ops=[
        sim.MendelianGenoTransmitter(),
        sim.MapSelector(loci=0, fitness={(0,0):1, (0,1):0.98, (1,1):0.97}),
    ]),
    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
pop = sim.Population(size=10000, loci=2, infoFields='fitness')
a, b, c, d = 1, 1.5, 2.5, 4
r = 0.02
pop.evolve(initOps=[
    sim.InitSex(),
    sim.InitGenotype(freq=(0.5, 0.5)),
    sim.PyOutput('LD,   AB,  Ab,  aB,  ab,  avg fitness\n'),
],
           preOps=[
               sim.MaSelector(loci=[0, 1],
                              wildtype=0,
                              fitness=[a, b, a, c, d, c, a, b, a]),
               sim.Stat(meanOfInfo='fitness'),
           ],
           matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=r)),
           postOps=[
               sim.Stat(haploFreq=[0, 1], LD=[0, 1], step=20),
               sim.PyEval(
                   r"'%.3f %.2f %.2f %.2f %.2f %.2f\n' % (LD[0][1], "
                   "haploFreq[(0,1)][(0,0)], haploFreq[(0,1)][(0,1)],"
                   "haploFreq[(0,1)][(1,0)], haploFreq[(0,1)][(1,1)],"
                   "meanOfInfo['fitness'])",
                   step=20),
           ],
           gen=100)
Exemplo n.º 5
0
                       selModel='multiplicative',
                       selDist='constant',
                       selCoef=None,
                       popFile='example.pop')
# load population
# print('Loading population example.pop')
# pop = sim.loadPopulation('example.pop')

# evolve the population for a few more generations to produce pedigrees
print('Evolving the population for three generations.')
pop.addInfoFields(['ind_id', 'father_id', 'mother_id'])
sim.tagID(pop)
# save all ancestral generations during evolution
pop.setAncestralDepth(-1)
pop.evolve(matingScheme=sim.RandomMating(
    numOffspring=(sim.UNIFORM_DISTRIBUTION, 2, 4),
    ops=(sim.MendelianGenoTransmitter(), sim.IdTagger(),
         sim.PedigreeTagger())),
           gen=3)
# what is the average number of mutants in this population?
avgMutants = (pop.popSize() * pop.totNumLoci() * 2. -
              pop.genotype().count(0)) / pop.popSize()
print(('Average number of mutants is %.2f' % avgMutants))
#
# This contains marker information for the initial population
print('Mutant locations are saved to file sample.map')
markers = saveMarkerInfoToFile(pop, 'pedigree.map')


#
def myPenet(geno):
    # count the number of mutants
                deepcopy(pop.vars(sp)[var[:-3]]))
    else:
        pop.vars()[accumulator].append(deepcopy(pop.vars()[var]))
    return True

init_ops['accumulators'] = sp.PyOperator(init_accumulators,
       param=['fst'])
init_ops['Sex'] = sp.InitSex()
init_ops['Freq'] = sp.InitGenotype(freq=[0.5, 0.5])
for i, mig in enumerate(migs):
   post_ops['mig-%d' % i] = sp.Migrator(demography.migrIslandRates(mig, num_pops),
   reps=[i])
post_ops['Stat-fst'] = sp.Stat(structure=sp.ALL_AVAIL)
post_ops['fst_accumulation'] = sp.PyOperator(update_accumulator, param=('fst', 'F_st'))

mating_scheme =  sp.RandomMating()

sim = sp.Simulator(pops, rep=len(migs))
sim.evolve(initOps=list(init_ops.values()),
           preOps=list(pre_ops.values()),
           postOps=list(post_ops.values()),
           matingScheme=mating_scheme,
           gen=num_gens)

import seaborn as sns
sns.set_style('white')
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(16, 9))
ax = fig.add_subplot(111)
for i, pop in enumerate(sim.populations()):
   ax.plot(pop.dvars().fst, label='mig rate %.4f' % migs[i])
Exemplo n.º 7
0
        infoFields=['ind_id','fitness','migrate_to'])


pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.IdTagger(),
    ]+init_geno,
    preOps=[
        sim.SNPMutator(u=args.sel_mut_rate, v=args.sel_mut_rate),
        sim.PyMlSelector(FixedFitness(args.selection_coef),
            output=">>"+args.selloci_file),
    ],
    matingScheme=sim.RandomMating(
        ops=[
            sim.IdTagger(),
            sim.Recombinator(intensity=args.recomb_rate,
                infoFields="ind_id"),
        ] ),
    postOps=[
        sim.Stat(numOfSegSites=sim.ALL_AVAIL, step=10),
        sim.PyEval(r"'Gen: %2d #seg sites: %d\n' % (gen, numOfSegSites)", step=50)
    ],
    gen = args.generations
)

logfile.write("Done simulating!\n")
logfile.write(time.strftime('%X %x %Z')+"\n")
logfile.write("----------\n")
logfile.flush()

Exemplo n.º 8
0
def evolvePop(model,
              N0,
              N1,
              G0,
              G1,
              initSpec,
              mu,
              k,
              fitness,
              m=1,
              migrRate=0,
              logfile='',
              sp_logfile='',
              **kwargs):
    '''Evolve a population with specified allele frequencies (parameter
    initSpec) using given demographic (model, N0, N1, G0, G1, m), mutation
    (a k-allele model with parameters mu and k) and natural selection models
    (a multi-locus selection model with fitness vector s). Total disease
    allele frequency and effective number of alleles in the population
    and in all subpopulations are recorded if names of log files are provided.
    This function returns a tuple of these two statistics at the end of the
    evolution. Additional keyword arguments could be used to control when and
    how often statisitcs are outputed.
    '''
    L = len(fitness) // 3
    if not hasattr(mu, '__iter__'):  # if a single mutation rate is given
        mu = [mu] * L
    # Create expressions to output f_e and ne at all loci, which are
    #   "%d\t%.4f\t%.4f\n" % (gen, 1-alleleFreq[x][0], ne[x])
    # for locus x.
    statExpr = '"%d' + r'\t%.4f\t%.4f'*L + r'\n" % (gen,' + \
        ', '.join(['1-alleleFreq[%d][0], ne[%d]' % (x, x) for x in range(L)]) + ')'
    demo_func = demoModel(model, N0, N1, G0, G1, m)
    pop = sim.Population(size=demo_func(0),
                         loci=[1] * L,
                         infoFields=['fitness', 'migrate_to'])
    pop.evolve(
        initOps=[sim.InitSex(), sim.InitGenotype(freq=initSpec)],
        preOps=[
            sim.KAlleleMutator(k=k, rates=mu, loci=range(L)),
            sim.MlSelector([
                sim.MaSelector(loci=i, fitness=fitness[3 * i:3 * (i + 1)])
                for i in range(L)
            ],
                           mode=sim.MULTIPLICATIVE),
            sim.Migrator(rate=migrIslandRates(migrRate, m), begin=G0 + 1),
        ],
        matingScheme=sim.RandomMating(subPopSize=demo_func),
        postOps=[
            sim.IfElse(
                logfile != '' or sp_logfile != '',
                Ne(loci=sim.ALL_AVAIL,
                   vars=['ne'] if m == 1 else ['ne', 'ne_sp']), **kwargs),
            sim.IfElse(logfile != '',
                       sim.PyEval(statExpr, output='>>' + logfile), **kwargs),
            sim.IfElse(
                m > 1 and sp_logfile != '',
                sim.PyEval(
                    statExpr,
                    output='>>' + sp_logfile,
                    # subPops=sim.ALL_AVAIL will evalulate the expression in each
                    # subpopulation's local namespace (vars(sp)).
                    subPops=sim.ALL_AVAIL,
                    begin=G0),
                **kwargs),
        ],
        finalOps=Ne(loci=sim.ALL_AVAIL),
        gen=G0 + G1)
    return tuple([1-pop.dvars().alleleFreq[x][0] for x in range(L)] + \
        [pop.dvars().ne[x] for x in range(L)])
Exemplo n.º 9
0
		preOps=[
	#Fisson of population into 16 at 'atgen' generation
		sim.SplitSubPops(proportions=[0.0625]*16, at=atgen),
	#Migration using a simple island  model
		sim.Migrator(rate=migrIslandRates(m,16),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
		ops=[sim.Recombinator(rates=0.002)]
		),
		postOps=[
	#Mutation rate 10e-6
		sim.SNPMutator(u=0.000001,v=0.000001)
		],
	#Evolve for a number 'numgen' of generations
		gen = numgen
	)
	#Getting population informations (number of subpopulations, population size)
	sim.stat(pop,popSize=True)
	subsize=pop.dvars().subPopSize
	numpop=len(subsize)
	#Setting environmental value for all individuals in each subpopulation
	for i in range(numpop):
Exemplo n.º 10
0
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
Exemplo n.º 11
0
    def replicate_random_mating(self, multi_pop, meta_sample_library, qtl,
                                allele_effects, recombination_rates):
        """
        Runs recurrent truncation selection on a multi-replicate population.

        :param multi_pop: Simulator object of full-sized population
        :param meta_sample_library: Simulator object of meta-populations
        :param qtl: Loci whose alleles have effects
        :param allele_effects: Allele effect container
        :param recombination_rates: Probabilities for recombination at each locus
        """

        for pop_rep in multi_pop.populations():
            pop_rep.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_random_mating, 2)
        ]
        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),
            ],
            matingScheme=sim.RandomMating(ops=[
                sim.IdTagger(),
                sim.PedigreeTagger(),
                sim.Recombinator(rates=recombination_rates)
            ]),
            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_random_mating)
Exemplo n.º 12
0
# 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(size=10000, loci=2)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(genotype=[1, 2, 2, 1])
    ],
    matingScheme = sim.RandomMating(ops=[
        sim.MendelianGenoTransmitter(end=29),
        sim.Recombinator(rates=0.01, begin=30),
    ]),
    postOps=[
        sim.Stat(LD=[0, 1]),
        sim.PyEval(r"'gen %d, LD: %.2f\n' % (gen, LD[0][1])", step=20)
    ],
    gen=100
)

Exemplo n.º 13
0
# description of this example.
#

import simuPOP as sim
pop = sim.Population(size=500, loci=1)
pop.setVirtualSplitter(sim.ProductSplitter([
    sim.AffectionSplitter(),
    sim.RangeSplitter([[0,500], [500, 1000]]),
    ])
)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5]),
    ],
    matingScheme=sim.RandomMating(
        ops=[
            sim.MendelianGenoTransmitter(),
            sim.MaPenetrance(loci=0, penetrance=[0, 0.01, 0.1]),
            sim.DiscardIf(True, subPops=[
                (0, 'Unaffected, Range [0, 500)'),
                (0, 'Affected, Range [500, 1000)')])
        ],
        subPopSize=1000,
    ),
    gen = 1
)
sim.stat(pop, numOfAffected=True)
print(pop.dvars().numOfAffected, pop.dvars().numOfUnaffected)

Exemplo n.º 14
0
# record recombinations
rc = CheckpointedRecombCollector(first_gen=pop.indInfo("ind_id"),
                                 ancestor_age=args.ancestor_age,
                                 length=args.length,
                                 locus_position=[0, args.length],
                                 level=args.level)

pop.evolve(initOps=[
    sim.InitSex(),
],
           preOps=[
               sim.PyOperator(lambda pop: rc.increment_time() or True),
           ],
           matingScheme=sim.RandomMating(ops=[
               id_tagger,
               sim.Recombinator(intensity=args.recomb_rate,
                                output=rc.collect_recombs,
                                infoFields="ind_id"),
           ]),
           postOps=[sim.PyEval(r"'Gen: %2d\n' % gen ", step=50)],
           gen=args.generations)

logfile.write("Done simulating!\n")
logfile.write(time.strftime('%X %x %Z') + "\n")
logfile.write("----------\n")
logfile.flush()

locations = [pop.subPopIndPair(x)[0] for x in range(pop.popSize())]
rc.add_diploid_samples(nsamples=args.nsamples,
                       sample_ids=pop.indInfo("ind_id"),
                       populations=locations)
Exemplo n.º 15
0
    return True


# describe this evolutionary process
print(
    sim.describeEvolProcess(initOps=[
        sim.InitSex(),
        sim.InitInfo(lambda: random.randint(0, 75), infoFields='age'),
        sim.InitGenotype(freq=[0.5, 0.5]),
        sim.IdTagger(),
        sim.PyOutput('Prevalence of disease in each age group:\n'),
    ],
                            preOps=sim.InfoExec('age += 1'),
                            matingScheme=sim.HeteroMating([
                                sim.CloneMating(subPops=[(0, 0), (0, 1),
                                                         (0, 2)],
                                                weight=-1),
                                sim.RandomMating(ops=[
                                    sim.IdTagger(),
                                    sim.Recombinator(intensity=1e-4)
                                ],
                                                 subPops=[(0, 1)]),
                            ]),
                            postOps=[
                                sim.MaPenetrance(loci=0,
                                                 penetrance=[0.01, 0.1, 0.3]),
                                sim.PyOperator(func=outputstat)
                            ],
                            gen=100,
                            numRep=3))
Exemplo n.º 16
0
import simuPOP as sim
from simuPOP.plotter import VarPlotter
def demo(gen):
    # split subpopulation 0 at generation 10 and 20
    if gen >=40 and gen < 50:
        return 20
    else:
        return 1000

pop = sim.Population(size=1000, loci=1)
simu = sim.Simulator(pop, rep=5)
simu.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5])
    ],
    matingScheme=sim.RandomMating(subPopSize=demo),
    postOps=[
        sim.Stat(alleleFreq=0),
        VarPlotter('alleleFreq[0][0]', update=10,
            saveAs='Figures/bottleneck.pdf',
            plot_ylim=[0,1], plot_ylab='Allele Frequency',
            lines_col='black', lines_lwd=1.5)
    ],
    gen=101
)
Exemplo n.º 17
0
        sim.InfoExec(
            "AccumAges[ind.sex()][ind.allele(0,0) + ind.allele(0,1)][int(ind.age)] += 1",
            usePopVars=True,
            exposeInd='ind'),
        sim.PySelector(loci=[0], func=fitness_func)
    ],
    matingScheme=sim.HeteroMating([
        sim.CloneMating(subPops=[(0, 0), (0, 1), (0, 2)], weight=-1),
        sim.RandomMating(ops=[
            sim.IdTagger(),
            sim.InfoExec("smurf = 0.0"),
            sim.InfoExec("birthday = gen"),
            sim.MendelianGenoTransmitter(),
            sim.PyQuanTrait(loci=sim.ALL_AVAIL,
                            func=MaleEffect,
                            infoFields=['a', 'b', 't0']),
            sim.PedigreeTagger(output='>>{}.ped'.format(args.output),
                               outputFields=['a', 'birthday'],
                               outputLoci=[0])
        ],
                         weight=1,
                         subPops=[(0, 1)],
                         numOffspring=1)
    ],
                                  subPopSize=demo),
    gen=args.G)

fh.close()

pop = simu.extract(0)
AccumAges = pop.dvars().AccumAges
Exemplo n.º 18
0
        # change population size at 50 and 60
        G=[50, 60],
        # change to population size 200 and back to 1000
        NG=[(200, 'bottleneck'), (1000, 'Post-Bottleneck')]),
    ExponentialGrowthModel(
        T=50,
        # split the population into two subpopulations
        N0=[(400, 'P1'), (600, 'P2')],
        # expand to size 4000 and 5000 respectively
        NT=[4000, 5000])
])
#
# model.init_size returns the initial population size
# migrate_to is required for migration
pop = sim.Population(size=model.init_size,
                     loci=1,
                     infoFields=model.info_fields)
pop.evolve(initOps=[sim.InitSex(),
                    sim.InitGenotype(freq=[0.5, 0.5])],
           matingScheme=sim.RandomMating(subPopSize=model),
           finalOps=sim.Stat(alleleFreq=0, vars=['alleleFreq_sp']),
           gen=model.num_gens)
# print out population size and frequency
for idx, name in enumerate(pop.subPopNames()):
    print('%s (%d): %.4f' %
          (name, pop.subPopSize(name), pop.dvars(idx).alleleFreq[0][0]))

# get a visual presentation of the demographic model
model.plot('log/demoModel.png',
           title='A bottleneck + exponential growth demographic model')
Exemplo n.º 19
0
   preOps = [
      sim.InfoExec("luck = random.random()"),
      sim.InfoExec("smurf = 1.0 if (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),
      sim.InfoExec("age += 1"),
      sim.PySelector(loci=[0], func=fitness_func)
   ],
   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"),
               sim.MendelianGenoTransmitter(),
               sim.PyQuanTrait(loci = sim.ALL_AVAIL, func = MaleEffect, infoFields = ['a', 'b', 't0']) 
            ],
            weight = 1,
            subPops = [(0,1)],
            numOffspring = 1
         )
      ],
      subPopSize = demo
   ),
   postOps = [
      sim.PyOperator(func=OutputStats, step=100)
   ],
   gen=args.G
)

pop = simu.extract(0)
Exemplo n.º 20
0
        sim.IdTagger()
    ],
    # The order should be: becoming a smurf or not since previous day, dying or not, aging one day
    # if lucky enough, and then mate at that age.
    preOps=[
        sim.InfoExec("luck = random.random()"),
        sim.InfoExec(
            "smurf = 1.0 if ((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),
        sim.InfoExec("age += 1")
    ],
    matingScheme=sim.HeteroMating([
        sim.CloneMating(subPops=[(0, 0), (0, 1), (0, 2)], weight=-1),
        sim.RandomMating(ops=[
            sim.IdTagger(),
            sim.PedigreeTagger(),
            sim.PyQuanTrait(loci=[0], func=qtrait, infoFields=['a', 'b']),
            sim.InfoExec("smurf = 0.0"),
            sim.MendelianGenoTransmitter()
        ],
                         weight=1,
                         subPops=[(0, 1)],
                         numOffspring=(sim.UNIFORM_DISTRIBUTION, 10, 50))
    ],
                                  subPopSize=demo),
    gen=args.G)

pop = simu.extract(0)
outputStructure(pop)
Exemplo n.º 21
0
from simuPOP.sampling import drawCaseControlSample


def assoTest(pop):
    'Draw case-control sample and apply association tests'
    sample = drawCaseControlSample(pop, cases=500, controls=500)
    sim.stat(sample,
             association=(0, 2),
             vars=['Allele_ChiSq_p', 'Geno_ChiSq_p', 'Armitage_p'])
    print('Allele test: %.2e, %.2e, Geno test: %.2e, %.2e, Trend test: %.2e, %.2e' \
        % (sample.dvars().Allele_ChiSq_p[0], sample.dvars().Allele_ChiSq_p[2],
        sample.dvars().Geno_ChiSq_p[0], sample.dvars().Geno_ChiSq_p[2],
        sample.dvars().Armitage_p[0], sample.dvars().Armitage_p[2]))
    return True


pop = sim.Population(size=100000, loci=3)
pop.setVirtualSplitter(sim.ProportionSplitter([0.5, 0.5]))
pop.evolve(initOps=[
    sim.InitSex(),
    sim.InitGenotype(genotype=[0] * 3, subPops=[(0, 0)]),
    sim.InitGenotype(genotype=[1] * 3, subPops=[(0, 1)]),
],
           matingScheme=sim.RandomMating(
               ops=sim.Recombinator(loci=[0, 1], rates=[0.01, 0.005])),
           postOps=[
               sim.MaPenetrance(loci=1, penetrance=[0.1, 0.2, 0.4]),
               sim.PyOperator(func=assoTest, step=20),
           ],
           gen=100)
Exemplo n.º 22
0
#
# 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(100, loci=1)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5])
    ],
    matingScheme=sim.RandomMating(),
    postOps=[
        sim.Stat(heteroFreq=0, step=10),
        sim.PyEval(r"'Gen: %d, HeteroFreq: %.2f\n' % (gen, heteroFreq[0])", step=20)
    ],
    gen = 100
)

Exemplo n.º 23
0
    initOps = [
        # individuals are randomly assigned as male or female
        sim.InitSex(),
        # each locus has 19 alleles with equal frequencies
        sim.InitGenotype(freq = [1/19]*19)
        ],
    # before mating rank individuals by their trait
    #preOps = sim.PyOperator(func = pickParents, param = args.selection_type),
    preOps = sim.PyOperator(func = assignFitness),
    # Random mating with fixed number of offspring
    # https://bopeng.github.io/simuPOP/userGuide_ch6_sec1.html#determine-the-number-of-offspring-during-mating
    matingScheme = sim.RandomMating(
        ops = [
            # average recombination across all chromosomes
            sim.Recombinator((1*len(pop.numLoci()))/sum(pop.numLoci())), # avg of 2 recombinations == poisson with rate 2
            sim.PedigreeTagger(),
            sim.IdTagger()
            ],
        # each cross results in 10 offspring
        numOffspring = 10
        ),
    # after mating assign quantitative trait based on genotype
    postOps = [
        sim.PyQuanTrait(
            func = qtrait,
            loci = adv_loci,
            infoFields = ['trait', 'nalleles'])
        ],
    # 11 generations (the first generation are the founders)
    gen = 11
)
Exemplo n.º 24
0
# define virtual subpopulations by affection sim.status
pop.setVirtualSplitter(sim.AffectionSplitter())
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5]),
    ],
    preOps=[
        # get affection sim.status for parents
        sim.MaPenetrance(loci=0, wildtype=0, penetrance=[0.1, 0.2, 0.4]),
        # set 'aff' of parents
        sim.InfoExec('aff = ind.affected()', exposeInd='ind'),
    ],
    # get number of affected parents for each offspring and store in numOfAff
    matingScheme=sim.RandomMating(ops=[
        sim.MendelianGenoTransmitter(),
        sim.SummaryTagger(mode=sim.SUMMATION, infoFields=['aff', 'numOfAff'])
    ]),
    postOps=[
        # get affection sim.status for offspring
        sim.MaPenetrance(loci=0, wildtype=0, penetrance=[0.1, 0.2, 0.4]),
        # calculate mean 'numOfAff' of offspring, for unaffected and affected subpopulations.
        sim.Stat(meanOfInfo='numOfAff',
                 subPops=[(0, 0), (0, 1)],
                 vars=['meanOfInfo_sp']),
        # print mean number of affected parents for unaffected and affected offspring.
        sim.PyEval(
            r"'Mean number of affected parents: %.2f (unaff), %.2f (aff)\n' % "
            "(subPop[(0,0)]['meanOfInfo']['numOfAff'], subPop[(0,1)]['meanOfInfo']['numOfAff'])"
        )
    ],
    gen=5)
Exemplo n.º 25
0
# 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(1000, loci=[10]), rep=3)
simu.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5])
    ],
    matingScheme=sim.ConditionalMating('rep == 0', 
        # the first replicate use standard random mating
        sim.RandomMating(),
        sim.ConditionalMating('rep == 1 and gen >= 5',
            # the second replicate produces more males for the first 5 generations
            sim.RandomMating(),
            # the last replicate produces more males all the time
            sim.RandomMating(sexMode=(sim.PROB_OF_MALES, 0.7))
            )
        ),
    postOps=[
        sim.Stat(numOfMales=True),
        sim.PyEval("'gen=%d' % gen", reps=0),
        sim.PyEval(r"'\t%d' % numOfMales"),
        sim.PyOutput('\n', reps=-1)
    ],        
    gen=10
)