Skip to content

Commit

Permalink
Merge pull request #579 from gaynorr/devel
Browse files Browse the repository at this point in the history
added setMisc and getMisc
  • Loading branch information
janaobsteter authored Jul 17, 2024
2 parents e0abfe5 + ee0e891 commit eb80929
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 45 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Encoding: UTF-8
LazyData: true
Imports: methods, R6, stats, utils, extraDistr (>= 1.9.1), RANN
Depends: R (>= 3.3.0), AlphaSimR (>= 1.3.2)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
rmarkdown,
knitr,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export(getGv)
export(getIbdHaplo)
export(getId)
export(getLocation)
export(getMisc)
export(getPheno)
export(getPooledGeno)
export(getQtlGeno)
Expand Down Expand Up @@ -191,6 +192,7 @@ export(replaceWorkers)
export(resetEvents)
export(selectColonies)
export(setLocation)
export(setMisc)
export(setQueensYearOfBirth)
export(simulateHoneyBeeGenomes)
export(split)
Expand Down Expand Up @@ -219,3 +221,4 @@ importFrom(stats,rbeta)
importFrom(stats,rnorm)
importFrom(stats,rpois)
importFrom(stats,runif)
importFrom(utils,packageVersion)
209 changes: 185 additions & 24 deletions R/Functions_L0_auxilary.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,15 @@ nCaste <- function(x, caste = "all", simParamBee = NULL) {
}
} else {
if (caste == "fathers") {
ret <- ifelse(!is.null(slot(x, "queen")), nInd(x@queen@misc[[1]]$fathers), 0)
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
ret <- ifelse(!is.null(slot(x, "queen")),
nInd(x@queen@misc$fathers[[1]]),
0)
}else{
ret <- ifelse(!is.null(slot(x, "queen")),
nInd(x@queen@misc[[1]]$fathers),
0)
}
} else if (caste == "drones") {
ret <- ifelse(!is.null(slot(x, caste)), sum(isDrone(x = x@drones, simParamBee = simParamBee)), 0)
} else {
Expand Down Expand Up @@ -209,7 +217,11 @@ nFathers <- function(x, simParamBee = NULL) {
ret <- rep(x = 0, times = nInd)
for (ind in seq_len(nInd)) {
if (isQueen(x[ind], simParamBee = simParamBee)) {
ret[ind] <- nInd(x@misc[[ind]]$fathers)
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
ret[ind] <- nInd(x@misc$fathers[[ind]])
}else{
ret[ind] <- nInd(x@misc[[ind]]$fathers)
}
}
}
} else {
Expand Down Expand Up @@ -320,10 +332,17 @@ calcQueensPHomBrood <- function(x, simParamBee = NULL) {
X = getCsdAlleles(x[ind], simParamBee = simParamBee), MARGIN = 1,
FUN = function(x) paste0(x, collapse = "")
)
fathersCsd <- apply(
X = getCsdAlleles(x@misc[[ind]]$fathers, simParamBee = simParamBee), MARGIN = 1,
FUN = function(x) paste0(x, collapse = "")
)
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
fathersCsd <- apply(
X = getCsdAlleles(x@misc$fathers[[ind]], simParamBee = simParamBee), MARGIN = 1,
FUN = function(x) paste0(x, collapse = "")
)
}else{
fathersCsd <- apply(
X = getCsdAlleles(x@misc[[ind]]$fathers, simParamBee = simParamBee), MARGIN = 1,
FUN = function(x) paste0(x, collapse = "")
)
}
nComb <- length(queensCsd) * length(fathersCsd)
ret[ind] <- sum(fathersCsd %in% queensCsd) / nComb
}
Expand Down Expand Up @@ -352,15 +371,29 @@ pHomBrood <- function(x, simParamBee = NULL) {
}
ret <- rep(x = NA, times = nInd(x))
for (ind in seq_len(nInd(x))) {
if (!is.null(x@misc[[ind]]$pHomBrood)) {
ret[ind] <- x@misc[[ind]]$pHomBrood
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
if (!is.null(x@misc$pHomBrood[[ind]])) {
ret[ind] <- x@misc$pHomBrood[[ind]]
}
}else{
if (!is.null(x@misc[[ind]]$pHomBrood)) {
ret[ind] <- x@misc[[ind]]$pHomBrood
}
}
}
} else if (isColony(x)) {
if (is.null(x@queen@misc[[1]]$pHomBrood)) {
ret <- NA
} else {
ret <- x@queen@misc[[1]]$pHomBrood
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
if (is.null(x@queen@misc$pHomBrood[[1]])) {
ret <- NA
} else {
ret <- x@queen@misc$pHomBrood[[1]]
}
}else{
if (is.null(x@queen@misc[[1]]$pHomBrood)) {
ret <- NA
} else {
ret <- x@queen@misc[[1]]$pHomBrood
}
}
} else if (isMultiColony(x)) {
ret <- sapply(X = x@colonies, FUN = pHomBrood)
Expand All @@ -384,15 +417,29 @@ nHomBrood <- function(x, simParamBee = NULL) {
}
ret <- rep(x = NA, times = nInd(x))
for (ind in seq_len(nInd(x))) {
if (!is.null(x@misc[[ind]]$nHomBrood)) {
ret[ind] <- x@misc[[ind]]$nHomBrood
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
if (!is.null(x@misc$nHomBrood[[ind]])) {
ret[ind] <- x@misc$nHomBrood[[ind]]
}
}else{
if (!is.null(x@misc[[ind]]$nHomBrood)) {
ret[ind] <- x@misc[[ind]]$nHomBrood
}
}
}
} else if (isColony(x)) {
if (is.null(x@queen@misc[[1]]$nHomBrood)) {
ret <- NA
} else {
ret <- x@queen@misc[[1]]$nHomBrood
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
if (is.null(x@queen@misc$nHomBrood[[1]])) {
ret <- NA
} else {
ret <- x@queen@misc$nHomBrood[[1]]
}
}else{
if (is.null(x@queen@misc[[1]]$nHomBrood)) {
ret <- NA
} else {
ret <- x@queen@misc[[1]]$nHomBrood
}
}
} else if (isMultiColony(x)) {
ret <- sapply(X = x@colonies, FUN = nHomBrood)
Expand Down Expand Up @@ -1001,15 +1048,25 @@ getQueenYearOfBirth <- function(x, simParamBee = NULL) {
nInd <- nInd(x)
ret <- rep(x = NA, times = nInd)
for (ind in seq_len(nInd)) {
if (!is.null(x@misc[[ind]]$yearOfBirth)) {
ret[ind] <- x@misc[[ind]]$yearOfBirth
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
if (!is.null(x@misc$yearOfBirth[[ind]])) {
ret[ind] <- x@misc$yearOfBirth[[ind]]
}
}else{
if (!is.null(x@misc[[ind]]$yearOfBirth)) {
ret[ind] <- x@misc[[ind]]$yearOfBirth
}
}
}
if (nInd > 1) {
names(ret) <- getId(x)
}
} else if (isColony(x)) {
ret <- ifelse(is.null(x@queen@misc[[1]]$yearOfBirth), NA, x@queen@misc[[1]]$yearOfBirth)
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
ret <- ifelse(is.null(x@queen@misc$yearOfBirth[[1]]), NA, x@queen@misc[[1]]$yearOfBirth)
}else{
ret <- ifelse(is.null(x@queen@misc[[1]]$yearOfBirth), NA, x@queen@misc[[1]]$yearOfBirth)
}
} else if (isMultiColony(x)) {
ret <- sapply(X = x@colonies, FUN = getQueenYearOfBirth, simParamBee = simParamBee)
names(ret) <- getId(x)
Expand Down Expand Up @@ -1068,16 +1125,26 @@ getQueenAge <- function(x, currentYear, simParamBee = NULL) {
nInd <- nInd(x)
ret <- rep(x = NA, times = nInd)
for (ind in seq_len(nInd)) {
if (!is.null(x@misc[[ind]]$yearOfBirth)) {
ret[ind] <- currentYear - x@misc[[ind]]$yearOfBirth
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
if (!is.null(x@misc$yearOfBirth[[ind]])) {
ret[ind] <- currentYear - x@misc$yearOfBirth[[ind]]
}
}else{
if (!is.null(x@misc[[ind]]$yearOfBirth)) {
ret[ind] <- currentYear - x@misc[[ind]]$yearOfBirth
}
}
}
if (nInd > 1) {
names(ret) <- getId(x)
}
} else if (isColony(x)) {
if (isQueenPresent(x, simParamBee = simParamBee)) {
ret <- currentYear - x@queen@misc[[1]]$yearOfBirth
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
ret <- currentYear - x@queen@misc$yearOfBirth[[1]]
}else{
ret <- currentYear - x@queen@misc[[1]]$yearOfBirth
}
} else {
ret <- NA
}
Expand Down Expand Up @@ -6524,3 +6591,97 @@ createCrossPlan <- function(x,
names(crossPlan) <- virginId
return(crossPlan)
}

# Misc helpers
# These functions replace the defunct functions of the same name in AlphaSimR

#' @rdname setMisc
#' @title Set miscellaneous information in a population
#'
#' @description Set miscellaneous information in a population
#'
#' @param x \code{\link{Pop-class}}
#' @param node character, name of the node to set within the \code{x@misc} slot
#' @param value, value to be saved into \code{x@misc[[*]][[node]]}; length of
#' \code{value} should be equal to \code{nInd(x)}; if its length is 1, then
#' it is repeated using \code{rep} (see examples)
#'
#' @details A \code{NULL} in \code{value} is ignored
#'
#' @return \code{\link{Pop-class}}
#'
#' @export
setMisc <- function(x, node = NULL, value = NULL) {
if (isPop(x)) {
if (is.null(node)) {
stop("Argument node must be provided!")
}
if (is.null(value)) {
stop("Argument value must be provided!")
}
n <- nInd(x)
if (length(value) == 1 && n > 1) {
value <- rep(x = value, times = n)
}
if (length(value) != n) {
stop("Argument value must be of length 1 or nInd(x)!")
}

# Check current AlphaSimR version for new or legacy misc slot
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
# New misc slot
x@misc[[node]] = value
}else{
# Legacy misc slot
names(value) = rep(x = node, times = n)
inode = match(names(x@misc[[1]]),node)
inode = inode[!is.na(inode)]
if(length(inode) == 0){
x@misc = sapply(seq_len(n),function(ind){
c(x@misc[[ind]],value[ind])
},simplify = FALSE)
}else{
x@misc = sapply(seq_len(n),function(ind){
c(x@misc[[ind]],value[ind])[-inode]
},simplify = FALSE)
}
}

}

return(x)
}

