]> git.donarmstrong.com Git - bamtools.git/blob - BamWriter.h
Full update to SVN after combining BamReader and BamWriter into cohesive BamTools...
[bamtools.git] / BamWriter.h
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
9 // Institute.
10 // ---------------------------------------------------------------------------
11 // Provides the basic functionality for producing BAM files
12 // ***************************************************************************
13
14 /*! 
15         \file BamWriter.h
16         \brief API for writing BAM files.
17 */
18
19 #pragma once
20
21 // C++ includes
22 #include <string>
23 #include <vector>
24
25 // zlib includes
26 #include <zlib.h>
27
28 // BamTools includes
29 #include "BamAux.h"
30
31 namespace BamTools { 
32
33         //! API for writing BAM files.
34         class BamWriter {
35                 
36                 public:
37                         
38                         //! Constructor.
39                         BamWriter(void);
40                         
41                         //! Destructor.
42                         ~BamWriter(void);
43                 
44                 public:
45                         
46                         /*! 
47                                 \brief Closes the alignment archive
48                                 \sa Open()
49                         */
50                         void Close(void);
51                         
52                         /*! 
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 
57                                 \sa Close()
58                         */
59                         void Open(const std::string& filename, const std::string& samHeader, const RefVector& referenceSequences);
60                         
61                         /*! 
62                                 \brief Saves an alignment to the archive
63                                 \param al The BamAlignment to be saved
64                         */
65                         void SaveAlignment(const BamAlignment& al);
66                 
67                 // --------------------------------------------------------------------------------------
68                 // internal methods
69                 private:
70                         // closes the BAM file
71                         void BgzfClose(void);
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
91                         BgzfData mBGZF;
92         };
93
94         //! \cond
95         // --------------------------------------------------------------------------------------
96         // static inline methods (internal - can exclude from main documentation)
97         
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);
104         }
105
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);
110         }
111         // --------------------------------------------------------------------------------------
112         //! \endcond
113         
114 } // end BamTools namespace