def createAge(pop): ageInitOps = [ # InitInfo(lambda: random.randint(0, cfg.ages-2), infoFields='age'), sp.IdTagger(), # PyOperator(func=outputAge,at=[0]), sp.PyOperator(func=setAge, at=[0]), ] agePreOps = [ sp.InfoExec("age += 1"), sp.InfoExec("mate = -1"), sp.InfoExec("force_skip = 0"), sp.PyOperator(func=outputAge), ] mySubPops = [] for age in range(cfg.ages - 2): mySubPops.append((0, age + 1)) mateOp = sp.HeteroMating([ sp.HomoMating( sp.PyParentsChooser(fitnessGenerator if cfg.doNegBinom else (litterSkipGenerator if cfg.Nb is None else restrictedGenerator)), sp.OffspringGenerator(numOffspring=1, ops=[ sp.MendelianGenoTransmitter(), sp.IdTagger(), sp.PedigreeTagger()], sexMode=(sp.PROB_OF_MALES, cfg.maleProb)), weight=1), sp.CloneMating(subPops=mySubPops, weight=-1)], subPopSize=calcDemo) agePostOps = [ sp.PyOperator(func=outputMega), sp.PyOperator(func=cull), ] pop.setVirtualSplitter(sp.InfoSplitter(field='age', cutoff=list(range(1, cfg.ages)))) return ageInitOps, agePreOps, mateOp, agePostOps
def test_selfing_after_outcrossing(self): """Test scinario: generations of selfing after generations of outcrossing.""" # outcrossing self.sim.evolve(initOps=self.initOps, matingScheme=simu.RandomMating( subPopSize=10, sexMode=self.sexMode, ops=[ simu.MendelianGenoTransmitter(), cf.MyOutcrossingTagger() ]), gen=10) for pop in self.sim.populations(): for ind in pop.individuals(): assert ind.info('self_gen') == 0 assert pop.dvars().gen == 10 # selfing self.sim.evolve( initOps=self.initOps, matingScheme=simu.SelfMating( subPopSize=10, sexMode=self.sexMode, ops=[simu.SelfingGenoTransmitter(), cf.MySelfingTagger()]), gen=10) for pop in self.sim.populations(): for ind in pop.individuals(): assert ind.info('self_gen') == 10 assert pop.dvars().gen == 20
def checkNumOffspring(numOffspring, ops=[]): '''Check the number of offspring for each family using information field father_idx ''' pop = sim.Population(size=[30], loci=1, infoFields=['father_idx', 'mother_idx']) pop.evolve(initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]), ], matingScheme=sim.RandomMating(ops=[ sim.MendelianGenoTransmitter(), sim.ParentsTagger(), ] + ops, numOffspring=numOffspring), gen=1) # get the parents of each offspring parents = [ (x, y) for x, y in zip(pop.indInfo('mother_idx'), pop.indInfo('father_idx')) ] # Individuals with identical parents are considered as siblings. famSize = [] lastParent = (-1, -1) for parent in parents: if parent == lastParent: famSize[-1] += 1 else: lastParent = parent famSize.append(1) return famSize
def LC_evolve(popSize, alleleFreq, diseaseModel): ''' ''' pop = sim.Population( size=popSize, loci=[1] * len(alleleFreq), infoFields=['age', 'smoking', 'age_death', 'age_LC', 'LC']) pop.setVirtualSplitter( sim.CombinedSplitter(splitters=[ sim.InfoSplitter(field='age', cutoff=[20, 40], names=['youngster', 'adult', 'senior']), sim.SexSplitter(), sim.InfoSplitter(field='smoking', values=[0, 1, 2], names=['nonSmoker', 'smoker', 'formerSmoker']) ])) pop.evolve( initOps=[sim.InitSex(), sim.InitInfo(range(75), infoFields='age')] + [ sim.InitGenotype(freq=[1 - f, f], loci=i) for i, f in enumerate(alleleFreq) ] + [ sim.PyOperator(func=diseaseModel.initialize), ], preOps=[ sim.InfoExec('age += 1'), # die of lung cancer or natural death sim.DiscardIf('age > age_death') ], matingScheme=sim.HeteroMating( [ sim.CloneMating(weight=-1), sim.RandomMating(ops=[ sim.MendelianGenoTransmitter(), sim.PyOperator(func=diseaseModel.initialize) ], subPops=[(0, 'adult')]) ], subPopSize=lambda pop: pop.popSize() + popSize / 75), postOps=[ # update individual, currently ding nothing. sim.PyOperator(func=diseaseModel.updateStatus), # determine if someone has LC at his or her age sim.InfoExec('LC = age >= age_LC'), # get statistics about COPD and LC prevalence sim.Stat(pop, meanOfInfo='LC', subPops=[(0, sim.ALL_AVAIL)], vars=['meanOfInfo', 'meanOfInfo_sp']), sim.PyEval( r"'Year %d: Overall %.2f%% M: %.2f%% F: %.2f%% " r"NS: %.1f%%, S: %.2f%%\n' % (gen, meanOfInfo['LC']*100, " r"subPop[(0,3)]['meanOfInfo']['LC']*100," r"subPop[(0,4)]['meanOfInfo']['LC']*100," r"subPop[(0,5)]['meanOfInfo']['LC']*100," r"subPop[(0,6)]['meanOfInfo']['LC']*100)"), ], gen=100)
def drawSample(self, pop, penet, nFamilies): self.pop = pop.clone() self.pop.addInfoFields(['ind_id', 'father_id', 'mother_id']) self.pop.setAncestralDepth(1) sim.tagID(self.pop, reset=True) self.pop.evolve( preOps = penet, matingScheme=sim.RandomMating(ops=[ sim.MendelianGenoTransmitter(), # pass genotype sim.IdTagger(), # assign new ID to offspring sim.PedigreeTagger(), # record the parent of each offspring penet, # determine offspring affection status sim.DiscardIf(cond=self._discardTrio) ], subPopSize=nFamilies), gen = 1 ) return self.pop
def test_pure_outcrossing(self): """Test pure outcrossing population. Without any selfing in history, the infoField `self_gen` should have 0 for all loci for all individuals. """ self.sim.evolve(initOps=self.initOps, matingScheme=simu.RandomMating( subPopSize=10, sexMode=self.sexMode, ops=[ simu.MendelianGenoTransmitter(), cf.MyOutcrossingTagger() ]), gen=10) for pop in self.sim.populations(): for ind in pop.individuals(): assert ind.info('self_gen') == 0
def genCaseControlSample(pop, nCase, nControl, penetrance): '''Draw nCase affected and nControl unaffected individuals by producing offspring from pop repeatedly until enough cases and controls are collected. A penetrance operator is needed to assign affection status to each offspring. ''' sample = pop.clone() sample.setVirtualSplitter( sim.ProductSplitter([ sim.AffectionSplitter(), sim.RangeSplitter([[0, nCase], [nCase, nCase + nControl]]) ])) sample.evolve(matingScheme=sim.RandomMating(ops=[ sim.MendelianGenoTransmitter(), penetrance, sim.DiscardIf(True, subPops=[(0, 0), (0, 3)]) ], subPopSize=nCase + nControl), gen=1) return sample
def genAffectedSibpairSample(pop, nFamilies, penetrance): '''Draw nFamilies affected sibpairs and their parents by producing siblings from pop repeatedly until enough affected sibpairs are collected. A penetrance operator is needed to assign affection status to each offspring. ''' pop1 = pop.clone() pop1.setAncestralDepth(1) pop1.addInfoFields(['ind_id', 'father_id', 'mother_id']) pop1.evolve(initOps=sim.IdTagger(), matingScheme=sim.RandomMating(ops=[ sim.MendelianGenoTransmitter(), penetrance, sim.IdTagger(), sim.PedigreeTagger(), ], numOffspring=2, subPopSize=pop.popSize() * 2), gen=1) sim.stat(pop1, numOfAffected=True) return drawAffectedSibpairSample(pop1, nFamilies)
def __init__(self, locationField='x', varOfLocation=1, vicinity=1, numOffspring=1, sexMode=sim.RANDOM_SEX, ops=sim.MendelianGenoTransmitter(), subPopSize=[], subPops=sim.ALL_AVAIL, weight=0): '''Creates a random mating scheme that selects a parent randomly and another random parent who is in vivinity with him/her, namely with location that is within [x-v, x+v] where x is the location of the first parent, and v is specified by parameter vicinity. For each offspring, its location is set according to a normal distribution with a mean that is the average of parental locations, and a variance varOfLocation. ''' self.field = locationField self.vicinity = vicinity self.varOfLocation = varOfLocation if hasattr(ops, '__iter__'): # if a sequence is given # WithArgs is needed because field name is a variable. allOps = ops + [ sim.PyTagger(sim.WithArgs(self._passLocation, [self.field])) ] else: allOps = [ ops, sim.PyTagger(sim.WithArgs(self._passLocation, [self.field])) ] sim.HomoMating.__init__( self, chooser=sim.PyParentsChooser(self._chooseParents), generator=sim.OffspringGenerator(allOps, numOffspring, sexMode), subPopSize=subPopSize, subPops=subPops, weight=weight)
def prepare_sim(self, params): for view in self._views: for info in view.info_fields: self._info_fields.add(info) nloci = 1 + params['neutral_loci'] pop, init_ops, pre_ops, post_ops = \ self._create_single_pop(params['pop_size'], nloci) view_ops = [] for view in self._views: view.pop = pop view_ops.extend(view.view_ops) for view in self._views: post_ops.append(sp.PyOperator(func=_hook_view, param=view)) post_ops = view_ops + post_ops loci, genome_init = self._create_snp_genome( nloci, freq=params['snp_freq']) sim = sp.Simulator(pop, 1, True) if params['sel_type'] == 'hz_advantage': ms = sp.MapSelector(loci=0, fitness={ (0, 0): 1 - params['sel'], (0, 1): 1, (1, 1): 1 - params['sel']}) elif params['sel_type'] == 'recessive': ms = sp.MapSelector(loci=0, fitness={ (0, 0): 1 - params['sel'], (0, 1): 1 - params['sel'], (1, 1): 1}) else: # dominant ms = sp.MapSelector(loci=0, fitness={ (0, 0): 1 - params['sel'], (0, 1): 1, (1, 1): 1}) return {'sim': sim, 'pop': pop, 'init_ops': init_ops + genome_init, 'pre_ops': pre_ops, 'post_ops': post_ops, 'mating_scheme': sp.RandomMating( ops=[sp.MendelianGenoTransmitter(), ms])}
sim.IdTagger(), sim.PyOutput('Prevalence of disease in each age group:\n'), ], # increase the age of everyone by 1 before mating. preOps=sim.InfoExec('age += 1'), matingScheme=sim.HeteroMating([ # all individuals with age < 75 will be kept. Note that # CloneMating will keep individual sex, affection status and all # information fields (by default). sim.CloneMating(subPops=[(0,0), (0,1), (0,2)], weight=-1), # only individuals with age between 20 and 50 will mate and produce # offspring. The age of offspring will be zero. sim.RandomMating(ops=[ sim.IdTagger(), # give new born an ID sim.PedigreeTagger(), # track parents of each individual sim.MendelianGenoTransmitter(), # transmit genotype ], numOffspring=(sim.UNIFORM_DISTRIBUTION, 1, 3), subPops=[(0,1)]),], subPopSize=demoModel), # number of individuals? postOps=[ sim.PyPenetrance(func=pene, loci=0), sim.PyOperator(func=outputstat, step=20) ], gen = 200 ) # draw two Pedigrees from the last age-structured population from simuPOP import sampling sample = sampling.drawNuclearFamilySample(pop, families=2, numOffspring=(2,3),
# 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())
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 )
def simuAssortativeMatingWithFitness(e): ''' Accepts: e an Experiment object. Returns a dict containing the results from each gen of the simulation: gen generation number. A frequency of the A allele. a frequency of the a allele. AA frequency of AA individuals. Aa frequency of Aa individuals. aa frequency of aa individuals. deaf frequency of deaf individuals (incl adventitious). AA_size size of the AA subpopulation. Aa_size size of the Aa subpopulation. aa_size size of the aa subpopulation. deaf_size size of the deaf subpopulation (incl adventitious). homogamy calculated actual homogamy. F calculated inbreeding coefficient. Adopted from: http://simupop.sourceforge.net/Cookbook/AssortativeMating ''' sim.setRNG(random.seed(sim.getRNG().seed())) pop = sim.Population(e.constant_pop_size * 1000, loci=[1]) # These variables need to be set in order to be available to customChooser(). # There appears to be no way to directly pass variables to customChooser(). pop.dvars().constant_pop_size = e.constant_pop_size pop.dvars().a = e.a pop.dvars().aa_fitness = e.aa_fitness pop.dvars().aa_homogamy = e.aa_homogamy pop.dvars().deaf = e.deaf pop.dvars().adv_deaf_target = int( round((e.deaf - e.a**2) * e.constant_pop_size * 1000)) # These will hold the final data pop.dvars().headers = [] pop.dvars().row = [] pop.evolve( initOps= [sim.InitGenotype(freq=[1-e.a, e.a])], matingScheme = sim.HomoMating( chooser = sim.PyParentsChooser(customChooser), generator = sim.OffspringGenerator(sim.MendelianGenoTransmitter())), postOps = [sim.Stat(alleleFreq=[0], genoFreq=[0]), sim.PyExec(r"headers += ['gen','A', 'a',"\ "'AA', 'Aa', 'aa', 'deaf', 'AA_size', 'Aa_size', " \ "'aa_size', 'deaf_size', 'homogamy', 'F'] \n" \ "F = 1.0-((genoFreq[0][(0,1)]+genoFreq[0][(1,0)])/" # F \ "(2.0*alleleFreq[0][0]*alleleFreq[0][1])) "\ "if alleleFreq[0][0]*alleleFreq[0][1] > 0. "\ "else 0. \n" \ "deaf_size = min(genoNum[0][(1,1)] + adv_deaf_target, constant_pop_size*1000) \n"\ "row += [gen, " # generation \ "alleleFreq[0][0], " # A \ "alleleFreq[0][1], " # a \ "genoFreq[0][(0,0)]," # AA \ "genoFreq[0][(0,1)]+genoFreq[0][(1,0)], " # Aa \ "genoFreq[0][(1,1)], " # aa \ "deaf_size/(constant_pop_size*1000.), " # deaf \ "genoNum[0][(0,0)], " # AA_size \ "genoNum[0][(0,1)]+genoNum[0][(1,0)], " # Aa_size \ "genoNum[0][(1,1)], " # aa_size \ "deaf_size, " # deaf_size \ "homogamy, " # homogamy \ "F if F>0. else 0.]") # F \ ], gen = e.generations ) return {'headers': pop.dvars().headers, 'row': pop.dvars().row}
import random class myParentsChooser: def __init__(self, maleIndexes, femaleIndexes): self.maleIndexes = maleIndexes self.femaleIndexes = femaleIndexes def chooseParents(self): return self.maleIndexes[random.randint(0, len(self.maleIndexes)-1)],\ self.femaleIndexes[random.randint(0, len(self.femaleIndexes)-1)] def parentsChooser(pop, sp): 'How to call a C++ level parents chooser.' # create an object with needed information (such as x, y) ... pc = myParentsChooser( [x for x in range(pop.popSize()) if pop.individual(x).sex() == sim.MALE], [x for x in range(pop.popSize()) if pop.individual(x).sex() == sim.FEMALE]) while True: # return indexes of parents repeatedly yield pc.chooseParents() pop = sim.Population(100, loci=1) simu.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]) ], matingScheme=sim.HomoMating(sim.PyParentsChooser(parentsChooser), sim.OffspringGenerator(ops=sim.MendelianGenoTransmitter())), gen = 100 )
def runSimulation(scenario_id, sub_population_size, minMatingAge, maxMatingAge, gen): ''' sub_population_size A vector giving the population sizes for each sub-population. The subpopulations determine which breeding ground an individual belongs to minMatingAge minimal mating age. maxMatingAge maximal mating age. Individuals older than this are effectively dead years number of years to simulate ''' # scenario_id describes the batch of files to load # The mitochondrial DNA will be in mtdna_<scenario_id> # The SNP DNA will be in snp_<scenario_id> # Read the mitochondrial haplotype frequencies. There's a bit to unpack here # We read the lines into an array, and for each one, call split() on it to get one element per column. # However, we do not want this - we want the transpose, where haplotype_frequencies[0] is a vector of # all the frequencies for population 0, and haplotype_frequencies[1] is the corresponding vector for # population 2. list(map(list, zip(*t))) will achieve this transformation for us. # While we are at it, we also convert the strings into floats. mitochondrial_file = "mtdna_" + scenario_id + ".txt" with open(mitochondrial_file, "r") as fd: haplotype_frequencies = list( map(list, zip(*[list(map(float, line[0:-1].split())) for line in fd]))) if len(haplotype_frequencies) != len(sub_population_size): raise ValueError( 'The number of populations in the population size vector and the number of populations deduced from the haplotype file are different' ) # Now read the SNP data. This builds a 2D array indexed as snp[locus][population] snp_file = "snp_" + scenario_id + ".txt" with open(snp_file, "r") as fd: snp = [list(map(float, line[0:-1].split())) for line in fd] sub_population_count = len(sub_population_size) print() print(sub_population_count, "subpopulations detected") # Now we can create the population. We want to give each population a population name, starting from A sub_population_names = list(map(chr, range(65, 65 + sub_population_count))) # We have two chromosomes. The first is an autosome with nb_loci loci, and the second is the mitochondrial chromosome with 1 locus pop = simuPOP.Population( sub_population_size, ploidy=2, loci=[nb_loci, 1], ancGen=2, infoFields=[ 'age', 'ind_id', 'father_id', 'mother_id', 'nitrogen', 'carbon', 'feeding_ground', 'native_breeding_ground', 'migrate_to' ], subPopNames=sub_population_names, chromTypes=[simuPOP.AUTOSOME, simuPOP.MITOCHONDRIAL]) sub_population_names = tuple(sub_population_names) # Create an attribute on each individual called 'age'. Set it to a random number between 0 and maxMatingAge # Note that size is a vector - the size of each population. We have to sum these to get the total number of individuals individual_count = sum(sub_population_size) # Assign a random age to each individual pop.setIndInfo( [random.randint(0, maxMatingAge) for x in range(individual_count)], 'age') # Assign a random feeding ground to each individual pop.setIndInfo([ random.randint(0, numberOfFeedingGrounds - 1) for x in range(individual_count) ], 'feeding_ground') # Currently we have these virtual subpopulations: # age < minMatingAge (juvenile) # age >= minMatingAge and age < maxMatingAge + 0.1 (age <= maxMatingAge) (mature) # age >= maxMatingAge (dead) # # Ideally we would want something like this: # 1) Immature # 2) Receptive female (every 3 years) # 3) Non-receptive female # 4) Mature male # 5) Dead # # Note that we use a cutoff InfoSplitter here, it is also possible to # provide a list of values, each corresponding to a virtual subpopulation. pop.setVirtualSplitter( simuPOP.CombinedSplitter([ simuPOP.ProductSplitter([ simuPOP.SexSplitter(), simuPOP.InfoSplitter('age', cutoff=[minMatingAge, maxMatingAge + 0.1], names=['juvenile', 'mature', 'dead']) ]) ], vspMap=[[0], [1], [2], [3], [4], [5], [0, 1, 3, 4], [1, 4]], names=[ 'Juvenile Male', 'Mature Male', 'Dead Male', 'Juvenile Female', 'Mature Female', 'Dead Female', 'Not dead yet', 'Active' ])) pop.evolve( initOps=[ simuPOP.InitSex(), simuPOP.IdTagger(), simuPOP.PyOperator(func=init_native_breeding_grounds) ] + [ simuPOP.InitGenotype(subPops=sub_population_names[i], freq=haplotype_frequencies[i], loci=[nb_loci]) for i in range(0, sub_population_count) ] + [ simuPOP.InitGenotype(subPops=sub_population_names[i], freq=[snp[n][i], 1 - snp[n][i]], loci=[n]) for i in range(0, sub_population_count) for n in range(0, nb_loci - 1) ], # increase age by 1 preOps=[simuPOP.InfoExec('age += 1')], matingScheme=simuPOP.HeteroMating( [ # age <= maxAge, copy to the next generation (weight=-1) # subPops is a list of tuples that will participate in mating. The tuple is a pair (subPopulation, virtualSubPopulation) # First, we propagate (clone) all individuals in all subpopulations (and all VSPs except the ones who are now in the VSP of deceased individuals) to the next generation simuPOP.CloneMating( ops=[simuPOP.CloneGenoTransmitter(chroms=[0, 1])], subPops=[ (sub_population, 6) for sub_population in range(0, sub_population_count) ], weight=-1), # Then we simulate random mating only in VSP 1 (ie reproductively mature individuals) within subpopulation (breeding/winter grounds) simuPOP.RandomMating( ops=[ simuPOP.MitochondrialGenoTransmitter(), simuPOP.MendelianGenoTransmitter(), simuPOP.IdTagger(), simuPOP.InheritTagger(mode=simuPOP.MATERNAL, infoFields=['feeding_ground']), simuPOP.InheritTagger( mode=simuPOP.MATERNAL, infoFields=['native_breeding_ground']), simuPOP.PedigreeTagger() ], subPops=[ (sub_population, 7) for sub_population in range(0, sub_population_count) ], weight=1) ], subPopSize=configure_new_population_size), postOps=[ # Determine the isotopic ratios in individuals simuPOP.PyOperator(func=postop_processing), simuPOP.Migrator(mode=simuPOP.BY_IND_INFO), # count the individuals in each virtual subpopulation #simuPOP.Stat(popSize=True, subPops=[(0,0), (0,1), (0,2), (1,0), (1, 1), (1, 2)]), # print virtual subpopulation sizes (there is no individual with age > maxAge after mating) #simuPOP.PyEval(r"'Size of age groups: %s\n' % (','.join(['%d' % x for x in subPopSize]))") # Alternatively, calculate the Fst # FIXME: How does this actually work? Does it work for > 2 populations? I don't really understand it yet # ELC: it is a calculation that partitions variance among and between populations, and can be calculated as a # global statistic or on a pairwise basis. We use it as an indication of genetic differentiation. simuPOP.Stat(structure=range(1), subPops=sub_population_names, suffix='_AB', step=10), simuPOP.PyEval(r"'Fst=%.3f \n' % (F_st_AB)", step=10) ], gen=years) #simuPOP.dump(pop, width=3, loci=[], subPops=[(simuPOP.ALL_AVAIL, simuPOP.ALL_AVAIL)], max=1000, structure=False); #return ped = simuPOP.Pedigree(pop) print("This is the pedigree stuff") simuPOP.dump(pop) # Now sample the individuals sample = drawRandomSample(pop, sizes=[sample_count] * sub_population_count) # Print out the allele frequency data simuPOP.stat(sample, alleleFreq=simuPOP.ALL_AVAIL) frequencies = sample.dvars().alleleFreq with open('freq.txt', 'w') as freqfile: index = 0 for locus in frequencies: if (locus == nb_loci): continue if (len(frequencies[locus]) < 2): continue print(index, end=' ', file=freqfile) index = index + 1 for allele in frequencies[locus]: print(frequencies[locus][allele], end=' ', file=freqfile) print(file=freqfile) # We want to remove monoallelic loci. This means a position in the genotype for which all individuals have the same value in both alleles # To implement this we will build up a list of loci that get ignored when we dump out the file. Generally speaking, if we add all the values up # then either they will sum to 0 (if all individuals have type 0) or to the number of individuals * 2 (if all individuals have type 1) geno_sum = [0] * (nb_loci + 1) * 2 for individual in sample.individuals(): geno_sum = list(map(add, geno_sum, individual.genotype())) final_sum = list( map(add, geno_sum[:(nb_loci + 1)], geno_sum[(nb_loci + 1):])) monoallelic_loci = [] for i in range(0, nb_loci): if final_sum[i] == 0 or final_sum[ i] == sample_count * sub_population_count * 2: monoallelic_loci = [i] + monoallelic_loci monoallelic_loci = sorted(monoallelic_loci, reverse=True) nb_ignored_loci = len(monoallelic_loci) # Generate the two files with open('mixfile.txt', 'w') as mixfile: with open('haploiso.txt', 'w') as haplofile: print(sub_population_count, nb_loci - nb_ignored_loci, 2, 1, file=mixfile) print("sex, haplotype, iso1, iso2, native_ground", file=haplofile) for i in range(0, nb_loci - nb_ignored_loci): print('Loc', i + 1, sep='_', file=mixfile) for individual in sample.individuals(): genotype = individual.genotype() print( 1 if individual.sex() == 1 else 0, genotype[nb_loci], individual.info('carbon'), individual.info('nitrogen'), # int(individual.info('native_breeding_ground')), file=haplofile, sep=' ') print(int(individual.info('native_breeding_ground') + 1), end=' ', file=mixfile) for i in range(0, nb_loci): if i not in monoallelic_loci: print(genotype[i] + 1, genotype[i + nb_loci + 1] + 1, ' ', end='', sep='', file=mixfile) print(file=mixfile) return sample
global counter for line in mutants.split('\n'): # a trailing \n will lead to an empty string if not line: continue (gen, loc, ploidy, a1, a2, id) = line.split('\t') counter[int(loc)] += 1 pop = sim.Population( [5000] * 3, loci=[2, 1, 1], infoFields='ind_id', chromTypes=[sim.AUTOSOME, sim.CHROMOSOME_X, sim.CHROMOSOME_Y]) pop.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5]), sim.IdTagger(), ], preOps=[ sim.KAlleleMutator(rates=[0.001] + [0.01] * 3, loci=range(4), k=100, output=countMutants), ], matingScheme=sim.RandomMating( ops=[sim.IdTagger(), sim.MendelianGenoTransmitter()]), gen=10) print(counter.items())
# 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, 1000], loci=2, infoFields=['father_idx', 'mother_idx']) pop.evolve( initOps=sim.InitSex(), matingScheme=sim.HeteroMating([ sim.RandomMating(numOffspring=2, subPops=0, ops=[sim.MendelianGenoTransmitter(), sim.ParentsTagger()] ), sim.RandomMating(numOffspring=4, subPops=1, ops=[sim.MendelianGenoTransmitter(), sim.ParentsTagger()] ) ]), gen=10 ) [int(ind.father_idx) for ind in pop.individuals(0)][:10] [int(ind.father_idx) for ind in pop.individuals(1)][:10]
sim.InitSex(maleProp=0.5), sim.InitGenotype(freq=[0.2, 0.2, 0.2, 0.2, 0.2], loci=[0, 1, 2]), sim.PedigreeTagger(output='>>simp_Pedigree.ped', outputLoci=[0, 1, 2], outputFields=['gen_id', 'sp_id']) ], #end of initOps preOps=[ PyOperator(lambda pop: [ pop.setIndInfo(x, "sp_id", x) for x in range(pop.numSubPop()) ] is not None), ], matingScheme=sim.MonogamousMating( subPopSize=censuscontrol, numOffspring=8, sexMode=(sim.NUM_OF_MALES, 2), ops=[ sim.InfoExec('gen_id = gen'), sim.MendelianGenoTransmitter(), sim.IdTagger(), sim.InheritTagger(infoFields='sp_id'), sim.PedigreeTagger(output='>>simp_Pedigree.ped', outputLoci=[0, 1, 2], outputFields=['gen_id', 'sp_id']), ], #end of Ops ), #end of matingScheme gen=gen_evolve, ) #end of pop.evolve #Pedigree plus info sent to the monitor print "ind_id, Sire, Dam, Sex, Affection, gen_id, sp_id, Loc1a,Loc1b, Loc2a,Loc2b, Loc3a, Loc3b," print(open('simp_Pedigree.ped').read())
# 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 )