BamInterface.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "BamInterface.h"
00019 #include "CharBuffer.h"
00020
00021 BamInterface::BamInterface()
00022 {
00023 }
00024
00025
00026 BamInterface::~BamInterface()
00027 {
00028 }
00029
00030
00031
00032 SamStatus::Status BamInterface::readHeader(IFILE filePtr, SamFileHeader& header)
00033 {
00034 if(filePtr == NULL)
00035 {
00036
00037 return(SamStatus::FAIL_ORDER);
00038 }
00039
00040
00041 header.resetHeader();
00042
00043 SamStatus::Status status = header.setHeaderFromBamFile(filePtr);
00044 if(status != SamStatus::SUCCESS)
00045 {
00046 return(status);
00047 }
00048
00049 int referenceCount;
00050
00051 ifread(filePtr, &referenceCount, sizeof(int));
00052
00053
00054
00055 CharBuffer refName;
00056
00057
00058 for (int i = 0; i < referenceCount; i++)
00059 {
00060 int nameLength;
00061 int rc;
00062
00063 rc = ifread(filePtr, &nameLength, sizeof(int));
00064 if(rc != sizeof(int)) {
00065 return SamStatus::FAIL_IO;
00066 }
00067
00068
00069 refName.readFromFile(filePtr, nameLength);
00070
00071
00072 int32_t refLen;
00073 rc = ifread(filePtr, &refLen, sizeof(int));
00074
00075 if(rc != sizeof(int)) {
00076 return SamStatus::FAIL_IO;
00077 }
00078
00079 header.addReferenceInfo(refName.c_str(), refLen);
00080 }
00081
00082
00083 return(SamStatus::SUCCESS);
00084 }
00085
00086
00087 SamStatus::Status BamInterface::writeHeader(IFILE filePtr,
00088 SamFileHeader& header)
00089 {
00090 if((filePtr == NULL) || (filePtr->isOpen() == false))
00091 {
00092
00093 return(SamStatus::FAIL_ORDER);
00094 }
00095
00096 char magic[4];
00097 magic[0] = 'B';
00098 magic[1] = 'A';
00099 magic[2] = 'M';
00100 magic[3] = 1;
00101
00102
00103 ifwrite(filePtr, magic, 4);
00104
00105
00106
00107
00108
00109 std::string headerString = "";
00110 header.getHeaderString(headerString);
00111
00112 int32_t headerLen = headerString.length();
00113 int numWrite = 0;
00114
00115
00116 numWrite = ifwrite(filePtr, &headerLen, sizeof(int32_t));
00117 if(numWrite != sizeof(int32_t))
00118 {
00119 return(SamStatus::FAIL_IO);
00120 }
00121
00122
00123 numWrite = ifwrite(filePtr, headerString.c_str(), headerLen);
00124 if(numWrite != headerLen)
00125 {
00126 return(SamStatus::FAIL_IO);
00127 }
00128
00129
00130
00131 const SamReferenceInfo* refInfo = header.getReferenceInfo();
00132 if(refInfo == NULL)
00133 {
00134
00135 return(SamStatus::INVALID);
00136 }
00137
00138
00139 int32_t numSeq = refInfo->getNumEntries();
00140
00141
00142
00143 if(numSeq == 0)
00144 {
00145 header.generateReferenceInfo();
00146 numSeq = refInfo->getNumEntries();
00147 }
00148 ifwrite(filePtr, &numSeq, sizeof(int32_t));
00149
00150
00151 for (int i = 0; i < numSeq; i++)
00152 {
00153 const char* refName = refInfo->getReferenceName(i);
00154
00155 int32_t nameLength = strlen(refName) + 1;
00156
00157 ifwrite(filePtr, &nameLength, sizeof(int32_t));
00158
00159
00160 ifwrite(filePtr, refName, nameLength);
00161
00162 int32_t refLen = refInfo->getReferenceLength(i);
00163 ifwrite(filePtr, &refLen, sizeof(int32_t));
00164 }
00165
00166 return(SamStatus::SUCCESS);
00167 }
00168
00169
00170 void BamInterface::readRecord(IFILE filePtr, SamFileHeader& header,
00171 SamRecord& record,
00172 SamStatus& samStatus)
00173 {
00174
00175
00176
00177 if(record.setBufferFromFile(filePtr, header) != SamStatus::SUCCESS)
00178 {
00179
00180 samStatus.addError(record.getStatus());
00181 }
00182 }
00183
00184 SamStatus::Status BamInterface::writeRecord(IFILE filePtr,
00185 SamFileHeader& header,
00186 SamRecord& record,
00187 SamRecord::SequenceTranslation translation)
00188 {
00189
00190 return(record.writeRecordBuffer(filePtr, translation));
00191 }
00192
00193