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 simulateSingleLoci(nu0=0.005, T=100, s=0.1, N=1000): print '.', step = 1 pop = sim.Population(size=N, ploidy=2, loci=[1],infoFields=['fitness']);sim.initGenotype(pop, prop=[1-nu0,nu0]);simulator = sim.Simulator(pop.clone(), rep=1); # sim.stat(pop, alleleFreq=[0]); print pop.dvars().alleleFreq[0][1] global a;a = "0;;{}\n".format(nu0) simulator.evolve(initOps=[sim.InitSex()], preOps=sim.MapSelector(loci=0, fitness={(0, 0): 1, (0, 1): 1 + s * 0.5, (1, 1): 1 + s}), matingScheme=sim.RandomMating(), postOps=[sim.Stat(alleleFreq=[0], step=step), sim.PyEval("'%d;;' % (gen+1)", reps=0, step=step, output=fff), sim.PyEval( r"'{}\n'.format(map(lambda x: round(x[1],5),alleleFreq.values())[0])", step=step, output=fff)], gen=T) return pd.DataFrame(zip(*map(lambda x: x.split(';;'), a.strip().split('\n')))).T.set_index(0)[1].astype(float)
def get_mean_r2(Ne, S, n_loci, gens, repeats, n_subpops, initial_frequencies, m): M = get_migration_matrix(m, n_subpops) pop = sim.Population(size=[Ne] * n_subpops, ploidy=2, loci=[1] * n_loci, infoFields='migrate_to') sim.initGenotype(pop, freq=initial_frequencies) pop.evolve( initOps=[sim.InitSex(), sim.InitGenotype(freq=initial_frequencies)], preOps=sim.Migrator(rate=M), matingScheme=sim.RandomMating(), gen=gens) sample_pop = drawRandomSample(pop, sizes=[S] * n_subpops) # get allele frequencies sim.stat(sample_pop, alleleFreq=range(0, n_loci), vars=['alleleFreq_sp']) # calculate r2 values sim.stat(sample_pop, LD=list(combinations(list(range(n_loci)), r=2)), vars=['R2_sp']) r2s = [] for sp in range(n_subpops): 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 = count = 0 for pairs in 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 get_FCs(Ne, S, n_loci, gens, n_subpops, initial_frequencies, m): ''''Runs simulations for allelic fluctuations model with n subpopulations, and returns a list of FC values (one for each subpopulation)''' # population to evolve ((from infinite gamete pool)) popNe = sim.Population(size=[Ne] * n_subpops, ploidy=2, loci=[1] * n_loci, infoFields='migrate_to') # initial sample population (from infinite gamete pool) popS = sim.Population(size=[S] * n_subpops, ploidy=2, loci=[1] * n_loci) sim.initGenotype(popNe, freq=initial_frequencies) sim.initGenotype(popS, freq=initial_frequencies) # get initial sample allele frequencies sim.stat(popS, alleleFreq=range(n_loci), vars=['alleleFreq_sp']) M = get_migration_matrix(m, n_subpops) popNe.evolve(initOps=[sim.InitSex()], preOps=sim.Migrator(rate=M), matingScheme=sim.RandomMating(), gen=gens) sample_pop = drawRandomSample(popNe, sizes=[S] * n_subpops) sim.stat(sample_pop, alleleFreq=range(n_loci), vars=['alleleFreq_sp']) all_FCs = [] for sp in range(n_subpops): initial_allele_frequencies = popS.dvars(sp).alleleFreq final_allele_frequencies = sample_pop.dvars(sp).alleleFreq sp_count = 0 sp_FC = 0 for locus in range(n_loci): init_pair = repair(initial_allele_frequencies[locus]) end_pair = repair(final_allele_frequencies[locus]) if init_pair[0]**2 + init_pair[1]**2 != 1: sp_FC += fc_variant([init_pair[0], init_pair[1]], [end_pair[0], end_pair[1]]) sp_count += 1 all_FCs.append(sp_FC / sp_count) return all_FCs
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)])
# # 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 from simuPOP.utils import viewVars pop = sim.Population([1000, 2000], loci=3) sim.initGenotype(pop, freq=[0.2, 0.4, 0.4], loci=0) sim.initGenotype(pop, freq=[0.2, 0.8], loci=2) sim.stat(pop, genoFreq=[0, 1, 2], haploFreq=[0, 1, 2], alleleFreq=range(3), vars=['genoFreq', 'genoNum', 'haploFreq', 'alleleNum_sp']) viewVars(pop.vars())
# create 3 subpops with different sizes pop = sim.Population(size=[3, 4, 5], ploidy=1, loci=1, infoFields='x') sim.dump(pop) # a diploid population pop = sim.Population(size=[10], ploidy=2, loci=10, infoFields='x') sim.dump(pop) # a tetraploid population pop = sim.Population(size=[10], ploidy=4, loci=10, infoFields='x') sim.dump(pop) # something with frequencies pop = sim.Population(10, ploidy=2, loci=[5]) sim.dump(pop) sim.initGenotype(pop, freq=[0.2, 0.3, 0.5]) sim.dump(pop) # access stuff pop = sim.Population(size=[2, 3], ploidy=2, loci=[5, 10], lociPos=list(range(0, 5)) + list(range(0, 20, 2)), chromNames=['Chr1', 'Chr2'], alleleNames=['A', 'C', 'T', 'G']) pop.ploidy() pop.popSize() pop.alleleName(1) pop.numChrom() pop.chromBegin(1) ind = pop.individual(2) # access individual
# 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', (1, 1): 'BB'
# 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 import random pop = sim.Population(20, loci=1, infoFields='a') pop.setVirtualSplitter(sim.InfoSplitter('a', cutoff=[3])) sim.initGenotype(pop, freq=[0.2, 0.8]) pop.setIndInfo([random.randint(2, 5) for x in range(20)], 'a') sim.infoEval(pop, 'a', subPops=[(0, 0)]);print(' ') sim.infoEval(pop, 'ind.allele(0, 0)', exposeInd='ind');print(' ') # use sim.population variables pop.dvars().b = 5 sim.infoEval(pop, '"%d " % (a+b)');print(' ')
################### # run simulations # ################### for rep in range(1, 501): pop = sim.Population([popsize] * 3, ploidy=2, loci=[20], lociPos=[ 1, 1.1, 2, 3, 3.1, 4, 5, 5.1, 6, 7, 7.1, 8, 9, 9.1, 10, 11, 11.1, 12, 13, 13.1 ], infoFields=['fitness', 'migrate_to']) # initiate genotypes in subpopulation 0 as fixed for '0' ancestry: sim.initGenotype(pop, genotype=[0] * 20, subPops=[0]) # genotypes at subpopulation 1: #sim.initGenotype(pop, genotype=[0]*20+[1]*20, subPops=[1]) # all F1 hybrids sim.initGenotype(pop, genotype=[0] * 20, subPops=[1]) # or a mixture of for ind in random.sample(range(popsize, popsize * 2), popsize / 2): # both parental species. pop.individual(ind).setGenotype([1] * 20) # initiate genotypes in subpopulation 2 as fixed for '1' ancestry sim.initGenotype(pop, genotype=[1] * 20, subPops=[2]) def printAlleleFreq(pop): 'Print allele frequencies of all loci and populations'
# # 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=2000, loci=2) sim.initGenotype(pop, freq=[.2, .8]) sim.mapPenetrance(pop, loci=0, penetrance={(0, 0): 0, (0, 1): .2, (1, 1): .3}) sim.stat(pop, genoFreq=0, numOfAffected=1, vars='genoNum') # number of affected individuals pop.dvars().numOfAffected # which should be roughly (#01 + #10) * 0.2 + #11 * 0.3 (pop.dvars().genoNum[0][(0,1)] + pop.dvars().genoNum[0][(1,0)]) * 0.2 \ + pop.dvars().genoNum[0][(1,1)] * 0.3
def do_forward_sims(sim_data, chrom_len, diploid_Ne, batchname, repilcates, simupop_seed): print("start getting chromosomes positions") chromosome_positions = get_chromosome_positions(sim_data=sim_data, chromsome_length=chrom_len) print("done getting chromosomes positions") haplotypes = get_haplotypes(sim_data) loci_per_chromsome = get_loci_per_chromosome(chromosome_positions) n_chrom = len(loci_per_chromsome) # set up the ancestral pop in simuPOP initial = simuPOP.Population( diploid_Ne, # here is the diploid number loci= loci_per_chromsome, # should be the number of loci on each chromosome lociPos=list(chromosome_positions), ploidy=2, infoFields=['father_idx', 'mother_idx', 'ind_id'], #alleleNames=['A', 'C', 'G', 'T'], lociNames=[ 'locus_{}'.format(x) for x in xrange(len(chromosome_positions)) ]) simuPOP.initGenotype(initial, prop=[1.0 / len(haplotypes)] * len(haplotypes), haplotypes=list(haplotypes)) simuPOP.tagID(initial, reset=1) initial_export = get_export_genotypes(initial, initial.popSize()) np.savetxt('./share/{}/Ne-{}_Chr-{}/Ne-{}_Chr-{}.inital.txt'.format( batchname, diploid_Ne, n_chrom, diploid_Ne, n_chrom), initial_export, delimiter='\t', fmt='%01d') # map-ped simuPOP.utils.export( initial, format='PED', output='./share/{}/Ne-{}_Chr-{}/Ne-{}_Chr-{}.inital.ped'.format( batchname, diploid_Ne, n_chrom, diploid_Ne, n_chrom), gui=False, idField='ind_id') simuPOP.utils.export( initial, format='MAP', output='./share/{}/Ne-{}_Chr-{}/Ne-{}_Chr-{}.inital.map'.format( batchname, diploid_Ne, n_chrom, diploid_Ne, n_chrom), gui=False) # Doesn't yet work!! # set the seed for simuPOP #simuPOP.setRNG(seed=simupop_seed) # and in Python #random.seed(simupop_seed) # and for numpy #np.random.seed(simupop_seed) print("initalizing the forward simulator") simu = simuPOP.Simulator( simuPOP.Population( diploid_Ne, # here is the diploid number loci=get_loci_per_chromosome( chromosome_positions ), # should be the number of loci on each chromosome lociPos=list(chromosome_positions), ploidy=2, infoFields=['ind_id', 'father_idx', 'mother_idx'], #alleleNames=['A', 'C', 'G', 'T'], lociNames=[ 'locus_{}'.format(x) for x in xrange(len(chromosome_positions)) ]), rep=repilcates) print("Start evolving {} replicates".format(repilcates)) simu.evolve( initOps=[ simuPOP.InitSex( sex=[simuPOP.MALE, simuPOP.FEMALE]), # alternate sex simuPOP.InitGenotype(prop=[1.0 / len(haplotypes)] * len(haplotypes), haplotypes=list(haplotypes)) ], matingScheme=simuPOP.HomoMating( chooser=simuPOP.PyParentsChooser(fixedChooser), generator=simuPOP.OffspringGenerator( sexMode=(simuPOP.GLOBAL_SEQUENCE_OF_SEX, simuPOP.MALE, simuPOP.FEMALE), ops=[ simuPOP.Recombinator(intensity=1.0 / chrom_len), simuPOP.ParentsTagger() ]), ), postOps=[], gen=20) print("Done evolving {} replicates!".format(repilcates)) # export the data print("Exporting data!".format(repilcates)) for rep, pop in enumerate(simu.populations()): if diploid_Ne >= 200: pop_genotypes = get_export_genotypes(pop, n_ind=200) # select 200 inds else: pop_genotypes = get_export_genotypes( pop, n_ind=diploid_Ne) # select 200 inds np.savetxt('./share/{}/Ne-{}_Chr-{}/Ne-{}_Chr-{}_Frep-{}.geno'.format( batchname, diploid_Ne, n_chrom, diploid_Ne, n_chrom, rep).format(rep), pop_genotypes, delimiter='\t', fmt='%01d') if rep % 10 == 0: print "saved rep {}".format(rep)
return (1 / 2) * (xi_1 - yi_1)**2 / ( (xi_1 + yi_1) / 2 - xi_1 * yi_1) + (1 / 2) * (xi_2 - yi_2)**2 / ( (xi_2 + yi_2) / 2 - xi_2 * yi_2) ########################## Ne_ests = [] FCs = [] for r in range(repeats): print(r + 1) popNe = sim.Population(size=[Ne], ploidy=2, loci=[1] * n_loci) popS = sim.Population(size=[S], ploidy=2, loci=[1] * n_loci) sim.initGenotype(popNe, freq=initial_frequencies) sim.initGenotype(popS, freq=initial_frequencies) sim.stat(popS, alleleFreq=range(0, n_loci), vars=['alleleFreq']) initial_allele_freqs = popS.vars()['alleleFreq'] popNe.evolve(initOps=[sim.InitSex()], matingScheme=sim.RandomMating(), gen=gens) sample_pop = drawRandomSample(popNe, sizes=S) sim.stat(sample_pop, alleleFreq=range(0, n_loci), vars=['alleleFreq']) meanFC_repeat = 0 countFC = 0
''' Created on Oct 6, 2010 @author: Gaurav Singhal ''' import simuPOP a import simuPOP as sim pop = sim.Population(size=1000, loci=[2]) pop.evolve(initOps = [sim.InitSex(),sim.initGenotype(pop, genotype=[1, 2, 2, 1])], matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=0.01)), postOps = [sim.stat(pop, LD=[0, 1]),sim.PyEval(r"'%.2f\n' % LD[0][1]", step=10),],gen=100)
# # 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( 100, loci=[1, 1, 1], lociNames=['A', 'X', 'Y'], chromTypes=[sim.AUTOSOME, sim.CHROMOSOME_X, sim.CHROMOSOME_Y]) sim.initGenotype(pop, freq=[0.01, 0.05, 0.94]) sim.stat(pop, genoFreq=['A', 'X']) # both loci indexes and names can be used. print('Available genotypes on autosome:', list(pop.dvars().genoFreq[0].keys())) for i in range(3): for j in range(3): print('%d-%d: %.3f' % (i, j, pop.dvars().genoFreq[0][(i, j)])) print('Genotype frequency on chromosome X:\n', \ '\n'.join(['%s: %.3f' % (x,y) for x,y in pop.dvars().genoFreq[1].items()]))
import simuPOP as sim def calcNe(pop, param): 'Calculated effective number of disease alleles at specified loci (param)' sim.stat(pop, alleleFreq=param) ne = {} for loc in param: freq = pop.dvars().alleleFreq[loc] sumFreq = 1 - pop.dvars().alleleFreq[loc][0] if sumFreq == 0: ne[loc] = 0 else: ne[loc] = 1. / sum([(freq[x]/sumFreq)**2 \ for x in list(freq.keys()) if x != 0]) # save the result to the sim.Population. pop.dvars().ne = ne return True pop = sim.Population(1000, loci=2) sim.initGenotype(pop, freq=[0.9] + [0.01] * 10, loci=0) sim.initGenotype(pop, freq=[0.9] + [0.05] + [0.005] * 10, loci=1) calcNe(pop, param=[0, 1]) print(pop.dvars().ne)
def __init__(self, loci, *args, **kwargs): self.loci = loci sim.PyOperator.__init__(self, func=self.calcNe, *args, **kwargs) # def calcNe(self, pop): sim.stat(pop, alleleFreq=self.loci) ne = {} for loc in self.loci: freq = pop.dvars().alleleFreq[loc] sumFreq = 1 - pop.dvars().alleleFreq[loc][0] if sumFreq == 0: ne[loc] = 0 else: ne[loc] = 1. / sum([(freq[x] / sumFreq)**2 for x in list(freq.keys()) if x != 0]) # save the result to the sim.Population. pop.dvars().ne = ne return True def Ne(pop, loci): '''Function form of operator ne''' ne(loci).apply(pop) return pop.dvars().ne pop = sim.Population(100, loci=[10]) sim.initGenotype(pop, freq=[.2] * 5) print(Ne(pop, loci=[2, 4]))
# (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 import random pop = sim.Population(10, loci=[2, 3], infoFields='Sex') sim.initSex(pop) pop.setVirtualSplitter(sim.SexSplitter()) # initialize male and females with different genotypes. sim.initGenotype(pop, genotype=[0]*5, subPops=[(0, 0)]) sim.initGenotype(pop, genotype=[1]*5, subPops=[(0, 1)]) # set Sex information field to 0 for all males, and 1 for all females pop.setIndInfo([sim.MALE], 'Sex', [0, 0]) pop.setIndInfo([sim.FEMALE], 'Sex', [0, 1]) # Print individual genotypes, followed by values at information field Sex sim.dump(pop, structure=False)
# # 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([1000]*2, loci=3) sim.initGenotype(pop, freq=[0.2, 0.8], subPops=0) sim.initGenotype(pop, freq=[0.8, 0.2], subPops=1) sim.stat(pop, LD=[[0, 1, 0, 0], [1, 2]], vars=['LD', 'LD_prime', 'R2', 'LD_ChiSq', 'LD_ChiSq_p', 'CramerV', 'LD_prime_sp', 'LD_ChiSq_p_sp']) from pprint import pprint pprint(pop.vars())
# 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(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)
# the user's guide (http://simupop.sourceforge.net/manual) for a detailed # 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)
# # 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 pprint import pprint pop = sim.Population(100, loci=2) sim.initGenotype(pop, freq=[0.3, 0.7]) print(pop.vars()) # No variable now pop.dvars().myVar = 21 print(pop.vars()) sim.stat(pop, popSize=1, alleleFreq=0) # pprint prints in a less messy format pprint(pop.vars()) # print number of allele 1 at locus 0 print(pop.vars()['alleleNum'][0][1]) # use the dvars() function to access dictionary keys as attributes print(pop.dvars().alleleNum[0][1]) print(pop.dvars().alleleFreq[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(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)