]> git.donarmstrong.com Git - rsem.git/blobdiff - BamWriter.h
Fixed a bug in perl scripts for printing error messages
[rsem.git] / BamWriter.h
index 12ab087d668966f905a495f4e608a777e157ebc0..f664710245ff265e184f922c642c0d3efc74a29a 100644 (file)
@@ -7,6 +7,7 @@
 #include<cassert>
 #include<string>
 #include<sstream>
+#include<iostream>
 
 #include <stdint.h>
 #include "sam/bam.h"
@@ -14,6 +15,8 @@
 #include "sam_rsem_aux.h"
 #include "sam_rsem_cvt.h"
 
+#include "utils.h"
+
 #include "SingleHit.h"
 #include "PairedEndHit.h"
 
@@ -76,28 +79,30 @@ void BamWriter::work(HitWrapper<SingleHit> wrapper) {
        bam1_t *b;
        SingleHit *hit;
 
-       int cnt = 0;
+       HIT_INT_TYPE cnt = 0;
 
        b = bam_init1();
 
        while (samread(in, b) >= 0) {
                ++cnt;
-               if (verbose && cnt % 1000000 == 0) { printf("%d alignment lines are loaded!\n", cnt); }
+               if (verbose && cnt % 1000000 == 0) { std::cout<< cnt<< " alignment lines are loaded!"<< std::endl; }
 
-               if (b->core.flag & 0x0004) continue;
+               if (!(b->core.flag & 0x0004)) {
+                 hit = wrapper.getNextHit();
+                 assert(hit != NULL);
 
-               hit = wrapper.getNextHit();
-               assert(hit != NULL);
+                 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
+                 convert(b, hit->getConPrb());
+               }
 
-               assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
-               convert(b, hit->getConPrb());
-               if (b->core.qual > 0) samwrite(out, b); // output only when MAPQ > 0
+               //if (b->core.qual > 0) samwrite(out, b); // output only when MAPQ > 0
+               samwrite(out, b);
        }
 
        assert(wrapper.getNextHit() == NULL);
 
        bam_destroy1(b);
-       if (verbose) { printf("Bam output file is generated!\n"); }
+       if (verbose) { std::cout<< "Bam output file is generated!"<< std::endl; }
 }
 
 void BamWriter::work(HitWrapper<PairedEndHit> wrapper) {
@@ -111,36 +116,52 @@ void BamWriter::work(HitWrapper<PairedEndHit> wrapper) {
 
        while (samread(in, b) >= 0 && samread(in, b2) >= 0) {
                cnt += 2;
-               if (verbose && cnt % 1000000 == 0) { printf("%d alignment lines are loaded!\n", cnt); }
-
-               //mate info is not complete, skip
-               if (!(((b->core.flag & 0x0040) && (b2->core.flag & 0x0080)) || ((b->core.flag & 0x0080) && (b2->core.flag & 0x0040)))) continue;
-               //unalignable reads, skip
-               if ((b->core.flag & 0x0004) || (b2->core.flag & 0x0004)) continue;
-
-               //swap if b is mate 2
-               if (b->core.flag & 0x0080) {
-                       assert(b2->core.flag & 0x0040);
-                       bam1_t *tmp = b;
-                       b = b2; b2 = tmp;
-               }
+               if (verbose && cnt % 1000000 == 0) { std::cout<< cnt<< "alignment lines are loaded!"<< std::endl; }
+
+               assert((b->core.flag & 0x0001) && (b2->core.flag & 0x0001));
+               assert((b->core.flag & 0x0040) && (b2->core.flag & 0x0080) || (b->core.flag & 0x0080) && (b2->core.flag & 0x0040));
 
-               hit = wrapper.getNextHit();
-               assert(hit != NULL);
+               //unalignable reads, skip               
+               bool notgood = (b->core.flag & 0x0004) || (b2->core.flag & 0x0004);
 
-               assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
-               assert(transcripts.getInternalSid(b2->core.tid + 1) == hit->getSid());
+               if (!notgood) {
+                 //swap if b is mate 2
+                 if (b->core.flag & 0x0080) {
+                   assert(b2->core.flag & 0x0040);
+                   bam1_t *tmp = b;
+                   b = b2; b2 = tmp;
+                 }
 
-               convert(b, hit->getConPrb());
-               convert(b2, hit->getConPrb());
+                 hit = wrapper.getNextHit();
+                 assert(hit != NULL);
 
-               b->core.mpos = b2->core.pos;
-               b2->core.mpos = b->core.pos;
+                 assert(transcripts.getInternalSid(b->core.tid + 1) == hit->getSid());
+                 assert(transcripts.getInternalSid(b2->core.tid + 1) == hit->getSid());
 
-               if (b->core.qual > 0) {
-                       samwrite(out, b);
-                       samwrite(out, b2);
+                 convert(b, hit->getConPrb());
+                 convert(b2, hit->getConPrb());
+
+                 b->core.mpos = b2->core.pos;
+                 b2->core.mpos = b->core.pos;
+               }
+
+               /*
+               else {
+                 // if only one mate can be aligned, mask it as unaligned and put an additional tag Z0:A:!
+                 char exclamation = '!';
+                 if (!(b->core.flag & 0x0004)) { 
+                   b->core.flag |= 0x0004;
+                   bam_aux_append(b, "Z0", 'A', bam_aux_type2size('A'), (uint8_t*)&exclamation);
+                 }
+                 if (!(b2->core.flag & 0x0004)) {
+                   b2->core.flag |= 0x0004;
+                   bam_aux_append(b2, "Z0", 'A', bam_aux_type2size('A'), (uint8_t*)&exclamation);
+                 }
                }
+               */
+
+               samwrite(out, b);
+               samwrite(out, b2);
        }
 
        assert(wrapper.getNextHit() == NULL);
@@ -148,7 +169,7 @@ void BamWriter::work(HitWrapper<PairedEndHit> wrapper) {
        bam_destroy1(b);
        bam_destroy1(b2);
 
-       if (verbose) { printf("Bam output file is generated!\n"); }
+       if (verbose) { std::cout<< "Bam output file is generated!"<< std::endl; }
 }
 
 void BamWriter::convert(bam1_t *b, double prb) {