-
Notifications
You must be signed in to change notification settings - Fork 36
/
protvista-coloured-sequence.js
181 lines (159 loc) · 5.33 KB
/
protvista-coloured-sequence.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
import { select, scaleLinear } from "d3";
import ProtVistaSequence from "protvista-sequence";
import { ColorScaleParser, String2Object } from "protvista-utils";
import hydroInterfaceScale from "./hydrophobicity-interface-scale.json";
import hydroOctanoleScale from "./hydrophobicity-octanol-scale.json";
import hydroScale from "./hydrophobicity-scale.json";
import isoelectricPointScale from "./isoelectric-point-scale.json";
const supportedScales = [
"hydrophobicity-interface-scale",
"hydrophobicity-octanol-scale",
"hydrophobicity-scale",
"isoelectric-point-scale"
];
const defaultScale = {
domain: [-2, 2],
range: ["#ffdd00", "#0000FF"]
};
const MIN_BASE_SIZE = 8;
class ProtVistaColouredSequence extends ProtVistaSequence {
static get observedAttributes() {
return ProtVistaSequence.observedAttributes.concat(
"scale", // One of the supported scales. Ora custom scale given in the following format. e.g. A:0.5,M:-3,P:3
"color_range" // Color values per point to define a colorScale. Default: #ffdd00:-2,#0000FF:2
);
}
_createSequence() {
this.svg = select(this)
.append("div")
.attr("class", "")
.append("svg")
.attr("id", "")
.attr("width", this.width)
.attr("height", this._height);
this.uniqueID = Math.random()
.toString(36)
.substring(7);
this.gradient = this.svg
.append("defs")
.append("linearGradient")
.attr("id", `scale-gradient-${this.uniqueID}`);
this.seq_g = this.svg.append("g").attr("class", "background");
this.seq_greadient = this.svg
.append("rect")
.attr("class", "seq-gradient")
.style("pointer-events", "none");
this._getCharWidth();
this.trackHighlighter.appendHighlightTo(this.svg);
this.refresh();
}
getScaleFromAttribute() {
let scale = null;
if (supportedScales.indexOf(this._scale) >= 0) {
switch (this._scale) {
case "hydrophobicity-scale":
return hydroScale;
case "hydrophobicity-interface-scale":
return hydroInterfaceScale;
case "isoelectric-point-scale":
return isoelectricPointScale;
case "hydrophobicity-octanol-scale":
return hydroOctanoleScale;
default:
return null;
}
}
if (
/([ILFVMPWHTEQCYASNDRGK]:-?\d+\.?\d*)(,[ILFVMPWHTEQCYASNDRGK]:-?\d+\.?\d*)*/.test(
this._scale
)
) {
try {
scale = String2Object(this._scale, {
keyFormatter: x => x.toUpperCase(),
valueFormatter: x => parseFloat(x)
});
} catch (error) {
console.error(`Couldn't parse the given scale ${this._scale}`, error);
}
}
return scale;
}
refresh() {
if (this.seq_g) {
const scale = this.getScaleFromAttribute();
if (scale === null) {
console.error("The attribute scale is not valid.");
return;
}
const colorScale = scaleLinear();
this.colorScale = defaultScale;
if (this._color_range) {
this.colorScale = ColorScaleParser(this._color_range);
}
colorScale.domain(this.colorScale.domain).range(this.colorScale.range);
const ftWidth = this.getSingleBaseWidth();
const first = Math.round(Math.max(0, this._displaystart - 2));
const last = Math.round(
Math.min(this.sequence.length, this._displayend + 1)
);
const bases = this.sequence
.slice(first, last)
.split("")
.map((aa, i) => {
// use 0 if the base is not in the given scale
const value = aa.toUpperCase() in scale ? scale[aa.toUpperCase()] : 0;
return {
start: 1 + first + i,
end: 1 + first + i,
aa,
value,
colour: colorScale(value)
};
});
this.residues = this.seq_g
.selectAll("rect.base_bg")
.data(ftWidth < MIN_BASE_SIZE ? [] : bases, d => d.start);
this.residues
.enter()
.append("rect")
.attr("class", "base_bg feature")
.attr("data-base", ({ aa }) => aa)
.attr("data-pos", ({ start }) => start)
.attr("height", this._height)
.merge(this.residues)
.attr("width", ftWidth)
.attr("fill", ({ colour }) => colour)
.attr("x", ({ start }) => this.getXFromSeqPosition(start))
.call(this.bindEvents, this);
this.residues.exit().remove();
const stops = this.gradient
.selectAll("stop")
.data(this.sequence.split(""));
stops
.enter()
.append("stop")
.merge(stops)
.attr("offset", (_, pos) => (pos + 0.5) / this.sequence.length)
.attr("stop-color", base => {
return colorScale(
base.toUpperCase() in scale ? scale[base.toUpperCase()] : 0 // if the base is not in the given scale
);
});
this.gradient.exit().remove();
this.seq_greadient
.attr("x", this.getXFromSeqPosition(1))
.attr("y", 0)
.attr("height", this._height)
.attr(
"width",
this.getXFromSeqPosition(this.sequence.length) -
this.getXFromSeqPosition(0)
)
.style("opacity", ftWidth < MIN_BASE_SIZE ? 1 : MIN_BASE_SIZE / ftWidth)
.attr("fill", `url(#scale-gradient-${this.uniqueID})`);
this._updateHighlight();
}
}
}
export default ProtVistaColouredSequence;