14 #include "sam_rsem_aux.h"
15 #include "sam_rsem_cvt.h"
17 #include "SingleHit.h"
18 #include "PairedEndHit.h"
20 #include "HitWrapper.h"
21 #include "Transcript.h"
22 #include "Transcripts.h"
26 BamWriter(char, const char*, const char*, const char*, Transcripts&);
29 void work(HitWrapper<SingleHit>);
30 void work(HitWrapper<PairedEndHit>);
33 Transcripts& transcripts;
36 void convert(bam1_t*, double);
40 BamWriter::BamWriter(char inpType, const char* inpF, const char* fn_list, const char* outF, Transcripts& transcripts)
41 : transcripts(transcripts)
44 case 's': in = samopen(inpF, "r", fn_list); break;
45 case 'b': in = samopen(inpF, "rb", fn_list); break;
46 default: assert(false);
50 //build mappings from external sid to internal sid
51 transcripts.buildMappings(in->header->n_targets, in->header->target_name);
53 //generate output's header
54 bam_header_t *out_header = bam_header_dwt(in->header);
55 for (int i = 0; i < out_header->n_targets; i++) {
56 out_header->target_len[i] = transcripts.getTranscriptViaEid(i + 1).getLength(); // transcript length without poly(A) tail
59 std::ostringstream strout;
60 strout<<"@HD\tVN:1.4\tSO:unknown\n@PG\tID:RSEM\n";
61 std::string content = strout.str();
62 append_header_text(out_header, content.c_str(), content.length());
64 out = samopen(outF, "wb", out_header);
67 bam_header_destroy(out_header);
70 BamWriter::~BamWriter() {
75 void BamWriter::work(HitWrapper<SingleHit> wrapper) {
83 while (samread(in, b) >= 0) {
85 if (verbose && cnt % 1000000 == 0) { printf("%d alignment lines are loaded!\n", cnt); }
87 if (b->core.flag & 0x0004) continue;
89 hit = wrapper.getNextHit();
92 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
93 convert(b, hit->getConPrb());
94 if (b->core.qual > 0) samwrite(out, b); // output only when MAPQ > 0
97 assert(wrapper.getNextHit() == NULL);
100 if (verbose) { printf("Bam output file is generated!\n"); }
103 void BamWriter::work(HitWrapper<PairedEndHit> wrapper) {
112 while (samread(in, b) >= 0 && samread(in, b2) >= 0) {
114 if (verbose && cnt % 1000000 == 0) { printf("%d alignment lines are loaded!\n", cnt); }
116 //mate info is not complete, skip
117 if (!(((b->core.flag & 0x0040) && (b2->core.flag & 0x0080)) || ((b->core.flag & 0x0080) && (b2->core.flag & 0x0040)))) continue;
118 //unalignable reads, skip
119 if ((b->core.flag & 0x0004) || (b2->core.flag & 0x0004)) continue;
121 //swap if b is mate 2
122 if (b->core.flag & 0x0080) {
123 assert(b2->core.flag & 0x0040);
128 hit = wrapper.getNextHit();
131 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
132 assert(transcripts.getInternalSid(b2->core.tid + 1) == hit->getSid());
134 convert(b, hit->getConPrb());
135 convert(b2, hit->getConPrb());
137 b->core.mpos = b2->core.pos;
138 b2->core.mpos = b->core.pos;
140 if (b->core.qual > 0) {
146 assert(wrapper.getNextHit() == NULL);
151 if (verbose) { printf("Bam output file is generated!\n"); }
154 void BamWriter::convert(bam1_t *b, double prb) {
155 const Transcript& transcript = transcripts.getTranscriptViaEid(b->core.tid + 1);
157 int pos = b->core.pos;
158 int readlen = b->core.l_qseq;
160 std::vector<uint32_t> data;
163 int core_pos, core_n_cigar;
164 std::vector<Interval> vec;
165 vec.assign(1, Interval(1, transcript.getLength()));
166 // make an artificial chromosome coordinates for the transcript to get new CIGAR strings
167 tr2chr(Transcript("", "", "", '+', vec, ""), pos + 1, pos + readlen, core_pos, core_n_cigar, data);
168 assert(core_pos >= 0);
170 int rest_len = b->data_len - b->core.l_qname - b->core.n_cigar * 4;
171 b->data_len = b->core.l_qname + core_n_cigar * 4 + rest_len;
173 uint8_t* pt = b->data + b->core.l_qname;
174 memmove(pt + core_n_cigar * 4, pt + b->core.n_cigar * 4, rest_len);
175 for (int i = 0; i < core_n_cigar; i++) { memmove(pt, &data[i], 4); pt += 4; }
177 b->core.pos = core_pos;
178 b->core.n_cigar = core_n_cigar;
179 b->core.qual = getMAPQ(prb);
180 b->core.bin = bam_reg2bin(b->core.pos, bam_calend(&(b->core), bam1_cigar(b)));
182 float val = (float)prb;
183 bam_aux_append(b, "ZW", 'f', bam_aux_type2size('f'), (uint8_t*)&val);
186 #endif /* BAMWRITER_H_ */