Beispiel #1
0
 def make_model_test_with_params(self):
     filenames = set(os.listdir('.'))
     g.make_parameters_file()
     new_filenames = set(os.listdir('.'))
     filename = [*new_filenames - filenames][0]
     try:
         g.make_model(filename)
     except:
         print("System error, fail make community test.")
Beispiel #2
0
    return p


# define a function to calculate the mean difference between phenotype
# and environment for a species
def calc_mean_z_e_diff(spp, trait_num=0):
    zs = spp._get_z().ravel()
    es = spp._get_e(lyr_num=spp.gen_arch.traits[trait_num].lyr_num)
    mean_diff = np.mean(np.abs(zs - es))
    return mean_diff


# set number of time steps for simulation
T = 1500

mod = gnx.make_model('./tests/validation/cline/cline_params.py')
#landscape and community will not be randomized between iterations, so I can
#just extract the non-neutral loci now
nonneut_loci = mod.comm[0].gen_arch.traits[0].loci
# create a data structure to store the z-e diffs at each time step
z_e_diffs = []
# burn the model
mod.walk(T=10000, mode='burn')
# store logistic regression of phenotypes on environment
corrs = []
corrs.append(
    sm.Logit(mod.comm[0]._get_z(), mod.comm[0]._get_e(0).reshape(
        (len(mod.comm[0]), 1))).fit())
# run the model for T timesteps
for t in range(T):
    # collect z-e diff
import msprime
import tskit
import geonomics as gnx
import numpy as np

# make a simple gnx model
dir = '/home/drew/Desktop/stuff/berk/research/projects/sim/geonomics'
file = '/GNX_default_model_params.py'
mod = gnx.make_model(dir + file)
mod.walk(1000, 'burn')

# reduce the population to just 3
mod.comm[0]._reduce(3)

# randomly choose 3 loci and make them fixed for either 0 or 1
#fixed_loci = np.random.choice(range(10), 3, replace=False)
#print(fixed_loci)
#for ind in mod.comm[0].values():
#    ind.g[fixed_loci, :] = np.array([0,0,1,1,0,0]).reshape((3,2))
# NOTE: setting all individuals' genotypes to all 0s
for ind in mod.comm[0].values():
    ind.g = np.zeros((mod.comm[0].gen_arch.L, mod.comm[0].gen_arch.x))
new_p = mod.comm[0].gen_arch.p[:]
#new_p[fixed_loci] = [0, 1, 0]
mod.comm[0].gen_arch.p = new_p

# grab all the genotypes
 # (axes: 0 = individuals, 1 = loci, 2 = homologues)
genotypes = np.stack([ind.g for ind in mod.comm[0].values()])
#segregating_sites = np.where(genotypes.sum(axis=2).sum(
#                                    axis=0)/(2 * len(mod.comm[0])) % 1 != 0)[0]
Beispiel #4
0
figsize = plt.rcParams['figure.figsize']
plt.rcParams['figure.figsize'] = [figsize[0], 2.5 * figsize[1]]

fig = plt.figure()
# plt.suptitle(('1-allele trajectories in a Wright-Fisher approximation '
#              'with %i independent loci') % (
#                  orig_params.comm.species.spp_0.gen_arch.L))
max_x = 0
# NOTE: run through K_factors from greatest downward, to make it
# easier to set all x-axes to the same maximum x-limit
for n, K_fact in enumerate(K_factors[::-1]):
    params = deepcopy(orig_params)
    params.comm.species['spp_0'].init['K_factor'] = K_fact
    print("USING K_fact %0.2f" % params.comm.species['spp_0'].init.K_factor)

    mod = gnx.make_model(params)

    mod.walk(mode='burn', T=10000, verbose=True)

    freqs = {loc: [] for loc in range(mod.comm[0].gen_arch.L)}
    # run model until all loci have fixed
    while (False not in [len(f) == 0 for f in freqs.values()]
           or False in [f[-1] in (0, 1) for f in freqs.values()]):
        freqs_t = get_allele_freqs(mod.comm[0])
        for loc, freq in freqs_t.items():
            freqs[loc].append(freq)
        # instead of using Walk-based movement, just randomly replace
        # individuals all over the map
        new_xs = np.random.uniform(low=0,
                                   high=mod.land.dim[1],
                                   size=len(mod.comm[0]))
# create data structures to store allele frequency trajectories,migrations,
# and phenotype-environment diff values for all selection coefficients
allele_freqs = {}
migration_rates = {}
z_e_diffs = {}

# create a dict to store the start and end phenotype-environment (z-e)
# correlations for each phi
z_e_corrs = {}

# for each strength of selection, create and run the model, tracking allele
# frequencies in both halves of the environment
for phi in phis:
    # create the model
    mod = gnx.make_model(('./tests/validation/divergence/'
                          'divergence_params.py'))
    mod.comm[0].gen_arch.traits[0].phi = phi
    # landscape and community will not be randomized between iterations,
    # so I can just extract the non-neutral loci now
    nonneut_loc = mod.comm[0].gen_arch.traits[0].loci

    # burn in the model
    mod.walk(mode='burn', T=200000, verbose=True)

    # create data structures to record allele frequencies in each half of the
    # landscape, migrations between the halves, and z-e diffs at each timestep
    allele_freqs_this_phi = {0: [], 1: []}
    migration_rates_this_phi = {(0, 1): [], (1, 0): []}
    z_e_diffs_this_phi = {0: calc_mean_z_e_diff(mod.comm[0])}

    # run and store the starting z-e correlation
