seurat-disk icon indicating copy to clipboard operation
seurat-disk copied to clipboard

Creating a H5Seurat object error

Open CeciPR opened this issue 1 year ago • 7 comments

Dear all,

I'm trying to create a h5Seurat object, however, the data that comes out is corrupt.

I updated my datset: sc <- UpdateSeuratObject(sc) Validating object structure Updating object slots Ensuring keys are in the proper structure Updating matrix keys for DimReduc ‘pca’ Updating matrix keys for DimReduc ‘umap’ Ensuring keys are in the proper structure Ensuring feature names don't have underscores or pipes Updating slots in RNA Updating slots in RNA_nn Setting default assay of RNA_nn to RNA Updating slots in RNA_snn Setting default assay of RNA_snn to RNA Updating slots in pca Updating slots in umap Setting umap DimReduc to global Setting assay used for NormalizeData.RNA to RNA Setting assay used for FindVariableFeatures.RNA to RNA Setting assay used for ScaleData.RNA to RNA Setting assay used for RunPCA.RNA to RNA Setting assay used for FindNeighbors.RNA.pca to RNA No assay information could be found for FindClusters Setting assay used for RunUMAP.RNA.pca to RNA Validating object structure for Assay5 ‘RNA’ Validating object structure for Graph ‘RNA_nn’ Validating object structure for Graph ‘RNA_snn’ Validating object structure for DimReduc ‘pca’ Validating object structure for DimReduc ‘umap’ Object representation is consistent with the most current Seurat version Warning message: Adding a command log without an assay associated with it

An object of class Seurat 25567 features across 88261 samples within 1 assay Active assay: RNA (25567 features, 2000 variable features) 3 layers present: counts, data, scale.data 2 dimensional reductions calculated: pca, umap

Seems right.

then:

SaveH5Seurat(sc, overwrite = TRUE) Creating h5Seurat file for version 3.1.5.9900 Adding cell embeddings for pca Adding loadings for pca No projected loadings for pca Adding standard deviations for pca No JackStraw data for pca Adding cell embeddings for umap No loadings for umap No projected loadings for umap No standard deviations for umap No JackStraw data for umap

But when I try to load my h5Seurat data I get this message: Validating h5Seurat file Error: Call assays must have either a 'counts' or 'data' slot, missing for RNA

What does it mean? Can you help me on that?

CeciPR avatar Dec 19 '23 10:12 CeciPR

Validating h5Seurat file Error: Call assays must have either a 'counts' or 'data' slot, missing for RNA

And I'm sure I have this info in my dataset...

CeciPR avatar Dec 21 '23 09:12 CeciPR

Maybe you can try

seob[["RNA3"]] <- as(object = seob[["RNA"]], Class = "Assay")

jmzhang1911 avatar Dec 24 '23 02:12 jmzhang1911

Validating h5Seurat file Adding data from RNA as X Error in assay.group$obj_copy_to(dst_loc = dfile, dst_name = "X", src_name = x.data) : HDF5-API Errors: error #000: H5Ocopy.c in H5Ocopy(): line 233: unable to copy object class: HDF5 major: Object header minor: Unable to copy object

did not work :/

CeciPR avatar Dec 27 '23 10:12 CeciPR

They have changed the structure of the Seurat object with sc@assays$RNA@layers$counts rather than sc@assays$RNA@counts, so the save isn't recognising this difference when saving.

Is it possible to have a version with the 'layers' bit added to the save and load Seurat Object?

KerryAM-R avatar Jan 03 '24 15:01 KerryAM-R

oh! Thank you!

can I assign like this:

sc["count"]<-as(object[["RNA"]],Class="Assay"

On Wed, Jan 3, 2024 at 4:26 PM KerryAM-R @.***> wrote:

They have changed the structure of the Seurat object with @.*** @.$counts rather than @.@.***, so the save isn't recognising this difference when saving.

Is it possible to have a version with the 'layers' bit added to the save and load Seurat Object?

— Reply to this email directly, view it on GitHub https://github.com/mojaveazure/seurat-disk/issues/172#issuecomment-1875547769, or unsubscribe https://github.com/notifications/unsubscribe-auth/BEZNLDMKJ7GARFCTAYQJIB3YMV2B3AVCNFSM6AAAAABA26PPN6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZVGU2DONZWHE . You are receiving this because you authored the thread.Message ID: @.***>

-- Cecilia Pessoa Rodrigues PhD

CeciPR avatar Jan 04 '24 08:01 CeciPR

They have changed the structure of the Seurat object with sc@assays$RNA@layers$counts rather than sc@assays$RNA@counts, so the save isn't recognising this difference when saving.

Is it possible to have a version with the 'layers' bit added to the save and load Seurat Object?

It is possible to "patch" each of SaveH5Seurat, LoadH5Seurat functions (tested with Seurat v5.0.1).

Here it is for SaveH5Seurat:

#' Patch of SeuratDisk::SaveH5Seurat function
#'
#' The "Assay5" class attribute "RNA" needs to be converted to a standard "Assay"
#' class for compatibility with SeuratDisk. It requires to make a temporary copy
#' so the size of the object grows bigger.
#'
#' @param object the Seurat object
#' @param filename the file path where to save the Seurat object
#' @param verbose SaveH5Seurat verbosity
#' @param overwrite whether to overwrite an existing file
#' 
#' @return NULL
SaveH5SeuratObject <- function(
    object,
    filename,
    verbose = TRUE,
    overwrite = TRUE
    ) {
  
  # add copy of "RNA" 
  object[["RNA-tmp"]] <- CreateAssayObject(counts = object[["RNA"]]$counts)
  # remove original
  object[["RNA"]] <- NULL
  # export
  SaveH5Seurat(object, filename = filename, overwrite, verbose)
  
  return(NULL)
}

And for LoadH5Seurat:

#' Patch of SeuratDisk::LoadH5Seurat function
#'
#' The "Assay" class attribute "RNA" needs to be converted to the new "Assay5"
#' class. It requires to make a temporary copy so the size of the object grows
#' bigger.
#'
#' @param filename the file path where to save the Seurat object
#' @param verbose LoadH5Seurat verbosity
#' 
#' @return NULL
LoadH5SeuratObject <- function(
    filename,
    verbose = TRUE
) {
  
  # load h5 data
  object <- LoadH5Seurat(filename)
  # create "Assay5" class from old "Assay" class
  keys <- Key(object)
  slotID <- names(keys)[startsWith(keys, "rna")]
  object[["RNA"]] <- CreateAssay5Object(counts = object[[slotID]]$counts)
  # delete "Assay" class
  object[[slotID]] <- NULL
  # reorder assays list
  object@assays <- object@assays[sort(names(object@assays))]

  return(object)
}

The biggest caveat is that it requires to make a copy of the RNA assay at each pass (saving or loading). A direct conversion would be preferable, but I have not found such a function in the SeuratObject package.

EDIT: A better function would iterate over all assays and do what I did for RNA for each "Assay5" class assay.

Gilquin avatar Feb 07 '24 10:02 Gilquin

I have an object saved as h5Seurat and get the same error. What can I do to load it into R under Seurat5? I need to open it in order to save it with the function mentioned before... Any suggestions? I saved it with the normal SaveH5Seurat and there was no problem.

sample.tumor <- LoadH5Seurat("/home/jovyan/sampletumor.h5Seurat") Validating h5Seurat file Error: Call assays must have either a 'counts' or 'data' slot, missing for RNA

vfincke avatar Apr 22 '24 10:04 vfincke