convert h5ad to rds
1
0
Entering edit mode
8 months ago

Does anyone knows how to convert an h5ad file into an .rds file using a python script?

single-cell transcriptome • 814 views
ADD COMMENT
0
Entering edit mode
8 months ago
bk11 ★ 3.0k

In python (Jupyter lab), do the following

import numpy as np
import pandas as pd
import scanpy as sc
import scipy
import os
import scipy.io as io

!mkdir matrix_files

adata = sc.read_h5ad('YourData.h5ad')
adata = adata.raw.to_adata()

with open('matrix_files/barcodes.tsv', 'w') as f:
    for item in adata.obs_names:
        f.write(item + '\n')

with open('matrix_files/features.tsv', 'w') as f:
    for item in ['\t'.join([x,x,'Gene Expression']) for x in adata.var_names]:
        f.write(item + '\n')

io.mmwrite('matrix_files/matrix', adata.X.T)
!ls matrix_files
!gzip matrix_files/*
!ls matrix_files
adata.obs.to_csv('metadata.csv')

And then in R do the following:

library(Seurat)

raw_data=Read10X("matrix_files")
metadata=read.csv("metadata.csv")
gc()
data_so=CreateSeuratObject(counts = raw_data, meta.data = metadata)
ADD COMMENT

Login before adding your answer.

Traffic: 3883 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6