src/hg/lib/bamFile.c 1.8

1.8 2009/09/23 23:50:30 angie
Added bamShowFlagsEnglish to explain the flag bits.
Index: src/hg/lib/bamFile.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/bamFile.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -b -B -U 4 -r1.7 -r1.8
--- src/hg/lib/bamFile.c	14 Sep 2009 23:44:25 -0000	1.7
+++ src/hg/lib/bamFile.c	23 Sep 2009 23:50:30 -0000	1.8
@@ -151,8 +151,43 @@
 	}
     }
 }
 
+static void descFlag(unsigned flag, unsigned bitMask, char *desc, boolean makeRed,
+	      boolean *retFirst)
+/* Describe a flag bit (or multi-bit mask) if it is set in flag. */
+{
+if ((flag & bitMask) == bitMask) // *all* bits in bitMask are set in flag
+    {
+    if (!*retFirst)
+	printf(" | ");
+    printf("<span%s>(<TT>0x%02x</TT>) %s</span>",
+	   (makeRed ? " style='color: red'" : ""), bitMask, desc);
+    *retFirst = FALSE;
+    }
+}
+
+void bamShowFlagsEnglish(const bam1_t *bam)
+/* Print out flags in English, e.g. "Mate is on '-' strand; Properly paired". */
+{
+const bam1_core_t *core = &bam->core;
+unsigned flag = core->flag;
+boolean first = TRUE;
+descFlag(flag, BAM_FDUP, "Optical or PCR duplicate", TRUE, &first);
+descFlag(flag, BAM_FQCFAIL, "QC failure", TRUE, &first);
+descFlag(flag, BAM_FSECONDARY, "Not primary alignment", TRUE, &first);
+descFlag(flag, BAM_FREAD2, "Read 2 of pair", FALSE, &first);
+descFlag(flag, BAM_FREAD1, "Read 1 of pair", FALSE, &first);
+descFlag(flag, BAM_FMREVERSE, "Mate is on '-' strand", FALSE, &first);
+descFlag(flag, BAM_FREVERSE, "Read is on '-' strand", FALSE, &first);
+descFlag(flag, BAM_FMUNMAP, "Mate is unmapped", TRUE, &first);
+if (flag & BAM_FUNMAP)
+    errAbort("Read is unmapped (what is it doing here?!?)");
+descFlag(flag, (BAM_FPROPER_PAIR | BAM_FPAIRED), "Properly paired", FALSE, &first);
+if ((flag & BAM_FPAIRED) && !(flag & BAM_FPROPER_PAIR))
+    descFlag(flag, BAM_FPAIRED, "Not properly paired", TRUE, &first);
+}
+
 int bamGetTargetLength(const bam1_t *bam)
 /* Tally up the alignment's length on the reference sequence from
  * bam's packed-int CIGAR representation. */
 {