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 replicate_tuson_drift_simulation(self, pop, meta_population, meta_sample_size, recombination_rates): for replicate in pop.populations(): replicate.dvars().gen = 0 female_chooser = sim.RandomParentChooser( selectionField='female_fitness') male_chooser = sim.RandomParentChooser(selectionField='male_fitness') print("Beginning simulation of genetic drift with parameters:") breeding_params = { 'n_breeding_females': self.number_of_breeding_females, 'n_breeding_males': self.number_of_breeding_males, 'sample_sizes': meta_sample_size} print("Breeding females: {n_breeding_females}, breeding males: " "{n_breeding_males}, " "sample sizes: {sample_sizes}".format(**breeding_params)) return pop.evolve( initOps=[ operators.ReplicateMetaPopulation(meta_population, meta_sample_size), sim.PyEval( r'"Initial: Sampled %d individuals from generation %d Replicate: %d.\n" % (ss, gen_sampled_from, rep)'), ], preOps=[ sim.PyEval(r'"Generation: %d\n" % gen'), sim.InfoExec('generation=gen'), operators.ReplicateMetaPopulation(meta_population, meta_sample_size, at=[2, 4, 6, 8]), # Evaluation specifying the generations should be the same as the evaluation at every generation. sim.PyEval( r'"Sampled %d individuals from generation %d from replicate: %d.\n" % (ss, gen_sampled_from, rep)', at=[2, 4, 6, 8]), operators.RandomlyAssignFemaleFitness( self.number_of_breeding_females), operators.RandomlyAssignMaleFitness( self.number_of_breeding_males), ], matingScheme=sim.HomoMating( sim.CombinedParentsChooser(female_chooser, male_chooser, allowSelfing=True), sim.OffspringGenerator(ops=[ sim.ParentsTagger(), sim.IdTagger(), sim.PedigreeTagger(), sim.Recombinator(rates=recombination_rates)], ), ), finalOps=[ sim.InfoExec('generation=gen'), operators.ReplicateMetaPopulation(meta_population, meta_sample_size), sim.PyEval( r'"Final: Sampled %d individuals from generation %d\n" % (ss, gen_sampled_from)') ], gen=self.number_of_generations)
def recombinatorial_convergence(self, pop, recombination_rates): """ Implements the MAGIC breeding scheme of breeding single individuals in pairs determined by the offspring of the initial population. The initial population is given by generate_f_one. :param pop: :type pop: :param recombination_rates: :type recombination_rates: :return: :rtype: """ while pop.numSubPop() > 1: new_parents = list(pop.indInfo('ind_id')) new_parent_id_pairs = [(pid, pid + 1) for pid in new_parents[::2]] if len(new_parent_id_pairs) % 2 != 0 and \ len(new_parent_id_pairs) != 1: new_parent_id_pairs.append(random.choice(new_parent_id_pairs)) new_os_size = len(new_parent_id_pairs) new_founder_chooser = breed.PairwiseIDChooser(new_parent_id_pairs) pop.evolve( preOps=[ sim.PyEval(r'"Generation: %d\t" % gen', ), sim.Stat(popSize=True, numOfMales=True), sim.PyEval(r'"popSize: %d\n" % popSize', ), ], matingScheme=sim.HomoMating( sim.PyParentsChooser(new_founder_chooser.by_id_pairs), sim.OffspringGenerator(ops=[ sim.IdTagger(), sim.ParentsTagger(), sim.PedigreeTagger(), sim.Recombinator(rates=recombination_rates) ], numOffspring=1), subPopSize=new_os_size, ), gen=1, )
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 )
# 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(100, loci=5 * 3, infoFields='parent_idx') pop.evolve(initOps=[sim.InitGenotype(freq=[0.2] * 5)], preOps=sim.Dumper(structure=False, max=5), matingScheme=sim.HomoMating( sim.SequentialParentChooser(), sim.OffspringGenerator(ops=[ sim.SelfingGenoTransmitter(), sim.ParentsTagger(infoFields='parent_idx'), ])), postOps=sim.Dumper(structure=False, max=5), gen=1)
# 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]
# 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, infoFields=['father_idx', 'mother_idx']) pop.evolve(initOps=sim.InitSex(sex=(sim.MALE, sim.FEMALE)), matingScheme=sim.MonogamousMating( numOffspring=2, sexMode=(sim.NUM_OF_MALES, 1), ops=[ sim.MendelianGenoTransmitter(), sim.ParentsTagger(), ], ), gen=5) [ind.sex() for ind in pop.individuals()] [int(ind.father_idx) for ind in pop.individuals()] [int(ind.mother_idx) for ind in pop.individuals()] # count the number of distinct parents len(set(pop.indInfo('father_idx'))) len(set(pop.indInfo('mother_idx')))
# 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(10, ploidy=sim.HAPLODIPLOID, loci=[5, 5], infoFields=['father_idx', 'mother_idx']) pop.setVirtualSplitter(sim.SexSplitter()) pop.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(genotype=[0]*10, subPops=[(0, 'Male')]), sim.InitGenotype(genotype=[1]*10+[2]*10, subPops=[(0, 'Female')]) ], preOps=sim.Dumper(structure=False), matingScheme=sim.HaplodiploidMating( ops=[sim.HaplodiploidGenoTransmitter(), sim.ParentsTagger()]), postOps=sim.Dumper(structure=False), gen = 1 )
# 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(10, loci=[20], ancGen=1, infoFields=['father_idx', 'mother_idx']) pop.evolve( initOps=[sim.InitSex(), sim.InitGenotype(genotype=[0] * 20 + [1] * 20)], matingScheme=sim.RandomMating( ops=[sim.Recombinator( rates=0.01), sim.ParentsTagger()]), gen=1) pop.indInfo('mother_idx') # mother of all offspring ind = pop.individual(0) mom = pop.ancestor(ind.mother_idx, 1) print(ind.genotype(0)) print(mom.genotype(0)) print(mom.genotype(1))
def test_generate_operating_population(): genetic_map = pd.read_csv('nam_prefounders_genetic_map.txt', index_col=None, sep='\t') pf_map = shelve.open('pf_map') misc_gmap = shelve.open('misc_gmap') uniparams = shelve.open('uniparams') locus_names = uniparams['locus_names'] pos_column = uniparams['pos_column'] allele_names = uniparams['allele_names'] snp_to_integer = uniparams['snp_to_integer'] integer_to_snp = uniparams['integer_to_snp'] alleles = misc_gmap['alleles'] chr_cM_positions = misc_gmap['chr_cM_positions'] cM_positions = misc_gmap['cM_positions'] integral_valued_loci = misc_gmap['integral_valued_loci'] relative_integral_valued_loci = misc_gmap['relative_integral_valued_loci'] recombination_rates = misc_gmap['recombination_rates'] nam = sim.loadPopulation(uniparams['prefounder_file_name']) sim.tagID(nam, reset=True) nam.setSubPopName('maize_nam_prefounders', 0) selection_statistics = { 'aggregate': {}, 'selected': {}, 'non-selected': {} } ind_names_for_gwas = {i: {} for i in range(uniparams[ 'number_of_replicates'])} uniparams['meta_pop_sample_sizes'] = {i: 100 for i in range(0, uniparams['generations_of_selection'] + 1, 2) } s = simulate.Truncation(uniparams['generations_of_selection'], uniparams['generations_of_random_mating'], uniparams['operating_population_size'], uniparams[ 'proportion_of_individuals_saved'], uniparams['overshoot_as_proportion'], uniparams['individuals_per_breeding_subpop'], uniparams['heritability'], uniparams['meta_pop_sample_sizes'], uniparams['number_of_replicates']) ind_names_for_gwas = {i: {} for i in range(uniparams[ 'number_of_replicates'])} founders = uniparams['founders'] replicated_nam = sim.Simulator(nam, rep=2, stealPops=False) pop = replicated_nam.extract(0) assert pop.popSize() == 26, "Population is too large." s.generate_f_one(pop, recombination_rates, founders, 100) assert pop.popSize() == 400, "Population should have size: {} after the F_1 mating " \ "procedure." \ "".format(len(founders) * 100) #pop.splitSubPop(0, [100] * 4) #subpop_list = list(range(pop.numSubPop())) intmd_os_struct = s.restructure_offspring(pop, 100, 4) snd_order = breed.SecondOrderPairIDChooser(intmd_os_struct, 1) pop.evolve( preOps=[sim.MergeSubPops()], matingScheme=sim.HomoMating( sim.PyParentsChooser(snd_order.snd_ord_id_pairs), sim.OffspringGenerator(ops=[ sim.IdTagger(), sim.ParentsTagger(), sim.PedigreeTagger(), sim.Recombinator(rates=recombination_rates) ], numOffspring=1), subPopSize=[200], ), gen=1, ) assert pop.popSize() == 1, "Population does not have correct size after second round of mating." second_intmd_os_struct = s.restructure_offspring(pop, 100, 2) third_order = breed.SecondOrderPairIDChooser(second_intmd_os_struct, 1) pop.evolve( preOps=[sim.MergeSubPops()], matingScheme=sim.HomoMating( sim.PyParentsChooser(third_order.snd_ord_id_pairs), sim.OffspringGenerator(ops=[ sim.IdTagger(), sim.ParentsTagger(), sim.PedigreeTagger(), sim.Recombinator(rates=recombination_rates) ], numOffspring=1), subPopSize=[100], ), gen=1, ) assert pop.popSize() == 100, "Second merge of breeding sub-populations. Offspring population does not have " \ "correct size"
# 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], loci=2, infoFields=['father_idx', 'mother_idx']) pop.setVirtualSplitter(sim.ProportionSplitter([0.2, 0.8])) pop.evolve( initOps=sim.InitSex(), matingScheme=sim.HeteroMating(matingSchemes=[ sim.SelfMating(subPops=[(0, 0)], ops=[sim.SelfingGenoTransmitter(), sim.ParentsTagger()]), sim.RandomMating( subPops=[(0, 1)], ops=[sim.SelfingGenoTransmitter(), sim.ParentsTagger()]) ]), gen=10) [int(ind.father_idx) for ind in pop.individuals(0)][:15] [int(ind.mother_idx) for ind in pop.individuals(0)][:15]
# 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(100, infoFields=['father_idx', 'mother_idx']) pop.evolve( initOps=sim.InitSex(), matingScheme=sim.PolygamousMating(polySex=sim.MALE, polyNum=2, ops=[sim.ParentsTagger(), sim.MendelianGenoTransmitter()], ), gen = 5 ) [int(ind.father_idx) for ind in pop.individuals()][:20] [int(ind.mother_idx) for ind in pop.individuals()][:20]
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)