-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseliftover.cpp
292 lines (240 loc) · 7.88 KB
/
baseliftover.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
/* baseliftover: lift over individual bases. Currently this has
* problems and I can't remember what the base_liftover program does
* differently.
*
* 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.
*/
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <stdexcept>
#include <algorithm>
#include "smithlab_utils.hpp"
#include "smithlab_os.hpp"
#include "OptionParser.hpp"
#include "GenomicRegion.hpp"
using std::string;
using std::ios_base;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;
using std::runtime_error;
struct site {
site() {}
site(const string &c, const size_t p) : chrom(c), pos(p) {}
string chrom;
size_t pos;
};
struct block {
string r_chrom;
size_t r_start;
size_t r_end;
string q_chrom;
size_t q_start;
size_t q_end;
char q_strand;
bool operator<(const block &o) const {
// not checking the r_end because these should not overlap
return r_chrom < o.r_chrom || (r_chrom == o.r_chrom && r_start < o.r_start);
}
};
std::ostream &
operator<<(std::ostream &os, const block &b) {
return os << b.r_chrom << '\t'
<< b.r_start << '\t'
<< b.r_end << '\t'
<< b.q_chrom << '\t'
<< b.q_start << '\t'
<< b.q_end << '\t'
<< b.q_strand;
}
std::istream &
operator>>(std::istream &is, block &b) {
return (is
>> b.r_chrom
>> b.r_start
>> b.r_end
>> b.q_chrom
>> b.q_start
>> b.q_end
>> b.q_strand);
}
static bool
precedes(const block &b, const site &s) {
return b.r_chrom < s.chrom || (b.r_chrom == s.chrom && b.r_end <= s.pos);
}
static bool
contains(const block &b, const site &s) {
return b.r_start <= s.pos && s.pos < b.r_end && b.r_chrom == s.chrom;
}
static void
lift_site(const block &b, const site &work, GenomicRegion &lifted) {
const size_t diff = work.pos - b.r_start;
lifted.set_chrom(b.q_chrom);
if (b.q_strand == '+')
lifted.set_start(b.q_start + diff);
else {
lifted.set_start(b.q_end - diff - 1);
lifted.set_strand(lifted.pos_strand() ? '-' : '+');
}
lifted.set_end(lifted.get_start());
}
static bool
lift_pos_strand(std::ostream &out,
const vector<block> &the_blocks, size_t curr_block,
site work, const GenomicRegion ®ion) {
const size_t n_blocks = the_blocks.size();
// make a temporary site based on 5' end of read to locate
const size_t w = region.get_width();
bool lifted_position = false;
for (size_t i = 0; i < w; ++i) {
// find relevant block
while (curr_block < n_blocks && precedes(the_blocks[curr_block], work))
++curr_block;
// if we have a relevant block, do the lifting
if (curr_block < n_blocks && contains(the_blocks[curr_block], work)) {
GenomicRegion lifted;
lift_site(the_blocks[curr_block], work, lifted);
out << lifted << endl;
lifted_position = true;
}
++work.pos;
}
return lifted_position;
}
static bool
lift_neg_strand(std::ostream &out,
const vector<block> &the_blocks, size_t curr_block,
site work, const GenomicRegion ®ion) {
const size_t n_blocks = the_blocks.size();
// make a temporary site based on 5' end of read to locate
const size_t w = region.get_width();
bool lifted_position = false;
for (size_t i = 0; i < w; ++i) {
// find relevant block
while (curr_block < n_blocks && precedes(the_blocks[curr_block], work))
++curr_block;
// if we have a relevant block, do the lifting
if (curr_block < n_blocks && contains(the_blocks[curr_block], work)) {
GenomicRegion lifted;
lift_site(the_blocks[curr_block], work, lifted);
out << lifted << endl;
lifted_position = true;
}
++work.pos;
}
return lifted_position;
}
int
main(int argc, const char **argv) {
try{
string outfile;
string fails_file;
bool VERBOSE = false;
/****************** COMMAND LINE OPTIONS ********************/
OptionParser opt_parse(strip_path(argv[0]),
"liftover each base possible in a set of intervals",
"<chain> <intervals>");
opt_parse.add_opt("outfile", 'o', "output file", false, outfile);
opt_parse.add_opt("fails", 'f', "failed liftovers",
false, fails_file);
opt_parse.add_opt("verbose", 'v', "(optional) Print more information",
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.option_missing_message() << endl;
return EXIT_SUCCESS;
}
const string chain_file(leftover_args.front());
const string intervals_file(leftover_args.back());
/****************** END COMMAND LINE OPTIONS *****************/
/* READ IN AND VERIFY THE CHAIN BLOCKS FOR LIFTING OVER
*/
if (VERBOSE)
cerr << "[reading input blocks]" << endl;
std::ifstream in_blocks(chain_file.c_str());
if (!in_blocks)
throw runtime_error("bad input file: " + chain_file);
vector<block> the_blocks;
block tmp_block;
while (in_blocks >> tmp_block)
the_blocks.push_back(tmp_block);
if (!check_sorted(the_blocks))
throw runtime_error("blocks not sorted in: " + chain_file);
const size_t n_blocks = the_blocks.size();
if (VERBOSE)
cerr << "[total blocks = " << n_blocks << "]" << endl;
/* PREPARE THE OUTPUT FILES (FOR THE LIFTED AND THE FAILS)
*/
std::ofstream of;
if (!outfile.empty()) of.open(outfile.c_str());
std::ostream out(outfile.empty() ? cout.rdbuf() : of.rdbuf());
std::ofstream ff;
if (!fails_file.empty()) ff.open(fails_file.c_str());
std::ostream fails(ff.rdbuf());
/* NOW PROCESS THE SITES
*/
std::ifstream in_intervals(intervals_file.c_str());
if (!in_intervals)
throw runtime_error("could not open file: " + intervals_file);
if (VERBOSE)
cerr << "[processing sites]" << endl;
size_t curr_block = 0;
GenomicRegion tmp_gr;
size_t lines_processed = 0;
while (in_intervals >> tmp_gr) {
++lines_processed;
site work(tmp_gr.get_chrom(), tmp_gr.get_start());
// find relevant block
while (curr_block < n_blocks && precedes(the_blocks[curr_block], work))
++curr_block;
size_t local_block = curr_block;
GenomicRegion lifted_gr(tmp_gr);
const bool lift_success = tmp_gr.pos_strand() ?
lift_pos_strand(out, the_blocks, local_block, work, lifted_gr) :
lift_neg_strand(out, the_blocks, local_block, work, lifted_gr);
if (!lift_success && !fails_file.empty())
fails << tmp_gr << '\n';
}
if (VERBOSE)
cerr << "[lines processed = " << lines_processed << "]" << endl;
}
catch (std::bad_alloc &ba) {
cerr << "ERROR: could not allocate memory" << endl;
return EXIT_FAILURE;
}
catch (const std::exception &e) {
cerr << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}