Skip to content

Commit

Permalink
updated default VAR values to be more meaningful
Browse files Browse the repository at this point in the history
  • Loading branch information
jrboyd committed Dec 2, 2024
1 parent fa78405 commit 3dd7611
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 131 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Encoding: UTF-8
LazyData: false
Suggests:
BiocStyle,
curl,
knitr,
testthat,
rmarkdown
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export(subsetSamples)
export(subsetValues)
export(swapNameVariable)
export(transformSignal)
export(translateSSVtoCT2)
exportMethods("[")
exportMethods("colToRowMatCols<-")
exportMethods("rowRanges<-")
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v 0.1.49
* changed all default VAR names

# v 0.1.47
* depends on bugfix in seqsetvis in 1.25.5

Expand Down
8 changes: 4 additions & 4 deletions R/class_ChIPtsne.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
ChIPtsne2 = function(
rowToRowMat=matrix(0,0,0),
colToRowMatCols=list(),
name_VAR = "sample",
position_VAR = "x",
value_VAR = "y",
region_VAR = "id",
name_VAR = "name",
position_VAR = "position",
value_VAR = "signal",
region_VAR = "region",
fetch_config = FetchConfig.null(),
...)
{
Expand Down
28 changes: 13 additions & 15 deletions R/class_ChIPtsne_alt_constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#'
#' @param prof_dt Profile data.table, as returned from seqsetvis::ssvFetch* functions
#' @param query_gr The query GRanges object used to fetch prof_dt.
#' @param name_VAR Variable name that contains sample ids/names. Links prof_dt to meta_dt. Default is "sample".
#' @param position_VAR Variable name that contains positional information in prof_dt. Default is "x".
#' @param value_VAR Variable name that contains signal value information in prof_dt. Default is "y".
#' @param region_VAR Variable name that contains region ID information in prof_dt. Default is "id".
#' @param name_VAR Variable name that contains sample ids/names. Links prof_dt to meta_dt. Default is "name".
#' @param position_VAR Variable name that contains positional information in prof_dt. Default is "position".
#' @param value_VAR Variable name that contains signal value information in prof_dt. Default is "signal".
#' @param region_VAR Variable name that contains region ID information in prof_dt. Default is "region".
#' @param sample_metadata Metadata for entries in prof_dt's name_VAR, must include name_VAR
#' @param region_metadata Metadata to append to rowRanges, mcols of query_gr will also be used.
#' @param auto_sample_metadata If true, additional attributes in prof_dt will used for metadata (minus certain region related attributes such as seqnames, start, end, etc.)
Expand All @@ -30,10 +30,10 @@ ChIPtsne2.from_tidy = function(prof_dt,
query_gr,
sample_metadata = NULL,
region_metadata = NULL,
name_VAR = "sample",
position_VAR = "x",
value_VAR = "y",
region_VAR = "id",
name_VAR = "name",
position_VAR = "position",
value_VAR = "signal",
region_VAR = "region",
auto_sample_metadata = TRUE,
obj_history = list(),
fetch_config = FetchConfig.null(),
Expand Down Expand Up @@ -135,12 +135,10 @@ ChIPtsne2.from_tidy = function(prof_dt,
"end",
"width",
"strand",
"id",
"y",
"x",
"cluster_id",
position_VAR,
value_VAR)
region_VAR,
value_VAR,
position_VAR
)
sample_metadata = prof_dt %>%
# dplyr::select(all_of(c(name_VAR))) %>%
dplyr::select(!dplyr::any_of(c(drop_vars))) %>%
Expand Down Expand Up @@ -253,7 +251,7 @@ ChIPtsne2.from_FetchConfig = function(fetch_config,

fetch_res = runFetchAtRegions(fetch_config, query_gr, use_cache = use_cache)
prof_dt = fetch_res$prof_dt

prof_dt = translateSSVtoCT2(prof_dt)
ct2 = ChIPtsne2.from_tidy(prof_dt = prof_dt,
name_VAR = name_VAR,
query_gr = query_gr,
Expand Down
8 changes: 4 additions & 4 deletions R/class_ChIPtsne_no_rowRanges.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
ChIPtsne2_no_rowRanges = function(
rowToRowMat=matrix(0,0,0),
colToRowMatCols=list(),
name_VAR = "sample",
position_VAR = "x",
value_VAR = "y",
region_VAR = "id",
name_VAR = "name",
position_VAR = "position",
value_VAR = "signal",
region_VAR = "region",
fetch_config = FetchConfig.null(),
...)
{
Expand Down
42 changes: 39 additions & 3 deletions R/functions_examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ exampleQueryGR = function(){
exampleProfDT = function(){
CTCF_in_10a_profiles_dt = NULL
utils::data(list = "CTCF_in_10a_profiles_dt", package = "seqsetvis", overwrite = TRUE, envir = environment())
CTCF_in_10a_profiles_dt = translateSSVtoCT2(CTCF_in_10a_profiles_dt)
CTCF_in_10a_profiles_dt[]
}

Expand All @@ -56,7 +57,8 @@ exampleProfDT = function(){
#' exampleChIPtsne2()
exampleChIPtsne2 = function(){
query_gr = exampleQueryGR()
prof_dt = exampleProfDT()
prof_dt = exampleProfDT() %>%
translateSSVtoCT2

ChIPtsne2.from_tidy(prof_dt, query_gr)
}
Expand All @@ -73,9 +75,10 @@ exampleChIPtsne2.with_meta = function(){
query_gr = exampleQueryGR()
prof_dt = exampleProfDT()
meta_dt = prof_dt %>%
dplyr::select(sample) %>%
translateSSVtoCT2 %>%
dplyr::select(name) %>%
unique %>%
tidyr::separate(sample, c("cell", "mark"), sep = "_", remove = FALSE)
tidyr::separate(name, c("cell", "mark"), sep = "_", remove = FALSE)

ChIPtsne2.from_tidy(prof_dt, query_gr, sample_metadata = meta_dt)
}
Expand Down Expand Up @@ -169,3 +172,36 @@ exampleDataPaths = function(){
url_dt[, narrowPeak_file := paste0(cell, "_", mark, ".narrowPeak")]
url_dt[]
}

#' translateSSVtoCT2
#'
#' *seqsetvis* creates data.table with certain default variables that *chiptsne2* uses more user-friendly values for. This function checks for *seqsetvis* defaults and converts them to *chiptsne2*.
#'
#' @param prof_dt A data.table generated by *seqsetvis* from ssvFetchGRanges
#'
#' @return
#' @export
#'
#' @examples
#' prof_dt = exampleProfDT()
#' prof_dt = translateSSVtoCT2(prof_dt)
#' ct2 = ChIPtsne2.from_tidy(prof_dt, exampleQueryGR())
translateSSVtoCT2 = function(prof_dt){
#convert to data.table if needed
if(!is(prof_dt, "data.table")){
prof_dt = data.table::as.data.table(prof_dt)
}
if("id" %in% colnames(prof_dt)){
data.table::setnames(prof_dt, "id", "region")
}
if("x" %in% colnames(prof_dt)){
data.table::setnames(prof_dt, "x", "position")
}
if("sample" %in% colnames(prof_dt)){
data.table::setnames(prof_dt, "sample", "name")
}
if("y" %in% colnames(prof_dt)){
data.table::setnames(prof_dt, "y", "signal")
}
prof_dt[]
}
8 changes: 4 additions & 4 deletions man/ChIPtsne2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/ChIPtsne2.from_tidy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/ChIPtsne2_no_rowRanges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/translateSSVtoCT2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 22 additions & 16 deletions tests/testthat/test_ChIPtsne_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ library(testthat)
query_gr = exampleQueryGR()
prof_dt = exampleProfDT()

metadata = prof_dt %>% dplyr::select(sample) %>% unique
metadata = metadata %>% tidyr::separate(sample, c("cell", "mark"), sep = "_", remove = FALSE)
metadata = prof_dt %>% dplyr::select(name) %>% unique
metadata = metadata %>% tidyr::separate(name, c("cell", "mark"), sep = "_", remove = FALSE)

map_dt = prof_dt %>% dplyr::select(sample, x) %>% unique %>%
dplyr::mutate(cn = paste(sample, x, sep = "_")) %>%
dplyr::mutate(nr = seq_along(x))
map_list = split(map_dt$n, map_dt$sample)
map_dt = prof_dt %>% dplyr::select(name, position) %>% unique %>%
dplyr::mutate(cn = paste(name, position, sep = "_")) %>%
dplyr::mutate(nr = seq_along(position))

tmp_wide = tidyr::pivot_wider(prof_dt, names_from = c("sample", "x"), values_from = "y", id_cols = "id")
map_list = split(map_dt$nr, map_dt$name)

tmp_wide = tidyr::pivot_wider(prof_dt, names_from = c("name", "position"), values_from = "signal", id_cols = "region")
prof_mat = as.matrix(tmp_wide[, -1])
rownames(prof_mat) = tmp_wide$id
rownames(prof_mat) = tmp_wide$region


prof_max = prof_dt %>%
dplyr::group_by(id, sample) %>%
dplyr::summarise(y = max(y)) %>%
tidyr::pivot_wider(names_from = "sample", id_cols = "id", values_from = "y")
dplyr::group_by(region, name) %>%
dplyr::summarise(signal = max(signal)) %>%
tidyr::pivot_wider(names_from = "name", id_cols = "region", values_from = "signal")
prof_max_mat = as.matrix(prof_max[, -1])
rownames(prof_max_mat) = prof_max$id
rownames(prof_max_mat) = prof_max$region

ct = ChIPtsne2(assay = list(max = prof_max_mat[names(query_gr),]),
rowRanges = query_gr,
Expand All @@ -34,14 +35,18 @@ ct = ChIPtsne2(assay = list(max = prof_max_mat[names(query_gr),]),
metadata = list(time = date()))


prof_dt = seqsetvis::ssvSignalClustering(prof_dt, nclust = 4)
region_metadata = prof_dt %>% dplyr::select(id, cluster_id) %>% unique
clust_dt = seqsetvis::ssvSignalClustering(prof_dt, nclust = 4, facet_ = "name", row_ = "region", column_ = "position", fill_ = "signal")
# prof_dt = translateSSVtoCT2(prof_dt)
# clust_dt = translateSSVtoCT2(clust_dt)

region_metadata = clust_dt %>% dplyr::select(region, cluster_id) %>% unique


ct2 = ChIPtsne2.from_tidy(prof_dt, query_gr, region_metadata = region_metadata)

test_that("Constructors - valid", {
expect_true(validObject(ct2))
expect_true(validObject(.ChIPtsne2())) # internal
expect_true(validObject(chiptsne2:::.ChIPtsne2())) # internal
expect_true(validObject(ChIPtsne2())) # exported

se = as(ct2, "SummarizedExperiment")
Expand All @@ -56,7 +61,7 @@ test_that("Constructors - invalid", {
})

test_that("Gettters", {
expect_identical(rowToRowMat(ct2), prof_mat[levels(prof_dt$id),])
expect_identical(rowToRowMat(ct2), prof_mat[unique(prof_dt$region),])
expect_identical(rownames(rowToRowMat(ct2)), rownames(ct2))
expect_identical(names(colToRowMatCols(ct2)), colnames(ct2))
})
Expand All @@ -69,6 +74,7 @@ query_gr = exampleQueryGR()[1:10]
suppressWarnings({
ct2.cfg = ChIPtsne2.from_FetchConfig(fetch_config, query_gr)
})

test_that("Constructor FetchConfig", {
expect_setequal(rownames(rowToRowMat(ct2.cfg)), names(query_gr))
expect_equal(ncol(rowToRowMat(ct2.cfg)), 400)
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test_ChIPtsne_getsetters.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ library(testthat)
ct2 = exampleChIPtsne2.with_meta()

test_that("NameVariable", {
expect_equal(getNameVariable(ct2), "sample")
expect_equal(getNameVariable(ct2), "name")
ct2.new = setNameVariable(ct2, "Name")
expect_equal(getNameVariable(ct2.new), "Name")
})

test_that("RegionVariable", {
expect_equal(getRegionVariable(ct2), "id")
expect_equal(getRegionVariable(ct2), "region")
ct2.new = setRegionVariable(ct2, "Region")
expect_equal(getRegionVariable(ct2.new), "Region")
})

test_that("PositionVariable", {
expect_equal(getPositionVariable(ct2), "x")
expect_equal(getPositionVariable(ct2), "position")
ct2.new = setPositionVariable(ct2, "Position")
expect_equal(getPositionVariable(ct2.new), "Position")
})

test_that("ValueVariable", {
expect_equal(getValueVariable(ct2), "y")
expect_equal(getValueVariable(ct2), "signal")
ct2.new = setValueVariable(ct2, "Value")
expect_equal(getValueVariable(ct2.new), "Value")
})
Expand Down Expand Up @@ -79,7 +79,7 @@ test_that("Switch name variable to smaller set", {
ct2.10a = swapNameVariable(ct2.by_cell$MCF10A, new_VAR = "mark")
expect_equal(getNameVariable(ct2.10a), "mark")
expect_equal(getSampleMetaData(ct2.10a)$mark, factor("CTCF"))
expect_equal(getSampleMetaData(ct2.10a)$sample, "MCF10A_CTCF")
expect_equal(getSampleMetaData(ct2.10a)$name, "MCF10A_CTCF")
expect_equal(colnames(rowToRowMat(ct2.10a))[1], "CTCF_-325")
})

Expand All @@ -89,7 +89,7 @@ test_that("When names match for operator", {
ct2.diff = ct2.by_cell$MCF10A - ct2.by_cell$MCF10AT1
expect_equal(colnames(rowToRowMat(ct2.diff))[1], "CTCF_-325")
ct2.diff1 = swapNameVariable(ct2.diff, new_VAR = "cell")
ct2.diff2 = swapNameVariable(ct2.diff, new_VAR = "sample")
ct2.diff2 = swapNameVariable(ct2.diff, new_VAR = "name")
expect_equal(colnames(ct2.diff1), "MCF10A - MCF10AT1")
expect_equal(colnames(rowToRowMat(ct2.diff1))[1], "MCF10A - MCF10AT1_-325")
expect_equal(colnames(ct2.diff2), "MCF10A_CTCF - MCF10AT1_CTCF")
Expand All @@ -101,7 +101,7 @@ test_that("Switch name variable same set", {
expect_equal(getNameVariable(ct2.name_cell), "cell")
expect_equal(getSampleMetaData(ct2.name_cell)$mark, rep("CTCF", 3))
expect_equal(colnames(rowToRowMat(ct2.name_cell))[1], "MCF10A_-325")
expect_equal(getSampleMetaData(ct2.name_cell)$sample, c("MCF10A_CTCF", "MCF10AT1_CTCF", "MCF10CA1_CTCF"))
expect_equal(getSampleMetaData(ct2.name_cell)$name, c("MCF10A_CTCF", "MCF10AT1_CTCF", "MCF10CA1_CTCF"))
})


Loading

0 comments on commit 3dd7611

Please sign in to comment.