GlfFile Class Reference

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

#include <GlfFile.h>

Inheritance diagram for GlfFile:
Inheritance graph
[legend]
Collaboration diagram for GlfFile:
Collaboration graph
[legend]

List of all members.

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.
 GlfFile (const char *filename, OpenType mode=READ)
 Constructor that opens the specified file based on the specified mode (READ/WRITE).
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.
bool openForRead (const char *filename)
 Open a glf file for reading with the specified filename.
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.
bool openForWrite (const char *filename, bool compressed=true)
 Open a glf file for writing with the specified filename.
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.
bool isEOF ()
 Returns whether or not the end of the file has been reached.
bool readHeader (GlfHeader &header)
 Reads the header section from the file and stores it in the passed in header.
bool writeHeader (GlfHeader &header)
 Writes the specified header into the file.
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.
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.
bool getNextRecord (GlfRecord &record)
 Gets the nextrecord from the file & stores it in the passed in record.
bool writeRecord (const GlfRecord &record)
 Writes the specified record into the file.
uint32_t getCurrentRecordCount ()
 Return the number of records that have been read/written so far.
GlfStatus::Status getFailure ()
 Get the Status of the last call that sets status.
GlfStatus::Status getStatus ()
 Get the Status of the last call that sets status.
const char * getStatusMessage ()
 Get the Status of the last call that sets status.

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

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.

00033         {
00034             READ, ///< open for reading.
00035             WRITE ///< open for writing.
00036         };


Constructor & Destructor Documentation

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:
filename name of the file to open.
mode mode to use for opening the file (defaults to READ).

Definition at line 33 of file GlfFile.cpp.

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

00034     : myFilePtr(NULL),
00035       myEndMarker()
00036 {
00037     resetFile();
00038 
00039     bool openStatus = true;
00040     if(mode == READ)
00041     {
00042         // open the file for read.
00043         openStatus = openForRead(filename);
00044     }
00045     else
00046     {
00047         // open the file for write.
00048         openStatus = openForWrite(filename);
00049     }
00050     if(!openStatus)
00051     {
00052         // Failed to open the file - print error and abort.
00053         fprintf(stderr, "%s\n", getStatusMessage());
00054         std::cerr << "FAILURE - EXITING!!!" << std::endl;
00055         exit(-1);
00056     }
00057 }

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.

00060 {
00061     resetFile();
00062 }


Member Function Documentation

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.

00143 {
00144     // Resetting the file will close it if it is open, and
00145     // will reset all other variables.
00146     resetFile();
00147 }

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 477 of file GlfFile.cpp.

00478 {
00479     return(myRecordCount);
00480 }

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.

References getStatus().

00122     {
00123         return(getStatus());
00124     }

bool GlfFile::getNextRecord ( GlfRecord record  ) 

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

Parameters:
record object 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.

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

Referenced by getNextRefSection().

00369 {
00370     if(myIsOpenForRead == false)
00371     {
00372         // File is not open for read
00373         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00374                            "Cannot read reference section since the file is not open for reading");
00375         throw(GlfException(myStatus));
00376         return(false);
00377     }
00378 
00379     if(myNextSection == HEADER)
00380     {
00381         // The header has not yet been read.
00382         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00383                            "Cannot read reference section since the header has not been read.");
00384         throw(GlfException(myStatus));
00385         return(false);
00386     }
00387     
00388     if(myNextSection == REF_SECTION)
00389     {
00390         // The reference section has not yet been read.
00391         // TODO - maybe just read the reference section.
00392         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00393                            "Cannot read record since a reference section has not been read.");
00394         throw(GlfException(myStatus));
00395         return(false);
00396     }
00397 
00398     // Read the record.
00399     if(record.read(myFilePtr))
00400     {
00401         myStatus = GlfStatus::SUCCESS;
00402         if(record.getRecordType() != 0)
00403         {
00404             return(true);
00405         }
00406         else
00407         {
00408             // Not an error, so no exception thrown, but no more records.
00409             // The next thing is a reference section.
00410             myNextSection = REF_SECTION;
00411             return(false);
00412         }
00413     }
00414     
00415     myStatus.setStatus(GlfStatus::UNKNOWN, 
00416                        "Failed reading a record from the file.");
00417     throw(GlfException(myStatus));
00418     return(false);
00419 }

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:
refSection object 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.

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

