Open andreyurch opened 8 months ago
I am having the same problem and I have even tried converting the V5 object to earlier versions.
Dear developers, do you have any suggestions or this package is not maintaned anymore?
Also facing this issue. It seems that the most up-to-date Seurat package has moved the counts slot. For an RNA assay this is now retrieved by obj@assays$RNA$counts rather than obj@assays$RNA@counts.
@sjdlabgroup, would it be possible to update the package to reflect this?
For other users arriving here for the same issue, it can be fixed by:
predict_node_M = function(obj, model, node, get_prob = T, allowed_nodes = NULL){
idx = which(model$hierarchy_mat == node, arr.ind = T)
new_ids = model$hierarchy_mat[idx[1,1] + 1, idx[,2]] %>% as.character() %>% unique()
if(!is.null(allowed_nodes) & any(new_ids %in% allowed_nodes == F)){
obj = obj[, Idents(obj) == node]
pred_res = data.frame(barcode = colnames(obj),
pred = new_ids[new_ids %in% allowed_nodes],
prob = NA,
census_clusters = obj$census_clusters,
u1 = obj@reductions$umap@cell.embeddings[,1],
u2 = obj@reductions$umap@cell.embeddings[,2])
}
if(any(new_ids %in% allowed_nodes == F) & get_prob == F){
return(pred_res)
} else {
g = intersect(model$markers[[node]]$gene, rownames(obj))
temp_seurat = obj[g, Idents(obj) == node]
x2 = temp_seurat@assays$RNA$counts %>% as.matrix()
# add missing genes
g = setdiff(model$markers[[node]]$gene, rownames(obj))
if(length(g) > 0){
x2 = rbind(x2, matrix(0, nrow = length(g), ncol = ncol(x2)))
rownames(x2) = c(rownames(temp_seurat), g)
}
x2 = x2[model$markers[[node]]$gene, ]
x2[x2 == 0] = NA
x2 = apply(x2, 2, dplyr::ntile, 100) %>% t()
colnames(x2) = model$markers[[node]]$gene
x2 = x2/matrixStats::rowMaxs(x2, na.rm = T)
pred.prob = predict(model$models[[node]], x2)
pred.class = ifelse(pred.prob > 0.5, max(new_ids), min(new_ids))
pred_df = data.frame(barcode = colnames(temp_seurat),
pred = pred.class,
prob = pred.prob,
census_clusters = temp_seurat$census_clusters,
u1 = temp_seurat@reductions$umap@cell.embeddings[,1],
u2 = temp_seurat@reductions$umap@cell.embeddings[,2],
stringsAsFactors = F)
# if(!is.null(allowed_nodes) & any(pred_df$pred %in% allowed_nodes == F)){
# pred_df$pred = new_ids[new_ids %in% allowed_nodes]
# }
pred_res = contour_adjust(pred_df)
return(pred_res)
}
}
assignInNamespace("predict_node", predict_node_M, ns = "Census")