Esempio n. 1
0
 def testAdmixInvalidProportion(self):
     """
     Test requirement that admix prop is 0 <= p <= 1
     """
     m = fp.MetaPopVec(64, [100, 100])
     rng = fp.GSLrng(101)
     with self.assertRaises(RuntimeError):
         demog.admix_pops(rng, m, 0, 1, -0.1, 100)
Esempio n. 2
0
#Population size
N=1000
#We'll evolve for 10N generations.
#nlist is a list of population sizes over time.
#len(nlist) is the length of the simulation
#We use numpy arrays for speed and optimised RAM
#use.  Note the dtype=np.uint32, which means 32-bit
#unsigned integer. Failure to use this type will
#cause a run-time error.
nlist = np.array([N]*10*N,dtype=np.uint32)


# In[6]:

#Initalize a random number generator with seed value of 101
rng = fp.GSLrng(101)


# In[7]:

#Simulate 40 replicate populations.  This uses C++11 threads behind the scenes:
pops = fp.evolve_regions(rng,       #The random number generator 
                         40,         #The number of pops to simulate = number of threads to use.
                         N,         #Initial population size for each of the 40 demes
                         nlist[0:], #List of population sizes over time.
                         0.005,     #Neutral mutation rate (per gamete, per generation)
                         0.01,      #Deleterious mutation rate (per gamete, per generation)
                         0.005,     #Recombination rate (per diploid, per generation)
                         nregions,  #Defined above
                         sregions,  #Defined above
                         recregions)#Defined above
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "m:e:H:S:O:N:t:s:r:F:n:")
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    N = 1000  # pop size
    e = 0.25  # s.d. of effect sizes
    S = 1  # V(S)
    H = None  # desired b-sense H^2
    mu_del_ttl = None  # Mutation rate (per gamete, per generation) to alleles affecting trait value
    r = 0.5  # rec. rate b/w loci (per diploid, per gen)
    Opt = 0.0  # Value of optimum after 10N gens
    ofile = None
    seed = 0
    t = None
    nsam = 100
    for o, a in opts:
        if o == '-m':
            mu_del_ttl = float(a)
        elif o == '-e':
            e = float(a)
        elif o == '-H':
            H = float(a)
        elif o == '-S':
            S = float(a)
        elif o == '-O':
            Opt = float(a)
        elif o == '-N':
            N = int(a)
        elif o == '-s':
            seed = int(a)
        elif o == '-r':
            r = float(a)
        elif o == '-F':
            ofile = a
        elif o == '-t':
            t = int(a)
        elif o == '-n':
            nsam = int(a)

    if H is None:
        usage()
        sys.exit(2)
    if mu_del_ttl is None:
        usage()
        sys.exit(2)
    if ofile is None:
        usage()
        sys.exit(2)
    if t is None:
        t = 0.1 * float(N)
    #Constants:
    NLOCI = 10
    NREPS = 64
    ##The next 2 are the 'sizes' of each locus in scaled params.  Roughly 100kb in 'humans' in terms of pi.
    theta = 100.0
    rho = 100.0

    #Can start working now:
    REP = 0
    out = pd.HDFStore(ofile, "w", complevel=6, complib='zlib')

    little_r_per_locus = rho / (4.0 * float(N))
    mu_n_region = theta / (4.0 * float(N))

    rnge = fp.GSLrng(seed)
    rngs = fp.GSLrng(seed)
    nlist = np.array([N] * (10 * N), dtype=np.uint32)
    fitness = qtm.MlocusAdditiveTrait()
    sregions = [fp.GaussianS(0, 1, 1, e, 1.0)] * NLOCI
    for BATCH in range(16):  #16*64=1024
        x = fp.MlocusPopVec(NREPS, N, NLOCI)
        sampler = fp.PopSampler(len(x), nsam, rngs)
        qtm.evolve_qtraits_mloc_sample_fitness(
            rnge,
            x,
            sampler,
            fitness,
            nlist,
            [mu_n_region] * NLOCI,
            [mu_del_ttl / float(NLOCI)] * NLOCI,
            sregions,
            [little_r_per_locus] * NLOCI,
            [0.5] * (NLOCI - 1),  #loci unlinked
            sample=t,
            VS=S)
        samples = sampler.get()
        get_summstats_parallel(samples, REP, out)
        sampler = fp.PopSampler(nsam, rngs)
        qtm.evolve_qtraits_mloc_sample_fitnes(
            rnge,
            x,
            sampler,
            fitness,
            nlist,
            [mu_n_region] * NLOCI,
            [mu_del_ttl / float(NLOCI)] * NLOCI,
            sregions,
            [little_r_per_locus] * NLOCI,
            [0.5] * (NLOCI - 1),  #loci unlinked
            sample=t,
            VS=S,
            optimum=Opt)
        samples = sampler.get()
        get_summstats_parallel(samples, REP, out)
        REP += NREPS
    out.close()
