libStatGen Software  1
TestSamCoordOutput.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 "TestSamCoordOutput.h"
00019 #include "TestValidate.h"
00020 #include "SamCoordOutput.h"
00021 #include "SamRecordPool.h"
00022 #include <assert.h>
00023 
00024 void testSamCoordOutput()
00025 {
00026     // Call generic test.
00027     SamCoordOutputTest::testSamCoordOutput();
00028 }
00029 
00030 
00031 void SamCoordOutputTest::testSamCoordOutput()
00032 {
00033     SamRecordPool pool(3);
00034 
00035     SamCoordOutput outputBuffer(pool);
00036 
00037     SamFile inSam;
00038     SamFile outSam;
00039     SamFileHeader samHeader;
00040     SamRecord* rec1 = NULL;
00041     SamRecord* rec2 = NULL;
00042     SamRecord* rec3 = NULL;
00043 
00044     // Open input file and read the header.
00045 #ifdef __ZLIB_AVAILABLE__
00046     assert(inSam.OpenForRead("testFiles/testBam.bam"));
00047 #else
00048     assert(inSam.OpenForRead("testFiles/testSam.sam"));
00049 #endif
00050     assert(inSam.ReadHeader(samHeader));
00051     validateHeader(samHeader);
00052 
00053     // Check failed to add empty record.
00054     assert(!outputBuffer.add(rec1));
00055 
00056     // Read the first 3 records from the input file.
00057     rec1 = pool.getRecord();
00058     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00059     validateRead1(*rec1);
00060     rec2 = pool.getRecord();
00061     assert(inSam.ReadRecord(samHeader, *rec2) == true);
00062     validateRead2(*rec2);
00063     rec3 = pool.getRecord();
00064     assert(inSam.ReadRecord(samHeader, *rec3) == true);
00065     validateRead3(*rec3);
00066     assert(pool.getRecord() == NULL);
00067 
00068     // Add the first 3 records to the output buffer.
00069     // Sorted order is rec 3, 1, 2
00070     assert(outputBuffer.add(rec1));
00071     assert(outputBuffer.add(rec2));
00072     assert(outputBuffer.add(rec3));
00073 
00074     // Test writing to the output buffer without having set it.
00075     // Should flush just rec3 out.
00076     assert(!outputBuffer.flush(0, 100));
00077     
00078     // Set the output buffer.
00079     outputBuffer.setOutputFile(&outSam, &samHeader);
00080 
00081     // Open output file and write the header.
00082     assert(outSam.OpenForWrite("results/TestSamCoordOutput.sam"));
00083     assert(outSam.WriteHeader(samHeader));
00084 
00085     // Read another 1 record (reuse record pointers).
00086     rec1 = pool.getRecord();
00087     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00088     validateRead4(*rec1);
00089     assert(outputBuffer.add(rec1));
00090 
00091     rec1 = pool.getRecord();
00092     assert(rec1 == NULL);
00093 
00094 
00095     // Flush out just the reads before this position.
00096     assert(outputBuffer.flush(0, 1011));
00097 
00098     // Read 2 more records.
00099     rec1 = pool.getRecord();
00100     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00101     validateRead5(*rec1);
00102     assert(outputBuffer.add(rec1));
00103 
00104     rec1 = pool.getRecord();
00105     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00106     validateRead6(*rec1);
00107     assert(outputBuffer.add(rec1));
00108 
00109     // Can get another record (tests to make sure flushes up to and
00110     // including the specified position).  If it did not
00111     // flush the specified position, there would not be
00112     // another record available.
00113     rec1 = pool.getRecord();
00114     assert(rec1 != NULL);
00115 
00116     // Flush out just the reads before this position.
00117     assert(outputBuffer.flush(0, 1012));
00118 
00119     // Read another record.
00120     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00121     validateRead7(*rec1);
00122     assert(outputBuffer.add(rec1));
00123     assert(pool.getRecord() == NULL);
00124 
00125     // Flush out just the reads on chrom 1 (chrom id 0).
00126     assert(outputBuffer.flush(0, -1));
00127 
00128     // Read another record.
00129     rec1 = pool.getRecord();
00130     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00131     validateRead8(*rec1);
00132     assert(outputBuffer.add(rec1));
00133     assert(pool.getRecord() == NULL);
00134 
00135     // Flush out the chrom 2 (chrom id 1) reads.
00136     assert(outputBuffer.flush(2, 0));
00137 
00138     // Read the rest of the records.
00139     rec1 = pool.getRecord();
00140     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00141     validateRead9(*rec1);
00142     assert(outputBuffer.add(rec1));
00143     rec1 = pool.getRecord();
00144     assert(inSam.ReadRecord(samHeader, *rec1) == true);
00145     validateRead10(*rec1);
00146     assert(outputBuffer.add(rec1));
00147     assert(pool.getRecord() == NULL);
00148 
00149     // Flush the rest by passing in -1, -1
00150     assert(outputBuffer.flush(-1, -1));
00151 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends