Skip to content

Commit

Permalink
add verbose arguements to some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-man committed Jan 13, 2025
1 parent 41b4de3 commit 57a9b89
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 85 deletions.
15 changes: 10 additions & 5 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#' @param type the spatial data type of Seurat object: "image" or "spatial"
#' @param assay_name the assay name
#' @param verbose verbose
#' @param ... Additional parameter passed to \link{formVoltRon}
#'
#' @rdname as.VoltRon
Expand All @@ -12,7 +13,7 @@
#' @importFrom stringr str_replace str_extract
#' @export
#'
as.VoltRon.Seurat <- function(object, type = c("image", "spatial"), assay_name = NULL, ...){
as.VoltRon.Seurat <- function(object, type = c("image", "spatial"), assay_name = NULL, verbose = TRUE, ...){

# check Seurat package
if(!requireNamespace('Seurat'))
Expand Down Expand Up @@ -40,7 +41,8 @@ as.VoltRon.Seurat <- function(object, type = c("image", "spatial"), assay_name =
for(fn in fov_names){

# message
message("Converting FOV: ", fn, " ...")
if(verbose)
message("Converting FOV: ", fn, " ...")

# image object
spatialobject <- spatialobjectlist[[fn]]
Expand Down Expand Up @@ -83,7 +85,8 @@ as.VoltRon.Seurat <- function(object, type = c("image", "spatial"), assay_name =
}

# merge object
message("Merging object ...")
if(verbose)
message("Merging object ...")
vrobject <- merge(voltron_list[[1]], voltron_list[-1])
} else{
image <- NULL
Expand Down Expand Up @@ -745,6 +748,7 @@ as.Giotto <- function(object, assay = NULL, reg = FALSE){
#' @param assay_type one of two types, 'cell' or 'spot' etc.
#' @param assay_name the assay name of the voltron assays (e.g. Visium, Xenium etc.)
#' @param image_id select image_id names if needed.
#' @param verbose verbose
#' @param ... Additional parameter passed to \link{formVoltRon}
#'
#' @rdname as.VoltRon
Expand All @@ -753,7 +757,7 @@ as.Giotto <- function(object, assay = NULL, reg = FALSE){
#' @importFrom magick image_read
#'
#' @export
as.VoltRon.SpatialExperiment <- function(object, assay_type = "cell", assay_name = NULL, image_id = NULL, ...){
as.VoltRon.SpatialExperiment <- function(object, assay_type = "cell", assay_name = NULL, image_id = NULL, verbose = TRUE, ...){

# check SpatialExperiment package
if(!requireNamespace('SpatialExperiment'))
Expand Down Expand Up @@ -846,7 +850,8 @@ as.VoltRon.SpatialExperiment <- function(object, assay_type = "cell", assay_name
}

# merge object
message("Merging object ...")
if(verbose)
message("Merging object ...")
if(length(voltron_list) > 1){
vrobject <- merge(voltron_list[[1]], voltron_list[-1])
} else {
Expand Down
111 changes: 73 additions & 38 deletions R/import.R

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions R/objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -1052,14 +1052,15 @@ subset.VoltRon <- function(object, subset, samples = NULL, assays = NULL, spatia
#' @param object_list a list of VoltRon objects
#' @param samples a single sample name or multiple sample names of the same size as the given VoltRon objects
#' @param main.assay the name of the main assay
#' @param verbose verbose
#'
#' @rdname merge
#' @aliases merge
#' @method merge VoltRon
#' @importFrom methods new
#'
#' @export
merge.VoltRon <- function(object, object_list, samples = NULL, main.assay = NULL) {
merge.VoltRon <- function(object, object_list, samples = NULL, main.assay = NULL, verbose = TRUE) {

# combine all elements
if(!is.list(object_list))
Expand All @@ -1080,12 +1081,14 @@ merge.VoltRon <- function(object, object_list, samples = NULL, main.assay = NULL
sample.metadata <- merge_sampleMetadata(sample.metadata_list)

# merge metadata and sample metadata
message("Merging metadata ...")
if(verbose)
message("Merging metadata ...")
metadata_list <- lapply(object_list, function(x) slot(x, name = "metadata"))
metadata <- merge(metadata_list[[1]], metadata_list[-1])

# combine samples and rename layers
message("Merging blocks and layers ...")
if(verbose)
message("Merging blocks and layers ...")
listofSamples <- NULL
for(i in 1:length(object_list)){
cur_object <- object_list[[i]]@samples
Expand Down
19 changes: 13 additions & 6 deletions R/spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ NULL
#' @param k number of neighbors for kNN.
#' @param radius When \code{method = "radius"} selected, determines the radius of a neighborhood ball around each spatial point.
#' @param graph.key the name of the graph.
#' @param verbose verbose
#'
#' @importFrom igraph add_edges simplify make_empty_graph vertices
#' @importFrom RCDT delaunay
Expand All @@ -34,7 +35,8 @@ getSpatialNeighbors <- function(object,
method = "delaunay",
k = 10,
radius = numeric(0),
graph.key = method){
graph.key = method,
verbose = TRUE){

# get coordinates
spatialpoints <- vrSpatialPoints(object, assay = assay)
Expand All @@ -56,7 +58,8 @@ getSpatialNeighbors <- function(object,
if(!is.null(group.by) && !is.null(group.ids)){

# metadata
message("Calculating Spatial Neighbors with group.by='", group.by, "' and group.ids='", paste(group.ids, collapse = ","), "'\n")
if(verbose)
message("Calculating Spatial Neighbors with group.by='", group.by, "' and group.ids='", paste(group.ids, collapse = ","), "'\n")
metadata = Metadata(object, assay = assy)
if(!group.by %in% colnames(metadata))
stop("The column '", group.by, "' was not found in the metadata!")
Expand Down Expand Up @@ -153,10 +156,11 @@ getSpatialNeighbors <- function(object,
#' @param graph.type the type of graph to determine spatial neighborhood
#' @param num.sim the number of simulations
#' @param seed seed
#' @param verbose verbose
#'
#' @export
#'
vrNeighbourhoodEnrichment <- function(object, assay = NULL, group.by = NULL, graph.type = "delaunay", num.sim = 1000, seed = 1){
vrNeighbourhoodEnrichment <- function(object, assay = NULL, group.by = NULL, graph.type = "delaunay", num.sim = 1000, seed = 1, verbose = TRUE){

# set the seed
set.seed(seed)
Expand All @@ -174,7 +178,8 @@ vrNeighbourhoodEnrichment <- function(object, assay = NULL, group.by = NULL, gra
# test for each assay
neigh_results <- list()
for(assy in assay_names){
message("Testing Neighborhood Enrichment of '", group.by ,"' for '", assy, "'")
if(verbose)
message("Testing Neighborhood Enrichment of '", group.by ,"' for '", assy, "'")
object_subset <- subset(object, assays = assy)
neigh_results[[assy]] <- vrNeighbourhoodEnrichmentSingle(object_subset, group.by = group.by, graph.type = graph.type,
num.sim = num.sim, seed = seed)
Expand Down Expand Up @@ -277,13 +282,14 @@ vrNeighbourhoodEnrichmentSingle <- function(object, group.by = NULL, graph.type
#' @param alpha.value the alpha value for the hot spot analysis test. Default is 0.01
#' @param norm if TRUE, the normalized data is used
#' @param seed seed
#' @param verbose verbose
#'
#' @importFrom Matrix rowSums
#' @importFrom igraph as_adjacency_matrix
#' @importFrom stats pnorm
#'
#' @export
getHotSpotAnalysis <- function(object, assay = NULL, method = "Getis-Ord", features, graph.type = "delaunay", alpha.value = 0.01, norm = TRUE, seed = 1){
getHotSpotAnalysis <- function(object, assay = NULL, method = "Getis-Ord", features, graph.type = "delaunay", alpha.value = 0.01, norm = TRUE, seed = 1, verbose = TRUE){

# set the seed
set.seed(seed)
Expand Down Expand Up @@ -311,7 +317,8 @@ getHotSpotAnalysis <- function(object, assay = NULL, method = "Getis-Ord", featu
for(assy in assay_names){

# verbose
message("Running Hot Spot Analysis with '", method, "' for '", assy, "'")
if(verbose)
message("Running Hot Spot Analysis with '", method, "' for '", assy, "'")

# get related data
graph <- vrGraph(object, assay = assy, graph.type = graph.type)
Expand Down
11 changes: 10 additions & 1 deletion man/as.VoltRon.Rd

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

10 changes: 9 additions & 1 deletion man/generateCosMxImage.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/generateXeniumImage.Rd

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

5 changes: 4 additions & 1 deletion man/getHotSpotAnalysis.Rd

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

5 changes: 4 additions & 1 deletion man/getSpatialNeighbors.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/importCosMx.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/importGenePS.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/importOpenST.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/importPhenoCycler.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/importXenium.Rd

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

4 changes: 3 additions & 1 deletion man/merge.Rd

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

Loading

0 comments on commit 57a9b89

Please sign in to comment.