2

I have processed a Seurat scRNAseq object with the CellTypist package (Jupyter Notebook) to annotate immune cell types. I managed to export the predicted cell labels as a CSV. I have read this into R and would like to merge the results as an Idents column in the Seurat object metadata.

However, when I use the AddMetaData function and view the merged object metadata, all of the new labels are listed as 'N/A' (they had the correct labels when I checked the csv). They share the exact same row labels as the original Seurat object, which is the cell identifier barcode. The headings from the csv have also transferred across correctly, as their own Idents columns. The two objects (the Seurat object and the csv) are also of the same length. Something seems to be going wrong when I merge them together.

The code I am using is this:

meta.data = read.csv("predicted_labels.csv")

Tum_July_new <- AddMetaData(object = Tum_July, metadata = meta.data)
Donald Seinen
  • 4,179
  • 5
  • 15
  • 40
clb96
  • 21
  • 3

3 Answers3

1
Hello, I encountered a similar problem when trying to read in scanpy (Jupyter Notebook) -preprocessed data, via AnnData > Seurat conversion, into R.
    # get data
    setwd("~/BLM_YOMm/coex_groups_out/")

    # .mtx file
    coex_obj <- ReadMtx(mtx = "matrix.mtx.gz",
                        features = "features.tsv.gz",
                        cells = "barcodes.tsv.gz")
    seurat_obj <- CreateSeuratObject(counts = seurat_obj)
    seurat_obj

    # Add metadata
    setwd("~/BLM_YOMm/")

    sc_cell_info <- read.csv("metadata.csv", header = T) 

   
When i tried the 'seurat_obj <- AddMetaData(object = seurat_obj, metadata = sc_cell_info)' approach, it returned NA values for the newly added metadata. Adapting Luca Mannino's approach (above), I did something as shown below, and it worked!
    # add Sample metadata to the seurat_obj[[]] slot, ONE by ONE!
                    seurat_obj[['Sample']] <- sc_cell_info$Sample[match(rownames(seurat_obj@meta.data), sc_cell_info$X)] 

    # add Day  
    seurat_obj[['Day']] <-sc_cell_info$Day[match(rownames(seurat_obj@meta.data), sc_cell_info$X)] 

    # add Age
    seurat_obj[['Age']] <-sc_cell_info$Age[match(rownames(seurat_obj@meta.data), sc_cell_info$X)] 

    # cell_type
    seurat_obj[['cell_type']] <-sc_cell_info$cell_type[match(rownames(seurat_obj@meta.data), sc_cell_info$X)] 

    # a look at the newly added metadata
    seurat_obj@meta.data
All the best, Qd
0

what does your meta.data look like? by "They share the exact same row labels" do you mean the rownames as cell id?

I used to get a similar error and I solved it by using:

rownames(meta.data) <- meta.data$whatever.column.has.the.cell.id

hope this helps good luck :)

0

My approach for that:

celltypist_predicted <- read.csv("predicted_labels.csv")

seuratOb[["PredictedLabels"]] <- celltypist_predicted$predicted_labels[match(rownames(seuratOb@meta.data), celltypist_predicted$X)]
MEC
  • 71
  • 1
  • 10