@@ -1276,8 +1276,6 @@ getCasteId <- function(x, caste = "all", collapse = FALSE, simParamBee = NULL) {
12761276# ' vector with sex information
12771277# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
12781278# '
1279- # ' @seealso \code{\link[SIMplyBee]{getCaste}}
1280- # '
12811279# ' @return when \code{x} is \code{\link[AlphaSimR]{Pop-class}} for \code{caste != "all"}
12821280# ' or list for \code{caste == "all"} with sex nodes named by caste;
12831281# ' when \code{x} is \code{\link[SIMplyBee]{Colony-class}} return is a named list of
@@ -2869,7 +2867,7 @@ nCsdAlleles <- function(x, collapse = FALSE, simParamBee = NULL) {
28692867# ' with haplotypes of all the individuals
28702868# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
28712869# '
2872- # ' @seealso \code{\link[SIMplyBee]{getIbdHaplo}} and \code{\link[ AlphaSimR]{pullIbdHaplo}}
2870+ # ' @seealso \code{\link[AlphaSimR]{pullIbdHaplo}}
28732871# '
28742872# ' @return matrix with haplotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}}
28752873# ' and list of matrices with haplotypes when \code{x} is
@@ -3124,7 +3122,7 @@ getDronesIbdHaplo <- function(x, nInd = NULL, chr = NULL, snpChip = NULL,
31243122# ' with haplotypes of all the individuals
31253123# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
31263124# '
3127- # ' @seealso \code{\link[SIMplyBee]{getQtlHaplo }} and \code{\link[AlphaSimR]{pullQtlHaplo}} as well as
3125+ # ' @seealso \code{\link[SIMplyBee]{getQtlGeno }} and \code{\link[AlphaSimR]{pullQtlHaplo}} as well as
31283126# ' \code{vignette(topic = "QuantitativeGenetics", package = "SIMplyBee")}
31293127# '
31303128# ' @return matrix with haplotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}}
@@ -3374,7 +3372,9 @@ getDronesQtlHaplo <- function(x, nInd = NULL,
33743372# ' with genotypes of all the individuals
33753373# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
33763374# '
3377- # ' @seealso \code{\link[SIMplyBee]{getQtlGeno}} and \code{\link[AlphaSimR]{pullQtlGeno}} as well as
3375+ # ' @seealso \code{\link[SIMplyBee]{getQtlHaplo}},
3376+ # ' \code{\link[AlphaSimR]{pullQtlGeno}}, and
3377+ # ' \code{\link[SIMplyBee]{getPooledGeno}}, as well as
33783378# ' \code{vignette(topic = "QuantitativeGenetics", package = "SIMplyBee")}
33793379# '
33803380# ' @return matrix with genotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}} and
@@ -3624,7 +3624,7 @@ getDronesQtlGeno <- function(x, nInd = NULL,
36243624# ' with haplotypes of all the individuals
36253625# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
36263626# '
3627- # ' @seealso \code{\link[SIMplyBee]{getSegSiteHaplo }} and \code{\link[AlphaSimR]{pullSegSiteHaplo}}
3627+ # ' @seealso \code{\link[SIMplyBee]{getSegSiteGeno }} and \code{\link[AlphaSimR]{pullSegSiteHaplo}}
36283628# '
36293629# ' @return matrix with haplotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}}
36303630# ' and list of matrices with haplotypes when \code{x} is
@@ -3866,7 +3866,9 @@ getDronesSegSiteHaplo <- function(x, nInd = NULL,
38663866# ' with genotypes of all the individuals
38673867# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
38683868# '
3869- # ' @seealso \code{\link[SIMplyBee]{getSegSiteGeno}} and \code{\link[AlphaSimR]{pullSegSiteGeno}}
3869+ # ' @seealso \code{\link[SIMplyBee]{getSegSiteHaplo}},
3870+ # ' \code{\link[AlphaSimR]{pullSegSiteGeno}}, and
3871+ # ' \code{\link[SIMplyBee]{getPooledGeno}}
38703872# '
38713873# ' @return matrix with genotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}} and
38723874# ' list of matrices with genotypes when \code{x} is
@@ -4106,7 +4108,7 @@ getDronesSegSiteGeno <- function(x, nInd = NULL,
41064108# ' with haplotypes of all the individuals
41074109# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
41084110# '
4109- # ' @seealso \code{\link[SIMplyBee]{getSnpHaplo }} and \code{\link[AlphaSimR]{pullSnpHaplo}}
4111+ # ' @seealso \code{\link[SIMplyBee]{getSnpGeno }} and \code{\link[AlphaSimR]{pullSnpHaplo}}
41104112# '
41114113# ' @return matrix with haplotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}}
41124114# ' and list of matrices with haplotypes when \code{x} is
@@ -4347,7 +4349,9 @@ getDronesSnpHaplo <- function(x, nInd = NULL,
43474349# ' with genotypes of all the individuals
43484350# ' @param simParamBee \code{\link[SIMplyBee]{SimParamBee}}, global simulation parameters
43494351# '
4350- # ' @seealso \code{\link[SIMplyBee]{getSnpGeno}} and \code{\link[AlphaSimR]{pullSnpGeno}}
4352+ # ' @seealso \code{\link[SIMplyBee]{getSnpHaplo}},
4353+ # ' \code{\link[AlphaSimR]{pullSnpGeno}}, and
4354+ # ' \code{\link[SIMplyBee]{getPooledGeno}}
43514355# '
43524356# ' @return matrix with genotypes when \code{x} is \code{\link[SIMplyBee]{Colony-class}} and
43534357# ' list of matrices with genotypes when \code{x} is
@@ -4573,14 +4577,14 @@ getDronesSnpGeno <- function(x, nInd = NULL,
45734577# ' genotypes to mimic genotyping of a pool of colony members.
45744578# '
45754579# ' @param x matrix, true genotypes with individuals in rows and sites in columns
4576- # ' @param type character, "mean" for average genotype or "count" for the counts
4577- # ' of reference and alternative alleles
4580+ # ' @param type character, \code{ "mean"} for average genotype (default) or
4581+ # ' \code{"count"} for the counts of reference and alternative alleles
45784582# ' @param sex character, vector of "F" and "M" to denote the sex of individuals
45794583# ' in \code{x}
45804584# '
45814585# ' @return a numeric vector with average allele dosage when \code{type = "mean"}
4582- # ' and a two-row matrix with the counts of reference (1st row) and
4583- # ' alternative (2nd row) alleles
4586+ # ' or a two-row matrix with the counts of reference (1st row) and
4587+ # ' alternative (2nd row) alleles when \code{type = "count"}
45844588# '
45854589# ' @examples
45864590# ' founderGenomes <- quickHaplo(nInd = 3, nChr = 1, segSites = 50)
@@ -4632,11 +4636,19 @@ getDronesSnpGeno <- function(x, nInd = NULL,
46324636# '
46334637# ' # As an exercise you could repeat the above with different numbers of workers!
46344638# '
4639+ # ' # How to get pooled genotypes of workers across multiple colonies?
4640+ # ' tmp <- getSegSiteGeno(x = apiary, caste = "workers")
4641+ # ' (tmp2 = lapply(X = tmp, FUN = getPooledGeno)) # as a list of one row matrices
4642+ # ' t(sapply(X = tmp, FUN = getPooledGeno)) # as one matrix - option A
4643+ # ' do.call(what = rbind, args = tmp2) # as one matrix - option B
46354644# ' @export
4636- getPooledGeno <- function (x , type = NULL , sex = NULL ) {
4645+ getPooledGeno <- function (x , type = " mean " , sex = NULL ) {
46374646 if (! is.matrix(x )) {
46384647 stop(" Argument x must be a matrix class object!" )
46394648 }
4649+ if (is.null(type ) | ! (type %in% c(" mean" , " count" ))) {
4650+ stop(" Argument type must be specified as either mean or count!" )
4651+ }
46404652 n <- nrow(x )
46414653 if (is.null(sex )) {
46424654 warning(" Argument sex is NULL. Assuming that all individuals are female/diploid!" )
@@ -4654,8 +4666,6 @@ getPooledGeno <- function(x, type = NULL, sex = NULL) {
46544666 } else if (type == " count" ) {
46554667 ret <- rbind(nPloids - ret , ret )
46564668 rownames(ret ) <- c(" 0" , " 1" )
4657- } else {
4658- stop(" Argument type must be mean or count!" )
46594669 }
46604670 return (ret )
46614671}
@@ -4676,9 +4686,16 @@ getPooledGeno <- function(x, type = NULL, sex = NULL) {
46764686# ' @param sex character vector denoting sex for individuals with genotypes in
46774687# ' \code{x} - \code{"F"} for female and \code{"M"} for male
46784688# ' @param alleleFreq numeric, vector of allele frequencies for the sites in
4679- # ' \code{x}; if \code{NULL}, then \code{\link[SIMplyBee]{calcBeeAlleleFreq}} is used
4689+ # ' \code{x}; if \code{NULL}, then \code{\link[SIMplyBee]{calcBeeAlleleFreq}}
4690+ # ' is used
4691+ # ' @param returnComponents logical, return GRM as well as the components used
4692+ # ' to compute it (useful for GWAS by GBLUP)
46804693# '
4681- # ' @return matrix of genomic relatedness coefficients
4694+ # ' @return if \code{returnComponents = FALSE} (default) return a matrix of
4695+ # ' genomic relatedness coefficients; if \code{returnComponents = TRUE} return
4696+ # ' a list with the GRM, centred genotype matrix, allele frequencies, and
4697+ # ' scaling factor used to scale the crossproduct of centred genotype matrix
4698+ # ' to get the GRM.
46824699# '
46834700# ' @references Druet and Legarra (2020) Theoretical and empirical comparisons of
46844701# ' expected and realized relationships for the X-chromosome. Genetics
@@ -4704,12 +4721,12 @@ getPooledGeno <- function(x, type = NULL, sex = NULL) {
47044721# ' GRM <- calcBeeGRMIbs(x = geno, sex = sex)
47054722# ' # You can visualise this matrix with the function image() from the package 'Matrix'
47064723# '
4707- # ' #Look at the diagonal at the relationship matrix
4724+ # ' # Look at the diagonal at the relationship matrix
47084725# ' x <- diag(GRM)
47094726# ' hist(x)
47104727# ' summary(x)
47114728# '
4712- # ' #Look at the off-diagonal at the relationship matrix
4729+ # ' # Look at the off-diagonal at the relationship matrix
47134730# ' x <- GRM[lower.tri(x = GRM, diag = FALSE)]
47144731# ' hist(x)
47154732# ' summary(x)
@@ -4745,8 +4762,12 @@ getPooledGeno <- function(x, type = NULL, sex = NULL) {
47454762# ' calcBeeGRMIbs(x = rbind(queenGeno, pooledGenoW), sex = c("F","F"))
47464763# ' # You can now compare how this compare to relationships between the queen
47474764# ' # individual workers!
4765+ # '
4766+ # ' # Return components
4767+ # ' calcBeeGRMIbs(x = rbind(queenGeno, pooledGenoW), sex = c("F","F"),
4768+ # ' returnComponents = TRUE)
47484769# ' @export
4749- calcBeeGRMIbs <- function (x , sex , alleleFreq = NULL ) {
4770+ calcBeeGRMIbs <- function (x , sex , alleleFreq = NULL , returnComponents = FALSE ) {
47504771 if (! is.matrix(x )) {
47514772 stop(" Argument x must be a matrix class object!" )
47524773 }
@@ -4776,8 +4797,13 @@ calcBeeGRMIbs <- function(x, sex, alleleFreq = NULL) {
47764797 # This would overwrite x only once, at expense of doubling RAM
47774798 x [, site ] <- x [, site ] - ploidy * alleleFreq [site ]
47784799 }
4779- G <- tcrossprod(x ) / (2 * sum(alleleFreq * (1 - alleleFreq )))
4780- return (G )
4800+ scale <- 2 * sum(alleleFreq * (1 - alleleFreq ))
4801+ G <- tcrossprod(x ) / scale
4802+ if (returnComponents ) {
4803+ return (list (G = G , x = x , alleleFreq = alleleFreq , scale = scale ))
4804+ } else {
4805+ return (G )
4806+ }
47814807}
47824808
47834809# ' @describeIn calcBeeGRMIbs Calculate allele frequencies from honeybee genotypes
@@ -6322,6 +6348,16 @@ calcColonyAa <- function(x, FUN = mapCasteToColonyAa, simParamBee = NULL, ...) {
63226348# ' in \code{\link[SIMplyBee]{SimParamBee}}. The two csd alleles must be different to
63236349# ' ensure heterozygosity at the csd locus.
63246350# ' @param simParamBee global simulation parameters.
6351+ # ' @examples
6352+ # ' founderGenomes <- quickHaplo(nInd = 4, nChr = 1, segSites = 10)
6353+ # '
6354+ # ' SP <- SimParamBee$new(founderGenomes, nCsdAlleles = 2)
6355+ # ' tmp <- createVirginQueens(founderGenomes)
6356+ # ' getCsdAlleles(tmp)
6357+ # '
6358+ # ' SP <- SimParamBee$new(founderGenomes, nCsdAlleles = 4)
6359+ # ' tmp <- createVirginQueens(founderGenomes)
6360+ # ' getCsdAlleles(tmp)
63256361# '
63266362# ' @return Returns an object of \code{\link[AlphaSimR]{Pop-class}}
63276363editCsdLocus <- function (pop , alleles = NULL , simParamBee = NULL ) {
@@ -6334,7 +6370,10 @@ editCsdLocus <- function(pop, alleles = NULL, simParamBee = NULL) {
63346370 alleles <- expand.grid(as.data.frame(matrix (rep(0 : 1 , length(csdSites )), nrow = 2 , byrow = FALSE )))
63356371 # Sample two different alleles (without replacement) for each individual
63366372 nAlleles <- simParamBee $ nCsdAlleles
6337- alleles <- sapply(seq_len(pop @ nInd ), FUN = function (x ) list (alleles [sample(nAlleles , size = 2 , replace = F ), ]))
6373+ alleles <- sapply(X = seq_len(pop @ nInd ),
6374+ FUN = function (x ) {
6375+ list (alleles [sample(nAlleles , size = 2 , replace = FALSE ), , drop = FALSE ])
6376+ })
63386377 }
63396378
63406379 if (pop @ nInd != length(alleles )) {
0 commit comments