1 #include "py/python.hpp"
2 #include "qui/MussaWindow.hpp"
3 #include "mussa_exceptions.hpp"
7 #include <QApplication>
8 #include <QAssistantClient>
11 #include <QFileDialog>
12 #include <QHBoxLayout>
15 #include <QMessageBox>
19 #include <QStringList>
26 #include <boost/filesystem/path.hpp>
27 #include <boost/filesystem/operations.hpp>
28 #include <boost/filesystem/convenience.hpp>
29 namespace fs = boost::filesystem;
30 #include <boost/bind.hpp>
34 MussaWindow::MussaWindow(MussaRef analysis_, QWidget *parent) :
37 default_dir(new QDir(QDir::home().absolutePath())),
39 setup_analysis_dialog(new MussaSetupDialog(this)),
40 subanalysis_window(new SubanalysisWindow(analysis)),
41 browser(new SequenceBrowserWidget(default_dir, this)),
42 mussaViewTB(new QToolBar("Path Views")),
44 threshold(new ThresholdWidget),
48 createNewAnalysisAction(0),
49 createSubAnalysisAction(0),
51 loadMotifListAction(0),
53 loadSavedAnalysisAction(0),
54 mussaManualAssistantAction(0),
55 newMussaWindowAction(0),
56 saveMotifListAction(0),
57 showMussaViewToolbarAction(0),
58 toggleMotifsAction(0),
59 saveBrowserPixmapAction(0),
61 viewMussaAlignmentAction(0),
68 //This next setWhatsThis function prevents
69 // a segfault when using WhatsThis feature with
71 //scene->setWhatsThis(tr("Mussa in OpenGL!"));
72 setCentralWidget(browser);
73 // well updatePosition isn't quite right as we really just need
75 connect(this, SIGNAL(changedAnnotations()), browser, SLOT(update()));
76 connect(this, SIGNAL(changedMotifs()), this, SLOT(updateAnnotations()));
78 //mussaViewTB->addAction(toggleMotifsAction);
79 mussaViewTB->addWidget(zoom);
81 connect(zoom, SIGNAL(valueChanged(double)),
82 browser, SLOT(setZoom(double)));
84 // threshold range is set in updateAnalysis
86 //scene->setClipPlane(20);
87 // FIXME: for when we get the paths drawn at the appropriate depth
88 //connect(threshold, SIGNAL(thresholdChanged(int)),
89 // this, SLOT(setClipPlane(int)));
90 connect(threshold, SIGNAL(thresholdChanged(int)),
91 this, SLOT(setSoftThreshold(int)));
92 mussaViewTB->addWidget(threshold);
94 addToolBar(mussaViewTB);
96 statusBar()->showMessage("Welcome to mussa", 2000);
97 // FIXME: we should start refactoring the connect call to updateAnalysis or something
99 connect(analysis.get(), SIGNAL(progress(const std::string&, int, int)),
100 this, SLOT(updateProgress(const std::string&, int, int)));
106 void MussaWindow::setAnalysis(MussaRef new_analysis)
108 if (new_analysis != 0) {
109 // only switch mussas if we loaded without error
111 analysis.swap(new_analysis);
117 void MussaWindow::setupActions()
119 // we really don't want to run this more than once.
120 assert (closeAction == 0);
122 // the ever popular about box
123 aboutAction = new QAction(tr("&About"), this);
124 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
125 aboutAction->setIcon(QIcon(":/icons/info.png"));
128 closeAction = new QAction(tr("&Close"), this);
129 closeAction->setStatusTip(tr("Close this window"));
130 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
131 closeAction->setIcon(QIcon(":/icons/exit.png"));
133 createNewAnalysisAction = new QAction(tr("Create Analysis"), this);
134 connect(createNewAnalysisAction, SIGNAL(triggered()),
135 this, SLOT(createNewAnalysis()));
136 createNewAnalysisAction->setIcon(QIcon(":/icons/filenew.png"));
138 createSubAnalysisAction = new QAction(tr("Add to Subanalysis"), this);
139 connect(createSubAnalysisAction, SIGNAL(triggered()),
140 this, SLOT(createSubAnalysis()));
142 saveAnalysisAction = new QAction(tr("&Save Analysis"), this);
143 connect(saveAnalysisAction, SIGNAL(triggered()),
144 this, SLOT(saveAnalysis()));
145 saveAnalysisAction->setIcon(QIcon(":/icons/filesave.png"));
147 saveAnalysisAsAction = new QAction(tr("Save Analysis &As"), this);
148 connect(saveAnalysisAsAction, SIGNAL(triggered()),
149 this, SLOT(saveAnalysisAs()));
150 saveAnalysisAsAction->setIcon(QIcon(":/icons/filesave.png"));
152 editMotifsAction = new QAction(tr("Edit Motifs"), this);;
153 connect(editMotifsAction, SIGNAL(triggered()), this, SLOT(editMotifs()));
155 loadMotifListAction = new QAction(tr("Load Motif List"), this);
156 connect(loadMotifListAction, SIGNAL(triggered()),
157 this, SLOT(loadMotifList()));
158 loadMotifListAction->setIcon(QIcon(":/icons/fileopen.png"));
160 loadMupaAction = new QAction(tr("Create Analysis from File"), this);
161 connect(loadMupaAction, SIGNAL(triggered()),
162 this, SLOT(loadMupa()));
163 loadMupaAction->setIcon(QIcon(":/icons/fileopen.png"));
165 loadSavedAnalysisAction = new QAction(tr("Load Existing &Analysis"), this);
166 connect(loadSavedAnalysisAction, SIGNAL(triggered()),
167 this, SLOT(loadSavedAnalysis()));
168 loadSavedAnalysisAction->setIcon(QIcon(":/icons/fileopen.png"));
170 mussaManualAssistantAction = new QAction(tr("Mussagl Manual..."), this);
171 mussaManualAssistantAction->setIcon(QIcon(":/icons/contents.png"));
172 connect(mussaManualAssistantAction, SIGNAL(triggered()),
173 this, SLOT(showManual()));
175 newMussaWindowAction = new QAction(tr("&New Mussa Window"), this);
176 newMussaWindowAction->setStatusTip("open another mussa window to allow comparing results");
177 connect(newMussaWindowAction, SIGNAL(triggered()),
178 this, SLOT(newMussaWindow()));
179 newMussaWindowAction->setIcon(QIcon(":/icons/window_new.png"));
181 saveMotifListAction = new QAction(tr("Save Motifs"), this);
182 connect(saveMotifListAction, SIGNAL(triggered()),
183 this, SLOT(saveMotifList()));
184 saveMotifListAction->setIcon(QIcon(":/icons/filesave.png"));
186 showMussaViewToolbarAction = new QAction(tr("Show Toolbar"), this);
187 connect(showMussaViewToolbarAction, SIGNAL(triggered()),
188 this, SLOT(showMussaToolbar()));
189 showMussaViewToolbarAction->setCheckable(true);
190 showMussaViewToolbarAction->setChecked(true);
192 toggleMotifsAction = new QAction(tr("Toggle Motifs"), this);
193 connect(toggleMotifsAction, SIGNAL(triggered()),
194 this, SLOT(toggleMotifs()));
195 toggleMotifsAction->setCheckable(true);
196 toggleMotifsAction->setIcon(QIcon(":/icons/motif_icon.png"));
197 toggleMotifsAction->setWhatsThis(tr("Toggle motif annotations on/off\n\n"
198 "You can load motif annotations via "
199 "'File->Load Motif List' menu option."));
201 //Save pixel map action
202 saveBrowserPixmapAction = new QAction(tr("Save to image..."), this);
203 connect(saveBrowserPixmapAction, (SIGNAL(triggered())),
204 browser, SLOT(promptSaveBrowserPixmap()));
205 saveBrowserPixmapAction->setIcon(QIcon(":/icons/image2.png"));
207 viewMussaAlignmentAction = new QAction(tr("View sequence alignment"), this);
208 connect(viewMussaAlignmentAction, SIGNAL(triggered()),
209 this, SLOT(viewMussaAlignment() ));
210 viewMussaAlignmentAction->setWhatsThis(tr("Create a zoomed in window "
211 "showing alignment of the seqcomp "
214 whatsThisAction = QWhatsThis::createAction(this);
215 whatsThisAction->setIcon(QIcon(":/icons/help.png"));
220 void MussaWindow::closeEvent(QCloseEvent *event)
222 if(isClearingAnalysisSafe()) {
229 void MussaWindow::setupMainMenu()
231 // we need to run setupActions first
232 assert (closeAction != 0);
234 QMenu *newMenu = menuBar()->addMenu(tr("&File"));
236 newMenu->addAction(newMussaWindowAction);
237 newMenu->addAction(createNewAnalysisAction);
238 newMenu->addAction(loadMupaAction);
239 newMenu->addAction(loadSavedAnalysisAction);
240 newMenu->addAction(saveAnalysisAction);
241 newMenu->addAction(saveAnalysisAsAction);
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(editMotifsAction);
252 newMenu->addAction(browser->getCopySelectedSequenceAsFastaAction());
253 newMenu->addAction(createSubAnalysisAction);
255 newMenu = menuBar()->addMenu(tr("&View"));
256 newMenu->addAction(viewMussaAlignmentAction);
257 newMenu->addAction(showMussaViewToolbarAction);
259 newMenu = menuBar()->addMenu(tr("&Help"));
260 newMenu->addAction(mussaManualAssistantAction);
261 newMenu->addAction(whatsThisAction);
262 newMenu->addSeparator();
263 newMenu->addAction(aboutAction);
265 // add some extra features to the context menu
266 QMenu *popupMenu = browser->getPopupMenu();
268 popupMenu->addAction(viewMussaAlignmentAction);
269 popupMenu->addAction(createSubAnalysisAction);
273 void MussaWindow::setupAssistant()
275 #if defined(QT_QTASSISTANT_FOUND)
276 QStringList manualAssistantArgs;
277 manualAssistantArgs = QStringList();
278 manualAssistantArgs << "-profile" << "./doc/manual/mussagl_manual.adp";
279 manualAssistant = new QAssistantClient("assistant", this);
280 manualAssistant->setArguments(manualAssistantArgs);
281 connect(manualAssistant, SIGNAL(error(QString)),
282 this, SLOT(assistantError(QString)));
286 void MussaWindow::about()
289 msg += "Welcome to Multiple Species Sequence Analysis\n";
290 msg += "(c) 2005-2006 California Institute of Technology\n";
291 msg += "Diane Trout, Tristan De Buysscher, Brandon King\n";
293 msg += mussa_version;
296 msg += (char *)glGetString(GL_VERSION);
298 QMessageBox::about(this, tr("About mussa"), msg);
301 void MussaWindow::clear()
303 aligned_windows.clear();
307 void MussaWindow::createNewAnalysis()
310 // ideally we should open a new window if there's an analysis
311 // but this should work for the moment.
312 if (not isClearingAnalysisSafe()) return;
314 if (setup_analysis_dialog->exec()) {
315 setAnalysis(setup_analysis_dialog->getMussa());
317 } catch(mussa_error e) {
318 QString msg(e.what());
319 QMessageBox::warning(this, tr("Create New Analysis"), msg);
323 void MussaWindow::createSubAnalysis()
325 list<SequenceLocation> result;
326 SequenceLocationModel& model = subanalysis_window->getModel();
327 browser->copySelectedTracksAsSeqLocation(result);
328 for(list<SequenceLocation>::iterator result_itor = result.begin();
329 result_itor != result.end();
332 model.push_back(*result_itor);
335 if (not subanalysis_window->isVisible()) {
336 subanalysis_window->show();
340 void MussaWindow::saveAnalysis()
342 // if we've got an analysis
343 if (analysis and not analysis->empty()) {
344 // if it doesn't have a name we need to pick one
345 if (analysis->get_analysis_path().empty()) {
348 // if we've got a name, has it changed any?
349 // this doesn't work when a sequence changes something (like its
351 //else if (analysis->is_dirty())
354 } catch (std::exception e) {
355 QMessageBox::critical(this,
356 tr("Mussa Save Error"),
358 QMessageBox::Ok, 0, 0);
365 void MussaWindow::saveAnalysisAs()
367 std::auto_ptr<QFileDialog> dialog(new QFileDialog(this));
368 dialog->setAcceptMode(QFileDialog::AcceptSave);
369 dialog->setFileMode(QFileDialog::AnyFile);
370 dialog->setDirectory(*default_dir);
372 QStringList fileNames;
373 if (not dialog->exec()) {
376 fileNames = dialog->selectedFiles();
378 if (fileNames.size() != 1) {
382 fs::path save_path(fileNames[0].toStdString(), fs::native);
383 // do you want to overwrite?
384 if (fs::exists(save_path) and
385 QMessageBox::question(
387 tr("Overwrite File? -- Mussa"),
388 tr("A file called %1 already exists"
389 "do you want to overwrite it?")
391 tr("&Yes"), tr("&No"),
396 analysis->save(save_path);
397 fs::path normalized_path = (save_path / "..").normalize();
398 default_dir->setPath(normalized_path.native_directory_string().c_str());
399 } catch (std::exception e) {
400 QMessageBox::critical(this,
401 tr("Mussa Save Error"),
403 QMessageBox::Ok, 0, 0);
407 bool MussaWindow::isClearingAnalysisSafe()
409 if (analysis and not analysis->empty() and analysis->is_dirty()) {
410 switch (QMessageBox::question(
412 tr("Save Unsaved Changes -- Mussa"),
413 tr("There are unsaved changes,\ndo you want to save?"),
414 tr("&Yes"), tr("&No"), tr("&Cancel"),
429 throw runtime_error("isClearingAnalysis QMesageBox failure");
432 // if we're here we've been saved and can replace
436 void MussaWindow::editMotifs()
438 if (motif_editor != 0) {
439 motif_editor->hide();
442 motif_editor = new MotifEditor(analysis);
443 connect(motif_editor, SIGNAL(changedMotifs()),
444 this, SLOT(updateAnnotations()));
445 motif_editor->show();
448 void MussaWindow::loadMotifList()
450 QString caption("Mussa Load Motifs");
451 QString filter("Motif list(*.txt *.mtl)");
452 QString path = QFileDialog::getOpenFileName(this,
454 default_dir->absolutePath(),
459 // try to load safely
461 fs::path converted_path(path.toStdString(), fs::native);
462 analysis->load_motifs(converted_path);
463 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
464 emit changedMotifs();
465 } catch (std::exception e) {
466 QString msg("Unable to load ");
470 QMessageBox::warning(this, caption, msg);
474 void MussaWindow::saveMotifList()
476 QString caption("Mussa Save Motifs");
477 QString filter("Motif list(*.txt *.mtl)");
478 QString path = QFileDialog::getSaveFileName(this,
480 default_dir->absolutePath(),
485 // try to load safely
487 fs::path converted_path(path.toStdString(), fs::native);
488 if (fs::extension(converted_path).size() == 0) {
489 // no extension, so add one
490 fs::path base_path = converted_path.branch_path();
491 fs::path filename(converted_path.leaf() + ".mtl", fs::native);
492 converted_path = base_path / filename;
494 analysis->save_motifs(converted_path);
495 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
496 } catch (std::exception e) {
497 QString msg("Unable to save ");
501 QMessageBox::warning(this, caption, msg);
504 void MussaWindow::loadMupa()
506 QString caption("Load a mussa parameter file");
507 QString filter("Mussa Parameters (*.mupa)");
508 QString mupa_path = QFileDialog::getOpenFileName(
511 default_dir->absolutePath(),
515 if (mupa_path.isNull())
517 // try to load safely
519 // ideally we should open a new window if there's an analysis
520 // but this should work for the moment.
521 if (not isClearingAnalysisSafe()) return;
523 MussaRef m(new Mussa);
524 fs::path converted_path(mupa_path.toStdString(), fs::native);
525 connect(m.get(), SIGNAL(progress(const std::string&, int, int)),
526 this, SLOT(updateProgress(const std::string&, int, int)));
527 m->load_mupa_file(converted_path);
531 // grab the path ignoring the mupa file portion
532 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
533 } catch (mussa_load_error e) {
534 QString msg("Unable to load ");
538 QMessageBox::warning(this, "Load Parameter", msg);
540 assert (analysis != 0);
543 void MussaWindow::loadSavedAnalysis()
545 QString caption("Load a previously run analysis");
546 QString muway_dir = QFileDialog::getExistingDirectory(
549 default_dir->absolutePath()
552 if (muway_dir.isNull())
554 // try to safely load
556 // ideally we should open a new window if there's an analysis
557 // but this should work for the moment.
558 if (not isClearingAnalysisSafe()) return;
560 MussaRef m(new Mussa);
561 fs::path converted_path(muway_dir.toStdString(), fs::native);
562 connect(m.get(), SIGNAL(progress(const std::string&, int, int)),
563 this, SLOT(updateProgress(const std::string&, int, int)));
564 m->load(converted_path);
565 // only switch mussas if we loaded without error
566 if (analysis->empty()) {
567 // our current window is empty so load and replace.
570 default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
572 MussaWindow *win = new MussaWindow(m);
574 win->default_dir->setPath(converted_path.branch_path().native_directory_string().c_str());
577 } catch (mussa_load_error e) {
578 QString msg("Unable to load ");
582 QMessageBox::warning(this, "Load Parameter", msg);
584 assert (analysis != 0);
587 void MussaWindow::newMussaWindow()
589 MussaRef a(new Mussa);
590 MussaWindow *win = new MussaWindow(a);
591 win->default_dir = default_dir;
595 void MussaWindow::setSoftThreshold(int value)
597 if (analysis->get_soft_threshold() != value) {
598 threshold->setEnabled( false );
599 analysis->set_soft_threshold(value);
603 threshold->setEnabled( true );
607 void MussaWindow::showMussaToolbar()
609 if (mussaViewTB->isVisible())
615 void MussaWindow::toggleMotifs()
620 void MussaWindow::showManual()
622 #if defined(QT_QTASSISTANT_FOUND)
623 if (manualAssistant) {
624 manualAssistant->openAssistant();
626 QMessageBox::warning(this,
627 tr("Mussa Help Error"),
628 tr("QtAssistant not setup correctly"),
630 QMessageBox::NoButton,
631 QMessageBox::NoButton);
635 boost::python::object webopen = get_py()["webbrowser.open"];
636 webopen("http://woldlab.caltech.edu/~king/mussagl_manual/");
637 } catch( boost::python::error_already_set ) {
639 QMessageBox::warning(this,
640 tr("Mussa Help Error"),
641 tr("Unable to launch webbrowser"),
643 QMessageBox::NoButton,
644 QMessageBox::NoButton);
646 #endif //QT_QTASSISTANT_FOUND
649 void MussaWindow::assistantError(QString message)
651 //std::cout << "QAssistantError: " << message.toStdString() << "\n";
652 QMessageBox::warning ( this, "Warning: Mussagl Manual", message,
654 QMessageBox::NoButton);
657 void MussaWindow::NotImplementedBox()
659 QMessageBox::warning(this, QObject::tr("mussa"), QObject::tr("Not implemented yet"));
662 void MussaWindow::viewMussaAlignment()
664 const set<int>& selected_paths = browser->selectedPaths();
665 if (selected_paths.size() == 0 ) {
666 QMessageBox::warning(this,
667 QObject::tr("mussa"),
668 QObject::tr("you should probably select some paths "
671 MussaAlignedWindowRef ma_win(
672 new MussaAlignedWindow(analysis, default_dir, selected_paths, subanalysis_window)
675 aligned_windows.push_back(ma_win);
676 connect(this, SIGNAL(changedAnnotations()),
677 aligned_windows.back().get(), SLOT(update()));
678 aligned_windows.back()->show();
682 void MussaWindow::updateAnalysis()
684 const Mussa::vector_sequence_type& seqs = analysis->sequences();
685 browser->setSequences(seqs, analysis->colorMapper());
686 assert(browser->sequences().size() == analysis->size());
688 // setRange eventually emits something that causes updateLinks to be called
689 // but it's possible for us to not have had a chance to set out sequences
691 threshold->setRange(analysis->get_threshold(),analysis->get_window());
694 zoom->setValue(browser->zoom());
697 void MussaWindow::updateAnnotations()
699 // motifs were changed in the sequences by
700 // Mussa::update_sequences_motifs
701 emit changedAnnotations();
705 void MussaWindow::updateLinks()
707 if(browser->sequences().size() == 0) {
708 // we don't have any sequences load so we have no business setting links
712 browser->clear_links();
713 bool reversed = false;
714 const NwayPaths& nway = analysis->paths();
716 typedef list<ConservedPath> conserved_paths;
717 typedef conserved_paths::const_iterator const_conserved_paths_itor;
718 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
719 path_itor != nway.refined_pathz.end();
722 // since we were drawing to the start of a window, and opengl lines
723 // are centered around the two connecting points our lines were slightly
724 // offset. the idea of window_offset is to adjust them to the
725 // right for forward compliment or left for reverse compliment
726 // FIXME: figure out how to unit test these computations
727 //GLfloat window_offset = (path_itor->window_size)/2.0;
729 size_t track_len = path_itor->track_indexes.size();
730 vector<int> normalized_path;
731 normalized_path.reserve(track_len);
732 vector<bool> rc_flags(false, track_len);
733 for (size_t track_i=0; track_i != track_len; ++track_i)
735 int x = path_itor->track_indexes[track_i];
736 // at some point when we modify the pathz data structure to keep
737 // track of the score we can put grab the depth here.
739 // are we reverse complimented?
747 normalized_path.push_back(x);
748 rc_flags.push_back(reversed);
750 browser->link(normalized_path, rc_flags, path_itor->window_size);
756 MussaWindow::updateProgress(const string& description, int current, int max)
759 if (current == max) {
760 if (progress_dialog != 0) {
761 progress_dialog->hide();
762 delete progress_dialog;
766 // if we're starting, create the dialog
767 if (progress_dialog == 0) {
768 QString desc(description.c_str());
769 QString cancel("Cancel");
770 progress_dialog = new QProgressDialog(desc, cancel, current, max, this);
771 progress_dialog->show();
773 // just update the dialog
774 progress_dialog->setValue(current);
777 qApp->processEvents();
780 void MussaWindow::updateTitle()
783 setWindowTitle(analysis->get_title().c_str());