/
RcsbFastSequenceDisplay.ts
207 lines (184 loc) · 7.2 KB
/
RcsbFastSequenceDisplay.ts
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
import {RcsbAbstractDisplay} from "./RcsbAbstractDisplay";
import {BaseType, ContainerElement, pointer, Selection} from "d3-selection";
import {LocationViewInterface} from "../RcsbBoard";
import {RcsbFvTrackData, RcsbFvTrackDataElementInterface} from "../../RcsbDataManager/RcsbDataManager";
import {RcsbD3Constants} from "../RcsbD3/RcsbD3Constants";
import {RcsbD3ScaleFactory, RcsbScaleInterface} from "../RcsbD3/RcsbD3ScaleFactory";
import classes from "../../scss/RcsbBoard.module.scss";
import {
MoveFastSequenceInterface,
PlotFastSequenceInterface,
PlotFastSequenceLineInterface,
RcsbD3FastSequenceManager
} from "../RcsbD3/RcsbD3DisplayManager/RcsbD3FastSequenceManager";
export class RcsbFastSequenceDisplay extends RcsbAbstractDisplay {
private yScale: RcsbScaleInterface = RcsbD3ScaleFactory.getLinearScale();
private intervalRatio: [number,number] = [5,16];
private hideFlag: boolean = false;
private compKey: string | undefined;
private nonEmptyDisplay: boolean = false;
private readonly rcsbD3SequenceManager: RcsbD3FastSequenceManager = new RcsbD3FastSequenceManager();
private definedScale: boolean = false;
private readonly innerData: Array<RcsbFvTrackDataElementInterface> = [];
private index: number;
private mousemove(event:MouseEvent){
const svgNode:ContainerElement | null = this.g?.node();
if(svgNode != null) {
const x = pointer(event, svgNode)[0];
this.index = Math.round(this.xScale.invert(x));
this.elementSubject.mouseenter.next({d: this.innerData[this.index] ?? {begin: this.index}, e: event});
}
}
private mouseleave(event:MouseEvent){
this.elementSubject.mouseleave.next({d: {begin: this.index}, e: event});
}
private mouseclick = (event: MouseEvent)=>{
const svgNode:ContainerElement | null = this.g.node();
if(svgNode != null) {
const x = pointer(event, svgNode)[0];
const position = Math.round(this.xScale.invert(x));
const region: RcsbFvTrackDataElementInterface = {begin: position, end: position};
this.getBoardHighlight()(region, event.shiftKey ? 'add' : 'set', 'select', false);
this.elementSubject.mouseclick.next({d: region, e: event});
}
};
setDynamicDisplay(){
this.hideFlag = true;
this.trackSubject.mouseleave.subscribe(()=>{
this.hideFlag = true;
this.getElements().remove();
})
this.trackSubject.mouseenter.subscribe(()=>{
this.hideFlag = false;
this.update(this.compKey);
});
}
setNonEmptyDisplay(flag: boolean): void{
this.nonEmptyDisplay = flag;
}
enter(e: Selection<SVGGElement, RcsbFvTrackDataElementInterface, BaseType, undefined>): void{
e.append<SVGTextElement>(RcsbD3Constants.TEXT);
}
_update(where: LocationViewInterface, compKey?: string) {
this.compKey = compKey;
if(this.hideFlag)
return;
this.setInnerData();
if (this.data() == null) {
return;
}
if(this.minIntervalRatio()){
const dataElems: Array<RcsbFvTrackDataElementInterface> = this.getSequenceData(where);
this.rmSequenceLine();
this.selectElements(dataElems,compKey);
this.getElements().attr("class", classes.rcsbElement)
.classed(classes.rcsbElement+"_" + compKey, typeof compKey === "string")
.call(this.plot.bind(this));
}else{
this.getElements().remove();
this.displayEmpty();
}
this.checkHideFlag();
}
displayEmpty(): void {
if(this.nonEmptyDisplay) {
this.plotSequenceLine();
}
}
plot(elements:Selection<SVGGElement,RcsbFvTrackDataElementInterface,BaseType,undefined>){
if(this.hideFlag)
return;
if(!this.definedScale) {
this.setScale();
}
const config: PlotFastSequenceInterface = {
elements: elements,
trackG: this.g,
xScale: this.xScale,
yScale: this.yScale,
color: this._displayColor as string,
height: this.height(),
intervalRatio: this.intervalRatio,
mouseclick: this.mouseclick.bind(this),
mousemove: this.mousemove.bind(this),
mouseleave: this.mouseleave.bind(this)
};
this.rcsbD3SequenceManager.plot(config);
this.checkHideFlag();
}
move(){
const config: MoveFastSequenceInterface = {
xScale: this.xScale,
intervalRatio: this.intervalRatio,
};
this.rcsbD3SequenceManager.move(config);
}
private setScale(): void {
this.yScale
.domain([0, this.height()])
.range([0, this.height()]);
this.definedScale = true
}
private plotSequenceLine(): void{
const config: PlotFastSequenceLineInterface = {
xScale: this.xScale,
yScale: this.yScale,
height: this.height(),
trackG:this.g,
mouseclick: this.mouseclick.bind(this),
mousemove: this.mousemove.bind(this),
mouseleave: this.mouseleave.bind(this)
};
RcsbD3FastSequenceManager.plotSequenceLine(config);
}
private rmSequenceLine(): void{
RcsbD3FastSequenceManager.clearLine({trackG: this.g})
}
private checkHideFlag(): void{
if(this.hideFlag) {
this.getElements().remove();
}
}
private minIntervalRatio(): boolean{
return (this.getRatio() >= this.intervalRatio[0]);
}
private getSequenceData(where: LocationViewInterface): Array<RcsbFvTrackDataElementInterface>{
const seqPath: Array<RcsbFvTrackDataElementInterface> = new Array<RcsbFvTrackDataElementInterface>();
for(let n = where.from; n <= where.to; n++){
if(this.innerData[n])
addResToSeqPath(this.innerData[n], seqPath);
}
return seqPath;
}
private setInnerData(): void {
const sequence: RcsbFvTrackData = this.data();
sequence.forEach(seqRegion=>{
if(typeof seqRegion.label !== "undefined") {
if(seqRegion.label.length>1) {
seqRegion.label.split("").forEach((s: string, i: number) => {
this.innerData[seqRegion.begin + i] = {
...seqRegion,
begin: (seqRegion.begin + i),
label: s
};
});
}else{
this.innerData[seqRegion.begin] = {
...seqRegion,
label: seqRegion.label
};
}
}
});
}
}
function addResToSeqPath(res: RcsbFvTrackDataElementInterface, seqPath: Array<RcsbFvTrackDataElementInterface>): void {
if(seqPath.length > 0 && seqPath[seqPath.length-1].label!.length + seqPath[seqPath.length-1].begin == res.begin) {
seqPath[seqPath.length-1].label! += res.label!;
}else{
seqPath.push({
begin: res.begin,
label: res.label!
});
}
}