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 "SamFile.h"
00019 #include "WriteFiles.h"
00020 #include "TestValidate.h"
00021 
00022 #include <assert.h>
00023 #include <stdio.h>
00024 
00025 void testWrite()
00026 {
00027     testHeaderWrite();
00028     testWriteCopiedHeader("testFiles/testSam.sam");
00029 #ifdef __ZLIB_AVAILABLE__
00030     testWriteCopiedHeader("testFiles/testBam.bam");
00031 #endif
00032 }
00033 
00034 void testHeaderWrite()
00035 {
00036     SamFile samOut;
00037 
00038     samOut.OpenForWrite("results/MyTestOut.sam");
00039 
00040     // Create a sam header.
00041     SamFileHeader samHeader;
00042    
00043     std::string headerString = "";
00044 
00045     // Test getting HD & PG and the HD-SO tag when they do not exist.
00046     assert(samHeader.getHD() == NULL);
00047     assert(samHeader.getPG("1") == NULL);
00048     assert(strcmp(samHeader.getTagSO(), "") == 0);
00049 
00050     // Test removing the HD tag that does not exist.
00051     assert(samHeader.removeHD() == true);
00052     assert(samHeader.getHD() == NULL);
00053     assert(strcmp(samHeader.getHDTagValue("VN"), "") == 0);
00054     assert(samHeader.getHeaderString(headerString) == true);
00055     assert(headerString == "");
00056 
00057     char type[3] = "HD";
00058     char tag[3] = "VN";
00059     // Verify it has not yet been added to the parsed header.
00060     assert(strcmp(samHeader.getHDTagValue("VN"), "") == 0);
00061     assert(samHeader.addHeaderLine(type, tag, "1.0") == true);
00062     assert(samHeader.getHeaderString(headerString) == true);
00063     assert(headerString == "@HD\tVN:1.0\n");
00064 
00065     // Verify it was added to the parsed header.
00066     assert(strcmp(samHeader.getHDTagValue("VN"), "1.0") == 0);
00067 
00068     type[0] = 'S';
00069     type[1] = 'Q';
00070     tag[0] = 'L';
00071     tag[1] = 'N';
00072     // Cannot add SQ LN tag without adding the SN tag also.
00073     assert(samHeader.addHeaderLine(type, tag, "123") == false);
00074     assert(samHeader.getHeaderString(headerString) == true);
00075     assert(headerString == "@HD\tVN:1.0\n");
00076 
00077     // Has not yet been added, so returns blank.
00078     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "") == 0);
00079 
00080     // Can't add the SQ type without a LN.
00081     std::string line = "@SQ\tSN:123";
00082     assert(samHeader.addHeaderLine(line.c_str()) == false);
00083     assert(samHeader.getHeaderString(headerString) == true);
00084     assert(headerString == "@HD\tVN:1.0\n");
00085 
00086     // Successfully add a SQ line.
00087     line = "@SQ\tLN:123\tSN:chr20";
00088     assert(samHeader.addHeaderLine(line.c_str()) == true);
00089     assert(samHeader.getHeaderString(headerString) == true);
00090     assert(headerString == "@HD\tVN:1.0\n@SQ\tLN:123\tSN:chr20\n");
00091     // Verify it was added to the parsed header.
00092     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00093 
00094     // Test to make sure nothing changes if try to copy into self.
00095     samHeader = samHeader;
00096     assert(samHeader.addHeaderLine(line.c_str()) == false);
00097     assert(samHeader.getHeaderString(headerString) == true);
00098     assert(headerString == "@HD\tVN:1.0\n@SQ\tLN:123\tSN:chr20\n");
00099     // Verify it was added to the parsed header.
00100     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00101 
00102     samHeader.copy(samHeader);
00103     assert(samHeader.addHeaderLine(line.c_str()) == false);
00104     assert(samHeader.getHeaderString(headerString) == true);
00105     assert(headerString == "@HD\tVN:1.0\n@SQ\tLN:123\tSN:chr20\n");
00106     // Verify it was added to the parsed header.
00107     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00108 
00109     // Test adding an HD that is already there.
00110     assert(samHeader.addHeaderLine("@HD\tVN:1.1") == false);
00111     assert(samHeader.getHeaderString(headerString) == true);
00112     assert(headerString == "@HD\tVN:1.0\n@SQ\tLN:123\tSN:chr20\n");
00113     // Verify it was added to the parsed header.
00114     assert(strcmp(samHeader.getHDTagValue("VN"), "1.0") == 0);
00115 
00116     // Test copying the header.
00117     SamFileHeader newHeader = samHeader;
00118     assert(newHeader.getHeaderString(headerString) == true);
00119     assert(headerString == "@HD\tVN:1.0\n@SQ\tLN:123\tSN:chr20\n");
00120     // Verify it was added to the parsed header.
00121     assert(strcmp(newHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00122     
00123 
00124     // Modify one of the tags.
00125     assert(samHeader.setHDTag("VN", "1.1") == true);
00126     assert(samHeader.getHeaderString(headerString) == true);
00127     assert(headerString == "@HD\tVN:1.1\n@SQ\tLN:123\tSN:chr20\n");
00128     // Verify it was modified.
00129     assert(strcmp(samHeader.getHDTagValue("VN"), "1.1") == 0);
00130 
00131     // Remove the version.
00132     assert(samHeader.setHDTag("VN", "") == true);
00133     assert(samHeader.getHeaderString(headerString) == true);
00134     assert(headerString == "@SQ\tLN:123\tSN:chr20\n");
00135     // Verify it was removed.
00136     assert(strcmp(samHeader.getHDTagValue("VN"), "") == 0);
00137    
00138     // Remove the SN from the SQ type - fails because SN is the key.
00139     assert(samHeader.setSQTag("SN", "", "chr20") == false);
00140     assert(samHeader.getHeaderString(headerString) == true);
00141     assert(headerString == "@SQ\tLN:123\tSN:chr20\n");
00142     // Verify it was not removed.
00143     assert(strcmp(samHeader.getSQTagValue("SN", "chr20"), "chr20") == 0);
00144     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00145    
00146     // Can't remove the LN from the SQ type
00147     assert(samHeader.setSQTag("LN", "", "chr20") == false);
00148     assert(samHeader.getHeaderString(headerString) == true);
00149     assert(headerString == "@SQ\tLN:123\tSN:chr20\n");
00150     // Verify it was not removed.
00151     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00152     assert(strcmp(samHeader.getSQTagValue("SN", "chr20"), "chr20") == 0);
00153 
00154     // Delete the SQ line.
00155     assert(samHeader.removeSQ("chr20") == true);
00156     // There is no header string.
00157     assert(samHeader.getHeaderString(headerString) == true);
00158     assert(headerString == "");
00159     // Verify it was removed.
00160     assert(strcmp(samHeader.getSQTagValue("SN", "chr20"), "") == 0);
00161     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "") == 0);
00162 
00163     // Create an SQ record and add it back in.
00164     SamHeaderSQ* sq = new SamHeaderSQ();
00165     assert(sq->setTag("LN", "123") == true);
00166     assert(sq->setTag("SN", "chr20") == true);
00167     assert(samHeader.addSQ(sq) == true);
00168     assert(samHeader.getHeaderString(headerString) == true);
00169     assert(headerString == "@SQ\tLN:123\tSN:chr20\n");
00170     // Verify it was added.
00171     assert(strcmp(samHeader.getSQTagValue("SN", "chr20"), "chr20") == 0);
00172     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00173 
00174     // Modify a tag.
00175     assert(sq->setTag("LN", "222") == true);
00176     assert(samHeader.getHeaderString(headerString) == true);
00177     assert(headerString == "@SQ\tLN:222\tSN:chr20\n");
00178     // Verify it was modified.
00179     assert(strcmp(samHeader.getSQTagValue("SN", "chr20"), "chr20") == 0);
00180     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "222") == 0);
00181    
00182 
00183     // Test adding another SQ with the same key.
00184     SamHeaderSQ* sq2 = new SamHeaderSQ();
00185     assert(sq2->setTag("LN", "333") == true);
00186     assert(sq2->setTag("SN", "chr20") == true);
00187     assert(samHeader.addSQ(sq2) == false);
00188     assert(samHeader.getHeaderString(headerString) == true);
00189     assert(headerString == "@SQ\tLN:222\tSN:chr20\n");
00190     // Verify it was not added.
00191     assert(strcmp(samHeader.getSQTagValue("SN", "chr20"), "chr20") == 0);
00192     assert(strcmp(samHeader.getSQTagValue("LN", "chr20"), "222") == 0);
00193     delete sq2;
00194 
00195     // Add a new tag to the SQ tag.
00196     assert(samHeader.setSQTag("AS", "HG18", "chr20") == true);
00197     assert(samHeader.getHeaderString(headerString) == true);
00198     assert(headerString == "@SQ\tLN:222\tSN:chr20\tAS:HG18\n");
00199     // Verify it was added.
00200     assert(strcmp(samHeader.getSQTagValue("AS", "chr20"), "HG18") == 0);
00201    
00202     // Modify the AS tag.
00203     assert(samHeader.setSQTag("AS", "HG19", "chr20") == true);
00204     assert(samHeader.getHeaderString(headerString) == true);
00205     assert(headerString == "@SQ\tLN:222\tSN:chr20\tAS:HG19\n");
00206     // Verify it was added.
00207     assert(strcmp(samHeader.getSQTagValue("AS", "chr20"), "HG19") == 0);
00208 
00209     // Add a new tag .
00210     sq2 = new SamHeaderSQ();
00211     assert(sq2->setTag("LN", "333") == true);
00212     assert(sq2->setTag("SN", "chr1") == true);
00213     assert(samHeader.addSQ(sq2) == true);
00214     assert(samHeader.getHeaderString(headerString) == true);
00215     assert(headerString == 
00216            "@SQ\tLN:222\tSN:chr20\tAS:HG19\n@SQ\tLN:333\tSN:chr1\n");
00217     // Verify it was added.
00218     assert(strcmp(samHeader.getSQTagValue("SN", "chr1"), "chr1") == 0);
00219     assert(strcmp(samHeader.getSQTagValue("LN", "chr1"), "333") == 0);
00220 
00221     // Test removing an SQ tag that does not exist.
00222     assert(samHeader.removeSQ("chr100") == true);
00223     assert(samHeader.getHeaderString(headerString) == true);
00224     assert(headerString == 
00225            "@SQ\tLN:222\tSN:chr20\tAS:HG19\n@SQ\tLN:333\tSN:chr1\n");
00226 
00227     // Remove the newly added sq2 by resetting it.
00228     sq2->reset();
00229     // Verify it was removed.
00230     assert(samHeader.getHeaderString(headerString) == true);
00231     assert(headerString == "@SQ\tLN:222\tSN:chr20\tAS:HG19\n");
00232     assert(strcmp(samHeader.getSQTagValue("SN", "chr1"), "") == 0);
00233     assert(strcmp(samHeader.getSQTagValue("LN", "chr1"), "") == 0);
00234 
00235     // Test getting HD which does exist since it was set before.  Even
00236     // though the VN was removed so it doesn't appear in the header string,
00237     // it was never actually removed.
00238     SamHeaderHD* hd = samHeader.getHD();
00239     assert(hd != NULL);
00240     // Blank since the sort order was never set.
00241     assert(strcmp(samHeader.getTagSO(), "") == 0);
00242 
00243     // Set the version number.
00244     assert(hd->setTag("VN", "2.1") == true);
00245     // Verify it was added.
00246     assert(samHeader.getHeaderString(headerString) == true);
00247     assert(headerString == "@HD\tVN:2.1\n@SQ\tLN:222\tSN:chr20\tAS:HG19\n");
00248     assert(strcmp(samHeader.getHDTagValue("VN"), "2.1") == 0);
00249 
00250     // Set the SO
00251     assert(hd->setTag("SO", "coordinate") == true);
00252     // Verify it was added.
00253     assert(samHeader.getHeaderString(headerString) == true);
00254     assert(headerString == 
00255            "@HD\tVN:2.1\tSO:coordinate\n@SQ\tLN:222\tSN:chr20\tAS:HG19\n");
00256     assert(strcmp(samHeader.getHDTagValue("SO"), "coordinate") == 0);
00257 
00258     // Reset the header.
00259     samHeader.resetHeader();
00260     assert(samHeader.getHeaderString(headerString) == true);
00261     assert(headerString == "");
00262    
00263     // Add a new HD tag.
00264     assert(samHeader.setHDTag("SO", "queryname") == true);
00265     assert(strcmp(samHeader.getHDTagValue("SO"), "queryname") == 0);
00266     assert(samHeader.getHeaderString(headerString) == true);
00267     // Blank since missing VN.
00268     assert(headerString == "");
00269 
00270     // Set the VN.
00271     assert(samHeader.setHDTag("VN", "3.1") == true);
00272     assert(strcmp(samHeader.getHDTagValue("SO"), "queryname") == 0);
00273     assert(strcmp(samHeader.getHDTagValue("VN"), "3.1") == 0);
00274     assert(samHeader.getHeaderString(headerString) == true);
00275     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n");
00276    
00277     //////////////////////////////////////////////////////////////
00278     // Test removing a non-existent PG.
00279     assert(samHeader.removePG("1") == true);
00280     assert(samHeader.getHeaderString(headerString) == true);
00281     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n");
00282 
00283     // Test adding a null PG.
00284     SamHeaderPG* pg = NULL;
00285     assert(samHeader.addPG(pg) == false);
00286     assert(samHeader.getHeaderString(headerString) == true);
00287     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n");
00288    
00289     // Add a PG tag.
00290     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "") == 0);
00291     assert(samHeader.setPGTag("ID", "pid", "pid") == true);
00292     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00293     assert(samHeader.getHeaderString(headerString) == true);
00294     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\n");
00295 
00296     // Verify can't modify the key.
00297     assert(samHeader.setPGTag("ID", "pid1", "pid") == false);
00298     assert(samHeader.getHeaderString(headerString) == true);
00299     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\n");
00300     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00301 
00302     // Test adding a null PG.
00303     pg = NULL;
00304     assert(samHeader.addPG(pg) == false);
00305     assert(samHeader.getHeaderString(headerString) == true);
00306     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\n");
00307    
00308     // Test adding a PG header when it already exists.
00309     pg = new SamHeaderPG();
00310     assert(pg->setTag("ID", "pid") == true);
00311     assert(samHeader.addPG(pg) == false);
00312     assert(samHeader.getHeaderString(headerString) == true);
00313     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\n");
00314     delete pg;
00315 
00316     // Get a PG that does not exist.
00317     pg = samHeader.getPG("pid1");
00318     assert(pg == NULL);
00319 
00320     // Get a PG tag that does not exist.
00321     assert(strcmp(samHeader.getPGTagValue("CL", "pid"), "") == 0);
00322 
00323     // Get the PG tag.
00324     pg = samHeader.getPG("pid");
00325     assert(pg != NULL);
00326     assert(strcmp(pg->getTagValue("ID"), "pid") == 0);
00327     // Add a tag to the PG.
00328     assert(pg->setTag("VN", "pg1") == true);
00329     assert(strcmp(samHeader.getPGTagValue("VN", "pid"), "pg1") == 0);
00330     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00331     assert(samHeader.getHeaderString(headerString) == true);
00332     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\tVN:pg1\n");
00333    
00334     // Test modifying the key tag - fails.
00335     assert(pg->setTag("ID", "pid1") == false);
00336     assert(strcmp(samHeader.getPGTagValue("VN", "pid"), "pg1") == 0);
00337     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00338     assert(samHeader.getHeaderString(headerString) == true);
00339     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\tVN:pg1\n");
00340 
00341     // Test modifying the VN tag.
00342     assert(samHeader.setPGTag("VN", "pg", "pid") == true);
00343     assert(strcmp(samHeader.getPGTagValue("VN", "pid"), "pg") == 0);
00344     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00345     assert(samHeader.getHeaderString(headerString) == true);
00346     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\tVN:pg\n");
00347    
00348     // Test removing the VN tag.
00349     assert(pg->setTag("VN", "") == true);
00350     assert(strcmp(samHeader.getPGTagValue("VN", "pid"), "") == 0);
00351     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00352     assert(samHeader.getHeaderString(headerString) == true);
00353     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\n");
00354    
00355     // Test removing a PG that does not exist.
00356     assert(samHeader.removePG("pid1") == true);
00357     assert(samHeader.getHeaderString(headerString) == true);
00358     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:pid\n");
00359     assert(strcmp(samHeader.getPGTagValue("VN", "pid"), "") == 0);
00360     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "pid") == 0);
00361 
00362     // Test removing the PG.
00363     assert(samHeader.removePG("pid") == true);
00364     assert(samHeader.getHeaderString(headerString) == true);
00365     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n");
00366     assert(strcmp(samHeader.getPGTagValue("VN", "pid"), "") == 0);
00367     assert(strcmp(samHeader.getPGTagValue("ID", "pid"), "") == 0);
00368 
00369     // Test adding a PG header.
00370     pg = new SamHeaderPG();
00371     assert(pg->setTag("ID", "newID") == true);
00372     assert(samHeader.addPG(pg) == true);
00373     assert(samHeader.getHeaderString(headerString) == true);
00374     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n");
00375 
00376     // Test adding a PG that is already there.
00377     assert(samHeader.addHeaderLine("@PG\tID:newID") == false);
00378     assert(samHeader.getHeaderString(headerString) == true);
00379     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n");
00380     // Verify it was added to the parsed header.
00381     assert(strcmp(samHeader.getPGTagValue("ID", "newID"), "newID") == 0);
00382 
00383     // Test adding another PG header.
00384     pg = new SamHeaderPG();
00385     assert(pg->setTag("ID", "newID1") == true);
00386     assert(samHeader.addPG(pg) == true);
00387     assert(samHeader.getHeaderString(headerString) == true);
00388     assert(headerString == 
00389            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@PG\tID:newID1\n");
00390 
00391     // Test adding another PG header.
00392     pg = new SamHeaderPG();
00393     assert(pg->setTag("ID", "pid") == true);
00394     assert(samHeader.addPG(pg) == true);
00395     assert(samHeader.getHeaderString(headerString) == true);
00396     assert(headerString == 
00397            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@PG\tID:newID1\n@PG\tID:pid\n");
00398 
00399     // Test removing the new pg.
00400     assert(samHeader.removePG("newID1") == true);
00401     assert(samHeader.getHeaderString(headerString) == true);
00402     assert(headerString == 
00403            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@PG\tID:pid\n");
00404 
00405     // Test removing the other new pg.
00406     assert(samHeader.removePG("pid") == true);
00407     assert(samHeader.getHeaderString(headerString) == true);
00408     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n");
00409 
00410     // Test adding a tag
00411     assert(samHeader.setPGTag("VN", "1.0", "newID") == true);
00412     assert(strcmp(samHeader.getPGTagValue("VN", "newID"), "1.0") == 0);
00413     assert(strcmp(samHeader.getPGTagValue("ID", "newID"), "newID") == 0);
00414     assert(samHeader.getHeaderString(headerString) == true);
00415     assert(headerString == 
00416            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\tVN:1.0\n");
00417    
00418     // Test removing a tag
00419     assert(samHeader.setPGTag("VN", "", "newID") == true);
00420     assert(strcmp(samHeader.getPGTagValue("VN", "newID"), "") == 0);
00421     assert(strcmp(samHeader.getPGTagValue("ID", "newID"), "newID") == 0);
00422     assert(samHeader.getHeaderString(headerString) == true);
00423     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n");
00424 
00425     ////////////////////////////////////////////////////////////////////
00426     // Add an SQ, but fail since LN is not specified.
00427     assert(samHeader.setSQTag("AS", "HG18", "newName") == false);
00428     assert(samHeader.getHeaderString(headerString) == true);
00429     // SQ does not show up since it is missing the LN field.
00430     assert(headerString == 
00431            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n");
00432     // Add the SQ's SN, but fail since LN is not specified.
00433     assert(samHeader.setSQTag("SN", "newName", "newName") == false);
00434     assert(samHeader.getHeaderString(headerString) == true);
00435     assert(headerString == 
00436            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n");
00437      sq = samHeader.getSQ("newName");
00438     assert(sq == NULL);
00439    // Add the SQ with the LN tag.
00440     assert(samHeader.setSQTag("LN", "111", "newName") == true);
00441     assert(strcmp(samHeader.getSQTagValue("SN", "newName"), "newName") == 0);
00442     assert(strcmp(samHeader.getSQTagValue("AS", "newName"), "") == 0);
00443     assert(strcmp(samHeader.getSQTagValue("LN", "newName"), "111") == 0);
00444     assert(samHeader.getHeaderString(headerString) == true);
00445     assert(headerString == 
00446            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\n");
00447     // Add the AS.
00448     assert(samHeader.setSQTag("AS", "HG18", "newName") == true);
00449     assert(strcmp(samHeader.getSQTagValue("AS", "newName"), "HG18") == 0);
00450     assert(strcmp(samHeader.getSQTagValue("SN", "newName"), "newName") == 0);
00451     assert(strcmp(samHeader.getSQTagValue("LN", "newName"), "111") == 0);
00452     assert(samHeader.getHeaderString(headerString) == true);
00453     assert(headerString == 
00454            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\n");
00455 
00456     // Get the SQ.
00457     sq = samHeader.getSQ("newName");
00458     assert(sq != NULL);
00459     // Modify the SQ
00460     assert(sq->setTag("SP", "species") == true);
00461     assert(strcmp(samHeader.getSQTagValue("SN", "newName"), "newName") == 0);
00462     assert(strcmp(samHeader.getSQTagValue("AS", "newName"), "HG18") == 0);
00463     assert(strcmp(samHeader.getSQTagValue("LN", "newName"), "111") == 0);
00464     assert(strcmp(samHeader.getSQTagValue("SP", "newName"), "species") == 0);
00465     assert(samHeader.getHeaderString(headerString) == true);
00466     assert(headerString == 
00467            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n");
00468 
00469     //////////////////////////////////////////////////////////////////////
00470     // Add a new RG Tag
00471     assert(samHeader.setRGTag("ID", "rgID", "rgID") == true);
00472     assert(samHeader.getHeaderString(headerString) == true);
00473     // New RG does not show up since it is still missing a required field.
00474     assert(headerString == 
00475            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n");
00476     assert(strcmp(samHeader.getRGTagValue("ID", "rgID"), "rgID") == 0);
00477    
00478     // Add the missing SM field.
00479     assert(samHeader.setRGTag("SM", "sm1", "rgID") == true);
00480     assert(samHeader.getHeaderString(headerString) == true);
00481     assert(headerString == 
00482            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n");
00483     assert(strcmp(samHeader.getRGTagValue("ID", "rgID"), "rgID") == 0);
00484    
00485     // Verify can't modify the key.
00486     assert(samHeader.setRGTag("ID", "rgID1", "rgID") == false);
00487     assert(samHeader.getHeaderString(headerString) == true);
00488     assert(headerString == 
00489            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n");
00490     assert(strcmp(samHeader.getRGTagValue("ID", "rgID"), "rgID") == 0);
00491 
00492     // Verify that the copied header did not change.
00493     assert(newHeader.getHeaderString(headerString) == true);
00494     assert(headerString == "@HD\tVN:1.0\n@SQ\tLN:123\tSN:chr20\n");
00495     // Verify it was added to the parsed header.
00496     assert(strcmp(newHeader.getSQTagValue("LN", "chr20"), "123") == 0);
00497 
00498     // Add a new RG Tag
00499     assert(samHeader.setRGTag("SM", "sample1", "rgID1") == true);
00500     assert(samHeader.getHeaderString(headerString) == true);
00501     // String does not show the tag until all required fields are there.
00502     assert(headerString ==
00503            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n");
00504     assert(strcmp(samHeader.getRGTagValue("ID", "rgID1"), "rgID1") == 0);
00505     assert(strcmp(samHeader.getRGTagValue("SM", "rgID1"), "sample1") == 0);
00506    
00507     // Modify an RG tag.
00508     assert(samHeader.setRGTag("SM", "sample", "rgID1") == true);
00509     assert(samHeader.getHeaderString(headerString) == true);
00510     assert(headerString == 
00511            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00512     assert(strcmp(samHeader.getRGTagValue("ID", "rgID1"), "rgID1") == 0);
00513     assert(strcmp(samHeader.getRGTagValue("SM", "rgID1"), "sample") == 0);
00514 
00515     // Test removing an rg that does not exist.
00516     assert(samHeader.removeRG("rgID2") == true);
00517     assert(samHeader.getHeaderString(headerString) == true);
00518     assert(headerString == 
00519            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00520    
00521     // Create a new RG, set some values and add it.
00522     SamHeaderRG* rg = new SamHeaderRG();
00523     // Try adding it without a key.
00524     assert(samHeader.addRG(rg) == false);
00525     assert(samHeader.getHeaderString(headerString) == true);
00526     assert(headerString == 
00527            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00528     // Set some values in rg.
00529     assert(rg->setTag("ID", "rgID2") == true);
00530     assert(rg->setTag("SM", "sm2") == true);
00531     assert(samHeader.getHeaderString(headerString) == true);
00532     assert(headerString == 
00533            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00534     // Add the new RG.
00535     assert(samHeader.addRG(rg) == true);
00536     assert(samHeader.getHeaderString(headerString) == true);
00537     assert(headerString == 
00538            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n@RG\tID:rgID2\tSM:sm2\n");
00539     assert(strcmp(samHeader.getRGTagValue("ID", "rgID2"), "rgID2") == 0);
00540 
00541     // Test trying to add another one with the same key.
00542     rg = new SamHeaderRG();
00543     assert(rg->setTag("ID", "rgID2") == true);
00544     assert(samHeader.addRG(rg) == false);
00545     assert(samHeader.getHeaderString(headerString) == true);
00546     assert(headerString == 
00547            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n@RG\tID:rgID2\tSM:sm2\n");
00548 
00549 
00550     // Test removing the rg again.
00551     assert(samHeader.removeRG("rgID2") == true);
00552     assert(samHeader.getHeaderString(headerString) == true);
00553     assert(headerString ==
00554            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00555    
00556     // Test getting an rg tag that doesn't exist
00557     assert(strcmp(samHeader.getRGTagValue("DS", "rgID"), "") == 0);
00558     assert(samHeader.getHeaderString(headerString) == true);
00559     assert(headerString == 
00560            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00561    
00562     // Test getting an rg tag from a removed key
00563     assert(strcmp(samHeader.getRGTagValue("ID", "rgID2"), "") == 0);
00564     assert(samHeader.getHeaderString(headerString) == true);
00565     assert(headerString == 
00566            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00567 
00568     // Test getting an rg tag from an key that doesn't exist
00569     assert(strcmp(samHeader.getRGTagValue("ID", "rgID22"), "") == 0);
00570     assert(samHeader.getHeaderString(headerString) == true);
00571     assert(headerString == 
00572            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00573 
00574     // Test adding a null header.
00575     rg = NULL;
00576     assert(samHeader.addRG(rg) == false);
00577     assert(samHeader.getHeaderString(headerString) == true);
00578     assert(headerString == 
00579            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n");
00580    
00581     // Test adding the deleted header back in.
00582     rg = new SamHeaderRG();
00583     assert(rg->setTag("ID", "rgID2") == true);
00584     assert(rg->setTag("SM", "sm2") == true);
00585     assert(samHeader.addRG(rg) == true);
00586     assert(samHeader.getHeaderString(headerString) == true);
00587     assert(headerString ==
00588            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n@RG\tID:rgID2\tSM:sm2\n");
00589 
00590     // Test adding an RG that is already there.
00591     assert(samHeader.addHeaderLine("@RG\tID:rgID\tSM:sm5") == false);
00592     assert(samHeader.getHeaderString(headerString) == true);
00593     assert(headerString == "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample\n@RG\tID:rgID2\tSM:sm2\n");
00594     // Verify it was added to the parsed header.
00595     assert(strcmp(samHeader.getRGTagValue("SM", "rgID"), "sm1") == 0);
00596 
00597 
00598     // Get an RG record then modify it.
00599     rg = samHeader.getRG("rgID1");
00600     assert(rg != NULL);
00601     assert(rg->setTag("SM", "sample1") == true);
00602     assert(samHeader.getHeaderString(headerString) == true);
00603     assert(headerString == 
00604            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n");
00605    
00606     // Try to modify the key.
00607     assert(rg->setTag("ID", "rgID111") == false);
00608     assert(samHeader.getHeaderString(headerString) == true);
00609     assert(headerString ==
00610            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n");
00611    
00612     ////////////////////////////////////////////////////////////////////////////
00613     // Test getting a comment when there aren't any.
00614     assert(strcmp(samHeader.getNextComment(), "") == 0);
00615     assert(samHeader.getHeaderString(headerString) == true);
00616     assert(headerString ==
00617            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n");
00618 
00619     // Test getting each headerline when there are no comments.
00620     const char* hdrlinechar;
00621     std::string hdrline;
00622     assert(samHeader.getNextHeaderLine(hdrline));
00623     hdrlinechar = hdrline.c_str();
00624     // Test to make sure there is not memory corruption.
00625     std::string tmpString = "@SQ\tSN:queryname\tVN:3.1\n";
00626     assert(hdrline == "@HD\tSO:queryname\tVN:3.1\n");
00627     assert(strcmp(hdrlinechar,
00628                   "@HD\tSO:queryname\tVN:3.1\n") == 0);
00629 
00630     assert(samHeader.getNextHeaderLine(hdrline));
00631     assert(hdrline == "@PG\tID:newID\n");
00632     assert(samHeader.getNextHeaderLine(hdrline));
00633     assert(hdrline == "@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n");
00634     assert(samHeader.getNextHeaderLine(hdrline)); 
00635     assert(hdrline == "@RG\tID:rgID\tSM:sm1\n");
00636     assert(samHeader.getNextHeaderLine(hdrline));
00637     assert(hdrline == "@RG\tID:rgID1\tSM:sample1\n");
00638     assert(samHeader.getNextHeaderLine(hdrline)); 
00639     assert(hdrline == "@RG\tID:rgID2\tSM:sm2\n");
00640     assert(!samHeader.getNextHeaderLine(hdrline));
00641     assert(hdrline == "");
00642     assert(!samHeader.getNextHeaderLine(hdrline));
00643     assert(hdrline == "");
00644     assert(samHeader.getHeaderString(headerString) == true);
00645     assert(headerString ==
00646            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n");
00647 
00648     // Verify that getHeaderRecord returns nothing.
00649     assert(samHeader.getNextHeaderRecord() == NULL);
00650 
00651     // Reset the header record iter.
00652     samHeader.resetHeaderRecordIter();
00653 
00654     // Test getting each headerrecord when there are no comments.
00655     SamHeaderRecord* hdrRec = samHeader.getNextHeaderRecord();
00656     assert(hdrRec != NULL);
00657     assert(strcmp(hdrRec->getTypeString(), "HD") == 0);
00658     assert(hdrRec->getType() == SamHeaderRecord::HD);
00659     assert(strcmp(hdrRec->getTagValue("SO"), "queryname") == 0);
00660     assert(strcmp(hdrRec->getTagValue("VN"), "3.1") == 0);
00661     hdrRec = samHeader.getNextHeaderRecord();
00662     assert(hdrRec != NULL);
00663     assert(strcmp(hdrRec->getTypeString(), "PG") == 0);
00664     assert(hdrRec->getType() == SamHeaderRecord::PG);
00665     assert(strcmp(hdrRec->getTagValue("ID"), "newID") == 0);
00666     hdrRec = samHeader.getNextHeaderRecord();
00667     assert(hdrRec != NULL);
00668     assert(strcmp(hdrRec->getTypeString(), "SQ") == 0);
00669     assert(hdrRec->getType() == SamHeaderRecord::SQ);
00670     assert(strcmp(hdrRec->getTagValue("SN"), "newName") == 0);
00671     assert(strcmp(hdrRec->getTagValue("AS"), "HG18") == 0);
00672     assert(strcmp(hdrRec->getTagValue("LN"), "111") == 0);
00673     hdrRec = samHeader.getNextHeaderRecord();
00674     assert(hdrRec != NULL);
00675     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00676     assert(hdrRec->getType() == SamHeaderRecord::RG);
00677     assert(strcmp(hdrRec->getTagValue("ID"), "rgID") == 0);
00678     assert(strcmp(hdrRec->getTagValue("SM"), "sm1") == 0);
00679 
00680     // Get the SQ Header Record (should have no affect on the general
00681     // getNextHeaderRecord calls).
00682     hdrRec = samHeader.getNextSQRecord();
00683     assert(hdrRec != NULL);
00684     assert(strcmp(hdrRec->getTypeString(), "SQ") == 0);
00685     assert(hdrRec->getType() == SamHeaderRecord::SQ);
00686     assert(strcmp(hdrRec->getTagValue("SN"), "newName") == 0);
00687     assert(strcmp(hdrRec->getTagValue("AS"), "HG18") == 0);
00688     assert(strcmp(hdrRec->getTagValue("LN"), "111") == 0);
00689     // Only one SQ Header Record.
00690     hdrRec = samHeader.getNextSQRecord();
00691     assert(hdrRec == NULL);
00692 
00693     // Get the RG/PG Header Records (should have no affect on the general
00694     // getNextHeaderRecord calls).
00695     hdrRec = samHeader.getNextRGRecord();
00696     assert(hdrRec != NULL);
00697     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00698     assert(hdrRec->getType() == SamHeaderRecord::RG);
00699     assert(strcmp(hdrRec->getTagValue("ID"), "rgID") == 0);
00700     assert(strcmp(hdrRec->getTagValue("SM"), "sm1") == 0);
00701     // Get the next RG record.
00702     hdrRec = samHeader.getNextRGRecord();
00703     assert(hdrRec != NULL);
00704     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00705     assert(hdrRec->getType() == SamHeaderRecord::RG);
00706     assert(strcmp(hdrRec->getTagValue("ID"), "rgID1") == 0);
00707     assert(strcmp(hdrRec->getTagValue("SM"), "sample1") == 0);
00708     // Get the PG record.
00709     hdrRec = samHeader.getNextPGRecord();
00710     assert(hdrRec != NULL);
00711     assert(strcmp(hdrRec->getTypeString(), "PG") == 0);
00712     assert(hdrRec->getType() == SamHeaderRecord::PG);
00713     assert(strcmp(hdrRec->getTagValue("ID"), "newID") == 0);
00714     // Get the last RG record.
00715     hdrRec = samHeader.getNextRGRecord();
00716     assert(hdrRec != NULL);
00717     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00718     assert(hdrRec->getType() == SamHeaderRecord::RG);
00719     assert(strcmp(hdrRec->getTagValue("ID"), "rgID2") == 0);
00720     assert(strcmp(hdrRec->getTagValue("SM"), "sm2") == 0);
00721     // Already got all RG Records.
00722     hdrRec = samHeader.getNextRGRecord();
00723     assert(hdrRec == NULL);
00724     // Reset the RG record.
00725     samHeader.resetRGRecordIter();
00726     // Get the RG record.
00727     hdrRec = samHeader.getNextRGRecord();
00728     assert(hdrRec != NULL);
00729     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00730     assert(hdrRec->getType() == SamHeaderRecord::RG);
00731     assert(strcmp(hdrRec->getTagValue("ID"), "rgID") == 0);
00732     assert(strcmp(hdrRec->getTagValue("SM"), "sm1") == 0);
00733     // No more PG records.
00734     hdrRec = samHeader.getNextPGRecord();
00735     assert(hdrRec == NULL);
00736     // No more SQ records.
00737     hdrRec = samHeader.getNextSQRecord();
00738     assert(hdrRec == NULL);    
00739     // Reset the SQ record iterator.
00740     samHeader.resetSQRecordIter();
00741     // No more PG records.
00742     hdrRec = samHeader.getNextPGRecord();
00743     assert(hdrRec == NULL);
00744     // Get the now reset SQ record.
00745     hdrRec = samHeader.getNextSQRecord();
00746     assert(hdrRec != NULL);
00747     assert(strcmp(hdrRec->getTypeString(), "SQ") == 0);
00748     assert(hdrRec->getType() == SamHeaderRecord::SQ);
00749     assert(strcmp(hdrRec->getTagValue("SN"), "newName") == 0);
00750     assert(strcmp(hdrRec->getTagValue("AS"), "HG18") == 0);
00751     assert(strcmp(hdrRec->getTagValue("LN"), "111") == 0);
00752     // Only one SQ Header Record.
00753     hdrRec = samHeader.getNextSQRecord();
00754     assert(hdrRec == NULL);
00755     // Reset the PG record iterator.
00756     samHeader.resetPGRecordIter();
00757     // No more SQ records.
00758     hdrRec = samHeader.getNextSQRecord();
00759     assert(hdrRec == NULL);    
00760     // Get the next RG record.
00761     hdrRec = samHeader.getNextRGRecord();
00762     assert(hdrRec != NULL);
00763     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00764     assert(hdrRec->getType() == SamHeaderRecord::RG);
00765     assert(strcmp(hdrRec->getTagValue("ID"), "rgID1") == 0);
00766     assert(strcmp(hdrRec->getTagValue("SM"), "sample1") == 0);
00767     // Get the PG record.
00768     hdrRec = samHeader.getNextPGRecord();
00769     assert(hdrRec != NULL);
00770     assert(strcmp(hdrRec->getTypeString(), "PG") == 0);
00771     assert(hdrRec->getType() == SamHeaderRecord::PG);
00772     assert(strcmp(hdrRec->getTagValue("ID"), "newID") == 0);
00773 
00774 
00775     hdrRec = samHeader.getNextHeaderRecord();
00776     assert(hdrRec != NULL);
00777     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00778     assert(hdrRec->getType() == SamHeaderRecord::RG);
00779     assert(strcmp(hdrRec->getTagValue("ID"), "rgID1") == 0);
00780     assert(strcmp(hdrRec->getTagValue("SM"), "sample1") == 0);
00781     hdrRec = samHeader.getNextHeaderRecord();
00782     assert(hdrRec != NULL);
00783     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00784     assert(hdrRec->getType() == SamHeaderRecord::RG);
00785     assert(strcmp(hdrRec->getTagValue("ID"), "rgID2") == 0);
00786     assert(strcmp(hdrRec->getTagValue("SM"), "sm2") == 0);
00787     hdrRec = samHeader.getNextHeaderRecord();
00788     assert(hdrRec == NULL);
00789     hdrRec = samHeader.getNextHeaderRecord();
00790     assert(hdrRec == NULL);
00791     assert(!samHeader.getNextHeaderLine(hdrline));
00792     assert(hdrline == "");
00793     assert(!samHeader.getNextHeaderLine(hdrline));
00794     assert(hdrline == "");
00795 
00796     assert(samHeader.getHeaderString(headerString) == true);
00797     assert(headerString ==
00798            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n");
00799 
00800     // Add some comments.
00801     assert(samHeader.addComment("My Comment") == true);
00802     assert(samHeader.getHeaderString(headerString) == true);
00803     assert(headerString ==
00804            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n@CO\tMy Comment\n");
00805 
00806     // Call getNextHeaderRecord - still nothing.
00807     hdrRec = samHeader.getNextHeaderRecord();
00808     assert(hdrRec == NULL);   
00809 
00810     // Call getNextHeaderLine - should return the comment.
00811     assert(samHeader.getNextHeaderLine(hdrline));
00812     assert(hdrline == "@CO\tMy Comment\n");
00813     assert(!samHeader.getNextHeaderLine(hdrline));
00814     assert(hdrline == "");
00815     assert(!samHeader.getNextHeaderLine(hdrline));
00816     assert(hdrline == "");
00817 
00818     // Call getNextCommentLine - should return the comment.
00819     assert(strcmp(samHeader.getNextComment(), "My Comment") == 0);
00820     assert(strcmp(samHeader.getNextComment(), "") == 0);
00821     assert(strcmp(samHeader.getNextComment(), "") == 0);
00822 
00823     // Add another comment.
00824     assert(samHeader.addComment("My Comment2") == true);
00825     assert(samHeader.getHeaderString(headerString) == true);
00826     assert(headerString ==
00827            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n@CO\tMy Comment\n@CO\tMy Comment2\n");
00828 
00829     newHeader = samHeader;
00830     assert(newHeader.getHeaderString(headerString) == true);
00831     assert(headerString ==
00832            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n@CO\tMy Comment\n@CO\tMy Comment2\n");
00833 
00834     // Call getNextHeaderLine - should return the comment.
00835     assert(samHeader.getNextHeaderLine(hdrline));
00836     assert(hdrline == "@CO\tMy Comment2\n");
00837     assert(!samHeader.getNextHeaderLine(hdrline));
00838     assert(hdrline == "");
00839     assert(!samHeader.getNextHeaderLine(hdrline));
00840     assert(hdrline == "");
00841 
00842     // Call getNextCommentLine - should return the comment.
00843     assert(strcmp(samHeader.getNextComment(), "My Comment2") == 0);
00844     assert(strcmp(samHeader.getNextComment(), "") == 0);
00845     assert(strcmp(samHeader.getNextComment(), "") == 0);
00846 
00847     // Reset the header record iter.
00848     samHeader.resetHeaderRecordIter();
00849    
00850     // Recall getNextCommentLine - should not return anything.
00851     assert(strcmp(samHeader.getNextComment(), "") == 0);
00852     assert(strcmp(samHeader.getNextComment(), "") == 0);   
00853 
00854     // Reset the next comment iter.
00855     samHeader.resetCommentIter();
00856 
00857     // Call the get next headerLine, record, comment interspersed with
00858     // each other.
00859     hdrRec = samHeader.getNextHeaderRecord();
00860     assert(hdrRec != NULL);
00861     assert(strcmp(hdrRec->getTypeString(), "HD") == 0);
00862     assert(hdrRec->getType() == SamHeaderRecord::HD);
00863     assert(strcmp(hdrRec->getTagValue("SO"), "queryname") == 0);
00864     assert(strcmp(hdrRec->getTagValue("VN"), "3.1") == 0);
00865     assert(samHeader.getNextHeaderLine(hdrline));
00866     assert(hdrline == "@PG\tID:newID\n");
00867     assert(samHeader.getNextHeaderLine(hdrline)); 
00868     assert(hdrline == "@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n");   
00869     hdrRec = samHeader.getNextHeaderRecord();
00870     assert(hdrRec != NULL);
00871     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00872     assert(hdrRec->getType() == SamHeaderRecord::RG);
00873     assert(strcmp(hdrRec->getTagValue("ID"), "rgID") == 0);
00874     hdrRec = samHeader.getNextHeaderRecord();
00875     assert(hdrRec != NULL);
00876     assert(strcmp(samHeader.getNextComment(), "My Comment") == 0);
00877     assert(strcmp(hdrRec->getTypeString(), "RG") == 0);
00878     assert(hdrRec->getType() == SamHeaderRecord::RG);
00879     assert(strcmp(hdrRec->getTagValue("ID"), "rgID1") == 0);
00880     assert(strcmp(hdrRec->getTagValue("SM"), "sample1") == 0);
00881     assert(samHeader.getNextHeaderLine(hdrline));
00882     assert(hdrline == "@RG\tID:rgID2\tSM:sm2\n");
00883     hdrRec = samHeader.getNextHeaderRecord();
00884     assert(hdrRec == NULL);
00885     assert(samHeader.getNextHeaderLine(hdrline));
00886     assert(hdrline == "@CO\tMy Comment\n");
00887     hdrRec = samHeader.getNextHeaderRecord();
00888     assert(hdrRec == NULL);
00889     assert(samHeader.getNextHeaderLine(hdrline));
00890     assert(hdrline == "@CO\tMy Comment2\n");
00891     assert(!samHeader.getNextHeaderLine(hdrline));
00892     assert(hdrline == "");
00893     assert(strcmp(samHeader.getNextComment(), "My Comment2") == 0);
00894     assert(!samHeader.getNextHeaderLine(hdrline));
00895     assert(hdrline == "");
00896     hdrRec = samHeader.getNextHeaderRecord();
00897     assert(hdrRec == NULL);
00898     assert(strcmp(samHeader.getNextComment(), "") == 0);
00899     assert(strcmp(samHeader.getNextComment(), "") == 0);
00900 
00901     samOut.WriteHeader(samHeader);
00902 
00903     // Reset the header.
00904     samHeader.resetHeader();
00905     assert(samHeader.getHeaderString(headerString) == true);
00906     assert(headerString == "");
00907     assert(!samHeader.getNextHeaderLine(hdrline)); 
00908     assert(hdrline == "");
00909     assert(strcmp(samHeader.getHDTagValue("SO"), "") == 0);
00910     assert(strcmp(samHeader.getHDTagValue("VN"), "") == 0);
00911 
00912     // Try adding a key to the HD tag.
00913     hd = new SamHeaderHD();
00914     assert(hd->addKey("3.1") == false);
00915     assert(strcmp(hd->getTagValue("VN"), "") == 0);
00916     assert(hd->isActiveHeaderRecord() == false);
00917 
00918     assert(hd->setTag("VN", "3.1") == true);
00919     assert(hd->isActiveHeaderRecord() == true);
00920     assert(strcmp(hd->getTagValue("VN"), "3.1") == 0);
00921 
00922     // Verify the copied header did not change.
00923     assert(newHeader.getHeaderString(headerString) == true);
00924     assert(headerString ==
00925            "@HD\tSO:queryname\tVN:3.1\n@PG\tID:newID\n@SQ\tSN:newName\tLN:111\tAS:HG18\tSP:species\n@RG\tID:rgID\tSM:sm1\n@RG\tID:rgID1\tSM:sample1\n@RG\tID:rgID2\tSM:sm2\n@CO\tMy Comment\n@CO\tMy Comment2\n");
00926     // Verify it was added to the parsed header.
00927     assert(strcmp(newHeader.getSQTagValue("LN", "chr20"), "") == 0);
00928 }
00929 
00930 
00931 void testWriteCopiedHeader(const char* fileName)
00932 {
00933     SamFile samIn;
00934     assert(samIn.OpenForRead(fileName));
00935 
00936     SamFile samOut;
00937     assert(samOut.OpenForWrite("results/MyTestOut2.bam"));
00938     SamFile samOut2;
00939     assert(samOut2.OpenForWrite("results/MyTestOut2.sam"));
00940 
00941     // Read the sam header.
00942     SamFileHeader samHeader;
00943     assert(samIn.ReadHeader(samHeader));
00944     validateHeader(samHeader);
00945 
00946     SamFileHeader newHeader;
00947 
00948     std::string hdrLine;
00949     assert(samHeader.getNextHeaderLine(hdrLine));
00950     newHeader.addHeaderLine("@HD\tVN:1.02");
00951     bool hdrStatus = true;
00952     while(hdrStatus)
00953     {
00954         newHeader.addHeaderLine(hdrLine.c_str());
00955         hdrStatus = samHeader.getNextHeaderLine(hdrLine);
00956     }
00957 
00958     // Write the sam header.
00959     assert(samOut.WriteHeader(newHeader));
00960     assert(samOut2.WriteHeader(newHeader));
00961 
00962     SamRecord samRecord;
00963 
00964     // Keep reading records until ReadRecord returns false.
00965     while(samIn.ReadRecord(samHeader, samRecord))
00966     {
00967         // Successfully read a record from the file, so write it.
00968         assert(samOut.WriteRecord(newHeader, samRecord));
00969         assert(samOut2.WriteRecord(newHeader, samRecord));
00970     }
00971 
00972     assert(samIn.GetStatus() == SamStatus::NO_MORE_RECS);
00973 
00974     // Close the output files.
00975     samOut.Close();
00976     samOut2.Close();
00977 
00978     SamFileReader bamRead("results/MyTestOut2.bam");
00979     SamFileReader samRead("results/MyTestOut2.sam");
00980     
00981     // Read and check the header.
00982     assert(samRead.ReadHeader(samHeader));
00983     validateHeaderFields(samHeader);
00984     std::string headerString = "";
00985     assert(samHeader.getHeaderString(headerString) == true);
00986     assert(headerString == "@HD\tVN:1.02\n@SQ\tSN:1\tLN:247249719\n@SQ\tSN:2\tLN:242951149\n@SQ\tSN:3\tLN:199501827\n@SQ\tSN:4\tLN:191273063\n@SQ\tSN:5\tLN:180857866\n@SQ\tSN:6\tLN:170899992\n@SQ\tSN:7\tLN:158821424\n@SQ\tSN:8\tLN:146274826\n@SQ\tSN:9\tLN:140273252\n@SQ\tSN:10\tLN:135374737\n@SQ\tSN:11\tLN:134452384\n@SQ\tSN:12\tLN:132349534\n@SQ\tSN:13\tLN:114142980\n@SQ\tSN:14\tLN:106368585\n@SQ\tSN:15\tLN:100338915\n@SQ\tSN:16\tLN:88827254\n@SQ\tSN:17\tLN:78774742\n@SQ\tSN:18\tLN:76117153\n@SQ\tSN:19\tLN:63811651\n@SQ\tSN:20\tLN:62435964\n@SQ\tSN:21\tLN:46944323\n@SQ\tSN:22\tLN:49691432\n@SQ\tSN:X\tLN:154913754\n@RG\tID:myID\tLB:library\tSM:sample\n@RG\tID:myID2\tSM:sample2\tLB:library2\n@CO\tComment 1\n@CO\tComment 2\n");
00987     
00988     assert(bamRead.ReadHeader(samHeader));
00989     validateHeaderFields(samHeader);
00990     headerString = "";
00991     assert(samHeader.getHeaderString(headerString) == true);
00992     assert(headerString == "@HD\tVN:1.02\n@SQ\tSN:1\tLN:247249719\n@SQ\tSN:2\tLN:242951149\n@SQ\tSN:3\tLN:199501827\n@SQ\tSN:4\tLN:191273063\n@SQ\tSN:5\tLN:180857866\n@SQ\tSN:6\tLN:170899992\n@SQ\tSN:7\tLN:158821424\n@SQ\tSN:8\tLN:146274826\n@SQ\tSN:9\tLN:140273252\n@SQ\tSN:10\tLN:135374737\n@SQ\tSN:11\tLN:134452384\n@SQ\tSN:12\tLN:132349534\n@SQ\tSN:13\tLN:114142980\n@SQ\tSN:14\tLN:106368585\n@SQ\tSN:15\tLN:100338915\n@SQ\tSN:16\tLN:88827254\n@SQ\tSN:17\tLN:78774742\n@SQ\tSN:18\tLN:76117153\n@SQ\tSN:19\tLN:63811651\n@SQ\tSN:20\tLN:62435964\n@SQ\tSN:21\tLN:46944323\n@SQ\tSN:22\tLN:49691432\n@SQ\tSN:X\tLN:154913754\n@RG\tID:myID\tLB:library\tSM:sample\n@RG\tID:myID2\tSM:sample2\tLB:library2\n@CO\tComment 1\n@CO\tComment 2\n");
00993     
00994     assert(samHeader.getNextHeaderLine(hdrLine));
00995     std::string expectedString = "@HD\tVN:1.02\n";
00996 
00997     assert(expectedString == hdrLine);
00998     
00999 
01000     // TODO - validate reading these written records back in.
01001 
01002 }
01003 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends