-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupset-plotly.js
More file actions
248 lines (220 loc) · 7.19 KB
/
Copy pathupset-plotly.js
File metadata and controls
248 lines (220 loc) · 7.19 KB
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
/**
* upset-plotly
*
* makeUpset function generates intersections of input sets and values.
* The Taken from 'https://github.com/chuntul/d3-upset', and modified to accomodate color coding feature.
*
* getPlotData generates color-coded plot data used by plotlyJS.
* getBarLayout returns layout configuration for the bar plot portion of the upset plot.
* getPointLayout returns layout configuration for the intersection points portion of the upset plot.
*/
// plot colors to be used to color code each set. These are 20 randomly generated colors.
const plotColors = [
'#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#6fbd22','#bcbd22','#17becf','#222AA1','#7DC922','#03F14A','#2F0248','#D31E70','#370E0F','#101A21','#FF9585','#BE93F6','#1CF4A5','#DACC14','#BB012F','#62AD27','#49947F','#A817D1','#159326','#652CBF','#1922A7','#2FC186','#6A0570'
]
function getBarLayout(barLength) {
return(
{
autosize: true,
xaxis: {
showgrid: false,
zeroline: false,
showticklabels: false,
range: [-1, barLength + 1]
},
yaxis: {
tickfont: {
size: 12
},
showgrid: true,
showticklabels: true
},
margin: {t: 10, b: 0, l: 100, r: 10},
showlegend: false
}
)
}
function getPointLayout(numPoints) {
return(
{
autosize: true,
xaxis: {
showgrid: false,
zeroline: false,
showticklabels: false,
range: [-1, numPoints + 1]
},
yaxis: {
showgrid: true,
tickfont: {
size: 12
},
showticklabels: true
},
margin: {t: 0, b: 10, l: 100, r: 10},
showlegend: false
}
)
}
function getPlotData(intersections, sets) {
let bars = []
let points = []
for(let i = 0; i < intersections.length; i++){
// Convert set indices into array of integers.
intersections[i].setIndices = intersections[i].setIndices.split('-').filter(x => x).map(s => {return parseInt(s)})
// Assign set names and colors to each intersection.
let setNames = []
let colors = []
for(let j = 0; j < intersections[i].setIndices.length; j++){
setNames.push(sets[intersections[i].setIndices[j]])
colors.push(plotColors[intersections[i].setIndices[j]])
}
intersections[i].setNames = setNames
intersections[i].colors = colors
}
for(let i = 0; i < intersections.length; i++){
bars.push({
x: [i, i],
y: [0, intersections[i].names.length],
mode: 'markers+lines',
type: 'scatter',
line: {
color: intersections[i].colors.length > 1 ? '#777777' : intersections[i].colors[0],
width: 10
},
marker: {
color: intersections[i].colors.length > 1 ? '#777777' : intersections[i].colors[0],
symbol: 'line-ew',
size: 10
},
text: intersections[i].names.length,
hoverinfo: 'text'
})
points.push({
x: new Array(intersections[i].setNames.length).fill(i),
y: intersections[i].setNames,
mode: intersections[i].setNames.length > 1 ? 'lines+markers' : 'markers',
type: 'scatter',
marker: {
size: 10,
color: intersections[i].colors
},
line: {
color: '#777777'
},
hoverinfo: 'text'
})
}
return {bars: bars, points: points}
}
function makeUpset(sets, names) { // names: [[],[]]
var numSets = sets.length
// computes intersections
var data2 = []
for (var i = 0; i < numSets; i++) {
var intSet = {
"set": i.toString(),
"names": names[i],
"setIndices": i.toString() + '-'
}
data2.push(intSet)
for (var j = i + 1; j < numSets; j++) {
var intSet2 = {
"set": i.toString() + j.toString(),
"names": findIntersection(names[i], names[j]),
"setIndices": i.toString() + '-' + j.toString() + '-'
}
data2.push(intSet2)
helperUpset(i, j+1, numSets, names, data2)
}
}
//removing all solo datasets and replacing with data just in those datasets (cannot intersect with others)
var tempData = []
for (var i = 0; i < data2.length; i++) {
if (data2[i].set.length != 1) { // solo dataset
tempData.push(data2[i])
}
}
data2 = tempData
for (var i = 0; i < numSets; i++) {
var inds = Array.apply(null, {length: numSets}).map(Function.call, Number)
var index = inds.indexOf(i)
if (index > -1) {
inds.splice(index, 1);
}
//console.log(inds)
data2.push({
"set": i.toString(),
"names": names[i],
"setIndices": i.toString() + '-'
})
}
// makes sure data is unique
var unique = []
var newData = []
for (var i = 0; i < data2.length; i++) {
if (unique.indexOf(data2[i].set) == -1) {
unique.push(data2[i].set)
newData.push(data2[i])
}
}
var data = newData
// sort data decreasing
data.sort(function(a, b) {
return parseFloat(b.names.length) - parseFloat(a.names.length);
});
return data
}
// takes two arrays of values and returns an array of intersecting values
function findIntersection(set1, set2) {
//see which set is shorter
var temp;
if (set2.length > set1.length) {
temp = set2; set2 = set1; set1 = temp;
}
return set1
.filter(function(e) { //puts in the intersecting names
return set2.indexOf(e) > -1;
})
.filter(function(e,i,c) { // gets rid of duplicates
return c.indexOf(e) === i;
})
}
//for the difference of arrays - particularly in the intersections and middles
//does not mutate any of the arrays
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
//for calculating solo datasets
function subtractUpset(i, inds, names) {
var result = names[i].slice(0)
for (var ind = 0; ind < inds.length; ind++) {
// set1 vs set2 -> names[i] vs names[ind]
for (var j = 0; j < names[inds[ind]].length; j++) { // for each element in set2
if (result.includes(names[inds[ind]][j])) {
// if result has the element, remove the element
// else, ignore
var index = result.indexOf(names[inds[ind]][j])
if (index > -1) {
result.splice(index, 1)
}
}
}
}
return result
}
//recursively gets the intersection for each dataset
function helperUpset(start, end, numSets, names, data) {
if (end == numSets) {
return data
}
else {
var intSet = {
"set": data[data.length-1].set + end.toString(),
"names": findIntersection(data[data.length-1].names, names[end]),
"setIndices": data[data.length-1].setIndices + '-' + end.toString() + '-'
}
data.push(intSet)
return helperUpset(start, end+1, numSets, names, data)
}
}