Skip to content

Commit

Permalink
Merge pull request #165 from BIMSBbioinfo/dev
Browse files Browse the repository at this point in the history
update combined plots legends and spot level conversion for as.SpatialExperiment
  • Loading branch information
Artur-man authored Nov 27, 2024
2 parents 7cf665c + ccb3240 commit 7942b71
Show file tree
Hide file tree
Showing 18 changed files with 10,280 additions and 34 deletions.
38 changes: 18 additions & 20 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ as.SpatialExperiment <- function(object, assay = NULL, reg = FALSE){

# check Seurat package
if(!requireNamespace('SpatialExperiment'))
stop("Please install SpatialExperiment package!")
stop("Please install SpatialExperiment package!: BiocManager::install('SpatialExperiment'')")

# check the number of assays
if(is.null(assay)){
Expand All @@ -754,44 +754,42 @@ as.SpatialExperiment <- function(object, assay = NULL, reg = FALSE){
}

# check the number of assays
if(unique(vrAssayTypes(object, assay = assay)) %in% c("spot","ROI")) {
stop("Conversion of Spot or ROI assays into SpatialExperiment is not yet permitted!")
if(unique(vrAssayTypes(object, assay = assay)) %in% c("ROI", "molecule", "tile")) {
stop("Conversion of ROI, molecule and tile assays into SpatialExperiment is not yet permitted!")
}

# data
rowdata <- vrData(object, assay = assay, norm = FALSE)
rawdata <- vrData(object, assay = assay, norm = FALSE)

# metadata
metadata <- Metadata(object, assay = assay)
if(is.null(rownames(metadata)))
rownames(metadata) <- metadata$id
assays <- stringr::str_extract(rownames(metadata), pattern = "_Assay[0-9]+$")
assays <- gsub("^_", "", assays)

# coordinates
coords <- vrCoordinates(object, assay = assay, reg = reg)
coords <- vrCoordinates(flipCoordinates(object, assay = assay), assay = assay, reg = reg)

# Seurat object
spe <- SpatialExperiment::SpatialExperiment(assay=rowdata,
spe <- SpatialExperiment::SpatialExperiment(assay=list(counts = rawdata),
colData=metadata,
sample_id=assays,
spatialCoords=coords)

# get image objects for each assay
for(assy in vrAssayNames(object)){
assay_object <- object[[assy]]
if(vrAssayTypes(assay_object) == "cell"){
img <- vrImages(assay_object)
imgfile <- tempfile(fileext='.png')
magick::image_write(image = img, path = imgfile, format = 'png')
spe <- SpatialExperiment::addImg(spe,
sample_id = vrAssayNames(assay_object),
image_id = "main",
imageSource = imgfile,
scaleFactor = NA_real_,
load = TRUE)
file.remove(imgfile)
} else {
stop("Currently VoltRon does only support converting cell type spatial data sets into SpatialExperiment objects!")
}
img <- vrImages(assay_object)
imgfile <- tempfile(fileext='.png')
magick::image_write(image = img, path = imgfile, format = 'png')
spe <- SpatialExperiment::addImg(spe,
sample_id = vrAssayNames(assay_object),
image_id = "main",
imageSource = imgfile,
scaleFactor = 1,
load = TRUE)
file.remove(imgfile)
}

# return
Expand Down
12 changes: 6 additions & 6 deletions R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ vrSpatialPlotSingle <- function(assay, metadata, group.by = "Sample", plot.segme
}
if(!is.null(polygon_data)){
g <- g +
geom_polygon(aes(x = x, y = y, fill = group.by, group = segment), data = polygon_data, alpha = alpha)
geom_polygon(aes(x = x, y = y, fill = group.by, group = segment), data = polygon_data, alpha = alpha, show.legend = TRUE)
}
if(!is.null(circle_data)){
g <- g +
ggforce::geom_ellipse(aes(x0 = as.numeric(x), y0 = as.numeric(y), a = as.numeric(rx), b = as.numeric(ry), angle = 0,
fill = group.by, group = segment), data = circle_data, lwd = 0, alpha = alpha)
fill = group.by, group = segment), data = circle_data, lwd = 0, alpha = alpha, show.legend = TRUE)
}
g <- g +
scale_fill_manual(values = colors, labels = names(colors), drop = FALSE, limits = names(colors),
Expand All @@ -335,7 +335,7 @@ vrSpatialPlotSingle <- function(assay, metadata, group.by = "Sample", plot.segme
# spot visualization
} else if(vrAssayTypes(assay) == "spot"){
g <- g +
geom_spot(mapping = aes_string(x = "x", y = "y", fill = group.by), coords, shape = 21, alpha = alpha,
geom_spot(mapping = aes_string(x = "x", y = "y", fill = group.by), coords, shape = 21, alpha = alpha, show.legend = TRUE,
spot.radius = vrAssayParams(assay, param = "vis.spot.radius")/scale_factors) +
scale_fill_manual(values = colors, labels = names(colors), drop = FALSE, limits = names(colors)) +
guides(fill = guide_legend(override.aes=list(shape = 21, size = 4, lwd = 0.1)))
Expand All @@ -358,7 +358,7 @@ vrSpatialPlotSingle <- function(assay, metadata, group.by = "Sample", plot.segme
len_segments <- sapply(segments, nrow, simplify = TRUE)
polygon_data <- data.frame(polygon_data, segment = rep(names(segments), len_segments), group.by = rep(coords[[group.by]], len_segments))
g <- g +
geom_polygon(aes(x = x, y = y, fill = group.by, group = segment), data = polygon_data, alpha = alpha) +
geom_polygon(aes(x = x, y = y, fill = group.by, group = segment), data = polygon_data, alpha = alpha, show.legend = TRUE) +
scale_fill_manual(values = colors, labels = names(colors), drop = FALSE, limits = names(colors)) +
guides(fill = guide_legend(title = group.by))
}
Expand All @@ -371,7 +371,7 @@ vrSpatialPlotSingle <- function(assay, metadata, group.by = "Sample", plot.segme
g <- vrGroupPlotTiling(g = g, data = coords, group.by = group.by, n.tile = n.tile, alpha = alpha)
} else {
g <- g +
geom_point(mapping = aes_string(x = "x", y = "y", fill = group.by, color = group.by), coords, shape = cell.shape, size = rel(pt.size), alpha = alpha)
geom_point(mapping = aes_string(x = "x", y = "y", fill = group.by, color = group.by), coords, shape = cell.shape, size = rel(pt.size), alpha = alpha, show.legend = TRUE)
}

# style, color and text
Expand All @@ -396,7 +396,7 @@ vrSpatialPlotSingle <- function(assay, metadata, group.by = "Sample", plot.segme
g <- vrGroupPlotTiling(g = g, data = coords, group.by = group.by, n.tile = n.tile, alpha = alpha)
} else {
g <- g +
geom_point(mapping = aes_string(x = "x", y = "y", fill = group.by, color = group.by), coords, shape = cell.shape, size = rel(pt.size), alpha = alpha)
geom_point(mapping = aes_string(x = "x", y = "y", fill = group.by, color = group.by), coords, shape = cell.shape, size = rel(pt.size), alpha = alpha, show.legend = TRUE)
}
g <- g +
scale_fill_manual(values = colors, labels = names(colors), drop = FALSE, limits = names(colors)) +
Expand Down
1 change: 0 additions & 1 deletion cleanup
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/sh
rm -f src/Makevars configure.log autobrew
rm -Rf .deps
rm -Rf .deps
6 changes: 3 additions & 3 deletions docs/conversion.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<title>Conversion</title>

<script src="site_libs/header-attrs-2.27/header-attrs.js"></script>
<script src="site_libs/header-attrs-2.29/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/flatly.min.css" rel="stylesheet" />
Expand All @@ -35,8 +35,8 @@
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-6.4.2/css/all.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.4.2/css/v4-shims.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.5.2/css/all.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.5.2/css/v4-shims.min.css" rel="stylesheet" />

<style type="text/css">
code{white-space: pre-wrap;}
Expand Down
Loading

0 comments on commit 7942b71

Please sign in to comment.