Hi,
Maybe this is somewhere in the manual and I just don't see it. But is there a way to read multiple 10X samples (either multiple .h5 or the matrix/genes/barcodes) in the same way that Seurat does with its Read10X() function?
I don't know how they do it Seurat, but I'd simply do
filenames = ['name0.h5', 'name1.h5', 'name2.h5']
adatas = [sc.read_10x_h5(filename) for filename in filenames]
adata = adatas[0].concatenate(adatas[1:])
Does this help?
Hi, thanks for the reply.
This example helps already. Thanks. I was thinking more about importing multiple samples from 10X where for each sample you have a folder containing the three files (matrix, barcodes, genes). But I guess I can do something to convert those into .h5 prior to read them into scanpy.
You can do the same as above using sc.read_10x_mtx, which is not in a release yet but on GitHub's Master branch. In .concatenate() you have the option to pass how you want to name your batches/samples by passing batch_categories.
PS: Note that I edited the example above to show sc.read_10x_h5.
Many thanks!!!
Hi falexwolf,
I try to use concatenate to read multiple 10X mtx and put them together.
But it seems like if I concatenate more than 15 mtx(already stored and read from cache), it becomes very slow. Do you have any advice?
Thanks for any information you may provide.
Hi @falexwolf, thanks for the solution you provided above for reading multiple files. I tried it and it worked when I had just 2 files. I am trying the same code with 23 files and I am getting an error message in the concatenation step. Any idea on how to fix this ? Thanks.
AttributeError Traceback (most recent call last)
12 adatas.obs['cell_names'] = pd.read_csv(path + sample + 'barcodes.tsv.gz', header=None)[0].values
13
---> 14 adata = adatas[0].concatenate(adatas[1:])
/Applications/anaconda3/lib/python3.7/site-packages/anndata/core/anndata.py in concatenate(self, join, batch_key, batch_categories, index_unique, *adatas)
1908
1909 if any_sparse:
-> 1910 sparse_format = all_adatas[0].X.getformat()
1911 X = X.asformat(sparse_format)
1912
AttributeError: 'numpy.ndarray' object has no attribute 'getformat'
Hi @elfore, were you able to concatenate your files successfully ? If yes, could you please share the code you used for concatenation ? Thanks.
If I do this : adata = adata1.concatenate (adata2, adata3). How can I keep the original sample names in adata? Thx!
@taopeng1100, this should work:
adata = adata1.concatenate(adata2, adata3, index_unique=None)
Most helpful comment
I don't know how they do it Seurat, but I'd simply do
Does this help?