I have a SAM file with alignments and for each entry alignment, I want to reconstruct the alignment between the reference and the read based on the CIGAR and MD strings. It seems like this should be possible, but this one example bothers me:

    SRR037452.3355  0       ENSG00000266658|ENST00000607521 3523    255     16M1I18M        *       0       0       CGGGCCGGTCCCCCCCCGCCGGGTCCGCCCCCGGC     IIIIIIIIIIIIIII:III/IIII=+IGC,I"/I. NH:i:1  HI:i:1  NM:i:2  MD:Z:33G0

Here, the CIGAR string has an insertion to the reference which messes up the MD string indexing. According to MD string, the read should be only 34 bases long (r=35). My guess is that the alignment is actually this: 16 matches, 1 base that is present in the read, not present in the reference, 17 matches, one mismatch where the read has a "G" and the reference has something else. Is that correct? Are there CIGAR/MD string combinations where reconstructing the alignment would be impossible (i.e. either of the strings is ambiguous)?
<p>Your guess is correct, the alignment has 16 matches, 1 unspecified insertion, followed by 17 matches and one mismatch (&#39;G&#39; on the reference which is substituted with a &#39;C&#39; in the read sequence).</p>

<p>If the MD tag is present, you should always be able to reconstruct the alignment from the MD tag and the CIGAR, as long as you don&#39;t care about the sequence of any mismatched or inserted bases (which you can only get from the read sequence).</p>
