-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rna velocity as possible input, fixes #112
- Loading branch information
Showing
8 changed files
with
112 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#' Calculate the attraction of cells to other cells using velocity | ||
#' | ||
#' @param current Current expression | ||
#' @param projected Projected expression based on RNA velocity | ||
#' | ||
#' @return Matrix containing the attraction ([-1, 1]) of each cell to the waypoint cells | ||
calculate_attraction <- function( | ||
current, | ||
projected, | ||
cells = colnames(projected), | ||
n_waypoints = 50, | ||
k = ceiling(n_waypoints / 2), | ||
corr_sigma = 1, | ||
debug = FALSE | ||
) { | ||
assertthat::assert_that(nrow(current) == nrow(projected)) | ||
assertthat::assert_that(ncol(current) == ncol(projected)) | ||
|
||
# select waypoint cells | ||
n_waypoints <- min(n_waypoints, length(cells)) | ||
k <- min(n_waypoints, k) | ||
waypoint_cells <- sample(cells, n_waypoints) | ||
|
||
# prepare for correlation calculation | ||
# em = expression | ||
# nd = velocity | ||
|
||
em <- as.matrix(current) | ||
ccells <- cells | ||
em <- em[, ccells] | ||
nd <- as.matrix(projected[, ccells] - current[, ccells]) | ||
cgenes <- intersect(rownames(em), rownames(nd)) | ||
nd <- nd[cgenes, ] | ||
em <- em[cgenes, ] | ||
|
||
# calculate correlation | ||
# this is an adapted version of colDeltaCorLog10 with waypoints | ||
transfo <- function(x) (log10(abs(x) + 1) * sign(x)) | ||
|
||
nd2 <- transfo(nd) | ||
|
||
waypoint <- waypoint_cells[[1]] | ||
cell <- cells[[1]] | ||
|
||
emw <- em[, waypoint_cells] | ||
|
||
# cors <- map(cells, function(cell) { | ||
# print(cell) | ||
# diff <- transfo(emw - em[, cell]) | ||
# cors <- cor(diff, nd2[, cell]) | ||
# colnames(cors) <- cell | ||
# cors[is.na(cors)] <- 0 | ||
# cors | ||
# }) | ||
# attraction <- do.call(cbind, cors) | ||
# attraction | ||
|
||
|
||
cors <- map(waypoint_cells, function(waypoint) { | ||
print(waypoint) | ||
diff <- transfo(emw[, waypoint] - em) | ||
cors <- pcor(diff, nd2) | ||
rownames(cors) <- waypoint | ||
cors[is.na(cors)] <- 0 | ||
cors | ||
}) | ||
attraction <- do.call(rbind, cors) | ||
colnames(attraction) <- colnames(em) | ||
attraction | ||
} | ||
|
||
|
||
|
||
pcor <- function(x, y = x, method = "pearson", use = "everything") { | ||
assertthat::assert_that(ncol(x) == ncol(y)); | ||
purrr::map_dbl(seq_len(ncol(x)), ~ cor(x[,.], y[,.], method = method, use = use)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters