-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
475 lines (435 loc) · 15.8 KB
/
index.html
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
width: 100%;
height: 100%;
text-align: center;
}
div {
margin-top: 12px;
}
.content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.button3,
.button1,
.button2,
.button4 {
margin-left: 10px;
font-size: 13px;
border-radius: 6px;
background-color: #cce7fb;
}
input {
width: 50px;
border: 1px solid #95acd2;
border-radius: 15%;
text-align: center;
}
#file-input,
#file-input1,
#file-input2,
#file-input3,
#file-input4 {
display: none;
}
table {
visibility: hidden;
}
.generate {
margin-top: 20px;
font-size: 13px;
}
</style>
</head>
<body>
<h1>SpatialCTD Converter Tool</h1>
<div class="content">
<div>
<span>Coordinates of single-cells local in FOV file</span>
<label class="button1">
<input type="file" id="file-input1" value="Upload">
Upload
</label>
</div>
<div>
<span>Coordinates of FOVs file</span>
<label class="button2">
<input type="file" id="file-input2" value="Upload">
Upload
</label>
</div>
<div>
<span>Gene expression at single-cell level file (Optional) (2MB limited)</span>
<label class="button3">
<input type="file" id="file-input3" value="Upload">
Upload
</label>
</div>
<div>
<span>Cell type at single-cell level file (Optional)</span>
<label class="button4">
<input type="file" id="file-input4" value="Upload">
Upload
</label>
</div>
<div>
<span>FOV size:</span>
<input id="fov1" placeholder=5472 />
<span>Pixel *</span>
<input id="fov2" placeholder=3648 />
<span>Pixel</span>
</div>
<div>
<span>Pseudo spots split in one FOV:</span>
<input id="fov3" placeholder=4 />
<span>*</span>
<input id="fov4" placeholder=5 />
</div>
<button class="generate" id="process-btn">Generate</button>
<!-- <label id="downloading" style="display: none;">...</label> -->
<a href="#" download="result.csv" id="download-link" style="display: none;">Download Pseudo Spots Spatial location file</a>
<a href="#" download="result_gene.csv" id="download-link1" style="display: none;">Download Mapping relationship file between cell id and pseudo spot id</a>
<a href="#" download="result_cell_type.csv" id="download-link2" style="display: none;">Download Pseudo Spots type file</a>
<a href="#" download="result_fov_cell_type.csv" id="download-link3" style="display: none;">Download Pseudo Spot Expression file between cell id and FOV id</a>
</div>
<table id="result-table"></table>
</body>
<script src="https://unpkg.com/papaparse@5.4.1/papaparse.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const fileInput = document.getElementById("file-input");
const processBtn = document.getElementById("process-btn");
const downloadLink = document.getElementById("download-link");
const downloadLink1 = document.getElementById("download-link1");
const downloadLink2 = document.getElementById("download-link2");
const downloadLink3 = document.getElementById("download-link3");
const hint = document.getElementById("downloading");
const resultTable = document.getElementById("result-table");
const button1 = document.getElementById("file-input1");
const button2 = document.getElementById("file-input2");
const button3 = document.getElementById("file-input3");
const button4 = document.getElementById("file-input4");
const fov_width = parseInt(document.getElementById("fov1"));
const fov_height = parseInt(document.getElementById("fov2"));
const num_rows = parseInt(document.getElementById("fov3"));
const num_columns = parseInt(document.getElementById("fov4"));
let data = null;
let single_cell = null;
let fov_data = null;
let fov_coord = null;
let gene_data = null;
let cell_type = null;
let fov_cell_type = null;
// const fov_width = 5472;
// const fov_height = 3648;
// const num_rows = 4;
// const num_columns = 5;
let result = null;
var fov_size = [5472, 3648];
var fov = [4, 5];
button1.addEventListener("change", () => {
const file = button1.files[0];
Papa.parse(file, {
header: true,
complete: (results) => {
single_cell = results.data;
alert("Coordinates of single-cells local in FOV file uploaded successfully!")
}
});
});
button2.addEventListener("change", () => {
const file = button2.files[0];
Papa.parse(file, {
header: true,
complete: (results) => {
fov_coord = results.data;
alert("Coordinates of FOVs file uploaded successfully!")
}
});
});
button3.addEventListener("change", () => {
const file = button3.files[0];
var maxSize = 2 * 1024 * 1024;
if(file.size > maxSize){
alert("File size is too large, please upload a smaller file!");
return;
}
Papa.parse(file, {
header: true,
complete: (results) => {
gene_data = results.data;
alert("Gene expression at single-cell level file uploaded successfully!")
}
});
});
button4.addEventListener("change", () => {
const file = button4.files[0];
Papa.parse(file, {
header: true,
complete: (results) => {
cell_type = results.data;
alert("Cell type at single-cell level file uploaded successfully!")
}
});
});
processBtn.addEventListener("click", () => {
if(!fov_coord){
alert("Please upload the coordinates of FOVs file!")
return;
}
if(!single_cell){
alert("Please upload the coordinates of single-cells local in FOV file!")
return;
}
setTimeout(() => {
let result0 = spot_position(fov_coord);
downloadLink.href = createResultCsv(result0);
downloadLink.style.display = "block";
let result1 = get_cell_id_map(single_cell);
downloadLink1.href = createResultCsv(result1);
downloadLink1.style.display = "block";
},1);
if(cell_type){
setTimeout(() => {
let result2 = get_spot_merged_types(single_cell, cell_type);
downloadLink2.href = createResultCsv(result2);
downloadLink2.style.display = "block";
},1);
}
if(gene_data){
setTimeout(() => {
let result3 = get_merged_gene_expression(single_cell, gene_data);
downloadLink3.href = createResultCsv(result3);
downloadLink3.style.display = "block";
},1);
}
});
function map_x_y_into_spot(x, y) {
var spot_width = fov_size[0] / fov[0];
var spot_height = fov_size[1] / fov[1];
var spot_row = Math.floor(y / spot_height) + 1;
var spot_column = Math.floor(x / spot_width) + 1;
var spot_id = (spot_row - 1) * fov[0] + spot_column;
return parseInt(spot_id);
}
function get_spot_center(spot_id, fov_center_x, fov_center_y) {
var spot_width = fov_size[0] / fov[0];
var spot_height = fov_size[1] / fov[1];
var spot_row = Math.floor((spot_id - 1) / fov[0]) + 1;
var spot_column = ((spot_id - 1) % fov[0]) + 1;
var spot_center_x = (spot_column - 1) * spot_width + spot_width / 2;
var spot_center_y = (spot_row - 1) * spot_height + spot_height / 2;
// 保留两位小数
var x = spot_center_x + Number(fov_center_x);
var y = spot_center_y + Number(fov_center_y);
x = Math.round(x * 1000) / 1000;
y = Math.round(y * 1000) / 1000;
return [x, y];
}
function get_fov_spots_coords(fov_data, fov_width, fov_height, num_rows, num_columns) {
const spot_width = fov_width / num_columns;
const spot_height = fov_height / num_rows;
const spots = [];
for (let i = 0; i < num_columns; i++) {
for (let j = 0; j < num_rows; j++) {
const spot_id = j * num_columns + i + 1;
const cx = i * spot_width + spot_width / 2;
const cy = j * spot_height + spot_height / 2;
spots.push({ spot_centerX: cx, spot_centerY: cy, spot_id });
}
}
return spots;
}
function spot_position(fov_data) {
var all_data = [["fov", "spot_id", "x_global_px", "y_global_px"]];
for (var i = 0; i < fov_data.length - 1; i++) {
var fov = fov_data[i]['fov'];
var fov_center = [fov_data[i]['x_global_px'], fov_data[i]['y_global_px']];
for (var spot_id = 1; spot_id <= 20; spot_id++) {
var spot_center = get_spot_center(spot_id, fov_center[0], fov_center[1]);
all_data.push([fov, spot_id, spot_center[0], spot_center[1]]);
}
}
return all_data;
}
function get_cell_id_map(meta_data) {
var cell_id_map = {};
var all_data = [['fov', 'cell_id', 'spot_id']]
for (var i = 0; i < meta_data.length; i++) {
var cell_id = meta_data[i]['cell_ID'];
var local_x = meta_data[i]['CenterX_local_px'];
var local_y = meta_data[i]['CenterY_local_px'];
var spot_id = map_x_y_into_spot(local_x, local_y);
all_data.push([meta_data[i]['fov'], cell_id, spot_id]);
}
return all_data;
}
function get_fov_cell_id_type_map(cell_type) {
var fov_cell_id_type_map = {};
var all_kind_cell_type = new Set();
for (var i = 0; i < cell_type.length; i++) {
var fov = cell_type[i]['fov'];
var cell_id = cell_type[i]['cell_ID'];
var currrent_cell_type = cell_type[i]['cell_type'];
all_kind_cell_type.add(currrent_cell_type);
if (fov_cell_id_type_map[fov]) {
fov_cell_id_type_map[fov][cell_id] = currrent_cell_type;
} else {
fov_cell_id_type_map[fov] = {};
fov_cell_id_type_map[fov][cell_id] = currrent_cell_type;
}
}
return [fov_cell_id_type_map, all_kind_cell_type]
}
function get_spot_merged_types(meta_data, type_data) {
var [fov_cell_id_type_map, all_kind_cell_type] = get_fov_cell_id_type_map(type_data);
var spot_merged_types = {};
var a_map = {};
all_kind_cell_type.forEach(function (item) {
a_map[item] = 0;
});
// 'fov', 'spot_id', all_kind_cell_type
var all_data = [['fov', 'spot_id', ...all_kind_cell_type]];
for (var i = 0; i < meta_data.length; i++) {
var fov = meta_data[i]['fov'];
var cell_id = meta_data[i]['cell_ID'];
var local_x = meta_data[i]['CenterX_local_px'];
var local_y = meta_data[i]['CenterY_local_px'];
var spot_id = map_x_y_into_spot(local_x, local_y);
var current_cell_type = fov_cell_id_type_map[fov][cell_id];
if (!all_kind_cell_type.has(current_cell_type)) {
continue;
}
if (spot_merged_types[fov]) {
if (spot_merged_types[fov][spot_id]) {
spot_merged_types[fov][spot_id][current_cell_type] += 1;
} else {
spot_merged_types[fov][spot_id] = deepClone(a_map);
spot_merged_types[fov][spot_id][current_cell_type] += 1;
}
} else {
spot_merged_types[fov] = {};
spot_merged_types[fov][spot_id] = deepClone(a_map);
spot_merged_types[fov][spot_id][current_cell_type] += 1;
}
}
for (var fov in spot_merged_types) {
for (var spot_id in spot_merged_types[fov]) {
all_data.push([fov, spot_id, ...Object.values(spot_merged_types[fov][spot_id])]);
}
}
return all_data;
}
function get_total_type(meta_data, cell_type) {
var meta_spot_data = get_cell_id_map(meta_data);
meta_spot_data.slice(1, meta_spot_data.length).forEach(function (item) {
var fov = item[0];
var cell_id = item[1];
var spot_id = item[2];
if (cell_id_map[cell_id]) {
cell_id_map[cell_id].push([fov, spot_id]);
} else {
cell_id_map[cell_id] = [[fov, spot_id]];
}
})
pass
}
function createResultCsv(result) {
const csv = Papa.unparse(result);
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, "result.csv");
} else {
return URL.createObjectURL(blob);
}
};
function deepClone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
var newObj = obj.constructor();
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = deepClone(obj[key]);
}
}
return newObj;
}
function get_fov_cell_id_expression_map(gene_expression){
var fov_cell_id_expression_map = {};
for (var i = 0; i < gene_expression.length; i++) {
var fov = gene_expression[i]['fov'];
var cell_id = gene_expression[i]['cell_ID'];
var values = Object.values(gene_expression[i]);
var expression = values.slice(2, values.length);
// expression to float
expression = expression.map(function (item) {
return parseFloat(item);
});
if (fov_cell_id_expression_map[fov]) {
fov_cell_id_expression_map[fov][cell_id] = expression;
} else {
fov_cell_id_expression_map[fov] = {};
fov_cell_id_expression_map[fov][cell_id] = expression;
}
}
return fov_cell_id_expression_map;
}
function handle_agg(alist,blist,ctype="SUM"){
if(ctype == "SUM"){
return alist.map((x, i) => x + blist[i]);
}
}
function get_merged_gene_expression(meta_data, gene_expression) {
var all_keys = Object.keys(gene_expression[0]);
var fov_cell_id_expression_map = get_fov_cell_id_expression_map(gene_expression);
var all_data = [['fov', 'spot_id', ...all_keys.slice(2, all_keys.length)]]; // 'fov', 'spot_id', 'gene1', 'gene2', ...
var data_map = {};
for (var i = 0; i < meta_data.length; i++) {
var fov = meta_data[i]['fov'];
var cell_id = meta_data[i]['cell_ID'];
var local_x = meta_data[i]['CenterX_local_px'];
var local_y = meta_data[i]['CenterY_local_px'];
var spot_id = map_x_y_into_spot(local_x, local_y);
// sums up the expression of the same gene
if(!fov_cell_id_expression_map[fov]){
continue;
}
if(!fov_cell_id_expression_map[fov][cell_id]){
continue;
}
var expression = fov_cell_id_expression_map[fov][cell_id];
if(!expression){
continue;
}
if (data_map[fov]) {
if (data_map[fov][spot_id]) {
data_map[fov][spot_id] = handle_agg(data_map[fov][spot_id], expression);
} else {
data_map[fov][spot_id] = expression;
}
} else {
data_map[fov] = {};
data_map[fov][spot_id] = expression;
}
}
for (var fov in data_map) {
for (var spot_id in data_map[fov]) {
all_data.push([fov, spot_id, ...data_map[fov][spot_id]]);
}
}
return all_data;
}
});
</script>
</html>