Skip to content

Convert error free from .rds to .h5ad (including the PCA and UMAP projections)

Notifications You must be signed in to change notification settings

ruchikabhat/Seurat-RDS-to-H5AD-Python-Scanpy-conversion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

This Script helps with generating files needed for .rds to h5ad conversion

#Date: 10 April 2024

#Time: 9:20 am

#Author: Dr. Ruchika Bhat

Assuming you have your final .rds object loaded in the seurat object named 'mouse'

mouse <-readRDS("FinalInputfile.rds")

Generate these files using:

counts_matrix <-GetAssayData(mouse, assay='RNA',slot='counts')

save matrix file using:

writeMM(counts_matrix, file=paste0(file='matrix.mtx'))

save pca embeddings using:

write.csv(mouse@[email protected],file='pca.csv', quote=F, row.names=F) write.table(data.frame('gene'=rownames(counts_matrix)),file='gene_names.csv',quote=F,row.names=F,col.names=F)

Save cell barcode information using:

mouse$barcode<-colnames(mouse)

Save UMAP reductions using:

mouse$UMAP_1<-mouse@[email protected][,1] mouse$UMAP_2<-mouse@[email protected][,2]

Finally save the metadata using:

write.csv([email protected],file='metadata.csv', quote=F,row.names=F)

#######################################################################################

Now go to Jupyter Notebook to convert it into h5ad with same embeddings information

Code Below

import pandas as pd
import matplotlib.pyplot as pl
import scanpy as sc
import igraph
import scvelo as scv
import loompy as lmp
import anndata
from scipy import io
from scipy.sparse import coo_matrix, csr_matrix
import os```


X= io.mmread("/data-store/iplant/home/ruchikabhat/data/CellOracle/matrix.mtx")

adata =anndata.AnnData(X=X.transpose().tocsr())

metadata = pd.read_csv("/data-store/iplant/home/ruchikabhat/data/CellOracle/metadata.csv")

with open("/data-store/iplant/home/ruchikabhat/data/CellOracle/gene_names.csv",'r') as f:
      gene_names = f.read().splitlines()

adata.obs = metadata
adata.obs.index =adata.obs['barcode']
adata.var.index = gene_names

pca =pd.read_csv("/data-store/iplant/home/ruchikabhat/data/CellOracle/pca.csv")
pca.index =adata.obs.index

adata.obsm['X_pca'] = pca.to_numpy()
adata.obsm['X_umap'] = np.vstack((adata.obs['UMAP_1'].to_numpy(), adata.obs['UMAP_2'].to_numpy())).T

sc.pl.umap(adata, color =['Clusters'],frameon=False, save=True)

# Save everything as a h5ad file

adata.write("/data-store/iplant/home/ruchikabhat/data/CellOracle/mouse.h5ad")

# To read this (h5ad) data back

adata=sc.read_h5ad("/data-store/iplant/home/ruchikabhat/data/CellOracle/mouse.h5ad")

###################################### DONE YAYYY! #######################################################

About

Convert error free from .rds to .h5ad (including the PCA and UMAP projections)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published