libStatGen Software 1
Modify.cpp
1/*
2 * Copyright (C) 2010 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 "SamFile.h"
19#include "SamFlag.h"
20#include "Modify.h"
21
22void testModify()
23{
24 modify modTest;
25 modTest.testModify("testFiles/testSam.sam");
26#ifdef __ZLIB_AVAILABLE__
27 modTest.testModify("testFiles/testBam.bam");
28#endif
29}
30
31
32void modify::testModify(const char* filename)
33{
34 myFilename = filename;
35
36 modifyPosition();
37 modifyCigar();
38
39 modifyFlag();
40
41 modifyTags();
42}
43
44void modify::modifyPosition()
45{
46 openAndRead1Rec();
47
48 // Verify the initial bin.
49 assert(samRecord.getBin() == 4681);
50
51 // Change the position and verify that the bin is updated.
52 assert(samRecord.set0BasedPosition(33768));
53
54 // Verify the bin was updated.
55 assert(samRecord.getBin() == 4683);
56 assert(samRecord.get0BasedPosition() == 33768);
57}
58
59
60void modify::modifyCigar()
61{
62 openAndRead1Rec();
63
64 // Verify the initial bin.
65 assert(samRecord.getBin() == 4681);
66
67 // Change the Cigar such that it modifies the bin.
68 assert(samRecord.setCigar("33768M"));
69
70 // Verify the bin was updated.
71 assert(samRecord.getBin() == 585);
72}
73
74
75void modify::modifyFlag()
76{
77 openAndRead1Rec();
78
79 // Verify the initial bin.
80 uint16_t flag = 73;
81 assert(samRecord.getFlag() == flag);
82
84 assert(flag == 1097);
85 assert(samRecord.setFlag(flag));
86 assert(samRecord.getFlag() == 1097);
87
89 assert(flag == 73);
90 assert(samRecord.setFlag(flag));
91 assert(samRecord.getFlag() == 73);
92}
93
94
95void modify::openAndRead1Rec()
96{
97 // Open the file for reading.
98 assert(samIn.OpenForRead(myFilename.c_str()));
99
100 // Read the sam header.
101 assert(samIn.ReadHeader(samHeader));
102
103 // Read the first record.
104 assert(samIn.ReadRecord(samHeader, samRecord));
105}
106
107
108void modify::modifyTags()
109{
110 assert(samIn.OpenForRead(myFilename.c_str()));
111 // Read the sam header.
112 assert(samIn.ReadHeader(samHeader));
113
114 SamFile samOut;
115 SamFile bamOut;
116
117 std::string inputType = myFilename.substr(myFilename.find_last_of('.'));
118 std::string outFileBase = "results/updateTagFrom";
119 if(inputType == ".bam")
120 {
121 outFileBase += "Bam";
122 }
123 else
124 {
125 outFileBase += "Sam";
126 }
127
128 std::string outFile = outFileBase + ".sam";
129 assert(samOut.OpenForWrite(outFile.c_str()));
130 outFile = outFileBase + ".bam";
131 assert(bamOut.OpenForWrite(outFile.c_str()));
132 assert(samOut.WriteHeader(samHeader));
133 assert(bamOut.WriteHeader(samHeader));
134
135 int count = 0;
136 // Read the records.
137 while(samIn.ReadRecord(samHeader, samRecord))
138 {
139 if(count == 0)
140 {
141 assert(samRecord.rmTag("MD", 'Z'));
142 }
143 else if(count == 2)
144 {
145 assert(samRecord.rmTags("XT:A;MD:Z;AB:c;NM:i"));
146 }
147 else if(count == 4)
148 {
149 assert(samRecord.rmTags("MD:Z,AB:c,NM:i"));
150 }
151
152 assert(bamOut.WriteRecord(samHeader, samRecord));
153 assert(samOut.WriteRecord(samHeader, samRecord));
154 ++count;
155 }
156}
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:36
bool ReadHeader(SamFileHeader &header)
Reads the header section from the file and stores it in the passed in header.
Definition: SamFile.cpp:450
bool ReadRecord(SamFileHeader &header, SamRecord &record)
Reads the next record from the file & stores it in the passed in record.
Definition: SamFile.cpp:514
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
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
bool WriteHeader(SamFileHeader &header)
Writes the specified header into the file.
Definition: SamFile.cpp:480
bool WriteRecord(SamFileHeader &header, SamRecord &record)
Writes the specified record into the file.
Definition: SamFile.cpp:632
static void setNotDuplicate(uint16_t &flag)
Mark the passed in flag as not duplicate.
Definition: SamFlag.h:106
static void setDuplicate(uint16_t &flag)
Mark the passed in flag as not duplicate.
Definition: SamFlag.h:108
bool rmTag(const char *tag, char type)
Remove a tag.
Definition: SamRecord.cpp:992
uint16_t getBin()
Get the BAM bin for the record.
Definition: SamRecord.cpp:1347
bool setFlag(uint16_t flag)
Set the bitwise FLAG to the specified value.
Definition: SamRecord.cpp:215
uint16_t getFlag()
Get the flag (FLAG).
Definition: SamRecord.cpp:1384
bool setCigar(const char *cigar)
Set the CIGAR to the specified SAM formatted cigar string.
Definition: SamRecord.cpp:259
int32_t get0BasedPosition()
Get the 0-based(BAM) leftmost position of the record.
Definition: SamRecord.cpp:1319
bool set0BasedPosition(int32_t position)
Set the leftmost position using the specified 0-based (BAM format) value.
Definition: SamRecord.cpp:242
bool rmTags(const char *tags)
Remove tags.
Definition: SamRecord.cpp:1083
Definition: Modify.h:21