ticket:62 fix local alignment
[mussa.git] / qui / MussaAlignedWindow.cpp
index a65a9555167ef4da175ddb7f99eb55e7715226b2..d113f48f65daab996d9ba959fa30b8de6697cb7a 100644 (file)
@@ -58,8 +58,8 @@ void MussaAlignedWindow::setSelectedPaths(Mussa &m, const set<int>& sel_paths)
 {
   // sets are sorted
   set<int>::iterator sel_i = sel_paths.begin();
-  list<ExtendedConservedPath>::const_iterator path_i = m.paths().refined_pathz.begin();
-  list<ExtendedConservedPath>::const_iterator path_end = m.paths().refined_pathz.end();
+  list<ConservedPath>::const_iterator path_i = m.paths().refined_pathz.begin();
+  list<ConservedPath>::const_iterator path_end = m.paths().refined_pathz.end();
   size_t path_size = m.paths().refined_pathz.size();
   size_t pathid=0;
 
@@ -91,7 +91,7 @@ void MussaAlignedWindow::setupMenus()
   pick_actions.clear();
   view_actions.clear();
 
-  for(vector<ExtendedConservedPath >::iterator pathz_i=selected_paths.begin(); 
+  for(vector<ConservedPath >::iterator pathz_i=selected_paths.begin(); 
       pathz_i != selected_paths.end(); 
       ++pathz_i)
   {
@@ -144,110 +144,31 @@ void MussaAlignedWindow::update()
 
 void MussaAlignedWindow::computeMatchLines()
 {
-  const vector<Sequence>& raw_seq = analysis.sequences();
-  vector<int> aligned_path;
-  size_t i2, i3;
-  int x_start, x_end;
-  int window_length, win_i;
-  int rc_1 = 0; 
-  int rc_2 = 0;
-  vector<bool> rc_list;
-  bool full_match;
-  vector<bool> matched;
-  int align_counter;
-
   browser.clear_links();
-  align_counter = 0;
-  for(vector<ExtendedConservedPath >::iterator pathz_i=selected_paths.begin(); 
-      pathz_i != selected_paths.end(); 
-      ++pathz_i)
+  
+  // filter out conserved paths
+  list<ConservedPath> filtered_paths;
+  vector<ConservedPath>::iterator path_i = selected_paths.begin();
+  list<ConservedPath::path_type> result;
+  list<vector<bool> > reversed;
+
+  for(vector<ConservedPath>::size_type count = 0; 
+      count != selected_paths.size();
+      ++count, ++path_i)
   {
-    if (view_paths[align_counter])
-    {
-      ExtendedConservedPath& a_path = *pathz_i;
-      window_length = a_path.window_size;
-      // determine which parts of the path are RC relative to first species
-      rc_list = a_path.reverseComplimented();
-      
-      // loop over each bp in the conserved region for all sequences
-      for(win_i = 0; win_i < window_length; win_i++)
-      {
-        aligned_path.clear();
-        // determine which exact base pairs match between the sequences
-        full_match = true;
-        for(i2 = 0; i2 < a_path.size()-1; i2++)
-        {
-          // assume not rc as most likely, adjust below
-          rc_1 = 0;
-          rc_2 = 0;
-          // no matter the case, any RC node needs adjustments
-          if (a_path[i2] < 0)
-            rc_1 = window_length-1;
-          if (a_path[i2+1] < 0)
-            rc_2 = window_length-1;        
-           
-          x_start = (abs(a_path[i2]-rc_1+win_i));
-          x_end =   (abs(a_path[i2+1]-rc_2+win_i));
-          
-          // RC case handling
-          // ugh, and xor...only want rc coloring if just one of the nodes is rc
-          // if both nodes are rc, then they are 'normal' relative to each other
-          if((rc_list[i2] || rc_list[i2+1] )&&!(rc_list[i2] && rc_list[i2+1]))
-          { //the hideous rc matching logic - not complex, but annoying
-            if(!(( (raw_seq[i2][x_start]=='A')&&(raw_seq[i2+1][x_end]=='T')) ||
-                  ((raw_seq[i2][x_start]=='T')&&(raw_seq[i2+1][x_end]=='A')) ||
-                  ((raw_seq[i2][x_start]=='G')&&(raw_seq[i2+1][x_end]=='C')) ||
-                  ((raw_seq[i2][x_start]=='C')&&(raw_seq[i2+1][x_end]=='G'))) )
-              full_match = false;
-          }
-          else
-          {
-            if (!( (raw_seq[i2][x_start] == raw_seq[i2+1][x_end]) &&
-                  (raw_seq[i2][x_start] != 'N') &&
-                  (raw_seq[i2+1][x_end] != 'N') ) )
-              full_match = false;
-          }
-        }
-        
-        // draw for matches stretching across all sequences
-        if (full_match)
-        {
-          // now can draw the line for each bp in this window that matches
-          // grrr, need to ask if anyone cares if I switch the seq 
-          // top-bot order...
-          i3 = 0;
-          //y_loc = y_min + 5;
-          for(i2 = 0; i2 < a_path.size()-1; i2++)
-          {
-            // assume not rc as most likely, adjust below
-            rc_1 = 0;
-            rc_2 = 0;
-            // no matter the case, any RC node needs adjustments
-            if (a_path[i2] < 0)
-            {
-              rc_1 = window_length;        
-            }
-            if (a_path[i2] < 0)
-            {
-              rc_2 = window_length;        
-            }
-            
-            // maybe shouldn't recalc these, but store values from first loop
-            x_start = (abs((int) (a_path[i2]-rc_1+win_i)));
-            x_end =   (abs((int) (a_path[i2+1]-rc_2+win_i)));
-            aligned_path.push_back(x_start);
-            // if we're on the last time through the loop, save x_end too
-            if (i2 == a_path.size()-2) {
-              aligned_path.push_back(x_end);
-            }
-          }
-        }
-        if (aligned_path.size() > 0) {
-          browser.link(aligned_path, rc_list,1);
-        }
-      }
-    }
-    align_counter++;
+    if (view_paths[count]) 
+      filtered_paths.push_back(*path_i);
+  }
+  analysis.createLocalAlignment(filtered_paths.begin(), 
+                                filtered_paths.end(),
+                                result, 
+                                reversed);
+
+  list<ConservedPath::path_type>::const_iterator result_i = result.begin();
+  list<vector<bool> >::const_iterator reversed_i = reversed.begin();
+  for(int i = 0; i != result.size(); ++i, ++result_i, ++reversed_i)
+  {
+    // make 1 base long links
+    browser.link(*result_i, *reversed_i, 1);
   }
 }
-