-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbamaddrg.cpp
295 lines (240 loc) · 9.3 KB
/
bamaddrg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <map>
#include <vector>
#include <string>
#include <getopt.h>
#include <stdlib.h>
#include <iostream>
#include "api/BamMultiReader.h"
#include "api/BamWriter.h"
#include "api/SamReadGroup.h"
using namespace BamTools;
using namespace std;
void printUsage(int argc, char** argv) {
cerr << "usage: " << argv[0] << " [-b FILE [-s NAME [-r GROUP]]]" << endl
<< endl
<< "options:" << endl
<< " -h, --help this dialog" << endl
<< " -b, --bam FILE use this BAM as input" << endl
<< " -u, --uncompressed write uncompressed BAM output" << endl
<< " -s, --sample NAME optionally apply this sample name to the preceeding BAM file" << endl
<< " -d, --delete NAME removes this sample name and all associated RGs from the header" << endl
<< " -c, --clear removes all RGs which were in the old file" << endl
<< " -r, --read-group GROUP optionally apply this read group to the preceeding BAM file" << endl
<< " -R, --region REGION limit alignments to those in this region (chr:start..end)" << endl
<< endl
<< "Merges the alignments in the supplied BAM files, using the supplied sample names" << endl
<< "and read groups to specifically add read group (RG) tags to each alignment. The" << endl
<< "output is uncompressed, and is suitable for input into downstream alignment systems" << endl
<< "which require RG tag information." << endl
<< endl
<< "Sample names and read groups may be specified by supplying a sample name or read group" << endl
<< "argument after each listed BAM file." << endl
<< endl
<< "When no sample names are supplied, the names of the BAM files are used as the sample" << endl
<< "names and read groups. When no read groups are supplied, the sample names are used" << endl
<< "as read groups." << endl
<< endl
<< "author: Erik Garrison <[email protected]>" << endl;
}
void setRegion(BamMultiReader& reader, string& regionStr) {
// parse the region string
if (!regionStr.empty()) {
map<string, int> refLength;
map<string, int> refID;
int id = 0;
RefVector references = reader.GetReferenceData();
for (RefVector::iterator r = references.begin(); r != references.end(); ++r) {
refLength[r->RefName] = r->RefLength;
refID[r->RefName] = id++;
}
// parse the region string
string startSeq;
int startPos;
int stopPos;
size_t foundFirstColon = regionStr.find(":");
// we only have a single string, use the whole sequence as the target
if (foundFirstColon == string::npos) {
startSeq = regionStr;
startPos = 0;
stopPos = -1;
} else {
startSeq = regionStr.substr(0, foundFirstColon);
size_t foundRangeDots = regionStr.find("..", foundFirstColon);
if (foundRangeDots == string::npos) {
startPos = atoi(regionStr.substr(foundFirstColon + 1).c_str());
// differ from bamtools in this regard, in that we process only
// the specified position if a range isn't given
stopPos = startPos + 1;
} else {
startPos = atoi(regionStr.substr(foundFirstColon + 1, foundRangeDots - foundRangeDots - 1).c_str());
// if we have range dots specified, but no second number, read to the end of sequence
if (foundRangeDots + 2 != regionStr.size()) {
stopPos = atoi(regionStr.substr(foundRangeDots + 2).c_str()); // end-exclusive, bed-format
} else {
stopPos = refLength[startSeq];
}
}
}
if (stopPos == -1) {
stopPos = refLength[startSeq];
}
int startSeqRefID = refID[startSeq];
reader.LocateIndexes();
if (!reader.SetRegion(startSeqRefID, startPos, startSeqRefID, stopPos)) {
cerr << "Could not set region" << endl;
exit(1);
}
}
}
int main(int argc, char** argv) {
vector<string> inputFilenames;
vector<string> sampleNames;
vector<string> readGroups;
map<string, int> samplesToDelete;
bool deleteOldRGs = false;
string currFileName;
string currReadGroup;
string currSampleName;
string regionStr;
bool writeUncompressed = false;
// parse command-line options
int c;
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"bam", required_argument, 0, 'b'},
{"uncompressed", no_argument, 0, 'u'},
{"read-group", required_argument, 0, 'r'},
{"delete", required_argument, 0, 'd'},
{"clear", no_argument, 0, 'c'},
{"sample", required_argument, 0, 's'},
{"region", required_argument, 0, 'R'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "hcb:d:s:r:R:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case '?':
printUsage(argc, argv);
return 0;
break;
case 'h':
printUsage(argc, argv);
return 0;
break;
case 'c':
deleteOldRGs = true;
break;
case 'u':
writeUncompressed = true;
break;
case 'd':
samplesToDelete[optarg];
break;
case 'b':
if (!currFileName.empty()) {
inputFilenames.push_back(currFileName);
if (currSampleName.empty()) {
currSampleName = currFileName;
}
sampleNames.push_back(currSampleName);
// use the sample name by default
readGroups.push_back(currReadGroup.empty() ? currSampleName : currReadGroup);
currFileName.clear();
currSampleName.clear();
currReadGroup.clear();
}
currFileName = optarg;
break;
case 's':
currSampleName = optarg;
break;
case 'r':
currReadGroup = optarg;
break;
case 'R':
regionStr = optarg;
break;
default:
return 1;
break;
}
}
// catch last iteration
if (!currFileName.empty()) {
inputFilenames.push_back(currFileName);
if (currSampleName.empty()) {
currSampleName = currFileName;
}
sampleNames.push_back(currSampleName);
// use the sample name by default
readGroups.push_back(currReadGroup.empty() ? currSampleName : currReadGroup);
currFileName.clear();
currSampleName.clear();
currReadGroup.clear();
}
if (inputFilenames.empty()) {
cerr << "no input files specified" << endl;
return 1;
}
map<string, string> filenameToReadGroup;
map<string, string> readGroupToSampleName;
vector<SamReadGroup> newReadGroups;
vector<SamReadGroup> readGroupsToDelete;
vector<string>::iterator b = inputFilenames.begin();
vector<string>::iterator r = readGroups.begin();
vector<string>::iterator s = sampleNames.begin();
for (; b != inputFilenames.end(); ++b, ++r, ++s) {
//cerr << *b << " " << *s << " " << *r << endl;
filenameToReadGroup[*b] = *r;
readGroupToSampleName[*r] = *s;
SamReadGroup samRG(*r);
samRG.Sample = *s;
newReadGroups.push_back(samRG);
}
BamMultiReader reader;
if (!reader.Open(inputFilenames)) {
cerr << "could not open input BAM files" << endl;
return 1;
}
setRegion(reader, regionStr);
const RefVector references = reader.GetReferenceData();
// add read groups
SamHeader header = reader.GetHeader();
vector<SamReadGroup> rgsToDelete;
for (SamReadGroupIterator g = header.ReadGroups.Begin(); g != header.ReadGroups.End(); ++g) {
if (samplesToDelete.find(g->Sample) != samplesToDelete.end() || deleteOldRGs) {
rgsToDelete.push_back(*g);
}
}
for (vector<SamReadGroup>::iterator r = newReadGroups.begin(); r != newReadGroups.end(); ++r) {
header.ReadGroups.Add(*r);
}
for (vector<SamReadGroup>::iterator r = rgsToDelete.begin(); r != rgsToDelete.end(); ++r) {
header.ReadGroups.Remove(*r);
}
BamWriter writer;
if (writeUncompressed) {
writer.SetCompressionMode(BamWriter::Uncompressed);
}
if (!writer.Open("stdout", header.ToString(), references)) {
cerr << "could not open BAM output stream" << endl;
return 1;
}
BamAlignment al;
while (reader.GetNextAlignment(al)) {
if (!al.EditTag("RG", "Z", filenameToReadGroup[al.Filename])) {
cerr << "could not add or edit RG tag on alignment " << al.Name << endl;
return 1;
}
writer.SaveAlignment(al);
}
reader.Close();
writer.Close();
return 0;
}