-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsne_on_mouse_genes_selection_perplexity_sweep_3D.m
176 lines (149 loc) · 5.16 KB
/
tsne_on_mouse_genes_selection_perplexity_sweep_3D.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
%% Perform Barnes-Hut t-SNE on mouse gene data
clear variables
close all
addpath('../bh-tsne');
% perplexity_vec = fliplr(2.^(9:10));
perplexity_vec = 256;
np = length(perplexity_vec);
initial_dims = [];
%% Load data (currently for a single brain)
outdir = 'results_mouse_genes_perplexity_sweep_th0p2/';
datadir = '/home/mvandegiessen/data/tSNE_ABA/AllenMouseBrain_coronal/';
expressionlevels = load([datadir 'expressionMatrix']);
ngenes = size(expressionlevels.expressionMatrix,2);
colors = load([datadir 'voxels']);
%% Load gene selection
selection = load([datadir 'highConfGenesInd_coronal']);
% Load gene coordinates
coordsdata = load([datadir 'voxels']);
[cx,cy,cz] = ind2sub([67 41 58],coordsdata.voxel.brain_idx);
coords = [cx cy cz];
%% Perform t-SNE
for pnr = 1:np
X = expressionlevels.expressionMatrix;
% selvec = randperm(size(X,1));
sampleselvec = 1:size(X,1);
geneselvec = selection.highConfGenes_coronal;
X = X(sampleselvec,geneselvec);
% Normalize
X = zscore(X);
coords = coords(sampleselvec,:);
theta = 0.2;
perplexity = perplexity_vec(pnr);
fprintf('Perplexity %d (%d/%d)\n',perplexity,pnr,np);
mappedX = fast_tsne(X,3,initial_dims,perplexity,theta);
%
% minx = min(mappedX);
% maxx = max(mappedX);
% maxval = max([abs(minx) abs(maxx)]);
% mappedab = mappedX*127/maxval;
% tC = makecform('lab2srgb');
% mappedRGB = applycform([50*ones(size(mappedab,1),1) mappedab],tC);
mappedRGB = (mappedX-min(mappedX(:)))/(max(mappedX(:))-min(mappedX(:)));
%% Save mapped data
save([outdir sprintf('MappedMouseGenesPerplexitySweep3Dperp%d',perplexity)],'X','mappedX','coords',...
'initial_dims','perplexity','theta','sampleselvec','geneselvec','mappedRGB');
% %% Visualize mapped data
% % Colorspace
% crange = max(coords)-min(coords);
% mincoords = min(coords);
% nsamples = size(X,1);
%
% %
% figure(1)
% clf
% hold on
% if size(mappedX,2) == 2
% for snr = 1:nsamples
% c = (coords(snr,:)-mincoords)./crange;
% plot(mappedX(snr,1),mappedX(snr,2),'.','Color',c);
% end
% else
% for snr = 1:nsamples
% c = (coords(snr,:)-mincoords)./crange;
% plot3(mappedX(snr,1),mappedX(snr,2),mappedX(snr,3),'.','Color',c);
% end
% view(3)
% axis equal
% end
%
% saveas(1,'MappedMouseGeneCoordsSelection2D300');
% saveas(1,'MappedMouseGeneCoordsSelection2D300.png');
% %% Visualize mapped data based on t-SNE coordinates
% minx = min(mappedX);
% maxx = max(mappedX);
% maxval = max([abs(minx) abs(maxx)]);
% mappedab = mappedX*127/maxval;
% tC = makecform('lab2srgb');
% mappedRGB = applycform([75*ones(size(mappedab,1),1) mappedab],tC);
%
% figure(2)
% clf
% hold on
% for snr = 1:nsamples
% plot3(coords(snr,1),coords(snr,2),coords(snr,3),'.','Color',mappedRGB(snr,:));
% end
% view(3)
% axis equal
%
% saveas(2,'MappedMouseGenesSelection2D300');
% saveas(2,'MappedMouseGenesSelection2D300.png');
%% Visualize mapped data based on t-SNE coordinates
nsamples = size(mappedX,1);
figure(2)
clf
hold on
for snr = 1:nsamples
if ~isempty(colors.voxel.color_HEX{snr})
colorRGB = rgbconv(colors.voxel.color_HEX{snr});
else
colorRGB = [0 0 0];
end
plot3(mappedX(snr,1),mappedX(snr,2),mappedX(snr,3),'.','Color',colorRGB);
end
view(3)
axis equal
saveas(3,[outdir sprintf('MappedMousePerplexitySweep3Dperp%d',perplexity)]);
saveas(3,[outdir sprintf('MappedMousePerplexitySweep3Dperp%d.png',perplexity)]);
%% Visualize mapped data based on t-SNE coordinates
nsamples = size(mappedX,1);
figure(3)
clf
hold on
cm = jet(67);
for snr = 1:nsamples
plot3(mappedX(snr,1),mappedX(snr,2),mappedX(snr,3),'.','Color',cm(coords(snr,1),:));
end
view(3)
axis equal
saveas(3,[outdir sprintf('MappedMousePerplexitySweepSlice3Dperp%d',perplexity)]);
saveas(3,[outdir sprintf('MappedMousePerplexitySweepSlice3Dperp%d.png',perplexity)]);
%% Image volume
isz = [67 41 58];
imgr = zeros(isz);
imgg = zeros(isz);
imgb = zeros(isz);
ind = sub2ind(isz,coords(:,1),coords(:,2),coords(:,3));
imgr(ind) = mappedRGB(:,1);
imgg(ind) = mappedRGB(:,2);
imgb(ind) = mappedRGB(:,3);
save([outdir sprintf('MappedMouseGeneVolumePerplexitySweep3Dperp%d',perplexity)],'imgr','imgg','imgb');
%% Image volume
isz = [67 41 58];
imgr = zeros(isz);
imgg = zeros(isz);
imgb = zeros(isz);
colorRGB = zeros(size(mappedRGB));
for snr = 1:nsamples
if ~isempty(colors.voxel.color_HEX{snr})
colorRGB(snr,:) = rgbconv(colors.voxel.color_HEX{snr});
else
colorRGB(snr,:) = [0 0 0];
end
end
ind = sub2ind(isz,coords(:,1),coords(:,2),coords(:,3));
imgr(ind) = colorRGB(:,1);
imgg(ind) = colorRGB(:,2);
imgb(ind) = colorRGB(:,3);
save([outdir 'MappedMouseGeneVolumePerplexitySweepRegions3D'],'imgr','imgg','imgb');
end