// Keywords: live plotting initialize() { initializeMutationRate(1e-7); initializeMutationType("m1", 0.5, "f", 0.0); initializeGenomicElementType("g1", m1, 1.0); initializeGenomicElement(g1, 0, 99999); initializeRecombinationRate(1e-8); } 1 { sim.addSubpop("p1", 5000); sim.setValue("fixed", NULL); defineConstant("pdfPath", writeTempFile("plot_", ".pdf", "")); // If we're running in SLiMgui, open a plot window if (exists("slimgui")) { slimgui.openDocument(pdfPath); sim.setValue("pdfPid", 0); } else sim.setValue("pdfPid", -1); } 1: { if (sim.generation % 10 == 0) { count = sim.substitutions.size(); sim.setValue("fixed", c(sim.getValue("fixed"), count)); } if (sim.generation % 1000 != 0) return; y = sim.getValue("fixed"); rstr = paste(c('{', 'x <- (1:' + size(y) + ') * 10', 'y <- c(' + paste(y, sep=", ") + ')', 'pdf(file="' + pdfPath + '", width=4, height=4)', 'par(mar=c(4.0, 4.0, 1.5, 1.5))', 'plot(x=x, y=y, xlim=c(0, 50000), ylim=c(0, 500), type="l",', 'xlab="Generation", ylab="Fixed mutations", cex.axis=0.95,', 'cex.lab=1.2, mgp=c(2.5, 0.7, 0), col="red", lwd=2,', 'xaxp=c(0, 50000, 2))', 'box()', 'dev.off()', '}'), sep="\n"); scriptPath = writeTempFile("plot_", ".R", rstr); system("/usr/local/bin/Rscript", args=scriptPath); deleteFile(scriptPath); if (!exists("slimgui")) { if (sim.getValue("pdfPid") < 0) { system("mupdf " + pdfPath, wait=F); pid = system("pgrep -a mupdf|grep " + pdfPath + " | cut -f1 -d' '"); sim.setValue("pdfPid", pid); } else system("kill -HUP " + sim.getValue("pdfPid")); } } 50000 late() { sim.outputFixedMutations(); }