GlfFile.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdexcept>
00018 #include <stdlib.h>
00019 #include "GlfFile.h"
00020 #include "GlfException.h"
00021
00022
00023 GlfFile::GlfFile()
00024 : myFilePtr(NULL),
00025 myEndMarker()
00026 {
00027 resetFile();
00028 }
00029
00030
00031
00032
00033 GlfFile::GlfFile(const char* filename, OpenType mode)
00034 : myFilePtr(NULL),
00035 myEndMarker()
00036 {
00037 resetFile();
00038
00039 bool openStatus = true;
00040 if(mode == READ)
00041 {
00042
00043 openStatus = openForRead(filename);
00044 }
00045 else
00046 {
00047
00048 openStatus = openForWrite(filename);
00049 }
00050 if(!openStatus)
00051 {
00052
00053 fprintf(stderr, "%s\n", getStatusMessage());
00054 std::cerr << "FAILURE - EXITING!!!" << std::endl;
00055 exit(-1);
00056 }
00057 }
00058
00059 GlfFile::~GlfFile()
00060 {
00061 resetFile();
00062 }
00063
00064
00065
00066 bool GlfFile::openForRead(const char * filename)
00067 {
00068
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
00085 myStatus = GlfStatus::SUCCESS;
00086 return(true);
00087 }
00088
00089
00090
00091
00092 bool GlfFile::openForRead(const char * filename, GlfHeader& header)
00093 {
00094 if(!openForRead(filename))
00095 {
00096 return(false);
00097 }
00098
00099
00100 if(!readHeader(header))
00101 {
00102 return(false);
00103 }
00104 return(true);
00105 }
00106
00107
00108
00109 bool GlfFile::openForWrite(const char * filename, bool compressed)
00110 {
00111
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
00136 myStatus = GlfStatus::SUCCESS;
00137 return(true);
00138 }
00139
00140
00141
00142 void GlfFile::close()
00143 {
00144
00145
00146 resetFile();
00147 }
00148
00149
00150
00151
00152 bool GlfFile::isEOF()
00153 {
00154 if (myFilePtr != NULL)
00155 {
00156
00157 return(ifeof(myFilePtr));
00158 }
00159
00160 return true;
00161 }
00162
00163
00164
00165 bool GlfFile::readHeader(GlfHeader& header)
00166 {
00167 if(myIsOpenForRead == false)
00168 {
00169
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
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
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 }
00197
00198
00199
00200 bool GlfFile::writeHeader(GlfHeader& header)
00201 {
00202 if(myIsOpenForWrite == false)
00203 {
00204
00205
00206
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
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
00225 myNextSection = REF_SECTION;
00226 myStatus = GlfStatus::SUCCESS;
00227 return(true);
00228 }
00229
00230
00231 myStatus.setStatus(GlfStatus::UNKNOWN,
00232 "Failed to write the header.");
00233 throw(GlfException(myStatus));
00234 return(false);
00235 }
00236
00237
00238
00239
00240 bool GlfFile::getNextRefSection(GlfRefSection& refSection)
00241 {
00242 if(myIsOpenForRead == false)
00243 {
00244
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
00254
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
00262 if(myNextSection == RECORD)
00263 {
00264 GlfRecord record;
00265 while(getNextRecord(record))
00266 {
00267
00268 }
00269 }
00270
00271
00272 if(isEOF())
00273 {
00274 return(false);
00275 }
00276
00277 if(myNextSection != REF_SECTION)
00278 {
00279
00280 myStatus.setStatus(GlfStatus::FAIL_IO,
00281 "Failed to get to a reference section.");
00282 throw(GlfException(myStatus));
00283 return(false);
00284 }
00285
00286
00287 if(refSection.read(myFilePtr))
00288 {
00289 myStatus = GlfStatus::SUCCESS;
00290
00291 myNextSection = RECORD;
00292 return(true);
00293 }
00294
00295
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 }
00305
00306
00307
00308 bool GlfFile::writeRefSection(const GlfRefSection& refSection)
00309 {
00310 if(myIsOpenForWrite == false)
00311 {
00312
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
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
00331 if(!writeRecord(myEndMarker))
00332 {
00333
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
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
00354 myNextSection = RECORD;
00355 return(true);
00356 }
00357
00358
00359 myStatus.setStatus(GlfStatus::UNKNOWN,
00360 "Failed writing a reference section to the file.");
00361 throw(GlfException(myStatus));
00362 return(false);
00363 }
00364
00365
00366
00367
00368 bool GlfFile::getNextRecord(GlfRecord& record)
00369 {
00370 if(myIsOpenForRead == false)
00371 {
00372
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
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
00391
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
00399 if(record.read(myFilePtr))
00400 {
00401 myStatus = GlfStatus::SUCCESS;
00402 if(record.getRecordType() != 0)
00403 {
00404 return(true);
00405 }
00406 else
00407 {
00408
00409
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 }
00420
00421
00422
00423 bool GlfFile::writeRecord(const GlfRecord& record)
00424 {
00425 if(myIsOpenForWrite == false)
00426 {
00427
00428
00429
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
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
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
00458
00459
00460
00461 if(record.getRecordType() == 0)
00462 {
00463 myNextSection = REF_SECTION;
00464 }
00465 return(true);
00466 }
00467
00468
00469 myStatus.setStatus(GlfStatus::UNKNOWN,
00470 "Failed writing a record to the file.");
00471 throw(GlfException(myStatus));
00472 return(false);
00473 }
00474
00475
00476
00477 uint32_t GlfFile::getCurrentRecordCount()
00478 {
00479 return(myRecordCount);
00480 }
00481
00482
00483
00484 void GlfFile::resetFile()
00485 {
00486
00487 if (myFilePtr != NULL)
00488 {
00489
00490
00491
00492 if(myNextSection == RECORD)
00493 {
00494 if(!writeRecord(myEndMarker))
00495 {
00496
00497 myStatus.setStatus(GlfStatus::FAIL_IO,
00498 "Failed to write end of chromosome/section marker.");
00499 throw(GlfException(myStatus));
00500 }
00501 }
00502 ifclose(myFilePtr);
00503 myFilePtr = NULL;
00504 }
00505
00506 myIsOpenForRead = false;
00507 myIsOpenForWrite = false;
00508 myRecordCount = 0;
00509 myStatus = GlfStatus::SUCCESS;
00510 myNextSection = HEADER;
00511 }
00512
00513
00514
00515 GlfFileReader::GlfFileReader()
00516 {
00517 }
00518
00519
00520
00521 GlfFileReader::GlfFileReader(const char* filename)
00522 {
00523 if(!openForRead(filename))
00524 {
00525
00526 fprintf(stderr, "%s\n", getStatusMessage());
00527 std::cerr << "FAILURE - EXITING!!!" << std::endl;
00528 exit(-1);
00529 }
00530 }
00531
00532
00533 GlfFileReader::~GlfFileReader()
00534 {
00535 }
00536
00537
00538
00539 GlfFileWriter::GlfFileWriter()
00540 {
00541 }
00542
00543
00544
00545 GlfFileWriter::GlfFileWriter(const char* filename)
00546 {
00547 if(!openForWrite(filename))
00548 {
00549
00550 fprintf(stderr, "%s\n", getStatusMessage());
00551 std::cerr << "FAILURE - EXITING!!!" << std::endl;
00552 exit(-1);
00553 }
00554 }
00555
00556
00557 GlfFileWriter::~GlfFileWriter()
00558 {
00559 }