-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
274 lines (239 loc) · 8.14 KB
/
graph.cpp
File metadata and controls
274 lines (239 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "graph.h"
void graph::clear(){
components.clear();
}
graph::graph(hash_key _max_hash_key){
max_hash_key = _max_hash_key;
}
void graph::construct_graph(list<int>& good_edge_inds,
const vector<edge>& _edges){//,
//const list<int>& good_edge_inds,
//vector<node>& _nodes){
graph_component starter(max_hash_key);
//starter.edges = _edges;
//starter.nodes = _nodes;
/*
vector<int> active_edges;
for(int i=0; i<starter.edges.size(); i++){
active_edges.push_back(i);
}
*/
vector<vector< hash_key > > map_hash_keys;
vector<vector< orientation > > map_orients;
vector<vector< int > > new_edges;
list<int> consistent_edge_set =
starter.consistent_edge_inds(good_edge_inds, _edges, map_hash_keys,
map_orients, new_edges);
//starter.make_hash_table();
for(int i=0; i<map_hash_keys.size(); i++){
cerr<<"construct_graph: making "<<i;
cerr<<" component of "<<map_hash_keys.size();
cerr<<" nodes: "<<map_hash_keys[i].size();
cerr<<" edges: "<<new_edges[i].size()<<endl;
assert(map_hash_keys[i].size() == map_orients[i].size());
graph_component new_comp(max_hash_key);
assert(map_hash_keys[i].size() == map_orients[i].size());
for(int j=0; j<map_hash_keys[i].size(); j++){
hash_key cur_map_hk = map_hash_keys[i][j];
//cerr<<"cur_map_hk = "<<cur_map_hk<<endl;
//string cur_map_name = return_map_name(cur_map_hash_key);
//int cur_node_index = starter.get_node_index(cur_map_name);
//int cur_node_index = starter.get_node_index(cur_map_hash_key);
//assert(cur_node_index != UNDEF_IND);
/*
node new_node(starter.nodes[cur_node_index].hk,
starter.nodes[cur_node_index].read_name,
map_orients[i][j],
starter.nodes[cur_node_index].map_read);
new_node.id = starter.nodes[cur_node_index].id;
*/
vector<fr_size> cur_map_read = return_map_read(cur_map_hk);
node new_node(cur_map_hk, return_map_name(cur_map_hk),
map_orients[i][j], cur_map_read);
new_comp.nodes.push_back(new_node);
}
for(int j=0; j<new_edges[i].size(); j++){
int cur_edge_ind = new_edges[i][j];
//cout<<"adding an edge:"<<cur_edge_ind<<endl;
//assert(cur_edge_ind >= 0 && cur_edge_ind<starter.edges.size());
assert(cur_edge_ind >= 0 && cur_edge_ind < _edges.size());
new_comp.edges.push_back(_edges[cur_edge_ind]);
}
cout<<"filling out the component structure."<<endl;
new_comp.fill_the_component_data();
cout<<"structure completed"<<endl;
if(new_comp.nodes.size() > 0) components.push_back(new_comp);
}
//starter.remove_hash_table();
}
void graph::init_data(){
for(int i=0; i<components.size(); i++){
components[i].clean();
components[i].fill_the_component_data();
//components[i].index_nodes();
//components[i].assign_edge_weights();
}
}
void graph::assign_weights(){
for(int i=0; i<components.size(); i++){
components[i].assign_edge_weights();
}
}
void graph::output_graph(const char* output_file){
remove (output_file);
ofstream out_str;
out_str.open(output_file);
if(!out_str.good()){
cout<<"bad graph output file name: "<<output_file;
cout<<endl;
assert(false);
}
out_str<<"digraph G{"<<endl;
out_str<<"size="<<char(34)<<"8,5"<<char(34)<<";"<<endl;
out_str<<"rankdir=LR;"<<endl;
//out_str<<"node [shape=circle];"<<endl;
for(int i=0; i<components.size(); i++){
if(components[i].edges.size() >= 10){
ostringstream comp_prefix;
comp_prefix<<i<<":";
//string prefix = comp_prefix.c_str();
//components[i].index_nodes();
components[i].output_edges(out_str, comp_prefix);
}
}
out_str<<"}"<<endl;
out_str.close();
}
/*
void graph::add_edge(edge& e, vector<fr_size>& left_map_read,
vector<fr_size>& right_map_read){
bool left_map_found = false;
bool right_map_found = false;
int left_comp_ind = -1;
int right_comp_ind = -1;
int left_read_ind = -1;
int right_read_ind = -1;
for(int i=0; i<components.size(); i++){
//first find which components the maps belong to
for(int j=0; j<components[i].nodes.size(); j++){
if(e.left_map == components[i].nodes[j].read_name){
left_map_found = true;
left_comp_ind = i;
left_read_ind = j;
}
if(e.right_map == components[i].nodes[j].read_name){
right_map_found = true;
right_comp_ind = i;
right_read_ind = j;
}
}
}
if(!left_map_found && !right_map_found){
//make a new component
cout<<"making a new component"<<endl;
graph_component new_component(max_hash_key);
new_component.add_edge(e, left_map_read, right_map_read);
components.push_back(new_component);
return;
}
if(left_map_found && !right_map_found){
assert(left_comp_ind >= 0 && left_comp_ind < components.size());
cout<<"updating existing component with ";
cout<<e.right_map<<endl;
components[left_comp_ind].add_edge(e, left_map_read, right_map_read);
return;
}
if(!left_map_found && right_map_found){
assert(right_comp_ind >= 0 && right_comp_ind < components.size());
cout<<"updating existing component with ";
cout<<e.left_map<<endl;
components[right_comp_ind].add_edge(e, left_map_read, right_map_read);
return;
}
assert(left_map_found && right_map_found);
if(right_comp_ind != left_comp_ind){
cout<<"merging components "<<left_comp_ind;
cout<<" and "<<right_comp_ind<<endl;
assert(left_comp_ind != -1 && right_comp_ind != -1);
//merge two components
graph_component new_comp(max_hash_key);
for(int i=0; i<components[left_comp_ind].nodes.size(); i++){
new_comp.nodes.push_back(components[left_comp_ind].nodes[i]);
}
if(e.left_orient == components[left_comp_ind].nodes[left_read_ind].orient){
if(e.right_orient == components[right_comp_ind].nodes[right_read_ind].orient){
//both maps in the same orientation as in the overlap
for(int i=0; i<components[right_comp_ind].nodes.size(); i++){
new_comp.nodes.push_back(components[right_comp_ind].nodes[i]);
}
}
else{
//left map in the same orient, right map in the opposite
cout<<"reversing required 1"<<endl;
for(int i=0; i<components[right_comp_ind].nodes.size(); i++){
new_comp.nodes.push_back
(components[right_comp_ind].nodes[i].inverse());
}
}
}
else{
if(e.right_orient == components[right_comp_ind].nodes[right_read_ind].orient){
cout<<"reversing required 2"<<endl;
for(int i=0; i<components[right_comp_ind].nodes.size(); i++){
new_comp.nodes.push_back
(components[right_comp_ind].nodes[i].inverse());
}
}
else{
cout<<"both in reverse orient, no reversing required"<<endl;
for(int i=0; i<components[right_comp_ind].nodes.size(); i++){
new_comp.nodes.push_back(components[right_comp_ind].nodes[i]);
}
}
}
for(int i=0; i<components[left_comp_ind].edges.size(); i++){
new_comp.edges.push_back(components[left_comp_ind].edges[i]);
}
for(int i=0; i<components[right_comp_ind].edges.size(); i++){
new_comp.edges.push_back(components[right_comp_ind].edges[i]);
}
new_comp.add_edge(e, left_map_read, right_map_read);
if(left_comp_ind < right_comp_ind){
vector<graph_component>::iterator it;
it = components.begin() + right_comp_ind;
components.erase(it);
it = components.begin() + left_comp_ind;
components.erase(it);
}
else{
vector<graph_component>::iterator it;
it = components.begin() + left_comp_ind;
components.erase(it);
it = components.begin() + right_comp_ind;
components.erase(it);
}
components.push_back(new_comp);
return;
}
else{
//both are in the same component
assert(left_comp_ind != -1 && right_comp_ind != -1);
components[left_comp_ind].add_edge(e, left_map_read, right_map_read);
return;
}
assert(false);
}
*/
void graph::print(){
cout<<"--------------"<<endl;
cout<<"printing the graph data:"<<endl;
cout<<"components: "<<components.size()<<endl;
for(int i=0; i<components.size(); i++){
cout<<endl;
cout<<"+++++"<<endl;
cout<<"component: "<<i<<endl;
components[i].print();
cout<<"+++++"<<endl;
}
cout<<"--------------"<<endl<<endl;
}