| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
| 6 | #include "aHit.H" |
| 7 | #include "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 beVerbose = false; |
| 16 | uint64 memoryLimit = 128 * 1024 * 1024; |
| 17 | char *tmpPath = 0L; |
| 18 | |
| 19 | |
| 20 | |
| 21 | class aHitReader { |
| 22 | public: |
| 23 | |
| 24 | // Open the file for reading, testing if it's binary or ascii input |
| 25 | // |
| 26 | aHitReader(char *filename) { |
| 27 | errno = 0; |
| 28 | theFile = fopen(filename, "r"); |
| 29 | if (theFile == 0L) { |
| 30 | fprintf(stderr, "sortHits-- ERROR opening '%s': %s\n", filename, strerror(errno)); |
| 31 | exit(1); |
| 32 | } |
| 33 | |
| 34 | char x = (char)fgetc(theFile); |
| 35 | ungetc(x, theFile); |
| 36 | |
| 37 | isBinary = (x != '-'); |
| 38 | |
| 39 | if (!isBinary) |
| 40 | buffer = new char [1024]; |
| 41 | }; |
| 42 | |
| 43 | ~aHitReader() { |
| 44 | fclose(theFile); |
| 45 | delete [] buffer; |
| 46 | }; |
| 47 | |
| 48 | bool readHit(aHit &hit) { |
| 49 | if (isBinary) { |
| 50 | ahit_readBinary(&hit, theFile); |
| 51 | } else { |
| 52 | fgets(buffer, 1024, theFile); |
| 53 | ahit_parseString(&hit, buffer); |
| 54 | } |
| 55 | |
| 56 | return(feof(theFile) == false); |
| 57 | }; |
| 58 | private: |
| 59 | FILE *theFile; |
| 60 | char *buffer; |
| 61 | bool 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 aHitTemporary { |
| 71 | public: |
| 72 | aHitTemporary(aHit *hits, uint32 hitsLen) { |
| 73 | theFile = makeTempFile(tmpPath); |
| 74 | |
| 75 | // XXX: Known bug on Tru64: fwrite() of data blocks > 2GB is broken |
| 76 | |
| 77 | uint32 outputPos = 0; |
| 78 | uint32 outputLen = 1024 * 1024 / sizeof(aHit); |
| 79 | |
| 80 | while (outputPos < hitsLen) { |
| 81 | errno = 0; |
| 82 | outputPos += fwrite(hits, sizeof(aHit), hitsLen, theFile); |
| 83 | if (errno) { |
| 84 | fprintf(stderr, "ERROR: sortHits()-- Failed to write temporary file: %s\n", strerror(errno)); |
| 85 | exit(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(theFile); |
| 95 | |
| 96 | hit._forward = false; |
| 97 | hit._merged = false; |
| 98 | hit._qsIdx = uint32ZERO; |
| 99 | hit._dsIdx = uint32ZERO; |
| 100 | hit._dsLo = uint32ZERO; |
| 101 | hit._dsHi = uint32ZERO; |
| 102 | hit._covered = uint32ZERO; |
| 103 | hit._matched = uint32ZERO; |
| 104 | hit._numMers = uint32ZERO; |
| 105 | |
| 106 | nextHit(); |
| 107 | }; |
| 108 | |
| 109 | ~aHitTemporary() { |
| 110 | fclose(theFile); |
| 111 | }; |
| 112 | |
| 113 | aHit *theHit(void) { |
| 114 | return(&hit); |
| 115 | }; |
| 116 | void nextHit(void) { |
| 117 | if (hit._qsIdx != ~uint32ZERO) { |
| 118 | errno = 0; |
| 119 | fread(&hit, sizeof(aHit), 1, theFile); |
| 120 | if (errno) { |
| 121 | fprintf(stderr, "ERROR: sortHits()-- Failed to read a hit: %s\n", strerror(errno)); |
| 122 | exit(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(theFile)) { |
| 129 | hit._forward = false; |
| 130 | hit._merged = false; |
| 131 | hit._qsIdx = ~uint32ZERO; |
| 132 | hit._dsIdx = ~uint32ZERO; |
| 133 | hit._dsLo = ~uint32ZERO; |
| 134 | hit._dsHi = ~uint32ZERO; |
| 135 | hit._covered = ~uint32ZERO; |
| 136 | hit._matched = ~uint32ZERO; |
| 137 | hit._numMers = ~uint32ZERO; |
| 138 | } |
| 139 | } |
| 140 | }; |
| 141 | private: |
| 142 | FILE *theFile; |
| 143 | aHit hit; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | int |
| 152 | hitcmp(const void *a, const void *b) { |
| 153 | aHit *A = (aHit *)a; |
| 154 | aHit *B = (aHit *)b; |
| 155 | |
| 156 | if (A->_dsIdx < B->_dsIdx) return(-1); |
| 157 | if (A->_dsIdx > B->_dsIdx) return(1); |
| 158 | if (A->_qsIdx < B->_qsIdx) return(-1); |
| 159 | if (A->_qsIdx > B->_qsIdx) return(1); |
| 160 | if (A->_dsLo < B->_dsLo) return(-1); |
| 161 | if (A->_dsLo > B->_dsLo) return(1); |
| 162 | return(0); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | int |
| 169 | main(int argc, char **argv) { |
| 170 | |
| 171 | if (argc < 4) { |
| 172 | fprintf(stderr, "usage: %s [-v] [-m memorylimit] [-t temppath] hitfile1 hitfile2 ... > sorted-hits\n", argv[0]); |
| 173 | fprintf(stderr, " memory limit is MB\n"); |
| 174 | exit(1); |
| 175 | } |
| 176 | |
| 177 | int arg = 1; |
| 178 | while (arg < argc) { |
| 179 | if (strncmp(argv[arg], "-v", 2) == 0) { |
| 180 | beVerbose = true; |
| 181 | } else if (strncmp(argv[arg], "-m", 2) == 0) { |
| 182 | arg++; |
| 183 | memoryLimit = atoi(argv[arg]); |
| 184 | memoryLimit <<= 20; |
| 185 | } else if (strncmp(argv[arg], "-t", 2) == 0) { |
| 186 | arg++; |
| 187 | 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 | uint64 hitsMax = memoryLimit / sizeof(aHit); |
| 198 | uint32 hitsPos = 0; |
| 199 | aHit *hits = new aHit [hitsMax]; |
| 200 | |
| 201 | uint32 tmpFlen = 0; |
| 202 | uint32 tmpFmax = 1024; |
| 203 | aHitTemporary **tmpF = new aHitTemporary * [tmpFmax]; |
| 204 | |
| 205 | while (arg < argc) { |
| 206 | aHitReader *R = new aHitReader(argv[arg]); |
| 207 | arg++; |
| 208 | |
| 209 | // Read hits until we exhaust out space, then sort and dump to disk. |
| 210 | // |
| 211 | while (R->readHit(hits[hitsPos])) { |
| 212 | hitsPos++; |
| 213 | |
| 214 | if (hitsPos == hitsMax) { |
| 215 | qsort(hits, hitsPos, sizeof(aHit), hitcmp); |
| 216 | |
| 217 | if (tmpFlen >= tmpFmax) { |
| 218 | tmpFmax *= 2; |
| 219 | aHitTemporary **tmp = new aHitTemporary * [tmpFmax]; |
| 220 | memcpy(tmp, tmpF, sizeof(aHitTemporary) * tmpFlen); |
| 221 | delete [] tmpF; |
| 222 | tmpF = tmp; |
| 223 | } |
| 224 | |
| 225 | tmpF[tmpFlen] = new 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 | qsort(hits, hitsPos, sizeof(aHit), hitcmp); |
| 239 | |
| 240 | // No temporary files? Just write the hits and exit. We're done. |
| 241 | // |
| 242 | if (tmpFlen == 0) { |
| 243 | for (uint32 i=0; i<hitsPos; i++) |
| 244 | ahit_printASCII(hits+i, stdout); |
| 245 | exit(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 | aHitTemporary **tmp = new aHitTemporary * [tmpFmax]; |
| 256 | memcpy(tmp, tmpF, sizeof(aHitTemporary) * tmpFlen); |
| 257 | delete [] tmpF; |
| 258 | tmpF = tmp; |
| 259 | } |
| 260 | |
| 261 | tmpF[tmpFlen] = new 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 | uint32 smallestHit = 0; |
| 277 | |
| 278 | for (uint32 nh = smallestHit+1; nh < tmpFlen; nh++) { |
| 279 | if (hitcmp(tmpF[smallestHit]->theHit(), tmpF[nh]->theHit()) > 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]->theHit()->_qsIdx == ~uint32ZERO) { |
| 287 | moreInput = false; |
| 288 | } else { |
| 289 | ahit_printASCII(tmpF[smallestHit]->theHit(), stdout); |
| 290 | tmpF[smallestHit]->nextHit(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Should clean up, I know. |
| 295 | |
| 296 | return(0); |
| 297 | } |