libStatGen Software 1
GlfFile Class Reference

This class allows a user to easily read/write a GLF file. More...

#include <GlfFile.h>

Inheritance diagram for GlfFile:

Public Types

enum  OpenType { READ , WRITE }
 Enum for indicating whether to open the file for read or write. More...
 

Public Member Functions

 GlfFile ()
 Default Constructor. More...
 
 GlfFile (const char *filename, OpenType mode=READ)
 Constructor that opens the specified file based on the specified mode (READ/WRITE). More...
 
virtual ~GlfFile ()
 Closes the file if there is one open, adding an end marker record if there is a previous section and one has not already been written. More...
 
bool openForRead (const char *filename)
 Open a glf file for reading with the specified filename. More...
 
bool openForRead (const char *filename, GlfHeader &header)
 Open a glf file for reading with the specified filename and read the header into the specified header. More...
 
bool openForWrite (const char *filename, bool compressed=true)
 Open a glf file for writing with the specified filename. More...
 
void close ()
 Close the file if there is one open, adding an end marker record if there is a previous section and one has not already been written. More...
 
bool isEOF ()
 Returns whether or not the end of the file has been reached. More...
 
bool readHeader (GlfHeader &header)
 Reads the header section from the file and stores it in the passed in header. More...
 
bool writeHeader (GlfHeader &header)
 Writes the specified header into the file. More...
 
bool getNextRefSection (GlfRefSection &refSection)
 Gets the next reference section from the file & stores it in the passed in section, consuming records until a new section is found. More...
 
bool writeRefSection (const GlfRefSection &refSection)
 Write the reference section to the file, adding an end marker record if there is a previous section and one has not already been written. More...
 
bool getNextRecord (GlfRecord &record)
 Gets the nextrecord from the file & stores it in the passed in record. More...
 
bool writeRecord (const GlfRecord &record)
 Writes the specified record into the file. More...
 
uint32_t getCurrentRecordCount ()
 Return the number of records that have been read/written so far. More...
 
GlfStatus::Status getFailure ()
 Get the Status of the last call that sets status. More...
 
GlfStatus::Status getStatus ()
 Get the Status of the last call that sets status. More...
 
const char * getStatusMessage ()
 Get the Status of the last call that sets status. More...
 

Detailed Description

This class allows a user to easily read/write a GLF file.

Definition at line 28 of file GlfFile.h.

Member Enumeration Documentation

◆ OpenType

Enum for indicating whether to open the file for read or write.

Enumerator
READ 

open for reading.

WRITE 

open for writing.

Definition at line 32 of file GlfFile.h.

33 {
34 READ, ///< open for reading.
35 WRITE ///< open for writing.
36 };
@ READ
open for reading.
Definition: GlfFile.h:34
@ WRITE
open for writing.
Definition: GlfFile.h:35

Constructor & Destructor Documentation

◆ GlfFile() [1/2]

GlfFile::GlfFile ( )

Default Constructor.

Definition at line 23 of file GlfFile.cpp.

24 : myFilePtr(NULL),
25 myEndMarker()
26{
27 resetFile();
28}

◆ GlfFile() [2/2]

GlfFile::GlfFile ( const char *  filename,
OpenType  mode = READ 
)

Constructor that opens the specified file based on the specified mode (READ/WRITE).

Default is READ.

Parameters
filenamename of the file to open.
modemode to use for opening the file (defaults to READ).

Definition at line 33 of file GlfFile.cpp.

34 : myFilePtr(NULL),
35 myEndMarker()
36{
37 resetFile();
38
39 bool openStatus = true;
40 if(mode == READ)
41 {
42 // open the file for read.
43 openStatus = openForRead(filename);
44 }
45 else
46 {
47 // open the file for write.
48 openStatus = openForWrite(filename);
49 }
50 if(!openStatus)
51 {
52 // Failed to open the file - print error and abort.
53 fprintf(stderr, "%s\n", getStatusMessage());
54 std::cerr << "FAILURE - EXITING!!!" << std::endl;
55 exit(-1);
56 }
57}
const char * getStatusMessage()
Get the Status of the last call that sets status.
Definition: GlfFile.h:135
bool openForWrite(const char *filename, bool compressed=true)
Open a glf file for writing with the specified filename.
Definition: GlfFile.cpp:109
bool openForRead(const char *filename)
Open a glf file for reading with the specified filename.
Definition: GlfFile.cpp:66

References getStatusMessage(), openForRead(), openForWrite(), and READ.

◆ ~GlfFile()

