-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenAssign.R
executable file
·70 lines (63 loc) · 2.16 KB
/
genAssign.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Generic
#' Generation assignment
#'
#' Given a pedigree, the function assigns the generation number to which each
#' individual belongs.
#'
#' 0 is the base population.
#'
#' Migrants, or any individuals where both parents are unknown, are assigned to
#' generation zero. If parents of an individual are from two different
#' generations (e.g., dam = 0 and sire = 1), the individual is assigned to the
#' generation following the greater of the two parents (e.g., 2 in this
#' example).
#'
#' @aliases genAssign genAssign.default genAssign.numPed
#' @param pedigree A pedigree where the columns are ordered ID, Dam, Sire
#' @param \dots Arguments to be passed to methods
#'
#' @return A vector of values is returned. This vector is in the same order as
#' the ID column of the pedigree.
#' @author \email{matthewwolak@@gmail.com}
#' @export
genAssign <- function(pedigree, ...){
UseMethod("genAssign", pedigree)
}
###############################################################################
# Methods:
#' @rdname genAssign
#' @method genAssign default
#' @export
genAssign.default <- function(pedigree, ...)
{
n <- nrow(pedigree)
numbCols <- which(apply(pedigree[, 1:3], MARGIN = 2, FUN = is.integer) |
apply(pedigree[, 1:3], MARGIN = 2, FUN = is.numeric))
if(length(numbCols) > 0 && any(apply(pedigree[, numbCols], MARGIN = 2, FUN = function(x){min(x, na.rm = TRUE) < 0}))){
warning("Negative values in pedigree interpreted as missing values")
pedigree[pedigree < 0] <- -998
}
if(!all(apply(pedigree[, 1:3], MARGIN = 2, FUN = is.numeric)) | any(apply(pedigree[, 1:3], MARGIN = 2, FUN = is.na))){
pedigree[, 1:3] <- numPed(pedigree[, 1:3])
}
Cout <- .C("ga", PACKAGE = "nadiv",
as.integer(pedigree[, 2] - 1),
as.integer(pedigree[, 3] - 1),
vector("integer", length = n),
as.integer(n))
Cout[[3]]
}
######################################
#' @rdname genAssign
#' @method genAssign numPed
#' @export
genAssign.numPed <- function(pedigree, ...)
{
n <- nrow(pedigree)
Cout <- .C("ga", PACKAGE = "nadiv",
as.integer(pedigree[, 2] - 1),
as.integer(pedigree[, 3] - 1),
vector("integer", length = n),
as.integer(n))
Cout[[3]]
}