Skip to content

Commit

Permalink
closes #2, closes #5 Improvements to accessor functions & show method
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarazpc committed Sep 28, 2023
1 parent 0c58195 commit afb0665
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 185 deletions.
70 changes: 26 additions & 44 deletions R/accessors.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@
#' keepCols = "essential",
#' addBoundaries = "cell"
#' )
#'
#' # get insight into MoleculeExperiment object (e.g., see assay names)
#' me
#'
#' # get insight into molecules slot
#' # get insight into molecules slot (e.g., see the assay names)
#' showMolecules(me)
#'
#' # for developers, use molecules() getter
#' # expect a large output from call below
#' # molecules(me)
#' # molecules(me, assayName = "detected")
#' # alternatively, return rectangular data structure with flatten = TRUE
#' molecules(me, assayName = "detected", flatten = TRUE)
#'
#' # get insight into boundaries slot
#' # get insight into boundaries slot (e.g., see the assay names)
#' showBoundaries(me)
#'
#' # for developers, use boundaries() getter
Expand All @@ -58,10 +61,10 @@
#' boundaries(me, assayName = "cell", flatten = TRUE)
#'
#' # features() getter
#' features(me)
#' features(me, assayName = "detected")
#'
#' # segmentIDs() getter
#' segmentIDs(me, "cell")
#' segmentIDs(me, assayName = "cell")
#'
#' # setter example
#' # read in and standardise nucleus boundaries too
Expand All @@ -77,23 +80,23 @@
#' )
#'
#' # use `boundaries<-` setter to add nucleus boundaries to the boundaries slot
#' boundaries(me, "nucleus") <- nucleiMEList
#' boundaries(me, assayName = "nucleus") <- nucleiMEList
#' me
#' @return A MoleculeExperiment object slot.
NULL

#' @rdname accessors
#' @export
#' @importFrom methods is
#' @importFrom cli cli_inform
setMethod("molecules",
signature = signature(object = "MoleculeExperiment"),
definition = function(object,
assayName = "detected",
assayName = NULL,
flatten = FALSE) {
# check arg validity
# retrieve molecules only when correct assay name has been provided
.stop_if_null(assayName)
.check_if_character(assayName)

if (!assayName %in% names(object@molecules)) {
stop("Assay name specified does not exist in molecules slot.
Please specify another assay name in the assayName argument.")
Expand All @@ -104,13 +107,6 @@ Please specify another assay name in the assayName argument.")
big_df <- .flatten_molecules(object, assay_name = assayName)
return(big_df)
} else {
cli::cli_inform(c(
"{.emph {assayName}} assay transcripts were retrieved.",
"i" = paste0(
"Other transcript assays can be retrieved by",
" specifying the {.var assayName} argument."
)
))
return(object@molecules[assayName])
}
}
Expand All @@ -123,32 +119,18 @@ setMethod("boundaries",
signature = signature(object = "MoleculeExperiment"),
definition = function(object, assayName = NULL, flatten = FALSE) {
# check arg validity
.stop_if_null(assayName)
.check_if_character(assayName)

# get boundaries slot information
if (is.null(assayName)) {
warning(
"All boundaries assays were returned: ",
names(object@boundaries), ". To select only a specific boundary
subslot, specify the assayName argument."
)
return(object@boundaries)
} else {
if (!assayName %in% names(object@boundaries)) {
stop("Assay name specified does not exist in boundaries slot.
if (!assayName %in% names(object@boundaries)) {
stop("Assay name specified does not exist in boundaries slot.
Please specify another assay name in the assayName argument.")
}
} else {
if (flatten) {
big_df <- .flatten_boundaries(object, assay_name = assayName)
return(big_df)
} else {
cli::cli_inform(c(
"{.emph {assayName}} assay boundaries were retrieved.",
"i" = paste0(
"Other boundary assays can be retrieved by",
" specifying the {.var assayName} argument."
)
))
return(object@boundaries[assayName])
}
}
Expand All @@ -159,26 +141,22 @@ Please specify another assay name in the assayName argument.")
#' @export
setMethod("features",
signature = signature(object = "MoleculeExperiment"),
definition = function(object, assayName = "detected") {
definition = function(object, assayName = NULL) {
# check arg validity
.stop_if_null(assayName)
.check_if_character(assayName)
if (!assayName %in% names(object@molecules)) {
stop("Assay name specified does not exist in molecules slot.
Please specify another assay name in the assayName argument.")
}

# get the features from the molecules slot
samples <- names(object@molecules[[assayName]])
f_list <- lapply(samples, function(s) {
names(object@molecules[[assayName]][[s]])
})
names(f_list) <- samples
# TODO: use a verbosity setting to fix this!!!
return(f_list)
cli::cli_inform(c(
" {.emph {assayName}} assay features were retrieved.",
"i" = paste0(
"To select features from a different assay, specify it ",
"assay in the {.var assayName} argument."
)
))

}
)

Expand All @@ -193,6 +171,10 @@ setMethod("segmentIDs",
retrieve the unique IDs. For example, the \"cells\" assay for cell boundaries.")
}
.check_if_character(assayName)
if (!assayName %in% names(object@boundaries)) {
stop("Assay name specified does not exist in boundaries slot.
Please specify another assay name in the assayName argument.")
}

# get the segment IDs from the boundaries slot
samples <- names(object@boundaries[[assayName]])
Expand Down
13 changes: 11 additions & 2 deletions R/countMolecules.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ countMolecules <- function(me,
buffer = 0,
matrixOnly = FALSE,
nCores = 1) {
# check arg validity
# check arg validity
.check_if_me(me)
.stop_if_null(boundariesAssay, moleculesAssay)
.check_if_character(boundariesAssay, moleculesAssay)
if (!moleculesAssay %in% names(me@molecules)) {
stop("Assay name specified does not exist in molecules slot.
Please specify another assay name in the assayName argument.")
}
if (!boundariesAssay %in% names(me@boundaries)) {
stop("Assay name specified does not exist in boundaries slot.
Please specify another assay name in the assayName argument.")
}

init_mols <- MoleculeExperiment::molecules(me, moleculesAssay)
init_bds <- MoleculeExperiment::boundaries(me, boundariesAssay)
Expand All @@ -66,7 +74,8 @@ countMolecules <- function(me,
@boundaries slot.")
}
samples <- names(me@molecules[[moleculesAssay]])
features <- sort(unique(unlist(MoleculeExperiment::features(me))))
features <- sort(unique(unlist(
MoleculeExperiment::features(me, moleculesAssay))))


bds_all <- init_bds[[boundariesAssay]]
Expand Down
Loading

0 comments on commit afb0665

Please sign in to comment.