Public
Edited
Feb 13, 2023
Insert cell
Insert cell
pyodide = {
const pyodide = await require("https://cdn.jsdelivr.net/pyodide/v0.22.1/full/pyodide.js");
return pyodide.loadPyodide()
}
Insert cell
make_treeseq_svg = async (seed, mutseed) => {
await pyodide.loadPackage("msprime");
const msprime = await pyodide.pyimport("msprime");

// It has eluded me how to round node times to integer
// values and set the node table columns from JS.
// I get wacky errors and haven't had the time to confirm
// my logic in Python.
// In order to not show students fractional generation times,
// I'm just living with the slower dtwf model.
const ts = msprime.sim_ancestry.callKwargs({samples: 5,
recombination_rate: 0.0,
sequence_length:1e6,
random_seed:seed,
population_size: 1000,
record_full_arg: false,
model: "dtwf"});
let output = 1;
let myns = {ts: ts, "mutseed": mutseed, output: output};
pyodide.registerJsModule("myns", myns);

pyodide.runPython(`
from myns import ts, mutseed, output
import msprime
tables = ts.dump_tables()
tscopy = tables.tree_sequence()
local_mutseed = mutseed
tsmut = msprime.sim_mutations(tree_sequence=tscopy,
model= msprime.BinaryMutationModel(),
random_seed= local_mutseed,
rate= 1e-9);
# while tsmut.num_mutations < 4 or tsmut.num_mutations > 10:
# # This is fragile in general, and only works b/c we constrain the user
# # input
# local_mutseed+=1;
# tscopy = tables.tree_sequence();
# tsmut = msprime.sim_mutations(tree_sequence= tscopy,
# model= msprime.BinaryMutationModel(),
# random_seed= local_mutseed,
# rate= 1e-9)
# print(tsmut.num_mutations)
myns.output = tsmut
`);

return myns.output;
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
ts= make_treeseq_svg(51, 42)
Insert cell
ts.num_edges
Insert cell
ts.num_mutations
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more