| 1 | #cxx . . $ `<file>/meryl/mapMers-depth.C`/include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "bio++.H"cxx . . $ `<file>/libbio/bio++.H`/ |
| 6 | #include "seqCache.H"cxx . . $ `<file>/libseq/seqCache.H`/ |
| 7 | #include "merStream.H"cxx . . $ `<file>/libseq/merStream.H`/ |
| 8 | #include "libmeryl.H"cxx . . $ `<file>/libmeryl/libmeryl.H`/ |
| 9 | #include "existDB.H"cxx . . $ `<file>/libkmer/existDB.H`/ |
| 10 | |
| 11 | // Three outputs: |
| 12 | // |
| 13 | // 1) Number of kmers that span this position. Count of each kmer is ignored. |
| 14 | // 2) Count of the kmer that begins at this position. |
| 15 | // 3) Stats of the counts of the kmers that span this position (e.g., ave, min, max, stddev). |
| 16 | |
| 17 | int |
| 18 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) { |
| 19 | uint32cxx . . $ uint32# merSize = 0; |
| 20 | char *merylFile = 0L; |
| 21 | |
| 22 | char *fastaFile = 0L; |
| 23 | |
| 24 | bool outputCount = false; |
| 25 | bool outputDepth = false; |
| 26 | bool outputStats = false; |
| 27 | |
| 28 | int arg=1; |
| 29 | int err=0; |
| 30 | while (arg < argc) { |
| 31 | |
| 32 | if (strcmp(argv[arg], "-m") == 0) |
| 33 | merSize = strtouint32cxx . . $ `libutil/util.h:82:10`!(argv[++arg], 0L); |
| 34 | |
| 35 | else if (strcmp(argv[arg], "-mers") == 0) |
| 36 | merylFile = argv[++arg]; |
| 37 | |
| 38 | else if (strcmp(argv[arg], "-seq") == 0) |
| 39 | fastaFile = argv[++arg]; |
| 40 | |
| 41 | else if (strcmp(argv[arg], "-count") == 0) |
| 42 | outputCount = true; |
| 43 | else if (strcmp(argv[arg], "-depth") == 0) |
| 44 | outputDepth = true; |
| 45 | else if (strcmp(argv[arg], "-stats") == 0) |
| 46 | outputStats = true; |
| 47 | |
| 48 | else { |
| 49 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "unknown option '%s'\n", argv[arg]); |
| 50 | err++; |
| 51 | } |
| 52 | |
| 53 | arg++; |
| 54 | } |
| 55 | |
| 56 | if (merSize == 0) |
| 57 | err++; |
| 58 | if (fastaFile == 0L) |
| 59 | err++; |
| 60 | if (merylFile == 0L) |
| 61 | err++; |
| 62 | if (outputCount + outputDepth + outputStats != 1) |
| 63 | err++; |
| 64 | |
| 65 | if (err) { |
| 66 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "usage: %s -mers MERYL -m MERSIZE -seq IN.FASTA [-count | -depth | -stats] > output\n", argv[0]); |
| 67 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 68 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "For sequence ordinal 's' and position in that sequence 'p':\n"); |
| 69 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 70 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -count - report the count (c) of the single kmer that starts at position (p).\n"); |
| 71 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " Format: 's p c'\n"); |
| 72 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -depth - report the number (n) of kmers that span position (p). Format: 's p n'\n"); |
| 73 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -stats - report the min (m), max (M), ave (a) count of all mers that span\n"); |
| 74 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " position (p). Format: 's p m M a t n'\n"); |
| 75 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " (also reports total count (t) and number of kmers (n))\n"); |
| 76 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\n"); |
| 77 | |
| 78 | if (merSize == 0) |
| 79 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: No mer size (-m) suppled.\n"); |
| 80 | if (fastaFile == 0L) |
| 81 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: No fasta input (-seq) suppled.\n"); |
| 82 | if (merylFile == 0L) |
| 83 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: No meryl database (-mers) suppled.\n"); |
| 84 | if (outputCount + outputDepth + outputStats != 1) |
| 85 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ERROR: Exactly one of -count, -depth and -stats may be supplied.\n"); |
| 86 | |
| 87 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 88 | } |
| 89 | |
| 90 | // Open the input sequences |
| 91 | |
| 92 | seqCachecxx . . $ seqCache# *F = new seqCachecxx . . $ seqCache#seqCache(cc76bd1196c143a3).(fastaFile); |
| 93 | |
| 94 | // Load kmer counts from a meryl database. existDBcompressBuckets is broken. |
| 95 | |
| 96 | existDBcxx . . $ existDB# *E = new existDBcxx . . $ existDB#(merylFile, merSize, existDBcountscxx . . $ existDBcounts. | existDBcompressCountscxx . . $ existDBcompressCounts., 0, UINT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:118:10`!); |
| 97 | |
| 98 | // For each sequence... |
| 99 | |
| 100 | for (uint32cxx . . $ uint32# Sid=0; Sid < F->getNumberOfSequencescxx . . $ seqCache#getNumberOfSequences(db52760e79bf9054).(); Sid++) { |
| 101 | seqInCorecxx . . $ seqInCore# *S = F->getSequenceInCorecxx . . $ seqCache#getSequenceInCore(715c05706546271f).(Sid); |
| 102 | merStreamcxx . . $ merStream# *MS = new merStreamcxx . . $ merStream#(new kMerBuildercxx . . $ kMerBuilder#(merSize), |
| 103 | new seqStreamcxx . . $ seqStream#(S->sequencecxx . . $ seqInCore#sequence(e7e5aa338b9d5147).(), S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).()), |
| 104 | true, true); |
| 105 | |
| 106 | // Build a lists of the min, max and total count at each position. |
| 107 | |
| 108 | uint32cxx . . $ uint32# *mincount = new uint32cxx . . $ uint32# [S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).() + 1]; |
| 109 | uint32cxx . . $ uint32# *maxcount = new uint32cxx . . $ uint32# [S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).() + 1]; |
| 110 | uint32cxx . . $ uint32# *totcount = new uint32cxx . . $ uint32# [S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).() + 1]; |
| 111 | uint32cxx . . $ uint32# *numcount = new uint32cxx . . $ uint32# [S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).() + 1]; |
| 112 | |
| 113 | for (uint32cxx . . $ uint32# xx=0; xx<S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).() + 1; xx++) { |
| 114 | mincount[xx] = UINT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:118:10`!; |
| 115 | maxcount[xx] = 0; |
| 116 | totcount[xx] = 0; |
| 117 | numcount[xx] = 0; |
| 118 | } |
| 119 | |
| 120 | // Scan the sequence, find the count. |
| 121 | |
| 122 | while (MS->nextMercxx . . $ merStream#nextMer(dfa6e707f7696010).()) { |
| 123 | uint32cxx . . $ uint32# pos = MS->thePositionInSequencecxx . . $ merStream#thePositionInSequence(f708dce9a8856c70).(); |
| 124 | uint32cxx . . $ uint32# cnt = E->countcxx . . $ existDB#count(ecdaa1632f49f591).(MS->theFMercxx . . $ merStream#theFMer(52e25c29fea41909).()) + E->countcxx . . $ existDB#count(ecdaa1632f49f591).(MS->theRMercxx . . $ merStream#theRMer(52e25c29fea41909).()); |
| 125 | |
| 126 | if (cnt == 0) |
| 127 | // Mer doesn't exist in the database. |
| 128 | continue; |
| 129 | |
| 130 | if (outputCount) |
| 131 | totcount[pos] = cnt; |
| 132 | |
| 133 | if (outputDepth) |
| 134 | for (uint32cxx . . $ uint32# xx=pos; xx<pos+merSize; xx++) |
| 135 | numcount[xx]++; |
| 136 | |
| 137 | if (outputStats) |
| 138 | for (uint32cxx . . $ uint32# xx=pos; xx<pos+merSize; xx++) { |
| 139 | totcount[xx] += cnt; |
| 140 | numcount[xx]++; |
| 141 | |
| 142 | if (cnt < mincount[xx]) |
| 143 | mincount[xx] = cnt; |
| 144 | |
| 145 | if (maxcount[xx] < cnt) |
| 146 | maxcount[xx] = cnt; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // If there is no coverage, the min is still set to UINT32_MAX. |
| 151 | |
| 152 | for (uint32cxx . . $ uint32# x=0; x < S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).(); x++) |
| 153 | if (numcount[x] == 0) |
| 154 | mincount[x] = 0; |
| 155 | |
| 156 | // Report the single kmer count? |
| 157 | |
| 158 | if (outputCount) { |
| 159 | for (uint32cxx . . $ uint32# x=0; x < S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).(); x++) |
| 160 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%u\t%u\t%u\n", Sid, x, totcount[x]); |
| 161 | } |
| 162 | |
| 163 | // Report the depth? |
| 164 | |
| 165 | if (outputDepth) { |
| 166 | for (uint32cxx . . $ uint32# x=0; x < S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).(); x++) |
| 167 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%u\t%u\t%u\n", Sid, x, numcount[x]); |
| 168 | } |
| 169 | |
| 170 | // Report the min/max/ave count? |
| 171 | |
| 172 | if (outputStats) { |
| 173 | for (uint32cxx . . $ uint32# x=0; x < S->sequenceLengthcxx . . $ seqInCore#sequenceLength(ddd8539a2fcdd4ce).(); x++) |
| 174 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%u\t%u\t%u\t%u\t%u\t%u\t%u\n", |
| 175 | Sid, x, |
| 176 | mincount[x], |
| 177 | maxcount[x], |
| 178 | (numcount[x] > 0) ? totcount[x] / numcount[x] : 0, |
| 179 | totcount[x], |
| 180 | numcount[x]); |
| 181 | } |
| 182 | |
| 183 | delete [] mincount; |
| 184 | delete [] maxcount; |
| 185 | delete [] totcount; |
| 186 | delete [] numcount; |
| 187 | |
| 188 | delete MS; |
| 189 | delete S; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | delete F; |
| 194 | delete E; |
| 195 | } |