Debian Codegraph @

src/tags.h

raw symbols SCIP
1 #ifndef TAGS_H
2 #define TAGS_H
3
4 namespace torali {
5
6 #ifndef DELLY_SVT_TRANS
7 #define DELLY_SVT_TRANS 5
8 #endif
9
10 #ifndef DELLY_OUTOFBAND
11 #define DELLY_OUTOFBAND -99999999
12 #endif
13
14 #ifndef DELLY_DUPLICATE
15 #define DELLY_DUPLICATE std::numeric_limits<uint32_t>::max()
16 #endif
17
18 #ifndef DELLY_KMER
19 #define DELLY_KMER 7
20 #endif
21
22 inline bool
23 _translocation(int32_t const svt) {
24 return (DELLY_SVT_TRANS <= svt) && (svt < 9);
25 }
26
27 inline bool
28 _translocation(bam1_t* rec) {
29 return (rec->core.tid != rec->core.mtid);
30 }
31
32 // Deletions
33 inline uint8_t
34 _getSpanOrientation(int32_t const svt) {
35 if (_translocation(svt)) {
36 return svt - DELLY_SVT_TRANS;
37 } else {
38 return svt;
39 }
40 }
41
42 // Junction record
43 struct Junction {
44 bool forward;
45 bool scleft;
46 int32_t refidx;
47 int32_t rstart;
48 int32_t refpos;
49 int32_t seqpos;
50 uint16_t qual;
51
52 Junction(bool const fw, bool const cl, int32_t const idx, int32_t const rst, int32_t const r, int32_t const s, uint16_t const qval) : forward(fw), scleft(cl), refidx(idx), rstart(rst), refpos(r), seqpos(s), qual(qval) {}
53
54 bool operator<(const Junction& j2) const {
55 return ((seqpos<j2.seqpos) || ((seqpos==j2.seqpos) && (refidx<j2.refidx)) || ((seqpos==j2.seqpos) && (refidx==j2.refidx) && (refpos<j2.refpos)) || ((seqpos==j2.seqpos) && (refidx==j2.refidx) && (refpos==j2.refpos) && (scleft < j2.scleft)));
56 }
57 };
58
59
60
61 // Split-read record
62 struct SRBamRecord {
63 int32_t chr;
64 int32_t pos;
65 int32_t chr2;
66 int32_t pos2;
67 int32_t rstart;
68 int32_t sstart;
69 int32_t qual;
70 int32_t inslen;
71 int32_t svid;
72 std::size_t id;
73
74 SRBamRecord(int32_t const c, int32_t const p, int32_t const c2, int32_t const p2, int32_t const rst, int32_t const sst, int32_t const qval, int32_t const il, std::size_t const idval) : chr(c), pos(p), chr2(c2), pos2(p2), rstart(rst), sstart(sst), qual(qval), inslen(il), svid(-1), id(idval) {}
75
76 bool operator<(const SRBamRecord& sv2) const {
77 return ((chr<sv2.chr) || ((chr==sv2.chr) && (pos<sv2.pos)) || ((chr==sv2.chr) && (pos==sv2.pos) && (chr2<sv2.chr2)) || ((chr==sv2.chr) && (pos==sv2.pos) && (chr2==sv2.chr2) && (pos2 < sv2.pos2)));
78 }
79 };
80
81 // Structural variant record
82 struct StructuralVariantRecord {
83 int32_t chr;
84 int32_t svStart;
85 int32_t chr2;
86 int32_t svEnd;
87 int32_t ciposlow;
88 int32_t ciposhigh;
89 int32_t ciendlow;
90 int32_t ciendhigh;
91 int32_t srSupport;
92 int32_t srMapQuality;
93 int32_t mapq;
94 int32_t insLen;
95 int32_t svt;
96 int32_t id;
97 int32_t homLen;
98 int32_t peSupport;
99 int32_t peMapQuality;
100 int32_t consBp;
101 float srAlignQuality;
102 bool precise;
103 std::string alleles;
104 std::string consensus;
105
106
107 StructuralVariantRecord() : chr(0), svStart(0), chr2(0), svEnd(0), ciposlow(0), ciposhigh(0), ciendlow(0), ciendhigh(0), srSupport(0), srMapQuality(0), mapq(0), insLen(0), svt(-1), id(0), homLen(0), peSupport(0), peMapQuality(0), consBp(0), srAlignQuality(0), precise(false) {}
108
109 StructuralVariantRecord(int32_t const c, int32_t const s, int32_t const e) : chr(c), svStart(s), chr2(c), svEnd(e), ciposlow(0), ciposhigh(0), ciendlow(0), ciendhigh(0), srSupport(0), srMapQuality(0), mapq(0), insLen(0), svt(-1), id(0), homLen(0), peSupport(0), peMapQuality(0), consBp(0), srAlignQuality(0), precise(false) {}
110
111 StructuralVariantRecord(int32_t const c1, int32_t const s, int32_t const c2, int32_t const e, int32_t const cipl, int32_t const ciph, int32_t const ciel, int32_t const cieh, int32_t const sup, int32_t const srmapq, int32_t const qval, int32_t const ilen, int32_t const svtype, int32_t const idval): chr(c1), svStart(s), chr2(c2), svEnd(e), ciposlow(cipl), ciposhigh(ciph), ciendlow(ciel), ciendhigh(cieh), srSupport(sup), srMapQuality(srmapq), mapq(qval), insLen(ilen), svt(svtype), id(idval), homLen(0), peSupport(0), peMapQuality(0), consBp(0), srAlignQuality(0), precise(true) {}
112
113 bool operator<(const StructuralVariantRecord& sv2) const {
114 return ((chr<sv2.chr) || ((chr==sv2.chr) && (svStart<sv2.svStart)) || ((chr==sv2.chr) && (svStart==sv2.svStart) && (chr2<sv2.chr2)) || ((chr==sv2.chr) && (svStart==sv2.svStart) && (chr2==sv2.chr2) && (svEnd<sv2.svEnd)) || ((chr==sv2.chr) && (svStart==sv2.svStart) && (chr2==sv2.chr2) && (svEnd==sv2.svEnd) && (peSupport > sv2.peSupport)) || ((chr==sv2.chr) && (svStart==sv2.svStart) && (chr2==sv2.chr2) && (svEnd==sv2.svEnd) && (peSupport == sv2.peSupport) && (srSupport > sv2.srSupport)));
115 }
116
117 };
118
119 struct Breakpoint {
120 int32_t svStartBeg;
121 int32_t svStartEnd;
122 int32_t svEndBeg;
123 int32_t svEndEnd;
124 int32_t svStart;
125 int32_t svEnd;
126 int32_t peSupport;
127 int32_t svt;
128 int32_t chr;
129 int32_t chr2;
130 std::string part1;
131
132 Breakpoint() : svStartBeg(0), svStartEnd(0), svEndBeg(0), svEndEnd(0), svStart(0), svEnd(0), peSupport(0), svt(-1), chr(0), chr2(0) {}
133 explicit Breakpoint(StructuralVariantRecord const& sv) : svStartBeg(sv.svStart), svStartEnd(sv.svStart), svEndBeg(sv.svEnd), svEndEnd(sv.svEnd), svStart(sv.svStart), svEnd(sv.svEnd), peSupport(sv.peSupport), svt(sv.svt), chr(sv.chr), chr2(sv.chr2) {}
134
135 };
136
137 // Initialize breakpoint
138 template<typename TBreakpoint>
139 inline void
140 _initBreakpoint(bam_hdr_t const* hdr, TBreakpoint& bp, int32_t const boundary, int32_t const svt) {
141 if (_translocation(svt)) {
142 bp.svStartBeg = std::max(0, bp.svStart - boundary);
143 bp.svStartEnd = std::min((int32_t) (hdr->target_len[bp.chr]), bp.svStart + boundary);
144 bp.svEndBeg = std::max(0, bp.svEnd - boundary);
145 bp.svEndEnd = std::min((int32_t) (hdr->target_len[bp.chr2]), bp.svEnd + boundary);
146 } else {
147 if (svt == 4) {
148 bp.svStartBeg = std::max(0, bp.svStart - boundary);
149 bp.svStartEnd = std::min((int32_t) (hdr->target_len[bp.chr]), bp.svStart + boundary);
150 bp.svEndBeg = std::max(0, bp.svEnd - boundary);
151 bp.svEndEnd = std::min((int32_t) (hdr->target_len[bp.chr2]), bp.svEnd + boundary);
152 } else {
153 bp.svStartBeg = std::max(0, bp.svStart - boundary);
154 bp.svStartEnd = std::min(bp.svStart + boundary, (bp.svStart + bp.svEnd)/2);
155 bp.svEndBeg = std::max((bp.svStart + bp.svEnd)/2 + 1, bp.svEnd - boundary);
156 bp.svEndEnd = std::min((int32_t) (hdr->target_len[bp.chr2]), bp.svEnd + boundary);
157 }
158 }
159 }
160
161 template<typename TPos>
162 inline TPos
163 _minCoord(TPos const position, TPos const matePosition, int32_t const svt) {
164 if (_translocation(svt)) return position;
165 else return std::min(position, matePosition);
166 }
167
168 template<typename TPos>
169 inline TPos
170 _maxCoord(TPos const position, TPos const matePosition, int32_t const svt) {
171 if (_translocation(svt)) return matePosition;
172 else return std::max(position, matePosition);
173 }
174
175
176 template<typename TSize>
177 inline bool
178 _svSizeCheck(TSize const s, TSize const e, int32_t const svt) {
179 // Short reads
180 if (svt == 0) return (( e - s ) >= 300);
181 else if (svt == 1) return (( e - s ) >= 300);
182 else if (svt == 2) return (( e - s ) >= 300);
183 else if (svt == 3) return (( e - s ) >= 100);
184 else return true;
185 }
186
187 template<typename TSize>
188 inline bool
189 _svSizeCheck(TSize const s, TSize const e, int32_t const svt, int32_t const inslen) {
190 // Long reads
191 if (svt == 0) return (( e - s ) >= 15);
192 else if (svt == 1) return (( e - s ) >= 15);
193 else if (svt == 2) return (( e - s ) >= 15);
194 else if (svt == 3) return (( e - s ) >= 15);
195 else if (svt == 4) return (inslen >= 15);
196 else return true;
197 }
198
199
200 // 0: Left-spanning inversion
201 // 1: Right-spanning inversion
202 // 2: Deletion-type
203 // 3: Duplication-type
204
205 inline uint8_t
206 getSVType(bam1_t* rec) {
207 if (!(rec->core.flag & BAM_FREVERSE)) {
208 if (!(rec->core.flag & BAM_FMREVERSE)) return 0;
209 else return (rec->core.pos < rec->core.mpos) ? 2 : 3;
210 } else {
211 if (!(rec->core.flag & BAM_FMREVERSE)) return (rec->core.pos > rec->core.mpos) ? 2 : 3;
212 else return 1;
213 }
214 }
215
216 inline int32_t
217 _isizeMappingPos(bam1_t* rec, int32_t isize) {
218 if (_translocation(rec)) {
219 uint8_t orient = getSVType(rec);
220 if (orient == 0) return DELLY_SVT_TRANS + 0;
221 else if (orient == 1) return DELLY_SVT_TRANS + 1;
222 else {
223 // 3to5 or 5to3?
224 if (rec->core.tid > rec->core.mtid) {
225 if (!(rec->core.flag & BAM_FREVERSE)) return DELLY_SVT_TRANS + 2;
226 else return DELLY_SVT_TRANS + 3;
227 } else {
228 if (!(rec->core.flag & BAM_FREVERSE)) return DELLY_SVT_TRANS + 3;
229 else return DELLY_SVT_TRANS + 2;
230 }
231 }
232 } else {
233 if (rec->core.pos == rec->core.mpos) return -1; // No SV
234 uint8_t orient = getSVType(rec);
235 if (orient == 0) return 0;
236 else if (orient == 1) return 1;
237 else if (orient == 2) {
238 if (isize > std::abs(rec->core.isize)) return -1;
239 else return 2;
240 } else {
241 if (std::abs(rec->core.pos - rec->core.mpos) < 100) return -1; // Too small
242 return 3;
243 }
244 }
245 }
246
247 inline unsigned hash_string(const char *s) {
248 unsigned h = 37;
249 while (*s) {
250 h = (h * 54059) ^ (s[0] * 76963);
251 s++;
252 }
253 return h;
254 }
255
256 template<typename TAlignedReads>
257 inline bool
258 _firstPairObs(bam1_t* rec, TAlignedReads const& lastAlignedPosReads) {
259 if (rec->core.tid == rec->core.mtid) return ((rec->core.pos < rec->core.mpos) || ((rec->core.pos == rec->core.mpos) && (lastAlignedPosReads.find(hash_string(bam_get_qname(rec))) == lastAlignedPosReads.end())));
260 else return (rec->core.tid < rec->core.mtid);
261 }
262
263 // Deletions
264 template<typename TSize, typename TISize>
265 inline bool
266 _pairsDisagree(TSize const pair1Min, TSize const pair1Max, TSize const pair1ReadLength, TISize const pair1maxNormalISize, TSize const pair2Min, TSize const pair2Max, TSize const pair2ReadLength, TISize const pair2maxNormalISize, int32_t const svt) {
267 if (_translocation(svt)) {
268 uint8_t ct = _getSpanOrientation(svt);
269
270 // Check read offsets
271 if (ct%2==0) {
272 if ((pair2Min + pair2ReadLength - pair1Min) > pair1maxNormalISize) return true;
273 if (ct>=2) {
274 if (pair2Max < pair1Max) {
275 if ((pair1Max + pair1ReadLength - pair2Max) > pair1maxNormalISize) return true;
276 } else {
277 if ((pair2Max + pair2ReadLength - pair1Max) > pair2maxNormalISize) return true;
278 }
279 } else {
280 if (pair2Max < pair1Max) {
281 if ((pair1Max + pair1ReadLength - pair2Max) > pair2maxNormalISize) return true;
282 } else {
283 if ((pair2Max + pair2ReadLength - pair1Max) > pair1maxNormalISize) return true;
284 }
285 }
286 } else {
287 if ((pair2Min + pair2ReadLength - pair1Min) > pair2maxNormalISize) return true;
288 if (ct>=2) {
289 if (pair2Max < pair1Max) {
290 if ((pair1Max + pair1ReadLength - pair2Max) > pair2maxNormalISize) return true;
291 } else {
292 if ((pair2Max + pair2ReadLength - pair1Max) > pair1maxNormalISize) return true;
293 }
294 } else {
295 if (pair2Max < pair1Max) {
296 if ((pair1Max + pair1ReadLength - pair2Max) > pair1maxNormalISize) return true;
297 } else {
298 if ((pair2Max + pair2ReadLength - pair1Max) > pair2maxNormalISize) return true;
299 }
300 }
301 }
302 return false;
303 } else {
304 if (svt < 2) {
305 // Inversion
306 if (!svt) {
307 // Left-spanning inversions
308 if ((pair2Min + pair2ReadLength - pair1Min) > pair1maxNormalISize) return true;
309 if ((pair2Max < pair1Max) && ((pair1Max + pair1ReadLength - pair2Max) > pair2maxNormalISize)) return true;
310 if ((pair2Max >= pair1Max) && ((pair2Max + pair2ReadLength - pair1Max) > pair1maxNormalISize)) return true;
311 } else {
312 // Right-spanning inversions
313 if ((pair2Min + pair2ReadLength - pair1Min) > pair2maxNormalISize) return true;
314 if ((pair2Max < pair1Max) && ((pair1Max + pair1ReadLength - pair2Max) > pair1maxNormalISize)) return true;
315 if ((pair2Max >= pair1Max) && ((pair2Max + pair2ReadLength - pair1Max) > pair2maxNormalISize)) return true;
316 }
317 return false;
318 } else if (svt == 2) {
319 // Deletion
320 if ((pair2Min + pair2ReadLength - pair1Min) > pair1maxNormalISize) return true;
321 if ((pair2Max < pair1Max) && ((pair1Max + pair1ReadLength - pair2Max) > pair1maxNormalISize)) return true;
322 if ((pair2Max >= pair1Max) && ((pair2Max + pair2ReadLength - pair1Max) > pair2maxNormalISize)) return true;
323 if ((pair1Max < pair2Min) || (pair2Max < pair1Min)) return true;
324 return false;
325 } else if (svt == 3) {
326 if ((pair2Min + pair2ReadLength - pair1Min) > pair2maxNormalISize) return true;
327 if ((pair2Max < pair1Max) && ((pair1Max + pair1ReadLength - pair2Max) > pair2maxNormalISize)) return true;
328 if ((pair2Max >= pair1Max) && ((pair2Max + pair2ReadLength - pair1Max) > pair1maxNormalISize)) return true;
329 return false;
330 }
331 }
332 return false;
333 }
334
335
336 }
337
338 #endif