Issues with .to_demes() specifically .add_pulse() #1918
-
Hello all! I am attempting to write a function that takes some demographic parameters and outputs a Demes graph. The problem I am encountering is that I think there are some issues with semantics where msprime uses the keyword "source" and "proportion" but Demes expects "sources" and "proportions"—at least I think that is going on... Here is an example of creating a Demes graph from msprime: # Define a demes builder from and msprime demography debugger.
def IUA_demes_conversion(
N,
f,
Tgf,
Tp2,
Tp3,
):
"""
Takes an msprime demographic model and returns a demes graph object.
"""
# Intialize the demographic model.
demography = msprime.Demography()
# We assume constant and equal effective population sizes for
# all lineages.
demography.add_population(name='P123', initial_size=N)
demography.add_population(name='P12', initial_size=N)
demography.add_population(name='P3', initial_size=N)
demography.add_population(name='P2', initial_size=N)
demography.add_population(name='P1', initial_size=N)
# Introgression from P3 to P2 with a probaility f at time Tgf.
demography.add_mass_migration(
time=Tgf, source='P2', dest='P3', proportion=f,
)
# P1 and P2 merge into P12.
demography.add_population_split(
time=Tp2, derived=['P1', 'P2'], ancestral='P12'
)
# P12 and P3 merge into P123.
demography.add_population_split(
time=Tp3, derived=['P12', 'P3'], ancestral='P123'
)
# Convert the demographic object to a demes graph.
graph = msprime.Demography.to_demes(demography)
#print(demography.debug())
return graph
# Generate a demes graph.
iua_msp_graph_f_01 = IUA_demes_conversion(
N=10**6,
f=0.1,
Tgf=0.2 * 10**6,
Tp2=1.2 * 10**6,
Tp3=2.4 * 10**6,
) Which will result in the following error message:
And after looking at the Demes source code https://popsim-consortium.github.io/demes-docs/main/_modules/demes/demes.html#Builder.add_pulse it seems like the verbiage is just inconsistent but I also could be making another mistake... So I then tried to build a graph for the same model in Demes using def demes_graph_builder(
N,
f,
Tgf,
Tp2,
Tp3,
):
build = demes.Builder(description='IUA')
build.add_deme('P123', epochs=[dict(end_time=float(Tp3), start_size=float(N))])
build.add_deme('P12', ancestors=['P123'], proportions=[float(1)], epochs=[dict(end_time=float(Tp2), start_size=float(N))])
build.add_deme('P3', ancestors=['P123'], proportions=[float(1)], epochs=[dict(end_time=float(0), start_size=float(N))])
build.add_deme('P2', ancestors=['P12'], proportions=[float(1)], epochs=[dict(end_time=float(0), start_size=float(N))])
build.add_deme('P1', ancestors=['P12'], proportions=[float(1)], epochs=[dict(end_time=float(0), start_size=float(N))])
build.add_pulse(sources=['P3'], dest=['P2'], proportions=[float(f)], time=float(Tgf))
graph = build.resolve()
return graph
iua_builder_graph_f_01 = demes_builder(
N=10**6,
f=0.1,
Tgf=0.2 * 10**6,
Tp2=1.2 * 10**6,
Tp3=2.4 * 10**6,
) But then I also get the following error:
So now I am not quite sure why what I am doing is not working, but I feel like I might be missing something obvious... Thanks in advance and sorry for the recent influx of simple issues 😅 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Any ideas @apragsdale @grahamgower ? |
Beta Was this translation helpful? Give feedback.
-
Hi David - looks like you're using |
Beta Was this translation helpful? Give feedback.
Hi David - looks like you're using
demes
built from the most up to date state of the repo instead of the most recent release (demes-0.1.2). Since demes-0.1.2, we've changed the pulses to allow for simultaneoussources
, instead of only allowing one at a time. Msprime is still set up to work with demes-0.1.2, since those changes to pulses haven't been released yet. But in both cases, thedest
should still be given as the name of the receiving deme, instead of as a list. Does that fix the issue for you, at least in yourdemes_graph_builder()
function?