1 // ***************************************************************************
2 // BamRandomAccessController_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 October 2011(DB)
6 // ---------------------------------------------------------------------------
7 // Manages random access operations in a BAM file
8 // ***************************************************************************
9 
10 #ifndef BAMRACONTROLLER_P_H
11 #define BAMRACONTROLLER_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 "api/BamAux.h"
26 #include "api/BamIndex.h"
27 
28 namespace BamTools {
29 
30 class BamAlignment;
31 
32 namespace Internal {
33 
34 class BamReaderPrivate;
35 
36 class API_NO_EXPORT BamRandomAccessController
37 {
38 
39     // enums
40 public:
41     enum RegionState
42     {
43         BeforeRegion = 0,
44         OverlapsRegion,
45         AfterRegion
46     };
47 
48     // ctor & dtor
49 public:
50     BamRandomAccessController();
51     ~BamRandomAccessController();
52 
53     // BamRandomAccessController interface
54 public:
55     // index methods
56     void ClearIndex();
57     bool CreateIndex(BamReaderPrivate* reader, const BamIndex::IndexType& type);
58     bool HasIndex() const;
59     bool IndexHasAlignmentsForReference(const int& refId);
60     bool LocateIndex(BamReaderPrivate* reader, const BamIndex::IndexType& preferredType);
61     bool OpenIndex(const std::string& indexFilename, BamReaderPrivate* reader);
62     void SetIndex(BamIndex* index);
63 
64     // region methods
65     void ClearRegion();
66     bool HasRegion() const;
67     RegionState AlignmentState(const BamAlignment& alignment) const;
68     bool RegionHasAlignments() const;
69     bool SetRegion(const BamRegion& region, const int& referenceCount);
70 
71     // general methods
72     void Close();
73     std::string GetErrorString() const;
74 
75     // internal methods
76 private:
77     // adjusts requested region if necessary (depending on where data actually begins)
78     void AdjustRegion(const int& referenceCount);
79     // error-string handling
80     void SetErrorString(const std::string& where, const std::string& what);
81 
82     // data members
83 private:
84     // index data
85     BamIndex* m_index;  // owns the index, not a copy - responsible for deleting
86 
87     // region data
88     BamRegion m_region;
89     bool m_hasAlignmentsInRegion;
90 
91     // general data
92     std::string m_errorString;
93 };
94 
95 }  // namespace Internal
96 }  // namespace BamTools
97 
98 #endif  // BAMRACONTROLLER_P_H
99