Skip to content

Commit 2f52b31

Browse files
committed
fix: Update regex patterns in tests and functions to use perl argument for improved performance
1 parent cbb9c3d commit 2f52b31

13 files changed

Lines changed: 63 additions & 58 deletions

R/predict_selection_score.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ predict_selection_score <- function(index_df, data, genotypes) {
2020
if (!"ID" %in% names(index_df)) {
2121
stop("index_df must contain an ID column.")
2222
}
23-
b_cols <- grep("^b\\.", names(index_df), value = TRUE)
23+
b_cols <- grep("^b\\.", names(index_df), value = TRUE, perl = TRUE)
2424
if (length(b_cols) == 0) {
2525
stop("index_df must contain b.* columns with index coefficients.")
2626
}
@@ -62,7 +62,7 @@ predict_selection_score <- function(index_df, data, genotypes) {
6262

6363
trait_means <- mean_mat[, idx, drop = FALSE]
6464
score_mat[, j] <- rowSums(sweep(trait_means, 2, b_vals, "*"))
65-
score_names[j] <- paste0("I_", gsub("\\s+", "", gsub(",", "_", id_str)))
65+
score_names[j] <- paste0("I_", gsub("\\s+", "", gsub(",", "_", id_str, fixed = TRUE), perl = TRUE))
6666
}
6767

6868
score_names <- make.unique(score_names)

tests/testthat/test-constrained_genomic_indices.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ test_that("crlgsi summary format", {
502502
expect_equal(nrow(result$summary), 1)
503503

504504
# Check summary has correct columns
505-
expect_true(any(grepl("^b_y\\.", colnames(result$summary))))
506-
expect_true(any(grepl("^b_g\\.", colnames(result$summary))))
505+
expect_true(any(grepl("^b_y\\.", colnames(result$summary), perl = TRUE)))
506+
expect_true(any(grepl("^b_g\\.", colnames(result$summary), perl = TRUE)))
507507
expect_true("R" %in% colnames(result$summary))
508508
expect_true("GA" %in% colnames(result$summary))
509509
expect_true("rHI" %in% colnames(result$summary))
@@ -687,8 +687,8 @@ test_that("cppg_lgsi summary format", {
687687
expect_equal(nrow(result$summary), 1)
688688

689689
# Check summary has correct columns
690-
expect_true(any(grepl("^b_y\\.", colnames(result$summary))))
691-
expect_true(any(grepl("^b_g\\.", colnames(result$summary))))
690+
expect_true(any(grepl("^b_y\\.", colnames(result$summary), perl = TRUE)))
691+
expect_true(any(grepl("^b_g\\.", colnames(result$summary), perl = TRUE)))
692692
expect_true("R" %in% colnames(result$summary))
693693
expect_true("theta_CP" %in% colnames(result$summary))
694694
})

tests/testthat/test-constrained_indices.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ test_that("dg_lpsi can disable implied weights calculation", {
256256
expect_null(result$implied_weights_normalized)
257257

258258
# Summary should not contain implied weight columns
259-
expect_false(any(grepl("implied_w", colnames(result$summary))))
259+
expect_false(any(grepl("implied_w", colnames(result$summary), fixed = TRUE)))
260260
})
261261

262262
test_that("dg_lpsi feasibility check warns for unrealistic gains", {

tests/testthat/test-eigen_indices.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ test_that("print.resim reaches rep(FALSE,...) when restricted_traits is NULL (li
804804
# Print should run without error and reach the else branch at line 954
805805
out <- capture.output(print(r))
806806
# The Restricted column should be present and all FALSE
807-
expect_true(any(grepl("Restricted", out)) || is.character(out))
807+
expect_true(any(grepl("Restricted", out, fixed = TRUE)) || is.character(out))
808808
})
809809

810810
# --- print.ppg_esim: lines 1062-1066 – high CV → "[!]" branch ---------------
@@ -820,7 +820,7 @@ test_that("print.ppg_esim prints '[!]' message when CV >= 0.02 (lines 1062-1066)
820820
# CV will be high.
821821
r$desired_gains <- c(0.001, 1000, 0.001) # wildly non-proportional to Delta_G
822822
out <- capture.output(print(r))
823-
expect_true(any(grepl("\\[!\\]", out)),
823+
expect_true(any(grepl("[!]", out, fixed = TRUE)),
824824
info = "Expected '[!]' line in print output when CV >= 0.02"
825825
)
826826
})

tests/testthat/test-genomic_eigen_indices.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ test_that("mesim returns correct structure", {
7777
expect_true("summary" %in% names(result))
7878

7979
# Check dimensions
80+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
81+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
82+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
83+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
84+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
8085
expect_equal(length(result$b_y), data$n_traits)
8186
expect_equal(length(result$b_s), data$n_traits)
8287
expect_equal(length(result$b_combined), 2 * data$n_traits)
@@ -612,7 +617,7 @@ test_that("mesim generates Trait_ names when pmat has no colnames (line 283)", {
612617

613618
result <- mesim(pmat_no_names, gmat_no_names, S_M_no_names)
614619

615-
expect_true(all(grepl("^Trait_", result$trait_names)))
620+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
616621
})
617622

618623
# ==============================================================================
@@ -653,7 +658,7 @@ test_that("gesim errors when fewer than 2 traits are provided (line 476)", {
653658
test_that("gesim generates Trait_ names when pmat has no colnames (line 480)", {
654659
data <- setup_eigen_test_data()
655660
result <- gesim(unname(data$pmat), unname(data$gmat), unname(data$Gamma))
656-
expect_true(all(grepl("^Trait_", result$trait_names)))
661+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
657662
})
658663

659664
# Lines 533-534: implied_w tryCatch warning + NA in gesim
@@ -799,7 +804,7 @@ test_that("gw_esim generates Trait_ names when pmat has no colnames (line 684)",
799804
)
800805
M <- diag(n_markers)
801806
result <- gw_esim(unname(data$pmat), unname(data$gmat), G_M, M)
802-
expect_true(all(grepl("^Trait_", result$trait_names)))
807+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
803808
})
804809

805810
# ==============================================================================
@@ -877,7 +882,7 @@ test_that("rgesim generates Trait_ names when pmat has no colnames (line 874)",
877882
data <- setup_eigen_test_data()
878883
U_mat <- matrix(c(1, rep(0, data$n_traits - 1)), nrow = 1)
879884
result <- rgesim(unname(data$pmat), unname(data$gmat), unname(data$Gamma), U_mat)
880-
expect_true(all(grepl("^Trait_", result$trait_names)))
885+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
881886
})
882887

883888
# Lines 958-959: implied_w tryCatch warning + NA in rgesim
@@ -971,7 +976,7 @@ test_that("ppg_gesim generates Trait_ names when pmat has no colnames (line 1120
971976
data <- setup_eigen_test_data()
972977
d <- rep(1, data$n_traits)
973978
result <- ppg_gesim(unname(data$pmat), unname(data$gmat), unname(data$Gamma), d)
974-
expect_true(all(grepl("^Trait_", result$trait_names)))
979+
expect_true(all(grepl("^Trait_", result$trait_names, perl = TRUE)))
975980
})
976981

977982
# Lines 1221-1222: implied_w tryCatch warning + NA in ppg_gesim

tests/testthat/test-genomic_indices.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ test_that("Summary data frames have correct structure", {
420420
# CLGSI summary
421421
expect_s3_class(result_clgsi$summary, "data.frame")
422422
expect_equal(nrow(result_clgsi$summary), 1)
423-
expect_true(any(grepl("b_y", names(result_clgsi$summary))))
424-
expect_true(any(grepl("b_g", names(result_clgsi$summary))))
423+
expect_true(any(grepl("b_y", names(result_clgsi$summary), fixed = TRUE)))
424+
expect_true(any(grepl("b_g", names(result_clgsi$summary), fixed = TRUE)))
425425
expect_true("GA" %in% names(result_clgsi$summary))
426426
})
427427

tests/testthat/test-genomic_varcov.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ test_that("phenomic_genomic_varcov adds dimension names", {
189189

190190
expect_false(is.null(colnames(Phi)))
191191
expect_false(is.null(rownames(Phi)))
192-
expect_true(any(grepl("_phen", colnames(Phi))))
193-
expect_true(any(grepl("_gebv", colnames(Phi))))
192+
expect_true(any(grepl("_phen", colnames(Phi), fixed = TRUE)))
193+
expect_true(any(grepl("_gebv", colnames(Phi), fixed = TRUE)))
194194
})
195195

196196
test_that("phenomic_genomic_varcov errors without necessary inputs", {

tests/testthat/test-marker_indices.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ test_that("lmsi generates 'Trait1...' names when pmat has no colnames (line 307)
725725
pmat = unname(d$pmat), gmat = unname(d$gmat),
726726
G_s = G_s, wmat = d$weights
727727
)
728-
expect_true(all(grepl("^Trait", res$trait_names)))
728+
expect_true(all(grepl("^Trait", res$trait_names, perl = TRUE)))
729729
})
730730

731731
# --- lmsi: line 835 – PRE printed when GAY is given ----------------------
@@ -997,7 +997,7 @@ test_that("gw_lmsi generates 'Trait...' names when gmat has no colnames (line 75
997997
marker_mat <- matrix(sample(0:2, 40 * nm, replace = TRUE), 40, nm)
998998
trait_mat <- matrix(rnorm(40 * d$n_traits, 15, 3), 40, d$n_traits)
999999
res <- gw_lmsi(marker_mat, trait_mat, unname(d$gmat), wmat = d$weights)
1000-
expect_true(all(grepl("^Trait", res$trait_names)))
1000+
expect_true(all(grepl("^Trait", res$trait_names, perl = TRUE)))
10011001
})
10021002

10031003
# --- print.gw_lmsi: line 882 – HIGH-DIMENSIONAL print --------------------

tests/testthat/test-mean_performance.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ test_that("mean_performance computes SPD SEm, CD5, CD1, and GV (lines 166-211)",
522522
sem_vals <- suppressWarnings(as.numeric(sem_row[, c("trait1", "trait2")]))
523523

524524
# Remove significance annotations (like " NS" or "**") before conversion
525-
cd5_clean <- gsub("[a-zA-Z*\\s]", "", cd5_row[, c("trait1", "trait2")])
526-
cd1_clean <- gsub("[a-zA-Z*\\s]", "", cd1_row[, c("trait1", "trait2")])
525+
cd5_clean <- gsub("[a-zA-Z*\\s]", "", cd5_row[, c("trait1", "trait2")], perl = TRUE)
526+
cd1_clean <- gsub("[a-zA-Z*\\s]", "", cd1_row[, c("trait1", "trait2")], perl = TRUE)
527527

528528
cd5_vals <- suppressWarnings(as.numeric(cd5_clean))
529529
cd1_vals <- suppressWarnings(as.numeric(cd1_clean))

tests/testthat/test-multistage_genomic_indices.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ test_that("mppg_lgsi warns with custom U1 matrix", {
567567
d1 = seq_len(d$n1), d2 = seq_len(d$n), U1 = U1_custom
568568
)
569569
)
570-
expect_true(any(grepl("Custom U1", w_cap)))
570+
expect_true(any(grepl("Custom U1", w_cap, fixed = TRUE)))
571571
})
572572

573573
test_that("mppg_lgsi C* adjustment differs between Identity and custom U1", {
@@ -869,7 +869,7 @@ test_that("mppg_lgsi Young's method error handler falls back to manual intensiti
869869
"Young's method failed"
870870
),
871871
warning = function(w) {
872-
if (grepl("positive definite|C\\*", conditionMessage(w))) {
872+
if (grepl("positive definite|C\\*", conditionMessage(w), perl = TRUE)) {
873873
invokeRestart("muffleWarning")
874874
}
875875
}

0 commit comments

Comments
 (0)