Esempio n. 1
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"
Esempio n. 2
0
def simulation(me, iterations, generations, cro_to_slo, slo_to_cro, slo_cull, cro_cull):
    x = []  # empty list to store LD Ne calculations
    Ne = [] # empty list to store demographic Ne calculations
    
    for i in range(iterations):
        pop = sim.Population(size = [500, 500], loci=[1]*20,
                                 infoFields = ["age",'ind_id', 'father_idx', 'mother_idx', "mating", "hc",'migrate_to'],
                                 subPopNames = ["croatia", "slovenia"])
        sim.initInfo(pop = pop, values = list(map(int, np.random.negative_binomial(n = 1, p = 0.25, size=500))), infoFields="age")

        pop.setVirtualSplitter(sim.CombinedSplitter([
            sim.ProductSplitter([
                sim.SexSplitter(),
                sim.InfoSplitter(field = "age", cutoff = [1,3,6,15])])],
            vspMap = [[0,1], [2], [3], [4], [5,6,7,8], [9] ]))

        # Age groups: from 0 to 1 - cubs, from 1 to 3 - prereproductive, from 3 to 6 - reproductive class, from 6 to 15 - dominant
        pop.evolve(
            initOps=[
                sim.InitSex(),
                # random genotype
                sim.InitGenotype(freq=[0.01]*2 + [0.03]*2 + [0.23]*4),
                # assign an unique ID to everyone.
                sim.IdTagger(),
            ],
            # increase the age of everyone by 1 before mating.
            preOps=[sim.InfoExec('age += 1'),
                    sim.InfoExec('mating = 0'),
                    sim.PyOperator(func=popmodel.NaturalMortality),
                    sim.InfoExec("hc +=1 if 0 < hc < 3  else 0"), # Mother bear can't have cubs for two years after pregnancy
                    sim.Migrator(rate=[[cro_to_slo]],
                                 mode=sim.BY_PROPORTION,
                                 subPops=[(0, 0)],
                                 toSubPops=[1]), # reproductive males migrate from Cro to Slo
                    sim.Migrator(rate=[[slo_to_cro]],
                                 mode=sim.BY_PROPORTION,
                                 subPops=[(1, 0)],
                                 toSubPops=[0]),
                     sim.Stat(effectiveSize=sim.ALL_AVAIL, subPops=[(0,1),(0,2),(0,4), (1,1), (1,2), (1,4)], vars='Ne_demo_base'),
                     sim.Stat(effectiveSize=sim.ALL_AVAIL,subPops=[(0,1),(0,2),(0,4), (1,1), (1,2), (1,4)], vars='Ne_demo_base_sp')
                    #sim.PyEval(r'"Cro %d, Slo %d' ' % (Cro, Slo)', "Cro = pop.subPopSize(0)" "Slo = pop.subPopSize(1)",exposePop='pop'),
                    ],
            matingScheme=sim.HeteroMating([
                # CloneMating will keep individual sex and all
                # information fields (by default).
                # The age of offspring will be zero.

                sim.HomoMating(subPops=sim.ALL_AVAIL,
                    chooser=sim.CombinedParentsChooser(
                        fatherChooser=sim.PyParentsChooser(generator=bearFather),
                        motherChooser=sim.PyParentsChooser(generator=bearMother)
                    ),
                    generator=sim.OffspringGenerator(ops=[
                        sim.InfoExec("age = 0"),
                        sim.IdTagger(),
                        #sim.PedigreeTagger(),
                        sim.ParentsTagger(),
                        sim.MendelianGenoTransmitter()
                    ], numOffspring=(sim.UNIFORM_DISTRIBUTION, 1, 3))),
                sim.CloneMating(subPops=[(0,0), (0,1), (0,2), (0,4), (1,0), (1,1), (1,2), (1,4)], weight=-1),

            ], subPopSize=popmodel.demoModel),
            postOps = [
                sim.PyOperator(func=popmodel.cullCountry,param={"slo_cull": slo_cull, "cro_cull": cro_cull}),
                sim.PyOperator(func = popmodel.CalcNe, param={"me":me, "Ne":Ne}, begin=int(0.2*generations)),
                sim.PyOperator(func = popmodel.CalcLDNe, param={"me":me, "x":x}, begin=int(0.2*generations))
                       ],

            gen = generations
        )
    x = pd.concat(x)
    Ne = pd.concat(Ne)
    x.loc[x["population"] ==0,"population"] = "cro"
    x.loc[x["population"] ==1,"population"] = "slo"
    x = x[x['cutoff'] == 0]
    x = x.rename(columns={0: "Ne"})
    return x, Ne
Esempio n. 3
0
        if loc in self.coefMap:
            s = self.coefMap[loc]
        else:
            s = random.gammavariate(self.alpha, self.beta)
            self.coefMap[loc] = s / self.two_N
        # print(str(loc)+":"+str(alleles)+"\n")
        # simupop does not call function for alleles=(0,0)
        if 0 in alleles:
            return max(0.0, 1. - s / 2.)
        else:
            return max(0.0, 1. - s)


before_time = time.process_time()

init_geno = [sim.InitGenotype(freq=1.0)]

pop = sim.Population(size=[args.popsize] * npops,
                     loci=[args.nloci],
                     lociPos=locus_position,
                     infoFields=['ind_id', 'fitness'])

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,
Esempio n. 4
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


def demo(pop):
    return int(pop.popSize() * 1.05)


