1 #include "alg/glsequence.hpp"
9 GlSequence::GlSequence(const Sequence &s, AnnotationColors& cm)
16 drawColor(0.0, 0.0, 0.0),
17 char_pix_per_world_unit(2.5)
21 GlSequence::GlSequence(const GlSequence &s)
26 seq_height(s.seq_height),
27 color_mapper(s.color_mapper),
28 drawColor(s.drawColor),
29 char_pix_per_world_unit(s.char_pix_per_world_unit)
33 GlSequence &GlSequence::operator=(const GlSequence & s)
36 const_cast<Sequence &>(seq) = s.seq;
40 seq_height = s.seq_height;
41 color_mapper = s.color_mapper;
42 drawColor = s.drawColor;
43 assert(char_pix_per_world_unit == s.char_pix_per_world_unit);
48 const Sequence& GlSequence::sequence() const
53 void GlSequence::setX(GLfloat value)
58 GLfloat GlSequence::x() const
63 GLfloat GlSequence::right() const
65 return length()+seq_x;
68 void GlSequence::setY(GLfloat value)
73 GLfloat GlSequence::y() const
78 GLfloat GlSequence::height() const
83 GLfloat GlSequence::length() const
88 Sequence::size_type GlSequence::leftbase(GLfloat left) const
90 left = ceil(left - seq_x);
93 else if (left > seq.size() )
96 return (Sequence::size_type)left;
99 Sequence::size_type GlSequence::rightbase(GLfloat right) const
101 right = floor(right) - seq_x;
102 if (right > seq.size())
107 return (Sequence::size_type)right;
110 Sequence::const_iterator GlSequence::sequence_begin() const
115 Sequence::const_iterator GlSequence::sequence_end() const
120 Sequence::const_iterator
121 GlSequence::sequence_begin(GLfloat left, GLfloat right) const
123 if ( leftbase(left) > seq.size() or left > right )
126 return seq.begin() + leftbase(left);
129 Sequence::const_iterator
130 GlSequence::sequence_end(GLfloat left, GLfloat right) const
132 if ( rightbase(right) > seq.size() or left > right )
135 return seq.begin() + rightbase(right);
139 //! set default track draw color
140 void GlSequence::setColor(Color &c)
145 //! get default track draw color
146 Color GlSequence::color()
152 int GlSequence::get_viewport_width_in_pixels()
155 glGetIntegerv(GL_VIEWPORT, viewport);
156 return viewport[3]; // grab the viewport width
159 GLfloat GlSequence::get_pixel_width(GLfloat left, GLfloat right) const
161 return get_pixel_width(left, right, get_viewport_width_in_pixels());
165 GlSequence::get_pixel_width(GLfloat left, GLfloat right, int vp_width) const
167 return round((right-left)/vp_width);
170 bool GlSequence::is_sequence_renderable(GLfloat left, GLfloat right) const
172 return is_sequence_renderable(left, right, get_viewport_width_in_pixels());
175 bool GlSequence::is_sequence_renderable(GLfloat left,
177 int viewport_width) const
179 GLfloat world_width = right - left;
180 GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
182 // if the number of pixels taken up by rendering the characters
183 // that'd show up in the current ortho width is less than the window
184 // width we can actually draw something
185 return pixels_needed < viewport_width;
189 void GlSequence::draw(GLfloat left, GLfloat right) const
191 if ( not is_sequence_renderable(left, right) ) {
192 draw_track(left, right);
194 draw_sequence(left, right);
196 draw_annotations(left, right);
199 void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
200 GLfloat left, GLfloat right,
201 GLfloat height, GLfloat z) const
203 GLfloat pixel_width = get_pixel_width(world_left, world_right);
204 GLfloat offset = height/2.0;
205 GLfloat top = seq_y + offset;
206 GLfloat bottom = seq_y - offset;
208 // make our box be at least 1 pixel
209 if ((right-left) < pixel_width) {
210 right = left + pixel_width;
213 glVertex3f(left, top, z);
214 glVertex3f(left, bottom, z);
215 glVertex3f(right, bottom, z);
216 glVertex3f(right, top, z);
220 void GlSequence::draw_track(GLfloat left, GLfloat right) const
222 glColor3fv(drawColor.get());
223 // draw main sequence track
224 draw_box(left, right, seq_x, seq_x+seq.size(), seq_height, 0.0);
227 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
230 GLfloat annotation_z = seq_z + 10.0;
231 const std::list<annot>& annots = seq.annotations();
232 const std::list<motif>& motifs = seq.motifs();
233 for (std::list<annot>::const_iterator annot_itor = annots.begin();
234 annot_itor != annots.end();
237 glColor3f(0.0, 0.8, 0.0);
238 draw_box(left, right, seq_x+annot_itor->start, seq_x+annot_itor->end,
239 seq_height, annotation_z);
242 for (std::list<motif>::const_iterator motifs_itor = motifs.begin();
243 motifs_itor != motifs.end();
246 glColor3fv(color_mapper.lookup("motif", motifs_itor->sequence).get());
247 draw_box(left, right, seq_x+motifs_itor->start, seq_x+motifs_itor->end,
248 seq_height, annotation_z+1.0);
254 const int STROKE = 2;
257 typedef struct charpoint {
263 {0, -5, PT}, {2.5, 5, PT}, {5, -5, STROKE},
264 {0.75, -2, PT}, {4.25, -2, END}
268 {2.5, -5, PT}, {2.5,5, STROKE}, {0, 5, PT}, {5, 5, END}
272 {5, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
273 {2, -5, PT}, {3, -5, PT}, {5, -3, STROKE},
274 {2.5, -1, PT}, {5, -1,PT}, {5, -5, END}
278 {4.9, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
279 {2, -5, PT}, {3, -5, PT}, {5, -3, END}
282 CP Xdata[] = {{ 0, 5, PT}, {5, -5,STROKE},{0,-5,PT},{5, 5, END}};
283 CP Ndata[] = {{ 0, -5, PT}, {0, 5, PT}, {5, -5, PT}, {5, 5, END}};
285 static void drawLetter(CP *l, GLfloat z)
287 glBegin(GL_LINE_STRIP);
291 glVertex3f(l->x, l->y, z);
294 glVertex3f(l->x, l->y, z);
296 glBegin(GL_LINE_STRIP);
299 glVertex3f(l->x, l->y, z);
304 throw runtime_error("data structure failure");
310 void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
312 // FIXME: basically this needs to be greater than the number of annotations
313 const GLfloat z = 30;
315 glColor3fv(drawColor.get());
317 Sequence::const_iterator seq_itor = sequence_begin(left, right);
318 Sequence::const_iterator seq_end = sequence_end(left, right);
319 Sequence::size_type basepair = 0;
321 assert(seq_end - seq_itor >= 0);
322 while(seq_itor != seq_end)
324 assert ( basepair < seq.size() );
326 glTranslatef( seq_x+leftbase(left) + basepair, seq_y, 1.0 );
327 glScalef(0.1, 1.0, 1.0);
330 drawLetter(Adata, z);
333 drawLetter(Tdata, z);
336 drawLetter(Gdata, z);
339 drawLetter(Cdata, z);
342 drawLetter(Ndata, z);
345 drawLetter(Xdata, z);
354 bool operator==(const GlSequence &left, const GlSequence &right)
356 return ( (left.seq_x == right.seq_x) and
357 (left.seq_y == right.seq_y) and
358 (left.seq_z == right.seq_z) and
359 (left.seq_height == right.seq_height) and
360 (left.drawColor == right.drawColor));