// Based on Example 18.1

initialize() {
    setSeed(23);
    defineConstant("OUTFILE", "recipe_nucleotides.trees");
    defineConstant("PEDIGREE", paste0(c(OUTFILE, ".pedigree")));
	defineConstant("L", 1e2);
	initializeSLiMOptions(keepPedigrees=T, nucleotideBased=T);
    initializeTreeSeq();
	initializeAncestralNucleotides(randomNucleotides(L));
	initializeMutationTypeNuc("m1", 0.5, "f", 0.0);
	initializeGenomicElementType("g1", m1, 1.0, mmJukesCantor(4e-2));
	initializeGenomicElement(g1, 0, L-1);
	initializeRecombinationRate(1e-2);
}

1 {
	sim.addSubpop("p1", 10);
}

10 late() {
    sim.treeSeqOutput(OUTFILE);
    catn("Done.");
    sim.simulationFinished();
}

// PEDIGREE OUTPUT
1 early() {
    writeFile(PEDIGREE,
       paste(c("generation", "stage", "individual", "age", "parent1", "parent2"),
             sep="\t"));
}

1: early() {
    for (pop in sim.subpopulations) {
        for (ind in pop.individuals) {
            age = sim.modelType == "nonWF" ? ind.age else -1;
            writeFile(PEDIGREE,
             paste(c(sim.generation, 'early', ind.pedigreeID, age, ind.pedigreeParentIDs),
                   sep='\t'),
             append=T);
        }
    }
}

1: late() {
    for (pop in sim.subpopulations) {
        for (ind in pop.individuals) {
            age = sim.modelType == "nonWF" ? ind.age else -1;
            writeFile(PEDIGREE,
             paste(c(sim.generation, 'late', ind.pedigreeID, age, ind.pedigreeParentIDs),
                   sep='\t'),
             append=T);
        }
    }
}

