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 if ((b->core.flag & 0x0004) || (b2->core.flag & 0x0004)) continue;
118 //swap if b is mate 2
119 if (b->core.flag & 0x0080) {
120 assert(b2->core.flag & 0x0040);
125 hit = wrapper.getNextHit();
128 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
129 assert(transcripts.getInternalSid(b2->core.tid + 1) == hit->getSid());
131 convert(b, hit->getConPrb());
132 convert(b2, hit->getConPrb());
134 b->core.mpos = b2->core.pos;
135 b2->core.mpos = b->core.pos;
137 if (b->core.qual > 0) {
143 assert(wrapper.getNextHit() == NULL);
148 if (verbose) { printf("Bam output file is generated!\n"); }
151 void BamWriter::convert(bam1_t *b, double prb) {
152 const Transcript& transcript = transcripts.getTranscriptViaEid(b->core.tid + 1);
154 int pos = b->core.pos;
155 int readlen = b->core.l_qseq;
157 std::vector<uint32_t> data;
160 int core_pos, core_n_cigar;
161 std::vector<Interval> vec;
162 vec.assign(1, Interval(1, transcript.getLength()));
163 // make an artificial chromosome coordinates for the transcript to get new CIGAR strings
164 tr2chr(Transcript("", "", "", '+', vec, ""), pos + 1, pos + readlen, core_pos, core_n_cigar, data);
165 assert(core_pos >= 0);
167 int rest_len = b->data_len - b->core.l_qname - b->core.n_cigar * 4;
168 b->data_len = b->core.l_qname + core_n_cigar * 4 + rest_len;
170 uint8_t* pt = b->data + b->core.l_qname;
171 memmove(pt + core_n_cigar * 4, pt + b->core.n_cigar * 4, rest_len);
172 for (int i = 0; i < core_n_cigar; i++) { memmove(pt, &data[i], 4); pt += 4; }
174 b->core.pos = core_pos;
175 b->core.n_cigar = core_n_cigar;
176 b->core.qual = getMAPQ(prb);
177 b->core.bin = bam_reg2bin(b->core.pos, bam_calend(&(b->core), bam1_cigar(b)));
179 float val = (float)prb;
180 bam_aux_append(b, "ZW", 'f', bam_aux_type2size('f'), (uint8_t*)&val);
183 #endif /* BAMWRITER_H_ */