sjdlabgroup / Census

Accurate, deep, fast, and fully-automated scRNA-seq cell-type annotation
10 stars 3 forks source link

A problem with Seurat object?? #3

Open andreyurch opened 8 months ago

andreyurch commented 8 months ago

res = census_main(pbmc, organ = 'Lung',verbose = T,predict_cancer = T) Running Seurat Warning in PrepDR5(object = object, features = features, layer = layer, : The following features were not available: ALB, CHIT1, SCGB1A1, GOLIM4, AMBP, MMP3, AHSG, MIR650, AGXT, PRSS2, MSMB, SLC38A2, APOA2, PRG4, AQP4, ALDOB, APOA1, ADH1A, MUC5AC, PCK1, CTNNB1, CYP2E1, FKBP9, RBP4, RREB1, WNT5A, NFKBIZ, NMU, FCN2, ADH4, HRG, STAT2, HPX, APOH, FGL1, CDC20B, TRIM5, GSTA2, PEBP4, ALDH3A2, IGFBP1, C20orf85, PRSS1, MARCKS, TSC22D2, THBS4, ITLN1, CYP2F1, GC, HNRNPH1, ADAM10, ARG1, TTR, BHMT, LIX1L, CCL7, WTAP, IBSP, HIPK1, TWIST1, APCS, CLDN18, APOC3, SULT2A1, KIAA1217, LOC100996447, LBP, TMX1, TMEM190, PLG, C9, PSCA, UGCG, SERPINC1, MSRB3, CASP14, RASGEF1B, EPYC, TMEM59L, FAM13C, FAM204A, SLC22A31, HMGCS2, CYP3A4, SNTN, KNG1, CSF3, MMD, IL24, PDS5B, HPD, BCL10, ZBTB32, ZNF532, ZMYND10, APOL2, PPFIBP1, ITLN2, NR0B2, ORM1, AKR1B10, CYP2C9, RSPH1, ROBO1, CRP, IRF2BPL, GAL, G3BP1, APOB, LRRC75A-AS1, FABP1, DUOXA2, SHOX2, UGT2B15, SVIL, B3GNT6, TRH, G6PC, MT1H, LDLRAD1, SERPINB9, CCL24, MLANA, ASCL3, A1BG, TNFRSF14, RASSF3, MS4A18, ROPN1L, COLEC10, MASP1, HAO1, KANSL1 [... truncated] Found more than one class "dist" in cache; using the first, from namespace 'spam' Also defined by ‘BiocGenerics’ Found more than one class "dist" in cache; using the first, from namespace 'spam' Also defined by ‘BiocGenerics’ Finding umap clusters Started cell-type annotation Predicting node: 1 Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'as.matrix': no slot of name "counts" for this object of class "Assay5"

ildralina commented 8 months ago

I am having the same problem and I have even tried converting the V5 object to earlier versions.

andreyurch commented 6 months ago

Dear developers, do you have any suggestions or this package is not maintaned anymore?

JackLMc commented 5 months ago

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?

JackLMc commented 5 months ago

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")