GlfFile::~GlfFile ( )
virtual

Closes the file if there is one open, adding an end marker record if there is a previous section and one has not already been written.

Definition at line 59 of file GlfFile.cpp.

60{
61 resetFile();
62}

Member Function Documentation

◆ close()

void GlfFile::close ( )

Close the file if there is one open, adding an end marker record if there is a previous section and one has not already been written.

Definition at line 142 of file GlfFile.cpp.

143{
144 // Resetting the file will close it if it is open, and
145 // will reset all other variables.
146 resetFile();
147}

◆ getCurrentRecordCount()

uint32_t GlfFile::getCurrentRecordCount ( )

Return the number of records that have been read/written so far.

Returns
number of records that have been read/written so far.

Definition at line 483 of file GlfFile.cpp.

484{
485 return(myRecordCount);
486}

◆ getFailure()

GlfStatus::Status GlfFile::getFailure ( )
inline

Get the Status of the last call that sets status.

To remain backwards compatable - will be removed later.

Definition at line 121 of file GlfFile.h.

122 {
123 return(getStatus());
124 }
GlfStatus::Status getStatus()
Get the Status of the last call that sets status.
Definition: GlfFile.h:128

References getStatus().

◆ getNextRecord()

bool GlfFile::getNextRecord ( GlfRecord record)

Gets the nextrecord from the file & stores it in the passed in record.

Parameters
recordobject to populate with the file's next record.
Returns
true = record was successfully set. false = record not successfully set or for the endMarker record.

Definition at line 368 of file GlfFile.cpp.

369{
370 if(myIsOpenForRead == false)
371 {
372 // File is not open for read
374 "Cannot read reference section since the file is not open for reading");
375 throw(GlfException(myStatus));
376 return(false);
377 }
378
379 if(myNextSection == HEADER)
380 {
381 // The header has not yet been read.
383 "Cannot read reference section since the header has not been read.");
384 throw(GlfException(myStatus));
385 return(false);
386 }
387
388 if(myNextSection == REF_SECTION)
389 {
390 // The reference section has not yet been read.
391 // TODO - maybe just read the reference section.
393 "Cannot read record since a reference section has not been read.");
394 throw(GlfException(myStatus));
395 return(false);
396 }
397
398 // Check for end of file. If end of file, return false.
399 if(isEOF())
400 {
401 return(false);
402 }
403
404 // Read the record.
405 if(record.read(myFilePtr))
406 {
407 myStatus = GlfStatus::SUCCESS;
408 if(record.getRecordType() != 0)
409 {
410 return(true);
411 }
412 else
413 {
414 // Not an error, so no exception thrown, but no more records.
415 // The next thing is a reference section.
416 myNextSection = REF_SECTION;
417 return(false);
418 }
419 }
420
422 "Failed reading a record from the file.");
423 throw(GlfException(myStatus));
424 return(false);
425}
GlfException objects should be thrown by functions that operate on Glf files for exceptions.
Definition: GlfException.h:28
bool isEOF()
Returns whether or not the end of the file has been reached.
Definition: GlfFile.cpp:152
int getRecordType() const
Return the record type.
Definition: GlfRecord.h:126
bool read(IFILE filePtr)
Read the record from the specified file (file MUST be in the correct position for reading a record).
Definition: GlfRecord.cpp:65
void setStatus(Status newStatus, const char *newMessage)
Set the status with the specified values.
Definition: GlfStatus.cpp:74
@ FAIL_ORDER
method failed because it was called out of order, like trying to read a file without opening it for r...
Definition: GlfStatus.h:35
@ UNKNOWN
unknown result (default value should never be used)
Definition: GlfStatus.h:33
@ SUCCESS
method completed successfully.
Definition: GlfStatus.h:32

References GlfStatus::FAIL_ORDER, GlfRecord::getRecordType(), isEOF(), GlfRecord::read(), GlfStatus::setStatus(), GlfStatus::SUCCESS, and GlfStatus::UNKNOWN.

Referenced by getNextRefSection().

◆ getNextRefSection()

bool GlfFile::getNextRefSection ( GlfRefSection refSection)

Gets the next reference section from the file & stores it in the passed in section, consuming records until a new section is found.

Parameters
refSectionobject to populate with the file's next reference section.
Returns
true = section was successfully set. false = section was not successfully set.

Definition at line 240 of file GlfFile.cpp.