00241 {
00242     if(myIsOpenForRead == false)
00243     {
00244         // File is not open for read
00245         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00246                            "Cannot read reference section since the file is not open for reading");
00247         throw(GlfException(myStatus));
00248         return(false);
00249     }
00250 
00251     if(myNextSection == HEADER)
00252     {
00253         // The header has not yet been read.
00254         // TODO - maybe just read the header.
00255         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00256                            "Cannot read reference section since the header has not been read.");
00257         throw(GlfException(myStatus));
00258         return(false);
00259     }
00260 
00261     // Keep reading until the next section is found.
00262     if(myNextSection == RECORD)
00263     {
00264         GlfRecord record;
00265         while(getNextRecord(record))
00266         {
00267             // Nothing to do, with the record.
00268         }
00269     }
00270 
00271     // Check for end of file.  If end of file, return false.
00272     if(isEOF())
00273     {
00274         return(false);
00275     }
00276 
00277     if(myNextSection != REF_SECTION)
00278     {
00279         // Failed reading all the records, so throw exception.
00280         myStatus.setStatus(GlfStatus::FAIL_IO, 
00281                            "Failed to get to a reference section.");
00282         throw(GlfException(myStatus));
00283         return(false);
00284     }
00285 
00286     // Ready to read the section:
00287     if(refSection.read(myFilePtr))
00288     {
00289         myStatus = GlfStatus::SUCCESS;
00290         // Next a record should be read.
00291         myNextSection = RECORD;
00292         return(true);
00293     }
00294 
00295     // If it is the EOF, just return false.
00296     if(isEOF())
00297     {
00298         return(false);
00299     }
00300     myStatus.setStatus(GlfStatus::UNKNOWN, 
00301                        "Failed reading a reference section from the file.");
00302     throw(GlfException(myStatus));
00303     return(false);
00304 }

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.

References GlfStatus::getStatus().

Referenced by getFailure().

00129     {
00130         return(myStatus.getStatus());
00131     }

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.

References GlfStatus::getStatusMessage().

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

00136     {
00137         return(myStatus.getStatusMessage());
00138     }

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, false is returned.

Definition at line 152 of file GlfFile.cpp.

References ifeof().

Referenced by getNextRefSection().

00153 {
00154     if (myFilePtr != NULL)
00155     {
00156         // File Pointer is set, so return if eof.
00157         return(ifeof(myFilePtr));
00158     }
00159     // File pointer is not set, so return true, eof.
00160     return true;
00161 }

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:
filename glf file to open for reading.
header header object to populate with the file's glf header.
Returns:
true = success; false = failure.

Definition at line 92 of file GlfFile.cpp.

References openForRead(), and readHeader().

00093 {
00094     if(!openForRead(filename))
00095     {
00096         return(false);
00097     }
00098 
00099     // Read the header
00100     if(!readHeader(header))
00101     {
00102         return(false);
00103     }
00104     return(true);
00105 }

bool GlfFile::openForRead ( const char *  filename  ) 

Open a glf file for reading with the specified filename.

Parameters:
filename glf file to open for reading.
Returns:
true = success; false = failure.

Definition at line 66 of file GlfFile.cpp.

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

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

00067 {
00068     // Reset for any previously operated on files.
00069     resetFile();
00070 
00071     myFilePtr = ifopen(filename, "rb");
00072    
00073     if (myFilePtr == NULL)
00074     {
00075         std::string errorMessage = "Failed to Open ";
00076         errorMessage += filename;
00077         errorMessage += " for reading";
00078         myStatus.setStatus(GlfStatus::FAIL_IO, errorMessage.c_str());
00079         throw(GlfException(myStatus));
00080         return(false);
00081     }
00082 
00083     myIsOpenForRead = true;
00084     // Successfully opened the file.
00085     myStatus = GlfStatus::SUCCESS;
00086     return(true);
00087 }

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

Open a glf file for writing with the specified filename.

Parameters:
filename glf file to open for writing.
compressed whether or not to compress the file, defaults to true
Returns:
true = success; false = failure.

Definition at line 109 of file GlfFile.cpp.

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

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

00110 {
00111     // Reset for any previously operated on files.
00112     resetFile();
00113 
00114     if(compressed)
00115     {
00116         myFilePtr = ifopen(filename, "wb", InputFile::BGZF);
00117     }
00118     else
00119     {
00120         myFilePtr = ifopen(filename, "wb", InputFile::UNCOMPRESSED);
00121     }
00122 
00123     if (myFilePtr == NULL)
00124     {
00125         std::string errorMessage = "Failed to Open ";
00126         errorMessage += filename;
00127         errorMessage += " for writing";
00128         myStatus.setStatus(GlfStatus::FAIL_IO, errorMessage.c_str());
00129         throw(GlfException(myStatus));
00130         return(false);
00131     }
00132    
00133     myIsOpenForWrite = true;
00134 
00135     // Successfully opened the file.
00136     myStatus = GlfStatus::SUCCESS;
00137     return(true);
00138 }

bool GlfFile::readHeader ( GlfHeader header  ) 

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

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

Definition at line 165 of file GlfFile.cpp.

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

Referenced by openForRead().

00166 {
00167     if(myIsOpenForRead == false)
00168     {
00169         // File is not open for read
00170         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00171                            "Cannot read header since the file is not open for reading");
00172         throw(GlfException(myStatus));
00173         return(false);
00174     }
00175 
00176     if(myNextSection != HEADER)
00177     {
00178         // The header has already been read.
00179         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00180                            "Cannot read header since it has already been read.");
00181         throw(GlfException(myStatus));
00182         return(false);
00183     }
00184 
00185     if(header.read(myFilePtr))
00186     {
00187         // The header has now been successfully read.
00188         myNextSection = REF_SECTION;
00189         myStatus = GlfStatus::SUCCESS;
00190         return(true);
00191     }
00192     myStatus.setStatus(GlfStatus::UNKNOWN, 
00193                        "Failed to read the header.");
00194     throw(GlfException(myStatus));
00195     return(false);
00196 }

