Debian Codegraph @

src/split.h

raw symbols SCIP
1 #ifndef SPLIT_H
2 #define SPLIT_H
3
4 #include <iostream>
5
6 //#include "bindings/cpp/WFAligner.hpp"
7
8 #include "edlib.h"
9 #include "gotoh.h"
10 #include "needle.h"
11
12 namespace torali
13 {
14
15 struct AlignDescriptor {
16 int32_t cStart;
17 int32_t cEnd;
18 int32_t rStart;
19 int32_t rEnd;
20 int32_t homLeft;
21 int32_t homRight;
22 float percId;
23
24 AlignDescriptor() : cStart(0), cEnd(0), rStart(0), rEnd(0), homLeft(0), homRight(0), percId(0) {}
25 };
26
27
28 /*
29 template<typename TWFAligner>
30 inline void
31 printWfaAlignment(std::string const& pattern, std::string const& text, TWFAligner& aligner) {
32 std::string cigar = aligner.getAlignmentCigar();
33
34 std::cerr << "Alignment score: " << aligner.getAlignmentScore() << std::endl;
35
36 // pattern
37 uint32_t pidx = 0;
38 for (uint32_t j = 0; j < cigar.size(); ++j) {
39 if (cigar[j] == 'I') std::cerr << '-';
40 else std::cerr << pattern[pidx++];
41 }
42 std::cerr << std::endl;
43
44 // text
45 uint32_t tidx = 0;
46 for (uint32_t j = 0; j < cigar.size(); ++j) {
47 if (cigar[j] == 'D') std::cerr << '-';
48 else std::cerr << text[tidx++];
49 }
50 std::cerr << std::endl;
51 }
52 */
53
54
55 template<typename TBPoint>
56 inline void
57 _adjustOrientation(std::string& sequence, TBPoint bpPoint, int32_t const svt) {
58 if (_translocation(svt)) {
59 uint8_t ct = _getSpanOrientation(svt);
60 if (((ct==0) && (bpPoint)) || ((ct==1) && (!bpPoint))) reverseComplement(sequence);
61 } else {
62 if (svt == 0) {
63 if (bpPoint) reverseComplement(sequence);
64 } else if (svt == 1) {
65 if (!bpPoint) reverseComplement(sequence);
66 }
67 }
68 }
69
70 template<typename TConfig, typename TSeq, typename TSVRecord, typename TRef>
71 inline std::string
72 _getSVRef(TConfig const& c, TSeq const* const ref, TSVRecord const& svRec, TRef const refIndex, int32_t const svt) {
73 if (_translocation(svt)) {
74 uint8_t ct = _getSpanOrientation(svt);
75 if (svRec.chr==refIndex) {
76 if ((ct==0) || (ct == 2)) return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd)) + svRec.part1;
77 else if (ct == 1) {
78 std::string strEnd=boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd));
79 std::string refPart=strEnd;
80 std::string::reverse_iterator itR = strEnd.rbegin();
81 std::string::reverse_iterator itREnd = strEnd.rend();
82 for(unsigned int i = 0; itR!=itREnd; ++itR, ++i) {
83 switch (*itR) {
84 case 'A': refPart[i]='T'; break;
85 case 'C': refPart[i]='G'; break;
86 case 'G': refPart[i]='C'; break;
87 case 'T': refPart[i]='A'; break;
88 case 'N': refPart[i]='N'; break;
89 default: break;
90 }
91 }
92 return refPart + svRec.part1;
93 } else return svRec.part1 + boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd));
94 } else {
95 // chr2
96 if (ct==0) {
97 std::string strEnd=boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd));
98 std::string refPart=strEnd;
99 std::string::reverse_iterator itR = strEnd.rbegin();
100 std::string::reverse_iterator itREnd = strEnd.rend();
101 for(unsigned int i = 0; itR!=itREnd; ++itR, ++i) {
102 switch (*itR) {
103 case 'A': refPart[i]='T'; break;
104 case 'C': refPart[i]='G'; break;
105 case 'G': refPart[i]='C'; break;
106 case 'T': refPart[i]='A'; break;
107 case 'N': refPart[i]='N'; break;
108 default: break;
109 }
110 }
111 return refPart;
112 } else return boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd));
113 }
114 } else {
115 if (svt == 2) {
116 if (svRec.svEnd - svRec.svStart <= c.indelsize) return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svEndEnd));
117 else return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd)) + boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd));
118 } else if (svt == 4) {
119 return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svEndEnd));
120 } else if (svt == 3) {
121 return boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd)) + boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd));
122 } else if (svt == 0) {
123 std::string strEnd;
124 if ((svRec.svEnd - svRec.svStart) > c.minConsWindow) strEnd = boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd));
125 else strEnd=boost::to_upper_copy(std::string(ref + svRec.svStart, ref + svRec.svEndEnd));
126 std::string strRevComp=strEnd;
127 std::string::reverse_iterator itR = strEnd.rbegin();
128 std::string::reverse_iterator itREnd = strEnd.rend();
129 for(unsigned int i = 0; itR!=itREnd; ++itR, ++i) {
130 switch (*itR) {
131 case 'A': strRevComp[i]='T'; break;
132 case 'C': strRevComp[i]='G'; break;
133 case 'G': strRevComp[i]='C'; break;
134 case 'T': strRevComp[i]='A'; break;
135 case 'N': strRevComp[i]='N'; break;
136 default: break;
137 }
138 }
139 if ((svRec.svEnd - svRec.svStart) > c.minConsWindow) return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd)) + strRevComp;
140 else return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd)) + strRevComp + boost::to_upper_copy(std::string(ref + svRec.svEnd, ref + svRec.svEndEnd));
141 } else if (svt == 1) {
142 std::string strStart;
143 if ((svRec.svEnd - svRec.svStart) > c.minConsWindow) strStart = boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStartEnd));
144 else strStart = boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svEnd));
145 std::string strRevComp=strStart;
146 std::string::reverse_iterator itR = strStart.rbegin();
147 std::string::reverse_iterator itREnd = strStart.rend();
148 for(unsigned int i = 0; itR!=itREnd; ++itR, ++i) {
149 switch (*itR) {
150 case 'A': strRevComp[i]='T'; break;
151 case 'C': strRevComp[i]='G'; break;
152 case 'G': strRevComp[i]='C'; break;
153 case 'T': strRevComp[i]='A'; break;
154 case 'N': strRevComp[i]='N'; break;
155 default: break;
156 }
157 }
158 if ((svRec.svEnd - svRec.svStart) > c.minConsWindow) return strRevComp + boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd));
159 else return boost::to_upper_copy(std::string(ref + svRec.svStartBeg, ref + svRec.svStart)) + strRevComp + boost::to_upper_copy(std::string(ref + svRec.svEndBeg, ref + svRec.svEndEnd));
160 }
161 }
162 return "";
163 }
164
165
166 template<typename TConfig, typename TString, typename TSvRecord, typename TAlignDescriptor, typename TPosition>
167 inline bool
168 _coordTransform(TConfig const& c, TString const& ref, TSvRecord const& sv, TAlignDescriptor const& ad, TPosition& finalGapStart, TPosition& finalGapEnd, int32_t svt) {
169 if (_translocation(svt)) {
170 uint8_t ct = _getSpanOrientation(svt);
171 if (ct == 0) {
172 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
173 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
174 finalGapStart = sv.svStartBeg + ad.rStart;
175 finalGapEnd = sv.svEndBeg + (ref.size() - ad.rEnd) + 1;
176 } else if (ct == 1) {
177 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
178 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
179 finalGapStart = sv.svStartBeg + (annealed - ad.rStart) + 1;
180 finalGapEnd = sv.svEndBeg + (ad.rEnd - annealed);
181 } else if (ct == 2) {
182 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
183 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
184 finalGapStart = sv.svStartBeg + ad.rStart;
185 finalGapEnd = sv.svEndBeg + (ad.rEnd - annealed);
186 } else if (ct == 3) {
187 int32_t annealed = sv.svEndEnd - sv.svEndBeg;
188 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
189 finalGapStart = sv.svStartBeg + (ad.rEnd - annealed);
190 finalGapEnd = sv.svEndBeg + ad.rStart;
191 } else return false;
192 return true;
193 } else {
194 if (svt == 2) {
195 if (sv.svEnd - sv.svStart > c.indelsize) {
196 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
197 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
198 finalGapStart = sv.svStartBeg + ad.rStart;
199 finalGapEnd = sv.svEndBeg + (ad.rEnd - annealed);
200 } else {
201 finalGapStart = sv.svStartBeg + ad.rStart;
202 finalGapEnd = sv.svStartBeg + ad.rEnd;
203 }
204 return true;
205 } else if (svt == 3) {
206 int32_t annealed = sv.svEndEnd - sv.svEndBeg;
207 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
208 finalGapStart = sv.svStartBeg + (ad.rEnd - annealed);
209 finalGapEnd = sv.svEndBeg + ad.rStart;
210 return true;
211 } else if (svt == 0) {
212 if ((sv.svEnd - sv.svStart) > c.minConsWindow) {
213 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
214 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
215 finalGapStart = sv.svStartBeg + ad.rStart;
216 finalGapEnd = sv.svEndBeg + (ref.size() - ad.rEnd) + 1;
217 } else {
218 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
219 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
220 finalGapStart = sv.svStartBeg + ad.rStart;
221 finalGapEnd = sv.svEndEnd - (ad.rEnd - annealed);
222 }
223 return true;
224 } else if (svt == 1) {
225 if ((sv.svEnd - sv.svStart) > c.minConsWindow) {
226 int32_t annealed = sv.svStartEnd - sv.svStartBeg;
227 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
228 finalGapStart = sv.svStartBeg + (annealed - ad.rStart) + 1;
229 finalGapEnd = sv.svEndBeg + (ad.rEnd - annealed);
230 } else {
231 int32_t annealed = (sv.svStart - sv.svStartBeg) + (sv.svEnd - sv.svStartBeg);
232 if ((ad.rStart >= annealed) || (ad.rEnd < annealed)) return false;
233 finalGapStart = sv.svStartBeg + (annealed - ad.rStart) + 1;
234 finalGapEnd = sv.svEndBeg + (ad.rEnd - annealed);
235 }
236 return true;
237 } else if (svt == 4) {
238 finalGapStart = sv.svStartBeg + ad.rStart;
239 finalGapEnd = sv.svStartBeg + ad.rEnd;
240 return true;
241 }
242 }
243 return true;
244 }
245
246
247 template<typename TPos>
248 inline bool
249 _validSRAlignment(TPos const cStart, TPos const cEnd, TPos const rStart, TPos const rEnd, int32_t const svt)
250 {
251 if (svt == 4) return (((rEnd - rStart) < 5) && ((cEnd - cStart) > 15));
252 else return (((cEnd - cStart) < 5) && ((rEnd - rStart) > 15));
253 }
254
255 template<typename TGap>
256 inline bool
257 _checkSVGap(TGap const refGap, TGap const oldRefGap, TGap const varGap, TGap const oldVarGap, int32_t const svt) {
258 if (svt == 4) return (varGap > oldVarGap);
259 else return (refGap > oldRefGap);
260 }
261
262 template<typename TAlignDescriptor>
263 inline void
264 _findHomology(std::string const& consensus, std::string const& svRefStr, TAlignDescriptor& ad, int32_t const svt) {
265 if (svt == 4) {
266 ad.homRight = longestHomology(consensus.substr(ad.cStart), svRefStr.substr(ad.rEnd -1), -1);
267 std::string preC = consensus.substr(0, ad.cEnd - 1);
268 std::string preR = svRefStr.substr(0, ad.rStart);
269 std::reverse(preC.begin(), preC.end());
270 std::reverse(preR.begin(), preR.end());
271 ad.homLeft = longestHomology(preC, preR, -1);
272 } else {
273 ad.homRight = longestHomology(consensus.substr(ad.cEnd - 1), svRefStr.substr(ad.rStart), -1);
274 std::string preC = consensus.substr(0, ad.cStart);
275 std::string preR = svRefStr.substr(0, ad.rEnd - 1);
276 std::reverse(preC.begin(), preC.end());
277 std::reverse(preR.begin(), preR.end());
278 ad.homLeft = longestHomology(preC, preR, -1);
279 }
280 }
281
282 template<typename TAlign, typename TAIndex, typename TFloat>
283 inline void
284 _percentIdentity(TAlign const& align, TAIndex const gS, TAIndex const gE, TFloat& percId) {
285 // Find percent identity
286 bool varSeen = false;
287 bool refSeen = false;
288 uint32_t gapMM = 0;
289 uint32_t mm = 0;
290 uint32_t ma = 0;
291 bool inGap=false;
292 for(TAIndex j = 0; j < (TAIndex) align.shape()[1]; ++j) {
293 if ((j < gS) || (j > gE)) {
294 if (align[0][j] != '-') varSeen = true;
295 if (align[1][j] != '-') refSeen = true;
296 // Internal gap?
297 if ((align[0][j] == '-') || (align[1][j] == '-')) {
298 if ((refSeen) && (varSeen)) {
299 if (!inGap) {
300 inGap = true;
301 gapMM = 0;
302 }
303 gapMM += 1;
304 }
305 } else {
306 if (inGap) {
307 mm += gapMM;
308 inGap=false;
309 }
310 if (align[0][j] == align[1][j]) ma += 1;
311 else mm += 1;
312 }
313 }
314 }
315 percId = (TFloat) ma / (TFloat) (ma + mm);
316 }
317
318
319 template<typename TConfig, typename TAlign, typename TAlignDescriptor>
320 inline bool
321 _findSplit(TConfig const& c, std::string const& consensus, std::string const& svRefStr, TAlign const& align, TAlignDescriptor& ad, int32_t const svt) {
322 // Initializiation
323 int32_t gS=0;
324 int32_t gE=0;
325
326 // Find longest internal gap
327 int32_t refIndex=0;
328 int32_t varIndex=0;
329 int32_t gapStartRefIndex=0;
330 int32_t gapStartVarIndex=0;
331 int32_t a1 = 0;
332 bool inGap=false;
333 for(int32_t j = 0; j < (int32_t) align.shape()[1]; ++j) {
334 if (align[0][j] != '-') ++varIndex;
335 if (align[1][j] != '-') ++refIndex;
336 // Internal gap?
337 if (((align[0][j] == '-') || (align[1][j] == '-')) && (refIndex>0) && (varIndex>0)) {
338 if (!inGap) {
339 gapStartVarIndex = (align[0][j] != '-') ? (varIndex - 1) : varIndex;
340 gapStartRefIndex = (align[1][j] != '-') ? (refIndex - 1) : refIndex;
341 a1 = j;
342 inGap = true;
343 }
344 } else {
345 if ((inGap) && (_checkSVGap((refIndex - gapStartRefIndex), (ad.rEnd - ad.rStart), (varIndex - gapStartVarIndex), (ad.cEnd - ad.cStart), svt))) {
346 ad.rStart=gapStartRefIndex;
347 ad.rEnd=refIndex;
348 ad.cStart=gapStartVarIndex;
349 ad.cEnd=varIndex;
350 gS = a1;
351 gE = j - 1;
352 }
353 inGap=false;
354 }
355 }
356 if (ad.rEnd <= ad.rStart) return false;
357
358 // Is this a valid split-read alignment?
359 if (!_validSRAlignment(ad.cStart, ad.cEnd, ad.rStart, ad.rEnd, svt)) return false;
360
361 // Check percent identity
362 _percentIdentity(align, gS, gE, ad.percId);
363 //std::cerr << ad.percId << ',' << c.flankQuality << std::endl;
364 if (ad.percId < c.flankQuality) return false;
365
366 // Find homology
367 _findHomology(consensus, svRefStr, ad, svt);
368
369 // Check flanking alignment length
370 if ((ad.homLeft + c.minimumFlankSize > ad.cStart) || ( varIndex < ad.cEnd + ad.homRight + c.minimumFlankSize)) return false;
371 if ((ad.homLeft + c.minimumFlankSize > ad.rStart) || ( refIndex < ad.rEnd + ad.homRight + c.minimumFlankSize)) return false;
372
373 // Valid split-read alignment
374 return true;
375 }
376
377
378 inline void
379 editDistanceVec(std::string const& seqI, std::string const& seqJ, EdlibAlignResult const& cigar, std::vector<uint32_t>& dist) {
380 // Fill edit distance vector
381 dist.resize(seqI.size());
382 std::fill(dist.begin(), dist.end(), 0);
383
384 // Compute edit distance
385 int32_t tIdx = -1;
386 int32_t qIdx = -1;
387 uint32_t editDist = 0;
388 // seqI
389 for (int32_t j = 0; j < cigar.alignmentLength; ++j) {
390 if (cigar.alignment[j] == EDLIB_EDOP_DELETE) {
391 ++tIdx;
392 ++editDist;
393 }
394 else if (cigar.alignment[j] == EDLIB_EDOP_INSERT) {
395 ++qIdx;
396 ++editDist;
397 dist[qIdx] = editDist;
398 }
399 else {
400 ++tIdx;
401 ++qIdx;
402 if (seqI[qIdx] != seqJ[tIdx]) ++editDist;
403 dist[qIdx] = editDist;
404 }
405 }
406 }
407
408 template<typename TAlign>
409 inline void
410 glueAlignment(std::string const& query, std::string const& target, uint32_t const gaplen, EdlibAlignMode const modeCode, EdlibAlignResult& cigarLeft, EdlibAlignResult& cigarRight, TAlign& align) {
411 // Create new alignment
412 int32_t tIdx = -1;
413 int32_t qIdx = -1;
414 uint32_t missingStart = 0;
415 if (modeCode == EDLIB_MODE_HW) {
416 tIdx = cigarLeft.endLocations[0];
417 for (int32_t i = 0; i < cigarLeft.alignmentLength; i++) {
418 if (cigarLeft.alignment[i] != EDLIB_EDOP_INSERT) --tIdx;
419 }
420 if (tIdx >= 0) missingStart = tIdx + 1;
421 }
422 uint32_t missingEnd = cigarRight.endLocations[0];
423 if (missingEnd < target.size()) missingEnd = target.size() - missingEnd - 1;
424 align.resize(boost::extents[2][missingStart + cigarLeft.alignmentLength + gaplen + cigarRight.alignmentLength + missingEnd]);
425
426 // fix start
427 if (modeCode == EDLIB_MODE_HW) {
428 if (missingStart) {
429 for (uint32_t j = 0; j < missingStart; ++j) {
430 align[1][j] = target[j];
431 align[0][j] = '-';
432 }
433 }
434 }
435 // target
436 for (int32_t j = 0; j < cigarLeft.alignmentLength; ++j) {
437 if (cigarLeft.alignment[j] == EDLIB_EDOP_INSERT) align[1][j + missingStart] = '-';
438 else {
439 ++tIdx;
440 align[1][j + missingStart] = target[tIdx];
441 }
442 }
443 // query
444 for (int32_t j = 0; j < cigarLeft.alignmentLength; ++j) {
445 if (cigarLeft.alignment[j] == EDLIB_EDOP_DELETE) align[0][j + missingStart] = '-';
446 else align[0][j + missingStart] = query[++qIdx];
447 }
448 // gap
449 for (uint32_t j = 0; j < gaplen; ++j) {
450 align[0][j + missingStart + cigarLeft.alignmentLength] = '-';
451 ++tIdx;
452 align[1][j + missingStart + cigarLeft.alignmentLength] = target[tIdx];
453 }
454 // target
455 for (int32_t j = 0; j < cigarRight.alignmentLength; ++j) {
456 if (cigarRight.alignment[j] == EDLIB_EDOP_INSERT) align[1][j + missingStart + cigarLeft.alignmentLength + gaplen] = '-';
457 else {
458 ++tIdx;
459 align[1][j + missingStart + cigarLeft.alignmentLength + gaplen] = target[tIdx];
460 }
461 }
462 // query
463 for (int32_t j = 0; j < cigarRight.alignmentLength; ++j) {
464 if (cigarRight.alignment[j] == EDLIB_EDOP_DELETE) align[0][j + missingStart + cigarLeft.alignmentLength + gaplen] = '-';
465 else align[0][j + missingStart + cigarLeft.alignmentLength + gaplen] = query[++qIdx];
466 }
467 // fix end
468 if (modeCode == EDLIB_MODE_HW) {
469 if (missingEnd) {
470 for (uint32_t j = 0; j < missingEnd; ++j) {
471 ++tIdx;
472 align[1][j + missingStart + cigarLeft.alignmentLength + gaplen + cigarRight.alignmentLength] = target[tIdx];
473 align[0][j + missingStart + cigarLeft.alignmentLength + gaplen + cigarRight.alignmentLength] = '-';
474 }
475 }
476 }
477 }
478
479
480 template<typename TAlign>
481 inline bool
482 splitAlign(std::string const& cons, std::string const& svRefStr, TAlign& align) {
483 // Prepare SHW alignment
484 std::string prefix = svRefStr.substr(0, svRefStr.size()/3);
485 EdlibAlignResult cigarPrefix = edlibAlign(prefix.c_str(), prefix.size(), cons.c_str(), cons.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0));
486 uint32_t csStart = infixStart(cigarPrefix);
487 edlibFreeAlignResult(cigarPrefix);
488 std::string suffix = svRefStr.substr(2 * svRefStr.size()/3);
489 EdlibAlignResult cigarSuffix = edlibAlign(suffix.c_str(), suffix.size(), cons.c_str(), cons.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0));
490 uint32_t csEnd = infixEnd(cigarSuffix);
491 edlibFreeAlignResult(cigarSuffix);
492
493 if (csStart >= csEnd) return false;
494 std::string cs = cons.substr(csStart, (csEnd - csStart));
495
496 // Prefix alignment
497 EdlibAlignResult cigar = edlibAlign(svRefStr.c_str(), svRefStr.size(), cs.c_str(), cs.size(), edlibNewAlignConfig(-1, EDLIB_MODE_SHW, EDLIB_TASK_PATH, NULL, 0));
498 std::vector<uint32_t> distFwd;
499 //printAlignment(svRefStr, cs, EDLIB_MODE_HW, cigar);
500 editDistanceVec(svRefStr, cs, cigar, distFwd);
501 edlibFreeAlignResult(cigar);
502 // Suffix alignment
503 std::string svRefStrRev = svRefStr;
504 std::string csRev = cs;
505 reverseComplement(svRefStrRev);
506 reverseComplement(csRev);
507 EdlibAlignResult cigarRev = edlibAlign(svRefStrRev.c_str(), svRefStrRev.size(), csRev.c_str(), csRev.size(), edlibNewAlignConfig(-1, EDLIB_MODE_SHW, EDLIB_TASK_PATH, NULL, 0));
508 std::vector<uint32_t> distRev;
509 //printAlignment(svRefStrRev, csRev, EDLIB_MODE_HW, cigarRev);
510 editDistanceVec(svRefStrRev, csRev, cigarRev, distRev);
511 edlibFreeAlignResult(cigarRev);
512
513 // Best join
514 uint32_t bestJoin = 0;
515 for(uint32_t i = 1; i < distFwd.size() - 1; ++i) {
516 if (distFwd[i] + distRev[distFwd.size() - i - 2] < distFwd[bestJoin] + distRev[distFwd.size() - bestJoin - 2]) bestJoin = i;
517 }
518
519 // Split reference and do infix alignment
520 std::string svRefLeft = svRefStr.substr(0, bestJoin + 1);
521 std::string svRefRight = svRefStr.substr(bestJoin + 1);
522
523 // Infix alignment
524 EdlibAlignResult cigarLeft = edlibAlign(svRefLeft.c_str(), svRefLeft.size(), cons.c_str(), cons.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0));
525 uint32_t leftEnd = infixEnd(cigarLeft);
526 //printAlignment(svRefLeft, cons, EDLIB_MODE_HW, cigarLeft);
527 EdlibAlignResult cigarRight = edlibAlign(svRefRight.c_str(), svRefRight.size(), cons.c_str(), cons.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0));
528 //printAlignment(svRefRight, cons, EDLIB_MODE_HW, cigarRight);
529 uint32_t rightStart = infixStart(cigarRight);
530
531 // Glue alignment
532 if (leftEnd + 15 >= rightStart) return false;
533 glueAlignment(svRefStr, cons, rightStart - leftEnd - 1, EDLIB_MODE_HW, cigarLeft, cigarRight, align);
534 edlibFreeAlignResult(cigarLeft);
535 edlibFreeAlignResult(cigarRight);
536 return true;
537 }
538
539
540 template<typename TAlign>
541 inline bool
542 _consRefAlignment(std::string const& cons, std::string const& svRefStr, TAlign& aln, int32_t const svt) {
543 AlignConfig<true, false> semiglobal;
544 DnaScore<int> lnsc(1, -1, -1, -1);
545 bool reNeedle = false;
546 if (svt == 4) {
547 reNeedle = splitAlign(cons, svRefStr, aln);
548 //reNeedle = longNeedle(svRefStr, cons, aln, semiglobal, lnsc);
549 for(uint32_t j = 0; j < aln.shape()[1]; ++j) {
550 char tmp = aln[0][j];
551 aln[0][j] = aln[1][j];
552 aln[1][j] = tmp;
553 }
554 } else {
555 reNeedle = longNeedle(cons, svRefStr, aln, semiglobal, lnsc);
556 }
557 return reNeedle;
558 }
559
560 template<typename TConfig>
561 inline bool
562 _alignConsensus(TConfig const& c, std::string& consensus, std::string& svRefStr, StructuralVariantRecord& sv, Breakpoint& bp, bool const realign) {
563 // Realign?
564 if (realign) {
565 std::string revc = consensus;
566 reverseComplement(revc);
567 EdlibAlignResult alignFwd = edlibAlign(svRefStr.c_str(), svRefStr.size(), consensus.c_str(), consensus.size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0));
568 EdlibAlignResult alignRev = edlibAlign(svRefStr.c_str(), svRefStr.size(), revc.c_str(), revc.size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0));
569 if (alignRev.editDistance < alignFwd.editDistance) consensus = revc;
570 edlibFreeAlignResult(alignFwd);
571 edlibFreeAlignResult(alignRev);
572 }
573
574 //wfa::WFAlignerGapAffine2Pieces aligner(8, 12, 4, 60, 1, wfa::WFAligner::Alignment, wfa::WFAligner::MemoryHigh);
575 //aligner.alignEnd2End(consensus, svRefStr);
576 //printWfaAlignment(consensus, svRefStr, aligner);
577
578 // Consensus to reference alignment
579 typedef boost::multi_array<char, 2> TAlign;
580 TAlign align;
581 //std::cerr << "Consensus-to-Reference alignment" << std::endl;
582 if (!_consRefAlignment(consensus, svRefStr, align, sv.svt)) return false;
583
584 // Debug consensus to reference alignment
585 //for(uint32_t i = 0; i < align.shape()[0]; ++i) {
586 //for(uint32_t j = 0; j< align.shape()[1]; ++j) {
587 //std::cerr << align[i][j];
588 //}
589 //std::cerr << std::endl;
590 //}
591 //std::cerr << std::endl;
592 //std::cerr << std::endl;
593
594 // Check breakpoint
595 AlignDescriptor ad;
596 if (!_findSplit(c, consensus, svRefStr, align, ad, sv.svt)) return false;
597
598 // Transform coordinates
599 unsigned int finalGapStart = 0;
600 unsigned int finalGapEnd = 0;
601 if (!_coordTransform(c, svRefStr, bp, ad, finalGapStart, finalGapEnd, sv.svt)) return false;
602
603 if ((_translocation(sv.svt)) || (finalGapStart < finalGapEnd)) {
604
605 // Get exact alleles for INS and DEL
606 if (sv.svEnd - sv.svStart <= c.indelsize) {
607 if ((sv.svt == 2) || (sv.svt == 4)) {
608 std::string refVCF;
609 std::string altVCF;
610 int32_t cpos = 0;
611 bool inSV = false;
612 for(uint32_t j = 0; j<align.shape()[1]; ++j) {
613 if (align[0][j] != '-') {
614 ++cpos;
615 if (cpos == ad.cStart) inSV = true;
616 else if (cpos == ad.cEnd) inSV = false;
617 }
618 if (inSV) {
619 if (align[0][j] != '-') altVCF += align[0][j];
620 if (align[1][j] != '-') refVCF += align[1][j];
621 }
622 }
623 sv.alleles = _addAlleles(refVCF, altVCF);
624 }
625 }
626 sv.precise=true;
627 sv.svStart=finalGapStart;
628 sv.svEnd=finalGapEnd;
629 sv.srAlignQuality = ad.percId;
630 sv.insLen=ad.cEnd - ad.cStart - 1;
631 sv.consBp = ad.cStart;
632 sv.homLen=std::max(0, ad.homLeft + ad.homRight - 2);
633 int32_t ci_wiggle = std::max(ad.homLeft, ad.homRight);
634 sv.ciposlow = -ci_wiggle;
635 sv.ciposhigh = ci_wiggle;
636 sv.ciendlow = -ci_wiggle;
637 sv.ciendhigh = ci_wiggle;
638 return true;
639 } else {
640 return false;
641 }
642 }
643
644 template<typename TConfig>
645 inline bool
646 alignConsensus(TConfig const& c, bam_hdr_t const* hdr, char const* seq, char const* sndSeq, StructuralVariantRecord& sv, bool const realign) {
647 if ( (int32_t) sv.consensus.size() < (2 * c.minimumFlankSize + sv.insLen)) return false;
648
649 // Get reference slice
650 Breakpoint bp(sv);
651 if (sv.svt ==4) {
652 int32_t bufferSpace = std::max((int32_t) ((sv.consensus.size() - sv.insLen) / 3), c.minimumFlankSize);
653 _initBreakpoint(hdr, bp, bufferSpace, sv.svt);
654 } else _initBreakpoint(hdr, bp, sv.consensus.size(), sv.svt);
655 if (bp.chr != bp.chr2) bp.part1 = _getSVRef(c, sndSeq, bp, bp.chr2, sv.svt);
656 std::string svRefStr = _getSVRef(c, seq, bp, bp.chr, sv.svt);
657
658 // Debug
659 //std::cerr << ">Cons:" << sv.svStart << std::endl;
660 //std::cerr << sv.consensus << std::endl;
661 //std::cerr << ">Ref:" << sv.svStart << std::endl;
662 //std::cerr << svRefStr << std::endl;
663
664 // Generate consensus alignment
665 return _alignConsensus(c, sv.consensus, svRefStr, sv, bp, realign);
666 }
667
668 template<typename TConfig>
669 inline bool
670 alignConsensus(TConfig const& c, bam_hdr_t* hdr, char const* seq, char const* sndSeq, StructuralVariantRecord& sv) {
671 return alignConsensus(c, hdr, seq, sndSeq, sv, false);
672 }
673
674
675 }
676
677 #endif