241{
242 if(myIsOpenForRead == false)
243 {
244 // File is not open for read
246 "Cannot read reference section since the file is not open for reading");
247 throw(GlfException(myStatus));
248 return(false);
249 }
250
251 if(myNextSection == HEADER)
252 {
253 // The header has not yet been read.
254 // TODO - maybe just read the header.
256 "Cannot read reference section since the header has not been read.");
257 throw(GlfException(myStatus));
258 return(false);
259 }
260
261 // Keep reading until the next section is found.
262 if(myNextSection == RECORD)
263 {
264 GlfRecord record;
265 while(getNextRecord(record))
266 {
267 // Nothing to do, with the record.
268 }
269 }
270
271 // Check for end of file. If end of file, return false.
272 if(isEOF())
273 {
274 return(false);
275 }
276
277 if(myNextSection != REF_SECTION)
278 {
279 // Failed reading all the records, so throw exception.
281 "Failed to get to a reference section.");
282 throw(GlfException(myStatus));
283 return(false);
284 }
285
286 // Ready to read the section:
287 if(refSection.read(myFilePtr))
288 {
289 myStatus = GlfStatus::SUCCESS;
290 // Next a record should be read.
291 myNextSection = RECORD;
292 return(true);
293 }
294
295 // If it is the EOF, just return false.
296 if(isEOF())
297 {
298 return(false);
299 }
301 "Failed reading a reference section from the file.");
302 throw(GlfException(myStatus));
303 return(false);
304}
bool getNextRecord(GlfRecord &record)
Gets the nextrecord from the file & stores it in the passed in record.
Definition: GlfFile.cpp:368
This class allows a user to easily get/set the fields in a GLF record.
Definition: GlfRecord.h:29
bool read(IFILE filePtr)
Read the refSection from the specified file (file MUST be in the correct position for reading a refSe...
@ FAIL_IO
method failed due to an I/O issue.
Definition: GlfStatus.h:34

References GlfStatus::FAIL_IO, GlfStatus::FAIL_ORDER, getNextRecord(), isEOF(), GlfRefSection::read(), GlfStatus::setStatus(), GlfStatus::SUCCESS, and GlfStatus::UNKNOWN.

◆ getStatus()

GlfStatus::Status GlfFile::getStatus ( )
inline

Get the Status of the last call that sets status.

Returns
status of the last method that sets a status.

Definition at line 128 of file GlfFile.h.

129 {
130 return(myStatus.getStatus());
131 }
Status getStatus() const
Return the enum for this status.
Definition: GlfStatus.cpp:118

References GlfStatus::getStatus().

Referenced by getFailure().

◆ getStatusMessage()

const char * GlfFile::getStatusMessage ( )
inline

Get the Status of the last call that sets status.

Returns
status message of the last method that sets a status.

Definition at line 135 of file GlfFile.h.

136 {
137 return(myStatus.getStatusMessage());
138 }
const char * getStatusMessage() const
Return the status message.
Definition: GlfStatus.cpp:125

References GlfStatus::getStatusMessage().

Referenced by GlfFile(), GlfFileReader::GlfFileReader(), and GlfFileWriter::GlfFileWriter().

◆ isEOF()

bool GlfFile::isEOF ( )

Returns whether or not the end of the file has been reached.

Returns
true = EOF; false = not eof. If the file is not open, true is returned.

Definition at line 152 of file GlfFile.cpp.

153{
154 if (myFilePtr != NULL)
155 {
156 // File Pointer is set, so return if eof.
157 return(ifeof(myFilePtr));
158 }
159 // File pointer is not set, so return true, eof.
160 return true;
161}
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition: InputFile.h:654

References ifeof().

Referenced by getNextRecord(), and getNextRefSection().

◆ openForRead() [1/2]

bool GlfFile::openForRead ( const char *  filename)

Open a glf file for reading with the specified filename.

Parameters
filenameglf file to open for reading.
Returns
true = success; false = failure.

Definition at line 66 of file GlfFile.cpp.

67{
68 // Reset for any previously operated on files.
69 resetFile();
70
71 myFilePtr = ifopen(filename, "rb");
72
73 if (myFilePtr == NULL)
74 {
75 std::string errorMessage = "Failed to Open ";
76 errorMessage += filename;
77 errorMessage += " for reading";
78 myStatus.setStatus(GlfStatus::FAIL_IO, errorMessage.c_str());
79 throw(GlfException(myStatus));
80 return(false);
81 }
82
83 myIsOpenForRead = true;
84 // Successfully opened the file.
85 myStatus = GlfStatus::SUCCESS;
86 return(true);
87}
IFILE ifopen(const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT)
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
Definition: InputFile.h:562

References GlfStatus::FAIL_IO, ifopen(), GlfStatus::setStatus(), and GlfStatus::SUCCESS.

Referenced by GlfFile(), GlfFileReader::GlfFileReader(), and openForRead().

◆ openForRead() [2/2]

bool GlfFile::openForRead ( const char *  filename,
GlfHeader header 
)

Open a glf file for reading with the specified filename and read the header into the specified header.

Parameters
filenameglf file to open for reading.
headerheader object to populate with the file's glf header.
Returns
true = success; false = failure.

Definition at line 92 of file GlfFile.cpp.

93{
94 if(!openForRead(filename))
95 {
96 return(false);
97 }
98
99 // Read the header
100 if(!readHeader(header))
101 {
102 return(false);
103 }
104 return(true);
105}
bool readHeader(GlfHeader &header)
Reads the header section from the file and stores it in the passed in header.
Definition: GlfFile.cpp:165

References openForRead(), and readHeader().

◆ openForWrite()

bool GlfFile::openForWrite ( const char *  filename,
bool  compressed = true 
)

Open a glf file for writing with the specified filename.

Parameters
filenameglf file to open for writing.
compressedwhether or not to compress the file, defaults to true
Returns
true = success; false = failure.

Definition at line 109 of file GlfFile.cpp.

110{
111 // Reset for any previously operated on files.
112 resetFile();
113
114 if(compressed)
115 {
116 myFilePtr = ifopen(filename, "wb", InputFile::BGZF);
117 }
118 else
119 {
120 myFilePtr = ifopen(filename, "wb", InputFile::UNCOMPRESSED);
121 }
122
123 if (myFilePtr == NULL)
124 {
125 std::string errorMessage = "Failed to Open ";
126 errorMessage += filename;
127 errorMessage += " for writing";
128 myStatus.setStatus(GlfStatus::FAIL_IO, errorMessage.c_str());
129 throw(GlfException(myStatus));
130 return(false);
131 }
132
133 myIsOpenForWrite = true;
134
135 // Successfully opened the file.
136 myStatus = GlfStatus::SUCCESS;
137 return(true);
138}
@ BGZF
bgzf file.
Definition: InputFile.h:48
@ UNCOMPRESSED
uncompressed file.
Definition: InputFile.h:46

References InputFile::BGZF, GlfStatus::FAIL_IO, ifopen(), GlfStatus::setStatus(), GlfStatus::SUCCESS, and InputFile::UNCOMPRESSED.

Referenced by GlfFile(), and GlfFileWriter::GlfFileWriter().

◆ readHeader()

bool GlfFile::readHeader ( GlfHeader header)

Reads the header section from the file and stores it in the passed in header.

Parameters
headerheader object to populate with the file's glf header.
Returns
true = success; false = failure.

Definition at line 165 of file GlfFile.cpp.

166{
167 if(myIsOpenForRead == false)
168 {
169 // File is not open for read
171 "Cannot read header since the file is not open for reading");
172 throw(GlfException(myStatus));
173 return(false);
174 }
175
176 if(myNextSection != HEADER)
177 {
178 // The header has already been read.
180 "Cannot read header since it has already been read.");
181 throw(GlfException(myStatus));
182 return(false);
183 }
184
185 if(header.read(myFilePtr))
186 {
187 // The header has now been successfully read.
188 myNextSection = REF_SECTION;
189 myStatus = GlfStatus::SUCCESS;
190 return(true);
191 }
193 "Failed to read the header.");
194 throw(GlfException(myStatus));
195 return(false);
196}
bool read(IFILE filePtr)
Read the header from the specified file (file MUST be in the correct position for reading the header)...
Definition: GlfHeader.cpp:80

