Beispiel #1
0
void testRead(SamFile &inSam)
{
    // Read the SAM Header.
    SamFileHeader samHeader;
    assert(inSam.ReadHeader(samHeader));

    validateHeader(samHeader);

    testCopyHeader(samHeader);    

    testModHeader(samHeader);

    SamRecord samRecord;
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    // Set a new quality and get the buffer.
    samRecord.setQuality("ABCDE");
    validateRead1ModQuality(samRecord);
    //   void* buffer = samRecord.getRecordBuffer();

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead3(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead4(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead5(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead6(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead7(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead9(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);
}
Beispiel #2
0
void testIndex(BamIndex& bamIndex)
{
    assert(bamIndex.getNumMappedReads(1) == 2);
    assert(bamIndex.getNumUnMappedReads(1) == 0);
    assert(bamIndex.getNumMappedReads(0) == 4);
    assert(bamIndex.getNumUnMappedReads(0) == 1);
    assert(bamIndex.getNumMappedReads(23) == -1);
    assert(bamIndex.getNumUnMappedReads(23) == -1);
    assert(bamIndex.getNumMappedReads(-1) == 0);
    assert(bamIndex.getNumUnMappedReads(-1) == 2);
    assert(bamIndex.getNumMappedReads(-2) == -1);
    assert(bamIndex.getNumUnMappedReads(-2) == -1);
    assert(bamIndex.getNumMappedReads(22) == 0);
    assert(bamIndex.getNumUnMappedReads(22) == 0);

    // Get the chunks for reference id 1.
    Chunk testChunk;
    SortedChunkList chunkList;
    assert(bamIndex.getChunksForRegion(1, -1, -1, chunkList) == true);
    assert(!chunkList.empty());
    testChunk = chunkList.pop();
    assert(chunkList.empty());
    assert(testChunk.chunk_beg == 0x4e7);
    assert(testChunk.chunk_end == 0x599);

    // Get the chunks for reference id 0.
    assert(bamIndex.getChunksForRegion(0, -1, -1, chunkList) == true);
    assert(!chunkList.empty());
    testChunk = chunkList.pop();
    assert(chunkList.empty());
    assert(testChunk.chunk_beg == 0x360);
    assert(testChunk.chunk_end == 0x4e7);


    // Get the chunks for reference id 2.
    assert(bamIndex.getChunksForRegion(2, -1, -1, chunkList) == true);
    assert(!chunkList.empty());
    testChunk = chunkList.pop();
    assert(chunkList.empty());
    assert(testChunk.chunk_beg == 0x599);
    assert(testChunk.chunk_end == 0x5ea);

    // Get the chunks for reference id 3.
    // There isn't one for this ref id, but still successfully read the file,
    // so it should return true, but the list should be empty.
    assert(bamIndex.getChunksForRegion(3, -1, -1, chunkList) == true);
    assert(chunkList.empty());

    // Test reading an indexed bam file.
    SamFile inFile;
    assert(inFile.OpenForRead("testFiles/sortedBam.bam"));
    inFile.setSortedValidation(SamFile::COORDINATE);
    assert(inFile.ReadBamIndex("testFiles/sortedBam.bam.bai"));
    SamFileHeader samHeader;
    assert(inFile.ReadHeader(samHeader));
    SamRecord samRecord;

    // Test getting num mapped/unmapped reads.
    assert(inFile.getNumMappedReadsFromIndex(1) == 2);
    assert(inFile.getNumUnMappedReadsFromIndex(1) == 0);
    assert(inFile.getNumMappedReadsFromIndex(0) == 4);
    assert(inFile.getNumUnMappedReadsFromIndex(0) == 1);
    assert(inFile.getNumMappedReadsFromIndex(23) == -1);
    assert(inFile.getNumUnMappedReadsFromIndex(23) == -1);
    assert(inFile.getNumMappedReadsFromIndex(-1) == 0);
    assert(inFile.getNumUnMappedReadsFromIndex(-1) == 2);
    assert(inFile.getNumMappedReadsFromIndex(-2) == -1);
    assert(inFile.getNumUnMappedReadsFromIndex(-2) == -1);
    assert(inFile.getNumMappedReadsFromIndex(22) == 0);
    assert(inFile.getNumUnMappedReadsFromIndex(22) == 0);

    assert(inFile.getNumMappedReadsFromIndex("2", samHeader) == 2);
    assert(inFile.getNumUnMappedReadsFromIndex("2", samHeader) == 0);
    assert(inFile.getNumMappedReadsFromIndex("1", samHeader) == 4);
    assert(inFile.getNumUnMappedReadsFromIndex("1", samHeader) == 1);
    assert(inFile.getNumMappedReadsFromIndex("22", samHeader) == 0);
    assert(inFile.getNumUnMappedReadsFromIndex("22", samHeader) == 0);
    assert(inFile.getNumMappedReadsFromIndex("", samHeader) == 0);
    assert(inFile.getNumUnMappedReadsFromIndex("*", samHeader) == 2);
    assert(inFile.getNumMappedReadsFromIndex("unknown", samHeader) == -1);
    assert(inFile.getNumUnMappedReadsFromIndex("unknown", samHeader) == -1);
    assert(inFile.getNumMappedReadsFromIndex("X", samHeader) == 0);
    assert(inFile.getNumUnMappedReadsFromIndex("X", samHeader) == 0);

    // Section -1 = Ref *: 2 records (8 & 10 from testSam.sam that is reflected
    // in the validation.
    assert(inFile.SetReadSection(-1));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead8(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead10(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);

    // Section 2 = Ref 3: 1 records (9 from testSam.sam that is reflected
    // in the validation.
    assert(inFile.SetReadSection(2));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead9(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);

    // Section 0 = Ref 1: 5 records (3, 4, 1, 2, & 6 from testSam.sam that is
    // reflected in the validation.
    assert(inFile.SetReadSection(0));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead3(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead4(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead1(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead2(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead6(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);

    // Section 1 = Ref 2: 2 records (5 & 7 from testSam.sam that is reflected
    // in the validation.
    assert(inFile.SetReadSection(1));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead5(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead7(samRecord);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);

    // Section 3 to 22 (ref 4 - 23): 0 records.
    for(int i = 3; i < 23; i++)
    {
        assert(inFile.SetReadSection(i));
        assert(inFile.ReadRecord(samHeader, samRecord) == false);
    }


    // Set the read section.
    assert(inFile.SetReadSection("1", 1010, 1012));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead1(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 2);
    assert(samRecord.getNumOverlaps(1010, 1012) == 2);
    assert(samRecord.getNumOverlaps(1010, 1020) == 5);
    assert(samRecord.getNumOverlaps(1010, 1011) == 1);
    assert(samRecord.getNumOverlaps(1011, 1012) == 1);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead2(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 0);
    assert(samRecord.getNumOverlaps(1010, 1012) == 0);
    assert(samRecord.getNumOverlaps(1010, 1020) == 0);
    assert(samRecord.getNumOverlaps(1010, 1011) == 0);
    assert(samRecord.getNumOverlaps(1011, 1012) == 0);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);
           
    assert(inFile.SetReadSection("1", 1010, 1020));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead1(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 5);
    assert(samRecord.getNumOverlaps(1010, 1012) == 2);
    assert(samRecord.getNumOverlaps(1010, 1020) == 5);
    assert(samRecord.getNumOverlaps(1010, 1011) == 1);
    assert(samRecord.getNumOverlaps(1011, 1012) == 1);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead2(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 0);
    assert(samRecord.getNumOverlaps(1010, 1012) == 0);
    assert(samRecord.getNumOverlaps(1010, 1020) == 0);
    assert(samRecord.getNumOverlaps(1010, 1011) == 0);
    assert(samRecord.getNumOverlaps(1011, 1012) == 0);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);
           
    assert(inFile.SetReadSection("1", 1010, 1011));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead1(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 1);
    assert(samRecord.getNumOverlaps(1010, 1012) == 2);
    assert(samRecord.getNumOverlaps(1010, 1020) == 5);
    assert(samRecord.getNumOverlaps(1010, 1011) == 1);
    assert(samRecord.getNumOverlaps(1011, 1012) == 1);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);
           
    assert(inFile.SetReadSection("1", 1011, 1012));
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead1(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 1);
    assert(samRecord.getNumOverlaps(1010, 1012) == 2);
    assert(samRecord.getNumOverlaps(1010, 1020) == 5);
    assert(samRecord.getNumOverlaps(1010, 1011) == 1);
    assert(samRecord.getNumOverlaps(1011, 1012) == 1);
    assert(inFile.ReadRecord(samHeader, samRecord));
    validateRead2(samRecord);
    assert(inFile.GetNumOverlaps(samRecord) == 0);
    assert(samRecord.getNumOverlaps(1010, 1012) == 0);
    assert(samRecord.getNumOverlaps(1010, 1020) == 0);
    assert(samRecord.getNumOverlaps(1010, 1011) == 0);
    assert(samRecord.getNumOverlaps(1011, 1012) == 0);
    assert(inFile.ReadRecord(samHeader, samRecord) == false);
           
}
Beispiel #3
0
// Test reading a file, validating it is sorted.
void testValidateSortedRead()
{
    // Open a file for reading.
    SamFile inSam(ErrorHandler::RETURN);
    assert(inSam.OpenForRead("testFiles/testSam.sam"));

    // Set the validation to COORDINATE.
    inSam.setSortedValidation(SamFile::COORDINATE);

    // Read the SAM Header.
    SamFileHeader samHeader;
    assert(inSam.ReadHeader(samHeader));
    
    SamRecord samRecord;
    // Succeed, first record.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    // Succeed, higher coordinate.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    // Failed sort order - due to coord.
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead3(samRecord);

    // Failed sort order - due to coord.
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead4(samRecord);

    // Succeed, new reference id
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead5(samRecord);

    // Fail, previous reference id.
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead6(samRecord);

    // Succeed, same reference id, higher coord.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead7(samRecord);

    // Succeed, *, new reference id.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    // Fail, reference id is not *
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead9(samRecord);

    // Succeed, valid reference id, and no coordinate.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);


    ////////////////////////////////////////////
    // Reopen the file for reading
    assert(inSam.OpenForRead("testFiles/testSam.sam"));

    // Set the validation to QUERY_NAME.
    inSam.setSortedValidation(SamFile::QUERY_NAME);

    // Read the SAM Header.
    assert(inSam.ReadHeader(samHeader));
   
    // Succeed, first record.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    // Succeed, same name.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead3(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead4(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead5(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead6(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead7(samRecord);

    // Succeed
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead9(samRecord);

    // Succeed
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);

    ////////////////////////////////////////////
    // Reopen the file for reading
    assert(inSam.OpenForRead("testFiles/testSam.sam"));

    // Set the validation to the SO Flag.  Not set, so it is UNSORTED, so 
    // all reads should pass.
    inSam.setSortedValidation(SamFile::FLAG);

    // Read the SAM Header.
    assert(inSam.ReadHeader(samHeader));
   
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead3(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead4(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead5(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead6(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead7(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead9(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);

    ////////////////////////////////////////////
    // Reopen for reading SO FLAG set to coordinate.
    assert(inSam.OpenForRead("testFiles/testSamSOcoord.sam"));

    // Set the validation to SO FLAG which is set to coordinate.
    inSam.setSortedValidation(SamFile::FLAG);

    // Read the SAM Header.
    assert(inSam.ReadHeader(samHeader));

    // Succeed, first record.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    // Succeed, higher coordinate.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    // Failed sort order - due to coord.
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead3(samRecord);

    // Failed sort order - due to coord.
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead4(samRecord);

    // Succeed, new reference id
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead5(samRecord);

    // Fail, previous reference id.
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead6(samRecord);

    // Succeed, same reference id, higher coord.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead7(samRecord);

    // Succeed, *, new reference id.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    // Fail, reference id is not *
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead9(samRecord);

    // Succeed, valid reference id, and no coordinate.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);


    ////////////////////////////////////////////
    // Reopen the file for reading
    assert(inSam.OpenForRead("testFiles/testSamSOquery.sam"));

    // Set the validation to FLAG, SO set to queryname.
    inSam.setSortedValidation(SamFile::FLAG);

    // Read the SAM Header.
    assert(inSam.ReadHeader(samHeader));
   
    // Succeed, first record.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    // Succeed, same name.
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead3(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead4(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead5(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead6(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead7(samRecord);

    // Succeed
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    // Failed sort order - due to alphabetical
    assert(inSam.ReadRecord(samHeader, samRecord) == false);
    validateRead9(samRecord);

    // Succeed
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);

    ////////////////////////////////////////////
    // Reopen the file for reading, SO flag set to junk.
    assert(inSam.OpenForRead("testFiles/testSamSOinvalid.sam"));

    // Set the validation to the SO Flag.  Not set to anything valid,
    // so it is considered UNSORTED, so all reads should pass.
    inSam.setSortedValidation(SamFile::FLAG);

    // Read the SAM Header.
    assert(inSam.ReadHeader(samHeader));
   
    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead1(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead2(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead3(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead4(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead5(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead6(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead7(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead8(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead9(samRecord);

    assert(inSam.ReadRecord(samHeader, samRecord) == true);
    validateRead10(samRecord);
}