Skip to content

Commit

Permalink
Merge branch 'dev' into dev2
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed Nov 25, 2024
2 parents 23b52fe + e386d71 commit ce9cad4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 54 deletions.
131 changes: 77 additions & 54 deletions R/interoperability.R
Original file line number Diff line number Diff line change
Expand Up @@ -1468,11 +1468,11 @@ giottoToSeuratV4 <- function(
#' @returns Seurat object
#' @keywords seurat interoperability
#' @export
giottoToSeuratV5 <- function(
gobject,
spat_unit = NULL,
res_type = c("hires", "lowres", "fullres"),
...) {
giottoToSeuratV5 <- function(gobject,
spat_unit = NULL,
dataType,
res_type = c("hires", "lowres", "fullres"),
...) {
# data.table vars
feat_type <- name <- dim_type <- nn_type <- NULL

Expand Down Expand Up @@ -1801,17 +1801,36 @@ giottoToSeuratV5 <- function(
# since we allow use non-lowres images
)
# see https://github.com/satijalab/seurat/issues/3595
newV1 <- new(
Class = "VisiumV1",
image = img_array,
scale.factors = scalefactors,
coordinates = coord,
spot.radius =
if (dataType != 0){
if(dataType == "xenium"){
coord1 <- coord
coord$cell_id <- rownames(coord)
coord <- coord[, c("cell_id", "imagerow", "imagecol")]
segmentations.data <- list(
"centroids" = SeuratObject::CreateCentroids(coord1),
"segmentation" = SeuratObject::CreateSegmentation(coord)
)
coords <- SeuratObject::CreateFOV(
coords = segmentations.data,
type = c("segmentation", "centroids"),
assay = "rna")
fov <- "default_fov"
sobj[[fov]] <- coords
}else{
newV1 <- new(
Class = "VisiumV1",
image = img_array,
scale.factors = scalefactors,
coordinates = coord,
spot.radius =
scalef$fiducial * scalef$lowres / max(dim(img_array)),
key = paste0(key, "_")
)

sobj@images[[key]] <- newV1
key = paste0(key, "_")
)

sobj@images[[key]] <- newV1
}
}

}
}

Expand Down Expand Up @@ -2377,50 +2396,54 @@ seuratToGiottoV5 <- function(
gpoints <- createGiottoPoints(mol_spatlocs,
feat_type = "rna"
)
if ("centroids" %in% names(sobject@images[[i]])) {
centroids_coords <-
sobject@images[[i]]$centroids@coords
centroids_coords <- vect(centroids_coords)
gpolygon <- create_giotto_polygon_object(
name = "cell", spatVector = centroids_coords
)
}
if ("segmentation" %in% names(sobject@images[[i]])) {
polygon_list <- list()

for (j in seq(sobject@images[[
i
]]@boundaries$segmentation@polygons)) {
polygon_info <- sobject@images[[
i
]]@boundaries$segmentation@polygons[[j]]

# Get coordinates from segmentation
seg_coords <- polygon_info@Polygons[[1]]@coords

# Fetch cell_Id from polygon information
cell_ID <- polygon_info@ID

# Convert it to SpatVector
seg_coords <- vect(seg_coords)

# Create giotto_polygon_object
gpolygon <- create_giotto_polygon_object(
name = "cell",
spatVector = centroids_coords,
spatVectorCentroids = seg_coords
)

# Add the cell_ID to the list of polygon names
polygon_list[[cell_ID]] <- gpolygon
}
}
}
}
if ("centroids" %in% names(sobject@images[[i]])) {
centroids_coords <-
sobject@images[[i]]$centroids@coords
centroids_coords <- vect(centroids_coords)
gpolygon <- create_giotto_polygon_object(
name = "cell", spatVector = centroids_coords
)
}
if ("segmentation" %in% names(sobject@images[[i]])) {
polygon_list <- list()
for (j in seq(sobject@images[[
i
]]@boundaries$segmentation@polygons)) {
polygon_info <- sobject@images[[
i
]]@boundaries$segmentation@polygons[[j]]
# Get coordinates from segmentation

seg_coords <- polygon_info@Polygons[[1]]@coords
# Fetch cell_Id from polygon information
cell_ID <- polygon_info@ID
# Convert it to SpatVector
seg_coords <- vect(seg_coords)
# Create giotto_polygon_object
gpolygon <- create_giotto_polygon_object(
name = "cell",
spatVector = centroids_coords,
spatVectorCentroids = seg_coords

)
# Add the cell_ID to the list of polygon names
polygon_list[[cell_ID]] <- gpolygon
}

}

}
}
}








# Find SueratImages, extract them, and pass to create image
image_list <- list()
for (i in names(sobject@images)) {
Expand Down
6 changes: 6 additions & 0 deletions R/methods-nesting.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ setMethod("spatUnit", signature = "spatData", function(x) x@spat_unit)
#' @export
setMethod("spatUnit", signature("giottoPolygon"), function(x) x@name)

#' @rdname spatUnit-generic
#' @export
setMethod("spatUnit<-", signature("ANY"), function(x, value) x)

#' @describeIn spatUnit-generic Set spatial unit information
#' @export
Expand Down Expand Up @@ -141,6 +144,9 @@ setMethod("featType", signature("list"), function(x) {
#' @export
setMethod("featType", signature = "featData", function(x) x@feat_type)

#' @rdname featType-generic
#' @export
setMethod("featType<-", signature("ANY"), function(x, value) x)

#' @describeIn featType-generic Set feature type information
#' @export
Expand Down
3 changes: 3 additions & 0 deletions man/featType-generic.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/spatUnit-generic.Rd

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

0 comments on commit ce9cad4

Please sign in to comment.