1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/******************************************************************************
*
* This file is part of canu, a software program that assembles whole-genome
* sequencing reads into contigs.
*
* This software is based on:
* 'Celera Assembler' r4587 (http://wgs-assembler.sourceforge.net)
* the 'kmer package' r1994 (http://kmer.sourceforge.net)
*
* Except as indicated otherwise, this is a 'United States Government Work',
* and is released in the public domain.
*
* File 'README.licenses' in the root directory of this distribution
* contains full conditions and disclaimers.
*/
#include "runtime.H"
#include "sqStore.H"
#include "strings.H"
#include <set>
// Reads seqStore, outputs three files:
// ovlbat - batch names
// ovljob - job names
// ovlopt - overlapper options
//
// From (very) old paper notes, overlapInCore only computes overlaps for referenceID < hashID.
uint32 batchMax = 1000;
uint32 *
loadReadLengths(sqStore *seq,
std::set<uint32> &libToHash, uint32 &hashMin, uint32 &hashMax,
std::set<uint32> &libToRef, uint32 &refMin, uint32 &refMax) {
uint32 numReads = seq->sqStore_lastReadID();
uint32 numLibs = seq->sqStore_lastLibraryID();
uint32 *readLen = new uint32 [numReads + 1];
bool testHash = false;
bool testRef = false;
if (libToHash.size() > 0) {
testHash = true;
hashMin = UINT32_MAX;
hashMax = 0;
}
if (libToRef.size() > 0) {
testRef = true;
refMin = UINT32_MAX;
refMax = 0;
}
bool *doHash = new bool [numLibs + 1];
bool *doRef = new bool [numLibs + 1];
for (uint32 i=0; i<=numLibs; i++) {
doHash[i] = (libToHash.count(i) == 0) ? false : true;
doRef[i] = (libToRef.count(i) == 0) ? false : true;
}
//fprintf(stderr, "Loading lengths of " F_U32 " fragments (" F_SIZE_T "mb)\n",
// numReads, (numReads * sizeof(uint32)) >> 20);
memset(readLen, 0, sizeof(uint32) * (numReads + 1));
uint64 rawReads = 0, rawBases = 0;
uint64 corReads = 0, corBases = 0;
uint64 triReads = 0, triBases = 0;
fprintf(stderr, "\n");
fprintf(stderr, " Raw Raw Corrected Corrected Trimmed Trimmed\n");
fprintf(stderr, " Reads Bases Reads Bases Reads Bases\n");
fprintf(stderr, "---------- ------------ ---------- ------------ ---------- ------------\n");
uint32 reportInterval = numReads / 39 + 1;
for (uint32 ii=1; ii<=numReads; ii++) {
uint32 rr = seq->sqStore_getReadLength(ii, sqRead_raw);
uint32 rc = seq->sqStore_getReadLength(ii, sqRead_corrected);
uint32 rt = seq->sqStore_getClearEnd(ii, sqRead_corrected) - seq->sqStore_getClearBgn(ii, sqRead_corrected);
uint32 li = seq->sqStore_getLibraryIDForRead(ii);
if (rr > 0) {
rawReads += 1;
rawBases += rr;
}
if (rc > 0) {
corReads += 1;
corBases += rc;
}
if (rt > 0) {
triReads += 1;
triBases += rt;
}
readLen[ii] = seq->sqStore_getReadLength(ii);
if ((testHash == true) && (doHash[li] == true)) {
hashMin = std::min(hashMin, ii);
hashMax = std::max(hashMax, ii);
}
if ((testRef == true) && (doRef[li] == true)) {
refMin = std::min(refMin, ii);
refMax = std::max(refMax, ii);
}
if ((ii % reportInterval) == 0)
fprintf(stderr, "%10" F_U64P " %12" F_U64P " %10" F_U64P " %12" F_U64P " %10" F_U64P " %12" F_U64P "\n",
rawReads, rawBases, corReads, corBases, triReads, triBases);
}
fprintf(stderr, "---------- ------------ ---------- ------------ ---------- ------------\n");
fprintf(stderr, "%10" F_U64P " %12" F_U64P " %10" F_U64P " %12" F_U64P " %10" F_U64P " %12" F_U64P "\n",
rawReads, rawBases, corReads, corBases, triReads, triBases);
fprintf(stderr, "\n");
delete [] doHash;
delete [] doRef;
return(readLen);
}
void
partitionLength(sqStore *seq,
uint32 *readLen,
FILE *BAT,
FILE *JOB,
FILE *OPT,
uint32 minOverlapLength,
uint64 ovlHashBlockLength,
uint64 ovlRefBlockLength,
std::set<uint32> &libToHash,
uint32 hashMin,
uint32 hashMax,
std::set<uint32> &libToRef,
uint32 refMin,
uint32 refMax) {
uint32 hashBeg = 1;
uint32 hashEnd = 0;
uint32 hashReads = 0;
uint64 hashBases = 0;
uint32 refBeg = 1;
uint32 refEnd = 0;
uint32 refReads = 0;
uint64 refBases = 0;
uint32 batchSize = 0; // Number of jobs in this directory
uint32 batchName = 1; // Name of the directory
uint32 jobName = 1; // Name of the job
uint32 numReads = seq->sqStore_lastReadID();
if (hashMax > numReads)
hashMax = numReads;
if (refMax > numReads)
refMax = numReads;
//fprintf(stderr, "Partitioning for hash: " F_U32 "-" F_U32 " ref: " F_U32 "," F_U32 "\n",
// hashMin, hashMax, refMin, refMax);
hashBeg = hashMin;
hashEnd = hashMin - 1;
while (hashBeg < hashMax) {
uint64 hashLen = 0;
assert(hashEnd == hashBeg - 1);
// Non deleted reads contribute one byte per untrimmed base, and every fragment contributes one
// more byte for the terminating zero. In canu, there are no deleted reads.
hashReads = 0;
hashBases = 0;
do {
hashEnd++;
if (readLen[hashEnd] < minOverlapLength)
continue;
hashLen += readLen[hashEnd] + 1;
hashReads += 1;
hashBases += readLen[hashEnd] + 1;
} while ((hashLen < ovlHashBlockLength) && (hashEnd < hashMax));
assert(hashEnd <= hashMax);
refBeg = refMin;
refEnd = 0;
while ((refBeg < refMax) &&
((refBeg < hashEnd) || (libToHash.size() != 0 && libToHash == libToRef))) {
uint64 refLen = 0;
refReads = 0;
refBases = 0;
do {
refEnd++;
if (readLen[refEnd] < minOverlapLength)
continue;
refLen += readLen[refEnd];
refReads += 1;
refBases += readLen[refEnd] + 1;
} while ((refLen < ovlRefBlockLength) && (refEnd < refMax));
if (refEnd > refMax)
refEnd = refMax;
if ((refEnd > hashEnd) && (libToHash.size() == 0 || libToHash != libToRef))
refEnd = hashEnd;
// Output the job.
fprintf(BAT, "%03" F_U32P "\n", batchName);
fprintf(JOB, "%06" F_U32P "\n", jobName);
if (hashReads == 0)
fprintf(OPT, "-h " F_U32 "-" F_U32 " -r " F_U32 "-" F_U32 "\n", hashBeg, hashEnd, refBeg, refEnd);
else
fprintf(OPT, "-h " F_U32 "-" F_U32 " -r " F_U32 "-" F_U32 " --hashdatalen " F_U64 "\n", hashBeg, hashEnd, refBeg, refEnd, hashBases);
fprintf(stderr, "%5" F_U32P " %10" F_U32P "-%-10" F_U32P " %9" F_U32P " %12" F_U64P " %10" F_U32P "-%-10" F_U32P " %9" F_U32P " %12" F_U64P "\n", jobName, hashBeg, hashEnd, hashReads, hashBases, refBeg, refEnd, refReads, refBases);
// Move to the next.
batchSize++;
if (batchSize >= batchMax) {
batchSize = 0;
batchName++;
}
jobName++;
refBeg = refEnd + 1;
}
hashBeg = hashEnd + 1;
}
}
FILE *
openOutput(char const *prefix, char const *type) {
char A[FILENAME_MAX];
snprintf(A, FILENAME_MAX, "%s.%s.WORKING", prefix, type);
errno = 0;
FILE *F = fopen(A, "w");
if (errno)
fprintf(stderr, "Failed to open '%s': %s\n", A, strerror(errno)), exit(1);
return(F);
}
void
renameToFinal(char const *prefix, char const *type) {
char A[FILENAME_MAX];
char B[FILENAME_MAX];
snprintf(A, FILENAME_MAX, "%s.%s.WORKING", prefix, type);
snprintf(B, FILENAME_MAX, "%s.%s", prefix, type);
AS_UTL_rename(A, B);
}
int
main(int argc, char **argv) {
char const *seqStoreName = NULL;
sqStore *seqStore = NULL;
char const *outputPrefix = NULL;
char outputName[FILENAME_MAX];
uint64 ovlHashBlockLength = 0;
uint64 ovlRefBlockLength = 0;
uint32 minOverlapLength = 0;
bool checkAllLibUsed = true;
std::set<uint32> libToHash;
std::set<uint32> libToRef;
AS_configure(argc, argv, 1);
int arg = 1;
int err = 0;
while (arg < argc) {
if (strcmp(argv[arg], "-S") == 0) {
seqStoreName = argv[++arg];
} else if (strcmp(argv[arg], "-hl") == 0) {
ovlHashBlockLength = strtouint64(argv[++arg]);
} else if (strcmp(argv[arg], "-rl") == 0) {
ovlRefBlockLength = strtouint64(argv[++arg]);
} else if (strcmp(argv[arg], "-ol") == 0) {
minOverlapLength = strtouint32(argv[++arg]);
} else if (strcmp(argv[arg], "-H") == 0) {
decodeRange(argv[++arg], libToHash);
} else if (strcmp(argv[arg], "-R") == 0) {
decodeRange(argv[++arg], libToRef);
} else if (strcmp(argv[arg], "-C") == 0) {
checkAllLibUsed = false;
} else if (strcmp(argv[arg], "-o") == 0) {
outputPrefix = argv[++arg];
} else {
fprintf(stderr, "ERROR: Unknown option '%s'\n", arg[argv]);
err++;
}
arg++;
}
if (ovlHashBlockLength == 0)
fprintf(stderr, "ERROR: Hash length (-hl) must be specified.\n"), err++;
if (ovlRefBlockLength == 0)
fprintf(stderr, "ERROR: Reference length (-rl) must be specified.\n"), err++;
if (seqStoreName == NULL)
fprintf(stderr, "ERROR: seqStore (-S) must be supplied.\n"), err++;
if (err) {
fprintf(stderr, "usage: %s [opts]\n", argv[0]);
fprintf(stderr, " Someone should write the command line help.\n");
fprintf(stderr, " But this is only used internally to canu, so...\n");
exit(1);
}
fprintf(stderr, "\n");
fprintf(stderr, "Configuring for:\n");
fprintf(stderr, " hash table: %12" F_U64P " bases.\n", ovlHashBlockLength);
fprintf(stderr, " read stream: %12" F_U64P " bases.\n", ovlRefBlockLength);
fprintf(stderr, "\n");
sqStore *seq = new sqStore(seqStoreName);
uint32 numLibs = seq->sqStore_lastLibraryID();
uint32 invalidLibs = 0;
for (auto it=libToHash.begin(); it != libToHash.end(); it++)
if (numLibs < *it)
fprintf(stderr, "ERROR: -H " F_U32 " is invalid; only " F_U32 " libraries in '%s'\n",
*it, numLibs, seqStoreName), invalidLibs++;
for (auto it=libToRef.begin(); it != libToRef.end(); it++)
if (numLibs < *it)
fprintf(stderr, "ERROR: -R " F_U32 " is invalid; only " F_U32 " libraries in '%s'\n",
*it, numLibs, seqStoreName), invalidLibs++;
if ((libToHash.size() > 0) && (libToRef.size() > 0)) {
for (uint32 lib=1; lib<=numLibs; lib++) {
if ((libToHash.find(lib) == libToHash.end()) &&
(libToRef.find(lib) == libToRef.end())) {
if (checkAllLibUsed == true)
fprintf(stderr, "ERROR: library " F_U32 " is not mentioned in either -H or -R.\n", lib), invalidLibs++;
else
fprintf(stderr, "Warning: library " F_U32 " is not mentioned in either -H or -R.\n", lib);
}
}
}
if (invalidLibs > 0)
fprintf(stderr, "ERROR: one of -H and/or -R are invalid.\n"), exit(1);
assert(ovlHashBlockLength > 0);
uint32 hashMin = 1;
uint32 hashMax = UINT32_MAX;
uint32 refMin = 1;
uint32 refMax = UINT32_MAX;
uint32 *readLen = loadReadLengths(seq, libToHash, hashMin, hashMax, libToRef, refMin, refMax);
FILE *BAT = openOutput(outputPrefix, "ovlbat");
FILE *JOB = openOutput(outputPrefix, "ovljob");
FILE *OPT = openOutput(outputPrefix, "ovlopt");
fprintf(stderr, " Job Hash Range # Reads # Bases Stream Range # Reads # Bases\n");
fprintf(stderr, "----- --------------------- --------- ------------ --------------------- --------- ------------\n");
partitionLength(seq, readLen, BAT, JOB, OPT, minOverlapLength, ovlHashBlockLength, ovlRefBlockLength, libToHash, hashMin, hashMax, libToRef, refMin, refMax);
AS_UTL_closeFile(BAT);
AS_UTL_closeFile(JOB);
AS_UTL_closeFile(OPT);
delete [] readLen;
renameToFinal(outputPrefix, "ovlbat");
renameToFinal(outputPrefix, "ovljob");
renameToFinal(outputPrefix, "ovlopt");
delete seq;
exit(0);
}
|