1 // ***************************************************************************
2 // BamWriter.h (c) 2009 Michael Strömberg, Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 24 June 2009 (DB)
7 // ---------------------------------------------------------------------------
8 // The BGZF routines were adapted from the bgzf.c code developed at the Broad
10 // ---------------------------------------------------------------------------
11 // Provides the basic functionality for producing BAM files
12 // ***************************************************************************
16 \brief API for writing BAM files.
33 //! API for writing BAM files.
47 \brief Closes the alignment archive
53 \brief Opens the alignment archive
54 \param filename output BAM file
55 \param samHeader SAM-format header text
56 \param referenceSequences Reference sequence data
59 void Open(const std::string& filename, const std::string& samHeader, const RefVector& referenceSequences);
62 \brief Saves an alignment to the archive
63 \param al The BamAlignment to be saved
65 void SaveAlignment(const BamAlignment& al);
67 // --------------------------------------------------------------------------------------
70 // closes the BAM file
72 // compresses the current block
73 int BgzfDeflateBlock(void);
74 // flushes the data in the BGZF block
75 void BgzfFlushBlock(void);
76 // opens the BAM file for writing
77 void BgzfOpen(const std::string& filename);
78 // packs an unsigned integer into the specified buffer
79 static inline void BgzfPackUnsignedInt(char* buffer, unsigned int value);
80 // packs an unsigned short into the specified buffer
81 static inline void BgzfPackUnsignedShort(char* buffer, unsigned short value);
82 // writes the supplied data into the BGZF buffer
83 unsigned int BgzfWrite(const char* data, const unsigned int dataLen);
84 // calculates the minimum bin that contains a region [begin, end)
85 static inline unsigned int CalculateMinimumBin(unsigned int begin, unsigned int end);
86 // creates a packed cigar string from the supplied alignment
87 static void CreatePackedCigar(const std::vector<CigarOp>& cigarOperations, std::string& packedCigar);
88 // encodes the supplied query sequence into 4-bit notation
89 static void EncodeQuerySequence(const std::string& query, std::string& encodedQuery);
90 // our BGZF output object
95 // --------------------------------------------------------------------------------------
96 // static inline methods (internal - can exclude from main documentation)
98 // packs an unsigned integer into the specified buffer
99 inline void BamWriter::BgzfPackUnsignedInt(char* buffer, unsigned int value) {
100 buffer[0] = (char)value;
101 buffer[1] = (char)(value >> 8);
102 buffer[2] = (char)(value >> 16);
103 buffer[3] = (char)(value >> 24);
106 // packs an unsigned short into the specified buffer
107 inline void BamWriter::BgzfPackUnsignedShort(char* buffer, unsigned short value) {
108 buffer[0] = (char)value;
109 buffer[1] = (char)(value >> 8);
111 // --------------------------------------------------------------------------------------
114 } // end BamTools namespace