-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathScaffoldRecord.cpp
351 lines (301 loc) · 11.3 KB
/
ScaffoldRecord.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
//-----------------------------------------------
// Copyright 2010 Wellcome Trust Sanger Institute
// Written by Jared Simpson ([email protected])
// Released under the GPL
//-----------------------------------------------
//
// ScaffoldRecord - A scaffold consisting of a
// starting component and a vector of ordered
// links
//
#include "ScaffoldRecord.h"
#include "OverlapTools.h"
#include "SGSearch.h"
//#define DEBUGRESOLVE 1
//
ScaffoldRecord::ScaffoldRecord()
{
}
//
void ScaffoldRecord::setRoot(const std::string& root)
{
m_rootID = root;
}
//
void ScaffoldRecord::addLink(const ScaffoldLink& link)
{
m_links.push_back(link);
}
//
size_t ScaffoldRecord::getNumComponents() const
{
if(m_rootID.empty())
return 0;
return 1 + m_links.size();
}
// Construct a string from the scaffold
std::string ScaffoldRecord::generateString(const ResolveParams& params, StringVector& ids) const
{
assert(params.pSequenceCollection != NULL);
params.pStats->numScaffolds += 1;
// Starting from the root, join the sequence(s) of the scaffold
// together along with the appropriate gaps/overlap
std::string sequence = params.pSequenceCollection->getSequence(m_rootID);
params.pSequenceCollection->setPlaced(m_rootID);
ids.push_back(m_rootID + "+");
if(m_links.empty())
return sequence;
EdgeDir rootDir = m_links[0].getDir();
EdgeComp relativeComp = EC_SAME;
EdgeComp prevComp = EC_SAME;
std::string currID = m_rootID;
// If this scaffold grows in the antisense direction,
// we reverse every component and perform appends of the reversed
// parts. After the scaffold is constructed we reverse again
// to obtain the final scaffold in the desired orientation
bool reverseAll = (rootDir == ED_ANTISENSE);
if(reverseAll)
sequence = reverse(sequence);
// Iterate over all the linked contigs and append their sequence
// to the scaffold
for(size_t i = 0; i < m_links.size(); ++i)
{
params.pStats->numGapsAttempted += 1;
// Mark the current link as placed in the scaffold
const ScaffoldLink& link = m_links[i];
params.pSequenceCollection->setPlaced(link.endpointID);
// Calculate the strand this sequence is on relative to the root
if(link.getComp() == EC_REVERSE)
relativeComp = !relativeComp;
// Attempt to resolve the sequence of the link
std::string resolvedSequence;
// Step 1, try to walk through the graph between the vertices
bool resolved = false;
if(params.resolveMask & RESOLVE_GRAPH_BEST || params.resolveMask & RESOLVE_GRAPH_UNIQUE)
{
resolved = graphResolve(params, currID, link, resolvedSequence);
if(resolved)
{
// The returned sequence is wrt currID. If we flipped the sequence of currID, we must
// flip this sequence
if(prevComp == EC_REVERSE)
resolvedSequence = reverseComplementIUPAC(resolvedSequence);
if(reverseAll)
resolvedSequence = reverse(resolvedSequence);
params.pStats->numGapsResolved += 1;
}
}
// Step 2, try to resolve a predicted overlap between current sequence
// and the sequence of the linked contig
if(!resolved)
{
// Get the sequence that should be potentially appended in
std::string toAppend = params.pSequenceCollection->getSequence(link.endpointID);
if(relativeComp == EC_REVERSE)
toAppend = reverseComplementIUPAC(toAppend);
if(reverseAll)
toAppend = reverse(toAppend);
//
if(link.distance < 0 && params.resolveMask & RESOLVE_OVERLAP)
{
resolved = overlapResolve(params, sequence, toAppend, link, resolvedSequence);
if(resolved)
{
params.pStats->numGapsResolved += 1;
params.pStats->overlapFound += 1;
}
else
{
params.pStats->overlapFailed += 1;
}
}
// Step 3, just introduce a gap between the sequences
if(!resolved)
introduceGap(params.minGapLength, toAppend, link, resolvedSequence);
}
sequence.append(resolvedSequence);
currID = link.endpointID;
std::string outID = currID;
outID.append(relativeComp == EC_SAME ? "+" : "-");
ids.push_back(outID);
prevComp = relativeComp;
}
if(reverseAll)
{
sequence = reverse(sequence);
std::reverse(ids.begin(), ids.end());
}
return sequence;
}
// Attempt to resolve a scaffold link by finding a walk through the graph linking the two vertices
bool ScaffoldRecord::graphResolve(const ResolveParams& params, const std::string& startID,
const ScaffoldLink& link, std::string& outExtensionString) const
{
assert(params.pGraph != NULL);
// Get the vertex to start the search from
Vertex* pStartVertex = params.pGraph->getVertex(startID);
Vertex* pEndVertex = params.pGraph->getVertex(link.endpointID);
assert(pStartVertex != NULL && pEndVertex != NULL);
int threshold = static_cast<int>(params.distanceFactor * link.stdDev);
int maxDistance = link.distance + threshold;
int maxExtensionDistance = maxDistance + pEndVertex->getSeqLen();
SGWalkVector walks;
SGSearch::findWalks(pStartVertex, pEndVertex, link.getDir(), maxExtensionDistance, 10000, true, walks);
int numWalksValid = 0;
int numWalksClosest = 0;
int selectedIdx = -1;
int closestDist = std::numeric_limits<int>::max();
#ifdef DEBUGRESOLVE
std::cout << "Attempting graph resolve of link " << startID << " -- " << link.endpointID << " expected distance: " << link.distance << " orientation: " << link.edgeData.getComp() << "\n";
#endif
// Select the closest walk to the distance estimate
for(size_t i = 0; i < walks.size(); ++i)
{
// Check that the orientation of the walk is the same as the expected link
std::vector<EdgeComp> vertexOrientations = walks[i].getOrientationsToStart();
assert(walks[i].getLastEdge()->getEndID() == link.endpointID);
if(vertexOrientations.back() != link.edgeData.getComp())
{
#ifdef DEBUGRESOLVE
std::cout << "SKIPPING WALK OF THE WRONG ORIENTATION\n";
#endif
continue;
}
int walkDistance = walks[i].getEndToStartDistance();
int diff = abs(abs(link.distance - walkDistance));
if(diff <= threshold)
{
#ifdef DEBUGRESOLVE
std::cout << " Walk distance: " << walkDistance << " diff: " << diff << " threshold: " << threshold << " close: " << closestDist << "\n";
#endif
++numWalksValid;
if(diff < closestDist)
{
selectedIdx = i;
closestDist = diff;
numWalksClosest = 1;
}
else if(diff == closestDist)
{
numWalksClosest += 1;
}
}
}
// Choose the best path, if any, depending on the algorithm to use
bool useWalk = false;
if(numWalksValid > 0)
{
if(params.resolveMask & RESOLVE_GRAPH_BEST)
{
// If the unique flag is not set, or we only have 1 closest walk, select it
if(!(params.resolveMask & RESOLVE_GRAPH_UNIQUE) || numWalksClosest == 1)
useWalk = true;
else if((params.resolveMask & RESOLVE_GRAPH_UNIQUE) && numWalksClosest > 1)
params.pStats->graphWalkTooMany += 1;
}
else
{
if(numWalksValid == 1)
useWalk = true;
else if(numWalksValid > 1)
params.pStats->graphWalkTooMany += 1;
}
}
#ifdef DEBUGRESOLVE
std::cout << " Num walks: " << walks.size() << " Num valid: " << numWalksValid << " Num closest: " << numWalksClosest << " using: " << useWalk << "\n";
#endif
// Was an acceptable walk found?
if(useWalk)
{
assert(selectedIdx != -1);
outExtensionString = walks[selectedIdx].getString(SGWT_EXTENSION);
params.pStats->graphWalkFound += 1;
// Mark all vertices in the walk as visited
VertexPtrVec vertexPtrVector = walks[selectedIdx].getVertices();
for(size_t i = 0; i < vertexPtrVector.size(); ++i)
params.pSequenceCollection->setPlaced(vertexPtrVector[i]->getID());
return true;
}
else
{
if(numWalksValid == 0)
params.pStats->graphWalkNoPath += 1;
assert(outExtensionString.empty());
return false;
}
}
// Attempt to resolve a predicted overlap between s1 and s2
// Returns true if there overlap was found and the overhang of s2 is placed in outString
bool ScaffoldRecord::overlapResolve(const ResolveParams& params, const std::string& s1, const std::string& s2,
const ScaffoldLink& link, std::string& outString) const
{
// Attempt to find an overlap between these sequences
int expectedOverlap = -1 * link.distance;
#ifdef DEBUGRESOLVE
std::cout << "Attempting overlap resolve of link to " << link.endpointID << " expected distance: " << link.distance << " orientation: " << link.edgeData.getComp() << "\n";
#endif
// If the maximum overlap was not set, set it to the expected overlap * 3 stddev
int upperBound = 0;
if(params.maxOverlap == -1)
upperBound = static_cast<int>(expectedOverlap + 3.0f * link.stdDev);
else
upperBound = params.maxOverlap;
// Calculate the best match
Match match;
bool overlapFound = OverlapTools::boundedOverlapDP(s1, s2, params.minOverlap, upperBound, params.maxErrorRate, match);
if(overlapFound)
{
#ifdef DEBUGRESOLVE
std::cout << "Overlap found, length: " << match.coord[1].length() << "\n";
#endif
SeqCoord overlapCoord = match.coord[1];
SeqCoord overhangCoord = overlapCoord.complement();
outString = overhangCoord.getSubstring(s2);
return true;
}
else
{
return false;
}
}
// Resolve a link with a gap
bool ScaffoldRecord::introduceGap(int minGapLength, const std::string& contigString, const ScaffoldLink& link, std::string& out) const
{
assert(out.empty());
if(link.distance < 0)
{
// Truncate the string using the expected overlap and add a gap with a fixed number of Ns
out.append(minGapLength, 'N');
int expectedOverlap = -1 * link.distance;
assert(expectedOverlap < (int)contigString.length());
out.append(contigString.substr(expectedOverlap));
}
else
{
int gap = std::max(link.distance, minGapLength);
out.append(gap, 'N');
out.append(contigString);
}
return true;
}
//
void ScaffoldRecord::parse(const std::string& text)
{
StringVector fields = split(text, '\t');
assert(fields.size() >= 1);
m_rootID = fields[0];
for(size_t i = 1; i < fields.size(); ++i)
{
ScaffoldLink link;
link.parse(fields[i]);
m_links.push_back(link);
}
}
//
void ScaffoldRecord::writeScaf(std::ostream* pWriter)
{
*pWriter << m_rootID;
for(size_t i = 0; i < m_links.size(); ++i)
*pWriter << "\t" << m_links[i];
*pWriter << "\n";
}