Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix motifs and related functionality #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ Collate:
'workflow.R'
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.1.1
RoxygenNote: 7.3.2
8 changes: 4 additions & 4 deletions R/DsATAC-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ setMethod("regionAggregation",
}
# DelayedArray
if (.object@diskDump){
rsFun <- BiocGenerics::rowSums
rsFun <- DelayedArray::rowSums
}

#sort the regions
Expand Down Expand Up @@ -837,7 +837,7 @@ setMethod("mergeSamples",
if(countAggrFun=="sum"){
mergeFun <- function(X){rowSums(X, na.rm=TRUE)}
if (.object@diskDump) {
mergeFun <- function(X){BiocGenerics::rowSums(X, na.rm=TRUE)}
mergeFun <- function(X){DelayedArray::rowSums(X, na.rm=TRUE)}
} else if (.object@sparseCounts) {
mergeFun <- function(X){Matrix::rowSums(X, na.rm=TRUE)}
}
Expand Down Expand Up @@ -1785,8 +1785,8 @@ setMethod("transformCounts",
}
# DelayedArray
if (.object@diskDump){
rsFun <- BiocGenerics::rowSums
csFun <- BiocGenerics::colSums
rsFun <- DelayedArray::rowSums
csFun <- DelayedArray::colSums
}

if (method == "quantile"){
Expand Down
30 changes: 15 additions & 15 deletions R/DsAcc-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,36 @@ DsAcc <- function(siteCoord, sampleAnnot, genome, diskDump=FALSE){
################################################################################
# Getters
################################################################################
# Ensure that the generic function is defined
if (!isGeneric("getSamples")) {
setGeneric(
"getSamples",
function(.object) standardGeneric("getSamples"),
signature=c(".object")
)
setGeneric(
"getSamples",
function(.object) standardGeneric("getSamples"),
signature = c(".object")
)
}

#' getSamples-methods
#'
#' Return sample IDs in a dataset
#'
#' @param .object \code{\linkS4class{DsAcc}} object
#' @return Character vector of sample IDs in the dataset
#'
#' @description Returns the sample IDs in the dataset
#' @rdname getSamples-DsAcc-method
#' @docType methods
#' @aliases getSamples
#' @aliases getSamples,DsAcc-method
#' @author Fabian Mueller
#' @export
setMethod("getSamples",
signature(
.object="DsAcc"
),
function(
.object
) {
return(rownames(.object@sampleAnnot))
}
signature(
.object = "DsAcc"
),
function(.object) {
return(rownames(.object@sampleAnnot))
}
)

#-------------------------------------------------------------------------------
#' Retrieve the number of samples contained in a DsAcc object
#'
Expand Down
192 changes: 102 additions & 90 deletions R/utils_motifs.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,114 @@
################################################################################

################################################################################
# motifmatchr helpers
# motifmatchr helpers
################################################################################
#' prepareMotifmatchr
#'
#' prepare objects for a \code{motifmatchr} analysis
#' Prepare objects for a \code{motifmatchr} analysis
#'
#' @param genome character string specifying genome assembly
#' @param motifs either a character string (currently only "jaspar" and sets contained in \code{chromVARmotifs} ("homer", "encode", "cisbp") are supported) or an object containing PWMs
#' that can be used by \code{motifmatchr::matchMotifs} (such as an \code{PFMatrixList} or \code{PWMatrixList} object)
#' @param motifs either a character string (currently "jaspar2018", "jaspar2020", "jaspar_vert" and sets contained in \code{chromVARmotifs} ("homer", "encode", "cisbp") are supported)
#' or an object containing PWMs that can be used by \code{motifmatchr::matchMotifs} (such as a \code{PFMatrixList} or \code{PWMatrixList} object)
#' @return a list containing objects to be used as arguments for \code{motifmatchr}
#' @author Fabian Mueller
#' @export
prepareMotifmatchr <- function(genome, motifs){
res <- list()

# get the species name and the genome sequence object based on the object
genomeObj <- genome
if (!is.element("BSgenome", class(genomeObj))){
genomeObj <- getGenomeObject(genome)
}
spec <- organism(genomeObj)

# get the motif PWMs
motifL <- TFBSTools::PWMatrixList()
if (is.character(motifs)){
if (is.element("jaspar", motifs)){
# copied code from chromVAR, but updated the JASPAR version
opts <- list()
opts["species"] <- spec
opts["collection"] <- "CORE"
# gets the non-redundant set by default
mlCur <- TFBSTools::getMatrixSet(JASPAR2018::JASPAR2018, opts)
if (!isTRUE(all.equal(TFBSTools::name(mlCur), names(mlCur)))){
names(mlCur) <- paste(names(mlCur), TFBSTools::name(mlCur), sep = "_")
}
motifL <- c(motifL, TFBSTools::toPWM(mlCur))
}
if (is.element("jaspar_vert", motifs)){
# JASPER for all vertebrate TFBS
opts <- list()
opts["tax_group"] <- "vertebrates"
opts["collection"] <- "CORE"
# gets the non-redundant set by default
mlCur <- TFBSTools::getMatrixSet(JASPAR2018::JASPAR2018, opts)
if (!isTRUE(all.equal(TFBSTools::name(mlCur), names(mlCur)))){
names(mlCur) <- paste(names(mlCur), TFBSTools::name(mlCur), sep = "_")
}
motifL <- c(motifL, TFBSTools::toPWM(mlCur))
}
if (is.element("jaspar2016", motifs)){
motifL <- c(motifL, TFBSTools::toPWM(chromVAR::getJasparMotifs(species=spec)))
}
if (is.element("homer", motifs)){
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
data("homer_pwms")
motifL <- c(motifL, chromVARmotifs::homer_pwms)
}
if (is.element("encode", motifs)){
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
data("encode_pwms")
motifL <- c(motifL, chromVARmotifs::encode_pwms)
}
if (is.element("cisbp", motifs)){
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
if (spec == "Mus musculus"){
data("mouse_pwms_v1")
motifL <- c(motifL, chromVARmotifs::mouse_pwms_v1)
} else if (spec == "Homo sapiens"){
data("human_pwms_v1")
motifL <- c(motifL, chromVARmotifs::human_pwms_v1)
} else {
logger.warning(c("Could not find cisBP annotation for species", spec))
}
}
if (is.element("cisbp_v2", motifs)){
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
if (spec == "Mus musculus"){
data("mouse_pwms_v2")
motifL <- c(motifL, chromVARmotifs::mouse_pwms_v2)
} else if (spec == "Homo sapiens"){
data("human_pwms_v2")
motifL <- c(motifL, chromVARmotifs::human_pwms_v2)
} else {
logger.warning(c("Could not find cisBP annotation for species", spec))
}
}
if (length(motifL) < 1) {
logger.error(c("No motifs were loaded. Unsupported motifs (?) :", motifs))
}
} else if (is.element("PWMatrixList", class(motifs)) || is.element("PFMatrixList", class(motifs))) {
motifL <- motifs
} else {
logger.error(c("unsupported value for motifs:", motifs))
}
res[["genome"]] <- genomeObj
res[["motifs"]] <- motifL
return(res)
prepareMotifmatchr <- function(genome, motifs) {
res <- list()

# get the species name and the genome sequence object based on the object
genomeObj <- genome
if (!is.element("BSgenome", class(genomeObj))) {
genomeObj <- getGenomeObject(genome)
}
spec <- provider(genomeObj) # Extract the provider (e.g., "Homo sapiens" or "Mus musculus")

# get the motif PWMs
motifL <- TFBSTools::PWMatrixList()
if (is.character(motifs)) {
if (is.element("jaspar2020", motifs)) {
# Added block for JASPAR2020
opts <- list()
opts["species"] <- 9606
opts["collection"] <- "CORE"
# gets the non-redundant set by default
mlCur <- TFBSTools::getMatrixSet(JASPAR2020::JASPAR2020, opts)
if (!isTRUE(all.equal(TFBSTools::name(mlCur), names(mlCur)))) {
names(mlCur) <- paste(names(mlCur), TFBSTools::name(mlCur), sep = "_")
}
motifL <- c(motifL, TFBSTools::toPWM(mlCur))
}
if (is.element("jaspar2018", motifs)) {
# copied code from chromVAR, but updated the JASPAR version
opts <- list()
opts["species"] <- spec
opts["collection"] <- "CORE"
# gets the non-redundant set by default
mlCur <- TFBSTools::getMatrixSet(JASPAR2018::JASPAR2018, opts)
if (!isTRUE(all.equal(TFBSTools::name(mlCur), names(mlCur)))) {
names(mlCur) <- paste(names(mlCur), TFBSTools::name(mlCur), sep = "_")
}
motifL <- c(motifL, TFBSTools::toPWM(mlCur))
}
if (is.element("jaspar_vert", motifs)) {
# JASPAR for all vertebrate TFBS
opts <- list()
opts["tax_group"] <- "vertebrates"
opts["collection"] <- "CORE"
# gets the non-redundant set by default
mlCur <- TFBSTools::getMatrixSet(JASPAR2018::JASPAR2018, opts)
if (!isTRUE(all.equal(TFBSTools::name(mlCur), names(mlCur)))) {
names(mlCur) <- paste(names(mlCur), TFBSTools::name(mlCur), sep = "_")
}
motifL <- c(motifL, TFBSTools::toPWM(mlCur))
}
if (is.element("jaspar2016", motifs)) {
motifL <- c(motifL, TFBSTools::toPWM(chromVAR::getJasparMotifs(species = spec)))
}
if (is.element("homer", motifs)) {
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
data("homer_pwms")
motifL <- c(motifL, chromVARmotifs::homer_pwms)
}
if (is.element("encode", motifs)) {
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
data("encode_pwms")
motifL <- c(motifL, chromVARmotifs::encode_pwms)
}
if (is.element("cisbp", motifs)) {
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
if (spec == "Mus musculus") {
data("mouse_pwms_v1")
motifL <- c(motifL, chromVARmotifs::mouse_pwms_v1)
} else if (spec == "Homo sapiens") {
data("human_pwms_v1")
motifL <- c(motifL, chromVARmotifs::human_pwms_v1)
} else {
logger.warning(c("Could not find cisBP annotation for species", spec))
}
}
if (is.element("cisbp_v2", motifs)) {
if (!requireNamespace("chromVARmotifs")) logger.error(c("Could not load dependency: chromVARmotifs"))
if (spec == "Mus musculus") {
data("mouse_pwms_v2")
motifL <- c(motifL, chromVARmotifs::mouse_pwms_v2)
} else if (spec == "Homo sapiens") {
data("human_pwms_v2")
motifL <- c(motifL, chromVARmotifs::human_pwms_v2)
} else {
logger.warning(c("Could not find cisBP annotation for species", spec))
}
}
if (length(motifL) < 1) {
logger.error(c("No motifs were loaded. Unsupported motifs (?) :", motifs))
}
} else if (is.element("PWMatrixList", class(motifs)) || is.element("PFMatrixList", class(motifs))) {
motifL <- motifs
} else {
logger.error(c("unsupported value for motifs:", motifs))
}
res[["genome"]] <- genomeObj
res[["motifs"]] <- motifL
return(res)
}

#' getMotifOccurrences
Expand Down
5 changes: 5 additions & 0 deletions man/ChrAccR.Rd

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

4 changes: 2 additions & 2 deletions man/callPeaks-DsATAC-method.Rd

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

2 changes: 0 additions & 2 deletions man/computeDiffAcc.rnb.nome.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/createReport_differential-DsATAC-method.Rd

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

6 changes: 3 additions & 3 deletions man/getSamples-DsAcc-method.Rd

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

9 changes: 3 additions & 6 deletions man/prepareMotifmatchr.Rd

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