コード例 #1
0
import simuPOP as sim
from simuPOP.utils import migrIslandRates
p = [0.2, 0.3, 0.5]
pop = sim.Population(size=[10000] * 3, loci=1, infoFields='migrate_to')
simu = sim.Simulator(pop, rep=2)
simu.evolve(
    initOps=[sim.InitSex()] +
    [sim.InitGenotype(prop=[p[i], 1 - p[i]], subPops=i) for i in range(3)],
    preOps=sim.Migrator(rate=migrIslandRates(0.01, 3), reps=0),
    matingScheme=sim.RandomMating(),
    postOps=[
        sim.Stat(alleleFreq=0, structure=0, vars='alleleFreq_sp', step=50),
        sim.PyEval(
            "'Fst=%.3f (%s)\t' % (F_st, ', '.join(['%.2f' % "
            "subPop[x]['alleleFreq'][0][0] for x in range(3)]))",
            step=50),
        sim.PyOutput('\n', reps=-1, step=50),
    ],
    gen=201)
コード例 #2
0
ファイル: replicate.py プロジェクト: sudorook/simuPOP
# 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
simu = sim.Simulator(sim.Population(100, loci=[20]), 5)
simu.evolve(
    initOps=[sim.InitSex(), sim.InitGenotype(freq=[0.2, 0.8])],
    matingScheme=sim.RandomMating(),
    postOps=[
        sim.Stat(alleleFreq=0, step=10),
        sim.PyEval('gen', step=10, reps=0),
        sim.PyEval(r"'\t%.2f' % alleleFreq[0][0]", step=10, reps=(0, 2, -1)),
        sim.PyOutput('\n', step=10, reps=-1)
    ],
    gen=30,
)
コード例 #3
0
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
)
コード例 #4
0
            p2 = inds[randint(0, len(inds) - 1)]
            # return indexes of both parents
            if s1 == sim.MALE:
                yield p1, p2
            else:
                yield p2, p1


pop = sim.Population(size=2000, loci=1, infoFields='x')
# define VSPs x<1, 1<=x<2, 2<=x<3, 3<=x<4, ...
pop.setVirtualSplitter(sim.InfoSplitter(field='x', cutoff=range(1, 8)))
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitInfo(lambda: uniform(0, 8), infoFields='x'),
        # only individuals in the middle range has certain genotype
        sim.InitGenotype(freq=[0.6, 0.4], subPops=[(0, 4)]),
    ],
    matingScheme=VicinityMating(locationField='x',
                                vicinity=1,
                                varOfLocation=0.5),
    postOps=[
        sim.Stat(alleleFreq=0,
                 subPops=[(0, sim.ALL_AVAIL)],
                 vars='alleleFreq_sp'),
        sim.PyEval(r"'%.3f ' % alleleFreq[0][1]",
                   subPops=[(0, sim.ALL_AVAIL)]),
        sim.PyOutput('\n'),
    ],
    gen=10)
コード例 #5
0
ファイル: ageStructured.py プロジェクト: sudorook/simuPOP
            pop.dvars((0,sp)).propOfAffected * 100.,
            pop.dvars((0,sp)).popSize))
    #
    return True


