question: public static void appendMDString ( final String alignedQuery , final String alignedReference , final StringBuilder buffer ) { buffer . append ( "MD:Z:" ) ; int countMatches = 0 ; boolean inDeletion = false ; for ( int i = 0 ; i < alignedQuery . length ( ) ; i ++ ) { final char qChar = alignedQuery . charAt ( i ) ; final char rChar = alignedReference . charAt ( i ) ; if ( qChar == '-' ) { if ( countMatches > 0 ) { buffer . append ( countMatches ) ; countMatches = 0 ; } if ( ! inDeletion ) { buffer . append ( "^" ) ; inDeletion = true ; } buffer . append ( rChar ) ; } else if ( rChar != '-' ) { if ( qChar == rChar ) { countMatches ++ ; } else { if ( inDeletion ) buffer . append ( "0" ) ; if ( countMatches > 0 ) { buffer . append ( countMatches ) ; countMatches = 0 ; } buffer . append ( rChar ) ; } if ( inDeletion ) inDeletion = false ; } } if ( countMatches > 0 ) buffer . append ( countMatches ) ; else if ( inDeletion ) buffer . append ( 0 ) ; }
target:  append the MD string