// BamToolsIndex.cpp (c) 2010 Derek Barnett
// Marth Lab, Department of Biology, Boston College
// ---------------------------------------------------------------------------
-// Last modified: 25 October 2011 (DB)
+// Last modified: 10 November 2011 (DB)
// ---------------------------------------------------------------------------
// Provides index operations for the BamTools index format (".bti")
// ***************************************************************************
// ----------------------------
BamToolsIndex::RaiiWrapper::RaiiWrapper(void)
- : IndexStream(0)
- , Device(0)
+ : Device(0)
{ }
BamToolsIndex::RaiiWrapper::~RaiiWrapper(void) {
- if ( IndexStream ) {
- fclose(IndexStream);
- IndexStream = 0;
- }
-
if ( Device ) {
Device->Close();
delete Device;
// read version from file
const int64_t numBytesRead = m_resources.Device->Read((char*)&m_inputVersion, sizeof(m_inputVersion));
-// size_t elementsRead = fread(&m_inputVersion, sizeof(m_inputVersion), 1, m_resources.IndexStream);
-// if ( elementsRead != 1 )
if ( numBytesRead != sizeof(m_inputVersion) )
throw BamException("BamToolsIndex::CheckVersion", "could not read format version");
if ( m_isBigEndian ) SwapEndian_32(m_inputVersion);
void BamToolsIndex::CloseFile(void) {
if ( IsDeviceOpen() ) {
- fclose(m_resources.IndexStream);
- m_resources.IndexStream = 0;
-
m_resources.Device->Close();
delete m_resources.Device;
m_resources.Device = 0;
if ( m_resources.Device == 0 )
return false;
return m_resources.Device->IsOpen();
-// return ( m_resources.IndexStream != 0 );
}
// attempts to use index data to jump to @region, returns success/fail
// use file's BTI block size to set member variable
const int64_t numBytesRead = m_resources.Device->Read((char*)&m_blockSize, sizeof(m_blockSize));
-// const size_t elementsRead = fread(&m_blockSize, sizeof(m_blockSize), 1, m_resources.IndexStream);
if ( m_isBigEndian ) SwapEndian_32(m_blockSize);
-// if ( elementsRead != 1 )
if ( numBytesRead != sizeof(m_blockSize) )
throw BamException("BamToolsIndex::LoadHeader", "could not read BTI block size");
}
void BamToolsIndex::LoadNumBlocks(int& numBlocks) {
const int64_t numBytesRead = m_resources.Device->Read((char*)&numBlocks, sizeof(numBlocks));
-// const size_t elementsRead = fread(&numBlocks, sizeof(numBlocks), 1, m_resources.IndexStream);
if ( m_isBigEndian ) SwapEndian_32(numBlocks);
-// if ( elementsRead != 1 )
if ( numBytesRead != sizeof(numBlocks) )
throw BamException("BamToolsIndex::LoadNumBlocks", "could not read number of BTI blocks");
}
void BamToolsIndex::LoadNumReferences(int& numReferences) {
const int64_t numBytesRead = m_resources.Device->Read((char*)&numReferences, sizeof(numReferences));
-// const size_t elementsRead = fread(&numReferences, sizeof(numReferences), 1, m_resources.IndexStream);
if ( m_isBigEndian ) SwapEndian_32(numReferences);
-// if ( elementsRead != 1 )
if ( numBytesRead != sizeof(numReferences) )
throw BamException("BamToolsIndex::LoadNumReferences", "could not read number of references");
}
}
// attempt to open file
-// m_resources.IndexStream = fopen(filename.c_str(), mode);
m_resources.Device->Open(mode);
if ( !IsDeviceOpen() ) {
const string message = string("could not open file: ") + filename;
void BamToolsIndex::ReadBlock(BtiBlock& block) {
// read in block data members
-// size_t elementsRead = 0;
-// elementsRead += fread(&block.MaxEndPosition, sizeof(block.MaxEndPosition), 1, m_resources.IndexStream);
-// elementsRead += fread(&block.StartOffset, sizeof(block.StartOffset), 1, m_resources.IndexStream);
-// elementsRead += fread(&block.StartPosition, sizeof(block.StartPosition), 1, m_resources.IndexStream);
-
int64_t numBytesRead = 0;
numBytesRead += m_resources.Device->Read((char*)&block.MaxEndPosition, sizeof(block.MaxEndPosition));
numBytesRead += m_resources.Device->Read((char*)&block.StartOffset, sizeof(block.StartOffset));
SwapEndian_32(block.StartPosition);
}
-// if ( elementsRead != 3 )
+ // check block read ok
const int expectedBytes = sizeof(block.MaxEndPosition) +
sizeof(block.StartOffset) +
sizeof(block.StartPosition);
void BamToolsIndex::Seek(const int64_t& position, const int origin) {
if ( !m_resources.Device->Seek(position, origin) )
-// if ( fseek64(m_resources.IndexStream, position, origin) != 0 )
throw BamException("BamToolsIndex::Seek", "could not seek in BAI file");
}
int64_t BamToolsIndex::Tell(void) const {
return m_resources.Device->Tell();
-// return ftell64(m_resources.IndexStream);
}
void BamToolsIndex::WriteBlock(const BtiBlock& block) {
numBytesWritten += m_resources.Device->Write((const char*)&maxEndPosition, sizeof(maxEndPosition));
numBytesWritten += m_resources.Device->Write((const char*)&startOffset, sizeof(startOffset));
numBytesWritten += m_resources.Device->Write((const char*)&startPosition, sizeof(startPosition));
+
+ // check block written ok
const int expectedBytes = sizeof(maxEndPosition) +
sizeof(startOffset) +
sizeof(startPosition);
-
-// size_t elementsWritten = 0;
-// elementsWritten += fwrite(&maxEndPosition, sizeof(maxEndPosition), 1, m_resources.IndexStream);
-// elementsWritten += fwrite(&startOffset, sizeof(startOffset), 1, m_resources.IndexStream);
-// elementsWritten += fwrite(&startPosition, sizeof(startPosition), 1, m_resources.IndexStream);
-// if ( elementsWritten != 3 )
if ( numBytesWritten != expectedBytes )
throw BamException("BamToolsIndex::WriteBlock", "could not write BTI block");
}
void BamToolsIndex::WriteHeader(void) {
int64_t numBytesWritten = 0 ;
-// size_t elementsWritten = 0;
// write BTI index format 'magic number'
numBytesWritten += m_resources.Device->Write(BamToolsIndex::BTI_MAGIC, 4);
-// elementsWritten += fwrite(BamToolsIndex::BTI_MAGIC, 1, 4, m_resources.IndexStream);
// write BTI index format version
int32_t currentVersion = (int32_t)m_outputVersion;
if ( m_isBigEndian ) SwapEndian_32(currentVersion);
numBytesWritten += m_resources.Device->Write((const char*)¤tVersion, sizeof(currentVersion));
-// elementsWritten += fwrite(¤tVersion, sizeof(currentVersion), 1, m_resources.IndexStream);
// write block size
uint32_t blockSize = m_blockSize;
if ( m_isBigEndian ) SwapEndian_32(blockSize);
numBytesWritten += m_resources.Device->Write((const char*)&blockSize, sizeof(blockSize));
-// elementsWritten += fwrite(&blockSize, sizeof(blockSize), 1, m_resources.IndexStream);
// write number of references
int32_t numReferences = m_indexFileSummary.size();
if ( m_isBigEndian ) SwapEndian_32(numReferences);
numBytesWritten += m_resources.Device->Write((const char*)&numReferences, sizeof(numReferences));
-// elementsWritten += fwrite(&numReferences, sizeof(numReferences), 1, m_resources.IndexStream);
+ // check header written ok
const int expectedBytes = 4 +
sizeof(currentVersion) +
sizeof(blockSize) +
sizeof(numReferences);
-
-// if ( elementsWritten != 7 )
if ( numBytesWritten != expectedBytes )
throw BamException("BamToolsIndex::WriteHeader", "could not write BTI header");
}
uint32_t numBlocks = refEntry.Blocks.size();
if ( m_isBigEndian ) SwapEndian_32(numBlocks);
const int64_t numBytesWritten = m_resources.Device->Write((const char*)&numBlocks, sizeof(numBlocks));
-// const size_t elementsWritten = fwrite(&numBlocks, sizeof(numBlocks), 1, m_resources.IndexStream);
-// if ( elementsWritten != 1 )
if ( numBytesWritten != sizeof(numBlocks) )
throw BamException("BamToolsIndex::WriteReferenceEntry", "could not write number of blocks");