Beispiel #6
0
 def make_model_test(self):
     try:
         g.make_model()
     except:
         print("System error, fail make model test without parameter.")
# set some plotting params
img_dir = ('/home/drew/Desktop/stuff/berk/research/projects/sim/methods_paper/'
           'img/final/')
titlesize = 20
axlabelsize = 18
ticklabelsize = 15

# set the data directory, and delete it if it already exists (so that we don't
# create mutliple, conflicting sets of data files)
data_dir = './GNX_mod-bottleneck_params'
if os.path.isdir(data_dir):
    shutil.rmtree(data_dir)

# make and run model
mod = gnx.make_model('./tests/validation/bottleneck/bottleneck_params.py')
mod.run(verbose=True)

# for each iteration
its_dirs = os.listdir(data_dir)
for it_dir in its_dirs:
    # read in the data
    files_dir = os.path.join(data_dir, it_dir, 'spp-spp_0')
    files = os.listdir(files_dir)
    vcf_files = [f for f in files if os.path.splitext(f)[1] == '.vcf']
    csv_files = [f for f in files if re.search('spp_0\\.csv$', f)]
    timesteps = [
        re.search('(?<=t\\-)\\d*(?=\\_spp)', f).group() for f in vcf_files
    ]
    timesteps = sorted([int(step) for step in timesteps])
    # create data structure to store allele frequencies
Beispiel #8
0
            if not np.isnan(r2):
                dist = np.sum(recomb_rates[i+1:j+1])
                r2s.append(r2)
                dists.append(dist)
    return(r2s, dists)


################################################################

# create data structures to save data to be analyzed/plotted
mean_fit = []
pi = []
ld = []

# build the model
mod = gnx.make_model('./tests/validation/sweep/sweep_params.py')

#get the non-neutral locus
nonneut_loc = mod.comm[0].gen_arch.traits[0].loci[0]
# NOTE: changing this to 0 because only the single non-neutral locus
# will be in each individ's genome array, so idx will be 0
nonneut_loc_idx = 0

# burn the model in
mod.walk(20000, 'burn', True)

# create the figure and its Axes instances
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
Beispiel #9
0
        print('\t\t' + str(new_params.comm.species.spp_0.init.K_factor))
        print('\tdim')
        print('\t\t' + str(new_params.landscape.main.dim))
        #print('\tL')
        #print('\t\t' + str(new_params.comm.species.spp_0.gen_arch.L))
        print('\tn_loci')
        print('\t\t' +
              str(new_params.comm.species.spp_0.gen_arch.traits.trt_0.n_loci))
        print('\tn_births_distr_lambda')
        print('\t\t' +
              str(new_params.comm.species.spp_0.mating.n_births_distr_lambda))
        print('\t' + '-' * 70)

        # get the start time
        # make the model
        mod = gnx.make_model(new_params)
        print('\tMODEL MADE')
        # burn it in
        mod.walk(1000000, 'burn', verbose=True)
        print('\tSTARTING MODEL...')
        print('\nEXAMPLE NONNEUT GENOME SHAPE' + str(mod.comm[0][
            mod.comm[0]._get_random_individuals(1)[0]].g.shape))
        start = time.time()
        # run the model
        mod.walk(T, 'main', verbose=True)
        # get the stop time, calculate the elapsed time, and append it to list
        stop = time.time()
        elapsed = stop - start
        mean_runtime = elapsed / T
        print('\tMODEL FINISHED')
        print('\tmean runtime: %0.6f' % mean_runtime)
import geonomics as gnx
import numpy as np

# FLAG DETERMINING WHETHER TO TEST TRAIT MUTATION OR DELETERIOUS MUTATION
mutate_trait = False

mod = gnx.make_model('./GNX_default_model_params.py')
mod.walk(10000, 'burn')
mod.walk(1)

spp = mod.comm[0]
ga = spp.gen_arch
re = ga.recombinations
trt = ga.traits[0]

off = [i.idx for i in spp.values() if i.age == 0]

ga.mutables = [*ga.neut_loci]
np.random.shuffle(ga.mutables)

if mutate_trait:
    #PRINT STUFF BEFOREHAND
    print('ga.nonneut_loci', ga.nonneut_loci)
    print('trait loci', ga.traits[0].loci)
    print('trait locus index', ga.traits[0].loc_idx)
    print('unmutated genome:\n', spp[off[0]].g)
    print('mutated genome:\n', spp[off[-1]].g)
    nonneut_loci_b4 = set([*ga.nonneut_loci])

    gnx.ops.mutation._do_nonneutral_mutation(spp, [off[-1]], trait_nums=[0])
    #PRINT STUFF AFTERWARD