1 #ifndef BAMCONVERTER_H_
2 #define BAMCONVERTER_H_
13 #include "sam_rsem_aux.h"
14 #include "sam_rsem_cvt.h"
17 #include "my_assert.h"
19 #include "Transcript.h"
20 #include "Transcripts.h"
24 BamConverter(const char*, const char*, const char*, Transcripts&);
30 Transcripts& transcripts;
32 std::map<std::string, int> refmap;
33 std::map<std::string, int>::iterator iter;
35 CollapseMap collapseMap;
37 void convert(bam1_t*, const Transcript&);
39 void writeCollapsedLines();
40 void flipSeq(uint8_t*, int);
41 void flipQual(uint8_t*, int);
42 void addXSTag(bam1_t*, const Transcript&);
45 BamConverter::BamConverter(const char* inpF, const char* outF, const char* chr_list, Transcripts& transcripts)
46 : transcripts(transcripts)
48 general_assert(transcripts.getType() == 0, "Genome information is not provided! RSEM cannot convert the transcript bam file!");
50 in = samopen(inpF, "rb", NULL);
53 transcripts.buildMappings(in->header->n_targets, in->header->target_name);
55 bam_header_t *out_header = sam_header_read2(chr_list);
57 for (int i = 0; i < out_header->n_targets; i++) {
58 refmap[out_header->target_name[i]] = i;
61 append_header_text(out_header, in->header->text, in->header->l_text);
63 out = samopen(outF, "wb", out_header);
66 bam_header_destroy(out_header);
69 BamConverter::~BamConverter() {
74 void BamConverter::process() {
77 bool isPaired = false;
82 b = bam_init1(); b2 = bam_init1();
84 while (samread(in, b) >= 0) {
86 isPaired = (b->core.flag & 0x0001) > 0;
88 assert(samread(in, b2) >= 0 && (b2->core.flag & 0x0001));
89 assert((b->core.flag & 0x0001) && (b2->core.flag & 0x0001));
90 assert(((b->core.flag & 0x0040) && (b2->core.flag & 0x0080)) || ((b->core.flag & 0x0080) && (b2->core.flag & 0x0040)));
94 if (cnt % 1000000 == 0) { printf("."); fflush(stdout); }
96 // at least one segment is not properly mapped
97 bool notgood = (b->core.flag & 0x0004) || (isPaired && (b2->core.flag & 0x0004));
100 if (isPaired && notgood) assert((b->core.flag & 0x0004) && (b2->core.flag & 0x0004));
104 if (isPaired) assert((b->core.tid == b2->core.tid) && (b->core.flag & 0x0040) && (b2->core.flag & 0x0080)); // for collapsing
106 const Transcript& transcript = transcripts.getTranscriptViaEid(b->core.tid + 1);
108 convert(b, transcript);
110 convert(b2, transcript);
111 b->core.mpos = b2->core.pos;
112 b2->core.mpos = b->core.pos;
115 if (cqname != bam1_qname(b)) {
116 writeCollapsedLines();
117 cqname = bam1_qname(b);
118 collapseMap.init(isPaired);
120 collapseMap.insert(b, b2, bam_aux2f(bam_aux_get(b, "ZW")));
123 assert(cqname != bam1_qname(b));
125 writeCollapsedLines();
126 cqname = bam1_qname(b);
127 collapseMap.init(isPaired);
130 if (isPaired) samwrite(out, b2);
135 writeCollapsedLines();
140 if (cnt >= 1000000) printf("\n");
143 void BamConverter::convert(bam1_t* b, const Transcript& transcript) {
144 int pos = b->core.pos;
145 int readlen = b->core.l_qseq;
147 general_assert(readlen > 0, "One alignment line has SEQ field as *. RSEM does not support this currently!");
149 iter = refmap.find(transcript.getSeqName());
150 assert(iter != refmap.end());
151 b->core.tid = iter->second;
152 if (b->core.flag & 0x0001) { b->core.mtid = b->core.tid; }
153 b->core.qual = 255; // set to not available temporarily
155 if (transcript.getStrand() == '-') {
156 b->core.flag ^= 0x0010;
157 if (b->core.flag & 0x0001) {
158 b->core.flag ^= 0x0020;
159 b->core.isize = -b->core.isize;
161 flipSeq(bam1_seq(b), readlen);
162 flipQual(bam1_qual(b), readlen);
165 std::vector<uint32_t> data;
168 int core_pos, core_n_cigar;
169 tr2chr(transcript, pos + 1, pos + readlen, core_pos, core_n_cigar, data);
170 assert(core_pos >= 0);
172 int rest_len = b->data_len - b->core.l_qname - b->core.n_cigar * 4;
173 b->data_len = b->core.l_qname + core_n_cigar * 4 + rest_len;
175 uint8_t* pt = b->data + b->core.l_qname;
176 memmove(pt + core_n_cigar * 4, pt + b->core.n_cigar * 4, rest_len);
177 for (int i = 0; i < core_n_cigar; i++) { memmove(pt, &data[i], 4); pt += 4; }
179 b->core.pos = core_pos;
180 b->core.n_cigar = core_n_cigar;
181 b->core.bin = bam_reg2bin(b->core.pos, bam_calend(&(b->core), bam1_cigar(b)));
183 addXSTag(b, transcript); // check if need to add XS tag, if need, add it
186 inline void BamConverter::writeCollapsedLines() {
187 bam1_t *tmp_b = NULL,*tmp_b2 = NULL;
191 if (!collapseMap.empty(isPaired)) {
192 while (collapseMap.next(tmp_b, tmp_b2, prb)) {
193 memcpy(bam_aux_get(tmp_b, "ZW") + 1, (uint8_t*)&(prb), bam_aux_type2size('f'));
194 tmp_b->core.qual = getMAPQ(prb);
195 samwrite(out, tmp_b);
197 memcpy(bam_aux_get(tmp_b2, "ZW") + 1, (uint8_t*)&(prb), bam_aux_type2size('f'));
198 tmp_b2->core.qual = tmp_b->core.qual;
199 samwrite(out, tmp_b2);
202 if (isPaired) bam_destroy1(tmp_b2);
207 inline void BamConverter::flipSeq(uint8_t* s, int readlen) {
209 std::vector<uint8_t> seq;
213 for (int i = 0; i < readlen; i++) {
214 switch (bam1_seqi(s, readlen - i - 1)) {
215 case 1: base = 8; break;
216 case 2: base = 4; break;
217 case 4: base = 2; break;
218 case 8: base = 1; break;
219 case 15: base = 15; break;
220 default: assert(false);
222 code |= base << (4 * (1 - i % 2));
223 if (i % 2 == 1) { seq.push_back(code); code = 0; }
225 if (readlen % 2 == 1) { seq.push_back(code); }
227 for (int i = 0; i < (int)seq.size(); i++) s[i] = seq[i];
230 inline void BamConverter::flipQual(uint8_t* q, int readlen) {
231 int32_t mid = readlen / 2;
233 for (int i = 0; i < mid; i++) {
234 tmp = q[i]; q[i] = q[readlen - i - 1]; q[readlen -i -1] = tmp;
238 inline void BamConverter::addXSTag(bam1_t* b, const Transcript& transcript) {
239 uint32_t* p = bam1_cigar(b);
241 for (int i = 0; i < (int)b->core.n_cigar; i++)
242 if ((*(p + i) & BAM_CIGAR_MASK) == BAM_CREF_SKIP) { hasN = true; break; }
244 char strand = transcript.getStrand();
245 bam_aux_append(b, "XS", 'A', 1, (uint8_t*)&strand);
248 #endif /* BAMCONVERTER_H_ */