12 SingleEndT(bam1_t *b) {
16 int getSign(bool value) const { return value ? -1 : 1; }
18 int compare(const SingleEndT& o) const {
22 if (b->core.tid != o.b->core.tid) return getSign(b->core.tid < o.b->core.tid);
23 if (b->core.pos != o.b->core.pos) return getSign(b->core.pos < o.b->core.pos);
24 strand1 = b->core.flag & 0x0010; strand2 = o.b->core.flag & 0x0010;
25 if (strand1 != strand2) return getSign(strand1 < strand2);
26 if (b->core.n_cigar != o.b->core.n_cigar) return getSign(b->core.n_cigar < o.b->core.n_cigar);
27 p1 = bam1_cigar(b); p2 = bam1_cigar(o.b);
28 for (int i = 0; i < (int)b->core.n_cigar; i++) {
29 if (*p1 != *p2) return getSign(*p1 < *p2);
36 bool operator< (const SingleEndT& o) const {
37 return compare(o) < 0;
42 SingleEndT mate1, mate2;
44 PairedEndT(const SingleEndT& mate1, const SingleEndT& mate2) : mate1(mate1), mate2(mate2) {
47 bool operator< (const PairedEndT& o) const {
48 int value = mate1.compare(o.mate1);
49 return value < 0 || value == 0 && mate2 < o.mate2;
55 CollapseMap() { isPaired = false; smap.clear(); pmap.clear(); }
57 void init(bool isPaired) {
58 this->isPaired = isPaired;
59 isPaired ? pmap.clear() : smap.clear();
62 void insert(bam1_t *b, bam1_t *b2, float prb) {
64 smapIter = smap.find(SingleEndT(b));
65 if (smapIter == smap.end()) { smap[SingleEndT(bam_dup1(b))] = prb; }
66 else smapIter->second += prb;
69 pmapIter = pmap.find(PairedEndT(SingleEndT(b), SingleEndT(b2)));
70 if (pmapIter == pmap.end()) { pmap[PairedEndT(SingleEndT(bam_dup1(b)), SingleEndT(bam_dup1(b2)))] = prb; }
71 else pmapIter->second += prb;
75 //once this function is called, "insert" cannot be called anymore
76 bool empty(bool& par) {
80 if (!isPaired) { value = smap.empty(); smapIter = smap.begin(); }
81 else { value = pmap.empty(); pmapIter = pmap.begin(); }
86 bool next(bam1_t*& b, bam1_t*& b2, float& prb) {
90 value = smapIter != smap.end();
92 b = smapIter->first.b;
93 prb = smapIter->second;
98 value = pmapIter != pmap.end();
100 b = pmapIter->first.mate1.b;
101 b2 = pmapIter->first.mate2.b;
102 prb = pmapIter->second;
113 std::map<SingleEndT, float> smap;
114 std::map<SingleEndT, float>::iterator smapIter;
116 std::map<PairedEndT, float> pmap;
117 std::map<PairedEndT, float>::iterator pmapIter;
120 #endif /* BC_AUX_H_ */