def evolve(pop, r=0): sim.dump(pop) pop.evolve( initOps=[sim.InitSex()], matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=0.01)), postOps=[sim.stat(pop, alleleFreq=range(24), step=10), sim.PyEval(r"alleleFreq[0]", step=10)], gen=50, )
def getPop(path): init_haps = np.loadtxt(path + "example_data/init_haps.txt") # 5 x 24 x 3 array of derived allele frequencies positions = np.loadtxt(path + "example_data/positions.txt", dtype=int) initHaps = [list(h) for h in init_haps] print "initHap AF:\n", init_haps.mean(0) pos = list(positions) n = 5 pop = Simulation.createInitDiploid(initHaps, pos, n) sim.dump(pop, max=10) return pop
# 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()) # remove virtual subpopulation (people with age between 20 and 60) pop.removeSubPops([(0, 1)]) print(pop.subPopSizes()) # extract another virtual subpopulation (people with age greater than 60) pop1 = pop.extractSubPops([(0, 2)]) sim.dump(pop1, structure=False, max=10)
# # This file is part of simuPOP, a forward-time population genetics # simulation environment. Please visit http://simupop.sourceforge.net # 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 pop = sim.Population(size=[2, 5], ploidy=sim.HAPLODIPLOID, loci=[3, 5]) sim.initGenotype(pop, freq=[0.3, 0.7]) sim.dump(pop)
# 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(4, loci=1, infoFields=['ind_id', 'father_id', 'mother_id'], ancGen=-1) pop.evolve(initOps=[ sim.InitSex(), sim.IdTagger(), sim.InitGenotype(freq=[0.5, 0.5]), sim.PedigreeTagger(output='>>pedigree.ped', outputLoci=0) ], matingScheme=sim.RandomMating(ops=[ sim.MendelianGenoTransmitter(), sim.IdTagger(), sim.PedigreeTagger(output='>>pedigree.ped', outputLoci=0) ], ), gen=2) # print(open('pedigree.ped').read()) pop.asPedigree() pop.save('pedigree1.ped', loci=0) print(open('pedigree1.ped').read()) # ped = sim.loadPedigree('pedigree1.ped') sim.dump(ped, ancGens=range(3))
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
simuOpt.setOptions(debug='DBG_WARNING') import simuPOP as sim pop = sim.Population(10, loci=3, chromNames=['chr1']) # 1 1 1, pop.setGenotype([1]) # 1 1 1, 0 0 0 pop.addChrom(lociPos=[0.5, 1, 2], lociNames=['rs1', 'rs2', 'rs3'], chromName='chr2') pop1 = sim.Population(10, loci=3, chromNames=['chr3'], lociNames=['rs4', 'rs5', 'rs6']) # 2 2 2, pop1.setGenotype([2]) # 1 1 1, 0 0 0, 2 2 2 pop.addChromFrom(pop1) # 1 1 1, 0 0 0, 2 0 2 2 0 pop.addLoci(chrom=[2, 2], pos=[1.5, 3.5], lociNames=['rs7', 'rs8']) # 1 1 1, 0 0 0, 2 0 2 0 pop.removeLoci(8) # loci names can also be used. pop.removeLoci(['rs1', 'rs7']) sim.dump(pop) # add loci from another population pop2 = sim.Population(10, loci=2, lociPos=[0.1, 2.2], chromNames='chr3') pop.addLociFrom(pop2) pop.addLociFrom(pop2, byName=2) sim.dump(pop, genotype=False)
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), affectedParents=(1,2), affectedOffspring=(1,3)) sim.dump(sample)
# description of this example. # import simuPOP as sim pop = sim.Population(size=[2000, 3000], loci=[5, 7]) # by allele frequency def printFreq(pop, loci): sim.stat(pop, alleleFreq=loci) print(', '.join( ['{:.3f}'.format(pop.dvars().alleleFreq[x][0]) for x in loci])) sim.initGenotype(pop, freq=[.4, .6]) sim.dump(pop, max=6, structure=False) printFreq(pop, range(5)) # by proportion sim.initGenotype(pop, prop=[0.4, 0.6]) printFreq(pop, range(5)) # by haplotype frequency sim.initGenotype(pop, freq=[.4, .6], haplotypes=[[1, 1, 0, 1], [0, 0, 1]]) sim.dump(pop, max=6, structure=False) printFreq(pop, range(5)) # by haplotype proportion sim.initGenotype(pop, prop=[0.4, 0.6], haplotypes=[[1, 1, 0], [0, 0, 1, 1]]) printFreq(pop, range(5)) # by genotype pop = sim.Population(size=[2, 3], loci=[5, 7]) sim.initGenotype(pop, genotype=[1] * 5 + [2] * 7 + [3] * 5 + [4] * 7) sim.dump(pop, structure=False)
# 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 pop = sim.Population(size=6, ploidy=2, loci=[3, 3, 3, 2, 2, 4, 4], chromTypes=[sim.AUTOSOME]*2 + [sim.CHROMOSOME_X, sim.CHROMOSOME_Y, sim.MITOCHONDRIAL] + [sim.CUSTOMIZED]*2) sim.initGenotype(pop, freq=[0.3, 0.7]) sim.dump(pop, structure=False) # does not display genotypic structure information
# 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=[10, 10], loci=[20, 30], infoFields='gen', ancGen=-1) sim.initSex(pop) pop.setVirtualSplitter(sim.SexSplitter()) pop1 = pop.clone() sim.initGenotype(pop, freq=[0]*20 + [0.1]*10) pop.setIndInfo(1, 'gen') sim.initGenotype(pop1, freq=[0]*50 + [0.1]*10) pop1.setIndInfo(2, 'gen') pop.push(pop1) sim.dump(pop, width=3, loci=[5, 6, 30], subPops=([0, 0], [1, 1]), max=10, structure=False) # list all male individuals in all subpopulations sim.dump(pop, width=3, loci=[5, 6, 30], subPops=[(sim.ALL_AVAIL, 0)], max=10, structure=False)
initOps=[ sim.InitSex(), # random assign age sim.InitInfo(lambda: randint(0, 74), infoFields='age'), 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([ # 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)]) ]), gen = 200 ) from simuPOP import sampling sample = sampling.drawNuclearFamilySample(pop, families=1, numOffspring=(2,3)) sim.dump(sample, structure=False)
# 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 pop = sim.Population(20, loci=8) # every chromosomes are different. :-) for idx, ind in enumerate(pop.individuals()): ind.setGenotype([idx*2], 0) ind.setGenotype([idx*2+1], 1) pop.evolve( matingScheme=sim.SelfMating(ops=sim.Recombinator(rates=0.01)), gen = 1 ) sim.dump(pop, width=3, structure=False, max=10)
# # 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)
sim.DiscardIf(natural_death), sim.InfoExec("age += 1"), sim.PySelector(loci=[0], func=fitness_func) ], matingScheme = sim.HeteroMating( [ sim.CloneMating(subPops = [(0,0), (0,1), (0,2)], weight = -1), sim.RandomMating( ops = [ sim.IdTagger(), sim.PedigreeTagger(), sim.InfoExec("smurf = 0.0"), sim.MendelianGenoTransmitter(), sim.PyQuanTrait(loci = sim.ALL_AVAIL, func = MaleEffect, infoFields = ['a', 'b', 't0']) ], weight = 1, subPops = [(0,1)], numOffspring = 1 ) ], subPopSize = demo ), postOps = [ sim.PyOperator(func=OutputStats, step=100) ], gen=args.G ) pop = simu.extract(0) sim.dump(pop, max=200)