Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_population_parameters_change_growth_rate(self):
g = 100
for growth_rate in [0.01, 1, 100, 1e6]:
event = msprime.PopulationParametersChange(time=g, growth_rate=growth_rate)
d = event.get_ll_representation(1)
dp = {
"time": g,
"population": -1,
"type": "population_parameters_change",
"growth_rate": growth_rate}
self.assertEqual(d, dp)
def test_event_before_start_time(self):
for start_time in [10, 20]:
for time in [start_time - 1, start_time - 1e-6]:
with self.assertRaises(_msprime.InputError):
msprime.simulate(
sample_size=10,
start_time=start_time,
demographic_events=[
msprime.PopulationParametersChange(
time=time, initial_size=2)])
# a single pop reflects the size history of that pop:
# - population sizes today are [100, 1000]
# - population 1 had a size change to 200 at t=100 generations ago
# - there is a mass migration event that entirely merges population 2 (source)
# into population 1 (dest) at t=200 generations ago
# - population 1 has a size change to 100 at t=300 generations ago
ddb = msprime.DemographyDebugger(
population_configurations=[
msprime.PopulationConfiguration(initial_size=1e2),
msprime.PopulationConfiguration(initial_size=1e3)
],
demographic_events=[
msprime.PopulationParametersChange(time=100, initial_size=200,
population_id=0),
msprime.MassMigration(time=200, source=1, dest=0),
msprime.PopulationParametersChange(time=300, initial_size=100)
],
migration_matrix=[
[0, 0],
[0, 0]
])
return ddb
def test_growth_rate_and_size_change(self):
g = 1024
growth_rate = 2
initial_size = 8192
event = msprime.PopulationParametersChange(
time=g, initial_size=initial_size,
growth_rate=growth_rate, population=1)
ll_event = {
"type": "population_parameters_change",
"time": g,
"population": 1,
"initial_size": initial_size,
"growth_rate": growth_rate
}
self.assertEqual(event.get_ll_representation(1), ll_event)
]
migration_matrix = [
[ 0, m_AF_EU, m_AF_AS],
[m_AF_EU, 0, m_EU_AS],
[m_AF_AS, m_EU_AS, 0],
]
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)
]
return dict(
population_configurations=population_configurations,
migration_matrix=migration_matrix,
demographic_events=demographic_events
)
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)
]
# Use the demography debugger to print out the demographic history
# that we have just described.
dp = msprime.DemographyDebugger(
Ne=N_A,
population_configurations=population_configurations,
migration_matrix=migration_matrix,
demographic_events=demographic_events)
dp.print_history()
return(population_configurations, migration_matrix, demographic_events)
def add_dtwf_vs_coalescent_2_pop_shrink(self):
initial_size = 1000
population_configurations = [
msprime.PopulationConfiguration(
sample_size=10, initial_size=initial_size, growth_rate=-0.01)]
recombination_map = msprime.RecombinationMap(
[0, int(1e7)], [1e-8, 0], num_loci=int(1e7))
demographic_events = [
msprime.PopulationParametersChange(
time=200, initial_size=initial_size, growth_rate=0.01, population_id=0)
]
def f():
self.run_dtwf_coalescent_comparison(
"dtwf_vs_coalescent_2_pop_shrink",
population_configurations=population_configurations,
recombination_map=recombination_map,
demographic_events=demographic_events,
num_replicates=300)
self._instances["dtwf_vs_coalescent_2_pop_shrink"] = f
if convert_int(event[1], parser) != num_populations:
parser.error(
"num_populations must be equal for new migration matrix")
matrix = convert_migration_matrix(parser, event[2:], num_populations)
for j in range(num_populations):
for k in range(num_populations):
if j != k:
msp_event = msprime.MigrationRateChange(
t, matrix[j][k], (j, k))
demographic_events.append((index, msp_event))
# We've created all the events and PopulationConfiguration objects. Because
# msprime uses absolute population sizes we need to rescale these relative
# to Ne, since this is what ms does.
for _, msp_event in demographic_events:
if isinstance(msp_event, msprime.PopulationParametersChange):
if msp_event.initial_size is not None:
msp_event.initial_size /= 4
for config in population_configurations:
if config.initial_size is not None:
config.initial_size /= 4
demographic_events.sort(key=lambda x: (x[0], x[1].time))
time_sorted = sorted(demographic_events, key=lambda x: x[1].time)
if demographic_events != time_sorted:
parser.error(
"Demographic events must be supplied in non-decreasing "
"time order")
runner = SimulationRunner(
sample_size=args.sample_size,
num_loci=num_loci,
migration_matrix=migration_matrix,
if len(pop_sizes) != len(times) + 1:
raise IOError('Number of population sizes must '
'match number of epochs.')
pop_sizes, times = decimate_sizes(pop_sizes,
times,
args.decimate_rel_tol,
args.decimate_anc_size)
pop_config = [
msprime.PopulationConfiguration(sample_size=args.samplesize,
initial_size=pop_sizes[0])]
demography = []
if times:
for pop_size, time in zip(pop_sizes[1:], times):
demography.append(
msprime.PopulationParametersChange(time=time * 2,
initial_size=pop_size,
population_id=0))
reco_maps = _load_hapmap()
pool = Pool(args.numthreads, maxtasksperchild=100)
logging.info('Simulating data...')
simulation_args = [((pop_config, args.mu, demography, args.ploidy),
reco_maps) for k in range(args.num_sims)]
test_set = list(pool.imap(_simulate_data, simulation_args, chunksize=50))
logging.info('\tdone simulating')
scores = {}
for block_penalty in block_penalties:
for window_size in window_sizes:
estimates = list(pool.imap(partial(_call_optimize,
metawindow=args.metawindow,
windowsize=window_size,
table=table,
]
migration_matrix = [
[ 0, m_AF_EU, m_AF_AS],
[m_AF_EU, 0, m_EU_AS],
[m_AF_AS, m_EU_AS, 0],
]
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)
]
# Use the demography debugger to print out the demographic history
# that we have just described.
dp = msprime.DemographyDebugger(
Ne=N_A,
population_configurations=population_configurations,
migration_matrix=migration_matrix,
demographic_events=demographic_events)
dp.print_history()