pop.evolve(
    initOps=[
        sim.InitSex(),
        # random assign age
        sim.InitInfo(lambda: random.randint(0, 75), infoFields='age'),
        # random genotype
        sim.InitGenotype(freq=[0.5, 0.5]),
        # assign an unique ID to everyone.
        sim.IdTagger(),
        sim.PyOutput('Prevalence of disease in each age group:\n'),
    ],
    # increase the age of everyone by 1 before mating.
    preOps=sim.InfoExec('age += 1'),
    matingScheme=sim.HeteroMating([
        # all individuals with age < 75 will be kept. Note that
        # CloneMating will keep individual sex, affection status and all
        # information fields (by default).
        sim.CloneMating(subPops=[(0,0), (0,1), (0,2)], weight=-1),
        # only individuals with age between 20 and 50 will mate and produce
        # offspring. The age of offspring will be zero.
        sim.RandomMating(ops=[
            sim.IdTagger(),                   # give new born an ID
            sim.PedigreeTagger(),             # track parents of each individual
            sim.MendelianGenoTransmitter(),   # transmit genotype
        ],
コード例 #6
0
import simuPOP as sim
# [100]*100 means a population with 100 subpopulations, each of size 100.
pop = sim.Population([100] * 100, loci=1)
pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[0.5, 0.5]),
        sim.PyOutput('gen:   mean freq   mean Ht (expected Ht)\n')
    ],
    preOps=[
        # Statistics in subpopulations are by default not calculated.
        # This is changed by specifying variables with a '_sp' suffix.
        sim.Stat(alleleFreq=0,
                 heteroFreq=0,
                 vars=['alleleFreq_sp', 'heteroFreq_sp'],
                 step=20),
        # Variables in subpopulations are stored in dictionaries
        # subPop[subPopID] where subPopID can be virtual.
        sim.PyEval(
            r'"%2d:    %.4f      %.4f (%.4f)\n" % (gen,'
            'sum([subPop[x]["alleleFreq"][0][1] for x in range(100)])/100.,'
            'sum([subPop[x]["heteroFreq"][0] for x in range(100)])/100.,'
            '0.5*(1-1/200.)**gen)',
            step=20)
    ],
    matingScheme=sim.RandomMating(),
    gen=100)
コード例 #7
0
ファイル: statNumOfSegSites.py プロジェクト: sudorook/simuPOP
#
# 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] * 100)
pop.evolve(initOps=[
    sim.InitSex(),
    sim.InitGenotype(freq=[0.3, 0.7]),
    sim.PyOutput('#all 0\t#seg sites\t#all 1\n'),
],
           matingScheme=sim.RandomMating(),
           postOps=[
               sim.Stat(numOfSegSites=sim.ALL_AVAIL,
                        vars=['numOfSegSites', 'numOfFixedSites']),
               sim.PyEval(
                   r'"%d\t%d\t%d\n" % (100-numOfSegSites-numOfFixedSites,'
                   'numOfSegSites, numOfFixedSites)',
                   step=50)
           ],
           gen=500)
# output a list of segregating sites
sim.stat(pop, numOfSegSites=sim.ALL_AVAIL, vars='segSites')
print(pop.dvars().segSites)
コード例 #8
0
# 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
simu = sim.Simulator(sim.Population(size=1000, loci=2), rep=3)
simu.evolve(initOps=[sim.InitSex(),
                     sim.InitGenotype(genotype=[1, 2, 2, 1])],
            matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=0.01)),
            postOps=[
                sim.Stat(LD=[0, 1]),
                sim.PyEval(r"'%.2f\t' % LD[0][1]", step=20, output='>>LD.txt'),
                sim.PyOutput('\n', reps=-1, step=20, output='>>LD.txt'),
                sim.PyEval(r"'%.2f\t' % R2[0][1]", output='R2.txt'),
                sim.PyEval(r"'%.2f\t' % LD[0][1]",
                           step=20,
                           output="!'>>LD_%d.txt' % rep"),
            ],
            gen=100)
print(open('LD.txt').read())
print(open('R2.txt').read())  # Only the last write operation succeed.
print(open('LD_2.txt').read())  # Each replicate writes to a different file.
コード例 #9
0
ファイル: recRate.py プロジェクト: sudorook/simuPOP
# 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
simu = sim.Simulator(sim.Population(size=[1000], loci=[100]), rep=2)
simu.evolve(
    initOps=[sim.InitSex(),
             sim.InitGenotype(genotype=[0] * 100 + [1] * 100)],
    matingScheme=sim.RandomMating(ops=[
        sim.Recombinator(rates=0.01, reps=0),
        sim.Recombinator(rates=[0.01] * 10, loci=range(50, 60), reps=1),
    ]),
    postOps=[
        sim.Stat(LD=[[40, 55], [60, 70]]),
        sim.PyEval(
            r'"%d:\t%.3f\t%.3f\t" % (rep, LD_prime[40][55], LD_prime[60][70])'
        ),
        sim.PyOutput('\n', reps=-1)
    ],
    gen=5)
