Saving output

Skip to first unread message

Nkrumah Grant

Aug 29, 2021, 1:57:24 PM8/29/21
to slim-discuss

Hi all:

I am trying to save run data every 500 generations for the script below. Currently, the data I want prints to the window. I want the same data to be printed to a txt or csv file.

I know about LogFile, but can't get that to work. Can someone help me with this?

Reproducible example below:





defineConstant("Ne", 10000);

defineConstant("Mu", 1e-8);

defineConstant("genomeSize", 1e6);

defineConstant("N_generations", 1000);


initializeMutationType("m1", 1.0, "f", 0.0); //neutral mutation

initializeMutationType("m2", 1.0, "f", 0.005); //beneficial mutation

initializeMutationType("m3", 1.0, "g", -0.03, 0.2); //deleterious mutation

m1.color = "yellow";

m2.color = "green";

m3.color = "red";

m1.colorSubstitution = "grey";

m2.colorSubstitution = "green";

m3.colorSubstitution = "red";

initializeGenomicElementType("g1", c(m1), c(1)); //neutral mutations

initializeGenomicElementType("g2", c(m1,m3), c(0.95,0.05)); // purifying selection

initializeGenomicElementType("g3", c(m1,m2), c(0.5,0.5)); // positive selection

initializeGenomicElementType("g4", c(m1,m3), c(0.5,0.5)); // non-essential genes

g1.color = "yellow";

g2.color = "blue";

g3.color = "green";

g4.color = "red";

for (index in 0:9)

initializeGenomicElement(g1, index*1000, index*1000 + 499);

for (index in 10:19)

initializeGenomicElement(g2, index*1000, index*1000 + 499);

for (index in 20:29)

initializeGenomicElement(g3, index*1000, index*1000 + 499);

for (index in 30:39)

initializeGenomicElement(g4, index*1000, index*1000 + 499);

initializeRecombinationRate(0); // In SLiM recombination is between sister chromatids


// At the first generation:

// - create a subpopulation (or many)

1 early ()


sim.chromosome.colorSubstitution = ""; //keeps the color I defined for mutation events above in the initialize callback.

sim.addSubpop("p1", asInteger(Ne));

p1.setCloningRate(1.0); // Essential for bacteria.

sim.rescheduleScriptBlock(s1, start=N_generations, end=N_generations);




// convert fecundity-based selection to survival-based selection

if (runif(1) < relFitness)

return 1.0;


return 0.0;


//Remove mutations from second chromosome



if (size(childGenome2.mutations))


return T;


// At each generation:

// - Remove fixed mutations



// Remove fixed mutation

// (SLiM does it automatically for diploid,

// but for haploid, mutations are fixed at 0.5)

//This must be done for each mutation class (m1-m3 in this model).

muts_m1 = sim.mutationsOfType(m1);

freqs = sim.mutationFrequencies(NULL, muts_m1);

fixed_muts_m1 = muts_m1[freqs == 0.5];

if (size(fixed_muts_m1) > 0)

sim.subpopulations.genomes.removeMutations(fixed_muts_m1, T);

muts_m2 = sim.mutationsOfType(m2);

freqs = sim.mutationFrequencies(NULL, muts_m2);

fixed_muts_m2 = muts_m2[freqs == 0.5];

if (size(fixed_muts_m2) > 0)

sim.subpopulations.genomes.removeMutations(fixed_muts_m2, T);

muts_m3 = sim.mutationsOfType(m3);

freqs = sim.mutationFrequencies(NULL, muts_m3);

fixed_muts_m3 = muts_m3[freqs == 0.5];

if (size(fixed_muts_m3) > 0)

sim.subpopulations.genomes.removeMutations(fixed_muts_m3, F); //pg 254


// Create custom output of segregating sites and those being fixed in the population.

// I would also like to know what type of mutation it is.

// pg 463

//Create output showing mutations segregating and fixing in the population at 500 generation intervals. Code works, but prints to GUI.

//TODO: figure out how to print to a file.

1:2000 late ()


if (sim.generation % 500 == 0)








// End simulation

s1 1000 late ()



Ben Haller

Aug 29, 2021, 2:07:34 PM8/29/21
to slim-discuss
Hi!  You're referring to the output from outputMutations() and outputFixedMutations(), I take it?  Both of those methods can take a file path as a parameter, and can either create a new file or append to an existing file.  See the SLiM manual for documentation of this.


Benjamin C. Haller
Messer Lab
Cornell University

Nkrumah Grant

Aug 29, 2021, 2:09:23 PM8/29/21
to Ben Haller, slim-discuss
Wow, thank you so much. Easy fix. 

SLiM forward genetic simulation:
You received this message because you are subscribed to the Google Groups "slim-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Nkrumah A.Grant, Ph.D.
Postdoctoral Research Fellow
Department of Biological Sciences
University of Idaho
875 Perimeter MS 3051
Moscow, ID 83844

Sent from mobile
Reply all
Reply to author
0 new messages