15 #include "sam_rsem_aux.h"
16 #include "sam_rsem_cvt.h"
20 #include "SingleHit.h"
21 #include "PairedEndHit.h"
23 #include "HitWrapper.h"
24 #include "Transcript.h"
25 #include "Transcripts.h"
29 BamWriter(char, const char*, const char*, const char*, Transcripts&);
32 void work(HitWrapper<SingleHit>);
33 void work(HitWrapper<PairedEndHit>);
36 Transcripts& transcripts;
39 void convert(bam1_t*, double);
43 BamWriter::BamWriter(char inpType, const char* inpF, const char* fn_list, const char* outF, Transcripts& transcripts)
44 : transcripts(transcripts)
47 case 's': in = samopen(inpF, "r", fn_list); break;
48 case 'b': in = samopen(inpF, "rb", fn_list); break;
49 default: assert(false);
53 //build mappings from external sid to internal sid
54 transcripts.buildMappings(in->header->n_targets, in->header->target_name);
56 //generate output's header
57 bam_header_t *out_header = bam_header_dwt(in->header);
58 for (int i = 0; i < out_header->n_targets; i++) {
59 out_header->target_len[i] = transcripts.getTranscriptViaEid(i + 1).getLength(); // transcript length without poly(A) tail
62 std::ostringstream strout;
63 strout<<"@HD\tVN:1.4\tSO:unknown\n@PG\tID:RSEM\n";
64 std::string content = strout.str();
65 append_header_text(out_header, content.c_str(), content.length());
67 out = samopen(outF, "wb", out_header);
70 bam_header_destroy(out_header);
73 BamWriter::~BamWriter() {
78 void BamWriter::work(HitWrapper<SingleHit> wrapper) {
86 while (samread(in, b) >= 0) {
88 if (verbose && cnt % 1000000 == 0) { std::cout<< cnt<< "alignment lines are loaded!"<< std::endl; }
90 if (b->core.flag & 0x0004) continue;
92 hit = wrapper.getNextHit();
95 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
96 convert(b, hit->getConPrb());
97 if (b->core.qual > 0) samwrite(out, b); // output only when MAPQ > 0
100 assert(wrapper.getNextHit() == NULL);
103 if (verbose) { std::cout<< "Bam output file is generated!"<< std::endl; }
106 void BamWriter::work(HitWrapper<PairedEndHit> wrapper) {
115 while (samread(in, b) >= 0 && samread(in, b2) >= 0) {
117 if (verbose && cnt % 1000000 == 0) { std::cout<< cnt<< "alignment lines are loaded!"<< std::endl; }
118 //mate info is not complete, skip
119 if (!(((b->core.flag & 0x0040) && (b2->core.flag & 0x0080)) || ((b->core.flag & 0x0080) && (b2->core.flag & 0x0040)))) continue;
120 //unalignable reads, skip
121 if ((b->core.flag & 0x0004) || (b2->core.flag & 0x0004)) continue;
123 //swap if b is mate 2
124 if (b->core.flag & 0x0080) {
125 assert(b2->core.flag & 0x0040);
130 hit = wrapper.getNextHit();
133 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
134 assert(transcripts.getInternalSid(b2->core.tid + 1) == hit->getSid());
136 convert(b, hit->getConPrb());
137 convert(b2, hit->getConPrb());
139 b->core.mpos = b2->core.pos;
140 b2->core.mpos = b->core.pos;
142 if (b->core.qual > 0) {
148 assert(wrapper.getNextHit() == NULL);
153 if (verbose) { std::cout<< "Bam output file is generated!"<< std::endl; }
156 void BamWriter::convert(bam1_t *b, double prb) {
157 const Transcript& transcript = transcripts.getTranscriptViaEid(b->core.tid + 1);
159 int pos = b->core.pos;
160 int readlen = b->core.l_qseq;
162 std::vector<uint32_t> data;
165 int core_pos, core_n_cigar;
166 std::vector<Interval> vec;
167 vec.assign(1, Interval(1, transcript.getLength()));
168 // make an artificial chromosome coordinates for the transcript to get new CIGAR strings
169 tr2chr(Transcript("", "", "", '+', vec, ""), 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.qual = getMAPQ(prb);
182 b->core.bin = bam_reg2bin(b->core.pos, bam_calend(&(b->core), bam1_cigar(b)));
184 float val = (float)prb;
185 bam_aux_append(b, "ZW", 'f', bam_aux_type2size('f'), (uint8_t*)&val);
188 #endif /* BAMWRITER_H_ */