コード例 #10
0
ファイル: srv.py プロジェクト: rogerdoyle/simuPOP-examples
def simuRareVariants(regions,
                     N,
                     G,
                     mu,
                     selDist,
                     selCoef,
                     selModel='exponential',
                     recRate=0,
                     splitTo=[1],
                     splitAt=0,
                     migrRate=0,
                     steps=[100],
                     mutationModel='finite_sites',
                     initPop='',
                     extMutantFile='',
                     addMutantsAt=0,
                     postHook=None,
                     statFile='',
                     popFile='',
                     markerFile='',
                     mutantFile='',
                     genotypeFile='',
                     verbose=1,
                     logger=None):
    '''
    Please refer to simuRareVariants.py -h for a detailed description of all parameters.
    Note that a user-defined function can be passed to parameter selDist to specify
    arbitrary distribution of fitness. A script-only feature is that a Python function
    can be provided through parameter postHook to process the population at each generation.
    '''
    #
    # convert regions to start/end positions
    ranges = []
    chromTypes = []
    for region in regions:
        start, end = [int(x) for x in region.split(':')[1].split('..')]
        ranges.append((start, end + 1))
        if region.split(':')[0] == 'chrX':
            chromTypes.append(sim.CHROMOSOME_X)
            if len(regions) > 1:
                raise ValueError(
                    'The current implementation only allows one region if it is on chromosome X'
                )
            logger.info('Chromosome {} is on chromosome X'.format(region))
        elif region.split(':')[0] == 'chrY':
            raise ValueError(
                'The current implementation does not support chromosome Y')
            chromTypes.append(sim.CHROMOSOME_Y)
            logger.info('Chromosome {} is on chromosome Y'.format(region))
        else:
            chromTypes.append(sim.AUTOSOME)
    if logger:
        logger.info('%s regions with a total length of %d basepair.' %
                    (len(ranges), sum([x[1] - x[0] for x in ranges])))
    #
    # set default parameter
    if selCoef is None:
        # set default parameters
        if selDist == 'mixed_gamma':
            selCoef = [0.0186, 0.0001, 0.184, 0.160 * 2, 0.5, 0.0001, 0.1]
        elif selDist == 'mixed_gamma1':
            selCoef = [0, -1, 0.562341, 0.01, 0.5, 0.00001, 0.1]
        elif selDist == 'gamma1':
            selCoef = [0.23, 0.185 * 2, 0.5]
        elif selDist == 'gamma2':
            selCoef = [0.184, 0.160 * 2, 0.5]
        elif selDist == 'gamma3':
            selCoef = [0.206, 0.146 * 2, 0.5]
        elif selDist == 'constant':
            selCoef = [0.01, 0.5]
        elif not isinstance(selDist, collections.Callable):
            raise ValueError("Unsupported random distribution")
    else:
        # force to list type
        selCoef = list(selCoef)
    if len(steps) == 0:
        # at the end of each stage
        steps = G
    elif len(steps) == 1:
        # save step for each stage
        steps = steps * len(G)
    # use a right selection operator.
    collector = fitnessCollector()
    mode = {
        'multiplicative': sim.MULTIPLICATIVE,
        'additive': sim.ADDITIVE,
        'exponential': sim.EXPONENTIAL
    }[selModel]
    #
    if type(popFile) == str:
        popFile = [popFile, -1]
    #
    if isinstance(selDist, collections.Callable):
        mySelector = MutSpaceSelector(selDist=selDist,
                                      mode=mode,
                                      output=collector.getCoef)
    elif selDist == 'mixed_gamma':
        mySelector = MutSpaceSelector(selDist=mixedGamma(selCoef),
                                      mode=mode,
                                      output=collector.getCoef)
    elif selDist == 'mixed_gamma1':
        mySelector = MutSpaceSelector(selDist=mixedGamma1(selCoef),
                                      mode=mode,
                                      output=collector.getCoef)
    elif selDist.startswith('gamma'):
        mySelector = MutSpaceSelector(selDist=[sim.GAMMA_DISTRIBUTION] +
                                      selCoef,
                                      mode=mode,
                                      output=collector.getCoef)
    elif selDist == 'constant':
        if selCoef == 0:
            mySelector = sim.NoneOp()
        else:
            mySelector = MutSpaceSelector(selDist=[sim.CONSTANT] + selCoef,
                                          mode=mode,
                                          output=collector.getCoef)
    #
    # Evolve
    if os.path.isfile(initPop):
        if logger:
            logger.info('Loading initial population %s...' % initPop)
        pop = sim.loadPopulation(initPop)
        if pop.numChrom() != len(regions):
            raise ValueError(
                'Initial population %s does not have specified regions.' %
                initPop)
        for ch, reg in enumerate(regions):
            if pop.chromName(ch) != reg:
                raise ValueError(
                    'Initial population %s does not have region %s' %
                    (initPop, reg))
        pop.addInfoFields(['fitness', 'migrate_to'])
    else:
        pop = sim.Population(size=N[0],
                             loci=[10] * len(regions),
                             chromNames=regions,
                             infoFields=['fitness', 'migrate_to'],
                             chromTypes=chromTypes)
    if logger:
        startTime = time.clock()
    #
    progGen = []
    # 0, G[0], G[0]+G[1], ..., sum(G)
    Gens = [sum(G[:i]) for i in range(len(G) + 1)]
    for i in range(len(Gens) - 1):
        progGen += list(range(Gens[i], Gens[i + 1], steps[i]))
    pop.evolve(
        initOps=sim.InitSex(),
        preOps=[
            sim.PyOutput('''Statistics outputted are
1. Generation number,
2. population size (a list),
3. number of segregation sites,
4. average number of segregation sites per individual
5. average allele frequency * 100
6. average fitness value
7. minimal fitness value of the parental population
''', at = 0)] + \
            [sim.PyOutput('Starting stage %d\n' % i, at = Gens[i]) for i in range(0, len(Gens))] + \
            # add alleles from an existing population

            [sim.IfElse(extMutantFile != '',
                ifOps = [
                    sim.PyOutput('Loading and converting population %s' % extMutantFile),
                    sim.PyOperator(func=addMutantsFrom, param=(extMutantFile, regions, logger)),
                ], at = addMutantsAt),
            # revert alleles at fixed loci to wildtype
            MutSpaceRevertFixedSites(),
            # mutate in a region at rate mu, if verbose > 2, save mutation events to a file
            MutSpaceMutator(mu, ranges, {'finite_sites':1, 'infinite_sites':2}[mutationModel],
                output='' if verbose < 2 else '>>mutations.lst'),
            # selection on all loci
            mySelector,
            # output statistics in verbose mode
            sim.IfElse(verbose > 0, ifOps=[
                sim.Stat(popSize=True, meanOfInfo='fitness', minOfInfo='fitness'),
                NumSegregationSites(),
                sim.PyEval(r'"%5d %s %5d %.6f %.6f %.6f %.6f\n" '
                    '% (gen, subPopSize, numSites, avgSites, avgFreq*100, meanOfInfo["fitness"], minOfInfo["fitness"])',
                    output='>>' + statFile),
                ], at = progGen
            ),
            sim.IfElse(len(splitTo) > 1,
                sim.Migrator(rate=migrIslandRates(migrRate, len(splitTo)),
                    begin=splitAt + 1)
            ),
        ],
        matingScheme=sim.RandomMating(ops=MutSpaceRecombinator(recRate, ranges),
            subPopSize=multiStageDemoFunc(N, G, splitTo, splitAt)),
        postOps = [
            sim.NoneOp() if postHook is None else sim.PyOperator(func=postHook),
            sim.SavePopulation(popFile[0], at=popFile[1]),
        ],
        finalOps=[
            # revert fixed sites so that the final population does not have fixed sites
            MutSpaceRevertFixedSites(),
            sim.IfElse(verbose > 0, ifOps=[
                # statistics after evolution
                sim.Stat(popSize=True),
                NumSegregationSites(),
                sim.PyEval(r'"%5d %s %5d %.6f %.6f %.6f %.6f\n" '
                    '% (gen+1, subPopSize, numSites, avgSites, avgFreq*100, meanOfInfo["fitness"], minOfInfo["fitness"])',
                    output='>>' + statFile),
                sim.PyEval(r'"Simulated population has %d individuals, %d segregation sites.'
                           r'There are on average %.1f sites per individual. Mean allele frequency is %.4f%%.\n"'
                           r'% (popSize, numSites, avgSites, avgFreq*100)'),
            ]),
        ],
        gen = Gens[-1]
    )
    # record selection coefficients to population
    if len(collector.selCoef) == 0:
        # this must be the neutral case where a NonOp has been used.
        pop.dvars().selCoef = 0
    else:
        pop.dvars().selCoef = collector.selCoef
    # re-save the file with the added selCoef
    if popFile[-1] == -1:
        pop.save(popFile[0])
    #
    if logger:
        logger.info('Population simulation takes %.2f seconds' %
                    (time.clock() - startTime))
    if markerFile or genotypeFile:
        if logger:
            logger.info('Saving marker information to file %s' % markerFile)
        mutants = saveMarkerInfoToFile(pop, markerFile, logger)
        if genotypeFile:
            if logger:
                logger.info('Saving genotype in .ped format to file %s' %
                            genotypeFile)
            saveGenotypeToFile(pop, genotypeFile, mutants, logger)
    if mutantFile:
        if logger:
            logger.info('Saving mutants to file %s' % mutantFile)
        saveMutantsToFile(pop, mutantFile, logger=logger)
    return pop
