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: 16 June 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 // re-calculate bin (in case BamAlignment's position has been previously modified)
160 const uint32_t alignmentBin = CalculateMinimumBin(al.Position, al.GetEndPosition());
162 // assign the BAM core data
163 uint32_t buffer[Constants::BAM_CORE_BUFFER_SIZE];
164 buffer[0] = al.RefID;
165 buffer[1] = al.Position;
166 buffer[2] = (alignmentBin << 16) | (al.MapQuality << 8) | al.SupportData.QueryNameLength;
167 buffer[3] = (al.AlignmentFlag << 16) | al.SupportData.NumCigarOperations;
168 buffer[4] = al.SupportData.QuerySequenceLength;
169 buffer[5] = al.MateRefID;
170 buffer[6] = al.MatePosition;
171 buffer[7] = al.InsertSize;
173 // swap BAM core endian-ness, if necessary
174 if ( m_isBigEndian ) {
175 for ( int i = 0; i < 8; ++i )
176 BamTools::SwapEndian_32(buffer[i]);
179 // write the BAM core
180 m_stream.Write((char*)&buffer, Constants::BAM_CORE_SIZE);
182 // write the raw char data
183 m_stream.Write((char*)al.SupportData.AllCharData.data(),
184 al.SupportData.BlockLength-Constants::BAM_CORE_SIZE);
187 // otherwise, BamAlignment should contain character in the standard fields: Name, QueryBases, etc
188 // ( resulting from BamReader::GetNextAlignment() *OR* being generated directly by client code )
191 // calculate char lengths
192 const unsigned int nameLength = al.Name.size() + 1;
193 const unsigned int numCigarOperations = al.CigarData.size();
194 const unsigned int queryLength = al.QueryBases.size();
195 const unsigned int tagDataLength = al.TagData.size();
197 // no way to tell if BamAlignment.Bin is already defined (no default, invalid value)
198 // force calculation of Bin before storing
199 const int endPosition = al.GetEndPosition();
200 const unsigned int alignmentBin = CalculateMinimumBin(al.Position, endPosition);
202 // create our packed cigar string
204 CreatePackedCigar(al.CigarData, packedCigar);
205 const unsigned int packedCigarLength = packedCigar.size();
209 EncodeQuerySequence(al.QueryBases, encodedQuery);
210 const unsigned int encodedQueryLength = encodedQuery.size();
212 // write the block size
213 const unsigned int dataBlockSize = nameLength +
218 unsigned int blockSize = Constants::BAM_CORE_SIZE + dataBlockSize;
219 if ( m_isBigEndian ) BamTools::SwapEndian_32(blockSize);
220 m_stream.Write((char*)&blockSize, Constants::BAM_SIZEOF_INT);
222 // assign the BAM core data
223 uint32_t buffer[Constants::BAM_CORE_BUFFER_SIZE];
224 buffer[0] = al.RefID;
225 buffer[1] = al.Position;
226 buffer[2] = (alignmentBin << 16) | (al.MapQuality << 8) | nameLength;
227 buffer[3] = (al.AlignmentFlag << 16) | numCigarOperations;
228 buffer[4] = queryLength;
229 buffer[5] = al.MateRefID;
230 buffer[6] = al.MatePosition;
231 buffer[7] = al.InsertSize;
233 // swap BAM core endian-ness, if necessary
234 if ( m_isBigEndian ) {
235 for ( int i = 0; i < 8; ++i )
236 BamTools::SwapEndian_32(buffer[i]);
239 // write the BAM core
240 m_stream.Write((char*)&buffer, Constants::BAM_CORE_SIZE);
242 // write the query name
243 m_stream.Write(al.Name.c_str(), nameLength);
245 // write the packed cigar
246 if ( m_isBigEndian ) {
247 char* cigarData = (char*)calloc(sizeof(char), packedCigarLength);
248 memcpy(cigarData, packedCigar.data(), packedCigarLength);
249 if ( m_isBigEndian ) {
250 for ( unsigned int i = 0; i < packedCigarLength; ++i )
251 BamTools::SwapEndian_32p(&cigarData[i]);
253 m_stream.Write(cigarData, packedCigarLength);
257 m_stream.Write(packedCigar.data(), packedCigarLength);
259 // write the encoded query sequence
260 m_stream.Write(encodedQuery.data(), encodedQueryLength);
262 // write the base qualities
263 char* pBaseQualities = (char*)al.Qualities.data();
264 for ( unsigned int i = 0; i < queryLength; ++i )
265 pBaseQualities[i] -= 33; // FASTQ conversion
266 m_stream.Write(pBaseQualities, queryLength);
268 // write the read group tag
269 if ( m_isBigEndian ) {
271 char* tagData = (char*)calloc(sizeof(char), tagDataLength);
272 memcpy(tagData, al.TagData.data(), tagDataLength);
275 while ( (unsigned int)i < tagDataLength ) {
277 i += Constants::BAM_TAG_TAGSIZE; // skip tag chars (e.g. "RG", "NM", etc.)
278 const char type = tagData[i]; // get tag type at position i
283 case(Constants::BAM_TAG_TYPE_ASCII) :
284 case(Constants::BAM_TAG_TYPE_INT8) :
285 case(Constants::BAM_TAG_TYPE_UINT8) :
289 case(Constants::BAM_TAG_TYPE_INT16) :
290 case(Constants::BAM_TAG_TYPE_UINT16) :
291 BamTools::SwapEndian_16p(&tagData[i]);
292 i += sizeof(uint16_t);
295 case(Constants::BAM_TAG_TYPE_FLOAT) :
296 case(Constants::BAM_TAG_TYPE_INT32) :
297 case(Constants::BAM_TAG_TYPE_UINT32) :
298 BamTools::SwapEndian_32p(&tagData[i]);
299 i += sizeof(uint32_t);
302 case(Constants::BAM_TAG_TYPE_HEX) :
303 case(Constants::BAM_TAG_TYPE_STRING) :
304 // no endian swapping necessary for hex-string/string data
307 // increment one more for null terminator
311 case(Constants::BAM_TAG_TYPE_ARRAY) :
315 const char arrayType = tagData[i];
318 // swap endian-ness of number of elements in place, then retrieve for loop
319 BamTools::SwapEndian_32p(&tagData[i]);
321 memcpy(&numElements, &tagData[i], sizeof(uint32_t));
322 i += sizeof(uint32_t);
324 // swap endian-ness of array elements
325 for ( int j = 0; j < numElements; ++j ) {
327 case (Constants::BAM_TAG_TYPE_INT8) :
328 case (Constants::BAM_TAG_TYPE_UINT8) :
329 // no endian-swapping necessary
332 case (Constants::BAM_TAG_TYPE_INT16) :
333 case (Constants::BAM_TAG_TYPE_UINT16) :
334 BamTools::SwapEndian_16p(&tagData[i]);
335 i += sizeof(uint16_t);
337 case (Constants::BAM_TAG_TYPE_FLOAT) :
338 case (Constants::BAM_TAG_TYPE_INT32) :
339 case (Constants::BAM_TAG_TYPE_UINT32) :
340 BamTools::SwapEndian_32p(&tagData[i]);
341 i += sizeof(uint32_t);
346 "BamWriter ERROR: unknown binary array type encountered: [%c]\n",
356 fprintf(stderr, "BamWriter ERROR: invalid tag value type\n"); // shouldn't get here
361 m_stream.Write(tagData, tagDataLength);
365 m_stream.Write(al.TagData.data(), tagDataLength);
369 void BamWriterPrivate::SetWriteCompressed(bool ok) {
371 // warn if BAM file is already open
372 // modifying compression is not allowed in this case
374 cerr << "BamWriter WARNING: attempting to change compression mode on an open BAM file is not allowed. "
375 << "Ignoring request." << endl;
379 // set BgzfStream compression mode
380 m_stream.SetWriteCompressed(ok);
383 void BamWriterPrivate::WriteMagicNumber(void) {
384 // write BAM file 'magic number'
385 m_stream.Write(Constants::BAM_HEADER_MAGIC, Constants::BAM_HEADER_MAGIC_LENGTH);
388 void BamWriterPrivate::WriteReferences(const BamTools::RefVector& referenceSequences) {
390 // write the number of reference sequences
391 uint32_t numReferenceSequences = referenceSequences.size();
392 if ( m_isBigEndian ) BamTools::SwapEndian_32(numReferenceSequences);
393 m_stream.Write((char*)&numReferenceSequences, Constants::BAM_SIZEOF_INT);
395 // foreach reference sequence
396 RefVector::const_iterator rsIter = referenceSequences.begin();
397 RefVector::const_iterator rsEnd = referenceSequences.end();
398 for ( ; rsIter != rsEnd; ++rsIter ) {
400 // write the reference sequence name length
401 uint32_t referenceSequenceNameLen = rsIter->RefName.size() + 1;
402 if ( m_isBigEndian ) BamTools::SwapEndian_32(referenceSequenceNameLen);
403 m_stream.Write((char*)&referenceSequenceNameLen, Constants::BAM_SIZEOF_INT);
405 // write the reference sequence name
406 m_stream.Write(rsIter->RefName.c_str(), referenceSequenceNameLen);
408 // write the reference sequence length
409 int32_t referenceLength = rsIter->RefLength;
410 if ( m_isBigEndian ) BamTools::SwapEndian_32(referenceLength);
411 m_stream.Write((char*)&referenceLength, Constants::BAM_SIZEOF_INT);
415 void BamWriterPrivate::WriteSamHeaderText(const std::string& samHeaderText) {
417 // write the SAM header text length
418 uint32_t samHeaderLen = samHeaderText.size();
419 if ( m_isBigEndian ) BamTools::SwapEndian_32(samHeaderLen);
420 m_stream.Write((char*)&samHeaderLen, Constants::BAM_SIZEOF_INT);
422 // write the SAM header text
423 if ( samHeaderLen > 0 )
424 m_stream.Write(samHeaderText.data(), samHeaderLen);