track labels
[mussa.git] / alg / glsequence.cpp
1 #include "alg/glsequence.hpp"
2
3 #include <iostream>
4 #include <cassert>
5 #include <math.h>
6 #include <stdexcept>
7 using namespace std;
8
9 GlSequence::GlSequence(const Sequence &s, AnnotationColors& cm) 
10   : seq(s),
11     seq_x(0.0), 
12     seq_y(0.0), 
13     seq_z(1.0), 
14     seq_height(12.0),
15     color_mapper(cm),
16     drawColor(0.0, 0.0, 0.0),
17     char_pix_per_world_unit(5.0)
18 {
19 }
20
21 GlSequence::GlSequence(const GlSequence &s)
22   : seq(s.seq),
23     seq_x(s.seq_x),
24     seq_y(s.seq_y),
25     seq_z(s.seq_z),
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)
30 {
31 }
32
33 GlSequence &GlSequence::operator=(const GlSequence & s)
34 {
35   if (this != &s) {
36     const_cast<Sequence &>(seq) = s.seq;
37     seq_x = s.seq_x;
38     seq_y = s.seq_y;
39     seq_z = s.seq_z;
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);
44   }
45   return *this;
46 }
47
48 const Sequence& GlSequence::sequence() const
49 {
50   return seq;
51 }
52
53 void GlSequence::setX(GLfloat value)
54 {
55   seq_x = value;
56 }
57
58 GLfloat GlSequence::x() const
59 {
60   return seq_x;
61 }
62
63 void GlSequence::setY(GLfloat value)
64 {
65   seq_y = value;
66 }
67
68 GLfloat GlSequence::y() const
69 {
70   return seq_y;
71 }
72
73 GLfloat GlSequence::height() const
74 {
75   return seq_height;
76 }
77
78 GLfloat GlSequence::length() const
79 {
80   return seq.size();
81 }
82
83 Sequence::size_type GlSequence::leftbase(GLfloat left) const
84 {
85   assert (seq_x == 0);
86   left = ceil(left);
87   if (left < seq_x)
88     return 0;
89   else if (left > seq.size() )
90     return seq.size();
91   else
92     return (Sequence::size_type)left;
93 }
94
95 Sequence::size_type GlSequence::rightbase(GLfloat right) const
96 {
97   assert (seq_x == 0);
98   right = floor(right);
99   if (right > seq.size())
100     return seq.size();
101   else
102     return (Sequence::size_type)right;
103 }
104
105 Sequence::const_iterator GlSequence::sequence_begin() const
106 {
107   return seq.begin();
108 }
109
110 Sequence::const_iterator GlSequence::sequence_end() const
111 {
112   return seq.end();
113 }
114
115 Sequence::const_iterator 
116 GlSequence::sequence_begin(GLfloat left, GLfloat right) const
117 {
118   // the following code will be wrong when sequences can be slid around
119   // so make sure we break.
120   assert (seq_x == 0);
121
122   if ( leftbase(left) > seq.size() or left > right )
123     return seq.end();
124   else
125     return seq.begin() + leftbase(left);
126 }
127
128 Sequence::const_iterator 
129 GlSequence::sequence_end(GLfloat left, GLfloat right) const
130 {
131   // the following code will be wrong when sequences can be slid around
132   // so make sure we break.
133   assert (seq_x == 0);
134
135   if ( rightbase(right) > seq.size() or left > right )
136     return seq.end();
137   else
138     return seq.begin() + rightbase(right); 
139 }
140
141
142 //! set default track draw color 
143 void GlSequence::setColor(Color &c)
144 {
145   drawColor = c;
146 }
147
148 //! get default track draw color
149 Color GlSequence::color()
150 {
151   return drawColor;
152 }
153
154
155 int GlSequence::get_viewport_pixel_width()
156 {
157   int viewport[4];
158   glGetIntegerv(GL_VIEWPORT, viewport);
159   return viewport[3]; // grab the viewport width
160 }
161
162 bool GlSequence::is_sequence_renderable(GLfloat left, 
163                                         GLfloat right, 
164                                         int viewport_width) const
165 {
166   // if called with default argument, go get the viewable width
167   if (viewport_width == -1) {
168     viewport_width = get_viewport_pixel_width();
169   }
170   GLfloat world_width = right - left;
171   GLfloat pixels_needed = (char_pix_per_world_unit * world_width);
172
173   // if the number of pixels taken up by rendering the characters 
174   // that'd show up in the current ortho width is less than the window
175   // width we can actually draw something 
176    return pixels_needed < viewport_width;
177 }
178
179
180 void GlSequence::draw(GLfloat left, GLfloat right) const
181 {
182   if ( not is_sequence_renderable(left, right) ) {
183     draw_track(left, right);
184   } else {
185     draw_sequence(left, right);
186   }
187   draw_annotations(left, right);
188 }
189
190 void GlSequence::draw_box(GLfloat left, GLfloat right, 
191                           GLfloat height, GLfloat z) const
192 {
193   GLfloat offset = height/2.0;
194   GLfloat top = seq_y + offset;
195   GLfloat bottom = seq_y - offset;
196     
197   glBegin(GL_QUADS);
198     glVertex3f(left,  top,    z);
199     glVertex3f(left,  bottom, z);
200     glVertex3f(right, bottom, z);
201     glVertex3f(right, top,    z);
202   glEnd();
203 }
204
205 void GlSequence::draw_track(GLfloat left, GLfloat right) const
206 {
207   glColor3fv(drawColor.get());
208   // draw main sequence track
209   draw_box(seq_x, seq_x+seq.size(), seq_height, 0.0);
210 }
211
212 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
213 {
214   // draw annotations
215   glLineWidth(seq_height);
216   GLfloat annotation_z = seq_z + 1.0;
217   const std::list<annot>& annots = seq.annotations();
218   const std::list<motif>& motifs = seq.motifs();
219   for (std::list<annot>::const_iterator annot_itor = annots.begin();
220        annot_itor != annots.end();
221        ++annot_itor, ++annotation_z)
222   {
223     glColor3f(0.0, 0.5, 0.0);
224     draw_box(seq_x+annot_itor->start, seq_x+annot_itor->end, 
225              seq_height, annotation_z);
226   }
227   // if motifs?
228   for (std::list<motif>::const_iterator motifs_itor = motifs.begin();
229        motifs_itor != motifs.end();
230        ++motifs_itor, ++annotation_z)
231   {
232     glColor3fv(color_mapper.lookup("motif", motifs_itor->sequence).get());
233     draw_box(seq_x+motifs_itor->start, seq_x+motifs_itor->end, 
234              seq_height, annotation_z);
235   }
236
237 }
238
239 const int PT = 1;
240 const int STROKE = 2;
241 const int END =3;
242
243 typedef struct charpoint {
244   GLfloat x, y;
245   int type;
246 } CP;
247
248 CP Adata[] = {
249   {0, -5, PT}, {2.5, 5, PT}, {5, -5, STROKE}, 
250   {0.75, -2, PT}, {4.25, -2, END}
251 };
252
253 CP Tdata[] = {
254   {2.5, -5, PT}, {2.5,5, STROKE}, {0, 5, PT}, {5, 5, END}
255 };
256
257 CP Gdata[] = {
258   {5, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
259   {2, -5, PT}, {3, -5, PT}, {5, -3, STROKE}, 
260   {2.5, -1, PT}, {5, -1,PT}, {5, -5, END}
261 };
262
263 CP Cdata[] = {
264   {4.9, 3, PT}, {3, 5, PT}, {2, 5, PT}, {0, 3, PT}, {0, -3, PT},
265   {2, -5, PT}, {3, -5, PT}, {5, -3, END}
266 };
267
268 CP Xdata[] = {{ 0, 5, PT}, {5, -5,STROKE},{0,-5,PT},{5, 5, END}};
269 CP Ndata[] = {{ 0, -5, PT}, {0, 5, PT}, {5, -5, PT}, {5, 5, END}};
270
271 static void drawLetter(CP *l, GLfloat z)
272 {
273   glBegin(GL_LINE_STRIP);
274   while(1) {
275     switch (l->type) {
276       case PT:
277         glVertex3f(l->x, l->y, z);
278         break;
279       case STROKE:
280         glVertex3f(l->x, l->y, z);
281         glEnd();
282         glBegin(GL_LINE_STRIP);
283         break;
284       case END:
285         glVertex3f(l->x, l->y, z);
286         glEnd();
287         return;
288         break;
289       default:
290          throw runtime_error("data structure failure");
291     }
292     l++;
293   }
294 }
295
296 void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
297 {
298   // FIXME: basically this needs to be greater than the number of annotations
299   const GLfloat z = 30;
300   glLineWidth(0.3);
301   glColor3fv(drawColor.get());
302
303   Sequence::const_iterator seq_itor = sequence_begin(left, right);
304   Sequence::const_iterator seq_end = sequence_end(left, right);
305   Sequence::size_type basepair = 0;
306
307   assert(seq_end - seq_itor >= 0);
308   while(seq_itor != seq_end)
309   {
310     assert ( basepair < seq.size() );
311     glPushMatrix();
312     glTranslatef( leftbase(left) + basepair, seq_y, 1.0 );
313     glScalef(0.1, 1.0, 1.0);
314     switch (*seq_itor) {
315       case 'A': case 'a':
316         drawLetter(Adata, z);
317         break;
318       case 'T': case 't':
319         drawLetter(Tdata, z);
320         break;
321       case 'G': case 'g':
322         drawLetter(Gdata, z);
323         break;
324       case 'C': case 'c':
325         drawLetter(Cdata, z);
326         break;
327       case 'N': case 'n':
328         drawLetter(Ndata, z);
329         break;
330       default:
331         drawLetter(Xdata, z);
332         break;
333     }
334     glPopMatrix();
335     ++seq_itor;
336     ++basepair;
337   }
338 }
339
340 bool operator==(const GlSequence &left, const GlSequence &right)
341 {
342   return ( (left.seq_x == right.seq_x) and
343            (left.seq_y == right.seq_y) and
344            (left.seq_z == right.seq_z) and
345            (left.seq_height == right.seq_height) and
346            (left.drawColor == right.drawColor));
347 }
348