-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantify_neighbourhood_function.R
44 lines (32 loc) · 1.96 KB
/
quantify_neighbourhood_function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
library(Seurat)
library(dplyr)
library(tidyverse)
# calculate the variance in coordinates of the neighbour cells for aech cell in a certain dimensionality reduction
#'@title neighbour_distance.scaled.
#'@description The function to calculate the average variance in coordinates of neighbour cells of a certain cell i based on the provided reduction map.
#'@param i The cell index.
#'@param reduction The reduction map used to calculate the coordinate variance.
#'@param seu The seurat object.
#'@return A number, the average variance in coordinates of neighbour cells of a certain cell i based on the provided reduction map.
neighbour_distance.scaled = function(i, reduction, seu, graph = "RNA_nn") {
# extract neighbour cells of cell i
n = colnames(seu@graphs[[graph]])[seu@graphs[[graph]][i,] == 1] # please change "RNA_nn" to "SCT_nn" if you are using SCTranform
# normalize dimension scales
embed.scale <- Embeddings(seu, reduction = reduction)
embed.scale[,1] <- BBmisc::normalize(embed.scale[,1], method = "range", range = c(-8, 8))
embed.scale[,2] <- BBmisc::normalize(embed.scale[,2], method = "range", range = c(-8, 8))
# dims of 20 cells
e = embed.scale[n, ]
# variance in dim
mean(var(e[,1]), var(e[,2]))
}
#'@title calculate_neighbour_distance_for_all_cells.
#'@description The function to calculate the average variance in coordinates of neighbour cells of all cells in the seurat object based on the provided reduction map.
#'@param colname The colname to store the neighbourhood distance value in metadata.
#'@param reduction The reduction map used to calculate the coordinate variance.
#'@param seu The seurat object.
#'@return A seurat object with a new metadata column storing the varaince in coodinates of neighbour cells for each cell
calculate_neighbour_distance_for_all_cells <- function(seu, reduction, colname, graph) {
for(i in 1:nrow(seu@meta.data)) {seu@meta.data[[colname]][i] = neighbour_distance.scaled(i, reduction, seu, graph)}
seu
}