2 #include <QApplication>
3 #include <QAssistantClient>
13 #include <QStringList>
17 #include <CoreFoundation/CoreFoundation.h>
20 #include "qui/MussaWindow.hpp"
21 #include "mussa_exceptions.hpp"
26 #include <boost/filesystem/path.hpp>
27 namespace fs = boost::filesystem;
28 #include <boost/bind.hpp>
32 MussaWindow::MussaWindow(Mussa *analysis_, QWidget *parent) :
36 setup_analysis_dialog(this),
38 mussaViewTB("Path Views"),
44 createNewAnalysisAction(0),
45 createSubAnalysisAction(0),
47 loadMotifListAction(0),
49 loadSavedAnalysisAction(0),
50 mussaManualAssistantAction(0),
51 newMussaWindowAction(0),
52 saveMotifListAction(0),
53 showMussaViewToolbarAction(0),
54 toggleMotifsAction(0),
55 saveBrowserPixmapAction(0),
57 viewMussaAlignmentAction(0),
64 //This next setWhatsThis function prevents
65 // a segfault when using WhatsThis feature with
67 //scene->setWhatsThis(tr("Mussa in OpenGL!"));
68 setCentralWidget(&browser);
69 // well updatePosition isn't quite right as we really just need
71 connect(this, SIGNAL(changedAnnotations()), &browser, SLOT(update()));
73 mussaViewTB.addAction(toggleMotifsAction);
74 mussaViewTB.addWidget(&zoom);
76 connect(&zoom, SIGNAL(valueChanged(double)),
77 &browser, SLOT(setZoom(double)));
79 // threshold range is set in updateAnalysis
81 //scene->setClipPlane(20);
82 // FIXME: for when we get the paths drawn at the appropriate depth
83 //connect(&threshold, SIGNAL(thresholdChanged(int)),
84 // this, SLOT(setClipPlane(int)));
85 connect(&threshold, SIGNAL(thresholdChanged(int)),
86 this, SLOT(setSoftThreshold(int)));
87 mussaViewTB.addWidget(&threshold);
89 addToolBar(&mussaViewTB);
91 statusBar()->showMessage("Welcome to mussa", 2000);
92 connect(analysis, SIGNAL(progress(const std::string&, int, int)),
93 this, SLOT(updateProgress(const std::string&, int, int)));
97 MussaWindow::~MussaWindow()
99 if (analysis != 0) delete analysis;
100 aligned_windows.clear();
101 if (motif_editor != 0) delete motif_editor;
102 if (progress_dialog != 0) delete progress_dialog;
104 if (aboutAction != 0) delete aboutAction;
105 if (closeAction != 0) delete closeAction;
106 if (createNewAnalysisAction != 0) delete createNewAnalysisAction;
107 if (createSubAnalysisAction != 0) delete createSubAnalysisAction;
108 if (editMotifsAction != 0) delete editMotifsAction;
109 if (loadMotifListAction != 0) delete loadMotifListAction;
110 if (loadMupaAction != 0) delete loadMupaAction;
111 if (loadSavedAnalysisAction != 0) delete loadSavedAnalysisAction;
112 if (mussaManualAssistantAction != 0) delete mussaManualAssistantAction;
113 if (newMussaWindowAction != 0) delete newMussaWindowAction;
114 if (saveBrowserPixmapAction != 0) delete saveBrowserPixmapAction;
115 if (saveMotifListAction != 0) delete saveMotifListAction;
116 if (showMussaViewToolbarAction != 0) delete showMussaViewToolbarAction;
117 if (toggleMotifsAction != 0) delete toggleMotifsAction;
118 if (whatsThisAction != 0) delete whatsThisAction;
119 if (viewMussaAlignmentAction != 0) delete viewMussaAlignmentAction;
121 if (manualAssistant != 0) delete manualAssistant;
125 void MussaWindow::setAnalysis(Mussa *new_analysis)
127 if (new_analysis != 0) {
128 // only switch mussas if we loaded without error
131 analysis = new_analysis;
132 setWindowTitle(analysis->get_name().c_str());
137 void MussaWindow::setupActions()
139 // we really don't want to run this more than once.
140 assert (closeAction == 0);
142 // the ever popular about box
143 aboutAction = new QAction(tr("&About"), this);
144 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
145 aboutAction->setIcon(QIcon(":/icons/info.png"));
148 closeAction = new QAction(tr("&Close"), this);
149 closeAction->setStatusTip(tr("Close this window"));
150 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
151 closeAction->setIcon(QIcon(":/icons/exit.png"));
153 createNewAnalysisAction = new QAction(tr("Create Analysis"), this);
154 connect(createNewAnalysisAction, SIGNAL(triggered()),
155 this, SLOT(createNewAnalysis()));
156 createNewAnalysisAction->setIcon(QIcon(":/icons/filenew.png"));
158 createSubAnalysisAction = new QAction(tr("Define SubAnalysis"), this);
159 connect(createSubAnalysisAction, SIGNAL(triggered()),
160 this, SLOT(createSubAnalysis()));
162 editMotifsAction = new QAction(tr("Edit Motifs"), this);;
163 connect(editMotifsAction, SIGNAL(triggered()), this, SLOT(editMotifs()));
165 loadMotifListAction = new QAction(tr("Load Motif List"), this);
166 connect(loadMotifListAction, SIGNAL(triggered()),
167 this, SLOT(loadMotifList()));
168 loadMotifListAction->setIcon(QIcon(":/icons/fileopen.png"));
170 loadMupaAction = new QAction(tr("Create Analysis from File"), this);
171 connect(loadMupaAction, SIGNAL(triggered()),
172 this, SLOT(loadMupa()));
173 loadMupaAction->setIcon(QIcon(":/icons/fileopen.png"));
175 loadSavedAnalysisAction = new QAction(tr("Load Existing &Analysis"), this);
176 connect(loadSavedAnalysisAction, SIGNAL(triggered()),
177 this, SLOT(loadSavedAnalysis()));
178 loadSavedAnalysisAction->setIcon(QIcon(":/icons/fileopen.png"));
180 mussaManualAssistantAction = new QAction(tr("Mussagl Manual..."), this);
181 mussaManualAssistantAction->setIcon(QIcon(":/icons/contents.png"));
182 connect(mussaManualAssistantAction, SIGNAL(triggered()),
183 this, SLOT(showManual()));
185 newMussaWindowAction = new QAction(tr("&New Mussa Window"), this);
186 newMussaWindowAction->setStatusTip("open another mussa window to allow comparing results");
187 connect(newMussaWindowAction, SIGNAL(triggered()),
188 this, SLOT(newMussaWindow()));
189 newMussaWindowAction->setIcon(QIcon(":/icons/window_new.png"));
191 saveMotifListAction = new QAction(tr("Save Motifs"), this);
192 connect(saveMotifListAction, SIGNAL(triggered()),
193 this, SLOT(saveMotifList()));
194 saveMotifListAction->setIcon(QIcon(":/icons/filesave.png"));
196 showMussaViewToolbarAction = new QAction(tr("Show Toolbar"), this);
197 connect(showMussaViewToolbarAction, SIGNAL(triggered()),
198 this, SLOT(showMussaToolbar()));
199 showMussaViewToolbarAction->setCheckable(true);
200 showMussaViewToolbarAction->setChecked(true);
202 toggleMotifsAction = new QAction(tr("Toggle Motifs"), this);
203 connect(toggleMotifsAction, SIGNAL(triggered()),
204 this, SLOT(toggleMotifs()));
205 toggleMotifsAction->setCheckable(true);
206 toggleMotifsAction->setIcon(QIcon(":/icons/motif_icon.png"));
207 toggleMotifsAction->setWhatsThis(tr("Toggle motif annotations on/off\n\n"
208 "You can load motif annotations via "
209 "'File->Load Motif List' menu option."));
211 //Save pixel map action
212 saveBrowserPixmapAction = new QAction(tr("Save to image..."), this);
213 connect(saveBrowserPixmapAction, (SIGNAL(triggered())),
214 &browser, SLOT(promptSaveBrowserPixmap()));
215 saveBrowserPixmapAction->setIcon(QIcon(":/icons/image2.png"));
217 viewMussaAlignmentAction = new QAction(tr("View mussa alignment"), this);
218 connect(viewMussaAlignmentAction, SIGNAL(triggered()),
219 this, SLOT(viewMussaAlignment() ));
220 viewMussaAlignmentAction->setWhatsThis(tr("Create a zoomed in window "
221 "showing alignment of the seqcomp "
224 whatsThisAction = QWhatsThis::createAction(this);
225 whatsThisAction->setIcon(QIcon(":/icons/help.png"));
230 void MussaWindow::setupMainMenu()
232 // we need to run setupActions first
233 assert (closeAction != 0);
235 QMenu *newMenu = menuBar()->addMenu(tr("&File"));
237 newMenu->addAction(newMussaWindowAction);
238 newMenu->addAction(createNewAnalysisAction);
239 newMenu->addAction(loadMupaAction);
240 newMenu->addAction(loadSavedAnalysisAction);
241 //newMenu->addAction(createSubAnalysisAction);
242 newMenu->addSeparator();
243 newMenu->addAction(loadMotifListAction);
244 newMenu->addAction(saveMotifListAction);
245 newMenu->addSeparator();
246 newMenu->addAction(saveBrowserPixmapAction);
247 newMenu->addSeparator();
248 newMenu->addAction(closeAction);
250 newMenu = menuBar()->addMenu(tr("&Edit"));
251 newMenu->addAction(&browser.getCopySelectedSequenceAsFastaAction());
253 newMenu = menuBar()->addMenu(tr("&View"));
254 newMenu->addAction(editMotifsAction);
255 newMenu->addAction(viewMussaAlignmentAction);
256 newMenu->addAction(showMussaViewToolbarAction);
258 newMenu = menuBar()->addMenu(tr("&Help"));
259 #if defined(QT_ASSISTANT_LIB)
260 newMenu->addAction(mussaManualAssistantAction);
262 newMenu->addAction(whatsThisAction);
263 newMenu->addSeparator();
264 newMenu->addAction(aboutAction);
266 // add some extra features to the context menu
267 QMenu& popupMenu = browser.getPopupMenu();
268 popupMenu.addAction(viewMussaAlignmentAction);
271 void MussaWindow::setupAssistant()
273 #if 0 && defined(Q_WS_MAC)
274 CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
275 CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,
276 kCFURLPOSIXPathStyle);
277 const char *pathPtr = CFStringGetCStringPtr(macPath,
278 CFStringGetSystemEncoding());
279 qDebug("Path = %s", pathPtr);
280 CFRelease(pluginRef);
283 #if defined(QT_ASSISTANT_LIB)
284 QStringList manualAssistantArgs;
285 manualAssistantArgs = QStringList();
286 manualAssistantArgs << "-profile" << "./doc/manual/mussagl_manual.adp";
287 manualAssistant = new QAssistantClient("assistant", this);
288 manualAssistant->setArguments(manualAssistantArgs);
289 connect(manualAssistant, SIGNAL(error(QString)),
290 this, SLOT(assistantError(QString)));
294 void MussaWindow::about()
296 QString msg("Welcome to Multiple Species Sequence Analysis\n"
297 "(c) 2005-2006 California Institute of Technology\n"
298 "Tristan De Buysscher, Diane Trout\n");
301 msg += QDir::currentPath();
304 msg += (char *)glGetString(GL_VERSION);
306 QMessageBox::about(this, tr("About mussa"), msg);
309 void MussaWindow::clear()
311 aligned_windows.clear();
315 void MussaWindow::createNewAnalysis()
318 if (setup_analysis_dialog.exec()) {
320 m = setup_analysis_dialog.getMussa();
323 std::cout << "New mussa exp. aborted!\n";
325 } catch(mussa_error e) {
326 QString msg(e.what());
327 QMessageBox::warning(this, tr("Create New Analysis"), msg);
331 void MussaWindow::createSubAnalysis()
336 void MussaWindow::editMotifs()
338 if (motif_editor != 0) {
339 motif_editor->hide();
342 motif_editor = new MotifEditor(analysis);
343 connect(motif_editor, SIGNAL(changedMotifs()),
344 this, SLOT(updateAnnotations()));
345 motif_editor->show();
348 void MussaWindow::loadMotifList()
350 QString caption("Load a motif list");
351 QString filter("Motif list(*.txt *.mtl)");
352 QString path = QFileDialog::getOpenFileName(this,
359 // try to load safely
361 fs::path converted_path(path.toStdString(), fs::native);
362 analysis->load_motifs(converted_path);
363 } catch (runtime_error e) {
364 QString msg("Unable to load ");
368 QMessageBox::warning(this, "Load Motifs", msg);
370 assert (analysis != 0);
373 void MussaWindow::saveMotifList()
378 void MussaWindow::loadMupa()
380 QString caption("Load a mussa parameter file");
381 QString filter("Mussa Parameters (*.mupa)");
382 QString mupa_path = QFileDialog::getOpenFileName(this,
387 if (mupa_path.isNull())
389 // try to load safely
391 Mussa *m = new Mussa;
392 fs::path converted_path(mupa_path.toStdString(), fs::native);
393 connect(m, SIGNAL(progress(const std::string&, int, int)),
394 this, SLOT(updateProgress(const std::string&, int, int)));
395 m->load_mupa_file(converted_path);
398 setWindowTitle(converted_path.native_file_string().c_str());
399 } catch (mussa_load_error e) {
400 QString msg("Unable to load ");
404 QMessageBox::warning(this, "Load Parameter", msg);
406 assert (analysis != 0);
409 void MussaWindow::loadSavedAnalysis()
411 QString caption("Load a previously run analysis");
412 QString muway_dir = QFileDialog::getExistingDirectory(this,
414 QDir::currentPath());
416 if (muway_dir.isNull())
418 // try to safely load
420 Mussa *m = new Mussa;
421 fs::path converted_path(muway_dir.toStdString(), fs::native);
422 connect(m, SIGNAL(progress(const std::string&, int, int)),
423 this, SLOT(updateProgress(const std::string&, int, int)));
424 m->load(converted_path);
425 // only switch mussas if we loaded without error
427 setWindowTitle(converted_path.native_file_string().c_str());
428 } catch (mussa_load_error e) {
429 QString msg("Unable to load ");
433 QMessageBox::warning(this, "Load Parameter", msg);
435 assert (analysis != 0);
438 void MussaWindow::newMussaWindow()
440 Mussa *a = new Mussa();
441 MussaWindow *win = new MussaWindow(a);
445 void MussaWindow::setSoftThreshold(int threshold)
447 if (analysis->get_soft_threshold() != threshold) {
448 analysis->set_soft_threshold(threshold);
455 void MussaWindow::showMussaToolbar()
457 if (mussaViewTB.isVisible())
463 void MussaWindow::toggleMotifs()
468 void MussaWindow::showManual()
470 manualAssistant->openAssistant();
473 void MussaWindow::assistantError(QString message)
475 //std::cout << "QAssistantError: " << message.toStdString() << "\n";
476 QMessageBox::warning ( this, "Warning: Mussagl Manual", message,
478 QMessageBox::NoButton);
481 void MussaWindow::NotImplementedBox()
483 QMessageBox::warning(this, QObject::tr("mussa"), QObject::tr("Not implemented yet"));
486 void MussaWindow::viewMussaAlignment()
488 const set<int>& selected_paths = browser.selectedPaths();
489 if (selected_paths.size() == 0 ) {
490 QMessageBox::warning(this,
491 QObject::tr("mussa"),
492 QObject::tr("you should probably select some paths "
495 boost::shared_ptr<MussaAlignedWindow> ma_win(
496 new MussaAlignedWindow(*analysis, selected_paths)
499 aligned_windows.push_back(ma_win);
500 connect(this, SIGNAL(changedAnnotations()),
501 aligned_windows.back().get(), SLOT(update()));
502 aligned_windows.back()->show();
506 void MussaWindow::updateAnalysis()
508 threshold.setRange(analysis->get_threshold(),analysis->get_window());
510 const vector<Sequence>& seqs = analysis->sequences();
511 browser.setSequences(seqs, analysis->colorMapper());
514 zoom.setValue(browser.zoom());
517 void MussaWindow::updateAnnotations()
519 // motifs were changed in the sequences by
520 // Mussa::update_sequences_motifs
521 emit changedAnnotations();
525 void MussaWindow::updateLinks()
527 browser.clear_links();
528 bool reversed = false;
529 const NwayPaths& nway = analysis->paths();
531 typedef list<ConservedPath> conserved_paths;
532 typedef conserved_paths::const_iterator const_conserved_paths_itor;
533 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
534 path_itor != nway.refined_pathz.end();
537 // since we were drawing to the start of a window, and opengl lines
538 // are centered around the two connecting points our lines were slightly
539 // offset. the idea of window_offset is to adjust them to the
540 // right for forward compliment or left for reverse compliment
541 // FIXME: figure out how to unit test these computations
542 //GLfloat window_offset = (path_itor->window_size)/2.0;
544 size_t track_len = path_itor->track_indexes.size();
545 vector<int> normalized_path;
546 normalized_path.reserve(track_len);
547 vector<bool> rc_flags(false, track_len);
548 for (size_t track_i=0; track_i != track_len; ++track_i)
550 int x = path_itor->track_indexes[track_i];
551 // at some point when we modify the pathz data structure to keep
552 // track of the score we can put grab the depth here.
554 // are we reverse complimented?
562 normalized_path.push_back(x);
563 rc_flags.push_back(reversed);
565 browser.link(normalized_path, rc_flags, path_itor->window_size);
571 MussaWindow::updateProgress(const string& description, int current, int max)
574 if (current == max) {
575 if (progress_dialog != 0) {
576 progress_dialog->hide();
577 delete progress_dialog;
581 // if we're starting, create the dialog
582 if (progress_dialog == 0) {
583 QString desc(description.c_str());
584 QString cancel("Cancel");
585 progress_dialog = new QProgressDialog(desc, cancel, current, max, this);
586 progress_dialog->show();
588 // just update the dialog
589 progress_dialog->setValue(current);
592 qApp->processEvents();