Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a H5Seurat object error #172

Open
CeciPR opened this issue Dec 19, 2023 · 7 comments
Open

Creating a H5Seurat object error #172

CeciPR opened this issue Dec 19, 2023 · 7 comments

Comments

@CeciPR
Copy link

CeciPR commented Dec 19, 2023

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
Copy link
Author

CeciPR commented Dec 21, 2023

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...

@jmzhang1911
Copy link

Maybe you can try

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

@CeciPR
Copy link
Author

CeciPR commented Dec 27, 2023

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 :/

@KerryAM-R
Copy link

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?

@CeciPR
Copy link
Author

CeciPR commented Jan 4, 2024 via email

@Gilquin
Copy link

Gilquin commented Feb 7, 2024

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.

@vfincke
Copy link

vfincke commented Apr 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants