1 // ***************************************************************************
2 // BamRandomAccessController_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 21 March 2011(DB)
7 // ---------------------------------------------------------------------------
8 // Manages random access operations in a BAM file
9 // **************************************************************************
11 #include <api/BamIndex.h>
12 #include <api/internal/BamRandomAccessController_p.h>
13 #include <api/internal/BamReader_p.h>
14 #include <api/internal/BamIndexFactory_p.h>
15 using namespace BamTools;
16 using namespace BamTools::Internal;
21 BamRandomAccessController::BamRandomAccessController(void)
23 , m_indexCacheMode(BamIndex::LimitedIndexCaching)
24 , m_hasAlignmentsInRegion(true)
27 BamRandomAccessController::~BamRandomAccessController(void) {
31 void BamRandomAccessController::AdjustRegion(const int& referenceCount) {
33 // skip if no index available
37 // see if any references in region have alignments
38 m_hasAlignmentsInRegion = false;
39 int currentId = m_region.LeftRefID;
40 const int rightBoundRefId = ( m_region.isRightBoundSpecified() ? m_region.RightRefID : referenceCount - 1 );
41 while ( currentId <= rightBoundRefId ) {
42 m_hasAlignmentsInRegion = m_index->HasAlignments(currentId);
43 if ( m_hasAlignmentsInRegion ) break;
47 // if no data found on any reference in region
48 if ( !m_hasAlignmentsInRegion )
51 // if left bound of desired region had no data, use first reference that had data
52 // otherwise, leave requested region as-is
53 if ( currentId != m_region.LeftRefID ) {
54 m_region.LeftRefID = currentId;
55 m_region.LeftPosition = 0;
59 // returns alignments' "RegionState": { Before|Overlaps|After } current region
60 BamRandomAccessController::RegionState
61 BamRandomAccessController::AlignmentState(const BamAlignment& alignment) const {
63 // if region has no left bound at all
64 if ( !m_region.isLeftBoundSpecified() )
65 return OverlapsRegion;
67 // handle unmapped reads - return AFTER region to halt processing
68 if ( alignment.RefID == -1 )
71 // if alignment is on any reference before left bound reference
72 if ( alignment.RefID < m_region.LeftRefID )
75 // if alignment is on left bound reference
76 else if ( alignment.RefID == m_region.LeftRefID ) {
78 // if alignment starts at or after left bound position
79 if ( alignment.Position >= m_region.LeftPosition) {
81 if ( m_region.isRightBoundSpecified() && // right bound is specified AND
82 m_region.LeftRefID == m_region.RightRefID && // left & right bounds on same reference AND
83 alignment.Position > m_region.RightPosition ) // alignment starts after right bound position
86 // otherwise, alignment overlaps region
87 else return OverlapsRegion;
90 // alignment starts before left bound position
93 // if alignment overlaps left bound position
94 if ( alignment.GetEndPosition() >= m_region.LeftPosition )
95 return OverlapsRegion;
101 // otherwise alignment is on a reference after left bound reference
104 // if region has a right bound
105 if ( m_region.isRightBoundSpecified() ) {
107 // alignment is on any reference between boundaries
108 if ( alignment.RefID < m_region.RightRefID )
109 return OverlapsRegion;
111 // alignment is on any reference after right boundary
112 else if ( alignment.RefID > m_region.RightRefID )
115 // alignment is on right bound reference
118 // if alignment starts on or before right bound position
119 if ( alignment.Position <= m_region.RightPosition )
120 return OverlapsRegion;
126 // otherwise, alignment starts after left bound and there is no right bound
127 else return OverlapsRegion;
131 void BamRandomAccessController::Close(void) {
136 void BamRandomAccessController::ClearIndex(void) {
141 void BamRandomAccessController::ClearRegion(void) {
143 m_hasAlignmentsInRegion = true;
146 bool BamRandomAccessController::CreateIndex(BamReaderPrivate* reader,
147 const BamIndex::IndexType& type) {
149 // skip if reader is invalid
153 // create new index of requested type
154 BamIndex* newIndex = BamIndexFactory::CreateIndexOfType(type);
155 if ( newIndex == 0 ) {
156 cerr << "BamRandomAccessController ERROR: could not create index of type " << type << endl;
160 // attempt to build index from current BamReader file
161 if ( !newIndex->Build(reader) ) {
162 cerr << "BamRandomAccessController ERROR: could not build index on BAM file: " << reader->Filename() << endl;
169 // attempt to write new index file
170 if ( newIndex->Write(reader->Filename()) ) {
171 cerr << "BamRandomAccessController ERROR: could not save new index for BAM file: " << reader->Filename() << endl;
175 // set new index's cache mode & return success
176 newIndex->SetCacheMode(m_indexCacheMode);
180 bool BamRandomAccessController::HasIndex(void) const {
181 return ( m_index != 0 );
184 bool BamRandomAccessController::HasRegion(void) const {
185 return ( !m_region.isNull() );
188 bool BamRandomAccessController::IndexHasAlignmentsForReference(const int& refId) {
189 return m_index->HasAlignments(refId);
192 bool BamRandomAccessController::LocateIndex(const string& bamFilename,
193 const BamIndex::IndexType& preferredType)
195 // look up index filename, deferring to preferredType if possible
196 const string& indexFilename = BamIndexFactory::FindIndexFilename(bamFilename, preferredType);
198 // if no index file found (of any type)
199 if ( indexFilename.empty() )
202 // otherwise open & use index file that was found
203 return OpenIndex(indexFilename);
206 bool BamRandomAccessController::OpenIndex(const string& indexFilename) {
208 // attempt create new index of type based on filename
209 BamIndex* index = BamIndexFactory::CreateIndexFromFilename(indexFilename);
211 cerr << "BamRandomAccessController ERROR: could not create index for file: " << indexFilename << endl;
216 index->SetCacheMode(m_indexCacheMode);
218 // attempt to load data from index file
219 if ( !index->Load(indexFilename) ) {
220 cerr << "BamRandomAccessController ERROR: could not load index data from file: " << indexFilename << endl;
224 // save new index & return success
229 bool BamRandomAccessController::RegionHasAlignments(void) const {
230 return m_hasAlignmentsInRegion;
233 void BamRandomAccessController::SetIndex(BamIndex* index) {
239 void BamRandomAccessController::SetIndexCacheMode(const BamIndex::IndexCacheMode& mode) {
240 m_indexCacheMode = mode;
242 m_index->SetCacheMode(mode);
245 bool BamRandomAccessController::SetRegion(BamReaderPrivate* reader,
246 const BamRegion& region,
247 const int& referenceCount)
252 // cannot jump when no index is available
256 // adjust region as necessary to reflect where data actually begins
257 AdjustRegion(referenceCount);
259 // if no data present, return true
260 // * Not an error, but future attempts to access alignments in this region will not return data
261 // Returning true is useful in a BamMultiReader setting where some BAM files may
262 // lack alignments in regions where other BAMs do have data.
263 if ( !m_hasAlignmentsInRegion )
266 // return success/failure of jump to specified region,
268 // * Index::Jump() is allowed to modify the m_hasAlignmentsInRegion flag
269 // This covers 'corner case' where a region is requested that lies beyond the last
270 // alignment on a reference. If this occurs, any subsequent calls to GetNextAlignment[Core]
271 // will not return data. BamMultiReader will still be able to successfully pull alignments
272 // from a region from multiple files even if one or more have no data.
273 return m_index->Jump(reader, m_region, &m_hasAlignmentsInRegion);