Skip to content

Commit af0f525

Browse files
committed
Merge branch 'devel' of https://github.com/HighlanderLab/SIMplyBee into devel
2 parents 9789a12 + bfe06ec commit af0f525

22 files changed

Lines changed: 149 additions & 105 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ vignettes/*.pdf
4040

4141
# Other
4242
.DS_Store
43+
src/*.o
44+
src/*.so

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: SIMplyBee
22
Type: Package
33
Title: 'AlphaSimR' Extension for Simulating Honeybee Populations and Breeding Programmes
4-
Version: 0.4.1
4+
Version: 0.4.2
55
Authors@R: c(
66
person("Jana", "Obšteter", email = "obsteter.jana@gmail.com",
77
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1511-3916")),
@@ -27,7 +27,7 @@ License: MIT + file LICENSE
2727
Encoding: UTF-8
2828
LazyData: true
2929
Imports: methods, R6, stats, utils, extraDistr (>= 1.9.1), RANN, Rcpp (>= 0.12.7)
30-
Depends: R (>= 3.3.0), AlphaSimR (>= 1.5.3)
30+
Depends: R (>= 3.3.0), AlphaSimR (>= 1.6.1)
3131
LinkingTo: Rcpp, RcppArmadillo (>= 0.7.500.0.0), BH
3232
RoxygenNote: 7.3.2
3333
Suggests:

NAMESPACE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export(getGv)
6969
export(getIbdHaplo)
7070
export(getId)
7171
export(getLocation)
72-
export(getMisc)
7372
export(getPheno)
7473
export(getPooledGeno)
7574
export(getQtlGeno)
@@ -192,7 +191,6 @@ export(replaceWorkers)
192191
export(resetEvents)
193192
export(selectColonies)
194193
export(setLocation)
195-
export(setMisc)
196194
export(setQueensYearOfBirth)
197195
export(simulateHoneyBeeGenomes)
198196
export(split)

NEWS.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ editor_options:
44
wrap: 72
55
---
66

7+
# SIMplyBee version 0.4.2
8+
9+
- 20??-??-??
10+
11+
## Major changes
12+
- TODO
13+
14+
## New features
15+
- TODO
16+
17+
## Bug fixes
18+
- editCsdLocus() works now also with just 2 alleles #591
19+
20+
## Backgrond/package/etc. work
21+
- calcBeeGRMIbs() can now return centred genotype matrix, allele frequencies,
22+
and scale factor used to calculate the GRM #594
23+
- Improved default for getPooledGeno() (to type="mean"") and added an example
24+
on how to collect pooled workers' genotype accross colonies (but pooling is
25+
done within a colony!) #592
26+
- We now removed setMisc() and getMisc() because we now use the new AlphaSimR
27+
structure of the misc slot that is much easier to use. #584
28+
729
# SIMplyBee version 0.4.1
830

931
- 2024-09-19
@@ -26,7 +48,7 @@ which caused an error. We now read in the locations from a csv file.
2648
now c(0, 0) PR#500
2749

2850

29-
## New features ##
51+
## New features
3052
- In setLocation(MultiColony) we can set one location (numeric) or
3153
multiple (list or data.frame) PR#500
3254
- getLocation(MultiColony) got the collapse argument
@@ -57,8 +79,6 @@ which caused an error. We now read in the locations from a csv file.
5779
- Bug fix - get\*Haplo() functions were returning diploid drones when
5880
input was a Pop-class
5981

60-
-
61-
6282
# SIMplyBee version 0.3.0
6383

6484
- 2022-12-05 First public/CRAN version of the package

R/Functions_L0_auxilary.R

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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}}
63276363
editCsdLocus <- 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)) {

R/Functions_L1_Pop.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,8 @@ createCastePop <- function(x, caste = NULL, nInd = NULL,
423423
stop("MapPop-class can only be used to create virgin queens!")
424424
}
425425
ret <- newPop(x, simParam = simParamBee)
426-
if (!is.null(simParamBee$csdChr)) {
427-
if (editCsd) {
428-
ret <- editCsdLocus(ret, alleles = csdAlleles, simParamBee = simParamBee)
429-
}
426+
if (!is.null(simParamBee$csdChr) && editCsd) {
427+
ret <- editCsdLocus(pop = ret, alleles = csdAlleles, simParamBee = simParamBee)
430428
}
431429
ret@sex[] <- "F"
432430
simParamBee$changeCaste(id = ret@id, caste = "virginQueens")

man/addCastePop.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/buildUp.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calcBeeGRMIbs.Rd

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)