|
| 1 | +# Tests for CCT (Cauchy Combination Test) function |
| 2 | +# Based on: https://weizhou0.github.io/SAIGE-QTL-doc/docs/gene_step3.html |
| 3 | + |
| 4 | +test_that("CCT returns correct p-value for simple input", { |
| 5 | + # Example from documentation |
| 6 | + pvalues <- c(2e-02, 4e-04, 0.2, 0.1, 0.8) |
| 7 | + result <- CCT(pvals = pvalues) |
| 8 | + |
| 9 | + expect_true(is.numeric(result)) |
| 10 | + expect_true(result >= 0 && result <= 1) |
| 11 | +}) |
| 12 | + |
| 13 | +test_that("CCT handles equal weights by default", { |
| 14 | + pvalues <- c(0.01, 0.05, 0.1) |
| 15 | + result <- CCT(pvals = pvalues) |
| 16 | + |
| 17 | + expect_true(is.numeric(result)) |
| 18 | + expect_length(result, 1) |
| 19 | +}) |
| 20 | + |
| 21 | +test_that("CCT handles custom weights", { |
| 22 | + pvalues <- c(0.01, 0.05, 0.1) |
| 23 | + weights <- c(0.5, 0.3, 0.2) |
| 24 | + result <- CCT(pvals = pvalues, weights = weights) |
| 25 | + |
| 26 | + expect_true(is.numeric(result)) |
| 27 | + expect_true(result >= 0 && result <= 1) |
| 28 | +}) |
| 29 | + |
| 30 | +test_that("CCT returns 0 when any p-value is 0", { |
| 31 | + pvalues <- c(0, 0.05, 0.1) |
| 32 | + result <- CCT(pvals = pvalues) |
| 33 | + |
| 34 | + expect_equal(result, 0) |
| 35 | +}) |
| 36 | + |
| 37 | +test_that("CCT handles p-values equal to 1", { |
| 38 | + pvalues <- c(1, 0.05, 0.1) |
| 39 | + result <- CCT(pvals = pvalues) |
| 40 | + |
| 41 | + expect_true(is.numeric(result)) |
| 42 | + expect_true(result >= 0 && result <= 1) |
| 43 | +}) |
| 44 | + |
| 45 | +test_that("CCT errors on NA values", { |
| 46 | + pvalues <- c(NA, 0.05, 0.1) |
| 47 | + |
| 48 | + expect_error(CCT(pvals = pvalues), "Cannot have NAs") |
| 49 | +}) |
| 50 | + |
| 51 | +test_that("CCT errors on invalid p-values", { |
| 52 | + # p-value > 1 |
| 53 | + expect_error(CCT(pvals = c(1.5, 0.05)), "between 0 and 1") |
| 54 | + |
| 55 | + # p-value < 0 |
| 56 | + expect_error(CCT(pvals = c(-0.1, 0.05)), "between 0 and 1") |
| 57 | +}) |
| 58 | + |
| 59 | +test_that("CCT errors on mismatched weights length", { |
| 60 | + pvalues <- c(0.01, 0.05, 0.1) |
| 61 | + weights <- c(0.5, 0.5) # wrong length |
| 62 | + |
| 63 | + expect_error(CCT(pvals = pvalues, weights = weights), "same as that of the p-values") |
| 64 | +}) |
| 65 | + |
| 66 | +test_that("CCT errors on negative weights", { |
| 67 | + pvalues <- c(0.01, 0.05, 0.1) |
| 68 | + weights <- c(-0.5, 0.3, 0.2) |
| 69 | + |
| 70 | + expect_error(CCT(pvals = pvalues, weights = weights), "positive") |
| 71 | +}) |
| 72 | + |
| 73 | +test_that("CCT handles very small p-values correctly", { |
| 74 | + pvalues <- c(1e-20, 1e-18, 0.1) |
| 75 | + result <- CCT(pvals = pvalues) |
| 76 | + |
| 77 | + expect_true(is.numeric(result)) |
| 78 | + expect_true(result >= 0 && result <= 1) |
| 79 | + expect_true(result < 0.01) # Should be very significant |
| 80 | +}) |
| 81 | + |
| 82 | +test_that("CCT handles single p-value", { |
| 83 | + result <- CCT(pvals = c(0.05)) |
| 84 | + |
| 85 | + expect_true(is.numeric(result)) |
| 86 | + expect_equal(result, 0.05, tolerance = 1e-10) |
| 87 | +}) |
0 commit comments