#include #include #include "util++.H" #include "bio++.H" #include "libmeryl.H" #include "existDB.H" #include "seqCache.H" #include "seqStream.H" #include "merStream.H" #include "sweatShop.H" class fastqRecord { public: fastqRecord(uint32 ml) { maxLength = ml; alloc = new char [maxLength * 8]; a2 = alloc + 0 * maxLength; af = alloc + 1 * maxLength; am = alloc + 2 * maxLength; a4 = alloc + 3 * maxLength; a1[0] = 0; a2[0] = 0; af[0] = 0; am[0] = 0; a3[0] = 0; a4[0] = 0; aLength = 0; aMasked = 0.0; aLabel = 0; b2 = alloc + 4 * maxLength; bf = alloc + 5 * maxLength; bm = alloc + 6 * maxLength; b4 = alloc + 7 * maxLength; b1[0] = 0; b2[0] = 0; bf[0] = 0; bm[0] = 0; b3[0] = 0; b4[0] = 0; bLength = 0; bMasked = 0.0; bLabel = 0; }; ~fastqRecord() { delete [] alloc; }; bool load(FILE *FASTQ1, FILE *FASTQ2) { bool tooShort = false; a2[maxLength - 2] = 0; b2[maxLength - 2] = 0; if (FASTQ1) { fgets(a1, 1024, FASTQ1); chomp(a1); fgets(a2, maxLength, FASTQ1); chomp(a2); fgets(a3, 1024, FASTQ1); chomp(a3); fgets(a4, maxLength, FASTQ1); chomp(a4); aLength = strlen(a2); aMasked = 0.0; aLabel = 0; if (a2[maxLength - 2] != 0) tooShort = true; } if (FASTQ2) { fgets(b1, 1024, FASTQ2); chomp(b1); fgets(b2, maxLength, FASTQ2); chomp(b2); fgets(b3, 1024, FASTQ2); chomp(b3); fgets(b4, maxLength, FASTQ2); chomp(b4); bLength = strlen(b2); bMasked = 0.0; bLabel = 0; if (b2[maxLength - 2] != 0) tooShort = true; } if (tooShort) { fprintf(stderr, "ERROR: -l too small for reads:\n"); fprintf(stderr, " a = '%s'\n", a1); fprintf(stderr, " b = '%s'\n", b1); exit(1); } return(!feof(FASTQ1)); }; void write(FILE *FASTQ1, FILE *FASTQ2) { if (FASTQ1) fprintf(FASTQ1, "%s fractionMasked=%.3f\n%s\n%s\n%s\n", a1, aMasked, am, a3, a4); if (FASTQ2) fprintf(FASTQ2, "%s fractionMasked=%.3f\n%s\n%s\n%s\n", b1, bMasked, bm, b3, b4); }; public: uint32 maxLength; char *alloc; char a1[1024]; char *a2; char *af; char *am; char a3[1024]; char *a4; uint32 aLength; double aMasked; uint32 aLabel; char b1[1024]; char *b2; char *bf; char *bm; char b3[1024]; char *b4; uint32 bLength; double bMasked; uint32 bLabel; }; class maskGlobal { public: maskGlobal() { merName = NULL; seq1Name = NULL; seq2Name = NULL; outPrefix = NULL; merSize = 0; maxLength = 512; existName = NULL; minSize = 0; extend = 0; cleaner = false; dirtier = false; discard = true; unlink = false; noMasking = false; cleanThreshold = 1. / 3.; matchThreshold = 2. / 3.; for (uint32 ii=0; ii<1001; ii++) scoreHistogram[ii] = 0; for (uint32 ii=0; ii<4; ii++) for (uint32 jj=0; jj<4; jj++) thresholdCounts[ii][jj] = 0.0; outputHistogram = NULL; exist = NULL; FASTQ1 = NULL; FASTQ1pipe = false; FASTQ2 = NULL; FASTQ2pipe = false; OUTPUT1[0] = NULL; OUTPUT1[1] = NULL; OUTPUT1[2] = NULL; OUTPUT2[0] = NULL; OUTPUT2[1] = NULL; OUTPUT2[2] = NULL; }; ~maskGlobal() { }; public: char *merName; char *seq1Name; char *seq2Name; char *outPrefix; uint32 merSize; uint32 maxLength; char *existName; uint32 minSize; uint32 extend; bool cleaner; bool dirtier; bool discard; bool unlink; bool noMasking; double cleanThreshold; double matchThreshold; uint32 scoreHistogram[1001]; uint64 thresholdCounts[4][4]; char *outputHistogram; existDB *exist; FILE *FASTQ1; bool FASTQ1pipe; FILE *FASTQ2; bool FASTQ2pipe; FILE *OUTPUT1[4]; FILE *OUTPUT2[4]; }; // Masks mers present in the database from the input sequences. Chains together // across small bits of missing mer. void printBits(char *S, uint32 Slen, char *found, char *display, const char *label) { for (uint32 i=0; iexists(MS.theFMer()) || exist->exists(MS.theRMer())) found[MS.thePositionInSequence()] = true; } // Searched for isolated 'true' bits, and removes them. Isolated means fewer than minSize true // bits are adjacent. // void removeIsolatedMers(char *S, uint32 Slen, char *found, uint32 minSize) { uint32 bgn = 0; uint32 end = 0; bool inRun = false; for (uint32 ii=0; ii 0)) { for (uint32 jj=ii; (jj 0) { found[ii] = true; isMasking--; } } } // Assumes the found[] array represents base-based masking. // Returns the fraction of the sequence that is masked. double maskSequence(char *S, uint32 Slen, char *found, bool noMasking, char *display) { uint32 masked = 0; // Flip the sense of the found[ii] test to mask kmers found in the database (true) or missing (false). for (uint32 ii=0; iimaxLength); if (s->load(g->FASTQ1, g->FASTQ2) == false) { delete s; s = NULL; } return(s); } void maskWorker(void *G, void *T, void *S) { maskGlobal *g = (maskGlobal *)G; //maskThread *t = (maskThread *)T; fastqRecord *s = (fastqRecord *)S; buildMask(s->a2, s->aLength, s->af, g->exist, g->merSize); buildMask(s->b2, s->bLength, s->bf, g->exist, g->merSize); //printBits(S, found, display , "INITIAL"); removeIsolatedMers(s->a2, s->aLength, s->af, g->minSize); removeIsolatedMers(s->b2, s->bLength, s->bf, g->minSize); //printBits(S, found, display, "ISOLATED REMOVAL"); convertToBases(s->a2, s->aLength, s->af, g->merSize, g->extend); convertToBases(s->b2, s->bLength, s->bf, g->merSize, g->extend); //printBits(S, found, display, "BASE COVERAGE"); s->aMasked = maskSequence(s->a2, s->aLength, s->af, g->noMasking, s->am); s->bMasked = maskSequence(s->b2, s->bLength, s->bf, g->noMasking, s->bm); s->aLabel = (s->aMasked < g->cleanThreshold) ? 0 : ((s->aMasked < g->matchThreshold) ? 1 : 2); s->bLabel = (s->bMasked < g->cleanThreshold) ? 0 : ((s->bMasked < g->matchThreshold) ? 1 : 2); // If both reads exist, adjust labels. if ((s->aLength > 0) && (s->bLength > 0)) { if ((s->aLabel != s->bLabel) && (g->dirtier)) { s->aLabel = MIN(s->aLabel, s->bLabel); s->bLabel = MIN(s->aLabel, s->bLabel); } if ((s->aLabel != s->bLabel) && (g->cleaner)) { s->aLabel = MAX(s->aLabel, s->bLabel); s->bLabel = MAX(s->aLabel, s->bLabel); } if ((s->aLabel != s->bLabel) && (g->discard)) { s->aLabel = 3; s->bLabel = 3; } } } void fastqWriter(void *G, void *S) { maskGlobal *g = (maskGlobal *)G; fastqRecord *s = (fastqRecord *)S; s->write(g->OUTPUT1[s->aLabel], g->OUTPUT2[s->bLabel]); g->thresholdCounts[s->aLabel][s->bLabel]++; g->scoreHistogram[(uint32)(1000 * s->aMasked)]++; g->scoreHistogram[(uint32)(1000 * s->bMasked)]++; delete s; } FILE * openInput(char *filename, bool &P) { char C[2 * FILENAME_MAX]; FILE *F = NULL; int32 L = (filename == NULL) ? 0 : strlen(filename); if (filename == NULL) return(NULL); if (fileExists(filename) == false) fprintf(stderr, "Failed to open Input file '%s' for reading: File not found.\n", filename), exit(1); if ((L > 3) && (strcmp(filename + L - 3, ".gz") == 0)) { sprintf(C, "gzip -dc %s", filename); F = popen(C, "r"); P = true; } else if ((L > 4) && (strcmp(filename + L - 4, ".bz2") == 0)) { sprintf(C, "bzip2 -dc %s", filename); F = popen(C, "r"); P = true; } else if ((L > 3) && (strcmp(filename + L - 3, ".xz") == 0)) { sprintf(C, "xz -dc %s", filename); F = popen(C, "r"); P = true; } else { errno = 0; F = fopen(filename, "r"); if (errno) fprintf(stderr, "Failed to open input file '%s' for reading: %s\n", filename, strerror(errno)), exit(1); P = false; } return(F); } void closeInput(FILE *F, char *filename, bool P) { if (F == NULL) return; if (P) pclose(F); else fclose(F); } FILE * openOutput(char *prefix, const char *extension) { char N[FILENAME_MAX]; FILE *F = NULL; errno = 0; sprintf(N, "%s.%s.fastq", prefix, extension); F = fopen(N, "w"); if (errno) fprintf(stderr, "ERROR: failed to open '%s': %s\n", N, strerror(errno)), exit(1); return(F); } void closeOutput(FILE *F, char *prefix, const char *extension) { if (F) fclose(F); } int main(int argc, char **argv) { maskGlobal *g = new maskGlobal(); uint32 numWorkers = 1; bool beVerbose = false; int32 arg=1; int32 err=0; while (arg < argc) { if (strcmp(argv[arg], "-mdb") == 0) { g->merName = argv[++arg]; } else if (strcmp(argv[arg], "-ms") == 0) { g->merSize = atoi(argv[++arg]); } else if (strcmp(argv[arg], "-l") == 0) { g->maxLength = atoi(argv[++arg]); } else if (strcmp(argv[arg], "-edb") == 0) { g->existName = argv[++arg]; } else if (strcmp(argv[arg], "-1") == 0) { g->seq1Name = argv[++arg]; } else if (strcmp(argv[arg], "-2") == 0) { g->seq2Name = argv[++arg]; } else if (strcmp(argv[arg], "-o") == 0) { g->outPrefix = argv[++arg]; } else if (strcmp(argv[arg], "-m") == 0) { g->minSize = atoi(argv[++arg]); } else if (strcmp(argv[arg], "-e") == 0) { g->extend = atoi(argv[++arg]); } else if (strcmp(argv[arg], "-t") == 0) { numWorkers = atoi(argv[++arg]); } else if (strcmp(argv[arg], "-v") == 0) { beVerbose = true; } else if (strcmp(argv[arg], "-cleaner") == 0) { g->dirtier = false; g->cleaner = true; g->discard = false; g->unlink = false; } else if (strcmp(argv[arg], "-dirtier") == 0) { g->dirtier = true; g->cleaner = false; g->discard = false; g->unlink = false; } else if (strcmp(argv[arg], "-discard") == 0) { g->dirtier = false; g->cleaner = false; g->discard = true; g->unlink = false; } else if (strcmp(argv[arg], "-unlink") == 0) { g->dirtier = false; g->cleaner = false; g->discard = false; g->unlink = true; } else if (strcmp(argv[arg], "-nomasking") == 0) { g->noMasking = true; } else if (strncmp(argv[arg], "-clean", 3) == 0) { g->cleanThreshold = atof(argv[++arg]); } else if (strncmp(argv[arg], "-match", 3) == 0) { g->matchThreshold = atof(argv[++arg]); } else if (strcmp(argv[arg], "-h") == 0) { g->outputHistogram = argv[++arg]; } else { err++; } arg++; } if ((g->outPrefix == NULL) && (g->seq1Name != NULL)) err++; if ((g->merName == NULL) && (g->existName == NULL)) err++; if (err) { fprintf(stderr, "usage: %s -mdb mer-database -ms mer-size ...\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "INPUTS:\n"); fprintf(stderr, "\n"); fprintf(stderr, " -mdb mer-database load masking kmers from meryl 'mer-database'\n"); fprintf(stderr, " -ms mer-size the mer size used to generate the meryl database\n"); fprintf(stderr, " -edb exist-database save masking kmers to 'exist-database', and reuse on\n"); fprintf(stderr, " future runs (optional)\n"); fprintf(stderr, "\n"); fprintf(stderr, " -1 in.1.fastq input reads - fastq, fastq.gz, fastq.bz2 or fastq.xz\n"); fprintf(stderr, " -2 in.2.fastq - optional, for mated reads\n"); fprintf(stderr, "\n"); fprintf(stderr, " -l length maximum length of input read (%u)\n", g->maxLength); fprintf(stderr, " If too small, program will fail.\n"); fprintf(stderr, " If too large, program will use excessive memory.\n"); fprintf(stderr, "\n"); fprintf(stderr, "THRESHOLDS and OPTIONS:\n"); fprintf(stderr, "\n"); fprintf(stderr, " A read with fewer than 'c' bases masked is 'clean', while a read with more than 'd' bases\n"); fprintf(stderr, " masked is 'match'ed. Reads in between are 'murky'. See OUTPUTS.\n"); fprintf(stderr, "\n"); fprintf(stderr, " -clean c mark reads with less than this fraction masked as 'clean' (%.4f)\n", g->cleanThreshold); fprintf(stderr, " -match d mark reads with more than this fraction masked as 'match' (%.4f)\n", g->matchThreshold); fprintf(stderr, "\n"); fprintf(stderr, " -m min-size ignore database hits below this many consecutive kmers (%d)\n", g->minSize); fprintf(stderr, " -e extend-size extend database hits across this many missing kmers (%d)\n", g->extend); fprintf(stderr, "\n"); fprintf(stderr, " For mate pairs, how to handle reads with different classifications:\n"); fprintf(stderr, "\n"); fprintf(stderr, " -cleaner use the cleaner classification of the two reads\n"); fprintf(stderr, " -dirtier use the dirtier classification of the two reads\n"); fprintf(stderr, " -discard discard pairs with conflicting classifications (DEFAULT)\n"); fprintf(stderr, " -unlink leave conflicting status alone, output reads to different files\n"); fprintf(stderr, " NOTE: mate pairing will be broken\n"); fprintf(stderr, "\n"); fprintf(stderr, " -nomasking do not trim masked sequence; output the original read\n"); fprintf(stderr, "\n"); fprintf(stderr, "OUTPUTS:\n"); fprintf(stderr, "\n"); fprintf(stderr, " -o prefix output reads:\n"); fprintf(stderr, " prefix.clean.[12].fastq - clean (unmasked) reads\n"); fprintf(stderr, " prefix.murky.[12].fastq - reads in between\n"); fprintf(stderr, " prefix.match.[12].fastq - matching (masked) reads\n"); fprintf(stderr, " prefix.mixed.[12].fastq - reads with conflicting status (for mated reads)\n"); fprintf(stderr, " -h histogram write a histogram of the amount of sequence RETAINED\n"); fprintf(stderr, "\n"); fprintf(stderr, "COMPUTE CONFIGURATION and LOGGING\n"); fprintf(stderr, "\n"); fprintf(stderr, " -t t use 't' compute threads\n"); fprintf(stderr, " -v show progress\n"); if ((g->outPrefix == NULL) && (g->seq1Name != NULL)) fprintf(stderr, "ERROR: an output prefix (-o) must be supplied.\n"); if ((g->merName == NULL) && (g->existName == NULL)) fprintf(stderr, "ERROR: either a mer-database (-mdb) or an exist-database (-edb) must be supplied.\n"); exit(1); } // Open inputs and outputs g->FASTQ1 = openInput(g->seq1Name, g->FASTQ1pipe); g->FASTQ2 = openInput(g->seq2Name, g->FASTQ2pipe); if (g->FASTQ1) { g->OUTPUT1[0] = openOutput(g->outPrefix, "clean.1"); // Label == 0 g->OUTPUT1[1] = openOutput(g->outPrefix, "murky.1"); // Label == 1 g->OUTPUT1[2] = openOutput(g->outPrefix, "match.1"); // Label == 2 g->OUTPUT1[3] = openOutput(g->outPrefix, "mixed.1"); // Label == 3 } if (g->FASTQ2) { g->OUTPUT2[0] = openOutput(g->outPrefix, "clean.2"); g->OUTPUT2[1] = openOutput(g->outPrefix, "murky.2"); g->OUTPUT2[2] = openOutput(g->outPrefix, "match.2"); g->OUTPUT2[3] = openOutput(g->outPrefix, "mixed.2"); } // Load data if ((g->existName != NULL) && (fileExists(g->existName))) { if (beVerbose) fprintf(stderr, "Load existDB existName='%s'.\n", g->existName); g->exist = new existDB(g->existName); } else { if (beVerbose) fprintf(stderr, "Build existDB from merName='%s'.\n", g->merName); g->exist = new existDB(g->merName, g->merSize, existDBnoFlags, 0, ~uint32ZERO); if (g->existName != NULL) { if (beVerbose) fprintf(stderr, "Save existDB into existName='%s'.\n", g->existName); g->exist->saveState(g->existName); } } // Process! if (g->FASTQ1) { sweatShop *ss = new sweatShop(fastqLoader, maskWorker, fastqWriter); ss->setNumberOfWorkers(numWorkers); ss->setWorkerBatchSize(1024); ss->setLoaderQueueSize(numWorkers * 81920); ss->setWriterQueueSize(numWorkers * 81920); ss->run(g, beVerbose); closeInput(g->FASTQ1, g->seq1Name, g->FASTQ1pipe); closeInput(g->FASTQ2, g->seq1Name, g->FASTQ2pipe); closeOutput(g->OUTPUT1[0], g->outPrefix, "clean.1"); closeOutput(g->OUTPUT1[1], g->outPrefix, "murky.1"); closeOutput(g->OUTPUT1[2], g->outPrefix, "match.1"); closeOutput(g->OUTPUT1[3], g->outPrefix, "mixed.1"); closeOutput(g->OUTPUT2[0], g->outPrefix, "clean.2"); closeOutput(g->OUTPUT2[1], g->outPrefix, "murky.2"); closeOutput(g->OUTPUT2[2], g->outPrefix, "match.2"); closeOutput(g->OUTPUT2[3], g->outPrefix, "mixed.2"); #define FW uint64FMTW(8) if (g->FASTQ2 == NULL) { fprintf(stderr, "aClean "FW"\n", g->thresholdCounts[0][0]); fprintf(stderr, "aMurky "FW"\n", g->thresholdCounts[1][0]); fprintf(stderr, "aMatch "FW"\n", g->thresholdCounts[2][0]); fprintf(stderr, "aMixed "FW"\n", g->thresholdCounts[3][0]); } else { fprintf(stderr, " bClean bMurky bMatch bMixed\n"); fprintf(stderr, "aClean "FW" "FW" "FW" "FW"\n", g->thresholdCounts[0][0], g->thresholdCounts[0][1], g->thresholdCounts[0][2], g->thresholdCounts[0][3]); fprintf(stderr, "aMurky "FW" "FW" "FW" "FW"\n", g->thresholdCounts[1][0], g->thresholdCounts[1][1], g->thresholdCounts[1][2], g->thresholdCounts[1][3]); fprintf(stderr, "aMatch "FW" "FW" "FW" "FW"\n", g->thresholdCounts[2][0], g->thresholdCounts[2][1], g->thresholdCounts[2][2], g->thresholdCounts[2][3]); fprintf(stderr, "aMixed "FW" "FW" "FW" "FW"\n", g->thresholdCounts[3][0], g->thresholdCounts[3][1], g->thresholdCounts[3][2], g->thresholdCounts[3][3]); } #undef FW if (g->outputHistogram != NULL) { FILE *H = fopen(g->outputHistogram, "w"); fprintf(H, "# fraction-masked number-of-sequences\n"); for (uint32 i=0; i<1001; i++) if (g->scoreHistogram[i] > 0) fprintf(H, "%.4f\t%u\n", i / 1000.0, g->scoreHistogram[i]); fclose(H); } } delete g->exist; delete g; exit(0); }