Moving data from Seurat to ScanPy
1
0
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.

library(Seurat)
library(loomR)
lfile <- as.loom(x=pbmc_small,filename="python/pbmc.loom")
lfile$close_all()

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/read.py in read_loom(filename, sparse, cleanup, X_name, obs_names, var_names, dtype, **kwargs)
    158 
    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
    161 
    162         layers = OrderedDict()

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/loom_layer.py 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=[self.name]):
    111                         if rows is not None:
    112                                 vals = view.layers[self.name][rows, :]

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/loompy.py 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], self.ca[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/graph_manager.py 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, g.data)

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

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

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/loompy/graph_manager.py 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/coo.py in __init__(self, arg1, shape, dtype, copy)
    196             self.data = self.data.astype(dtype, copy=False)
    197 
--> 198         self._check()
    199 
    200     def reshape(self, *args, **kwargs):

~/miniconda3/envs/ss-py/lib/python3.7/site-packages/scipy/sparse/coo.py 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
Seurat 3.1.2.9010

Python 3.7.6
scanpy 1.4.4.post1
seurat scanpy single-cell R python • 4.6k views
ADD COMMENT
0
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)

ADD COMMENT

Login before adding your answer.

Traffic: 2217 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