|
| 1 | +test_that("clean_granges removes internal columns", { |
| 2 | + gr <- GenomicRanges::GRanges( |
| 3 | + seqnames = "chr1", |
| 4 | + ranges = IRanges::IRanges(start = c(100, 200), end = c(150, 250)) |
| 5 | + ) |
| 6 | + GenomicRanges::mcols(gr)$myCol <- c("A", "B") |
| 7 | + GenomicRanges::mcols(gr)$start <- c(100, 200) |
| 8 | + GenomicRanges::mcols(gr)$end <- c(150, 250) |
| 9 | + |
| 10 | + result <- echoannot:::clean_granges(gr) |
| 11 | + mc <- GenomicRanges::mcols(result) |
| 12 | + expect_true("myCol" %in% colnames(mc)) |
| 13 | + expect_false("start" %in% colnames(mc)) |
| 14 | + expect_false("end" %in% colnames(mc)) |
| 15 | +}) |
| 16 | + |
| 17 | +test_that("clean_granges preserves user columns", { |
| 18 | + gr <- GenomicRanges::GRanges( |
| 19 | + seqnames = "chr1", |
| 20 | + ranges = IRanges::IRanges(start = 100, end = 200) |
| 21 | + ) |
| 22 | + GenomicRanges::mcols(gr)$SNP <- "rs123" |
| 23 | + GenomicRanges::mcols(gr)$P <- 0.05 |
| 24 | + result <- echoannot:::clean_granges(gr) |
| 25 | + mc <- GenomicRanges::mcols(result) |
| 26 | + expect_true("SNP" %in% colnames(mc)) |
| 27 | + expect_true("P" %in% colnames(mc)) |
| 28 | +}) |
| 29 | + |
| 30 | +test_that("rbind_granges merges GRanges with different mcols", { |
| 31 | + gr1 <- GenomicRanges::GRanges( |
| 32 | + seqnames = "chr1", |
| 33 | + ranges = IRanges::IRanges(start = 100, end = 200) |
| 34 | + ) |
| 35 | + GenomicRanges::mcols(gr1)$shared <- "A" |
| 36 | + GenomicRanges::mcols(gr1)$only_gr1 <- "X" |
| 37 | + |
| 38 | + gr2 <- GenomicRanges::GRanges( |
| 39 | + seqnames = "chr1", |
| 40 | + ranges = IRanges::IRanges(start = 300, end = 400) |
| 41 | + ) |
| 42 | + GenomicRanges::mcols(gr2)$shared <- "B" |
| 43 | + GenomicRanges::mcols(gr2)$only_gr2 <- "Y" |
| 44 | + |
| 45 | + result <- echoannot:::rbind_granges(gr1, gr2) |
| 46 | + expect_true(methods::is(result, "GRanges")) |
| 47 | + expect_equal(length(result), 2) |
| 48 | + expect_true("shared" %in% colnames(GenomicRanges::mcols(result))) |
| 49 | + # Non-shared columns should be dropped |
| 50 | + expect_false("only_gr1" %in% colnames(GenomicRanges::mcols(result))) |
| 51 | + expect_false("only_gr2" %in% colnames(GenomicRanges::mcols(result))) |
| 52 | +}) |
| 53 | + |
| 54 | +test_that("rbind_granges with identical mcols preserves all", { |
| 55 | + gr1 <- GenomicRanges::GRanges( |
| 56 | + seqnames = "chr1", |
| 57 | + ranges = IRanges::IRanges(start = 100, end = 200) |
| 58 | + ) |
| 59 | + GenomicRanges::mcols(gr1)$col1 <- "A" |
| 60 | + GenomicRanges::mcols(gr1)$col2 <- 1 |
| 61 | + |
| 62 | + gr2 <- GenomicRanges::GRanges( |
| 63 | + seqnames = "chr2", |
| 64 | + ranges = IRanges::IRanges(start = 300, end = 400) |
| 65 | + ) |
| 66 | + GenomicRanges::mcols(gr2)$col1 <- "B" |
| 67 | + GenomicRanges::mcols(gr2)$col2 <- 2 |
| 68 | + |
| 69 | + result <- echoannot:::rbind_granges(gr1, gr2) |
| 70 | + expect_equal(length(result), 2) |
| 71 | + mc <- GenomicRanges::mcols(result) |
| 72 | + expect_true(all(c("col1", "col2") %in% colnames(mc))) |
| 73 | +}) |
| 74 | + |
| 75 | +test_that("check_grlist handles data.frame input", { |
| 76 | + dat <- as.data.frame(echodata::BST1[1:5, ]) |
| 77 | + result <- echoannot:::check_grlist(dat, style = "NCBI") |
| 78 | + expect_true(is.list(result)) |
| 79 | + expect_true(methods::is(result[[1]], "GRanges")) |
| 80 | +}) |
| 81 | + |
| 82 | +test_that("check_grlist handles GRanges input", { |
| 83 | + gr <- GenomicRanges::GRanges( |
| 84 | + seqnames = "1", |
| 85 | + ranges = IRanges::IRanges(start = 100, end = 200) |
| 86 | + ) |
| 87 | + result <- echoannot:::check_grlist(gr, style = "NCBI") |
| 88 | + expect_true(is.list(result)) |
| 89 | +}) |
| 90 | + |
| 91 | +test_that("check_grlist errors on invalid input", { |
| 92 | + expect_error( |
| 93 | + echoannot:::check_grlist("invalid"), |
| 94 | + "grlist must be" |
| 95 | + ) |
| 96 | +}) |
| 97 | + |
| 98 | +test_that("name_filter_convert filters by min_hits", { |
| 99 | + gr1 <- GenomicRanges::GRanges( |
| 100 | + seqnames = c("chr1", "chr1"), |
| 101 | + ranges = IRanges::IRanges(start = c(100, 200), end = c(150, 250)) |
| 102 | + ) |
| 103 | + gr2 <- GenomicRanges::GRanges( |
| 104 | + seqnames = character(0), |
| 105 | + ranges = IRanges::IRanges() |
| 106 | + ) |
| 107 | + grl <- list(has_hits = gr1, empty = gr2) |
| 108 | + result <- echoannot:::name_filter_convert(grl, min_hits = 1) |
| 109 | + expect_true(methods::is(result, "GRangesList")) |
| 110 | + expect_true(length(result) >= 1) |
| 111 | +}) |
| 112 | + |
| 113 | +test_that("name_filter_convert removes NULL entries", { |
| 114 | + gr1 <- GenomicRanges::GRanges( |
| 115 | + seqnames = "chr1", |
| 116 | + ranges = IRanges::IRanges(start = 100, end = 200) |
| 117 | + ) |
| 118 | + grl <- list(valid = gr1, null_entry = NULL) |
| 119 | + result <- echoannot:::name_filter_convert(grl, min_hits = 1) |
| 120 | + expect_true(methods::is(result, "GRangesList")) |
| 121 | +}) |
0 commit comments