1 #include "py/python.hpp"
2 #include "qui/MussaWindow.hpp"
3 #include "mussa_exceptions.hpp"
6 #include <QApplication>
7 #include <QAssistantClient>
10 #include <QFileDialog>
11 #include <QHBoxLayout>
14 #include <QMessageBox>
18 #include <QStringList>
25 #include <boost/filesystem/path.hpp>
26 #include <boost/filesystem/operations.hpp>
27 namespace fs = boost::filesystem;
28 #include <boost/bind.hpp>
32 MussaWindow::MussaWindow(Mussa *analysis_, QWidget *parent) :
35 default_dir(QDir::home().absolutePath().toStdString(), fs::native),
37 setup_analysis_dialog(this),
39 mussaViewTB("Path Views"),
45 createNewAnalysisAction(0),
46 createSubAnalysisAction(0),
48 loadMotifListAction(0),
50 loadSavedAnalysisAction(0),
51 mussaManualAssistantAction(0),
52 newMussaWindowAction(0),
53 saveMotifListAction(0),
54 showMussaViewToolbarAction(0),
55 toggleMotifsAction(0),
56 saveBrowserPixmapAction(0),
58 viewMussaAlignmentAction(0),
65 //This next setWhatsThis function prevents
66 // a segfault when using WhatsThis feature with
68 //scene->setWhatsThis(tr("Mussa in OpenGL!"));
69 setCentralWidget(&browser);
70 // well updatePosition isn't quite right as we really just need
72 connect(this, SIGNAL(changedAnnotations()), &browser, SLOT(update()));
74 //mussaViewTB.addAction(toggleMotifsAction);
75 mussaViewTB.addWidget(&zoom);
77 connect(&zoom, SIGNAL(valueChanged(double)),
78 &browser, SLOT(setZoom(double)));
80 // threshold range is set in updateAnalysis
82 //scene->setClipPlane(20);
83 // FIXME: for when we get the paths drawn at the appropriate depth
84 //connect(&threshold, SIGNAL(thresholdChanged(int)),
85 // this, SLOT(setClipPlane(int)));
86 connect(&threshold, SIGNAL(thresholdChanged(int)),
87 this, SLOT(setSoftThreshold(int)));
88 mussaViewTB.addWidget(&threshold);
90 addToolBar(&mussaViewTB);
92 statusBar()->showMessage("Welcome to mussa", 2000);
93 connect(analysis, SIGNAL(progress(const std::string&, int, int)),
94 this, SLOT(updateProgress(const std::string&, int, int)));
98 void MussaWindow::setAnalysis(Mussa *new_analysis)
100 if (new_analysis != 0) {
101 // only switch mussas if we loaded without error
104 analysis = new_analysis;
105 setWindowTitle(analysis->get_name().c_str());
110 void MussaWindow::setupActions()
112 // we really don't want to run this more than once.
113 assert (closeAction == 0);
115 // the ever popular about box
116 aboutAction = new QAction(tr("&About"), this);
117 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
118 aboutAction->setIcon(QIcon(":/icons/info.png"));
121 closeAction = new QAction(tr("&Close"), this);
122 closeAction->setStatusTip(tr("Close this window"));
123 connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
124 closeAction->setIcon(QIcon(":/icons/exit.png"));
126 createNewAnalysisAction = new QAction(tr("Create Analysis"), this);
127 connect(createNewAnalysisAction, SIGNAL(triggered()),
128 this, SLOT(createNewAnalysis()));
129 createNewAnalysisAction->setIcon(QIcon(":/icons/filenew.png"));
131 createSubAnalysisAction = new QAction(tr("Add to Subanalysis"), this);
132 connect(createSubAnalysisAction, SIGNAL(triggered()),
133 this, SLOT(createSubAnalysis()));
135 saveAnalysisAction = new QAction(tr("&Save Analysis"), this);
136 connect(saveAnalysisAction, SIGNAL(triggered()),
137 this, SLOT(saveAnalysis()));
138 saveAnalysisAction->setIcon(QIcon(":/icons/filesave.png"));
140 saveAnalysisAsAction = new QAction(tr("Save Analysis &As"), this);
141 connect(saveAnalysisAsAction, SIGNAL(triggered()),
142 this, SLOT(saveAnalysisAs()));
143 saveAnalysisAsAction->setIcon(QIcon(":/icons/filesave.png"));
145 editMotifsAction = new QAction(tr("Edit Motifs"), this);;
146 connect(editMotifsAction, SIGNAL(triggered()), this, SLOT(editMotifs()));
148 loadMotifListAction = new QAction(tr("Load Motif List"), this);
149 connect(loadMotifListAction, SIGNAL(triggered()),
150 this, SLOT(loadMotifList()));
151 loadMotifListAction->setIcon(QIcon(":/icons/fileopen.png"));
153 loadMupaAction = new QAction(tr("Create Analysis from File"), this);
154 connect(loadMupaAction, SIGNAL(triggered()),
155 this, SLOT(loadMupa()));
156 loadMupaAction->setIcon(QIcon(":/icons/fileopen.png"));
158 loadSavedAnalysisAction = new QAction(tr("Load Existing &Analysis"), this);
159 connect(loadSavedAnalysisAction, SIGNAL(triggered()),
160 this, SLOT(loadSavedAnalysis()));
161 loadSavedAnalysisAction->setIcon(QIcon(":/icons/fileopen.png"));
163 mussaManualAssistantAction = new QAction(tr("Mussagl Manual..."), this);
164 mussaManualAssistantAction->setIcon(QIcon(":/icons/contents.png"));
165 connect(mussaManualAssistantAction, SIGNAL(triggered()),
166 this, SLOT(showManual()));
168 newMussaWindowAction = new QAction(tr("&New Mussa Window"), this);
169 newMussaWindowAction->setStatusTip("open another mussa window to allow comparing results");
170 connect(newMussaWindowAction, SIGNAL(triggered()),
171 this, SLOT(newMussaWindow()));
172 newMussaWindowAction->setIcon(QIcon(":/icons/window_new.png"));
174 saveMotifListAction = new QAction(tr("Save Motifs"), this);
175 connect(saveMotifListAction, SIGNAL(triggered()),
176 this, SLOT(saveMotifList()));
177 saveMotifListAction->setIcon(QIcon(":/icons/filesave.png"));
179 showMussaViewToolbarAction = new QAction(tr("Show Toolbar"), this);
180 connect(showMussaViewToolbarAction, SIGNAL(triggered()),
181 this, SLOT(showMussaToolbar()));
182 showMussaViewToolbarAction->setCheckable(true);
183 showMussaViewToolbarAction->setChecked(true);
185 toggleMotifsAction = new QAction(tr("Toggle Motifs"), this);
186 connect(toggleMotifsAction, SIGNAL(triggered()),
187 this, SLOT(toggleMotifs()));
188 toggleMotifsAction->setCheckable(true);
189 toggleMotifsAction->setIcon(QIcon(":/icons/motif_icon.png"));
190 toggleMotifsAction->setWhatsThis(tr("Toggle motif annotations on/off\n\n"
191 "You can load motif annotations via "
192 "'File->Load Motif List' menu option."));
194 //Save pixel map action
195 saveBrowserPixmapAction = new QAction(tr("Save to image..."), this);
196 connect(saveBrowserPixmapAction, (SIGNAL(triggered())),
197 &browser, SLOT(promptSaveBrowserPixmap()));
198 saveBrowserPixmapAction->setIcon(QIcon(":/icons/image2.png"));
200 viewMussaAlignmentAction = new QAction(tr("View sequence alignment"), this);
201 connect(viewMussaAlignmentAction, SIGNAL(triggered()),
202 this, SLOT(viewMussaAlignment() ));
203 viewMussaAlignmentAction->setWhatsThis(tr("Create a zoomed in window "
204 "showing alignment of the seqcomp "
207 whatsThisAction = QWhatsThis::createAction(this);
208 whatsThisAction->setIcon(QIcon(":/icons/help.png"));
213 void MussaWindow::closeEvent(QCloseEvent *event)
215 if(isClearingAnalysisSafe()) {
222 void MussaWindow::setupMainMenu()
224 // we need to run setupActions first
225 assert (closeAction != 0);
227 QMenu *newMenu = menuBar()->addMenu(tr("&File"));
229 newMenu->addAction(newMussaWindowAction);
230 newMenu->addAction(createNewAnalysisAction);
231 newMenu->addAction(loadMupaAction);
232 newMenu->addAction(loadSavedAnalysisAction);
233 newMenu->addAction(saveAnalysisAction);
234 newMenu->addAction(saveAnalysisAsAction);
235 newMenu->addSeparator();
236 newMenu->addAction(loadMotifListAction);
237 newMenu->addAction(saveMotifListAction);
238 newMenu->addSeparator();
239 newMenu->addAction(saveBrowserPixmapAction);
240 newMenu->addSeparator();
241 newMenu->addAction(closeAction);
243 newMenu = menuBar()->addMenu(tr("&Edit"));
244 newMenu->addAction(editMotifsAction);
245 newMenu->addAction(&browser.getCopySelectedSequenceAsFastaAction());
246 newMenu->addAction(createSubAnalysisAction);
248 newMenu = menuBar()->addMenu(tr("&View"));
249 newMenu->addAction(viewMussaAlignmentAction);
250 newMenu->addAction(showMussaViewToolbarAction);
252 newMenu = menuBar()->addMenu(tr("&Help"));
253 newMenu->addAction(mussaManualAssistantAction);
254 newMenu->addAction(whatsThisAction);
255 newMenu->addSeparator();
256 newMenu->addAction(aboutAction);
258 // add some extra features to the context menu
259 QMenu& popupMenu = browser.getPopupMenu();
260 popupMenu.addAction(viewMussaAlignmentAction);
261 popupMenu.addAction(createSubAnalysisAction);
264 void MussaWindow::setupAssistant()
266 #if defined(QT_ASSISTANT_FOUND)
267 QStringList manualAssistantArgs;
268 manualAssistantArgs = QStringList();
269 manualAssistantArgs << "-profile" << "./doc/manual/mussagl_manual.adp";
270 manualAssistant = new QAssistantClient("assistant", this);
271 manualAssistant->setArguments(manualAssistantArgs);
272 connect(manualAssistant, SIGNAL(error(QString)),
273 this, SLOT(assistantError(QString)));
277 void MussaWindow::about()
279 QString msg("Welcome to Multiple Species Sequence Analysis\n"
280 "(c) 2005-2006 California Institute of Technology\n"
281 "Tristan De Buysscher, Diane Trout\n");
284 msg += (char *)glGetString(GL_VERSION);
286 QMessageBox::about(this, tr("About mussa"), msg);
289 void MussaWindow::clear()
291 aligned_windows.clear();
295 void MussaWindow::createNewAnalysis()
298 // ideally we should open a new window if there's an analysis
299 // but this should work for the moment.
300 if (not isClearingAnalysisSafe()) return;
302 if (setup_analysis_dialog.exec()) {
304 m = setup_analysis_dialog.getMussa();
307 } catch(mussa_error e) {
308 QString msg(e.what());
309 QMessageBox::warning(this, tr("Create New Analysis"), msg);
313 void MussaWindow::createSubAnalysis()
315 list<SequenceLocation> result;
316 SequenceLocationModel& model = subanalysis_window.getModel();
317 browser.copySelectedTracksAsSeqLocation(result);
318 for(list<SequenceLocation>::iterator result_itor = result.begin();
319 result_itor != result.end();
322 model.push_back(*result_itor);
325 if (not subanalysis_window.isVisible()) {
326 subanalysis_window.show();
330 void MussaWindow::saveAnalysis()
332 // if we've got an analysis
333 if (analysis and not analysis->empty()) {
334 // if it doesn't have a name we need to pick one
335 if (analysis->get_analysis_path().empty()) {
338 // if we've got a name, has it changed any?
339 // this doesn't work when a sequence changes something (like its
341 //else if (analysis->is_dirty())
347 void MussaWindow::saveAnalysisAs()
349 std::auto_ptr<QFileDialog> dialog(new QFileDialog(this));
350 dialog->setAcceptMode(QFileDialog::AcceptSave);
351 dialog->setFileMode(QFileDialog::AnyFile);
352 dialog->setDirectory(QDir(default_dir.native_directory_string().c_str()));
354 QStringList fileNames;
355 if (not dialog->exec()) {
358 fileNames = dialog->selectedFiles();
360 if (fileNames.size() != 1) {
363 fs::path save_path(fileNames[0].toStdString(), fs::native);
364 // do you want to overwrite?
365 if (fs::exists(save_path) and
366 QMessageBox::question(
368 tr("Overwrite File? -- Mussa"),
369 tr("A file called %1 already exists"
370 "do you want to overwrite it?")
372 tr("&Yes"), tr("&No"),
377 analysis->save(save_path);
378 default_dir = (save_path / "..").normalize();
381 bool MussaWindow::isClearingAnalysisSafe()
383 if (analysis and not analysis->empty() and analysis->is_dirty()) {
384 switch (QMessageBox::question(
386 tr("Save Unsaved Changes -- Mussa"),
387 tr("There are unsaved changes,\ndo you want to save?"),
388 tr("&Yes"), tr("&No"), tr("&Cancel"),
403 throw runtime_error("isClearingAnalysis QMesageBox failure");
406 // if we're here we've been saved and can replace
410 void MussaWindow::editMotifs()
412 if (motif_editor != 0) {
413 motif_editor->hide();
416 motif_editor = new MotifEditor(analysis);
417 connect(motif_editor, SIGNAL(changedMotifs()),
418 this, SLOT(updateAnnotations()));
419 motif_editor->show();
422 void MussaWindow::loadMotifList()
424 QString caption("Load a motif list");
425 QString filter("Motif list(*.txt *.mtl)");
426 QDir default_qdir(default_dir.native_directory_string().c_str());
427 QString path = QFileDialog::getOpenFileName(this,
429 default_qdir.absolutePath(),
434 // try to load safely
436 fs::path converted_path(path.toStdString(), fs::native);
437 analysis->load_motifs(converted_path);
438 default_dir = converted_path.branch_path();
439 } catch (runtime_error e) {
440 QString msg("Unable to load ");
444 QMessageBox::warning(this, "Load Motifs", msg);
446 assert (analysis != 0);
449 void MussaWindow::saveMotifList()
454 void MussaWindow::loadMupa()
456 QString caption("Load a mussa parameter file");
457 QString filter("Mussa Parameters (*.mupa)");
458 QDir default_qdir(QDir(default_dir.native_directory_string().c_str()));
460 QString mupa_path = QFileDialog::getOpenFileName(
463 default_qdir.absolutePath(),
467 if (mupa_path.isNull())
469 // try to load safely
471 // ideally we should open a new window if there's an analysis
472 // but this should work for the moment.
473 if (not isClearingAnalysisSafe()) return;
475 Mussa *m = new Mussa;
476 fs::path converted_path(mupa_path.toStdString(), fs::native);
477 connect(m, SIGNAL(progress(const std::string&, int, int)),
478 this, SLOT(updateProgress(const std::string&, int, int)));
479 m->load_mupa_file(converted_path);
482 setWindowTitle(converted_path.native_file_string().c_str());
483 // grab the path ignoring the mupa file portion
484 default_dir = converted_path.branch_path();
485 } catch (mussa_load_error e) {
486 QString msg("Unable to load ");
490 QMessageBox::warning(this, "Load Parameter", msg);
492 assert (analysis != 0);
495 void MussaWindow::loadSavedAnalysis()
497 QDir default_qdir(QDir(default_dir.native_directory_string().c_str()));
498 QString caption("Load a previously run analysis");
499 QString muway_dir = QFileDialog::getExistingDirectory(
502 default_qdir.absolutePath()
505 if (muway_dir.isNull())
507 // try to safely load
509 // ideally we should open a new window if there's an analysis
510 // but this should work for the moment.
511 if (not isClearingAnalysisSafe()) return;
513 Mussa *m = new Mussa;
514 fs::path converted_path(muway_dir.toStdString(), fs::native);
515 connect(m, SIGNAL(progress(const std::string&, int, int)),
516 this, SLOT(updateProgress(const std::string&, int, int)));
517 m->load(converted_path);
518 // only switch mussas if we loaded without error
519 if (analysis->empty()) {
520 // our current window is empty so load and replace.
522 setWindowTitle(converted_path.native_file_string().c_str());
523 default_dir = converted_path.branch_path();
525 MussaWindow *win = new MussaWindow(m);
526 win->setWindowTitle(converted_path.native_file_string().c_str());
527 win->default_dir = converted_path.branch_path();
530 } catch (mussa_load_error e) {
531 QString msg("Unable to load ");
535 QMessageBox::warning(this, "Load Parameter", msg);
537 assert (analysis != 0);
540 void MussaWindow::newMussaWindow()
542 Mussa *a = new Mussa();
543 MussaWindow *win = new MussaWindow(a);
544 win->default_dir = default_dir;
548 void MussaWindow::setSoftThreshold(int threshold)
550 if (analysis->get_soft_threshold() != threshold) {
551 analysis->set_soft_threshold(threshold);
558 void MussaWindow::showMussaToolbar()
560 if (mussaViewTB.isVisible())
566 void MussaWindow::toggleMotifs()
571 void MussaWindow::showManual()
573 #if QT_QTASSISTANT_FOUND
574 manualAssistant->openAssistant();
577 boost::python::object webopen = get_py()["webbrowser.open"];
578 webopen("http://woldlab.caltech.edu/~king/mussagl_manual/");
579 } catch( boost::python::error_already_set ) {
582 #endif //QT_QTASSISTANT_FOUND
585 void MussaWindow::assistantError(QString message)
587 //std::cout << "QAssistantError: " << message.toStdString() << "\n";
588 QMessageBox::warning ( this, "Warning: Mussagl Manual", message,
590 QMessageBox::NoButton);
593 void MussaWindow::NotImplementedBox()
595 QMessageBox::warning(this, QObject::tr("mussa"), QObject::tr("Not implemented yet"));
598 void MussaWindow::viewMussaAlignment()
600 const set<int>& selected_paths = browser.selectedPaths();
601 if (selected_paths.size() == 0 ) {
602 QMessageBox::warning(this,
603 QObject::tr("mussa"),
604 QObject::tr("you should probably select some paths "
607 boost::shared_ptr<MussaAlignedWindow> ma_win(
608 new MussaAlignedWindow(*analysis, selected_paths)
611 aligned_windows.push_back(ma_win);
612 connect(this, SIGNAL(changedAnnotations()),
613 aligned_windows.back().get(), SLOT(update()));
614 aligned_windows.back()->show();
618 void MussaWindow::updateAnalysis()
620 threshold.setRange(analysis->get_threshold(),analysis->get_window());
622 const Mussa::vector_sequence_type& seqs = analysis->sequences();
623 browser.setSequences(seqs, analysis->colorMapper());
626 zoom.setValue(browser.zoom());
629 void MussaWindow::updateAnnotations()
631 // motifs were changed in the sequences by
632 // Mussa::update_sequences_motifs
633 emit changedAnnotations();
637 void MussaWindow::updateLinks()
639 browser.clear_links();
640 bool reversed = false;
641 const NwayPaths& nway = analysis->paths();
643 typedef list<ConservedPath> conserved_paths;
644 typedef conserved_paths::const_iterator const_conserved_paths_itor;
645 for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
646 path_itor != nway.refined_pathz.end();
649 // since we were drawing to the start of a window, and opengl lines
650 // are centered around the two connecting points our lines were slightly
651 // offset. the idea of window_offset is to adjust them to the
652 // right for forward compliment or left for reverse compliment
653 // FIXME: figure out how to unit test these computations
654 //GLfloat window_offset = (path_itor->window_size)/2.0;
656 size_t track_len = path_itor->track_indexes.size();
657 vector<int> normalized_path;
658 normalized_path.reserve(track_len);
659 vector<bool> rc_flags(false, track_len);
660 for (size_t track_i=0; track_i != track_len; ++track_i)
662 int x = path_itor->track_indexes[track_i];
663 // at some point when we modify the pathz data structure to keep
664 // track of the score we can put grab the depth here.
666 // are we reverse complimented?
674 normalized_path.push_back(x);
675 rc_flags.push_back(reversed);
677 browser.link(normalized_path, rc_flags, path_itor->window_size);
683 MussaWindow::updateProgress(const string& description, int current, int max)
686 if (current == max) {
687 if (progress_dialog != 0) {
688 progress_dialog->hide();
689 delete progress_dialog;
693 // if we're starting, create the dialog
694 if (progress_dialog == 0) {
695 QString desc(description.c_str());
696 QString cancel("Cancel");
697 progress_dialog = new QProgressDialog(desc, cancel, current, max, this);
698 progress_dialog->show();
700 // just update the dialog
701 progress_dialog->setValue(current);
704 qApp->processEvents();