Skip to content

Commit

Permalink
spot level conversion for as.SpatialExperiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-man committed Nov 22, 2024
1 parent 0caf9d1 commit e085829
Show file tree
Hide file tree
Showing 17 changed files with 10,274 additions and 28 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
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 e085829

Please sign in to comment.