pop = sim.Population(size=10000, loci=1)
pop.evolve(
    initOps=[sim.InitSex(), sim.InitGenotype(freq=[0.7, 0.3])],
    preOps=[
        sim.Stat(popSize=True),
        sim.PyEval(r'"%d %s --> " % (gen, subPopSize)'),
        sim.ResizeSubPops(0, proportions=[0.5], at=2),
        sim.MaPenetrance(loci=0, penetrance=[0.01, 0.2, 0.6], begin=4),
        sim.DiscardIf('ind.affected()', exposeInd='ind', begin=4),
        sim.Stat(popSize=True),
        sim.PyEval(r'"%s --> " % subPopSize'),
    ],
    matingScheme=sim.RandomMating(subPopSize=demo),
    postOps=[sim.Stat(popSize=True),
             sim.PyEval(r'"%s\n" % subPopSize')],
    gen=6)
import simuPOP as sim
from math import log
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)
Esempio n. 6
0
# 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,
                     ploidy=1,
                     loci=[5, 5],
                     ancGen=1,
                     infoFields='parent_idx')
pop.evolve(initOps=sim.InitGenotype(freq=[0.3, 0.7]),
           matingScheme=sim.RandomSelection(ops=[
               sim.ParentsTagger(infoFields='parent_idx'),
               sim.CloneGenoTransmitter(),
           ]),
           gen=5)
ind = pop.individual(0)
par = pop.ancestor(ind.parent_idx, 1)
print(ind.sex(), ind.genotype())
print(par.sex(), par.genotype())
Esempio n. 7
0
import simuPOP as sim
# [100]*100 means a population with 100 subpopulations, each of size 100.
pop = sim.Population([100] * 100, loci=1)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5]),
        sim.PyOutput('gen:   mean freq   mean Ht (expected Ht)\n')
    ],
    preOps=[
        # Statistics in subpopulations are by default not calculated.
        # This is changed by specifying variables with a '_sp' suffix.
        sim.Stat(alleleFreq=0,
                 heteroFreq=0,
                 vars=['alleleFreq_sp', 'heteroFreq_sp'],
                 step=20),
        # Variables in subpopulations are stored in dictionaries
        # subPop[subPopID] where subPopID can be virtual.
        sim.PyEval(
            r'"%2d:    %.4f      %.4f (%.4f)\n" % (gen,'
            'sum([subPop[x]["alleleFreq"][0][1] for x in range(100)])/100.,'
            'sum([subPop[x]["heteroFreq"][0] for x in range(100)])/100.,'
            '0.5*(1-1/200.)**gen)',
            step=20)
    ],
    matingScheme=sim.RandomMating(),
    gen=100)
                    (1 + spacing_fac))
locus_position = [x * pos_fac for x in rel_positions]

# 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))
]

init_geno = [
    sim.InitGenotype(freq=init_freqs[k], loci=init_classes[k])
    for k in range(len(init_freqs))
]

###
# modified from http://simupop.sourceforge.net/manual_svn/build/userGuide_ch5_sec9.html

pop = sim.Population(size=[popsize] * 50,
                     loci=[nloci],
                     lociPos=locus_position,
                     infoFields=['ind_id', 'fitness'])
simu = sim.Simulator(pop)

meioser = ftprime.MeiosisTagger(nsamples, ngens=1 + generations)

Esempio n. 9
0
    return True

model = EventBasedModel(
...     N0=([1000000]*3),
...     events=[
...         ResizeEvent(at=500, sizes=1000),
...     ]
... )

pop = sim.Population(size=model.init_size, loci=[10]*10,
...     infoFields='migrate_to')

pop.evolve(
initOps=[
sim.InitSex(),
sim.InitGenotype(freq=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1])
],
preOps=[
	sim.Migrator(rate=[
		[0,0.000005,0.000005],
		[0.000005,0,0.000005],
		[0.000005,0.000005,0],
	],mode=sim.BY_PROPORTION, end=499),
	sim.Migrator(rate=[
		[0,0.005,0.005],
		[0.005,0,0.005],
		[0.005,0.005,0],
	],mode=sim.BY_PROPORTION, begin=500)
],
matingScheme=sim.RandomMating(subPopSize=model,ops=sim.Recombinator(rates=0.01)),
postOps=[
Esempio n. 10
0
# 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,
                     chromTypes=[
                         sim.AUTOSOME, sim.CHROMOSOME_X, sim.CHROMOSOME_Y,
                         sim.MITOCHONDRIAL
                     ])
pop.setVirtualSplitter(sim.SexSplitter())
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(haplotypes=[[0, 1, 2, 0, 1] * 4, [2, 1, 0, 2, 3] * 4],
                         prop=[0.4, 0.6]),
    ],
    matingScheme=sim.RandomMating(ops=[
        sim.MendelianGenoTransmitter(),
        sim.MitochondrialGenoTransmitter()
    ]),
    preOps=[
        sim.Stat(neutrality=range(5)),
        sim.Stat(neutrality=range(5, 10), suffix='_X'),
        sim.Stat(neutrality=range(10, 15), suffix='_Y'),
        sim.Stat(neutrality=range(15, 20), suffix='_mt'),
        sim.PyEval(r'"%.3f %.3f %.3f %.3f\n" % (Pi, Pi_X, Pi_Y, Pi_mt)'),
    ],
    gen=2)
Esempio n. 11
0
import simuPOP
from simuPOP import utils
from simuPOP import sampling
from simuPOP import demography

