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