Tuesday, May 4, 2010

samtools, bam cigar to string cigar

This code fragment converts a bam style binary cigar string in to a normal cigar string.

    CigarLen = b->core.n_cigar;  // Cigar length
uint32_t *cigar = bam1_cigar(b);

Cigar.clear();
for(size_t n=0;n<cigar_len;n++) {
uint32_t opcode = cigar[n] & 0x0000000F;
uint32_t len = cigar[n] & 0xFFFFFFF0;

len = len >> 4;

if(opcode == 0) { Cigar += stringify(len); Cigar += "M"; }
if(opcode == 1) { Cigar += stringify(len); Cigar += "I"; }
if(opcode == 2) { Cigar += stringify(len); Cigar += "D"; }
if(opcode == 3) { Cigar += stringify(len); Cigar += "N"; }
if(opcode == 4) { Cigar += stringify(len); Cigar += "S"; }
if(opcode == 5) { Cigar += stringify(len); Cigar += "H"; }
if(opcode == 6) { Cigar += stringify(len); Cigar += "P"; }
if(opcode == 7) { Cigar += stringify(len); Cigar += "a"; }
if(opcode == 8) { Cigar += stringify(len); Cigar += "b"; }
if(opcode == 9) { Cigar += stringify(len); Cigar += "c"; }
if(opcode == 10) { Cigar += stringify(len); Cigar += "d"; }
if(opcode == 11) { Cigar += stringify(len); Cigar += "e"; }
if(opcode == 12) { Cigar += stringify(len); Cigar += "f"; }
if(opcode == 13) { Cigar += stringify(len); Cigar += "g"; }
if(opcode == 14) { Cigar += stringify(len); Cigar += "h"; }
}
}


No comments: