-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
264 lines (229 loc) · 8.49 KB
/
utilities.js
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
// -------------------------------------------------------------
// Javascript utility functions
// -------------------------------------------------------------
const hslToRGB = require('hsl-to-rgb-for-reals')
const { scaleLog, scaleLinear } = require("d3-scale")
const logicleScale = require('./scales/logicle.js')
const arcsinScale = require('./scales/arcsinh-scale')
const constants = require('./constants')
const _ = require('lodash')
const heatMapHSLStringForValue = function (value) {
var h = (1.0 - value) * 240
return "hsl(" + h + ", 100%, 50%)";
}
const getPolygonCenter = function(polygon) {
var x = polygon.map(function(a){ return a[0] });
var y = polygon.map(function(a){ return a[1] });
var minX = Math.min.apply(null, x);
var maxX = Math.max.apply(null, x);
var minY = Math.min.apply(null, y);
var maxY = Math.max.apply(null, y);
return [(minX + maxX)/2, (minY + maxY)/2];
}
const heatMapRGBForValue = function (value) {
const h = (1.0 - value) * 240
const s = 1
const l = 0.5
let r, g, b;
if (s == 0){
r = g = b = l; // achromatic
} else {
const hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return hslToRGB(h, s, l)
}
const getPlotImageKey = function (options) {
return `${options.machineType}_${options.selectedXParameterIndex}_${options.selectedXScale}-${options.selectedYParameterIndex}_${options.selectedYScale}`
}
// options shape:
// {
// selectedXScale,
// selectedYScale,
// xRange,
// yRange,
// width,
// height
// }
const getScales = (options) => {
const scales = {}
// console.log(options)
if (options.selectedXScale === constants.SCALE_LINEAR) {
scales.xScale = scaleLinear().range([0, options.width]) // value -> display
// don't want dots overlapping axis, so add in buffer to data domain
scales.xScale.domain([options.xRange[0], options.xRange[1]]);
// Log Scale
} else if (options.selectedXScale === constants.SCALE_LOG) {
// Log scale will break for values <= 0
scales.xScale = scaleLog()
.range([0, options.width])
.base(Math.E)
.domain([options.xRange[0], options.xRange[1]])
// Biexponential Scale
} else if (options.selectedXScale === constants.SCALE_BIEXP) {
scales.xScale = logicleScale().range([0, options.width])
// Arcsin scale
} else if (options.selectedXScale === constants.SCALE_ARCSIN) {
scales.xScale = arcsinScale().range([0, options.width])
}
// setup y
if (options.selectedYScale === constants.SCALE_LINEAR) {
scales.yScale = scaleLinear().range([options.height, 0]) // value -> display
scales.yScale.domain([options.yRange[0], options.yRange[1]]);
// Log Scale
} else if (options.selectedYScale === constants.SCALE_LOG) {
scales.yScale = scaleLog()
.range([options.height, 0])
.base(Math.E)
.domain([options.yRange[0], options.yRange[1]])
// Biexponential Scale
} else if (options.selectedYScale === constants.SCALE_BIEXP) {
scales.yScale = logicleScale().range([options.height, 0])
// Arcsin scale
} else if (options.selectedYScale === constants.SCALE_ARCSIN) {
scales.yScale = arcsinScale().range([options.height, 0])
}
// window.scales = scales
return scales
}
// Get the min and max points of a polygon. See return value.
function getPolygonBoundaries (points) {
let minX = [Infinity, 0]
let maxX = [-Infinity, 0]
let minY = [0, Infinity]
let maxY = [0, -Infinity]
for (let point of points) {
if (point[0] < minX[0]) {
minX = point
}
if (point[0] > maxX[0]) {
maxX = point
}
if (point[1] < minY[1]) {
minY = point
}
if (point[1] > maxY[1]) {
maxY = point
}
}
return [ [ minX, maxX ], [ minY, maxY ] ]
}
// Returns peaks arranged into groups along the x and y axis
const getAxisGroups = (peaks) => {
// Percentage of maximum distance between furthest peak to group together
const maxGroupDistance = 0.3
// Divide peaks into groups along the x and y axis
// Get [minX, maxX] range of peaks along x axis
let xRange = peaks.reduce((acc, curr) => { return [ Math.min(acc[0], curr.nucleus[0]), Math.max(acc[1], curr.nucleus[0]) ] }, [Infinity, -Infinity])
// Get [minY, maxY] range of peaks along y axis
let yRange = peaks.reduce((acc, curr) => { return [ Math.min(acc[0], curr.nucleus[1]), Math.max(acc[1], curr.nucleus[1]) ] }, [Infinity, -Infinity])
// Create buckets and place peaks into groups along each axis
let xGroups = []
let yGroups = []
for (let peak of peaks) {
const newXGroup = () => {
xGroups.push({
position: peak.nucleus[0],
peaks: [ peak.id ]
})
}
// Create a group from the first peak
if (xGroups.length === 0) {
newXGroup()
} else {
let found = false
for (let group of xGroups) {
const distance = Math.abs(group.position - peak.nucleus[0])
// If the peak is within 10% of an existing group, add it to that group
if (distance < (xRange[1] - xRange[0]) * maxGroupDistance || distance < 30) {
group.peaks.push(peak.id)
found = true
}
}
// Otherwise create a new group
if (!found) {
newXGroup()
}
}
const newYGroup = () => {
yGroups.push({
position: peak.nucleus[1],
peaks: [ peak.id ]
})
}
// Create a group from the first peak
if (yGroups.length === 0) {
newYGroup()
} else {
let found = false
for (let group of yGroups) {
const distance = Math.abs(group.position - peak.nucleus[1])
// If the peak is within 10% of an existing group, add it to that group
if (distance < (yRange[1] - yRange[0]) * maxGroupDistance || distance < 20) {
group.peaks.push(peak.id)
found = true
}
}
// Otherwise create a new group
if (!found) {
newYGroup()
}
}
}
xGroups.sort((a, b) => { return a.position - b.position })
yGroups.sort((a, b) => { return a.position - b.position })
return { xGroups, yGroups }
}
export const getMetadataFromFCSFileText = (text) => {
// Loop through the parameters and get the min and max values of all the data points
const FCSParameters = []
for (let key of _.keys(text)) {
if ((key.match(/^\$P.+N$/) || key.match(/^\$P.+S$/)) &&
!FCSParameters[parseInt(key.match(/\d+/)[0]) - 1]) {
FCSParameters[parseInt(key.match(/\d+/)[0]) - 1] = {
key: text[key],
label: text[key],
index: parseInt(key.match(/\d+/)[0]) - 1,
statistics: {
min: Infinity,
positiveMin: Infinity,
max: -Infinity,
mean: 0
}
}
}
if (key.match(/^\$P.+N$/)) {
FCSParameters[parseInt(key.match(/\d+/)[0]) - 1].key = text[key]
} else if (key.match(/^\$P.+S$/)) {
FCSParameters[parseInt(key.match(/\d+/)[0]) - 1].label = text[key]
}
}
return FCSParameters
}
export const getMetadataFromCSVFileHeader = (header) => {
return header.map((column, index) => {
return {
key: column,
label: column,
index,
statistics: {
min: Infinity,
positiveMin: Infinity,
max: -Infinity,
mean: 0
}
}
})
}
module.exports = { heatMapHSLStringForValue, heatMapRGBForValue, getPlotImageKey, getScales, getPolygonCenter, getPolygonBoundaries, getAxisGroups, getMetadataFromFCSFileText, getMetadataFromCSVFileHeader }