def calcFst(pop):
    'Calculate Fst and Gst for the whole population and a random sample'
    simuPOP.stat(pop, structure=range(5), vars=['F_st', 'G_st'])
    sample = simuPOP.sampling.drawRandomSample(pop, sizes=[500]*pop.numSubPop())
    simuPOP.stat(sample, structure=range(5), vars=['F_st', 'G_st'])
    print ('Gen: %3d Gst: %.6f (all), %.6f (sample) Fst: %.6f (all) %.6f (sample)' \
        % (pop.dvars().gen,
           pop.dvars().G_st, sample.dvars().G_st,
           pop.dvars().F_st, sample.dvars().F_st))
    return True

pop = simuPOP.Population([10000]*5, loci=[1]*5, infoFields='migrate_to')
pop.evolve(
    initOps = [
        simuPOP.InitSex(),
        simuPOP.InitGenotype(freq=[0.5, 0.5], loci=[0, 2]),
        simuPOP.InitGenotype(freq=[0.2, 0.4, 0.4], loci=[1, 3, 4]),
    ],
    matingScheme = simuPOP.RandomMating(),
    postOps = [
        #simuPOP.Migrator(rate=simuPOP.demography.migrIslandRates(0.01, 3)),
        simuPOP.PyOperator(func=calcFst, step=20),
    ],
    gen = 500
)

def update_accumulator(pop, param):
    accumulator, var = param
    if var.endswith('_sp'):
        for sp in range(pop.numSubPop()):
            pop.vars()[accumulator][sp].append(
                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)
Esempio n. 13
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)])
Esempio n. 14
0
    elif geno[0] + geno[1] == 2 and geno[2] + geno[3] == 1:
        v = 0.03  # case of aaBb
    elif geno[0] + geno[1] == 2 and geno[2] + geno[3] == 2:
        v = 0.05  # case of aabb
    else:
        v = 0.01  # other cases
    if smoking:
        return v * 2
    else:
        return v


pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[.5, .5]),
        sim.PyOutput('Calculate prevalence in smoker and non-smokers\n'),
    ],
    matingScheme=sim.RandomMating(),
    postOps=[
        # set smoking status randomly
        sim.InitInfo(lambda: random.randint(0, 1), infoFields='smoking'),
        # assign affection status
        sim.PyPenetrance(loci=[0, 1], func=penet),
        sim.Stat(numOfAffected=True,
                 subPops=[(0, sim.ALL_AVAIL)],
                 vars='propOfAffected_sp',
                 step=20),
        sim.PyEval(
            r"'Non-smoker: %.2f%%\tSmoker: %.2f%%\n' % "
            "(subPop[(0,0)]['propOfAffected']*100, subPop[(0,1)]['propOfAffected']*100)",
Esempio n. 15
0
# for details.
#
# Copyright (C) 2004 - 2010 Bo Peng ([email protected])
#
# 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(100), rep=10)
simu.evolve(
    initOps=[sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5])],
    matingScheme=sim.RandomMating(),
    postOps=[sim.Pause(stopOnKeyStroke=str(x), reps=x) for x in range(10)],
    gen=100)
