1 // ***************************************************************************
\r
2 // BamReader.h (c) 2009 Derek Barnett, Michael Str�mberg
\r
3 // Marth Lab, Department of Biology, Boston College
\r
4 // All rights reserved.
\r
5 // ---------------------------------------------------------------------------
\r
6 // Last modified: 19 November 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 reading BAM files
\r
12 // ***************************************************************************
\r
17 #include <api/api_global.h>
\r
18 #include <api/BamAlignment.h>
\r
19 #include <api/BamIndex.h>
\r
22 namespace BamTools {
\r
24 class API_EXPORT BamReader {
\r
26 // constructor / destructor
\r
34 // ----------------------
\r
35 // BAM file operations
\r
36 // ----------------------
\r
40 // returns whether reader is open for reading or not
\r
41 bool IsOpen(void) const;
\r
42 // performs random-access jump using (reference, position) as a left-bound
\r
43 bool Jump(int refID, int position = 0);
\r
44 // opens BAM file (and optional BAM index file, if provided)
\r
45 // @lookForIndex - if no indexFilename provided, look in BAM file's directory for an existing index file
\r
46 // default behavior is to skip index file search if no index filename given
\r
47 // @preferStandardIndex - if true, give priority in index file searching to standard BAM index (*.bai)
\r
48 // default behavior is to prefer the BamToolsIndex (*.bti) if both are available
\r
49 bool Open(const std::string& filename,
\r
50 const std::string& indexFilename = "",
\r
51 const bool lookForIndex = false,
\r
52 const bool preferStandardIndex = false);
\r
53 // returns file pointer to beginning of alignments
\r
55 // sets a region of interest (with left & right bound reference/position)
\r
56 // returns success/failure of seeking to left bound of region
\r
57 bool SetRegion(const BamRegion& region);
\r
58 bool SetRegion(const int& leftRefID, const int& leftBound, const int& rightRefID, const int& rightBound);
\r
60 // ----------------------
\r
61 // access alignment data
\r
62 // ----------------------
\r
64 // retrieves next available alignment (returns success/fail)
\r
65 bool GetNextAlignment(BamAlignment& bAlignment);
\r
66 // retrieves next available alignment core data (returns success/fail)
\r
67 // ** DOES NOT parse any character data (read name, bases, qualities, tag data) **
\r
68 // useful for operations requiring ONLY aligner-related information
\r
69 // (refId/position, alignment flags, CIGAR, mapQuality, etc)
\r
70 bool GetNextAlignmentCore(BamAlignment& bAlignment);
\r
72 // ----------------------
\r
73 // access auxiliary data
\r
74 // ----------------------
\r
76 // returns SAM header text
\r
77 const std::string GetHeaderText(void) const;
\r
78 // returns number of reference sequences
\r
79 int GetReferenceCount(void) const;
\r
80 // returns vector of reference objects
\r
81 const BamTools::RefVector& GetReferenceData(void) const;
\r
82 // returns reference id (used for BamReader::Jump()) for the given reference name
\r
83 int GetReferenceID(const std::string& refName) const;
\r
84 // returns the name of the file associated with this BamReader
\r
85 const std::string GetFilename(void) const;
\r
87 // ----------------------
\r
88 // BAM index operations
\r
89 // ----------------------
\r
91 // creates index for BAM file, saves to file
\r
92 // default behavior is to create the BAM standard index (".bai")
\r
93 // set flag to false to create the BamTools-specific index (".bti")
\r
94 bool CreateIndex(bool useStandardIndex = true);
\r
95 // returns whether index data is available for reading
\r
96 // (e.g. if true, BamReader should be able to seek to a region)
\r
97 bool HasIndex(void) const;
\r
98 // change the index caching behavior
\r
99 // default BamReader/Index mode is LimitedIndexCaching
\r
100 // @mode - can be either FullIndexCaching, LimitedIndexCaching,
\r
101 // or NoIndexCaching. See BamIndex.h for more details
\r
102 void SetIndexCacheMode(const BamIndex::BamIndexCacheMode mode);
\r
104 // deprecated methods
\r
107 // deprecated (but still available): prefer HasIndex() instead
\r
109 // Deprecated purely for API semantic clarity - HasIndex() should be clearer
\r
110 // than IsIndexLoaded() in light of the new caching modes that may clear the
\r
111 // index data from memory, but leave the index file open for later random access
\r
114 // For example, what would (IsIndexLoaded() == true) mean when cacheMode has been
\r
115 // explicitly set to NoIndexCaching? This is confusing at best, misleading about
\r
116 // current memory behavior at worst.
\r
118 // returns whether index data is available
\r
119 // (e.g. if true, BamReader should be able to seek to a region)
\r
120 bool IsIndexLoaded(void) const;
\r
122 // private implementation
\r
124 struct BamReaderPrivate;
\r
125 BamReaderPrivate* d;
\r
128 } // namespace BamTools
\r
130 #endif // BAMREADER_H
\r