There can be only one (filename convention)
[mussa.git] / alg / nway_paths.cpp
1 //  This file is part of the Mussa source distribution.
2 //  http://mussa.caltech.edu/
3 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
4
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
9
10
11 //                        ----------------------------------------
12 //                         ---------- mussa_nway.cc  -----------
13 //                        ----------------------------------------
14
15 #include "alg/nway_paths.hpp"
16 #include "alg/conserved_path.hpp"
17 #include "mussa_exceptions.hpp"
18
19 #include <fstream>
20 #include <iostream>
21 #include <stdexcept>
22
23 using namespace std;
24
25 NwayPaths::NwayPaths()
26 {
27 }
28
29 void
30 NwayPaths::setup(int w, int t)
31 {
32   threshold = t;
33   soft_thres = threshold;
34   win_size = w;
35   pathz.clear();
36
37   //cout << "nway: thres = " << threshold
38   //     << ", soft threo = " << soft_thres << endl;
39 }
40
41 void
42 NwayPaths::set_soft_thres(int sft_thres)
43 {
44   soft_thres = sft_thres;
45 }
46
47
48 // dumbly goes thru and combines path windows exactly adjacent (ie + 1 index)
49 // doesn't deal with interleaved adjacency
50 void
51 NwayPaths::simple_refine()
52 {
53   // ext_path remembers the first window set in an extending path
54   ExtendedConservedPath ext_path, new_path;
55   list<ConservedPath>::iterator cur_path, next_path;
56   list<ConservedPath>::iterator pathz_i;
57   int win_ext_len = 0;
58   bool extending, end = false;
59
60   refined_pathz.clear();
61
62   //cout << "path number is: " << pathz.size() << endl;
63   pathz_i = pathz.begin();
64
65   // only try to extend when pathz isn't empty.
66   if (pathz_i != pathz.end())
67   {
68     ext_path = ExtendedConservedPath( win_size, *pathz_i);
69
70     while(pathz_i != pathz.end())
71     {
72       // keep track of current path and advance to next path
73       cur_path = pathz_i;
74       ++pathz_i;
75       if (pathz_i == pathz.end())
76         end = true;
77       else
78         next_path = pathz_i;
79
80       if (not end)
81       {
82         // if node for each seq is equal to the next node+1 then for all
83         // sequences then we are extending
84         extending = cur_path->nextTo(*next_path);
85       }
86       else
87         extending = false;
88   
89       if (extending)
90       {
91         win_ext_len++;
92       }
93       else
94       {
95         // add the extend window length as first element and add as refined
96         // now that we have the path to extend save it
97         new_path = ext_path;
98         new_path.extend(win_ext_len);
99         refined_pathz.push_back(new_path);
100         // reset stuff
101         win_ext_len = 0;
102         ext_path = ExtendedConservedPath( win_size, *next_path);
103       }
104     }
105   }
106   //cout << "r_path number is: " << refined_pathz.size() << endl;
107 }
108
109
110 void
111 NwayPaths::add_path(int threshold, vector<int>& loaded_path)
112 {
113   pathz.push_back(ConservedPath(threshold, loaded_path));
114 }
115
116 void
117 NwayPaths::add_path(ConservedPath loaded_path)
118 {
119   pathz.push_back(loaded_path);
120 }
121
122
123 void
124 NwayPaths::save(string save_file_path)
125 {
126   fstream save_file;
127   list<ExtendedConservedPath >::iterator path_i, paths_end;
128
129   save_file.open(save_file_path.c_str(), ios::out);
130
131   save_file << "<Mussa type=flp seq_count=" << sequence_count();
132   save_file << " win=" << win_size;
133   // add a function para new_thres defaults to -1 to later deal with
134   // reanalysis with higher thres - if statement whether to record base thres
135   // or new thres (ie if -1, then base)
136   save_file << " thres=" << threshold << " >\n";
137
138   path_i = refined_pathz.begin();
139   paths_end = refined_pathz.end();
140   //path_i = pathz.begin();
141   //paths_end = pathz.end();
142   while (path_i != paths_end)
143   {
144     ExtendedConservedPath& a_path = *path_i;
145     //cout << a_path.size() << endl;
146     //first entry is the window length of the windows in the path
147     save_file << a_path.window_size << ":";
148     for(size_t i = 0; i != sequence_count(); ++i)
149     {
150       save_file << a_path[i];
151       if (i != sequence_count())
152         save_file << ",";
153     }
154     save_file << endl;
155     ++path_i;
156   }
157
158   save_file << "</Mussa>\n";
159   save_file.close();
160 }
161
162
163 size_t
164 NwayPaths::sequence_count()
165 {
166   if (refined_pathz.begin() == refined_pathz.end() )
167     return 0;
168   else
169     return refined_pathz.begin()->size();
170 }
171
172
173 void
174 NwayPaths::load(string load_file_path)
175 {
176   fstream load_file;
177   string file_data_line, header_data, data, path_node, path_width;
178   int space_split_i, equal_split_i, comma_split_i, colon_split_i;
179   vector<int> loaded_path;
180
181   load_file.open(load_file_path.c_str(), ios::in);
182
183   if (!load_file)
184   {
185     throw mussa_load_error("Sequence File: " + load_file_path + " not found");
186   }
187   else
188   {
189     // get header data
190     // grab mussa tag - discard for now...maybe check in future...
191     getline(load_file,file_data_line);
192     space_split_i = file_data_line.find(" ");
193     file_data_line = file_data_line.substr(space_split_i+1);
194     // grab type tag - need in future to distinguish between flp and vlp paths
195     space_split_i = file_data_line.find(" ");
196     file_data_line = file_data_line.substr(space_split_i+1);
197     // get species/seq number
198     space_split_i = file_data_line.find(" ");
199     header_data = file_data_line.substr(0,space_split_i); 
200     equal_split_i = header_data.find("=");
201     data = file_data_line.substr(equal_split_i+1); 
202     unsigned int species_num = atoi (data.c_str());
203     file_data_line = file_data_line.substr(space_split_i+1);
204     // get window size
205     space_split_i = file_data_line.find(" ");
206     header_data = file_data_line.substr(0,space_split_i); 
207     equal_split_i = header_data.find("=");
208     data = file_data_line.substr(equal_split_i+1); 
209     win_size = atoi (data.c_str());
210     file_data_line = file_data_line.substr(space_split_i+1);
211     // get window size
212     space_split_i = file_data_line.find(" ");
213     header_data = file_data_line.substr(0,space_split_i); 
214     equal_split_i = header_data.find("=");
215     data = file_data_line.substr(equal_split_i+1); 
216     threshold = atoi (data.c_str());
217     file_data_line = file_data_line.substr(space_split_i+1);
218     
219     //cout << "seq_num=" << species_num << " win=" << win_size;
220     //cout << " thres=" << threshold << endl;
221     
222     // clear out the current data
223     refined_pathz.clear();
224     
225     int temp;
226     
227     getline(load_file,file_data_line);
228     while ( (!load_file.eof()) && (file_data_line != "</Mussa>") )
229     {
230       if (file_data_line != "")
231       {
232         loaded_path.clear();
233         colon_split_i = file_data_line.find(":");
234         // whats our window size?
235         path_width = file_data_line.substr(0,colon_split_i); 
236         file_data_line = file_data_line.substr(colon_split_i+1);
237         for(size_t i = 0; i < species_num; i++)
238         {
239           comma_split_i = file_data_line.find(",");
240           path_node = file_data_line.substr(0, comma_split_i); 
241           temp = atoi (path_node.c_str());
242           loaded_path.push_back(temp);
243           file_data_line = file_data_line.substr(comma_split_i+1);
244         }
245         assert (loaded_path.size() == species_num );
246         refined_pathz.push_back(ExtendedConservedPath(atoi(path_width.c_str()), 
247                                                       threshold, 
248                                                       loaded_path));
249       }
250       getline(load_file,file_data_line);
251     }
252     load_file.close();
253   }
254 }
255
256
257 void
258 NwayPaths::path_search(vector<vector<FLPs> > all_comparisons, ConservedPath path, int depth)
259 {
260   list<int> new_nodes, trans_check_nodes;
261   list<int>::iterator new_nodes_i, new_nodes_end;
262   int i;
263   bool trans_check_good;
264
265   new_nodes = all_comparisons[depth - 1][depth].match_locations(path[depth-1]);
266   new_nodes_i = new_nodes.begin();
267   new_nodes_end = new_nodes.end();
268   while(new_nodes_i != new_nodes_end)
269   {
270     //cout << "    * species " << depth << " node: " << *new_nodes_i << endl;
271     // check transitivity with previous nodes in path
272     trans_check_good = true;
273     for(i = 0; i < depth - 1; i++)
274     {
275       trans_check_nodes = all_comparisons[i][depth].match_locations(path[i]);
276       if ( (trans_check_nodes.end() == find(trans_check_nodes.begin(),
277                                             trans_check_nodes.end(),
278                                             *new_nodes_i) ) &&
279            (trans_check_nodes.end() == find(trans_check_nodes.begin(),
280                                            trans_check_nodes.end(),
281                                            *new_nodes_i * -1) ) )
282         trans_check_good = false;
283     }
284
285     if (trans_check_good)
286     {
287       // this makes sure path nodes are recorded with RC status relative to
288       // the base species
289       if ( path[depth-1] >= 0)
290         path.push_back(*new_nodes_i);
291       else
292         path.push_back(*new_nodes_i * -1);
293
294       if (depth < all_comparisons.size() - 1)
295         path_search(all_comparisons, path, depth + 1);
296       else
297         pathz.push_back(path);
298       path.pop_back();
299     } 
300     ++new_nodes_i;
301   }
302 }
303 /* use this if I ever get the friggin seqcomp match lists to sort...
304       if (binary_search(trans_check_nodes.begin(), trans_check_nodes.end(), 
305                         *new_nodes_i))
306 */
307
308 void
309 NwayPaths::find_paths_r(vector<vector<FLPs> > all_comparisons)
310 {
311   ConservedPath path;
312   int win_i, window_num;
313   list<int> new_nodes;
314   list<int>::iterator new_nodes_i, new_nodes_end;
315
316   pathz.clear();
317   window_num = all_comparisons[0][1].size();
318   // loop thru all windows in first species
319   for (win_i = 0; win_i < window_num; win_i++)
320   {
321     path.clear();
322     path.push_back(win_i);
323     new_nodes = all_comparisons[0][1].match_locations(path[0]);
324     new_nodes_i = new_nodes.begin();
325     new_nodes_end = new_nodes.end();
326     //if (new_nodes_i != new_nodes_end)
327     //cout << "* species 0 node: " << win_i << endl;
328     path.push_back(0);
329     while(new_nodes_i != new_nodes_end)
330     {
331       //cout << "  * species 1 node: " << *new_nodes_i << endl;
332       path[1] = *new_nodes_i;
333       path_search(all_comparisons, path, 2);
334       ++new_nodes_i;
335     }
336   }
337 }
338
339
340 void
341 NwayPaths::save_old(string save_file_path)
342 {
343   fstream save_file;
344   list<ConservedPath >::iterator path_i, paths_end;
345   int i;
346
347   save_file.open(save_file_path.c_str(), ios::app);
348
349   path_i = pathz.begin();
350   paths_end = pathz.end();
351   while(path_i != paths_end)
352   {
353     ConservedPath& a_path = *path_i;
354     //cout << a_path.size() << endl;
355     for(i = 0; i < path_i->size(); ++i)
356       save_file << i << "," << a_path[i] << " ";
357     save_file << endl;
358     ++path_i;
359   }
360   save_file.close();
361 }
362
363
364 /*
365 void
366 NwayPaths::find_paths(vector<vector<FLPs> > all_comparisons)
367 {
368   int win_i, sp_i;
369   vector<list<list<int> > > path_src_tree;
370   list<int> new_nodes;
371   list<int>::iterator node_i, node_end;
372   <list<list<int> > >::iterator branch_i, branch_end;  
373
374   pathz.clear();
375   path_src_tree.reserve(all_comparisons.size()- 1);
376
377
378   // loop thru all windows in first species
379   for (win_i = 0; win_i < window_num; win_i++)
380   {
381     // clear the path search tree
382     for(i = 0; i < all_comparisons.size(); i++)
383       path_src_tree[i].clear();
384
385     // top level kept empty even tho implicity has one entry of the first
386     // species at this window - why bother, adds a little speed
387
388     // get connection list for first species, creating a list of nodes
389     // of second species connected to the first species at this window
390     new_nodes = all_comparisons[0][1];
391     path_src_tree[1].push_back(new_nodes);
392
393     // loop thru rest of species for this window to see if any paths of matches
394     // go across all species
395     // if path search tree becomes empty, break out of loop, no reason to search further
396     sp_i = 1;
397     while ((sp_i < all_comparisons.size()) && (path tree not empty))
398     {
399       branch_i = path_src_tree[1].begin();
400       branch_end = path_src_tree[1].end();
401       while (branch_i != branch_end)
402       {
403         node_i = branch_i->begin();
404         node_end = branch_i->end();
405       }
406
407
408       // loop over all current nodes
409          // get connection list for each node
410          // loop over each previous node in list
411             // get those nodes connection list
412             // intersect previous node connections with current
413
414       ++sp_i;
415     }
416
417     // insert any of the paths found into the master list of paths
418
419     // add no paths if tmp_pathz is empty...
420   }
421 }
422
423 void NwayPaths::refine()
424 {
425 }
426 */
427
428
429 void NwayPaths::print(list<vector<int> >& dump_path)
430 {
431   list<vector<int> >::iterator pathz_i;
432   vector<int>::iterator path_i;
433
434   cout << "printing list of lists\n";
435   for (pathz_i = dump_path.begin(); pathz_i != dump_path.end(); ++pathz_i)
436   {
437     for (path_i = pathz_i->begin(); path_i != pathz_i->end(); ++path_i)
438       cout << *path_i << " ";
439     cout << endl;
440   }
441 }