bool GlfFile::writeHeader ( GlfHeader header  ) 

Writes the specified header into the file.

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

Definition at line 200 of file GlfFile.cpp.

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

00201 {
00202     if(myIsOpenForWrite == false)
00203     {
00204         // File is not open for write
00205         // -OR-
00206         // The header has already been written.
00207         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00208                            "Cannot write header since the file is not open for writing");
00209         throw(GlfException(myStatus));
00210         return(false);
00211     }
00212 
00213     if(myNextSection != HEADER)
00214     {
00215         // The header has already been written.
00216         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00217                            "Cannot write header since it has already been written");
00218         throw(GlfException(myStatus));
00219         return(false);
00220     }
00221 
00222     if(header.write(myFilePtr))
00223     {
00224         // The header has now been successfully written.
00225         myNextSection = REF_SECTION;
00226         myStatus = GlfStatus::SUCCESS;
00227         return(true);
00228     }
00229 
00230     // return the status.
00231     myStatus.setStatus(GlfStatus::UNKNOWN, 
00232                        "Failed to write the header.");
00233     throw(GlfException(myStatus));
00234     return(false);
00235 }

bool GlfFile::writeRecord ( const GlfRecord record  ) 

Writes the specified record into the file.

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

Definition at line 423 of file GlfFile.cpp.

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

Referenced by writeRefSection().

00424 {
00425     if(myIsOpenForWrite == false)
00426     {
00427         // File is not open for write
00428         // -OR-
00429         // The header has already been written.
00430         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00431                            "Cannot write record since the file is not open for writing");
00432         throw(GlfException(myStatus));
00433        return(false);
00434     }
00435 
00436     if(myNextSection == HEADER)
00437     {
00438         // The header has not been written.
00439         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00440                            "Cannot write record since the header has not been written");
00441         throw(GlfException(myStatus));
00442         return(false);
00443     }
00444 
00445     if(myNextSection != RECORD)
00446     {
00447         // The header has not been written.
00448         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00449                            "Cannot write record since a reference section has not been written");
00450         throw(GlfException(myStatus));
00451         return(false);
00452     }
00453 
00454     if(record.write(myFilePtr))
00455     {
00456         myStatus = GlfStatus::SUCCESS;
00457         // The record has now been successfully written.
00458 
00459         // Check if it was the end marker - if so, set that next a 
00460         // reference section is expected.
00461         if(record.getRecordType() == 0)
00462         {
00463             myNextSection = REF_SECTION;
00464         }
00465         return(true);
00466     }
00467 
00468     // return the status.
00469     myStatus.setStatus(GlfStatus::UNKNOWN, 
00470                        "Failed writing a record to the file.");
00471     throw(GlfException(myStatus));
00472     return(false);    
00473 }

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:
refSection reference section to write to the file.
Returns:
true = succes; false = failure.

Definition at line 308 of file GlfFile.cpp.

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

00309 {
00310     if(myIsOpenForWrite == false)
00311     {
00312         // File is not open for write
00313         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00314                            "Cannot write reference section since the file is not open for writing");
00315         throw(GlfException(myStatus));
00316         return(false);
00317     }
00318 
00319     if(myNextSection == HEADER)
00320     {
00321         // The header has not been written.
00322         myStatus.setStatus(GlfStatus::FAIL_ORDER, 
00323                            "Cannot write reference section since the header has not been written");
00324         throw(GlfException(myStatus));
00325        return(false);
00326     }
00327 
00328     if(myNextSection == RECORD)
00329     {
00330         // did not write a end marker record, so write one now.
00331         if(!writeRecord(myEndMarker))
00332         {
00333             // Failed to write the end marker record.
00334             myStatus.setStatus(GlfStatus::FAIL_IO,
00335                                "Failed to write end of chromosome/section marker.");
00336             throw(GlfException(myStatus));
00337             return(false);
00338         }
00339     }
00340 
00341     if(myNextSection != REF_SECTION)
00342     {
00343         // Not ready to write a reference section.
00344         myStatus.setStatus(GlfStatus::FAIL_IO,
00345                            "Not ready for a chromosome/section header.");
00346         throw(GlfException(myStatus));
00347         return(false);
00348     }
00349 
00350     if(refSection.write(myFilePtr))
00351     {
00352         myStatus = GlfStatus::SUCCESS;
00353         // A reference section has now been successfully written.
00354         myNextSection = RECORD;
00355         return(true);
00356     }
00357 
00358     // return the status.
00359     myStatus.setStatus(GlfStatus::UNKNOWN, 
00360                        "Failed writing a reference section to the file.");
00361     throw(GlfException(myStatus));
00362     return(false);    
00363 }


The documentation for this class was generated from the following files:
Generated on Tue Aug 23 18:19:07 2011 for libStatGen Software by  doxygen 1.6.3