Provides read access to BAM files. More...
#include <BamReader.h>
Public Member Functions | |
BamReader (void) | |
constructor | |
~BamReader (void) | |
destructor | |
bool | Close (void) |
Closes the current BAM file. | |
const std::string | GetFilename (void) const |
Returns name of current BAM file. | |
bool | IsOpen (void) const |
Returns true if a BAM file is open for reading. | |
bool | Jump (int refID, int position=0) |
Performs a random-access jump within BAM file. | |
bool | Open (const std::string &filename) |
Opens a BAM file. | |
bool | Rewind (void) |
Returns the internal file pointer to the first alignment record. | |
bool | SetRegion (const BamRegion ®ion) |
Sets a target region of interest. | |
bool | SetRegion (const int &leftRefID, const int &leftPosition, const int &rightRefID, const int &rightPosition) |
Sets a target region of interest. | |
bool | GetNextAlignment (BamAlignment &alignment) |
Retrieves next available alignment. | |
bool | GetNextAlignmentCore (BamAlignment &alignment) |
Retrieves next available alignment, without populating the alignment's string data fields. | |
SamHeader | GetHeader (void) const |
Returns SAM header data. | |
std::string | GetHeaderText (void) const |
Returns SAM header data, as SAM-formatted text. | |
int | GetReferenceCount (void) const |
Returns number of reference sequences. | |
const RefVector & | GetReferenceData (void) const |
Returns all reference sequence entries. | |
int | GetReferenceID (const std::string &refName) const |
Returns the ID of the reference with this name. | |
bool | CreateIndex (const BamIndex::IndexType &type=BamIndex::STANDARD) |
Creates an index file for current BAM file. | |
bool | HasIndex (void) const |
Returns true if index data is available. | |
bool | LocateIndex (const BamIndex::IndexType &preferredType=BamIndex::STANDARD) |
Looks in BAM file's directory for a matching index file. | |
bool | OpenIndex (const std::string &indexFilename) |
Opens a BAM index file. | |
void | SetIndex (BamIndex *index) |
Sets a custom BamIndex on this reader. | |
std::string | GetErrorString (void) const |
Returns a human-readable description of the last error that occurred. |
Provides read access to BAM files.
BamReader::BamReader | ( | void | ) |
constructor
BamReader::~BamReader | ( | void | ) |
destructor
bool BamReader::Close | ( | void | ) |
bool BamReader::CreateIndex | ( | const BamIndex::IndexType & | type = BamIndex::STANDARD |
) |
Creates an index file for current BAM file.
[in] | type | file format to create, see BamIndex::IndexType for available formats |
true
if index created OK string BamReader::GetErrorString | ( | void | ) | const |
Returns a human-readable description of the last error that occurred.
This method allows elimination of STDERR pollution. Developers of client code may choose how the messages are displayed to the user, if at all.
const std::string BamReader::GetFilename | ( | void | ) | const |
SamHeader BamReader::GetHeader | ( | void | ) | const |
Returns SAM header data.
Header data is wrapped in a SamHeader object that can be conveniently queried & modified.
std::string BamReader::GetHeaderText | ( | void | ) | const |
Returns SAM header data, as SAM-formatted text.
bool BamReader::GetNextAlignment | ( | BamAlignment & | alignment | ) |
Retrieves next available alignment.
Attempts to read the next alignment record from BAM file, and checks to see if it overlaps the current region. If no region is currently set, then the next alignment available is always considered valid.
If a region has been set, via Jump() or SetRegion(), an alignment is only considered valid if it overlaps the region. If the actual 'next' alignment record in the BAM file does not overlap this region, then this function will read sequentially through the file until the next alignment that overlaps this region is found. Once the region has been exhausted (i.e. the next alignment loaded is beyond the region), the function aborts and returns false
. In this case, there is no point to continue reading, assuming properly sorted alignments.
This function fully populates all of the alignment's available data fields, including the string data fields (read name, bases, qualities, tags, filename). If only positional data (refID, position, CIGAR ops, alignment flags, etc.) are required, consider using GetNextAlignmentCore() for a significant performance boost.
[out] | alignment | destination for alignment record data |
true
if a valid alignment was found bool BamReader::GetNextAlignmentCore | ( | BamAlignment & | alignment | ) |
Retrieves next available alignment, without populating the alignment's string data fields.
Equivalent to GetNextAlignment() with respect to what is a valid overlapping alignment.
However, this method does NOT populate the alignment's string data fields (read name, bases, qualities, tags, filename). This provides a boost in speed when these fields are not required for every alignment. These fields can be populated 'lazily' (as needed) by calling BamAlignment::BuildCharData() later.
[out] | alignment | destination for alignment record data |
true
if a valid alignment was found int BamReader::GetReferenceCount | ( | void | ) | const |
Returns number of reference sequences.
const RefVector & BamReader::GetReferenceData | ( | void | ) | const |
Returns all reference sequence entries.
int BamReader::GetReferenceID | ( | const std::string & | refName | ) | const |
Returns the ID of the reference with this name.
If refName is not found, returns -1.
[in] | refName | name of reference to look up |
bool BamReader::HasIndex | ( | void | ) | const |
Returns true
if index data is available.
bool BamReader::IsOpen | ( | void | ) | const |
Returns true
if a BAM file is open for reading.
bool BamReader::Jump | ( | int | refID, | |
int | position = 0 | |||
) |
Performs a random-access jump within BAM file.
This is a convenience method, equivalent to calling SetRegion() with only a left boundary specified.
[in] | refID | left-bound reference ID |
[in] | position | left-bound position |
true
if jump was successful bool BamReader::LocateIndex | ( | const BamIndex::IndexType & | preferredType = BamIndex::STANDARD |
) |
Looks in BAM file's directory for a matching index file.
Use this function when you need an index file, and perhaps have a preferred index format, but do not depend heavily on which format actually gets loaded at runtime.
This function will defer to your preferredType whenever possible. However, if an index file of preferredType can not be found, then it will look for any other index file that corresponds to this BAM file.
If you want precise control over which index file is loaded, use OpenIndex() with the desired index filename. If that function returns false, you can use CreateIndex() to then build an index of the exact requested format.
[in] | preferredType | desired index file format, see BamIndex::IndexType for available formats |
true
if (any) index file could be found bool BamReader::Open | ( | const std::string & | filename | ) |
Opens a BAM file.
If BamReader is already opened on another file, this function closes that file, then attempts to open requested filename.
[in] | filename | name of BAM file to open |
true
if BAM file was opened successfully bool BamReader::OpenIndex | ( | const std::string & | indexFilename | ) |
Opens a BAM index file.
[in] | indexFilename | name of BAM index file to open |
true
if BAM index file was opened & data loaded successfully bool BamReader::Rewind | ( | void | ) |
Returns the internal file pointer to the first alignment record.
Useful for performing multiple sequential passes through a BAM file. Calling this function clears any prior region that may have been set.
true
if rewind operation was successful void BamReader::SetIndex | ( | BamIndex * | index | ) |
Sets a custom BamIndex on this reader.
Only necessary for custom BamIndex subclasses. Most clients should never have to use this function.
Example:
BamReader reader; reader.SetIndex(new MyCustomBamIndex);
[in] | index | custom BamIndex subclass created by client |
bool BamReader::SetRegion | ( | const int & | leftRefID, | |
const int & | leftPosition, | |||
const int & | rightRefID, | |||
const int & | rightPosition | |||
) |
Sets a target region of interest.
This is an overloaded function.
[in] | leftRefID | referenceID of region's left boundary |
[in] | leftPosition | position of region's left boundary |
[in] | rightRefID | reference ID of region's right boundary |
[in] | rightPosition | position of region's right boundary |
true
if reader was able to jump successfully to the region's left boundary bool BamReader::SetRegion | ( | const BamRegion & | region | ) |
Sets a target region of interest.
Requires that index data be available. Attempts a random-access jump in the BAM file, near region left boundary position.
Subsequent calls to GetNextAlignment() or GetNextAlignmentCore() will only return true
when alignments can be found that overlap this region.
A region with no right boundary is considered open-ended, meaning that all alignments that lie downstream of the left boundary are considered valid, continuing to the end of the BAM file.
[in] | region | desired region-of-interest to activate |
true
if reader was able to jump successfully to the region's left boundary