nregions = []
rregions = [fp.Region(0, 1, 1)]

reference_mu = 1e-3
recrate = 0.5
N = 1000
simlen = 10 * N
Nlist = np.array([N] * (simlen), dtype=np.uint32)
#grid of VS values.  1 is our reference value
relative_mu = [0.25, 0.5, 1.0, 5.0, 10.0]

reference_sigma = math.sqrt(0.05)
reference_vm = 2.0 * reference_mu * (math.pow(reference_sigma, 2.0))
reference_vg = 4 * reference_mu * 1.0
rng = fp.GSLrng(1525152)

#plot VG, ebar, tbar,max_expl over time
fig = plt.figure(figsize=(10, 10))
tbar = plt.subplot2grid((3, 2), (1, 0), rowspan=1, colspan=2)
VG = plt.subplot2grid((3, 2), (0, 0), rowspan=1, colspan=2)
sfs = plt.subplot2grid((3, 2), (2, 0), rowspan=1, colspan=2)

#hdf5 files to save the output for further plotting...
popstats_output = pd.HDFStore('popstats_vary_mu_VMconstant.h5',
                              'w',
                              complevel=6,
                              complib='zlib')
sfs_output = pd.HDFStore('sfs_vary_mu_VMconstant.h5',
                         'w',
                         complevel=6,
Esempio n. 5
0
#The next three lines process and compile our custom fitness module:
import pyximport
pyximport.install()
import test_fwdpy_extensions.test_custom_fitness as tfp
#import fwdpy and numpy as usual
import fwdpy as fp
import numpy as np

rng = fp.GSLrng(101)
rngs = fp.GSLrng(202)
p = fp.SpopVec(3, 1000)
s = fp.NothingSampler(len(p))

n = np.array([1000] * 1000, dtype=np.uint32)
nr = [fp.Region(0, 1, 1)]
sr = [fp.ExpS(0, 1, 1, 0.1)]

#Now, let's do some evolution with our 'custom' fitness functions:
fitness = tfp.AdditiveFitnessTesting()
fp.evolve_regions_sampler_fitness(rng, p, s, fitness, n, 0.001, 0.001, 0.001,
                                  nr, sr, nr, 1)

fitness = tfp.AaOnlyTesting()
fp.evolve_regions_sampler_fitness(rng, p, s, fitness, n, 0.001, 0.001, 0.001,
                                  nr, sr, nr, 1)

fitness = tfp.GBRFitness()
fp.evolve_regions_sampler_fitness(rng, p, s, fitness, n, 0.001, 0.001, 0.001,
                                  nr, sr, nr, 1)
Esempio n. 6
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:],"m:e:H:S:O:N:s:r:",["traj="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    N=1000   # pop size
    e = 0.25 # s.d. of effect sizes
    S = 1    # V(S)
    H = None # desired b-sense H^2
    mu_del_ttl = None # Mutation rate (per gamete, per generation) to alleles affecting trait value
    r = 0.5 # rec. rate b/w loci (per diploid, per gen)
    Opt = 0.0  # Value of optimum after 10N gens
    trajFile=None
    seed = 0
    for o,a in opts:
        if o == '-m':
            mu_del_ttl = float(a)
        elif o == '-e':
            e = float(a)
        elif o == '-H':
            H = float(a)
        elif o == '-S':
            S = float(a)
        elif o == '-O':
            Opt = float(a)
        elif o == '-N':
            N=int(a)
        elif o == '-s':
            seed = int(a)
        elif o == '-r':
            r = float(a)
        elif o == '--traj':
            trajFile=a

    if H is None:
        usage()
        sys.exit(2)
    if mu_del_ttl is None:
        usage()
        sys.exit(2)
    if trajFile is None:
        usage()
        sys.exit(2)
        
    #Constants:
    NLOCI=10
    NREPS=64
    ##The next 2 are the 'sizes' of each locus in scaled params.  Roughly 100kb in 'humans' in terms of pi.
    theta=100.0
    rho=100.0

    #Can start working now:
    REP=0
    out=pd.HDFStore(trajFile,"w",complevel=6,complib='zlib')

    little_r_per_locus = rho/(4.0*float(N))
    mu_n_region=theta/(4.0*float(N))
    
    rnge=fp.GSLrng(seed)
    nlist=np.array([N]*(10*N),dtype=np.uint32)
    fitness = qtm.MlocusAdditiveTrait()
    sregions=[fp.GaussianS(0,1,1,e,1.0)]*NLOCI
    for BATCH in range(16): #16*64=1024
        x = fp.MlocusPopVec(NREPS,N,NLOCI)

        sampler=fp.FreqSampler(len(x))
        qtm.evolve_qtraits_mloc_sample_fitness(rnge,x,sampler,fitness,nlist,
                                               [mu_n_region]*NLOCI,
                                               [mu_del_ttl/float(NLOCI)]*NLOCI,
                                               sregions,
                                               [little_r_per_locus]*NLOCI,
                                               [0.5]*(NLOCI-1),#loci unlinked
                                               sample=1,VS=S)
        traj1=sampler.get()
        sampler=fp.FreqSampler(len(x))
        qtm.evolve_qtraits_mloc_sample_fitness(rnge,x,sampler,fitness,nlist,
                                               [mu_n_region]*NLOCI,
                                               [mu_del_ttl/float(NLOCI)]*NLOCI,
                                               sregions,
                                               [little_r_per_locus]*NLOCI,
                                               [0.5]*(NLOCI-1),#loci unlinked
                                               sample=1,VS=S,optimum=Opt)
        traj2=sampler.get()
        m=fp.tidy_trajectories(fp.merge_trajectories(traj1,traj2))
        for i in m:
            temp=pd.DataFrame(i)
            temp['rep']=[REP]*len(temp.index)
            out.append('traj',temp)
            REP+=1

    out.close()
Esempio n. 7
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:],"m:e:H:S:O:N:s:r:",["fixed=","ages=","traj="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    #set up default params
    N=1000   # pop size
    e = 0.25 # s.d. of effect sizes
    S = 1    # V(S)
    H = None # desired b-sense H^2
    m = None # Mutation rate (per gamete, per generation) to alleles affecting trait value
    r = 0.5 # rec. rate (per diploid, per gen)
    Opt = 0.0  # Value of optimum after 10N gens
    fixationsFile=None
    lostFile=None
    trajFile=None
    seed = 0
    for o,a in opts:
        if o == '-m':
            m = float(a)
        elif o == '-e':
            e = float(a)
        elif o == '-H':
            H = float(a)
        elif o == '-S':
            S = float(a)
        elif o == '-O':
            Opt = float(a)
        elif o == '-N':
            N=int(a)
        elif o == '-s':
            seed = int(a)
        elif o == '-r':
            r = float(a)
        elif o == '--fixed':
            fixationsFile=a
        elif o == '--ages':
            lostFile=a
        elif o == '--traj':
            trajFile=a

    if H is None:
        usage()
        sys.exit(2)
    if m is None:
        usage()
        sys.exit(2)
    if fixationsFile is None or lostFile is None or trajFile is None:
        usage()
        sys.exit(2)

    rng = fp.GSLrng(seed)
    hdf_fixed = pd.HDFStore(fixationsFile,'w',complevel=6,complib='zlib')
    hdf_fixed.open()
    hdf_lost = pd.HDFStore(lostFile,'w',complevel=6,complib='zlib')
    hdf_lost.open()
    hdf_traj = pd.HDFStore(trajFile,'w',complevel=6,complib='zlib')
    hdf_traj.open()
    sigE = get_sigE_additive(m,S,H)

    nregions = []
    recregions = [fp.Region(0,1,1)]
    sregions = [fp.GaussianS(0,1,1,e)]
    #population size over time -- constant & we re-use this over and over
    nlist = np.array([N]*(10*N),dtype=np.uint32)

    REPLICATE=0
    #16 batches of 64 runs = 1024 replicates
    for i in range(16):
        #set up populations
        pops=fp.SpopVec(64,N)
        #Evolve to equilibrium
        sampler = fp.FreqSampler(len(pops))
        qt.evolve_regions_qtrait_sampler(rng,
                                         pops,
                                         sampler,
                                         nlist[0:],
                                         0,
                                         m,
                                         r,
                                         nregions,sregions,recregions,
                                         sigmaE=sigE,
                                         sample=1,
                                         VS=S) ##Do not track popstats during "burn-in"

        traj1=sampler.get()
        sampler = fp.FreqSampler(len(pops))
        #evolve for another 10N generations at new optimum
        qt.evolve_regions_qtrait_sampler(rng,pops,sampler,
                                         nlist[0:],
                                         0,
                                         m,
                                         r,
                                         nregions,sregions,recregions,
                                         sigmaE=sigE,
                                         VS=S,optimum=Opt,sample=1)
        traj2=sampler.get()
        AGES=[]
        FIXATIONS=[]
        #merge trajectories and get allele ages (parallelized via open MP)
        traj1=fp.merge_trajectories(traj1,traj2)
        ages = fp.allele_ages(traj1)
        REPTEMP=REPLICATE
        for ai in range(len(ages)):
            dfi=pd.DataFrame(ages[ai])
            dfi['rep']=[REPTEMP]*len(dfi.index)
            FIXATIONS.append(dfi[dfi['max_freq']==1.0])
            AGES.append(dfi[dfi['max_freq']<1.0])
            REPTEMP+=1
        # for j in range(len(pops)):
        #     #Merge all trajectories for this replicate
        #     df = pd.concat([pd.DataFrame(traj1[j]),
        #                     pd.DataFrame(traj2[j])])
        #     for name,group in df.groupby(['pos','esize']):
        #         if group.freq.max() < 1:  #mutation did not reach fixation...
        #             if group.generation.max()-group.generation.min()>1: #... and it lived > 1 generation ...
        #                AGES.append({'rep':REPLICATE,
        #                             'esize':name[1],
        #                             'origin':group.generation.min(),
        #                             'final_g':group.generation.max(),
        #                             'max_q':group.freq.max(),
        #                             'last_q':group.freq.iloc[-1]})
        #         else: #mutation did reach fixation!
        #             FIXATIONS.append({'rep':REPLICATE,
        #                               'esize':name[1],
        #                               'origin':group.generation.min(),
        #                               'final_g':group.generation.max()})
        #     REPLICATE+=1

        ##Add more info into the trajectories
        for t in traj1:
            LD=[]
            for i in t:
                I=int(0)
                for j in i[1]:
                    x=copy.deepcopy(i[0])
                    x['freq']=j
                    x['generation']=i[0]['origin']+I
                    I+=1
                    LD.append(x)
            d=pd.DataFrame(LD)
            d['rep']=[REPLICATE]*len(d.index)
            REPLICATE+=1
            hdf_traj.append('trajectories',d)

        hdf_fixed.append('fixations',pd.concat(FIXATIONS))
        hdf_lost.append('allele_ages',pd.concat(AGES))

    hdf_fixed.close()
    hdf_lost.close()
    hdf_traj.close()
Esempio n. 8
0
 def testSplitOutOfRange(self):
     m = fp.MetaPopVec(64, [100])
     rng = fp.GSLrng(101)
     with self.assertRaises(IndexError):
         demog.split_pops(rng, m, 2, 50)
Esempio n. 9
0
 def testAdmixOutOfRange(self):
     m = fp.MetaPopVec(64, [100, 100])
     rng = fp.GSLrng(101)
     with self.assertRaises(IndexError):
         demog.admix_pops(rng, m, 0, 2, 0.1, 100)
Esempio n. 10
0
import fwdpy as fp
import fwdpy.qtrait_mloc as qtm
import fwdpy.fitness as fpw
import numpy as np
import pandas as pd
#import matplotlib
#import matplotlib.pyplot as plt
import copy,sys

N=1000
NLOCI=10
NREPS=20

rnge=fp.GSLrng(100)
nlist=np.array([N]*(15*N),dtype=np.uint32)
theta_neutral_per_locus=0.0
rho_per_locus=100.0
little_r_per_locus=rho_per_locus/(4.0*float(N))
mu_n_region=theta_neutral_per_locus/(4.0*float(N))
mu_del_ttl=1e-3

#sigmas=[0.1]*NLOCI

hdf=pd.HDFStore("cumVA.h5",'w')

fmodel = fpw.MlocusAdditive()
sregions = []
for i in range(NLOCI):
    sregions.append(fp.GaussianS(i,i+1,1,0.1))
    
REPLICATE=0
Esempio n. 11
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "m:e:H:S:O:N:t:s:F:r:n:d:", [
            "theta=", "rho=", "trait=", "sampler=", "nsam=", "cores=",
            "batches=", "nstub=", "sstub=", "nloci=", "fixations=", "t2=",
            "g2="
        ])  #,"neutral="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    #set up default params
    N = 1000  # pop size
    t = None  # 0.1N
    t2 = None
    e = 0.25  # s.d. of effect sizes
    S = 1  # V(S)
    H = 1.0  # desired b-sense H^2
    m = None  # Mutation rate (per gamete, per generation) to alleles affecting trait value
    r = 0.5  #Recombination rate between locis
    Opt = 0.0  # Value of optimum after 10N gens
    ofile = None
    seed = 0
    NLOCI = 10  #The total number of loci to simulate
    #NeutralLocus = 5 #The index of the locus where no causal mutations will occur
    theta = 100  # mutational size of region where neutral mutations occur
    rho = 100  # recombination size of region where neutral mutations occur
    traitString = "additive"
    samplerString = None
    ncores = 64
    nbatches = 16
    dominance = 1.0
    ssize = None
    fixationsFileName = None
    G2 = None
    nstub = None
    sstub = None
    for o, a in opts:
        if o == '-m':
            m = float(a)
        elif o == '-e':
            e = float(a)
        elif o == '-H':
            H = float(a)
        elif o == '-S':
            S = float(a)
        elif o == '-O':
            Opt = float(a)
        elif o == '-N':
            N = int(a)
        elif o == '-t':
            t = int(a)
            t2 = t
        elif o == '-s':
            seed = int(a)
        elif o == '-F':
            ofile = a
        elif o == '-r':
            r = float(a)
        elif o == '--nsam':
            ssize = int(a)
        elif o == '--theta':
            theta = float(a)
        elif o == '--rho':
            rho = float(a)
        elif o == '--trait':
            traitString = a
            if a not in valid_trait_models:
                print("Error: invalid trait model")
                usage()
                sys.exit(0)
        elif o == '--sampler':
            samplerString = a
            if a not in valid_sampler_names:
                print("Error: invalid sampler name")
                usage()
                sys.exit(0)
        elif o == '--cores':
            ncores = int(a)
            if ncores < 1:
                print("--ncores must be > 0")
                usage()
                sys.exit(0)
        elif o == '--batches':
            nbatches = int(a)
            if nbatches < 1:
                print("--nbatches myst be > 0")
                usage()
                sys.exit(0)
        elif o == '--nloci':
            NLOCI = int(a)
            if NLOCI < 2:
                rpint("--nloci must be > 1")
                usage()
                sys.exit(0)
        elif o == "--fixations":
            fixationsFileName = a
        elif o == "--t2":
            t2 = int(a)
        elif o == "--g2":
            G2 = int(a)
        elif o == "--nstub":
            nstub = a
        elif o == '--sstub':
            sstub = a
        #elif o == "--neutral":
        #    if a != "None":
        #        NeutralLocus=int(a)
        #    else:
        #        NeutralLocus=a
    if samplerString is None:
        print("Error: sampler must be defined")
        usage()
        sys.exit(0)

    if t is None:
        print("Error: sampling interval must be defined")
        usage()
        sys.exit(0)
    if t2 is None:
        t2 = t
    if m is None:
        usage()
        sys.exit(2)
    if ofile is None:
        usage()
        sys.exit(2)
    if G2 is None:
        G2 = 10 * N

    #Can start working now:
    REP = 0
    out = pd.HDFStore(ofile, "w", complevel=6, complib='zlib')

    if fixationsFileName is not None:
        if os.path.exists(fixationsFileName):
            os.remove(fixationsFileName)

    little_r_per_locus = rho / (4.0 * float(N))
    mu_n_region = theta / (4.0 * float(N))

    rnge = fp.GSLrng(seed)
    rngs = fp.GSLrng(seed)
    nlist = np.array([N] * (10 * N), dtype=np.uint32)
    fitness = get_trait_model(traitString)
    sregions = [fp.GaussianS(0, 1, 1, e, dominance)] * NLOCI

    neutral_mut_rates = [mu_n_region] * NLOCI
    causal_mut_rates = [m / float(NLOCI)] * NLOCI
    #if NeutralLocus is not None:
    #    causal_mut_rates[NeutralLocus]=0.0
    for BATCH in range(nbatches):
        x = fp.MlocusPopVec(ncores, N, NLOCI)
        nstub_t = None
        sstub_t = None
        if nstub is not None:
            nstub_t = nstub + b'.pre_shift.batch' + str(BATCH)
        if sstub is not None:
            sstub_t = sstub + b'.pre_shift.batch' + str(BATCH)
        sampler = get_sampler(samplerString, len(x), Opt, ssize, rngs, nstub_t,
                              sstub_t)
        qtm.evolve_qtraits_mloc_sample_fitness(
            rnge,
            x,
            sampler,
            fitness,
            nlist,
            neutral_mut_rates,
            causal_mut_rates,
            sregions,
            [little_r_per_locus] * NLOCI,
            [r] * (NLOCI - 1),  #loci unlinked
            sample=t,
            VS=S)
        write_output(sampler, out, NLOCI, REP)

        if nstub is not None:
            nstub_t = nstub + b'.post_shift.batch' + str(BATCH)
        if sstub is not None:
            sstub_t = sstub + b'.post_shift.batch' + str(BATCH)
        sampler = get_sampler(samplerString, len(x), Opt, ssize, rngs, nstub_t,
                              sstub_t)
        qtm.evolve_qtraits_mloc_sample_fitness(
            rnge,
            x,
            sampler,
            fitness,
            nlist[:G2],
            neutral_mut_rates,
            causal_mut_rates,
            sregions,
            [little_r_per_locus] * NLOCI,
            [r] * (NLOCI - 1),  #loci unlinked
            sample=t2,
            VS=S,
            optimum=Opt)
        write_output(sampler, out, NLOCI, REP)
        if fixationsFileName is not None:
            write_fixations(x, fixationsFileName, REP)
        REP += len(x)

    if fixationsFileName is not None:
        con = sqlite3.connect(fixationsFileName)
        con.execute("create index gen on fixations (generation)")
        con.execute("create index gen_rep on fixations (generation,rep)")
        con.close()
    out.close()
