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 "alg/nway_paths.hh"
16 #include "alg/conserved_path.h"
17 #include "mussa_exceptions.hh"
25 NwayPaths::NwayPaths()
30 NwayPaths::setup(int w, int t)
33 soft_thres = threshold;
37 //cout << "nway: thres = " << threshold
38 // << ", soft threo = " << soft_thres << endl;
42 NwayPaths::set_soft_thres(int sft_thres)
44 soft_thres = sft_thres;
48 // dumbly goes thru and combines path windows exactly adjacent (ie + 1 index)
49 // doesn't deal with interleaved adjacency
51 NwayPaths::simple_refine()
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;
58 bool extending, end = false;
60 refined_pathz.clear();
62 //cout << "path number is: " << pathz.size() << endl;
63 pathz_i = pathz.begin();
65 // only try to extend when pathz isn't empty.
66 if (pathz_i != pathz.end())
68 ext_path = ExtendedConservedPath( win_size, *pathz_i);
70 while(pathz_i != pathz.end())
72 // keep track of current path and advance to next path
75 if (pathz_i == pathz.end())
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);
95 // add the extend window length as first element and add as refined
96 // now that we have the path to extend save it
98 new_path.extend(win_ext_len);
99 refined_pathz.push_back(new_path);
102 ext_path = ExtendedConservedPath( win_size, *next_path);
106 //cout << "r_path number is: " << refined_pathz.size() << endl;
111 NwayPaths::add_path(int threshold, vector<int>& loaded_path)
113 pathz.push_back(ConservedPath(threshold, loaded_path));
117 NwayPaths::add_path(ConservedPath loaded_path)
119 pathz.push_back(loaded_path);
124 NwayPaths::save(string save_file_path)
127 list<ExtendedConservedPath >::iterator path_i, paths_end;
129 save_file.open(save_file_path.c_str(), ios::out);
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";
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)
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)
150 save_file << a_path[i];
151 if (i != sequence_count())
158 save_file << "</Mussa>\n";
164 NwayPaths::sequence_count()
166 if (refined_pathz.begin() == refined_pathz.end() )
169 return refined_pathz.begin()->size();
174 NwayPaths::load(string load_file_path)
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;
181 load_file.open(load_file_path.c_str(), ios::in);
185 throw mussa_load_error("Sequence File: " + load_file_path + " not found");
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);
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);
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);
219 //cout << "seq_num=" << species_num << " win=" << win_size;
220 //cout << " thres=" << threshold << endl;
222 // clear out the current data
223 refined_pathz.clear();
227 getline(load_file,file_data_line);
228 while ( (!load_file.eof()) && (file_data_line != "</Mussa>") )
230 if (file_data_line != "")
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++)
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);
245 assert (loaded_path.size() == species_num );
246 refined_pathz.push_back(ExtendedConservedPath(atoi(path_width.c_str()),
250 getline(load_file,file_data_line);
258 NwayPaths::path_search(vector<vector<FLPs> > all_comparisons, ConservedPath path, int depth)
260 list<int> new_nodes, trans_check_nodes;
261 list<int>::iterator new_nodes_i, new_nodes_end;
263 bool trans_check_good;
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)
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++)
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(),
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;
285 if (trans_check_good)
287 // this makes sure path nodes are recorded with RC status relative to
289 if ( path[depth-1] >= 0)
290 path.push_back(*new_nodes_i);
292 path.push_back(*new_nodes_i * -1);
294 if (depth < all_comparisons.size() - 1)
295 path_search(all_comparisons, path, depth + 1);
297 pathz.push_back(path);
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(),
309 NwayPaths::find_paths_r(vector<vector<FLPs> > all_comparisons)
312 int win_i, window_num;
314 list<int>::iterator new_nodes_i, new_nodes_end;
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++)
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;
329 while(new_nodes_i != new_nodes_end)
331 //cout << " * species 1 node: " << *new_nodes_i << endl;
332 path[1] = *new_nodes_i;
333 path_search(all_comparisons, path, 2);
341 NwayPaths::save_old(string save_file_path)
344 list<ConservedPath >::iterator path_i, paths_end;
347 save_file.open(save_file_path.c_str(), ios::app);
349 path_i = pathz.begin();
350 paths_end = pathz.end();
351 while(path_i != paths_end)
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] << " ";
366 NwayPaths::find_paths(vector<vector<FLPs> > all_comparisons)
369 vector<list<list<int> > > path_src_tree;
371 list<int>::iterator node_i, node_end;
372 <list<list<int> > >::iterator branch_i, branch_end;
375 path_src_tree.reserve(all_comparisons.size()- 1);
378 // loop thru all windows in first species
379 for (win_i = 0; win_i < window_num; win_i++)
381 // clear the path search tree
382 for(i = 0; i < all_comparisons.size(); i++)
383 path_src_tree[i].clear();
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
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);
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
397 while ((sp_i < all_comparisons.size()) && (path tree not empty))
399 branch_i = path_src_tree[1].begin();
400 branch_end = path_src_tree[1].end();
401 while (branch_i != branch_end)
403 node_i = branch_i->begin();
404 node_end = branch_i->end();
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
417 // insert any of the paths found into the master list of paths
419 // add no paths if tmp_pathz is empty...
423 void NwayPaths::refine()
429 void NwayPaths::print(list<vector<int> >& dump_path)
431 list<vector<int> >::iterator pathz_i;
432 vector<int>::iterator path_i;
434 cout << "printing list of lists\n";
435 for (pathz_i = dump_path.begin(); pathz_i != dump_path.end(); ++pathz_i)
437 for (path_i = pathz_i->begin(); path_i != pathz_i->end(); ++path_i)
438 cout << *path_i << " ";