def test_discsim_n3(self): sim = discsim.Simulator(10) sim.sample = [None, (3, 2), (6, 4), (7, 0)] sim.event_classes = [ercs.DiscEventClass(u=0.5, r=1)] sim.run() pi, tau = sim.get_history() self.verify(3, pi, tau)
def test_discsim_n10_nonbinary(self): sim = discsim.Simulator(10) sim.sample = [None] + [(j, j) for j in range(10)] sim.event_classes = [ercs.DiscEventClass(u=0.99, r=2.5)] sim.run() pi, tau = sim.get_history() self.verify(10, pi, tau)
def run_var_sample(save_name): results = np.zeros( (len(sample_sizes), nr_runs, 2)) # Container for the data position_list = [(i + sample_steps / 2, j + sample_steps / 2) for i in range(0, grid_size, sample_steps) for j in range(0, grid_size, sample_steps)] '''Actual runs:''' row = 0 for k in sample_sizes: for j in range(0, nr_runs): # Single run: print("Doing run: %.1f for %.0f samples" % (j + 1, k)) trans = Unit_Transformer(grid_size, u, r) sim = discsim.Simulator(grid_size) # Create new Discsim-Simulator shuffle(position_list) # Randomize position List sim.sample = [None] + position_list[:k] # Set k random chromosomes sim.event_classes = [ercs.DiscEventClass(r, u, rate=grid_size**2) ] # Fall with constant rate per unit area sim.recombination_probability = recombination_rate sim.num_loci = num_loci sim.max_population_size = 100000 # Do the run. start = timer() for i in range(1, int(np.ceil( trans.to_model_time(time)))): # Update to generation time! sim.run(until=(i)) end = timer() print("\nRun time: %.2f s" % (end - start)) print("Total Generations: %.2f" % time) # Extract pedigrees and do Block detection pi, tau = sim.get_history() tau = trans.to_gen_time( np.array(tau)) # Vectorize and measure time in Gen time. det = IBD_Detector(tau, pi, recombination_rate, grid_size, position_list[:k], IBD_treshold) # Turn on a IBD_Detector det.IBD_detection() block_nr = len(det.IBD_list) print("Number of IBD-blocks detected %.2f" % block_nr) # Do Data mle_analysis of Blocks and extract sigma data = Analysis(det) # Do Data-Analysis and extract sigma! data.fit_expdecay(show=False) sigma = data.sigma_estimate print("Sigma Estimate: %.4f\n" % sigma) results[row, j, :] = (sigma, block_nr) row += 1 # Go one down in the results_row print("RUN COMPLETE!!") parameters = (sigma, grid_size, sample_sizes, "DISCSIM") pickle.dump((results, parameters), open(save_name, "wb")) # Pickle the data pickle.dump((results, parameters), open("DataE/test1.p", "wb")) # Pickle the data print("SAVED")
def empirical_IBD_list(save_name): '''Generate empirical IBD-list. Nr. of run times''' parameters = (sigma, grid_size, sample_steps, "DISCSIM") results = [] # Container for the data startlist = [(i + sample_steps / 2.0, j + sample_steps / 2.0) for i in range(0, grid_size, sample_steps) for j in range(0, grid_size, sample_steps)] trans = Unit_Transformer(grid_size, u, r) '''Actual runs:''' for i in range(nr_runs): print("Doing run: %i" % i) sim = discsim.Simulator(grid_size) # startlist = [j for i in zip(startlist, startlist) for j in i] # Do for diploids sim.sample = [None] + startlist sim.event_classes = [ercs.DiscEventClass(r, u, rate=grid_size**2) ] # Fall with constant rate per unit area sim.recombination_probability = recombination_rate sim.num_loci = num_loci sim.max_population_size = 100000 # Do the actual run for j in range(1, int(np.ceil( trans.to_model_time(time)))): # Update to generation time! print("Simulation %i Doing step: %i" % (i, j)) sim.run(until=(j)) pi, tau = sim.get_history() # Extract the necessary data tau = trans.to_gen_time( np.array(tau)) # Vectorize and measure time in Gen time. chrom_l = num_loci * recombination_rate * 100 det = IBD_Detector(tau, pi, recombination_rate, grid_size, startlist, IBD_treshold, time, chrom_l) # Turn on a IBD_Detector det.IBD_detection() pair_dist, pair_IBD, pair_nr = det.give_lin_IBD(bin_pairs=True) results.append([pair_dist, pair_IBD, pair_nr]) pickle.dump((np.array(results), parameters), open(save_name, "wb")) # Pickle the data print("SAVED")
os.makedirs("output/phyrex/LV/phyrex_output") if not os.path.exists("output/phyrex/LV/phyrex_input"): os.makedirs("output/phyrex/LV/phyrex_input") if not os.path.exists("output/LV/root_data"): os.makedirs("output/LV/root_data") for index in range(job_index * num_simulations, (job_index + 1) * num_simulations): if (not reRun_beast_only) or ( not os.path.exists("output/phyrex/LV/phyrex_input/phyrex" + str(index) + ".xml")): L = 100 ''' R is the diameter of the torus we are simulating on. This defines the size of the 1D or 2D space that lineages can move around in.''' sim = discsim.Simulator(L) #array for sample locations a = [None] #number of samples n = 100 x = np.zeros(n) y = np.zeros(n) for i in range(n): if index < 100: x[i] = (random.uniform(25, 75)) % L y[i] = (random.uniform(25, 75)) % L a.append((x[i], y[i])) else: x[i] = (random.uniform(45, 55)) % L
def single_run(run_i, u=u, nb=False): ''' Do a single run, parameters are saved in grid''' trans = Unit_Transformer(grid_size, u, r) sim = discsim.Simulator(grid_size) # Create new Discsim-Simulator startlist = [(i, j) for i in range(0, grid_size, sample_steps) for j in range(0, grid_size, sample_steps)] # startlist = [j for i in zip(startlist, startlist) for j in i] # Do for diploids sim.sample = [None] + startlist sim.event_classes = [ercs.DiscEventClass(r, u, rate=grid_size**2) ] # Fall with constant rate per unit area sim.recombination_probability = recombination_rate sim.num_loci = num_loci sim.max_population_size = 100000 # Do the run. start = timer() for i in range(1, int(np.ceil( trans.to_model_time(time)))): # Update to generation time! print("Simulation %.0f Doing step: %.0f u: %.3f" % (run_i, i, u)) sim.run(until=(i)) end = timer() print("\nRun time: %.2f s" % (end - start)) print("Total Generations: %.2f" % time) print("Transformation factor: 1 Time unit is %.3f generations:" % trans.to_gen_time(1.0)) # Extract pedigrees and do Block detection pi, tau = sim.get_history() # Extract the necessary data tau = trans.to_gen_time( np.array(tau)) # Vectorize and measure time in Gen time. chrom_l = num_loci * recombination_rate * 100 det = IBD_Detector(tau, pi, recombination_rate, grid_size, startlist, IBD_treshold, time, chrom_l) # Turn on a IBD_Detector ######################################################################################## if nb == False: # Do classic IBD-Detection det.IBD_detection() block_nr = len(det.IBD_list) print("Number of IBD-blocks detected %.2f" % block_nr) data = Analysis(det) # Do Data-Analysis and extract sigma! data.fit_expdecay(show=False) sigma0 = data.sigma_estimate return (sigma0, block_nr) elif nb == True: # Mode where effective Detection Values are returned: # det.IBD_detection() # block_nr0 = len(det.IBD_blocks) # print("Number of classic IBD-blocks detected %.2f" % block_nr0) # Do the MLE Inference # analysis = det.create_MLE_object() # analysis.create_mle_model("constant", chrom_l, [0.5, sigma]) # analysis.mle_analysis_error() # D0, sigma0 = analysis.estimates[0], analysis.estimates[1] # Do effective Detecition det.IBD_detection_eff() block_nr1 = len(det.IBD_blocks) print("Number of effective IBD-blocks detected %.2f" % block_nr1) # Do the MLE inference analysis = det.create_MLE_object() analysis.create_mle_model("constant", chrom_l, [0.5, sigma]) analysis.mle_analysis_error() D1, sigma1 = analysis.estimates[0], analysis.estimates[1] return (sigma1, D1, block_nr1)
""" import ercs import discsim import time import math from random import random import numpy as np start = time.clock() ##record the time of running the programme rad = 10 ''' rad is the diameter of the torus we are simulating on. This defines the size of the 1D or 2D space that lineages can move around in.''' sim = discsim.Simulator(rad) a = [None] ##sample n points uniformly in a unit square and centering at (rad/2,rad/2) n = 22 x = np.zeros(n) y = np.zeros(n) for i in range(n): x[i] = random() + rad / 2 - 0.5 y[i] = random() + rad / 2 - 0.5 a.append((x[i], y[i])) sim.sample = a ##According to the Latex file 'Diffusion_rate' provided, ##we are going to simulate the event class under disc model
def main(): '''The heart of the program.''' print("I missed you old buddy.") trans = Unit_Transformer(grid_size, u, r) sim = discsim.Simulator(grid_size) startlist = [(i + sample_steps / 2.0, j + sample_steps / 2.0) for i in range(0, grid_size, sample_steps) for j in range(0, grid_size, sample_steps)] # startlist = [j for i in zip(startlist, startlist) for j in i] # Do for diploids sim.sample = [None] + startlist sim.event_classes = [ercs.DiscEventClass(r, u, rate=grid_size**2) ] # Fall with constant rate per unit area sim.recombination_probability = recombination_rate sim.num_loci = num_loci sim.max_population_size = 100000 while True: inp = int( input( "\nWhat u wanna do bud?! \n (1) Run DISCSIM\n (2) Detect IBD\n (3) Analyze IBD-blocks \n " "(4) Load/Save Data\n (5) MLE-Analysis\n (6) Exit\n")) if inp == 1: start = timer() for i in range(1, time): print("Doing step: %2.f" % (i)) sim.run(until=(i)) end = timer() print("\nRun time: %.2f s" % (end - start)) print("Total Generations: %.2f" % trans.to_gen_time(time)) print("Transformation factor: 1 Time unit is %.3f generations:" % trans.to_gen_time(1.0)) pi, tau = sim.get_history() # Extract the necessary data tau = trans.to_gen_time( np.array(tau)) # Vectorize and measure time in Gen time. elif inp == 2: chrom_l = num_loci * recombination_rate * 100 det = IBD_Detector(tau, pi, recombination_rate, grid_size, startlist, IBD_treshold, time, chrom_l) # Turn on a IBD_Detector inp1 = int( input( "What mode?\n (1) Classic\n (2) Effective Recombination\n (3) Calculate inbreeding fraction\n" )) if inp1 == 1: det.IBD_detection() elif inp1 == 2: det.IBD_detection_eff() elif inp1 == 3: det.detect_inbreeding(input("Loop time: ")) print("Number of IBD-blocks detected %.2f" % len(det.IBD_blocks)) elif inp == 3: if det == 0: print("\n No IBD anaylsis, please do one") continue data = Analysis(det) print("\nData loaded: " + str(len(data.IBD_blocks)) + " IBD Blocks\n") print("Sigma: %.3f" % trans.sigma_calculator()) # Inner loop for this menue while True: inp1 = int( input( " (1) Block-Statistics \n (2) Generate Histogram \n (3) Plot exponential decay \n " "(4) Show IBD-List \n (5) Exponential Fit \n (6) Exit to main menu \n" )) if inp1 == 1: data.which_blocks() data.which_times() elif inp1 == 2: data.IBD_analysis() elif inp1 == 3: data.plot_expdecay(logy=False) elif inp1 == 4: print(data.IBD_blocks) elif inp1 == 5: data.fit_expdecay() elif inp1 == 6: break else: print("Input invalid!") elif inp == 4: inp1 = int( input( "\nSaving/Loading: \n(1) Save DISCSIM-data \n(2) Load DISCSIM-data " " \n(3) Save IBD-Data \n(4) Load IBD-Data \n(5) Exit\n")) if inp1 == 1: print("SAAAAAVE") pickle.dump((pi, tau), open("data.p", "wb")) # Pickle the data if inp1 == 2: print("LOOOOOOOAD") (pi, tau) = pickle.load(open("data.p", "rb")) print("LOOOOAAADED: \nLoci loaded: %.2f" % len(tau)) print("Nodes per locus: %.2f: " % len(tau[0])) if inp1 == 3: print("SAVE IBD-Data...") det.delete_history() # Wont need Pi and Tau anymore pickle.dump(det, open("IBD-data.p", "wb")) if inp1 == 4: print("Load IBD-Data...") det = pickle.load(open("IBD-data.p", "rb")) print("IBD blocks loaded: %.0f" % len(det.IBD_blocks)) elif inp == 5: analysis = det.create_MLE_object( bin_pairs=True) # Create the POPRES-MLE-Object while True: inp2 = input( "\n(1) Choose MLE-model \n(2) Run Fit\n(3) Bin-plot fitted data \n(4) Log-Likelihood surface" "\n(5) Jack-Knive Countries \n(7) Boots-Trap (Country Pairs) \n(8) Boots-Trap (Blocks)" "\n(9) Which times? \n(10) Analyze Residuals \n(0) Exit\n") if inp2 == 1: inp3 = input( "Which Model?\n(1) Constant \n(2) Doomsday" "\n(3) Power growth \n(4) Power-Const\n(0) Back\n") if inp3 == 1: analysis.create_mle_model("constant", det.chrom_l, [1.0, 2.0]) elif inp3 == 2: analysis.create_mle_model("doomsday", det.chrom_l, [200, 2.0]) elif inp3 == 3: analysis.create_mle_model("power_growth", det.chrom_l, [200, 2.0, 1.0]) elif inp3 == 4: analysis.create_mle_model("ddd") else: print("Invalid Input!! Please do again") elif inp2 == 2: analysis.mle_analysis_error() elif inp2 == 3: analysis.plot_fitted_data_error() elif inp2 == 4: analysis.plot_loglike_surface() elif inp2 == 5: analysis.jack_knife_ctries() elif inp2 == 7: bts_nr = input("How many boots traps?\n") analysis.boots_trap_ctry(bts_nr) elif inp2 == 8: bts_nr = input("How many boots traps?\n") analysis.boot_trap_blocks(bts_nr) elif inp2 == 9: analysis.which_times() elif inp2 == 10: analysis.calculate_pw_residuals() elif inp2 == 0: break elif inp == 6: print("Good bye horses. Im flying over you.") break else: print("U DRUNK")