BamInterface.cpp

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 // Read a BAM file's header.
00032 bool BamInterface::readHeader(IFILE filePtr, SamFileHeader& header,
00033                               SamStatus& status)
00034 {
00035     if(filePtr == NULL)
00036     {
00037         // File is not open, return false.
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     // Clear the passed in header.
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         // Read the header.
00065         readSize = 
00066             ifread(filePtr, headerStr.LockBuffer(headerLength + 1), headerLength);
00067         headerStr[headerLength] = 0;
00068         headerStr.UnlockBuffer();
00069         if(readSize != headerLength)
00070         {
00071             // Failed to read the header.
00072             status.setStatus(SamStatus::FAIL_IO, "Failed to read the BAM header.");
00073             return(false);
00074         }
00075     }
00076     
00077     // Parse the header that was read.
00078     if(!header.addHeader(headerStr))
00079     {
00080         // Status is set in the method on failure.
00081         status.setStatus(SamStatus::FAIL_PARSE, header.getErrorMessage());
00082         return(false);
00083     }
00084 
00085     int referenceCount;
00086     // Read the number of references sequences.
00087     ifread(filePtr, &referenceCount, sizeof(int));
00088 
00089     // Get and clear the reference info so it can be set
00090     // from the bam reference table.
00091     SamReferenceInfo& refInfo = 
00092         header.getReferenceInfoForBamInterface();
00093     refInfo.clear();
00094 
00095     CharBuffer refName;
00096     
00097     // Read each reference sequence
00098     for (int i = 0; i < referenceCount; i++)
00099     {
00100         int nameLength;
00101         int rc;
00102         // Read the length of the reference name.
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         // Read the name.
00112         refName.readFromFile(filePtr, nameLength);
00113 
00114         // Read the length of the reference sequence.
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     // Successfully read the file.
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         // File is not open, return false.
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     // Write magic to the file.
00150     ifwrite(filePtr, magic, 4);
00151 
00152     ////////////////////////////////
00153     // Write the header to the file.
00154     ////////////////////////////////
00155     // Construct a string containing the entire header.
00156     std::string headerString = "";
00157     header.getHeaderString(headerString);
00158 
00159     int32_t headerLen = headerString.length();
00160     int numWrite = 0;
00161     
00162     // Write the header length.
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     // Write the header to the file.
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     // Write the Reference Information.
00182     const SamReferenceInfo& refInfo = header.getReferenceInfo();
00183 
00184     // Get the number of sequences.    
00185     int32_t numSeq = refInfo.getNumEntries();
00186     ifwrite(filePtr, &numSeq, sizeof(int32_t));
00187 
00188     // Write each reference sequence
00189     for (int i = 0; i < numSeq; i++)
00190     {
00191         const char* refName = refInfo.getReferenceName(i);
00192         // Add one for the null value.
00193         int32_t nameLength = strlen(refName) + 1;
00194         // Write the length of the reference name.
00195         ifwrite(filePtr, &nameLength, sizeof(int32_t));
00196       
00197         // Write the name.
00198         ifwrite(filePtr, refName, nameLength);
00199         // Write the length of the reference sequence.
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     // TODO - need to validate there are @SQ lines in both sam/bam - MAYBE!
00213 
00214     // SetBufferFromFile will reset the record prior to reading a new one.
00215     if(record.setBufferFromFile(filePtr, header) != SamStatus::SUCCESS)
00216     {
00217         // Failed, so add the error message.
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     // Write the file, returning the status.
00228     return(record.writeRecordBuffer(filePtr, translation));
00229 }
00230 
00231 
Generated on Mon Feb 11 13:45:17 2013 for libStatGen Software by  doxygen 1.6.3