Exemplo n.º 1
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
import random
pop = sim.Population(size=[200, 200], loci=[5, 5], infoFields='age')
sim.initGenotype(pop, genotype=range(10))
sim.initInfo(pop, lambda: random.randint(0, 75), infoFields='age')
pop.setVirtualSplitter(sim.InfoSplitter(field='age', cutoff=[20, 60]))
# remove individuals
pop.removeIndividuals(indexes=range(0, 300, 10))
print(pop.subPopSizes())
# remove individuals using IDs
pop.setIndInfo([1, 2, 3, 4], field='age')
pop.removeIndividuals(IDs=[2, 4], idField='age')
# remove indiviuals using a filter function
sim.initSex(pop)
pop.removeIndividuals(filter=lambda ind: ind.sex() == sim.MALE)
print([pop.individual(x).sex() for x in range(8)])
#
# remove subpopulation
pop.removeSubPops(1)
print(pop.subPopSizes())
Exemplo n.º 2
0
 def simulation(self):
     self.pop = sim.Population(size = [500, 500], loci=[1]*20,
                              infoFields = ["age",'ind_id', 'father_idx', 'mother_idx', "hc", "ywc",'migrate_to'],
                              subPopNames = ["croatia", "slovenia"])
     sim.initInfo(pop = self.pop, values = list(map(int, np.random.negative_binomial(n = 1, p = 0.25, size=500))), infoFields="age")
 
     self.pop.setVirtualSplitter(sim.CombinedSplitter([
         sim.ProductSplitter([
             sim.SexSplitter(),
             sim.InfoSplitter(field = "age", cutoff = [1,3,6,10])])],
         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 10 - dominant
     self.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("hc +=1 if 0 < hc < 3  else 0"), # Mother bear can't have cubs for two years after pregnancy
                 sim.Migrator(rate=[[self.cro_to_slo]],
                              mode=sim.BY_PROPORTION,
                              subPops=[(0, 0)],
                              toSubPops=[1]), # reproductive males migrate from Cro to Slo
                 sim.Migrator(rate=[[self.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=self.bearFather),
                     motherChooser=sim.PyParentsChooser(generator=self.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),
         # number of individuals?
         postOps = [
             #sim.PyOperator(func=popmodel.NaturalMortality),
             sim.PyOperator(func = popmodel.CalcNe, param={"me":self.me, "Ne":self.Ne}, begin=int(0.2*self.generations)),
             sim.PyOperator(func = popmodel.CalcLDNe, param={"me":self.me, "x":self.x}, begin=int(0.2*self.generations)),
             sim.PyOperator(func=popmodel.cullCountry,param={"slo_cull": self.slo_cull, "cro_cull": self.cro_cull}),
                    ],
 
         gen = self.generations
     ) 
 pop = getHapMapMarkers('~/Data/IBDAdmixed', 
     names = [],
     chroms=[1], 
     HapMap_pops=['CEU', 'YRI'],
     startPos = [],
     endPos = [],
     numMarkers = [num_snps],
     minAF = 0,
     minDist = 0,
     logger=logger)
 
 #pop = sim.Population(size=(50,50), loci=[10]*4, )
     
 pop.addInfoFields(['ancestry', 'migrate_to'])
 # initialize ancestry
 sim.initInfo(pop, [0]*pop.subPopSize(0) + [1]*pop.subPopSize(1),
     infoFields='ancestry')
 # define two virtual subpopulations by ancestry value
 # pop.setVirtualSplitter(sim.InfoSplitter(field='ancestry', cutoff = [0.5]))
 pop.addInfoFields(['ind_id'])
 sim.initInfo(pop,values=range(pop.popSize()),infoFields='ind_id')
 
 transmitters=[
     #sim.MendelianGenoTransmitter(),
     sim.Recombinator(intensity=1e-9),
     sim.InheritTagger(mode=sim.MEAN, infoFields='ancestry')]
 pop.evolve(
     initOps=[sim.InitSex(),
              sim.InitLineage(mode=sim.PER_PLOIDY),
              ],
     preOps=[sim.ResizeSubPops(sizes=(2000, 8000), at=17),
             sim.MergeSubPops(subPops=[0, 1], at=17),
Exemplo 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
from simuPOP.utils import saveCSV
pop = sim.Population(size=[10],
                     loci=[2, 3],
                     lociNames=['r11', 'r12', 'r21', 'r22', 'r23'],
                     alleleNames=['A', 'B'],
                     infoFields='age')
sim.initSex(pop)
sim.initInfo(pop, [2, 3, 4], infoFields='age')
sim.initGenotype(pop, freq=[0.4, 0.6])
sim.maPenetrance(pop, loci=0, penetrance=(0.2, 0.2, 0.4))
# no filename so output to standard output
saveCSV(pop, infoFields='age')
# change affection code and how to output genotype
saveCSV(pop,
        infoFields='age',
        affectionFormatter={
            True: 1,
            False: 2
        },
        genoFormatter={
            (0, 0): 'AA',
            (0, 1): 'AB',
            (1, 0): 'AB',
Exemplo n.º 5
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
import random
pop = sim.Population(size=[2000, 4000], loci=[30], infoFields='x')
# assign random information fields
sim.initSex(pop)
sim.initInfo(pop, lambda: random.randint(0, 3), infoFields='x')
#
# 1, use a combined splitter
pop.setVirtualSplitter(sim.CombinedSplitter(splitters = [
    sim.SexSplitter(),
    sim.InfoSplitter(field='x', values=[0, 1, 2, 3])
]))
pop.numVirtualSubPop()    # Number of defined VSPs
pop.subPopName([0, 0])    # Each VSP has a name
pop.subPopSize([0, 0])    # sim.MALE
pop.subPopSize([1, 4])    # individuals in sp 1 with value 2 at field x
#
# use a product splitter that defines additional VSPs by sex and info
pop.setVirtualSplitter(sim.ProductSplitter(splitters = [
    sim.SexSplitter(names=['M', 'F']),  # give a new set of names
    sim.InfoSplitter(field='x', values=[0, 1, 2, 3])
Exemplo n.º 6
0
     names = [],
     chroms=[1], 
     HapMap_pops=['CEU'],
     startPos = [],
     endPos = [],
     numMarkers = [num_snps],
     minAF = 0,
     minDist = 0,
     logger=logger)
 
 #pop = sim.Population(size=(50,50), loci=[10]*4, )
     
 # define two virtual subpopulations by ancestry value
 # pop.setVirtualSplitter(sim.InfoSplitter(field='ancestry', cutoff = [0.5]))
 pop.addInfoFields(['ind_id'])
 sim.initInfo(pop,values=range(pop.popSize()),infoFields='ind_id')
 
 transmitters=[
     #sim.MendelianGenoTransmitter(),
     sim.Recombinator(intensity=1e-9),
     sim.InheritTagger(mode=sim.MEAN)]
 pop.evolve(
     initOps=[sim.InitSex(),
              sim.InitLineage(mode=sim.PER_PLOIDY),
              ],
     preOps=[sim.Stat(popSize=True),
             #sim.Stat(effectiveSize=range(3), subPops=[0, 1], vars='Ne_demo_base_sp'),
             #sim.Stat(inbreeding=sim.ALL_AVAIL, popSize=True, step=1),
             #sim.PyEval(r'"gen %d: IBD freq %.4f, IBS freq %.4f, est: %.4f\n" % '
             #'(gen, sum(IBD_freq.values()) /len(IBD_freq), '
             #' sum(IBS_freq.values()) /len(IBS_freq), '
Exemplo n.º 7
0
# contribute genotype to the last generation?
anc = ped.identifyAncestors()
len(anc)
# remove individuals who do not contribute genotype to the last generation
allIDs = [x.ind_id for x in ped.allIndividuals()]
removedIDs = list(set(allIDs) - set(anc))
ped.removeIndividuals(IDs=removedIDs)
# now create a top most population, but we do not need all of them
# so we record only used individuals
IDs = [x.ind_id for x in ped.allIndividuals(ancGens=N)]
sex = [x.sex() for x in ped.allIndividuals(ancGens=N)]
# create a population, this time with genotype. Note that we do not need
# populaton structure because PedigreeMating disregard population structure.
pop = sim.Population(size=len(IDs), loci=1000, infoFields='ind_id')
# manually initialize ID and sex
sim.initInfo(pop, IDs, infoFields='ind_id')
sim.initSex(pop, sex=sex)
pop.evolve(
    initOps=sim.InitGenotype(freq=[0.4, 0.6]),
    # we do not need migration, or set number of offspring,
    # or demographic model, but we do need a genotype transmitter
    matingScheme=sim.PedigreeMating(ped, ops=sim.MendelianGenoTransmitter()),
    gen=100)
# let us compare the pedigree and the population object
print(ped.indInfo('ind_id')[:5])
print(pop.indInfo('ind_id')[:5])
print([ped.individual(x).sex() for x in range(5)])
print([pop.individual(x).sex() for x in range(5)])
print(ped.subPopSizes())
print(pop.subPopSizes())
Exemplo n.º 8
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
import random
pop = sim.Population(1000, loci=1, infoFields='smoking')
sim.initInfo(pop, lambda:random.randint(0,1), infoFields='smoking')
sim.initGenotype(pop, freq=[0.3, 0.7])

# a penetrance function that depends on smoking
def func(geno, smoking):
    if smoking:
        return (geno[0]+geno[1])*0.4
    else:
        return (geno[0]+geno[1])*0.1

sim.pyPenetrance(pop, loci=0, func=func)
sim.stat(pop, numOfAffected=True)
print(pop.dvars().numOfAffected)


Exemplo n.º 9
0
import simuOpt
simuOpt.setOptions(gui=False, alleleType='binary')
import simuPOP as sim
pop.addInfoFields(['ancestry', 'migrate_to'])
# initialize ancestry
sim.initInfo(pop, [0] * pop.subPopSize(0) + [1] * pop.subPopSize(1),
             infoFields='ancestry')
# define two virtual subpopulations by ancestry value
pop.setVirtualSplitter(sim.InfoSplitter(field='ancestry', cutoff=[0.5]))
transmitters = [
    sim.MendelianGenoTransmitter(),
    sim.InheritTagger(mode=sim.MEAN, infoFields='ancestry')
]
pop.evolve(
    initOps=sim.InitSex(),
    preOps=sim.Migrator(rate=[[0., 0], [0.05, 0]]),
    matingScheme=sim.HeteroMating(matingSchemes=[
        sim.RandomMating(ops=transmitters),
        sim.RandomMating(subPops=[(0, 0)], weight=-0.80, ops=transmitters),
        sim.RandomMating(subPops=[(0, 1)], weight=-0.80, ops=transmitters)
    ], ),
    gen=10,
)
# remove the second subpop
pop.removeSubPops(1)
Exemplo n.º 10
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
import random
pop = sim.Population(1000, loci=1, infoFields=('x', 'y'))
sim.initInfo(pop, lambda:random.randint(0,1), infoFields=('x', 'y'))
sim.initGenotype(pop, freq=[0.3, 0.7])

# a penetrance function that depends on unknown information fields
def func(*fields):
    return 0.4*sum(fields)

# function WithArgs tells PyPenetrance that func accepts fields x, y so that
# it will pass values at fields x and y to func.
sim.pyPenetrance(pop, loci=0, func=sim.WithArgs(func, pop.infoFields()))
sim.stat(pop, numOfAffected=True)
print(pop.dvars().numOfAffected)

Exemplo n.º 11
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
from simuPOP.utils import importPopulation, export
pop = sim.Population([2, 4],
                     loci=5,
                     lociNames=['a1', 'a2', 'a3', 'a4', 'a5'],
                     infoFields='BMI')
sim.initGenotype(pop, freq=[0.3, 0.5, 0.2])
sim.initSex(pop)
sim.initInfo(pop, [20, 30, 40, 50, 30, 25], infoFields='BMI')
export(pop, format='fstat', output='fstat.txt')
print(open('fstat.txt').read())
export(pop, format='structure', phenotype='BMI', output='stru.txt')
print(open('stru.txt').read())
pop1 = importPopulation(format='fstat', filename='fstat.txt')
sim.dump(pop1)