00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdexcept>
00018 #include "SamFile.h"
00019 #include "SamFileHeader.h"
00020 #include "SamRecord.h"
00021 #include "BamInterface.h"
00022 #include "SamInterface.h"
00023
00024
00025 SamFile::SamFile()
00026 : myFilePtr(NULL),
00027 myInterfacePtr(NULL),
00028 myStatistics(NULL),
00029 myStatus(),
00030 myBamIndex(NULL),
00031 myRefPtr(NULL),
00032 myReadTranslation(SamRecord::NONE),
00033 myWriteTranslation(SamRecord::NONE)
00034 {
00035 resetFile();
00036 }
00037
00038
00039
00040 SamFile::SamFile(ErrorHandler::HandlingType errorHandlingType)
00041 : myFilePtr(NULL),
00042 myInterfacePtr(NULL),
00043 myStatistics(NULL),
00044 myStatus(errorHandlingType),
00045 myBamIndex(NULL),
00046 myRefPtr(NULL),
00047 myReadTranslation(SamRecord::NONE),
00048 myWriteTranslation(SamRecord::NONE)
00049 {
00050 resetFile();
00051 }
00052
00053
00054
00055
00056 SamFile::SamFile(const char* filename, OpenType mode)
00057 : myFilePtr(NULL),
00058 myInterfacePtr(NULL),
00059 myStatistics(NULL),
00060 myStatus(),
00061 myBamIndex(NULL),
00062 myRefPtr(NULL),
00063 myReadTranslation(SamRecord::NONE),
00064 myWriteTranslation(SamRecord::NONE)
00065 {
00066 resetFile();
00067
00068 bool openStatus = true;
00069 if(mode == READ)
00070 {
00071
00072 openStatus = OpenForRead(filename);
00073 }
00074 else
00075 {
00076
00077 openStatus = OpenForWrite(filename);
00078 }
00079 if(!openStatus)
00080 {
00081
00082 fprintf(stderr, "%s\n", GetStatusMessage());
00083 std::cerr << "FAILURE - EXITING!!!" << std::endl;
00084 exit(-1);
00085 }
00086 }
00087
00088
00089
00090
00091 SamFile::SamFile(const char* filename, OpenType mode,
00092 ErrorHandler::HandlingType errorHandlingType)
00093 : myFilePtr(NULL),
00094 myInterfacePtr(NULL),
00095 myStatistics(NULL),
00096 myStatus(errorHandlingType),
00097 myBamIndex(NULL),
00098 myRefPtr(NULL),
00099 myReadTranslation(SamRecord::NONE),
00100 myWriteTranslation(SamRecord::NONE)
00101 {
00102 resetFile();
00103
00104 bool openStatus = true;
00105 if(mode == READ)
00106 {
00107
00108 openStatus = OpenForRead(filename);
00109 }
00110 else
00111 {
00112
00113 openStatus = OpenForWrite(filename);
00114 }
00115 if(!openStatus)
00116 {
00117
00118 fprintf(stderr, "%s\n", GetStatusMessage());
00119 std::cerr << "FAILURE - EXITING!!!" << std::endl;
00120 exit(-1);
00121 }
00122 }
00123
00124
00125 SamFile::~SamFile()
00126 {
00127 resetFile();
00128 if(myStatistics != NULL)
00129 {
00130 delete myStatistics;
00131 }
00132 }
00133
00134
00135
00136 bool SamFile::OpenForRead(const char * filename)
00137 {
00138
00139 resetFile();
00140
00141 int lastchar = 0;
00142
00143 while (filename[lastchar] != 0) lastchar++;
00144
00145
00146 if((lastchar >= 1) && (filename[0] == '-'))
00147 {
00148
00149
00150 if(strcmp(filename, "-.bam") == 0)
00151 {
00152
00153
00154 filename = "-";
00155 myFilePtr = ifopen(filename, "rb", InputFile::BGZF);
00156
00157 myInterfacePtr = new BamInterface;
00158
00159
00160 char magic[4];
00161 ifread(myFilePtr, magic, 4);
00162 }
00163 else if(strcmp(filename, "-.ubam") == 0)
00164 {
00165
00166
00167 filename = "-";
00168 myFilePtr = ifopen(filename, "rb", InputFile::UNCOMPRESSED);
00169
00170 myInterfacePtr = new BamInterface;
00171
00172
00173 char magic[4];
00174 ifread(myFilePtr, magic, 4);
00175 }
00176 else
00177 {
00178
00179
00180 filename = "-";
00181 myFilePtr = ifopen(filename, "rb", InputFile::UNCOMPRESSED);
00182 myInterfacePtr = new SamInterface;
00183 }
00184 }
00185 else
00186 {
00187
00188 myFilePtr = ifopen(filename, "rb");
00189
00190 if (myFilePtr == NULL)
00191 {
00192 std::string errorMessage = "Failed to Open ";
00193 errorMessage += filename;
00194 errorMessage += " for reading";
00195 myStatus.setStatus(SamStatus::FAIL_IO, errorMessage.c_str());
00196 return(false);
00197 }
00198
00199 char magic[4];
00200 ifread(myFilePtr, magic, 4);
00201
00202 if (magic[0] == 'B' && magic[1] == 'A' && magic[2] == 'M' &&
00203 magic[3] == 1)
00204 {
00205 myInterfacePtr = new BamInterface;
00206
00207
00208 myIsBamOpenForRead = true;
00209 }
00210 else
00211 {
00212
00213
00214 ifrewind(myFilePtr);
00215 myInterfacePtr = new SamInterface;
00216 }
00217 }
00218
00219
00220 myIsOpenForRead = true;
00221
00222 myStatus = SamStatus::SUCCESS;
00223 return(true);
00224 }
00225
00226
00227
00228 bool SamFile::OpenForWrite(const char * filename)
00229 {
00230
00231 resetFile();
00232
00233 int lastchar = 0;
00234 while (filename[lastchar] != 0) lastchar++;
00235 if (lastchar >= 4 &&
00236 filename[lastchar - 4] == 'u' &&
00237 filename[lastchar - 3] == 'b' &&
00238 filename[lastchar - 2] == 'a' &&
00239 filename[lastchar - 1] == 'm')
00240 {
00241
00242
00243 if((lastchar == 6) && (filename[0] == '-') && (filename[1] == '.'))
00244 {
00245 filename = "-";
00246 }
00247 myFilePtr = ifopen(filename, "wb", InputFile::UNCOMPRESSED);
00248
00249 myInterfacePtr = new BamInterface;
00250 }
00251 else if (lastchar >= 3 &&
00252 filename[lastchar - 3] == 'b' &&
00253 filename[lastchar - 2] == 'a' &&
00254 filename[lastchar - 1] == 'm')
00255 {
00256
00257
00258 if((lastchar == 5) && (filename[0] == '-') && (filename[1] == '.'))
00259 {
00260 filename = "-";
00261 }
00262 myFilePtr = ifopen(filename, "wb", InputFile::BGZF);
00263
00264 myInterfacePtr = new BamInterface;
00265 }
00266 else
00267 {
00268
00269
00270
00271 if((lastchar >= 1) && (filename[0] == '-'))
00272 {
00273 filename = "-";
00274 }
00275 myFilePtr = ifopen(filename, "wb", InputFile::UNCOMPRESSED);
00276
00277 myInterfacePtr = new SamInterface;
00278 }
00279
00280 if (myFilePtr == NULL)
00281 {
00282 std::string errorMessage = "Failed to Open ";
00283 errorMessage += filename;
00284 errorMessage += " for writing";
00285 myStatus.setStatus(SamStatus::FAIL_IO, errorMessage.c_str());
00286 return(false);
00287 }
00288
00289 myIsOpenForWrite = true;
00290
00291
00292 myStatus = SamStatus::SUCCESS;
00293 return(true);
00294 }
00295
00296
00297
00298 bool SamFile::ReadBamIndex(const char* bamIndexFilename)
00299 {
00300
00301 if(myBamIndex != NULL)
00302 {
00303 delete myBamIndex;
00304 myBamIndex = NULL;
00305 }
00306
00307
00308 myBamIndex = new BamIndex();
00309 SamStatus::Status indexStat = myBamIndex->readIndex(bamIndexFilename);
00310
00311 if(indexStat != SamStatus::SUCCESS)
00312 {
00313 std::string errorMessage = "Failed to read the bam Index file: ";
00314 errorMessage += bamIndexFilename;
00315 myStatus.setStatus(indexStat, errorMessage.c_str());
00316 delete myBamIndex;
00317 myBamIndex = NULL;
00318 return(false);
00319 }
00320 myStatus = SamStatus::SUCCESS;
00321 return(true);
00322 }
00323
00324
00325
00326 void SamFile::SetReference(GenomeSequence* reference)
00327 {
00328 myRefPtr = reference;
00329 }
00330
00331
00332
00333 void SamFile::SetReadSequenceTranslation(SamRecord::SequenceTranslation translation)
00334 {
00335 myReadTranslation = translation;
00336 }
00337
00338
00339
00340 void SamFile::SetWriteSequenceTranslation(SamRecord::SequenceTranslation translation)
00341 {
00342 myWriteTranslation = translation;
00343 }
00344
00345
00346 void SamFile::Close()
00347 {
00348
00349
00350 resetFile();
00351 }
00352
00353
00354
00355
00356 bool SamFile::IsEOF()
00357 {
00358 if (myFilePtr != NULL)
00359 {
00360
00361 return(ifeof(myFilePtr));
00362 }
00363
00364 return true;
00365 }
00366
00367
00368
00369 bool SamFile::ReadHeader(SamFileHeader& header)
00370 {
00371 if(myIsOpenForRead == false)
00372 {
00373
00374 myStatus.setStatus(SamStatus::FAIL_ORDER,
00375 "Cannot read header since the file is not open for reading");
00376 return(false);
00377 }
00378
00379 if(myHasHeader == true)
00380 {
00381
00382 myStatus.setStatus(SamStatus::FAIL_ORDER,
00383 "Cannot read header since it has already been read.");
00384 return(false);
00385 }
00386
00387 myStatus = myInterfacePtr->readHeader(myFilePtr, header);
00388 if(myStatus == SamStatus::SUCCESS)
00389 {
00390
00391 myHasHeader = true;
00392 return(true);
00393 }
00394 return(false);
00395 }
00396
00397
00398
00399 bool SamFile::WriteHeader(SamFileHeader& header)
00400 {
00401 if(myIsOpenForWrite == false)
00402 {
00403
00404
00405
00406 myStatus.setStatus(SamStatus::FAIL_ORDER,
00407 "Cannot write header since the file is not open for writing");
00408 return(false);
00409 }
00410
00411 if(myHasHeader == true)
00412 {
00413
00414 myStatus.setStatus(SamStatus::FAIL_ORDER,
00415 "Cannot write header since it has already been written");
00416 return(false);
00417 }
00418
00419 myStatus = myInterfacePtr->writeHeader(myFilePtr, header);
00420 if(myStatus == SamStatus::SUCCESS)
00421 {
00422
00423 myHasHeader = true;
00424 return(true);
00425 }
00426
00427
00428 return(false);
00429 }
00430
00431
00432
00433 bool SamFile::ReadRecord(SamFileHeader& header,
00434 SamRecord& record)
00435 {
00436 myStatus = SamStatus::SUCCESS;
00437
00438 if(myIsOpenForRead == false)
00439 {
00440
00441 myStatus.setStatus(SamStatus::FAIL_ORDER,
00442 "Cannot read record since the file is not open for reading");
00443 throw(std::runtime_error("SOFTWARE BUG: trying to read a SAM/BAM record prior to opening the file."));
00444 return(false);
00445 }
00446
00447 if(myHasHeader == false)
00448 {
00449
00450
00451 myStatus.setStatus(SamStatus::FAIL_ORDER,
00452 "Cannot read record since the header has not been read.");
00453 throw(std::runtime_error("SOFTWARE BUG: trying to read a SAM/BAM record prior to reading the header."));
00454 return(false);
00455 }
00456
00457
00458
00459 if(myNewSection)
00460 {
00461 if(!processNewSection(header))
00462 {
00463
00464
00465
00466
00467 return(false);
00468 }
00469 }
00470
00471
00472 if(myRefID != BamIndex::REF_ID_ALL)
00473 {
00474
00475 return(readIndexedRecord(header, record));
00476 }
00477
00478 record.setReference(myRefPtr);
00479 record.setSequenceTranslation(myReadTranslation);
00480
00481
00482
00483 myInterfacePtr->readRecord(myFilePtr, header, record, myStatus);
00484 if(myStatus == SamStatus::SUCCESS)
00485 {
00486
00487 myRecordCount++;
00488
00489 if(myStatistics != NULL)
00490 {
00491
00492 myStatistics->updateStatistics(record);
00493 }
00494
00495
00496 if(!validateSortOrder(record, header))
00497 {
00498
00499 return(false);
00500 }
00501 return(true);
00502 }
00503
00504 return(false);
00505 }
00506
00507
00508
00509
00510 bool SamFile::WriteRecord(SamFileHeader& header,
00511 SamRecord& record)
00512 {
00513 if(myIsOpenForWrite == false)
00514 {
00515
00516 myStatus.setStatus(SamStatus::FAIL_ORDER,
00517 "Cannot write record since the file is not open for writing");
00518 return(false);
00519 }
00520
00521 if(myHasHeader == false)
00522 {
00523
00524 myStatus.setStatus(SamStatus::FAIL_ORDER,
00525 "Cannot write record since the header has not been written");
00526 return(false);
00527 }
00528
00529
00530 if(!validateSortOrder(record, header))
00531 {
00532
00533 myStatus.setStatus(SamStatus::INVALID_SORT,
00534 "Cannot write the record since the file is not properly sorted.");
00535 return(false);
00536 }
00537
00538 if(myRefPtr != NULL)
00539 {
00540 record.setReference(myRefPtr);
00541 }
00542
00543
00544
00545 myStatus = myInterfacePtr->writeRecord(myFilePtr, header, record,
00546 myWriteTranslation);
00547
00548 if(myStatus == SamStatus::SUCCESS)
00549 {
00550
00551 myRecordCount++;
00552 return(true);
00553 }
00554 return(false);
00555 }
00556
00557
00558
00559
00560 void SamFile::setSortedValidation(SortedType sortType)
00561 {
00562 mySortedType = sortType;
00563 }
00564
00565
00566
00567 uint32_t SamFile::GetCurrentRecordCount()
00568 {
00569 return(myRecordCount);
00570 }
00571
00572
00573
00574 bool SamFile::SetReadSection(int32_t refID)
00575 {
00576
00577 return(SetReadSection(refID, -1, -1));
00578 }
00579
00580
00581
00582
00583 bool SamFile::SetReadSection(const char* refName)
00584 {
00585
00586 return(SetReadSection(refName, -1, -1));
00587 }
00588
00589
00590
00591 bool SamFile::SetReadSection(int32_t refID, int32_t start, int32_t end)
00592 {
00593
00594
00595
00596 if(!myIsBamOpenForRead)
00597 {
00598
00599 myStatus.setStatus(SamStatus::FAIL_ORDER,
00600 "Canot set section since there is no bam file open");
00601 return(false);
00602 }
00603
00604 myNewSection = true;
00605 myStartPos = start;
00606 myEndPos = end;
00607 myRefID = refID;
00608 myRefName.clear();
00609 myChunksToRead.clear();
00610
00611
00612 myCurrentChunkEnd = 0;
00613 myStatus = SamStatus::SUCCESS;
00614
00615 return(true);
00616 }
00617
00618
00619
00620 bool SamFile::SetReadSection(const char* refName, int32_t start, int32_t end)
00621 {
00622
00623
00624
00625 if(!myIsBamOpenForRead)
00626 {
00627
00628 myStatus.setStatus(SamStatus::FAIL_ORDER,
00629 "Canot set section since there is no bam file open");
00630 return(false);
00631 }
00632
00633 myNewSection = true;
00634 myStartPos = start;
00635 myEndPos = end;
00636 if((strcmp(refName, "") == 0) || (strcmp(refName, "*") == 0))
00637 {
00638
00639 myRefID = BamIndex::REF_ID_UNMAPPED;
00640 }
00641 else
00642 {
00643
00644
00645 myRefName = refName;
00646 myRefID = BamIndex::REF_ID_ALL;
00647 }
00648 myChunksToRead.clear();
00649
00650
00651 myCurrentChunkEnd = 0;
00652 myStatus = SamStatus::SUCCESS;
00653
00654 return(true);
00655 }
00656
00657
00658
00659
00660 uint32_t SamFile::GetNumOverlaps(SamRecord& samRecord)
00661 {
00662 if(myRefPtr != NULL)
00663 {
00664 samRecord.setReference(myRefPtr);
00665 }
00666 samRecord.setSequenceTranslation(myReadTranslation);
00667
00668
00669
00670 return(samRecord.getNumOverlaps(myStartPos, myEndPos));
00671 }
00672
00673
00674 void SamFile::GenerateStatistics(bool genStats)
00675 {
00676 if(genStats)
00677 {
00678 if(myStatistics == NULL)
00679 {
00680
00681
00682 myStatistics = new SamStatistics();
00683 }
00684 }
00685 else
00686 {
00687
00688
00689 if(myStatistics != NULL)
00690 {
00691 delete myStatistics;
00692 myStatistics = NULL;
00693 }
00694 }
00695
00696 }
00697
00698
00699
00700 void SamFile::resetFile()
00701 {
00702
00703 if (myFilePtr != NULL)
00704 {
00705
00706 ifclose(myFilePtr);
00707 myFilePtr = NULL;
00708 }
00709 if(myInterfacePtr != NULL)
00710 {
00711 delete myInterfacePtr;
00712 myInterfacePtr = NULL;
00713 }
00714
00715 myIsOpenForRead = false;
00716 myIsOpenForWrite = false;
00717 myHasHeader = false;
00718 mySortedType = UNSORTED;
00719 myPrevReadName = "";
00720 myPrevCoord = -1;
00721 myPrevRefID = 0;
00722 myRecordCount = 0;
00723 myStatus = SamStatus::SUCCESS;
00724
00725
00726 myIsBamOpenForRead = false;
00727 myRefID = BamIndex::REF_ID_ALL;
00728 myStartPos = -1;
00729 myEndPos = -1;
00730 myNewSection = false;
00731 myCurrentChunkEnd = 0;
00732 myChunksToRead.clear();
00733 if(myBamIndex != NULL)
00734 {
00735 delete myBamIndex;
00736 myBamIndex = NULL;
00737 }
00738
00739
00740 if(myStatistics != NULL)
00741 {
00742 myStatistics->reset();
00743 }
00744
00745 myRefName.clear();
00746 }
00747
00748
00749
00750
00751
00752 bool SamFile::validateSortOrder(SamRecord& record, SamFileHeader& header)
00753 {
00754 if(myRefPtr != NULL)
00755 {
00756 record.setReference(myRefPtr);
00757 }
00758 record.setSequenceTranslation(myReadTranslation);
00759
00760 bool status = false;
00761 if(mySortedType == UNSORTED)
00762 {
00763
00764 status = true;
00765 }
00766 else
00767 {
00768
00769 if(mySortedType == FLAG)
00770 {
00771
00772 mySortedType = getSortOrderFromHeader(header);
00773 }
00774
00775 if(mySortedType == QUERY_NAME)
00776 {
00777
00778
00779 const char* readName = record.getReadName();
00780 if(myPrevReadName.compare(readName) > 0)
00781 {
00782
00783
00784 String errorMessage = "ERROR: File is not sorted at record ";
00785 errorMessage += myRecordCount;
00786 myStatus.setStatus(SamStatus::INVALID_SORT,
00787 errorMessage.c_str());
00788 status = false;
00789 }
00790 else
00791 {
00792 myPrevReadName = readName;
00793 status = true;
00794 }
00795 }
00796 else
00797 {
00798
00799
00800 int32_t refID = record.getReferenceID();
00801 int32_t coord = record.get0BasedPosition();
00802
00803 if(refID == BamIndex::REF_ID_UNMAPPED)
00804 {
00805
00806
00807 status = true;
00808 myPrevRefID = refID;
00809 myPrevCoord = coord;
00810 }
00811 else if(myPrevRefID == BamIndex::REF_ID_UNMAPPED)
00812 {
00813
00814
00815 String errorMessage = "ERROR: File is not sorted at record ";
00816 errorMessage += myRecordCount;
00817 myStatus.setStatus(SamStatus::INVALID_SORT,
00818 errorMessage.c_str());
00819 status = false;
00820 }
00821 else if(refID < myPrevRefID)
00822 {
00823
00824
00825 String errorMessage = "ERROR: File is not sorted at record ";
00826 errorMessage += myRecordCount;
00827 myStatus.setStatus(SamStatus::INVALID_SORT,
00828 errorMessage.c_str());
00829 status = false;
00830 }
00831 else
00832 {
00833
00834 if(refID > myPrevRefID)
00835 {
00836
00837 myPrevCoord = -1;
00838 }
00839
00840
00841 if(coord < myPrevCoord)
00842 {
00843
00844 String errorMessage = "ERROR: File is not sorted at record ";
00845 errorMessage += myRecordCount;
00846 myStatus.setStatus(SamStatus::INVALID_SORT,
00847 errorMessage.c_str());
00848 status = false;
00849 }
00850 else
00851 {
00852 myPrevRefID = refID;
00853 myPrevCoord = coord;
00854 status = true;
00855 }
00856 }
00857 }
00858 }
00859
00860 return(status);
00861 }
00862
00863
00864 SamFile::SortedType SamFile::getSortOrderFromHeader(SamFileHeader& header)
00865 {
00866 const char* tag = header.getSortOrder();
00867
00868
00869
00870 SortedType headerSortOrder = UNSORTED;
00871 if(strcmp(tag, "queryname") == 0)
00872 {
00873 headerSortOrder = QUERY_NAME;
00874 }
00875 else if(strcmp(tag, "coordinate") == 0)
00876 {
00877 headerSortOrder = COORDINATE;
00878 }
00879 return(headerSortOrder);
00880 }
00881
00882
00883
00884 bool SamFile::readIndexedRecord(SamFileHeader& header,
00885 SamRecord& record)
00886 {
00887 record.setReference(myRefPtr);
00888 record.setSequenceTranslation(myReadTranslation);
00889
00890
00891
00892 bool recordFound = false;
00893 while(!recordFound)
00894 {
00895
00896
00897
00898
00899 uint64_t currentPos = iftell(myFilePtr);
00900 if(currentPos >= myCurrentChunkEnd)
00901 {
00902
00903 if(myChunksToRead.empty())
00904 {
00905 myStatus = SamStatus::NO_MORE_RECS;
00906 return(false);
00907 }
00908
00909
00910 Chunk newChunk = myChunksToRead.pop();
00911
00912
00913
00914 if(newChunk.chunk_beg != currentPos)
00915 {
00916
00917 if(ifseek(myFilePtr, newChunk.chunk_beg, SEEK_SET) != true)
00918 {
00919
00920 myStatus.setStatus(SamStatus::FAIL_IO,
00921 "Failed to seek to the next record");
00922 return(false);
00923 }
00924 }
00925
00926 myCurrentChunkEnd = newChunk.chunk_end;
00927 }
00928
00929
00930 myInterfacePtr->readRecord(myFilePtr, header, record, myStatus);
00931 if(myStatus != SamStatus::SUCCESS)
00932 {
00933
00934 return(false);
00935 }
00936
00937
00938
00939 if(record.getReferenceID() != myRefID)
00940 {
00941
00942 myStatus = SamStatus::NO_MORE_RECS;
00943 return(false);
00944 }
00945
00946
00947 recordFound = true;
00948
00949
00950
00951 if((myStartPos != -1) && (myEndPos != -1))
00952 {
00953
00954
00955
00956
00957
00958 if(record.get0BasedPosition() >= myEndPos)
00959 {
00960 myStatus = SamStatus::NO_MORE_RECS;
00961 return(false);
00962 }
00963
00964
00965
00966
00967 if(record.get0BasedAlignmentEnd() < myStartPos)
00968 {
00969
00970
00971 recordFound = false;
00972 }
00973 }
00974 }
00975
00976
00977 if(!validateSortOrder(record, header))
00978 {
00979 myStatus.setStatus(SamStatus::INVALID_SORT,
00980 "The file is not properly sorted.");
00981 return(false);
00982 }
00983
00984
00985
00986 myRecordCount++;
00987
00988 if(myStatistics != NULL)
00989 {
00990
00991 myStatistics->updateStatistics(record);
00992 }
00993
00994 return(true);
00995 }
00996
00997
00998 bool SamFile::processNewSection(SamFileHeader &header)
00999 {
01000 myNewSection = false;
01001
01002
01003 if(myBamIndex == NULL)
01004 {
01005
01006 myStatus.setStatus(SamStatus::FAIL_ORDER,
01007 "Canot read section since there is no index file open");
01008 throw(std::runtime_error("SOFTWARE BUG: trying to read a BAM record by section prior to opening the BAM Index file."));
01009 return(false);
01010 }
01011
01012
01013 if(!myIsBamOpenForRead)
01014 {
01015
01016 myStatus.setStatus(SamStatus::FAIL_ORDER,
01017 "Canot read section since there is no bam file open");
01018 throw(std::runtime_error("SOFTWARE BUG: trying to read a BAM record by section without opening a BAM file."));
01019 return(false);
01020 }
01021
01022 if(myHasHeader == false)
01023 {
01024
01025 myStatus.setStatus(SamStatus::FAIL_ORDER,
01026 "Cannot read record since the header has not been read.");
01027 throw(std::runtime_error("SOFTWARE BUG: trying to read a BAM record by section prior to opening the header."));
01028 return(false);
01029 }
01030
01031 myChunksToRead.clear();
01032
01033
01034 myCurrentChunkEnd = 0;
01035
01036
01037
01038 if(!myRefName.empty())
01039 {
01040 myRefID = header.getReferenceID(myRefName.c_str());
01041
01042 myRefName.clear();
01043 }
01044
01045
01046 if(myBamIndex->getChunksForRegion(myRefID, myStartPos, myEndPos,
01047 myChunksToRead) == true)
01048 {
01049 myStatus = SamStatus::SUCCESS;
01050 }
01051 else
01052 {
01053 myStatus.setStatus(SamStatus::FAIL_PARSE,
01054 "Failed to get the specified region.");
01055 }
01056 return(true);
01057 }
01058
01059
01060
01061 SamFileReader::SamFileReader()
01062 {
01063 }
01064
01065
01066
01067 SamFileReader::SamFileReader(const char* filename)
01068 {
01069 if(!OpenForRead(filename))
01070 {
01071
01072 fprintf(stderr, "%s\n", GetStatusMessage());
01073 std::cerr << "FAILURE - EXITING!!!" << std::endl;
01074 exit(-1);
01075 }
01076 }
01077
01078
01079 SamFileReader::~SamFileReader()
01080 {
01081 }
01082
01083
01084
01085 SamFileWriter::SamFileWriter()
01086 {
01087 }
01088
01089
01090
01091 SamFileWriter::SamFileWriter(const char* filename)
01092 {
01093 if(!OpenForWrite(filename))
01094 {
01095
01096 fprintf(stderr, "%s\n", GetStatusMessage());
01097 std::cerr << "FAILURE - EXITING!!!" << std::endl;
01098 exit(-1);
01099 }
01100 }
01101
01102
01103 SamFileWriter::~SamFileWriter()
01104 {
01105 }