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 bool BamInterface::readHeader(IFILE filePtr, SamFileHeader& header,
00033 SamStatus& status)
00034 {
00035 if(filePtr == NULL)
00036 {
00037
00038 status.setStatus(SamStatus::FAIL_ORDER,
00039 "Cannot read header since the file pointer is null");
00040 return(false);
00041 }
00042 if(filePtr->isOpen() == false)
00043 {
00044 status.setStatus(SamStatus::FAIL_ORDER,
00045 "Cannot read header since the file is not open");
00046 return(false);
00047 }
00048
00049
00050 header.resetHeader();
00051
00052 int32_t headerLength;
00053 int readSize = ifread(filePtr, &headerLength, sizeof(headerLength));
00054
00055 if(readSize != sizeof(headerLength))
00056 {
00057 status.setStatus(SamStatus::FAIL_IO, "Failed to read the BAM header length.");
00058 return(false);
00059 }
00060
00061 String headerStr;
00062 if(headerLength > 0)
00063 {
00064
00065 readSize =
00066 ifread(filePtr, headerStr.LockBuffer(headerLength + 1), headerLength);
00067 headerStr[headerLength] = 0;
00068 headerStr.UnlockBuffer();
00069 if(readSize != headerLength)
00070 {
00071
00072 status.setStatus(SamStatus::FAIL_IO, "Failed to read the BAM header.");
00073 return(false);
00074 }
00075 }
00076
00077
00078 if(!header.addHeader(headerStr))
00079 {
00080
00081 status.setStatus(SamStatus::FAIL_PARSE, header.getErrorMessage());
00082 return(false);
00083 }
00084
00085 int referenceCount;
00086
00087 ifread(filePtr, &referenceCount, sizeof(int));
00088
00089
00090
00091 SamReferenceInfo& refInfo =
00092 header.getReferenceInfoForBamInterface();
00093 refInfo.clear();
00094
00095 CharBuffer refName;
00096
00097
00098 for (int i = 0; i < referenceCount; i++)
00099 {
00100 int nameLength;
00101 int rc;
00102
00103 rc = ifread(filePtr, &nameLength, sizeof(int));
00104 if(rc != sizeof(int))
00105 {
00106 status.setStatus(SamStatus::FAIL_IO,
00107 "Failed to read the BAM reference dictionary.");
00108 return(false);
00109 }
00110
00111
00112 refName.readFromFile(filePtr, nameLength);
00113
00114
00115 int32_t refLen;
00116 rc = ifread(filePtr, &refLen, sizeof(int));
00117
00118 if(rc != sizeof(int)) {
00119 status.setStatus(SamStatus::FAIL_IO,
00120 "Failed to read the BAM reference dictionary.");
00121 return(false);
00122 }
00123
00124 refInfo.add(refName.c_str(), refLen);
00125 }
00126
00127
00128 return(true);
00129 }
00130
00131
00132 bool BamInterface::writeHeader(IFILE filePtr, SamFileHeader& header,
00133 SamStatus& status)
00134 {
00135 if((filePtr == NULL) || (filePtr->isOpen() == false))
00136 {
00137
00138 status.setStatus(SamStatus::FAIL_ORDER,
00139 "Cannot write header since the file pointer is null");
00140 return(false);
00141 }
00142
00143 char magic[4];
00144 magic[0] = 'B';
00145 magic[1] = 'A';
00146 magic[2] = 'M';
00147 magic[3] = 1;
00148
00149
00150 ifwrite(filePtr, magic, 4);
00151
00152
00153
00154
00155
00156 std::string headerString = "";
00157 header.getHeaderString(headerString);
00158
00159 int32_t headerLen = headerString.length();
00160 int numWrite = 0;
00161
00162
00163 numWrite = ifwrite(filePtr, &headerLen, sizeof(int32_t));
00164 if(numWrite != sizeof(int32_t))
00165 {
00166 status.setStatus(SamStatus::FAIL_IO,
00167 "Failed to write the BAM header length.");
00168 return(false);
00169 }
00170
00171
00172 numWrite = ifwrite(filePtr, headerString.c_str(), headerLen);
00173 if(numWrite != headerLen)
00174 {
00175 status.setStatus(SamStatus::FAIL_IO,
00176 "Failed to write the BAM header.");
00177 return(false);
00178 }
00179
00180
00181
00182 const SamReferenceInfo& refInfo = header.getReferenceInfo();
00183
00184
00185 int32_t numSeq = refInfo.getNumEntries();
00186 ifwrite(filePtr, &numSeq, sizeof(int32_t));
00187
00188
00189 for (int i = 0; i < numSeq; i++)
00190 {
00191 const char* refName = refInfo.getReferenceName(i);
00192
00193 int32_t nameLength = strlen(refName) + 1;
00194
00195 ifwrite(filePtr, &nameLength, sizeof(int32_t));
00196
00197
00198 ifwrite(filePtr, refName, nameLength);
00199
00200 int32_t refLen = refInfo.getReferenceLength(i);
00201 ifwrite(filePtr, &refLen, sizeof(int32_t));
00202 }
00203
00204 return(true);
00205 }
00206
00207
00208 void BamInterface::readRecord(IFILE filePtr, SamFileHeader& header,
00209 SamRecord& record,
00210 SamStatus& samStatus)
00211 {
00212
00213
00214
00215 if(record.setBufferFromFile(filePtr, header) != SamStatus::SUCCESS)
00216 {
00217
00218 samStatus.addError(record.getStatus());
00219 }
00220 }
00221
00222 SamStatus::Status BamInterface::writeRecord(IFILE filePtr,
00223 SamFileHeader& header,
00224 SamRecord& record,
00225 SamRecord::SequenceTranslation translation)
00226 {
00227
00228 return(record.writeRecordBuffer(filePtr, translation));
00229 }
00230
00231