Esempio n. 16
0
def main():
    start = time()
    MAXALLELES = 10000000

    parser = argparse.ArgumentParser()
    parser.add_argument("--experiment",
                        help="provide name for experiment",
                        required=True,
                        type=str)
    parser.add_argument(
        "--cores",
        type=int,
        help=
        "Number of cores to use for simuPOP, overrides devel flag and auto calculation"
    )
    parser.add_argument("--debug", help="turn on debugging output")
    parser.add_argument("--devel",
                        help="Use only half of the available CPU cores",
                        type=int,
                        default=1)
    parser.add_argument("--dbhost",
                        help="database hostname, defaults to localhost",
                        default="localhost")
    parser.add_argument("--dbport",
                        help="database port, defaults to 27017",
                        default="27017")
    parser.add_argument("--reps",
                        help="Replicated populations per parameter set",
                        type=int,
                        default=4)
    parser.add_argument(
        "--networkmodel",
        help=
        "Path of a zipfile containing GML files representing the temporal network model for this simulation",
        required=True,
        type=str)
    parser.add_argument("--numloci",
                        help="Number of loci per individual",
                        type=int,
                        required=True)
    parser.add_argument(
        "--maxinittraits",
        help="Max initial number of traits per locus for initialization",
        type=int,
        required=True)
    parser.add_argument(
        "--samplefraction",
        help=
        "Size of samples taken to calculate all statistics, as a proportion",
        type=float,
        required=True)
    parser.add_argument(
        "--innovrate",
        help=
        "Rate at which innovations occur in population as a per-locus rate",
        type=float,
        required=True)
    parser.add_argument(
        "--simlength",
        help=
        "Time at which simulation and sampling end, defaults to 3000 generations",
        type=int,
        default="3000")
    parser.add_argument(
        "--popsize",
        help="Initial size of population for each community in the model",
        type=int,
        required=True)
    parser.add_argument(
        "--migrationfraction",
        help="Fraction of population that migrates each time step",
        type=float,
        required=True,
        default=0.2)
    parser.add_argument(
        "--seed",
        type=int,
        help="Seed for random generators to ensure replicability")

    (config, sim_id, script, cores) = setup(parser)

    log.info("config: %s", config)

    ### NOTE ###
    ###
    ### the simuPOP module is deliberately imported here because we need to process the
    ### command line arguments first, to understand which version of the simuPOP module (e.g.,
    ### long allele representation, etc, to import, and because we need to figure out how
    ### many cores the machine has, etc., to set it up for parallel processing.  If we import
    ### at the top of the file as normal, the imports happen before any code is executed,
    ### and we can't set those options.  DO NOT move these imports out of setup and main.
    import simuPOP as sim
    import seriationct.demography as demo

    log.info("Starting simulation run %s", sim_id)
    log.debug("config: %s", config)
    if config.seed is None:
        log.info(
            "No random seed given, allowing RNGs to initialize with random seed"
        )
    else:
        log.debug("Seeding RNGs with seed: %s", config.seed)
        npr.seed(config.seed)
        random.seed(config.seed)

    full_command_line = " ".join(sys.argv)

    # Calculate the burn in time

    burn_time = utils.simulation_burnin_time(config.popsize, config.innovrate)
    log.info("Minimum burn in time given popsize and theta: %s", burn_time)

    initial_distribution = pypopgen.constructUniformAllelicDistribution(
        config.maxinittraits)
    log.debug("Initial allelic distribution (for each locus): %s",
              initial_distribution)

    #innovation_rate = pypopgen.wf_mutation_rate_from_theta(config.popsize, config.innovrate)
    innovation_rate = float(config.innovrate)
    log.debug("Per-locus innov rate within populations: %s", innovation_rate)

    # Construct a demographic model from a collection of network slices which represent a temporal network
    # of changing subpopulations and interaction strengths.  This object is Callable, and simply is handed
    # to the mating function which applies it during the copying process
    networkmodel = demo.TemporalNetwork(
        networkmodel_path=config.networkmodel,
        simulation_id=sim_id,
        sim_length=config.simlength,
        burn_in_time=burn_time,
        initial_subpop_size=config.popsize,
        migrationfraction=config.migrationfraction)

    # The regional network model defines both of these, in order to configure an initial population for evolution
    # Construct the initial population

    pop = sim.Population(size=networkmodel.get_initial_size(),
                         subPopNames=networkmodel.get_subpopulation_names(),
                         infoFields=networkmodel.get_info_fields(),
                         ploidy=1,
                         loci=config.numloci)

    log.info("population sizes: %s names: %s", pop.subPopSizes(),
             pop.subPopNames())

    # We are going to evolve the same population over several replicates, in order to measure how stochastic variation
    # effects the measured copying process.
    simu = sim.Simulator(pop, rep=config.reps)

    # Start the simulation and evolve the population, taking samples after the burn-in time has elapsed
    simu.evolve(
        initOps=sim.InitGenotype(freq=initial_distribution),
        preOps=[
            sim.PyOperator(func=sampling.logGenerationCount,
                           param=(),
                           step=100,
                           reps=0)
        ],
        matingScheme=sim.RandomSelection(subPopSize=networkmodel),
        postOps=[
            sim.KAlleleMutator(k=MAXALLELES, rates=innovation_rate),
            sim.PyOperator(func=sampling.sampleAlleleAndGenotypeFrequencies,
                           param=(config.samplefraction, config.innovrate,
                                  config.popsize, sim_id, config.numloci,
                                  script, full_command_line, config.seed),
                           subPops=sim.ALL_AVAIL,
                           step=1,
                           begin=burn_time),
        ],
        gen=config.simlength,
    )

    endtime = time()
    elapsed = endtime - start
    #log.info("simulation complete in %s seconds with %s cores", elapsed, cores)
    log.info("simulation complete,%s,%s", cores, elapsed)
    sampled_length = int(config.simlength) - burn_time

    database = config.experiment
    database += "_samples_raw"
    db_args = {}
    db_args['dbhost'] = config.dbhost
    db_args['dbport'] = config.dbport
    db_args['database'] = database
    db_args['dbuser'] = None
    db_args['dbpassword'] = None
    sm_db = data.SimulationMetadataDatabase(db_args)
    sm_db.store_simulation_run_parameters(
        sim_id, script, config.experiment, elapsed, config.simlength,
        sampled_length, config.popsize, config.networkmodel,
        networkmodel.get_subpopulation_durations(),
        full_command_line, config.seed,
        networkmodel.get_subpopulation_origin_times(), innovation_rate,
        config.migrationfraction, config.numloci, config.maxinittraits)
Esempio n. 17
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


def myPenetrance(geno):
    'A three-locus heterogeneity penetrance model'
    if sum(geno) < 2:
        return 0
    else:
        return sum(geno) * 0.1


pop = sim.Population(1000, loci=[20] * 3)
pop.evolve(initOps=[sim.InitSex(),
                    sim.InitGenotype(freq=[0.8, 0.2])],
           matingScheme=sim.RandomMating(),
           postOps=[
               sim.PyPenetrance(func=myPenetrance, loci=[10, 30, 50]),
               sim.Stat(numOfAffected=True),
               sim.PyEval(r"'%d: %d\n' % (gen, numOfAffected)")
           ],
           gen=5)
Esempio n. 18
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
from simuPOP.sampling import indexToID
pop = sim.Population(size=15,
                     loci=5,
                     infoFields=['father_idx', 'mother_idx'],
                     ancGen=2)
pop.evolve(preOps=[
    sim.InitSex(),
    sim.InitGenotype(freq=[0.7, 0.3]),
],
           matingScheme=sim.RandomMating(
               numOffspring=(sim.UNIFORM_DISTRIBUTION, 2, 4),
               ops=[sim.MendelianGenoTransmitter(),
                    sim.ParentsTagger()]),
           postOps=sim.MaPenetrance(loci=3, penetrance=(0.1, 0.4, 0.7)),
           gen=5)
