1 // ***************************************************************************
2 // BamWriter_p.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 19 April 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides the basic functionality for producing BAM files
9 // ***************************************************************************
11 #include <api/BamAlignment.h>
12 #include <api/BamConstants.h>
13 #include <api/internal/BamWriter_p.h>
14 using namespace BamTools;
15 using namespace BamTools::Internal;
23 BamWriterPrivate::BamWriterPrivate(void)
24 : m_isBigEndian( BamTools::SystemIsBigEndian() )
28 BamWriterPrivate::~BamWriterPrivate(void) {
32 // calculates minimum bin for a BAM alignment interval
33 unsigned int BamWriterPrivate::CalculateMinimumBin(const int begin, int end) const {
35 if ( (begin >> 14) == (end >> 14) ) return 4681 + (begin >> 14);
36 if ( (begin >> 17) == (end >> 17) ) return 585 + (begin >> 17);
37 if ( (begin >> 20) == (end >> 20) ) return 73 + (begin >> 20);
38 if ( (begin >> 23) == (end >> 23) ) return 9 + (begin >> 23);
39 if ( (begin >> 26) == (end >> 26) ) return 1 + (begin >> 26);
43 // closes the alignment archive
44 void BamWriterPrivate::Close(void) {
48 // creates a cigar string from the supplied alignment
49 void BamWriterPrivate::CreatePackedCigar(const vector<CigarOp>& cigarOperations, string& packedCigar) {
52 const unsigned int numCigarOperations = cigarOperations.size();
53 packedCigar.resize(numCigarOperations * Constants::BAM_SIZEOF_INT);
55 // pack the cigar data into the string
56 unsigned int* pPackedCigar = (unsigned int*)packedCigar.data();
58 // iterate over cigar operations
59 vector<CigarOp>::const_iterator coIter = cigarOperations.begin();
60 vector<CigarOp>::const_iterator coEnd = cigarOperations.end();
61 for ( ; coIter != coEnd; ++coIter ) {
63 // store op in packedCigar
65 switch ( coIter->Type ) {
66 case (Constants::BAM_CIGAR_MATCH_CHAR) : cigarOp = Constants::BAM_CIGAR_MATCH; break;
67 case (Constants::BAM_CIGAR_INS_CHAR) : cigarOp = Constants::BAM_CIGAR_INS; break;
68 case (Constants::BAM_CIGAR_DEL_CHAR) : cigarOp = Constants::BAM_CIGAR_DEL; break;
69 case (Constants::BAM_CIGAR_REFSKIP_CHAR) : cigarOp = Constants::BAM_CIGAR_REFSKIP; break;
70 case (Constants::BAM_CIGAR_SOFTCLIP_CHAR) : cigarOp = Constants::BAM_CIGAR_SOFTCLIP; break;
71 case (Constants::BAM_CIGAR_HARDCLIP_CHAR) : cigarOp = Constants::BAM_CIGAR_HARDCLIP; break;
72 case (Constants::BAM_CIGAR_PAD_CHAR) : cigarOp = Constants::BAM_CIGAR_PAD; break;
73 case (Constants::BAM_CIGAR_SEQMATCH_CHAR) : cigarOp = Constants::BAM_CIGAR_SEQMATCH; break;
74 case (Constants::BAM_CIGAR_MISMATCH_CHAR) : cigarOp = Constants::BAM_CIGAR_MISMATCH; break;
76 fprintf(stderr, "BamWriter ERROR: unknown cigar operation found: %c\n", coIter->Type);
80 *pPackedCigar = coIter->Length << Constants::BAM_CIGAR_SHIFT | cigarOp;
85 // encodes the supplied query sequence into 4-bit notation
86 void BamWriterPrivate::EncodeQuerySequence(const string& query, string& encodedQuery) {
88 // prepare the encoded query string
89 const unsigned int queryLen = query.size();
90 const unsigned int encodedQueryLen = (unsigned int)((queryLen / 2.0) + 0.5);
91 encodedQuery.resize(encodedQueryLen);
92 char* pEncodedQuery = (char*)encodedQuery.data();
93 const char* pQuery = (const char*)query.data();
95 unsigned char nucleotideCode;
96 bool useHighWord = true;
100 case (Constants::BAM_DNA_EQUAL) : nucleotideCode = Constants::BAM_BASECODE_EQUAL; break;
101 case (Constants::BAM_DNA_A) : nucleotideCode = Constants::BAM_BASECODE_A; break;
102 case (Constants::BAM_DNA_C) : nucleotideCode = Constants::BAM_BASECODE_C; break;
103 case (Constants::BAM_DNA_G) : nucleotideCode = Constants::BAM_BASECODE_G; break;
104 case (Constants::BAM_DNA_T) : nucleotideCode = Constants::BAM_BASECODE_T; break;
105 case (Constants::BAM_DNA_N) : nucleotideCode = Constants::BAM_BASECODE_N; break;
107 fprintf(stderr, "BamWriter ERROR: only the following bases are supported in the BAM format: {=, A, C, G, T, N}. Found [%c]\n", *pQuery);
111 // pack the nucleotide code
113 *pEncodedQuery = nucleotideCode << 4;
116 *pEncodedQuery |= nucleotideCode;
121 // increment the query position
126 // returns whether BAM file is open for writing or not
127 bool BamWriterPrivate::IsOpen(void) const {
128 return m_stream.IsOpen;
131 // opens the alignment archive
132 bool BamWriterPrivate::Open(const string& filename,
133 const string& samHeaderText,
134 const RefVector& referenceSequences)
136 // open the BGZF file for writing, return failure if error
137 if ( !m_stream.Open(filename, "wb") )
140 // write BAM file 'metadata' components
142 WriteSamHeaderText(samHeaderText);
143 WriteReferences(referenceSequences);
147 // saves the alignment to the alignment archive
148 void BamWriterPrivate::SaveAlignment(const BamAlignment& al) {
150 // if BamAlignment contains only the core data and a raw char data buffer
151 // (as a result of BamReader::GetNextAlignmentCore())
152 if ( al.SupportData.HasCoreOnly ) {
154 // write the block size
155 unsigned int blockSize = al.SupportData.BlockLength;
156 if ( m_isBigEndian ) BamTools::SwapEndian_32(blockSize);
157 m_stream.Write((char*)&blockSize, Constants::BAM_SIZEOF_INT);
159 // assign the BAM core data
160 uint32_t buffer[Constants::BAM_CORE_BUFFER_SIZE];
161 buffer[0] = al.RefID;
162 buffer[1] = al.Position;
163 buffer[2] = (al.Bin << 16) | (al.MapQuality << 8) | al.SupportData.QueryNameLength;
164 buffer[3] = (al.AlignmentFlag << 16) | al.SupportData.NumCigarOperations;
165 buffer[4] = al.SupportData.QuerySequenceLength;
166 buffer[5] = al.MateRefID;
167 buffer[6] = al.MatePosition;
168 buffer[7] = al.InsertSize;
170 // swap BAM core endian-ness, if necessary
171 if ( m_isBigEndian ) {
172 for ( int i = 0; i < 8; ++i )
173 BamTools::SwapEndian_32(buffer[i]);
176 // write the BAM core
177 m_stream.Write((char*)&buffer, Constants::BAM_CORE_SIZE);
179 // write the raw char data
180 m_stream.Write((char*)al.SupportData.AllCharData.data(),
181 al.SupportData.BlockLength-Constants::BAM_CORE_SIZE);
184 // otherwise, BamAlignment should contain character in the standard fields: Name, QueryBases, etc
185 // ( resulting from BamReader::GetNextAlignment() *OR* being generated directly by client code )
188 // calculate char lengths
189 const unsigned int nameLength = al.Name.size() + 1;
190 const unsigned int numCigarOperations = al.CigarData.size();
191 const unsigned int queryLength = al.QueryBases.size();
192 const unsigned int tagDataLength = al.TagData.size();
194 // no way to tell if BamAlignment.Bin is already defined (no default, invalid value)
195 // force calculation of Bin before storing
196 const int endPosition = al.GetEndPosition();
197 const unsigned int alignmentBin = CalculateMinimumBin(al.Position, endPosition);
199 // create our packed cigar string
201 CreatePackedCigar(al.CigarData, packedCigar);
202 const unsigned int packedCigarLength = packedCigar.size();
206 EncodeQuerySequence(al.QueryBases, encodedQuery);
207 const unsigned int encodedQueryLength = encodedQuery.size();
209 // write the block size
210 const unsigned int dataBlockSize = nameLength +
215 unsigned int blockSize = Constants::BAM_CORE_SIZE + dataBlockSize;
216 if ( m_isBigEndian ) BamTools::SwapEndian_32(blockSize);
217 m_stream.Write((char*)&blockSize, Constants::BAM_SIZEOF_INT);
219 // assign the BAM core data
220 uint32_t buffer[Constants::BAM_CORE_BUFFER_SIZE];
221 buffer[0] = al.RefID;
222 buffer[1] = al.Position;
223 buffer[2] = (alignmentBin << 16) | (al.MapQuality << 8) | nameLength;
224 buffer[3] = (al.AlignmentFlag << 16) | numCigarOperations;
225 buffer[4] = queryLength;
226 buffer[5] = al.MateRefID;
227 buffer[6] = al.MatePosition;
228 buffer[7] = al.InsertSize;
230 // swap BAM core endian-ness, if necessary
231 if ( m_isBigEndian ) {
232 for ( int i = 0; i < 8; ++i )
233 BamTools::SwapEndian_32(buffer[i]);
236 // write the BAM core
237 m_stream.Write((char*)&buffer, Constants::BAM_CORE_SIZE);
239 // write the query name
240 m_stream.Write(al.Name.c_str(), nameLength);
242 // write the packed cigar
243 if ( m_isBigEndian ) {
244 char* cigarData = (char*)calloc(sizeof(char), packedCigarLength);
245 memcpy(cigarData, packedCigar.data(), packedCigarLength);
246 if ( m_isBigEndian ) {
247 for ( unsigned int i = 0; i < packedCigarLength; ++i )
248 BamTools::SwapEndian_32p(&cigarData[i]);
250 m_stream.Write(cigarData, packedCigarLength);
254 m_stream.Write(packedCigar.data(), packedCigarLength);
256 // write the encoded query sequence
257 m_stream.Write(encodedQuery.data(), encodedQueryLength);
259 // write the base qualities
260 char* pBaseQualities = (char*)al.Qualities.data();
261 for ( unsigned int i = 0; i < queryLength; i++ )
262 pBaseQualities[i] -= 33; // FASTQ conversion
263 m_stream.Write(pBaseQualities, queryLength);
265 // write the read group tag
266 if ( m_isBigEndian ) {
268 char* tagData = (char*)calloc(sizeof(char), tagDataLength);
269 memcpy(tagData, al.TagData.data(), tagDataLength);
272 while ( (unsigned int)i < tagDataLength ) {
274 i += Constants::BAM_TAG_TAGSIZE; // skip tag chars (e.g. "RG", "NM", etc.)
275 const char type = tagData[i]; // get tag type at position i
280 case(Constants::BAM_TAG_TYPE_ASCII) :
281 case(Constants::BAM_TAG_TYPE_INT8) :
282 case(Constants::BAM_TAG_TYPE_UINT8) :
286 case(Constants::BAM_TAG_TYPE_INT16) :
287 case(Constants::BAM_TAG_TYPE_UINT16) :
288 BamTools::SwapEndian_16p(&tagData[i]);
289 i += sizeof(uint16_t);
292 case(Constants::BAM_TAG_TYPE_FLOAT) :
293 case(Constants::BAM_TAG_TYPE_INT32) :
294 case(Constants::BAM_TAG_TYPE_UINT32) :
295 BamTools::SwapEndian_32p(&tagData[i]);
296 i += sizeof(uint32_t);
299 case(Constants::BAM_TAG_TYPE_HEX) :
300 case(Constants::BAM_TAG_TYPE_STRING) :
301 // no endian swapping necessary for hex-string/string data
304 // increment one more for null terminator
308 case(Constants::BAM_TAG_TYPE_ARRAY) :
312 const char arrayType = tagData[i];
315 // swap endian-ness of number of elements in place, then retrieve for loop
316 BamTools::SwapEndian_32p(&tagData[i]);
318 memcpy(&numElements, &tagData[i], sizeof(uint32_t));
319 i += sizeof(uint32_t);
321 // swap endian-ness of array elements
322 for ( int j = 0; j < numElements; ++j ) {
324 case (Constants::BAM_TAG_TYPE_INT8) :
325 case (Constants::BAM_TAG_TYPE_UINT8) :
326 // no endian-swapping necessary
329 case (Constants::BAM_TAG_TYPE_INT16) :
330 case (Constants::BAM_TAG_TYPE_UINT16) :
331 BamTools::SwapEndian_16p(&tagData[i]);
332 i += sizeof(uint16_t);
334 case (Constants::BAM_TAG_TYPE_FLOAT) :
335 case (Constants::BAM_TAG_TYPE_INT32) :
336 case (Constants::BAM_TAG_TYPE_UINT32) :
337 BamTools::SwapEndian_32p(&tagData[i]);
338 i += sizeof(uint32_t);
343 "BamWriter ERROR: unknown binary array type encountered: [%c]\n",
353 fprintf(stderr, "BamWriter ERROR: invalid tag value type\n"); // shouldn't get here
358 m_stream.Write(tagData, tagDataLength);
362 m_stream.Write(al.TagData.data(), tagDataLength);
366 void BamWriterPrivate::SetWriteCompressed(bool ok) {
368 // warn if BAM file is already open
369 // modifying compression is not allowed in this case
371 cerr << "BamWriter WARNING: attempting to change compression mode on an open BAM file is not allowed. "
372 << "Ignoring request." << endl;
376 // set BgzfStream compression mode
377 m_stream.SetWriteCompressed(ok);
380 void BamWriterPrivate::WriteMagicNumber(void) {
381 // write BAM file 'magic number'
382 m_stream.Write(Constants::BAM_HEADER_MAGIC, Constants::BAM_HEADER_MAGIC_LENGTH);
385 void BamWriterPrivate::WriteReferences(const BamTools::RefVector& referenceSequences) {
387 // write the number of reference sequences
388 uint32_t numReferenceSequences = referenceSequences.size();
389 if ( m_isBigEndian ) BamTools::SwapEndian_32(numReferenceSequences);
390 m_stream.Write((char*)&numReferenceSequences, Constants::BAM_SIZEOF_INT);
392 // foreach reference sequence
393 RefVector::const_iterator rsIter = referenceSequences.begin();
394 RefVector::const_iterator rsEnd = referenceSequences.end();
395 for ( ; rsIter != rsEnd; ++rsIter ) {
397 // write the reference sequence name length
398 uint32_t referenceSequenceNameLen = rsIter->RefName.size() + 1;
399 if ( m_isBigEndian ) BamTools::SwapEndian_32(referenceSequenceNameLen);
400 m_stream.Write((char*)&referenceSequenceNameLen, Constants::BAM_SIZEOF_INT);
402 // write the reference sequence name
403 m_stream.Write(rsIter->RefName.c_str(), referenceSequenceNameLen);
405 // write the reference sequence length
406 int32_t referenceLength = rsIter->RefLength;
407 if ( m_isBigEndian ) BamTools::SwapEndian_32(referenceLength);
408 m_stream.Write((char*)&referenceLength, Constants::BAM_SIZEOF_INT);
412 void BamWriterPrivate::WriteSamHeaderText(const std::string& samHeaderText) {
414 // write the SAM header text length
415 uint32_t samHeaderLen = samHeaderText.size();
416 if ( m_isBigEndian ) BamTools::SwapEndian_32(samHeaderLen);
417 m_stream.Write((char*)&samHeaderLen, Constants::BAM_SIZEOF_INT);
419 // write the SAM header text
420 if ( samHeaderLen > 0 )
421 m_stream.Write(samHeaderText.data(), samHeaderLen);