| 1 | #cxx . . $ `<file>/seagen/sortHits.C`/include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
| 6 | #include "aHit.H"cxx . . $ `<file>/seagen/aHit.H`/ |
| 7 | #include "bio++.H"cxx . . $ `<file>/libbio/bio++.H`/ |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | // Command line options. Only tmpPath needs to be global, and it can |
| 13 | // be easily localized. |
| 14 | // |
| 15 | bool beVerbosecxx . . $ beVerbose. = false; |
| 16 | uint64cxx . . $ uint64# memoryLimitcxx . . $ memoryLimit. = 128 * 1024 * 1024; |
| 17 | char *tmpPathcxx . . $ tmpPath. = 0L; |
| 18 | |
| 19 | |
| 20 | |
| 21 | class aHitReadercxx . . $ aHitReader# { |
| 22 | public: |
| 23 | |
| 24 | // Open the file for reading, testing if it's binary or ascii input |
| 25 | // |
| 26 | aHitReadercxx . . $ aHitReader#aHitReader(fda57c8427c60377).(char *filename) { |
| 27 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 28 | theFilecxx . . $ aHitReader#theFile. = fopen(filename, "r"); |
| 29 | if (theFilecxx . . $ aHitReader#theFile. == 0L) { |
| 30 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "sortHits-- ERROR opening '%s': %s\n", filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 31 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 32 | } |
| 33 | |
| 34 | char x = (char)fgetc(theFilecxx . . $ aHitReader#theFile.); |
| 35 | ungetc(x, theFilecxx . . $ aHitReader#theFile.); |
| 36 | |
| 37 | isBinarycxx . . $ aHitReader#isBinary. = (x != '-'); |
| 38 | |
| 39 | if (!isBinarycxx . . $ aHitReader#isBinary.) |
| 40 | buffercxx . . $ aHitReader#buffer. = new char [1024]; |
| 41 | }; |
| 42 | |
| 43 | ~cxx . . $ aHitReader#`~aHitReader`(ced63f7c635d850d).aHitReadercxx . . $ aHitReader#() { |
| 44 | fclose(theFilecxx . . $ aHitReader#theFile.); |
| 45 | delete [] buffercxx . . $ aHitReader#buffer.; |
| 46 | }; |
| 47 | |
| 48 | bool readHitcxx . . $ aHitReader#readHit(6c421a15b799c67b).(aHitcxx . . $ aHit# &hit) { |
| 49 | if (isBinarycxx . . $ aHitReader#isBinary.) { |
| 50 | ahit_readBinarycxx . . $ ahit_readBinary(6c917abaf95665ca).(&hit, theFilecxx . . $ aHitReader#theFile.); |
| 51 | } else { |
| 52 | fgets(buffercxx . . $ aHitReader#buffer., 1024, theFilecxx . . $ aHitReader#theFile.); |
| 53 | ahit_parseStringcxx . . $ ahit_parseString(e8f5be3781ac6c2f).(&hit, buffercxx . . $ aHitReader#buffer.); |
| 54 | } |
| 55 | |
| 56 | return(feof(theFilecxx . . $ aHitReader#theFile.) == false); |
| 57 | }; |
| 58 | private: |
| 59 | FILE *theFilecxx . . $ aHitReader#theFile.; |
| 60 | char *buffercxx . . $ aHitReader#buffer.; |
| 61 | bool isBinarycxx . . $ aHitReader#isBinary.; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | |
| 66 | // Write a bunch of hits to a temporary file (unlink the file after |
| 67 | // it's opened) then allow those hits to be read back in. Doesn't |
| 68 | // need the aHitReader, as we use just the binary format. |
| 69 | // |
| 70 | class aHitTemporarycxx . . $ aHitTemporary# { |
| 71 | public: |
| 72 | aHitTemporarycxx . . $ aHitTemporary#aHitTemporary(2ac9cb14c2725045).(aHitcxx . . $ aHit# *hits, uint32cxx . . $ uint32# hitsLen) { |
| 73 | theFilecxx . . $ aHitTemporary#theFile. = makeTempFile(tmpPathcxx . . $ tmpPath.); |
| 74 | |
| 75 | // XXX: Known bug on Tru64: fwrite() of data blocks > 2GB is broken |
| 76 | |
| 77 | uint32cxx . . $ uint32# outputPos = 0; |
| 78 | uint32cxx . . $ uint32# outputLen = 1024 * 1024 / sizeof(aHitcxx . . $ aHit#); |
| 79 | |
| 80 | while (outputPos < hitsLen) { |
| 81 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 82 | outputPos += fwrite(hits, sizeof(aHitcxx . . $ aHit#), hitsLen, theFilecxx . . $ aHitTemporary#theFile.); |
| 83 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) { |
| 84 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: sortHits()-- Failed to write temporary file: %s\n", strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 85 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 86 | } |
| 87 | |
| 88 | // XXX: do we write one too many? |
| 89 | |
| 90 | if (outputPos + outputLen > hitsLen) |
| 91 | outputLen = hitsLen - outputPos; |
| 92 | } |
| 93 | |
| 94 | rewind(theFilecxx . . $ aHitTemporary#theFile.); |
| 95 | |
| 96 | hitcxx . . $ aHitTemporary#hit.._forwardcxx . . $ aHit#_forward. = false; |
| 97 | hitcxx . . $ aHitTemporary#hit.._mergedcxx . . $ aHit#_merged. = false; |
| 98 | hitcxx . . $ aHitTemporary#hit.._qsIdxcxx . . $ aHit#_qsIdx. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 99 | hitcxx . . $ aHitTemporary#hit.._dsIdxcxx . . $ aHit#_dsIdx. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 100 | hitcxx . . $ aHitTemporary#hit.._dsLocxx . . $ aHit#_dsLo. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 101 | hitcxx . . $ aHitTemporary#hit.._dsHicxx . . $ aHit#_dsHi. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 102 | hitcxx . . $ aHitTemporary#hit.._coveredcxx . . $ aHit#_covered. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 103 | hitcxx . . $ aHitTemporary#hit.._matchedcxx . . $ aHit#_matched. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 104 | hitcxx . . $ aHitTemporary#hit.._numMerscxx . . $ aHit#_numMers. = uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 105 | |
| 106 | nextHitcxx . . $ aHitTemporary#nextHit(49f6e7a06ebc5aa8).(); |
| 107 | }; |
| 108 | |
| 109 | ~cxx . . $ aHitTemporary#`~aHitTemporary`(ced63f7c635d850d).aHitTemporarycxx . . $ aHitTemporary#() { |
| 110 | fclose(theFilecxx . . $ aHitTemporary#theFile.); |
| 111 | }; |
| 112 | |
| 113 | aHitcxx . . $ aHit# *theHitcxx . . $ aHitTemporary#theHit(95840130fd782174).(void) { |
| 114 | return(&hitcxx . . $ aHitTemporary#hit.); |
| 115 | }; |
| 116 | void nextHitcxx . . $ aHitTemporary#nextHit(49f6e7a06ebc5aa8).(void) { |
| 117 | if (hitcxx . . $ aHitTemporary#hit.._qsIdxcxx . . $ aHit#_qsIdx. != ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!) { |
| 118 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 119 | fread(&hitcxx . . $ aHitTemporary#hit., sizeof(aHitcxx . . $ aHit#), 1, theFilecxx . . $ aHitTemporary#theFile.); |
| 120 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) { |
| 121 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: sortHits()-- Failed to read a hit: %s\n", strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 122 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 123 | } |
| 124 | |
| 125 | // If we hit eof, this hit is invalid, and so are all future ones. Set |
| 126 | // hit to be junk. |
| 127 | // |
| 128 | if (feof(theFilecxx . . $ aHitTemporary#theFile.)) { |
| 129 | hitcxx . . $ aHitTemporary#hit.._forwardcxx . . $ aHit#_forward. = false; |
| 130 | hitcxx . . $ aHitTemporary#hit.._mergedcxx . . $ aHit#_merged. = false; |
| 131 | hitcxx . . $ aHitTemporary#hit.._qsIdxcxx . . $ aHit#_qsIdx. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 132 | hitcxx . . $ aHitTemporary#hit.._dsIdxcxx . . $ aHit#_dsIdx. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 133 | hitcxx . . $ aHitTemporary#hit.._dsLocxx . . $ aHit#_dsLo. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 134 | hitcxx . . $ aHitTemporary#hit.._dsHicxx . . $ aHit#_dsHi. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 135 | hitcxx . . $ aHitTemporary#hit.._coveredcxx . . $ aHit#_covered. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 136 | hitcxx . . $ aHitTemporary#hit.._matchedcxx . . $ aHit#_matched. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 137 | hitcxx . . $ aHitTemporary#hit.._numMerscxx . . $ aHit#_numMers. = ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!; |
| 138 | } |
| 139 | } |
| 140 | }; |
| 141 | private: |
| 142 | FILE *theFilecxx . . $ aHitTemporary#theFile.; |
| 143 | aHitcxx . . $ aHit# hitcxx . . $ aHitTemporary#hit.; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | int |
| 152 | hitcmpcxx . . $ hitcmp(9148c8c3ff42c9d8).(const void *a, const void *b) { |
| 153 | aHitcxx . . $ aHit# *A = (aHitcxx . . $ aHit# *)a; |
| 154 | aHitcxx . . $ aHit# *B = (aHitcxx . . $ aHit# *)b; |
| 155 | |
| 156 | if (A->_dsIdxcxx . . $ aHit#_dsIdx. < B->_dsIdxcxx . . $ aHit#_dsIdx.) return(-1); |
| 157 | if (A->_dsIdxcxx . . $ aHit#_dsIdx. > B->_dsIdxcxx . . $ aHit#_dsIdx.) return(1); |
| 158 | if (A->_qsIdxcxx . . $ aHit#_qsIdx. < B->_qsIdxcxx . . $ aHit#_qsIdx.) return(-1); |
| 159 | if (A->_qsIdxcxx . . $ aHit#_qsIdx. > B->_qsIdxcxx . . $ aHit#_qsIdx.) return(1); |
| 160 | if (A->_dsLocxx . . $ aHit#_dsLo. < B->_dsLocxx . . $ aHit#_dsLo.) return(-1); |
| 161 | if (A->_dsLocxx . . $ aHit#_dsLo. > B->_dsLocxx . . $ aHit#_dsLo.) return(1); |
| 162 | return(0); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | int |
| 169 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) { |
| 170 | |
| 171 | if (argc < 4) { |
| 172 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "usage: %s [-v] [-m memorylimit] [-t temppath] hitfile1 hitfile2 ... > sorted-hits\n", argv[0]); |
| 173 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " memory limit is MB\n"); |
| 174 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 175 | } |
| 176 | |
| 177 | int arg = 1; |
| 178 | while (arg < argc) { |
| 179 | if (strncmp(argv[arg], "-v", 2) == 0) { |
| 180 | beVerbosecxx . . $ beVerbose. = true; |
| 181 | } else if (strncmp(argv[arg], "-m", 2) == 0) { |
| 182 | arg++; |
| 183 | memoryLimitcxx . . $ memoryLimit. = atoicxx . . $ atoi(f5ecf3191ed0cd93).(argv[arg]); |
| 184 | memoryLimitcxx . . $ memoryLimit. <<= 20; |
| 185 | } else if (strncmp(argv[arg], "-t", 2) == 0) { |
| 186 | arg++; |
| 187 | tmpPathcxx . . $ tmpPath. = argv[arg]; |
| 188 | } else { |
| 189 | // Must be at the first file name. Break. |
| 190 | break; |
| 191 | } |
| 192 | arg++; |
| 193 | } |
| 194 | |
| 195 | // Allocate a bunch of spaces to store hits. |
| 196 | // |
| 197 | uint64cxx . . $ uint64# hitsMax = memoryLimitcxx . . $ memoryLimit. / sizeof(aHitcxx . . $ aHit#); |
| 198 | uint32cxx . . $ uint32# hitsPos = 0; |
| 199 | aHitcxx . . $ aHit# *hits = new aHitcxx . . $ aHit# [hitsMax]; |
| 200 | |
| 201 | uint32cxx . . $ uint32# tmpFlen = 0; |
| 202 | uint32cxx . . $ uint32# tmpFmax = 1024; |
| 203 | aHitTemporarycxx . . $ aHitTemporary# **tmpF = new aHitTemporarycxx . . $ aHitTemporary# * [tmpFmax]; |
| 204 | |
| 205 | while (arg < argc) { |
| 206 | aHitReadercxx . . $ aHitReader# *R = new aHitReadercxx . . $ aHitReader#(argv[arg]); |
| 207 | arg++; |
| 208 | |
| 209 | // Read hits until we exhaust out space, then sort and dump to disk. |
| 210 | // |
| 211 | while (R->readHitcxx . . $ aHitReader#readHit(6c421a15b799c67b).(hits[hitsPos])) { |
| 212 | hitsPos++; |
| 213 | |
| 214 | if (hitsPos == hitsMax) { |
| 215 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(hits, hitsPos, sizeof(aHitcxx . . $ aHit#), hitcmpcxx . . $ hitcmp(9148c8c3ff42c9d8).); |
| 216 | |
| 217 | if (tmpFlen >= tmpFmax) { |
| 218 | tmpFmax *= 2; |
| 219 | aHitTemporarycxx . . $ aHitTemporary# **tmp = new aHitTemporarycxx . . $ aHitTemporary# * [tmpFmax]; |
| 220 | memcpy(tmp, tmpF, sizeof(aHitTemporarycxx . . $ aHitTemporary#) * tmpFlen); |
| 221 | delete [] tmpF; |
| 222 | tmpF = tmp; |
| 223 | } |
| 224 | |
| 225 | tmpF[tmpFlen] = new aHitTemporarycxx . . $ aHitTemporary#(hits, hitsPos); |
| 226 | |
| 227 | tmpFlen++; |
| 228 | hitsPos = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | delete R; |
| 233 | } |
| 234 | |
| 235 | // All done reading. If we have stuff to sort, sort it. |
| 236 | // |
| 237 | if (hitsPos > 0) |
| 238 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(hits, hitsPos, sizeof(aHitcxx . . $ aHit#), hitcmpcxx . . $ hitcmp(9148c8c3ff42c9d8).); |
| 239 | |
| 240 | // No temporary files? Just write the hits and exit. We're done. |
| 241 | // |
| 242 | if (tmpFlen == 0) { |
| 243 | for (uint32cxx . . $ uint32# i=0; i<hitsPos; i++) |
| 244 | ahit_printASCIIcxx . . $ ahit_printASCII(6c917abaf95665ca).(hits+i, stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!); |
| 245 | exitcxx . . $ exit(44ed87f96534fad7).(0); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | // We have at least one temporary file already on disk, so to make things |
| 250 | // easier, we write out the current set of hits and do an all disk merge. |
| 251 | |
| 252 | |
| 253 | if (tmpFlen >= tmpFmax) { |
| 254 | tmpFmax *= 2; |
| 255 | aHitTemporarycxx . . $ aHitTemporary# **tmp = new aHitTemporarycxx . . $ aHitTemporary# * [tmpFmax]; |
| 256 | memcpy(tmp, tmpF, sizeof(aHitTemporarycxx . . $ aHitTemporary#) * tmpFlen); |
| 257 | delete [] tmpF; |
| 258 | tmpF = tmp; |
| 259 | } |
| 260 | |
| 261 | tmpF[tmpFlen] = new aHitTemporarycxx . . $ aHitTemporary#(hits, hitsPos); |
| 262 | |
| 263 | tmpFlen++; |
| 264 | |
| 265 | |
| 266 | |
| 267 | // While there is still input, merge to the output |
| 268 | // |
| 269 | bool moreInput = true; |
| 270 | |
| 271 | while (moreInput) { |
| 272 | |
| 273 | // Pick the smallest hit -- if file [i] is finished, then hit[i] |
| 274 | // is bogus and all the values are set to maximal values. |
| 275 | // |
| 276 | uint32cxx . . $ uint32# smallestHit = 0; |
| 277 | |
| 278 | for (uint32cxx . . $ uint32# nh = smallestHit+1; nh < tmpFlen; nh++) { |
| 279 | if (hitcmpcxx . . $ hitcmp(9148c8c3ff42c9d8).(tmpF[smallestHit]->theHitcxx . . $ aHitTemporary#theHit(95840130fd782174).(), tmpF[nh]->theHitcxx . . $ aHitTemporary#theHit(95840130fd782174).()) > 0) |
| 280 | smallestHit = nh; |
| 281 | } |
| 282 | |
| 283 | // If the smallest hit is invalid, we're done. Otherwise, write |
| 284 | // the hit, and read a new one. |
| 285 | // |
| 286 | if (tmpF[smallestHit]->theHitcxx . . $ aHitTemporary#theHit(95840130fd782174).()->_qsIdxcxx . . $ aHit#_qsIdx. == ~uint32ZEROcxx . . $ `libutil/util.h:60:10`!) { |
| 287 | moreInput = false; |
| 288 | } else { |
| 289 | ahit_printASCIIcxx . . $ ahit_printASCII(6c917abaf95665ca).(tmpF[smallestHit]->theHitcxx . . $ aHitTemporary#theHit(95840130fd782174).(), stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!); |
| 290 | tmpF[smallestHit]->nextHitcxx . . $ aHitTemporary#nextHit(49f6e7a06ebc5aa8).(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Should clean up, I know. |
| 295 | |
| 296 | return(0); |
| 297 | } |