indexToID(pop, reset=True)
# three information fields were added
print(pop.infoFields())
# save this population for future use
pop.save('log/pedigree.pop')

from simuPOP.sampling import drawAffectedSibpairSample
pop = sim.loadPopulation('log/pedigree.pop')
Esempio n. 19
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(size=1000, loci=2)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(genotype=[1, 2, 2, 1])
    ],
    matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=0.01)),
    postOps=[
        sim.Stat(LD=[0, 1], step=10),
        sim.PyEval(r"'%.2f\n' % LD[0][1]", step=10),
    ],
    gen=100
)

#

import simuPOP as sim

import random
N = 10000
pop = sim.Population(N, infoFields=['age', 'ind_id', 'father_id', 'mother_id'])
# we simulate age 0, 1, 2, 3 
pop.setVirtualSplitter(sim.InfoSplitter(field='age', values=[0, 1, 2, 3]))
pop.evolve(
    initOps=[
        sim.InitSex(),
        # random assign age
        sim.InitInfo(lambda: random.randint(0, 3), infoFields='age'),
        # random genotype
        sim.InitGenotype(freq=[0.5, 0.5]),
        # assign an unique ID to everyone.
        sim.IdTagger(),
    ],
    # increase the age of everyone by 1 before mating.
    preOps=sim.InfoExec('age += 1'),
    matingScheme=sim.HeteroMating([
        # age 1, 2 will be copied
        sim.CloneMating(
            ops=[
                # This will set offspring ID
                sim.CloneGenoTransmitter(),
                # new ID for offspring in order to track pedigree
                sim.IdTagger(),
                # both offspring and parental IDs will be the same
                sim.PedigreeTagger(output='>>structured.ped'),
Esempio n. 21
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--experiment",
                        help="provide name for experiment",
                        required=True,
                        type=str,
                        default="test")
    parser.add_argument("--debug", help="turn on debugging output")
    parser.add_argument("--reps",
                        help="Replicated populations per parameter set",
                        type=int,
                        default=1)
    parser.add_argument(
        "--networkfile",
        help=
        "Name of GML file representing the  network model for this simulation",
        required=True,
        type=str)
    parser.add_argument("--numloci",
                        help="Number of loci per individual",
                        type=int,
                        required=True)
    parser.add_argument(
        "--maxinittraits",
        help="Max initial number of traits per locus for initialization",
        type=int,
        required=True)
    parser.add_argument(
        "--innovrate",
        help=
        "Rate at which innovations occur in population as a per-locus rate",
        type=float,
        default=0.001)
    parser.add_argument(
        "--simlength",
        help=
        "Time at which simulation and sampling end, defaults to 20 generations",
        type=int,
        default="20")
    parser.add_argument(
        "--popsize",
        help="Initial size of population for each community in the model",
        type=int,
        required=True)
    parser.add_argument(
        "--migrationfraction",
        help="Fraction of population that migrates each time step",
        type=float,
        required=True,
        default=0.0001)
    parser.add_argument(
        "--seed",
        type=int,
        help="Seed for random generators to ensure replicability")
    parser.add_argument("--k_values",
                        nargs='+',
                        type=int,
                        help="list of k-values to explore [e.g., 2 4 20 24",
                        default=[])
    parser.add_argument("--sub_pops",
                        type=int,
                        help="Number of sub populations",
                        required=True,
                        default=10)
    parser.add_argument("--maxalleles",
                        type=int,
                        help="Maximum number of alleles",
                        default=50)
    parser.add_argument("--save_figs",
                        type=bool,
                        help="Save figures or not?",
                        default=True)
    parser.add_argument("--burnintime",
                        type=int,
                        help="How long to wait before making measurements? ",
                        default=2000)
    parser.add_argument("--rewiringprob",
                        type=float,
                        help="Probability of random rewiring",
                        default=0)

    config = parser.parse_args()

    # check the k and migration rate combinations
    for kvalue in list(config.k_values):
        if float(kvalue) * float(config.migrationfraction) >= 1.0:
            print("k=%s * mig=%4f is greater than 1.0\n" %
                  (kvalue, config.migrationfraction))
            print(
                "Please adjust input values for k and/or migration rate and restart.\n "
            )
            sys.exit()

    # setup output directories for writing
    output_path = utils.setup_output(config.experiment)

    # save parameters
    utils.save_parameters(str(sys.argv), config, output_path)

    run_param = config.k_values

    ## initialize the output dictionary
    for k in run_param:
        output[k] = {}
        if k >= config.sub_pops:
            print("k values cannot be greater than the number of sub pops.\n")
            sys.exit()

    # set up the frequencies for the alleles in each loci. Here assuming a uniform distribution as a starting point
    distribution = utils.constructUniformAllelicDistribution(
        config.maxinittraits)

    iteration_number = -1
    for param_value in run_param:
        iteration_number += 1
        ## these are lists of things that simuPop will do at different stages
        init_ops = OrderedDict()
        pre_ops = OrderedDict()
        post_ops = OrderedDict()

        # Construct a demographic model from a collection of network slices which represent a temporal network
        # of changing subpopulations and interaction strengths.  This object is Callable, and simply is handed
        # to the mating function which applies it during the copying process
        #networkmodel = NetworkModel( networkmodel="/Users/clipo/Documents/PycharmProjects/RapaNuiSim/notebooks/test_graph.gml",
        networkmodel = network.NetworkModel(
            networkmodel=config.networkfile,
            simulation_id=config.experiment,
            sim_length=config.simlength,
            burn_in_time=config.burnintime,
            initial_subpop_size=config.popsize,
            migrationfraction=config.migrationfraction,
            sub_pops=int(config.sub_pops),
            connectedness=param_value,  # if 0, then distance decay
            save_figs=config.save_figs,
            network_iteration=iteration_number,
            output_path=output_path)

        num_pops = networkmodel.get_subpopulation_number()
        if param_value >= num_pops:
            print(
                "k values cannot be greater or equal to the number of sub pops.\n"
            )
            sys.exit()
        sub_pop_size = int(config.popsize / num_pops)

        # The regional network model defines both of these, in order to configure an initial population for evolution
        # Construct the initial population
        pops = sp.Population(size=[sub_pop_size] * num_pops,
                             subPopNames=str(
                                 list(networkmodel.get_subpopulation_names())),
                             infoFields='migrate_to',
                             ploidy=1,
                             loci=config.numloci)

        ### now set up the activities
        init_ops['acumulators'] = sp.PyOperator(
            utils.init_acumulators, param=['fst', 'alleleFreq', 'haploFreq'])
        init_ops['subpop_counts'] = sp.PyOperator(
            utils.init_count_traits_in_subpops)
        init_ops['Sex'] = sp.InitSex()
        init_ops['Freq'] = sp.InitGenotype(loci=list(range(config.numloci)),
                                           freq=distribution)

        post_ops['Innovate'] = sp.KAlleleMutator(k=config.maxalleles,
                                                 rates=config.innovrate,
                                                 loci=sp.ALL_AVAIL)
        post_ops['mig'] = sp.Migrator(
            rate=networkmodel.get_migration_matrix())  #, reps=[3])
        post_ops['Stat-fst'] = sp.Stat(structure=sp.ALL_AVAIL)
        post_ops['Stat-richness'] = sp.Stat(
            alleleFreq=[0],
            haploFreq=[0],
            vars=['alleleFreq', 'haploFreq', 'alleleNum', 'genoNum'])
        post_ops['fst_acumulation'] = sp.PyOperator(utils.update_acumulator,
                                                    param=['fst', 'F_st'])
        post_ops['richness_acumulation'] = sp.PyOperator(
            utils.update_richness_acumulator,
            param=('alleleFreq', 'Freq of Alleles'))
        post_ops['class_richness'] = sp.PyOperator(
            utils.calculateAlleleAndGenotypeFrequencies,
            param=(config.popsize, config.numloci))
        post_ops['count_traits_in_subpops'] = sp.PyOperator(
            utils.count_traits_in_subpops,
            param=(config.numloci, num_pops),
            subPops=sp.ALL_AVAIL)

        mating_scheme = sp.RandomSelection()

        ## go simuPop go! evolve your way to the future!
        sim = sp.Simulator(pops, rep=config.reps)
        print("now evolving... k= %s" % param_value)
        sim.evolve(initOps=list(init_ops.values()),
                   preOps=list(pre_ops.values()),
                   postOps=list(post_ops.values()),
                   matingScheme=mating_scheme,
                   gen=config.simlength)

        # now make a figure of the Fst results
        fig = plt.figure(figsize=(16, 9))
        ax = fig.add_subplot(111)
        count = 0
        for pop in sim.populations():
            ax.plot(pop.dvars().fst, label='Replicate: %s' % count)
            output[param_value][count] = deepcopy(pop.dvars())
            count += 1
        ax.legend(loc=2, fontsize=24)
        ax.set_ylabel('FST', fontsize=24)
        ax.set_xlabel('Generations', fontsize=24)
        ax.tick_params(axis='x', labelsize=20)
        ax.tick_params(axis='y', labelsize=20)
        plt.show()

    ## draw traits in 1s or 2s of the subpops
    subpop_fig = plt.figure(figsize=(16, 9))
    ax = subpop_fig.add_subplot(111)
    iteration = -1
    for k in run_param:
        iteration += 1
        # only label the first one
        for n in range(config.reps):
            if n == 0:
                #print(output[k][n].ones)
                ax.plot(output[k][n].ones,
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration],
                        label='k = %s - traits in just one subpopulation' % k)
                ax.plot(
                    output[k][n].twos,
                    "--",
                    color=list(
                        dict(mcolors.BASE_COLORS,
                             **mcolors.CSS4_COLORS).keys())[iteration],
                    label='k = %s - traits in just two or fewer subpopulations'
                    % k)
            else:
                ax.plot(output[k][n].ones,
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration])
                ax.plot(output[k][n].twos,
                        "--",
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration])
    ax.legend(loc=2, fontsize=24)
    ax.set_ylabel('Numbers of Traits', fontsize=24)
    ax.set_xlabel('Generations', fontsize=24)
    ax.tick_params(axis='x', labelsize=20)
    ax.tick_params(axis='y', labelsize=20)
    plt.show()
    savefilename = output_path + "/subpop_fig.svg"
    subpop_fig.savefig(savefilename, bbox_inches='tight')

    sum_fig = plt.figure(figsize=(16, 9))
    ax = sum_fig.add_subplot(111)
    iteration = -1
    for k in run_param:
        iteration += 1
        # only label the first one
        for n in range(config.reps):
            if n == 0:
                ax.plot(output[k][n].fst,
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration],
                        label='k = %s' % k)
            else:
                ax.plot(output[k][n].fst,
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration])
    ax.legend(loc=2, fontsize=24)
    ax.set_ylabel('Fst', fontsize=24)
    ax.set_xlabel('Generations', fontsize=24)
    ax.tick_params(axis='x', labelsize=20)
    ax.tick_params(axis='y', labelsize=20)
    plt.show()
    savefilename = output_path + "/sum_fig.svg"
    sum_fig.savefig(savefilename, bbox_inches='tight')

    rich_fig = plt.figure(figsize=(16, 9))
    ax = rich_fig.add_subplot(111)
    iteration = -1
    for k in run_param:
        iteration += 1
        # only add a label for the first one (not all the replicates)
        for n in range(config.reps):
            if n == 0:
                ax.plot(output[k][n].richness,
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration],
                        label='k = %s' % k)
            else:
                ax.plot(output[k][n].richness,
                        color=list(
                            dict(mcolors.BASE_COLORS,
                                 **mcolors.CSS4_COLORS).keys())[iteration])
    ax.legend(loc=2, fontsize=24)
    ax.set_ylabel('Richness', fontsize=24)
    ax.set_xlabel('Generations', fontsize=24)
    ax.tick_params(axis='x', labelsize=20)
    ax.tick_params(axis='y', labelsize=20)
    plt.show()
    savefilename = output_path + "/richness.svg"
    rich_fig.savefig(savefilename, bbox_inches='tight')

    ## output CI for the parameters

    summary_fig = plt.figure(figsize=(16, 9))
    ax = summary_fig.add_subplot(111)

    iteration = -1
    for k in run_param:
        iteration += 1
        CI_average = []
        CI_min = []
        CI_max = []
        for t in range(len(output[k][0].fst)):
            point_in_time = []
            for n in range(config.reps):
                list_of_points = list(output[k][n].fst)
                point_in_time.append(list_of_points[t])
            (ave, min, max) = utils.mean_confidence_interval(point_in_time,
                                                             confidence=0.95)
            CI_average.append(ave)
            CI_min.append(min)
            CI_max.append(max)
        ax.plot(list(CI_average),
                color=list(
                    dict(mcolors.BASE_COLORS,
                         **mcolors.CSS4_COLORS).keys())[iteration],
                label='k = %s' % k)
        ax.plot(list(CI_min), "--", color="0.5")
        ax.plot(list(CI_max), "--", color="0.5")
        ax.fill_between(list(CI_average),
                        list(CI_max),
                        list(CI_min),
                        color="None",
                        linestyle="--")
        ax.legend(loc=2, fontsize=24)
        ax.set_ylabel('Fst', fontsize=24)
        ax.set_xlabel('Generations', fontsize=24)
        ax.tick_params(axis='x', labelsize=20)
        ax.tick_params(axis='y', labelsize=20)
    plt.show()
    savefilename = output_path + "/summary-ci.svg"
    summary_fig.savefig(savefilename, bbox_inches='tight')

    ## now the richness graph
    richness_sum_fig = plt.figure(figsize=(16, 9))
    ax = richness_sum_fig.add_subplot(111)

    iteration = -1
    for k in run_param:
        iteration += 1
        CI_average = []
        CI_min = []
        CI_max = []
        for t in range(len(output[k][0].richness)):
            point_in_time = []
            for n in range(config.reps):
                list_of_points = list(output[k][n].richness)
                point_in_time.append(list_of_points[t])
            (ave, min, max) = utils.mean_confidence_interval(point_in_time,
                                                             confidence=0.95)
            CI_average.append(ave)
            CI_min.append(min)
            CI_max.append(max)
        ax.plot(list(CI_average),
                color=list(
                    dict(mcolors.BASE_COLORS,
                         **mcolors.CSS4_COLORS).keys())[iteration],
                label='k = %s' % k)
        ax.plot(list(CI_min), "--", color="0.5")
        ax.plot(list(CI_max), "--", color="0.5")
        ax.fill_between(list(CI_average),
                        list(CI_max),
                        list(CI_min),
                        color="None",
                        linestyle="--")
        ax.legend(loc=2, fontsize=24)
        ax.set_ylabel('Richness', fontsize=24)
        ax.set_xlabel('Generations', fontsize=24)
        ax.tick_params(axis='x', labelsize=20)
        ax.tick_params(axis='y', labelsize=20)
    plt.show()
    savefilename = output_path + "/richness-ci.svg"
    richness_sum_fig.savefig(savefilename, bbox_inches='tight')
