Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ importFrom(ggplot2,scale_x_log10)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,scale_y_discrete)
importFrom(ggplot2,scale_y_log10)
importFrom(ggplot2,scale_y_reverse)
importFrom(ggplot2,stat_density2d)
importFrom(ggplot2,stat_qq)
importFrom(ggplot2,sym)
Expand Down
99 changes: 48 additions & 51 deletions R/preprocessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ Load10X_Spatial <- function (
image = NULL,
image.name = "tissue_lowres_image.png",
segmentation.type = NULL,
lightweight = FALSE,
...
) {
# if more than one directory is passed in
Expand Down Expand Up @@ -710,49 +711,22 @@ Load10X_Spatial <- function (
image.dir = file.path(seg.data.dir, "spatial"),
data.dir = data.dir,
image.name = image.name,
segmentation.type = segmentation.type
segmentation.type = segmentation.type,
lightweight = lightweight
)

# Holds the segmentation object
segmentation_obj <- visium.segmentation@boundaries$segmentation

# Get sf data
sf_data <- visium.segmentation@[email protected]

# Set the attribute-geometry relationship to constant
# See https://r-spatial.github.io/sf/reference/sf.html#details
st_agr(sf_data) <- "constant"

# Create a dataframe from sf data to hold centroids
centroids_obj <- visium.segmentation@boundaries$centroids
centroid_coords <- sf::st_coordinates(sf::st_centroid(sf_data))
centroids_df <- data.frame(
x = centroid_coords[, "X"],
y = centroid_coords[, "Y"],
row.names = sf_data$barcodes
)

# Create centroids object
centroids <- CreateCentroids(centroids_df,
nsides = Inf,
radius = NULL,
theta = 0)

# Add centroids to the Visium object
visium.segmentation@boundaries$centroids <- centroids

# Create a new Seurat object with the raw counts
segmentation.object <- CreateSeuratObject(
segmentation.counts,
assay = segmentation.assay.name
)

# Make sure the list of cell names between segmentations & raw counts matches exactly
common_cells <- unique(Cells(visium.segmentation)[Cells(visium.segmentation) %in% Cells(segmentation.object)])
visium.segmentation <- subset(
x = visium.segmentation,
cells = intersect(Cells(segmentation.object), Cells(visium.segmentation))
cells = common_cells
)

# Set the default boundary type to centroids for plotting
DefaultBoundary(object = visium.segmentation) <- "centroids"

Expand Down Expand Up @@ -1426,7 +1400,7 @@ Read10X_Image <- function(

# Create an `sp` compatible `FOV` instance.
fov <- CreateFOV(
coordinates[, c("imagerow", "imagecol")],
coordinates[, c("imagecol", "imagerow")],
type = "centroids",
radius = scale.factors[["spot"]],
assay = assay,
Expand Down Expand Up @@ -1560,27 +1534,51 @@ Read10X_Segmentations <- function (image.dir,
image.name = "tissue_lowres_image.png",
assay = "Spatial.Polygons",
slice = "slice1.polygons",
segmentation.type = "cell") {
segmentation.type = "cell",
lightweight = FALSE) {

image <- png::readPNG(source = file.path(image.dir, image.name))

scale.factors <- Read10X_ScaleFactors(filename = file.path(image.dir,
"scalefactors_json.json"))
key <- Key(slice, quiet = TRUE)

# Pass proper scale.factor based on whether image is lowres or hires
# We assume if not lowres it is hires - image has been validated above
scale.factor <- if (grepl("lowres", image.name)) "lowres" else "hires"

sf.data <- Read10X_HD_GeoJson(data.dir = data.dir,
image.dir = image.dir,
segmentation.type = segmentation.type)

# Create a Segmentation object based on sf, populate sf.data and polygons
segmentation <- CreateSegmentation(sf.data)
# Set the attribute-geometry relationship to constant
# See https://r-spatial.github.io/sf/reference/sf.html#details
st_agr(sf.data) <- "constant"

# Extract coordinates as a dataframe from sf object
coords_mat <- sf::st_coordinates(sf.data)
l2_indices <- coords_mat[, "L2"] # L2 column corresponds to polygon (cell) index

coords_df <- data.frame(x = coords_mat[, 1],
y = coords_mat[, 2],
cell = sf.data$barcodes[l2_indices],
stringsAsFactors = FALSE)

# Create a Segmentation object; populate it based on the coordinates from the sf object
segmentations <- CreateSegmentation(coords_df, lightweight = lightweight)

# Create a dataframe from sf data to hold centroids
centroid_coords <- sf::st_coordinates(sf::st_centroid(sf.data))
centroids_df <- data.frame(
x = centroid_coords[, "X"],
y = centroid_coords[, "Y"],
row.names = sf.data$barcodes
)

# Named list with segmentation
boundaries <- list(segmentation = segmentation)
# Create centroids object
centroids <- CreateCentroids(centroids_df,
nsides = Inf,
radius = NULL,
theta = 0)

# Named list with segmentations and centroids
boundaries <- list(segmentations = segmentations, centroids = centroids)

# Get image, scale factors, key
image <- png::readPNG(source = file.path(image.dir, image.name))
scale.factors <- Read10X_ScaleFactors(filename = file.path(image.dir,
"scalefactors_json.json"))
key <- Key(slice, quiet = TRUE)

# Build VisiumV2 object
visium.v2 <- new(
Expand Down Expand Up @@ -1619,15 +1617,14 @@ Format10X_GeoJson_CellID <- function(ids, prefix = "cellid_", suffix = "-1", dig
#' Load 10X Genomics GeoJson
#'
#' @param data.dir Path to the directory containing matrix data
#' @param image.dir Path to the directory with spatial GeoJSON data
#' @param segmentation.type Which segmentations to load, cell or nucleus. If using nucleus the full matrix from cells is still used
#'
#' @return A FOV
#'
#' @export
#' @concept preprocessing
#'
Read10X_HD_GeoJson <- function(data.dir, image.dir, segmentation.type = "cell") {
Read10X_HD_GeoJson <- function(data.dir, segmentation.type = "cell") {
segmentation_polygons <- read_sf(file.path(data.dir,"segmented_outputs", paste0(segmentation.type, "_segmentations.geojson")))

# Restructure sf geometry for downstream compatibility
Expand Down
Loading