-
Notifications
You must be signed in to change notification settings - Fork 0
/
getNNMatrix.R
160 lines (130 loc) · 4.63 KB
/
getNNMatrix.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
getNNMatrix <- function(
## INPUT
x, ## X coordinates
y, ## Y coordinates
cell_label, ## Cell type labels
## Delaunay Triangulation Option: fast, and number of times run depends on delaunayNNDegrees
delaunayTriangulation = T,
delaunayTriangulationDistanceThreshold = 20,
delaunayNNDegrees = c(1),
## Distances option: much slower, but only done once
euclideanDistances = NULL,
## For tripack::tri.mesh function
duplicate = "error",
jitter = 10^-12,
jitter.iter = 6,
jitter.random = FALSE,
verbose = TRUE
){
###
if(length(x) != length(y) | length(x) != length(cell_label) ){
cat('\n\nERROR: X, Y, AND CELL_LABELS REQUIRE THE SAME NUMBER OF ENTRIES!')
break
}
if(!delaunayTriangulation & is.null(euclideanDistances)){
cat("\n\nERROR: EITHER SPECIFY DISTANCES TO SEARCH FOR NEAREST NEIGHBOURS, OR SET delaunayTriangulation TO TRUE!")
break
}
###
###
CLB <- as.integer(factor(cell_label))
OUT <- list()
all_coords <- x + (y * 1i)
# idx <- x + max(x) * (y-min(y))
###
###
if(delaunayTriangulation){
if(verbose){ cat('\n\nRunning delaunay triangulation approach for getting nearest neighbours...') }
require(tripack)
dt <- tripack::tri.mesh(
x=x, y=y, duplicate = duplicate, jitter = jitter, jitter.iter = jitter.iter, jitter.random = jitter.random
)
nns_raw <- tripack::neighbours(dt)
convex_hull <- tripack::convex.hull(dt)
if(verbose){ cat('\nTabulating...') }
DTS <- list()
for(j in 1:length(delaunayNNDegrees)){
DEGREE <- delaunayNNDegrees[j]
cat(paste0('\nExamining neighbours of ', DEGREE, ' degree...'))
## Iteratively add more neighbours
di = 1
while(di <= DEGREE){
if(verbose){cat(paste0('\nUpdating neighbours list with ', di, ' order neighbours...'))}
if(di == 1){
nns <- nns_raw
di = di + 1
next
}
nns <- lapply(nns, function(x){
return(unique(unlist(nns_raw[x])))
})
di = di + 1
}
if(DEGREE != 1){
nns <- lapply(1:length(nns), function(j){
return(nns[[j]][(nns[[j]]!=j)])
})
}
frequency_matrix <- do.call(rbind, lapply(1:length(nns), function(i){
if(verbose){
if(i %% 1000 == 0 | i == 1){
cat(paste0(i, ' of ', length(nns), '...'))
}
}
## Removal of convex hull
nnvector <- nns[[i]]
if(i %in% convex_hull$i){
nnvector <- nnvector[!(nnvector %in% convex_hull$i)]
}
## Cutting by distance
if(!is.null(delaunayTriangulationDistanceThreshold) &
!is.na(delaunayTriangulationDistanceThreshold) &
!is.infinite(delaunayTriangulationDistanceThreshold)){
ref_coord <- all_coords[i]
coords <- all_coords[nnvector]
dists <- Mod(coords - ref_coord)
nnvector <- nnvector[dists < delaunayTriangulationDistanceThreshold]
}
values <- tabulate( CLB[nnvector] )
if(length(values) < max(CLB)){
values <- c(values, rep(0, max(CLB) - length(values)))
}
return(values)
}))
colnames(frequency_matrix) <-
paste0('DT', sprintf(paste0('%0', max(nchar(delaunayNNDegrees)), 'd'), DEGREE), '_', levels(factor(cell_label)))
DTS[[j]] <- frequency_matrix
}
DTS <- do.call(cbind, DTS)
OUT[['DT']] <- DTS
}
###
###
if(!is.null(euclideanDistances)){
euDists <- sort(unique(euclideanDistances))
if(verbose){ cat('\n\nGetting nearest neighbours within specific euclidean distances...') }
FM <- matrix(0, nrow = length(all_coords), ncol = max(CLB) * length(euDists))
for(i in 1:length(all_coords)){
if(verbose){
if(i %% 1000 == 0 | i == 1){
cat(paste0(i, ' of ', length(all_coords), '...'))
}
}
dists <- Mod(all_coords - all_coords[i])
reflab <- CLB[i] + c( (1:length(euDists)) - 1) * max(CLB)
for(j in 1:length(reflab)){
FM[,reflab[j]] <- FM[,reflab[j]] + (dists < euDists[j])
}
}
colns <- sapply(1:length(euDists), function(j){
paste0('EUD',
sprintf( paste0('%0', max(nchar(euclideanDistances)), 'd'), euDists[j]),
'_', levels(factor(cell_label)))
})
colnames(FM) <- as.vector(colns)
OUT[['FM']] <- FM
}
###
OUT <- do.call(cbind, OUT)
return(OUT)
}