Skip to content

Commit

Permalink
Auto-style code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Eagles committed Oct 24, 2024
1 parent c117e6c commit 15b6708
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 75 deletions.
6 changes: 3 additions & 3 deletions R/add_array_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#' ########################################################################
#' # Prepare sample_info
#' ########################################################################
#'
#'
#' sample_info <- dplyr::tibble(
#' group = "Br2719",
#' capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1")
Expand All @@ -77,7 +77,7 @@
#' sample_info$spaceranger_dir <- file.path(
#' sr_dir, sample_info$capture_area, "outs", "spatial"
#' )
#'
#'
#' # Add Fiji-output-related columns
#' fiji_dir <- tempdir()
#' temp <- unzip(
Expand All @@ -86,7 +86,7 @@
#' )
#' sample_info$fiji_xml_path <- temp[grep("xml$", temp)]
#' sample_info$fiji_image_path <- temp[grep("png$", temp)]
#'
#'
#' ## Re-size images and add more information to the sample_info
#' sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir())
#'
Expand Down
2 changes: 1 addition & 1 deletion R/array_error_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
.add_error_metrics <- function(coords, coords_new, inter_spot_dist_px) {
## For R CMD CHECK
pxl_col_in_fullres <- pxl_col_in_fullres_rounded <- pxl_row_in_fullres <- pxl_row_in_fullres_rounded <- key <- capture_area <- NULL

coords_new <- coords_new |>
mutate(
euclidean_error = (
Expand Down
21 changes: 10 additions & 11 deletions R/as.Seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@
#'
#' ## Let's look at our resulting Seurat object
#' seur_stitched
as.Seurat <- function(
spe,
spatial_cols = c(
"tissue" = "in_tissue",
"row" = "array_row",
"col" = "array_col",
"imagerow" = "pxl_row_in_fullres",
"imagecol" = "pxl_col_in_fullres"
),
verbose = TRUE) {
as.Seurat <- function(spe,
spatial_cols = c(
"tissue" = "in_tissue",
"row" = "array_row",
"col" = "array_col",
"imagerow" = "pxl_row_in_fullres",
"imagecol" = "pxl_col_in_fullres"
),
verbose = TRUE) {
# Seurat is only suggested
BiocBaseUtils::checkInstalled('Seurat')
BiocBaseUtils::checkInstalled("Seurat")

SPOT_DIAMETER <- 55e-6

Expand Down
4 changes: 2 additions & 2 deletions R/build_SpatialExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#' sample_info$spaceranger_dir <- file.path(
#' sr_dir, sample_info$capture_area, "outs", "spatial"
#' )
#'
#'
#' # Add Fiji-output-related columns
#' fiji_dir <- tempdir()
#' temp <- unzip(
Expand All @@ -61,7 +61,7 @@
#' )
#' sample_info$fiji_xml_path <- temp[grep("xml$", temp)]
#' sample_info$fiji_image_path <- temp[grep("png$", temp)]
#'
#'
#' ## Re-size images and add more information to the sample_info
#' sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir())
#'
Expand Down
62 changes: 31 additions & 31 deletions R/merge_overlapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' merge overlapping (same array coordinates) spots by adding
#' expression (i.e. from \code{assays(spe)$counts}), returning a
#' \code{SpatialExperiment} with at most one spot per array location.
#'
#'
#' \code{colData(spe)} and \code{spatialCoords(spe)} of the merged spots are
#' taken from the spots whose \code{exclude_overlapping} values are \code{TRUE}.
#'
Expand All @@ -27,77 +27,77 @@
#' if (!exists("spe")) {
#' spe <- spatialLIBD::fetch_data(type = "visiumStitched_brain_spe")
#' }
#'
#'
#' # Group colData by group and array coordinates
#' grouped_coldata = colData(spe) |>
#' grouped_coldata <- colData(spe) |>
#' dplyr::as_tibble() |>
#' dplyr::group_by(group, array_row, array_col)
#'
#'
#' # Find the first 100 keys that overlap other spots and don't, respectively
#' overlapping_keys = grouped_coldata |>
#' overlapping_keys <- grouped_coldata |>
#' dplyr::filter(dplyr::n() > 1) |>
#' dplyr::slice_head(n = 2) |>
#' dplyr::ungroup() |>
#' dplyr::slice_head(n = 100) |>
#' dplyr::pull(key)
#' nonoverlapping_keys = grouped_coldata |>
#' nonoverlapping_keys <- grouped_coldata |>
#' dplyr::filter(dplyr::n() == 1) |>
#' dplyr::ungroup() |>
#' dplyr::slice_head(n = 100) |>
#' dplyr::pull(key)
#'
#'
#' # Built a small SPE containing some overlaps and some non-overlapping spots
#' small_spe = spe[, c(overlapping_keys, nonoverlapping_keys)]
#'
#' small_spe <- spe[, c(overlapping_keys, nonoverlapping_keys)]
#'
#' # Merge overlapping spots
#' small_spe_merged = merge_overlapping(small_spe)
#'
#' small_spe_merged <- merge_overlapping(small_spe)
#'
#' # All array coordinates have just one unique spot after merging
#' colData(small_spe_merged) |>
#' dplyr::as_tibble() |>
#' dplyr::group_by(group, array_row, array_col) |>
#' dplyr::summarize(n = dplyr::n()) |>
#' dplyr::pull(n) |>
#' table()
#'
#'
merge_overlapping <- function(spe) {
## For R CMD CHECK
array_row = array_col = exclude_overlapping = gene_id = group = key = NULL
array_row <- array_col <- exclude_overlapping <- gene_id <- group <- key <- NULL

# Find keys corresponding to spots that must be merged
overlapping_keys = colData(spe) |>
overlapping_keys <- colData(spe) |>
as_tibble() |>
group_by(group, array_row, array_col) |>
filter(n() > 1) |>
pull(key)

# Nothing to merge
if(length(overlapping_keys) == 0) {
if (length(overlapping_keys) == 0) {
return(spe)
}

# Check assays and halt or warn
if (!('counts' %in% names(assays(spe)))) {
if (!("counts" %in% names(assays(spe)))) {
stop("'counts' assay missing; unable to merge overlapping spots.")
}
if (length(assays(spe)) > 1) {
warning("Dropping assays other than 'counts' for merging.")
}
assays(spe) = list(counts = assays(spe)$counts)
assays(spe) <- list(counts = assays(spe)$counts)

if(length(reducedDims(spe)) > 0) {
if (length(reducedDims(spe)) > 0) {
warning("Dropped reducedDims(spe) for merging")
}
reducedDims(spe) = list()
merged_data =
reducedDims(spe) <- list()

merged_data <-
# For the spots to merge, form a tibble with spots as rows and genes
# and colData() columns as columns
assays(spe)$counts[,match(overlapping_keys, spe$key)] |>
assays(spe)$counts[, match(overlapping_keys, spe$key)] |>
as.matrix() |>
t() |>
cbind(
colData(spe[,match(overlapping_keys, spe$key)]) |>
colData(spe[, match(overlapping_keys, spe$key)]) |>
as_tibble()
) |>
as_tibble() |>
Expand All @@ -112,10 +112,10 @@ merge_overlapping <- function(spe) {
ungroup() |>
# Now just take colData variables from the non-excluded spot
filter(exclude_overlapping) |>
tidyr::pivot_wider(names_from = 'gene_id', values_from = 'expression')
tidyr::pivot_wider(names_from = "gene_id", values_from = "expression")

# Construct the SPE for just the merged spots
spe_overlap = SpatialExperiment(
spe_overlap <- SpatialExperiment(
assays = list(
counts = merged_data |>
select(!colnames(colData(spe))) |>
Expand All @@ -125,12 +125,12 @@ merge_overlapping <- function(spe) {
colData = merged_data |>
select(colnames(colData(spe))),
rowData = rowData(spe),
spatialCoords = spatialCoords(spe)[match(merged_data$key, spe$key),]
spatialCoords = spatialCoords(spe)[match(merged_data$key, spe$key), ]
)
colnames(spe_overlap) = spe_overlap$key
colnames(spe_overlap) <- spe_overlap$key

# Combined the merged spots and non-overlapping spots
spe = BiocGenerics::cbind(spe_overlap, spe[, !(spe$key %in% overlapping_keys)])
spe <- BiocGenerics::cbind(spe_overlap, spe[, !(spe$key %in% overlapping_keys)])

return(spe)
}
12 changes: 6 additions & 6 deletions R/prep_fiji.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#' in particular, `tissue_positions.csv`, `tissue_lowres_image.png`, and
#' `scalefactors_json.json` files are created. These functions are necessary to
#' run in preparation for \code{build_SpatialExperiment()}.
#'
#'
#' Given a `data.frame()` of sample information (\code{sample_info}) with
#' columns \code{capture_area}, \code{group}, and \code{fiji_xml_path},
#' expected to have one unique path to Fiji XML output per group, `prep_fiji_coords`
#' reads in the pixel coordinates from each capture area's \code{tissue_positions.csv}
#' file from SpaceRanger, and transform using the rotation matrix specified
#' by Fiji <https://imagej.net/software/fiji/>. It writes one new \code{tissue_positions.csv}
#' file per group.
#'
#'
#' After stitching all groups in \code{sample_info} with Fiji, images of
#' various resolutions (pixel dimensions) are left. `prep_fiji_image()` creates copies
#' of each image whose largest dimension is \code{lowres_max_size} pixels. It
Expand Down Expand Up @@ -52,7 +52,7 @@
#' sample_info$spaceranger_dir <- file.path(
#' sr_dir, sample_info$capture_area, "outs", "spatial"
#' )
#'
#'
#' # Add Fiji-output-related columns
#' fiji_dir <- tempdir()
#' temp <- unzip(
Expand All @@ -61,7 +61,7 @@
#' )
#' sample_info$fiji_xml_path <- temp[grep("xml$", temp)]
#' sample_info$fiji_image_path <- temp[grep("png$", temp)]
#'
#'
#' ## Re-size images and add more information to the sample_info
#' sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir())
#'
Expand All @@ -83,7 +83,7 @@
#'
#' # 'prep_fiji_image' produced an image and scalefactors
#' out_paths_image
#'
#'
#' # 'prep_fiji_coords' produced a file of spatial coordinates for the
#' # stitched Br2719
#' readr::read_csv(out_path_coords)
Expand Down Expand Up @@ -177,7 +177,7 @@ prep_fiji_image <- function(sample_info, out_dir, lowres_max_size = 1200) {
}

#' @describeIn prep_fiji Apply transform info from Fiji XML output
#'
#'
#' @import xml2
#' @importFrom stringr str_replace_all str_detect
#' @importFrom readr read_csv write_csv
Expand Down
2 changes: 1 addition & 1 deletion R/rescale_fiji_inputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ rescale_fiji_inputs <- function(sample_info, out_dir) {
)
)
}

if (!dir.exists(out_dir)) {
stop("'out_dir' does not exist; please create it.")
}
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-array_error_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that(
# Use the existing object's colData to define coords before and after
# aligning to the nearest new array coordinates (as well as define the
# distance between spot centroids in pixels)
coords_new = colData(spe) |>
coords_new <- colData(spe) |>
cbind(spatialCoords(spe)) |>
as_tibble() |>
select(
Expand All @@ -27,14 +27,14 @@ test_that(
arrange(array_row) |>
slice_head(n = 10) |>
ungroup()
coords = coords_new |>
coords <- coords_new |>
select(-c(array_row, array_col)) |>
rename(
array_row = array_row_original, array_col = array_col_original
)
inter_spot_dist_px = 277.1524
inter_spot_dist_px <- 277.1524

coords_err = .add_error_metrics(coords, coords_new, inter_spot_dist_px)
coords_err <- .add_error_metrics(coords, coords_new, inter_spot_dist_px)

# Two error-metric columns should be added
expect_equal(
Expand All @@ -50,7 +50,7 @@ test_that(

# Shared neighbors must similarly be between 0 and 1 (it's a
# proportion). NAs are allowed when there are no neighbors originally
temp = coords_err$shared_neighbors[!is.na(coords_err$shared_neighbors)]
temp <- coords_err$shared_neighbors[!is.na(coords_err$shared_neighbors)]
expect_equal(all((temp >= 0) & (temp <= 1)), TRUE)
}
)
24 changes: 12 additions & 12 deletions tests/testthat/test-merge_overlapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@ test_that(
}

# Group colData by group and array coordinates
grouped_coldata = colData(spe) |>
grouped_coldata <- colData(spe) |>
as_tibble() |>
group_by(group, array_row, array_col)

# Find the first 100 keys that overlap other spots and don't, respectively
overlapping_keys = grouped_coldata |>
overlapping_keys <- grouped_coldata |>
filter(n() > 1) |>
slice_head(n = 2) |>
ungroup() |>
slice_head(n = 100) |>
pull(key)
nonoverlapping_keys = grouped_coldata |>
nonoverlapping_keys <- grouped_coldata |>
filter(n() == 1) |>
ungroup() |>
slice_head(n = 100) |>
pull(key)

# Built a small SPE containing some overlaps and some non-overlapping spots
small_spe = spe[, c(overlapping_keys, nonoverlapping_keys)]
assays(small_spe) = list(counts = assays(small_spe)$counts)
reducedDims(small_spe) = list()
small_spe <- spe[, c(overlapping_keys, nonoverlapping_keys)]
assays(small_spe) <- list(counts = assays(small_spe)$counts)
reducedDims(small_spe) <- list()

# Merge overlapping spots
small_spe_merged = merge_overlapping(small_spe)
small_spe_merged <- merge_overlapping(small_spe)

# All array coordinates should have just one unique spot after merging
spots_per_coord = colData(small_spe_merged) |>
spots_per_coord <- colData(small_spe_merged) |>
as_tibble() |>
group_by(group, array_row, array_col) |>
summarize(n = n()) |>
pull(n)
expect_equal(all(spots_per_coord == 1), TRUE)

# Grab a couple keys that overlap from different capture areas
overlapping_keys = colData(small_spe) |>
overlapping_keys <- colData(small_spe) |>
as_tibble() |>
group_by(group, array_row, array_col) |>
filter(n() == 2, length(unique(capture_area)) == 2) |>
slice_head(n = 2) |>
ungroup() |>
slice_head(n = 2) |>
pull(key)
# The key
dominant_key = overlapping_keys[

# The key
dominant_key <- overlapping_keys[
small_spe$exclude_overlapping[
match(overlapping_keys, small_spe$key)
]
Expand Down
2 changes: 1 addition & 1 deletion vignettes/misc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,5 @@ where `exclude_overlapping` is `FALSE`. Note that the function can be quite
memory-intensive and time-consuming.

```{r "merge_overlapping", eval = FALSE}
spe_merged = merge_overlapping(spe)
spe_merged <- merge_overlapping(spe)
```
Loading

0 comments on commit 15b6708

Please sign in to comment.