Plumber API how to upload a .RDS file? (Seurat object)

Dear team,

I have used shiny and now I want to transform the code into api so that I can call them on other computers. However, I got stuck on the first step because when I upload the .RDS file I could not make it into a seurat object (when I print out the name of req, there is nothing but only { } )that I can do further visualization. Can anyone help? Thanks! The code is below.

When I try /upload_files, I get: {
"error": "500 - Internal server error",
"message": "Error in readRDS(file = file, ...): bad 'file' argument\n"
}

library(plumber)

#* @apiTitle Plumber scRNA-seq api

# plumber.R

  library(SingleCellExperiment)
  library(Seurat)

uploaded_file <- NULL

#* @param f:[file]
#* @post /upload_files
function(req) {
  uploaded_file <<- LoadSeuratRds(req$files$f)
  return(list(names = names(uploaded_file), file = uploaded_file))
}

#* testing
#* @get /qcplot
#* @serializer png
qcplot <- function(){
  seurat_obj <- uploaded_file
  if(is.null(seurat_obj)){
    stop("No Seurat object uploaded", call. = FALSE)
  }
  seurat_obj[["percent.mt"]] <- PercentageFeatureSet(seurat_obj, pattern = "^MT-")
  
  d <- VlnPlot(seurat_obj, features = c("nFeature_RNA", "percent.mt"), ncol = 2, pt.size = 0.1)
  plot_ly_obj <- ggplotly(d)
  return(plot_ly_obj)
}

Something like this?

# plumber.R
library(plumber)
library(SingleCellExperiment)
library(Seurat)

parser_seurat_rds <- function(...) {  parser_read_file(function(tmpfile) { LoadSeuratRds(tmpfile, ...) })}
register_parser("rds",     parser_seurat_rds ,     fixed = "application/rds")

#* @apiTitle Plumber scRNA-seq api

uploaded_file <- NULL

#* @param f:file
#* @parser multi
#* @parser rds
#* @post /upload_files
function(f) {
  uploaded_file <<- f[[1]]
  return(f)
}

#* testing
#* @get /qcplot
#* @serializer png
qcplot <- function(){
  seurat_obj <- uploaded_file
  if(is.null(seurat_obj)){
    stop("No Seurat object uploaded", call. = FALSE)
  }
  seurat_obj[["percent.mt"]] <- PercentageFeatureSet(seurat_obj, pattern = "^MT-")
  
  d <- VlnPlot(seurat_obj, features = c("nFeature_RNA", "percent.mt"), ncol = 2, pt.size = 0.1)
  plot_ly_obj <- ggplotly(d)
  return(plot_ly_obj)
}

Hi Thank you for your reply! But it still doesn't work... It says "An exception occurred" :frowning:

Probably because it's not able to serialize the Seurat object into a json with the default serializer. Using serializer print it seems to work.

library(plumber)
library(SingleCellExperiment)
library(Seurat)

parser_seurat_rds <- function(...) {
  parser_read_file(function(tmpfile) { LoadSeuratRds(tmpfile, ...) })
}

register_parser("rds", parser_seurat_rds, fixed = "application/rds")

#* @apiTitle Plumber scRNA-seq api
uploaded_file <- NULL

#* @param f:file
#* @parser multi
#* @parser rds
#* @post /upload_files
#* @serializer print
function(f) {
  uploaded_file <<- f[[1]]
  f
}

#* testing
#* @get /qcplot
#* @serializer png
qcplot <- function(){
  seurat_obj <- uploaded_file
  if(is.null(seurat_obj)){
    stop("No Seurat object uploaded", call. = FALSE)
  }
  seurat_obj[["percent.mt"]] <- PercentageFeatureSet(seurat_obj, pattern = "^MT-")
  
  d <- VlnPlot(seurat_obj, features = c("nFeature_RNA", "percent.mt"), ncol = 2, pt.size = 0.1)
  plot_ly_obj <- ggplotly(d)
  return(plot_ly_obj)
}
1 Like

Yes it works! Thanks!
Response body:
$counts.RDS
An object of class Seurat
13714 features across 2700 samples within 1 assay
Active assay: RNA (13714 features, 0 variable features)
1 layer present: counts

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.