コード例 #11
0
ファイル: PyPenetrance.py プロジェクト: sudorook/simuPOP
        v = 0.03  # case of aaBb
    elif geno[0] + geno[1] == 2 and geno[2] + geno[3] == 2:
        v = 0.05  # case of aabb
    else:
        v = 0.01  # other cases
    if smoking:
        return v * 2
    else:
        return v


pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(freq=[.5, .5]),
        sim.PyOutput('Calculate prevalence in smoker and non-smokers\n'),
    ],
    matingScheme=sim.RandomMating(),
    postOps=[
        # set smoking status randomly
        sim.InitInfo(lambda: random.randint(0, 1), infoFields='smoking'),
        # assign affection status
        sim.PyPenetrance(loci=[0, 1], func=penet),
        sim.Stat(numOfAffected=True,
                 subPops=[(0, sim.ALL_AVAIL)],
                 vars='propOfAffected_sp',
                 step=20),
        sim.PyEval(
            r"'Non-smoker: %.2f%%\tSmoker: %.2f%%\n' % "
            "(subPop[(0,0)]['propOfAffected']*100, subPop[(0,1)]['propOfAffected']*100)",
            step=20)
コード例 #12
0
import simuPOP as sim
pop = sim.Population(size=10000, loci=2, infoFields='fitness')
a, b, c, d = 1, 1.5, 2.5, 4
r = 0.02
pop.evolve(initOps=[
    sim.InitSex(),
    sim.InitGenotype(freq=(0.5, 0.5)),
    sim.PyOutput('LD,   AB,  Ab,  aB,  ab,  avg fitness\n'),
],
           preOps=[
               sim.MaSelector(loci=[0, 1],
                              wildtype=0,
                              fitness=[a, b, a, c, d, c, a, b, a]),
               sim.Stat(meanOfInfo='fitness'),
           ],
           matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=r)),
           postOps=[
               sim.Stat(haploFreq=[0, 1], LD=[0, 1], step=20),
               sim.PyEval(
                   r"'%.3f %.2f %.2f %.2f %.2f %.2f\n' % (LD[0][1], "
                   "haploFreq[(0,1)][(0,0)], haploFreq[(0,1)][(0,1)],"
                   "haploFreq[(0,1)][(1,0)], haploFreq[(0,1)][(1,1)],"
                   "meanOfInfo['fitness'])",
                   step=20),
           ],
           gen=100)