16 #include "qui/GlSequence.h"
20 PathScene::PathScene(Mussa* analysis, QWidget *parent) :
25 maxOrtho2d(-50.0, -50, 3000000.0, 300.0),
26 curOrtho2d(maxOrtho2d),
33 mussaAnalysis = new Mussa;
37 mussaAnalysis = analysis;
42 QSize PathScene::sizeHint() const
44 return QSize(400, 400);
47 void PathScene::setX(int newX)
55 static float max(float a, float b)
63 static float min(float a, float b)
71 void PathScene::setZoom(int newZoom)
73 std::cout << newZoom << " " << zoom << std::endl;
74 if (zoom != newZoom) {
75 // try to figure out where we should be now?
77 float width = maxOrtho2d.width();
78 std::cout << "max width " << width
79 << " max left " << maxOrtho2d.left()
80 << " max right " << maxOrtho2d.right() << std::endl;
81 float newWidth = width / newZoom;
82 std::cout << "new width" << newWidth<< std::endl;
83 float center = curOrtho2d.width()/2 + curOrtho2d.left();
84 std::cout << "l: " << curOrtho2d.left() << " r: " << curOrtho2d.right()
85 << " ctr: " << center << std::endl;
86 curOrtho2d.setLeft(max(center-newWidth, maxOrtho2d.left()));
87 curOrtho2d.setRight(min(center+newWidth, maxOrtho2d.right()));
88 std::cout << "nl: " << curOrtho2d.left() << " nr: " << curOrtho2d.right()
95 void PathScene::setClipPlane(int newZ)
97 if (clipZ != (double) newZ){
98 clipZ = (double) newZ;
103 void PathScene::loadMupa()
105 QString caption("Load a mussa parameter file");
106 QString filter("Mussa Parameters (*.mupa)");
107 QString mupa_path = QFileDialog::getOpenFileName(this,
112 if (mupa_path.isNull())
114 // try to load safely
116 Mussa *m = new Mussa;
117 m->load_mupa_file(mupa_path.toStdString());
118 m->analyze(0, 0, Mussa::TransitiveNway, 0.0);
119 // only switch mussas if we loaded without error
120 delete mussaAnalysis;
123 } catch (mussa_load_error e) {
124 QString msg("Unable to load ");
128 QMessageBox::warning(this, "Load Parameter", msg);
130 assert (mussaAnalysis != 0);
133 void PathScene::loadSavedAnalysis()
135 QString caption("Load a previously run analysis");
136 QString muway_dir = QFileDialog::getExistingDirectory(this,
138 QDir::currentPath());
140 if (muway_dir.isNull())
142 // try to safely load
144 Mussa *m = new Mussa;
145 m->load(muway_dir.toStdString());
146 // only switch mussas if we loaded without error
147 delete mussaAnalysis;
150 } catch (mussa_load_error e) {
151 QString msg("Unable to load ");
155 QMessageBox::warning(this, "Load Parameter", msg);
157 assert (mussaAnalysis != 0);
160 void PathScene::setSoftThreshold(int threshold)
162 if (mussaAnalysis->get_threshold() != threshold) {
163 mussaAnalysis->set_soft_thres(threshold);
164 mussaAnalysis->nway();
171 void PathScene::initializeGL()
173 glEnable(GL_DEPTH_TEST);
174 glClearColor(1.0, 1.0, 1.0, 0.0);
175 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
176 glShadeModel(GL_FLAT);
179 void PathScene::resizeGL(int width, int height)
181 glViewport(0, 0, (GLsizei)width, (GLsizei)height);
182 glMatrixMode(GL_PROJECTION);
184 // I'm abusing this as Qt and OpenGL disagree about the direction of the
186 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
187 curOrtho2d.top(), curOrtho2d.bottom(),
191 void PathScene::paintGL()
193 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
196 glMatrixMode(GL_PROJECTION);
198 glOrtho(curOrtho2d.left(), curOrtho2d.right(),
199 curOrtho2d.top(), curOrtho2d.bottom(),
201 glMatrixMode(GL_MODELVIEW);
204 glDepthMask(GL_FALSE);
205 if (selectedMode && !drawingBand) {
206 glColor4f(0.4, 0.4, 0.4, 0.9);
207 glRectf(previousBand.x(), previousBand.y(),
208 previousBand.right(), previousBand.bottom());
209 cout << "x: " << previousBand.x() << " r:" << previousBand.right() << endl;
211 glDepthMask(GL_TRUE);
217 void PathScene::updateScene()
219 // Delete old glsequences
220 // FIXME: does this actually free the memory from the new'ed GlSequences?
223 // save a reference to our GlSequences
225 float y = mussaAnalysis->sequences().size() * 100 ;
226 float max_base_pairs = 0;
227 typedef vector<Sequence>::const_iterator seqs_itor_t;
228 for(seqs_itor_t seq_itor = mussaAnalysis->sequences().begin();
229 seq_itor != mussaAnalysis->sequences().end();
233 gl_seq = new GlSequence(*seq_itor);
236 if (gl_seq->width() > max_base_pairs ) max_base_pairs = gl_seq->width();
237 tracks.push_back( *gl_seq );
239 maxOrtho2d.setWidth(max_base_pairs + 100.0);
240 maxOrtho2d.setHeight((mussaAnalysis->sequences().size()) * 100 );
241 curOrtho2d = maxOrtho2d;
244 void PathScene::processSelection(GLuint hits, GLuint buffer[], GLuint bufsize)
246 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
254 selectedPaths.clear();
255 selectedPaths.reserve(pathz_count);
256 selectedPaths.insert(selectedPaths.begin(), pathz_count, false);
257 selectedTrack = 0x7fffffff;
259 std::cout << "hits = " << hits << " " << pathz_count << std::endl;
260 ptr = (GLuint *) buffer;
263 for (GLuint i=0; i < hits; ++i)
265 if ((i + 5) > bufsize) {
266 std::clog << "*** selection overflow***" << std::endl;
269 z1 = ((float)*ptr++)/0x7fffffff;
270 z2 = ((float)*ptr++)/0x7fffffff;
274 std::clog << "wrong number of glNames, selection is having trouble ";
275 std::clog << " got " << names << std::endl;
276 std::clog << " names: " ;
277 for (GLuint j = 0 ; j < names-2; ++j) {
278 std::clog << *ptr++ << " ";
285 selectedPaths[objid] = true;
288 if (objid < selectedTrack) {
289 selectedTrack = objid;
297 void PathScene::mousePressEvent( QMouseEvent *e)
300 std::cout << "x=" << e->x() << " y=" << e->y() << std::endl;
302 selectedMode = false;
303 bandOrigin = e->pos();
305 rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
307 rubberBand->setGeometry(QRect(bandOrigin, QSize()));
311 void PathScene::mouseMoveEvent( QMouseEvent *e)
313 rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
316 void dumpRect(const QRect &r)
318 std::cout << "x=" << r.x() << " y=" << r.y()
319 << " w=" << r.width() << " h=" << r.height() << std::endl;
322 void PathScene::mouseReleaseEvent( QMouseEvent *e)
326 QRect r = QRect(bandOrigin, e->pos()).normalized();
327 bandOrigin = r.topLeft();
328 std::cout << "band ";
331 std::cout << "window ";
332 dumpRect(geometry());
334 GLfloat x_scale = curOrtho2d.width()/((float)geometry().width());
335 GLfloat y_scale = curOrtho2d.height()/((float)geometry().height());
336 GLfloat x_left = curOrtho2d.left() + (r.x()*x_scale);
337 GLfloat x_right = x_left + r.width() * x_scale;
338 // using QRectF with Y axis swapped
339 GLfloat y_top = curOrtho2d.bottom()-(r.y()*y_scale);
340 GLfloat y_bottom = y_top - r.height() * y_scale;
341 previousBand.setCoords(x_left, y_top, x_right, y_bottom);
342 std::cout << x_left << " " << x_right << " " << y_top << " " << y_bottom
345 // hopefully this will make a buffer big enough to receive
346 // everything being selected
347 const size_t pathz_count = mussaAnalysis->paths().refined_pathz.size();
348 const GLuint select_buf_size = 1 + 5 * (pathz_count + tracks.size()) ;
349 GLuint selectBuf[select_buf_size];
350 glSelectBuffer(select_buf_size, selectBuf);
353 (void)glRenderMode(GL_SELECT);
355 glMatrixMode(GL_PROJECTION);
357 glOrtho(x_left, x_right, y_top, y_bottom, -50.0, 50.0);
358 glMatrixMode(GL_MODELVIEW);
365 hits = glRenderMode(GL_RENDER);
366 processSelection(hits, selectBuf, select_buf_size);
368 resizeGL(geometry().width(), geometry().height());
373 // openGl rendering code
375 void PathScene::draw_tracks() const
379 for (vector<GlSequence>::size_type i = 0; i != tracks.size(); ++i )
388 void PathScene::draw_lines() const
394 bool reversed = false;
395 bool prevReversed = false;
396 const NwayPaths& nway = mussaAnalysis->paths();
400 vector<GlSequence>::const_iterator track_itor;
402 typedef list<ExtendedConservedPath> conserved_paths;
403 typedef conserved_paths::const_iterator const_conserved_paths_itor;
404 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
405 path_itor != nway.refined_pathz.end();
406 ++path_itor, ++objid)
408 track_itor = tracks.begin();
409 // since we were drawing to the start of a window, and opengl lines
410 // are centered around the two connecting points our lines were slightly
411 // offset. the idea of window_offset is to adjust them to the
412 // right for forward compliment or left for reverse compliment
413 // FIXME: figure out how to unit test these computations
414 GLfloat window_offset = (path_itor->window_size)/2.0;
416 glBegin(GL_LINE_STRIP);
417 for (vector<int>::const_iterator sp_itor = path_itor->begin();
418 sp_itor != path_itor->end();
419 ++sp_itor, ++track_itor)
423 // at some point when we modify the pathz data structure to keep
424 // track of the score we can put grab the depth here.
426 // are we reverse complimented?
431 x = -x; // make positive
434 x += window_offset; // move right for forward compliment
436 x -= window_offset; // move left for reverse compliment
437 // the following boolean implements logical xor
438 if ( (reversed || prevReversed) && (!reversed || !prevReversed)) {
439 // we have a different orientation
440 if (not selectedMode or selectedPaths[objid] == true) {
441 // if we have nothing selected, or we're the highlight, be bright
442 glColor3f(0.0, 0.0, 1.0);
445 glColor3f(0.5, 0.5, 1.0);
448 // both current and previous path have the same orientation
449 if (not selectedMode or selectedPaths[objid] == true) {
450 glColor3f(1.0, 0.0, 0.2);
452 glColor3f(1.0, 0.5, 0.5);
455 prevReversed = reversed;
456 glVertex3f(x, y, 0.0);
459 glLoadName(++pathid);
464 GLuint PathScene::make_line_list()
466 GLuint line_list = glGenLists(1);
467 glNewList(line_list, GL_COMPILE);
476 void PathScene::mussaesque()
478 //static GLuint theLines = make_line_list();
481 glPushName(MussaPaths);
482 //glCallList(theLines);
484 glLoadName(MussaTracks);