Esempio n. 22
0
# 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=[1000], loci=[100]), rep=2)
simu.evolve(
    initOps=[sim.InitSex(),
             sim.InitGenotype(genotype=[0] * 100 + [1] * 100)],
    matingScheme=sim.RandomMating(ops=[
        sim.Recombinator(rates=0.01, reps=0),
        sim.Recombinator(rates=[0.01] * 10, loci=range(50, 60), reps=1),
    ]),
    postOps=[
        sim.Stat(LD=[[40, 55], [60, 70]]),
        sim.PyEval(
            r'"%d:\t%.3f\t%.3f\t" % (rep, LD_prime[40][55], LD_prime[60][70])'
        ),
        sim.PyOutput('\n', reps=-1)
    ],
    gen=5)
Esempio n. 23
0
# the user's guide (http://simupop.sourceforge.net/manual) for a detailed
# description of this example.
#

import simuOpt

simuOpt.setOptions(alleleType='lineage', quiet=True)
import simuPOP as sim

pop = sim.Population(size=10000, loci=[10] * 10, infoFields='ind_id')
# just to make sure IDs starts from 1
sim.IdTagger().reset(1)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.2, 0.3, 0.4, 0.1]),
        sim.IdTagger(),
        sim.InitLineage(mode=sim.FROM_INFO),
    ],
    # an extremely high mutation rate, just for demonstration
    preOps=sim.AcgtMutator(rate=0.01, model='JC69'),
    matingScheme=sim.RandomMating(ops=[
        sim.IdTagger(),
        sim.MendelianGenoTransmitter(),
    ]),
    gen=10)
