]> git.donarmstrong.com Git - rsem.git/blob - HitContainer.h
For genome BAM, modified MD tag accordingly
[rsem.git] / HitContainer.h
1 #ifndef HITCONTAINER_H_
2 #define HITCONTAINER_H_
3
4 #include<cassert>
5 #include<iostream>
6 #include<vector>
7 #include<algorithm>
8
9 #include "utils.h"
10 #include "GroupInfo.h"
11
12 template<class HitType>
13 class HitContainer {
14 public:
15         HitContainer() {
16                 clear();
17         }
18
19         void clear() {
20                 n = nhits = 0;
21                 s.clear();
22                 hits.clear();
23
24                 s.push_back(0);
25         }
26
27         bool read(std::istream&); // each time a read
28         void write(std::ostream&); // write all reads' hit out
29
30         void push_back(const HitType& hit)  {
31                 hits.push_back(hit);
32                 ++nhits;
33         }
34
35         //update read information vector etc
36         void updateRI() {
37                 if (nhits > s.back()) {  //Do not change if last read does not have hits
38                         s.push_back(nhits);
39                         ++n;
40                 }
41         }
42
43         READ_INT_TYPE getN() { return n; }
44
45         HIT_INT_TYPE getNHits() { return nhits; }
46
47         READ_INT_TYPE calcNumGeneMultiReads(const GroupInfo&);
48         READ_INT_TYPE calcNumIsoformMultiReads();
49
50         HIT_INT_TYPE getSAt(READ_INT_TYPE pos) { assert(pos >= 0 && pos <= n); return s[pos]; }
51
52         HitType& getHitAt(HIT_INT_TYPE pos) { assert(pos >= 0 && pos < nhits); return hits[pos]; }
53
54 private:
55         READ_INT_TYPE n; // n reads in total
56         HIT_INT_TYPE nhits; // # of hits
57         std::vector<HIT_INT_TYPE> s;
58         std::vector<HitType> hits;
59 };
60
61 //Each time only read one read's hits. If you want to start over, must call clear() first!
62 template<class HitType>
63 bool HitContainer<HitType>::read(std::istream& in) {
64         HIT_INT_TYPE tot;
65
66         if (!(in>>tot)) return false;
67         assert(tot > 0);
68         for (HIT_INT_TYPE i = 0; i < tot; i++) {
69                 HitType hit;
70                 if (!hit.read(in)) return false;
71                 hits.push_back(hit);
72         }
73
74         nhits = nhits + tot;
75         ++n;
76         s.push_back(nhits);
77
78         return true;
79 }
80
81 template<class HitType>
82 void HitContainer<HitType>::write(std::ostream& out) {
83         if (n <= 0) return;
84         for (READ_INT_TYPE i = 0; i < n; i++) {
85                 out<<s[i + 1] - s[i];
86                 for (HIT_INT_TYPE j = s[i]; j < s[i + 1]; j++) {
87                         hits[j].write(out);
88                 }
89                 out<<std::endl;
90         }
91 }
92
93 template<class HitType>
94 READ_INT_TYPE HitContainer<HitType>::calcNumGeneMultiReads(const GroupInfo& gi) {
95         READ_INT_TYPE res = 0;
96         int *sortgids = NULL;
97
98         for (READ_INT_TYPE i = 0; i < n; i++) {
99                 HIT_INT_TYPE num = s[i + 1] - s[i];
100                 sortgids = new int[num];
101                 for (HIT_INT_TYPE j = s[i]; j < s[i + 1]; j++) sortgids[j] = gi.gidAt(hits[j].getSid());
102                 std::sort(sortgids, sortgids + num);
103                 if (std::unique(sortgids, sortgids + num) - sortgids > 1) ++res;
104                 delete[] sortgids;
105         }
106
107         return res;
108 }
109
110 template<class HitType>
111 READ_INT_TYPE HitContainer<HitType>::calcNumIsoformMultiReads() {
112         READ_INT_TYPE res = 0;
113         for (READ_INT_TYPE i = 0; i < n; i++)
114                 if (s[i + 1] - s[i] > 1) ++res;
115         return res;
116 }
117
118 #endif /* HITCONTAINER_H_ */