libStatGen Software  1
TestFilter.cpp
00001 /*
00002  *  Copyright (C) 2011  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 "TestFilter.h"
00019 #include "TestValidate.h"
00020 #include "SamFilter.h"
00021 #include <assert.h>
00022 
00023 void testFilter()
00024 {
00025     // Call generic test which since the sam and bam are identical, should
00026     // contain the same results.
00027     FilterTest::testFilter(FilterTest::SAM);
00028 #ifdef __ZLIB_AVAILABLE__
00029     FilterTest::testFilter(FilterTest::BAM);
00030 #endif
00031 }
00032 
00033 
00034 void FilterTest::testFilter(FileType inputType)
00035 {
00036     SamFile inSam;
00037 
00038     if(inputType == SAM)
00039     {
00040         assert(inSam.OpenForRead("testFiles/testSam.sam"));
00041     }
00042     else
00043     {
00044         assert(inSam.OpenForRead("testFiles/testBam.bam"));
00045     }
00046 
00047    // Read the SAM Header.
00048     SamFileHeader samHeader;
00049     assert(inSam.ReadHeader(samHeader));
00050     validateHeader(samHeader);
00051 
00052     SamRecord samRecord;
00053     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00054     validateRead1(samRecord);
00055 
00056     // Clip the read, 2 from the front and 2 from the back, which causes 2D to
00057     // be dropped.
00058     assert(SamFilter::softClip(samRecord, 2, 2) == SamFilter::CLIPPED);
00059     assert(samRecord.get0BasedPosition() == TestValidate::READ1_POS + 2);
00060     std::string expectedCigar = "2S1M2S";
00061     assert(samRecord.getCigar() == expectedCigar);
00062     assert(samRecord.getSequence() == TestValidate::READ1_SEQ);
00063     assert(samRecord.getQuality() == TestValidate::READ1_QUAL);
00064     // Only 1 base, so the end is the same as start
00065     assert(samRecord.get0BasedAlignmentEnd() == TestValidate::READ1_POS + 2);
00066     assert(samRecord.getAlignmentLength() == 1);
00067     assert(samRecord.get0BasedUnclippedStart() == TestValidate::READ1_UNCLIP_START);
00068     // The new unclipped end is not the same as the original end because the
00069     // 2 deletions are lost.
00070     assert(samRecord.get0BasedUnclippedEnd() == TestValidate::READ1_UNCLIP_END - 2);
00071 
00072 
00073     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00074     validateRead2(samRecord);
00075 
00076     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00077     validateRead3(samRecord);
00078 
00079     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00080     validateRead4(samRecord);
00081 
00082     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00083     validateRead5(samRecord);
00084 
00085     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00086     validateRead6(samRecord);
00087 
00088     // Clip the read 2 more from the front and 2 from the back.
00089     assert(SamFilter::softClip(samRecord, 5, 2) == SamFilter::CLIPPED);
00090     assert(samRecord.get0BasedPosition() == TestValidate::READ6_POS + 2);
00091     expectedCigar = "2H5S1M2S";
00092     assert(samRecord.getCigar() == expectedCigar);
00093     assert(samRecord.getSequence() == TestValidate::READ6_SEQ);
00094     assert(samRecord.getQuality() == TestValidate::READ6_QUAL);
00095     // Only 1 base, so the end is the same as start
00096     assert(samRecord.get0BasedAlignmentEnd() == TestValidate::READ6_POS + 2);
00097     assert(samRecord.getAlignmentLength() == 1);
00098     assert(samRecord.get0BasedUnclippedStart() == TestValidate::READ6_UNCLIP_START);
00099     assert(samRecord.get0BasedUnclippedEnd() == TestValidate::READ6_UNCLIP_END);
00100 
00101     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00102     validateRead7(samRecord);
00103 
00104     // Clip the read 2 more from the front and 2 morefrom the back.
00105     assert(SamFilter::softClip(samRecord, 5, 3) == SamFilter::CLIPPED);
00106     assert(samRecord.get0BasedPosition() == TestValidate::READ7_POS + 2);
00107     expectedCigar = "5S1M3S3H";
00108     assert(samRecord.getCigar() == expectedCigar);
00109     assert(samRecord.getSequence() == TestValidate::READ7_SEQ);
00110     assert(samRecord.getQuality() == TestValidate::READ7_QUAL);
00111     // Only 1 base, so the end is the same as start
00112     assert(samRecord.get0BasedAlignmentEnd() == TestValidate::READ7_POS + 2);
00113     assert(samRecord.getAlignmentLength() == 1);
00114     assert(samRecord.get0BasedUnclippedStart() == TestValidate::READ7_UNCLIP_START);
00115     assert(samRecord.get0BasedUnclippedEnd() == TestValidate::READ7_UNCLIP_END);
00116 
00117     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00118     validateRead8(samRecord);
00119 
00120     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00121     validateRead9(samRecord);
00122 
00123     assert(inSam.ReadRecord(samHeader, samRecord) == true);
00124     validateRead10(samRecord);
00125 }
00126 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends