Skip to content

Commit d004747

Browse files
committed
tSpace updated to deal with negative distance values in knn graph
1 parent ac02df7 commit d004747

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

R/tSpace.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5,
9797
## Functions
9898
cat(paste0('Step 1:Finding graph'))
9999
knn <- graphfinder(x = df, k = K, distance = D, core_n = core_no)
100+
101+
# Some users experienced negative values in knn graph and core shortest path algorithm (Dijkstra) does not operate on
102+
# negative distance, therefore warning message is implemented, and all non-positive values are aproximated with a zero
103+
# In addition user can see which cells are involved in negative values pairs and can examine them.
104+
105+
if(min(knn) < 0){
106+
warning(paste0("\nSome cell-cell pairs have negative distances. \nIn order for this analysis to proceed, these distances will be aproximated to zero. \nIf substantial number of cells exhibit negative distances\nplease check the original data and examine which cells are causing the issue. \nSome of these cells may be just noise and should be removed"))
107+
knn[which(knn[,3] < 0), ]
108+
}
109+
if(min(knn) < 0){
110+
knn[which(knn[,3] < 0), 3] <- 0
111+
}
112+
100113
knn <- igraph::get.adjacency(igraph::graph.adjacency(Matrix::sparseMatrix(i=knn[,'I'], j=knn[,'J'], x=knn[,'D']), mode ='max', weighted = TRUE), attr = 'weight') # For comapriosn wiht MATLAB , index1 = F)
101114

102115
cat(paste0('\nStep 2: Finding trajectories in sub-graphs \nCalculation may take time, don\'t close R'))
@@ -134,9 +147,15 @@ tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5,
134147
}
135148
time <- tictoc::toc()
136149

137-
arr <- array( unlist(graph_panel) , c(nrow(graph_panel[[1]]),ncol(graph_panel[[1]]),graph) )
138-
tspace_mat <- rowMeans( arr , dims = 2 )
150+
#Remove last large tspace matrix, for large datasets not to be kept in the environment
151+
rm(tspacem)
152+
153+
tspace_mat <- array( unlist(graph_panel) , c(nrow(graph_panel[[1]]),ncol(graph_panel[[1]]),graph) )
154+
155+
#Remove last large tspace matrix, for large datasets not to be kept in the environment
156+
rm(graph_panel)
139157

158+
tspace_mat <- rowMeans( tspace_mat , dims = 2 )
140159
colnames(tspace_mat) <- paste0('T_', s)
141160

142161
cat(paste0('\nStep 3: Low dimensionality embbeding for visualization step'))

0 commit comments

Comments
 (0)