Skip to content

Commit

Permalink
upload NetID v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
WWXkenmo authored Nov 25, 2024
1 parent 6ad1ffd commit 3416ef1
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 433 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Package: NetID
Type: Package
Title: A Meta-cell Base Method to Infer Fate Specific Gene Regulatory
Network from Single Cell Gene Expression Data
Version: 0.1.0
Version: 0.1.1
biocViews:
Imports: Matrix, lmtest, glmnet, RobustRankAggreg, irlba, rsvd,
reticulate, Seurat, Hmisc, mclust, pracma, doParallel, doRNG,
RaceID (>= 0.2.9), igraph, rARPACK
RaceID (== 0.3.9), igraph, rARPACK
Suggests: GENIE3, SingleCellExperiment, knitr, rmarkdown
VignetteBuilder: knitr
Author: Weixu Wang and Dominic Grün
Expand Down
257 changes: 147 additions & 110 deletions R/FateDynamic.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,121 +21,158 @@
#'
#' @export
#'
FateDynamic <- function(sce,global_params = list(),palantir_params = list(),cellrank_params = list(),work_dir = NULL){
## Build Annodata object using SingleCellExperiments
if(!is.null(work_dir)){setwd(work_dir)}
env = environment()

anndata <- reticulate::import('anndata', convert = FALSE)
sc <- reticulate::import('scanpy', convert = FALSE)
scv <- reticulate::import('scvelo', convert = FALSE)

sce <- sce[,colSums(SummarizedExperiment::assays(sce)$spliced)>0]
X <- SummarizedExperiment::assays(sce)$spliced
obs <- as.data.frame(SummarizedExperiment::colData(sce))
var <- data.frame(genes = rownames(sce))
X <- X[!duplicated(var$genes),]
var <- data.frame(genes = rownames(X))
rownames(var) <- rownames(X)
obsm <- NULL
reductions <- SingleCellExperiment::reducedDimNames(sce)
if (length(reductions) > 0) {
obsm <- sapply(
reductions,
function(name) as.matrix(SingleCellExperiment::reducedDim(sce, name)),
simplify = FALSE
)
names(obsm) <- paste0('X_', tolower(SingleCellExperiment::reducedDimNames(sce)))
}

layers <- list()
for (layer in c("spliced","unspliced")) {
mat <- SummarizedExperiment::assays(sce)[[layer]]
mat <- mat[rownames(X),]
layers[[layer]] <- Matrix::t(mat)
}

adata <- anndata$AnnData(
X = Matrix::t(X),
obs = obs,
var = var,
obsm = obsm,
layers = layers
)

adata$write("adata.h5ad")
####
# input global parameters
global_params <- check_global_params(global_params)
list2env(global_params,env)
## check parameters
palantir_params <- check_palantir_params(palantir_params)
list2env(palantir_params,env)
cellrank_params <- check_cellrank_params(cellrank_params)
list2env(cellrank_params,env)
FateDynamic <- function (sce, global_params = list(), palantir_params = list(),
cellrank_params = list(), work_dir = NULL)
{
if (!is.null(work_dir)) {
setwd(work_dir)
}
env = environment()
anndata <- reticulate::import("anndata", convert = FALSE)
sc <- reticulate::import("scanpy", convert = FALSE)
scv <- reticulate::import("scvelo", convert = FALSE)

# input parameters
writeLines(paste("Using palantir to perform cell fate analysis...",sep=""))
n.cores = 8
if(is.null(palantir_params[["start_cell"]])){
if(method == "cytotrace"){
writeLines("Using CyToTRACE to identify start cell...")
if (require(CytoTRACE)) {
res = CytoTRACE::CytoTRACE(as.matrix(X),ncores = n.cores)$CytoTRACE
res = as.matrix(res)
}
if ( class(sce) == "SingleCellExperiment" | class(sce) == "Seurat" ){
if ( class(sce) == "Seurat" ) sce <- as.SingleCellExperiment(sce)
if ( ! ( "spliced" %in% names(assays(sce)) & "unspliced" %in% names(assays(sce)) ) ){
assaySp = "counts"
assayUn = NA
}else{
assaySp = "spliced"
assayUn = "unspliced"
}


sce <- sce[, colSums(SummarizedExperiment::assays(sce)[[assaySp]]) > 0]
X <- SummarizedExperiment::assays(sce)[[assaySp]]
obs <- as.data.frame(SummarizedExperiment::colData(sce))
var <- data.frame(genes = rownames(sce))
X <- X[!duplicated(var$genes), ]
var <- data.frame(genes = rownames(X))
rownames(var) <- rownames(X)
obsm <- NULL
reductions <- SingleCellExperiment::reducedDimNames(sce)

if (length(reductions) > 0) {
obsm <- sapply(reductions, function(name) as.matrix(SingleCellExperiment::reducedDim(sce,name)), simplify = FALSE)
names(obsm) <- paste0("X_", tolower(SingleCellExperiment::reducedDimNames(sce)))
}
layers <- list()
for (layer in c(assaySp, assayUn)) {
if ( !is.na(layer) ){
mat <- SummarizedExperiment::assays(sce)[[layer]]
mat <- mat[rownames(X), ]
layers[[layer]] <- Matrix::t(mat)
}
}
}else if (class(sce) == "SCseq" ) {
assaySp = "counts"
assayUn = NA

X <- getExpData(sce,genes=rownames(sce@expdata))

obs <- data.frame(cpart= as.character(sce@cpart))
rownames(obs) <- names(sce@cpart)
var <- data.frame(genes = rownames(X))
rownames(var) <- rownames(X)
obsm <- NULL
layers <- list()
layers[[assaySp]] <- Matrix::t(X)
}
if(method == "scent"){
writeLines("Using SCENT to identify start cell...")
object <- Seurat::CreateSeuratObject(X)
object <- Seurat::NormalizeData(object)
X_norm <- Seurat::GetAssayData(object)
rm(object);gc()
if(length(intersect(list.files(getwd()),"PPI.rds")) == 1){
writeLines("Find PPI object at local dictionary, Read PPI...")
PPI <- readRDS("PPI.rds")
}else{
PPI <- getPPI_String(X_norm,species=species)
}
if (require(SCENT)) {
res = SCENT::CompCCAT(X_norm, PPI)
}
saveRDS(PPI,file="PPI.rds")
res = as.matrix(res)
rownames(res) <- colnames(X_norm)
rm(X_norm,PPI);gc()
adata <- anndata$AnnData(X = Matrix::t(X), obs = obs, var = var, obsm = obsm, layers = layers)
adata$write("adata.h5ad")
global_params <- check_global_params(global_params)
list2env(global_params, env)
palantir_params <- check_palantir_params(palantir_params)
list2env(palantir_params, env)
cellrank_params <- check_cellrank_params(cellrank_params)
list2env(cellrank_params, env)
writeLines(paste("Using palantir to perform cell fate analysis...", sep = ""))
n.cores = 8
if (is.null(palantir_params[["start_cell"]])) {
if (method == "cytotrace") {
writeLines("Using CyToTRACE to identify start cell...")
if (require(CytoTRACE)) {
res = CytoTRACE::CytoTRACE(as.matrix(X), ncores = n.cores)$CytoTRACE
res = as.matrix(res)
}
}
if (method == "scent") {
writeLines("Using SCENT to identify start cell...")
object <- Seurat::CreateSeuratObject(X)
object <- Seurat::NormalizeData(object)
X_norm <- Seurat::GetAssayData(object)
rm(object)
gc()
if (length(intersect(list.files(getwd()), "PPI.rds")) ==
1) {
writeLines("Find PPI object at local dictionary, Read PPI...")
PPI <- readRDS("PPI.rds")
} else {
PPI <- getPPI_String(X_norm, species = species)
}
if (require(SCENT)) {
res = SCENT::CompCCAT(X_norm, PPI)
}
saveRDS(PPI, file = "PPI.rds")
res = as.matrix(res)
rownames(res) <- colnames(X_norm)
rm(X_norm, PPI)
gc()
}
if (method == "markergene") {
res = as.matrix(X[root_gene, ])
}
start_cell = rownames(res)[which.max(res)]
}
if(method == "markergene"){
res = as.matrix(X[root_gene,])
terminalstate = terminal_state[1]
if (is.null(terminalstate)) {
terminalstate = "None"
} else {
for (i in 2:length(terminal_state)) {
terminalstate = paste(terminalstate, " ", terminal_state[i],
sep = "")
}
}
start_cell = rownames(res)[which.max(res)]
}
## terminal_state
terminalstate = terminal_state[1]
if(is.null(terminalstate)){
terminalstate = "None"
}else{
for(i in 2:length(terminal_state)){
terminalstate = paste(terminalstate," ",terminal_state[i],sep="")
}
}

input = "adata.h5ad"
fate_predict_py = Hmisc::capitalize(tolower(as.character(fate_predict)))
plot = Hmisc::capitalize(tolower(as.character(plot)))
#if(is.null(cluster_label)) cluster_label = ""
script = system.file("/python/FateDynamic_py.py", package = "NetID")
commandLines = paste('python ',script,' -i ',input,' -m ',min_counts,' -n ',nhvgs,' -pc ',npcs,' -k ', knn, ' -pl ',"True",' -dc ',ndcs,' -r ',start_cell,' -ts ',terminalstate,' -nw ',nwps,' -mo ',mode,' -p ',plot,' -f ',fate_predict_py, ' -c ', cluster_label, ' -w ',weight_connectivities,' -nc ', ncores, ' -t ',tolerance,sep="")
#reticulate::py_run_file(system.file(commandLines, package = "NetID"))
system(commandLines, wait=TRUE)

if(velo){
writeLines(paste("Using cellrank with ",mode," mode velocity to perform cell fate analysis...",sep=""))
input = "adata.h5ad"
fate_predict_py = Hmisc::capitalize(tolower(as.character(fate_predict)))
plot = Hmisc::capitalize(tolower(as.character(plot)))
script = system.file("/python/FateDynamic_py.py", package = "NetID")
commandLines = paste('python ',script,' -i ',input,' -m ',min_counts,' -n ',nhvgs,' -pc ',npcs,' -k ', knn, ' -pl ',"False",' -dc ',ndcs,' -r ',start_cell, ' -ts ',terminalstate, ' -nw ',nwps,' -mo ',mode,' -p ',plot,' -f ',fate_predict_py, ' -c ', cluster_label, ' -w ',weight_connectivities,' -nc ', ncores, ' -t ',tolerance,sep="")
#reticulate::py_run_file(system.file(commandLines, package = "NetID"))
system(commandLines, wait=TRUE)
}
if ( class(sce) == "SCseq" ){
commandLines = paste("python ", script, " -i ", input, " -m ",
min_counts, " -n ", nhvgs, " -pc ", npcs, " -k ", knn,
" -pl ", "True", " -dc ", ndcs, " -r ", start_cell, " -ts ",
terminalstate, " -nw ", nwps, " -mo ", mode, " -p ",
plot, " -f ", fate_predict_py, " -c cpart",
" -w ", weight_connectivities, " -nc ", ncores, " -t ",
tolerance, sep = "")
}else{
commandLines = paste("python ", script, " -i ", input, " -m ",
min_counts, " -n ", nhvgs, " -pc ", npcs, " -k ", knn,
" -pl ", "True", " -dc ", ndcs, " -r ", start_cell, " -ts ",
terminalstate, " -nw ", nwps, " -mo ", mode, " -p ",
plot, " -f ", fate_predict_py, " -c ", cluster_label,
" -w ", weight_connectivities, " -nc ", ncores, " -t ",
tolerance, sep = "")
}
system(commandLines, wait = TRUE)

if (velo) {
if (!is.na(assayUn) ){
writeLines(paste("Using cellrank with ", mode, " mode velocity to perform cell fate analysis...", sep = ""))
script = system.file("/python/FateDynamic_py.py", package = "NetID")
commandLines = paste("python ", script, " -i ", input,
" -m ", min_counts, " -n ", nhvgs, " -pc ", npcs,
" -k ", knn, " -pl ", "False", " -dc ", ndcs, " -r ",
start_cell, " -ts ", terminalstate, " -nw ", nwps,
" -mo ", mode, " -p ", plot, " -f ", fate_predict_py,
" -c ", cluster_label, " -w ", weight_connectivities,
" -nc ", ncores, " -t ", tolerance, sep = "")
system(commandLines, wait = TRUE)
}else{
writeLines(paste("No unspliced counts available! Falling back to Palantir...", sep = ""))
}
}
}

getPPI_String <- function (object = NULL, species = 9606, score_threshold = 600,
Expand Down
Loading

0 comments on commit 3416ef1

Please sign in to comment.