| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <errno.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "sim4.H" |
| 8 |
#include "bio.h" |
| 9 |
#include "util.h" |
| 10 |
|
| 11 |
// Sorts a file of polishes by cDNA or genomic idx. |
| 12 |
|
| 13 |
sim4polishReader * |
| 14 |
writeTemporary(char *filePrefix, sim4polish **p, uint32 pLen, sim4polishStyle style, int (*fcn)(const void *, const void *)) { |
| 15 |
sim4polishWriter *W = new sim4polishWriter(0L, style, true); |
| 16 |
sim4polishReader *R; |
| 17 |
|
| 18 |
qsort(p, pLen, sizeof(sim4polish *), fcn); |
| 19 |
|
| 20 |
for (uint32 i=0; i<pLen; i++) |
| 21 |
W->writeAlignment(p[i]); |
| 22 |
|
| 23 |
R = new sim4polishReader(0L, W); |
| 24 |
|
| 25 |
delete W; |
| 26 |
|
| 27 |
return(R); |
| 28 |
} |
| 29 |
|
| 30 |
// Save the polish using palloc; |
| 31 |
// |
| 32 |
sim4polish * |
| 33 |
savePolish(sim4polish *q, uint64 *alloc) { |
| 34 |
int l; |
| 35 |
|
| 36 |
// Copy the base polish structure. |
| 37 |
// |
| 38 |
sim4polish *r = (sim4polish *)palloc(sizeof(sim4polish)); |
| 39 |
memcpy(r, q, sizeof(sim4polish)); |
| 40 |
*alloc += sizeof(sim4polish); |
| 41 |
|
| 42 |
// Copy the deflines. |
| 43 |
// |
| 44 |
if (q->_estDefLine && q->_genDefLine) { |
| 45 |
l = strlen(q->_estDefLine) + 1; |
| 46 |
r->_estDefLine = (char *)palloc(sizeof(char) * l); |
| 47 |
memcpy(r->_estDefLine, q->_estDefLine, sizeof(char) * l); |
| 48 |
*alloc += l * sizeof(char); |
| 49 |
|
| 50 |
l = strlen(q->_genDefLine) + 1; |
| 51 |
r->_genDefLine = (char *)palloc(sizeof(char) * l); |
| 52 |
memcpy(r->_genDefLine, q->_genDefLine, sizeof(char) * l); |
| 53 |
*alloc += l * sizeof(char); |
| 54 |
} |
| 55 |
|
| 56 |
// Copy the base exon structure. |
| 57 |
// |
| 58 |
r->_exons = (sim4polishExon *)palloc(sizeof(sim4polishExon) * q->_numExons); |
| 59 |
memcpy(r->_exons, q->_exons, sizeof(sim4polishExon) * q->_numExons); |
| 60 |
*alloc += sizeof(sim4polishExon) * q->_numExons; |
| 61 |
|
| 62 |
// Copy the exon alignments. |
| 63 |
// |
| 64 |
for (uint32 i=0; i<q->_numExons; i++) { |
| 65 |
if (q->_exons[i]._estAlignment) { |
| 66 |
l = strlen(q->_exons[i]._estAlignment) + 1; |
| 67 |
r->_exons[i]._estAlignment = (char *)palloc(sizeof(char) * l); |
| 68 |
memcpy(r->_exons[i]._estAlignment, q->_exons[i]._estAlignment, sizeof(char) * l); |
| 69 |
*alloc += l * sizeof(char); |
| 70 |
} |
| 71 |
|
| 72 |
if (q->_exons[i]._genAlignment) { |
| 73 |
l = strlen(q->_exons[i]._genAlignment) + 1; |
| 74 |
r->_exons[i]._genAlignment = (char *)palloc(sizeof(char) * l); |
| 75 |
memcpy(r->_exons[i]._genAlignment, q->_exons[i]._genAlignment, sizeof(char) * l); |
| 76 |
*alloc += l * sizeof(char); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return(r); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
void |
| 85 |
statusReport(uint32 pLen, uint32 mergeFilesLen, uint64 arrayAlloc, uint64 matchAlloc, uint64 upperAlloc) { |
| 86 |
if (pLen > 0) { |
| 87 |
fprintf(stderr, "Read: "uint32FMTW(8)" polishes -- "uint32FMTW(5)" temporary files -- "uint64FMTW(5)"MB / "uint64FMTW(5)"MB -- "uint64FMTW(5)" bytes/polish\r", |
| 88 |
pLen, |
| 89 |
mergeFilesLen, |
| 90 |
(arrayAlloc + matchAlloc) >> 20, |
| 91 |
upperAlloc >> 20, |
| 92 |
matchAlloc / pLen); |
| 93 |
fflush(stderr); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
// The OS limit is usually hit before this, but this is |
| 101 |
// the maximum number of files we can have open at once. |
| 102 |
// |
| 103 |
//#define MERGE_FILES_MAX OPEN_MAX |
| 104 |
|
| 105 |
|
| 106 |
int |
| 107 |
main(int argc, char **argv) { |
| 108 |
bool beVerbose = false; |
| 109 |
char *filePrefix = NULL; |
| 110 |
|
| 111 |
uint32 pLen = 0; |
| 112 |
uint32 pMax = 1 * 1024 * 1024; |
| 113 |
|
| 114 |
uint64 upperAlloc = getProcessSizeLimit(); // Maximum allowed memory usage |
| 115 |
uint64 arrayAlloc = 0; // Static stuff: the process, arrays |
| 116 |
uint64 matchAlloc = 0; // palloc size, matches |
| 117 |
|
| 118 |
int (*fcn)(const void *, const void *) = 0L; |
| 119 |
|
| 120 |
bool moreInput = true; |
| 121 |
|
| 122 |
uint32 mergeFilesLen = 0; |
| 123 |
uint32 mergeFilesMax = sysconf(_SC_OPEN_MAX); |
| 124 |
sim4polishReader **mergeFiles = new sim4polishReader * [mergeFilesMax]; |
| 125 |
char **mergeNames = new char * [mergeFilesMax]; |
| 126 |
|
| 127 |
sim4polishStyle style = sim4polishStyleDefault; |
| 128 |
|
| 129 |
|
| 130 |
if ((mergeFiles == 0L) || (mergeNames == 0L)) { |
| 131 |
fprintf(stderr, "sortPolishes: Failed to initialize.\n"); |
| 132 |
exit(1); |
| 133 |
} |
| 134 |
for (uint32 i=0; i<mergeFilesMax; i++) { |
| 135 |
mergeFiles[i] = NULL; |
| 136 |
mergeNames[i] = NULL; |
| 137 |
} |
| 138 |
|
| 139 |
int arg = 1; |
| 140 |
int err = 0; |
| 141 |
while (arg < argc) { |
| 142 |
if (strncmp(argv[arg], "-v", 2) == 0) { |
| 143 |
beVerbose = true; |
| 144 |
|
| 145 |
} else if (strncmp(argv[arg], "-c", 2) == 0) { |
| 146 |
fcn = s4p_estIDcompare; |
| 147 |
|
| 148 |
} else if (strncmp(argv[arg], "-g", 2) == 0) { |
| 149 |
fcn = s4p_genIDcompare; |
| 150 |
|
| 151 |
} else if (strncmp(argv[arg], "-C", 2) == 0) { |
| 152 |
fcn = s4p_estDEFcompare; |
| 153 |
|
| 154 |
} else if (strncmp(argv[arg], "-G", 2) == 0) { |
| 155 |
fcn = s4p_genDEFcompare; |
| 156 |
|
| 157 |
} else if (strncmp(argv[arg], "-m", 2) == 0) { |
| 158 |
arg++; |
| 159 |
upperAlloc = atoi(argv[arg]); |
| 160 |
upperAlloc *= 1048576; |
| 161 |
|
| 162 |
} else if (strncmp(argv[arg], "-t", 2) == 0) { |
| 163 |
arg++; |
| 164 |
filePrefix = argv[arg]; |
| 165 |
|
| 166 |
} else if (strcmp(argv[arg], "-gff3") == 0) { |
| 167 |
style = sim4polishGFF3; |
| 168 |
|
| 169 |
} else if (strncmp(argv[arg], "-M", 2) == 0) { |
| 170 |
arg++; |
| 171 |
while ((arg < argc) && (fileExists(argv[arg]))) { |
| 172 |
if (mergeFilesLen >= mergeFilesMax) { |
| 173 |
fprintf(stderr, "%s: ERROR! Too many input files! Should be less than %d\n", argv[0], mergeFilesMax); |
| 174 |
exit(1); |
| 175 |
} |
| 176 |
mergeNames[mergeFilesLen] = argv[arg]; |
| 177 |
mergeFiles[mergeFilesLen++] = new sim4polishReader(argv[arg]); |
| 178 |
arg++; |
| 179 |
} |
| 180 |
arg--; |
| 181 |
|
| 182 |
} else { |
| 183 |
fprintf(stderr, "unknown option: %s\n", argv[arg]); |
| 184 |
err++; |
| 185 |
} |
| 186 |
|
| 187 |
arg++; |
| 188 |
} |
| 189 |
if ((err) || |
| 190 |
(fcn == 0L) || |
| 191 |
((mergeFilesLen == 0) && (isatty(fileno(stdin))))) { |
| 192 |
fprintf(stderr, "usage: %s [-c | -g] [-m M] [-t T] [-gff3] [-M [file ...]]\n", argv[0]); |
| 193 |
fprintf(stderr, " -c (-C) Sort by the cDNA index (defline).\n"); |
| 194 |
fprintf(stderr, " -g (-G) Sort by the genomic index (defline).\n"); |
| 195 |
fprintf(stderr, " -M Skip the sort, just do a merge.\n"); |
| 196 |
fprintf(stderr, " -m M Use at most M MB of core, using a disk-based merge if memory\n"); |
| 197 |
fprintf(stderr, " is exhausted. Default: 4096.\n"); |
| 198 |
fprintf(stderr, " -t T Use directory 'T' for temporary files. Default is the current\n"); |
| 199 |
fprintf(stderr, " working directory. The sort unlinks files immediately after\n"); |
| 200 |
fprintf(stderr, " creation: no files will exist, but space will be used.\n"); |
| 201 |
fprintf(stderr, " -gff3 Format output as GFF3.\n"); |
| 202 |
fprintf(stderr, " -v Be verbose.\n"); |
| 203 |
fprintf(stderr, "\n"); |
| 204 |
fprintf(stderr, " Both sort methods use the OTHER index as a secondary key.\n"); |
| 205 |
|
| 206 |
if (fcn == 0L) |
| 207 |
fprintf(stderr, "\nERROR: what key do you want to sort on (-c, -g, -C, -G)\n"); |
| 208 |
|
| 209 |
if ((mergeFilesLen == 0) && (isatty(fileno(stdin)))) |
| 210 |
fprintf(stderr, "\nERROR: no files to merge\n"); |
| 211 |
|
| 212 |
exit(1); |
| 213 |
} |
| 214 |
|
| 215 |
if (mergeFilesLen > 0) |
| 216 |
fprintf(stderr, "Found %d files to merge!\n", mergeFilesLen); |
| 217 |
|
| 218 |
|
| 219 |
// XXX: Experimental method to automagically determine the amount of memory available (or, to at |
| 220 |
// least, determine if this process can get to be as big as the user said it can. |
| 221 |
// |
| 222 |
arrayAlloc = getProcessSizeCurrent(); |
| 223 |
|
| 224 |
sim4polish **p = new sim4polish * [pMax]; |
| 225 |
memset(p, 0, sizeof(sim4polish *) * pMax); |
| 226 |
|
| 227 |
arrayAlloc += sizeof(sim4polish *) * pMax; |
| 228 |
|
| 229 |
|
| 230 |
// With small memory sizes, we occasionally run out of data space. This looks like an artifact |
| 231 |
// of not having palloc() use a blocksize that divides our upperAlloc size. This attempts to |
| 232 |
// sync them up. |
| 233 |
// |
| 234 |
psetblocksize(upperAlloc / 16); // This produced a crash in readBuffer |
| 235 |
//psetdebug(2); |
| 236 |
|
| 237 |
sim4polishReader *R = new sim4polishReader("-"); |
| 238 |
sim4polish *q = 0L; |
| 239 |
|
| 240 |
if (R->getsim4polishStyle() != style) |
| 241 |
fprintf(stderr, "warning: input format and output format differ.\n"); |
| 242 |
|
| 243 |
while (R->nextAlignment(q)) { |
| 244 |
|
| 245 |
// Allocate more pointer space, if we need to |
| 246 |
// |
| 247 |
if ((pLen >= pMax) || |
| 248 |
(arrayAlloc + matchAlloc >= upperAlloc)) { |
| 249 |
|
| 250 |
// Either realloc space (if we're still small enough to do so) or |
| 251 |
// write an intermediate file. |
| 252 |
|
| 253 |
if (arrayAlloc + matchAlloc + sizeof(sim4polish*) * pMax * 2 < upperAlloc) { |
| 254 |
sim4polish **P = new sim4polish * [pMax * 2]; |
| 255 |
memcpy(P, p, sizeof(sim4polish *) * pMax); |
| 256 |
delete [] p; |
| 257 |
pMax *= 2; |
| 258 |
p = P; |
| 259 |
arrayAlloc += sizeof(sim4polish *) * pMax; |
| 260 |
|
| 261 |
} else { |
| 262 |
if (beVerbose) { |
| 263 |
statusReport(pLen, mergeFilesLen+1, arrayAlloc, matchAlloc, upperAlloc); |
| 264 |
fprintf(stderr, "\n"); |
| 265 |
} |
| 266 |
|
| 267 |
if (mergeFilesLen >= mergeFilesMax) { |
| 268 |
fprintf(stderr, "Too many open files. Try increasing memory size.\n"); |
| 269 |
exit(1); |
| 270 |
} |
| 271 |
mergeFiles[mergeFilesLen++] = writeTemporary(filePrefix, p, pLen, style, fcn); |
| 272 |
|
| 273 |
pfree(); |
| 274 |
matchAlloc = 0; |
| 275 |
pLen = 0; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
p[pLen++] = savePolish(q, &matchAlloc); // COPY the polish. |
| 280 |
|
| 281 |
if (beVerbose && ((pLen % 2000) == 0)) |
| 282 |
statusReport(pLen, mergeFilesLen+1, arrayAlloc, matchAlloc, upperAlloc); |
| 283 |
} |
| 284 |
|
| 285 |
if (beVerbose) { |
| 286 |
statusReport(pLen, mergeFilesLen+1, arrayAlloc, matchAlloc, upperAlloc); |
| 287 |
fprintf(stderr, "\n"); |
| 288 |
} |
| 289 |
|
| 290 |
sim4polishWriter *W = new sim4polishWriter("-", style); |
| 291 |
|
| 292 |
if (mergeFilesLen == 0) { |
| 293 |
// No temporary files. Sort the polishes, and dump. |
| 294 |
qsort(p, pLen, sizeof(sim4polish *), fcn); |
| 295 |
|
| 296 |
for (uint32 i=0; i<pLen; i++) |
| 297 |
W->writeAlignment(p[i]); |
| 298 |
} else { |
| 299 |
|
| 300 |
// Crud. Temporary files. Sort the last batch, dump it, then do |
| 301 |
// a merge. |
| 302 |
// |
| 303 |
if (mergeFilesLen >= mergeFilesMax) { |
| 304 |
fprintf(stderr, "Too many open files. Try increasing memory size.\n"); |
| 305 |
exit(1); |
| 306 |
} |
| 307 |
mergeFiles[mergeFilesLen++] = writeTemporary(filePrefix, p, pLen, style, fcn); |
| 308 |
|
| 309 |
pfree(); |
| 310 |
matchAlloc = 0; |
| 311 |
pLen = 0; |
| 312 |
|
| 313 |
delete [] p; |
| 314 |
} |
| 315 |
|
| 316 |
// |
| 317 |
// The merge |
| 318 |
// |
| 319 |
|
| 320 |
if (mergeFilesLen > 0) { |
| 321 |
if (beVerbose) |
| 322 |
fprintf(stderr, "Merging temporary files....\n"); |
| 323 |
|
| 324 |
sim4polish **p = new sim4polish * [mergeFilesLen]; |
| 325 |
|
| 326 |
memset(p, 0, sizeof(sim4polish *) * mergeFilesLen); |
| 327 |
|
| 328 |
for (uint32 i=0; i<mergeFilesLen; i++) |
| 329 |
mergeFiles[i]->nextAlignment(p[i]); |
| 330 |
|
| 331 |
while (moreInput) { |
| 332 |
uint32 smallestPolish = 0; |
| 333 |
|
| 334 |
// Find the smallest polish. |
| 335 |
// |
| 336 |
for (uint32 nextPolish = smallestPolish+1; nextPolish < mergeFilesLen; nextPolish++) { |
| 337 |
if ((*fcn)(p+smallestPolish, p+nextPolish) > 0) |
| 338 |
smallestPolish = nextPolish; |
| 339 |
} |
| 340 |
|
| 341 |
// If the smallestPolish is 0L, we're all done. Otherwise, dump |
| 342 |
// the current smallest and fill it with a new polish. |
| 343 |
// |
| 344 |
if (p[smallestPolish] == 0L) { |
| 345 |
moreInput = false; |
| 346 |
} else { |
| 347 |
W->writeAlignment(p[smallestPolish]); |
| 348 |
mergeFiles[smallestPolish]->nextAlignment(p[smallestPolish]); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// Attempt cleanup |
| 353 |
// |
| 354 |
for (uint32 i=0; i<mergeFilesLen; i++) |
| 355 |
delete mergeFiles[i]; |
| 356 |
|
| 357 |
delete [] p; |
| 358 |
} |
| 359 |
|
| 360 |
delete W; |
| 361 |
|
| 362 |
delete [] mergeFiles; |
| 363 |
delete [] mergeNames; |
| 364 |
|
| 365 |
pfree(); |
| 366 |
|
| 367 |
return(0); |
| 368 |
} |