1 #include "qui/MussaWindow.hpp"
2 #include "mussa_exceptions.hpp"
6 #include <QApplication>
7 #include <QAssistantClient>
9 #include <QDesktopServices>
11 #include <QFileDialog>
12 #include <QHBoxLayout>
15 #include <QMessageBox>
19 #include <QStringList>
27 #include <boost/filesystem/path.hpp>
28 #include <boost/filesystem/operations.hpp>
29 #include <boost/filesystem/convenience.hpp>
30 namespace fs = boost::filesystem;
31 #include <boost/bind.hpp>
35 static void init_resources() {
36 static bool resources_loaded = false;
37 if (not resources_loaded) {
38 Q_INIT_RESOURCE(icons);
39 resources_loaded = true;
43 MussaWindow::MussaWindow(MussaRef analysis_, QWidget *parent) :
47 setup_analysis_dialog(0),
55 createNewAnalysisAction(0),
56 createSubAnalysisAction(0),
58 loadMotifListAction(0),
60 loadSavedAnalysisAction(0),
61 mussaManualAssistantAction(0),
62 newMussaWindowAction(0),
63 saveMotifListAction(0),
64 showMussaViewToolbarAction(0),
65 toggleMotifsAction(0),
66 saveBrowserPixmapAction(0),
68 viewMussaAlignmentAction(0),
77 setWindowIcon(QIcon(":/icons/mussa.png"));
78 default_dir.reset(new QDir(QDir::home().absolutePath()));
80 setCentralWidget(browser);
81 // well updatePosition isn't quite right as we really just need
83 connect(this, SIGNAL(changedAnnotations()), browser, SLOT(update()));
84 connect(this, SIGNAL(changedMotifs()), this, SLOT(updateAnnotations()));
85 connect(browser, SIGNAL(basepairsCopied(size_t)),
86 this, SLOT(showBasePairsCopied(size_t)));
87 connect(zoom, SIGNAL(valueChanged(double)),
88 browser, SLOT(setZoom(double)));
89 mussaViewTB->addWidget(zoom);
91 // threshold range is set in updateAnalysis
92 connect(threshold, SIGNAL(thresholdChanged(int)),
93 this, SLOT(setSoftThreshold(int)));
94 mussaViewTB->addWidget(threshold);
96 addToolBar(mussaViewTB);
98 statusBar()->showMessage("Welcome to mussa", 2000);
100 // FIXME: we should start refactoring the connect call to updateAnalysis or something
102 connect(analysis.get(), SIGNAL(progress(const QString&, int, int)),
103 this, SLOT(updateProgress(const QString&, int, int)));
104 connect(analysis.get(), SIGNAL(isModified(bool)),
105 this, SLOT(updateAnalysisModified(bool)));
111 void MussaWindow::setAnalysis(MussaRef new_analysis)
113 if (new_analysis != 0) {
114 // only switch mussas if we loaded without error
116 analysis.swap(new_analysis);
122 void MussaWindow::setupActions()
124 // we really don't want to run this more than once.
125 assert (closeAction == 0);
127 // the ever popular about box
128 aboutAction = new QAction(tr("&About"), this);
129 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
130 aboutAction->setIcon(QIcon(":/icons/info.png"));
133 closeAction = new QAction(tr("&Close"), this);
134 closeAction->setStatusTip(tr("Close this window"));
135 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
136 closeAction->setIcon(QIcon(":/icons/exit.png"));
138 createNewAnalysisAction = new QAction(tr("Create &New Analysis"), this);
139 connect(createNewAnalysisAction, SIGNAL(triggered()),
140 this, SLOT(createNewAnalysis()));
141 createNewAnalysisAction->setIcon(QIcon(":/icons/filenew.png"));
142 createNewAnalysisAction->setShortcut(Qt::CTRL | Qt::Key_N);
144 createSubAnalysisAction = new QAction(tr("Add to Subanalysis"), this);
145 connect(createSubAnalysisAction, SIGNAL(triggered()),
146 this, SLOT(createSubAnalysis()));
148 saveAnalysisAction = new QAction(tr("&Save Analysis"), this);
149 connect(saveAnalysisAction, SIGNAL(triggered()),
150 this, SLOT(saveAnalysis()));
151 saveAnalysisAction->setIcon(QIcon(":/icons/filesave.png"));
152 saveAnalysisAction->setShortcut(Qt::CTRL | Qt::Key_S);
154 saveAnalysisAsAction = new QAction(tr("Save Analysis &As"), this);
155 connect(saveAnalysisAsAction, SIGNAL(triggered()),
156 this, SLOT(saveAnalysisAs()));
157 saveAnalysisAsAction->setIcon(QIcon(":/icons/filesave.png"));
159 editMotifsAction = new QAction(tr("Edit Motifs"), this);;
160 connect(editMotifsAction, SIGNAL(triggered()), this, SLOT(editMotifs()));
162 loadMotifListAction = new QAction(tr("Open Motif List"), this);
163 connect(loadMotifListAction, SIGNAL(triggered()),
164 this, SLOT(loadMotifList()));
165 loadMotifListAction->setIcon(QIcon(":/icons/fileopen.png"));
167 loadMupaAction = new QAction(tr("Create Analysis from File"), this);
168 connect(loadMupaAction, SIGNAL(triggered()),
169 this, SLOT(loadMupa()));
170 loadMupaAction->setIcon(QIcon(":/icons/fileopen.png"));
172 loadSavedAnalysisAction = new QAction(tr("&Open Existing &Analysis"), this);
173 connect(loadSavedAnalysisAction, SIGNAL(triggered()),
174 this, SLOT(loadSavedAnalysis()));
175 loadSavedAnalysisAction->setIcon(QIcon(":/icons/fileopen.png"));
176 loadSavedAnalysisAction->setShortcut(Qt::CTRL | Qt::Key_O);
178 mussaManualAssistantAction = new QAction(tr("Mussagl Manual..."), this);
179 mussaManualAssistantAction->setIcon(QIcon(":/icons/contents.png"));
180 connect(mussaManualAssistantAction, SIGNAL(triggered()),
181 this, SLOT(showManual()));
183 newMussaWindowAction = new QAction(tr("&New Mussa Window"), this);
184 newMussaWindowAction->setStatusTip("open another mussa window to allow comparing results");
185 connect(newMussaWindowAction, SIGNAL(triggered()),
186 this, SLOT(newMussaWindow()));
187 newMussaWindowAction->setIcon(QIcon(":/icons/window_new.png"));
189 saveMotifListAction = new QAction(tr("Save Motifs"), this);
190 connect(saveMotifListAction, SIGNAL(triggered()),
191 this, SLOT(saveMotifList()));
192 saveMotifListAction->setIcon(QIcon(":/icons/filesave.png"));
194 showMussaViewToolbarAction = new QAction(tr("Show Toolbar"), this);
195 connect(showMussaViewToolbarAction, SIGNAL(triggered()),
196 this, SLOT(showMussaToolbar()));
197 showMussaViewToolbarAction->setCheckable(true);
198 showMussaViewToolbarAction->setChecked(true);
200 toggleMotifsAction = new QAction(tr("Toggle Motifs"), this);
201 connect(toggleMotifsAction, SIGNAL(triggered()),
202 this, SLOT(toggleMotifs()));
203 toggleMotifsAction->setCheckable(true);
204 toggleMotifsAction->setIcon(QIcon(":/icons/motif_icon.png"));
205 toggleMotifsAction->setWhatsThis(tr("Toggle motif annotations on/off\n\n"
206 "You can load motif annotations via "
207 "'File->Load Motif List' menu option."));
209 //Save pixel map action
210 saveBrowserPixmapAction = new QAction(tr("Save to image..."), this);
212 connect(saveBrowserPixmapAction, (SIGNAL(triggered())),
213 browser, SLOT(promptSaveBrowserPixmap()));
214 saveBrowserPixmapAction->setIcon(QIcon(":/icons/image2.png"));
217 viewMussaAlignmentAction = new QAction(tr("View sequence 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::closeEvent(QCloseEvent *event)
232 if(isClearingAnalysisSafe()) {
239 void MussaWindow::setupMainMenu()
241 // we need to run setupActions first
242 assert (closeAction != 0);
244 QMenu *newMenu = menuBar()->addMenu(tr("&File"));
246 newMenu->addAction(newMussaWindowAction);
247 newMenu->addAction(createNewAnalysisAction);
248 newMenu->addAction(loadMupaAction);
249 newMenu->addAction(loadSavedAnalysisAction);
250 newMenu->addAction(saveAnalysisAction);
251 newMenu->addAction(saveAnalysisAsAction);
252 newMenu->addSeparator();
253 newMenu->addAction(loadMotifListAction);
254 newMenu->addAction(saveMotifListAction);
255 newMenu->addSeparator();
256 newMenu->addAction(saveBrowserPixmapAction);
257 newMenu->addSeparator();
258 newMenu->addAction(closeAction);
260 newMenu = menuBar()->addMenu(tr("&Edit"));
261 newMenu->addAction(editMotifsAction);
262 if (browser) newMenu->addAction(browser->getCopySelectedSequenceAsStringAction());
263 if (browser) newMenu->addAction(browser->getCopySelectedSequenceAsFastaAction());
264 newMenu->addAction(createSubAnalysisAction);
265 if (browser) newMenu->addAction(browser->getEditSequencePropertiesAction());
267 newMenu = menuBar()->addMenu(tr("&View"));
268 newMenu->addAction(viewMussaAlignmentAction);
269 newMenu->addAction(showMussaViewToolbarAction);
271 newMenu = menuBar()->addMenu(tr("&Help"));
272 newMenu->addAction(mussaManualAssistantAction);
273 newMenu->addAction(whatsThisAction);
274 newMenu->addSeparator();
275 newMenu->addAction(aboutAction);
277 // add some extra features to the context menu
279 QMenu *popupMenu = browser->getPopupMenu();
281 popupMenu->addAction(viewMussaAlignmentAction);
282 popupMenu->addAction(createSubAnalysisAction);
287 void MussaWindow::setupWidgets()
289 setup_analysis_dialog = new MussaSetupDialog;
290 subanalysis_window.reset(new SubanalysisWindow(analysis));
291 browser = new SequenceBrowserWidget(default_dir);
292 mussaViewTB = new QToolBar("Path Views", this);
293 zoom = new ZoomWidget(mussaViewTB);
294 threshold = new ThresholdWidget(mussaViewTB);
297 void MussaWindow::setupAssistant()
299 #if defined(QT_QTASSISTANT_FOUND)
300 QStringList manualAssistantArgs;
301 manualAssistantArgs = QStringList();
302 manualAssistantArgs << "-profile" << "./doc/manual/mussagl_manual.adp";
303 manualAssistant = new QAssistantClient("assistant", this);
304 manualAssistant->setArguments(manualAssistantArgs);
305 connect(manualAssistant, SIGNAL(error(QString)),
306 this, SLOT(assistantError(QString)));
310 void MussaWindow::about()
313 msg += "Welcome to Multiple Species Sequence Analysis\n";
314 msg += "(c) 2005-2006 California Institute of Technology\n";
315 msg += "Diane Trout, Tristan De Buysscher, Brandon King\n";
317 msg += mussa_version;
320 msg += (char *)glGetString(GL_VERSION);
322 QMessageBox::about(this, tr("About mussa"), msg);
325 void MussaWindow::clear()
327 aligned_windows.clear();
331 void MussaWindow::createNewAnalysis()
334 // ideally we should open a new window if there's an analysis
335 // but this should work for the moment.
336 if (not isClearingAnalysisSafe()) return;
338 if (setup_analysis_dialog->exec()) {
339 setAnalysis(setup_analysis_dialog->getMussa());
341 } catch(mussa_error e) {
342 QString msg(e.what());
343 QMessageBox::warning(this, tr("Create New Analysis"), msg);
347 void MussaWindow::createSubAnalysis()
349 list<SequenceLocation> result;
350 SequenceLocationModel& model = subanalysis_window->getModel();
351 browser->copySelectedTracksAsSeqLocation(result);
352 for(list<SequenceLocation>::iterator result_itor = result.begin();
353 result_itor != result.end();
356 model.push_back(*result_itor);
359 if (not subanalysis_window->isVisible()) {
360 subanalysis_window->show();
364 void MussaWindow::saveAnalysis()
366 // if we've got an analysis
367 if (analysis and not analysis->empty()) {
368 // if it doesn't have a name we need to pick one
369 if (analysis->get_analysis_path().empty()) {
372 // if we've got a name, has it changed any?
373 // this doesn't work when a sequence changes something (like its
375 //else if (analysis->is_dirty())
378 } catch (std::exception e) {
379 QMessageBox::critical(this,
380 tr("Mussa Save Error"),
382 QMessageBox::Ok, 0, 0);
389 void MussaWindow::saveAnalysisAs()
391 std::auto_ptr<QFileDialog> dialog(new QFileDialog(this));
392 dialog->setAcceptMode(QFileDialog::AcceptSave);
393 dialog->setFileMode(QFileDialog::AnyFile);
394 dialog->setDirectory(*default_dir);
396 QStringList fileNames;
397 if (not dialog->exec()) {
400 fileNames = dialog->selectedFiles();
402 if (fileNames.size() != 1) {
406 fs::path save_path(fileNames[0].toStdString(), fs::native);
407 // do you want to overwrite?
408 if (fs::exists(save_path) and
409 QMessageBox::question(
411 tr("Overwrite File? -- Mussa"),
412 tr("A file called %1 already exists"
413 "do you want to overwrite it?")
415 tr("&Yes"), tr("&No"),
420 analysis->save(save_path);
421 fs::path normalized_path = (save_path / "..").normalize();
422 default_dir->setPath(normalized_path.native_directory_string().c_str());
423 } catch (std::exception e) {
424 QMessageBox::critical(this,
425 tr("Mussa Save Error"),
427 QMessageBox::Ok, 0, 0);
431 bool MussaWindow::isClearingAnalysisSafe()
433 if (analysis and not analysis->empty() and analysis->is_dirty()) {
434 switch (QMessageBox::question(
436 tr("Save Unsaved Changes -- Mussa"),
437 tr("There are unsaved changes,\ndo you want to save?"),
438 tr("&Yes"), tr("&No"), tr("&Cancel"),
453 throw runtime_error("isClearingAnalysis QMesageBox failure");
456 // if we're here we've been saved and can replace
460 void MussaWindow::editMotifs()
462 if (motif_editor != 0) {
463 motif_editor->hide();
466 motif_editor = new MotifEditor(analysis);
467 connect(motif_editor, SIGNAL(changedMotifs()),
468 this, SLOT(updateAnnotations()));
469 motif_editor->show();
472 void MussaWindow::loadMotifList()
474 QString caption("Mussa Load Motifs");
475 QString filter("Motif list(*.txt *.mtl)");
476 QString path = QFileDialog::getOpenFileName(this,
478 default_dir->absolutePath(),
483 // try to load safely
485 fs::path converted_path(path.toStdString(), fs::native);
486 analysis->load_motifs(converted_path);
487 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
488 emit changedMotifs();
489 } catch (std::exception e) {
490 QString msg("Unable to load ");
494 QMessageBox::warning(this, caption, msg);
498 void MussaWindow::saveMotifList()
500 QString caption("Mussa Save Motifs");
501 QString filter("Motif list(*.txt *.mtl)");
502 QString path = QFileDialog::getSaveFileName(this,
504 default_dir->absolutePath(),
509 // try to load safely
511 fs::path converted_path(path.toStdString(), fs::native);
512 if (fs::extension(converted_path).size() == 0) {
513 // no extension, so add one
514 fs::path base_path = converted_path.branch_path();
515 fs::path filename(converted_path.leaf() + ".mtl", fs::native);
516 converted_path = base_path / filename;
518 analysis->save_motifs(converted_path);
519 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
520 } catch (std::exception e) {
521 QString msg("Unable to save ");
525 QMessageBox::warning(this, caption, msg);
528 void MussaWindow::loadMupa()
530 QString caption("Load a mussa parameter file");
531 QString filter("Mussa Parameters (*.mupa)");
532 QString mupa_path = QFileDialog::getOpenFileName(
535 default_dir->absolutePath(),
539 if (mupa_path.isNull())
541 // try to load safely
543 // ideally we should open a new window if there's an analysis
544 // but this should work for the moment.
545 if (not isClearingAnalysisSafe()) return;
547 MussaRef m = Mussa::init();
548 fs::path converted_path(mupa_path.toStdString(), fs::native);
549 connect(m.get(), SIGNAL(progress(const QString&, int, int)),
550 this, SLOT(updateProgress(const QString&, int, int)));
551 m->load_mupa_file(converted_path);
555 // grab the path ignoring the mupa file portion
556 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
557 } catch (mussa_load_error e) {
558 QString msg("Unable to load ");
562 QMessageBox::warning(this, "Load Parameter", msg);
564 assert (analysis != 0);
567 void MussaWindow::loadSavedAnalysis()
569 QString caption("Load a previously run analysis");
570 QString muway_dir = QFileDialog::getExistingDirectory(
573 default_dir->absolutePath()
576 if (muway_dir.isNull())
578 // try to safely load
580 // ideally we should open a new window if there's an analysis
581 // but this should work for the moment.
582 if (not isClearingAnalysisSafe()) return;
584 MussaRef m = Mussa::init();
585 fs::path converted_path(muway_dir.toStdString(), fs::native);
586 connect(m.get(), SIGNAL(progress(const QString&, int, int)),
587 this, SLOT(updateProgress(const QString&, int, int)));
588 m->load(converted_path);
589 // only switch mussas if we loaded without error
590 if (analysis->empty()) {
591 // our current window is empty so load and replace.
594 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
596 MussaWindow *win = new MussaWindow(m);
598 win->default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
601 } catch (boost::filesystem::filesystem_error e) {
602 QString msg("Unable to load ");
606 QMessageBox::warning(this, "Load Parameter", msg);
607 } catch (mussa_load_error e) {
608 QString msg("Unable to load ");
612 QMessageBox::warning(this, "Load Parameter", msg);
614 assert (analysis != 0);
617 void MussaWindow::newMussaWindow()
619 MussaWindow *win = new MussaWindow(Mussa::init());
620 win->default_dir = default_dir;
624 void MussaWindow::setSoftThreshold(int value)
626 if (analysis->get_soft_threshold() != value) {
627 threshold->setEnabled( false );
628 analysis->set_soft_threshold(value);
632 threshold->setEnabled( true );
636 void MussaWindow::showMussaToolbar()
638 if (mussaViewTB->isVisible())
644 void MussaWindow::showBasePairsCopied(size_t bp_copied)
646 QString msg("Copied ");
648 num.setNum(bp_copied);
649 msg += num + " base pairs";
650 statusBar()->showMessage(msg, 5000);
654 void MussaWindow::toggleMotifs()
659 void MussaWindow::showManual()
661 #if defined(QT_QTASSISTANT_FOUND)
662 if (manualAssistant) {
663 manualAssistant->openAssistant();
665 QMessageBox::warning(this,
666 tr("Mussa Help Error"),
667 tr("QtAssistant not setup correctly"),
669 QMessageBox::NoButton,
670 QMessageBox::NoButton);
673 QUrl manual_url("http://woldlab.caltech.edu/~king/mussagl_manual/");
674 if (not QDesktopServices::openUrl(manual_url)) {
675 QMessageBox::warning(this,
676 tr("Mussa Help Error"),
677 tr("Unable to launch webbrowser"),
679 QMessageBox::NoButton,
680 QMessageBox::NoButton);
682 #endif //QT_QTASSISTANT_FOUND
685 void MussaWindow::assistantError(QString message)
687 //std::cout << "QAssistantError: " << message.toStdString() << "\n";
688 QMessageBox::warning ( this, "Warning: Mussagl Manual", message,
690 QMessageBox::NoButton);
693 void MussaWindow::NotImplementedBox()
695 QMessageBox::warning(this, QObject::tr("mussa"), QObject::tr("Not implemented yet"));
698 void MussaWindow::viewMussaAlignment()
700 const set<int>& selected_paths = browser->selectedPaths();
701 if (selected_paths.size() == 0 ) {
702 QMessageBox::warning(this,
703 QObject::tr("mussa"),
704 QObject::tr("you should probably select some paths "
707 MussaAlignedWindowRef ma_win(
708 new MussaAlignedWindow(analysis, default_dir, selected_paths, subanalysis_window)
711 aligned_windows.push_back(ma_win);
712 connect(this, SIGNAL(changedAnnotations()),
713 aligned_windows.back().get(), SLOT(update()));
714 aligned_windows.back()->show();
718 void MussaWindow::updateAnalysis()
720 const Mussa::vector_sequence_type& seqs = analysis->sequences();
721 browser->setSequences(seqs, analysis->colorMapper());
722 assert(browser->sequences().size() == analysis->size());
724 // setRange eventually emits something that causes updateLinks to be called
725 // but it's possible for us to not have had a chance to set out sequences
727 threshold->setRange(analysis->get_threshold(),analysis->get_window());
730 zoom->setValue(browser->zoom());
733 void MussaWindow::updateAnnotations()
735 // motifs were changed in the sequences by
736 // Mussa::update_sequences_motifs
737 emit changedAnnotations();
741 void MussaWindow::updateLinks()
743 if(browser->sequences().size() == 0) {
744 // we don't have any sequences load so we have no business setting links
748 browser->clear_links();
749 bool reversed = false;
750 const NwayPaths& nway = analysis->paths();
752 typedef list<ConservedPath> conserved_paths;
753 typedef conserved_paths::const_iterator const_conserved_paths_itor;
754 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
755 path_itor != nway.refined_pathz.end();
758 // since we were drawing to the start of a window, and opengl lines
759 // are centered around the two connecting points our lines were slightly
760 // offset. the idea of window_offset is to adjust them to the
761 // right for forward compliment or left for reverse compliment
762 // FIXME: figure out how to unit test these computations
763 //GLfloat window_offset = (path_itor->window_size)/2.0;
765 size_t track_len = path_itor->track_indexes.size();
766 vector<int> normalized_path;
767 normalized_path.reserve(track_len);
768 vector<bool> rc_flags(false, track_len);
769 for (size_t track_i=0; track_i != track_len; ++track_i)
771 int x = path_itor->track_indexes[track_i];
772 // at some point when we modify the pathz data structure to keep
773 // track of the score we can put grab the depth here.
775 // are we reverse complimented?
783 normalized_path.push_back(x);
784 rc_flags.push_back(reversed);
786 browser->link(normalized_path, rc_flags, path_itor->window_size);
792 MussaWindow::updateProgress(const QString& description, int current, int max)
795 if (current == max) {
796 if (progress_dialog != 0) {
797 progress_dialog->hide();
798 delete progress_dialog;
802 // if we're starting, create the dialog
803 if (progress_dialog == 0) {
804 QString cancel("Cancel");
805 progress_dialog = new QProgressDialog(description, cancel, current, max, this);
806 progress_dialog->show();
808 // just update the dialog
809 progress_dialog->setValue(current);
812 qApp->processEvents();
815 void MussaWindow::updateAnalysisModified(bool is_modified)
817 setWindowModified(is_modified);
820 void MussaWindow::updateTitle()
823 QString title(analysis->get_title().c_str());
825 setWindowTitle(title);