-
Notifications
You must be signed in to change notification settings - Fork 3
Enhance com2links to subset matrices and improve error handling #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c53c4ca
f0d3ab3
2865017
9db01b4
7aed335
8a80c8d
e4d7a6c
34a260d
8bb42fe
e67fb0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| #' @param dadID character. Name of the column in ped for the father ID variable | ||
| #' @param famID character. Name of the column to be created in ped for the family ID variable | ||
| #' @param twinID character. Name of the column in ped for the twin ID variable, if applicable | ||
| #' @param overwrite logical. If TRUE, will overwrite existing famID variable if it exists. Default is TRUE. | ||
| #' @param ... additional arguments to be passed to \code{\link{ped2com}} | ||
| #' @details | ||
| #' The general idea of this function is to use person ID, mother ID, and father ID to | ||
|
|
@@ -31,18 +32,20 @@ | |
| ped2fam <- function(ped, personID = "ID", | ||
| momID = "momID", dadID = "dadID", famID = "famID", | ||
| twinID = "twinID", | ||
| overwrite = TRUE, | ||
| ...) { | ||
|
Comment on lines
32
to
36
|
||
| # Call to wrapper function | ||
| .ped2id( | ||
| ped = ped, personID = personID, momID = momID, dadID = dadID, famID = famID, twinID = twinID, | ||
| type = "parents" | ||
| type = "parents", | ||
| overwrite = overwrite | ||
| ) | ||
| } | ||
|
|
||
| .ped2id <- function(ped, | ||
| personID = "ID", momID = "momID", dadID = "dadID", | ||
| famID = "famID", twinID = "twinID", | ||
| type, | ||
| type, overwrite = TRUE, | ||
| ...) { | ||
| # Turn pedigree into family | ||
| pg <- ped2graph( | ||
|
|
@@ -55,23 +58,44 @@ | |
|
|
||
| # Create famID data.frame | ||
| # Convert IDs to numeric, with warning if coercion collapses IDs | ||
|
|
||
| uniques <- suppressWarnings(unique(as.numeric(names(wcc$membership)))) | ||
| keep_string <- FALSE | ||
|
|
||
| if (length(uniques) == 1L && is.na(uniques)) { | ||
| warning("After converting IDs to numeric, all IDs became NA. This indicates ID coercion collapsed IDs. Please ensure IDs aren't character or factor variables.") | ||
|
|
||
| keep_string <- TRUE | ||
| } else if (length(uniques) < length(wcc$membership)) { | ||
| warning("After converting IDs to numeric, some IDs became NA. This indicates ID coercion collapsed some IDs. Please ensure IDs aren't character or factor variables.") | ||
smasongarrison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| keep_string <- TRUE | ||
| } | ||
| if(keep_string==TRUE) { | ||
|
Check notice on line 72 in R/segmentPedigree.R
|
||
| fam <- data.frame( | ||
| V1 = names(wcc$membership), | ||
| V2 = wcc$membership | ||
| ) | ||
| } else { | ||
| } else { | ||
| fam <- data.frame( | ||
| V1 = as.numeric(names(wcc$membership)), | ||
| V2 = wcc$membership | ||
| ) | ||
| } | ||
|
|
||
| names(fam) <- c(personID, famID) | ||
|
|
||
| if(famID %in% names(ped)) { | ||
| if(overwrite) { | ||
| overwrite_message <- "be overwritten." | ||
| ped[[famID]] <- NULL | ||
| } else { | ||
| overwrite_message <- "not be overwritten." | ||
| fam[[famID]] <- NULL | ||
| } | ||
|
|
||
| warning(sprintf("The famID variable '%s' already exists in the pedigree. The existing variable will %s", famID, overwrite_message)) | ||
|
|
||
| } | ||
|
|
||
| ped2 <- merge(fam, ped, | ||
| by = personID, all.x = FALSE, all.y = TRUE | ||
| ) | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subsetting larger matrices using
ad_ped_matrix[guide_ids, guide_ids]assumes the larger matrix has matching dimnames and contains allguide_ids. If dimnames are missing or the ID sets differ, this will fail with a low-level indexing error (or introduce NA rows/cols). Consider checking for non-NULL dimnames and verifyingall(guide_ids %in% dimnames(mat)[[1]])before subsetting, and stop with a clear message when IDs cannot be aligned.