Moving data from Seurat to ScanPy
Entering edit mode
2.3 years ago
rmf ★ 1.4k

I am trying to move data from Seurat to ScanPy. It seems like exporting to loom is one of the ways to do it.

In R, I am using an example dataset.

lfile <- as.loom(x=pbmc_small,filename="python/pbmc.loom")

In python, I read it in and it fails.

import scvelo as scv
pbmc = scv.read_loom("pbmc.loom")

ValueError                                Traceback (most recent call last)
<ipython-input-10-063344121bde> in <module>
----> 1 pbmc = scv.read_loom("pbmc.loom")

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/anndata/readwrite/ in read_loom(filename, sparse, cleanup, X_name, obs_names, var_names, dtype, **kwargs)
    159         if X_name not in lc.layers.keys(): X_name = ''
--> 160         X = lc.layers[X_name].sparse().T.tocsr() if sparse else lc.layers[X_name][()].T
    162         layers = OrderedDict()

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/ in sparse(self, rows, cols)
    108                 row: List[np.ndarray] = []
    109                 col: List[np.ndarray] = []
--> 110                 for (ix, selection, view) in self.ds.scan(items=cols, axis=1, layers=[]):
    111                         if rows is not None:
    112                                 vals = view.layers[][rows, :]

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/ in scan(self, items, axis, layers, key, batch_size)
    595                                 for key, layer in vals.items():
    596                                         lm[key] = loompy.MemoryLoomLayer(key, layer)
--> 597                                 view = loompy.LoomView(lm, self.ra[ordering],[ix + selection], self.row_graphs[ordering], self.col_graphs[ix + selection], filename=self.filename, file_attrs=self.attrs)
    598                                 yield (ix, ix + selection, view)
    599                                 ix += cols_per_chunk

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/ in __getitem__(self, thing)
     96                 if type(thing) is slice or type(thing) is np.ndarray or type(thing) is int:
     97                         gm = GraphManager(None, axis=self.axis)
---> 98                         for key, g in self.items():
     99                                 # Slice the graph matrix properly without making it dense
    100                                 (a, b, w) = (g.row, g.col,

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/ in items(self)
     55         def items(self) -> Iterable[Tuple[str, sparse.coo_matrix]]:
     56                 for key in self.keys():
---> 57                         yield (key, self[key])
     59         def __len__(self) -> int:

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/ in __getitem__(self, thing)
    116                         raise AttributeError(f"'{type(self)}' object has no attribute {thing}")
    117                 else:
--> 118                         return self.__getattr__(thing)
    120         def __getattr__(self, name: str) -> sparse.coo_matrix:

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/ in __getattr__(self, name)
    127                                 c = self.ds._file[a][name]["b"]
    128                                 w = self.ds._file[a][name]["w"]
--> 129                                 g = sparse.coo_matrix((w, (r, c)), shape=(self.ds.shape[self.axis], self.ds.shape[self.axis]))
    130                                 self.__dict__["storage"][name] = g
    131                         return g

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/scipy/sparse/ in __init__(self, arg1, shape, dtype, copy)
    196    =, copy=False)
--> 198         self._check()
    200     def reshape(self, *args, **kwargs):

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/scipy/sparse/ in _check(self)
    285                 raise ValueError('row index exceeds matrix dimensions')
    286             if self.col.max() >= self.shape[1]:
--> 287                 raise ValueError('column index exceeds matrix dimensions')
    288             if self.row.min() < 0:
    289                 raise ValueError('negative row index found')

ValueError: column index exceeds matrix dimensions

I don't know enough Python to figure out what's wrong. It already fails with the example dataset. With my actual data, the read in works, but fails in subsequent analysis steps.

Is there any method that works? Or can I build the anndata format in Python manually by reading in various tables separately?

R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Python 3.7.6
scanpy 1.4.4.post1
seurat scanpy single-cell R python • 4.6k views
Entering edit mode
2.3 years ago
khorms ▴ 210

Try reading the loom file with scanpy instead of scvelo - it works for me. Something like
import scanpy as sc
current_obj = sc.read_loom(input_filename)


Login before adding your answer.

Traffic: 2217 users visited in the last hour
Help About
Access RSS

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

Powered by the version 2.3.6