-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncbins.cpp
189 lines (167 loc) · 5.89 KB
/
syncbins.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
/* syncbins: take a set of non-overlapping intervals with
* associated scores (bedgraph format) and position the
* corresponding information relative to a set of bins.
*
* Copyright (C) 2018 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;
using std::unordered_map;
using std::pair;
struct bedgraph {
string chr;
size_t start;
size_t end;
double score;
};
static std::istream&
operator>>(std::istream &in, bedgraph &bg) {
string line;
if (getline(in, line)) {
std::istringstream iss(line);
if (!(iss >> bg.chr >> bg.start >> bg.end >> bg.score))
throw std::runtime_error("bad line: " + line);
}
return in;
}
static bool
ends_before(bedgraph &bg, const string &chr, const size_t start) {
return bg.chr < chr || (bg.chr == chr && bg.end <= start);
}
static bool
doesnt_extend_past(bedgraph &bg, const string &chr, const size_t end) {
return bg.chr < chr || (bg.chr == chr && (bg.end <= end));
}
static bool
overlaps(bedgraph &bg, const string &chr, const size_t start,
const size_t end) {
return bg.chr == chr &&
(std::max(bg.start, start) < std::min(bg.end, end));
}
static void
update_bin(const size_t start, const size_t end,
const bedgraph &bg, double &total, size_t &count) {
const size_t overlap_size =
std::min(bg.end, end) - std::max(bg.start, start);
total += bg.score*overlap_size;
count += overlap_size;
}
int
main(int argc, const char **argv) {
try {
/* FILES */
string outfile;
bool VERBOSE = false;
bool require_data = false;
size_t bin_size = 100;
/****************** GET COMMAND LINE ARGUMENTS ***************************/
OptionParser opt_parse(strip_path(argv[0]), "", "<chroms> <bedgraph>");
opt_parse.add_opt("output", 'o', "output file (default: stdout)",
false , outfile);
opt_parse.add_opt("bin-size", 'b', "bin size (default: 100)",
false , bin_size);
opt_parse.add_opt("with-data", 'd', "only output bins with data",
false , require_data);
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() != 2) {
cerr << opt_parse.help_message() << endl;
return EXIT_SUCCESS;
}
const string chroms_file(leftover_args.front());
const string bg_file(leftover_args.back());
/**********************************************************************/
std::ifstream chroms_in(chroms_file.c_str());
if (!chroms_in)
throw std::runtime_error("bad file: " + chroms_file);
string line;
vector<pair<string, size_t> > chroms;
while (getline(chroms_in, line)) {
std::istringstream iss(line);
string chr;
size_t sz = 0;
if (!(iss >> chr >> sz))
throw std::runtime_error("bad line: " + line);
chroms.push_back(make_pair(chr, sz));
}
sort(chroms.begin(), chroms.end());
if (VERBOSE)
for (auto &i : chroms)
cerr << i.first << '\t' << i.second << endl;
std::ofstream of;
if (!outfile.empty()) of.open(outfile.c_str());
std::ostream out(outfile.empty() ? std::cout.rdbuf() : of.rdbuf());
bedgraph curr;
std::ifstream in(bg_file.c_str());
in >> curr;
for (size_t i = 0; i < chroms.size(); ++i) {
const string chr(chroms[i].first);
const size_t chr_size = chroms[i].second;
for (size_t start = 0; start < chr_size; start += bin_size) {
const size_t end = std::min(chr_size, start + bin_size);
double total = 0.0; // sum of (bases * scores) for covered bases in bin
size_t count = 0; // sum of (bases) == number of covered bases
// move past all earlier intervals; might not be needed as a
// loop, since bins cover entire genome
if (in && ends_before(curr, chr, start))
in >> curr;
// process overlapping intervals not relevant for the next bin
while (in && doesnt_extend_past(curr, chr, end)) {
update_bin(start, end, curr, total, count);
in >> curr;
}
// process any overlapping intervals relevant for the next bin
if (in && overlaps(curr, chr, start, end))
update_bin(start, end, curr, total, count);
if (!require_data || count > 0)
out << chr << '\t'
<< start << '\t' << end << '\t'
<< (count > 0 ? total/count : 0) << '\n';
}
}
}
catch (std::bad_alloc &ba) {
cerr << "ERROR: could not allocate memory" << endl;
return EXIT_FAILURE;
}
catch (std::exception &e) {
cerr << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}