14 #include "qui/PathScene.hpp"
15 #include "alg/glsequence.hpp"
16 #include "mussa_exceptions.hpp"
20 PathScene::PathScene(Mussa* analysis, QWidget *parent) :
26 maxOrtho2d(-50.0, -50, 3000000.0, 300.0),
27 curOrtho2d(maxOrtho2d),
28 viewport_center(((curOrtho2d.right()-curOrtho2d.left())/2)+curOrtho2d.left()),
35 mussaAnalysis = new Mussa;
39 mussaAnalysis = analysis;
44 QSize PathScene::sizeHint() const
46 return QSize(400, 400);
49 float PathScene::left()
51 return maxOrtho2d.left();
54 float PathScene::right()
56 return maxOrtho2d.right();
59 float PathScene::viewportLeft()
61 return curOrtho2d.left();
64 float PathScene::viewportRight()
66 return curOrtho2d.right();
69 float PathScene::viewportCenter()
71 return viewport_center;
74 void PathScene::updateViewport(float center, int new_zoom)
76 float max_width = maxOrtho2d.width();
77 if (new_zoom < 1) new_zoom = 1;
78 float new_max_width = max_width / new_zoom;
79 //curOrtho2d.setLeft(max(center-new_max_width, maxOrtho2d.left()));
80 //curOrtho2d.setRight(min(center+new_max_width, maxOrtho2d.right()));
81 curOrtho2d.setLeft(center-new_max_width);
82 curOrtho2d.setRight(center+new_max_width);
83 emit viewportChanged();
86 void PathScene::setViewportX(float x)
88 //hopefully this calculates a sufficiently reasonable == for a float
89 if (x != viewport_center )
91 updateViewport(x, zoom);
97 void PathScene::setZoom(int new_zoom)
99 if (zoom != new_zoom) {
100 // try to figure out where we should be now?
101 updateViewport(viewport_center, new_zoom);
107 void PathScene::setClipPlane(int newZ)
109 if (clipZ != (double) newZ){
110 clipZ = (double) newZ;
115 void PathScene::loadMupa()
117 QString caption("Load a mussa parameter file");
118 QString filter("Mussa Parameters (*.mupa)");
119 QString mupa_path = QFileDialog::getOpenFileName(this,
124 if (mupa_path.isNull())
126 // try to load safely
128 Mussa *m = new Mussa;
129 m->load_mupa_file(mupa_path.toStdString());
130 m->analyze(0, 0, Mussa::TransitiveNway, 0.0);
131 // only switch mussas if we loaded without error
132 delete mussaAnalysis;
135 } catch (mussa_load_error e) {
136 QString msg("Unable to load ");
140 QMessageBox::warning(this, "Load Parameter", msg);
142 assert (mussaAnalysis != 0);
145 void PathScene::loadSavedAnalysis()
147 QString caption("Load a previously run analysis");
148 QString muway_dir = QFileDialog::getExistingDirectory(this,
150 QDir::currentPath());
152 if (muway_dir.isNull())
154 // try to safely load
156 Mussa *m = new Mussa;
157 m->load(muway_dir.toStdString());
158 // only switch mussas if we loaded without error
159 delete mussaAnalysis;
162 } catch (mussa_load_error e) {
163 QString msg("Unable to load ");
167 QMessageBox::warning(this, "Load Parameter", msg);
169 assert (mussaAnalysis != 0);
172 void PathScene::setSoftThreshold(int threshold)
174 if (mussaAnalysis->get_threshold() != threshold) {
175 mussaAnalysis->set_soft_thres(threshold);
176 mussaAnalysis->nway();
183 void PathScene::initializeGL()
185 glEnable(GL_DEPTH_TEST);
186 glClearColor(1.0, 1.0, 1.0, 0.0);
187 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
188 glShadeModel(GL_FLAT);
191 void PathScene::resizeGL(int width, int height)
193 viewport_width = width;
194 viewport_height = height;
195 assert (geometry().width() == width);
196 assert (geometry().height() == height);
197 glViewport(0, 0, (GLsizei)width, (GLsizei)height);
198 glMatrixMode(GL_PROJECTION);
200 // I'm abusing this as Qt and OpenGL disagree about the direction of the
202 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
203 curOrtho2d.top(), curOrtho2d.bottom(),
207 void PathScene::paintGL()
209 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
212 glMatrixMode(GL_PROJECTION);
214 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
215 curOrtho2d.top(), curOrtho2d.bottom(),
217 glMatrixMode(GL_MODELVIEW);
220 glDepthMask(GL_FALSE);
221 if (selectedMode && !drawingBand) {
222 glColor4f(0.6, 0.6, 0.6, 0.9);
223 glRectf(previousBand.x(), previousBand.y(),
224 previousBand.right(), previousBand.bottom());
226 glDepthMask(GL_TRUE);
232 void PathScene::updateScene()
234 // Delete old glsequences
235 // FIXME: does this actually free the memory from the new'ed GlSequences?
238 // save a reference to our GlSequences
240 float y = mussaAnalysis->sequences().size() * 100 ;
241 float max_base_pairs = 0;
242 typedef vector<Sequence>::const_iterator seqs_itor_t;
243 for(seqs_itor_t seq_itor = mussaAnalysis->sequences().begin();
244 seq_itor != mussaAnalysis->sequences().end();
248 gl_seq = new GlSequence(*seq_itor);
251 if (gl_seq->length() > max_base_pairs ) max_base_pairs = gl_seq->length();
252 tracks.push_back( *gl_seq );
254 maxOrtho2d.setWidth(max_base_pairs + 100.0);
255 maxOrtho2d.setHeight((mussaAnalysis->sequences().size()) * 100 );
256 curOrtho2d = maxOrtho2d;
257 viewport_center = (curOrtho2d.right()-curOrtho2d.left())/2+curOrtho2d.left();
260 void PathScene::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
262 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
270 selectedPaths.clear();
271 selectedPaths.reserve(pathz_count);
272 selectedPaths.insert(selectedPaths.begin(), pathz_count, false);
273 selectedTrack = 0x7fffffff;
275 std::cout << "hits = " << hits << " " << pathz_count << std::endl;
276 ptr = (GLuint *) buffer;
279 for (GLuint i=0; i < hits; ++i)
281 if ((i + 5) > bufsize) {
282 std::clog << "*** selection overflow***" << std::endl;
285 z1 = ((float)*ptr++)/0x7fffffff;
286 z2 = ((float)*ptr++)/0x7fffffff;
290 std::clog << "wrong number of glNames, selection is having trouble ";
291 std::clog << " got " << names << std::endl;
292 std::clog << " names: " ;
293 for (GLuint j = 0 ; j < names-2; ++j) {
294 std::clog << *ptr++ << " ";
301 selectedPaths[objid] = true;
304 if (objid < selectedTrack) {
305 selectedTrack = objid;
313 void PathScene::mousePressEvent( QMouseEvent *e)
316 std::cout << "x=" << e->x() << " y=" << e->y() << std::endl;
318 selectedMode = false;
319 bandOrigin = e->pos();
321 rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
323 rubberBand->setGeometry(QRect(bandOrigin, QSize()));
327 void PathScene::mouseMoveEvent( QMouseEvent *e)
330 rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
333 void dumpRect(const QRect &r)
335 std::cout << "x=" << r.x() << " y=" << r.y()
336 << " w=" << r.width() << " h=" << r.height() << std::endl;
339 void PathScene::mouseReleaseEvent( QMouseEvent *e)
343 QRect r = QRect(bandOrigin, e->pos()).normalized();
344 bandOrigin = r.topLeft();
345 std::cout << "band ";
348 std::cout << "window ";
349 dumpRect(geometry());
351 GLfloat x_scale = curOrtho2d.width()/((float)geometry().width());
352 GLfloat y_scale = curOrtho2d.height()/((float)geometry().height());
353 GLfloat x_left = curOrtho2d.left() + (r.x()*x_scale);
354 GLfloat x_right = x_left + r.width() * x_scale;
355 // using QRectF with Y axis swapped
356 GLfloat y_top = curOrtho2d.bottom()-(r.y()*y_scale);
357 GLfloat y_bottom = y_top - r.height() * y_scale;
358 previousBand.setCoords(x_left, y_top, x_right, y_bottom);
359 std::cout << x_left << " " << x_right << " " << y_top << " " << y_bottom
362 // hopefully this will make a buffer big enough to receive
363 // everything being selected
364 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
365 const GLuint select_buf_size = 1 + 5 * (pathz_count + tracks.size()) ;
366 GLuint selectBuf[select_buf_size];
367 glSelectBuffer(select_buf_size, selectBuf);
370 (void)glRenderMode(GL_SELECT);
372 glMatrixMode(GL_PROJECTION);
374 glOrtho(x_left, x_right, y_top, y_bottom, -50.0, 50.0);
375 glMatrixMode(GL_MODELVIEW);
382 hits = glRenderMode(GL_RENDER);
383 processSelection(hits, selectBuf, select_buf_size);
385 resizeGL(geometry().width(), geometry().height());
390 // openGl rendering code
392 void PathScene::draw_tracks() const
396 for (vector<GlSequence>::size_type i = 0; i != tracks.size(); ++i )
399 tracks[i].draw(curOrtho2d.left(), curOrtho2d.right());
405 void PathScene::draw_lines() const
411 bool reversed = false;
412 bool prevReversed = false;
413 const NwayPaths& nway = mussaAnalysis->paths();
417 vector<GlSequence>::const_iterator track_itor;
419 typedef list<ExtendedConservedPath> conserved_paths;
420 typedef conserved_paths::const_iterator const_conserved_paths_itor;
421 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
422 path_itor != nway.refined_pathz.end();
423 ++path_itor, ++objid)
425 track_itor = tracks.begin();
426 // since we were drawing to the start of a window, and opengl lines
427 // are centered around the two connecting points our lines were slightly
428 // offset. the idea of window_offset is to adjust them to the
429 // right for forward compliment or left for reverse compliment
430 // FIXME: figure out how to unit test these computations
431 GLfloat window_offset = (path_itor->window_size)/2.0;
433 glBegin(GL_LINE_STRIP);
434 for (vector<int>::const_iterator sp_itor = path_itor->begin();
435 sp_itor != path_itor->end();
436 ++sp_itor, ++track_itor)
440 // at some point when we modify the pathz data structure to keep
441 // track of the score we can put grab the depth here.
443 // are we reverse complimented?
448 x = -x; // make positive
451 x += window_offset; // move right for forward compliment
453 x -= window_offset; // move left for reverse compliment
454 // the following boolean implements logical xor
455 if ( (reversed || prevReversed) && (!reversed || !prevReversed)) {
456 // we have a different orientation
457 if (not selectedMode or selectedPaths[objid] == true) {
458 // if we have nothing selected, or we're the highlight, be bright
459 glColor3f(0.0, 0.0, 1.0);
462 glColor3f(0.7, 0.7, 1.0);
465 // both current and previous path have the same orientation
466 if (not selectedMode or selectedPaths[objid] == true) {
467 glColor3f(1.0, 0.0, 0.0);
469 glColor3f(1.0, 0.7, 0.7);
472 prevReversed = reversed;
473 glVertex3f(x, y, -1.0);
476 glLoadName(++pathid);
481 GLuint PathScene::make_line_list()
483 GLuint line_list = glGenLists(1);
484 glNewList(line_list, GL_COMPILE);
493 void PathScene::mussaesque()
495 //static GLuint theLines = make_line_list();
498 glPushName(MussaPaths);
499 //glCallList(theLines);
501 glLoadName(MussaTracks);