-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for images in SeuratToGiotto function
- Loading branch information
Showing
1 changed file
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1592,8 +1592,15 @@ seuratToGiottoV5 = function(sobject, | |
normexp = Seurat::GetAssayData(object = sobject, slot = "counts", assay = 'SCT') | ||
} | ||
|
||
if(!is.null(slot(sobject, 'assays')[[spatial_assay]]@layers)){ | ||
if("data" %in% slotNames(sobject@assays[[spatial_assay]])){ | ||
if(!is.null(slot(sobject, 'assays')[[spatial_assay]]@data)){ | ||
normexp = Seurat::GetAssayData(object = sobject, slot = "data", assay = spatial_assay) | ||
} | ||
} | ||
if("layers" %in% slotNames(sobject@assays[[spatial_assay]])){ | ||
if(!is.null(slot(sobject, 'assays')[[spatial_assay]]@layers)){ | ||
normexp = LayerData(object = sobject, assay = spatial_assay) | ||
} | ||
} | ||
|
||
# Cell Metadata | ||
|
@@ -1639,13 +1646,23 @@ seuratToGiottoV5 = function(sobject, | |
if (!is.null(Seurat::Images(object = sobject, assay = spatial_assay))) { | ||
spat_coord = Seurat::GetTissueCoordinates(sobject) | ||
# spat_coord = cbind(rownames(spat_coord), data.frame(spat_coord, row.names=NULL)) | ||
colnames(spat_coord) = c("sdimx", "sdimy", "cell_ID") | ||
|
||
if(!("cell" %in% spat_coord)){ | ||
spat_coord$cell_ID <- rownames(spat_coord) | ||
colnames(spat_coord) = c("sdimx", "sdimy", "cell_ID" ) | ||
} | ||
else{ | ||
colnames(spat_coord) = c("sdimx", "sdimy", "cell_ID") | ||
} | ||
|
||
spat_loc = spat_coord | ||
length_assay <- length(sobject@assays$Vizgen@cells@.Data) | ||
length_assay <-length(colnames(sobject)) | ||
#length_assay <- length(sobject@assays$Vizgen@[email protected]) | ||
spat_datatable <- data.table(cell_ID = character(length_assay), | ||
sdimx = rep(NA_real_, length_assay), | ||
sdimy = rep(NA_real_, length_assay)) | ||
spat_datatable$cell_ID <- rownames(sobject@assays$Vizgen@cells@.Data) | ||
#spat_datatable$cell_ID <- rownames(sobject@assays$Vizgen@[email protected]) | ||
spat_datatable$cell_ID <- colnames(sobject) | ||
match_cell_ID <- match(spat_loc$cell_ID, spat_datatable$cell_ID) | ||
matching_indices <- match_cell_ID | ||
matching_indices <- matching_indices[!is.na(matching_indices)] | ||
|
@@ -1688,6 +1705,46 @@ seuratToGiottoV5 = function(sobject, | |
|
||
#Find SueratImages, extract them, and pass to create seuratobj | ||
|
||
for (i in names(sobject@images)){ | ||
|
||
#check if image slot has image in it | ||
if("image" %in% slotNames(sobject@images[[i]])){ | ||
if(!is.null(sobject@images[[i]]@image)){ | ||
|
||
# Extract the red (r), green (g), and blue (b) channels | ||
r <- as.matrix(sobject@images[[i]]@image[,,1]) | ||
g <- as.matrix(sobject@images[[i]]@image[,,2]) | ||
b <- as.matrix(sobject@images[[i]]@image[,,3]) | ||
|
||
# Convert channels to rasters | ||
r <- raster::raster(r) | ||
g <- raster::raster(g) | ||
b <- raster::raster(b) | ||
|
||
# Stack the channels and convert to brick | ||
rgb_stack <- raster::stack(r, g, b) | ||
rgb_raster <- raster::brick(rgb_stack) | ||
|
||
#values rgb_raster | ||
values_rgb_raster <- raster::values(rgb_raster) | ||
|
||
# Rescale pixel values to the desired range (0-255) first | ||
rescaled_values <- scales::rescale(values_rgb_raster, to = c(0, 255)) | ||
|
||
# Then set the rescaled values in the raster object | ||
rgb_raster <- raster::setValues(rgb_raster, values = rescaled_values) | ||
# Convert to SpatRaster | ||
rgb_raster <- as(rgb_raster, "SpatRaster") | ||
|
||
# Create Giotto LargeImage | ||
gImg <- createGiottoLargeImage(raster_object = rgb_raster) | ||
|
||
# Plot the image | ||
plot(gImg) | ||
} | ||
} | ||
} | ||
|
||
|
||
gobject = createGiottoObject(exp, | ||
spatial_locs = spat_loc, | ||
|