-
Notifications
You must be signed in to change notification settings - Fork 10
/
sc_read10xdir.m
135 lines (125 loc) · 3.74 KB
/
sc_read10xdir.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function [X, genelist, celllist, ftdone] = sc_read10xdir(selpath, coln)
%Read 10x folder
%Read files from a 10x Genomics cellranger output folder
%[X,genelist,celllist]=sc_read10xdir(selpath,coln);
%[X,genelist,celllist]=sc_read10xdir(pwd(),2);
if nargin < 2, coln = 2; end
if nargin < 1, selpath = uigetdir; end
% if isempty(selpath) || selpath==0 || ~isfolder(selpath)
% error('Need valide folder name.');
% end
fprintf('Processing %s...\n', selpath);
[out, aff] = i_guessmtxfile(selpath);
if isempty(out)
selpath = fullfile(selpath, 'filtered_feature_bc_matrix');
[out, aff] = i_guessmtxfile(selpath);
if ~isempty(out)
disp('Found folder ''filtered_feature_bc_matrix''');
end
end
if ~isempty(aff)
mmfname = fullfile(selpath, sprintf('%smatrix.mtx', aff));
zmmfname = fullfile(selpath, sprintf('%smatrix.mtx.gz', aff));
else
mmfname = fullfile(selpath, 'matrix.mtx');
zmmfname = fullfile(selpath, 'matrix.mtx.gz');
end
if ~exist(mmfname, 'file')
if ~exist(zmmfname, 'file')
error('[sc_read10xdir] No matrix.mtx file.');
else
[~, nametxt] = fileparts(zmmfname);
fprintf('Unzipping %s.gz...\n', nametxt);
gunzip(zmmfname);
end
end
ftdone = false;
if ~isempty(aff)
ftfname = fullfile(selpath, sprintf('%sfeatures.tsv', aff));
zftfname = fullfile(selpath, sprintf('%sfeatures.tsv.gz', aff));
else
ftfname = fullfile(selpath, 'features.tsv');
zftfname = fullfile(selpath, 'features.tsv.gz');
end
if ~exist(ftfname, 'file')
if ~exist(zftfname, 'file')
% error('No features.tsv file.');
ftdone = false;
else
[~, nametxt] = fileparts(zftfname);
fprintf('Unzipping %s.gz...\n', nametxt);
gunzip(zftfname);
ftdone = true;
end
else % ftfname exisiting
ftdone = true;
end
if ~ftdone
if ~isempty(aff)
ftfname = fullfile(selpath, sprintf('%sgenes.tsv', aff));
zftfname = fullfile(selpath, sprintf('%sgenes.tsv.gz', aff));
else
ftfname = fullfile(selpath, 'genes.tsv');
zftfname = fullfile(selpath, 'genes.tsv.gz');
end
if ~exist(ftfname, 'file')
if ~exist(zftfname, 'file')
error('No genes/features.tsv file.');
else
[~, nametxt] = fileparts(zftfname);
fprintf('Unzipping %s.gz...\n', nametxt);
gunzip(zftfname);
ftdone = true;
end
else
ftdone = true;
end
end
if ~isempty(aff)
bcfname = fullfile(selpath, sprintf('%sbarcodes.tsv', aff));
zbcfname = fullfile(selpath, sprintf('%sbarcodes.tsv.gz', aff));
else
bcfname = fullfile(selpath, 'barcodes.tsv');
zbcfname = fullfile(selpath, 'barcodes.tsv.gz');
end
if ~exist(bcfname, 'file')
if ~exist(zbcfname, 'file')
warning('No barcodes.tsv file.');
else
[~, nametxt] = fileparts(zbcfname);
fprintf('Unzipping %s.gz...\n', nametxt);
gunzip(zbcfname);
end
end
if ~exist(mmfname, 'file'), error('No matrix file'); end
if ~exist(ftfname, 'file'), error('No feature file'); end
fprintf('Reading matrix file...');
if exist(bcfname, 'file')
[X, genelist, celllist] = sc_readmtxfile(mmfname, ftfname, bcfname, coln);
else
[X, genelist] = sc_readmtxfile(mmfname, ftfname, [], coln);
celllist = [];
end
fprintf('done.\n');
if exist(zmmfname, 'file') && exist(mmfname, 'file')
delete(mmfname);
end
if exist(zftfname, 'file') && exist(ftfname, 'file')
delete(ftfname);
end
if exist(zbcfname, 'file') && exist(bcfname, 'file')
delete(bcfname);
end
end
function [out, aff] = i_guessmtxfile(selpath)
out = [];
aff = [];
a = dir(selpath);
for k = 1:length(a)
if contains(a(k).name, 'matrix.mtx')
out = a(k).name;
aff = extractBefore(out, 'matrix.mtx');
continue;
end
end
end