References GlfStatus::FAIL_ORDER, GlfHeader::read(), GlfStatus::setStatus(), GlfStatus::SUCCESS, and GlfStatus::UNKNOWN.

Referenced by openForRead().

◆ writeHeader()

bool GlfFile::writeHeader ( GlfHeader header)

Writes the specified header into the file.

Parameters
headerheader object to write into the file.
Returns
true = success; false = failure.

Definition at line 200 of file GlfFile.cpp.

201{
202 if(myIsOpenForWrite == false)
203 {
204 // File is not open for write
205 // -OR-
206 // The header has already been written.
208 "Cannot write header since the file is not open for writing");
209 throw(GlfException(myStatus));
210 return(false);
211 }
212
213 if(myNextSection != HEADER)
214 {
215 // The header has already been written.
217 "Cannot write header since it has already been written");
218 throw(GlfException(myStatus));
219 return(false);
220 }
221
222 if(header.write(myFilePtr))
223 {
224 // The header has now been successfully written.
225 myNextSection = REF_SECTION;
226 myStatus = GlfStatus::SUCCESS;
227 return(true);
228 }
229
230 // return the status.
232 "Failed to write the header.");
233 throw(GlfException(myStatus));
234 return(false);
235}
bool write(IFILE filePtr) const
Write the header to the specified file.
Definition: GlfHeader.cpp:141

