ae9b69a975a70927ba8ed2aab369e13c502c21ec
[mussa.git] / mussa_nway.cc
1 //                        ----------------------------------------
2 //                         ---------- mussa_nway.cc  -----------
3 //                        ----------------------------------------
4
5 #include "mussa_nway.hh"
6 /*
7       cout << "fee\n";
8       cout << "fie\n";
9       cout << "foe\n";
10       cout << "fum\n";
11 */
12
13
14 Nway_Paths::Nway_Paths()
15 {
16 }
17
18 void
19 Nway_Paths::setup(int sp_num)
20 {
21   species_num = sp_num;
22   pathz.clear();
23 }
24
25
26 void
27 Nway_Paths::path_search(vector<vector<FLPs> > all_comparisons, vector<int> path, int depth)
28 {
29   list<int> new_nodes, trans_check_nodes;
30   list<int>::iterator new_nodes_i, new_nodes_end;
31   int i;
32   bool trans_check_good;
33
34   new_nodes = all_comparisons[depth - 1][depth].matches(path[depth-1]);
35   new_nodes_i = new_nodes.begin();
36   new_nodes_end = new_nodes.end();
37   while(new_nodes_i != new_nodes_end)
38   {
39     //cout << "    * species " << depth << " node: " << *new_nodes_i << endl;
40     // check transitivity with previous nodes in path
41     for(i = 0; i < depth - 1; i++)
42     {
43       trans_check_good = true;
44       trans_check_nodes = all_comparisons[i][depth].matches(path[i]);
45       if ( (trans_check_nodes.end() == find(trans_check_nodes.begin(),
46                                             trans_check_nodes.end(),
47                                             *new_nodes_i) ) &&
48            (trans_check_nodes.end() == find(trans_check_nodes.begin(),
49                                            trans_check_nodes.end(),
50                                            *new_nodes_i * -1) ) )
51         trans_check_good = false;
52     }
53
54     if (trans_check_good)
55     {
56       if ( path[depth-1] >= 0)
57         path.push_back(*new_nodes_i);
58       else
59         path.push_back(*new_nodes_i * -1);
60       if (depth < species_num - 1)
61         path_search(all_comparisons, path, depth + 1);
62       else
63         pathz.push_back(path);
64       path.pop_back();
65     } 
66     ++new_nodes_i;
67   }
68 }
69 //          cout << "    ****I have the power...\n";
70
71 /* use this if I ever get the friggin seqcomp match lists to sort...
72       if (binary_search(trans_check_nodes.begin(), trans_check_nodes.end(), 
73                         *new_nodes_i))
74 */
75
76 void
77 Nway_Paths::find_paths_r(vector<vector<FLPs> > all_comparisons)
78 {
79   vector<int> path;
80   int win_i, window_num;
81   list<int> new_nodes;
82   list<int>::iterator new_nodes_i, new_nodes_end;
83
84   pathz.clear();
85   window_num = all_comparisons[0][1].win_num();
86   cout << window_num << endl;
87   // loop thru all windows in first species
88   for (win_i = 0; win_i < window_num; win_i++)
89   {
90     path.clear();
91     path.push_back(win_i);
92     new_nodes = all_comparisons[0][1].matches(path[0]);
93     new_nodes_i = new_nodes.begin();
94     new_nodes_end = new_nodes.end();
95     //if (new_nodes_i != new_nodes_end)
96     //cout << "* species 0 node: " << win_i << endl;
97     //  cout << "foookin hell\n";
98     path.push_back(0);
99     while(new_nodes_i != new_nodes_end)
100     {
101       //cout << "  * species 1 node: " << *new_nodes_i << endl;
102       path[1] = *new_nodes_i;
103       path_search(all_comparisons, path, 2);
104       ++new_nodes_i;
105     }
106   }
107 }
108
109
110 void
111 Nway_Paths::add_path(vector<int> loaded_path)
112 {
113   pathz.push_back(loaded_path);
114 }
115
116
117 void
118 Nway_Paths::save(string save_file_path)
119 {
120   fstream save_file;
121   list<vector<int> >::iterator path_i, paths_end;
122   vector<int> a_path;
123   int i;
124
125   save_file.open(save_file_path.c_str(), ios::out);
126
127   //save_file << "<Seqcomp type=" << ana_type << " win=" << window_size;
128   //save_file << " thres=" << hard_threshold << ">\n";
129
130   path_i = pathz.begin();
131   paths_end = pathz.end();
132   if (path_i == paths_end)
133     cout << "Arrrrrrgggghhhhhh\n";
134   while(path_i != paths_end)
135   {
136     a_path = *path_i;
137     //cout << a_path.size() << endl;
138     for(i = 0; i < species_num; ++i)
139       save_file << i << "," << a_path[i] << " ";
140     save_file << endl;
141     ++path_i;
142   }
143
144 //save_file << "</Mussa>\n";
145   save_file.close();
146 }
147
148 void
149 Nway_Paths::save_old(string save_file_path)
150 {
151   fstream save_file;
152   list<vector<int> >::iterator path_i, paths_end;
153   vector<int> a_path;
154   int i;
155
156   save_file.open(save_file_path.c_str(), ios::app);
157
158   path_i = pathz.begin();
159   paths_end = pathz.end();
160   while(path_i != paths_end)
161   {
162     a_path = *path_i;
163     //cout << a_path.size() << endl;
164     for(i = 0; i < species_num; ++i)
165       save_file << i << "," << a_path[i] << " ";
166     save_file << endl;
167     ++path_i;
168   }
169   save_file.close();
170 }
171
172
173 /*
174 void
175 Nway_Paths::find_paths(vector<vector<FLPs> > all_comparisons)
176 {
177   int win_i, sp_i;
178   vector<list<list<int> > > path_src_tree;
179   list<int> new_nodes;
180   list<int>::iterator node_i, node_end;
181   <list<list<int> > >::iterator branch_i, branch_end;  
182
183   pathz.clear();
184   path_src_tree.reserve(species_num - 1);
185
186
187   // loop thru all windows in first species
188   for (win_i = 0; win_i < window_num; win_i++)
189   {
190     // clear the path search tree
191     for(i = 0; i < species_num; i++)
192       path_src_tree[i].clear();
193
194     // top level kept empty even tho implicity has one entry of the first
195     // species at this window - why bother, adds a little speed
196
197     // get connection list for first species, creating a list of nodes
198     // of second species connected to the first species at this window
199     new_nodes = all_comparisons[0][1];
200     path_src_tree[1].push_back(new_nodes);
201
202     // loop thru rest of species for this window to see if any paths of matches
203     // go across all species
204     // if path search tree becomes empty, break out of loop, no reason to search further
205     sp_i = 1;
206     while ((sp_i < species_num) && (path tree not empty))
207     {
208       branch_i = path_src_tree[1].begin();
209       branch_end = path_src_tree[1].end();
210       while (branch_i != branch_end)
211       {
212         node_i = branch_i->begin();
213         node_end = branch_i->end();
214       }
215
216
217       // loop over all current nodes
218          // get connection list for each node
219          // loop over each previous node in list
220             // get those nodes connection list
221             // intersect previous node connections with current
222
223       ++sp_i;
224     }
225
226     // insert any of the paths found into the master list of paths
227
228     // add no paths if tmp_pathz is empty...
229   }
230 }
231
232 void Nway_Paths::refine()
233 {
234 }
235
236 void Nway_Paths::print()
237 {
238   list<list<int> >::iterator pathz_i;
239
240   cout << "printing list of lists\n";
241   for (pathz_i = pathz.begin(); pathz_i != pathz.end(); ++pathz_i)
242   {
243     for (path_i = pathz_i->begin(); path_i != pathz_i->end(); ++path_i)
244       cout << *path_i << " ";
245     cout << endl;
246   }
247 }
248 */
249
250
251
252