/** * Convert from a byte array containing =AaCcGgTtNn represented as ASCII, to * a byte array half as long, with =, A, C, G, T converted to 0, 1, 2, 4, 8, * 15. * * @param readBases * Bases as ASCII bytes. * @return New byte array with bases represented as nybbles, in BAM binary * format. */ private static byte[] bytesToCompressedBases(final byte[] readBases) { final byte[] compressedBases = new byte[(readBases.length + 1) / 2]; int i; for (i = 1; i < readBases.length; i += 2) { compressedBases[i / 2] = (byte) (charToCompressedBaseHigh(readBases[i - 1]) | charToCompressedBaseLow(readBases[i])); } // Last nybble if (i == readBases.length) { compressedBases[i / 2] = charToCompressedBaseHigh((char) readBases[i - 1]); } return compressedBases; }
public void setBases(byte[] bases, int offset, int length) { if (BASES < 0) throw new RuntimeException("Premature setting of bases."); int i; for (i = 1; i < length; i += 2) buf[start + BASES + i / 2] = (byte) (charToCompressedBaseHigh(bases[offset + i - 1]) | charToCompressedBaseLow(bases[offset + i])); // Last nybble if (i == length) buf[start + BASES + i / 2] = charToCompressedBaseHigh((char) bases[offset + i - 1]); setReadLength(length); SCORES = BASES + length / 2 + length % 2; }