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/nway_paths.hpp"
19 #include "alg/conserved_path.hpp"
20 #include "mussa_exceptions.hpp"
27 NwayPaths::NwayPaths()
32 NwayPaths::setup(int w, int t)
35 soft_thres = threshold;
39 //cout << "nway: thres = " << threshold
40 // << ", soft threo = " << soft_thres << endl;
44 NwayPaths::set_soft_thres(int sft_thres)
46 soft_thres = sft_thres;
49 int NwayPaths::get_threshold() const
54 int NwayPaths::get_window() const
59 // dumbly goes thru and combines path windows exactly adjacent (ie + 1 index)
60 // doesn't deal with interleaved adjacency
62 NwayPaths::simple_refine()
64 // ext_path remembers the first window set in an extending path
65 ConservedPath ext_path, new_path;
66 list<ConservedPath>::iterator cur_path, next_path;
67 list<ConservedPath>::iterator pathz_i;
69 bool extending, end = false;
71 refined_pathz.clear();
73 //cout << "path number is: " << pathz.size() << endl;
74 pathz_i = pathz.begin();
76 // only try to extend when pathz isn't empty.
77 if (pathz_i != pathz.end())
81 while(pathz_i != pathz.end())
83 // keep track of current path and advance to next path
87 if (pathz_i == pathz.end()) {
92 // if node for each seq is equal to the next node+1 then for all
93 // sequences then we are extending
94 extending = cur_path->nextTo(*next_path);
103 // add the extend window length as first element and add as refined
104 // now that we have the path to extend save it
106 new_path.extend(win_ext_len);
107 refined_pathz.push_back(new_path);
111 ext_path = *next_path;
116 //cout << "r_path number is: " << refined_pathz.size() << endl;
121 NwayPaths::add_path(int threshold, vector<int>& loaded_path)
123 pathz.push_back(ConservedPath(threshold, 0.0, loaded_path));
127 NwayPaths::add_path(ConservedPath loaded_path)
129 pathz.push_back(loaded_path);
134 NwayPaths::save(fs::path save_file_path)
136 fs::fstream save_file;
137 list<ConservedPath >::iterator path_i, paths_end;
139 save_file.open(save_file_path, ios::out);
141 save_file << "<Mussa type=flp seq_count=" << sequence_count();
142 save_file << " win=" << win_size;
143 // add a function para new_thres defaults to -1 to later deal with
144 // reanalysis with higher thres - if statement whether to record base thres
145 // or new thres (ie if -1, then base)
146 save_file << " thres=" << threshold << " >\n";
148 path_i = refined_pathz.begin();
149 paths_end = refined_pathz.end();
150 //path_i = pathz.begin();
151 //paths_end = pathz.end();
152 while (path_i != paths_end)
154 ConservedPath& a_path = *path_i;
155 //cout << a_path.size() << endl;
156 //first entry is the window length of the windows in the path
157 save_file << a_path.window_size << ":";
158 for(size_t i = 0; i != sequence_count(); ++i)
160 save_file << a_path[i];
161 if (i != sequence_count())
168 save_file << "</Mussa>\n";
174 NwayPaths::sequence_count()
176 if (refined_pathz.begin() == refined_pathz.end() )
179 return refined_pathz.begin()->size();
184 NwayPaths::load(fs::path load_file_path)
186 fs::fstream load_file;
187 string file_data_line, header_data, data, path_node, path_width;
188 int space_split_i, equal_split_i, comma_split_i, colon_split_i;
189 vector<int> loaded_path;
191 load_file.open(load_file_path, ios::in);
195 throw mussa_load_error("Sequence File: " + load_file_path.string() + " not found");
200 // grab mussa tag - discard for now...maybe check in future...
201 getline(load_file,file_data_line);
202 space_split_i = file_data_line.find(" ");
203 file_data_line = file_data_line.substr(space_split_i+1);
204 // grab type tag - need in future to distinguish between flp and vlp paths
205 space_split_i = file_data_line.find(" ");
206 file_data_line = file_data_line.substr(space_split_i+1);
207 // get species/seq number
208 space_split_i = file_data_line.find(" ");
209 header_data = file_data_line.substr(0,space_split_i);
210 equal_split_i = header_data.find("=");
211 data = file_data_line.substr(equal_split_i+1);
212 unsigned int species_num = atoi (data.c_str());
213 file_data_line = file_data_line.substr(space_split_i+1);
215 space_split_i = file_data_line.find(" ");
216 header_data = file_data_line.substr(0,space_split_i);
217 equal_split_i = header_data.find("=");
218 data = file_data_line.substr(equal_split_i+1);
219 win_size = atoi (data.c_str());
220 file_data_line = file_data_line.substr(space_split_i+1);
221 // get threshold size
222 space_split_i = file_data_line.find(" ");
223 header_data = file_data_line.substr(0,space_split_i);
224 equal_split_i = header_data.find("=");
225 data = file_data_line.substr(equal_split_i+1);
226 threshold = atoi (data.c_str());
227 file_data_line = file_data_line.substr(space_split_i+1);
229 //cout << "seq_num=" << species_num << " win=" << win_size;
230 //cout << " thres=" << threshold << endl;
232 // clear out the current data
233 refined_pathz.clear();
237 getline(load_file,file_data_line);
238 while ( (!load_file.eof()) && (file_data_line != "</Mussa>") )
240 if (file_data_line != "")
243 colon_split_i = file_data_line.find(":");
244 // whats our window size?
245 path_width = file_data_line.substr(0,colon_split_i);
246 file_data_line = file_data_line.substr(colon_split_i+1);
247 for(size_t i = 0; i < species_num; i++)
249 comma_split_i = file_data_line.find(",");
250 path_node = file_data_line.substr(0, comma_split_i);
251 temp = atoi (path_node.c_str());
252 loaded_path.push_back(temp);
253 file_data_line = file_data_line.substr(comma_split_i+1);
255 assert (loaded_path.size() == species_num );
256 refined_pathz.push_back(ConservedPath(atoi(path_width.c_str()),
260 getline(load_file,file_data_line);
268 NwayPaths::path_search(vector<vector<FLPs> > all_comparisons, ConservedPath path, size_t depth)
270 list<int> new_nodes, trans_check_nodes;
271 list<int>::iterator new_nodes_i, new_nodes_end;
272 bool trans_check_good;
274 new_nodes = all_comparisons[depth - 1][depth].match_locations(path[depth-1]);
275 new_nodes_i = new_nodes.begin();
276 new_nodes_end = new_nodes.end();
277 while(new_nodes_i != new_nodes_end)
279 //cout << " * species " << depth << " node: " << *new_nodes_i << endl;
280 // check transitivity with previous nodes in path
281 trans_check_good = true;
282 for(size_t i = 0; i < depth - 1; i++)
284 trans_check_nodes = all_comparisons[i][depth].match_locations(path[i]);
285 if ( (trans_check_nodes.end() == find(trans_check_nodes.begin(),
286 trans_check_nodes.end(),
288 (trans_check_nodes.end() == find(trans_check_nodes.begin(),
289 trans_check_nodes.end(),
290 *new_nodes_i * -1) ) )
291 trans_check_good = false;
294 if (trans_check_good)
296 // this makes sure path nodes are recorded with RC status relative to
298 if ( path[depth-1] >= 0)
299 path.push_back(*new_nodes_i);
301 path.push_back(*new_nodes_i * -1);
303 if (depth < all_comparisons.size() - 1)
304 path_search(all_comparisons, path, depth + 1);
306 pathz.push_back(path);
312 /* use this if I ever get the friggin seqcomp match lists to sort...
313 if (binary_search(trans_check_nodes.begin(), trans_check_nodes.end(),
318 NwayPaths::find_paths_r(vector<vector<FLPs> > all_comparisons)
321 int win_i, window_num;
323 list<int>::iterator new_nodes_i, new_nodes_end;
326 window_num = all_comparisons[0][1].size();
327 // loop thru all windows in first species
328 for (win_i = 0; win_i < window_num; win_i++)
331 path.push_back(win_i);
332 new_nodes = all_comparisons[0][1].match_locations(path[0]);
333 new_nodes_i = new_nodes.begin();
334 new_nodes_end = new_nodes.end();
335 //if (new_nodes_i != new_nodes_end)
336 //cout << "* species 0 node: " << win_i << endl;
338 while(new_nodes_i != new_nodes_end)
340 //cout << " * species 1 node: " << *new_nodes_i << endl;
341 path[1] = *new_nodes_i;
342 path_search(all_comparisons, path, 2);
350 NwayPaths::save_old(fs::path save_file_path)
352 fs::fstream save_file;
353 list<ConservedPath >::iterator path_i, paths_end;
356 save_file.open(save_file_path, ios::app);
358 path_i = pathz.begin();
359 paths_end = pathz.end();
360 while(path_i != paths_end)
362 ConservedPath& a_path = *path_i;
363 //cout << a_path.size() << endl;
364 for(i = 0; i < path_i->size(); ++i)
365 save_file << i << "," << a_path[i] << " ";
375 NwayPaths::find_paths(vector<vector<FLPs> > all_comparisons)
378 vector<list<list<int> > > path_src_tree;
380 list<int>::iterator node_i, node_end;
381 <list<list<int> > >::iterator branch_i, branch_end;
384 path_src_tree.reserve(all_comparisons.size()- 1);
387 // loop thru all windows in first species
388 for (win_i = 0; win_i < window_num; win_i++)
390 // clear the path search tree
391 for(i = 0; i < all_comparisons.size(); i++)
392 path_src_tree[i].clear();
394 // top level kept empty even tho implicity has one entry of the first
395 // species at this window - why bother, adds a little speed
397 // get connection list for first species, creating a list of nodes
398 // of second species connected to the first species at this window
399 new_nodes = all_comparisons[0][1];
400 path_src_tree[1].push_back(new_nodes);
402 // loop thru rest of species for this window to see if any paths of matches
403 // go across all species
404 // if path search tree becomes empty, break out of loop, no reason to search further
406 while ((sp_i < all_comparisons.size()) && (path tree not empty))
408 branch_i = path_src_tree[1].begin();
409 branch_end = path_src_tree[1].end();
410 while (branch_i != branch_end)
412 node_i = branch_i->begin();
413 node_end = branch_i->end();
417 // loop over all current nodes
418 // get connection list for each node
419 // loop over each previous node in list
420 // get those nodes connection list
421 // intersect previous node connections with current
426 // insert any of the paths found into the master list of paths
428 // add no paths if tmp_pathz is empty...
432 void NwayPaths::refine()
438 void NwayPaths::print(list<vector<int> >& dump_path)
440 list<vector<int> >::iterator pathz_i;
441 vector<int>::iterator path_i;
443 cout << "printing list of lists\n";
444 for (pathz_i = dump_path.begin(); pathz_i != dump_path.end(); ++pathz_i)
446 for (path_i = pathz_i->begin(); path_i != pathz_i->end(); ++path_i)
447 cout << *path_i << " ";