@@ -84,20 +84,20 @@ def read_umi_tools(filename: Union[Path, str]) -> AnnData:
84
84
# import gzip to read a gzipped file :-)
85
85
import gzip
86
86
from pandas import DataFrame
87
-
87
+
88
88
dod = {} # this will contain basically everything
89
89
fh = gzip .open (filename )
90
90
header = fh .readline () # read the first line
91
-
91
+
92
92
for line in fh :
93
93
t = line .decode ('ascii' ).split ('\t ' ) # gzip read bytes, hence the decoding
94
94
try :
95
95
dod [t [1 ]].update ({t [0 ]:int (t [2 ])})
96
96
except KeyError :
97
97
dod [t [1 ]] = {t [0 ]:int (t [2 ])}
98
-
98
+
99
99
df = DataFrame .from_dict (dod , orient = 'index' ) # build the matrix
100
- df .fillna (value = 0. , inplace = True ) # many NaN, replace with zeros
100
+ df .fillna (value = 0. , inplace = True ) # many NaN, replace with zeros
101
101
return AnnData (np .array (df ), {'obs_names' : df .index }, {'var_names' : df .columns })
102
102
103
103
@@ -138,28 +138,39 @@ def read_hdf(filename: Union[Path, str], key: str) -> AnnData:
138
138
return adata
139
139
140
140
141
- def read_loom (filename : Union [Path , str ]) -> AnnData :
141
+ def read_loom (filename : Union [Path , str ], sparse = False ) -> AnnData :
142
142
"""Read `.loom`-formatted hdf5 file.
143
143
144
+ This reads the whole file into memory.
145
+
146
+ Beware that you have to explicitly state when you want to read the file as
147
+ sparse data.
148
+
144
149
Parameters
145
150
----------
146
151
filename : `str`
147
152
The filename.
153
+ sparse : `bool`
154
+ Whether to read the data matrix as sparse.
148
155
149
156
Returns
150
157
-------
151
158
An :class:`~anndata.AnnData` object.
152
159
"""
153
160
filename = str (filename ) # allow passing pathlib.Path objects
154
161
from loompy import connect
155
- lc = connect (filename , 'r' )
156
- with h5py .File (filename , 'r' ) as f :
157
- X = f ['matrix' ][()]
158
- adata = AnnData (
159
- X .T ,
160
- obs = dict (lc .col_attrs ), # not ideal: make the generator a dict...
161
- var = dict (lc .row_attrs ))
162
- lc .close ()
162
+ if sparse :
163
+ with connect (filename , 'r' ) as lc :
164
+ X = lc .sparse ()
165
+ else :
166
+ with h5py .File (filename , 'r' ) as f :
167
+ X = f ['matrix' ][()]
168
+ with connect (filename , 'r' ) as lc :
169
+ adata = AnnData (
170
+ X .T ,
171
+ obs = dict (lc .col_attrs ), # not ideal: make the generator a dict...
172
+ var = dict (lc .row_attrs ))
173
+ lc .close ()
163
174
return adata
164
175
165
176
0 commit comments