From db905235cfb2d2335ef76df2eba18bfd420c0794 Mon Sep 17 00:00:00 2001 From: Volker Bergen <31883718+VolkerBergen@users.noreply.github.com> Date: Fri, 31 Aug 2018 14:47:31 +0200 Subject: [PATCH] update write_loom to write layers (#53) --- anndata/readwrite/write.py | 12 ++++++------ anndata/tests/layers.py | 14 +++++++++++++- requires_tests.txt | 4 +++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/anndata/readwrite/write.py b/anndata/readwrite/write.py index 5a86cdf5a..e20e18f12 100644 --- a/anndata/readwrite/write.py +++ b/anndata/readwrite/write.py @@ -71,15 +71,15 @@ def write_loom(filename: PathLike, adata: AnnData): row_attrs['var_names'] = adata.var_names.values col_attrs = adata.obs.to_dict('list') col_attrs['obs_names'] = adata.obs_names.values - X = adata.X.T - if issparse(X): - logg.info( - '... writing to \'.loom\' file densifies sparse matrix') - X = X.tocoo() + + layers = {'': adata.X.T} + for key in adata.layers.keys(): + layers[key] = adata.layers[key].T + from loompy import create if filename.exists(): filename.unlink() - create(fspath(filename), X, row_attrs=row_attrs, col_attrs=col_attrs) + create(fspath(filename), layers, row_attrs=row_attrs, col_attrs=col_attrs) # TODO: store can be a MutableMapping or a string - accept PathLike too? def write_zarr(store, adata: AnnData, **kwargs): diff --git a/anndata/tests/layers.py b/anndata/tests/layers.py index cda7882de..c03c5a594 100644 --- a/anndata/tests/layers.py +++ b/anndata/tests/layers.py @@ -1,3 +1,5 @@ +from importlib.util import find_spec +import pytest import numpy as np import anndata as ad import os @@ -38,7 +40,6 @@ def test_views(): def test_readwrite(): adata = ad.AnnData(X=X, layers={'L':L.copy()}) adata.write('test.h5ad') - adata_read = ad.read_h5ad('test.h5ad') assert adata.layers.keys() == adata_read.layers.keys() @@ -46,6 +47,17 @@ def test_readwrite(): os.remove('test.h5ad') +@pytest.mark.skipif(find_spec('loompy') is None, reason="loompy not installed") +def test_readwrite_loom(): + adata = ad.AnnData(X=X, layers={'L': L.copy()}) + adata.write_loom('test.loom') + adata_read = ad.read_loom('test.loom', X_name='') + + assert adata.layers.keys() == adata_read.layers.keys() + assert (adata.layers['L'] == adata_read.layers['L']).all() + + os.remove('test.loom') + def test_backed(): #backed mode for layers isn't implemented, layers stay in memory pass diff --git a/requires_tests.txt b/requires_tests.txt index 4c247ff91..c3bebe3d0 100644 --- a/requires_tests.txt +++ b/requires_tests.txt @@ -1,4 +1,6 @@ -loompy>=2.0; python_version>='3.6' +loompy>=2.0.12; python_version>='3.6' pytest-cov codecov zarr +matplotlib +sklearn \ No newline at end of file