libStatGen Software 1
BamInterface Class Reference
Inheritance diagram for BamInterface:
Collaboration diagram for BamInterface:

Public Member Functions

virtual bool readHeader (IFILE filePtr, SamFileHeader &header, SamStatus &samStatus)
 
virtual bool writeHeader (IFILE filePtr, SamFileHeader &header, SamStatus &samStatus)
 
virtual void readRecord (IFILE filePtr, SamFileHeader &header, SamRecord &record, SamStatus &samStatus)
 
virtual SamStatus::Status writeRecord (IFILE filePtr, SamFileHeader &header, SamRecord &record, SamRecord::SequenceTranslation translation)
 
- Public Member Functions inherited from GenericSamInterface
virtual bool readHeader (IFILE filePtr, SamFileHeader &header, SamStatus &status)=0
 
virtual bool writeHeader (IFILE filePtr, SamFileHeader &header, SamStatus &status)=0
 
virtual void readRecord (IFILE filePtr, SamFileHeader &header, SamRecord &record, SamStatus &samStatus)=0
 
virtual SamStatus::Status writeRecord (IFILE filePtr, SamFileHeader &header, SamRecord &record, SamRecord::SequenceTranslation translation)=0
 
virtual bool isEOF (IFILE filePtr)
 

Detailed Description

Definition at line 23 of file BamInterface.h.

Constructor & Destructor Documentation

◆ BamInterface()

BamInterface::BamInterface ( )

Definition at line 21 of file BamInterface.cpp.

22{
23}

◆ ~BamInterface()

BamInterface::~BamInterface ( )

Definition at line 26 of file BamInterface.cpp.

27{
28}

Member Function Documentation

◆ readHeader()

bool BamInterface::readHeader ( IFILE  filePtr,
SamFileHeader header,
SamStatus samStatus 
)
virtual

Implements GenericSamInterface.

Definition at line 32 of file BamInterface.cpp.

34{
35 if(filePtr == NULL)
36 {
37 // File is not open, return false.
38 status.setStatus(SamStatus::FAIL_ORDER,
39 "Cannot read header since the file pointer is null");
40 return(false);
41 }
42 if(filePtr->isOpen() == false)
43 {
44 status.setStatus(SamStatus::FAIL_ORDER,
45 "Cannot read header since the file is not open");
46 return(false);
47 }
48
49 // Clear the passed in header.
50 header.resetHeader();
51
52 int32_t headerLength;
53 int readSize = ifread(filePtr, &headerLength, sizeof(headerLength));
54
55 if(readSize != sizeof(headerLength))
56 {
57 String errMsg = "Failed to read the BAM header length, read ";
58 errMsg += readSize;
59 errMsg += " bytes instead of ";
60 errMsg += (unsigned int)sizeof(headerLength);
61 status.setStatus(SamStatus::FAIL_IO, errMsg.c_str());
62 return(false);
63 }
64
65 String headerStr;
66 if(headerLength > 0)
67 {
68 // Read the header.
69 readSize =
70 ifread(filePtr, headerStr.LockBuffer(headerLength + 1), headerLength);
71 headerStr[headerLength] = 0;
72 headerStr.UnlockBuffer();
73 if(readSize != headerLength)
74 {
75 // Failed to read the header.
76 status.setStatus(SamStatus::FAIL_IO, "Failed to read the BAM header.");
77 return(false);
78 }
79 }
80
81 // Parse the header that was read.
82 if(!header.addHeader(headerStr))
83 {
84 // Status is set in the method on failure.
85 status.setStatus(SamStatus::FAIL_PARSE, header.getErrorMessage());
86 return(false);
87 }
88
89 int referenceCount;
90 // Read the number of references sequences.
91 ifread(filePtr, &referenceCount, sizeof(int));
92
93 // Get and clear the reference info so it can be set
94 // from the bam reference table.
95 SamReferenceInfo& refInfo =
96 header.getReferenceInfoForBamInterface();
97 refInfo.clear();
98
99 CharBuffer refName;
100
101 // Read each reference sequence
102 for (int i = 0; i < referenceCount; i++)
103 {
104 int nameLength;
105 int rc;
106 // Read the length of the reference name.
107 rc = ifread(filePtr, &nameLength, sizeof(int));
108 if(rc != sizeof(int))
109 {
110 status.setStatus(SamStatus::FAIL_IO,
111 "Failed to read the BAM reference dictionary.");
112 return(false);
113 }
114
115 // Read the name.
116 refName.readFromFile(filePtr, nameLength);
117
118 // Read the length of the reference sequence.
119 int32_t refLen;
120 rc = ifread(filePtr, &refLen, sizeof(int));
121
122 if(rc != sizeof(int)) {
123 status.setStatus(SamStatus::FAIL_IO,
124 "Failed to read the BAM reference dictionary.");
125 return(false);
126 }
127
128 refInfo.add(refName.c_str(), refLen);
129 }
130
131 // Successfully read the file.
132 return(true);
133}
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition: InputFile.h:600
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition: InputFile.h:423
const char * getErrorMessage()
Get the failure message if a method returned failure.
void resetHeader()
Initialize the header.
bool addHeader(const char *header)
Add a header that is already preformatted in a const char*.
Class for tracking the reference information mapping between the reference ids and the reference name...
void clear()
Reset this reference info.
void add(const char *referenceSequenceName, int32_t referenceSequenceLength)
Add reference sequence name and reference sequence length.
@ FAIL_IO
method failed due to an I/O issue.
Definition: StatGenStatus.h:37
@ FAIL_PARSE
failed to parse a record/header - invalid format.
Definition: StatGenStatus.h:42
@ FAIL_ORDER
FAIL_ORDER: method failed because it was called out of order, like trying to read a file without open...
Definition: StatGenStatus.h:41

