Difference between revisions of "C++ Class: SamFile"

From Genome Analysis Wiki
Jump to navigationJump to search
Line 86: Line 86:
 
| Tell the class which reference ID should be read from the BAM file.  This is the index into the BAM Index list of reference information: 0 - #references.  The records for that reference id will be retrieved on each <code>ReadRecord</code> call.  When all records have been retrieved for the specified reference id, <code>ReadRecord</code> will return false until a new read section is set.
 
| Tell the class which reference ID should be read from the BAM file.  This is the index into the BAM Index list of reference information: 0 - #references.  The records for that reference id will be retrieved on each <code>ReadRecord</code> call.  When all records have been retrieved for the specified reference id, <code>ReadRecord</code> will return false until a new read section is set.
 
Pass in -1 in order to read the section of the bam file not associated with any reference ID.
 
Pass in -1 in order to read the section of the bam file not associated with any reference ID.
 +
 
Returns true if the read section was successfully set, false if not.  False is returned if the BAM Index File has not yet been read or if a BAM file is not open for reading.
 
Returns true if the read section was successfully set, false if not.  False is returned if the BAM Index File has not yet been read or if a BAM file is not open for reading.
 +
|-
 +
| <code>bool SamFile::SetReadSection(int32_t refID, int32_t start, int32_t end)</code>
 +
| Sets what part of the BAM file should be read.  Fails if this is not a BAM file &/or the index file has not yet been read.  This version will set it to only read a specific reference id and start/end position. The records for this section will be retrieved on each <code>ReadRecord</code> call.  When all records have been retrieved for the specified section, ReadRecord will return failure until a new read section is set.
 +
 +
Return Value: true = success; false = failure.
 
|}
 
|}
  

Revision as of 11:25, 30 June 2010


Reading/Writing SAM/BAM Files In Your Program

The SamFile class allows a user to easily read/write a SAM/BAM file.

The SamFile class contains additional functionality that allows a user to read specific sections of sorted & indexed BAM files. In order take advantage of this capability, the index file must be read prior to setting the read section. This logic saves the time of having to read the entire file and takes advantage of the seeking capability of BGZF files.

Future Enhancements: Add the ability to read alignments that match a given start, end position for a specific reference sequence.

This class is part of libbam.

Class Methods

Method Name Description
SamFile::SamFile() Default Constructor - initializes the variables, but does not open any files.
SamFile::SamFile(const char* filename, OpenType mode = READ) Constructor - initializes the variables, and opens the specified file for READ/WRITE based on the passed in mode. If the mode is not specified, it defaults to READ.

Aborts if the specified file could not be opened.

bool SamFile::IsEOF() bool: true if the end of file has been reached, false if not.

Be careful using this method when you are only reading a specific section since you may reach the end of your section without hitting the end of the file

bool SamFile::OpenForRead(const char* filename) Opens the specified file for reading.

Determines if it is a BAM/SAM file by reading the beginning of the file. Returns true if successfully opened reading, false if not.

bool SamFile::OpenForWrite(const char * filename) bool: true if successfully opened, false if not.

Opens as BAM file if the specified filename ends in .bam. Otherwise it is opened as a SAM file. Returns true if successfully opened for writing, false if not.

bool SamFile::ReadBamIndex(const char * filename) bool: true if the bam index file was successfully read, false if not.

Reads the specified bam index file. It must be read prior to setting a read section, for seeking and reading portions of a bam file.

void SamFile::Close() Close the file if there is one open.
bool SamFile::ReadHeader(SamFileHeader& header) Reads the header section from the file and stores it in the passed in header.

Returns true if successfully read, false if not.

bool SamFile::WriteHeader(SamFileHeader& header) Writes the specified header into the file.

Returns true if successfully written, false if not.

bool SamFile::ReadRecord(SamFileHeader& header, SamRecord& record) Reads the next record from the file and stores it in the passed in record.

If it is an indexed BAM file and SetReadSection was called, only alignments in the section specified by SetReadSection are read. If they all have already been read, this method returns false.

Validates that the record is sorted according to the value set by setSortedValidation. No sorting validation is done if specified to be unsorted, or setSortedValidation was never called.

Returns false if the record was not successfully read or not properly sorted. Returns true if successfully read and properly sorted.

bool SamFile::WriteRecord(SamFileHeader& header, SamRecord& record) Writes the specified record into the file.

Validates that the record is sorted according to the value set by setSortedValidation. No sorting validation is done if specified to be unsorted, or setSortedValidation was never called. Returns false and does not write the record if the record was not properly sorted.

Returns false if the record was not properly sorted or not successfully written. Returns true if properly sorted and successfully written.

void SamFile::setSortedValidation(SortedType sortType)<\code> Set the flag to validate that the file is sorted as it is read/written. Must be called after the file has been opened.

sortType specifies the type of sort to be checked for.

uint32_t SamFile::GetCurrentRecordCount() Return the number of records that have been read/written so far.
SamStatus::Status SamFile::GetStatus() Get the status result of the last status reporting method call.
const char* SamFile::GetStatusMessage() Get the Status Message of the last call that sets status.
DEPRECATED: SamStatus::Status SamFile::GetFailure() Get the type of failure that occurred on a method failure.
bool SamFile::SetReadSection(int32_t refID) Tell the class which reference ID should be read from the BAM file. This is the index into the BAM Index list of reference information: 0 - #references. The records for that reference id will be retrieved on each ReadRecord call. When all records have been retrieved for the specified reference id, ReadRecord will return false until a new read section is set.

Pass in -1 in order to read the section of the bam file not associated with any reference ID.

Returns true if the read section was successfully set, false if not. False is returned if the BAM Index File has not yet been read or if a BAM file is not open for reading.

bool SamFile::SetReadSection(int32_t refID, int32_t start, int32_t end) Sets what part of the BAM file should be read. Fails if this is not a BAM file &/or the index file has not yet been read. This version will set it to only read a specific reference id and start/end position. The records for this section will be retrieved on each ReadRecord call. When all records have been retrieved for the specified section, ReadRecord will return failure until a new read section is set.

Return Value: true = success; false = failure.

Class Enums

enum OpenType
Enum Value Description
READ Open the file for read.
WRITE Open the file for write.
enum SortedType
Enum Value Description
UNSORTED Do not do any sorting validation.
FLAG Validate that the file is sorted by the type specified in the SO Tag of the file's header.
COORDINATE Validate that the file is sorted by Coordinate.
QUERY_NAME Validate that the file is sorted by Query Name.


Child Classes

SamFileReader

Additional Child Class Methods
Method Name Description
SamFileReader::SamFileReader(const char* filename) Constructor - initializes the variables, and opens the specified file for reading.

Aborts if the specified file could not be opened.

SamFileWriter

Additional Child Class Methods
Method Name Description
SamFileWriter::SamFileWriter(const char* filename) Constructor - initializes the variables, and opens the specified file for writing.

Aborts if the specified file could not be opened.

Usage Examples

Sam Library Usage Examples