libStatGen Software  1
WriteFiles.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 "WriteFiles.h"
00019 #include "Validate.h"
00020 
00021 #include <assert.h>
00022 
00023 void testWrite()
00024 {
00025     TestWrite writeTest;
00026     writeTest.testWrite();
00027 }
00028 
00029 const std::string TestWrite::HEADER_TEXT1 = "This is my 1st test header.";
00030 const std::string TestWrite::SEC1_REFNAME = "This is my 1st RefName";
00031 const std::string TestWrite::SEC1REC2_INDELSEQ1 = "AC";
00032 const std::string TestWrite::SEC1REC2_INDELSEQ2 = "TCA";
00033 const std::string TestWrite::SEC2_REFNAME = "This is my 2nd RefName";
00034 const std::string TestWrite::HEADER_TEXT2 = "This is my 2nd test header.";
00035 const std::string TestWrite::HEADER_TEXT3 = "This is my 3rd test header.";
00036 
00037 void TestWrite::testWrite()
00038 {
00039     GlfFile glfOut;
00040     
00041     std::string testFile = "results/MyTestOut1.glf";
00042 
00043     assert(glfOut.openForWrite(testFile.c_str(), false));
00044 
00045     // Create a glf header.
00046     GlfHeader glfHeader;
00047     GlfRefSection glfSection;
00048     GlfRecord record;
00049   
00050     // Test writing refsection with no header - exception
00051     bool caughtException = false;
00052     try
00053     {
00054         assert(glfOut.writeRefSection(glfSection) == false);
00055     }
00056     catch (std::exception& e) 
00057     {
00058         caughtException = true;
00059     }
00060     assert(caughtException);
00061 
00062     // Test writing record with no header - exception.
00063     caughtException = false;
00064     try
00065     {
00066         assert(glfOut.writeRecord(record) == false);
00067     }
00068     catch (std::exception& e) 
00069     {
00070         caughtException = true;
00071     }
00072     assert(caughtException);
00073 
00074     // Write the header.
00075     writeHeader(glfOut, 1);
00076 
00077     // Test writing record with no refsection - exception.
00078     caughtException = false;
00079     try
00080     {
00081         assert(glfOut.writeRecord(record) == false);
00082     }
00083     catch (std::exception& e) 
00084     {
00085         caughtException = true;
00086     }
00087     assert(caughtException);
00088 
00089 
00090     //////////////////////////////////////////////
00091     writeRefSection1(glfOut);
00092 
00093     // Test writing header after refSection - exception
00094     caughtException = false;
00095     try
00096     {
00097         assert(glfOut.writeHeader(glfHeader) == false);
00098     }
00099     catch (std::exception& e) 
00100     {
00101         caughtException = true;
00102     }
00103     assert(caughtException);
00104 
00105     writeSec1Record1(glfOut);
00106     // Test writing header after record - exception
00107     caughtException = false;
00108     try
00109     {
00110         assert(glfOut.writeHeader(glfHeader) == false);
00111     }
00112     catch (std::exception& e) 
00113     {
00114         caughtException = true;
00115     }
00116     assert(caughtException);
00117 
00118     writeSec1Record2(glfOut);
00119     writeEndMarker(glfOut);
00120 
00121     writeRefSection2(glfOut);
00122     writeSec2Record1(glfOut);
00123     writeEndMarker(glfOut);
00124 
00125     ////////////////////
00126     // Close the file.
00127     glfOut.close();
00128 
00129     //////////////////////////////////////////////
00130     // Validate the just written file.
00131     GlfFile glfIn;
00132     assert(glfIn.openForRead(testFile.c_str()));
00133 
00134     readHeader(glfIn, 1);
00135     readRefSection1(glfIn);
00136     readSec1Record1(glfIn);
00137     readSec1Record2(glfIn);
00138     readEndMarker(glfIn);
00139     readRefSection2(glfIn);
00140     readSec2Record1(glfIn);
00141     readEndMarker(glfIn);
00142     checkEOF(glfIn);
00143 
00144     ////////////////////////////////
00145     // NEW FILE
00146     testFile = "results/MyTestOut2.glf";
00147     assert(glfOut.openForWrite(testFile.c_str()));
00148   
00149     writeHeader(glfOut, 2);
00150     writeRefSection1(glfOut);
00151     writeSec1Record1(glfOut);
00152     writeSec1Record2(glfOut);
00153     // Test writing new section without end of section marker - auto-added.
00154     writeRefSection2(glfOut);
00155     writeSec2Record1(glfOut);
00156     // Test closing file with no end of section marker - auto-added.
00157     glfOut.close();
00158 
00159     //////////////////////////////////////////////
00160     // Validate the just written file.
00161     assert(glfIn.openForRead(testFile.c_str()));
00162 
00163     readHeader(glfIn, 2);
00164     readRefSection1(glfIn);
00165     readSec1Record1(glfIn);
00166     readSec1Record2(glfIn);
00167     readEndMarker(glfIn);
00168     readRefSection2(glfIn);
00169     readSec2Record1(glfIn);
00170     readEndMarker(glfIn);
00171     checkEOF(glfIn);
00172 
00173  
00174     ////////////////////////////////
00175     // NEW FILE
00176     testFile = "results/MyTestOut3.glf";
00177     {
00178         GlfFile glfOutScoped;
00179         assert(glfOutScoped.openForWrite(testFile.c_str()));
00180         
00181         writeHeader(glfOutScoped, 3);
00182         writeRefSection1(glfOutScoped);
00183         writeSec1Record1(glfOutScoped);
00184         writeSec1Record2(glfOutScoped);
00185         // Test writing new section without end of section marker - auto-added.
00186         writeRefSection2(glfOutScoped);
00187         writeSec2Record1(glfOutScoped);
00188         // Test just letting the file go out of scope with no end
00189         // of section marker - auto added.
00190     }
00191     //////////////////////////////////////////////
00192     // Validate the just written file.
00193     assert(glfIn.openForRead(testFile.c_str()));
00194     
00195     // Test reading refsection with no header - exception.
00196     caughtException = false;
00197     try
00198     {
00199         assert(glfIn.getNextRefSection(glfSection) == false);
00200     }
00201     catch (std::exception& e) 
00202     {
00203         caughtException = true;
00204     }
00205     assert(caughtException);
00206 
00207     // Test reading record with no header - exception.
00208     caughtException = false;
00209     try
00210     {
00211         assert(glfIn.getNextRecord(record) == false);
00212     }
00213     catch (std::exception& e) 
00214     {
00215         caughtException = true;
00216     }
00217     assert(caughtException);
00218 
00219     readHeader(glfIn, 3);
00220 
00221     // Test reading record with no reference section - exception.
00222     caughtException = false;
00223     try
00224     {
00225         assert(glfIn.getNextRecord(record) == false);
00226     }
00227     catch (std::exception& e) 
00228     {
00229         caughtException = true;
00230     }
00231     assert(caughtException);
00232 
00233     // Test reading header after already read - exception
00234     caughtException = false;
00235     try
00236     {
00237         assert(glfIn.readHeader(glfHeader) == false);
00238     }
00239     catch (std::exception& e) 
00240     {
00241         caughtException = true;
00242     }
00243     assert(caughtException);
00244 
00245     readRefSection1(glfIn);
00246     readSec1Record1(glfIn);
00247     readSec1Record2(glfIn);
00248     readEndMarker(glfIn);
00249     readRefSection2(glfIn);
00250     readSec2Record1(glfIn);
00251     readEndMarker(glfIn);
00252     checkEOF(glfIn);
00253    
00254 
00255     // Read again, but text reading next refsection before 
00256     //end of current section - consumes the rest of the records.
00257     assert(glfIn.openForRead(testFile.c_str()));
00258 
00259     readHeader(glfIn, 3);
00260     readRefSection1(glfIn);
00261     readRefSection2(glfIn);
00262     readSec2Record1(glfIn);
00263     readEndMarker(glfIn);
00264     checkEOF(glfIn);
00265  
00266 
00267 }
00268 
00269 
00270 void TestWrite::writeHeader(GlfFile& glfOut, int headerNum)
00271 {
00272     GlfHeader glfHeader;
00273     std::string headerString = "t";
00274     std::string expectedHeader = "";
00275     if(headerNum == 1)
00276     {
00277         expectedHeader = HEADER_TEXT1;
00278     }
00279     else if(headerNum == 2)
00280     {
00281         expectedHeader = HEADER_TEXT2;
00282     }
00283     else if(headerNum == 3)
00284     {
00285         expectedHeader = HEADER_TEXT3;
00286     }
00287 
00288     assert(glfHeader.getHeaderTextString(headerString));
00289     assert(headerString == "");
00290     assert(glfHeader.setHeaderTextString(expectedHeader));
00291     assert(glfHeader.getHeaderTextString(headerString));
00292     assert(headerString == expectedHeader);
00293     assert(glfOut.writeHeader(glfHeader));
00294 }
00295 
00296 
00297 void TestWrite::writeRefSection1(GlfFile& glfOut)
00298 {
00299     GlfRefSection glfSection;
00300 
00301     ////////////////////////////////
00302     // Write the reference section.
00303     std::string refNameString = "";
00304     // Check the default settings (no data has been set yet).
00305     assert(glfSection.getName(refNameString));
00306     assert(refNameString == "");
00307     assert(glfSection.getRefLen() == 0);
00308 
00309     // Set the reference name.
00310     assert(glfSection.setName(SEC1_REFNAME));
00311     // Check properly set.
00312     assert(glfSection.getName(refNameString));
00313     assert(refNameString == SEC1_REFNAME);
00314     assert(glfSection.getRefLen() == 0);
00315 
00316     // Set the reference sequence length.
00317     assert(glfSection.setRefLen(SEC1_REFLEN));
00318     // Check properly set.
00319     assert(glfSection.getRefLen() == SEC1_REFLEN);
00320     assert(glfSection.getName(refNameString));
00321     assert(refNameString == SEC1_REFNAME);
00322 
00323     // Write the reference section
00324     assert(glfOut.writeRefSection(glfSection));
00325 }
00326 
00327 
00328 void TestWrite::writeSec1Record1(GlfFile& glfOut)
00329 {
00330     GlfRecord record;
00331     assert(record.setRecordType(SEC1REC1_RECTYPE));
00332     assert(record.setRefBaseInt(SEC1REC1_REFBASE));
00333     assert(record.setOffset(SEC1REC1_OFFSET));
00334     assert(record.setMinLk(SEC1REC1_MINLK));
00335     assert(record.setReadDepth(SEC1REC1_READDEPTH));
00336     assert(record.setRmsMapQ(SEC1REC1_RMSMAPQ));
00337     assert(glfOut.writeRecord(record));
00338     
00339     // Verify the settings of record 1.
00340     assert(record.getRecordType() == SEC1REC1_RECTYPE);
00341     assert(record.getRefBase() == SEC1REC1_REFBASE);
00342     assert(record.getOffset() == SEC1REC1_OFFSET);
00343     assert(record.getMinLk() == SEC1REC1_MINLK);
00344     assert(record.getReadDepth() == SEC1REC1_READDEPTH);
00345     assert(record.getRmsMapQ() == SEC1REC1_RMSMAPQ);
00346 }
00347 
00348 
00349 void TestWrite::writeSec1Record2(GlfFile& glfOut)
00350 {
00351     //////////////////////////////////////////////
00352     // Write a record of type 2.
00353     GlfRecord record;
00354 
00355     assert(record.setRecordType(SEC1REC2_RECTYPE));
00356     assert(record.setRefBaseInt(SEC1REC2_REFBASE));
00357     assert(record.setOffset(SEC1REC2_OFFSET));
00358     assert(record.setMinLk(SEC1REC2_MINLK));
00359     assert(record.setReadDepth(SEC1REC2_READDEPTH));
00360     assert(record.setRmsMapQ(SEC1REC2_RMSMAPQ));
00361     assert(record.setLkHom1(SEC1REC2_LKHOM1));
00362     assert(record.setLkHom2(SEC1REC2_LKHOM2));
00363     assert(record.setLkHet(SEC1REC2_LKHET));
00364     assert(record.setInsertionIndel1(SEC1REC2_INDELSEQ1));
00365     assert(record.setDeletionIndel2(SEC1REC2_INDELSEQ2));
00366     assert(glfOut.writeRecord(record));
00367 
00368     // Verify the settings of record 2.
00369     std::string indelSeq = "";
00370     assert(record.getRecordType() == SEC1REC2_RECTYPE);
00371     assert(record.getRefBase() == SEC1REC2_REFBASE);
00372     assert(record.getOffset() == SEC1REC2_OFFSET);
00373     assert(record.getMinLk() == SEC1REC2_MINLK);
00374     assert(record.getReadDepth() == SEC1REC2_READDEPTH);
00375     assert(record.getRmsMapQ() == SEC1REC2_RMSMAPQ);
00376     assert(record.getLkHom1() == SEC1REC2_LKHOM1);
00377     assert(record.getLkHom2() == SEC1REC2_LKHOM2);
00378     assert(record.getLkHet() == SEC1REC2_LKHET);
00379     assert(record.getIndel1(indelSeq) == SEC1REC2_INDELLEN1);
00380     assert(indelSeq == SEC1REC2_INDELSEQ1);
00381     assert(record.getIndel2(indelSeq) == SEC1REC2_INDELLEN2);
00382     assert(indelSeq == SEC1REC2_INDELSEQ2);
00383 }
00384 
00385 
00386 void TestWrite::writeEndMarker(GlfFile& glfOut)
00387 {
00388     //////////////////////////////////////////////
00389     // Write a record of type 0.
00390     GlfRecord record;
00391     assert(glfOut.writeRecord(record));
00392 
00393     // Verify the settings of the types.
00394     assert(record.getRecordType() == 0);
00395     assert(record.getRefBase() == 0);
00396 }
00397 
00398 
00399 void TestWrite::writeRefSection2(GlfFile& glfOut)
00400 {
00401     GlfRefSection glfSection;
00402 
00403     ////////////////////////////////
00404     // Write the reference section.
00405     std::string refNameString = "";
00406     // Check the default settings (no data has been set yet).
00407     assert(glfSection.getName(refNameString));
00408     assert(refNameString == "");
00409     assert(glfSection.getRefLen() == 0);
00410 
00411     // Set the reference name.
00412     assert(glfSection.setName(SEC2_REFNAME));
00413     // Check properly set.
00414     assert(glfSection.getName(refNameString));
00415     assert(refNameString == SEC2_REFNAME);
00416     assert(glfSection.getRefLen() == 0);
00417 
00418     // Set the reference sequence length.
00419     assert(glfSection.setRefLen(SEC2_REFLEN));
00420     // Check properly set.
00421     assert(glfSection.getRefLen() == SEC2_REFLEN);
00422     assert(glfSection.getName(refNameString));
00423     assert(refNameString == SEC2_REFNAME);
00424 
00425     // Write the reference section
00426     assert(glfOut.writeRefSection(glfSection));
00427 }
00428 
00429 
00430 void TestWrite::writeSec2Record1(GlfFile& glfOut)
00431 {
00432     GlfRecord record;
00433     assert(record.setRecordType(SEC2REC1_RECTYPE));
00434     assert(record.setRefBaseInt(SEC2REC1_REFBASE));
00435     assert(record.setOffset(SEC2REC1_OFFSET));
00436     assert(record.setMinLk(SEC2REC1_MINLK));
00437     assert(record.setReadDepth(SEC2REC1_READDEPTH));
00438     assert(record.setRmsMapQ(SEC2REC1_RMSMAPQ));
00439     assert(glfOut.writeRecord(record));
00440     
00441     // Verify the settings of record 1.
00442     assert(record.getRecordType() == SEC2REC1_RECTYPE);
00443     assert(record.getRefBase() == SEC2REC1_REFBASE);
00444     assert(record.getOffset() == SEC2REC1_OFFSET);
00445     assert(record.getMinLk() == SEC2REC1_MINLK);
00446     assert(record.getReadDepth() == SEC2REC1_READDEPTH);
00447     assert(record.getRmsMapQ() == SEC2REC1_RMSMAPQ);
00448 }
00449 
00450 
00451 void TestWrite::readHeader(GlfFile& glfIn, int headerNum)
00452 {
00453     GlfHeader glfHeader;
00454     std::string expectedHeader = "";
00455     std::string headerString;
00456     if(headerNum == 1)
00457     {
00458         expectedHeader = HEADER_TEXT1;
00459     }
00460     else if(headerNum == 2)
00461     {
00462         expectedHeader = HEADER_TEXT2;
00463     }
00464     else if(headerNum == 3)
00465     {
00466         expectedHeader = HEADER_TEXT3;
00467     }
00468     // Check the header string.
00469     assert(glfIn.readHeader(glfHeader));
00470     assert(glfHeader.getHeaderTextString(headerString));
00471     assert(headerString == expectedHeader);
00472 }
00473 
00474 void TestWrite::readRefSection1(GlfFile& glfIn)
00475 {
00476     GlfRefSection glfSection;
00477     std::string refNameString;
00478     // Check the reference section.
00479     assert(glfIn.getNextRefSection(glfSection));
00480     assert(glfSection.getName(refNameString));
00481     assert(refNameString == SEC1_REFNAME);
00482     assert(glfSection.getRefLen() == SEC1_REFLEN);
00483 }
00484 
00485 void TestWrite::readSec1Record1(GlfFile& glfIn)
00486 {
00487     GlfRecord record;
00488     // Check the record of type 1.
00489     assert(glfIn.getNextRecord(record));
00490     assert(record.getRecordType() == SEC1REC1_RECTYPE);
00491     assert(record.getRefBase() == SEC1REC1_REFBASE);
00492     assert(record.getOffset() == SEC1REC1_OFFSET);
00493     assert(record.getMinLk() == SEC1REC1_MINLK);
00494     assert(record.getReadDepth() == SEC1REC1_READDEPTH);
00495     assert(record.getRmsMapQ() == SEC1REC1_RMSMAPQ);
00496 }
00497 
00498 void TestWrite::readSec1Record2(GlfFile& glfIn)
00499 {
00500     GlfRecord record;
00501     std::string indelSeq;
00502     //Check the record of type 2.
00503     assert(glfIn.getNextRecord(record));
00504     assert(record.getRecordType() == SEC1REC2_RECTYPE);
00505     assert(record.getRefBase() == SEC1REC2_REFBASE);
00506     assert(record.getOffset() == SEC1REC2_OFFSET);
00507     assert(record.getMinLk() == SEC1REC2_MINLK);
00508     assert(record.getReadDepth() == SEC1REC2_READDEPTH);
00509     assert(record.getRmsMapQ() == SEC1REC2_RMSMAPQ);
00510     assert(record.getLkHom1() == SEC1REC2_LKHOM1);
00511     assert(record.getLkHom2() == SEC1REC2_LKHOM2);
00512     assert(record.getLkHet() == SEC1REC2_LKHET);
00513     assert(record.getIndel1(indelSeq) == SEC1REC2_INDELLEN1);
00514     assert(indelSeq == SEC1REC2_INDELSEQ1);
00515     assert(record.getIndel2(indelSeq) == SEC1REC2_INDELLEN2);
00516     assert(indelSeq == SEC1REC2_INDELSEQ2);
00517 }
00518 
00519 void TestWrite::readEndMarker(GlfFile& glfIn)
00520 {
00521     GlfRecord record;
00522     // Check the record of type 0.
00523     // False, since there are no more records in this section.
00524     assert(glfIn.getNextRecord(record) == false);
00525     assert(record.getRecordType() == 0);
00526     assert(record.getRefBase() == 0);
00527 }
00528 
00529 void TestWrite::readRefSection2(GlfFile& glfIn)
00530 {
00531     GlfRefSection glfSection;
00532     std::string refNameString;
00533     // Check the reference section.
00534     assert(glfIn.getNextRefSection(glfSection));
00535     assert(glfSection.getName(refNameString));
00536     assert(refNameString == SEC2_REFNAME);
00537     assert(glfSection.getRefLen() == SEC2_REFLEN);
00538 }
00539 
00540 
00541 void TestWrite::readSec2Record1(GlfFile& glfIn)
00542 {
00543     GlfRecord record;
00544     // Check the record of type 1.
00545     assert(glfIn.getNextRecord(record));
00546     assert(record.getRecordType() == SEC2REC1_RECTYPE);
00547     assert(record.getRefBase() == SEC2REC1_REFBASE);
00548     assert(record.getOffset() == SEC2REC1_OFFSET);
00549     assert(record.getMinLk() == SEC2REC1_MINLK);
00550     assert(record.getReadDepth() == SEC2REC1_READDEPTH);
00551     assert(record.getRmsMapQ() == SEC2REC1_RMSMAPQ);
00552 }
00553 
00554 void TestWrite::checkEOF(GlfFile& glfIn)
00555 {
00556     GlfHeader glfHeader;
00557     GlfRefSection glfSection;
00558     GlfRecord record;
00559     // Check end of file - no more refsections
00560     assert(glfIn.getNextRefSection(glfSection) == false);
00561     assert(glfIn.isEOF());
00562 }
00563 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends