private static byte[] GetEncodedSequence(SAMAlignedSequence alignedSeq)
{
List<byte> byteList = new List<byte>();
ISequence seq = alignedSeq.QuerySequence;
if (seq != null)
{
if (!(seq.Alphabet is DnaAlphabet))
{
throw new ArgumentException(Properties.Resource.BAMFormatterSupportsDNAOnly);
}
byte[] symbolMap = seq.Alphabet.GetSymbolValueMap();
for (int i = 0; i < seq.Count; i++)
{
char symbol = (char)symbolMap[seq[i]];
byte encodedvalue = 0;
// 4-bit encoded read: =ACMGRSVTWYHKDBN -> 0-15; the earlier base is stored in the
// high-order 4 bits of the byte.
//Note:
// All the other symbols which are not supported by BAM specification (other than "=ACMGRSVTWYHKDBN") are converted to 'N'
// for example a '.' symbol which is supported by SAM specification will be converted to symbol 'N'
switch (symbol)
{
case '=':
encodedvalue = 0;
break;
case 'A':
encodedvalue = 1;
break;
case 'C':
encodedvalue = 2;
break;
case 'M':
encodedvalue = 3;
break;
case 'G':
encodedvalue = 4;
break;
case 'R':
encodedvalue = 5;
break;
case 'S':
encodedvalue = 6;
break;
case 'V':
encodedvalue = 7;
break;
case 'T':
encodedvalue = 8;
break;
case 'W':
encodedvalue = 9;
break;
case 'Y':
encodedvalue = 10;
break;
case 'H':
encodedvalue = 11;
break;
case 'K':
encodedvalue = 12;
break;
case 'D':
encodedvalue = 13;
break;
case 'B':
encodedvalue = 14;
break;
default:
encodedvalue = 15;
break;
}
if ((i + 1) % 2 > 0)
{
byteList.Add((byte)(encodedvalue << 4));
}
else
{
byteList[byteList.Count - 1] = (byte)(byteList[byteList.Count - 1] | encodedvalue);
}
}
}
return byteList.ToArray();
}