11 Cell Annotation CosMx

11.1 Loading the reference —-

Now here you have to make sre that both refernce and your data set are normalised the same way. In this case it was the LogNormalise method.

hpca      <- qs::qread("./Wu_Swabrick_2021/swabrick_processed_seurat.qs")

11.1.1 Splitting the data (optional) —-

This was done because the HPCs could not handle the downstream integration. But this step is optional depending on your computing resources and the size of the data set.

obj_list <- SplitObject(merged, split.by = "Run_Tissue_name")

11.2 Seurat Integration —-

To my surprise, Seurats’ integration method worked well for this data set. However there are other popular methods such as SingleR which can be used for this, unfortunately did not work well for me in this case.

plot_dir <-"./plots/"
dir.create(plot_dir, showWarnings = FALSE)

# config
num_dims           <- 30

Note that here I use the maximum number of PC loadings as possible.

annotated_obj_list <- list()
for (obj_name in names(obj_list)) {
  obj <- obj_list[[obj_name]]
  hpca.anchors   <- FindTransferAnchors(reference = hpca, 
                                        query     = obj, 
                                        dims      = 1:num_dims,
                                        reference.reduction = "pca")
  predictions    <- TransferData(anchorset = hpca.anchors, 
                                 refdata   = hpca$celltype_major, 
                                 dims      = 1:num_dims)
  obj            <- AddMetaData(obj, metadata = predictions)
  
  qsave(predictions, paste0("swabrick_predictions_", obj_name, "_integration.qs"))
  annotated_obj_list[[obj_name]] <- obj
  
  # optionally save the split but annotated object
  # qsave(obj, paste0(obj_name, "_annot.qs"))
}

11.3 Plotting the cell annotations —-

Here because we have two spit objects and multiple cores within those slides, we will plot them invididually.

lapply(names(annotated_obj_list), function(x){
  library(RColorBrewer)
  # automate color selection
  colourCount  <- length(unique(obj_list[[x]]$predicted.id))
  getPalette   <- colorRampPalette(brewer.pal(7, "Dark2"))
  col_celltypes <- getPalette(colourCount)
  
  # or if you like to add manuak colours:
  # col_celltypes  <- c( "Cancer Epithelial" = "#00AFBB", 
  #                       "T-cells"  = "#E7B800", 
  #                       "Myeloid"  = "#FC4E07", 
  #                       "PVL"      = "green4", 
  #                       "B-cells"  = "grey", 
  #                       "CAFs"     = "#c2b280", #"lightyellow",
  #                       "Endothelial" = "violet",
  #                       "Plasmablasts" = "#0073C2FF",
  #                       "Normal Epithelial" =  "#E0115F")
  
  pdf(paste0(plot_dir, "celltypes_annotations_",x, ".pdf"), width = 17, height = 12)
  print(
    DimPlot(annotated_obj_list[[x]], group.by = "predicted.id", 
          pt.size = 1.5, label = TRUE, repel = TRUE,
          cols = col_celltypes,
          label.color = "black",
          label.box = TRUE, label.size = 12,
          raster=FALSE) +
      ggtitle(x)
  )
  dev.off()
  
  pdf(paste0(plot_dir, "celltypes_annotations_spatial_",x, ".pdf"), width = 17, height = 12)
  print(
    ImageDimPlot(obj, axes = TRUE, fov = x, group.by = "predicted.id", cols = "plasma") + 
    ggtitle(x) +
    coord_flip()
  )
  dev.off()
})