def get_mean_r2(Ne, S, n_loci, gens, n_subpops, initial_frequencies, m): """Returns the mean r2 value for each subpopulation, in list of length n_subpops""" # make pairwise migration matrix M = get_migration_matrix(m, n_subpops) # initialise population n_alleles = 2 pop = sim.Population(size=[Ne] * n_subpops, ploidy=2, loci=[1] * n_loci, alleleNames=[str(i) for i in range(n_alleles)], infoFields='migrate_to') sim.initGenotype(pop, freq=[initial_frequencies, 1 - initial_frequencies]) #sim.initGenotype(pop, freq = [1/n_alleles for i in range(n_alleles)]) sim.initSex(pop) print(M) # run burn in generations pop.evolve(initOps=[], preOps=sim.Migrator(M, mode=sim.BY_PROBABILITY), matingScheme=sim.RandomMating(), gen=gens) # take sample from each subpopulation sample_pop = drawRandomSample(pop, sizes=[S] + [0] * (n_subpops - 1)) #sim.dump(sample_pop) # get allele frequencies sim.stat(sample_pop, alleleFreq=range(0, n_loci), vars=['alleleFreq_sp']) #print(sample_pop.dvars(0).alleleFreq) # calculate r2 values sim.stat(sample_pop, LD=list(itertools.combinations(list(range(n_loci)), r=2)), vars=['R2_sp']) #print(sample_pop.dvars(0).R2) r2s = [] for sp in [0]: #range(n_subpops*0): allele_freqs = sample_pop.dvars(sp).alleleFreq seg_alleles = [ k for k in range(n_loci) if np.abs(.5 - allele_freqs[k][0]) < .5 - 0.05 ] if len(seg_alleles) < 2: raise Exception("<2 segregating alleles") r2_sum = 0 count = 0 for pairs in itertools.combinations(seg_alleles, r=2): r2_sum += sample_pop.dvars(sp).R2[pairs[0]][pairs[1]] count += 1 mean_r2 = r2_sum / count r2s.append(mean_r2) return r2s
def setUp(self): self.fs = 0.9 self.ms = 0.5 self.pop = sim.Population(size=[4, 4], loci=[1, 1, 1, 1], chromTypes=[sim.AUTOSOME, sim.CHROMOSOME_X, sim.CHROMOSOME_Y, sim.CUSTOMIZED], infoFields=['fitness']) self.pop.setVirtualSplitter(sim.SexSplitter()) sim.initSex(self.pop, sex=[sim.MALE, sim.FEMALE]) for i in range(2): for j in range(2): sim.initGenotype(self.pop, loci=[0], # Autosome prop=[0.5, 0.5], subPops=[(i,j)]) sim.initGenotype(self.pop, loci=[3], # Mitochondria ploidy=0, prop=[0.5, 0.5], subPops=[(i,j)]) sim.initGenotype(self.pop, loci=[1], # Chromosome X prop=[0.5, 0.5], subPops=[(i,1)]) sim.initGenotype(self.pop, loci=[1], # Chromosome X prop=[0.5, 0.5], ploidy=0, subPops=[(i,0)]) sim.initGenotype(self.pop, loci=[2], # Chromosome Y prop=[0.5, 0.5], ploidy=1, subPops=[(i,0)])
# 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)
pop = simuPOP.population(size=[n_ind] * n_pop, ploidy=n_ploidy, loci=[n_loci], lociPos=[15, 25], chromNames=['chr3', 'chr5'], chromTypes=[simuPOP.Autosome], alleleNames=["A", "T"], infoFields='migrate_to') simu = simuPOP.simulator(pop, simuPOP.randomMating()) myInitOps, myPreOps, myPostOps = [], [], [] #iniate sex & frequency myInitOps += [ simuPOP.initSex(sex=[simuPOP.Male, simuPOP.Female]), simuPOP.initByFreq(loci=[0], alleleFreq=init_freq_2) ] #parameter transfer myInitOps += [ simuPOP.pyExec("traj_gen=[0] * n_pop"), simuPOP.pyExec("traj_pop=range(n_pop)"), simuPOP.pyExec("traj_af=init_freq"), simuPOP.pyExec("traj_popsize=[n_ind] * n_pop") ] myPostOps += [ simuPOP.stat(alleleFreq=[0], subPops=range(n_pop), vars=['alleleFreq_sp']), simuPOP.pyExec("traj_gen += [gen+1] * n_pop"), simuPOP.pyExec("traj_pop += range(n_pop)"), simuPOP.pyExec( "traj_af += [subPop[x]['alleleFreq'][0][0] for x in range(n_pop)]"),
# 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())
# # 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=[1000, 1000]) sim.initSex(pop, maleFreq=0.3, subPops=0) sim.initSex(pop, sex=[sim.MALE, sim.FEMALE, sim.FEMALE], subPops=1) sim.stat(pop, numOfMales=True, vars='numOfMales_sp') print(pop.dvars(0).numOfMales) print(pop.dvars(1).numOfMales)