◆ readRecord()

void BamInterface::readRecord ( IFILE  filePtr,
SamFileHeader header,
SamRecord record,
SamStatus samStatus 
)
virtual

Implements GenericSamInterface.

Definition at line 212 of file BamInterface.cpp.

215{
216 // TODO - need to validate there are @SQ lines in both sam/bam - MAYBE!
217
218 // SetBufferFromFile will reset the record prior to reading a new one.
219 if(record.setBufferFromFile(filePtr, header) != SamStatus::SUCCESS)
220 {
221 // Failed, so add the error message.
222 samStatus.addError(record.getStatus());
223 }
224}
SamStatus::Status setBufferFromFile(IFILE filePtr, SamFileHeader &header)
Read the BAM record from a file.
Definition: SamRecord.cpp:558
const SamStatus & getStatus()
Returns the status associated with the last method that sets the status.
Definition: SamRecord.cpp:2403
@ SUCCESS
method completed successfully.
Definition: StatGenStatus.h:32
void addError(Status newStatus, const char *newMessage)
Add the specified error message to the status message, setting the status to newStatus if the current...

◆ writeHeader()

bool BamInterface::writeHeader ( IFILE  filePtr,
SamFileHeader header,
SamStatus samStatus 
)
virtual

Implements GenericSamInterface.

Definition at line 136 of file BamInterface.cpp.

138{
139 if((filePtr == NULL) || (filePtr->isOpen() == false))
140 {
141 // File is not open, return false.
142 status.setStatus(SamStatus::FAIL_ORDER,
143 "Cannot write header since the file pointer is null");
144 return(false);
145 }
146
147 char magic[4];
148 magic[0] = 'B';
149 magic[1] = 'A';
150 magic[2] = 'M';
151 magic[3] = 1;
152
153 // Write magic to the file.
154 ifwrite(filePtr, magic, 4);
155
156 ////////////////////////////////
157 // Write the header to the file.
158 ////////////////////////////////
159 // Construct a string containing the entire header.
160 std::string headerString = "";
161 header.getHeaderString(headerString);
162
163 int32_t headerLen = headerString.length();
164 int numWrite = 0;
165
166 // Write the header length.
167 numWrite = ifwrite(filePtr, &headerLen, sizeof(int32_t));
168 if(numWrite != sizeof(int32_t))
169 {
170 status.setStatus(SamStatus::FAIL_IO,
171 "Failed to write the BAM header length.");
172 return(false);
173 }
174
175 // Write the header to the file.
176 numWrite = ifwrite(filePtr, headerString.c_str(), headerLen);
177 if(numWrite != headerLen)
178 {
179 status.setStatus(SamStatus::FAIL_IO,
180 "Failed to write the BAM header.");
181 return(false);
182 }
183
184 ////////////////////////////////////////////////////////
185 // Write the Reference Information.
186 const SamReferenceInfo& refInfo = header.getReferenceInfo();
187
188 // Get the number of sequences.
189 int32_t numSeq = refInfo.getNumEntries();
190 ifwrite(filePtr, &numSeq, sizeof(int32_t));
191
192 // Write each reference sequence
193 for (int i = 0; i < numSeq; i++)
194 {
195 const char* refName = refInfo.getReferenceName(i);
196 // Add one for the null value.
197 int32_t nameLength = strlen(refName) + 1;
198 // Write the length of the reference name.
199 ifwrite(filePtr, &nameLength, sizeof(int32_t));
200
201 // Write the name.
202 ifwrite(filePtr, refName, nameLength);
203 // Write the length of the reference sequence.
204 int32_t refLen = refInfo.getReferenceLength(i);
205 ifwrite(filePtr, &refLen, sizeof(int32_t));
206 }
207
208 return(true);
209}
unsigned int ifwrite(IFILE file, const void *buffer, unsigned int size)
Write the specified number of bytes from the specified buffer into the file.
Definition: InputFile.h:669
const SamReferenceInfo & getReferenceInfo() const
Get the Reference Information.
bool getHeaderString(std::string &header) const
Set the passed in string to the entire header string, clearing its current contents.
int32_t getNumEntries() const
Get the number of entries contained here.
const char * getReferenceName(int index) const
Return the reference name at the specified index, returning "" if the index is out of bounds.
int32_t getReferenceLength(int index) const
Return the reference length at the specified index, returning 0 if the index is out of bounds.

◆ writeRecord()

SamStatus::Status BamInterface::writeRecord ( IFILE  filePtr,
SamFileHeader header,
SamRecord record,
SamRecord::SequenceTranslation  translation 
)
virtual

Implements GenericSamInterface.

Definition at line 226 of file BamInterface.cpp.

230{
231 // Write the file, returning the status.
232 return(record.writeRecordBuffer(filePtr, translation));
233}
SamStatus::Status writeRecordBuffer(IFILE filePtr)
Write the record as a BAM into the specified already opened file.
Definition: SamRecord.cpp:1237

The documentation for this class was generated from the following files: