18 #include "BamInterface.h"
19 #include "CharBuffer.h"
21 BamInterface::BamInterface()
26 BamInterface::~BamInterface()
39 "Cannot read header since the file pointer is null");
42 if(filePtr->
isOpen() ==
false)
45 "Cannot read header since the file is not open");
53 int readSize =
ifread(filePtr, &headerLength,
sizeof(headerLength));
55 if(readSize !=
sizeof(headerLength))
57 String errMsg =
"Failed to read the BAM header length, read ";
59 errMsg +=
" bytes instead of ";
60 errMsg += (
unsigned int)
sizeof(headerLength);
70 ifread(filePtr, headerStr.LockBuffer(headerLength + 1), headerLength);
71 headerStr[headerLength] = 0;
72 headerStr.UnlockBuffer();
73 if(readSize != headerLength)
91 ifread(filePtr, &referenceCount,
sizeof(
int));
96 header.getReferenceInfoForBamInterface();
102 for (
int i = 0; i < referenceCount; i++)
107 rc =
ifread(filePtr, &nameLength,
sizeof(
int));
108 if(rc !=
sizeof(
int))
111 "Failed to read the BAM reference dictionary.");
116 refName.readFromFile(filePtr, nameLength);
120 rc =
ifread(filePtr, &refLen,
sizeof(
int));
122 if(rc !=
sizeof(
int)) {
124 "Failed to read the BAM reference dictionary.");
128 refInfo.
add(refName.c_str(), refLen);
139 if((filePtr == NULL) || (filePtr->
isOpen() ==
false))
143 "Cannot write header since the file pointer is null");
160 std::string headerString =
"";
163 int32_t headerLen = headerString.length();
167 numWrite =
ifwrite(filePtr, &headerLen,
sizeof(int32_t));
168 if(numWrite !=
sizeof(int32_t))
171 "Failed to write the BAM header length.");
176 numWrite =
ifwrite(filePtr, headerString.c_str(), headerLen);
177 if(numWrite != headerLen)
180 "Failed to write the BAM header.");
190 ifwrite(filePtr, &numSeq,
sizeof(int32_t));
193 for (
int i = 0; i < numSeq; i++)
197 int32_t nameLength = strlen(refName) + 1;
199 ifwrite(filePtr, &nameLength,
sizeof(int32_t));
202 ifwrite(filePtr, refName, nameLength);
205 ifwrite(filePtr, &refLen,
sizeof(int32_t));
void clear()
Reset this reference info.
int32_t getNumEntries() const
Get the number of entries contained here.
SequenceTranslation
Enum containing the settings on how to translate the sequence if a reference is available.
int32_t getReferenceLength(int index) const
Return the reference length at the specified index, returning 0 if the index is out of bounds.
Class for tracking the reference information mapping between the reference ids and the reference name...
@ SUCCESS
method completed successfully.
This class is used to track the status results of some methods in the BAM classes.
@ FAIL_PARSE
failed to parse a record/header - invalid format.
SamStatus::Status writeRecordBuffer(IFILE filePtr)
Write the record as a BAM into the specified already opened file.
SamStatus::Status setBufferFromFile(IFILE filePtr, SamFileHeader &header)
Read the BAM record from a file.
void setStatus(Status newStatus, const char *newMessage)
Set the status with the specified status enum and message.
Status
Return value enum for StatGenFile methods.
const SamStatus & getStatus()
Returns the status associated with the last method that sets the status.
void addError(Status newStatus, const char *newMessage)
Add the specified error message to the status message, setting the status to newStatus if the current...
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
const char * getReferenceName(int index) const
Return the reference name at the specified index, returning "" if the index is out of bounds.
@ FAIL_IO
method failed due to an I/O issue.
@ FAIL_ORDER
FAIL_ORDER: method failed because it was called out of order, like trying to read a file without open...
void add(const char *referenceSequenceName, int32_t referenceSequenceLength)
Add reference sequence name and reference sequence length.