-
Notifications
You must be signed in to change notification settings - Fork 10
/
sc_readmtxfile.m
49 lines (48 loc) · 1.37 KB
/
sc_readmtxfile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function [X, genelist, celllist] = sc_readmtxfile(matrixmtxfile, featurestxtfile, barcodestxtfile, coln)
%Read MTX file
if nargin < 4, coln = 2; end
if nargin < 2, featurestxtfile = []; end
%[X,genelist,celllist]=sc_readmtxfile('matrix.mtx','features.tsv','barcodes.tsv')
if exist(matrixmtxfile, 'file') ~= 2
error(message('FileNotFound'));
end
%tic
fprintf('Reading mtx file %s...', matrixmtxfile);
X = pkg.mmread(matrixmtxfile);
X = pkg.e_uint2sparse(X);
% try
% X=full(X);
% catch
%
% end
fprintf('...done.\n');
if isempty(featurestxtfile)
genelist = [];
celllist = [];
return;
end
fprintf('Reading tsv file %s...', featurestxtfile);
T = readtable(featurestxtfile, 'ReadVariableNames', false, ...
'filetype', 'text', 'Delimiter', {'\t', ',', ' ', ';', '|'});
if coln == 1
genelist = string(T.Var1);
elseif coln == 2
try
genelist = string(T.Var2);
if sum(strlength(genelist) == 0) > 0.85 * length(genelist)
genelist = string(T.Var1);
end
catch
genelist = string(T.Var1);
end
end
fprintf('...done.\n');
if nargout > 2 && ~isempty(barcodestxtfile)
fprintf('Reading tsv file %s...', barcodestxtfile);
T = readtable(barcodestxtfile, 'FileType', 'text', 'Delimiter', '\n', 'ReadVariableNames', false);
celllist = string(T.Var1);
fprintf('...done.\n');
end
%assert(isequal(size(X,1),length(genelist)))
%toc
end