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 void PathScene::updateViewport(float center, int new_zoom)
77 float max_width = maxOrtho2d.width();
78 if (new_zoom < 1) new_zoom = 1;
79 float new_max_width = max_width / new_zoom;
80 //curOrtho2d.setLeft(max(center-new_max_width, maxOrtho2d.left()));
81 //curOrtho2d.setRight(min(center+new_max_width, maxOrtho2d.right()));
82 curOrtho2d.setLeft(center-new_max_width);
83 curOrtho2d.setRight(center+new_max_width);
84 emit viewportChanged();
87 void PathScene::setViewportX(float x)
89 cout << "setViewportX " << x << " " << curOrtho2d.left() << endl;
90 //hopefully this calculates a sufficiently reasonable == for a float
91 if (x != viewport_center )
93 updateViewport(x, zoom);
99 void PathScene::setZoom(int new_zoom)
101 if (zoom != new_zoom) {
102 // try to figure out where we should be now?
103 updateViewport(viewport_center, new_zoom);
109 void PathScene::setClipPlane(int newZ)
111 if (clipZ != (double) newZ){
112 clipZ = (double) newZ;
117 void PathScene::loadMupa()
119 QString caption("Load a mussa parameter file");
120 QString filter("Mussa Parameters (*.mupa)");
121 QString mupa_path = QFileDialog::getOpenFileName(this,
126 if (mupa_path.isNull())
128 // try to load safely
130 Mussa *m = new Mussa;
131 m->load_mupa_file(mupa_path.toStdString());
132 m->analyze(0, 0, Mussa::TransitiveNway, 0.0);
133 // only switch mussas if we loaded without error
134 delete mussaAnalysis;
137 } catch (mussa_load_error e) {
138 QString msg("Unable to load ");
142 QMessageBox::warning(this, "Load Parameter", msg);
144 assert (mussaAnalysis != 0);
147 void PathScene::loadSavedAnalysis()
149 QString caption("Load a previously run analysis");
150 QString muway_dir = QFileDialog::getExistingDirectory(this,
152 QDir::currentPath());
154 if (muway_dir.isNull())
156 // try to safely load
158 Mussa *m = new Mussa;
159 m->load(muway_dir.toStdString());
160 // only switch mussas if we loaded without error
161 delete mussaAnalysis;
164 } catch (mussa_load_error e) {
165 QString msg("Unable to load ");
169 QMessageBox::warning(this, "Load Parameter", msg);
171 assert (mussaAnalysis != 0);
174 void PathScene::setSoftThreshold(int threshold)
176 if (mussaAnalysis->get_threshold() != threshold) {
177 mussaAnalysis->set_soft_thres(threshold);
178 mussaAnalysis->nway();
185 void PathScene::initializeGL()
187 glEnable(GL_DEPTH_TEST);
188 glClearColor(1.0, 1.0, 1.0, 0.0);
189 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
190 glShadeModel(GL_FLAT);
193 void PathScene::resizeGL(int width, int height)
195 viewport_width = width;
196 viewport_height = height;
197 assert (geometry().width() == width);
198 assert (geometry().height() == height);
199 glViewport(0, 0, (GLsizei)width, (GLsizei)height);
200 glMatrixMode(GL_PROJECTION);
202 // I'm abusing this as Qt and OpenGL disagree about the direction of the
204 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
205 curOrtho2d.top(), curOrtho2d.bottom(),
209 void PathScene::paintGL()
211 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
214 glMatrixMode(GL_PROJECTION);
216 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
217 curOrtho2d.top(), curOrtho2d.bottom(),
219 glMatrixMode(GL_MODELVIEW);
222 glDepthMask(GL_FALSE);
223 if (selectedMode && !drawingBand) {
224 glColor4f(0.4, 0.4, 0.4, 0.9);
225 glRectf(previousBand.x(), previousBand.y(),
226 previousBand.right(), previousBand.bottom());
227 cout << "x: " << previousBand.x() << " r:" << previousBand.right() << endl;
229 glDepthMask(GL_TRUE);
235 void PathScene::updateScene()
237 // Delete old glsequences
238 // FIXME: does this actually free the memory from the new'ed GlSequences?
241 // save a reference to our GlSequences
243 float y = mussaAnalysis->sequences().size() * 100 ;
244 float max_base_pairs = 0;
245 typedef vector<Sequence>::const_iterator seqs_itor_t;
246 for(seqs_itor_t seq_itor = mussaAnalysis->sequences().begin();
247 seq_itor != mussaAnalysis->sequences().end();
251 gl_seq = new GlSequence(*seq_itor);
254 if (gl_seq->length() > max_base_pairs ) max_base_pairs = gl_seq->length();
255 tracks.push_back( *gl_seq );
257 maxOrtho2d.setWidth(max_base_pairs + 100.0);
258 maxOrtho2d.setHeight((mussaAnalysis->sequences().size()) * 100 );
259 curOrtho2d = maxOrtho2d;
260 viewport_center = (curOrtho2d.right()-curOrtho2d.left())/2+curOrtho2d.left();
263 void PathScene::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
265 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
273 selectedPaths.clear();
274 selectedPaths.reserve(pathz_count);
275 selectedPaths.insert(selectedPaths.begin(), pathz_count, false);
276 selectedTrack = 0x7fffffff;
278 std::cout << "hits = " << hits << " " << pathz_count << std::endl;
279 ptr = (GLuint *) buffer;
282 for (GLuint i=0; i < hits; ++i)
284 if ((i + 5) > bufsize) {
285 std::clog << "*** selection overflow***" << std::endl;
288 z1 = ((float)*ptr++)/0x7fffffff;
289 z2 = ((float)*ptr++)/0x7fffffff;
293 std::clog << "wrong number of glNames, selection is having trouble ";
294 std::clog << " got " << names << std::endl;
295 std::clog << " names: " ;
296 for (GLuint j = 0 ; j < names-2; ++j) {
297 std::clog << *ptr++ << " ";
304 selectedPaths[objid] = true;
307 if (objid < selectedTrack) {
308 selectedTrack = objid;
316 void PathScene::mousePressEvent( QMouseEvent *e)
319 std::cout << "x=" << e->x() << " y=" << e->y() << std::endl;
321 selectedMode = false;
322 bandOrigin = e->pos();
324 rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
326 rubberBand->setGeometry(QRect(bandOrigin, QSize()));
330 void PathScene::mouseMoveEvent( QMouseEvent *e)
333 rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
336 void dumpRect(const QRect &r)
338 std::cout << "x=" << r.x() << " y=" << r.y()
339 << " w=" << r.width() << " h=" << r.height() << std::endl;
342 void PathScene::mouseReleaseEvent( QMouseEvent *e)
346 QRect r = QRect(bandOrigin, e->pos()).normalized();
347 bandOrigin = r.topLeft();
348 std::cout << "band ";
351 std::cout << "window ";
352 dumpRect(geometry());
354 GLfloat x_scale = curOrtho2d.width()/((float)geometry().width());
355 GLfloat y_scale = curOrtho2d.height()/((float)geometry().height());
356 GLfloat x_left = curOrtho2d.left() + (r.x()*x_scale);
357 GLfloat x_right = x_left + r.width() * x_scale;
358 // using QRectF with Y axis swapped
359 GLfloat y_top = curOrtho2d.bottom()-(r.y()*y_scale);
360 GLfloat y_bottom = y_top - r.height() * y_scale;
361 previousBand.setCoords(x_left, y_top, x_right, y_bottom);
362 std::cout << x_left << " " << x_right << " " << y_top << " " << y_bottom
365 // hopefully this will make a buffer big enough to receive
366 // everything being selected
367 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
368 const GLuint select_buf_size = 1 + 5 * (pathz_count + tracks.size()) ;
369 GLuint selectBuf[select_buf_size];
370 glSelectBuffer(select_buf_size, selectBuf);
373 (void)glRenderMode(GL_SELECT);
375 glMatrixMode(GL_PROJECTION);
377 glOrtho(x_left, x_right, y_top, y_bottom, -50.0, 50.0);
378 glMatrixMode(GL_MODELVIEW);
385 hits = glRenderMode(GL_RENDER);
386 processSelection(hits, selectBuf, select_buf_size);
388 resizeGL(geometry().width(), geometry().height());
393 // openGl rendering code
395 void PathScene::draw_tracks() const
399 for (vector<GlSequence>::size_type i = 0; i != tracks.size(); ++i )
402 tracks[i].draw(curOrtho2d.left(), curOrtho2d.right());
408 void PathScene::draw_lines() const
414 bool reversed = false;
415 bool prevReversed = false;
416 const NwayPaths& nway = mussaAnalysis->paths();
420 vector<GlSequence>::const_iterator track_itor;
422 typedef list<ExtendedConservedPath> conserved_paths;
423 typedef conserved_paths::const_iterator const_conserved_paths_itor;
424 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
425 path_itor != nway.refined_pathz.end();
426 ++path_itor, ++objid)
428 track_itor = tracks.begin();
429 // since we were drawing to the start of a window, and opengl lines
430 // are centered around the two connecting points our lines were slightly
431 // offset. the idea of window_offset is to adjust them to the
432 // right for forward compliment or left for reverse compliment
433 // FIXME: figure out how to unit test these computations
434 GLfloat window_offset = (path_itor->window_size)/2.0;
436 glBegin(GL_LINE_STRIP);
437 for (vector<int>::const_iterator sp_itor = path_itor->begin();
438 sp_itor != path_itor->end();
439 ++sp_itor, ++track_itor)
443 // at some point when we modify the pathz data structure to keep
444 // track of the score we can put grab the depth here.
446 // are we reverse complimented?
451 x = -x; // make positive
454 x += window_offset; // move right for forward compliment
456 x -= window_offset; // move left for reverse compliment
457 // the following boolean implements logical xor
458 if ( (reversed || prevReversed) && (!reversed || !prevReversed)) {
459 // we have a different orientation
460 if (not selectedMode or selectedPaths[objid] == true) {
461 // if we have nothing selected, or we're the highlight, be bright
462 glColor3f(0.0, 0.0, 1.0);
465 glColor3f(0.7, 0.7, 1.0);
468 // both current and previous path have the same orientation
469 if (not selectedMode or selectedPaths[objid] == true) {
470 glColor3f(1.0, 0.0, 0.0);
472 glColor3f(1.0, 0.7, 0.7);
475 prevReversed = reversed;
476 glVertex3f(x, y, 0.0);
479 glLoadName(++pathid);
484 GLuint PathScene::make_line_list()
486 GLuint line_list = glGenLists(1);
487 glNewList(line_list, GL_COMPILE);
496 void PathScene::mussaesque()
498 //static GLuint theLines = make_line_list();
501 glPushName(MussaPaths);
502 //glCallList(theLines);
504 glLoadName(MussaTracks);