16 #include "alg/glsequence.h"
17 #include "mussa_exceptions.hh"
21 PathScene::PathScene(Mussa* analysis, QWidget *parent) :
27 maxOrtho2d(-50.0, -50, 3000000.0, 300.0),
28 curOrtho2d(maxOrtho2d),
29 viewport_center(((curOrtho2d.right()-curOrtho2d.left())/2)+curOrtho2d.left()),
36 mussaAnalysis = new Mussa;
40 mussaAnalysis = analysis;
45 QSize PathScene::sizeHint() const
47 return QSize(400, 400);
50 float PathScene::left()
52 return maxOrtho2d.left();
55 float PathScene::right()
57 return maxOrtho2d.right();
60 float PathScene::viewportLeft()
62 return curOrtho2d.left();
65 float PathScene::viewportRight()
67 return curOrtho2d.right();
70 float PathScene::viewportCenter()
72 return viewport_center;
75 static float max(float a, float b)
83 static float min(float a, float b)
91 void PathScene::updateViewport(float center, int new_zoom)
93 float max_width = maxOrtho2d.width();
94 if (new_zoom < 1) new_zoom = 1;
95 float new_max_width = max_width / new_zoom;
96 //curOrtho2d.setLeft(max(center-new_max_width, maxOrtho2d.left()));
97 //curOrtho2d.setRight(min(center+new_max_width, maxOrtho2d.right()));
98 curOrtho2d.setLeft(center-new_max_width);
99 curOrtho2d.setRight(center+new_max_width);
100 emit viewportChanged();
103 void PathScene::setViewportX(float x)
105 cout << "setViewportX " << x << " " << curOrtho2d.left() << endl;
106 //hopefully this calculates a sufficiently reasonable == for a float
107 if (x != viewport_center )
109 updateViewport(x, zoom);
115 void PathScene::setZoom(int new_zoom)
117 if (zoom != new_zoom) {
118 // try to figure out where we should be now?
119 updateViewport(viewport_center, new_zoom);
125 void PathScene::setClipPlane(int newZ)
127 if (clipZ != (double) newZ){
128 clipZ = (double) newZ;
133 void PathScene::loadMupa()
135 QString caption("Load a mussa parameter file");
136 QString filter("Mussa Parameters (*.mupa)");
137 QString mupa_path = QFileDialog::getOpenFileName(this,
142 if (mupa_path.isNull())
144 // try to load safely
146 Mussa *m = new Mussa;
147 m->load_mupa_file(mupa_path.toStdString());
148 m->analyze(0, 0, Mussa::TransitiveNway, 0.0);
149 // only switch mussas if we loaded without error
150 delete mussaAnalysis;
153 } catch (mussa_load_error e) {
154 QString msg("Unable to load ");
158 QMessageBox::warning(this, "Load Parameter", msg);
160 assert (mussaAnalysis != 0);
163 void PathScene::loadSavedAnalysis()
165 QString caption("Load a previously run analysis");
166 QString muway_dir = QFileDialog::getExistingDirectory(this,
168 QDir::currentPath());
170 if (muway_dir.isNull())
172 // try to safely load
174 Mussa *m = new Mussa;
175 m->load(muway_dir.toStdString());
176 // only switch mussas if we loaded without error
177 delete mussaAnalysis;
180 } catch (mussa_load_error e) {
181 QString msg("Unable to load ");
185 QMessageBox::warning(this, "Load Parameter", msg);
187 assert (mussaAnalysis != 0);
190 void PathScene::setSoftThreshold(int threshold)
192 if (mussaAnalysis->get_threshold() != threshold) {
193 mussaAnalysis->set_soft_thres(threshold);
194 mussaAnalysis->nway();
201 void PathScene::initializeGL()
203 glEnable(GL_DEPTH_TEST);
204 glClearColor(1.0, 1.0, 1.0, 0.0);
205 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
206 glShadeModel(GL_FLAT);
209 void PathScene::resizeGL(int width, int height)
211 viewport_width = width;
212 viewport_height = height;
213 assert (geometry().width() == width);
214 assert (geometry().height() == height);
215 glViewport(0, 0, (GLsizei)width, (GLsizei)height);
216 glMatrixMode(GL_PROJECTION);
218 // I'm abusing this as Qt and OpenGL disagree about the direction of the
220 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
221 curOrtho2d.top(), curOrtho2d.bottom(),
225 void PathScene::paintGL()
227 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
230 glMatrixMode(GL_PROJECTION);
232 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
233 curOrtho2d.top(), curOrtho2d.bottom(),
235 glMatrixMode(GL_MODELVIEW);
238 glDepthMask(GL_FALSE);
239 if (selectedMode && !drawingBand) {
240 glColor4f(0.4, 0.4, 0.4, 0.9);
241 glRectf(previousBand.x(), previousBand.y(),
242 previousBand.right(), previousBand.bottom());
243 cout << "x: " << previousBand.x() << " r:" << previousBand.right() << endl;
245 glDepthMask(GL_TRUE);
251 void PathScene::updateScene()
253 // Delete old glsequences
254 // FIXME: does this actually free the memory from the new'ed GlSequences?
257 // save a reference to our GlSequences
259 float y = mussaAnalysis->sequences().size() * 100 ;
260 float max_base_pairs = 0;
261 typedef vector<Sequence>::const_iterator seqs_itor_t;
262 for(seqs_itor_t seq_itor = mussaAnalysis->sequences().begin();
263 seq_itor != mussaAnalysis->sequences().end();
267 gl_seq = new GlSequence(*seq_itor);
270 if (gl_seq->width() > max_base_pairs ) max_base_pairs = gl_seq->width();
271 tracks.push_back( *gl_seq );
273 maxOrtho2d.setWidth(max_base_pairs + 100.0);
274 maxOrtho2d.setHeight((mussaAnalysis->sequences().size()) * 100 );
275 curOrtho2d = maxOrtho2d;
276 viewport_center = (curOrtho2d.right()-curOrtho2d.left())/2+curOrtho2d.left();
279 void PathScene::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
281 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
289 selectedPaths.clear();
290 selectedPaths.reserve(pathz_count);
291 selectedPaths.insert(selectedPaths.begin(), pathz_count, false);
292 selectedTrack = 0x7fffffff;
294 std::cout << "hits = " << hits << " " << pathz_count << std::endl;
295 ptr = (GLuint *) buffer;
298 for (GLuint i=0; i < hits; ++i)
300 if ((i + 5) > bufsize) {
301 std::clog << "*** selection overflow***" << std::endl;
304 z1 = ((float)*ptr++)/0x7fffffff;
305 z2 = ((float)*ptr++)/0x7fffffff;
309 std::clog << "wrong number of glNames, selection is having trouble ";
310 std::clog << " got " << names << std::endl;
311 std::clog << " names: " ;
312 for (GLuint j = 0 ; j < names-2; ++j) {
313 std::clog << *ptr++ << " ";
320 selectedPaths[objid] = true;
323 if (objid < selectedTrack) {
324 selectedTrack = objid;
332 void PathScene::mousePressEvent( QMouseEvent *e)
335 std::cout << "x=" << e->x() << " y=" << e->y() << std::endl;
337 selectedMode = false;
338 bandOrigin = e->pos();
340 rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
342 rubberBand->setGeometry(QRect(bandOrigin, QSize()));
346 void PathScene::mouseMoveEvent( QMouseEvent *e)
349 rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
352 void dumpRect(const QRect &r)
354 std::cout << "x=" << r.x() << " y=" << r.y()
355 << " w=" << r.width() << " h=" << r.height() << std::endl;
358 void PathScene::mouseReleaseEvent( QMouseEvent *e)
362 QRect r = QRect(bandOrigin, e->pos()).normalized();
363 bandOrigin = r.topLeft();
364 std::cout << "band ";
367 std::cout << "window ";
368 dumpRect(geometry());
370 GLfloat x_scale = curOrtho2d.width()/((float)geometry().width());
371 GLfloat y_scale = curOrtho2d.height()/((float)geometry().height());
372 GLfloat x_left = curOrtho2d.left() + (r.x()*x_scale);
373 GLfloat x_right = x_left + r.width() * x_scale;
374 // using QRectF with Y axis swapped
375 GLfloat y_top = curOrtho2d.bottom()-(r.y()*y_scale);
376 GLfloat y_bottom = y_top - r.height() * y_scale;
377 previousBand.setCoords(x_left, y_top, x_right, y_bottom);
378 std::cout << x_left << " " << x_right << " " << y_top << " " << y_bottom
381 // hopefully this will make a buffer big enough to receive
382 // everything being selected
383 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
384 const GLuint select_buf_size = 1 + 5 * (pathz_count + tracks.size()) ;
385 GLuint selectBuf[select_buf_size];
386 glSelectBuffer(select_buf_size, selectBuf);
389 (void)glRenderMode(GL_SELECT);
391 glMatrixMode(GL_PROJECTION);
393 glOrtho(x_left, x_right, y_top, y_bottom, -50.0, 50.0);
394 glMatrixMode(GL_MODELVIEW);
401 hits = glRenderMode(GL_RENDER);
402 processSelection(hits, selectBuf, select_buf_size);
404 resizeGL(geometry().width(), geometry().height());
409 // openGl rendering code
411 void PathScene::draw_tracks() const
415 for (vector<GlSequence>::size_type i = 0; i != tracks.size(); ++i )
418 tracks[i].draw(curOrtho2d.left(), curOrtho2d.right());
424 void PathScene::draw_lines() const
430 bool reversed = false;
431 bool prevReversed = false;
432 const NwayPaths& nway = mussaAnalysis->paths();
436 vector<GlSequence>::const_iterator track_itor;
438 typedef list<ExtendedConservedPath> conserved_paths;
439 typedef conserved_paths::const_iterator const_conserved_paths_itor;
440 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
441 path_itor != nway.refined_pathz.end();
442 ++path_itor, ++objid)
444 track_itor = tracks.begin();
445 // since we were drawing to the start of a window, and opengl lines
446 // are centered around the two connecting points our lines were slightly
447 // offset. the idea of window_offset is to adjust them to the
448 // right for forward compliment or left for reverse compliment
449 // FIXME: figure out how to unit test these computations
450 GLfloat window_offset = (path_itor->window_size)/2.0;
452 glBegin(GL_LINE_STRIP);
453 for (vector<int>::const_iterator sp_itor = path_itor->begin();
454 sp_itor != path_itor->end();
455 ++sp_itor, ++track_itor)
459 // at some point when we modify the pathz data structure to keep
460 // track of the score we can put grab the depth here.
462 // are we reverse complimented?
467 x = -x; // make positive
470 x += window_offset; // move right for forward compliment
472 x -= window_offset; // move left for reverse compliment
473 // the following boolean implements logical xor
474 if ( (reversed || prevReversed) && (!reversed || !prevReversed)) {
475 // we have a different orientation
476 if (not selectedMode or selectedPaths[objid] == true) {
477 // if we have nothing selected, or we're the highlight, be bright
478 glColor3f(0.0, 0.0, 1.0);
481 glColor3f(0.5, 0.5, 1.0);
484 // both current and previous path have the same orientation
485 if (not selectedMode or selectedPaths[objid] == true) {
486 glColor3f(1.0, 0.0, 0.2);
488 glColor3f(1.0, 0.5, 0.5);
491 prevReversed = reversed;
492 glVertex3f(x, y, 0.0);
495 glLoadName(++pathid);
500 GLuint PathScene::make_line_list()
502 GLuint line_list = glGenLists(1);
503 glNewList(line_list, GL_COMPILE);
512 void PathScene::mussaesque()
514 //static GLuint theLines = make_line_list();
517 glPushName(MussaPaths);
518 //glCallList(theLines);
520 glLoadName(MussaTracks);