| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "bio++.H" |
| 6 | #include "seqCache.H" |
| 7 | #include "merStream.H" |
| 8 | #include "libmeryl.H" |
| 9 | #include "existDB.H" |
| 10 | |
| 11 | #define OP_NONE 0 |
| 12 | #define OP_STATS 1 |
| 13 | #define OP_REGIONS 2 |
| 14 | #define OP_DETAILS 3 |
| 15 | |
| 16 | int |
| 17 | main(int argc, char **argv) { |
| 18 | uint32 merSize = 16; |
| 19 | char *merylFile = 0L; |
| 20 | char *fastaFile = 0L; |
| 21 | bool beVerbose = false; |
| 22 | uint32 loCount = 0; |
| 23 | uint32 hiCount = ~uint32ZERO; |
| 24 | uint32 operation = OP_NONE; |
| 25 | |
| 26 | // For OP_STATS |
| 27 | |
| 28 | uint32 Clen = 0; |
| 29 | uint32 Cmax = 4 * 1024 * 1024; |
| 30 | uint32 *C = new uint32 [Cmax]; |
| 31 | |
| 32 | int arg=1; |
| 33 | while (arg < argc) { |
| 34 | if (strcmp(argv[arg], "-m") == 0) { |
| 35 | merSize = strtouint32(argv[++arg], 0L); |
| 36 | |
| 37 | } else if (strcmp(argv[arg], "-mers") == 0) { |
| 38 | merylFile = argv[++arg]; |
| 39 | |
| 40 | } else if (strcmp(argv[arg], "-seq") == 0) { |
| 41 | fastaFile = argv[++arg]; |
| 42 | |
| 43 | } else if (strcmp(argv[arg], "-v") == 0) { |
| 44 | beVerbose = true; |
| 45 | |
| 46 | } else if (strcmp(argv[arg], "-lo") == 0) { |
| 47 | loCount = strtouint32(argv[++arg], 0L); |
| 48 | |
| 49 | } else if (strcmp(argv[arg], "-hi") == 0) { |
| 50 | hiCount = strtouint32(argv[++arg], 0L); |
| 51 | |
| 52 | } else if (strcmp(argv[arg], "-stats") == 0) { |
| 53 | operation = OP_STATS; |
| 54 | |
| 55 | } else if (strcmp(argv[arg], "-regions") == 0) { |
| 56 | operation = OP_REGIONS; |
| 57 | |
| 58 | } else if (strcmp(argv[arg], "-details") == 0) { |
| 59 | operation = OP_DETAILS; |
| 60 | |
| 61 | } else { |
| 62 | fprintf(stderr, "unknown option '%s'\n", argv[arg]); |
| 63 | } |
| 64 | arg++; |
| 65 | } |
| 66 | |
| 67 | if ((operation == OP_NONE) || (merylFile == 0L) || (fastaFile == 0L)) { |
| 68 | fprintf(stderr, "usage: %s [-stats | -regions | -details] -m mersize -mers mers -seq fasta > output\n", argv[0]); |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | #if 0 |
| 73 | existDB *E = NULL; |
| 74 | |
| 75 | if (fileExists("junk.existDB")) { |
| 76 | fprintf(stderr, "loading from junk.existDB\n"); |
| 77 | E = new existDB("junk.existDB"); |
| 78 | fprintf(stderr, "loaded\n"); |
| 79 | } else { |
| 80 | exit(1); |
| 81 | E = new existDB(merylFile, merSize, existDBcounts, loCount, hiCount); |
| 82 | E->saveState("junk.existDB"); |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | existDB *E = new existDB(merylFile, merSize, existDBcounts, loCount, hiCount); |
| 87 | seqCache *F = new seqCache(fastaFile); |
| 88 | |
| 89 | fprintf(stderr, "Begin.\n"); |
| 90 | |
| 91 | |
| 92 | for (uint32 Sid=0; Sid < F->getNumberOfSequences(); Sid++) { |
| 93 | seqInCore *S = F->getSequenceInCore(Sid); |
| 94 | merStream *MS = new merStream(new kMerBuilder(merSize), |
| 95 | new seqStream(S->sequence(), S->sequenceLength()), |
| 96 | true, true); |
| 97 | |
| 98 | // with counts, report mean, mode, median, min, max for each frag. |
| 99 | if (operation == OP_STATS) { |
| 100 | |
| 101 | Clen = 0; |
| 102 | |
| 103 | while (MS->nextMer()) |
| 104 | C[Clen++] = E->count(MS->theFMer()) + E->count(MS->theRMer()); |
| 105 | |
| 106 | uint64 mean = uint64ZERO; |
| 107 | uint64 min = ~uint64ZERO; |
| 108 | uint64 max = uint64ZERO; |
| 109 | uint64 hist[16] = { 0 }; |
| 110 | |
| 111 | // Histogram values are powers of two, e.g., <=1, <=2, <=4, <=8, <=16, <=32, <=64, <=128, <=256, <=512, <=1024, <=4096, <=8192, <=328768 |
| 112 | |
| 113 | for (uint32 i=0; i<Clen; i++) { |
| 114 | mean += C[i]; |
| 115 | |
| 116 | if (min > C[i]) |
| 117 | min = C[i]; |
| 118 | if (max < C[i]) |
| 119 | max = C[i]; |
| 120 | |
| 121 | hist[ logBaseTwo64(C[i]) ]++; |
| 122 | } |
| 123 | |
| 124 | if (Clen > 0) { |
| 125 | mean /= Clen; |
| 126 | |
| 127 | } else { |
| 128 | mean = uint64ZERO; |
| 129 | min = uint64ZERO; |
| 130 | max = uint64ZERO; |
| 131 | } |
| 132 | |
| 133 | fprintf(stdout, |
| 134 | "%s\t" |
| 135 | uint64FMT"\t"uint64FMT"\t"uint64FMT"\t" |
| 136 | uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t" |
| 137 | uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\n", |
| 138 | S->header(), |
| 139 | mean, min, max, |
| 140 | hist[ 0], hist[ 1], hist[ 2], hist[ 3], hist[ 4], hist[ 5], hist[ 6], hist[ 7], |
| 141 | hist[ 8], hist[ 9], hist[10], hist[11], hist[12], hist[13], hist[14], hist[15]); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | // without counts, reports regions with mer coverage. |
| 146 | // Orientation tells us nothing, since the mers are probably canonical |
| 147 | if (operation == OP_REGIONS) { |
| 148 | uint64 beg = ~uint64ZERO; |
| 149 | uint64 end = ~uint64ZERO; |
| 150 | uint64 pos = ~uint64ZERO; |
| 151 | |
| 152 | uint64 numCovReg = 0; |
| 153 | uint64 lenCovReg = 0; |
| 154 | |
| 155 | while (MS->nextMer()) { |
| 156 | if (E->exists(MS->theFMer()) || E->exists(MS->theRMer())) { |
| 157 | pos = MS->thePositionInSequence(); |
| 158 | |
| 159 | if (beg == ~uint64ZERO) |
| 160 | beg = end = pos; |
| 161 | |
| 162 | if (pos <= end + merSize) { |
| 163 | end = pos; |
| 164 | } else { |
| 165 | fprintf(stdout, "%s\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\n", S->header(), beg, end+merSize, end+merSize - beg); |
| 166 | numCovReg++; |
| 167 | lenCovReg += end+merSize - beg; |
| 168 | beg = end = pos; |
| 169 | } |
| 170 | } else { |
| 171 | fprintf(stdout, "%s\t"uint64FMT"\tuncovered\n", S->header(), MS->thePositionInSequence()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (beg != ~uint64ZERO) |
| 176 | fprintf(stdout, "%s\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\n", S->header(), beg, end+merSize, end+merSize - beg); |
| 177 | |
| 178 | fprintf(stderr, "numCovReg: "uint64FMT"\n", numCovReg); |
| 179 | fprintf(stderr, "lenCovReg: "uint64FMT"\n", lenCovReg); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | if (operation == OP_DETAILS) { |
| 185 | char merString[256]; |
| 186 | |
| 187 | while (MS->nextMer()) { |
| 188 | uint64 beg = MS->thePositionInSequence(); |
| 189 | uint64 end = beg + merSize; |
| 190 | uint64 fnt = E->count(MS->theFMer()); |
| 191 | uint64 rnt = E->count(MS->theRMer()); |
| 192 | |
| 193 | fprintf(stdout, "%s\t%s\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\t"uint64FMT"\n", |
| 194 | S->header(), |
| 195 | MS->theFMer().merToString(merString), |
| 196 | beg, |
| 197 | end, |
| 198 | fnt, |
| 199 | rnt, |
| 200 | fnt + rnt); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | delete MS; |
| 206 | delete S; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | delete F; |
| 211 | delete E; |
| 212 | } |