-
Notifications
You must be signed in to change notification settings - Fork 6
/
CouplingsLogoDiagramD3.js
175 lines (168 loc) · 5.79 KB
/
CouplingsLogoDiagramD3.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
// CouplingsLogoDiagramD3.js
// This class generates a SVG image (via D3) representing couplings of residues within the sequence alignment.
// Constructor for the CouplingsLogoDiagramD3 class
// @param alignment A multiple sequence alignment
// @param options presentation and data processing / statistical options
class CouplingsLogoDiagramD3 {
//##ROC##
constructor(options, data, seqLen) {
this.options = jQuery.extend(true, {}, options);
// TODO: Currently dumping in the symColHash (normalized by alignment height) - but should be an integrated data model.
this.rawData = data; // other class members
this.svg = null;
this.bounds = null;
this.seqLen = seqLen;
const defaultOpts = {
elementId: 'couplingslogo', //html element Id to use as container
elementWidth: 600, // desired width of the container
elementHeight: 32, // desired height of the container
};
}
// initDiagram()
// constructs element in container from options and populates using drawDiagram()
initDiagram() {
this.svg = this.createSvg(d3.select('#' + this.options.elementId), this.options.elementWidth, this.options.elementHeight);
this.drawDiagram(this.svg, this.bounds, this.options, this.rawData);
}
// createSVG()
// constructs svg element using dimensions from options
createSvg(container, width, height) {
return container
.append('svg')
.attr('width', width)
.attr('height', height)
.attr('style', 'background-color:white');
}
// drawDiagram()
// set up scaling and any labels, then call drawPlot
drawDiagram(svg, bounds, options, derivedColumnProportions) {
const xScale = null; //TODO implement
const yScale = null; //TODO implement
const finalBounds = null; //TODO implement
this.drawPlot(svg, options, finalBounds, xScale, yScale);
}
// addcolumnToPlot()
// given a plot group (g) element, add a column to it
addColumnToPlot(plotGroup, options, columnIndexA, columnIndexB, symbolCode) {
const nextcolA = plotGroup.append('g').attr('transform', 'translate(' + 100 * columnIndexA + ',0)');
const nextcolB = plotGroup.append('g').attr('transform', 'translate(' + 100 * columnIndexB + ',0)');
const nextcolC = plotGroup.append('g').attr('transform', 'translate(' + 100 * columnIndexB + ',0)');
let columnFloorLevel = 100;
const symbolProportion = 1;
columnFloorLevel -= symbolProportion * 100;
nextcolA
.append('g')
.attr('transform', 'matrix(1,0,0,' + symbolProportion + ',0,' + columnFloorLevel + ')')
.append('use')
.attr('xlink:href', '#' + symbolCode);
nextcolB
.append('g')
.attr('transform', 'matrix(1,0,0,' + symbolProportion + ',0,' + columnFloorLevel + ')')
.append('use')
.attr('xlink:href', '#' + symbolCode);
const circleData = [{ cx: 20, cy: 20, radius: 30, color: 'green' }, { cx: 70, cy: 70, radius: 20, color: 'purple' }];
nextcolC
.append('g')
.attr('transform', 'matrix(1,0,0,' + symbolProportion + ',0,' + columnFloorLevel + ')')
.selectAll('circle')
.data(circleData)
.enter()
.append('circle');
const circleAttributes = nextcolC
.attr('cx', function(d) {
return d.cx;
})
.attr('cy', function(d) {
return d.cy;
})
.attr('r', function(d) {
return d.radius;
})
.style('fill', function(d) {
return d.color;
});
}
// drawPlot()
// Create the group element which represents the entire plot (using scaling) and add all columns.
drawPlot(svg, options, bounds, xScale, yScale) {
if (this.rawData == null) {
return;
}
const columnCount = this.seqLen;
if (columnCount === 0) {
return;
}
this.plotGroup = svg
.append('g')
.attr(
'transform',
'matrix(' + this.options.elementWidth / columnCount / 100 + ',0,0,' + this.options.elementHeight / 100 + ',0,0)',
);
const colors = [
'#3366cc',
'#dc3912',
'#ff9900',
'#109618',
'#990099',
'#0099c6',
'#dd4477',
'#66aa00',
'#b82e2e',
'#316395',
'#994499',
'#22aa99',
'#aaaa11',
'#6633cc',
'#e67300',
'#8b0707',
'#651067',
'#329262',
'#5574a6',
'#3b3eac',
]; // http://bl.ocks.org/aaizemberg/78bd3dade9593896a59d
for (let col = 0; col < this.rawData.A.length && col < colors.length; col++) {
// this.addColumnToPlot(plotGroup, options, this.rawData.A[col], this.rawData.B[col], symbolCodes[col]);
const circleDataA = [{ cx: -60, cy: 50, radius: 40, color: colors[col] }];
const circleDataB = [{ cx: -55, cy: 50, radius: 40, color: colors[col] }];
// Translate.
const trans1A = this.plotGroup.append('g').attr('transform', 'translate(' + 100 * this.rawData.A[col] + ',0)');
const trans1B = this.plotGroup.append('g').attr('transform', 'translate(' + 100 * this.rawData.B[col] + ',0)');
const circlesA = trans1A
.selectAll('circle')
.data(circleDataA)
.enter()
.append('circle');
const circlesB = trans1B
.selectAll('circle')
.data(circleDataB)
.enter()
.append('circle');
const circleAttributesA = circlesA
.attr('cx', function(d) {
return d.cx;
})
.attr('cy', function(d) {
return d.cy;
})
.attr('r', function(d) {
return d.radius;
})
.style('fill', function(d) {
return d.color;
});
const circleAttributesB = circlesB
.attr('cx', function(d) {
return d.cx;
})
.attr('cy', function(d) {
return d.cy;
})
.attr('r', function(d) {
return d.radius;
})
.style('fill', function(d) {
return d.color;
});
}
}
}