From 021e8fdc9de744db5cc549a1e8e80948a1919e01 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Feb 2026 21:13:43 +0000 Subject: [PATCH] Fix twin matrix size mismatch between addtwins and merging methods Two issues caused the sparse matrices from the two mz_method paths to differ structurally: 1. The merging method iterated over mz_pairs (row indices) but treated the values as IDs. Changed to iterate over mz_id_pairs which holds the actual pedigree IDs. Currently masked because test data has sequential IDs matching row positions, but would break with non-sequential IDs. 2. The different computational paths (column-merge-before-tcrossprod vs pedigree-modify-then-post-copy) leave different explicit zeros in the sparse matrix. Added Matrix::drop0() before returning to normalize the sparse representation so both methods produce structurally identical results. https://claude.ai/code/session_01PXbgV7bdA9sg7St6xBbg3T --- R/buildComponent.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/buildComponent.R b/R/buildComponent.R index bad3b3f6..e0392390 100644 --- a/R/buildComponent.R +++ b/R/buildComponent.R @@ -144,7 +144,7 @@ ped2com <- function(ped, component, c(ped$ID[pair[1]], ped$ID[pair[2]]) }) - for (id_pair in mz_pairs) { + for (id_pair in mz_id_pairs) { twin1_id <- id_pair[1] twin2_id <- id_pair[2] twin2_row <- which(ped$ID == twin2_id) @@ -383,7 +383,11 @@ ped2com <- function(ped, component, # Assign 1 to all nonzero elements for mitochondrial component } - if (config$sparse == FALSE) { + # Remove explicit zeros so that both mz_method paths produce + # structurally identical sparse matrices + if (config$sparse == TRUE) { + r <- Matrix::drop0(r) + } else { r <- as.matrix(r) } # flattens diagonal if you don't want to deal with inbreeding