Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golden Helix patches #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/api/BamAlignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct API_EXPORT BamAlignment {

//! \internal
// internal utility methods
private:
//private: Still useful
bool FindTag(const std::string& tag,
char*& pTagData,
const unsigned int& tagDataLength,
Expand All @@ -150,6 +150,7 @@ struct API_EXPORT BamAlignment {
uint32_t QueryNameLength;
uint32_t QuerySequenceLength;
bool HasCoreOnly;
int64_t FileOffset;

// constructor
BamAlignmentSupportData(void)
Expand All @@ -160,7 +161,9 @@ struct API_EXPORT BamAlignment {
, HasCoreOnly(false)
{ }
};
public: //Sometimes useful publicly
BamAlignmentSupportData SupportData;
private:
friend class Internal::BamReaderPrivate;
friend class Internal::BamWriterPrivate;

Expand Down
4 changes: 2 additions & 2 deletions src/api/BamConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ const char BAM_DNA_PAD = '*';

// zlib & BGZF constants
const char GZIP_ID1 = 31;
const char GZIP_ID2 = 139;
const char GZIP_ID2 = 139u;
const char CM_DEFLATE = 8;
const char FLG_FEXTRA = 4;
const char OS_UNKNOWN = 255;
const char OS_UNKNOWN = 255u;
const char BGZF_XLEN = 6;
const char BGZF_ID1 = 66;
const char BGZF_ID2 = 67;
Expand Down
7 changes: 6 additions & 1 deletion src/api/BamIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "api/api_global.h"
#include "api/BamAux.h"
#include "api/BamAlignment.h"
#include <string>

namespace BamTools {
Expand All @@ -20,6 +21,8 @@ namespace Internal {
class BamReaderPrivate;
} // namespace Internal

typedef bool (*CreateIndexProgressCallback)(const BamAlignment& alignment, void* cbData);

/*! \class BamTools::BamIndex
\brief Provides methods for generating & loading BAM index files.

Expand Down Expand Up @@ -49,7 +52,9 @@ class API_EXPORT BamIndex {
// index interface
public:
// builds index from associated BAM file & writes out to index file
virtual bool Create(void) =0;
virtual bool Create(std::string* indexFileName=0,
CreateIndexProgressCallback cb=0,
void* cbData=0) =0; // creates index file from BAM file

// returns a human-readable description of the last error encountered
std::string GetErrorString(void) { return m_errorString; }
Expand Down
7 changes: 5 additions & 2 deletions src/api/BamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ bool BamReader::Close(void) {
\return \c true if index created OK
\sa LocateIndex(), OpenIndex()
*/
bool BamReader::CreateIndex(const BamIndex::IndexType& type) {
return d->CreateIndex(type);
bool BamReader::CreateIndex(const BamIndex::IndexType& type,
std::string* indexFileName,
CreateIndexProgressCallback cb,
void* cbData) {
return d->CreateIndex(type, indexFileName, cb, cbData);
}

/*! \fn std::string BamReader::GetErrorString(void) const
Expand Down
5 changes: 4 additions & 1 deletion src/api/BamReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ class API_EXPORT BamReader {
// ----------------------

// creates an index file for current BAM file, using the requested index type
bool CreateIndex(const BamIndex::IndexType& type = BamIndex::STANDARD);
bool CreateIndex(const BamIndex::IndexType& type = BamIndex::STANDARD,
std::string* indexFileName=0,
CreateIndexProgressCallback cb=0,
void* cbData=0);
// returns true if index data is available
bool HasIndex(void) const;
// looks in BAM file's directory for a matching index file
Expand Down
5 changes: 5 additions & 0 deletions src/api/IBamIODevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace BamTools {

class IBamIODevice;
typedef IBamIODevice* (*CreateBamIODeviceCallback)(const std::string& source);

class API_EXPORT IBamIODevice {

// enums
Expand Down Expand Up @@ -56,6 +59,8 @@ class API_EXPORT IBamIODevice {
virtual bool IsOpen(void) const;
virtual OpenMode Mode(void) const;

static void RegisterCreatorCallback(CreateBamIODeviceCallback cb);

// internal methods
protected:
IBamIODevice(void); // hidden ctor
Expand Down
9 changes: 6 additions & 3 deletions src/api/internal/bam/BamRandomAccessController_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ void BamRandomAccessController::ClearRegion(void) {
}

bool BamRandomAccessController::CreateIndex(BamReaderPrivate* reader,
const BamIndex::IndexType& type)
{
const BamIndex::IndexType& type,
std::string* indexFileName,
CreateIndexProgressCallback cb,
void* cbData) {

// skip if reader is invalid
assert(reader);
if ( !reader->IsOpen() ) {
Expand All @@ -166,7 +169,7 @@ bool BamRandomAccessController::CreateIndex(BamReaderPrivate* reader,
}

// attempt to build index from current BamReader file
if ( !newIndex->Create() ) {
if ( !newIndex->Create(indexFileName, cb, cbData) ) {
const string indexError = newIndex->GetErrorString();
const string message = "could not create index: \n\t" + indexError;
SetErrorString("BamRandomAccessController::CreateIndex", message);
Expand Down
5 changes: 4 additions & 1 deletion src/api/internal/bam/BamRandomAccessController_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class BamRandomAccessController {

// index methods
void ClearIndex(void);
bool CreateIndex(BamReaderPrivate* reader, const BamIndex::IndexType& type);
bool CreateIndex(BamReaderPrivate* reader, const BamIndex::IndexType& type,
std::string* indexFileName,
CreateIndexProgressCallback cb,
void* cbData);
bool HasIndex(void) const;
bool IndexHasAlignmentsForReference(const int& refId);
bool LocateIndex(BamReaderPrivate* reader, const BamIndex::IndexType& preferredType);
Expand Down
13 changes: 9 additions & 4 deletions src/api/internal/bam/BamReader_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ bool BamReaderPrivate::Close(void) {
}

// creates an index file of requested type on current BAM file
bool BamReaderPrivate::CreateIndex(const BamIndex::IndexType& type) {

bool BamReaderPrivate::CreateIndex(const BamIndex::IndexType& type,
std::string* indexFileName,
CreateIndexProgressCallback cb,
void* cbData) {
// skip if BAM file not open
if ( !IsOpen() ) {
SetErrorString("BamReader::CreateIndex", "cannot create index on unopened BAM file");
return false;
}

// attempt to create index
if ( m_randomAccessController.CreateIndex(this, type) )
if ( m_randomAccessController.CreateIndex(this, type, indexFileName, cb, cbData) )
return true;
else {
const string bracError = m_randomAccessController.GetErrorString();
Expand Down Expand Up @@ -230,10 +232,13 @@ void BamReaderPrivate::LoadHeaderData(void) {
// populates BamAlignment with alignment data under file pointer, returns success/fail
bool BamReaderPrivate::LoadNextAlignment(BamAlignment& alignment) {

alignment.SupportData.FileOffset = m_stream.Tell(); //Get the offset before we progress the stream

// read in the 'block length' value, make sure it's not zero
char buffer[sizeof(uint32_t)];
fill_n(buffer, sizeof(uint32_t), 0);
m_stream.Read(buffer, sizeof(uint32_t));
if(m_stream.Read(buffer, sizeof(uint32_t)) < sizeof(uint32_t))
return false;
alignment.SupportData.BlockLength = BamTools::UnpackUnsignedInt(buffer);
if ( m_isBigEndian ) BamTools::SwapEndian_32(alignment.SupportData.BlockLength);
if ( alignment.SupportData.BlockLength == 0 )
Expand Down
5 changes: 4 additions & 1 deletion src/api/internal/bam/BamReader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class BamReaderPrivate {
int GetReferenceID(const std::string& refName) const;

// index operations
bool CreateIndex(const BamIndex::IndexType& type);
bool CreateIndex(const BamIndex::IndexType& type,
std::string* indexFileName,
CreateIndexProgressCallback cb,
void* cbData);
bool HasIndex(void) const;
bool LocateIndex(const BamIndex::IndexType& preferredType);
bool OpenIndex(const std::string& indexFilename);
Expand Down
21 changes: 19 additions & 2 deletions src/api/internal/index/BamStandardIndex_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ void BamStandardIndex::CloseFile(void) {
}

// builds index from associated BAM file & writes out to index file
bool BamStandardIndex::Create(void) {
bool BamStandardIndex::Create(std::string* indexFileNamePreferred,
CreateIndexProgressCallback progressCB,
void* progressCBData) {

// skip if BamReader is invalid or not open
if ( m_reader == 0 || !m_reader->IsOpen() ) {
Expand All @@ -281,7 +283,11 @@ bool BamStandardIndex::Create(void) {
try {

// open new index file (read & write)
string indexFilename = m_reader->Filename() + Extension();
string indexFilename;
if(indexFileNamePreferred != 0)
indexFilename = *indexFileNamePreferred;
else
indexFilename = m_reader->Filename() + Extension();
OpenFile(indexFilename, IBamIODevice::ReadWrite);

// initialize BaiFileSummary with number of references
Expand All @@ -306,6 +312,17 @@ bool BamStandardIndex::Create(void) {
BaiReferenceEntry refEntry;
while ( m_reader->LoadNextAlignment(al) ) {

if(progressCB){
//Call progress CB for every read
if(!progressCB(al, progressCBData)){
//progressCB can return false indicating user canceled the index operation
CloseFile();
remove(indexFilename.c_str());
m_reader->Rewind();
return false;
}
}

// changed to new reference
if ( lastRefID != al.RefID ) {

Expand Down
4 changes: 3 additions & 1 deletion src/api/internal/index/BamStandardIndex_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ class BamStandardIndex : public BamIndex {
// BamIndex implementation
public:
// builds index from associated BAM file & writes out to index file
bool Create(void);
bool Create(std::string* indexFileName=0,
CreateIndexProgressCallback cb=0,
void* cbData=0);
// returns whether reference has alignments or no
bool HasAlignments(const int& referenceID) const;
// attempts to use index data to jump to @region, returns success/fail
Expand Down
21 changes: 19 additions & 2 deletions src/api/internal/index/BamToolsIndex_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ void BamToolsIndex::CloseFile(void) {
}

// builds index from associated BAM file & writes out to index file
bool BamToolsIndex::Create(void) {
bool BamToolsIndex::Create(std::string* indexFileNamePreferred,
CreateIndexProgressCallback progressCB,
void* progressCBData) {

// skip if BamReader is invalid or not open
if ( m_reader == 0 || !m_reader->IsOpen() ) {
Expand All @@ -149,7 +151,11 @@ bool BamToolsIndex::Create(void) {

try {
// open new index file (read & write)
const string indexFilename = m_reader->Filename() + Extension();
string indexFilename;
if(indexFileNamePreferred != 0)
indexFilename = *indexFileNamePreferred;
else
indexFilename = m_reader->Filename() + Extension();
OpenFile(indexFilename, IBamIODevice::ReadWrite);

// initialize BtiFileSummary with number of references
Expand All @@ -172,6 +178,17 @@ bool BamToolsIndex::Create(void) {
BtiReferenceEntry refEntry;
while ( m_reader->LoadNextAlignment(al) ) {

if(progressCB){
//Call progress CB every read
if(!progressCB(al, progressCBData)){
//progressCB can return false indicating user canceled the index operation
CloseFile();
remove(indexFilename.c_str());
m_reader->Rewind();
return false;
}
}

// if moved to new reference
if ( al.RefID != blockRefId ) {

Expand Down
4 changes: 3 additions & 1 deletion src/api/internal/index/BamToolsIndex_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ class BamToolsIndex : public BamIndex {
// BamIndex implementation
public:
// builds index from associated BAM file & writes out to index file
bool Create(void);
bool Create(std::string* indexFileName=0,
CreateIndexProgressCallback cb=0,
void* cbData=0);
// returns whether reference has alignments or no
bool HasAlignments(const int& referenceID) const;
// attempts to use index data to jump to @region, returns success/fail
Expand Down
28 changes: 18 additions & 10 deletions src/api/internal/io/BamDeviceFactory_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@

#include "api/internal/io/BamDeviceFactory_p.h"
#include "api/internal/io/BamFile_p.h"
#include "api/internal/io/BamFtp_p.h"
#include "api/internal/io/BamHttp_p.h"
#include "api/internal/io/BamPipe_p.h"
using namespace BamTools;
using namespace BamTools::Internal;

#include <iostream>
#include <vector>
using namespace std;

static std::vector<CreateBamIODeviceCallback> m_callBacks;

IBamIODevice* BamDeviceFactory::CreateDevice(const string& source) {

for(int i=0; i<m_callBacks.size(); i++){
IBamIODevice* device = m_callBacks[i](source);
if(device) //Callback did something with this source URL
return device;
}

// check for requested pipe
if ( source == "-" || source == "stdin" || source == "stdout" )
return new BamPipe;

// check for HTTP prefix
if ( source.find("http://") == 0 )
return new BamHttp(source);

// check for FTP prefix
if ( source.find("ftp://") == 0 )
return new BamFtp(source);

// otherwise assume a "normal" file
return new BamFile(source);
}

void BamDeviceFactory::RegisterCreatorCallback(CreateBamIODeviceCallback cb)
{
m_callBacks.push_back(cb);
}

void IBamIODevice::RegisterCreatorCallback(CreateBamIODeviceCallback cb){
BamDeviceFactory::RegisterCreatorCallback(cb);
}
2 changes: 2 additions & 0 deletions src/api/internal/io/BamDeviceFactory_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace Internal {
class BamDeviceFactory {
public:
static IBamIODevice* CreateDevice(const std::string& source);
static void RegisterCreatorCallback(CreateBamIODeviceCallback cb);

};

} // namespace Internal
Expand Down
15 changes: 15 additions & 0 deletions src/api/internal/io/BamFtp_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ using namespace BamTools::Internal;
#include <vector>
using namespace std;

static BamTools::IBamIODevice* createBamFtpDeviceCb(const std::string& source)
{
if ( source.find("ftp://") == 0 )
return new BamFtp(source);
return 0;
}

struct RegisterBamFtpCallback{
RegisterBamFtpCallback(){
BamTools::IBamIODevice::RegisterCreatorCallback(createBamFtpDeviceCb);
}
};
static RegisterBamFtpCallback _cbInitializer; //Global ctor sets up BamFtp as device


namespace BamTools {
namespace Internal {

Expand Down
Loading