Skip to content

Commit

Permalink
v bump 0.1.52
Browse files Browse the repository at this point in the history
added normalizeSignalPerRegion
aggregation methods use better terminology
  • Loading branch information
jrboyd committed Dec 13, 2024
1 parent 36aca71 commit f82b4cd
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 20 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export(isFetchConfigNull)
export(mutateRegions)
export(mutateSamples)
export(normalizeSignalCapValue)
export(normalizeSignalPerRegion)
export(normalizeSignalRPM)
export(plotDimReduceBins)
export(plotDimReducePoints)
Expand Down Expand Up @@ -139,6 +140,7 @@ exportMethods(groupRegionsByValues)
exportMethods(groupRegionsManually)
exportMethods(lapply)
exportMethods(normalizeSignalCapValue)
exportMethods(normalizeSignalPerRegion)
exportMethods(normalizeSignalRPM)
exportMethods(plotDimReduceBins)
exportMethods(plotDimReducePoints)
Expand Down
66 changes: 61 additions & 5 deletions R/functions_normalize_signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ setMethod("normalizeSignalCapValue", c("ChIPtsne2_no_rowRanges"), .normalizeSign
args = get_args()
prof_dt = getTidyProfile(ct2)
cap_dt = seqsetvis::calc_norm_factors(prof_dt,
value_ = ct2@value_VAR,
cap_value_ = signal_cap_VAR,
by1 = ct2@region_VAR,
by2 = ct2@name_VAR,
aggFUN2 = function(x)stats::quantile(x, cap_quantile))
value_ = ct2@value_VAR,
cap_value_ = signal_cap_VAR,
by1 = ct2@region_VAR,
by2 = ct2@name_VAR,
aggFUN2 = function(x)stats::quantile(x, cap_quantile))

# remove signal_cap_VAR if present to overwrite
old_meta_dt = getSampleMetaData(ct2)
Expand Down Expand Up @@ -255,3 +255,59 @@ setGeneric("calculateSignalCapValue",
#' @export
#' @rdname ct2-calccap
setMethod("calculateSignalCapValue", c("ChIPtsne2_no_rowRanges"), .calculateSignalCapValue)

#### norm per region ####
.norm_per_region = function(ct2){
mat = ct2@rowToRowMat
rmax = apply(mat, 1, max)
# pheatmap::pheatmap(mat/rmax, cluster_cols = FALSE, cluster_rows = FALSE)
rowToRowMat(ct2) = mat/rmax
ct2
}

.normalizeSignalPerRegion = function(ct2, group_VARS = NULL){
if(!is.null(group_VARS)){
ct2.by_group = split(ct2, group_VARS)
ct2_norm.by_group = lapply(ct2.by_group, .norm_per_region)
ct2_norm = cbind(ct2_norm.by_group)
}else{
ct2_norm = .norm_per_region(ct2)
}
ct2_norm
}

#' normalizeSignalPerRegion
#'
#' Applies a normalization where the maximum value at each region is scaled to
#' 1. By default this is applied across all samples but you may supply one or
#' more variables from column/sample metadata to group them by.
#'
#' @param ct2 `r doc_ct2_nrr()`
#' @param group_VARS Attributes in colData that define groups to apply the
#' normalization to.
#'
#' @export
#' @return `r doc_ct2_nrr()` where values have been scaled per region where max
#' equals 1.
#'
#' @examples
#' ct2 = exampleChIPtsne2.with_meta()
#' # plotting raw values, it can be hard to assess differences between cells at
#' # regions with varying average enrichment.
#' plotSignalHeatmap(ct2)
#'
#' # with region normalizatoin, it's much clearer that MCF10CA1 has lower
#' # enrichment
#' plotSignalHeatmap(normalizeSignalPerRegion(ct2))
#'
#' # this data isn't complicated enough to warrant it, but normalization can be
#' # applied with sample groups
#' plotSignalHeatmap(normalizeSignalPerRegion(ct2, group_VARS = "cell"))
setGeneric("normalizeSignalPerRegion",
function(ct2, group_VARS = NULL)
standardGeneric("normalizeSignalPerRegion"),
signature = "ct2")

#' @export
#' @rdname ct2-calccap
setMethod("normalizeSignalPerRegion", c("ChIPtsne2_no_rowRanges"), .normalizeSignalPerRegion)
6 changes: 4 additions & 2 deletions man/aggregateByGroup.Rd

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

17 changes: 12 additions & 5 deletions man/aggregateRegionsByGroup.Rd

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

22 changes: 17 additions & 5 deletions man/aggregateSamplesByGroup.Rd

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

2 changes: 1 addition & 1 deletion man/calculateGroupCentroid.Rd

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

3 changes: 3 additions & 0 deletions man/ct2-calccap.Rd

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

39 changes: 39 additions & 0 deletions man/normalizeSignalPerRegion.Rd

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

5 changes: 3 additions & 2 deletions tests/testthat/test_centroid_and_aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("calculateGroupCentroid", {
})

test_that("valid ChIPtsne2_no_rowRanges by aggregate", {
ct2.nrr = aggregateRegionsByGroup(ct2, c("peak_MCF10A_CTCF", "peak_MCF10AT1_CTCF"))
ct2.nrr = aggregateRegionsByGroup(ct2, c("peak_MCF10A_CTCF", "peak_MCF10AT1_CTCF"), sep = ",")
rowData(ct2.nrr)
colData(ct2.nrr)
getRegionMetaData(ct2.nrr)
Expand All @@ -20,7 +20,7 @@ test_that("valid ChIPtsne2_no_rowRanges by aggregate", {
expect_equal(rownames(rowData(ct2.nrr)), c("FALSE,FALSE", "FALSE,TRUE", "TRUE,FALSE", "TRUE,TRUE"))
reg_meta = getRegionMetaData(ct2.nrr)

expect_equal(colnames(reg_meta), c("meta_id", "peak_MCF10A_CTCF", "peak_MCF10AT1_CTCF"))
expect_equal(colnames(reg_meta), c("grouped_regions", "peak_MCF10A_CTCF", "peak_MCF10AT1_CTCF"))
expect_equal(rownames(reg_meta), c("FALSE,FALSE", "FALSE,TRUE", "TRUE,FALSE", "TRUE,TRUE"))
})

Expand Down Expand Up @@ -72,3 +72,4 @@ test_that("aggregateByGroup variable names", {
expect_equal(nrow(getRegionMetaData(ct2.1col)), 100)
expect_equal(nrow(getRegionMetaData(ct2.1x1)), 1)
})

0 comments on commit f82b4cd

Please sign in to comment.