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()
예제 #2
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()
예제 #3
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
for batch in range(1): #4,000 reps...
    pops = fp.MlocusPopVec(NREPS,N,NLOCI)
    sampler = fp.NothingSampler(len(pops))
    x = qtm.evolve_qtraits_mloc_sample_fitness(rnge,pops,sampler,fmodel,
                                               nlist[:(10*N)],
                                               [mu_n_region]*NLOCI,
                                               [mu_del_ttl/float(NLOCI)]*NLOCI,
                                               sregions,
                                               [little_r_per_locus]*NLOCI,
                                               [0.5]*(NLOCI-1),#loci unlinked
                                               sample=1)
    sampler = fp.VASampler(len(pops))
    qtm.evolve_qtraits_mloc_sample_fitness(rnge,pops,sampler,fmodel,
                                           nlist, #Evolve 15N gens past optimum shift
                                           [mu_n_region]*NLOCI,
                                           [mu_del_ttl/float(NLOCI)]*NLOCI,
                                           sregions,
예제 #4
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()
예제 #5
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