libStatGen Software  1
TestSamCoordOutput.cpp
1 /*
2  * Copyright (C) 2011 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "TestSamCoordOutput.h"
19 #include "TestValidate.h"
20 #include "SamCoordOutput.h"
21 #include "SamRecordPool.h"
22 #include <assert.h>
23 
24 void testSamCoordOutput()
25 {
26  // Call generic test.
27  SamCoordOutputTest::testSamCoordOutput();
28 }
29 
30 
31 void SamCoordOutputTest::testSamCoordOutput()
32 {
33  SamRecordPool pool(3);
34 
35  SamCoordOutput outputBuffer(pool);
36 
37  SamFile inSam;
38  SamFile outSam;
39  SamFileHeader samHeader;
40  SamRecord* rec1 = NULL;
41  SamRecord* rec2 = NULL;
42  SamRecord* rec3 = NULL;
43 
44  // Open input file and read the header.
45 #ifdef __ZLIB_AVAILABLE__
46  assert(inSam.OpenForRead("testFiles/testBam.bam"));
47 #else
48  assert(inSam.OpenForRead("testFiles/testSam.sam"));
49 #endif
50  assert(inSam.ReadHeader(samHeader));
51  validateHeader(samHeader);
52 
53  // Check failed to add empty record.
54  assert(!outputBuffer.add(rec1));
55 
56  // Read the first 3 records from the input file.
57  rec1 = pool.getRecord();
58  assert(inSam.ReadRecord(samHeader, *rec1) == true);
59  validateRead1(*rec1);
60  rec2 = pool.getRecord();
61  assert(inSam.ReadRecord(samHeader, *rec2) == true);
62  validateRead2(*rec2);
63  rec3 = pool.getRecord();
64  assert(inSam.ReadRecord(samHeader, *rec3) == true);
65  validateRead3(*rec3);
66  assert(pool.getRecord() == NULL);
67 
68  // Add the first 3 records to the output buffer.
69  // Sorted order is rec 3, 1, 2
70  assert(outputBuffer.add(rec1));
71  assert(outputBuffer.add(rec2));
72  assert(outputBuffer.add(rec3));
73 
74  // Test writing to the output buffer without having set it.
75  // Should flush just rec3 out.
76  assert(!outputBuffer.flush(0, 100));
77 
78  // Set the output buffer.
79  outputBuffer.setOutputFile(&outSam, &samHeader);
80 
81  // Open output file and write the header.
82  assert(outSam.OpenForWrite("results/TestSamCoordOutput.sam"));
83  assert(outSam.WriteHeader(samHeader));
84 
85  // Read another 1 record (reuse record pointers).
86  rec1 = pool.getRecord();
87  assert(inSam.ReadRecord(samHeader, *rec1) == true);
88  validateRead4(*rec1);
89  assert(outputBuffer.add(rec1));
90 
91  rec1 = pool.getRecord();
92  assert(rec1 == NULL);
93 
94 
95  // Flush out just the reads before this position.
96  assert(outputBuffer.flush(0, 1011));
97 
98  // Read 2 more records.
99  rec1 = pool.getRecord();
100  assert(inSam.ReadRecord(samHeader, *rec1) == true);
101  validateRead5(*rec1);
102  assert(outputBuffer.add(rec1));
103 
104  rec1 = pool.getRecord();
105  assert(inSam.ReadRecord(samHeader, *rec1) == true);
106  validateRead6(*rec1);
107  assert(outputBuffer.add(rec1));
108 
109  // Can get another record (tests to make sure flushes up to and
110  // including the specified position). If it did not
111  // flush the specified position, there would not be
112  // another record available.
113  rec1 = pool.getRecord();
114  assert(rec1 != NULL);
115 
116  // Flush out just the reads before this position.
117  assert(outputBuffer.flush(0, 1012));
118 
119  // Read another record.
120  assert(inSam.ReadRecord(samHeader, *rec1) == true);
121  validateRead7(*rec1);
122  assert(outputBuffer.add(rec1));
123  assert(pool.getRecord() == NULL);
124 
125  // Flush out just the reads on chrom 1 (chrom id 0).
126  assert(outputBuffer.flush(0, -1));
127 
128  // Read another record.
129  rec1 = pool.getRecord();
130  assert(inSam.ReadRecord(samHeader, *rec1) == true);
131  validateRead8(*rec1);
132  assert(outputBuffer.add(rec1));
133  assert(pool.getRecord() == NULL);
134 
135  // Flush out the chrom 2 (chrom id 1) reads.
136  assert(outputBuffer.flush(2, 0));
137 
138  // Read the rest of the records.
139  rec1 = pool.getRecord();
140  assert(inSam.ReadRecord(samHeader, *rec1) == true);
141  validateRead9(*rec1);
142  assert(outputBuffer.add(rec1));
143  rec1 = pool.getRecord();
144  assert(inSam.ReadRecord(samHeader, *rec1) == true);
145  validateRead10(*rec1);
146  assert(outputBuffer.add(rec1));
147  assert(pool.getRecord() == NULL);
148 
149  // Flush the rest by passing in -1, -1
150  assert(outputBuffer.flush(-1, -1));
151 }
SamFile::ReadRecord
bool ReadRecord(SamFileHeader &header, SamRecord &record)
Reads the next record from the file & stores it in the passed in record.
Definition: SamFile.cpp:501
SamCoordOutput
Class for buffering up output reads to ensure that it is sorted.
Definition: SamCoordOutput.h:28
SamFile::OpenForWrite
bool OpenForWrite(const char *filename, SamFileHeader *header=NULL)
Open a sam/bam file for writing with the specified filename, determining SAM/BAM from the extension (...
Definition: SamFile.cpp:223
SamFileHeader
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:35
SamRecord
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition: SamRecord.h:52
SamFile::OpenForRead
bool OpenForRead(const char *filename, SamFileHeader *header=NULL)
Open a sam/bam file for reading with the specified filename, determing the type of file and SAM/BAM b...
Definition: SamFile.cpp:93
SamFile::WriteHeader
bool WriteHeader(SamFileHeader &header)
Writes the specified header into the file.
Definition: SamFile.cpp:467
SamFile::ReadHeader
bool ReadHeader(SamFileHeader &header)
Reads the header section from the file and stores it in the passed in header.
Definition: SamFile.cpp:437
SamFile
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:36
SamRecordPool
Definition: SamRecordPool.h:26