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