| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <math.h> |
| 5 |
|
| 6 |
#include "bio++.H" |
| 7 |
#include "seqStream.H" |
| 8 |
#include "libmeryl.H" |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
|
| 12 |
#define MAX_COVERAGE 51 |
| 13 |
|
| 14 |
// Wed May 20 02:39:41 EDT 2015 |
| 15 |
// |
| 16 |
// This appears to be an analysis of the repeat/unique kmer content of a genome. From 2008. |
| 17 |
// |
| 18 |
// The 'mate rescue' appears to be measuring if mates from a library of some size would |
| 19 |
// anchor a repeat to a unique. I think. |
| 20 |
|
| 21 |
class mateRescueData { |
| 22 |
public: |
| 23 |
mateRescueData() { |
| 24 |
_mean = 0; |
| 25 |
_stddev = 0; |
| 26 |
_coverage = 0; |
| 27 |
_normal = 0L; |
| 28 |
_normalZero = 0; |
| 29 |
}; |
| 30 |
|
| 31 |
void init(int32 mean_, int32 stddev_, uint32 coverage_) { |
| 32 |
_mean = mean_; |
| 33 |
_stddev = stddev_; |
| 34 |
_coverage = coverage_; |
| 35 |
|
| 36 |
assert(_mean > 3 * _stddev); |
| 37 |
|
| 38 |
double a = 1.0 / (_stddev * sqrt(2 * M_PI)); |
| 39 |
double c = 2 * _stddev * _stddev; |
| 40 |
|
| 41 |
int32 b1l = (int32)floor(-3 * _stddev); |
| 42 |
int32 b1h = (int32)ceil ( 3 * _stddev); |
| 43 |
|
| 44 |
_normal = new double [b1h - b1l + 1]; |
| 45 |
_normalZero = -b1l; |
| 46 |
|
| 47 |
for (int32 l=0; l<b1h - b1l + 1; l++) |
| 48 |
_normal[l] = 0.0; |
| 49 |
|
| 50 |
for (int32 l=b1l; l<b1h; l++) |
| 51 |
_normal[l + _normalZero] = a * exp(- l*l / c); |
| 52 |
}; |
| 53 |
~mateRescueData() { |
| 54 |
}; |
| 55 |
|
| 56 |
int32 mean(void) { return(_mean); }; |
| 57 |
int32 stddev(void) { return(_stddev); }; |
| 58 |
uint32 coverage(void) { return(_coverage); }; |
| 59 |
|
| 60 |
double normal(int32 p) { return(_normal[p + _normalZero]); }; |
| 61 |
|
| 62 |
private: |
| 63 |
int32 _mean; |
| 64 |
int32 _stddev; |
| 65 |
uint32 _coverage; |
| 66 |
|
| 67 |
double *_normal; |
| 68 |
int32 _normalZero; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
class merMaskedSequence { |
| 73 |
public: |
| 74 |
merMaskedSequence(char *fastaName_, char *merylName_, uint32 onlySeqIID_=~uint32ZERO) { |
| 75 |
_numSeq = 0; |
| 76 |
_seqLen = 0L; |
| 77 |
_masking = 0L; |
| 78 |
_repeatID = 0L; |
| 79 |
_merSize = 0; |
| 80 |
|
| 81 |
strcpy(_fastaName, fastaName_); |
| 82 |
strcpy(_merylName, merylName_); |
| 83 |
|
| 84 |
strcpy(_maskMersName, _merylName); |
| 85 |
strcat(_maskMersName, ".maskMers"); |
| 86 |
|
| 87 |
if (fileExists(_maskMersName)) |
| 88 |
loadMasking(onlySeqIID_); |
| 89 |
else |
| 90 |
buildMasking(); |
| 91 |
}; |
| 92 |
~merMaskedSequence() { |
| 93 |
delete [] _seqLen; |
| 94 |
for (uint32 i=0; i<_numSeq; i++) { |
| 95 |
delete [] _masking[i]; |
| 96 |
delete [] _repeatID[i]; |
| 97 |
} |
| 98 |
delete [] _masking; |
| 99 |
delete [] _repeatID; |
| 100 |
}; |
| 101 |
|
| 102 |
public: |
| 103 |
uint32 numSeq(void) { return(_numSeq); }; |
| 104 |
int32 seqLen(uint32 i) { return(_seqLen[i]); }; |
| 105 |
char masking(uint32 s, uint32 p) { return(_masking[s][p]); }; |
| 106 |
uint32 repeatID(uint32 s, uint32 p) { return(_repeatID[s][p]); }; |
| 107 |
|
| 108 |
uint32 merSize(void) { return(_merSize); }; |
| 109 |
|
| 110 |
private: |
| 111 |
void loadMasking(uint32 onlySeqIID_=~uint32ZERO); // Read the masking from the saved file |
| 112 |
void saveMasking(void); // Write the masking to a file |
| 113 |
void buildMasking(void); // Read the mers to build the masking |
| 114 |
|
| 115 |
uint32 _numSeq; |
| 116 |
int32 *_seqLen; // signed just for convenience later (positions are signed for same reason) |
| 117 |
char **_masking; |
| 118 |
uint32 **_repeatID; |
| 119 |
|
| 120 |
uint32 _merSize; |
| 121 |
|
| 122 |
char _fastaName[FILENAME_MAX]; |
| 123 |
char _merylName[FILENAME_MAX]; |
| 124 |
char _maskMersName[FILENAME_MAX]; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
void |
| 129 |
merMaskedSequence::loadMasking(uint32 onlySeqIID_) { |
| 130 |
FILE *maskMersFile = fopen(_maskMersName, "r"); |
| 131 |
|
| 132 |
fread(&_numSeq, sizeof(uint32), 1, maskMersFile); |
| 133 |
fread(&_merSize, sizeof(uint32), 1, maskMersFile); |
| 134 |
|
| 135 |
_seqLen = new int32 [_numSeq]; |
| 136 |
_masking = new char * [_numSeq]; |
| 137 |
_repeatID = new uint32 * [_numSeq]; |
| 138 |
|
| 139 |
fprintf(stderr, uint32FMT" sequences in '%s'\n", _numSeq, _fastaName); |
| 140 |
|
| 141 |
fread( _seqLen, sizeof(uint32), _numSeq, maskMersFile); |
| 142 |
|
| 143 |
for (uint32 i=0; i<_numSeq; i++) { |
| 144 |
_masking[i] = 0L; |
| 145 |
_repeatID[i] = 0L; |
| 146 |
|
| 147 |
if ((onlySeqIID_ >= _numSeq) || (onlySeqIID_ == i)) { |
| 148 |
fprintf(stderr, "Loading sequence "uint32FMT" of length "uint32FMT"\n", i, _seqLen[i]); |
| 149 |
|
| 150 |
_masking[i] = new char [_seqLen[i]]; |
| 151 |
_repeatID[i] = new uint32 [_seqLen[i]]; |
| 152 |
|
| 153 |
//memset(_masking[i], 'g', sizeof(char) * _seqLen[i]); |
| 154 |
//memset(_repeatID[i], 0, sizeof(uint32) * _seqLen[i]); |
| 155 |
|
| 156 |
fread(_masking[i], sizeof(char), _seqLen[i], maskMersFile); |
| 157 |
fread(_repeatID[i], sizeof(uint32), _seqLen[i], maskMersFile); |
| 158 |
} else { |
| 159 |
fseek(maskMersFile, sizeof(char) * _seqLen[i], SEEK_CUR); |
| 160 |
fseek(maskMersFile, sizeof(uint32) * _seqLen[i], SEEK_CUR); |
| 161 |
_seqLen[i] = 0; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
fclose(maskMersFile); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
void |
| 170 |
merMaskedSequence::saveMasking(void) { |
| 171 |
FILE *maskMersFile = fopen(_maskMersName, "w"); |
| 172 |
|
| 173 |
fwrite(&_numSeq, sizeof(uint32), 1, maskMersFile); |
| 174 |
fwrite(&_merSize, sizeof(uint32), 1, maskMersFile); |
| 175 |
fwrite( _seqLen, sizeof(uint32), _numSeq, maskMersFile); |
| 176 |
|
| 177 |
for (uint32 i=0; i<_numSeq; i++) { |
| 178 |
fwrite(_masking[i], sizeof(char), _seqLen[i], maskMersFile); |
| 179 |
fwrite(_repeatID[i], sizeof(uint32), _seqLen[i], maskMersFile); |
| 180 |
} |
| 181 |
|
| 182 |
fclose(maskMersFile); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
void |
| 187 |
merMaskedSequence::buildMasking(void) { |
| 188 |
seqStream *STR = new seqStream(_fastaName); |
| 189 |
|
| 190 |
_numSeq = STR->numberOfSequences(); |
| 191 |
|
| 192 |
_seqLen = new int32 [_numSeq]; |
| 193 |
_masking = new char * [_numSeq]; |
| 194 |
_repeatID = new uint32 * [_numSeq]; |
| 195 |
|
| 196 |
_merSize = 0; |
| 197 |
|
| 198 |
fprintf(stderr, uint32FMT" sequences in '%s'\n", _numSeq, _fastaName); |
| 199 |
|
| 200 |
for (uint32 i=0; i<_numSeq; i++) { |
| 201 |
_seqLen[i] = STR->lengthOf(i); |
| 202 |
|
| 203 |
_masking[i] = new char [_seqLen[i]]; |
| 204 |
_repeatID[i] = new uint32 [_seqLen[i]]; |
| 205 |
|
| 206 |
memset(_masking[i], 'g', sizeof(char) * _seqLen[i]); |
| 207 |
memset(_repeatID[i], 0, sizeof(uint32) * _seqLen[i]); |
| 208 |
} |
| 209 |
|
| 210 |
// g -> gap in sequence |
| 211 |
// u -> unique mer |
| 212 |
// r -> repeat mer |
| 213 |
// |
| 214 |
// For all the r's we also need to remember the other locations |
| 215 |
// that repeat is at. We annotate the map with a repeat id, set if |
| 216 |
// another copy of the repeat is nearby. |
| 217 |
|
| 218 |
merylStreamReader *MS = new merylStreamReader(_merylName); |
| 219 |
speedCounter *CT = new speedCounter(" Masking mers in sequence: %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1fffff, true); |
| 220 |
|
| 221 |
uint32 rid = 0; |
| 222 |
|
| 223 |
_merSize = MS->merSize(); |
| 224 |
|
| 225 |
while (MS->nextMer()) { |
| 226 |
//fprintf(stderr, "mer count="uint64FMT" pos="uint32FMT"\n", MS->theCount(), MS->getPosition(0)); |
| 227 |
|
| 228 |
if (MS->theCount() == 1) { |
| 229 |
uint32 p = MS->getPosition(0); |
| 230 |
uint32 s = STR->sequenceNumberOfPosition(p); |
| 231 |
p -= STR->startOf(s); |
| 232 |
|
| 233 |
_masking[s][p] = 'u'; |
| 234 |
} else { |
| 235 |
std::sort(MS->thePositions(), MS->thePositions() + MS->theCount()); |
| 236 |
|
| 237 |
uint32 lastS = ~uint32ZERO; |
| 238 |
uint32 lastP = 0; |
| 239 |
|
| 240 |
rid++; |
| 241 |
|
| 242 |
for (uint32 i=0; i<MS->theCount(); i++) { |
| 243 |
uint32 p = MS->getPosition(i); |
| 244 |
uint32 s = STR->sequenceNumberOfPosition(p); |
| 245 |
p -= STR->startOf(s); |
| 246 |
|
| 247 |
// Always set the masking. |
| 248 |
_masking[s][p] = 'r'; |
| 249 |
|
| 250 |
// If there is a repeat close by, set the repeat ID. |
| 251 |
if ((s == lastS) && (lastP + 40000 > p)) { |
| 252 |
_repeatID[s][lastP] = rid; |
| 253 |
_repeatID[s][p] = rid; |
| 254 |
} |
| 255 |
|
| 256 |
lastS = s; |
| 257 |
lastP = p; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
CT->tick(); |
| 262 |
} |
| 263 |
|
| 264 |
delete CT; |
| 265 |
|
| 266 |
delete MS; |
| 267 |
|
| 268 |
delete STR; |
| 269 |
|
| 270 |
saveMasking(); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
void |
| 275 |
computeDensity(merMaskedSequence *S, char *outputPrefix) { |
| 276 |
char outputName[FILENAME_MAX]; |
| 277 |
FILE *outputFile; |
| 278 |
uint32 windowSizeMax = 10000; |
| 279 |
|
| 280 |
for (uint32 s=0; s<S->numSeq(); s++) { |
| 281 |
|
| 282 |
// seqLen == 0 iff that sequence is not loaded. |
| 283 |
if (S->seqLen(s) == 0) |
| 284 |
continue; |
| 285 |
|
| 286 |
sprintf(outputName, "%s.density.seq"uint32FMTW(02), outputPrefix, s); |
| 287 |
outputFile = fopen(outputName, "w"); |
| 288 |
|
| 289 |
fprintf(stderr, "Starting '%s'\n", outputName); |
| 290 |
|
| 291 |
fprintf(outputFile, "#window\tunique\trepeat\tgaps\n"); |
| 292 |
|
| 293 |
// Not the most efficient, but good enough for us right now. |
| 294 |
|
| 295 |
for (int32 p=0; p<S->seqLen(s); ) { |
| 296 |
uint32 windowSize = 0; |
| 297 |
uint32 uniqueSum = 0; |
| 298 |
uint32 repeatSum = 0; |
| 299 |
uint32 gapSum = 0; |
| 300 |
|
| 301 |
while ((windowSize < windowSizeMax) && |
| 302 |
(p < S->seqLen(s))) { |
| 303 |
char m = S->masking(s, p); |
| 304 |
|
| 305 |
if (m == 'u') uniqueSum++; |
| 306 |
if (m == 'g') gapSum++; |
| 307 |
if (m == 'r') repeatSum++; |
| 308 |
|
| 309 |
windowSize++; |
| 310 |
p++; |
| 311 |
} |
| 312 |
|
| 313 |
fprintf(outputFile, uint32FMT"\t%f\t%f\t%f\n", |
| 314 |
p - windowSize, |
| 315 |
(double)uniqueSum / windowSize, |
| 316 |
(double)repeatSum / windowSize, |
| 317 |
(double)gapSum / windowSize); |
| 318 |
} |
| 319 |
|
| 320 |
fclose(outputFile); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// For each 'r' mer, compute the number of 'u' mers |
| 326 |
// that are within some mean +- stddev range. |
| 327 |
// |
| 328 |
// We count for two blocks: |
| 329 |
// |
| 330 |
// | <- mean -> | <- mean -> | |
| 331 |
// ---[block1]---------------mer---------------[block2]--- |
| 332 |
// |
| 333 |
// Once we know that, we can compute the probability that |
| 334 |
// a repeat mer can be rescued. |
| 335 |
// |
| 336 |
// p1 = uniq/total -- for 1 X coverage |
| 337 |
// pn = 1 - (1-p1)^n -- for n X coverage |
| 338 |
|
| 339 |
|
| 340 |
void |
| 341 |
computeMateRescue(merMaskedSequence *S, char *outputPrefix, mateRescueData *lib, uint32 libLen) { |
| 342 |
char outputName[FILENAME_MAX]; |
| 343 |
FILE *outputFile; |
| 344 |
FILE *outputData; |
| 345 |
|
| 346 |
uint32 closeRepeatsLen = 0; |
| 347 |
uint32 closeRepeatsMax = 80000; |
| 348 |
int32 *closeRepeats = new int32 [closeRepeatsMax]; |
| 349 |
|
| 350 |
speedCounter *CT = new speedCounter(" Examining repeats: %7.2f Kbases -- %5.2f Kbases/second\r", 1000.0, 0x1ffff, true); |
| 351 |
|
| 352 |
uint32 totalDepth = 0; |
| 353 |
for (uint32 l=0; l<libLen; l++) |
| 354 |
totalDepth += lib[l].coverage(); |
| 355 |
|
| 356 |
for (uint32 s=0; s<S->numSeq(); s++) { |
| 357 |
|
| 358 |
// seqLen == 0 iff that sequence is not loaded. |
| 359 |
if (S->seqLen(s) == 0) |
| 360 |
continue; |
| 361 |
|
| 362 |
fprintf(stderr, "Starting sequence "uint32FMT"\n", s); |
| 363 |
|
| 364 |
sprintf(outputName, "%s.mateRescue.seq"uint32FMTW(02)".out", outputPrefix, s); |
| 365 |
outputFile = fopen(outputName, "w"); |
| 366 |
|
| 367 |
sprintf(outputName, "%s.mateRescue.seq"uint32FMTW(02)".dat", outputPrefix, s); |
| 368 |
outputData = fopen(outputName, "w"); |
| 369 |
|
| 370 |
double numRR[MAX_COVERAGE] = {0}; // num repeats rescued (expected) for [] X coverage |
| 371 |
double numNR[MAX_COVERAGE] = {0}; // num repeats nonrescuable (expected) for [] X coverage |
| 372 |
|
| 373 |
uint32 numRT = 0; // num repeats total |
| 374 |
|
| 375 |
for (int32 p=0; p<S->seqLen(s); p++) { |
| 376 |
CT->tick(); |
| 377 |
|
| 378 |
double pRtot = 0.0; |
| 379 |
double pFtot = 0.0; |
| 380 |
|
| 381 |
if ((S->masking(s, p) != 'g') && |
| 382 |
(S->masking(s, p) != 'u') && |
| 383 |
(S->masking(s, p) != 'r')) |
| 384 |
fprintf(stderr, "INVALID MASKING - got %d = %c\n", S->masking(s, p), S->masking(s, p)); |
| 385 |
|
| 386 |
|
| 387 |
if (S->masking(s, p) == 'r') { |
| 388 |
numRT++; |
| 389 |
|
| 390 |
// Index over x-coverage in libraries. MUST BE 1. |
| 391 |
uint32 ridx = 1; |
| 392 |
|
| 393 |
for (uint32 l=0; l<libLen; l++) { |
| 394 |
int32 mean = lib[l].mean(); |
| 395 |
int32 stddev = lib[l].stddev(); |
| 396 |
|
| 397 |
// Build a list of the same repeat close to this guy. |
| 398 |
closeRepeatsLen = 0; |
| 399 |
|
| 400 |
if (S->repeatID(s, p) > 0) { |
| 401 |
int32 pl = (int32)floor(p - 3 * stddev); |
| 402 |
int32 ph = (int32)ceil (p + 3 * stddev); |
| 403 |
|
| 404 |
if (pl < 0) pl = 0; |
| 405 |
if (ph > S->seqLen(s)) ph = S->seqLen(s); |
| 406 |
|
| 407 |
for (int32 pi=pl; pi<ph; pi++) |
| 408 |
if ((S->repeatID(s, pi) == S->repeatID(s, p)) && (pi != p)) |
| 409 |
closeRepeats[closeRepeatsLen++] = pi; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
int32 b1l = (int32)floor(p - mean - 3 * stddev); |
| 414 |
int32 b1h = (int32)ceil (p - mean + 3 * stddev); |
| 415 |
|
| 416 |
int32 b2l = (int32)floor(p + mean - 3 * stddev); |
| 417 |
int32 b2h = (int32)ceil (p + mean + 3 * stddev); |
| 418 |
|
| 419 |
if (b1l < 0) b1l = 0; |
| 420 |
if (b1h < 0) b1h = 0; |
| 421 |
if (b1h > S->seqLen(s)) b1h = S->seqLen(s); |
| 422 |
|
| 423 |
if (b2l < 0) b2l = 0; |
| 424 |
if (b2h > S->seqLen(s)) b2h = S->seqLen(s); |
| 425 |
if (b2l > S->seqLen(s)) b2l = S->seqLen(s); |
| 426 |
|
| 427 |
//fprintf(stderr, "b1: %d-%d b2:%d-%d\n", b1l, b1h, b2l, b2h); |
| 428 |
|
| 429 |
// probability we can rescue this repeat with this mate pair |
| 430 |
double pRescue = 0.0; |
| 431 |
double pFailed = 0.0; |
| 432 |
|
| 433 |
if (closeRepeatsLen == 0) { |
| 434 |
// No close repeats, use the fast method. |
| 435 |
for (int32 b=b1l; b<b1h; b++) { |
| 436 |
if (S->masking(s, b) == 'u') |
| 437 |
pRescue += lib[l].normal(b - p + mean); |
| 438 |
} |
| 439 |
|
| 440 |
for (int32 b=b2l; b<b2h; b++) { |
| 441 |
if (S->masking(s, b) == 'u') |
| 442 |
pRescue += lib[l].normal(b - p - mean); |
| 443 |
} |
| 444 |
} else { |
| 445 |
// Close repeats, gotta be slow. |
| 446 |
for (int32 b=b1l; b<b1h; b++) { |
| 447 |
if (S->masking(s, b) == 'u') { |
| 448 |
int32 mrl = b + mean - 3 * stddev; |
| 449 |
int32 mrh = b + mean + 3 * stddev; |
| 450 |
|
| 451 |
bool rescuable = true; |
| 452 |
|
| 453 |
for (uint32 cri=0; rescuable && cri<closeRepeatsLen; cri++) |
| 454 |
if ((mrl <= closeRepeats[cri]) && (closeRepeats[cri] <= mrh)) |
| 455 |
rescuable = false; |
| 456 |
|
| 457 |
if (rescuable) |
| 458 |
pRescue += lib[l].normal(b - p + mean); |
| 459 |
else |
| 460 |
pFailed += lib[l].normal(b - p + mean); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
for (int32 b=b2l; b<b2h; b++) { |
| 465 |
if (S->masking(s, b) == 'u') { |
| 466 |
int32 mrl = b - mean - 3 * stddev; |
| 467 |
int32 mrh = b - mean + 3 * stddev; |
| 468 |
|
| 469 |
bool rescuable = true; |
| 470 |
|
| 471 |
for (uint32 cri=0; rescuable && cri<closeRepeatsLen; cri++) |
| 472 |
if ((mrl <= closeRepeats[cri]) && (closeRepeats[cri] <= mrh)) |
| 473 |
rescuable = false; |
| 474 |
|
| 475 |
if (rescuable) |
| 476 |
pRescue += lib[l].normal(b - p - mean); |
| 477 |
else |
| 478 |
pFailed += lib[l].normal(b - p - mean); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
// We're summing over two distributions. |
| 484 |
pRescue /= 2.0; |
| 485 |
pFailed /= 2.0; |
| 486 |
|
| 487 |
// Compute probability of rescuing with libraries we've |
| 488 |
// seen already, and the expected number of repeats |
| 489 |
// rescued. |
| 490 |
// |
| 491 |
// We keep track of the probability we rescue this repeat |
| 492 |
// with additional coverage of libraries. First 1x of the |
| 493 |
// first lib, then 2x of the first, etc, etc. |
| 494 |
// |
| 495 |
{ |
| 496 |
double pR = 1.0; |
| 497 |
double pF = 1.0; |
| 498 |
for (uint32 x=0; x<lib[l].coverage(); x++) { |
| 499 |
// Makes it here. pRescue != 1.0 |
| 500 |
pR *= (1.0 - pRescue); |
| 501 |
numRR[ridx] += 1 - pR; |
| 502 |
pRtot += 1 - pR; |
| 503 |
|
| 504 |
pF *= (1.0 - pFailed); |
| 505 |
numNR[ridx] += 1 - pF; |
| 506 |
pFtot += 1 - pF; |
| 507 |
|
| 508 |
ridx++; |
| 509 |
} |
| 510 |
} |
| 511 |
} // over all libs |
| 512 |
|
| 513 |
fprintf(outputData, int32FMT"\t%f\t%f\n", p, pRtot / totalDepth, pFtot / totalDepth); |
| 514 |
|
| 515 |
} // if masking is r |
| 516 |
} // over all positions |
| 517 |
|
| 518 |
fprintf(outputFile, "seqIID\tmerSize\ttRepeat\teRescue\teFailed\tXcov\tmean\tstddev\n"); |
| 519 |
|
| 520 |
for (uint32 x=1, l=0, n=0; l<libLen; x++) { |
| 521 |
fprintf(outputFile, uint32FMT"\t"uint32FMT"\t"uint32FMT"\t%.0f\t%.0f\t"uint32FMT"\t"int32FMT"\t"int32FMT"\n", |
| 522 |
s, S->merSize(), numRT, numRR[x], numNR[x], x, lib[l].mean(), lib[l].stddev()); |
| 523 |
n++; |
| 524 |
if (n >= lib[l].coverage()) { |
| 525 |
l++; |
| 526 |
n = 0; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
fclose(outputFile); |
| 531 |
fclose(outputData); |
| 532 |
} |
| 533 |
|
| 534 |
delete CT; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
int |
| 540 |
main(int argc, char **argv) { |
| 541 |
char *merylName = 0L; |
| 542 |
char *fastaName = 0L; |
| 543 |
char *outputPrefix = 0L; |
| 544 |
|
| 545 |
uint32 onlySeqIID = ~uint32ZERO; |
| 546 |
|
| 547 |
bool doDensity = false; |
| 548 |
bool doRescue = false; |
| 549 |
|
| 550 |
mateRescueData lib[MAX_COVERAGE]; |
| 551 |
uint32 libLen = 0; |
| 552 |
|
| 553 |
int arg=1; |
| 554 |
int err=0; |
| 555 |
while (arg < argc) { |
| 556 |
if (strcmp(argv[arg], "-mers") == 0) { |
| 557 |
merylName = argv[++arg]; |
| 558 |
|
| 559 |
} else if (strcmp(argv[arg], "-seq") == 0) { |
| 560 |
fastaName = argv[++arg]; |
| 561 |
|
| 562 |
} else if (strcmp(argv[arg], "-only") == 0) { |
| 563 |
onlySeqIID = atoi(argv[++arg]); |
| 564 |
|
| 565 |
} else if (strcmp(argv[arg], "-output") == 0) { |
| 566 |
outputPrefix = argv[++arg]; |
| 567 |
|
| 568 |
} else if (strcmp(argv[arg], "-d") == 0) { |
| 569 |
doDensity = true; |
| 570 |
|
| 571 |
} else if (strcmp(argv[arg], "-r") == 0) { |
| 572 |
if (atoi(argv[arg+3]) > 0) { |
| 573 |
doRescue = true; |
| 574 |
lib[libLen++].init(atoi(argv[arg+1]), atoi(argv[arg+2]), atoi(argv[arg+3])); |
| 575 |
} |
| 576 |
arg += 3; |
| 577 |
|
| 578 |
} else { |
| 579 |
fprintf(stderr, "unknown option '%s'\n", argv[arg]); |
| 580 |
err++; |
| 581 |
} |
| 582 |
arg++; |
| 583 |
} |
| 584 |
if ((err) || (merylName == 0L) || (fastaName == 0L) || (outputPrefix == 0L)) { |
| 585 |
fprintf(stderr, "usage: %s -mers mers -seq fasta -output prefix [-d] [-r mean stddev coverage]\n", argv[0]); |
| 586 |
exit(1); |
| 587 |
} |
| 588 |
|
| 589 |
merMaskedSequence *S = new merMaskedSequence(fastaName, merylName, onlySeqIID); |
| 590 |
|
| 591 |
if (doDensity) |
| 592 |
computeDensity(S, outputPrefix); |
| 593 |
|
| 594 |
if (doRescue) |
| 595 |
computeMateRescue(S, outputPrefix, lib, libLen); |
| 596 |
|
| 597 |
return(0); |
| 598 |
} |