Modify.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 "Modify.h"
00020 
00021 void testModify()
00022 {
00023     modify modTest;
00024     modTest.testModify("testFiles/testSam.sam");
00025     modTest.testModify("testFiles/testBam.bam");
00026 
00027     
00028 }
00029 
00030 
00031 void modify::testModify(const char* filename)
00032 {
00033     myFilename = filename;
00034 
00035     modifyPosition();
00036     modifyCigar();
00037 
00038     modifyTags();
00039 }
00040 
00041 void modify::modifyPosition()
00042 {
00043     openAndRead1Rec();
00044    
00045     // Verify the initial bin.
00046     assert(samRecord.getBin() == 4681);
00047 
00048     // Change the position and verify that the bin is updated.
00049     assert(samRecord.set0BasedPosition(33768));
00050 
00051     // Verify the bin was updated.
00052     assert(samRecord.getBin() == 4683);
00053     assert(samRecord.get0BasedPosition() == 33768);
00054 }
00055 
00056 
00057 void modify::modifyCigar()
00058 {
00059     openAndRead1Rec();
00060    
00061     // Verify the initial bin.
00062     assert(samRecord.getBin() == 4681);
00063 
00064     // Change the Cigar such that it modifies the bin.
00065     assert(samRecord.setCigar("33768M"));
00066 
00067     // Verify the bin was updated.
00068     assert(samRecord.getBin() == 585);
00069 }
00070 
00071 
00072 void modify::openAndRead1Rec()
00073 {
00074     // Open the file for reading.   
00075     assert(samIn.OpenForRead(myFilename.c_str()));
00076 
00077     // Read the sam header.
00078     assert(samIn.ReadHeader(samHeader));
00079    
00080     // Read the first record.   
00081     assert(samIn.ReadRecord(samHeader, samRecord));
00082 }
00083 
00084 
00085 void modify::modifyTags()
00086 {
00087     assert(samIn.OpenForRead(myFilename.c_str()));
00088     // Read the sam header.
00089     assert(samIn.ReadHeader(samHeader));
00090    
00091     SamFile samOut;
00092     SamFile bamOut;
00093 
00094     std::string inputType = myFilename.substr(myFilename.find_last_of('.'));
00095     std::string outFileBase = "results/updateTagFrom";
00096     if(inputType == ".bam")
00097     {
00098         outFileBase += "Bam";
00099     }
00100     else
00101     {
00102         outFileBase += "Sam";
00103     }
00104 
00105     std::string outFile = outFileBase + ".sam";
00106     assert(samOut.OpenForWrite(outFile.c_str()));
00107     outFile = outFileBase + ".bam";
00108     assert(bamOut.OpenForWrite(outFile.c_str()));
00109     assert(samOut.WriteHeader(samHeader));
00110     assert(bamOut.WriteHeader(samHeader));
00111 
00112     int count = 0;
00113     // Read the records.
00114     while(samIn.ReadRecord(samHeader, samRecord))
00115     {
00116         if((count == 0) || (count == 4))
00117         {
00118             assert(samRecord.rmTag("MD", 'Z'));
00119         }
00120         else if(count == 2)
00121         {
00122             assert(samRecord.rmTags("XT:A;MD:Z;AB:c;NM:i"));
00123         }
00124 
00125         assert(bamOut.WriteRecord(samHeader, samRecord));
00126         assert(samOut.WriteRecord(samHeader, samRecord));
00127         ++count;
00128     }
00129     
00130 }
Generated on Tue Sep 6 17:52:00 2011 for libStatGen Software by  doxygen 1.6.3