find_nearest_distance: Nearest neighbor distances for each cell and phenotype.

Description Usage Arguments Details Value See Also Examples

View source: R/distance_funcs.R

Description

For each cell in a single sample, find the distances from the cell to the nearest neighbor cells in each of the provided phenotypes.

Usage

1
find_nearest_distance(csd, phenotypes = NULL, dst = NULL)

Arguments

csd

A data frame with Cell X Position, Cell Y Position and Phenotype columns, such as the result of calling read_cell_seg_data.

phenotypes

Optional list of phenotypes to include. If omitted, unique_phenotypes(csd) will be used.

dst

Optional distance matrix. If provided, this should be distance_matrix(csd). Not used if rtree is available.

Details

If the rtree package is available, this will use a fast, memory-efficient algorithm capable of processing fields with many thousand cells. Otherwise, a simple distance matrix algorithm is used. The simple algorithm requires at least 8 * (number of cells)^2 bytes of memory which becomes prohibitive as the number of cells becomes large.

Install the rtree package from GitHub using the command devtools::install_github('akoyabio/rtree').

Value

A tibble containing a Distance to <phenotype> column and Cell ID <phenotype> column for each phenotype. Columns will contain NA values where there is no other cell of the phenotype.

See Also

compute_all_nearest_distance which applies this function to a (possibly merged) data file.

Other distance functions: compute_all_nearest_distance(), count_touching_cells(), count_within_batch(), count_within_many(), count_within(), distance_matrix(), spatial_distribution_report(), subset_distance_matrix()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Compute distance columns
csd <- sample_cell_seg_data
nearest <- find_nearest_distance(csd)
dplyr::glimpse(nearest)

# Make a combined data frame including original data and distance columns
csd <- cbind(csd, find_nearest_distance(csd))

## Not run: 
# If `merged` is a data frame containing cell seg data from multiple fields,
# this code will create a new `tibble` with distance columns computed
# for each `Sample Name` in the data.
merged_with_distance <- merged %>%
  dplyr::group_by(`Sample Name`) %>%
  dplyr::do(dplyr::bind_cols(., find_nearest_distance(.)))

## End(Not run)

akoyabio/phenoptr documentation built on Jan. 7, 2022, 5:37 p.m.