-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout_utils.cpp
341 lines (293 loc) · 9.64 KB
/
layout_utils.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2014 Bruno Rahle
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <utility>
#include <zlib.h>
#include <string>
#include <sstream>
#include <unordered_map>
#include "lib/amos/reader.cpp"
#include "layout/layout_utils.h"
using std::stringstream;
using std::string;
using std::swap;
using std::unordered_map;
using std::vector;
using std::ostream;
namespace layout {
/**
* Since ids in ReadSet start with 0 and real ids (reads read from afg file/AMOS bank) can start with arbitrary number,
* we have to map real_id -> internal_id (sequence that starts with 0).
* That's why introduced this type.
*/
typedef std::unordered_map<int, int> ReadIdMap;
ReadIdMap _MapIds(overlap::ReadSet* reads) {
ReadIdMap mapped;
int reads_len = reads->size();
for (int i = 0; i < reads_len; ++i) {
auto& read = (*reads)[i];
if (mapped.count(read->orig_id())) {
fprintf(stderr, "Read with orig_id '%d' already seen\n", read->orig_id());
exit(2);
}
mapped[read->orig_id()] = read->id();
}
return mapped;
}
uint32_t ReadReadsAfg(overlap::ReadSet& container, const char* filename) {
int records = 0;
clock_t start = clock();
auto read_set = new overlap::ReadSet(10000);
vector< AMOS::Read* > tmp_reads;
uint32_t reads_size = AMOS::get_reads(tmp_reads, filename);
if (reads_size < 0) {
return reads_size;
}
for (int i = 0; i < reads_size; ++i) {
const auto& read = tmp_reads[i];
container.Add(new overlap::Read(
(uint8_t*) read->seq,
read->clr_lo,
read->clr_hi,
i,
read->iid
));
}
printf(
"Reads read in %.2lfs\n",
(clock() - start)/static_cast<double>(CLOCKS_PER_SEC));
return records;
}
overlap::OverlapSet* ReadOverlapsAfg(overlap::ReadSet* read_set, FILE *fd) {
clock_t start = clock();
ReadIdMap internal_id = _MapIds(read_set);
overlap::OverlapSet* overlap_set = new overlap::OverlapSet(10000);
char type;
int read_one;
int read_two;
int score;
int hang_one;
int hang_two;
while (fscanf(
fd,
" {OVL adj:%c rds:%d,%d scr:%d ahg:%d bhg:%d }",
&type,
&read_one,
&read_two,
&score,
&hang_one,
&hang_two) == 6) {
if (!internal_id.count(read_one)) {
fprintf(stderr, "Read with orig_id '%d' has not been found\n", read_one);
exit(3);
}
if (!internal_id.count(read_two)) {
fprintf(stderr, "Read with orig_id '%d' has not been found\n", read_two);
exit(3);
}
if (type != 'N' && type != 'I') {
fprintf(stderr, "Unkown overlap type '%c'\n", type);
exit(3);
}
read_one = internal_id[read_one];
read_two = internal_id[read_two];
std::pair<int, int> lenghts = getOverlapLengths(read_set, read_one, read_two, hang_one, hang_two);
overlap_set->Add(new overlap::Overlap(
read_one,
read_two,
lenghts.first,
lenghts.second,
hang_one,
hang_two,
type == 'N' ? overlap::Overlap::Type::EB : overlap::Overlap::Type::EE,
0));
}
printf(
"Overlaps read in %.2lfs\n",
(clock() - start)/static_cast<double>(CLOCKS_PER_SEC));
return overlap_set;
}
int n50(Unitigging::ContigSetPtr contig_set) {
std::vector< int > v;
int cnt = 0;
v.reserve(contig_set->size()*10);
for (size_t i = 0; i < contig_set->size(); ++i) {
if ((*contig_set)[i]->IsUsable()) {
printf("Contig %d: size = %d\n", ++cnt, (*contig_set)[i]->size());
for (size_t j = 0; j < (*contig_set)[i]->size(); ++j) {
v.push_back((*contig_set)[i]->size());
}
}
}
if (v.size() == 0) {
return 0;
}
std::sort(v.begin(), v.end());
return v[v.size()/2];
}
/**
* Calculates lengths of overlaps between read_one and read_two.
*/
std::pair<int, int> getOverlapLengths(const overlap::ReadSet* read_set, const int a, const int b, const int a_hang, const int b_hang) {
int len_one, len_two;
int a_size = (*read_set)[a]->size();
int b_size = (*read_set)[b]->size();
//
// -------|--------------> b_hang
// a_hang ---------------|------>
//
// -a_hang -------------|------->
// --------|------------> -b_hang
//
// -------|-------------|------->
// a_hang --------------> -b_hang
//
// -a_hang --------------> b_hang
// --------|-------------|------>
//
if (a_hang >= 0 && b_hang >= 0) {
len_one = a_size - a_hang;
len_two = b_size - b_hang;
} else if (a_hang <= 0 && b_hang <= 0) {
len_one = a_size + b_hang;
len_two = b_size + a_hang;
} else if (a_hang >= 0 && b_hang <= 0) {
len_one = a_size + b_hang - a_hang;
len_two = b_size;
} else if (a_hang <= 0 && b_hang >= 0) {
len_one = a_size;
len_two = b_size + a_hang - b_hang;
} else {
// case not covered
assert(false);
}
assert(len_one > 0 && len_two > 0);
return std::make_pair(len_one, len_two);
}
/**
* Writes all usable contigs from contig set to a file with given filename.
* If file does not exist, it will be created.
* Returns the number of written contigs.
*/
int ContigsToFile(std::shared_ptr<ContigSet> contigs, const char *contigs_filename) {
FILE *contigs_file = fopen(contigs_filename, "w");
if (contigs_file == nullptr) {
return -1;
}
int written = 0;
int contigs_size = contigs->size();
for (int i = 0; i < contigs_size; ++i) {
// skip non-usable contigs
if (!((*contigs)[i]->IsUsable())) continue;
const std::deque<BetterRead*> &reads = (*contigs)[i]->getReads();
const std::deque<BetterOverlap*> &overlaps = (*contigs)[i]->getOverlaps();
const auto& f_read = reads[0];
const auto& f_overlap = overlaps[0]->overlap();
// forward is ---->, backward is <----
bool forward = overlaps[0]->Suf(f_read->id());
uint32_t offset = 0;
auto process_read =
[&contigs_file, &forward, &offset] (const BetterRead* r, const BetterOverlap* o) {
const auto& read = r->read();
const auto& overlap = o->overlap();
uint32_t lo = read->lo();
uint32_t hi = read->hi();
if (!forward) {
swap(lo, hi);
}
fprintf(contigs_file, "{TLE\n");
fprintf(contigs_file, "clr:%u,%u\n", lo, hi);
fprintf(contigs_file, "off:%u\n", offset);
fprintf(contigs_file, "src:%d\n}\n", read->orig_id());
if (read->id() == overlap->read_one) {
if (overlap->a_hang > 0) {
offset += overlap->a_hang;
} else {
offset += abs(overlap->b_hang);
}
} else if (read->id() == overlap->read_two) {
if (overlap->b_hang > 0) {
offset += overlap->b_hang;
} else {
offset += abs(overlap->a_hang);
}
}
if (overlap->type == overlap::Overlap::Type::EE) {
forward = !forward;
}
};
fprintf(contigs_file, "{LAY\n");
process_read(reads[0], overlaps[0]);
int num_reads = reads.size();
for (int j = 1; j < num_reads - 1; ++j) {
process_read(reads[j], overlaps[j]);
}
process_read(reads[num_reads-1], overlaps[num_reads-2]);
fprintf(contigs_file, "}\n");
written++;
}
fclose(contigs_file);
return written;
}
std::string dot_graph(overlap::ReadSet* reads, overlap::OverlapSet* overlaps) {
BetterReadSet brs(reads, false);
BetterOverlapSet bos(reads, overlaps);
return dot_graph(&brs, &bos);
}
std::string dot_graph(const BetterReadSet* reads, const BetterOverlapSet* overlaps) {
stringstream graph;
graph << "digraph overlaps {\n";
int overlaps_size = overlaps->size();
for (int i = 0; i < overlaps_size; ++i) {
const auto& overlap = (*overlaps)[i];
int read1 = (*reads)[overlap->overlap()->read_one]->read()->orig_id();
int read2 = (*reads)[overlap->overlap()->read_two]->read()->orig_id();
if (overlap->GoesFrom(overlap->overlap()->read_one)) {
graph << read1 << " -> " << read2;
} else {
graph << read2 << " -> " << read1;
}
if (overlap->overlap()->type == overlap::Overlap::Type::EB) {
graph << " [color=green] ";
} else {
graph << " [color=pink] ";
}
graph << ";\n";
}
graph << "}\n";
return graph.str();
}
int write_overlaps(ostream& output, const BetterReadSet* reads, const BetterOverlapSet* overlaps) {
int lines = 0;
int overlaps_size = overlaps->size();
for (int i = 0; i < overlaps_size; ++i) {
const auto& overlap = (*overlaps)[i]->overlap();
int read1 = (*reads)[overlap->read_one]->read()->orig_id();
int read2 = (*reads)[overlap->read_two]->read()->orig_id();
output << "{OVL" << std::endl;
lines++;
char adj = '?';
switch (overlap->type) {
case overlap::Overlap::Type::EB:
adj = 'N';
break;
case overlap::Overlap::Type::EE:
adj = 'I';
break;
default:
assert(false);
}
output << "rds:" << read1 << "," << read2 << std::endl;
output << "adj:" << adj << std::endl;
output << "ahg:" << overlap->a_hang << std::endl;
output << "bhg:" << overlap->b_hang << std::endl;
output << "scr:" << overlap->score << std::endl;
lines += 5;
output << "}" << std::endl;
lines++;
}
return lines;
}
}; // namespace layout