1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.gwt.utgb.client.canvas;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.utgenome.gwt.utgb.client.track.TrackWindow;
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 public class TrackWindowChain {
57
58 private ArrayList<TrackWindow> windowList = new ArrayList<TrackWindow>();
59
60 public TrackWindowChain() {
61
62 }
63
64 private TrackWindow viewWindow;
65 private TrackWindow globalWindow;
66
67 private int PREFETCH_FACTOR = 1;
68
69 public static class WindowUpdateInfo {
70 public final List<TrackWindow> windowToCreate;
71 public final List<TrackWindow> windowToDiscard;
72
73 private WindowUpdateInfo(List<TrackWindow> windowToCreate, List<TrackWindow> windowToDiscard) {
74 this.windowToCreate = windowToCreate;
75 this.windowToDiscard = windowToDiscard;
76 }
77 }
78
79 public void clear() {
80 windowList.clear();
81 }
82
83 public void setPrefetchFactor(int factor) {
84 this.PREFETCH_FACTOR = factor;
85 }
86
87 public List<TrackWindow> getTrackWindowList() {
88 return windowList;
89 }
90
91 public TrackWindow getGlobalWindow() {
92 return globalWindow;
93 }
94
95 public TrackWindow getViewWindow() {
96 return viewWindow;
97 }
98
99 public WindowUpdateInfo setViewWindow(TrackWindow view) {
100
101 final int factor = PREFETCH_FACTOR * 2 + 1;
102 final int viewSize = view.getSequenceLength();
103 final int viewExtensionDirection = view.isReverseStrand() ? -1 : 1;
104 int gvStart = view.getStartOnGenome() - viewSize * PREFETCH_FACTOR * viewExtensionDirection;
105 int gvEnd = view.getEndOnGenome() + viewSize * PREFETCH_FACTOR * viewExtensionDirection;
106 this.globalWindow = new TrackWindow(view.getPixelWidth() * factor, gvStart, gvEnd);
107
108 ArrayList<TrackWindow> windowToPreserve = new ArrayList<TrackWindow>();
109 ArrayList<TrackWindow> windowToDiscard = new ArrayList<TrackWindow>();
110
111 if (viewWindow != null && viewWindow.hasSameScaleWith(view)) {
112
113
114 for (TrackWindow each : windowList) {
115
116 if (each.overlapWith(globalWindow)) {
117 windowToPreserve.add(each);
118 }
119 else {
120 windowToDiscard.add(each);
121 }
122 }
123 }
124 else {
125 windowToDiscard.addAll(windowList);
126 }
127 this.viewWindow = view;
128
129
130 ArrayList<TrackWindow> newWindowList = new ArrayList<TrackWindow>();
131
132
133 Collections.sort(windowToPreserve);
134 int gridStartOnGenome = windowToPreserve.isEmpty() ? view.getViewStartOnGenome() : windowToPreserve.get(0).getViewStartOnGenome();
135 while (gridStartOnGenome > globalWindow.getViewStartOnGenome()) {
136 gridStartOnGenome -= viewSize;
137 }
138 while (gridStartOnGenome < globalWindow.getViewEndOnGenome()) {
139 TrackWindow grid;
140 if (view.isPositiveStrand()) {
141 grid = view.newWindow(gridStartOnGenome, gridStartOnGenome + viewSize);
142 }
143 else {
144 grid = view.newWindow(gridStartOnGenome + viewSize, gridStartOnGenome);
145 }
146
147 if (!windowToPreserve.contains(grid)) {
148 newWindowList.add(grid);
149 }
150
151 gridStartOnGenome += viewSize;
152 }
153
154 windowList.clear();
155 windowList.addAll(windowToPreserve);
156 windowList.addAll(newWindowList);
157
158 return new WindowUpdateInfo(newWindowList, windowToDiscard);
159 }
160
161 }