| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
|
| 6 |
// This reads the assembly frgctg, varctg and merQC badmers, computes |
| 7 |
// the number and location of bad-mer, bad-var regions, and their |
| 8 |
// depth, in contig space. |
| 9 |
// |
| 10 |
// File paths are hardcoded. |
| 11 |
// This code ONLY works on 64-bit hardware, but it's easy to fix. |
| 12 |
|
| 13 |
using namespace std; |
| 14 |
#include <map> |
| 15 |
|
| 16 |
// |
| 17 |
// Begin code from Bri's intervalList.H, intervalList.C, splitToWords.H |
| 18 |
// |
| 19 |
typedef unsigned long uint64; |
| 20 |
typedef unsigned int uint32; |
| 21 |
|
| 22 |
#define uint64FMT "%lu" |
| 23 |
#define uint32FMT "%u" |
| 24 |
#define uint32FMTW(X) "%" #X "u" |
| 25 |
|
| 26 |
#define strtouint32(N,O) (uint32)strtoul(N, O, 10) |
| 27 |
#define strtouint64(N,O) (uint64)strtoul(N, O, 10) |
| 28 |
|
| 29 |
class splitToWords { |
| 30 |
public: |
| 31 |
splitToWords() { |
| 32 |
_argWords = 0; |
| 33 |
_maxWords = 0; |
| 34 |
_arg = 0L; |
| 35 |
_maxChars = 0; |
| 36 |
_cmd = 0L; |
| 37 |
}; |
| 38 |
splitToWords(char *cmd) { |
| 39 |
_argWords = 0; |
| 40 |
_maxWords = 0; |
| 41 |
_arg = 0L; |
| 42 |
_maxChars = 0; |
| 43 |
_cmd = 0L; |
| 44 |
|
| 45 |
split(cmd); |
| 46 |
}; |
| 47 |
~splitToWords() { |
| 48 |
delete [] _cmd; |
| 49 |
delete [] _arg; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
void split(char *cmd) { |
| 54 |
|
| 55 |
// Step Zero: |
| 56 |
// |
| 57 |
// Count the length of the string, in words and in characters. |
| 58 |
// For simplicity, we overcount words, by just counting white-space. |
| 59 |
// |
| 60 |
// Then, allocate space for a temporary copy of the string, and a |
| 61 |
// set of pointers into the temporary copy (much like argv). |
| 62 |
// |
| 63 |
uint32 cmdChars = 1; // 1 == Space for terminating 0 |
| 64 |
uint32 cmdWords = 2; // 2 == Space for first word and terminating 0L |
| 65 |
|
| 66 |
for (char *tmp=cmd; *tmp; tmp++) { |
| 67 |
cmdWords += *tmp == ' '; |
| 68 |
cmdWords += *tmp == '\t'; |
| 69 |
cmdChars++; |
| 70 |
} |
| 71 |
|
| 72 |
if (cmdChars > _maxChars) { |
| 73 |
delete [] _cmd; |
| 74 |
_cmd = new char [cmdChars]; |
| 75 |
_maxChars = cmdChars; |
| 76 |
} |
| 77 |
if (cmdWords > _maxWords) { |
| 78 |
delete [] _arg; |
| 79 |
_arg = new char * [cmdWords]; |
| 80 |
_maxWords = cmdWords; |
| 81 |
} |
| 82 |
|
| 83 |
_argWords = 0; |
| 84 |
|
| 85 |
// Step One: |
| 86 |
// |
| 87 |
// Determine where the words are in the command string, copying the |
| 88 |
// string to _cmd and storing words in _arg. |
| 89 |
// |
| 90 |
bool isFirst = true; |
| 91 |
char *cmdI = cmd; |
| 92 |
char *cmdO = _cmd; |
| 93 |
|
| 94 |
while (*cmdI) { |
| 95 |
|
| 96 |
// If we are at a non-space character, we are in a word. If |
| 97 |
// this is the first character in the word, save the word in |
| 98 |
// the args list. |
| 99 |
// |
| 100 |
// Otherwise we are at a space and thus not in a word. Make |
| 101 |
// all spaces be string terminators, and declare that we are |
| 102 |
// at the start of a word. |
| 103 |
// |
| 104 |
if ((*cmdI != ' ') && (*cmdI != '\t')) { |
| 105 |
*cmdO = *cmdI; |
| 106 |
|
| 107 |
if (isFirst) { |
| 108 |
_arg[_argWords++] = cmdO; |
| 109 |
isFirst = false; |
| 110 |
} |
| 111 |
} else { |
| 112 |
*cmdO = 0; |
| 113 |
isFirst = true; |
| 114 |
} |
| 115 |
|
| 116 |
cmdI++; |
| 117 |
cmdO++; |
| 118 |
} |
| 119 |
|
| 120 |
// Finish off the list by terminating the last arg, and |
| 121 |
// terminating the list of args. |
| 122 |
// |
| 123 |
*cmdO = 0; |
| 124 |
_arg[_argWords] = 0L; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
uint32 numWords(void) { return(_argWords); }; |
| 129 |
char *getWord(uint32 i) { return(_arg[i]); }; |
| 130 |
char *operator[](uint32 i) { return(_arg[i]); }; |
| 131 |
private: |
| 132 |
uint32 _argWords; |
| 133 |
uint32 _maxWords; |
| 134 |
char **_arg; |
| 135 |
uint32 _maxChars; |
| 136 |
char *_cmd; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
typedef uint64 intervalNumber; |
| 143 |
|
| 144 |
struct _intervalPair { |
| 145 |
intervalNumber lo; |
| 146 |
intervalNumber hi; |
| 147 |
}; |
| 148 |
|
| 149 |
struct _intervalDepth { |
| 150 |
intervalNumber lo; |
| 151 |
intervalNumber hi; |
| 152 |
uint32 de; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
class intervalList { |
| 157 |
public: |
| 158 |
intervalList(); |
| 159 |
~intervalList(); |
| 160 |
|
| 161 |
intervalList &operator=(intervalList &src); |
| 162 |
|
| 163 |
// Clear a list |
| 164 |
void clear(void) { |
| 165 |
_isSorted = true; |
| 166 |
_isMerged = true; |
| 167 |
_listLen = 0; |
| 168 |
} |
| 169 |
|
| 170 |
// Insert a new interval into the list |
| 171 |
void add(intervalNumber position, intervalNumber length); |
| 172 |
|
| 173 |
// Sort the set of intervals by the lo value |
| 174 |
void sort(void); |
| 175 |
|
| 176 |
// Merge overlapping or adjacent intervals together. |
| 177 |
void merge(void); |
| 178 |
|
| 179 |
void invert(intervalNumber lo, intervalNumber hi); |
| 180 |
|
| 181 |
// Returns the number of intervals |
| 182 |
uint32 numberOfIntervals(void) { |
| 183 |
return(_listLen); |
| 184 |
}; |
| 185 |
|
| 186 |
// Returns the sum of the length of all intervals |
| 187 |
intervalNumber sumOfLengths(void) { |
| 188 |
intervalNumber len = 0; |
| 189 |
uint32 i = numberOfIntervals(); |
| 190 |
|
| 191 |
if (i > 0) |
| 192 |
while (i--) |
| 193 |
len += _list[i].hi - _list[i].lo; |
| 194 |
|
| 195 |
return(len); |
| 196 |
}; |
| 197 |
|
| 198 |
// Populates an array with the intervals that are within the |
| 199 |
// supplied interval. Return |
| 200 |
// |
| 201 |
uint32 overlapping(intervalNumber lo, |
| 202 |
intervalNumber hi, |
| 203 |
uint32 *&intervals, |
| 204 |
uint32 &intervalsLen, |
| 205 |
uint32 &intervalsMax); |
| 206 |
|
| 207 |
// Populates this intervalList with the intersection of A and B. |
| 208 |
// This intervalList is not cleared prior to adding new intervals. |
| 209 |
// |
| 210 |
// Both A and B call merge(). |
| 211 |
// |
| 212 |
void intersect(intervalList &A, |
| 213 |
intervalList &B); |
| 214 |
|
| 215 |
// Populates this intervalList with regions in A that are completely |
| 216 |
// contained in a region in B. |
| 217 |
// |
| 218 |
// Both A and B call merge(). |
| 219 |
// |
| 220 |
void contained(intervalList &A, |
| 221 |
intervalList &B); |
| 222 |
|
| 223 |
|
| 224 |
intervalNumber lo(uint32 i) { return(_list[i].lo); }; |
| 225 |
intervalNumber hi(uint32 i) { return(_list[i].hi); }; |
| 226 |
|
| 227 |
private: |
| 228 |
bool _isSorted; |
| 229 |
bool _isMerged; |
| 230 |
uint32 _listLen; |
| 231 |
uint32 _listMax; |
| 232 |
_intervalPair *_list; |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
// Takes as input an intervalList, computes the number of intervals |
| 238 |
// covering every position in there, stores this as a new set of |
| 239 |
// intervals, annotated with the depth. |
| 240 |
// |
| 241 |
// This is a static object, initialized once by the intervalList. |
| 242 |
// |
| 243 |
class intervalDepth { |
| 244 |
public: |
| 245 |
intervalDepth(intervalList &IL); |
| 246 |
~intervalDepth(); |
| 247 |
|
| 248 |
// Returns the number of intervals |
| 249 |
uint32 numberOfIntervals(void) { |
| 250 |
return(_listLen); |
| 251 |
}; |
| 252 |
|
| 253 |
intervalNumber lo(uint32 i) { return(_list[i].lo); }; |
| 254 |
intervalNumber hi(uint32 i) { return(_list[i].hi); }; |
| 255 |
uint32 de(uint32 i) { return(_list[i].de); }; |
| 256 |
|
| 257 |
private: |
| 258 |
uint32 _listLen; |
| 259 |
uint32 _listMax; |
| 260 |
_intervalDepth *_list; |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
intervalList::intervalList() { |
| 265 |
_isSorted = true; |
| 266 |
_isMerged = true; |
| 267 |
_listLen = 0; |
| 268 |
_listMax = 16; |
| 269 |
_list = new _intervalPair [_listMax]; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
intervalList::~intervalList() { |
| 274 |
delete [] _list; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
intervalList & |
| 279 |
intervalList::operator=(intervalList &src) { |
| 280 |
_isSorted = src._isSorted; |
| 281 |
_isMerged = src._isMerged; |
| 282 |
_listLen = src._listLen; |
| 283 |
|
| 284 |
if (_listMax < src._listMax) { |
| 285 |
delete [] _list; |
| 286 |
_listMax = src._listMax; |
| 287 |
_list = new _intervalPair [_listMax]; |
| 288 |
} |
| 289 |
|
| 290 |
memcpy(_list, src._list, _listLen * sizeof(_intervalPair)); |
| 291 |
|
| 292 |
return(*this); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
void |
| 297 |
intervalList::add(intervalNumber position, intervalNumber length) { |
| 298 |
|
| 299 |
if (_listLen >= _listMax) { |
| 300 |
_listMax *= 2; |
| 301 |
_intervalPair *l = new _intervalPair [_listMax]; |
| 302 |
memcpy(l, _list, sizeof(_intervalPair) * _listLen); |
| 303 |
delete [] _list; |
| 304 |
_list = l; |
| 305 |
} |
| 306 |
|
| 307 |
_list[_listLen].lo = position; |
| 308 |
_list[_listLen].hi = position + length; |
| 309 |
|
| 310 |
#if 0 |
| 311 |
// Aborted attempt to add a data field here. Got stuck |
| 312 |
// deciding how to handle merges lightweight |
| 313 |
|
| 314 |
_list[_listLen].data = 0L; |
| 315 |
|
| 316 |
if (data != ~uint64ZERO) { |
| 317 |
_list[_listLen].dataLen = 1; |
| 318 |
_list[_listLen].dataMax = 4; |
| 319 |
_list[_listLen].data = new uint64 [_list[_listLen].dataMax]; |
| 320 |
_list[_listLen].data[0] = data; |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
if ((_listLen > 0) && |
| 325 |
(_list[_listLen-1].lo > _list[_listLen].lo)) { |
| 326 |
_isSorted = false; |
| 327 |
_isMerged = false; |
| 328 |
} |
| 329 |
|
| 330 |
_listLen++; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
static |
| 335 |
int |
| 336 |
intervalList_sort_helper(const void *a, const void *b) { |
| 337 |
_intervalPair *A = (_intervalPair *)a; |
| 338 |
_intervalPair *B = (_intervalPair *)b; |
| 339 |
|
| 340 |
if (A->lo < B->lo) return(-1); |
| 341 |
if (A->lo > B->lo) return(1); |
| 342 |
if (A->hi < B->hi) return(-1); |
| 343 |
if (A->hi > B->hi) return(1); |
| 344 |
return(0); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
void |
| 349 |
intervalList::sort(void) { |
| 350 |
|
| 351 |
if (_isSorted) |
| 352 |
return; |
| 353 |
|
| 354 |
if (_listLen > 1) |
| 355 |
qsort(_list, _listLen, sizeof(_intervalPair), intervalList_sort_helper); |
| 356 |
|
| 357 |
_isSorted = true; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
void |
| 362 |
intervalList::merge(void) { |
| 363 |
uint32 thisInterval = 0; |
| 364 |
uint32 nextInterval = 1; |
| 365 |
|
| 366 |
if (_listLen < 2) |
| 367 |
return; |
| 368 |
|
| 369 |
sort(); |
| 370 |
|
| 371 |
while (nextInterval < _listLen) { |
| 372 |
|
| 373 |
if ((_list[thisInterval].lo == 0) && |
| 374 |
(_list[thisInterval].hi == 0)) { |
| 375 |
|
| 376 |
// Our interval is empty. Copy in the interval we are |
| 377 |
// examining and move to the next. |
| 378 |
|
| 379 |
// XXX This is probably useless, thisInterval should always be |
| 380 |
// valid. |
| 381 |
|
| 382 |
_list[thisInterval].lo = _list[nextInterval].lo; |
| 383 |
_list[thisInterval].hi = _list[nextInterval].hi; |
| 384 |
|
| 385 |
_list[nextInterval].lo = 0; |
| 386 |
_list[nextInterval].hi = 0; |
| 387 |
nextInterval++; |
| 388 |
} else { |
| 389 |
|
| 390 |
// This interval is valid. See if it overlaps with the next |
| 391 |
// interval. |
| 392 |
|
| 393 |
if (_list[thisInterval].hi >= _list[nextInterval].lo) { |
| 394 |
|
| 395 |
// Got an intersection. |
| 396 |
|
| 397 |
// Merge nextInterval into thisInterval -- the hi range |
| 398 |
// is extended if the nextInterval range is larger. |
| 399 |
// |
| 400 |
if (_list[thisInterval].hi < _list[nextInterval].hi) |
| 401 |
_list[thisInterval].hi = _list[nextInterval].hi; |
| 402 |
|
| 403 |
// Clear the just merged nextInterval and move to the next one. |
| 404 |
// |
| 405 |
_list[nextInterval].lo = 0; |
| 406 |
_list[nextInterval].hi = 0; |
| 407 |
nextInterval++; |
| 408 |
} else { |
| 409 |
|
| 410 |
// No intersection. Move along. Nothing to see here. |
| 411 |
|
| 412 |
// If there is a gap between the target and the examine (we |
| 413 |
// must have merged sometime in the past), copy examine to |
| 414 |
// the next target. |
| 415 |
|
| 416 |
thisInterval++; |
| 417 |
|
| 418 |
if (thisInterval != nextInterval) { |
| 419 |
_list[thisInterval].lo = _list[nextInterval].lo; |
| 420 |
_list[thisInterval].hi = _list[nextInterval].hi; |
| 421 |
} |
| 422 |
|
| 423 |
nextInterval++; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
if (thisInterval+1 < _listLen) |
| 429 |
_listLen = thisInterval + 1; |
| 430 |
|
| 431 |
_isMerged = true; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
void |
| 436 |
intervalList::invert(intervalNumber lo, intervalNumber hi) { |
| 437 |
|
| 438 |
if (!_isSorted || !_isMerged) { |
| 439 |
fprintf(stderr, "intervalList::invert()-- ERROR! List is not sorted or not merged!\n"); |
| 440 |
exit(1); |
| 441 |
} |
| 442 |
|
| 443 |
// Create a new list to store the inversion |
| 444 |
// |
| 445 |
uint32 invLen = 0; |
| 446 |
uint32 invMax = _listLen + 2; |
| 447 |
_intervalPair *inv = new _intervalPair [invMax]; |
| 448 |
|
| 449 |
// Add the first |
| 450 |
// |
| 451 |
if (lo < _list[0].lo) { |
| 452 |
inv[invLen].lo = lo; |
| 453 |
inv[invLen].hi = _list[0].lo; |
| 454 |
invLen++; |
| 455 |
} |
| 456 |
|
| 457 |
// Add the pieces |
| 458 |
for (uint32 i=1; i<_listLen; i++) { |
| 459 |
if (_list[i-1].hi < _list[i].lo) { |
| 460 |
inv[invLen].lo = _list[i-1].hi; |
| 461 |
inv[invLen].hi = _list[i].lo; |
| 462 |
invLen++; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// Add the last |
| 467 |
if (_list[_listLen-1].hi < hi) { |
| 468 |
inv[invLen].lo = _list[_listLen-1].hi; |
| 469 |
inv[invLen].hi = hi; |
| 470 |
invLen++; |
| 471 |
} |
| 472 |
|
| 473 |
// Nuke the old list, swap in the new one |
| 474 |
delete [] _list; |
| 475 |
|
| 476 |
_list = inv; |
| 477 |
_listLen = invLen; |
| 478 |
_listMax = invMax; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
uint32 |
| 484 |
intervalList::overlapping(intervalNumber rangelo, |
| 485 |
intervalNumber rangehi, |
| 486 |
uint32 *&intervals, |
| 487 |
uint32 &intervalsLen, |
| 488 |
uint32 &intervalsMax) { |
| 489 |
|
| 490 |
|
| 491 |
// XXX: Naive implementation that is easy to verify (and that works |
| 492 |
// on an unsorted list). |
| 493 |
|
| 494 |
if (intervals == 0L) { |
| 495 |
intervalsMax = 256; |
| 496 |
intervals = new uint32 [intervalsMax]; |
| 497 |
} |
| 498 |
|
| 499 |
intervalsLen = 0; |
| 500 |
|
| 501 |
for (uint32 i=0; i<_listLen; i++) { |
| 502 |
if ((rangelo <= _list[i].hi) && |
| 503 |
(rangehi >= _list[i].lo)) { |
| 504 |
if (intervalsLen >= intervalsMax) { |
| 505 |
intervalsMax *= 2; |
| 506 |
uint32 *X = new uint32 [intervalsMax]; |
| 507 |
memcpy(X, intervals, sizeof(uint32) * intervalsLen); |
| 508 |
delete [] intervals; |
| 509 |
intervals = X; |
| 510 |
} |
| 511 |
|
| 512 |
intervals[intervalsLen++] = i; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return(intervalsLen); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
void |
| 522 |
intervalList::intersect(intervalList &A, |
| 523 |
intervalList &B) { |
| 524 |
A.merge(); |
| 525 |
B.merge(); |
| 526 |
|
| 527 |
uint32 ai = 0; |
| 528 |
uint32 bi = 0; |
| 529 |
|
| 530 |
while ((ai < A.numberOfIntervals()) && |
| 531 |
(bi < B.numberOfIntervals())) { |
| 532 |
uint32 al = A.lo(ai); |
| 533 |
uint32 ah = A.hi(ai); |
| 534 |
uint32 bl = B.lo(bi); |
| 535 |
uint32 bh = B.hi(bi); |
| 536 |
uint32 nl = 0; |
| 537 |
uint32 nh = 0; |
| 538 |
|
| 539 |
// If they intersect, make a new region |
| 540 |
// |
| 541 |
if ((al <= bl) && (bl < ah)) { |
| 542 |
nl = bl; |
| 543 |
nh = (ah < bh) ? ah : bh; |
| 544 |
} |
| 545 |
|
| 546 |
if ((bl <= al) && (al < bh)) { |
| 547 |
nl = al; |
| 548 |
nh = (ah < bh) ? ah : bh; |
| 549 |
} |
| 550 |
|
| 551 |
if (nl < nh) |
| 552 |
add(nl, nh - nl); |
| 553 |
|
| 554 |
// Advance the list with the earlier region. |
| 555 |
// |
| 556 |
if (ah < bh) { |
| 557 |
// A ends before B |
| 558 |
ai++; |
| 559 |
} else if (ah > bh) { |
| 560 |
// B ends before A |
| 561 |
bi++; |
| 562 |
} else { |
| 563 |
// Exactly the same ending! |
| 564 |
ai++; |
| 565 |
bi++; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
void |
| 571 |
intervalList::contained(intervalList &A, |
| 572 |
intervalList &B) { |
| 573 |
A.merge(); |
| 574 |
B.merge(); |
| 575 |
|
| 576 |
uint32 ai = 0; |
| 577 |
uint32 bi = 0; |
| 578 |
|
| 579 |
while ((ai < A.numberOfIntervals()) && |
| 580 |
(bi < B.numberOfIntervals())) { |
| 581 |
uint32 al = A.lo(ai); |
| 582 |
uint32 ah = A.hi(ai); |
| 583 |
uint32 bl = B.lo(bi); |
| 584 |
uint32 bh = B.hi(bi); |
| 585 |
|
| 586 |
// If A is contained in B, make a new region. |
| 587 |
// |
| 588 |
if ((bl <= al) && (ah <= bh)) |
| 589 |
add(bl, bh - bl); |
| 590 |
|
| 591 |
#if 0 |
| 592 |
if ((al <= bl) && (bh <= ah)) |
| 593 |
add(al, ah - al); |
| 594 |
#endif |
| 595 |
|
| 596 |
// Advance the list with the earlier region. |
| 597 |
// |
| 598 |
if (ah < bh) { |
| 599 |
// A ends before B |
| 600 |
ai++; |
| 601 |
} else if (ah > bh) { |
| 602 |
// B ends before A |
| 603 |
bi++; |
| 604 |
} else { |
| 605 |
// Exactly the same ending! |
| 606 |
ai++; |
| 607 |
bi++; |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
static |
| 618 |
int |
| 619 |
intervalDepth_sort_helper(const void *a, const void *b) { |
| 620 |
_intervalDepth *A = (_intervalDepth *)a; |
| 621 |
_intervalDepth *B = (_intervalDepth *)b; |
| 622 |
|
| 623 |
if (A->lo < B->lo) return(-1); |
| 624 |
if (A->lo > B->lo) return(1); |
| 625 |
return(0); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
intervalDepth::intervalDepth(intervalList &IL) { |
| 630 |
|
| 631 |
uint32 idlen = IL.numberOfIntervals() * 2; |
| 632 |
_intervalDepth *id = new _intervalDepth [idlen]; |
| 633 |
|
| 634 |
for (uint32 i=0; i<IL.numberOfIntervals(); i++) { |
| 635 |
id[2*i ].lo = IL.lo(i); |
| 636 |
id[2*i ].hi = 0; |
| 637 |
id[2*i ].de = 1; |
| 638 |
id[2*i+1].lo = IL.hi(i); |
| 639 |
id[2*i+1].hi = 0; |
| 640 |
id[2*i+1].de = 0; |
| 641 |
} |
| 642 |
|
| 643 |
qsort(id, idlen, sizeof(_intervalDepth), intervalDepth_sort_helper); |
| 644 |
|
| 645 |
// Scan the list, counting how many times we change depth. |
| 646 |
// |
| 647 |
_listMax = 1; |
| 648 |
for (uint32 i=1; i<idlen; i++) { |
| 649 |
if (id[i-1].lo != id[i].lo) |
| 650 |
_listMax++; |
| 651 |
} |
| 652 |
|
| 653 |
// Allocate the real depth of coverage intervals |
| 654 |
// |
| 655 |
_listLen = 0; |
| 656 |
_list = new _intervalDepth [_listMax]; |
| 657 |
|
| 658 |
// Build new intervals |
| 659 |
// |
| 660 |
// Initialize the first interval |
| 661 |
// |
| 662 |
_list[_listLen].lo = id[0].lo; |
| 663 |
_list[_listLen].hi = id[0].lo; |
| 664 |
_list[_listLen].de = 1; |
| 665 |
|
| 666 |
for (uint32 i=1; i<idlen; i++) { |
| 667 |
|
| 668 |
if (_list[_listLen].de == 0) { |
| 669 |
// Update the start position if the current interval is at zero |
| 670 |
// depth. |
| 671 |
// |
| 672 |
_list[_listLen].lo = id[i].lo; |
| 673 |
} else { |
| 674 |
|
| 675 |
// If we are at a position different from the start, we need to |
| 676 |
// close out the current interval and make a new one. |
| 677 |
// |
| 678 |
if (id[i-1].lo != id[i].lo) { |
| 679 |
_list[_listLen].hi = id[i].lo; |
| 680 |
|
| 681 |
_listLen++; |
| 682 |
|
| 683 |
_list[_listLen].lo = id[i].lo; |
| 684 |
_list[_listLen].hi = id[i].lo; |
| 685 |
_list[_listLen].de = _list[_listLen-1].de; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
// Finally, update the depth of the current interval |
| 690 |
// |
| 691 |
if (id[i].de) |
| 692 |
_list[_listLen].de++; |
| 693 |
else |
| 694 |
_list[_listLen].de--; |
| 695 |
} |
| 696 |
|
| 697 |
// Toss out the last one if it's zero length -- I think it's always |
| 698 |
// zero length, just can convince myself. |
| 699 |
// |
| 700 |
if (_list[_listLen].lo == _list[_listLen].hi) |
| 701 |
_listLen--; |
| 702 |
|
| 703 |
delete [] id; |
| 704 |
} |
| 705 |
|
| 706 |
intervalDepth::~intervalDepth() { |
| 707 |
delete [] _list; |
| 708 |
} |
| 709 |
|
| 710 |
// |
| 711 |
// End code from Bri's libutil/util++.H |
| 712 |
// |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
void |
| 717 |
readDepth(char *depthname, map<uint64,intervalDepth*> &lowCoverage) { |
| 718 |
char line[1024] = {0}; |
| 719 |
map<uint64,intervalList*> ILs; |
| 720 |
|
| 721 |
fprintf(stderr, "Reading depth from '%s'\n", depthname); |
| 722 |
|
| 723 |
errno = 0; |
| 724 |
FILE *F = fopen(depthname, "r"); |
| 725 |
if (errno) |
| 726 |
fprintf(stderr, "failed to open '%s': %s\n", depthname, strerror(errno)), exit(1); |
| 727 |
|
| 728 |
uint32 i=0; |
| 729 |
|
| 730 |
fgets(line, 1024, F); |
| 731 |
while (!feof(F)) { |
| 732 |
splitToWords W(line); |
| 733 |
|
| 734 |
uint64 uid = strtouint64(W[1], 0L); |
| 735 |
uint32 beg = strtouint32(W[2], 0L); |
| 736 |
uint32 end = strtouint32(W[3], 0L); |
| 737 |
|
| 738 |
if (beg > end) |
| 739 |
fprintf(stderr, "ERROR: l="uint32FMT" h="uint32FMT"\n", beg, end); |
| 740 |
|
| 741 |
if (ILs[uid] == 0L) |
| 742 |
ILs[uid] = new intervalList(); |
| 743 |
ILs[uid]->add(beg, end - beg); |
| 744 |
|
| 745 |
i++; |
| 746 |
|
| 747 |
fgets(line, 1024, F); |
| 748 |
} |
| 749 |
|
| 750 |
fclose(F); |
| 751 |
fprintf(stderr, " "uint32FMT" lines.\n", i); |
| 752 |
|
| 753 |
map<uint64,intervalList*>::iterator it = ILs.begin(); |
| 754 |
map<uint64,intervalList*>::iterator ed = ILs.end(); |
| 755 |
|
| 756 |
while (it != ed) { |
| 757 |
lowCoverage[it->first] = new intervalDepth(*it->second); |
| 758 |
delete it->second; |
| 759 |
it->second = 0L; |
| 760 |
it++; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
void |
| 766 |
readVariation(char *depthname, map<uint64,intervalList*> &variation) { |
| 767 |
char line[1024 * 1024] = {0}; |
| 768 |
|
| 769 |
fprintf(stderr, "Reading variation from '%s'\n", depthname); |
| 770 |
|
| 771 |
errno = 0; |
| 772 |
FILE *F = fopen(depthname, "r"); |
| 773 |
if (errno) |
| 774 |
fprintf(stderr, "failed to open '%s': %s\n", depthname, strerror(errno)), exit(1); |
| 775 |
|
| 776 |
uint32 i=0; |
| 777 |
|
| 778 |
fgets(line, 1024 * 1024, F); |
| 779 |
while (!feof(F)) { |
| 780 |
splitToWords W(line); |
| 781 |
|
| 782 |
uint64 uid = strtouint64(W[1], 0L); |
| 783 |
uint32 beg = strtouint32(W[2], 0L); |
| 784 |
uint32 end = strtouint32(W[3], 0L); |
| 785 |
|
| 786 |
if (variation[uid] == 0L) |
| 787 |
variation[uid] = new intervalList(); |
| 788 |
variation[uid]->add(beg, end - beg); |
| 789 |
|
| 790 |
i++; |
| 791 |
|
| 792 |
fgets(line, 1024 * 1024, F); |
| 793 |
} |
| 794 |
|
| 795 |
fclose(F); |
| 796 |
fprintf(stderr, " "uint32FMT" lines.\n", i); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
void |
| 801 |
readBadMers(char *depthname, map<uint64,intervalList*> &badMers) { |
| 802 |
char line[1024] = {0}; |
| 803 |
|
| 804 |
fprintf(stderr, "Reading badMers from '%s'\n", depthname); |
| 805 |
|
| 806 |
errno = 0; |
| 807 |
FILE *F = fopen(depthname, "r"); |
| 808 |
if (errno) |
| 809 |
fprintf(stderr, "failed to open '%s': %s\n", depthname, strerror(errno)), exit(1); |
| 810 |
|
| 811 |
uint32 i=0; |
| 812 |
|
| 813 |
fgets(line, 1024, F); |
| 814 |
while (!feof(F)) { |
| 815 |
splitToWords W(line); |
| 816 |
|
| 817 |
// Change every non-digit to a space in the first word. |
| 818 |
for (uint32 z=strlen(W[0])-1; z--; ) |
| 819 |
if (!isdigit(W[0][z])) |
| 820 |
W[0][z] = ' '; |
| 821 |
|
| 822 |
uint64 uid = strtouint64(W[0], 0L); |
| 823 |
uint32 beg = strtouint32(W[3], 0L); |
| 824 |
uint32 end = strtouint32(W[4], 0L); |
| 825 |
|
| 826 |
if (badMers[uid] == 0L) |
| 827 |
badMers[uid] = new intervalList(); |
| 828 |
badMers[uid]->add(beg, end - beg); |
| 829 |
|
| 830 |
i++; |
| 831 |
|
| 832 |
fgets(line, 1024, F); |
| 833 |
} |
| 834 |
|
| 835 |
fclose(F); |
| 836 |
fprintf(stderr, " "uint32FMT" lines.\n", i); |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
int |
| 842 |
main(int argc, char **argv) { |
| 843 |
map<uint64,intervalList*> badMers; |
| 844 |
map<uint64,intervalList*> variation; |
| 845 |
map<uint64,intervalDepth*> lowCoverage; |
| 846 |
|
| 847 |
bool showDepthIntersect = false; |
| 848 |
bool showVariantIntersect = false; |
| 849 |
bool showVarDepthIntersect = false; |
| 850 |
|
| 851 |
int arg=1; |
| 852 |
while (arg < argc) { |
| 853 |
if (strcmp(argv[arg], "-D") == 0) { |
| 854 |
|
| 855 |
} else if (strcmp(argv[arg], "-pd") == 0) { |
| 856 |
showDepthIntersect = true; |
| 857 |
} else if (strcmp(argv[arg], "-pv") == 0) { |
| 858 |
showVariantIntersect = true; |
| 859 |
} else if (strcmp(argv[arg], "-pvd") == 0) { |
| 860 |
showVarDepthIntersect = true; |
| 861 |
} else { |
| 862 |
fprintf(stderr, "usage: %s [-D debugfile] [-pd] [-pv] [-pvd]\n", argv[0]); |
| 863 |
fprintf(stderr, " -pd print bad mers regions isect depth\n"); |
| 864 |
fprintf(stderr, " -pv print bad mers regions isect variants\n"); |
| 865 |
fprintf(stderr, " -pvd print bad mers regions isect both variants and depth\n"); |
| 866 |
exit(1); |
| 867 |
} |
| 868 |
arg++; |
| 869 |
} |
| 870 |
|
| 871 |
#if 1 |
| 872 |
// HuRef6, in the assembly directory. |
| 873 |
// |
| 874 |
readDepth ("/project/huref6/assembly/h6/9-terminator/h6.posmap.frgctg", lowCoverage); |
| 875 |
readVariation("/project/huref6/assembly/h6/9-terminator/h6.posmap.varctg", variation); |
| 876 |
readBadMers ("/project/huref6/assembly/h6-mer-validation/h6-ms22-allfrags-normalcontigs.badmers.0.singlecontig.zerofrag.badmers", badMers); |
| 877 |
#endif |
| 878 |
|
| 879 |
#if 0 |
| 880 |
// HuRef6, ws=25, in the assembly directory. |
| 881 |
// |
| 882 |
readDepth ("/project/huref6/assembly/h6/9-terminator-ws25/h6.posmap.frgctg", lowCoverage); |
| 883 |
readVariation("/project/huref6/assembly/h6/9-terminator-ws25/h6.posmap.varctg", variation); |
| 884 |
readBadMers ("/project/huref6/assembly/h6-mer-validation/h6-version4-ws25/h6-ms22-allfrags-normalcontigs.badmers.0.singlecontig.zerofrag.badmers", badMers); |
| 885 |
#endif |
| 886 |
|
| 887 |
#if 0 |
| 888 |
// Our scratch huref |
| 889 |
// |
| 890 |
readDepth ("/project/huref6/redo_consensus-gennady/mer-validation/h6tmp.posmap.frgctg", lowCoverage); |
| 891 |
readVariation("/project/huref6/redo_consensus-gennady/mer-validation/h6tmp.posmap.varctg", variation); |
| 892 |
readBadMers ("/project/huref6/redo_consensus-gennady/mer-validation/h6tmp-ms22-allfrags-allcontigs.badmers.0.singlecontig.zerofrag.badmers", badMers); |
| 893 |
#endif |
| 894 |
|
| 895 |
uint32 badBegDepth[1024] = {0}; |
| 896 |
uint32 badEndDepth[1024] = {0}; |
| 897 |
|
| 898 |
uint32 badDepth[32][32]; |
| 899 |
for (uint32 i=0; i<32; i++) |
| 900 |
for (uint32 j=0; j<32; j++) |
| 901 |
badDepth[i][j] = 0; |
| 902 |
|
| 903 |
map<uint64,intervalList*>::iterator it = badMers.begin(); |
| 904 |
map<uint64,intervalList*>::iterator ed = badMers.end(); |
| 905 |
while (it != ed) { |
| 906 |
uint64 uid = it->first; |
| 907 |
|
| 908 |
intervalList *Iv = variation[uid]; |
| 909 |
intervalList *Ib = badMers[uid]; |
| 910 |
intervalList *Ii = 0L; |
| 911 |
intervalDepth *Id = lowCoverage[uid]; |
| 912 |
|
| 913 |
if (Iv) |
| 914 |
Iv->merge(); |
| 915 |
if (Ib) |
| 916 |
Ib->merge(); |
| 917 |
|
| 918 |
if (Iv && Ib) { |
| 919 |
Ii = new intervalList(); |
| 920 |
Ii->intersect(*Iv, *Ib); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
if (Ii) { |
| 925 |
uint32 ii = 0; |
| 926 |
uint32 id = 0; |
| 927 |
|
| 928 |
while ((ii < Ii->numberOfIntervals()) && |
| 929 |
(id < Id->numberOfIntervals())) { |
| 930 |
|
| 931 |
// We want to count the number of times a badmer region |
| 932 |
// begins/ends in some depth. |
| 933 |
|
| 934 |
//fprintf(stderr, "testing beg "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT"\n", |
| 935 |
// Ii->lo(ii), Ii->hi(ii), Id->lo(id), Id->hi(id)); |
| 936 |
|
| 937 |
uint32 beg = 0; |
| 938 |
uint32 end = 0; |
| 939 |
|
| 940 |
// Low points are not allowed to be equal to high points, skip to the next |
| 941 |
while ((id < Id->numberOfIntervals()) && |
| 942 |
(Id->hi(id) <= Ii->lo(ii))) { |
| 943 |
id++; |
| 944 |
//fprintf(stderr, "testing beg (m) "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT"\n", |
| 945 |
// Ii->lo(ii), Ii->hi(ii), Id->lo(id), Id->hi(id)); |
| 946 |
} |
| 947 |
if (id < Id->numberOfIntervals()) { |
| 948 |
uint32 lo = Id->lo(id); |
| 949 |
uint32 hi = Id->hi(id); |
| 950 |
|
| 951 |
// Low points are not allowed to be equal to high points. |
| 952 |
if ((lo <= Ii->lo(ii)) && (Ii->lo(ii) < hi)) { |
| 953 |
beg = Id->de(id); |
| 954 |
} else { |
| 955 |
fprintf(stderr, "failed to find begin "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT" "uint32FMT"\n", |
| 956 |
Ii->lo(ii), Ii->hi(ii), Id->lo(id), Id->hi(id), Id->de(id)); |
| 957 |
if (id > 0) |
| 958 |
fprintf(stderr, " "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT" "uint32FMT"\n", |
| 959 |
Ii->lo(ii), Ii->hi(ii), Id->lo(id-1), Id->hi(id-1), Id->de(id-1)); |
| 960 |
//exit(1); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
//fprintf(stderr, "testing end "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT"\n", |
| 965 |
// Ii->lo(ii), Ii->hi(ii), Id->lo(id), Id->hi(id)); |
| 966 |
|
| 967 |
// High points can be equal. |
| 968 |
while ((id < Id->numberOfIntervals()) && |
| 969 |
(Id->hi(id) < Ii->hi(ii))) { |
| 970 |
id++; |
| 971 |
//fprintf(stderr, "testing end (m) "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT"\n", |
| 972 |
// Ii->lo(ii), Ii->hi(ii), Id->lo(id), Id->hi(id)); |
| 973 |
} |
| 974 |
if (id < Id->numberOfIntervals()) { |
| 975 |
uint32 lo = Id->lo(id); |
| 976 |
uint32 hi = Id->hi(id); |
| 977 |
|
| 978 |
// High points aren't allowed to be equal to lo, but can be equal to hi. |
| 979 |
if ((lo < Ii->hi(ii)) && (Ii->hi(ii) <= hi)) { |
| 980 |
end = Id->de(id); |
| 981 |
} else { |
| 982 |
fprintf(stderr, "failed to find end "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT" "uint32FMT"\n", |
| 983 |
Ii->lo(ii), Ii->hi(ii), Id->lo(id), Id->hi(id), Id->de(id)); |
| 984 |
if (id > 0) |
| 985 |
fprintf(stderr, " "uint32FMT" "uint32FMT" -- "uint32FMT" "uint32FMT" "uint32FMT"\n", |
| 986 |
Ii->lo(ii), Ii->hi(ii), Id->lo(id-1), Id->hi(id-1), Id->de(id-1)); |
| 987 |
//exit(1); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
badBegDepth[beg]++; |
| 992 |
badEndDepth[end]++; |
| 993 |
|
| 994 |
fprintf(stdout, uint64FMT"\t"uint32FMT"\t"uint32FMT"\tdepth="uint32FMT","uint32FMT"\n", |
| 995 |
uid, Ii->lo(ii), Ii->hi(ii), beg, end); |
| 996 |
|
| 997 |
if ((beg < 32) && (end < 32)) |
| 998 |
badDepth[beg][end]++; |
| 999 |
|
| 1000 |
ii++; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
it++; |
| 1005 |
} |
| 1006 |
|
| 1007 |
uint32 bb = 0; |
| 1008 |
uint32 be = 0; |
| 1009 |
for (uint32 x=0; x<32; x++) { |
| 1010 |
fprintf(stdout, uint32FMT"\t"uint32FMT"\t"uint32FMT"\n", x, badBegDepth[x], badEndDepth[x]); |
| 1011 |
bb += badBegDepth[x]; |
| 1012 |
be += badEndDepth[x]; |
| 1013 |
} |
| 1014 |
fprintf(stdout, "total\t"uint32FMT"\t"uint32FMT"\n", bb, be); |
| 1015 |
|
| 1016 |
for (uint32 i=0; i<30; i++) { |
| 1017 |
for (uint32 j=0; j<30; j++) |
| 1018 |
fprintf(stdout, uint32FMTW(5), badDepth[i][j]); |
| 1019 |
fprintf(stdout, "\n"); |
| 1020 |
} |
| 1021 |
|
| 1022 |
return(0); |
| 1023 |
} |