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

Extension of BamReader BamWriter API #124

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/api/BamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,22 @@ bool BamReader::Open(const std::string& filename) {
return d->Open(filename);
}

/*! \fn bool BamReader::Open(const std::string& filename, IBamIODevice* device)
\brief Opens a BAM file with a custom io device.

If BamReader is already opened on another file, this function closes
that file, then attempts to open requested \a filename.

\param[in] filename name of BAM file to open

\returns \c true if BAM file was opened successfully
\sa Close(), IsOpen(), OpenIndex()
*/
bool BamReader::Open(const std::string& filename, IBamIODevice* device)
{
return d->Open(filename, device);
}

/*! \fn bool BamReader::OpenIndex(const std::string& indexFilename)
\brief Opens a BAM index file.

Expand Down
5 changes: 4 additions & 1 deletion src/api/BamReader.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ***************************************************************************
// ***************************************************************************
// BamReader.h (c) 2009 Derek Barnett, Michael Str�mberg
// Marth Lab, Department of Biology, Boston College
// ---------------------------------------------------------------------------
Expand All @@ -14,6 +14,7 @@
#include "api/BamAlignment.h"
#include "api/BamIndex.h"
#include "api/SamHeader.h"
#include "api/IBamIODevice.h"
#include <string>

namespace BamTools {
Expand Down Expand Up @@ -46,6 +47,8 @@ class API_EXPORT BamReader {
bool Jump(int refID, int position = 0);
// opens a BAM file
bool Open(const std::string& filename);
// opens a BAM file with a custom io device. Device is deleted by BamReader so it must be created on a heap
bool Open(const std::string& filename, IBamIODevice* device);
// returns internal file pointer to beginning of alignment data
bool Rewind(void);
// sets the target region of interest
Expand Down
9 changes: 9 additions & 0 deletions src/api/BamWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ bool BamWriter::Open(const std::string& filename,
return d->Open(filename, samHeaderText, referenceSequences);
}

bool BamWriter::Open(const std::string& filename,
const SamHeader& samHeader,
const RefVector& referenceSequences,
IBamIODevice* device)
{
return d->Open(filename, samHeader.ToString(), referenceSequences, device);
}


/*! \fn bool BamWriter::Open(const std::string& filename,
const SamHeader& samHeader,
const RefVector& referenceSequences)
Expand Down
7 changes: 7 additions & 0 deletions src/api/BamWriter.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/IBamIODevice.h"
#include <string>

namespace BamTools {
Expand Down Expand Up @@ -54,6 +55,12 @@ class API_EXPORT BamWriter {
bool Open(const std::string& filename,
const SamHeader& samHeader,
const RefVector& referenceSequences);
// opens a BAM file for writing using a custom oi device
// Device is deleted by BamWriter so it must be created on a heap
bool Open(const std::string& filename,
const SamHeader& samHeader,
const RefVector& referenceSequences,
IBamIODevice* device);
// saves the alignment to the alignment archive
bool SaveAlignment(const BamAlignment& alignment);
// sets the output compression mode
Expand Down
4 changes: 2 additions & 2 deletions src/api/internal/bam/BamReader_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,15 @@ bool BamReaderPrivate::LocateIndex(const BamIndex::IndexType& preferredType) {
}

// opens BAM file (and index)
bool BamReaderPrivate::Open(const string& filename) {
bool BamReaderPrivate::Open(const string& filename, IBamIODevice* device) {

try {

// make sure we're starting with fresh state
Close();

// open BgzfStream
m_stream.Open(filename, IBamIODevice::ReadOnly);
m_stream.Open(filename, IBamIODevice::ReadOnly, device);

// load BAM metadata
LoadHeaderData();
Expand Down
3 changes: 2 additions & 1 deletion src/api/internal/bam/BamReader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "api/internal/bam/BamHeader_p.h"
#include "api/internal/bam/BamRandomAccessController_p.h"
#include "api/internal/io/BgzfStream_p.h"
#include "api/IBamIODevice.h"
#include <string>

namespace BamTools {
Expand All @@ -46,7 +47,7 @@ class BamReaderPrivate {
bool Close(void);
const std::string Filename(void) const;
bool IsOpen(void) const;
bool Open(const std::string& filename);
bool Open(const std::string& filename, IBamIODevice* device = 0);
bool Rewind(void);
bool SetRegion(const BamRegion& region);

Expand Down
5 changes: 3 additions & 2 deletions src/api/internal/bam/BamWriter_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ bool BamWriterPrivate::IsOpen(void) const {
// opens the alignment archive
bool BamWriterPrivate::Open(const string& filename,
const string& samHeaderText,
const RefVector& referenceSequences)
const RefVector& referenceSequences,
IBamIODevice* device)
{
try {

// open the BGZF file for writing
m_stream.Open(filename, IBamIODevice::WriteOnly);
m_stream.Open(filename, IBamIODevice::WriteOnly, device);

// write BAM file 'metadata' components
WriteMagicNumber();
Expand Down
4 changes: 3 additions & 1 deletion src/api/internal/bam/BamWriter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "api/BamAux.h"
#include "api/internal/io/BgzfStream_p.h"
#include "api/IBamIODevice.h"
#include <string>
#include <vector>

Expand All @@ -45,7 +46,8 @@ class BamWriterPrivate {
bool IsOpen(void) const;
bool Open(const std::string& filename,
const std::string& samHeaderText,
const BamTools::RefVector& referenceSequences);
const BamTools::RefVector& referenceSequences,
IBamIODevice* device = 0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably fix the formatting here. Also it looks like you should be using NULL in this repository, not 0.

bool SaveAlignment(const BamAlignment& al);
void SetWriteCompressed(bool ok);

Expand Down
12 changes: 9 additions & 3 deletions src/api/internal/io/BgzfStream_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,21 @@ bool BgzfStream::IsOpen(void) const {
return m_device->IsOpen();
}

void BgzfStream::Open(const string& filename, const IBamIODevice::OpenMode mode) {
void BgzfStream::Open(const string& filename, const IBamIODevice::OpenMode mode, IBamIODevice* device) {

// close current device if necessary
Close();
BT_ASSERT_X( (m_device == 0), "BgzfStream::Open() - unable to properly close previous IO device" );

// retrieve new IO device depending on filename
m_device = BamDeviceFactory::CreateDevice(filename);
BT_ASSERT_X( m_device, "BgzfStream::Open() - unable to create IO device from filename" );
// or use the device provided by the client
if (device == 0) {
m_device = BamDeviceFactory::CreateDevice(filename);
}
else {
m_device = device;
}
BT_ASSERT_X( m_device, "BgzfStream::Open() - unable to create IO device from filename" );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed tabs and spaces, here.


// if device fails to open
if ( !m_device->Open(mode) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/internal/io/BgzfStream_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BgzfStream {
// returns true if BgzfStream open for IO
bool IsOpen(void) const;
// opens the BGZF file
void Open(const std::string& filename, const IBamIODevice::OpenMode mode);
void Open(const std::string& filename, const IBamIODevice::OpenMode mode, IBamIODevice* device = 0);
// reads BGZF data into a byte buffer
size_t Read(char* data, const size_t dataLength);
// seek to position in BGZF file
Expand Down