Skip to content

Commit ccdf6c8

Browse files
bschilderclaude
andcommitted
Add 10 test files for utility and annotation functions
Tests for GOSHIFTER utilities, GRanges helpers, NOTT2019 data, ROADMAP functions, XGR helpers, add_mb, data helpers, standardize_celltypes, utility functions, window_limits. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 66b53b6 commit ccdf6c8

10 files changed

Lines changed: 752 additions & 0 deletions
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
test_that("GOSHIFTER_find_folder errors on nonexistent path", {
2+
expect_error(
3+
echoannot:::GOSHIFTER_find_folder(goshifter = "/nonexistent/path"),
4+
"does not exist"
5+
)
6+
})
7+
8+
test_that("GOSHIFTER_find_folder works with existing directory", {
9+
tmp <- tempdir()
10+
result <- echoannot:::GOSHIFTER_find_folder(goshifter = tmp)
11+
expect_equal(result, tmp)
12+
})
13+
14+
test_that("GOSHIFTER_search_ROADMAP returns matching entries", {
15+
ref_path <- system.file(
16+
"extdata/ROADMAP",
17+
"ROADMAP_Epigenomic.js",
18+
package = "echoannot"
19+
)
20+
result <- echoannot:::GOSHIFTER_search_ROADMAP(
21+
Roadmap_reference = ref_path,
22+
fuzzy_search = "brain",
23+
verbose = FALSE
24+
)
25+
expect_true(data.table::is.data.table(result))
26+
expect_true(nrow(result) > 0)
27+
expect_true("EID" %in% colnames(result))
28+
})
29+
30+
test_that("GOSHIFTER_search_ROADMAP errors when no matches", {
31+
ref_path <- system.file(
32+
"extdata/ROADMAP",
33+
"ROADMAP_Epigenomic.js",
34+
package = "echoannot"
35+
)
36+
expect_error(
37+
echoannot:::GOSHIFTER_search_ROADMAP(
38+
Roadmap_reference = ref_path,
39+
fuzzy_search = "zzz_nonexistent_tissue_xyz",
40+
verbose = FALSE
41+
),
42+
"No annotation BED files found"
43+
)
44+
})
45+
46+
test_that("GOSHIFTER_bed_names generates file names from reference", {
47+
ref <- data.table::data.table(EID = c("E001", "E002", "E003"))
48+
result <- echoannot:::GOSHIFTER_bed_names(RM_ref = ref)
49+
expect_length(result, 3)
50+
expect_true(all(grepl("^E00[123]_15_coreMarks_mnemonics\\.bed\\.gz$", result)))
51+
})
52+
53+
test_that("GOSHIFTER_bed_names with custom suffix", {
54+
ref <- data.table::data.table(EID = c("E001"))
55+
result <- echoannot:::GOSHIFTER_bed_names(
56+
RM_ref = ref,
57+
suffix = "_custom.bed.gz"
58+
)
59+
expect_equal(result, "E001_custom.bed.gz")
60+
})
61+
62+
test_that("GOSHIFTER_bed_names deduplicates EIDs", {
63+
ref <- data.table::data.table(EID = c("E001", "E001", "E002"))
64+
result <- echoannot:::GOSHIFTER_bed_names(RM_ref = ref)
65+
expect_length(result, 2)
66+
})
67+
68+
test_that("GOSHIFTER_list_chromatin_states returns data.table", {
69+
result <- echoannot:::GOSHIFTER_list_chromatin_states()
70+
expect_true(data.table::is.data.table(result))
71+
expect_true(nrow(result) > 0)
72+
expect_true("MNEMONIC" %in% colnames(result) ||
73+
"STATE" %in% colnames(result) ||
74+
ncol(result) > 0)
75+
})
76+
77+
test_that("GOSHIFTER_list_chromatin_states matches ROADMAP_chromatin_states", {
78+
goshifter_result <- echoannot:::GOSHIFTER_list_chromatin_states()
79+
roadmap_result <- echoannot:::ROADMAP_chromatin_states()
80+
expect_equal(nrow(goshifter_result), nrow(roadmap_result))
81+
})
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
test_that("NOTT2019_bigwig_metadata loads correctly", {
2+
data("NOTT2019_bigwig_metadata", package = "echoannot")
3+
expect_true(data.table::is.data.table(NOTT2019_bigwig_metadata))
4+
expect_true(nrow(NOTT2019_bigwig_metadata) > 0)
5+
expect_true("name" %in% colnames(NOTT2019_bigwig_metadata) ||
6+
"data_link" %in% colnames(NOTT2019_bigwig_metadata))
7+
})
8+
9+
test_that("NOTT2019_marker_key covers expected markers", {
10+
mk <- echoannot:::NOTT2019_marker_key()
11+
expect_true(all(c("PU1", "Olig2", "NeuN", "LHX2") %in% names(mk)))
12+
})
13+
14+
test_that("NOTT2019_get_promoter_celltypes returns cell type string", {
15+
## Create mock data matching expected structure
16+
annot_sub <- data.frame(
17+
PU1_active_promoter = c(0, 1, 1),
18+
NeuN_active_promoter = c(1, 0, 0),
19+
Olig2_active_promoter = c(0, 0, 0),
20+
LHX2_active_promoter = c(0, 0, 0)
21+
)
22+
marker_key <- echoannot:::NOTT2019_marker_key()
23+
24+
result <- echoannot:::NOTT2019_get_promoter_celltypes(
25+
annot_sub = annot_sub,
26+
marker_key = marker_key,
27+
verbose = FALSE
28+
)
29+
expect_true(is.character(result))
30+
expect_true(nchar(result) > 0)
31+
expect_true(grepl("microglia", result))
32+
expect_true(grepl("neurons", result))
33+
})
34+
35+
test_that("NOTT2019_get_promoter_celltypes empty when no active promoters", {
36+
annot_sub <- data.frame(
37+
PU1_active_promoter = c(0, 0),
38+
NeuN_active_promoter = c(0, 0),
39+
Olig2_active_promoter = c(0, 0),
40+
LHX2_active_promoter = c(0, 0)
41+
)
42+
marker_key <- echoannot:::NOTT2019_marker_key()
43+
44+
result <- echoannot:::NOTT2019_get_promoter_celltypes(
45+
annot_sub = annot_sub,
46+
marker_key = marker_key,
47+
verbose = FALSE
48+
)
49+
expect_true(is.character(result))
50+
expect_equal(result, "")
51+
})
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
test_that("ROADMAP_chromatin_states returns data.table", {
2+
d <- echoannot:::ROADMAP_chromatin_states()
3+
expect_true(data.table::is.data.table(d))
4+
expect_true("MNEMONIC" %in% colnames(d))
5+
expect_true("DESCRIPTION" %in% colnames(d))
6+
expect_true(nrow(d) > 0)
7+
})
8+
9+
test_that("ROADMAP_chromatin_states as_dict returns named character", {
10+
d <- echoannot:::ROADMAP_chromatin_states(as_dict = TRUE)
11+
expect_true(is.character(d))
12+
expect_true(length(d) > 0)
13+
expect_true("Enh" %in% names(d))
14+
expect_equal(unname(d["Enh"]), "Enhancer")
15+
expect_equal(unname(d["EnhG"]), "Genic enhancer")
16+
expect_equal(unname(d["TssAFlnk"]), "Active flanking TSS")
17+
expect_equal(unname(d["BivFlnk"]), "Bivalent/Poised flanking TSS")
18+
})
19+
20+
test_that("ROADMAP_construct_reference loads reference data", {
21+
ref <- echoannot::ROADMAP_construct_reference(verbose = FALSE)
22+
expect_true(data.table::is.data.table(ref))
23+
expect_true("EID" %in% colnames(ref))
24+
expect_true(nrow(ref) > 0)
25+
})
26+
27+
test_that("ROADMAP_construct_reference keyword_query filters", {
28+
ref_all <- echoannot::ROADMAP_construct_reference(verbose = FALSE)
29+
ref_sub <- echoannot::ROADMAP_construct_reference(
30+
keyword_query = "brain",
31+
verbose = FALSE
32+
)
33+
expect_true(nrow(ref_sub) > 0)
34+
expect_true(nrow(ref_sub) < nrow(ref_all))
35+
})
36+
37+
test_that("ROADMAP_construct_reference limit_files works", {
38+
ref <- echoannot::ROADMAP_construct_reference(
39+
limit_files = 3,
40+
verbose = FALSE
41+
)
42+
expect_equal(nrow(ref), 3)
43+
})
44+
45+
test_that("ROADMAP_construct_reference keyword + limit combined", {
46+
ref <- echoannot::ROADMAP_construct_reference(
47+
keyword_query = "brain",
48+
limit_files = 2,
49+
verbose = FALSE
50+
)
51+
expect_equal(nrow(ref), 2)
52+
})

