Debian Codegraph @

src/BamRecord.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #include "SeqLib/BamRecord.h"
2
3 #include <cassert>
4 #include <bitset>
5 #include <cctype>
6 #include <stdexcept>
7
•8 #include <ssw_cpp.h>
9
10 #define TAG_DELIMITER "^"
11 #define CTAG_DELIMITER '^'
12
13 namespace SeqLib {
14
15 const int CigarCharToInt[128] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, //0-9
16 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, //10-19
17 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, //20
18 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, //30
19 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, //40
20 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, //50
21 -1,BAM_CEQUAL,-1,-1,-1,-1,BAM_CBACK,-1,BAM_CDEL,-1, //60-69
22 -1,-1,BAM_CHARD_CLIP,BAM_CINS,-1,-1,-1,BAM_CMATCH,BAM_CREF_SKIP,-1,
23 BAM_CPAD,-1,-1,BAM_CSOFT_CLIP,-1,-1,-1,-1,BAM_CDIFF,-1,
24 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
25 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
26 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
27 -1,-1,-1,-1,-1,-1,-1,-1};
28
29
30
31 struct free_delete {
32 void operator()(void* x) { bam_destroy1((bam1_t*)x); }
33 };
34
35 void BamRecord::init() {
36 bam1_t* f = bam_init1();
37 b = SeqPointer<bam1_t>(f, free_delete());
38 }
39
40 void BamRecord::assign(bam1_t* a) {
41 b = SeqPointer<bam1_t>(a, free_delete());
42 }
43
44 int32_t BamRecord::PositionWithSClips() const {
45 if(!b) return -1; // to be consistent with BamRecord::Position()
46
47 uint32_t* cig = bam_get_cigar(b);
48 return ((*cig) & 0xF) == BAM_CSOFT_CLIP ? b->core.pos - ((*cig) >> 4) : b->core.pos;
49 }
50
51 int32_t BamRecord::PositionEnd() const {
52 return b ? (b->core.l_qseq > 0 ? bam_endpos(b.get()) : b->core.pos + GetCigar().NumQueryConsumed()) : -1;
53 }
54
55 int32_t BamRecord::PositionEndWithSClips() const {
56 if(!b) return -1; // to be consistent with BamRecord::PositionEnd()
57
58 uint32_t* cig_last = bam_get_cigar(b) + b->core.n_cigar - 1;
59 if(b->core.l_qseq > 0) {
60 return ((*cig_last) & 0xF) == BAM_CSOFT_CLIP ? bam_endpos(b.get()) + ((*cig_last) >> 4) :
61 bam_endpos(b.get());
62 } else {
63 return b->core.pos + GetCigar().NumQueryConsumed();
64 }
65 }
66
67 int32_t BamRecord::PositionEndMate() const {
68 return b ? (b->core.mpos + (b->core.l_qseq > 0 ? b->core.l_qseq : GetCigar().NumQueryConsumed())) : -1;
69 }
70
71 GenomicRegion BamRecord::AsGenomicRegion() const {
72 char s = '*';
73 if (MappedFlag())
74 s = ReverseFlag() ? '-' : '+';
75 return GenomicRegion(b->core.tid, b->core.pos, PositionEnd(), s);
76 }
77
78 GenomicRegion BamRecord::AsGenomicRegionMate() const {
79 char s = '*';
80 if (MateMappedFlag())
81 s = MateReverseFlag() ? '-' : '+';
82 return GenomicRegion(b->core.mtid, b->core.mpos, PositionEndMate(), s);
83 }
84
85 std::string BamRecord::Sequence() const {
86 uint8_t * p = bam_get_seq(b);
87 std::string out(b->core.l_qseq, 'N');
88 for (int32_t i = 0; i < b->core.l_qseq; ++i)
89 out[i] = BASES[bam_seqi(p,i)];
90 return out;
91
92 }
93
94 void BamRecord::SetCigar(const Cigar& c) {
95
96 // case where they are equal, just swap them out
97 if (c.size() == b->core.n_cigar) {
98 b->core.n_cigar = c.size();
99 uint32_t * cigr = bam_get_cigar(b);
100 for (size_t i = 0; i < b->core.n_cigar; ++i)
101 cigr[i] = c[i].raw();
102 return;
103 }
104
105 // make the new cigar structure
106 uint32_t* new_cig = (uint32_t*)malloc(4 * c.size());
107 for (size_t i = 0; i < c.size(); ++i)
108 new_cig[i] = c[i].raw();
109
110 int new_size = b->l_data - (b->core.n_cigar<<2) + (c.size()<<2);
111 int old_seqaux_spot = (b->core.n_cigar<<2) + b->core.l_qname;
112 int old_seqaux_len = bam_get_l_aux(b) + ((b->core.l_qseq + 1)>>1) + b->core.l_qseq;
113
114 // set the new cigar size
115 b->core.n_cigar = c.size();
116
117 // copy out all the old data
118 uint8_t* oldd = (uint8_t*)malloc(b->l_data);
119 memcpy(oldd, b->data, b->l_data);
120
121 // clear out the old data and alloc the new amount
122 free(b->data);
123 b->data = (uint8_t*)calloc(new_size, sizeof(uint8_t));
124
125 // add back the qname
126 memcpy(b->data, oldd, b->core.l_qname);
127
128 // add in the new cigar
129 memcpy(b->data + b->core.l_qname, new_cig, c.size()<<2);
130
131 // add back the rest of the data
132 memcpy(b->data + b->core.l_qname + (b->core.n_cigar<<2), oldd + old_seqaux_spot, old_seqaux_len);
133
134 // update the sizes
135 // >>1 shift is because only 4 bits needed per ATCGN base
136 b->l_data = new_size;
137 b->core.n_cigar = c.size();
138
139 free(oldd);
140 free(new_cig);
141 }
142
143 BamRecord::BamRecord(const std::string& name, const std::string& seq, const std::string& ref, const GenomicRegion * gr) {
144
145 StripedSmithWaterman::Aligner aligner;
146 // Declares a default filter
147 StripedSmithWaterman::Filter filter;
148 // Declares an alignment that stores the result
149 StripedSmithWaterman::Alignment alignment;
150 // Aligns the seq to the ref
151 aligner.Align(seq.c_str(), ref.c_str(), ref.size(), filter, &alignment);
152
153 init();
154 b->core.tid = gr->chr;
155 b->core.pos = gr->pos1 + alignment.ref_begin + 1; // add to make it 1-indexed, not 0-indexed
156 b->core.qual = 60; //alignment.sw_score;
157 b->core.flag = 0;
158 b->core.n_cigar = alignment.cigar.size();
159
160 // set dumy mate
161 b->core.mtid = -1;
162 b->core.mpos = -1;
163 b->core.isize = 0;
164
165 // allocate all the data
166 b->core.l_qname = name.length() + 1;
167 b->core.l_qseq = seq.length(); //(seq.length()>>1) + seq.length() % 2; // 4-bit encoding
168 b->l_data = b->core.l_qname + (b->core.n_cigar<<2) + ((b->core.l_qseq+1)>>1) + (b->core.l_qseq);
169 b.get()->data = (uint8_t*)malloc(b.get()->l_data);
170
171 // allocate the qname
172 memcpy(b->data, name.c_str(), name.length() + 1);
173
174 // allocate the cigar. 32 bits per elem (4 type, 28 length)
175 uint32_t * cigr = bam_get_cigar(b);
176 for (size_t i = 0; i < alignment.cigar.size(); ++i) {
177 cigr[i] = alignment.cigar[i]; //Length << BAM_CIGAR_SHIFT | BAM_CMATCH;
178 }
179
180 // allocate the sequence
181 uint8_t* m_bases = b->data + b->core.l_qname + (b->core.n_cigar<<2);
182
183 // TODO move this out of bigger loop
184 int slen = seq.length();
185 for (int i = 0; i < slen; ++i) {
186 // bad idea but works for now
187 uint8_t base = 15;
188 if (seq.at(i) == 'A')
189 base = 1;
190 else if (seq.at(i) == 'C')
191 base = 2;
192 else if (seq.at(i) == 'G')
193 base = 4;
194 else if (seq.at(i) == 'T')
195 base = 8;
196
197 m_bases[i >> 1] &= ~(0xF << ((~i & 1) << 2)); ///< zero out previous 4-bit base encoding
198 m_bases[i >> 1] |= base << ((~i & 1) << 2); ///< insert new 4-bit base encoding
199
200 }
201
202 // add in the actual alignment score
203 AddIntTag("AS", alignment.sw_score);
204
205 }
206
207 void BamRecord::SmartAddTag(const std::string& tag, const std::string& val)
208 {
209 // get the old tag
210 assert(tag.length());
211 assert(val.length());
212 std::string tmp;
213 GetZTag(tag, tmp);
214 if (!tmp.length())
215 {
216 AddZTag(tag, val);
217 return;
218 }
219
220 // check that we don't have the tag delimiter in the stirng
221 if (val.find(TAG_DELIMITER) != std::string::npos)
222 std::cerr << "BamRecord::SmartAddTag -- Tag delimiter " << TAG_DELIMITER << " is in the value to be added. Compile with diff tag delimiter or change val" << std::endl;
223
224 // append the tag
225 tmp += TAG_DELIMITER + val;
226
227 // remove the old tag
228 RemoveTag(tag.c_str());
229
230 // add the new one
231 assert(tmp.length());
232 AddZTag(tag, tmp);
233 }
234
235 void BamRecord::ClearSeqQualAndTags() {
236
237 int new_size = b->core.l_qname + ((b)->core.n_cigar<<2);// + 1; ///* 0xff seq */ + 1 /* 0xff qual */;
238 b->data = (uint8_t*)realloc(b->data, new_size);
239 b->l_data = new_size;
240 b->core.l_qseq = 0;
241 }
242
243 void BamRecord::SetSequence(const std::string& seq) {
244
245 int new_size = b->l_data - ((b->core.l_qseq+1)>>1) - b->core.l_qseq + ((seq.length()+1)>>1) + seq.length();
246 int old_aux_spot = (b->core.n_cigar<<2) + b->core.l_qname + ((b->core.l_qseq + 1)>>1) + b->core.l_qseq;
247 int old_aux_len = bam_get_l_aux(b); //(b->core.n_cigar<<2) + b->core.l_qname + ((b->core.l_qseq + 1)>>1) + b->core.l_qseq;
248
249 // copy out all the old data
250 uint8_t* oldd = (uint8_t*)malloc(b->l_data);
251 memcpy(oldd, b->data, b->l_data);
252
253 // clear out the old data and alloc the new amount
254 free(b->data);
255 b->data = (uint8_t*)calloc(new_size, sizeof(uint8_t));
256
257 // add back the qname and cigar
258 memcpy(b->data, oldd, b->core.l_qname + (b->core.n_cigar<<2));
259
260 // update the sizes
261 // >>1 shift is because only 4 bits needed per ATCGN base
262 b->l_data = new_size; //b->l_data - ((b->core.l_qseq + 1)>>1) - b->core.l_qseq + ((seq.length()+1)>>1) + seq.length();
263 b->core.l_qseq = seq.length();
264
265 // allocate the sequence
266 uint8_t* m_bases = b->data + b->core.l_qname + (b->core.n_cigar<<2);
267 int slen = seq.length();
268
269 for (int i = 0; i < slen; ++i) {
270
271 // bad idea but works for now
272 uint8_t base = 15;
273 if (seq.at(i) == 'A')
274 base = 1;
275 else if (seq.at(i) == 'C')
276 base = 2;
277 else if (seq.at(i) == 'G')
278 base = 4;
279 else if (seq.at(i) == 'T')
280 base = 8;
281
282 m_bases[i >> 1] &= ~(0xF << ((~i & 1) << 2)); ///< zero out previous 4-bit base encoding
283 m_bases[i >> 1] |= base << ((~i & 1) << 2); ///< insert new 4-bit base encoding
284 }
285
286 // add in a NULL qual
287 uint8_t* s = bam_get_qual(b);
288 s[0] = 0xff;
289
290 // add the aux data
291 uint8_t* t = bam_get_aux(b);
292 memcpy(t, oldd + old_aux_spot, old_aux_len);
293
294 // reset the max size
295 b->m_data = b->l_data;
296
297 free(oldd); //just added
298
299 }
300
301 void BamRecord::SetQname(const std::string& n)
302 {
303 // copy out the non-qname data
304 size_t nonq_len = b->l_data - b->core.l_qname;
305 uint8_t* nonq = (uint8_t*)malloc(nonq_len);
306 memcpy(nonq, b->data + b->core.l_qname, nonq_len);
307
308 // clear the old data and alloc the new amount
309 free(b->data);
310 b->data = (uint8_t*)calloc(nonq_len + n.length() + 1, 1);
311
312 // add in the new qname
313 memcpy(b->data, (uint8_t*)n.c_str(), n.length() + 1); // +1 for \0
314
315 // update the sizes
316 b->l_data = b->l_data - b->core.l_qname + n.length() + 1;
317 b->core.l_qname = n.length() + 1;
318
319 // copy over the old data
320 memcpy(b->data + b->core.l_qname, nonq, nonq_len);
321 free(nonq);
322
323 // reset the max size
324 b->m_data = b->l_data;
325 }
326
327 void BamRecord::SetQualities(const std::string& n, int offset) {
328
329 if (!n.empty() && n.length() != b->core.l_qseq)
330 throw std::invalid_argument("New quality score should be same as seq length");
331
332 // length of qual is always same as seq. If empty qual, just set first bit of qual to 0
333 if (n.empty()) {
334 uint8_t* r = bam_get_qual(b);
335 r[0] = 0;
336 return;
337 }
338
339 char * q = strdup(n.data());
340 for (size_t i = 0; i < n.length(); ++i)
341 q[i] -= offset;
342 memcpy(bam_get_qual(b), q, n.length()); // dont copy /0 terminator
343 free(q);
344
345 }
346
347 double BamRecord::MeanPhred() const {
348
349 if (b->core.l_qseq <= 0)
350 return -1;
351
352 double s = 0;
353 uint8_t* p = bam_get_qual(b);
354 for (int32_t i = 0; i < b->core.l_qseq; ++i)
355 s += p[i];
356 return s / b->core.l_qseq;
357 }
358
359 std::string BamRecord::QualitySequence() const {
360 std::string seq;
361 GetZTag("GV", seq);
362 if (!seq.length())
363 seq = Sequence();
364 return seq;
365 }
366
367 std::ostream& operator<<(std::ostream& out, const BamRecord &r)
368 {
369 if (!r.b) {
370 out << "empty read";
371 return out;
372 }
373 out << bam_get_qname(r.b) << "\t" << r.b->core.flag
374 << "\t" << (r.b->core.tid+1) << "\t" << r.b->core.pos
375 << "\t" << r.b->core.qual << "\t" << r.CigarString()
376 << "\t" << (r.b->core.mtid+1) << "\t" << r.b->core.mpos << "\t"
377 << r.FullInsertSize() //r.b->core.isize
378 << "\t" << r.Sequence() << "\t*" << std::endl;
379 return out;
380
381
382 }
383
384 int32_t BamRecord::CountBWASecondaryAlignments() const
385 {
386 int xp_count = 0;
387
388 // xa tag
389 std::string xar_s;
390 GetZTag("XA", xar_s);
391 if (xar_s.length()) {
392 xp_count += std::count(xar_s.begin(), xar_s.end(), ';');
393 }
394
395 return xp_count;
396
397 }
398
399 int32_t BamRecord::CountBWAChimericAlignments() const
400 {
401 int xp_count = 0;
402
403 // sa tag (post bwa mem v0.7.5)
404 std::string xar_s;
405 GetZTag("SA", xar_s);
406 if (xar_s.length())
407 xp_count += std::count(xar_s.begin(), xar_s.end(), ';');
408
409 // xp tag (pre bwa mem v0.7.5)
410 std::string xpr_s;
411 GetZTag("XP", xpr_s);
412 if (xpr_s.length())
413 xp_count += std::count(xpr_s.begin(), xpr_s.end(), ';');
414
415 return xp_count;
416
417 }
418
419 int32_t BamRecord::CountNBases() const {
420 uint8_t* p = bam_get_seq(b);
421 int32_t n = 0;
422 for (int ww = 0; ww < b->core.l_qseq; ww++)
423 if (bam_seqi(p,ww) == 15)
424 ++n;
425 return n;
426 }
427
428 void BamRecord::QualityTrimmedSequence(int32_t qualTrim, int32_t& startpoint, int32_t& endpoint) const {
429
430 endpoint = -1; //seq.length();
431 startpoint = 0;
432 int i = 0;
433
434 uint8_t * qual = bam_get_qual(b.get());
435
436 // if there is no quality score, return whole thing
437 if (qual[0] == 0xff) {
438 startpoint = 0;
439 return;
440
441 //return Sequence();
442 }
443
444 // get the start point (loop forward)
445 while(i < b->core.l_qseq) {
446 int ps = qual[i];
447 if (ps >= qualTrim) {
448 startpoint = i;
449 break;
450 }
451 ++i;
452 }
453
454 // get the end point (loop backwards)
455 i = b->core.l_qseq - 1; //seq.length() - 1;
456 while(i >= 0) {
457
458 int ps = qual[i];
459
460 if (ps >= qualTrim) { //ps >= qualTrim) {
461 endpoint = i + 1; // endpoint is one past edge
462 break;
463 }
464 --i;
465 }
466 }
467
468 void BamRecord::AddZTag(std::string tag, std::string val) {
469 if (tag.empty() || val.empty())
470 return;
471 bam_aux_append(b.get(), tag.data(), 'Z', val.length()+1, (uint8_t*)val.c_str());
472 }
473
474 bool BamRecord::GetTag(const std::string& tag, std::string& s) const {
475
476 if (GetZTag(tag, s))
477 return true;
478
479 int32_t t;
480 if (GetIntTag(tag, t)) {
481 std::stringstream ss;
482 ss << t;
483 s = ss.str();
484 return true;
485 }
486
487 float f;
488 if (GetFloatTag(tag, f)) {
489 std::stringstream ss;
490 ss << f;
491 s = ss.str();
492 return true;
493 }
494
495 return false;
496
497 }
498
499 bool BamRecord::GetZTag(const std::string& tag, std::string& s) const {
500 uint8_t* p = bam_aux_get(b.get(),tag.c_str());
501 if (!p)
502 return false;
503
504 int type = *p;
505 if (type != 'Z')
506 return false;
507
508 char* pp = bam_aux2Z(p);
509 if (!pp)
510 return false;
511 s = std::string(pp);
512 return true;
513 }
514
515
516 // get a string tag that might be separted by "x"
517 std::vector<std::string> BamRecord::GetSmartStringTag(const std::string& tag) const {
518
519 std::vector<std::string> out;
520 std::string tmp;
521 GetZTag(tag, tmp);
522
523 if (tmp.empty())
524 return std::vector<std::string>();
525
526 if (tmp.find(TAG_DELIMITER) != std::string::npos) {
527 std::istringstream iss(tmp);
528 std::string line;
529 while (std::getline(iss, line, CTAG_DELIMITER)) {
530 out.push_back(line);
531 }
532 } else {
533 out.push_back(tmp);
534 }
535
536 assert(out.size());
537 return out;
538
539 }
540
541
542 std::vector<int> BamRecord::GetSmartIntTag(const std::string& tag) const {
543
544 std::vector<int> out;
545 std::string tmp;
546
547 GetZTag(tag, tmp);
548 if (tmp.empty())
549 return std::vector<int>();
550
551 if (tmp.find(TAG_DELIMITER) != std::string::npos) {
552 std::istringstream iss(tmp);
553 std::string line;
554 while (std::getline(iss, line, CTAG_DELIMITER))
555 out.push_back(atoi(line.c_str()));
556 } else {
557 out.push_back(atoi(tmp.c_str()));
558 }
559
560 assert(out.size());
561 return out;
562
563 }
564
565 std::vector<double> BamRecord::GetSmartDoubleTag(const std::string& tag) const {
566
567 std::vector<double> out;
568 std::string tmp;
569
570 GetZTag(tag, tmp);
571 if (tmp.empty())
572 return std::vector<double>();
573
574 if (tmp.find(TAG_DELIMITER) != std::string::npos) {
575 std::istringstream iss(tmp);
576 std::string line;
577 while (std::getline(iss, line, CTAG_DELIMITER))
578 out.push_back(std::atof(line.c_str()));
579 } else { // single entry
580 out.push_back(std::atof(tmp.c_str()));
581 }
582
583 assert(out.size());
584 return out;
585
586 }
587
588 BamRecord::BamRecord(const std::string& name, const std::string& seq, const GenomicRegion * gr, const Cigar& cig) {
589
590 // make sure cigar fits with sequence
591 if (cig.NumQueryConsumed() != seq.length())
592 throw std::invalid_argument("Sequence string length mismatches cigar consumed query bases");
593
594 // make sure alignment fits
595 if (cig.NumReferenceConsumed() != gr->Width())
596 throw std::invalid_argument("Alignment position mismatches cigar consumed reference bases");
597
598 init();
599 b->core.tid = gr->chr;
600 b->core.pos = gr->pos1; //gr->pos1 + 1;
601 b->core.qual = 60;
602 b->core.flag = 0;
603 b->core.n_cigar = cig.size();
604
605 // set dumy mate
606 b->core.mtid = -1;
607 b->core.mpos = -1;
608 b->core.isize = 0;
609
610 // if alignment is reverse, set it
611 if (gr->strand == '-') // just choose this convention to reverse
612 b->core.flag |= BAM_FREVERSE;
613
614 // allocate all the data
615 b->core.l_qname = name.length() + 1;
616 b->core.l_qseq = seq.length(); //(seq.length()>>1) + seq.length() % 2; // 4-bit encoding
617 b->l_data = b->core.l_qname + (b->core.n_cigar<<2) + ((b->core.l_qseq+1)>>1) + (b->core.l_qseq);
618 b.get()->data = (uint8_t*)malloc(b.get()->l_data);
619
620 // allocate the qname
621 memcpy(b->data, name.c_str(), name.length() + 1);
622
623 // allocate the cigar. 32 bits per elem (4 type, 28 length)
624 uint32_t * cigr = bam_get_cigar(b);
625 for (size_t i = 0; i < cig.size(); ++i)
626 cigr[i] = cig[i].raw(); //Length << BAM_CIGAR_SHIFT | BAM_CMATCH;
627
628 // allocate the sequence
629 uint8_t* m_bases = b->data + b->core.l_qname + (b->core.n_cigar<<2);
630
631 // TODO move this out of bigger loop
632 int slen = seq.length();
633 for (int i = 0; i < slen; ++i) {
634 // bad idea but works for now
635 uint8_t base = 15;
636 if (seq.at(i) == 'A')
637 base = 1;
638 else if (seq.at(i) == 'C')
639 base = 2;
640 else if (seq.at(i) == 'G')
641 base = 4;
642 else if (seq.at(i) == 'T')
643 base = 8;
644
645 m_bases[i >> 1] &= ~(0xF << ((~i & 1) << 2)); ///< zero out previous 4-bit base encoding
646 m_bases[i >> 1] |= base << ((~i & 1) << 2); ///< insert new 4-bit base encoding
647
648 }
649 }
650
651
652 CigarField::CigarField(char t, uint32_t len) {
653 int op = CigarCharToInt[(int)t];
654 if (op < 0)
655 throw std::invalid_argument("Cigar type must be one of MIDSHPN=X");
656 data = len << BAM_CIGAR_SHIFT;
657 data = data | static_cast<uint32_t>(op);
658 }
659
660 std::ostream& operator<<(std::ostream& out, const CigarField& c) {
661 out << bam_cigar_oplen(c.data) << bam_cigar_opchr(c.data);
662 return out;
663 }
664
665
666 std::ostream& operator<<(std::ostream& out, const Cigar& c) {
667 for (Cigar::const_iterator i = c.begin(); i != c.end(); ++i)
668 out << *i;
669 return out;
670 }
671
672
673 Cigar::Cigar(const std::string& cig) {
674
675 //Cigar tc;
676
677 // get the ops (MIDSHPN)
678 std::vector<char> ops;
679 for (size_t i = 0; i < cig.length(); ++i)
680 if (!isdigit(cig.at(i))) {
681 ops.push_back(cig.at(i));
682 }
683
684 std::size_t prev = 0, pos;
685 std::vector<std::string> lens;
686 while ((pos = cig.find_first_of("MIDSHPNX", prev)) != std::string::npos) {
687 if (pos > prev)
688 lens.push_back(cig.substr(prev, pos-prev));
689 prev = pos+1;
690 }
691 if (prev < cig.length())
692 lens.push_back(cig.substr(prev, std::string::npos));
693
694 assert(ops.size() == lens.size());
695 for (size_t i = 0; i < lens.size(); ++i) {
696 add(CigarField(ops[i], std::atoi(lens[i].c_str())));
697 }
698
699 //return tc;
700
701 }
702
703 bool Cigar::operator==(const Cigar& c) const {
704 if (m_data.size() != c.size())
705 return false;
706 if (!m_data.size()) // both empty
707 return true;
708 for (size_t i = 0; i < m_data.size(); ++i)
709 if (m_data[i].Type() != c[i].Type() || m_data[i].Length() != c[i].Length())
710 return false;
711 return true;
712 }
713
714
715 int BamRecord::OverlappingCoverage(const BamRecord& r) const {
716
717 uint32_t* c = bam_get_cigar(b);
718 uint32_t* c2 = bam_get_cigar(r.b);
719
720 //uint8_t * cov1 = (uint8_t*)calloc(l > 0 ? l : b->core.l_qseq, sizeof(uint8_t));
721 uint8_t * cov1 = (uint8_t*)calloc(GetCigar().NumQueryConsumed(), sizeof(uint8_t));
722 size_t pos = 0;
723 for (int k = 0; k < b->core.n_cigar; ++k) {
724 if (bam_cigar_opchr(c[k]) == 'M') // is match, so track locale
725 for (size_t j = 0; j < bam_cigar_oplen(c[k]); ++j)
726 cov1[pos + j] = 1;
727 if (bam_cigar_type(bam_cigar_op(c[k]))&1) // consumes query, so move position
728 pos = pos + bam_cigar_oplen(c[k]);
729 }
730
731 pos = 0;
732 size_t ocov = 0; // overlapping coverage
733 for (int k = 0; k < r.b->core.n_cigar; ++k) {
734 if (bam_cigar_opchr(c2[k]) == 'M') // is match, so track local
735 for (size_t j = 0; j < bam_cigar_oplen(c2[k]); ++j)
736 if (cov1[pos+j]) // r is covered. Check again this too
737 ++ocov;
738 if (bam_cigar_type(bam_cigar_op(c2[k]))&1) // consumes query, so move position
739 pos = pos + bam_cigar_oplen(c2[k]);
740 }
741
742 free(cov1);
743
744 return ocov;
745 }
746
747
748 }