Skip to content

Commit 2562330

Browse files
all keep_ids to be used on all matrices
1 parent a462396 commit 2562330

8 files changed

Lines changed: 133 additions & 17 deletions

File tree

R/buildComponent.R

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ ped2com <- function(ped, component,
316316
}
317317

318318
if (config$component == "generation") { # no need to do the rest
319+
gen <- .subsetKeepIds(
320+
component = gen,
321+
keep_ids = config$keep_ids,
322+
available_ids = rownames(r),
323+
config = config,
324+
verbose_message = "Subsetting generation component to %d target individuals\n"
325+
)
319326
return(gen)
320327
} else {
321328
if (config$verbose == TRUE) {
@@ -360,20 +367,14 @@ ped2com <- function(ped, component,
360367
# Subset rows of r2 to target individuals if requested.
361368
# All columns are kept so dot products use the full ancestry paths.
362369
if (!is.null(config$keep_ids)) {
363-
idx <- match(config$keep_ids, rownames(r2))
364-
missing <- config$keep_ids[is.na(idx)]
365-
if (length(missing) > 0) {
366-
warning(
367-
length(missing), " keep_ids not found in pedigree and will be dropped: ",
368-
paste(Matrix::head(missing, 5), collapse = ", "),
369-
if (length(missing) > 5) " ..." else ""
370-
)
371-
}
372-
idx <- idx[!is.na(idx)]
373-
if (config$verbose == TRUE) {
374-
cat(sprintf("Subsetting r2 to %d target individuals before tcrossprod\n", length(idx)))
375-
}
376-
r2 <- r2[idx, , drop = FALSE]
370+
r2 <- .subsetKeepIds(
371+
component = r2,
372+
keep_ids = config$keep_ids,
373+
available_ids = rownames(r2),
374+
config = config,
375+
verbose_message = "Subsetting r2 to %d target individuals before tcrossprod\n",
376+
drop = FALSE
377+
)
377378
}
378379

379380
use_tcrossprod_checkpoint <- FALSE
@@ -840,3 +841,45 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
840841
}
841842
list_of_adjacencies
842843
}
844+
845+
846+
#' Subset output to requested IDs
847+
#' @inheritParams ped2com
848+
#' @param component A component to subset.
849+
#' @param keep_ids Character vector of IDs to retain.
850+
#' @param available_ids Character vector of IDs available in \code{x}.
851+
#' @param verbose_message Character. Message prefix to print when \code{config$verbose == TRUE}.
852+
#' @param drop logical. Passed to \code{[} when subsetting matrices.
853+
#' @keywords internal
854+
.subsetKeepIds <- function(component, keep_ids = NULL, available_ids, config,
855+
verbose_message = "Subsetting to %d target individuals\n",
856+
drop = FALSE) {
857+
if (is.null(keep_ids)) {
858+
return(component)
859+
}
860+
861+
idx <- match(keep_ids, available_ids)
862+
missing <- keep_ids[is.na(idx)]
863+
864+
if (length(missing) > 0) {
865+
warning(
866+
length(missing), " keep_ids not found in pedigree and will be dropped: ",
867+
paste(Matrix::head(missing, 5), collapse = ", "),
868+
if (length(missing) > 5) " ..." else ""
869+
)
870+
}
871+
872+
idx <- idx[!is.na(idx)]
873+
874+
if (config$verbose == TRUE) {
875+
cat(sprintf(verbose_message, length(idx)))
876+
}
877+
878+
if (is.matrix(component) || methods::is(component, "Matrix")) {
879+
component <- component[idx, , drop = drop]
880+
} else {
881+
component <- component[idx]
882+
}
883+
884+
component
885+
}

R/buildComponentWrappers.R

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ped2add <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
1919
compress = TRUE,
2020
mz_twins = FALSE,
2121
mz_method = "addtwins",
22-
force_symmetric = FALSE,
22+
force_symmetric = TRUE,
2323
...) {
2424
ped2com(
2525
ped = ped,
@@ -59,6 +59,7 @@ ped2mit <- ped2mt <- function(ped, max_gen = 25,
5959
flatten_diag = FALSE,
6060
standardize_colnames = TRUE,
6161
transpose_method = "tcrossprod",
62+
keep_ids = NULL,
6263
adjacency_method = "direct",
6364
saveable = FALSE,
6465
resume = FALSE,
@@ -79,6 +80,7 @@ ped2mit <- ped2mt <- function(ped, max_gen = 25,
7980
flatten_diag = flatten_diag,
8081
standardize_colnames = standardize_colnames,
8182
transpose_method = transpose_method,
83+
keep_ids = keep_ids,
8284
adjacency_method = adjacency_method,
8385
saveable = saveable,
8486
resume = resume,
@@ -100,6 +102,7 @@ ped2cn <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
100102
gc = FALSE, flatten_diag = FALSE,
101103
standardize_colnames = TRUE,
102104
transpose_method = "tcrossprod",
105+
keep_ids = NULL,
103106
saveable = FALSE,
104107
resume = FALSE,
105108
save_rate = 5,
@@ -121,6 +124,7 @@ ped2cn <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
121124
flatten_diag = flatten_diag,
122125
standardize_colnames = standardize_colnames,
123126
transpose_method = transpose_method,
127+
keep_ids = keep_ids,
124128
saveable = saveable,
125129
resume = resume,
126130
save_rate_gen = save_rate_gen,
@@ -140,6 +144,7 @@ ped2gen <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
140144
gc = FALSE, flatten_diag = FALSE,
141145
standardize_colnames = TRUE,
142146
transpose_method = "tcrossprod",
147+
keep_ids = NULL,
143148
saveable = FALSE,
144149
resume = FALSE,
145150
save_rate = 5,
@@ -161,6 +166,7 @@ ped2gen <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
161166
flatten_diag = flatten_diag,
162167
standardize_colnames = standardize_colnames,
163168
transpose_method = transpose_method,
169+
keep_ids = keep_ids,
164170
saveable = saveable,
165171
resume = resume,
166172
save_rate_gen = save_rate_gen,
@@ -180,6 +186,21 @@ ped2gen <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
180186
#' @export
181187
#'
182188
ped2ce <- function(ped, personID = "ID",
189+
keep_ids = NULL,
190+
sparse = FALSE, verbose = FALSE,
183191
...) {
192+
if (!is.null(keep_ids)) {
193+
ped <- ped[ped[[personID]] %in% keep_ids, ]
194+
}
195+
if (sparse) {
196+
mat <- Matrix::sparseMatrix(
197+
i = seq_len(nrow(ped)),
198+
j = seq_len(nrow(ped)),
199+
x = 1,
200+
dimnames = list(ped[[personID]], ped[[personID]])
201+
)
202+
return(mat)
203+
} else {
184204
matrix(1, nrow = nrow(ped), ncol = nrow(ped), dimnames = list(ped[[personID]], ped[[personID]]))
205+
}
185206
}

man/dot-subsetKeepIds.Rd

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

man/ped2add.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/ped2ce.Rd

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

man/ped2cn.Rd

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

man/ped2gen.Rd

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

man/ped2mit.Rd

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

0 commit comments

Comments
 (0)