#' @rdname getMisc
#' @title Get miscellaneous information in a population
#'
#' @description Get miscellaneous information in a population
#'
#' @param x \code{\link{Pop-class}}
#' @param node character, name of the node to get from the \code{x@misc} slot;
#' if \code{NULL} the whole \code{x@misc} slot is returned
#'
#' @return The \code{x@misc} slot or its nodes \code{x@misc[[*]][[node]]}
#'
#' @export
getMisc <- function(x, node = NULL) {
if (isPop(x)) {
if (is.null(node)) {
ret <- x@misc
} else {
# Check current AlphaSimR version for new or legacy misc slot
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
# New misc slot
ret = x@misc[[node]]
}else{
# Legacy misc slot
nInd = nInd(x)
ret = lapply(x@misc,'[[',node)
}
}
} else {
stop("Argument x must be a Pop class object!")
}
return(ret)
}

49 changes: 36 additions & 13 deletions R/Functions_L1_Pop.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ getCastePop <- function(x, caste = "all", nInd = NULL, use = "rand",
} else {
if (caste == "fathers") {
if (isQueenPresent(x, simParamBee = simParamBee)) {
pop <- x@queen@misc[[1]]$fathers
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
pop <- x@queen@misc$fathers[[1]]
}else{
pop <- x@queen@misc[[1]]$fathers
}
} else {
pop <- NULL
}
Expand Down Expand Up @@ -218,19 +222,34 @@ getFathers <- function(x, nInd = NULL, use = "rand", collapse = FALSE, simParamB
simParamBee <- get(x = "SP", envir = .GlobalEnv)
}
if (isPop(x)) { # DO WE WANT TO PUT THIS IN getCastePop???
ret <- lapply(
X = x@misc,
FUN = function(z) {
if (is.null(z$fathers)) {
ret <- NULL
} else {
if (is.null(nInd)) {
n <- nInd(z$fathers)
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
ret = lapply(X = x@misc$fathers,
FUN = function(z){
if(is.null(z)){
ret = NULL
}else{
if (is.null(nInd)) {
n <- nInd(z)
}
ret <- selectInd(pop = z, nInd = n, use = use, simParam = simParamBee)
}
}
)
}else{
ret <- lapply(
X = x@misc,
FUN = function(z) {
if (is.null(z$fathers)) {
ret <- NULL
} else {
if (is.null(nInd)) {
n <- nInd(z$fathers)
}
ret <- selectInd(pop = z$fathers, nInd = n, use = use, simParam = simParamBee)
}
ret <- selectInd(pop = z$fathers, nInd = n, use = use, simParam = simParamBee)
}
}
)
)
}
if (nInd(x) == 1) {
ret <- ret[[1]]
}
Expand Down Expand Up @@ -1464,7 +1483,11 @@ cross <- function(x,
} else if (isColony(x)) {
virginQueen <- selectInd(x@virginQueens, nInd = 1, use = "rand", simParam = simParamBee)
}
virginQueen@misc[[1]]$fathers <- virginQueenDrones
if(packageVersion("AlphaSimR") > package_version("1.5.3")){
virginQueen@misc$fathers[[1]] <- virginQueenDrones
}else{
virginQueen@misc[[1]]$fathers <- virginQueenDrones
}
simParamBee$changeCaste(id = virginQueen@id, caste = "Q")
simParamBee$changeCaste(id = virginQueenDrones@id, caste = "F")

Expand Down
Loading

0 comments on commit eb80929

Please sign in to comment.