Skip to content

Commit 0c85e13

Browse files
committed
tSpace updates, how to deal with negative distances in knn graph.
1 parent d004747 commit 0c85e13

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

R/tSpace.R

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
#' 'both' calculates pca and umap
3030
#' @param seed an integer specifying seed for set.seed function in order to have reproducible umap
3131
#' @param core_no and integer specifying number of cores for parallelization, check how many cores your machine has and adjust accordingly
32-
#' @return tSpace returns a list of objects: 1. ts_file: a data frame of pca and/or umap embbedings of trajectory space matrix and input data,
33-
#' 2. pca_tspace and/or umap_tspace: pca and/or UMAP objects. pca object contians all the outputs of pca analysis,
32+
#' @return tSpace returns a list of objects: 1. **ts_file**: a data frame of pca and/or umap embbedings of trajectory space matrix and input data,
33+
#' 2. pca_tspace and/or **umap_tspace**: pca and/or UMAP objects. pca object contians all the outputs of pca analysis,
3434
#' umap contians all the outputs of the umap analysis, see \code{\link[umap:umap]{umap}}
35-
#' 3. tspace_matrix: trajectory space matrix with calculated distances
35+
#' 3. **tspace_matrix**: trajectory space matrix with calculated distances. In case negative distances are calculated during knn graph computation, these will be aproximated to zero
36+
#' for trajectory inference completion, and reported in an object **negative_distances**, and a message will be reported in console.
3637
#' @importFrom foreach %dopar%
3738
#' @export
3839
tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5, trajectories = 200, wp = 20, ground_truth = F, weights = 'exponential', dr = 'pca', seed = NULL, core_no = 1, ...){
@@ -103,10 +104,8 @@ tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5,
103104
# In addition user can see which cells are involved in negative values pairs and can examine them.
104105

105106
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){
107+
cat(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"))
108+
negative.distance <- knn[which(knn[,3] < 0), ]
110109
knn[which(knn[,3] < 0), 3] <- 0
111110
}
112111

@@ -176,7 +175,11 @@ tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5,
176175
#Shaping data output
177176
data.out <- as.data.frame(cbind(Index = Index, pca_out, df))
178177

179-
tspace_obj <- list(ts_file = data.out, pca_embbeding = pca_tspace, tspace_matrix = tspace_mat)
178+
if(exists("negative.distance") == T){
179+
tspace_obj <- list(ts_file = data.out, pca_embbeding = pca_tspace, tspace_matrix = tspace_mat, negative_distances = negative.distance)
180+
}else{
181+
tspace_obj <- list(ts_file = data.out, pca_embbeding = pca_tspace, tspace_matrix = tspace_mat)
182+
}
180183
}
181184

182185
if( dr == 'umap'){
@@ -196,7 +199,11 @@ tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5,
196199
#Shaping data output
197200
data.out <- as.data.frame(cbind(Index = Index, umap_out, df))
198201

199-
tspace_obj <- list(ts_file = data.out, umap_embbeding = umap_tspace, tspace_matrix = tspace_mat)
202+
if(exists("negative.distance") == T){
203+
tspace_obj <- list(ts_file = data.out, umap_embbeding = umap_tspace, tspace_matrix = tspace_mat, negative_distances = negative.distance)
204+
}else{
205+
tspace_obj <- list(ts_file = data.out, umap_embbeding = umap_tspace, tspace_matrix = tspace_mat)
206+
}
200207
}
201208

202209
if( dr == 'both'){
@@ -228,7 +235,11 @@ tSpace <- function(df, K = 20, L = NULL, D = 'pearson_correlation', graph = 5,
228235
#Shaping data output
229236
data.out <- as.data.frame(cbind(Index = Index, pca_out, umap_out, df))
230237

231-
tspace_obj <- list(ts_file = data.out, pca = pca_tspace, umap = umap_tspace, tspace_matrix = tspace_mat)
238+
if(exists("negative.distance") == T){
239+
tspace_obj <- list(ts_file = data.out, pca = pca_tspace, umap_embbeding = umap_tspace, tspace_matrix = tspace_mat, negative_distances = negative.distance)
240+
}else{
241+
tspace_obj <- list(ts_file = data.out, pca = pca_tspace, umap_embbeding = umap_tspace, tspace_matrix = tspace_mat)
242+
}
232243
}
233244

234245
return(tspace_obj)

man/tSpace.Rd

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)