| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | using namespace std; |
| 5 | #include <map> |
| 6 | |
| 7 | #include "util++.H" |
| 8 | |
| 9 | // Reads a clump-annotated atac file, builds a search tree of all the |
| 10 | // matches in those clumps. Then reads a list of happy clones mapped |
| 11 | // to the sequence, figures out what clump each read in the clone is |
| 12 | // in, and reports whenever the clone spans a clump. |
| 13 | |
| 14 | |
| 15 | // Contains a list of intervalLists, one for each clump. The |
| 16 | // intervalList stores the positions of the matches in this clump. |
| 17 | // |
| 18 | class atacClumpCoordTreeScaffold { |
| 19 | public: |
| 20 | atacClumpCoordTreeScaffold() { |
| 21 | clumpsLen = 0; |
| 22 | clumpsMax = 64; |
| 23 | clumpID = new uint32 [clumpsMax]; |
| 24 | clumps = new intervalList<uint64> * [clumpsMax]; |
| 25 | clumpmin = new uint32 [clumpsMax]; |
| 26 | clumpmax = new uint32 [clumpsMax]; |
| 27 | |
| 28 | clumpconfirm = new uint32 [clumpsMax * clumpsMax]; |
| 29 | |
| 30 | for (uint32 i=0; i<clumpsMax * clumpsMax; i++) |
| 31 | clumpconfirm[i] = 0; |
| 32 | |
| 33 | intervalsLen = 0; |
| 34 | intervalsMax = 0; |
| 35 | intervals = 0L; |
| 36 | }; |
| 37 | |
| 38 | ~atacClumpCoordTreeScaffold() { |
| 39 | for (uint32 i=0; i<clumpsLen; i++) |
| 40 | delete clumps[i]; |
| 41 | delete [] clumpID; |
| 42 | delete [] clumps; |
| 43 | delete [] clumpmin; |
| 44 | delete [] clumpmax; |
| 45 | delete [] clumpconfirm; |
| 46 | delete [] intervals; |
| 47 | }; |
| 48 | |
| 49 | // Add a match to some clump. |
| 50 | // |
| 51 | void addMatch(int32 clumpid, uint32 begin, uint32 length) { |
| 52 | |
| 53 | // Not in a clump, get the heck outta here! |
| 54 | // |
| 55 | if (clumpid < 0) |
| 56 | return; |
| 57 | |
| 58 | // Linear search through the clumps to find the correct id, we |
| 59 | // don't expect to have many clumps per scaffold. |
| 60 | // |
| 61 | for (uint32 i=0; i<clumpsLen; i++) { |
| 62 | if (clumpID[i] == (uint32)clumpid) { |
| 63 | clumps[i]->add(begin, length); |
| 64 | if (clumpmin[i] > begin) |
| 65 | clumpmin[i] = begin; |
| 66 | if (clumpmax[i] < begin + length) |
| 67 | clumpmax[i] = begin + length; |
| 68 | return; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (clumpsLen == clumpsMax) { |
| 73 | fprintf(stderr, "ERROR: increase clumpsMax!\n"); |
| 74 | exit(1); |
| 75 | } |
| 76 | |
| 77 | // Didn't add to an existing clump, so must be a new clump. |
| 78 | // |
| 79 | clumpID[clumpsLen] = clumpid; |
| 80 | clumps[clumpsLen] = new intervalList<uint64>; |
| 81 | clumps[clumpsLen]->add(begin, length); |
| 82 | clumpmin[clumpsLen] = begin; |
| 83 | clumpmax[clumpsLen] = begin + length; |
| 84 | clumpsLen++; |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | uint32 getClumpID(uint32 begin, uint32 end) { |
| 89 | uint32 clumpid = 0; |
| 90 | uint32 numhits = 0; |
| 91 | |
| 92 | // We can make this much quicker if we remember the extent of |
| 93 | // each interval list. |
| 94 | // |
| 95 | // We want to allow partial matches, so check that the end is |
| 96 | // above the min, and the begin is before the max. |
| 97 | // |
| 98 | // b-------e b-----e |
| 99 | // -------clump------ |
| 100 | // |
| 101 | for (uint32 i=0; i<clumpsLen; i++) { |
| 102 | if ((clumpmin[i] <= end) && (begin <= clumpmax[i])) { |
| 103 | if (clumps[i]->overlapping(begin, end, intervals, intervalsLen, intervalsMax) > 0) { |
| 104 | clumpid = clumpID[i]; |
| 105 | numhits++; |
| 106 | } |
| 107 | } else { |
| 108 | // If you really want to check.... |
| 109 | //if (clumps[i]->overlapping(begin, end, intervals, intervalsLen, intervalsMax) > 0) |
| 110 | // fprintf(stderr, "WARNING: Found overlapping clump outside extent!\n"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (numhits == 0) |
| 115 | return(0); |
| 116 | if (numhits == 1) |
| 117 | return(clumpid); |
| 118 | |
| 119 | //fprintf(stderr, "FOUND MORE THAN ONE CLUMP MATCHING!\n"); |
| 120 | return(~uint32ZERO); |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | void sortClumps(void) { |
| 125 | uint32 ciid; |
| 126 | intervalList<uint64> *cptr; |
| 127 | uint32 cmin; |
| 128 | uint32 cmax; |
| 129 | |
| 130 | uint32 i = 0; |
| 131 | uint32 j = 0; |
| 132 | |
| 133 | // an insertion sort |
| 134 | |
| 135 | for (i=clumpsLen; i--; ) { |
| 136 | ciid = clumpID[i]; |
| 137 | cptr = clumps[i]; |
| 138 | cmin = clumpmin[i]; |
| 139 | cmax = clumpmax[i]; |
| 140 | |
| 141 | for (j=i+1; (j < clumpsLen) && (cmin > clumpmin[j]); j++) { |
| 142 | clumpID[j-1] = clumpID[j]; |
| 143 | clumps[j-1] = clumps[j]; |
| 144 | clumpmin[j-1] = clumpmin[j]; |
| 145 | clumpmax[j-1] = clumpmax[j]; |
| 146 | } |
| 147 | |
| 148 | clumpID[j-1] = ciid; |
| 149 | clumps[j-1] = cptr; |
| 150 | clumpmin[j-1] = cmin; |
| 151 | clumpmax[j-1] = cmax; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | void confirm(uint32 ca, uint32 cb) { |
| 157 | uint32 caidx = 0; |
| 158 | uint32 cbidx = 0; |
| 159 | for (uint32 i=0; i<clumpsLen; i++) { |
| 160 | if (ca == clumpID[i]) |
| 161 | caidx = i; |
| 162 | if (cb == clumpID[i]) |
| 163 | cbidx = i; |
| 164 | } |
| 165 | clumpconfirm[caidx * clumpsMax + cbidx]++; |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | uint32 clumpsLen; |
| 170 | uint32 clumpsMax; |
| 171 | uint32 *clumpID; |
| 172 | intervalList<uint64> **clumps; |
| 173 | uint32 *clumpmin; |
| 174 | uint32 *clumpmax; |
| 175 | |
| 176 | uint32 *clumpconfirm; |
| 177 | |
| 178 | uint32 intervalsLen; |
| 179 | uint32 intervalsMax; |
| 180 | uint32 *intervals; |
| 181 | }; |
| 182 | |
| 183 | |
| 184 | class atacClumpCoordTree { |
| 185 | public: |
| 186 | atacClumpCoordTree() { |
| 187 | scaffoldsMax = 262144; |
| 188 | scaffolds = new atacClumpCoordTreeScaffold * [scaffoldsMax]; |
| 189 | for (uint32 i=0; i<scaffoldsMax; i++) |
| 190 | scaffolds[i] = 0L; |
| 191 | }; |
| 192 | ~atacClumpCoordTree() { |
| 193 | for (uint32 i=0; i<scaffoldsMax; i++) |
| 194 | if (scaffolds[i]) |
| 195 | delete scaffolds[i]; |
| 196 | delete [] scaffolds; |
| 197 | }; |
| 198 | |
| 199 | |
| 200 | void addMatch(uint32 scaffoldid, int32 clumpid, uint32 begin, uint32 length) { |
| 201 | if (scaffoldid >= scaffoldsMax) { |
| 202 | fprintf(stderr, "ERROR: increase scaffoldsMax "uint32FMT"\n", scaffoldid); |
| 203 | exit(1); |
| 204 | } |
| 205 | |
| 206 | if (scaffolds[scaffoldid] == 0L) |
| 207 | scaffolds[scaffoldid] = new atacClumpCoordTreeScaffold; |
| 208 | |
| 209 | scaffolds[scaffoldid]->addMatch(clumpid, begin, length); |
| 210 | }; |
| 211 | |
| 212 | |
| 213 | void removeSingleClumpScaffolds(void) { |
| 214 | uint32 deleted = 0; |
| 215 | uint32 remain = 0; |
| 216 | |
| 217 | |
| 218 | for (uint32 i=0; i<scaffoldsMax; i++) { |
| 219 | if ((scaffolds[i]) && (scaffolds[i]->clumpsLen < 2)) { |
| 220 | delete scaffolds[i]; |
| 221 | scaffolds[i] = 0L; |
| 222 | deleted++; |
| 223 | } |
| 224 | if (scaffolds[i]) { |
| 225 | scaffolds[i]->sortClumps(); |
| 226 | remain++; |
| 227 | } |
| 228 | } |
| 229 | fprintf(stderr, "Deleted "uint32FMT" scaffolds with less than 2 clumps.\n", deleted); |
| 230 | fprintf(stderr, "Remain "uint32FMT" scaffolds with more than 2 clumps.\n", remain); |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | void showMultipleClumpScaffolds(void) { |
| 235 | |
| 236 | for (uint32 i=0; i<scaffoldsMax; i++) { |
| 237 | if ((scaffolds[i]) && (scaffolds[i]->clumpsLen >= 2)) { |
| 238 | |
| 239 | fprintf(stdout, "\n"); |
| 240 | |
| 241 | for (uint32 j=0; j<scaffolds[i]->clumpsLen; j++) { |
| 242 | bool overlap = false; |
| 243 | |
| 244 | if ((j+1 < scaffolds[i]->clumpsLen) && |
| 245 | (scaffolds[i]->clumpmax[j] > scaffolds[i]->clumpmin[j+1])) |
| 246 | overlap = true; |
| 247 | |
| 248 | fprintf(stdout, "scaffold "uint32FMT" clump "uint32FMT" begin "uint32FMT" end "uint32FMT"\n", |
| 249 | i, |
| 250 | scaffolds[i]->clumpID[j], |
| 251 | scaffolds[i]->clumpmin[j], |
| 252 | scaffolds[i]->clumpmax[j]); |
| 253 | |
| 254 | if (overlap) |
| 255 | fprintf(stdout, "scaffold "uint32FMT" clump "uint32FMT" and clump "uint32FMT" OVERLAP\n", |
| 256 | i, |
| 257 | scaffolds[i]->clumpID[j], |
| 258 | scaffolds[i]->clumpID[j+1]); |
| 259 | |
| 260 | for (uint32 b=0; b<scaffolds[i]->clumpsLen; b++) { |
| 261 | uint32 cc = j * scaffolds[i]->clumpsMax + b; |
| 262 | if (scaffolds[i]->clumpconfirm[cc]) { |
| 263 | fprintf(stdout, "scaffold "uint32FMT" clump "uint32FMT" and "uint32FMT" confirmed by "uint32FMT" clones.\n", |
| 264 | i, |
| 265 | scaffolds[i]->clumpID[j], |
| 266 | scaffolds[i]->clumpID[b], |
| 267 | scaffolds[i]->clumpconfirm[cc]); |
| 268 | |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | uint32 getClumpID(uint32 scaffoldid, uint32 begin, uint32 end) { |
| 278 | if (scaffolds[scaffoldid]) |
| 279 | return(scaffolds[scaffoldid]->getClumpID(begin, end)); |
| 280 | return(0); |
| 281 | }; |
| 282 | |
| 283 | void confirmClump(uint32 scaffoldid, uint32 ca, uint32 cb) { |
| 284 | if (scaffolds[scaffoldid]) { |
| 285 | scaffolds[scaffoldid]->confirm(ca, cb); |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | |
| 290 | uint32 scaffoldsMax; |
| 291 | atacClumpCoordTreeScaffold **scaffolds; |
| 292 | }; |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | atacClumpCoordTree* |
| 299 | buildCoordTree(char *clumpFile) { |
| 300 | atacClumpCoordTree *ct = new atacClumpCoordTree; |
| 301 | |
| 302 | FILE *inf; |
| 303 | char inl[1024]; |
| 304 | |
| 305 | // We can't use the built-in atac reader, because it strips out |
| 306 | // clump information. Bummer. |
| 307 | |
| 308 | errno = 0; |
| 309 | inf = fopen(clumpFile, "r"); |
| 310 | if (errno) |
| 311 | fprintf(stderr, "Failed to open '%s': %s\n", clumpFile, strerror(errno)), exit(1); |
| 312 | |
| 313 | fgets(inl, 1024, inf); |
| 314 | |
| 315 | if (feof(inf)) |
| 316 | return(0L); |
| 317 | |
| 318 | while (!feof(inf)) { |
| 319 | if ((inl[0] == 'M') && |
| 320 | (inl[2] == 'u')) { |
| 321 | splitToWords S(inl); |
| 322 | |
| 323 | //fprintf(stderr, "%s", inl); |
| 324 | |
| 325 | if (S[12][0] != '#') |
| 326 | fprintf(stderr, "no clump for '%s'\n", inl); |
| 327 | |
| 328 | if (S[13][0] != '-') { |
| 329 | char *scfid = S[8]; |
| 330 | while (*scfid != ':') |
| 331 | scfid++; |
| 332 | |
| 333 | ct->addMatch(atoi(scfid + 1), |
| 334 | atoi(S[13]), |
| 335 | atoi(S[9]), |
| 336 | atoi(S[10])); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | fgets(inl, 1024, inf); |
| 341 | } |
| 342 | |
| 343 | fclose(inf); |
| 344 | |
| 345 | return(ct); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | int |
| 356 | main(int argc, char **argv) { |
| 357 | char *clumpFile = 0L; |
| 358 | char *happyFile = 0L; |
| 359 | |
| 360 | int arg=1; |
| 361 | int err=0; |
| 362 | while (arg < argc) { |
| 363 | if (strcmp(argv[arg], "-clumps") == 0) { |
| 364 | clumpFile = argv[++arg]; |
| 365 | } else if (strcmp(argv[arg], "-happy") == 0) { |
| 366 | happyFile = argv[++arg]; |
| 367 | } else { |
| 368 | err++; |
| 369 | } |
| 370 | |
| 371 | arg++; |
| 372 | } |
| 373 | |
| 374 | if (clumpFile == 0L) |
| 375 | fprintf(stderr, "No -clumps supplied!\n"), err++; |
| 376 | if (happyFile == 0L) |
| 377 | fprintf(stderr, "No -happy clones supplied!\n"), err++; |
| 378 | if (err) |
| 379 | fprintf(stderr, "usage: %s ...\n", argv[0]), exit(1); |
| 380 | |
| 381 | atacClumpCoordTree *ct = buildCoordTree(clumpFile); |
| 382 | |
| 383 | ct->removeSingleClumpScaffolds(); |
| 384 | ct->showMultipleClumpScaffolds(); |
| 385 | |
| 386 | //////////////////////////////////////// |
| 387 | // |
| 388 | // ugly hack -- read in the map from HUREF6A UID to scaffold |
| 389 | // |
| 390 | map<uint64,uint32> UIDtoIID; |
| 391 | |
| 392 | { |
| 393 | char *uidmapName = "/project/huref6/assembly/fasta/HUREF6A.info"; |
| 394 | |
| 395 | errno = 0; |
| 396 | FILE *F = fopen(uidmapName, "r"); |
| 397 | if (errno) |
| 398 | fprintf(stderr, "Failed to open '%s': %s\n", uidmapName, strerror(errno)), exit(1); |
| 399 | |
| 400 | char L[1024]; |
| 401 | |
| 402 | fgets(L, 1024, F); |
| 403 | while (!feof(F)) { |
| 404 | if (L[0] == 'G') { |
| 405 | splitToWords S(L); |
| 406 | |
| 407 | UIDtoIID[strtouint64(S[13]+1, 0L)] = strtouint32(S[10], 0L); |
| 408 | } |
| 409 | fgets(L, 1024, F); |
| 410 | } |
| 411 | } |
| 412 | // |
| 413 | //////////////////////////////////////// |
| 414 | |
| 415 | FILE *inf; |
| 416 | char ina[1024]; |
| 417 | char inb[1024]; |
| 418 | |
| 419 | speedCounter S("%9.0f clones (%6.1f clones/sec)\r", 1, 4096, true); |
| 420 | |
| 421 | errno = 0; |
| 422 | inf = fopen(happyFile, "r"); |
| 423 | if (errno) |
| 424 | fprintf(stderr, "Failed to open '%s': %s\n", happyFile, strerror(errno)), exit(1); |
| 425 | |
| 426 | fgets(ina, 1024, inf); chomp(ina); |
| 427 | fgets(inb, 1024, inf); chomp(inb); |
| 428 | while (!feof(inf)) { |
| 429 | splitToWords A(ina); |
| 430 | splitToWords B(inb); |
| 431 | |
| 432 | // Some sanity checking. |
| 433 | if (strcmp(A[2], B[2]) != 0) { |
| 434 | fprintf(stderr, "ERROR: Different clone!\n%s\n%s\n", ina, inb); |
| 435 | } |
| 436 | |
| 437 | |
| 438 | uint32 scfa = UIDtoIID[strtouint64(A[7], 0L)]; |
| 439 | uint32 scfb = UIDtoIID[strtouint64(B[7], 0L)]; |
| 440 | |
| 441 | uint32 cla = ct->getClumpID(scfa, |
| 442 | atoi(A[8]), |
| 443 | atoi(A[9])); |
| 444 | uint32 clb = ct->getClumpID(scfb, |
| 445 | atoi(B[8]), |
| 446 | atoi(B[9])); |
| 447 | |
| 448 | if (cla == ~uint32ZERO) { |
| 449 | fprintf(stdout, "%s spans clump in scaffold %s,"uint32FMT"\n", ina, A[7], scfa); |
| 450 | cla = 0; |
| 451 | } |
| 452 | if (clb == ~uint32ZERO) { |
| 453 | fprintf(stdout, "%s spans clump in scaffold %s,"uint32FMT" \n", inb, B[7], scfb); |
| 454 | clb = 0; |
| 455 | } |
| 456 | |
| 457 | if ((cla != 0) && |
| 458 | (clb != 0) && |
| 459 | (cla != clb)) { |
| 460 | ct->confirmClump(scfa, |
| 461 | (cla < clb) ? cla : clb, |
| 462 | (cla < clb) ? clb : cla); |
| 463 | |
| 464 | fprintf(stdout, "scaffold %s,"uint32FMT" clump "uint32FMT" "uint32FMT" confirmed by %s\n", |
| 465 | A[7], scfa, |
| 466 | (cla < clb) ? cla : clb, |
| 467 | (cla < clb) ? clb : cla, |
| 468 | A[2]); |
| 469 | } |
| 470 | |
| 471 | S.tick(); |
| 472 | |
| 473 | fgets(ina, 1024, inf); chomp(ina); |
| 474 | fgets(inb, 1024, inf); chomp(inb); |
| 475 | } |
| 476 | |
| 477 | fclose(inf); |
| 478 | |
| 479 | S.finish(); |
| 480 | |
| 481 | ct->showMultipleClumpScaffolds(); |
| 482 | |
| 483 | delete ct; |
| 484 | } |