1 // This file is part of the Mussa source distribution.
2 // http://mussa.caltech.edu/
3 // Contact author: Tristan De Buysscher, tristan@caltech.edu
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.
11 // ----------------------------------------
12 // ---------- mussa_nway.cc -----------
13 // ----------------------------------------
15 #include <boost/filesystem/fstream.hpp>
16 namespace fs = boost::filesystem;
18 #include "alg/mussa_callback.hpp"
19 #include "alg/nway_paths.hpp"
20 #include "alg/conserved_path.hpp"
21 #include "mussa_exceptions.hpp"
28 NwayPaths::NwayPaths()
34 NwayPaths::setup(int w, int t)
37 soft_thres = threshold;
42 //cout << "nway: thres = " << threshold
43 // << ", soft threo = " << soft_thres << endl;
47 NwayPaths::set_soft_threshold(int sft_thres)
49 soft_thres = sft_thres;
52 int NwayPaths::get_threshold() const
57 int NwayPaths::get_window() const
62 void NwayPaths::set_progress_callback(analysis_callback cb)
67 analysis_callback NwayPaths::get_progress_callback() const
72 // dumbly goes thru and combines path windows exactly adjacent (ie + 1 index)
73 // doesn't deal with interleaved adjacency
75 NwayPaths::simple_refine()
77 // ext_path remembers the first window set in an extending path
78 ConservedPath ext_path, new_path;
79 list<ConservedPath>::iterator cur_path, next_path;
80 list<ConservedPath>::iterator pathz_i;
82 bool extending, end = false;
84 refined_pathz.clear();
86 //cout << "path number is: " << pathz.size() << endl;
87 pathz_i = pathz.begin();
90 // only try to extend when pathz isn't empty.
91 if (pathz_i != pathz.end())
96 while(pathz_i != pathz.end())
98 // keep track of current path and advance to next path
103 if (pathz_i == pathz.end()) {
108 // if node for each seq is equal to the next node+1 then for all
109 // sequences then we are extending
110 extending = cur_path->nextTo(*next_path);
119 // add the extend window length as first element and add as refined
120 // now that we have the path to extend save it
122 new_path.extend(win_ext_len);
123 refined_pathz.push_back(new_path);
127 ext_path = *next_path;
131 progress_cb("refine", path_count-1, pathz.size());
135 //cout << "r_path number is: " << refined_pathz.size() << endl;
140 NwayPaths::add_path(int threshold, vector<int>& loaded_path)
142 pathz.push_back(ConservedPath(threshold, 0.0, loaded_path));
146 NwayPaths::add_path(ConservedPath loaded_path)
148 pathz.push_back(loaded_path);
153 NwayPaths::save(fs::path save_file_path)
155 fs::fstream save_file;
156 list<ConservedPath >::iterator path_i, paths_end;
158 save_file.open(save_file_path, ios::out);
160 save_file << "<Mussa type=flp seq_count=" << sequence_count();
161 save_file << " win=" << win_size;
162 // add a function para new_thres defaults to -1 to later deal with
163 // reanalysis with higher thres - if statement whether to record base thres
164 // or new thres (ie if -1, then base)
165 save_file << " thres=" << threshold << " >\n";
167 path_i = refined_pathz.begin();
168 paths_end = refined_pathz.end();
169 //path_i = pathz.begin();
170 //paths_end = pathz.end();
171 while (path_i != paths_end)
173 ConservedPath& a_path = *path_i;
174 //cout << a_path.size() << endl;
175 //first entry is the window length of the windows in the path
176 save_file << a_path.window_size << ":";
177 for(size_t i = 0; i != sequence_count(); ++i)
179 save_file << a_path[i];
180 if (i != sequence_count())
187 save_file << "</Mussa>\n";
193 NwayPaths::sequence_count()
195 if (refined_pathz.begin() == refined_pathz.end() )
198 return refined_pathz.begin()->size();
203 NwayPaths::load(fs::path load_file_path)
205 fs::fstream load_file;
206 string file_data_line, header_data, data, path_node, path_width;
207 int space_split_i, equal_split_i, comma_split_i, colon_split_i;
208 vector<int> loaded_path;
210 load_file.open(load_file_path, ios::in);
214 throw mussa_load_error("Sequence File: " + load_file_path.string() + " not found");
219 // grab mussa tag - discard for now...maybe check in future...
220 getline(load_file,file_data_line);
221 space_split_i = file_data_line.find(" ");
222 file_data_line = file_data_line.substr(space_split_i+1);
223 // grab type tag - need in future to distinguish between flp and vlp paths
224 space_split_i = file_data_line.find(" ");
225 file_data_line = file_data_line.substr(space_split_i+1);
226 // get species/seq number
227 space_split_i = file_data_line.find(" ");
228 header_data = file_data_line.substr(0,space_split_i);
229 equal_split_i = header_data.find("=");
230 data = file_data_line.substr(equal_split_i+1);
231 unsigned int species_num = atoi (data.c_str());
232 file_data_line = file_data_line.substr(space_split_i+1);
234 space_split_i = file_data_line.find(" ");
235 header_data = file_data_line.substr(0,space_split_i);
236 equal_split_i = header_data.find("=");
237 data = file_data_line.substr(equal_split_i+1);
238 win_size = atoi (data.c_str());
239 file_data_line = file_data_line.substr(space_split_i+1);
240 // get threshold size
241 space_split_i = file_data_line.find(" ");
242 header_data = file_data_line.substr(0,space_split_i);
243 equal_split_i = header_data.find("=");
244 data = file_data_line.substr(equal_split_i+1);
245 threshold = atoi (data.c_str());
246 file_data_line = file_data_line.substr(space_split_i+1);
248 //cout << "seq_num=" << species_num << " win=" << win_size;
249 //cout << " thres=" << threshold << endl;
251 // clear out the current data
252 refined_pathz.clear();
256 getline(load_file,file_data_line);
257 while ( (!load_file.eof()) && (file_data_line != "</Mussa>") )
259 if (file_data_line != "")
262 colon_split_i = file_data_line.find(":");
263 // whats our window size?
264 path_width = file_data_line.substr(0,colon_split_i);
265 file_data_line = file_data_line.substr(colon_split_i+1);
266 for(size_t i = 0; i < species_num; i++)
268 comma_split_i = file_data_line.find(",");
269 path_node = file_data_line.substr(0, comma_split_i);
270 temp = atoi (path_node.c_str());
271 loaded_path.push_back(temp);
272 file_data_line = file_data_line.substr(comma_split_i+1);
274 assert (loaded_path.size() == species_num );
275 refined_pathz.push_back(ConservedPath(atoi(path_width.c_str()),
279 getline(load_file,file_data_line);
287 NwayPaths::path_search(vector<vector<FLPs> > all_comparisons, ConservedPath path, size_t depth)
289 list<int> new_nodes, trans_check_nodes;
290 list<int>::iterator new_nodes_i, new_nodes_end;
291 bool trans_check_good;
293 new_nodes = all_comparisons[depth - 1][depth].match_locations(path[depth-1]);
294 new_nodes_i = new_nodes.begin();
295 new_nodes_end = new_nodes.end();
296 while(new_nodes_i != new_nodes_end)
298 //cout << " * species " << depth << " node: " << *new_nodes_i << endl;
299 // check transitivity with previous nodes in path
300 trans_check_good = true;
301 for(size_t i = 0; i < depth - 1; i++)
303 trans_check_nodes = all_comparisons[i][depth].match_locations(path[i]);
304 if ( (trans_check_nodes.end() == find(trans_check_nodes.begin(),
305 trans_check_nodes.end(),
307 (trans_check_nodes.end() == find(trans_check_nodes.begin(),
308 trans_check_nodes.end(),
309 *new_nodes_i * -1) ) )
310 trans_check_good = false;
313 if (trans_check_good)
315 // this makes sure path nodes are recorded with RC status relative to
317 if ( path[depth-1] >= 0)
318 path.push_back(*new_nodes_i);
320 path.push_back(*new_nodes_i * -1);
322 if (depth < all_comparisons.size() - 1)
323 path_search(all_comparisons, path, depth + 1);
325 pathz.push_back(path);
331 /* use this if I ever get the friggin seqcomp match lists to sort...
332 if (binary_search(trans_check_nodes.begin(), trans_check_nodes.end(),
337 NwayPaths::find_paths_r(vector<vector<FLPs> > all_comparisons)
340 int win_i, window_num;
342 list<int>::iterator new_nodes_i, new_nodes_end;
345 window_num = all_comparisons[0][1].size();
346 // loop thru all windows in first species
347 for (win_i = 0; win_i < window_num; win_i++)
350 path.push_back(win_i);
351 new_nodes = all_comparisons[0][1].match_locations(path[0]);
352 new_nodes_i = new_nodes.begin();
353 new_nodes_end = new_nodes.end();
354 //if (new_nodes_i != new_nodes_end)
355 //cout << "* species 0 node: " << win_i << endl;
357 while(new_nodes_i != new_nodes_end)
359 //cout << " * species 1 node: " << *new_nodes_i << endl;
360 path[1] = *new_nodes_i;
361 path_search(all_comparisons, path, 2);
369 NwayPaths::save_old(fs::path save_file_path)
371 fs::fstream save_file;
372 list<ConservedPath >::iterator path_i, paths_end;
375 save_file.open(save_file_path, ios::app);
377 path_i = pathz.begin();
378 paths_end = pathz.end();
379 while(path_i != paths_end)
381 ConservedPath& a_path = *path_i;
382 //cout << a_path.size() << endl;
383 for(i = 0; i < path_i->size(); ++i)
384 save_file << i << "," << a_path[i] << " ";
394 NwayPaths::find_paths(vector<vector<FLPs> > all_comparisons)
397 vector<list<list<int> > > path_src_tree;
399 list<int>::iterator node_i, node_end;
400 <list<list<int> > >::iterator branch_i, branch_end;
403 path_src_tree.reserve(all_comparisons.size()- 1);
406 // loop thru all windows in first species
407 for (win_i = 0; win_i < window_num; win_i++)
409 // clear the path search tree
410 for(i = 0; i < all_comparisons.size(); i++)
411 path_src_tree[i].clear();
413 // top level kept empty even tho implicity has one entry of the first
414 // species at this window - why bother, adds a little speed
416 // get connection list for first species, creating a list of nodes
417 // of second species connected to the first species at this window
418 new_nodes = all_comparisons[0][1];
419 path_src_tree[1].push_back(new_nodes);
421 // loop thru rest of species for this window to see if any paths of matches
422 // go across all species
423 // if path search tree becomes empty, break out of loop, no reason to search further
425 while ((sp_i < all_comparisons.size()) && (path tree not empty))
427 branch_i = path_src_tree[1].begin();
428 branch_end = path_src_tree[1].end();
429 while (branch_i != branch_end)
431 node_i = branch_i->begin();
432 node_end = branch_i->end();
436 // loop over all current nodes
437 // get connection list for each node
438 // loop over each previous node in list
439 // get those nodes connection list
440 // intersect previous node connections with current
445 // insert any of the paths found into the master list of paths
447 // add no paths if tmp_pathz is empty...
451 void NwayPaths::refine()
457 void NwayPaths::print(list<vector<int> >& dump_path)
459 list<vector<int> >::iterator pathz_i;
460 vector<int>::iterator path_i;
462 cout << "printing list of lists\n";
463 for (pathz_i = dump_path.begin(); pathz_i != dump_path.end(); ++pathz_i)
465 for (path_i = pathz_i->begin(); path_i != pathz_i->end(); ++path_i)
466 cout << *path_i << " ";