You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: R/buildComponent.R
+120-3Lines changed: 120 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,9 @@
19
19
#' @param isChild_method character. The method to use for computing the isChild matrix. Options are "classic" or "partialparent"
20
20
#' @param adjBeta_method numeric The method to use for computing the building the adjacency_method matrix when using the "beta" build
21
21
#' @param compress logical. If TRUE, use compression when saving the checkpoint files. Defaults to TRUE.
22
+
#' @param mz_twins logical. If TRUE, merge MZ co-twin columns in the r2 matrix before tcrossprod so that MZ twins are coded with relatedness 1 instead of 0.5. Twin pairs are identified from the \code{twinID} column. When a \code{zygosity} column is also present, only pairs where both members have \code{zygosity == "MZ"} are used; otherwise all \code{twinID} pairs are assumed to be MZ. Defaults to FALSE.
23
+
#' @param mz_method character. The method to handle MZ twins. Options are "merging" (default) or "addtwins". "addtwins" adds the twin2 column to the twin1 column before tcrossprod so that all relatedness flows through a single source, then leaves the twin2 column as zero and relies on the fact that the row/col names are the same to copy the values back to twin2 after tcrossprod. "merging" merges the twin2 column into the twin1 column before tcrossprod and then copies the values back to twin2 after tcrossprod so that both twins appear in the final matrix.
24
+
#' @param beta logical. Used for benchmarking
22
25
#' @param ... additional arguments to be passed to \code{\link{ped2com}}
23
26
#' @details The algorithms and methodologies used in this function are further discussed and exemplified in the vignette titled "examplePedigreeFunctions". For more advanced scenarios and detailed explanations, consult this vignette.
if (config$resume==TRUE&& file.exists(checkpoint_files$final_matrix)) {
126
148
if (config$verbose==TRUE) cat("Loading final computed matrix...\n")
127
149
return(readRDS(checkpoint_files$final_matrix))
128
150
}
129
151
152
+
if (mz_method%in% c("merging") &&mz_twins==TRUE&&!is.null(mz_row_pairs) && length(mz_row_pairs) >0&&
153
+
config$component%in% c("additive")) {
154
+
# replace all MZ twin IDs with the first twin's ID in each pair so they are merged for the path tracing and all subsequent steps. We will copy the values back to the second twin at the end.
# Copy twin1's row/col to twin2 so both twins appear in the final matrix.
363
+
if (config$sparse==FALSE) {
364
+
r<- as.matrix(r)
365
+
rnames<- rownames(r)
366
+
ids_mat<- do.call(rbind, mz_id_pairs)
367
+
idx1_all<- match(ids_mat[, 1], rnames)
368
+
idx2_all<- match(ids_mat[, 2], rnames)
369
+
# Batch copy: twin1 rows/cols -> twin2 rows/cols
370
+
r[idx2_all, ] <-r[idx1_all, ]
371
+
372
+
r[, idx2_all] <-r[, idx1_all]
373
+
} else {
374
+
# TODO this is really slow. Can we do it without coercing to dense? Maybe by doing row/col replacement on the sparse matrix directly? Or by constructing a sparse matrix with the twin2 values and adding it to r?
375
+
# r <- df_add
376
+
377
+
rnames<-r@Dimnames[[1]]
378
+
379
+
ids_mat<- do.call(rbind, mz_id_pairs)
380
+
# needs to use sparse indexing to avoid coercion to dense
381
+
idx1_all<- match(ids_mat[, 1], rnames)
382
+
idx2_all<- match(ids_mat[, 2], rnames)
383
+
384
+
twin1_rows<-r[idx1_all, , drop=FALSE]
385
+
twin1_cols<-r[, idx1_all, drop=FALSE]
386
+
twin1_rows@Dimnames[[1]] <-rnames[idx2_all]
387
+
twin1_cols@Dimnames[[2]] <-rnames[idx2_all]
388
+
twin1_self<-r[idx1_all, idx1_all, drop=FALSE]
389
+
twin1_self@Dimnames[[1]] <-rnames[idx2_all]
390
+
391
+
r[idx2_all, ] <-twin1_rows
392
+
r[, idx2_all] <-twin1_cols
393
+
r[idx2_all, idx2_all] <-twin1_self
394
+
395
+
# Batch copy: twin1 rows/cols -> twin2 rows/cols
396
+
397
+
# Row/column replacement on a dsCMatrix (symmetric) causes Matrix to
398
+
# coerce to dgCMatrix (general), doubling stored entries. Convert back
399
+
400
+
r<-Matrix::drop0(r)
401
+
402
+
# so both mz_method paths return the same sparse class.
403
+
if (methods::is(r, "CsparseMatrix") &&!methods::is(r, "symmetricMatrix")) {
404
+
r<-Matrix::forceSymmetric(r)
405
+
}
406
+
}
407
+
if (config$verbose==TRUE) {
408
+
message("Restored ", length(mz_row_pairs), " MZ twin pair(s) in relatedness matrix")
409
+
}
410
+
}
411
+
412
+
303
413
if (config$component%in% c("mitochondrial", "mtdna", "mitochondria")) {
304
414
r@x<- rep(1, length(r@x))
305
415
# Assign 1 to all nonzero elements for mitochondrial component
306
416
}
307
417
308
-
if (config$sparse==FALSE) {
418
+
# Remove explicit zeros so that both mz_method paths produce
419
+
# structurally identical sparse matrices
420
+
421
+
if (config$sparse==FALSE&&!methods::is(r, "matrix")) {
309
422
r<- as.matrix(r)
310
423
}
311
424
# flattens diagonal if you don't want to deal with inbreeding
0 commit comments