def setup(parser): config = parser.parse_args() sim_id = uuid.uuid1().urn script = __file__ if config.debug == '1': log.basicConfig(level=log.DEBUG, format='%(asctime)s %(levelname)s: %(message)s') else: log.basicConfig(level=log.INFO, format='%(asctime)s %(levelname)s: %(message)s') data.set_experiment_name(config.experiment) data.set_database_hostname(config.dbhost) data.set_database_port(config.dbport) dbconfig = data.getMingConfiguration(data.modules) ming.configure(**dbconfig) # set up parallelism. At the moment, this doesn't do anything on OSX # but should provide parallelism on Linux across the replicates at least if config.cores is not None: cores = config.cores else: cores = utils.get_parallel_cores(config.devel) log.info("Setting up %s cores for parallel simulation", cores) import simuOpt if(config.debug == 1): simuOpt.setOptions(alleleType='long',optimized=True,quiet=False,numThreads = cores) else: simuOpt.setOptions(alleleType='long',optimized=True,quiet=False,numThreads = cores) return (config,sim_id,script, cores)
def rep_case_control(rep_opt,logger=None): """Given the gridding scheme options, create simuPOP files in the given subdirectories.""" simuOpt.setOptions(alleleType='binary', optimized=True) from simuPOP import * cc_opt = simuOpt.Params(singleGeneModel.options) for maf in xrange(len(rep_opt.MAFs)): for maf_rep in xrange(rep_opt.MAF_replicates): maf_dir = "MAF_%.2f_%d" %(rep_opt.MAFs[maf],maf_rep+1) cc_opt.expandPop = "%s/%s.pop" % (maf_dir,maf_dir) cc_opt.pop = loadPopulation(cc_opt.expandPop) logger.info("Expanded population %s loaded!" %cc_opt.expandPop) for cc in xrange(len(rep_opt.CCs)): for cc_rep in xrange(rep_opt.CC_replicates): cc_dir = "cc_%d_%d" % (rep_opt.CCs[cc],cc_rep+1) for wtr in xrange(len(rep_opt.WTRs)): for wtr_rep in xrange(rep_opt.WTR_replicates): wtr_dir = "WTR_%.2f_%d" % (rep_opt.WTRs[wtr],wtr_rep+1) for grr in xrange(len(rep_opt.GRRs)): for grr_rep in xrange(rep_opt.GRR_replicates): grr_dir = "GRR_%.2f_%d" % (rep_opt.GRRs[grr],grr_rep+1) dirpath = path_maker(maf_dir,cc_dir,wtr_dir,grr_dir) cc_opt.wtr = rep_opt.WTRs[wtr] cc_opt.GRR = rep_opt.GRRs[grr] cc_opt.numControls= rep_opt.CCs[cc] cc_opt.sampledPop = os.path.join(dirpath,"sample.pop") pop = singleGeneModel.penetrance(cc_opt,logger) case_control_dataset.save(pars.sampledPop) logger.info("Sampled population saved at %s" %pars.sampledPop)
def setup(parser): config = parser.parse_args() sim_id = uuid.uuid1().urn script = __file__ if config.debug == '1': log.basicConfig(level=log.DEBUG, format='%(asctime)s %(levelname)s: %(message)s') else: log.basicConfig(level=log.INFO, format='%(asctime)s %(levelname)s: %(message)s') # set up parallelism. At the moment, this doesn't do anything on OSX # but should provide parallelism on Linux across the replicates at least if config.cores is not None: cores = config.cores else: cores = int(utils.get_parallel_cores(config.devel)) log.info("Setting up %s cores for parallel simulation", cores) import simuOpt if (config.debug == 1): simuOpt.setOptions(alleleType='long', optimized=True, quiet=False, numThreads=cores) else: simuOpt.setOptions(alleleType='long', optimized=True, quiet=False, numThreads=cores) return (config, sim_id, script, cores)
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #!/usr/bin/env python # import simuOpt, os, sys, json # use 'binary module' for representing long sequences simuOpt.setOptions(optimized=True, alleleType="binary") import simuPOP as sim import random from itertools import izip from itertools import islice from pprint import pprint from bisect import bisect from optparse import OptionParser import my_timer # equal fitness function def eq_fit(): return 1
#!/usr/bin/env python # Copyright (c) 2013. Mark E. Madsen <*****@*****.**> # # This work is licensed under the terms of the Apache Software License, Version 2.0. See the file LICENSE for details. from __future__ import print_function import simuOpt, sys simuOpt.setOptions(alleleType='long',optimized=False,quiet=True) import simuPOP as sim import uuid import ctpy.data as data import ctpy.utils as utils import ctpy.math as cpm import ming import itertools import logging as log import argparse """ This program simulates the Wright-Fisher model of genetic drift with infinite-alleles mutation in a single population, and counts the number of alleles present in the population and in samples of specified size. This process is performed for each combination of key model parameters, and the results are saved to MongoDB. """ ## setup sargs = utils.ScriptArgs()
Simulation related code specific to mutational model (the infinite sites model). """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals # standard imports import csv import io import sys import simuOpt simuOpt.setOptions(alleleType='binary') import simuPOP as simu from . import common as cf from . import utils def get_pure_hermaphrodite_mating(r_rate, weight, size, loci, allele_length, field='self_gen'): """ Construct mating scheme for pure hermaphrodite with partial selfing under the infinite sites model.
import simuPOP as sim import simuOpt simuOpt.setOptions(alleleType='short', quiet=True) import pandas as pd import numpy as np import collections np.set_printoptions(suppress=True, precision=3) from ggplot import * # 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],
"""Requires the simuPOP module be installed in the python path. Imports the results of a sampled population using the simuPOP methods.""" import simuOpt as inputsimuOpt inputsimuOpt.setOptions(quiet=True) import simuPOP as inputsimuPop def inRead(filename): """ Reads a binary file containing a population in simuPOP format. Returns the population as a simuPOP object. """ pop = inputsimuPop.loadPopulation(filename) return pop
#! /usr/bin/python # This version comes from simu20-10GRACE.py # It has larger population sizes and fewer loci # Other population gentic variables are also recorded import simuOpt simuOpt.setOptions(numThreads=12, quiet=True, alleleType='long') import simuPOP as sim from simuPOP.utils import importPopulation, export from simuPOP.sampling import drawRandomSample #g = 5000 # gen #u = 1e-6 # u #m = 0 # m #c = 400000 # contig #r = 1.0/c # recomb def SbPSz(gen): if gen<50: return [10] if gen>999: return [200]*10 else: return [2000] pop = sim.Population(size=[10], ploidy=2, loci=100000, chromNames=['Chr1'], # Find out whether simuPOP 'thinks' all these loci are on the same chromosome alleleNames=['A', 'C', 'T', 'G'], infoFields=['migrate_to']) pop.evolve( initOps=[ sim.InitSex(maleFreq=0.5),
# 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 simuOpt 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])
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #!/usr/bin/env python # import simuOpt, os, sys, json # use 'binary module' for representing long sequences simuOpt.setOptions(optimized=True, alleleType='binary') import simuPOP as sim import random from itertools import izip from itertools import islice from pprint import pprint from bisect import bisect from optparse import OptionParser import my_timer # equal fitness function def eq_fit(): return 1
happens to the structure of trait trees, sampled at fixed intervals. We will leave in homophily, but ignore convergence since it should be temporary with innovation_rateation/noise. """ import logging as log from time import time import argparse import uuid import seriationct.utils as utils import simuOpt simuOpt.setOptions(alleleType='long', optimized=True, quiet=False, numThreads=utils.get_parallel_cores(dev_flag=True)) import simuPOP as sim import seriationct.demography as demo global config, sim_id, script def setup(parser): config = parser.parse_args() sim_id = uuid.uuid4().urn script = __file__ if config.debug == '1':
# -*- mode: python; coding: utf-8; -*- """Import simuPOP's function and constant names.""" # import functions import simuOpt # we unconditionally use 'lineage' version of simuPOP as our interest is lineage. simuOpt.setOptions(quiet = True, alleleType = 'lineage') from simuPOP import RandomMating, getRNG import simuPOP # import constants from simuPOP import MALE, FEMALE, UNIFORM_DISTRIBUTION, \ AUTOSOME, CHROMOSOME_X, CHROMOSOME_Y, MITOCHONDRIAL, ALL_AVAIL, \ PER_LOCI, PER_INDIVIDUAL, PER_PLOIDY, PER_CHROMOSOME, \ BY_PROBABILITY, BY_PROPORTION, PROB_OF_MALES # metadata of this package. This is likely to change once I start # to use distutils2/packaging instead of setuptools/distutils. # Then metadata is defined in PKG-INFO file. from migselsim import metadata def enum(*seq, **named): enums = dict(zip(seq, range(len(seq))), **named) return type('Enum', (), enums) SCENARIO = enum('NOT_SPECIFIC', 'SEX_SPECIFIC', 'DEME_SPECIFIC', 'SEX_AND_DEME_SPECIFIC') COMMAND = __name__.split('.')[0] VERSION = metadata.__version__
#!/usr/bin/env python import simuOpt, types simuOpt.setOptions(alleleType='long', numThreads=2) import simuPOP as sim from simuPOP import * from simuPOP.utils import * from simuPOP.sampling import drawRandomSample options = [ { 'name':'NA', 'default':5000, 'label':'Ancestral Population Size', 'type':[int, long], 'validator':simuOpt.valueGT(0), 'description':'Ancestral population size' }, { 'name':'N1', 'default':1000, 'label':'Daugther 1 Population Size', 'type':[int, long], 'validator':simuOpt.valueGT(0), 'description':'Daughter 1 population size' }, { 'name':'N2', 'default':1000, 'label':'Daugther 2 Population Size', 'type':[int, long], 'validator':simuOpt.valueGT(0), 'description':'Daughter 2 population size'
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #!/usr/bin/env python # import simuOpt, os, sys, time # use 'mutant module' for representing long sequences simuOpt.setOptions(optimized=True, alleleType='long') import simuPOP as sim import random from itertools import izip from pprint import pprint from bisect import bisect # equal fitness function def eq_fit(): return 1 # modified version of model provided in User Guide (pg. 86-87) def infSitesMutate(pop, param): (startPos, endPos, rate) = param
#!/usr/bin/env python # # This example simulates recent and remote selective sweep and draw # trio samples. import sys, os, math, logging import simuOpt simuOpt.setOptions(alleleType='binary', optimized=False, gui=False) from simuPOP import * import loadHapMap3, selectMarkers, simuGWAS def downloadData(chroms, logger): ''' Download and create populations from the third phase of the HapMap3 data. This equivalent to command > loadHapMap3.py --chroms=2 ''' if not os.path.isdir('HapMap'): os.mkdir('HapMap') for chrom in chroms: for popName in loadHapMap3.HapMap3_pops: filename = 'HapMap/HapMap3_%s_chr%d.pop' % (popName, chrom) if not os.path.isfile(filename): pop = loadHapMap3.loadHapMapPop(chrom, popName, logger) pop.save(filename) def getInitPop(logger):
''' import copy import inspect import math import numpy as np import networkx as nx from matplotlib import pyplot as plt from matplotlib.patches import Ellipse from IPython.core.pylabtools import print_figure from IPython.display import Image import simuOpt simuOpt.setOptions(gui=False, quiet=True) import simuPOP as sp from simuPOP import demography def _hook_view(pop, param): view = param view.complete_cycle(pop) return True class Model: def __init__(self, gens): self._gens = gens self._views = [] self.pop_size = 100
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #!/usr/bin/env python # import simuOpt, os, sys, json # use 'mutant module' for representing long sequences simuOpt.setOptions(optimized=True, alleleType='mutant') import simuPOP as sim import random from itertools import izip from itertools import islice from pprint import pprint from bisect import bisect from optparse import OptionParser import my_timer # equal fitness function def eq_fit(): return 1
#!/usr/bin/env python # # Purpose: # Testing penetrance. # # Author: # Bo Peng ([email protected]) # # $LastChangedRevision$ # $LastChangedDate$ # import unittest, os, sys from simuOpt import setOptions setOptions(quiet=True) new_argv = [] for arg in sys.argv: if arg in ['short', 'long', 'binary', 'mutant', 'lineage']: setOptions(alleleType=arg) elif arg.startswith('-j'): setOptions(numThreads=int(arg[2:])) else: new_argv.append(arg) sys.argv = new_argv from simuPOP import * class TestPenetrance(unittest.TestCase): def setUp(self): self.pop = Population(size=[500, 100, 1000], ploidy=2, loci=[1])
import simuOpt simuOpt.setOptions(quiet=True, numThreads=4) import simuPOP as sim import os, numpy as np, pandas as pd, collections as col from saegus import analyze, simulate, parameters import pytest @pytest.fixture(scope="session") def tuson_pop(): tuson = sim.loadPopulation('tuson.pop') return tuson def test_info_fields(tuson_pop): pop = tuson_pop assert 'primary' in pop.infoFields( ), "Population lacks the primary infoField. Cannot assign structured mating."
}, #migration { 'longarg': 'mig=', 'default': 0, 'label': 'migration model', 'allowedTypes': [types.IntType, types.LongType], 'description': '''0: No migration |1: MigrIslandRates(r, n) |2: MigrHierarchicalIslandRates([r11, r12], r2, [n1, n2]) |3: MigrSteppingStoneRates(r, n, circular=False)''', 'chooseOneOf': range(4), 'validate': simuOpt.valueOneOf(range(4)) } ] simuOpt.setOptions(optimized=True, alleleType='long', quiet=True) pars = simuOpt.simuParam(options, 'A demo simulation') cfgPath = os.path.join(dirI, 'sample.cfg') if os.path.exists(cfgPath): pars.loadConfig(cfgPath) if not pars.getParam(): sys.exit(1) pars.saveConfig(os.path.join(dirI, 'sample.cfg')) n_gen, n_pop, n_ind, n_loci, n_chrom, n_ploidy, n_allele, init_freq = \ pars.n_gen, pars.n_pop, pars.n_ind, pars.n_loci, pars.n_chrom, pars.n_ploidy, \ len(pars.allele), pars.init_freq init_freq_2 = [] for freq in init_freq: init_freq_2.append([freq, 1 - freq])
import simuOpt, os, sys, time, subprocess, errno, csv simuOpt.setOptions(alleleType="lineage", quiet=True) from simuPOP import * from simuPOP.sampling import drawRandomSample import datetime from numpy import * from itertools import * homeDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) options = [ {"name": "popSize", "default": 100, "label": "Population Size", "type": "integer", "validator": "popSize > 0"}, {"name": "sampSize", "default": 100, "label": "Sample Size", "type": "integer", "validator": "sampSize > 0"}, {"name": "loc", "default": 5, "label": "Number of Loci", "type": "integer", "validator": "loc > 0"}, { "name": "allPerLoc", "default": 10, "label": "Number of Alleles per Loci", "type": "integer", "validator": "allPerLoc > 1", }, { "name": "generations", "default": 50, "label": "Number of Generations", "type": "integer", "validator": "generations >= 0", }, { "name": "replications",
#!/home/yairf/Develop/python/loopy/bin/python import os, sys, re, math import argparse import numpy as np import simuOpt simuOpt.setOptions(quiet=True) import simuPOP as sim import math from simuPOP.utils import * #-----------------# # Parse arguments # #-----------------# parser = argparse.ArgumentParser( description= "Backward simulation of selection using the simuPOP package. The input includes the demographic model, the current derived allele frequency, the selection coefficient, and the backward time window at which selection was active." ) parser.add_argument("-g", "--number_of_generations", type=int, default=10000, help="The number of generations simulated \ (default = %(default)s).") parser.add_argument(
dest="num_vars", action='store_true', help="print the number of variants post mut") parser.add_argument("--len-trees", "-l", dest="len_trees", action='store_true', help="print the dist of tree lengths pre mut") args = parser.parse_args() if args.generations is None: args.generations = args.popsize * args.multiplier from simuOpt import setOptions setOptions(alleleType='mutant') setOptions(optimized=True) if not args.verbose: setOptions(quiet=True) import simuPOP as sim sim.setRNG(seed=args.seed) random.seed(args.seed) logfile = fileopt(args.logfile, "w") logfile.write("Options:\n") logfile.write(str(args) + "\n") logfile.write(time.strftime('%X %x %Z') + "\n") logfile.write("----------\n")
''' This python module provides several utility functions that handles HapMap populations. When used as a script, this module creates a population using selected markers and populations. ''' import simuOpt as opt opt.setOptions(alleleType='lineage', quiet=True, optimized=True) from simuPOP import * from types import * import os import sys import random import string def mergeHapMapPops(HapMap_dir, HapMap_pops, chrom, logger=None): ''' Load HapMap dataset for multiple populations and merge them. The important step is to find a common set of markers and make sure alleles are recoded if necessary. (Alleles are sometimes coded differently in different HapMap populations.) HapMap_dir Directory where HapMap files (in simuPOP format) are saved. HapMap_pops HapMap populations to load. chrom
import simuOpt simuOpt.setOptions(alleleType='lineage', optimized=True, quiet=True) import simuPOP as sim import pandas as pd import collections as col from wgs import breed, operators, selection, helpers, parser, parameterizer, selection import random import numpy as np np.set_printoptions(suppress=True, precision=3) import matplotlib.pyplot as plt hapmap = pd.read_csv('clean_hapmap.txt') genetic_map = hapmap.ix[:, :'cM_pos'] genetic_map = pd.read_csv('nam_prefounders_genetic_map.txt', index_col=None, sep='\t') chr_cm_positions = col.OrderedDict() for i in range(1, 11): chr_cm_positions[i] = [] for idx in range(len(genetic_map)): chr_cm_positions[int(genetic_map.iloc[idx]['chr'])].append( float(genetic_map.iloc[idx]['cM_pos'])) cM_positions = [] for k, v in chr_cm_positions.items(): cM_positions.append(v) snp_to_integer = {'A': 0, 'C': 1, 'G': 2, 'T': 3, '-': 4, '+': 5}
# Software is furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice (including the next # paragraph) shall be included in all copies or substantial portions of the # Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. import simuOpt simuOpt.setOptions(quiet=True, alleleType='binary') import simuPOP as simu import partial_selfing.common as cf import partial_selfing.infinite_sites as isf class TestRecordSelfing: def setUp(self): # A locus with 10 sites "Let there are 5 loci with 2 sites each" self.allele_length = 2 self.loci = 5 self.pop = simu.Population(size=10, loci=self.allele_length * self.loci, infoFields='self_gen')
selfingsim.infinite_alleles =========================== Simulation related code specific to mutational model (the infinite alleles model). """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import csv import io import sys import simuOpt simuOpt.setOptions(alleleType='long') import simuPOP as simu from . import common as cf from . import utils def get_init_genotype_by_prop(prop): """ Sets up initial genotypes of initial individuals by specifying frequencies of alleles. """ s = sum(prop) return (len(prop), simu.InitGenotype(prop=[p / s for p in prop])) def get_mutation_operator(m_rate, loci, nrep, burnin, new_idx=0):
selfingsim.infinite_alleles =========================== Simulation related code specific to mutational model (the infinite alleles model). """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import csv import io import sys import simuOpt simuOpt.setOptions(alleleType='long') import simuPOP as simu from . import common as cf from . import utils def get_init_genotype_by_prop(prop): """ Sets up initial genotypes of initial individuals by specifying frequencies of alleles. """ s = sum(prop) return (len(prop), simu.InitGenotype(prop=[p / s for p in prop])) def get_mutation_operator(m_rate, loci, nrep, burnin, new_idx=0): """
import simuOpt simuOpt.setOptions(alleleType='short', optimized=True, quiet=True) import simuPOP as sim import pandas as pd import collections as col from wgs import breed, operators, selection, helpers, parser, parameterizer, selection import random import numpy as np np.set_printoptions(suppress=True, precision=3) import matplotlib.pyplot as plt hapmap = pd.read_csv('clean_hapmap.txt') genetic_map = hapmap.ix[:, :'cM_pos'] genetic_map = pd.read_csv('nam_prefounders_genetic_map.txt', index_col=None, sep='\t') chr_cm_positions = col.OrderedDict() for i in range(1, 11): chr_cm_positions[i] = [] for idx in range(len(genetic_map)): chr_cm_positions[int(genetic_map.iloc[idx]['chr'])].append( float(genetic_map.iloc[idx]['cM_pos'])) cM_positions = [] for k, v in chr_cm_positions.items(): cM_positions.append(v) snp_to_integer = {'A': 0, 'C': 1, 'G': 2, 'T': 3, '-': 4, '+': 5}
#Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Type "copyright", "credits" or "license()" for more information. from simuOpt import setOptions setOptions(quiet=True) import simuPOP as sim from simuPOP.utils import importPopulation pop = importPopulation('GENEPOP', 'Genpop_2subpops.txt') import random pop.addInfoFields(['age']) # age_cat is not imported as they are defined by age # 0, 1, 2 as juvenile and 2-above as adult with open('Fstat_dat(age,agecat,sex).txt') as fs: for idx, line in enumerate(fs): if idx < 10: # skip the first few lines continue if line.startswith('1 '): age, age_cat, sex = line.strip().split()[-3:] pop.individual(idx - 18).setSex(sim.MALE if sex == '2' else sim.FEMALE) pop.individual(idx - 18).age = int(age) #pop.individual(idx-18).age_cat = int(age_cat) elif line.startswith('2 '): age_cat, sex = line.strip().split()[-2:] pop.individual(idx - 18).setSex(sim.MALE if sex == '2' else sim.FEMALE) # no age info
# 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 simuOpt simuOpt.setOptions(alleleType='lineage', quiet=True) import simuPOP as sim pop = sim.Population(1000, loci=[10]*4) pop.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(freq=[0.25]*4), sim.InitLineage(range(1000), mode=sim.PER_INDIVIDUAL), ], matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=0.001)), gen = 100 ) # average number of 'contributors' num_contributors = [len(set(ind.lineage())) for ind in pop.individuals()] print('Average number of contributors is %.2f' % (sum(num_contributors) / float(pop.popSize())))
# 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/>. import simuOpt simuOpt.setOptions( quiet = True, alleleType = 'long') import genealogy as genea genealogy = [genea.Genealogy(24)] class Reindexer(sim.PyOperator): def __init__(self, loci, *args, **kwargs): self._loci = loci sim.PyOperator.__init__(self, func = self.reindex, *args, **kwargs) def reindex(self, pop): loci = self._loci marker = loci[0]
#! /usr/bin/python # The A.1A simuPOp example # Use simuOpt to import options import simuOpt simuOpt.setOptions(quiet=True, numThreads=4, gui=True) #This simuOpt allows selection of one of the six simuPOP modules #setOptions(optimized=False, alleleType='long', quiet=True) import simuPOP as sim # Set up th epopulation, ploidy is 2 by default, add (1000)*5 to make # 5 populations pop = sim.Population(size=1000, loci=2) pop.evolve( initOps=[ # Initialize individuals with random sex (MALE or FEMALE) sim.InitSex(), # Initialize individuals with two haplotypes. sim.InitGenotype(haplotypes=[[1, 2], [2, 1]]) ], # Random mating using a recombination operator matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=0.01)), postOps=[ # Calculate Linkage Disequilibrium between two loci sim.Stat(LD=[0, 1], step=10), # Print calculated LD values sim.PyEval(r"'%2d: %.2f\n' % (gen, LD[0][1])", step=10), ], gen=100 )
import fileio experiment = fileio.Experiment( constant_pop_size = 200, # Nance and Kearsey: 200k a = 0.01304, # Nance and Kearsey: 0.01304 aa_fitness = 1.5, # Nance and Kearsey: 1.0 aa_homogamy = 0.9, # Nance and Kearsey: 0.9 deafness_freq = 0.0008, # Nance and Kearsey: look again generations = 20, # Nance and Kearsey: 5 gen with 0 fitness + 20 gen with 1.0 fitness simulations = 5000) # Nance and Kearsey: unspecified import os import time import random import argparse import subprocess import multiprocessing import simuOpt simuOpt.setOptions(optimized=True, numThreads=0, quiet=True) import simuPOP as sim def customChooser(pop, subPop): ''' Generator function which chooses parents. Upon initialization, this chooser mates couples in a monogamous mating scheme. The algorithm goes through each eligible person listwise. If that person is deaf, that person marries deaf or not based on the probability set by aa_homogamy. Further, if either parent is deaf, their number of offspring is based on the fitness set by aa_fitness (non-integer fitnesses are handled using a randomizer for the fractional amount).
# required). # # Change Log: # 2009-06-25 Bo Peng <*****@*****.**> # # Add functions mergeHapMapPops and getHapMapMarkers # ''' This python module provides several utility functions that handles HapMap populations. When used as a script, this module creates a population using selected markers and populations. ''' import simuOpt simuOpt.setOptions(alleleType='binary', quiet=True, version='1.0.1') from simuPOP import * from types import * import os, sys, exceptions def mergeHapMapPops(HapMap_dir, HapMap_pops, chrom, logger=None): ''' Load HapMap dataset for multiple populations and merge them. The important step is to find a common set of markers and make sure alleles are recoded if necessary. (Alleles are sometimes coded differently in different HapMap populations.) HapMap_dir Directory where HapMap files (in simuPOP format) are saved.
parser.add_option("-w","--width",dest="width",help="width of square grid, in populations",default=3) parser.add_option("-L","--length",dest="length",help="number of bp in the chromosome",default=1e4) parser.add_option("-l","--nloci",dest="nloci",help="number of selected loci",default=20) parser.add_option("-m","--migr",dest="migr",help="migration proportion between adjacent populations",default=.01) parser.add_option("-u","--mut_rate",dest="mut_rate",help="mutation rate of selected alleles",default=1e-7) parser.add_option("-r","--recomb_rate",dest="recomb_rate",help="recombination rate",default=2.5e-8) parser.add_option("-a","--gamma_alpha",dest="gamma_alpha",help="alpha parameter in gamma distributed selection coefficients",default=.23) parser.add_option("-b","--gamma_beta",dest="gamma_beta",help="beta parameter in gamma distributed selection coefficients",default=5.34) parser.add_option("-o","--outfile",dest="outfile",help="name of output file (or '-' for stdout)",default="-") parser.add_option("-g","--logfile",dest="logfile",help="name of log file (or '-' for stdout)",default="-") parser.add_option("-s","--selloci_file",dest="selloci_file",help="name of file to output selected locus information",default="sel_loci.txt") parser.add_option("-t","--num_threads",dest="num_threads",help="Number of threads (MORE THREADS=SLOWER?!?!).",default=1) (options,args) = parser.parse_args() import simuOpt simuOpt.setOptions(alleleType='mutant') simuOpt.setOptions(numThreads = int(options.num_threads)) import simuPOP as sim from simuPOP.demography import migr2DSteppingStoneRates outfile = fileopt(options.outfile, "w") logfile = fileopt(options.logfile, "w") selloci_file = options.selloci_file logfile.write("Options:\n") logfile.write(str(options)+"\n") generations=int(options.generations) popsize=int(options.popsize) nloci=int(options.nloci) width=int(options.width)
# # random fitness effect # from simuOpt import setOptions setOptions(alleleType='mutant', quiet=True) import simuPOP as sim class RandomFitness: def __init__(self, scale=1): self.coefMap = {} self.scale = scale def _newS(self, loc, alleles): raise ValueError( "This function should be redefined in the subclass of this class") def __call__(self, loc, alleles): # because s is assigned for each locus, we need to make sure the # same s is used for fitness of genotypes 01 (1-s) and 11 (1-2s) # at each locus if loc in self.coefMap: s, h = self.coefMap[loc] else: res = self._newS(loc, alleles) if type(res) in [list, tuple]: if len(res) != 2: raise ValueError("A list or tuple of s, h is expected") s, h = res else: s = res
import simuOpt # I use the lineage module to keep track of allele lineages. simuOpt.setOptions(alleleType = 'lineage') from simuPOP.utils import viewVars from simuPOP import * # This class is to recombine only in females. Note that it comes from an example # in the simuPOP manual (sexSpecificRecombinator.py), but Bo Peng had to correct # it after I told him it didn't work. The default loci and maleLoci were not well # defined. class sexSpecificRecombinator(PyOperator): def __init__(self, intensity=0, rates=0, loci=ALL_AVAIL, convMode=NO_CONVERSION, maleIntensity=0, maleRates=0, maleLoci=ALL_AVAIL, maleConvMode=NO_CONVERSION, *args, **kwargs): # This operator is used to recombine maternal chromosomes self.Recombinator = Recombinator(rates, intensity, loci, convMode) # This operator is used to recombine paternal chromosomes self.maleRecombinator = Recombinator(maleRates, maleIntensity, maleLoci, maleConvMode) # PyOperator.__init__(self, func=self.transmitGenotype, *args, **kwargs) # def transmitGenotype(self, pop, off, dad, mom): # Form the first homologous copy of offspring. self.Recombinator.transmitGenotype(mom, off, 0) # Form the second homologous copy of offspring. self.maleRecombinator.transmitGenotype(dad, off, 1) return True #pop = Population(2, loci=1000, infoFields=['ind_id', 'father_idx', 'mother_idx'])
#!/usr/bin/env python # # This example simulates a case-control sample using a GxE model. # import sys, os, random, math, logging import simuOpt simuOpt.setOptions(gui=False, alleleType='binary', optimized=True) from simuPOP import * import loadHapMap3, selectMarkers, simuGWAS def downloadData(chroms, logger): ''' Download and create populations from the third phase of the HapMap3 data. This equivalent to command > loadHapMap3.py --chroms='[2, 5,10]' --dest=HapMap ''' if not os.path.isdir('HapMap'): os.mkdir('HapMap') for chrom in chroms: for popName in loadHapMap3.HapMap3_pops: filename = 'HapMap/HapMap3_%s_chr%d.pop' % (popName, chrom) if not os.path.isfile(filename): pop = loadHapMap3.loadHapMapPop(chrom, popName, logger) pop.save(filename) def getInitPop(logger):
#! /usr/bin/python #Object: Hierarchically structured Isolation with Migration model #Aim: Simulating SNPs data for genom scan methods comparison #Author: Pierre de Villemereuil #October 2013 #************************************************************************************ #---------------------------------Header--------------------------------------------- #************************************************************************************ #Setting allele size from simuOpt import setOptions setOptions(alleleType='binary') #Binary for two alleles states (SNPs) #Importing simuPOP import simuPOP as sim from simuPOP.utils import saveCSV from simuPOP.sampling import drawRandomSample #Importing some other python libraries from math import * from numpy import * from random import * #********************************************************************************************** #-----------------------------Simulation parameters------------------------------------------- #********************************************************************************************** #Population(s) size(s) popsize = 500
import simuOpt simuOpt.setOptions(quiet=True, alleleType='binary') import simuPOP as sim pop = sim.Population(size=[10000] * 3, loci=[1] * 2, infoFields='fitness') simu = sim.Simulator(pop, rep=3) simu.evolve(initOps=[sim.InitSex(), sim.InitGenotype(freq=[0.5, 0.5])], preOps=[ sim.MaSelector(loci=0, fitness=[1, 0.99, 0.98], reps=0), sim.MaSelector(loci=0, fitness=[1, 1, 0.99], reps=1), sim.MlSelector([ sim.MaSelector(loci=0, fitness=[1, 0.99, 0.98]), sim.MaSelector(loci=1, fitness=[1, 1, 0.99]) ], mode=sim.MULTIPLICATIVE, reps=2) ], matingScheme=sim.RandomMating(), postOps=[ sim.Stat(alleleFreq=[0, 1], step=50), sim.PyEval(r"'%.3f\t' % alleleFreq[0][1]", reps=0, step=50), sim.PyEval(r"'%.3f\t' % alleleFreq[0][1]", reps=1, step=50), sim.PyEval( r"'%.3f\t%.3f\n' % (alleleFreq[0][1], alleleFreq[1][1])", reps=2, step=50), ], gen=151)
#!/usr/bin/env python # Copyright (c) 2013. Mark E. Madsen <*****@*****.**> # # This work is licensed under the terms of the Apache Software License, Version 2.0. See the file LICENSE for details. from __future__ import print_function import simuOpt, sys simuOpt.setOptions(alleleType='long', optimized=False, quiet=True) import simuPOP as sim import uuid import rapanuisim.data as data import rapanuisim.utils as utils import rapanuisim.math as cpm import ming import logging as log import pprint as pp import argparse """ This program simulates the Wright-Fisher model of genetic drift with infinite-alleles mutation in a single population, and counts the number of alleles present in the population and in samples of specified size """ # get all parameters args = argparse.ArgumentParser() args.add_argument("--popsize", default=1000, type=int, help="Population Size") args.add_argument("--stepsize", default=100, type=int, help="Interval Between Data Samples") args.add_argument( "--length",
a_FREQ = 0.01304 # Nance and Kearsey: 0.01304 aa_FITNESS = 1.0 # Nance and Kearsey: 1.0 aa_HOMOGAMY = 0.9 # Nance and Kearsey: 0.9 DEAF_FREQ = 0.0008 # Nance and Kearsey: two other genes each at 0.003^2 freq (tiny) GENERATIONS = 20 # Nance and Kearsey: 5 gen with 0 fitness + 20 gen with 1.0 fitness SIMULATIONS = 5000 import os import time import random import argparse import subprocess import multiprocessing import simuOpt simuOpt.setOptions(optimized=True, numThreads=0, quiet=True) import simuPOP as sim import fileio def customChooser(pop, subPop): ''' Generator function which chooses parents. Upon initialization, this chooser mates couples in a monogamous mating scheme. The algorithm goes through each eligible person listwise. If that person is deaf, that person marries deaf or hearing based on the probability set by aa_homogamy. Further, if either parent is deaf, their number of offspring is based on the
#==================================================================================================================== ###################################### Contact : [email protected] ##################################### #==================================================================================================================== #====================== Path ======================# PATHF = os.path.join( '/home/clement/Documents/SimulationPP') #BEWARE : insert your path line 65 #====================== Packages ======================# import math, os, time, random, sys, subprocess, shutil import simuPOP as sim import simuOpt from simuOpt import setOptions setOptions(optimized=False, alleleType='short', numThreads=2, quiet=True) import simuPOP.utils as utils from simuPOP.utils import saveCSV, export #====================== Variables ======================# nb_indiv = int(sys.argv[1]) #Number of individuals in the total population nb_loci = int(sys.argv[2]) #Number of locii nb_gen = int(sys.argv[3]) #Number of studied generations selection_intensity_z1 = int(sys.argv[4]) #Intensity of the selection repeat = int(sys.argv[5])
# Change Log: # 2012-05-20 Matt Johnson <*****@*****.**> # Modify to use in DPLSim # 2010-03-01 Bo Peng <*****@*****.**> # Add script. ''' This script evolves a population forward in time, subject to rapid population expansion, mutation, recombination and natural selection. A trajectory simulation method is used to control the allele frequency of optional disease predisposing loci. A scaling approach can be used to improve efficiency when weak, additive genetic factors are used. ''' import simuOpt simuOpt.setOptions(alleleType='binary', version='1.0.1', optimized=False, quiet=True) from simuPOP import * from simuPOP.utils import simulateForwardTrajectory, simulateBackwardTrajectory, migrSteppingStoneRates import os, sys, math, types, time options = [ {'longarg': 'initPop=', 'default': 'init.pop', 'label': 'Initial population', 'allowedTypes': types.StringType, 'validate': simuOpt.valueValidFile(), 'description': 'An initial population created using script selectedMarkers.py' }, {'longarg': 'dumpRec=',
# # Identical QTL Parameters # # ### Twenty Loci # # This simulates 20 replicates of recurrent selection. # Each replicate has the same QTL and allele effects. # # ### Generating Data for GWAS with TASSEL # In[ ]: import simuOpt simuOpt.setOptions(alleleType='short', optimized=True, numThreads=4, quiet=True) import simuPOP as sim import pandas as pd import collections as col from saegus import breed, operators, simulate, analyze, parse, parameters import random import copy import yaml import numpy as np np.set_printoptions(suppress=True, precision=3) # In[ ]: hapmap = pd.read_csv('clean_hapmap.txt')
from __future__ import division from collections import defaultdict, OrderedDict from copy import deepcopy import psutil import simuOpt simuOpt.setOptions(alleleType='long', optimized=True, quiet=False) simuOpt.setOptions(numThreads=psutil.cpu_count()) import math import seaborn as sns sns.set_style('white') import matplotlib.pyplot as plt import networkdrift.demography.network as network from networkdrift.utils import utils import simuPOP as sp import logging as log import numpy as np import scipy.stats import argparse import uuid from time import time import sys import os from collections import defaultdict import csv global config, sim_id, script, cores ''' Example use of script. parameter-sweep-for-localization.py --experiment paramsweep5 --networkfile smallworld --numloci 1 --maxinittraits 100
import simuOpt simuOpt.setOptions(gui=False, alleleType='binary') import simuPOP as sim import random, math from ch6_genCaseCtrl import genCaseControlSample def penetrance(alpha, beta1, beta2, beta3, gamma1, gamma2): def func(geno): e = random.randint(0, 1) g1 = geno[0] + geno[1] g2 = geno[2] + geno[3] logit = alpha + beta1 * g1 + beta2 * g2 + beta3 * g1 * g2 + gamma1 * e * g1 + gamma2 * e * g2 return 1 / (1. + math.exp(-logit)) return func sample = genCaseControlSample( pop, 1000, 1000, sim.PyPenetrance(func=penetrance(-5, 0.20, 0.4, 0.4, 0.2, 0.4), loci=['rs4491689', 'rs6869003'])) sim.stat(sample, association=sim.ALL_AVAIL) # get p-values sample.dvars().Allele_ChiSq_p
""" Functions for removing SNPs from simuPOP population objects. removeRare: removes SNPs with minor allele frequencies below a threshold. The threshold defaults are 0.0000001 or 0.999999, meaning only monomorphic loci are removed. A list of names of Disease Loci are required. writeSNPLoc: writes a text file with the name of each DPL and the new index location. This function is useful for the programs which do not use marker names or locations, to later identify the causative SNP. removeDPL: removes the DPL """ import simuOpt simuOpt.setOptions(alleleType='binary', optimized=True,quiet=True) import simuPOP as sim def removeRare(pop,thresh_hi=0.999999,thresh_lo=0.000001,DPL=['rs4491689'],savefile=False): """ Removes rare SNPs with a minor allele frequency below a threshold value. The default thresholds will only remove monomorphic loci. If savefile=False, the population is simply modified. Set savefile to a string to save the population to a binary file. The function returns: the number of loci removed a list of the the relative locations of the DPL. """ sim.stat(pop,alleleFreq=range(pop.totNumLoci())) lociToRemove = [l for l in xrange(pop.totNumLoci()) if pop.dvars().alleleFreq[l][0] > thresh_hi or pop.dvars().alleleFreq[l][0] < thresh_lo] pop.removeLoci(lociToRemove) if savefile:
elif alleleType == 'binary': size = (memsize * 1024.0 * 1024.0 * 1024.0) / ( (loci * ploidy / 8.0 + 32.0) * 2.0 + 8) mating = timeit.Timer( setup='from __main__ import Population, InitSex, RandomMating,' 'MendelianGenoTransmitter\n' "pop = Population(size=%d, loci=%d, ploidy = %d)" % (size, loci, ploidy), stmt="pop.evolve(\n" "initOps=InitSex(),\n" "matingScheme=RandomMating(ops=MendelianGenoTransmitter()),\n" "gen=%d)" % gen) print(">Maximum number of population size: %d\n>Time(sec):%f" % (size, mating.timeit(number=1))) if __name__ == '__main__': # Number of processors(CPU Core) numThreads = 1 alleleType = 'mutant' from simuOpt import setOptions setOptions(quiet=True) setOptions(alleleType=alleleType) setOptions(numThreads=numThreads) from simuPOP import * print("How many individuals can we simulate in 8GB of ram for") for N, theta in [(1000, 1000), (1000, 5000), (1000, 10000), (5000, 1000), (5000, 5000)]: print("T =", 10 * N, " generations; theta =", theta, "?") memory(N, theta)
parser.add_argument("--recomb_rate","-r", type=float, help="recombination rate",default=2.5e-8) parser.add_argument("--selection_coef","-S", type=float, help="strength of selection",default=.1) parser.add_argument("--nsamples","-k", type=int, help="number of *diploid* samples, total") parser.add_argument("--ancestor_age","-A", type=int, help="time to ancestor above beginning of sim") parser.add_argument("--mut_rate","-U", type=float, help="mutation rate",default=1e-7) parser.add_argument("--treefile","-t", type=str, help="name of output file for trees (default: not output)") parser.add_argument("--outfile","-o", type=str, help="name of output VCF file (default: not output)") parser.add_argument("--logfile","-g", type=str, help="name of log file (or '-' for stdout)",default="-") parser.add_argument("--selloci_file","-s", type=str, help="name of file to output selected locus information") parser.add_argument("--samples_file", "-e", help="name of file to output information on samples (default=(dir)/samples.tsv)") args = parser.parse_args() import simuOpt simuOpt.setOptions(alleleType='mutant') import simuPOP as sim if args.selloci_file is None: args.selloci_file = os.path.join(os.path.dirname(args.treefile),"sel_loci.txt") if args.samples_file is None: args.samples_file = os.path.join(os.path.dirname(args.treefile),"samples.tsv") samples_file = fileopt(args.samples_file,"w") if args.outfile is not None: outfile = fileopt(args.outfile, "w") logfile = fileopt(args.logfile, "w") logfile.write("Options:\n") logfile.write(str(args)+"\n") logfile.write(time.strftime('%X %x %Z')+"\n")
import simuOpt simuOpt.setOptions(quiet=True, alleleType='long') import simuPOP as sim pop = sim.Population(size=[2500]*10, loci=1) simu = sim.Simulator(pop, rep=2) simu.evolve( initOps=[ sim.InitSex(), sim.InitGenotype(genotype=20), ], preOps=[ sim.StepwiseMutator(rates=0.0001, reps=0), sim.KAlleleMutator(k=10000, rates=0.0001, reps=1), ], matingScheme=sim.RandomMating(), postOps=[ # Use vars=['alleleFreq_sp'] to calculate allele frequency for # each subpopulation sim.Stat(alleleFreq=0, vars=['alleleFreq_sp'], step=200), sim.PyEval('gen', step=200, reps=0), sim.PyEval(r"'\t%.2f' % (sum([len(subPop[x]['alleleFreq'][0]) " "for x in range(10)])/10.)", step=200), sim.PyOutput('\n', reps=-1, step=200) ], gen=601 )
========================= Simulation related code specific to mutational model (the infinite sites model). """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals # standard imports import csv import io import sys import simuOpt simuOpt.setOptions(alleleType='binary') import simuPOP as simu from . import common as cf from . import utils def get_pure_hermaphrodite_mating(r_rate, weight, size, loci, allele_length, field='self_gen'): """ Construct mating scheme for pure hermaphrodite with partial selfing under the infinite sites model. A fraction, 0 <= weight <= 1, of offspring is generated by selfing, and others are generated by outcrossing. In this model, there is no specific sex so that any individual can mate with any other individuals in a population. Furthermore, a parent can participate in both selfing and outcrossing.