Title: | Mutation Models for Pedigree Likelihood Computations |
---|---|
Description: | A collection of functions for modelling mutations in pedigrees with marker data, as used e.g. in likelihood computations with microsatellite data. Implemented models include equal, proportional and stepwise models, as well as random models for experimental work, and custom models allowing the user to apply any valid mutation matrix. Allele lumping is done following the lumpability criteria of Kemeny and Snell (1976), ISBN:0387901922. |
Authors: | Magnus Dehli Vigeland [aut, cre] , Thore Egeland [ctb] |
Maintainer: | Magnus Dehli Vigeland <[email protected]> |
License: | GPL-3 |
Version: | 0.7.1 |
Built: | 2025-01-21 04:25:14 UTC |
Source: | https://github.com/magnusdv/pedmut |
Finds the stationary distribution of allele frequencies, if it exists, w.r.t. a given mutation matrix.
findStationary(mutmat)
findStationary(mutmat)
mutmat |
A mutation matrix. |
A vector of length ncol(mutmat)
, or NULL.
m1 = mutationMatrix("equal", alleles = 1:4, rate = 0.1) findStationary(m1) m2 = mutationMatrix("random", alleles = 1:3, seed = 123) a = findStationary(m2) a %*% m2 - a # check
m1 = mutationMatrix("equal", alleles = 1:4, rate = 0.1) findStationary(m1) m2 = mutationMatrix("random", alleles = 1:3, seed = 123) a = findStationary(m2) a %*% m2 - a # check
Extract model parameters of a mutation matrix/model.
getParams(mut, params = NULL, format = 1, sep = "/")
getParams(mut, params = NULL, format = 1, sep = "/")
mut |
|
params |
A vector contain some or all of the words "model", "rate", "range", "rate2", "seed". If NULL (default), all present parameters are included. |
format |
A numeric code indicating the wanted output format. See Value. |
sep |
A separator character used to paste male and female values.
Ignored unless |
When mut
is a mutationModel
with different male/female
parameters, the output format is dictated by the format
option, with the
following possibilities:
A data frame with 2 rows labelled 'female' and 'male'.
A data frame with 1 row and female/male columns suffixed by .F/.M respectively.
A data frame with 1 row, in which female/male values are pasted together
(separated with sep
) if different.
If mut
is a mutationMatrix
the output always has 1 row.
M = mutationModel("equal", 1:2, rate = list(female = 0.2, male = 0.1)) getParams(M) getParams(M, format = 2) getParams(M, format = 3) getParams(M, format = 3, sep = "|")
M = mutationModel("equal", 1:2, rate = list(female = 0.2, male = 0.1)) getParams(M) getParams(M, format = 2) getParams(M, format = 3) getParams(M, format = 3, sep = "|")
Test for mutation matrix/model
isMutationModel(x) isMutationMatrix(x)
isMutationModel(x) isMutationMatrix(x)
x |
Any object. |
TRUE or FALSE
mat = mutationMatrix("equal", alleles = 1:2, rate = 0.1) isMutationMatrix(mat) isMutationModel(mat) # FALSE (not a complete model) mod = mutationModel(mat) isMutationModel(mod)
mat = mutationMatrix("equal", alleles = 1:2, rate = 0.1) isMutationMatrix(mat) isMutationModel(mat) # FALSE (not a complete model) mod = mutationModel(mat) isMutationModel(mod)
Reduce a mutation matrix by combining a set of alleles into one "lump", if this can be done without distorting the mutation process of the remaining alleles. Such "allele lumping" can give dramatic efficiency improvements in likelihood computations with multi-allelic markers, in cases where only some of the alleles are observed in the pedigree.
lumpedMatrix(mutmat, lump, afreq = NULL, check = TRUE, labelSep = NULL) lumpedModel(mutmod, lump, afreq = NULL, check = TRUE)
lumpedMatrix(mutmat, lump, afreq = NULL, check = TRUE, labelSep = NULL) lumpedModel(mutmod, lump, afreq = NULL, check = TRUE)
mutmat |
A |
lump |
A nonempty subset of the alleles (i.e., the column names of
|
afreq |
A vector with frequency vector, of the same length as the size
of |
check |
A logical indicating if lumpability should be checked before lumping. Default: TRUE. |
labelSep |
((For debugging) A character used to name lumps by pasting allele labels. |
mutmod |
A |
A reduced mutation model. If the original matrix has dimensions
, the result will be
, where
.
mutationModel()
, mutationMatrix()
### Example 1: Lumping a mutation matrix mat = mutationMatrix("eq", alleles = 1:5, afreq = rep(0.2, 5), rate = 0.1) mat # Lump alleles 3, 4 and 5 mat2 = lumpedMatrix(mat, lump = 3:5) mat2 # Example 2: Full model, proportional mutrate = list(male = 0.1, female = 0.2) mod = mutationModel("prop", alleles = 1:4, rate = mutrate, afreq = c(.1,.2,.3,.4)) mod # Lump alleles 3 and 4 mod2 = lumpedModel(mod, lump = 3:4) mod2
### Example 1: Lumping a mutation matrix mat = mutationMatrix("eq", alleles = 1:5, afreq = rep(0.2, 5), rate = 0.1) mat # Lump alleles 3, 4 and 5 mat2 = lumpedMatrix(mat, lump = 3:5) mat2 # Example 2: Full model, proportional mutrate = list(male = 0.1, female = 0.2) mod = mutationModel("prop", alleles = 1:4, rate = mutrate, afreq = c(.1,.2,.3,.4)) mod # Lump alleles 3 and 4 mod2 = lumpedModel(mod, lump = 3:4) mod2
Upper limits for overall mutation rate for the stepwise reversible model.
maxRate(alleles, afreq, range)
maxRate(alleles, afreq, range)
alleles |
A character vector with allele labels. |
afreq |
A numeric vector of allele frequencies. |
range |
A positive number. |
A vector of two numbers named UW
and UB
. The first of these is
the maximum overall mutation rate for a well-defined stepwise reversible
mutation matrix with the given input. The latter (UB) is the upper limit of
the overall mutation rate under the additional restraint that the model is
bounded by afreq
.
Thore Egeland.
Functions for checking various properties of a mutation model, including stationarity, reversibility and lumpability.
isStationary(mutmat, afreq = NULL) isReversible(mutmat, afreq = NULL) isBounded(mutmat, afreq = NULL) isLumpable(mutmat, lump) alwaysLumpable(mutmat)
isStationary(mutmat, afreq = NULL) isReversible(mutmat, afreq = NULL) isBounded(mutmat, afreq = NULL) isLumpable(mutmat, lump) alwaysLumpable(mutmat)
mutmat |
A |
afreq |
A vector with allele frequencies, of the same length as the size
of |
lump |
A nonempty subset of the colnames of |
The function isBounded()
checks that a mutation model is bounded by the
allele frequencies, i.e., that mutmat[i,j] <= afreq[j]
whenever i
is not
equal to j
.
For each of these functions, if mutmat
is a mutationModel
object, i.e.,
with male and female components, the output is TRUE if and only if both
components satisfy the property in question.
Each of these functions returns TRUE of FALSE.
# "proportional" models are stationary and reversible afr = c(0.2, 0.3, 0.5) m_prop = mutationMatrix(model = "prop", alleles = 1:3, afreq = afr, rate = 0.1) stopifnot(isStationary(m_prop, afr), isReversible(m_prop, afr)) # "equal" model is stationary and reversible only when freqs are equal m_eq = mutationMatrix(model = "eq", alleles = 1:3, rate = 0.1) stopifnot(isStationary(m_eq, rep(1/3, 3)), isReversible(m_eq, rep(1/3, 3))) stopifnot(!isStationary(m_eq, afr), !isReversible(m_eq, afr)) # "equal" and "proportional" models allow allele lumping stopifnot(isLumpable(m_eq, lump = 1:2)) stopifnot(isLumpable(m_prop, lump = 1:2)) # In fact lumpable for any allele subset stopifnot(alwaysLumpable(m_eq), alwaysLumpable(m_prop))
# "proportional" models are stationary and reversible afr = c(0.2, 0.3, 0.5) m_prop = mutationMatrix(model = "prop", alleles = 1:3, afreq = afr, rate = 0.1) stopifnot(isStationary(m_prop, afr), isReversible(m_prop, afr)) # "equal" model is stationary and reversible only when freqs are equal m_eq = mutationMatrix(model = "eq", alleles = 1:3, rate = 0.1) stopifnot(isStationary(m_eq, rep(1/3, 3)), isReversible(m_eq, rep(1/3, 3))) stopifnot(!isStationary(m_eq, afr), !isReversible(m_eq, afr)) # "equal" and "proportional" models allow allele lumping stopifnot(isLumpable(m_eq, lump = 1:2)) stopifnot(isLumpable(m_prop, lump = 1:2)) # In fact lumpable for any allele subset stopifnot(alwaysLumpable(m_eq), alwaysLumpable(m_prop))
Construct mutation matrices for pedigree likelihood computations.
mutationMatrix( model = c("custom", "equal", "proportional", "random", "onestep", "stepwise", "trivial"), matrix = NULL, alleles = NULL, afreq = NULL, rate = NULL, seed = NULL, rate2 = NULL, range = NULL ) validateMutationMatrix(mutmat, alleles = NULL)
mutationMatrix( model = c("custom", "equal", "proportional", "random", "onestep", "stepwise", "trivial"), matrix = NULL, alleles = NULL, afreq = NULL, rate = NULL, seed = NULL, rate2 = NULL, range = NULL ) validateMutationMatrix(mutmat, alleles = NULL)
model |
A string: either "custom", "equal", "proportional", "random", "stepwise" or "onestep". |
matrix |
When |
alleles |
A character vector (or coercible to character) with allele
labels. Required in all models, except "custom" if |
afreq |
A numeric vector of allele frequencies. Required in model "proportional". |
rate |
A number between 0 and 1. Required in models "equal", "proportional", "stepwise" and "onestep". |
seed |
A single number. Optional parameter in the "random" model, passed
on to |
rate2 |
A number between 0 and 1. The mutation rate between integer alleles and microvariants. Required in the "stepwise" model. |
range |
A positive number. The relative probability of mutating n+1 steps versus mutating n steps. Required in the "stepwise" model. |
mutmat |
An object of class |
Descriptions of the models:
custom
: Allows any mutation matrix to be provided by the user, in the
matrix
parameter.
equal
: All mutations equally likely; probability of no
mutation.
proportional
: Mutation probabilities are proportional to the target
allele frequencies.
random
: This produces a matrix of random numbers, where each row is
normalised so that it sums to 1.
onestep
: A mutation model for microsatellite markers, allowing mutations
only to the nearest neighbours in the allelic ladder. For example, '10' may
mutate to either '9' or '11', unless '10' is the lowest allele, in which case
'11' is the only option. This model is not applicable to loci with
non-integral microvariants.
stepwise
: A common model in forensic genetics, allowing different
mutation rates between integer alleles (like '16') and non-integer
"microvariants" like '9.3'). Mutations also depend on the size of the
mutation if the parameter 'range' differs from 1.
trivial
: The identity matrix; i.e. no mutations are possible.
A square matrix with entries in [0, 1]
, with the allele labels as
both colnames and rownames.
mutationMatrix(alleles = 1:3, model = "equal", rate = 0.05)
mutationMatrix(alleles = 1:3, model = "equal", rate = 0.05)
Constructor for the class mutationModel
. An object of this class is
essentially a list of two mutation matrices, named "female" and "male".
mutationModel( model, alleles = NULL, afreq = NULL, matrix = NULL, rate = NULL, rate2 = NULL, range = NULL, seed = NULL, validate = TRUE ) validateMutationModel(mutmod, alleles = NULL) sexEqual(mutmod)
mutationModel( model, alleles = NULL, afreq = NULL, matrix = NULL, rate = NULL, rate2 = NULL, range = NULL, seed = NULL, validate = TRUE ) validateMutationModel(mutmod, alleles = NULL) sexEqual(mutmod)
model |
Either:
|
alleles |
A character vector with allele labels; passed on to
|
afreq |
A numeric vector of allele frequencies; passed on to
|
matrix |
A matrix, or a list of two (named "female" and "male") |
rate |
A numeric mutation rate, or a list of two (named "female" and "male") |
rate2 |
A numeric mutation rate, or a list of two (named "female" and
"male"). Required in the "stepwise" model; see |
range |
A positive number, or a list of two (named "female" and "male").
Required in the "stepwise" model; see |
seed |
An integer, or a list of two (named "female" and "male"). |
validate |
A logical, by default TRUE. |
mutmod |
A |
An object of class mutationModel
. This is a list of two
mutationMatrix
objects, named "female" and "male", and the following
attributes:
sexEqual
: TRUE if both genders have identical models, otherwise FALSE
alwaysLumpable
: TRUE if both genders have models that are lumpable for
any allele subset, otherwise FALSE
# "Equal" model, same parameters for both genders M1 = mutationModel("eq", alleles = 1:2, rate = 0.1) M1 # Different mutation rates M2 = mutationModel("eq", alleles = 1:2, rate = list(male = 0.1, female = 0.01)) M2 stopifnot(identical(M1$male, M1$female), identical(M2$male, M1$male)) # A custom mutation matrix: mat = matrix(c(0,0,1,1), ncol = 2, dimnames = list(1:2, 1:2)) M3 = mutationModel(model = "custom", matrix = mat) # Under the hood arguments are passed to `mutationMatrix()`. # Alternatively, this can be done explicitly in the `model` argument M4 = mutationModel(model = mutationMatrix("custom", matrix = mat)) stopifnot(identical(M3, M4)) # The latter strategy is needed e.g. in pedtools::marker(), which gives the # user access to `model`, but not `matrix`.
# "Equal" model, same parameters for both genders M1 = mutationModel("eq", alleles = 1:2, rate = 0.1) M1 # Different mutation rates M2 = mutationModel("eq", alleles = 1:2, rate = list(male = 0.1, female = 0.01)) M2 stopifnot(identical(M1$male, M1$female), identical(M2$male, M1$male)) # A custom mutation matrix: mat = matrix(c(0,0,1,1), ncol = 2, dimnames = list(1:2, 1:2)) M3 = mutationModel(model = "custom", matrix = mat) # Under the hood arguments are passed to `mutationMatrix()`. # Alternatively, this can be done explicitly in the `model` argument M4 = mutationModel(model = mutationMatrix("custom", matrix = mat)) stopifnot(identical(M3, M4)) # The latter strategy is needed e.g. in pedtools::marker(), which gives the # user access to `model`, but not `matrix`.
Calculate the overall mutation rate at a locus, given a mutation model an a set of allele frequencies.
mutRate(mut, afreq = NULL)
mutRate(mut, afreq = NULL)
mut |
|
afreq |
A vector of allele frequencies. |
The mutation rate is found by the formula 1 - sum(diag(mut) * afreq)
.
If mut
is a mutationModel()
, the rate is calculated separately for the
male and female matrices.
A single number, or (if mut
is a mutationModel()
and the female
and male rates differ) a list of two numbers, named "female" and "male".
m = mutationMatrix("stepwise", alleles = 1:4, afreq = c(.1,.2,.3,.4), rate = 0.01, rate2 = 1e-6, range = 0.1) r = mutRate(m) stopifnot(all.equal(r, 0.01))
m = mutationMatrix("stepwise", alleles = 1:4, afreq = c(.1,.2,.3,.4), rate = 0.01, rate2 = 1e-6, range = 0.1) r = mutRate(m) stopifnot(all.equal(r, 0.01))
Produces a mutation matrix close to the input mutmat
, for which the given
frequency vector is the stationary distribution. Several methods for doing
this are described by Simonsson and Mostad (2016); only the "PM" method is
included here.
stabilize(mutmat, afreq = NULL, method = "PM", details = FALSE)
stabilize(mutmat, afreq = NULL, method = "PM", details = FALSE)
mutmat |
A mutation matrix. |
afreq |
A vector of allele frequencies |
method |
Either "DP", "RM" or "PM". Currently only "PM" is implemented. |
details |
A logical. If TRUE, the complete Familias output is included. |
This function is based on, and reuses code from, the stabilize()
method of
the Familias R package.
An object of the same class the input mutmat
; either a matrix, a
mutationMatrix
or a mutationModel
.
Petter Mostad, Thore Egeland, Ivar Simonsson, Magnus D. Vigeland
Simonsson, Mostad: Stationary Mutation models. (FSI: Genetics, 2016).
afreq = c(.2, .3, .5) m = mutationMatrix("stepwise", alleles = 1:3, afreq = afreq, rate = 0.1, rate2 = 0.01, range = 0.1) m stabilize(m, afreq = c(.3,.3,.4)) ### Example with full model (i.e., male and female) M = mutationModel("stepwise", alleles = 1:3, afreq = afreq, rate = list(male = 0.1, female = 0.2), rate2 = 0.01, range = 0.1) M stabilize(M)
afreq = c(.2, .3, .5) m = mutationMatrix("stepwise", alleles = 1:3, afreq = afreq, rate = 0.1, rate2 = 0.01, range = 0.1) m stabilize(m, afreq = c(.3,.3,.4)) ### Example with full model (i.e., male and female) M = mutationModel("stepwise", alleles = 1:3, afreq = afreq, rate = list(male = 0.1, female = 0.2), rate2 = 0.01, range = 0.1) M stabilize(M)
A reversible stepwise mutation model is created following the approach of Dawid et al. (2002).
stepwiseReversible(alleles, afreq, rate, range, maxRateOnly = FALSE)
stepwiseReversible(alleles, afreq, rate, range, maxRateOnly = FALSE)
alleles |
A vector of integer integers. |
afreq |
A numeric vector of allele frequencies. |
rate |
A numeric mutation rate. |
range |
A positive number. |
maxRateOnly |
A logical, by default FALSE. See Value. |
For the stepwise reversible model, the mutation rate is proportional to the overall mutation rate
for given
values of the range, the allele frequency
and n, the number of
alleles. Hence, one can determine bounds UW and UB so that the model is well
defined if
and bounded, i.e.,
, if
, The bounds UW and UB are computed.
A reversible stepwise mutation model with overall mutation rate equal
to rate
.
If maxRateOnly
is TRUE, the function returns a vector of two numbers
named UW
and UB
. The first of these is the maximum overall mutation
rate for a well-defined stepwise reversible mutation matrix with the given
input. The latter (UB) is the maximum rate under the additional restraint
that the model is bounded by afreq
.
Thore Egeland.
stepwiseReversible(alleles = 1:3, afreq = c(0.2, 0.3, 0.5), rate = 0.001, range = 0.1) stepwiseReversible(alleles = 1:3, afreq = c(0.2, 0.3, 0.5), range = 0.1, maxRateOnly = TRUE) # Model not well defined: ## Not run: stepwiseReversible(alleles = 1:3, afreq = c(0.2, 0.3, 0.5), rate = 0.7, range = 0.1) ## End(Not run)
stepwiseReversible(alleles = 1:3, afreq = c(0.2, 0.3, 0.5), rate = 0.001, range = 0.1) stepwiseReversible(alleles = 1:3, afreq = c(0.2, 0.3, 0.5), range = 0.1, maxRateOnly = TRUE) # Model not well defined: ## Not run: stepwiseReversible(alleles = 1:3, afreq = c(0.2, 0.3, 0.5), rate = 0.7, range = 0.1) ## End(Not run)