Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
8a1aadf
cran prep
smasongarrison Jan 7, 2026
6fdd2d3
Merge branch 'main' into dev_main
smasongarrison Jan 9, 2026
e1b35b8
Merge branch 'main' into dev_main
smasongarrison Jan 14, 2026
f98188e
Create buildmxPedigrees.R
smasongarrison Jan 14, 2026
0b8dc52
Update buildmxPedigrees.R
smasongarrison Jan 16, 2026
2ffd87f
Add documentation and improve pedigree model functions
smasongarrison Jan 20, 2026
d3087c9
Improve vignette VignetteIndexEntry titles
smasongarrison Feb 5, 2026
bfa9223
Update vignettes: images, timestamps, Rmd tweak
smasongarrison Feb 5, 2026
58dcf49
add optimized branch
smasongarrison Feb 9, 2026
8546c4c
Optimize simulatePedigree parent selection with vectorized operations…
smasongarrison Feb 10, 2026
2330dcf
Update test-simulatePedigree.R
smasongarrison Feb 11, 2026
bb0c759
Add twinID support and mz_twins option
smasongarrison Feb 11, 2026
2c4b710
Change beta_match_base from TRUE to FALSE
smasongarrison Feb 11, 2026
bc9b50e
Add MZ twin support to adjacency matrix via twinID column (#116)
smasongarrison Feb 11, 2026
78e9a06
Add mz_method option and refine MZ merging
smasongarrison Feb 12, 2026
98fcc48
Update buildComponent.R
smasongarrison Feb 12, 2026
55eb4e7
Update buildComponent.R
smasongarrison Feb 12, 2026
53d8e5a
Add mz_method parameter to ped2add
smasongarrison Feb 12, 2026
91d573f
two twin methods
smasongarrison Feb 12, 2026
4d841f0
Compare MZ twin methods and add checks
smasongarrison Feb 13, 2026
8c0d792
docs
smasongarrison Feb 13, 2026
298095c
Update v5_ASOIAF.Rmd
smasongarrison Feb 13, 2026
12e40e7
Support 'addtwins' in MZ handling and add tests
smasongarrison Feb 13, 2026
71cca2d
Refactor MZ twin merging logic and tests
smasongarrison Feb 13, 2026
9d4d507
Fix twin matrix size mismatch between addtwins and merging methods (#…
smasongarrison Feb 13, 2026
2072a03
Restrict MZ twin restoration and add isTwin()
smasongarrison Feb 13, 2026
c2138ae
Update buildComponent.R
smasongarrison Feb 13, 2026
f33aed8
Claude/fix twin matrix sizes bn n ve (#120)
smasongarrison Feb 13, 2026
59e9fa8
Update test-buildComponent.R
smasongarrison Feb 13, 2026
619ae5e
Update optimizing.R
smasongarrison Feb 14, 2026
565876b
Merge branch 'dev_main' into mztwins
smasongarrison Feb 14, 2026
283e627
Update optimizing.R
smasongarrison Feb 14, 2026
38816e9
Improve benchmarking and add parent checks in tests
smasongarrison Feb 14, 2026
5ba1ad8
Improve benchmarking and add parent checks in tests
smasongarrison Feb 14, 2026
bc5d0f3
Improve benchmarking and add parent checks in tests
smasongarrison Feb 14, 2026
16ae62c
move to help twins
smasongarrison Feb 14, 2026
ed7f6c0
Add test flag and improve fuseTwins logic
smasongarrison Feb 14, 2026
380378c
Merge branch 'dev_main' into mztwins
smasongarrison Feb 14, 2026
5d21c9a
Update buildComponent.R
smasongarrison Feb 14, 2026
1ecd5c7
Update test-helpTwins.R
smasongarrison Feb 14, 2026
fdb44be
Improve MZ twin handling and fuseTwins
smasongarrison Feb 14, 2026
80f6d26
Update optimizing.R
smasongarrison Feb 14, 2026
cd677af
test some optimizations
smasongarrison Feb 14, 2026
9d45d96
Add beta flag & fuseTwins; optimize MZ handling
smasongarrison Feb 15, 2026
ff3fb1a
Reformat helpTwins, fuseTwins, buildComponent, tests
smasongarrison Feb 15, 2026
2f06155
Update buildComponent.R
smasongarrison Feb 15, 2026
ac9314e
Update buildComponent.R
smasongarrison Feb 15, 2026
ba12485
benchmarking
smasongarrison Feb 15, 2026
5022a69
clean up
smasongarrison Feb 15, 2026
51c5f50
fix
smasongarrison Feb 15, 2026
c43657d
smart fuse
smasongarrison Feb 15, 2026
9dccf8a
docs and concluding remarks for simualtions
smasongarrison Feb 15, 2026
db49f35
moving
smasongarrison Feb 15, 2026
09a435b
drop0 to top
smasongarrison Feb 15, 2026
10fcfc2
docs
smasongarrison Feb 15, 2026
da49f16
twinning
smasongarrison Feb 16, 2026
3b2e9d7
optimized speed
smasongarrison Feb 16, 2026
fcc6d9b
Update test-buildComponent.R
smasongarrison Feb 16, 2026
f3d477f
Update tweakPedigree.R
smasongarrison Feb 16, 2026
6ae78ef
Enable single-ID twin/mate auto-selection
smasongarrison Feb 16, 2026
cdf24b0
Enable single-ID twin/mate auto-selection
smasongarrison Feb 16, 2026
b1382ca
Merge branch 'mztwins' of https://github.com/R-Computing-Lab/BGmisc i…
smasongarrison Feb 16, 2026
aad8113
docs
smasongarrison Feb 16, 2026
f8e409c
Merge pull request #118 from R-Computing-Lab/mztwins
smasongarrison Feb 16, 2026
30fb7b3
Merge branch 'main' into pedigree_helpers
smasongarrison Feb 17, 2026
89bb348
Merge branch 'dev_main' into pedigree_helpers
smasongarrison Feb 17, 2026
a232402
Add OpenMx pedigree model builders and docs
smasongarrison Feb 17, 2026
096f398
clean up pedigree functions
smasongarrison Feb 17, 2026
fa990b5
Merge branch 'dev_main' into pedigree_helpers
smasongarrison Feb 17, 2026
988e762
Create unit tests
smasongarrison Feb 17, 2026
122bf8e
Update vignettes/v6_pedigree_model_fitting.Rmd
smasongarrison Feb 17, 2026
b814bc7
Remove HTML vignette build artifacts from version control (#124)
Copilot Feb 17, 2026
4ba25dd
Use requireNamespace for OpenMx checks
smasongarrison Feb 17, 2026
ec6dd3a
Add relatedness matrix parameters to fitPedigreeModel (#125)
Copilot Feb 17, 2026
b73c55d
fixed failing test
smasongarrison Feb 17, 2026
e37aaa7
Update NEWS.md
smasongarrison Feb 18, 2026
2b448e7
Merge pull request #123 from R-Computing-Lab/pedigree_helpers
smasongarrison Feb 18, 2026
537f60f
Merge branch 'dev_main' of https://github.com/R-Computing-Lab/BGmisc …
smasongarrison Feb 18, 2026
b395780
Apply suggestion from @Copilot
smasongarrison Feb 18, 2026
ccbd0a7
Update R/tweakPedigree.R
smasongarrison Feb 18, 2026
3262b16
Update v6_pedigree_model_fitting.Rmd
smasongarrison Feb 18, 2026
91eafd6
reknitting
smasongarrison Feb 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Suggests:
rmarkdown,
testthat (>= 3.0.0),
tidyverse,
mvtnorm,
withr
VignetteBuilder:
knitr
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

export(SimPed)
export(addPersonToPed)
export(buildFamilyGroups)
export(buildOneFamilyGroup)
export(buildPedigreeModelCovariance)
export(buildPedigreeMx)
export(buildTreeGrid)
export(calcAllGens)
export(calcFamilySize)
Expand All @@ -18,6 +22,7 @@ export(createGenDataFrame)
export(determineSex)
export(dropLink)
export(fitComponentModel)
export(fitPedigreeModel)
export(getWikiTreeSummary)
export(identifyComponentModel)
export(inferRelatedness)
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# BGmisc NEWS
# Development version: 1.6.0.9000
Add OpenMx pedigree model builders and docs
Added vignette for OpenMx pedigree model builders
Add option for MZ twins in the additive genetic matrix
Add option to select sex for MZ twin generation.
Add option to tweak pedigree with one id provided

# BGmisc 1.6.0.1
* Add helper functions for checkParents etc
* fixed incorrect direction so that parents are pointing to children in the graphs
* Optimize simulatePedigree and helpers for speed and memory usage
* Optimize simulatePedigree and helpers for speed and memory usage
* Major gains in speed for deeper pedigrees
* Added more tests for simulatePedigree
* Fix error when not enough single people available
Expand Down
123 changes: 120 additions & 3 deletions R/buildComponent.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#' @param isChild_method character. The method to use for computing the isChild matrix. Options are "classic" or "partialparent"
#' @param adjBeta_method numeric The method to use for computing the building the adjacency_method matrix when using the "beta" build
#' @param compress logical. If TRUE, use compression when saving the checkpoint files. Defaults to TRUE.
#' @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.
#' @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.
#' @param beta logical. Used for benchmarking
#' @param ... additional arguments to be passed to \code{\link{ped2com}}
#' @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.
#' @export
Expand All @@ -32,7 +35,7 @@ ped2com <- function(ped, component,
standardize_colnames = TRUE,
transpose_method = "tcrossprod",
adjacency_method = "direct",
isChild_method = "classic",
isChild_method = "partialparent",
saveable = FALSE,
resume = FALSE,
save_rate = 5,
Expand All @@ -42,6 +45,9 @@ ped2com <- function(ped, component,
save_path = "checkpoint/",
adjBeta_method = NULL,
compress = TRUE,
mz_twins = TRUE,
mz_method = "addtwins",
beta = FALSE,
...) {
#------
# Check inputs
Expand Down Expand Up @@ -121,12 +127,37 @@ ped2com <- function(ped, component,
ped <- standardizeColnames(ped, verbose = config$verbose)
}

mz_row_pairs <- NULL
mz_id_pairs <- NULL

if (mz_twins == TRUE && "twinID" %in% colnames(ped)) {
df_mz <- findMZtwins(ped,
verbose = config$verbose,
returnIDs = TRUE,
returnRows = TRUE,
returnAsList = TRUE,
beta = beta
)
mz_row_pairs <- df_mz$pair_rows
mz_id_pairs <- df_mz$pair_ids
}


# Load final result if computation was completed
if (config$resume == TRUE && file.exists(checkpoint_files$final_matrix)) {
if (config$verbose == TRUE) cat("Loading final computed matrix...\n")
return(readRDS(checkpoint_files$final_matrix))
}

if (mz_method %in% c("merging") && mz_twins == TRUE && !is.null(mz_row_pairs) && length(mz_row_pairs) > 0 &&
config$component %in% c("additive")) {
# 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.
ped <- fuseTwins(ped = ped, mz_row_pairs = mz_row_pairs, mz_id_pairs = mz_id_pairs, config = config, beta = beta)
if (config$verbose == TRUE) {
message("Merged ", length(mz_row_pairs), " MZ twin pair(s) in pedigree dataset for path tracing")
}
}


#------
# Algorithm
Expand All @@ -141,6 +172,7 @@ ped2com <- function(ped, component,
cat(paste0("Family Size = ", config$nr, "\n"))
}

#
# Step 1: Construct parent-child adjacency matrix

## A. Resume from Checkpoint if Needed
Expand Down Expand Up @@ -195,6 +227,8 @@ ped2com <- function(ped, component,
config = config,
compress = config$compress
)

# TODO merge twin columns
# --- Step 2: Compute Relatedness Matrix ---


Expand Down Expand Up @@ -226,7 +260,7 @@ ped2com <- function(ped, component,
# r is I + A + A^2 + ... = (I-A)^-1 from RAM
# could trim, here
## it keeps going until it explains all of the relatedness with themselves (i.e., mtSum == 0)
# some of this precision is articifuial because we literally get to the point that the condon is eaither there or not. probabiliticy
# some of this precision is artificial because we literally get to the point that the condon is eaither there or not. probabiliticy

# how much percision do we need to get unbiased estimates

Expand Down Expand Up @@ -281,6 +315,29 @@ ped2com <- function(ped, component,
compress = config$compress
)

if (mz_method == "addtwins" && mz_twins == TRUE && !is.null(mz_row_pairs) && length(mz_row_pairs) > 0) {
if (config$verbose == TRUE) {
message("MZ twin merging enabled: Will merge MZ twin columns in r2 before tcrossprod")
}

# --- Step 3b: Add ---
# MZ twins share the same genetic source. We absorb twin2's column into
# twin1's before tcrossprod so all path-traced relatedness flows through a
# single source. After tcrossprod we copy twin1's row/col back to twin2.
if (!is.null(mz_row_pairs) && length(mz_row_pairs) > 0 && config$component %in% c("additive")) {
# Extract all indices at once for batch operations
pairs_mat <- do.call(rbind, mz_row_pairs)
idx1_all <- pairs_mat[, 1]
idx2_all <- pairs_mat[, 2]
# Batch: absorb all twin2 columns into twin1 columns, then zero twin2
r2[, idx1_all] <- r2[, idx1_all, drop = FALSE] + r2[, idx2_all, drop = FALSE]
r2[, idx2_all] <- 0

if (config$verbose == TRUE) {
message("Added ", length(mz_row_pairs), " MZ twin pair column(s) in r2")
}
}
}
# --- Step 4: T crossproduct ---

if (config$resume == TRUE && file.exists(checkpoint_files$tcrossprod_checkpoint) &&
Expand All @@ -300,12 +357,68 @@ ped2com <- function(ped, component,
}
}

if (mz_method %in% c("merging", "addtwins") && mz_twins == TRUE && config$component %in% c("additive") && !is.null(mz_row_pairs) && length(mz_row_pairs) > 0) {
# --- Step 4b: Restore MZ twins ---
# Copy twin1's row/col to twin2 so both twins appear in the final matrix.
if (config$sparse == FALSE) {
r <- as.matrix(r)
rnames <- rownames(r)
ids_mat <- do.call(rbind, mz_id_pairs)
idx1_all <- match(ids_mat[, 1], rnames)
idx2_all <- match(ids_mat[, 2], rnames)
# Batch copy: twin1 rows/cols -> twin2 rows/cols
r[idx2_all, ] <- r[idx1_all, ]

r[, idx2_all] <- r[, idx1_all]
} else {
# 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?
# r <- df_add

rnames <- r@Dimnames[[1]]

ids_mat <- do.call(rbind, mz_id_pairs)
# needs to use sparse indexing to avoid coercion to dense
idx1_all <- match(ids_mat[, 1], rnames)
idx2_all <- match(ids_mat[, 2], rnames)

twin1_rows <- r[idx1_all, , drop = FALSE]
twin1_cols <- r[, idx1_all, drop = FALSE]
twin1_rows@Dimnames[[1]] <- rnames[idx2_all]
twin1_cols@Dimnames[[2]] <- rnames[idx2_all]
twin1_self <- r[idx1_all, idx1_all, drop = FALSE]
twin1_self@Dimnames[[1]] <- rnames[idx2_all]

r[idx2_all, ] <- twin1_rows
r[, idx2_all] <- twin1_cols
r[idx2_all, idx2_all] <- twin1_self

# Batch copy: twin1 rows/cols -> twin2 rows/cols

# Row/column replacement on a dsCMatrix (symmetric) causes Matrix to
# coerce to dgCMatrix (general), doubling stored entries. Convert back

r <- Matrix::drop0(r)

# so both mz_method paths return the same sparse class.
if (methods::is(r, "CsparseMatrix") && !methods::is(r, "symmetricMatrix")) {
r <- Matrix::forceSymmetric(r)
}
}
if (config$verbose == TRUE) {
message("Restored ", length(mz_row_pairs), " MZ twin pair(s) in relatedness matrix")
}
}


if (config$component %in% c("mitochondrial", "mtdna", "mitochondria")) {
r@x <- rep(1, length(r@x))
# 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 == FALSE && !methods::is(r, "matrix")) {
r <- as.matrix(r)
}
# flattens diagonal if you don't want to deal with inbreeding
Expand Down Expand Up @@ -335,6 +448,8 @@ ped2add <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
save_rate_parlist = 100000 * save_rate,
save_path = "checkpoint/",
compress = TRUE,
mz_twins = FALSE,
mz_method = "addtwins",
...) {
ped2com(
ped = ped,
Expand All @@ -353,6 +468,8 @@ ped2add <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
save_rate_parlist = save_rate_parlist,
save_path = save_path,
compress = compress,
mz_twins = mz_twins,
mz_method = mz_method,
...
)
}
Expand Down
Loading
Loading