References GlfStatus::FAIL_ORDER, GlfStatus::setStatus(), GlfStatus::SUCCESS, GlfStatus::UNKNOWN, and GlfHeader::write().

◆ writeRecord()

bool GlfFile::writeRecord ( const GlfRecord record)

Writes the specified record into the file.

Parameters
recordrecord to write to the file.
Returns
true = success; false = failure.

Definition at line 429 of file GlfFile.cpp.

430{
431 if(myIsOpenForWrite == false)
432 {
433 // File is not open for write
434 // -OR-
435 // The header has already been written.
437 "Cannot write record since the file is not open for writing");
438 throw(GlfException(myStatus));
439 return(false);
440 }
441
442 if(myNextSection == HEADER)
443 {
444 // The header has not been written.
446 "Cannot write record since the header has not been written");
447 throw(GlfException(myStatus));
448 return(false);
449 }
450
451 if(myNextSection != RECORD)
452 {
453 // The header has not been written.
455 "Cannot write record since a reference section has not been written");
456 throw(GlfException(myStatus));
457 return(false);
458 }
459
460 if(record.write(myFilePtr))
461 {
462 myStatus = GlfStatus::SUCCESS;
463 // The record has now been successfully written.
464
465 // Check if it was the end marker - if so, set that next a
466 // reference section is expected.
467 if(record.getRecordType() == 0)
468 {
469 myNextSection = REF_SECTION;
470 }
471 return(true);
472 }
473
474 // return the status.
476 "Failed writing a record to the file.");
477 throw(GlfException(myStatus));
478 return(false);
479}
bool write(IFILE filePtr) const
Write the record to the specified file.
Definition: GlfRecord.cpp:113

References GlfStatus::FAIL_ORDER, GlfRecord::getRecordType(), GlfStatus::setStatus(), GlfStatus::SUCCESS, GlfStatus::UNKNOWN, and GlfRecord::write().

Referenced by writeRefSection().

◆ writeRefSection()

bool GlfFile::writeRefSection ( const GlfRefSection refSection)

Write the reference section to the file, adding an end marker record if there is a previous section and one has not already been written.

Parameters
refSectionreference section to write to the file.
Returns
true = succes; false = failure.

Definition at line 308 of file GlfFile.cpp.

309{
310 if(myIsOpenForWrite == false)
311 {
312 // File is not open for write
314 "Cannot write reference section since the file is not open for writing");
315 throw(GlfException(myStatus));
316 return(false);
317 }
318
319 if(myNextSection == HEADER)
320 {
321 // The header has not been written.
323 "Cannot write reference section since the header has not been written");
324 throw(GlfException(myStatus));
325 return(false);
326 }
327
328 if(myNextSection == RECORD)
329 {
330 // did not write a end marker record, so write one now.
331 if(!writeRecord(myEndMarker))
332 {
333 // Failed to write the end marker record.
335 "Failed to write end of chromosome/section marker.");
336 throw(GlfException(myStatus));
337 return(false);
338 }
339 }
340
341 if(myNextSection != REF_SECTION)
342 {
343 // Not ready to write a reference section.
345 "Not ready for a chromosome/section header.");
346 throw(GlfException(myStatus));
347 return(false);
348 }
349
350 if(refSection.write(myFilePtr))
351 {
352 myStatus = GlfStatus::SUCCESS;
353 // A reference section has now been successfully written.
354 myNextSection = RECORD;
355 return(true);
356 }
357
358 // return the status.
360 "Failed writing a reference section to the file.");
361 throw(GlfException(myStatus));
362 return(false);
363}
bool writeRecord(const GlfRecord &record)
Writes the specified record into the file.
Definition: GlfFile.cpp:429
bool write(IFILE filePtr) const
Write the refSection to the specified file.

References GlfStatus::FAIL_IO, GlfStatus::FAIL_ORDER, GlfStatus::setStatus(), GlfStatus::SUCCESS, GlfStatus::UNKNOWN, GlfRefSection::write(), and writeRecord().


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