Esempio n. 12
0
import unittest
import fwdpy
import numpy as np

nregions = [fwdpy.Region(0, 1, 1), fwdpy.Region(2, 3, 1)]
sregions = [fwdpy.ExpS(1, 2, 1, -0.1), fwdpy.ExpS(1, 2, 0.01, 0.001)]
rregions = [fwdpy.Region(0, 3, 1)]
rng = fwdpy.GSLrng(100)
N = 1000
NGENS = 100
popsizes = np.array([N], dtype=np.uint32)
popsizes = np.tile(popsizes, NGENS)
pops = fwdpy.evolve_regions(rng, 1, N, popsizes[0:], 0.001, 0.0001, 0.001,
                            nregions, sregions, rregions)

#The sum of the gamete counts must be 2*(deme size):
#mpops = fwdpy.evolve_regions_split(rng,pops,popsizes[0:],popsizes[0:],0.001,0.0001,0.001,nregions,sregions,rregions,[0]*2)


class test_singlepop_views(unittest.TestCase):
    def testNumGametes(self):
        gams = fwdpy.view_gametes(pops[0])
        nsingle = 0
        for i in gams:
            nsingle += i['n']
        self.assertEqual(nsingle, 2000)

    def testDipsize(self):
        dips_single = fwdpy.view_diploids(pops[0], [0, 1, 2])
        self.assertEqual(len(dips_single), 3)
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "m:e:H:S:O:N:t:s:F:r:",
                                   ["cores=", "batches="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    #set up default params
    N = 1000  # pop size
    t = None  # 0.1N
    e = 0.25  # s.d. of effect sizes
    S = 1  # V(S)
    H = None  # desired b-sense H^2
    m = None  # Mutation rate (per gamete, per generation) to alleles affecting trait value
    r = 0.5  # rec. rate (per diploid, per gen)
    Opt = 0.0  # Value of optimum after 10N gens
    ofile = None
    seed = 0
    ncores = 64
    nbatches = 16
    for o, a in opts:
        if o == '-m':
            m = float(a)
        elif o == '-e':
            e = float(a)
        elif o == '-H':
            H = float(a)
        elif o == '-S':
            S = float(a)
        elif o == '-O':
            Opt = float(a)
        elif o == '-N':
            N = int(a)
        elif o == '-t':
            t = int(a)
        elif o == '-s':
            seed = int(a)
        elif o == '-F':
            ofile = a
        elif o == '-r':
            r = float(a)
        elif o == '--cores':
            ncores = int(a)
        elif o == '--batches':
            nbatches = int(a)

    if t is None:
        t = int(0.1 * float(N))
    if H is None:
        usage()
        sys.exit(2)
    if m is None:
        usage()
        sys.exit(2)
    if ofile is None:
        usage()
        sys.exit(2)

    rng = fp.GSLrng(seed)
    hdf = pd.HDFStore(ofile, 'w', complevel=6, complib='zlib')
    hdf.open()

    sigE = get_sigE_additive(m, S, H)

    nregions = []
    recregions = [fp.Region(0, 1, 1)]
    sregions = [fp.GaussianS(0, 1, 1, e)]
    #population size over time -- constant & we re-use this over and over
    nlist = np.array([N] * (10 * N), dtype=np.uint32)
    #16 batches of 64 runs = 1024 replicates
    fitness = qt.SpopAdditiveTrait()
    REPLICATE = 0
    for i in range(nbatches):
        pops = fp.SpopVec(ncores, N)
        sampler = fp.QtraitStatsSampler(len(pops), 0.0)
        #Evolve to equilibrium, tracking along the way
        qt.evolve_regions_qtrait_sampler_fitness(rng,
                                                 pops,
                                                 sampler,
                                                 fitness,
                                                 nlist[0:],
                                                 0,
                                                 m,
                                                 r,
                                                 nregions,
                                                 sregions,
                                                 recregions,
                                                 sigmaE=sigE,
                                                 sample=t,
                                                 VS=S,
                                                 optimum=0)
        stats = sampler.get()
        RTEMP = REPLICATE
        for si in stats:
            ti = pd.DataFrame(si)
            ti['rep'] = [RTEMP] * len(ti.index)
            RTEMP += 1
            hdf.append('popstats', ti)
        #simulate another 10*N generations, sampling stats every 't' generations
        sampler = fp.QtraitStatsSampler(len(pops), Opt)
        qt.evolve_regions_qtrait_sampler_fitness(rng,
                                                 pops,
                                                 sampler,
                                                 fitness,
                                                 nlist[0:],
                                                 0,
                                                 m,
                                                 r,
                                                 nregions,
                                                 sregions,
                                                 recregions,
                                                 sigmaE=sigE,
                                                 sample=t,
                                                 VS=S,
                                                 optimum=Opt)
        stats = sampler.get()
        for si in stats:
            ti = pd.DataFrame(si)
            ti['rep'] = [REPLICATE] * len(ti.index)
            hdf.append('popstats', ti)
            REPLICATE += 1

    hdf.close()
Esempio n. 14
0
def main():
    parser=make_parser()
    args=parser.parse_args(sys.argv[1:])

    if args.verbose:
        print (args)
    ##Figure out sigma_E from params
    sigE = get_sigE_additive(args.mutrate,args.VS,args.H2)

    trait = get_trait_model(args.trait)

    nlist = np.array([args.popsize]*(10*args.popsize),dtype=np.uint32)
    rng=fp.GSLrng(args.seed)
    mu_neutral = 0.0
    nregions=[]
    sregions=[fp.GaussianS(0,1,1,args.sigmu,args.dominance)]
    recregions=[fp.Region(0,1,1)]
    
    if args.sampler == 'stats':
        output=pd.HDFStore(args.outfile,'w',complevel=6,complib='zlib')
        output.close()
    REPID=0
    for BATCH in range(args.nbatches):
        pops=fp.SpopVec(args.ncores,args.popsize)
        sampler=get_sampler_type(args.sampler,args.trait,len(pops),0.0)
        qt.evolve_regions_qtrait_sampler_fitness(rng,pops,sampler,trait,
                                                 nlist,
                                                 0.0,
                                                 args.mutrate,
                                                 args.recrate,
                                                 nregions,
                                                 sregions,
                                                 recregions,
                                                 args.tsample,
                                                 sigE,
                                                 optimum=0.0,
                                                 VS=args.VS)
        if args.sampler != 'freq':
            if args.sampler == 'freq':
                dummy=write_output(sampler,args,REPID,BATCH,'w')
            elif args.sampler == 'stats':
                dummy=write_output(sampler,args,REPID,BATCH,'a')
            else:
                dummy=write_output(sampler,args,REPID,BATCH,'a')
            sampler=get_sampler_type(args.sampler,args.trait,len(pops),args.optimum)
        qt.evolve_regions_qtrait_sampler_fitness(rng,pops,sampler,trait,
                                                 nlist,
                                                 0.0,
                                                 args.mutrate,
                                                 args.recrate,
                                                 nregions,
                                                 sregions,
                                                 recregions,
                                                 args.tsample,
                                                 sigE,
                                                 optimum=args.optimum,
                                                 VS=args.VS)
        if args.sampler == 'freq':
            #Append this time!
            REPID=write_output(sampler,args,REPID,BATCH,'w')
        elif args.sampler == 'stats':
            REPID=write_output(sampler,args,REPID,BATCH,'a')
        else:
            REPID=write_output(sampler,args,REPID,BATCH,'a')
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "m:e:H:S:O:N:t:s:F:r:n:",
                                   ["theta=", "rho="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    #set up default params
    N = 1000  # pop size
    t = None  # 0.1N
    e = 0.25  # s.d. of effect sizes
    S = 1  # V(S)
    H = None  # desired b-sense H^2
    m = None  # Mutation rate (per gamete, per generation) to alleles affecting trait value
    r = 0.5  # rec. rate (per diploid, per gen)
    Opt = 0.0  # Value of optimum after 10N gens
    ofile = None
    seed = 0
    ssize = 50  #sample size
    theta = 100  # mutational size of region where neutral mutations occur
    rho = 100  # recombination size of region where neutral mutations occur
    for o, a in opts:
        if o == '-m':
            m = float(a)
        elif o == '-e':
            e = float(a)
        elif o == '-H':
            H = float(a)
        elif o == '-S':
            S = float(a)
        elif o == '-O':
            Opt = float(a)
        elif o == '-N':
            N = int(a)
        elif o == '-t':
            t = int(a)
        elif o == '-s':
            seed = int(a)
        elif o == '-F':
            ofile = a
        elif o == '-r':
            r = float(a)
        elif o == '-n':
            ssize = int(a)
        elif o == '--theta':
            theta = float(a)
        elif o == '--rho':
            rho = float(a)

    if t is None:
        t = 0
    if H is None:
        usage()
        sys.exit(2)
    if m is None:
        usage()
        sys.exit(2)
    if ofile is None:
        usage()
        sys.exit(2)

    ##Figure out sigma_E from params
    sigE = get_sigE_additive(m, S, H)

    rng = fp.GSLrng(seed)
    rng2 = fp.GSLrng(seed * 100)
    PIK = gzip.open(ofile, "wb")
    #16 batches of 64 runs = 1024 replicates

    mun = theta / float(4 * N)
    littler = rho / float(4 * N)

    nreg = make_neutral_region()
    recstuff = make_buried_rec_region(littler)
    sreg = make_Gaussian_sregion(recstuff['beg'], recstuff['end'], sigma=e)
    rrg = recstuff['region']

    nlist = np.array([N] * (10 * N), dtype=np.uint32)
    for i in range(16):
        #Evolve to equilibrium
        pops = qt.evolve_regions_qtrait(
            rng,
            64,
            N,
            nlist[0:],
            mun,
            m,
            r,
            nreg,
            sreg,
            rrg,
            sigmaE=sigE,
            f=0.,
            VS=S,
            optimum=0)  ##Do not track popstats during "burn-in"

        #simulate another 2*N generations, sampling stats every 't' generations
        sampler = fp.PopSampler(len(pops), ssize, rng2)
        qt.evolve_regions_qtrait_sampler(rng,
                                         pops,
                                         sampler,
                                         nlist[0:],
                                         mun,
                                         m,
                                         r,
                                         nreg,
                                         sreg,
                                         rrg,
                                         sigmaE=sigE,
                                         sample=t,
                                         VS=S,
                                         optimum=Opt)
        samples = sampler.get()
        print samples
        for j in samples:
            pickle.dump(j, PIK)

    PIK.close()
Esempio n. 16
0
def run_batch(argtuple):
    args, repid, batch = argtuple
    print("seed for batch = ", args.seed)
    nstub = "neutral.mu" + str(args.mu) + ".opt" + str(args.opt)
    sstub = "selected.mu" + str(args.mu) + ".opt" + str(args.opt)
    rnge = fp.GSLrng(args.seed)

    NANC = 7310
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    nregions = [
        fp.Region(j[0], j[1], args.theta / (4. * float(NANC)), coupled=True)
        for i, j in zip(range(args.nloci), locus_boundaries)
    ]
    recregions = [
        fp.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
        for i, j in zip(range(args.nloci), locus_boundaries)
    ]
    sregions = [
        fp.GaussianS(j[0] + 5., j[0] + 6., args.mu, args.sigmu, coupled=False)
        for i, j in zip(range(args.nloci), locus_boundaries)
    ]
    f = qtm.MlocusAdditiveTrait()

    nlist = np.array(get_nlist1(), dtype=np.uint32)
    pops = fp.MlocusPopVec(args.ncores, nlist[0], args.nloci)
    sampler = fp.NothingSampler(len(pops))
    d = datetime.datetime.now()
    print("starting batch, ", batch, "at ", d.now())
    qtm.evolve_qtraits_mloc_regions_sample_fitness(rnge, pops, sampler, f,
                                                   nlist[0:], nregions,
                                                   sregions, recregions,
                                                   [0.5] * (args.nloci - 1), 0,
                                                   0, 0.)
    d = datetime.datetime.now()
    print(d.now())
    nlist = np.array(get_nlist2(), dtype=np.uint32)
    qtm.evolve_qtraits_mloc_regions_sample_fitness(rnge, pops, sampler, f,
                                                   nlist[0:], nregions,
                                                   sregions, recregions,
                                                   [0.5] * (args.nloci - 1), 0,
                                                   args.opt)
    d = datetime.datetime.now()
    print(d.now())
    if args.statfile is not None:
        sched = lsp.scheduler_init(args.TBB)
    for pi in pops:
        #Apply the sampler 1 population at a time.
        #This saves a fair bit of RAM.
        neutralFile = nstub + '.rep' + str(repid) + '.gz'
        selectedFile = sstub + '.rep' + str(repid) + '.gz'
        BIGsampler = fp.PopSampler(1,
                                   6000,
                                   rnge,
                                   False,
                                   neutralFile,
                                   selectedFile,
                                   recordSamples=True,
                                   boundaries=locus_boundaries)
        fp.apply_sampler_single(pi, BIGsampler)
        if args.statfile is not None:
            for di in BIGsampler:
                process_samples((di, args.statfile, locus_boundaries, repid))
        repid += 1
    pops.clear()
    pops = None