lin = pop.lineage()
# Number of alleles from each generation
for gen in range(10):
    id_start = gen * 10000 + 1
    id_end = (gen + 1) * 10000
Esempio n. 24
0
import simuPOP as sim
from simuPOP.utils import migrIslandRates

p = [0.2, 0.3, 0.5]

pop = sim.Population(size=[10000] * 3, loci=1, infoFields='migrate_to')
simu = sim.Simulator(pop, rep=2)

simu.evolve(
    initOps=[sim.InitSex()] +
    [sim.InitGenotype(prop=[p[i], 1 - p[i]], subPops=i) for i in range(3)],
    preOps=sim.Migrator(rate=migrIslandRates(0.01, 3), reps=0),
    matingScheme=sim.RandomMating(),
    postOps=[
        sim.Stat(alleleFreq=0, structure=0, vars='alleleFreq_sp', step=50),
        sim.PyEval(
            r"'Fst=%.3f (%s)' % (F_st, ', '.join(['%.2f’ % "
            "subPop[x]['alleleFreq'][0][0] for x in range(3)]))",
            step=50),
        sim.PyOutput('\n', reps=-1, step=50),
    ],
    gen=201)
Esempio n. 25
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(size=1000, loci=[1, 1])
pop.evolve(
    # all start from allele 50
    initOps=[sim.InitSex(),
             sim.InitGenotype(freq=[0] * 50 + [1])],
    matingScheme=sim.RandomMating(),
    preOps=[
        sim.StepwiseMutator(rates=1e-3, loci=0),
        sim.StepwiseMutator(rates=1e-3,
                            incProb=0.6,
                            loci=1,
                            mutStep=(sim.GEOMETRIC_DISTRIBUTION, 0.2)),
    ],
    gen=100)
