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

00136 {
00137     // Resetting the file will close it if it is open, and
00138     // will reset all other variables.
00139     resetFile();
00140 }

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

00471 {
00472     return(myRecordCount);
00473 }

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 120 of file GlfFile.h.

References getStatus().

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

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

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

Referenced by getNextRefSection().

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

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

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

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

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 127 of file GlfFile.h.

References GlfStatus::getStatus().

Referenced by getFailure().

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

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 134 of file GlfFile.h.

References GlfStatus::getStatusMessage().

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

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

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

Referenced by getNextRefSection().

00146 {
00147     if (myFilePtr != NULL)
00148     {
00149         // File Pointer is set, so return if eof.
00150         return(ifeof(myFilePtr));
00151     }
00152     // File pointer is not set, so return true, eof.
00153     return true;
00154 }

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, 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  ) 

Open a glf file for writing with the specified filename.

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

Definition at line 109 of file GlfFile.cpp.

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

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

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

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

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

Referenced by openForRead().

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

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

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

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

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

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

Referenced by writeRefSection().

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

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

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

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


The documentation for this class was generated from the following files:
Generated on Wed Nov 17 15:38:32 2010 for StatGen Software by  doxygen 1.6.3