1 #include "alg/gltracks.hpp"
10 max_ortho(400.0, 0.0, 600.0, 0.0),
12 viewport_size(600, 400),
20 GlTracks::GlTracks(const GlTracks& gt)
22 max_ortho(gt.max_ortho),
23 cur_ortho(gt.cur_ortho),
24 viewport_size(gt.viewport_size),
25 viewport_center(gt.viewport_center),
26 zoom_level(gt.zoom_level),
27 color_mapper(gt.color_mapper),
28 track_container(gt.track_container)
32 void GlTracks::initializeGL()
34 glEnable(GL_DEPTH_TEST);
35 glClearColor(1.0, 1.0, 1.0, 0.0);
36 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
37 glShadeModel(GL_FLAT);
40 void GlTracks::resizeGL(int width, int height)
42 viewport_size.x = width;
43 viewport_size.y = height;
44 glViewport(0, 0, (GLsizei)width, (GLsizei)height);
48 void GlTracks::paintGL() const
50 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
53 glMatrixMode(GL_PROJECTION);
55 glOrtho(cur_ortho.left, cur_ortho.right,
56 cur_ortho.bottom, cur_ortho.top,
65 float GlTracks::left() const
67 return max_ortho.left;
70 float GlTracks::right() const
72 return max_ortho.right;
75 void GlTracks::setViewportCenter(float x)
77 update_viewport(x, zoom_level);
81 float GlTracks::viewportLeft() const
83 return cur_ortho.left;
86 float GlTracks::viewportCenter() const
88 return viewport_center;
91 float GlTracks::viewportRight() const
93 return cur_ortho.right;
96 float GlTracks::viewportHeight() const
98 return cur_ortho.top - cur_ortho.bottom;
101 float GlTracks::viewportWidth() const
103 return cur_ortho.right - cur_ortho.left;
106 void GlTracks::setZoom(int new_zoom)
108 update_viewport(viewport_center, new_zoom);
109 zoom_level = new_zoom;
112 int GlTracks::zoom() const
117 void GlTracks::setColorMapper(AnnotationColors& cm)
122 AnnotationColors& GlTracks::colorMapper()
127 void GlTracks::clear()
130 //path_segments.clear();
131 track_container.clear();
134 void GlTracks::push_sequence(const Sequence &s)
136 GlSequence gs(s, color_mapper);
140 void GlTracks::push_sequence(GlSequence &gs)
144 track_container.push_back(gs);
146 if (track_container.size() > 1)
147 path_segments.push_back(pair_segment_map());
150 const std::vector<GlSequence>& GlTracks::tracks() const
152 return track_container;
155 void GlTracks::clear_links()
157 path_segment_map_vector::iterator psmv_i;
158 for(psmv_i = path_segments.begin();
159 psmv_i != path_segments.end();
167 GlTracks::link(vector<int> path, vector<bool> rc, int length)
169 if (path.size() < 2) {
170 // should i throw an error instead?
173 if (path.size() != rc.size()) {
174 throw runtime_error("path and reverse compliment must be the same length");
176 vector<int>::iterator path_i = path.begin();
177 vector<bool>::iterator rc_i = rc.begin();
179 int prev_x = *path_i; ++path_i;
180 bool prev_rc = *rc_i; ++rc_i;
181 while (path_i != path.end() and rc_i != rc.end())
183 segment_key p(prev_x, *path_i);
184 pair_segment_map::iterator found_segment = path_segments[track_i].find(p);
185 if (found_segment == path_segments[track_i].end()) {
187 float y1 = track_container[track_i].y();
188 y1 -= track_container[track_i].height()/2;
189 float y2 = track_container[track_i+1].y();
190 y2 -= track_container[track_i+1].height()/2;
192 Segment s(prev_x, y1, *path_i, y2, prev_rc);
193 s.path_ids.push_back(pathid);
194 path_segments[track_i][p] = s;
197 found_segment->second.path_ids.push_back(pathid);
205 // pathid is reset by push_sequence
209 void GlTracks::update_viewport(float center, int new_zoom)
211 float max_width = max_ortho.width();
212 // division by zero is a major bummer
216 float new_max_width = max_width / new_zoom;
217 cur_ortho.left = center-new_max_width;
218 cur_ortho.right = center+new_max_width;
221 void GlTracks::update_layout()
223 typedef std::vector<GlSequence>::iterator glseq_itor_type;
224 float available_height = (float)cur_ortho.top - 2 * (float)border;
225 float max_base_pairs = 0;
226 size_t track_count = track_container.size();
228 if (track_count > 1) {
229 // we have several tracks
230 float track_spacing = available_height / (track_count-1);
231 float y = available_height + (float)border;
232 for(glseq_itor_type seq_i = track_container.begin();
233 seq_i != track_container.end();
234 ++seq_i, y-=track_spacing)
238 if (seq_i->length() > max_base_pairs)
239 max_base_pairs = seq_i->length();
241 } else if (track_count == 1) {
242 // center the single track
243 glseq_itor_type seq_i = track_container.begin();
245 seq_i->setY(viewport_size.x /2);
246 max_base_pairs = seq_i->length();
248 // nothing to do as we're empty
251 max_ortho.right = max_base_pairs + border;
252 max_ortho.left = -border;
253 max_ortho.top = viewport_size.x;
254 max_ortho.bottom = 0;
255 cur_ortho = max_ortho;
256 viewport_center = (cur_ortho.width()/2) + cur_ortho.left;
259 void GlTracks::draw() const
261 glMatrixMode(GL_MODELVIEW);
263 //glPushName(MussaPaths);
270 void GlTracks::draw_selection() const
272 /* draw selection box
274 glDepthMask(GL_FALSE);
275 if (selectedMode && !drawingBand) {
276 glColor4f(0.6, 0.6, 0.6, 0.9);
277 glRectf(previousBand.x(), previousBand.y(),
278 previousBand.right(), previousBand.bottom());
280 glDepthMask(GL_TRUE);
285 void GlTracks::draw_tracks() const
287 typedef std::vector<GlSequence>::const_iterator glseq_citor_type;
288 for(glseq_citor_type seq_i = track_container.begin();
289 seq_i != track_container.end();
292 seq_i->draw(cur_ortho.left, cur_ortho.right);
296 void GlTracks::draw_segments() const
300 // each vector contains path_segment_maps of all the connections
301 // between this track and the next
302 path_segment_map_vector::const_iterator psmv_i;
303 for(psmv_i = path_segments.begin();
304 psmv_i != path_segments.end();
307 // these maps contain the pair index (used so we dont keep drawing the
308 // same segment) and the actual segment structure.
309 pair_segment_map::const_iterator psm_i;
310 for(psm_i = psmv_i->begin();
311 psm_i != psmv_i->end();
314 // the second element of our map pair is a segment
315 const Segment &s = psm_i->second;
316 // need to do something so we can detect our selection
317 if (not s.reversed) {
318 glColor3f(1.0, 0.0, 0.0);
320 glColor3f(0.0, 0.0, 1.0);
322 glVertex3f(s.start.x, s.start.y, -1);
323 glVertex3f(s.end.x, s.end.y, -1);