1 // ***************************************************************************
2 // BamReader_p.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 18 November 2012 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides the basic functionality for reading BAM files
8 // ***************************************************************************
9 
10 #ifndef BAMREADER_P_H
11 #define BAMREADER_P_H
12 
13 #include "api/api_global.h"
14 
15 //  -------------
16 //  W A R N I N G
17 //  -------------
18 //
19 // This file is not part of the BamTools API.  It exists purely as an
20 // implementation detail. This header file may change from version to version
21 // without notice, or even be removed.
22 //
23 // We mean it.
24 
25 #include <string>
26 #include "api/BamAlignment.h"
27 #include "api/BamIndex.h"
28 #include "api/BamReader.h"
29 #include "api/SamHeader.h"
30 #include "api/internal/bam/BamHeader_p.h"
31 #include "api/internal/bam/BamRandomAccessController_p.h"
32 #include "api/internal/io/BgzfStream_p.h"
33 
34 namespace BamTools {
35 namespace Internal {
36 
37 class API_NO_EXPORT BamReaderPrivate
38 {
39 
40     // ctor & dtor
41 public:
42     BamReaderPrivate(BamReader* parent);
43     ~BamReaderPrivate();
44 
45     // BamReader interface
46 public:
47     // file operations
48     bool Close();
49     const std::string Filename() const;
50     bool IsOpen() const;
51     bool Open(const std::string& filename);
52     bool Rewind();
53     bool SetRegion(const BamRegion& region);
54 
55     // access alignment data
56     bool GetNextAlignment(BamAlignment& alignment);
57     bool GetNextAlignmentCore(BamAlignment& alignment);
58     bool Tag2Cigar(BamAlignment& alignment, RaiiBuffer& buf);
59 
60     // access auxiliary data
61     std::string GetHeaderText() const;
62     const SamHeader& GetConstSamHeader() const;
63     SamHeader GetSamHeader() const;
64     int GetReferenceCount() const;
65     const RefVector& GetReferenceData() const;
66     int GetReferenceID(const std::string& refName) const;
67 
68     // index operations
69     bool CreateIndex(const BamIndex::IndexType& type);
70     bool HasIndex() const;
71     bool LocateIndex(const BamIndex::IndexType& preferredType);
72     bool OpenIndex(const std::string& indexFilename);
73     void SetIndex(BamIndex* index);
74 
75     // error handling
76     std::string GetErrorString() const;
77     void SetErrorString(const std::string& where, const std::string& what);
78 
79     // internal methods, but available as a BamReaderPrivate 'interface'
80     //
81     // these methods should only be used by BamTools::Internal classes
82     // (currently only used by the BamIndex subclasses)
83 public:
84     // retrieves header text from BAM file
85     void LoadHeaderData();
86     // retrieves BAM alignment under file pointer
87     // (does no overlap checking or character data parsing)
88     bool LoadNextAlignment(BamAlignment& alignment);
89     // builds reference data structure from BAM file
90     bool LoadReferenceData();
91     // seek reader to file position
92     bool Seek(const int64_t& position);
93     // return reader's file position
94     int64_t Tell() const;
95 
96     // data members
97 public:
98     // general BAM file data
99     int64_t m_alignmentsBeginOffset;
100     std::string m_filename;
101     RefVector m_references;
102 
103     // system data
104     bool m_isBigEndian;
105 
106     // parent BamReader
107     BamReader* m_parent;
108 
109     // BamReaderPrivate components
110     BamHeader m_header;
111     BamRandomAccessController m_randomAccessController;
112     BgzfStream m_stream;
113 
114     // error handling
115     std::string m_errorString;
116 };
117 
118 }  // namespace Internal
119 }  // namespace BamTools
120 
121 #endif  // BAMREADER_P_H
122