085a8b67e2bc4788602a2b231ad0e03bab0d4dd9
[mussa.git] / qui / MussaAlignedWindow.cpp
1 #include <list>
2 #include <vector>
3 #include <sstream>
4
5 #include <QStatusBar>
6 #include <QString>
7 #include <QMenuBar>
8
9 #include "qui/MussaAlignedWindow.hpp"
10 #include "alg/sequence.hpp"
11
12 #include <iostream>
13 using namespace std;
14
15 MussaAlignedWindow::MussaAlignedWindow(MussaRef m,
16                                        boost::shared_ptr<QDir> default_dir_,
17                                        const set<int>& sel_paths,
18                                        SubanalysisWindowRef window, 
19                                        QWidget *parent)
20   : QMainWindow(parent),
21     analysis(m),
22     default_dir(default_dir_),
23     subanalysis_window(window),
24     browser(default_dir),
25     pick_align_menu(tr("Choose Alignment")),
26     view_align_menu(tr("View Alignment")),
27     threshold_widget(0),
28     zoom(0),
29     alignTB(0)
30 {
31   setupActions();
32   connect(&browser, SIGNAL(basepairsCopied(size_t)), 
33           this, SLOT(showBasePairsCopied(size_t)));
34   browser.setSequences(analysis->sequences(), analysis->colorMapper());
35   setSelectedPaths(analysis, sel_paths);
36   setAlignment(0);
37   double zoom_level = browser.zoomToSequence();
38
39   zoom = new ZoomWidget();
40   connect(zoom, SIGNAL(valueChanged(double)), 
41           &browser, SLOT(setZoom(double)));
42   zoom->setValue(zoom_level);
43   computeMatchLines();
44   setupMenus();
45   setupAlignmentMenus();
46   
47   setCentralWidget(&browser);
48
49   // Add a threhold widget set to our current threshold and make it readonly 
50   threshold_widget = new ThresholdWidget;
51   threshold_widget->setRange(analysis->get_threshold(),analysis->get_window());
52   threshold_widget->setBasepairThreshold(analysis->get_soft_threshold());
53   threshold_widget->setReadOnly(true);
54   
55   alignTB = new QToolBar();
56   alignTB->addWidget(zoom);
57   alignTB->addWidget(threshold_widget);
58   addToolBar(alignTB);
59   
60   ostringstream message;
61   message << "Selected " << selected_paths.size() << " paths";
62   statusBar()->showMessage(message.str().c_str(), 5000);
63   browser.updatePosition();
64   
65   updateTitle();
66 }
67
68 void MussaAlignedWindow::setupActions()
69 {
70   // more cut-n-paste from MussaWindow
71   createSubAnalysisAction = new QAction(tr("Add to Subanalysis"), this);
72   connect(createSubAnalysisAction, SIGNAL(triggered()), 
73           this, SLOT(createSubAnalysis()));
74             
75   //Save pixel map action
76   saveBrowserPixmapAction = new QAction(tr("Save to image..."), this);
77   connect(saveBrowserPixmapAction, (SIGNAL(triggered())),
78           &browser, SLOT(promptSaveBrowserPixmap()));
79   saveBrowserPixmapAction->setIcon(QIcon(":/icons/image2.png"));
80 }
81
82 void MussaAlignedWindow::setupMenus()
83 {
84   QMenu *newMenu = menuBar()->addMenu(tr("&File"));
85   newMenu->addAction(saveBrowserPixmapAction);
86
87   newMenu = menuBar()->addMenu(tr("&Edit"));
88   newMenu->addAction(browser.getCopySelectedSequenceAsStringAction());
89   newMenu->addAction(browser.getCopySelectedSequenceAsFastaAction());
90   newMenu->addAction(createSubAnalysisAction);
91   
92   // add some extra features to the context menu
93   QMenu *popupMenu = browser.getPopupMenu();
94   if (popupMenu) {
95     popupMenu->addAction(createSubAnalysisAction);
96   }  
97 }
98
99 void MussaAlignedWindow::setupAlignmentMenus()
100 {
101   pick_align_menu.clear();
102   view_align_menu.clear();
103   pick_actions.clear();
104   view_actions.clear();
105
106   for(vector<ConservedPath >::iterator pathz_i=selected_paths.begin(); 
107       pathz_i != selected_paths.end(); 
108       ++pathz_i)
109   {
110     ConservedPath::path_type normalized_path = pathz_i->normalizedIndexes();
111     ostringstream menu_text;
112     menu_text << pathz_i->window_size << ":";
113     ConservedPath::iterator element_i = normalized_path.begin();
114     menu_text << *element_i++;
115     for (;
116          element_i != normalized_path.end();
117          ++element_i)
118     {
119       menu_text << ", ";
120       menu_text << *element_i;
121     }
122     int index = pathz_i - selected_paths.begin();
123     IntAction *pick = new IntAction(QString(menu_text.str().c_str()), index, this);
124     connect(pick, SIGNAL(triggered(int)), this, SLOT(setAlignment(int)));
125     pick_actions.push_back(pick);
126     pick_align_menu.addAction(pick);
127     IntAction *view = new IntAction(QString(menu_text.str().c_str()), index, this);
128     connect(view, SIGNAL(triggered(int)), this, SLOT(toggleViewAlignment(int)));
129     view->setCheckable(true);
130     view->setChecked(true);
131     view_actions.push_back(view);
132     view_align_menu.addAction(view);
133   }
134
135   menuBar()->addMenu(&pick_align_menu);
136   menuBar()->addMenu(&view_align_menu);
137 }
138
139
140 void MussaAlignedWindow::setSelectedPaths(MussaRef m, const set<int>& sel_paths)
141 {
142   // sets are sorted
143   set<int>::iterator sel_i = sel_paths.begin();
144   list<ConservedPath>::const_iterator path_i = m->paths().refined_pathz.begin();
145   list<ConservedPath>::const_iterator path_end = m->paths().refined_pathz.end();
146   size_t path_size = m->paths().refined_pathz.size();
147   size_t pathid=0;
148
149   selected_paths.reserve(sel_paths.size());
150   view_paths.reserve(sel_paths.size());
151   while (pathid != path_size and sel_i != sel_paths.end())
152   {
153     assert (*sel_i >= 0);
154     size_t sel_pathid = (size_t)(*sel_i);
155     if (pathid == sel_pathid) {
156       selected_paths.push_back(*path_i);
157       view_paths.push_back(true);
158       ++pathid;
159       ++path_i;
160       ++sel_i;
161     } else if (pathid < sel_pathid) {
162       ++pathid;
163       ++path_i;
164     } else if (pathid > sel_pathid) {
165       ++sel_i;
166     }
167   }
168 }
169
170 // FIXME: this is a cut-n-paste from MussaWindow, perhaps they should be refactored to
171 // some shared place
172 void MussaAlignedWindow::createSubAnalysis()
173 {
174   list<SequenceLocation> result;
175   SequenceLocationModel& model = subanalysis_window->getModel();
176   browser.copySelectedTracksAsSeqLocation(result);
177   for(list<SequenceLocation>::iterator result_itor = result.begin();
178       result_itor != result.end();
179       ++result_itor)
180   {
181     model.push_back(*result_itor);
182   }
183
184   if (not subanalysis_window->isVisible()) {
185     subanalysis_window->show();
186   }
187 }
188
189 void MussaAlignedWindow::setAlignment(int alignment_index)
190 {
191   if (selected_paths.size() > 0) {
192     browser.centerOnPath(selected_paths[alignment_index].normalizedIndexes());
193   }
194 }
195
196 void MussaAlignedWindow::showBasePairsCopied(size_t bp_copied)
197 {
198   QString msg("Copied ");
199   QString num;
200   num.setNum(bp_copied);
201   msg += num + " base pairs";
202   statusBar()->showMessage(msg, 5000);
203 }
204
205 void MussaAlignedWindow::toggleViewAlignment(int alignment_index)
206 {
207   view_paths[alignment_index]= not view_paths[alignment_index]; 
208   // perhaps it'd be better if we could erase specific sets
209   // of matches instead of erasing them all and recomputing them all
210   // but this is easier
211   computeMatchLines();
212 }
213
214 void MussaAlignedWindow::update()
215 {
216   browser.update();
217 }
218
219 void MussaAlignedWindow::updateTitle()
220 {
221   std::string title("Sequence View: ");
222   if (analysis) {
223     title += analysis->get_title();
224   }
225   setWindowTitle(title.c_str());
226 }
227
228 void MussaAlignedWindow::computeMatchLines()
229 {
230   browser.clear_links();
231   
232   // filter out conserved paths
233   list<ConservedPath> filtered_paths;
234   vector<ConservedPath>::iterator path_i = selected_paths.begin();
235   list<ConservedPath::path_type> result;
236   list<vector<bool> > reversed;
237
238   for(vector<ConservedPath>::size_type count = 0; 
239       count != selected_paths.size();
240       ++count, ++path_i)
241   {
242     if (view_paths[count]) 
243       filtered_paths.push_back(*path_i);
244   }
245   analysis->createLocalAlignment(filtered_paths.begin(), 
246                                  filtered_paths.end(),
247                                  result, 
248                                  reversed);
249
250   list<ConservedPath::path_type>::const_iterator result_i = result.begin();
251   list<vector<bool> >::const_iterator reversed_i = reversed.begin();
252   for(int i = 0; i != result.size(); ++i, ++result_i, ++reversed_i)
253   {
254     // make 1 base long links
255     browser.link(*result_i, *reversed_i, 1);
256   }
257 }