tests/testthat/test-XGR_helpers.R

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
test_that("XGR_sep_handler returns correct separators", {
2+
expect_equal(
3+
echoannot:::XGR_sep_handler("ENCODE_TFBS_ClusteredV3_CellTypes"),
4+
"[.]"
5+
)
6+
expect_equal(
7+
echoannot:::XGR_sep_handler("ENCODE_DNaseI_ClusteredV3_CellTypes"),
8+
"_(?=[^_]+$)"
9+
)
10+
expect_equal(
11+
echoannot:::XGR_sep_handler("Broad_Histone"),
12+
"_(?=[^_]+$)"
13+
)
14+
expect_equal(
15+
echoannot:::XGR_sep_handler("FANTOM5_Enhancer"),
16+
"_(?=[^_]+$)"
17+
)
18+
expect_equal(
19+
echoannot:::XGR_sep_handler("TFBS_Conserved"),
20+
"[$]"
21+
)
22+
})
23+
24+
test_that("XGR_sep_handler default for unknown library", {
25+
expect_equal(
26+
echoannot:::XGR_sep_handler("SomeUnknownLib"),
27+
"_(?=[^_]+$)"
28+
)
29+
})
30+
31+
test_that("XGR_filter_assays works with xgr_example", {
32+
data("xgr_example", package = "echoannot")
33+
result <- echoannot::XGR_filter_assays(
34+
gr.lib = xgr_example,
35+
n_top_assays = 1
36+
)
37+
expect_true(methods::is(result, "GRanges"))
38+
expect_true(length(result) > 0)
39+
expect_true(length(result) <= length(xgr_example))
40+
assays_remaining <- unique(result$Assay)
41+
expect_true(length(assays_remaining) >= 1)
42+
})
43+
44+
test_that("XGR_filter_assays with NULL keeps all", {
45+
data("xgr_example", package = "echoannot")
46+
result <- echoannot::XGR_filter_assays(
47+
gr.lib = xgr_example,
48+
n_top_assays = NULL
49+
)
50+
expect_equal(length(result), length(xgr_example))
51+
})
52+
53+
test_that("XGR_filter_sources works with xgr_example", {
54+
data("xgr_example", package = "echoannot")
55+
result <- echoannot::XGR_filter_sources(
56+
gr.lib = xgr_example,
57+
n_top_sources = 1
58+
)
59+
expect_true(methods::is(result, "GRanges"))
60+
expect_true(length(result) > 0)
61+
expect_true(length(result) <= length(xgr_example))
62+
})
63+
64+
test_that("XGR_filter_sources with NULL keeps all", {
65+
data("xgr_example", package = "echoannot")
66+
result <- echoannot::XGR_filter_sources(
67+
gr.lib = xgr_example,
68+
n_top_sources = NULL
69+
)
70+
expect_equal(length(result), length(xgr_example))
71+
})
72+
73+
test_that("XGR_parse_metadata parses fullname into Source and Assay", {
74+
data("xgr_example", package = "echoannot")
75+
## Create a small GRanges with a fullname column
76+
gr <- xgr_example[1:5]
77+
GenomicRanges::mcols(gr) <- data.frame(
78+
fullname = paste0("CellA_AssayX"),
79+
stringsAsFactors = FALSE
80+
)
81+
result <- echoannot:::XGR_parse_metadata(
82+
gr.lib = gr,
83+
lib.name = "ENCODE_DNaseI_ClusteredV3_CellTypes"
84+
)
85+
expect_true("Source" %in% colnames(GenomicRanges::mcols(result)))
86+
expect_true("Assay" %in% colnames(GenomicRanges::mcols(result)))
87+
})

0 commit comments

Comments
 (0)