Gene Drop Simulations in AlphaSimR
Chris Gaynor
2026-12-01
Source:vignettes/articles/GeneDropSimulations.Rmd
GeneDropSimulations.RmdIntroduction
A gene drop simulation takes a pedigree and a set of founder genomes and works forwards through the pedigree, sampling gametes according to Mendelian inheritance and recombination, until every individual has a genome (MacCluer et al. 1986). The pedigree supplies the structure and the founders supply the genetic material. Nothing about the genomes of the non-founders is assumed; they are the outcome of the sampling.
Gene drops are useful whenever the question is about how genetic material moves through a known set of relationships. Typical uses are estimating the probability that a descendant carries a founder allele, quantifying how variable realized relationships are around their pedigree expectations, generating genotypes for individuals that were never genotyped, and producing a set of starting genomes for a forward simulation that begins from a real breeding population.
AlphaSimR performs gene drops with pedigreeCross(). This
article works through the function in detail, with an emphasis on the
common case where both the pedigree and the founder genotypes come from
outside AlphaSimR.
The pedigree
A pedigree is given to pedigreeCross() as three vectors
of equal length: id, mother and
father.
These are always character vectors. Anything else is coerced with
as.character() before it is used, so an integer pedigree
works, but it becomes character internally and the IDs on the returned
population will be character.
An unknown parent is NA by default. This matters,
because many pedigree files use 0 to mean unknown, and
AlphaSimR does not read it that way on its own: a parent recorded as
"0" names an individual called “0”. The
unknownParent argument says which values mean unknown, so a
file using 0 is handled by passing
unknownParent = "0", and a merged file using several
conventions by passing them all. NA is always unknown,
whatever else is given, because it cannot name anything. An
NA in id is an error, since every individual
needs a name, and an id that is one of the
unknownParent values is an error for the same reason.
The pedigree is extended backwards before it is used, so that every parent is either another row of the pedigree or unknown. Which names get a row of their own depends on whether they carry any information about how individuals are related.
A name used as a parent twice or more, counting the mother and father columns together, is added as a founder row. Leaving it out would change the pedigree: two half sibs sharing an unrecorded mother would come back unrelated, and an individual whose mother and father are the same unrecorded name would come back as an outcross rather than a self.
A name used once is different. It links its child to nothing else in the pedigree, so the child can simply be a founder in its own right. Such a name is treated as unknown and no row is added for it. This pedigree:
ped = data.frame(id = "3",
mother = "2",
father = "1")is therefore a single founder called “3”. Neither “1” nor “2” appears
anywhere else, so neither is worth simulating, and individual “3” takes
a genome from founderPop directly. If you want the cross,
write the founders out:
ped = data.frame(id = c("1", "2", "3"),
mother = c(NA, NA, "2"),
father = c(NA, NA, "1"))The returned mother and father show what
was actually simulated, so a name that was dropped this way comes back
as NA rather than as you wrote it. Matching on ID is the
exception to all of this: with matchID = TRUE every missing
name gets a row, because a name has to have a row before it can be
matched against founderPop.
The pedigree does not have to be sorted. pedigreeCross()
sorts it, and in doing so it will detect a few problems: duplicated IDs,
and loops in which an individual ends up being its own ancestor. The
maxCycle argument bounds how many passes over the pedigree
are made while sorting; left at NULL it follows the size of
the pedigree, which is always enough, so it is not something you
normally need to set.
A first example
The simplest case is a pedigree dropped onto founders that were simulated in AlphaSimR. Here is a biparental cross taken to the F8 by single seed descent.
founderGenomes = quickHaplo(nInd = 2, nChr = 2, segSites = 100)
SP = SimParam$new(founderGenomes)
pop = newPop(founderGenomes)
id = as.character(1:10)
mother = c(NA, NA, "1", as.character(3:9))
father = c(NA, NA, "2", as.character(3:9))
f8 = pedigreeCross(founderPop = pop, id = id, mother = mother,
father = father)
f8
#> An object of class "Pop"
#> Ploidy: 2
#> Individuals: 10
#> Chromosomes: 2
#> Loci: 200
#> Traits: 0Individuals 1 and 2 have unknown parents, so they are the founders
and they take their genomes from pop. Individual 3 is their
cross, and individuals 4 to 10 are successive self-pollinations. The
pedigree is returned with the population.
tail(getPed(f8))
#> id mother father
#> 5 5 4 4
#> 6 6 5 5
#> 7 7 6 6
#> 8 8 7 7
#> 9 9 8 8
#> 10 10 9 9Because the founders were assigned at random from pop,
repeating the call gives a different, equally valid, realization. That
is the point of a gene drop.
Working with an external pedigree
Real pedigrees usually arrive as a file. The two things to get right are the type of the columns and the coding of unknown parents.
read.csv() will happily turn an ID column of numbers
into an integer column, and then "007" becomes
7. Reading the identifier columns as character avoids the
problem entirely:
ped = read.csv("pedigree.csv",
colClasses = c(id = "character",
mother = "character",
father = "character"))For this article we will build the same object directly, in the form
it would have after being read from a file that uses 0 for
unknown parents.
ped = data.frame(
id = c("F1", "F2", "F3", "F4", "C1", "C2", "C3", "D1", "D2", "D3"),
mother = c("0", "0", "0", "0", "F1", "F3", "C1", "C3", "C3", "C3"),
father = c("0", "0", "0", "0", "F2", "F4", "C2", "C3", "C3", "C3")
)
ped
#> id mother father
#> 1 F1 0 0
#> 2 F2 0 0
#> 3 F3 0 0
#> 4 F4 0 0
#> 5 C1 F1 F2
#> 6 C2 F3 F4
#> 7 C3 C1 C2
#> 8 D1 C3 C3
#> 9 D2 C3 C3
#> 10 D3 C3 C3Four inbred founders are crossed in pairs, the two single crosses are
crossed to give C3, and three doubled haploid lines are
derived from C3.
The 0 codes are handled by unknownParent,
so the file can be used as it is:
pedigreeCross(founderPop, ped$id, ped$mother, ped$father,
unknownParent = "0")A merged pedigree often uses several conventions at once, and they can all be given together:
pedigreeCross(founderPop, ped$id, ped$mother, ped$father,
unknownParent = c("0", "", "NA", "*", "-9"))For the rest of this article we will recode once instead, so that the calls that follow stay short:
ped$mother[ped$mother == "0"] = NA
ped$father[ped$father == "0"] = NA
ped
#> id mother father
#> 1 F1 <NA> <NA>
#> 2 F2 <NA> <NA>
#> 3 F3 <NA> <NA>
#> 4 F4 <NA> <NA>
#> 5 C1 F1 F2
#> 6 C2 F3 F4
#> 7 C3 C1 C2
#> 8 D1 C3 C3
#> 9 D2 C3 C3
#> 10 D3 C3 C3It is worth checking, before simulating, that every named parent is either a row of the pedigree or unknown. Anything else is a typo, and a typo is silent: a misspelled parent name used once is simply treated as unknown, and its child becomes a founder.
named = unique(c(ped$mother, ped$father))
named = named[!is.na(named)]
setdiff(named, ped$id)
#> character(0)An empty result means the pedigree is complete. Here it is, so nothing is added and nothing is dropped.
Working with external genotypes
Founder genomes can come from real data. AlphaSimR reads them with
importGenMap() together with either
importInbredGeno(), for inbred diploid lines, or
importHaplo(), for phased haplotypes. Both return a
MapPop, or a NamedMapPop when a pedigree is
supplied with them.
To keep the article self contained, we will manufacture a genotype matrix and a genetic map of the kind these functions expect. In practice these two objects would be read from files.
# Stands in for real data
sourceGenomes = runMacs(nInd = 6, nChr = 2, segSites = 100, inbred = TRUE)
genoFile = pullSegSiteGeno(sourceGenomes)
rownames(genoFile) = c("F1", "F2", "F3", "F4", "C1", "C2")
mapFile = getGenMap(sourceGenomes)
genoFile[, 1:8]
#> 1_1 1_2 1_3 1_4 1_5 1_6 1_7 1_8
#> F1 0 2 2 2 0 0 2 2
#> F2 2 0 0 0 0 0 2 2
#> F3 2 0 2 0 0 0 2 0
#> F4 2 0 2 0 2 2 0 0
#> C1 2 0 2 0 2 2 2 2
#> C2 2 0 2 0 0 0 2 2
head(mapFile)
#> id chr pos
#> 1 1_1 1 0.01283383
#> 2 1_2 1 0.01288743
#> 3 1_3 1 0.01373035
#> 4 1_4 1 0.03040356
#> 5 1_5 1 0.04055117
#> 6 1_6 1 0.04467583The genotype matrix has one row per individual and one column per marker, coded as allele dosages. The map has three columns: marker name, chromosome, and position in Morgans. Marker names link the two, so the column names of the genotype matrix must appear in the map.
Importing gives a founder population:
founderPop = importInbredGeno(geno = genoFile,
genMap = mapFile,
ped = rownames(genoFile))
founderPop
#> An object of class "NamedMapPop"
#> Ploidy: 2
#> Individuals: 6
#> Chromosomes: 2
#> Loci: 200Supplying ped is what makes the result a
NamedMapPop rather than a plain MapPop, and it
is what allows the founders to be matched to the pedigree by name in the
next section.
Map populations and SimParam
pedigreeCross() treats a map population differently from
a population, because the two say different things about where you
are.
Passing a Pop, as in the first example, means a
simulation is already running. The pedigree is built with your
SimParam and the result belongs to that simulation.
Passing a MapPop or NamedMapPop means no
simulation has been set up yet, which is the usual situation when
founder genotypes have just been imported. No SimParam is
looked for, and none is created for you. pedigreeCross()
makes a temporary one of its own, uses it to run the crossing, and
discards it. The result is a NamedMapPop carrying the
genetic map and the pedigree’s IDs, which is exactly what
SimParam$new() wants. Because the temporary
SimParam defines no traits, nothing about traits or SNP
chips survives the call; those are added afterwards, to the simulation
you start from the result.
This gives the natural order of operations for a simulation that begins from a real population: import the genotypes, drop the pedigree, then set up the simulation from the dropped population.
Matching founders by ID
matchID decides how individuals in the pedigree are
connected to individuals in founderPop.
matchID = FALSE
With matchID = FALSE, the two sets of IDs have nothing
to do with each other. The founders are the individuals whose parents
are both unknown, and each one is assigned a randomly sampled individual
from founderPop. Every individual in the extended pedigree
is returned.
dropped = pedigreeCross(founderPop = founderPop,
id = ped$id, mother = ped$mother,
father = ped$father,
matchID = FALSE)
dropped
#> An object of class "NamedMapPop"
#> Ploidy: 2
#> Individuals: 10
#> Chromosomes: 2
#> Loci: 200
getPed(dropped)
#> id mother father
#> 1 F1 <NA> <NA>
#> 2 F2 <NA> <NA>
#> 3 F3 <NA> <NA>
#> 4 F4 <NA> <NA>
#> 5 C1 F1 F2
#> 6 C2 F3 F4
#> 7 C3 C1 C2
#> 8 D1 C3 C3
#> 9 D2 C3 C3
#> 10 D3 C3 C3founderPop needs at least as many individuals as the
pedigree has founders. Here there are four founders and six imported
lines, so there is room to spare.
An individual with exactly one unknown parent is a half founder, and that unknown parent is given a founder genome of its own. A pedigree with several half founders therefore needs one extra founder for each of them, and two half founders never share a genome.
halfPed = data.frame(id = c("A", "B", "C", "D"),
mother = c(NA, NA, NA, "B"),
father = c(NA, "A", "A", "C"))
halfDrop = pedigreeCross(founderPop = founderPop,
id = halfPed$id, mother = halfPed$mother,
father = halfPed$father)
halfDrop@id
#> [1] "A" "B" "C" "D"A has both parents unknown, so it is a founder.
B and C each have one unknown parent, and each
of those gets a founder genome of its own, so three founder genomes are
drawn from founderPop even though the pedigree only names
one individual with two unknown parents. The unknown mothers of
B and C are not the same individual, and they
are not rows of the returned population; only the named individuals are
returned.
The other thing that shapes the founder count is the extension rule from the first section. A parent named only once is treated as unknown, so an individual whose parents are both unrecorded singletons is one founder rather than the cross of two:
oncePed = data.frame(id = c("X", "Y", "Z"),
mother = c("m1", "shared", "shared"),
father = c("f1", NA, NA))
onceDrop = pedigreeCross(founderPop = founderPop,
id = oncePed$id, mother = oncePed$mother,
father = oncePed$father)
getPed(onceDrop)
#> id mother father
#> 1 shared <NA> <NA>
#> 2 X <NA> <NA>
#> 3 Y shared <NA>
#> 4 Z shared <NA>m1 and f1 are each used once, so both are
treated as unknown and X is a founder. shared
is used twice, so it is added, and Y and Z
come back as the half sibs the pedigree says they are. Four founder
genomes are used in all: one for X, one for
shared, and one each for the unknown fathers of
Y and Z.
This keeps the founder count close to what the pedigree actually
constrains, but it is a genetic difference and not only a bookkeeping
one. X is now a single individual taken from
founderPop, where it would otherwise have been the cross of
two. With an inbred founderPop, that means X
is inbred where it used to be heterozygous.
matchID = TRUE
With matchID = TRUE, an individual whose ID is found in
founderPop takes its genotype from there, and its own
ancestry is not simulated. Only the matched individuals and what
descends from them are simulated. This is the setting to use when the
founder genotypes are real data for named individuals.
Our imported population contains C1 and C2
as well as F1 to F4, so all six are matched
and take their observed genotypes, and only their descendants are
simulated:
matched = pedigreeCross(founderPop = founderPop,
id = ped$id, mother = ped$mother,
father = ped$father,
matchID = TRUE)
matched@id
#> [1] "F1" "F2" "F3" "F4" "C1" "C2" "C3" "D1" "D2" "D3"All six matched individuals are returned, plus C3 and
the three doubled haploids that descend from them. Nothing was dropped
through F1 to F4, because their genotypes were
observed rather than inferred.
The more common case is that only part of the pedigree is genotyped,
and then the returned population can be shorter than the extended
pedigree, because ancestry above a match is skipped. Suppose only the
two single crosses, C1 and C2, were genotyped.
Because founderPop was imported with ped, it
is a NamedMapPop and can be subset by ID. A plain
MapPop can only be subset by position.
genotyped = founderPop[c("C1", "C2")]
partial = pedigreeCross(founderPop = genotyped,
id = ped$id, mother = ped$mother,
father = ped$father,
matchID = TRUE)
partial@id
#> [1] "C1" "C2" "C3" "D1" "D2" "D3"F1 to F4 are legitimately skipped: they are
ancestors of a match, so their genomes are not needed. What is returned
is the two matched lines and their four descendants.
An individual that is neither matched nor descended from a match
cannot be made at all. Rather than inventing a genome for it,
pedigreeCross() stops and names the individuals it could
not build:
pedigreeCross(founderPop = founderPop[c("F1", "F2")],
id = ped$id, mother = ped$mother, father = ped$father,
matchID = TRUE)
#> Error in `resolveFounders()`:
#> ! Not enough individuals in founderPop match the pedigree. These individuals are neither matched nor descended from a match: F3, F4, C2, C3, D1, D2, D3C1 is the cross of two matched founders and could be
built, but C2 needs F3 and F4,
which were not supplied, so the pedigree cannot be completed.
Matching needs a population that has IDs. A plain MapPop
does not, so it can only be used with matchID = FALSE. Use
importInbredGeno() or importHaplo() with their
ped argument, as above, to get a NamedMapPop
instead.
Starting a simulation from the result
The population returned from a map population is a
NamedMapPop, so it can be passed straight to
SimParam$new():
SP = SimParam$new(matched)
SP$addTraitA(nQtlPerChr = 10)
SP$setVarE(h2 = 0.3)
pop = newPop(matched)
pop
#> An object of class "Pop"
#> Ploidy: 2
#> Individuals: 10
#> Chromosomes: 2
#> Loci: 200
#> Traits: 1
head(getPed(pop))
#> id mother father
#> 1 F1 <NA> <NA>
#> 2 F2 <NA> <NA>
#> 3 F3 <NA> <NA>
#> 4 F4 <NA> <NA>
#> 5 C1 F1 F2
#> 6 C2 F3 F4The IDs, mothers and fathers from the pedigree are carried onto the population, so the simulated individuals keep the names they had in the original data. From here the simulation continues as any other AlphaSimR simulation would.
Doubled haploids and selfing
The three lines D1 to D3 in our pedigree
are doubled haploids, which the pedigree alone cannot express: as far as
it is concerned they are three self-pollinations of C3. The
DH and nSelf arguments supply the missing
information.
DH is a logical vector saying which individuals are
doubled haploids. nSelf is an integer vector saying how
many generations of selfing an individual represents, which is useful
when a pedigree records a line by its single cross and its inbreeding is
implied.
Both are aligned with the id vector as you supply it.
Rows added by the backwards extension are given DH = FALSE
and nSelf = 0, so you never have to leave space for
them.
dh = ped$id %in% c("D1", "D2", "D3")
dhDrop = pedigreeCross(founderPop = founderPop,
id = ped$id, mother = ped$mother,
father = ped$father,
matchID = TRUE,
DH = dh)Doubled haploids are completely homozygous, which is easy to confirm:
geno = pullSegSiteGeno(dhDrop)
any(geno[c("D1", "D2", "D3"), ] == 1)
#> [1] FALSE
any(geno["C3", ] == 1)
#> [1] TRUEnSelf works the same way. An individual recorded as the
cross of two parents, but understood to be an F5 line, is written as the
cross with four generations of selfing after it:
selfPed = data.frame(id = c("C1", "C2", "L1"),
mother = c(NA, NA, "C1"),
father = c(NA, NA, "C2"))
selfDrop = pedigreeCross(founderPop = founderPop,
id = selfPed$id, mother = selfPed$mother,
father = selfPed$father,
matchID = TRUE,
nSelf = c(0, 0, 4))
mean(pullSegSiteGeno(selfDrop)["L1", ] == 1)
#> [1] 0.015The result is a line that is mostly, but not entirely, homozygous, as an F5 should be.
Recombination settings
When founderPop is a map population,
pedigreeCross() builds its own SimParam, so
the recombination settings it uses can be given through
.... The three that are accepted are v, the
interference parameter, p, the proportion of recombination
following a Poisson process, and quadProb, the probability
of quadrivalent pairing in autopolyploids. Any that are not given keep
the SimParam defaults.
noInterference = pedigreeCross(founderPop = founderPop,
id = ped$id, mother = ped$mother,
father = ped$father,
matchID = TRUE,
v = 1)These can only be set this way when founderPop is a map
population. SimParam is an R6 object, with reference
semantics, so setting them during a call that uses your own
SimParam would change your simulation permanently rather
than just for the call. Passing them with a Pop is
therefore an error, and the settings should be made on the
SimParam itself:
pedigreeCross(founderPop = pop, id = ped$id, mother = ped$mother,
father = ped$father, v = 1)
#> Error in `pedigreeCross()`:
#> ! v, p and quadProb can only be set when founderPop is a map population. Set them on the SimParam insteadRepeating the drop
A single gene drop is one realization of a random process. Most questions about a pedigree are answered by repeating it and looking at the distribution of the result.
Here we drop the pedigree many times and record the genotype of
D1, one of the doubled haploid lines descended from
C3, at a single marker. C1 and C2
are inbred and matched to their observed genotypes every time, so
C3 has the same genotype in every drop: heterozygous at
this marker, because C1 and C2 differ there.
All of the variation in D1 comes from which of
C3’s alleles it inherited. DH = dh is passed,
as in the previous section, so D1 is a doubled haploid and
is homozygous for one allele or the other.
nRep = 200
dose = numeric(nRep)
for(i in seq_len(nRep)){
drop = pedigreeCross(founderPop = founderPop,
id = ped$id, mother = ped$mother,
father = ped$father,
matchID = TRUE, DH = dh)
dose[i] = pullSegSiteGeno(drop)["D1", "1_50"]
}
table(dose)
#> dose
#> 0 2
#> 112 88The same approach answers questions about founder contributions, about how much realized relationships vary around their pedigree expectations, and about the probability that a particular descendant carries a particular founder allele. Because the drops are independent, the loop parallelizes trivially.
The drops are also reproducible. Setting the seed before the loop fixes the whole sequence, and this is worth doing whenever a result is going to be reported.
Things to watch for
0 is not an unknown parent by default.
A 0 in a parent column names an individual called “0”, and
because every row with that parent then shares a single genome,
unrelated individuals come out related. Pass
unknownParent = "0", or recode the file before dropping
it.
A parent named once is not simulated. Under
matchID = FALSE, a parent that appears nowhere else in the
pedigree is treated as unknown and its child becomes a founder. This is
usually what you want, and it keeps the founder count down, but it means
a misspelled parent name fails quietly. The setdiff() check
above catches it.
matchID = FALSE samples founders at
random. The IDs in the pedigree and the IDs in
founderPop are unconnected under this setting, so a
pedigree whose founders happen to share names with genotyped individuals
will not use those genotypes. Use matchID = TRUE when the
names are meant to line up.
Separate sexes are not supported.
pedigreeCross() currently requires a SimParam
with sexes = "no". The pedigree itself determines who
crosses with whom, so the distinction is not needed, but an existing
simulation that uses separate sexes cannot be used as the founder
population.
Traits do not survive the map population route. The
temporary SimParam defines no traits, so add traits to the
SimParam you build from the returned population, not
before.
Check the size of the result. With
matchID = TRUE the returned population is the matched
individuals and their descendants, which is often smaller than the
pedigree supplied. Comparing nInd against the pedigree, or
inspecting getPed() on the result, is a quick way to
confirm that the drop did what was intended.
Summary
pedigreeCross() drops founder genomes through a
pedigree. The pedigree is three character vectors, with unknown parents
written as NA or as whatever unknownParent
says, and it is extended backwards over the parent names that carry a
relationship. Founders are either sampled at random, with
matchID = FALSE, or matched by name to genotyped
individuals, with matchID = TRUE. Given a map population it
builds and discards its own SimParam and returns a
NamedMapPop, which makes the import, drop, simulate
sequence the natural way to start a simulation from a real breeding
population.