Convert h5ad anndata to a Seurat single-cell R object

Sdílet
Vložit
  • čas přidán 5. 09. 2024
  • How do you convert a python h5ad to a seurat object that you can open in R? There are multiple ways, but I have found the method here to be the most consistent and reliable. I convert the anndata object back into a raw format that mimics 10x output then open it normally in Seurat.
    Notebook:
    github.com/mou...

Komentáře • 47

  • @ylg9025
    @ylg9025 Před měsícem +1

    have been searching this for days! you are my life saver!

  • @valilali7084
    @valilali7084 Před 5 měsíci

    Hi thanks for the tutorial! Is there also a way to keep the workflow that was done (preprocessing, normalization, dimensionality reduction and clustering)? Or do you always have to redo this once you create a Seurat Object?

  • @karlacatacora235
    @karlacatacora235 Před rokem

    Omg finally a clear and easy to understand answer !! thanks :)

    • @sanbomics
      @sanbomics  Před rokem

      Glad it helped!

    • @sanbomics
      @sanbomics  Před rokem

      Although the best solution is just to do your analysis in Python xD

  • @scott12
    @scott12 Před rokem +1

    This is exactly what I need! thank you dude, you saved my day :D

    • @sanbomics
      @sanbomics  Před rokem

      No problem! All existing methods seem to have some sort of issue and/or overly complicated

  • @ilyasimutin
    @ilyasimutin Před 2 lety +2

    I am wondering if it's possible to include in the metadata the scores for scvi and umap (stored in the .obsm slot)? And then run the umap plots (and clustering) in seurat on the same latent space coordinates? Thanks again!

    • @sanbomics
      @sanbomics  Před 2 lety +1

      Hmm I haven't tried it... but that is an interesting question. I bet there is a way. It might be easier to run umap in scanpy then save the x,y coordinates as a csv and save it to the seurat UMAP slot.

    • @ilyasimutin
      @ilyasimutin Před 2 lety

      @@sanbomics yes, exactly. That's what I am thinking about. How to add these coordinates to metadata csv and then plot in Seurat...

    • @takhoa5302
      @takhoa5302 Před rokem +1

      Hi llia Simutin, I have a same question like yours. Have you solved it? Thank you

  • @marwanmohamed3844
    @marwanmohamed3844 Před rokem

    please keep going and keep posting vidios u are amazing, thanks alot
    it would be great to descripe how to convert from seurat to scanpy because it seems impossible to me to do it

    • @sanbomics
      @sanbomics  Před rokem +1

      Thank you! Hmm, may be easiest way if it is the raw data would just to be to save it as a csv then open it in scanpy with a .T to transpose

  • @sarahbonnin3410
    @sarahbonnin3410 Před rokem

    Thank you, that was very useful!

  • @user-zg1gj6oc4j
    @user-zg1gj6oc4j Před rokem

    I got the same problem .read10x get an error Error in scan(file, nmax = 1, what = what, quiet = TRUE, ...) :scan() expected 'an integer', got '0.00000000e+00'. I found that after 'sc.pp.scale' adata.X will convert to array , rather than the oridinary sparse matrix

    • @sanbomics
      @sanbomics  Před rokem

      It might be better to break it down and sending it over via rpy2 and anndata2ri. This is my preferred method now

  • @dhkwnr97
    @dhkwnr97 Před rokem

    What a really nice tip!

  • @MrMadcaw
    @MrMadcaw Před rokem

    When I try to read the files in R using read10x i always get an error Error in scan(file, nmax = 1, what = what, quiet = TRUE, ...) :scan() expected 'an integer', got '0.00000000e+00'. what can I do?

    • @sanbomics
      @sanbomics  Před rokem

      hmm, you can try converting the matrix to int. Something like astype(int). Before saving it. I've never had this issue though.

  • @bioinfo3
    @bioinfo3 Před rokem

    Could you please do the same for converting a R object (Seurat or SingleCellExperiment) to a h5ad anndata object? The tools out there are not good at doing this smoothly (Ive tried a lot of them!).

    • @sanbomics
      @sanbomics  Před rokem

      save the counts and metadata as a csv if the dataset isn't too large and just read it into scanpy like that

    • @sanbomics
      @sanbomics  Před rokem

      but transpose the counts

    • @bioinfo3
      @bioinfo3 Před rokem

      @@sanbomics with scanpy "scanpy.read_csv()" ? can both df's be read in in 1 function call?

  • @ilyasimutin
    @ilyasimutin Před 2 lety

    Great, thank you!

  • @hadijatmakinde1643
    @hadijatmakinde1643 Před rokem

    This was very helpful! Thank you so much.
    For some reason, I am stuck at the -io.mmwrite('matrix_files/matrix', adata.X.T)- step. My kernel just says busy. I have restarted the kernel but no luck.
    Any tips?

    • @hadijatmakinde1643
      @hadijatmakinde1643 Před rokem

      Nevermind, it was just such a large file that it took a while. Thank you again for this helpful tutorial.

    • @sanbomics
      @sanbomics  Před rokem +2

      No problem! Haha, yeah that happens to me sometimes too: "is it broken or is it just taking forever?"

  • @user-hl8cb6qw2c
    @user-hl8cb6qw2c Před rokem

    is there a way to do this but using the normalized matrix? or integrated matrix? instead of the raw

    • @sanbomics
      @sanbomics  Před rokem

      Yes, this video is a little outdated now. You can use something like anndata2ri and rpy2 to pass the components to R and construct them into a seurat object

    • @stefisjustthebest
      @stefisjustthebest Před rokem

      Could you do a video on this?! Ive so far managed to get the h5ad file into a SCE using anndata2ri...

  • @oliviaringham8706
    @oliviaringham8706 Před rokem

    Is there a way to convert an RDS to an h5ad object? been trying to do this without good results

    • @sanbomics
      @sanbomics  Před rokem

      Yeah "theoretically" there is. But when I go to use seuratDisk or any of those things I always have issues. I think the best way is to do something similar to this where you export the underlying data. Scanpy can read even a csv table that is genes x cells and then you can read in metadata directly as a pandas dataframe into the obs slot. I've never tried it though

  • @Reza_Ghamsari
    @Reza_Ghamsari Před rokem

    Do you have any suggestion how I can convert multiome .h5ad to a Seurat object? Thank you

    • @sanbomics
      @sanbomics  Před rokem +1

      I haven't done it, but it might involve breaking it down and sending it over via rpy2 and anndata2ri
      This video is a little outdated per how i convert now using the above steps. If you want an example: check out my most recent video (ambient rna) I break something down and send it to R from python

    • @Reza_Ghamsari
      @Reza_Ghamsari Před rokem

      @@sanbomics Thank you.

  • @wangmengfei5462
    @wangmengfei5462 Před rokem

    Thank you for your excellent work!
    I was stopped at :
    io.mmwrite('matrix_files/matrix', adata.X.T)
    The error message is :
    AttributeError: Module 'scipy' has no attribute 'mmwrite'.
    I changed the version as :python=3.9, scipy=1.11.1, numpy=1.24.
    But it didn't work. Could you please give me some help?
    Thank you for your work and help.

    • @wangmengfei5462
      @wangmengfei5462 Před rokem

      Finally it works. I think it's not because of the version, but the deficiency of package "scvelo".
      I found another similar video, and use:
      import numpy as np
      import pandas as pd
      import matplotlib.pyplot as pl
      import scanpy as sc
      import igraph
      import scvelo as scy
      import loompy as lmp
      import anndata
      from scipy import io
      from scipy.sparse import coo_matrix,csr_matrix
      import os
      After this step, it remindered me to download package "scvelo". And then, it works well.
      Actually, I still don't kwon why it didn't work, and how could I find the right way to work it out. :)

    • @sanbomics
      @sanbomics  Před rokem

      Great, glad you were able to figure it out! Sometimes things just don't make sense xD

  • @anaburgos4969
    @anaburgos4969 Před rokem

    Thank you :D

    • @sanbomics
      @sanbomics  Před rokem

      No problem! I know its sad when we have to do something in R instead of python.. but it happens sometimes xD

  • @68ai
    @68ai Před rokem

    I am new to scRNAseq and your videos are great. I am tasked with converting h5ad to Seurat and this video is very helpful. I was able to reproduce the example with the PBMC dataset from h5ad to Seurat object. But when I try it on a dataset from a publication, after I create the barcodes, features, and matrix file, the Seurat::Read10X returns a error:
    Error in scan(file, nmax = 1, what = what, quiet = TRUE, ...) :
    scan() expected 'an integer', got '0.00000000e+00'
    I know you are not a Seurat developer, but do you have any ideas why? As far as I can tell the matrix.mtx.gz isn't corrupted
    Thanks for any input .

    • @sanbomics
      @sanbomics  Před rokem

      Were the data the raw counts or normalized counts?

    • @68ai
      @68ai Před rokem

      @@sanbomics raw counts ...

    • @68ai
      @68ai Před rokem

      When I look at the matrix.mtx from the PBMC example I see the format as:
      %%MatrixMarket matrix coordinate real general
      %
      13714 2638 2238732
      30 1 1.6358733e+00
      74 1 1.6358733e+00
      81 1 2.2265546e+00
      But the matrix.mtx file I generated in the same manner with my dataset of interest is :
      %%MatrixMarket matrix array real general
      %
      27341 599926
      0.00000000e+00
      0.00000000e+00
      0.00000000e+00
      I"m pretty sure this why Seurat is throwing an error, but I can't figure out why my file is formatted like that.