-
Notifications
You must be signed in to change notification settings - Fork 1
/
collapsebed.cpp
306 lines (263 loc) · 8.71 KB
/
collapsebed.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
296
297
298
299
300
301
302
303
304
305
306
/* collapsebed: report contiguous genomic intervals covered at
* least the specified number of times by given intervals.
*
* Copyright (C) 2014 Andrew D. Smith
*
* Authors: Andrew D. Smith
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "OptionParser.hpp"
#include "smithlab_utils.hpp"
#include "smithlab_os.hpp"
#include "GenomicRegion.hpp"
using std::string;
using std::vector;
using std::endl;
using std::cerr;
struct end_point_score {
end_point_score(const string c, const size_t s, const bool isf,
const double sc) :
chr(c), start(s), count(1), total(sc), is_first(isf) {}
bool operator<(const end_point_score &other) const {
return (chr < other.chr ||
(chr == other.chr &&
(start < other.start ||
(start == other.start &&
is_first < other.is_first))));
}
string chr;
size_t start;
size_t count;
double total;
bool is_first;
};
static end_point_score
first_end_point_score(const GenomicRegion &r) {
return end_point_score(r.get_chrom(), r.get_start(), true, r.get_score());
}
static end_point_score
second_end_point_score(const GenomicRegion &r) {
return end_point_score(r.get_chrom(), r.get_end(), false, r.get_score());
}
static bool
equal_end_points_score(const end_point_score &a, const end_point_score &b) {
return a.chr == b.chr && a.start == b.start && a.is_first == b.is_first;
}
static void
collapse_end_points(vector<end_point_score> &ep) {
size_t j = 0;
for (size_t i = 1; i < ep.size(); ++i) {
if (equal_end_points_score(ep[i], ep[j])) {
ep[j].count++;
ep[j].total += ep[i].total;
}
else ep[++j] = ep[i];
}
ep.erase(ep.begin() + j + 1, ep.end());
}
static void
collapse_bed(const size_t cutoff, vector<end_point_score> &end_points,
vector<GenomicRegion> &collapsed) {
collapse_end_points(end_points);
size_t count = 0;
double total = 0.0;
GenomicRegion region;
for (size_t i = 0; i < end_points.size() - 1; ++i) {
if (end_points[i].is_first) {
count += end_points[i].count;
total += end_points[i].total;
}
else {
count -= end_points[i].count;
total -= end_points[i].total;
}
if (count >= cutoff) {
region.set_chrom(end_points[i].chr);
region.set_start(end_points[i].start);
region.set_end(end_points[i + 1].start);
region.set_score(total/count);
collapsed.push_back(region);
}
}
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
struct end_point {
end_point(const string c, const size_t s, const bool isf) :
chr(c), start(s), count(1), is_first(isf) {}
bool operator<(const end_point &other) const {
return (chr < other.chr ||
(chr == other.chr &&
(start < other.start ||
(start == other.start &&
is_first < other.is_first))));
}
string chr;
size_t start;
size_t count;
bool is_first;
};
static end_point
first_end_point(const GenomicRegion &r) {
return end_point(r.get_chrom(), r.get_start(), true);
}
static end_point
second_end_point(const GenomicRegion &r) {
return end_point(r.get_chrom(), r.get_end(), false);
}
static bool
equal_end_points(const end_point &a, const end_point &b) {
return a.chr == b.chr && a.start == b.start && a.is_first == b.is_first;
}
static void
collapse_end_points(vector<end_point> &ep) {
size_t j = 0;
for (size_t i = 1; i < ep.size(); ++i) {
if (equal_end_points(ep[i], ep[j]))
ep[j].count++;
else ep[++j] = ep[i];
}
ep.erase(ep.begin() + j + 1, ep.end());
}
static void
collapse_bed(const size_t cutoff, vector<end_point> &end_points,
vector<GenomicRegion> &collapsed) {
collapse_end_points(end_points);
size_t count = 0;
GenomicRegion region;
for (size_t i = 0; i < end_points.size() - 1; ++i) {
if (end_points[i].is_first)
count += end_points[i].count;
else
count -= end_points[i].count;
if (count >= cutoff) {
region.set_chrom(end_points[i].chr);
region.set_start(end_points[i].start);
region.set_end(end_points[i + 1].start);
region.set_score(count);
collapsed.push_back(region);
}
}
}
static void
collapse_bed_merged(const size_t cutoff, const vector<end_point> &end_points,
vector<GenomicRegion> &collapsed) {
size_t count = 0;
GenomicRegion region;
for (size_t i = 0; i < end_points.size(); ++i) {
if (end_points[i].is_first) {
++count;
if (count == cutoff) {
region.set_chrom(end_points[i].chr);
region.set_start(end_points[i].start);
}
}
else {
if (count == cutoff) {
region.set_end(end_points[i].start);
collapsed.push_back(region);
}
--count;
}
}
}
int
main(int argc, const char **argv) {
try {
/* FILES */
string outfile;
bool VERBOSE = false;
bool FRAGMENTS = false;
size_t cutoff = 1;
bool AVERAGE_SCORES = false;
/****************** GET COMMAND LINE ARGUMENTS ***************************/
OptionParser opt_parse(strip_path(argv[0]), "", "<interval-files>");
opt_parse.add_opt("output", 'o', "output file (default: stdout)",
false , outfile);
opt_parse.add_opt("frag", 'f', "keep fragments separate",
false , FRAGMENTS);
opt_parse.add_opt("cutoff", 'c', "report intervals covered this many times",
false , cutoff);
opt_parse.add_opt("average", 'a', "average scores (implies -f)",
false, AVERAGE_SCORES);
// opt_parse.add_opt("verbose", 'v', "print more run info",
// false , VERBOSE);
vector<string> leftover_args;
opt_parse.parse(argc, argv, leftover_args);
if (argc == 1 || opt_parse.help_requested()) {
cerr << opt_parse.help_message() << endl;
return EXIT_SUCCESS;
}
if (opt_parse.about_requested()) {
cerr << opt_parse.about_message() << endl;
return EXIT_SUCCESS;
}
if (opt_parse.option_missing()) {
cerr << opt_parse.option_missing_message() << endl;
return EXIT_SUCCESS;
}
if (leftover_args.size() < 1) {
cerr << opt_parse.help_message() << endl;
return EXIT_SUCCESS;
}
const vector<string> interval_files(leftover_args);
/**********************************************************************/
vector<GenomicRegion> collapsed;
if (AVERAGE_SCORES) {
vector<end_point_score> end_points;
for (size_t i = 0; i < interval_files.size(); ++i) {
if (VERBOSE)
cerr << interval_files[i] << endl;
vector<GenomicRegion> intervals;
ReadBEDFile(interval_files[i], intervals);
for (size_t j = 0; j < intervals.size(); ++j) {
end_points.push_back(first_end_point_score(intervals[j]));
end_points.push_back(second_end_point_score(intervals[j]));
}
}
sort(end_points.begin(), end_points.end());
collapse_bed(cutoff, end_points, collapsed);
}
else {
vector<end_point> end_points;
for (size_t i = 0; i < interval_files.size(); ++i) {
if (VERBOSE)
cerr << interval_files[i] << endl;
vector<GenomicRegion> intervals;
ReadBEDFile(interval_files[i], intervals);
for (size_t j = 0; j < intervals.size(); ++j) {
end_points.push_back(first_end_point(intervals[j]));
end_points.push_back(second_end_point(intervals[j]));
}
}
sort(end_points.begin(), end_points.end());
if (FRAGMENTS)
collapse_bed(cutoff, end_points, collapsed);
else
collapse_bed_merged(cutoff, end_points, collapsed);
}
std::ofstream of;
if (!outfile.empty()) of.open(outfile.c_str());
std::ostream out(outfile.empty() ? std::cout.rdbuf() : of.rdbuf());
copy(collapsed.begin(), collapsed.end(),
std::ostream_iterator<GenomicRegion>(out, "\n"));
}
catch (std::exception &e) {
cerr << "ERROR:\t" << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}