-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphTrimmer.cpp
383 lines (369 loc) · 12.1 KB
/
graphTrimmer.cpp
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
Copyright (C) 2013 INRA-URGI
This file is part of TEDNA, a short reads transposable elements assembler
TEDNA is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GNU Affero General Public License for more details.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program.
**/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cmath>
#include <vector>
#include <algorithm>
#include "globals.hpp"
#include "graphTrimmer.hpp"
GraphTrimmer::GraphTrimmer(SequenceGraph &graph): _graph(graph) {}
void GraphTrimmer::trim(bool merge) {
//cout << "Starting with\n" << _graph;
bool changed;
do {
if (merge) {
mergeNodes();
}
//cout << "Now\n" << _graph;
pinchBubbles();
changed = fuseTips();
if (removeTips()) changed = true;
}
while (changed);
//cout << "Ending with\n" << _graph;
}
/*
void GraphTrimmer::removeHubs() {
unsigned int dummyIndex = _graph.getSize();
_graph.addNode(_graph.getSize(), 0);
SequenceNode &dummyNode = _graph.getNode(dummyIndex);
dummyNode.unset();
for (unsigned int i = 0; i < dummyIndex; i++) {
SequenceNode &node = _graph.getNode(i);
if (node.isSet()) {
for (short position = 0; position < Globals::POSITIONS; position++) {
if (node.isHub(position)) {
//cout << "Node " << node << ", " << position << " is a hub.";
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int ni = 0; ni < node.getNbNeighbors(position, direction); ni++) {
unsigned int neighborIndex = node.getNeighbor(position, direction, ni);
SequenceNode &neighbor = _graph.getNode(neighborIndex);
neighbor.updateLinks(i, dummyIndex, true);
}
}
node.removeLinks(position, Globals::DIRECTIONS);
//cout << " Now " << node << endl;
}
}
}
}
for (unsigned int i = 0; i < dummyIndex; i++) {
SequenceNode &node = _graph.getNode(i);
if (node.isSet()) {
for (short position = 0; position < Globals::POSITIONS; position++) {
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
bool empty = true;
for (int ni = 0; (ni < node.getNbNeighbors(position, direction)) && (empty); ni++) {
if (node.getNeighbor(position, direction, ni) != dummyIndex) {
empty = false;
}
}
if (empty) {
node.removeLinks(position, direction);
//cout << "Updated " << node << endl;
}
}
}
}
}
}
*/
void GraphTrimmer::mergeNodes() {
//cout << "Merging " << _graph.getSize() << " nodes..." << endl;
//cout << _graph << endl;
int nbMerges = 0;
vector <unsigned int> toBeMerged;
for (unsigned int i = 0; i < _graph.getSize(); i++) {
if (_graph.getNode(i).isSet()) {
toBeMerged.push_back(i);
}
}
while (! toBeMerged.empty()) {
unsigned int i = toBeMerged.back();
SequenceNode &n1 = _graph.getNode(i);
toBeMerged.pop_back();
if (n1.isSet()) {
bool merged = false;
//cout << "Inspecting node " << i << endl;
for (short position = 0; (position < Globals::POSITIONS) && (! merged); position++) {
unsigned int j = -1;
int nbNeigbors = 0;
short direction = -1;
for (short d = 0; (d < Globals::DIRECTIONS) && (nbNeigbors < 2); d++) {
for (int neighbor = 0; (neighbor < n1.getNbNeighbors(position, d)) && (nbNeigbors < 2); neighbor++) {
int id2 = n1.getNeighbor(position, d, neighbor);
const SequenceNode &n2 = _graph.getNode(id2);
if (n2.isSet()) {
j = id2;
direction = d;
nbNeigbors++;
}
}
}
if ((nbNeigbors == 1) && (i != j)) {
SequenceNode &n2 = _graph.getNode(j);
nbNeigbors = 0;
short otherPosition = (direction == Globals::DIRECT)? 1-position: position;
for (short d = 0; (d < Globals::DIRECTIONS) && (nbNeigbors < 2); d++) {
for (int neighbor = 0; (neighbor < n2.getNbNeighbors(otherPosition, d)) && (nbNeigbors < 2); neighbor++) {
const SequenceNode &n3 = _graph.getNode(n2.getNeighbor(otherPosition, d, neighbor));
if (n3.isSet()) {
nbNeigbors++;
}
}
}
if (nbNeigbors == 1) {
bool loop = false;
for (short d = 0; (d < Globals::DIRECTIONS) && (! loop); d++) {
for (int neighbor = 0; (neighbor < n2.getNbNeighbors(1-otherPosition, d)) && (! loop); neighbor++) {
if (n2.getNeighbor(1-otherPosition, d, neighbor) == i) {
loop = true;
}
}
}
if (! loop) {
//cout << "Merging nodes " << i << " and " << j << ": " << n1 << " and " << n2 << " with position " << position << " and direction " << direction;
n1.merge(position, direction, n2);
//cout << ", now have " << n1 << endl;
for (short p = 0; p < Globals::POSITIONS; p++) {
for (short d = 0; d < Globals::DIRECTIONS; d++) {
for (int n = 0; n < n2.getNbNeighbors(p, d); n++) {
unsigned int k = n2.getNeighbor(p, d, n);
if (k != i) {
SequenceNode &n3 = _graph.getNode(k);
if (n3.isSet()) {
//cout << "\tNodes of " << k << " were : " << _graph.getNode(k);
n3.updateLinks(j, i, (direction == Globals::DIRECT));
//cout << " they are now " << _graph.getNode(k) << endl;
}
}
}
}
}
n2.unset();
nbMerges++;
toBeMerged.push_back(i);
merged = true;
}
}
}
}
}
}
//cout << "... done (" << nbMerges << " merges)" << endl;
//cout << _graph << endl;
}
void GraphTrimmer::pinchBubbles() {
//cout << "Finding bubbles (graph size is " << _graph.getSize() << ")..." << endl;
if (_graph.getSize() < 3) {
return;
}
int nbBubbles = 0;
bool bubble;
for (unsigned int i = 0; i < _graph.getSize(); i++) {
const SequenceNode &node = _graph.getNode(i);
do {
bubble = false;
if (node.isSet()) {
short fork = _graph.isVee(node);
if (fork != 0) {
short positions = fork-1;
vector <short> positionArray;
if (positions == Globals::POSITIONS) {
positionArray = {Globals::AFTER, Globals::BEFORE};
}
else {
positionArray = {positions};
}
SequencePath path(i);
for (short position: positionArray) {
//cout << "Starting bubbles with " << i << " " << position << endl;
if (pinchBubbles(path, position, false)) {
pinchBubble(path);
bubble = true;
nbBubbles++;
}
}
}
}
} while (bubble);
}
//cout << "... done (" << nbBubbles << " bubbles found)" << endl;
//cout << _graph << endl;
}
bool GraphTrimmer::pinchBubbles(SequencePath &path, short position, bool change) {
if (_graph.getCumulatedSize(path) > Globals::BUBBLE_SIZE) {
return false;
}
unsigned int firstIndex = path.getNode(0);
unsigned int lastIndex = path.getLastNode();
SequenceNode &lastNode = _graph.getNode(lastIndex);
for (short p = 0; p < Globals::DIRECTIONS; p++) {
if ((p == position) || ((! change) && (path.getSize() > 2))) {
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < lastNode.getNbNeighbors(p, direction); i++) {
unsigned int nextIndex = lastNode.getNeighbor(p, direction, i);
//cout << "ni: " << nextIndex << ", n-2: " << path.getNode(path.getSize()-2) << endl;
const SequenceNode &nextNode = _graph.getNode(nextIndex);
if (nextNode.isSet()) {
//cout << "\tContinuing bubble with " << path << " and change " << change << ", added " << nextIndex << endl;
if ((nextIndex == firstIndex) && ((change) || (p != position))) {
//cout << "\t\tFound bubble in path " << path << endl;
return true;
}
else if (! path.contains(nextIndex)) {
path.addNode(nextIndex);
if (pinchBubbles(path, (direction == Globals::DIRECT)? p: 1-p, (change) || (p != position))) {
return true;
}
path.removeLastNode();
}
}
}
}
}
}
return false;
}
void GraphTrimmer::pinchBubble(SequencePath &path) {
int index = 0;
KmerNb count = -1;
for (unsigned int i = 0; i < path.getSize(); i++) {
if (_graph.getNode(path.getNode(i)).getCount() < count) {
index = i;
}
}
_graph.getNode(path.getNode(index)).unset();
//cout << "\t\t\tPinching node " << path.getNode(index) << endl;
}
bool GraphTrimmer::fuseTips() {
bool fused = false;
for (unsigned int i = 0; i < _graph.getSize(); i++) {
const SequenceNode &node = _graph.getNode(i);
if (node.isSet()) {
int fork =_graph.isVee(node);
if (fork > 0) {
short positions = fork - 1;
vector <short> positionArray;
if (positions == Globals::POSITIONS) {
positionArray = {Globals::AFTER, Globals::BEFORE};
}
else {
positionArray = {positions};
}
for (short position: positionArray) {
fused = fused || fuseTips(node, position);
}
}
}
}
//cout << "Tip fusing done" << endl;
//cout << _graph << endl;
return fused;
}
bool GraphTrimmer::fuseTips(const SequenceNode &node, short position) {
unsigned int longestSize = 0;
unsigned int longestNode = 0;
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < node.getNbNeighbors(position, direction); i++) {
int index = node.getNeighbor(position, direction, i);
const SequenceNode &nextNode = _graph.getNode(index);
if (nextNode.isSet()) {
if ((! _graph.isLeaf(nextNode)) || (node == nextNode)) {
return false;
}
if (nextNode.getSize() > longestSize) {
longestSize = nextNode.getSize();
longestNode = index;
}
}
}
}
//cout << "Fusing tip of " << node << endl;
bool fused = false;
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < node.getNbNeighbors(position, direction); i++) {
unsigned int index = node.getNeighbor(position, direction, i);
if (index != longestNode) {
SequenceNode &nextNode = _graph.getNode(index);
if (nextNode.isSet()) {
//cout << "\t\t\tFusing node " << nextNode << endl;
nextNode.unset();
fused = true;
}
}
}
}
return fused;
}
bool GraphTrimmer::removeTips() {
bool removed = false;
for (unsigned int i = 0; i < _graph.getSize(); i++) {
const SequenceNode &node = _graph.getNode(i);
if (node.isSet()) {
int fork =_graph.isFork(node);
if (fork > 0) {
short position = fork - 1;
removed = removed || removeTips(node, position);
}
}
}
//cout << "Tip removal done" << endl;
//cout << _graph << endl;
return removed;
}
bool GraphTrimmer::removeTips(const SequenceNode &node, short position) {
unsigned int longestSize = 0;
unsigned int longestNode = 0;
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < node.getNbNeighbors(position, direction); i++) {
int index = node.getNeighbor(position, direction, i);
const SequenceNode &nextNode = _graph.getNode(index);
if (nextNode.isSet()) {
if ((! _graph.isLeaf(nextNode)) || (node == nextNode)) {
return false;
}
if (nextNode.getSize() > longestSize) {
longestSize = nextNode.getSize();
longestNode = index;
}
}
}
}
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < node.getNbNeighbors(position, direction); i++) {
unsigned int index = node.getNeighbor(position, direction, i);
if ((index != longestNode) && (! _graph.isLeaf(_graph.getNode(index)))) {
return false;
}
}
}
//cout << "Removing tip of " << node << endl;
bool removed = false;
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < node.getNbNeighbors(position, direction); i++) {
unsigned int index = node.getNeighbor(position, direction, i);
if (index != longestNode) {
//cout << "\t\t\tRemoving node " << _graph.getNode(index) << endl;
_graph.getNode(index).unset();
removed = true;
}
}
}
return removed;
}