def test_interleaved_migrations(self): t1 = 1.5 t2 = 10.5 t3 = 50.5 ts = msprime.simulate( Ne=1/4, samples=[ msprime.Sample(0, 0), msprime.Sample(1, t1), msprime.Sample(2, t2), msprime.Sample(3, t3)], population_configurations=[ msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), msprime.PopulationConfiguration()], demographic_events=[ msprime.MassMigration(time=t1, source=0, destination=1), msprime.MassMigration(time=t2, source=1, destination=2), msprime.MassMigration(time=t3, source=2, destination=3)]) t = next(ts.trees()) self.assertEqual(t.get_time(0), 0) self.assertEqual(t.get_time(1), t1) self.assertEqual(t.get_time(2), t2) self.assertEqual(t.get_time(3), t3) self.assertEqual(t.get_population(0), 0) self.assertEqual(t.get_population(1), 1) self.assertEqual(t.get_population(2), 2) self.assertEqual(t.get_population(3), 3) self.assertEqual(t.get_population(4), 1) self.assertEqual(t.get_population(5), 2) self.assertEqual(t.get_population(6), 3) self.assertTrue(t1 < t.get_time(4) < t2) self.assertTrue(t2 < t.get_time(5) < t3) self.assertGreater(t.get_time(6), t3)
def test_two_pops_multiple_samples(self): # Made absolutely sure that all samples have coalesced within # the source deme n = 10 t = 100 population_configurations = [ msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(0), ] demographic_events = [ msprime.MassMigration(time=t, source=0, destination=2), msprime.MassMigration(time=t, source=1, destination=2), ] ts = msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1) tree = next(ts.trees()) self.assertEqual(tree.get_root(), 2 * n - 2) self.assertGreater(tree.get_time(tree.get_root()), t) for j in range(n // 2): self.assertEqual(tree.get_population(j), 0) self.assertEqual(tree.get_population(n // 2 + j), 1) self.assertEqual(ts.get_population(j), 0) self.assertEqual(ts.get_population(n // 2 + j), 1) self.assertEqual(tree.get_population(tree.get_root()), 2) self.assertEqual(ts.get_samples(0), list(range(n // 2))) self.assertEqual(ts.get_samples(1), list(range(n // 2, n))) self.assertEqual(ts.get_samples(2), [])
def test_two_pops_single_sample(self): population_configurations = [ msprime.PopulationConfiguration(1), msprime.PopulationConfiguration(1), msprime.PopulationConfiguration(0), ] t = 5 demographic_events = [ msprime.MassMigration(time=t, source=0, destination=2), msprime.MassMigration(time=t, source=1, destination=2), ] ts = msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1, record_migrations=True) migrations = list(ts.migrations()) self.assertEqual(len(migrations), 2) m0 = migrations[0] m1 = migrations[1] self.assertEqual(m0.left, 0) self.assertEqual(m0.right, 1) self.assertEqual(m0.source, 0) self.assertEqual(m0.dest, 2) self.assertEqual(m0.time, t) self.assertEqual(m1.left, 0) self.assertEqual(m1.right, 1) self.assertEqual(m1.source, 1) self.assertEqual(m1.dest, 2) self.assertEqual(m1.time, t)
def migration_example(): n = 10 t = 1 population_configurations = [ msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(0), ] demographic_events = [ msprime.MassMigration(time=t, source=0, destination=2), msprime.MassMigration(time=t, source=1, destination=2), ] ts = msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1, mutation_rate=1, record_migrations=True, ) tables = ts.dump_tables() for j in range(n): tables.individuals.add_row(flags=j, location=(j, j), parents=(j - 1, j - 1)) return tables.tree_sequence()
def __init__(self): generation_time = 25 # Population sizes N_A = 7300 N_AF = 12300 N_B = 2100 N_EU0 = 1000 N_AS0 = 510 # Growth rates per generation r_EU = 0.4e-2 r_AS = 0.55e-2 # Migration rates m_AF_B = 25e-5 m_AF_EU = 3e-5 m_AF_AS = 1.9e-5 m_EU_AS = 9.6e-5 # Epoch times T_AF = 220e3/generation_time T_B = 140e3/generation_time T_EU_AS = 21.2e3/generation_time # Calculate population sizes at modern (T=0) time N_EUF = N_EU0 * math.exp(r_EU * T_EU_AS) N_ASF = N_AS0 * math.exp(r_AS * T_EU_AS) self.population_configurations = [ msprime.PopulationConfiguration( initial_size=N_AF, growth_rate=0), msprime.PopulationConfiguration( initial_size=N_EUF, growth_rate=r_EU), msprime.PopulationConfiguration( initial_size=N_ASF, growth_rate=r_AS) ] # Setup initial migration matrix self.migration_matrix = [ [0, m_AF_EU, m_AF_AS], [m_AF_EU, 0, m_EU_AS], [m_AF_AS, m_EU_AS, 0] ] self.demographic_events = [ # CEU and CHB merge into B, reset migration rates to Af-B, change pop size msprime.MassMigration(time=T_EU_AS, source=2, dest=1, proportion=1), msprime.MigrationRateChange(time=T_EU_AS, rate=0), msprime.MigrationRateChange(time=T_EU_AS, rate=m_AF_B, matrix_index=(0, 1)), msprime.MigrationRateChange(time=T_EU_AS, rate=m_AF_B, matrix_index=(1, 0)), msprime.PopulationParametersChange(time=T_EU_AS, initial_size=N_B, population_id=1, growth_rate=0), # B and AF merge, turn migration off, reset population size msprime.MassMigration(time=T_B, source=1, dest=0, proportion=1), msprime.MigrationRateChange(time=T_B, rate=0), # Ancestral size change, reset population size msprime.PopulationParametersChange(time=T_AF, initial_size=N_A, population_id=0) ]
def demo_event_at(t, e, pop_indexes, demo_events): # t is the time of the event # e is the event (split, merger, pulse, or pass), with the needed info # takes demo_events and appends events to end if e[0] == 'pass': demo_events.append( msprime.MassMigration(time=t, source=pop_indexes[e[2]], destination=pop_indexes[e[1]], proportion=1.)) elif e[0] == 'split': for source_pop in e[2:]: demo_events.append( msprime.MassMigration(time=t, source=pop_indexes[source_pop], destination=pop_indexes[e[1]], proportion=1.)) elif e[0] == 'merger': demo_events.append( msprime.MassMigration(time=t, source=pop_indexes[e[3]], destination=pop_indexes[e[1][0]], proportion=e[2][0])) demo_events.append( msprime.MassMigration(time=t, source=pop_indexes[e[3]], destination=pop_indexes[e[1][1]], proportion=1.)) elif e[0] == 'pulse': demo_events.append( msprime.MassMigration(time=t, source=pop_indexes[e[2]], destination=pop_indexes[e[1]], proportion=e[3])) return demo_events
def simulate(args, model, recombination_map, admixture_time): population_configurations = [ msprime.PopulationConfiguration(sample_size=args.sample_size, initial_size=args.Ne), msprime.PopulationConfiguration(sample_size=0, initial_size=args.Ne) ] demographic_events = [ msprime.MassMigration(time=admixture_time, source=0, dest=1, proportion=args.admixture_prop), msprime.MigrationRateChange(time=admixture_time, rate=0), msprime.MassMigration(time=admixture_time + 1, source=1, dest=0, proportion=1), msprime.PopulationParametersChange(time=admixture_time + 2, initial_size=1.0, population_id=0) ] replicates = msprime.simulate( recombination_map=recombination_map, demographic_events=demographic_events, population_configurations=population_configurations, model=model, record_migrations=True, num_replicates=args.replicates) return replicates
def neanderthal_admixture_model(num_modern=10000,anc_pop = 1, anc_num = 1, anc_time=900,mix_time=1000,split_time=12000,f=0.03,Ne0=10000,Ne1=2500,mu=1.5e-8,length=1000,num_rep=1000,coverage=False): #when is best time to sample Neanderthal? 100 gen before f? #error catching, leave there for now if f < 0 or f > 1: print "Admixture fraction is not in [0,1]" return None samples = [msp.Sample(population=0,time=0)]*num_modern #sample 1 neanderthal for comparison samples.extend([msp.Sample(population=anc_pop,time=anc_time)]*(anc_num)) pop_config = [msp.PopulationConfiguration(initial_size=Ne0),msp.PopulationConfiguration(initial_size=Ne1)] divergence = [msp.MassMigration(time=mix_time,source=0,destination=1,proportion = f), msp.MassMigration(time=split_time,source=1,destination=0,proportion=1.0)] sims = msp.simulate(samples=samples,Ne=Ne0,population_configurations=pop_config,demographic_events=divergence,mutation_rate=mu,length=length,num_replicates=num_rep) outfile = open('outfile.csv', 'w') outfile.write("Frequency") outfile.write('\n') freq = [] sim_num = 0 for sim in sims: for tree in sim.trees(): cur_node = len(samples)-1 # the very last leaf, when adding more modern pops make sure Neanderthal is still last while tree.get_time(tree.get_parent(cur_node)) < split_time: cur_node = tree.get_parent(cur_node) N_freq = (tree.get_num_leaves(cur_node) - 1) freq.append(N_freq) outfile.write(str(N_freq)) outfile.write("\n") outfile.write("\n") outfile.close() return np.array(freq)
def test_instantaneous_bottleneck_locations(self): population_configurations = [ msprime.PopulationConfiguration(100), msprime.PopulationConfiguration(100), msprime.PopulationConfiguration(100), ] strength = 1e6 # Make sure everyone coalescences. t1 = 0.0001 t2 = 0.0002 t3 = 0.0003 t4 = 0.0004 demographic_events = [ msprime.InstantaneousBottleneck(time=t1, population_id=0, strength=strength), msprime.InstantaneousBottleneck(time=t2, population_id=1, strength=strength), msprime.InstantaneousBottleneck(time=t3, population_id=2, strength=strength), msprime.MassMigration(time=t4, source=2, destination=0), msprime.MassMigration(time=t4, source=1, destination=0) ] ts = msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1) tree = next(ts.trees()) self.assertGreater(tree.get_time(tree.get_root()), t4) self.assertEqual(tree.get_population(tree.get_root()), 0) # The parent of all the samples from each deme should be in that deme. for pop in range(3): parents = [ tree.get_parent(u) for u in ts.get_samples(population_id=pop)] for v in parents: self.assertEqual(tree.get_population(v), pop)
def ts_sim(t_a, t_as, alpha, N1, N2, N3): t_s = 100 N1 = 200 pop_configs = [ msprime.PopulationConfiguration(sample_size=50, growth_rate=0, initial_size=N1), msprime.PopulationConfiguration(sample_size=50, growth_rate=0, initial_size=N2), msprime.PopulationConfiguration(sample_size=50, growth_rate=0, initial_size=N2), msprime.PopulationConfiguration(sample_size=50, growth_rate=0, initial_size=N3) ] demographic_events = [ msprime.MassMigration(time=t_as, source=2, dest=1, proportion=1), msprime.MassMigration(time=t_a, source=1, dest=0, proportion=alpha), msprime.MassMigration(time=t_a + 0.001, source=1, dest=3, proportion=1), msprime.CensusEvent(time=t_a + 0.002), msprime.MassMigration(time=t_s, source=3, dest=0, proportion=1) ] length = 248956422 recomb = 1.14856e-08 mutation_rate = 1.29e-08 ts = msprime.simulate(population_configurations=pop_configs, length=length, mutation_rate=mutation_rate, demographic_events=demographic_events, recombination_rate=recomb) return (ts)
def test_two_pops_single_sample(self): population_configurations = [ msprime.PopulationConfiguration(1), msprime.PopulationConfiguration(1), msprime.PopulationConfiguration(0), ] t = 5 demographic_events = [ msprime.MassMigration(time=t, source=0, destination=2), msprime.MassMigration(time=t, source=1, destination=2), ] ts = msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1) tree = next(ts.trees()) self.assertEqual(tree.get_root(), 2) self.assertGreater(tree.get_time(2), t) self.assertEqual(tree.get_population(0), 0) self.assertEqual(tree.get_population(1), 1) self.assertEqual(tree.get_population(2), 2) self.assertEqual(ts.get_population(0), 0) self.assertEqual(ts.get_population(1), 1) self.assertEqual(ts.get_samples(), [0, 1]) self.assertEqual(ts.get_samples(0), [0]) self.assertEqual(ts.get_samples(1), [1])
def struct0001(print_): N_A0 = 1e+04 N_B0 = 1e+04 T_A = 2e+04 T_B = 3e+04 # initially. population_configurations = [ msprime.PopulationConfiguration( sample_size=2, initial_size=N_A0, growth_rate=0), msprime.PopulationConfiguration( sample_size=0, initial_size=N_B0, growth_rate=0), ] migration_matrix = [[0,0],[0,0]] demographic_events = [ msprime.MassMigration( time=T_A, source = 0, dest=1,proportion=1), msprime.MassMigration( time=T_B, source=1, dest=0, proportion=1) ] # Use the demography debugger to print out the demographic history # that we have just described. dd = msprime.DemographyDebugger( population_configurations=population_configurations, migration_matrix=migration_matrix, demographic_events=demographic_events) if print_: dd.print_history() sim = msprime.simulate(population_configurations=population_configurations, migration_matrix=migration_matrix, demographic_events=demographic_events, length=3e+6, recombination_rate=2e-8, mutation_rate=2e-8,random_seed=50) return sim
def test_msprime_dtwf(self): migration_matrix = np.zeros((4, 4)) population_configurations = [ msprime.PopulationConfiguration( sample_size=10, initial_size=10, growth_rate=0 ), msprime.PopulationConfiguration( sample_size=10, initial_size=10, growth_rate=0 ), msprime.PopulationConfiguration( sample_size=10, initial_size=10, growth_rate=0 ), msprime.PopulationConfiguration( sample_size=0, initial_size=10, growth_rate=0 ), ] demographic_events = [ msprime.PopulationParametersChange(population=1, time=0.1, initial_size=5), msprime.PopulationParametersChange(population=0, time=0.2, initial_size=5), msprime.MassMigration(time=1.1, source=0, dest=2), msprime.MassMigration(time=1.2, source=1, dest=3), msprime.MigrationRateChange(time=2.1, rate=0.3, matrix_index=(2, 3)), msprime.MigrationRateChange(time=2.2, rate=0.3, matrix_index=(3, 2)), ] ts = msprime.simulate( migration_matrix=migration_matrix, population_configurations=population_configurations, demographic_events=demographic_events, random_seed=2, model="dtwf", ) self.verify_topologies(ts)
def set_demographic_events(self): ids = self.get_population_map() migrations = self.get_later_migrations() self.events = [] self.events += [ # CEU and CHB merge into B with rate changes at T_EU_AS msprime.MassMigration( time=self.T_EU_AS, source=ids['AS'], destination=ids['EU'], proportion=1.0), msprime.MigrationRateChange( time=self.T_EU_AS, rate=0), msprime.MigrationRateChange( time=self.T_EU_AS, rate=migrations.get('AF_B', 0), matrix_index=(ids['EU'], ids['AF'])), msprime.MigrationRateChange( time=self.T_EU_AS, rate=migrations.get('B_AF', 0), matrix_index=(ids['AF'], ids['EU'])), msprime.PopulationParametersChange( time=self.T_EU_AS, initial_size=self.N_B, growth_rate=0, population_id=ids['EU']) ] ids['B'] = ids['EU'] self.events += [ # Population B merges into YRI at T_B msprime.MassMigration( time=self.T_B, source=ids['B'], destination=ids['AF'], proportion=1.0), msprime.MassMigration( time=self.T_oAF_AF, source=ids['oAF'], destination=ids['AF'], proportion=1.0), # Size changes to N_A at T_AF msprime.PopulationParametersChange( time=self.T_AF, initial_size=self.N_A, population_id=ids['AF']) ]
def no_hybridization(positions, rates, mu=7.7e-07): """ num cyd mel-W mel-E | | | | | | | | | | |-----| | | | | | | | |-----------| | | | | | | | | |------------------| | """ # Coalescent units are the number of 2N generations time_units = 2.0 * 2000.0 # Divergence times tau1 = 0.5 tau2 = 1.5 tau3 = 4.0 # Recombination rates rmap = msp.RecombinationMap(positions, rates) # Set up population configurations population_configurations = [ msp.PopulationConfiguration(sample_size=10, initial_size=2000), msp.PopulationConfiguration(sample_size=10, initial_size=2000), msp.PopulationConfiguration(sample_size=10, initial_size=2000), msp.PopulationConfiguration(sample_size=4, initial_size=2000) ] # Set up demographic events demographic_events = [ msp.MassMigration(time=tau1 * time_units, source=0, destination=1, proportion=1.0), msp.MassMigration(time=tau2 * time_units, source=1, destination=2, proportion=1.0), msp.MassMigration(time=tau3 * time_units, source=2, destination=3, proportion=1.0) ] return ([time_units, tau1, tau2, tau3, mu], msp.simulate(mutation_rate=mu, recombination_map=rmap, population_configurations=population_configurations, demographic_events=demographic_events))
def simulate_im(params, sample_sizes, L, seed, prior=[], weights=[]): """Note this is a 2 population model""" assert len(sample_sizes) == 2 # sample reco or use value if prior != []: reco = draw_background_rate_from_prior(prior, weights) else: reco = params.reco.value # condense params N1 = params.N1.value N2 = params.N2.value T_split = params.T_split.value N_anc = params.N_anc.value mig = params.mig.value population_configurations = [ msprime.PopulationConfiguration(sample_size=sample_sizes[0], \ initial_size = N1), msprime.PopulationConfiguration(sample_size=sample_sizes[1], \ initial_size = N2)] # no migration initially mig_time = T_split/2 # directional (pulse) if mig >= 0: # migration from pop 1 into pop 0 (back in time) mig_event = msprime.MassMigration(time = mig_time, source = 1, \ destination = 0, proportion = abs(mig)) else: # migration from pop 0 into pop 1 (back in time) mig_event = msprime.MassMigration(time = mig_time, source = 0, \ destination = 1, proportion = abs(mig)) demographic_events = [ mig_event, # move all in deme 1 to deme 0 msprime.MassMigration( time = T_split, source = 1, destination = 0, proportion = 1.0), # change to ancestral size msprime.PopulationParametersChange(time=T_split, initial_size=N_anc, \ population_id=0) ] # simulate tree sequence ts = msprime.simulate( population_configurations = population_configurations, demographic_events = demographic_events, mutation_rate = params.mut.value, length = L, recombination_rate = reco, random_seed = seed) return ts
def __init__(self): super().__init__() # First we set out the maximum likelihood values of the various parameters # given in Table 1. N_A = 7300 N_B = 2100 N_AF = 12300 N_EU0 = 1000 N_AS0 = 510 # Times are provided in years, so we convert into generations. generation_time = 25 T_AF = 220e3 / generation_time T_B = 140e3 / generation_time T_EU_AS = 21.2e3 / generation_time # We need to work out the starting (diploid) population sizes based on # the growth rates provided for these two populations r_EU = 0.004 r_AS = 0.0055 N_EU = N_EU0 / math.exp(-r_EU * T_EU_AS) N_AS = N_AS0 / math.exp(-r_AS * T_EU_AS) # Migration rates during the various epochs. m_AF_B = 25e-5 m_AF_EU = 3e-5 m_AF_AS = 1.9e-5 m_EU_AS = 9.6e-5 # Population IDs correspond to their indexes in the population # configuration array. Therefore, we have 0=YRI, 1=CEU and 2=CHB # initially. self.population_configurations = [ msprime.PopulationConfiguration(initial_size=N_AF), msprime.PopulationConfiguration(initial_size=N_EU, growth_rate=r_EU), msprime.PopulationConfiguration(initial_size=N_AS, growth_rate=r_AS) ] self.migration_matrix = [ [ 0, m_AF_EU, m_AF_AS], # noqa [m_AF_EU, 0, m_EU_AS], # noqa [m_AF_AS, m_EU_AS, 0], # noqa ] self.demographic_events = [ # CEU and CHB merge into B with rate changes at T_EU_AS msprime.MassMigration( time=T_EU_AS, source=2, destination=1, proportion=1.0), msprime.MigrationRateChange(time=T_EU_AS, rate=0), msprime.MigrationRateChange( time=T_EU_AS, rate=m_AF_B, matrix_index=(0, 1)), msprime.MigrationRateChange( time=T_EU_AS, rate=m_AF_B, matrix_index=(1, 0)), msprime.PopulationParametersChange( time=T_EU_AS, initial_size=N_B, growth_rate=0, population_id=1), # Population B merges into YRI at T_B msprime.MassMigration( time=T_B, source=1, destination=0, proportion=1.0), # Size changes to N_A at T_AF msprime.PopulationParametersChange( time=T_AF, initial_size=N_A, population_id=0) ]
def simulate_ooa2(params, sample_sizes, L, seed, prior=[], weights=[]): """Note this is a 2 population model""" assert len(sample_sizes) == 2 # sample reco or use value if prior != []: reco = draw_background_rate_from_prior(prior, weights) else: reco = params.reco.value # condense params T1 = params.T1.value T2 = params.T2.value mig = params.mig.value population_configurations = [ msprime.PopulationConfiguration(sample_size=sample_sizes[0], \ initial_size = params.N3.value), # YRI is first msprime.PopulationConfiguration(sample_size=sample_sizes[1], \ initial_size = params.N2.value)] # CEU/CHB is second # directional (pulse) if mig >= 0: # migration from pop 1 into pop 0 (back in time) mig_event = msprime.MassMigration(time = T2, source = 1, \ destination = 0, proportion = abs(mig)) else: # migration from pop 0 into pop 1 (back in time) mig_event = msprime.MassMigration(time = T2, source = 0, \ destination = 1, proportion = abs(mig)) demographic_events = [ mig_event, # change size of EUR msprime.PopulationParametersChange(time=T2, \ initial_size=params.N1.value, population_id=1), # move all in deme 1 to deme 0 msprime.MassMigration(time = T1, source = 1, destination = 0, \ proportion = 1.0), # change to ancestral size msprime.PopulationParametersChange(time=T1, \ initial_size=params.N_anc.value, population_id=0) ] ts = msprime.simulate( population_configurations = population_configurations, demographic_events = demographic_events, mutation_rate = params.mut.value, length = L, recombination_rate = reco, random_seed = seed) return ts
def split(N_A, N_B, N_C, N_D, split_time1, split_time2, sample_A, sample_B, sample_C, sample_D, seg_length, recomb_rate, mut_rate, N_anc, seed): # Times are provided in years, so we convert into generations. generation_time = 25 T_S1 = split_time1 / generation_time T_S2 = split_time2 / generation_time # Population IDs correspond to their indexes in the population # configuration array. Therefore, we have 0=A, 1=B, 2=C, 3=D initially. population_configurations = [ msprime.PopulationConfiguration(sample_size=sample_A, initial_size=N_A), msprime.PopulationConfiguration(sample_size=sample_B, initial_size=N_B), msprime.PopulationConfiguration(sample_size=sample_C, initial_size=N_C), msprime.PopulationConfiguration(sample_size=sample_D, initial_size=N_D) ] demographic_events = [ msprime.MassMigration(time=T_S2, source=3, destination=2, proportion=1.0), msprime.MassMigration(time=T_S2, source=1, destination=0, proportion=1.0), msprime.MassMigration(time=T_S1, source=2, destination=0, proportion=1.0), msprime.PopulationParametersChange(time=T_S1, initial_size=N_anc, growth_rate=0, population_id=0) ] # Use the demography debugger to print out the demographic history # that we have just described. #dd = msprime.DemographyDebugger( # population_configurations=population_configurations, # demographic_events=demographic_events) #dd.print_history() ts = msprime.simulate(population_configurations=population_configurations, demographic_events=demographic_events, length=seg_length, recombination_rate=recomb_rate, random_seed=seed) ts = msprime.mutate(ts, rate=mut_rate, random_seed=seed) return ts
def full_ts(): """ Return a tree sequence that has data in all fields. """ """ A tree sequence with data in all fields - duplcated from tskit's conftest.py as other test suites using this file will not have that fixture defined. """ n = 10 t = 1 population_configurations = [ msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(0), ] demographic_events = [ msprime.MassMigration(time=t, source=0, destination=2), msprime.MassMigration(time=t, source=1, destination=2), ] ts = msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1, mutation_rate=1, record_migrations=True, ) tables = ts.dump_tables() # TODO replace this with properly linked up individuals using sim_ancestry # once 1.0 is released. for j in range(n): tables.individuals.add_row(flags=j, location=(j, j), parents=(j - 1, j - 1)) for name, table in tables.name_map.items(): if name != "provenances": table.metadata_schema = tskit.MetadataSchema({"codec": "json"}) metadatas = [f"n_{name}_{u}" for u in range(len(table))] metadata, metadata_offset = tskit.pack_strings(metadatas) table.set_columns( **{ **table.asdict(), "metadata": metadata, "metadata_offset": metadata_offset, }) tables.metadata_schema = tskit.MetadataSchema({"codec": "json"}) tables.metadata = "Test metadata" # Add some more provenance so we have enough rows for the offset deletion test. for j in range(10): tables.provenances.add_row(timestamp="x" * j, record="y" * j) return tables.tree_sequence()
def main(): index = sys.argv[1] directory = '/exports/csce/eddie/biology/groups/lohselab/sims/output/msprime/current_winner/' #directory = '/users/s1854903/sims/scripts/HeliconiusPopHist/msprime/test/' num_replicates = 1 sample_size = 5 mig = 3.8866e-7 seqLength = 32e3 recr = 1.84675e-8 Ne0 = 2.3241e6 #Cydno = ancestral Ne1 = 9.8922e5 #Mel_rosina = derived splitT = 4.8580e6 secT = 1e5 proportion = 0.1 mu = 1.9e-9 population_configurations = [ msprime.PopulationConfiguration(sample_size=sample_size, initial_size=Ne0), msprime.PopulationConfiguration(sample_size=sample_size, initial_size=Ne1), ] #demographic events: specify in the order they occur backwards in time demographic_events = [ msprime.MassMigration(time=secT, source=1, destination=0, proportion=proportion), msprime.PopulationParametersChange(time=splitT, initial_size=Ne0, population_id=0), msprime.MassMigration(time=splitT, source=1, destination=0, proportion=1.0) ] seed = np.random.randint(1, 2**32 - 1, num_replicates) replicates = msprime.simulate( num_replicates = 1, length = seqLength, recombination_rate = recr, population_configurations = population_configurations, demographic_events = demographic_events, migration_matrix = [[0,0], [mig,0]], mutation_rate = mu, random_seed=seed) for ts in replicates: msprime.mutate(ts, rate=mu, keep=True) with open(directory+'sim{}.vcf'.format(str(index)), 'w') as vcf_file: ts.write_vcf(vcf_file, ploidy=2) ts.dump(directory+'sim{}.trees'.format(str(index)))
def test_three_populations_no_migration_recombination(self): seed = 1234 ts1 = msprime.simulate( population_configurations=[ msprime.PopulationConfiguration(10), msprime.PopulationConfiguration(10), msprime.PopulationConfiguration(10), ], migration_matrix=np.zeros((3, 3)), recombination_rate=0.5, end_time=0.1, random_seed=seed, ) ts2 = msprime.simulate( population_configurations=[ msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), ], migration_matrix=np.zeros((3, 3)), from_ts=ts1, recombination_rate=0.5, demographic_events=[ msprime.SimpleBottleneck(time=0.5, population=0, proportion=1.0), msprime.SimpleBottleneck(time=0.5, population=1, proportion=1.0), msprime.SimpleBottleneck(time=0.5, population=2, proportion=1.0), msprime.MassMigration(0.61, 1, 0, 1.0), msprime.MassMigration(0.61, 2, 0, 1.0), msprime.SimpleBottleneck(0.61, population=0, proportion=1.0), ], random_seed=seed, ) self.assertGreater(ts2.num_trees, 1) for tree in ts2.trees(): # We should have three children at the root, and every node below # should be in one population. root_children = tree.children(tree.root) self.assertEqual(len(root_children), 3) populations = {ts2.node(u).population: u for u in root_children} self.assertEqual(len(populations), 3) for pop in [0, 1, 2]: for node in tree.nodes(populations[pop]): self.assertEqual(ts2.node(node).population, pop)
def split_times(split_time): T1 = split_time / gen_time demographic_events = [ #joining DIN to NGS msprime.MassMigration(time=T1, source=0, destination=1, proportion=1), #joining DIN with BSJ msprime.MassMigration(time=T2, source=1, destination=2, proportion=1), #bottleneck for CHW msprime.PopulationParametersChange(time=T3, initial_size=13000, population_id=3), #joining CHW with BSJ msprime.MassMigration(time=T4, source=2, destination=3, proportion=1) ] return demographic_events
def __init__(self, Ne, t_div, mod_n, t_anc, n_anc, eps=1e-8): """Class defining two-population serial coalescent with divergence. one population contains all of the modern samples and a second diverged population contains the ancient samples. """ super().__init__() # Define effective population size self.Ne = Ne # Setup samples here assert len(t_anc) == len(n_anc) samples = [msp.Sample(population=0, time=0) for i in range(mod_n)] for (t, n) in zip(t_anc, n_anc): for i in range(n): # Append ancient samples in the other population samples.append(msp.Sample(population=1, time=t)) self.samples = samples # Define population configuration self.pop_config = [msp.PopulationConfiguration(), msp.PopulationConfiguration()] # Define a mass migration of all the lineages post-split self.demography = [ msp.MassMigration(time=np.max(t_anc) + t_div + eps, source=1, dest=0) ]
def test_two_populations_no_migration_one_locus(self): seed = 1234 ts1 = msprime.simulate( population_configurations=[ msprime.PopulationConfiguration(10), msprime.PopulationConfiguration(10), ], migration_matrix=np.zeros((2, 2)), end_time=0.1, random_seed=seed, ) ts2 = msprime.simulate( population_configurations=[ msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), ], migration_matrix=np.zeros((2, 2)), from_ts=ts1, demographic_events=[msprime.MassMigration(100, 0, 1, 1.0)], random_seed=seed, ) tree = ts2.first() # We should have two children at the root, and every node below # should be in one population. root_children = tree.children(tree.root) self.assertEqual(len(root_children), 2) populations = {ts2.node(u).population: u for u in root_children} self.assertEqual(len(populations), 2) for pop in [0, 1]: for node in tree.nodes(populations[pop]): self.assertEqual(ts2.node(node).population, pop)
def test_coalescence_after_size_change(self): Ne = 20000 # Migrations and bottleneck occured 100 generations ago. g = 1000 population_configurations = [ msprime.PopulationConfiguration(1), msprime.PopulationConfiguration(1), ] # At this time, we migrate the lineage in 1 to 0, and # have a very strong bottleneck, resulting in almost instant # coalescence. demographic_events = [ msprime.MassMigration(time=g, source=1, destination=0), msprime.PopulationParametersChange(time=g, initial_size=1e-3), ] reps = msprime.simulate( Ne=Ne, population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1, num_replicates=10) for ts in reps: tree = next(ts.trees()) u = tree.get_mrca(0, 1) self.assertEqual(u, 2) self.assertAlmostEqual(g, tree.get_time(u), places=1)
def constmig_m5e05(): T_1 = 2e+04 T_2 = 6e+04 N_A0 = 1e+04 N_B0 = 1e+04 m = 5e-05 seq_length = 150e+06 population_configurations = [ msprime.PopulationConfiguration( sample_size=2, initial_size=N_A0, growth_rate=0), msprime.PopulationConfiguration( sample_size=0, initial_size=N_B0, growth_rate=0) ] migration_matrix = [[0,0],[0,0]] demographic_events = [ msprime.MigrationRateChange(time = T_1,rate = m, matrix_index=(0,1)), msprime.MigrationRateChange(time = T_1,rate = m, matrix_index=(1,0)), msprime.MigrationRateChange(time = T_2,rate = 0, matrix_index=(0,1)), msprime.MigrationRateChange(time = T_2,rate = 0, matrix_index=(1,0)), msprime.MassMigration(time=T_2, source =1, destination =0, proportion = 1) ] dd = msprime.DemographyDebugger( population_configurations=population_configurations,migration_matrix=migration_matrix, demographic_events=demographic_events) print('Demographic history:\n') dd.print_history() sim = msprime.simulate(population_configurations=population_configurations, demographic_events=demographic_events, length=seq_length, recombination_rate=2e-08,mutation_rate=2e-08) return sim
def get_msprime_examples(self): # NOTE: we use DTWF below to avoid rounding of floating-point times # that occur with a continuous-time simulator demographic_events = [ msprime.MassMigration(time=5, source=1, destination=0, proportion=1.0) ] seed = 6 for n in [2, 10, 20]: for mutrate in [0.0]: for recrate in [0.0, 0.01]: yield msprime.simulate(n, mutation_rate=mutrate, recombination_rate=recrate, length=200, random_seed=seed, model="dtwf") seed += 1 population_configurations = [ msprime.PopulationConfiguration(sample_size=n, initial_size=100), msprime.PopulationConfiguration(sample_size=n, initial_size=100) ] yield msprime.simulate( population_configurations=population_configurations, demographic_events=demographic_events, recombination_rate=recrate, mutation_rate=mutrate, length=250, random_seed=seed, model="dtwf") seed += 1
def get_msprime_event(self, params_dict, pop_ids_dict): t = self.t(params_dict) i = _get_pop_id(self.pop1, pop_ids_dict) j = _get_pop_id(self.pop2, pop_ids_dict) pij = self.p(params_dict) return msprime.MassMigration(t, i, j, proportion=pij)
def migration_example(): n = 10 t = 1 population_configurations = [ msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(n // 2), msprime.PopulationConfiguration(0), ] demographic_events = [ msprime.MassMigration(time=t, source=0, destination=2), msprime.MassMigration(time=t, source=1, destination=2), ] ts = msprime.simulate(population_configurations=population_configurations, demographic_events=demographic_events, random_seed=1) return ts