# count the average number tandem repeats at both loci
cnt0 = cnt1 = 0
for ind in pop.individuals():
    cnt0 += ind.allele(0, 0) + ind.allele(0, 1)
    cnt1 += ind.allele(1, 0) + ind.allele(1, 1)
Esempio n. 26
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(10,
                     loci=[20],
                     ancGen=1,
                     infoFields=['father_idx', 'mother_idx'])
pop.evolve(
    initOps=[sim.InitSex(),
             sim.InitGenotype(genotype=[0] * 20 + [1] * 20)],
    matingScheme=sim.RandomMating(
        ops=[sim.Recombinator(
            rates=0.01), sim.ParentsTagger()]),
    gen=1)
pop.indInfo('mother_idx')  # mother of all offspring
ind = pop.individual(0)
mom = pop.ancestor(ind.mother_idx, 1)
print(ind.genotype(0))
print(mom.genotype(0))
print(mom.genotype(1))
Esempio n. 27
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([2000], loci=[1] * 50)
pop.setVirtualSplitter(sim.RangeSplitter([0, 500]))
pop.evolve(initOps=[
    sim.InitSex(),
    sim.InitGenotype(freq=[0.3, 0.7]),
    sim.Stat(effectiveSize=range(50),
             subPops=[(0, 0)],
             vars='Ne_temporal_base'),
],
           preOps=[
               sim.Stat(effectiveSize=range(50),
                        subPops=[(0, 0)],
                        vars='Ne_waples89_P1',
                        step=20),
               sim.Stat(effectiveSize=range(50),
                        subPops=[(0, 0)],
                        step=20,
                        suffix='_i',
                        vars=['Ne_temporal_base', 'Ne_waples89_P1']),
               sim.PyEval(
Esempio n. 28
0
# 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=2)
simu.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.2, 0.8]),
        sim.PyExec('traj=[]')
    ],
    matingScheme=sim.RandomMating(),
    postOps=[
        sim.Stat(alleleFreq=0),
        sim.PyExec('traj.append(alleleFreq[0][1])'),
    ],
    gen=5
)
# print Trajectory
print(', '.join(['%.3f' % x for x in simu.dvars(0).traj]))

Esempio n. 29
0
totalSimulationLength = beginCollectingData + args.length
log.info("Simulation will sample %s generations after stationarity",
         args.length)

#data.storeSimulationData(args.popsize,args.mutationrate,sim_id,args.samplesize,args.replications,args.numloci,__file__,args.numalleles,simconfig.MAXALLELES)

initial_distribution = utils.constructUniformAllelicDistribution(args.numloci)

log.info("Initial allelic distribution: %s", initial_distribution)

pop = sim.Population(size=args.popsize, ploidy=1, loci=args.numloci)
simu = sim.Simulator(pop, rep=args.replications)

simu.evolve(
    initOps=sim.InitGenotype(freq=initial_distribution),
    preOps=[
        sim.PyOperator(func=utils.logGenerationCount,
                       param=(),
                       step=1000,
                       reps=0),
    ],
    matingScheme=sim.RandomSelection(),
    postOps=[
        sim.KAlleleMutator(k=simconfig.MAXALLELES,
                           rates=args.mutationrate,
                           loci=sim.ALL_AVAIL),
        sim.PyOperator(func=data.sampleNumAlleles,
                       param=(args.samplesize, args.mutationrate, args.popsize,
                              sim_id, args.numloci),
                       step=args.stepsize,
Esempio n. 30
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"