Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
romagnolid committed Oct 11, 2022
1 parent 8bb271b commit 1157108
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Package: PAMES
Date: 2021-03-12
Date: 2021-12-01
Type: Package
Title: Purity Assessment from clonal MEthylation Sites
Description: Exploiting data from DNA methylation, this package provides the operations
required to evaluate the purity of tumor samples.
Version: 2.7.1
Version: 2.7.2
Authors@R: c(person("Dario", "Romagnoli", role=c("aut", "cre"),
email="[email protected]"),
person("Matteo", "Benelli", role="aut"),
Expand All @@ -24,5 +24,5 @@ Suggests:
License: GPL-3 | file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.2.0
VignetteBuilder: knitr
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# PAMES v2.7.2
- rename `median_of_regions`: `reduce_region`
- add `method` parameter to `reduce_to_regions`: allow choice between "median" (default) or mean
# PAMES v2.7.1
- fix `select_informative_regions_ext` with flag `return_info=TRUE`
# PAMES v2.7.0
Expand Down
20 changes: 13 additions & 7 deletions R/reduce_to_regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
#' @param min_CpGs An integer (default to 3). Minimum number of CpG sites
#' within a single genomic region required to compute the reduced beta value
#' (return NA otherwise).
#' @param method Take `median` or `mean`of CpG sites.
#' @return A matrix of beta values (nrow == length(cpg_indexes)).
#' @importFrom stats median
#' @export
#' @examples
#' reduced_data <- reduce_to_regions(bs_toy_matrix, bs_toy_sites, cpg_islands[1:10,])
reduce_to_regions <- function(beta_table, cpg_sites, cpg_regions, min_CpGs = 3){
reduce_to_regions <- function(beta_table, cpg_sites, cpg_regions, min_CpGs = 3, method=c("median", "mean")){

message(sprintf("[%s] # Reduce to regions #", Sys.time()))
# check parameters
method <- match.arg(method)
min_CpGs <- as.integer(min_CpGs)
assertthat::assert_that(min_CpGs > 0)
assertthat::assert_that(ncol(cpg_sites) >= 2)
Expand Down Expand Up @@ -63,7 +66,7 @@ reduce_to_regions <- function(beta_table, cpg_sites, cpg_regions, min_CpGs = 3){
reduced_data <- lapply(seq_along(idx_list[above_thr_regions]), function(i) {
utils::setTxtProgressBar(pb, i)
idx <- idx_list[[i]]
median_of_region(beta_table[idx,,drop = FALSE], min_CpGs)
summarise_region(beta_table[idx,,drop = FALSE], min_CpGs, method)
})
close(pb)

Expand All @@ -73,20 +76,23 @@ reduce_to_regions <- function(beta_table, cpg_sites, cpg_regions, min_CpGs = 3){
return(reduced_table)
}

#' Transform CpG sites to one CpG region
#' Reduce many CpG sites to one CpG region
#'
#' If the number of sites is sufficient take the median value else return NA.
#' If the number of sites is sufficient, take the median/mean value else return NA.
#' @param x A subset matrix.
#' @param n Minimum required number of sites per region (return NA otherwise).
#' @param method Either `median` or `mean`.
#' @return A vector
#' @keywords internal
median_of_region <- function(x, n) {
# remove sites non reported for all samples
summarise_region <- function(x, n, method) {
# remove fully NA sites
valid_sites <- which(rowSums(is.na(x)) != ncol(x))
x <- x[valid_sites,,drop=FALSE]
if (nrow(x) < n) {
return(rep(NA, ncol(x)))
} else {
} else if (method=="median"){
return(apply(x, 2, median, na.rm = TRUE))
} else if (method=="mean"){
return(apply(x, 2, mean, na.rm = TRUE))
}
}
20 changes: 0 additions & 20 deletions man/median_of_region.Rd

This file was deleted.

10 changes: 9 additions & 1 deletion man/reduce_to_regions.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-PAMES.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ context("CpG regions") ##################################################
test_that("median of regions", {
x <- rbind(rep(NA, 10), sample(100, 10), sample(100, 10))
x[,3] <- NA
expect_type(median_of_region(x, 2), "double")
expect_true(all(is.na(median_of_region(x, 3))))
expect_type(summarise_region(x, 2, "median"), "double")
expect_true(all(is.na(summarise_region(x, 3, "mean"))))
})
test_that("reduce_to_regions works", {
expect_error(reduce_to_regions(tumor_toy_data, illumina27k_hg38[3:4], cpg_islands), "No shared chromosomes")
Expand Down

0 comments on commit 1157108

Please sign in to comment.