1 // ***************************************************************************
\r
2 // BamWriter.cpp (c) 2009 Michael Str�mberg, Derek Barnett
\r
3 // Marth Lab, Department of Biology, Boston College
\r
4 // All rights reserved.
\r
5 // ---------------------------------------------------------------------------
\r
6 // Last modified: 30 March 2010 (DB)
\r
7 // ---------------------------------------------------------------------------
\r
8 // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad
\r
10 // ---------------------------------------------------------------------------
\r
11 // Provides the basic functionality for producing BAM files
\r
12 // ***************************************************************************
\r
16 #include "BamWriter.h"
\r
17 using namespace BamTools;
\r
18 using namespace std;
\r
20 struct BamWriter::BamWriterPrivate {
\r
27 // constructor / destructor
\r
28 BamWriterPrivate(void) {
\r
29 IsBigEndian = SystemIsBigEndian();
\r
32 ~BamWriterPrivate(void) {
\r
36 // "public" interface
\r
38 void Open(const std::string& filename, const std::string& samHeader, const BamTools::RefVector& referenceSequences);
\r
39 void SaveAlignment(const BamTools::BamAlignment& al);
\r
42 void CreatePackedCigar(const std::vector<CigarOp>& cigarOperations, std::string& packedCigar);
\r
43 void EncodeQuerySequence(const std::string& query, std::string& encodedQuery);
\r
46 // -----------------------------------------------------
\r
47 // BamWriter implementation
\r
48 // -----------------------------------------------------
\r
51 BamWriter::BamWriter(void) {
\r
52 d = new BamWriterPrivate;
\r
56 BamWriter::~BamWriter(void) {
\r
61 // closes the alignment archive
\r
62 void BamWriter::Close(void) {
\r
66 // opens the alignment archive
\r
67 void BamWriter::Open(const string& filename, const string& samHeader, const RefVector& referenceSequences) {
\r
68 d->Open(filename, samHeader, referenceSequences);
\r
71 // saves the alignment to the alignment archive
\r
72 void BamWriter::SaveAlignment(const BamAlignment& al) {
\r
73 d->SaveAlignment(al);
\r
76 // -----------------------------------------------------
\r
77 // BamWriterPrivate implementation
\r
78 // -----------------------------------------------------
\r
80 // closes the alignment archive
\r
81 void BamWriter::BamWriterPrivate::Close(void) {
\r
85 // creates a cigar string from the supplied alignment
\r
86 void BamWriter::BamWriterPrivate::CreatePackedCigar(const vector<CigarOp>& cigarOperations, string& packedCigar) {
\r
89 const unsigned int numCigarOperations = cigarOperations.size();
\r
90 packedCigar.resize(numCigarOperations * BT_SIZEOF_INT);
\r
92 // pack the cigar data into the string
\r
93 unsigned int* pPackedCigar = (unsigned int*)packedCigar.data();
\r
95 unsigned int cigarOp;
\r
96 vector<CigarOp>::const_iterator coIter;
\r
97 for(coIter = cigarOperations.begin(); coIter != cigarOperations.end(); coIter++) {
\r
99 switch(coIter->Type) {
\r
101 cigarOp = BAM_CMATCH;
\r
104 cigarOp = BAM_CINS;
\r
107 cigarOp = BAM_CDEL;
\r
110 cigarOp = BAM_CREF_SKIP;
\r
113 cigarOp = BAM_CSOFT_CLIP;
\r
116 cigarOp = BAM_CHARD_CLIP;
\r
119 cigarOp = BAM_CPAD;
\r
122 printf("ERROR: Unknown cigar operation found: %c\n", coIter->Type);
\r
126 *pPackedCigar = coIter->Length << BAM_CIGAR_SHIFT | cigarOp;
\r
131 // encodes the supplied query sequence into 4-bit notation
\r
132 void BamWriter::BamWriterPrivate::EncodeQuerySequence(const string& query, string& encodedQuery) {
\r
134 // prepare the encoded query string
\r
135 const unsigned int queryLen = query.size();
\r
136 const unsigned int encodedQueryLen = (unsigned int)((queryLen / 2.0) + 0.5);
\r
137 encodedQuery.resize(encodedQueryLen);
\r
138 char* pEncodedQuery = (char*)encodedQuery.data();
\r
139 const char* pQuery = (const char*)query.data();
\r
141 unsigned char nucleotideCode;
\r
142 bool useHighWord = true;
\r
149 nucleotideCode = 0;
\r
153 nucleotideCode = 1;
\r
157 nucleotideCode = 2;
\r
161 nucleotideCode = 4;
\r
165 nucleotideCode = 8;
\r
169 nucleotideCode = 15;
\r
173 printf("ERROR: Only the following bases are supported in the BAM format: {=, A, C, G, T, N}. Found [%c]\n", *pQuery);
\r
177 // pack the nucleotide code
\r
179 *pEncodedQuery = nucleotideCode << 4;
\r
180 useHighWord = false;
\r
182 *pEncodedQuery |= nucleotideCode;
\r
184 useHighWord = true;
\r
187 // increment the query position
\r
192 // opens the alignment archive
\r
193 void BamWriter::BamWriterPrivate::Open(const string& filename, const string& samHeader, const RefVector& referenceSequences) {
\r
195 // open the BGZF file for writing
\r
196 mBGZF.Open(filename, "wb");
\r
198 // ================
\r
199 // write the header
\r
200 // ================
\r
202 // write the BAM signature
\r
203 const unsigned char SIGNATURE_LENGTH = 4;
\r
204 const char* BAM_SIGNATURE = "BAM\1";
\r
205 mBGZF.Write(BAM_SIGNATURE, SIGNATURE_LENGTH);
\r
207 // write the SAM header text length
\r
208 uint32_t samHeaderLen = samHeader.size();
\r
209 if ( IsBigEndian ) { SwapEndian_32(samHeaderLen); }
\r
210 mBGZF.Write((char*)&samHeaderLen, BT_SIZEOF_INT);
\r
212 // write the SAM header text
\r
213 if(samHeaderLen > 0) {
\r
214 mBGZF.Write(samHeader.data(), samHeaderLen);
\r
217 // write the number of reference sequences
\r
218 uint32_t numReferenceSequences = referenceSequences.size();
\r
219 if ( IsBigEndian ) { SwapEndian_32(numReferenceSequences); }
\r
220 mBGZF.Write((char*)&numReferenceSequences, BT_SIZEOF_INT);
\r
222 // =============================
\r
223 // write the sequence dictionary
\r
224 // =============================
\r
226 RefVector::const_iterator rsIter;
\r
227 for(rsIter = referenceSequences.begin(); rsIter != referenceSequences.end(); rsIter++) {
\r
229 // write the reference sequence name length
\r
230 uint32_t referenceSequenceNameLen = rsIter->RefName.size() + 1;
\r
231 if ( IsBigEndian ) { SwapEndian_32(referenceSequenceNameLen); }
\r
232 mBGZF.Write((char*)&referenceSequenceNameLen, BT_SIZEOF_INT);
\r
234 // write the reference sequence name
\r
235 mBGZF.Write(rsIter->RefName.c_str(), referenceSequenceNameLen);
\r
237 // write the reference sequence length
\r
238 int32_t referenceLength = rsIter->RefLength;
\r
239 if ( IsBigEndian ) { SwapEndian_32(referenceLength); }
\r
240 mBGZF.Write((char*)&referenceLength, BT_SIZEOF_INT);
\r
244 // saves the alignment to the alignment archive
\r
245 void BamWriter::BamWriterPrivate::SaveAlignment(const BamAlignment& al) {
\r
248 const unsigned int nameLen = al.Name.size() + 1;
\r
249 const unsigned int queryLen = al.QueryBases.size();
\r
250 const unsigned int numCigarOperations = al.CigarData.size();
\r
252 // create our packed cigar string
\r
253 string packedCigar;
\r
254 CreatePackedCigar(al.CigarData, packedCigar);
\r
255 const unsigned int packedCigarLen = packedCigar.size();
\r
257 // encode the query
\r
258 string encodedQuery;
\r
259 EncodeQuerySequence(al.QueryBases, encodedQuery);
\r
260 const unsigned int encodedQueryLen = encodedQuery.size();
\r
262 // store the tag data length
\r
263 // -------------------------------------------
\r
264 // Modified: 3-25-2010 DWB
\r
265 // Contributed: ARQ
\r
266 // Fixed: "off by one" error when parsing tags in files produced by BamWriter
\r
267 const unsigned int tagDataLength = al.TagData.size();
\r
269 // const unsigned int tagDataLength = al.TagData.size() + 1;
\r
270 // -------------------------------------------
\r
272 // assign the BAM core data
\r
273 uint32_t buffer[8];
\r
274 buffer[0] = al.RefID;
\r
275 buffer[1] = al.Position;
\r
276 buffer[2] = (al.Bin << 16) | (al.MapQuality << 8) | nameLen;
\r
277 buffer[3] = (al.AlignmentFlag << 16) | numCigarOperations;
\r
278 buffer[4] = queryLen;
\r
279 buffer[5] = al.MateRefID;
\r
280 buffer[6] = al.MatePosition;
\r
281 buffer[7] = al.InsertSize;
\r
283 // write the block size
\r
284 unsigned int dataBlockSize = nameLen + packedCigarLen + encodedQueryLen + queryLen + tagDataLength;
\r
285 unsigned int blockSize = BAM_CORE_SIZE + dataBlockSize;
\r
286 if ( IsBigEndian ) { SwapEndian_32(blockSize); }
\r
287 mBGZF.Write((char*)&blockSize, BT_SIZEOF_INT);
\r
289 // write the BAM core
\r
290 if ( IsBigEndian ) {
\r
291 for ( int i = 0; i < 8; ++i ) {
\r
292 SwapEndian_32(buffer[i]);
\r
295 mBGZF.Write((char*)&buffer, BAM_CORE_SIZE);
\r
297 // write the query name
\r
298 mBGZF.Write(al.Name.c_str(), nameLen);
\r
300 // write the packed cigar
\r
301 if ( IsBigEndian ) {
\r
303 char* cigarData = (char*)calloc(sizeof(char), packedCigarLen);
\r
304 memcpy(cigarData, packedCigar.data(), packedCigarLen);
\r
306 for (unsigned int i = 0; i < packedCigarLen; ++i) {
\r
307 if ( IsBigEndian ) {
\r
308 SwapEndian_32p(&cigarData[i]);
\r
312 mBGZF.Write(cigarData, packedCigarLen);
\r
316 mBGZF.Write(packedCigar.data(), packedCigarLen);
\r
319 // write the encoded query sequence
\r
320 mBGZF.Write(encodedQuery.data(), encodedQueryLen);
\r
322 // write the base qualities
\r
323 string baseQualities = al.Qualities;
\r
324 char* pBaseQualities = (char*)al.Qualities.data();
\r
325 for(unsigned int i = 0; i < queryLen; i++) { pBaseQualities[i] -= 33; }
\r
326 mBGZF.Write(pBaseQualities, queryLen);
\r
328 // write the read group tag
\r
329 if ( IsBigEndian ) {
\r
331 char* tagData = (char*)calloc(sizeof(char), tagDataLength);
\r
332 memcpy(tagData, al.TagData.data(), tagDataLength);
\r
335 while ( (unsigned int)i < tagDataLength ) {
\r
337 i += 2; // skip tag type (e.g. "RG", "NM", etc)
\r
338 uint8_t type = toupper(tagData[i]); // lower & upper case letters have same meaning
\r
339 ++i; // skip value type
\r
349 SwapEndian_16p(&tagData[i]);
\r
350 i+=2; // sizeof(uint16_t)
\r
355 SwapEndian_32p(&tagData[i]);
\r
356 i+=4; // sizeof(uint32_t)
\r
360 SwapEndian_64p(&tagData[i]);
\r
361 i+=8; // sizeof(uint64_t)
\r
366 while (tagData[i]) { ++i; }
\r
367 ++i; // increment one more for null terminator
\r
371 printf("ERROR: Invalid tag value type\n"); // shouldn't get here
\r
377 mBGZF.Write(tagData, tagDataLength);
\r
380 mBGZF.Write(al.TagData.data(), tagDataLength);
\r