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 Sep 5, 2024
1 parent ff772f0 commit 85b0b85
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 58 deletions.
14 changes: 7 additions & 7 deletions R/add_array_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#' ########################################################################
#'
#' if (file.exists("sample_info.rds")) {
#' sample_info <- readRDS('sample_info.rds')
#' sample_info <- readRDS("sample_info.rds")
#' } else {
#' sample_info <- dplyr::tibble(
#' group = "Br2719",
Expand All @@ -80,7 +80,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 @@ -89,18 +89,18 @@
#' )
#' 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())
#'
#'
#' saveRDS(sample_info, "sample_info.rds")
#' }
#'
#' ## Preparing Fiji coordinates and images for build_spe()
#' spe_input_dir <- tempdir()
#' prep_fiji_coords(sample_info, out_dir = spe_input_dir)
#' prep_fiji_image(sample_info, out_dir = spe_input_dir)
#'
#'
#' ########################################################################
#' # Add array coordinates
#' ########################################################################
Expand Down Expand Up @@ -172,9 +172,9 @@ add_array_coords <- function(spe, sample_info, coords_dir, calc_error_metrics =
coords_list[[i]] <- .fit_to_array(coords, inter_spot_dist_px)

if (calc_error_metrics) {
coords_list[[i]] = coords |>
coords_list[[i]] <- coords |>
mutate(
capture_area = stringr::str_split_i(key, '^[ACTG]+-1_', 2)
capture_area = stringr::str_split_i(key, "^[ACTG]+-1_", 2)
) |>
.add_error_metrics(coords_list[[i]], inter_spot_dist_px)
}
Expand Down
46 changes: 23 additions & 23 deletions R/array_error_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
#'
#' @author Nicholas J. Eagles
#' @keywords internal
.get_neighbors = function(i, coords) {
this_array_row = coords[[i, 'array_row']]
this_array_col = coords[[i, 'array_col']]
this_capture_area = coords[[i, 'capture_area']]
.get_neighbors <- function(i, coords) {
this_array_row <- coords[[i, "array_row"]]
this_array_col <- coords[[i, "array_col"]]
this_capture_area <- coords[[i, "capture_area"]]

# Grab the keys of the neighboring spots and return
neighbor_keys = coords |>
neighbor_keys <- coords |>
filter(
capture_area == this_capture_area,
(array_row == this_array_row & array_col == this_array_col - 2) |
(array_row == this_array_row & array_col == this_array_col + 2) |
(array_row == this_array_row - 1 & array_col == this_array_col - 1) |
(array_row == this_array_row - 1 & array_col == this_array_col + 1) |
(array_row == this_array_row + 1 & array_col == this_array_col - 1) |
(array_row == this_array_row + 1 & array_col == this_array_col + 1)
(array_row == this_array_row & array_col == this_array_col + 2) |
(array_row == this_array_row - 1 & array_col == this_array_col - 1) |
(array_row == this_array_row - 1 & array_col == this_array_col + 1) |
(array_row == this_array_row + 1 & array_col == this_array_col - 1) |
(array_row == this_array_row + 1 & array_col == this_array_col + 1)
) |>
pull(key)
return(neighbor_keys)
Expand All @@ -52,12 +52,12 @@
#'
#' @author Nicholas J. Eagles
#' @keywords internal
.get_shared_neighbors = function(coords_new, coords) {
coords_new$shared_neighbors = vapply(
.get_shared_neighbors <- function(coords_new, coords) {
coords_new$shared_neighbors <- vapply(
seq_len(nrow(coords)),
function(i) {
n_before = .get_neighbors(i, coords)
n_after = .get_neighbors(i, coords_new)
n_before <- .get_neighbors(i, coords)
n_after <- .get_neighbors(i, coords_new)
return(mean(n_before %in% n_after))
},
numeric(1)
Expand All @@ -70,7 +70,7 @@
#'
#' Given `tibble()`s before and after mapping to new array coordinates,
#' calculate metrics related to the suitability of the mapping.
#'
#'
#' Add column `shared_neighbors`, the fraction of neighbors a spot started
#' with that are retained after mapping; add column `euclidean_error`, the
#' number of multiples of the inter-spot distance a spot must move to be
Expand All @@ -91,20 +91,20 @@
#' and `euclidean_error` columns.
#'
#' @importFrom stringr str_split_i
#'
#'
#' @author Nicholas J. Eagles
#' @keywords internal
.add_error_metrics = function(coords, coords_new, inter_spot_dist_px) {
coords_new = coords_new |>
.add_error_metrics <- function(coords, coords_new, inter_spot_dist_px) {
coords_new <- coords_new |>
mutate(
euclidean_error = (
(pxl_col_in_fullres - pxl_col_in_fullres_rounded) ** 2 +
(pxl_row_in_fullres - pxl_row_in_fullres_rounded) ** 2
) ** 0.5 / inter_spot_dist_px,
capture_area = stringr::str_split_i(key, '^[ACTG]+-1_', 2),
(pxl_col_in_fullres - pxl_col_in_fullres_rounded)**2 +
(pxl_row_in_fullres - pxl_row_in_fullres_rounded)**2
)**0.5 / inter_spot_dist_px,
capture_area = stringr::str_split_i(key, "^[ACTG]+-1_", 2),
) |>
.get_shared_neighbors(coords) |>
select(-capture_area)

return(coords_new)
}
15 changes: 8 additions & 7 deletions R/build_spe.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
#' ########################################################################
#' # Prepare sample_info
#' ########################################################################
#'
#'
#' if (file.exists("sample_info.rds")) {
#' sample_info <- readRDS('sample_info.rds')
#' sample_info <- readRDS("sample_info.rds")
#' } else {
#' sample_info <- dplyr::tibble(
#' group = "Br2719",
Expand All @@ -55,7 +55,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 @@ -64,13 +64,13 @@
#' )
#' 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())
#'
#'
#' saveRDS(sample_info, "sample_info.rds")
#' }
#'
#'
#' ## Preparing Fiji coordinates and images for build_spe()
#' spe_input_dir <- tempdir()
#' prep_fiji_coords(sample_info, out_dir = spe_input_dir)
Expand Down Expand Up @@ -186,7 +186,8 @@ build_spe <- function(sample_info, coords_dir, count_type = "sparse", reference_
}

spe <- add_array_coords(
spe, sample_info, coords_dir, calc_error_metrics = calc_error_metrics
spe, sample_info, coords_dir,
calc_error_metrics = calc_error_metrics
)
spe <- add_overlap_info(spe, "sum_umi")

Expand Down
8 changes: 4 additions & 4 deletions R/prep_fiji_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#'
#' @examples
#' if (file.exists("sample_info.rds")) {
#' sample_info <- readRDS('sample_info.rds')
#' sample_info <- readRDS("sample_info.rds")
#' } else {
#' sample_info <- dplyr::tibble(
#' group = "Br2719",
Expand All @@ -44,7 +44,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 @@ -53,10 +53,10 @@
#' )
#' 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())
#'
#'
#' saveRDS(sample_info, "sample_info.rds")
#' }
#'
Expand Down
8 changes: 4 additions & 4 deletions R/prep_fiji_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#'
#' @examples
#' if (file.exists("sample_info.rds")) {
#' sample_info <- readRDS('sample_info.rds')
#' sample_info <- readRDS("sample_info.rds")
#' } else {
#' sample_info <- dplyr::tibble(
#' group = "Br2719",
Expand All @@ -46,7 +46,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 @@ -55,10 +55,10 @@
#' )
#' 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())
#'
#'
#' saveRDS(sample_info, "sample_info.rds")
#' }
#'
Expand Down
19 changes: 10 additions & 9 deletions R/spe_to_seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@
#'
#' ## Let's look at our resulting Seurat object
#' seur_stitched
spe_to_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) {
spe_to_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) {
SPOT_DIAMETER <- 55e-6

# Ensure all necessary columns are present in colData
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-add_array_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that(
########################################################################

if (file.exists("sample_info.rds")) {
sample_info <- readRDS('sample_info.rds')
sample_info <- readRDS("sample_info.rds")
} else {
sample_info <- dplyr::tibble(
group = "Br2719",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-build_spe.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_that(
########################################################################

if (file.exists("sample_info.rds")) {
sample_info <- readRDS('sample_info.rds')
sample_info <- readRDS("sample_info.rds")
} else {
sample_info <- dplyr::tibble(
group = "Br2719",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-prep_fiji_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that(
########################################################################

if (file.exists("sample_info.rds")) {
sample_info <- readRDS('sample_info.rds')
sample_info <- readRDS("sample_info.rds")
} else {
sample_info <- dplyr::tibble(
group = "Br2719",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-prep_fiji_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that(
########################################################################

if (file.exists("sample_info.rds")) {
sample_info <- readRDS('sample_info.rds')
sample_info <- readRDS("sample_info.rds")
} else {
sample_info <- dplyr::tibble(
group = "Br2719",
Expand Down

0 comments on commit 85b0b85

Please sign in to comment.