Debian Codegraph @

meryl/mapMers-depth.C

raw symbols SCIP
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 // 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 main(int argc, char **argv) {
19 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 = strtouint32(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(stderr, "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(stderr, "usage: %s -mers MERYL -m MERSIZE -seq IN.FASTA [-count | -depth | -stats] > output\n", argv[0]);
67 fprintf(stderr, "\n");
68 fprintf(stderr, "For sequence ordinal 's' and position in that sequence 'p':\n");
69 fprintf(stderr, "\n");
70 fprintf(stderr, " -count - report the count (c) of the single kmer that starts at position (p).\n");
71 fprintf(stderr, " Format: 's p c'\n");
72 fprintf(stderr, " -depth - report the number (n) of kmers that span position (p). Format: 's p n'\n");
73 fprintf(stderr, " -stats - report the min (m), max (M), ave (a) count of all mers that span\n");
74 fprintf(stderr, " position (p). Format: 's p m M a t n'\n");
75 fprintf(stderr, " (also reports total count (t) and number of kmers (n))\n");
76 fprintf(stderr, "\n");
77
78 if (merSize == 0)
79 fprintf(stderr, "ERROR: No mer size (-m) suppled.\n");
80 if (fastaFile == 0L)
81 fprintf(stderr, "ERROR: No fasta input (-seq) suppled.\n");
82 if (merylFile == 0L)
83 fprintf(stderr, "ERROR: No meryl database (-mers) suppled.\n");
84 if (outputCount + outputDepth + outputStats != 1)
85 fprintf(stderr, "ERROR: Exactly one of -count, -depth and -stats may be supplied.\n");
86
87 exit(1);
88 }
89
90 // Open the input sequences
91
92 seqCache *F = new seqCache(fastaFile);
93
94 // Load kmer counts from a meryl database. existDBcompressBuckets is broken.
95
96 existDB *E = new existDB(merylFile, merSize, existDBcounts | existDBcompressCounts, 0, UINT32_MAX);
97
98 // For each sequence...
99
100 for (uint32 Sid=0; Sid < F->getNumberOfSequences(); Sid++) {
101 seqInCore *S = F->getSequenceInCore(Sid);
102 merStream *MS = new merStream(new kMerBuilder(merSize),
103 new seqStream(S->sequence(), S->sequenceLength()),
104 true, true);
105
106 // Build a lists of the min, max and total count at each position.
107
108 uint32 *mincount = new uint32 [S->sequenceLength() + 1];
109 uint32 *maxcount = new uint32 [S->sequenceLength() + 1];
110 uint32 *totcount = new uint32 [S->sequenceLength() + 1];
111 uint32 *numcount = new uint32 [S->sequenceLength() + 1];
112
113 for (uint32 xx=0; xx<S->sequenceLength() + 1; xx++) {
114 mincount[xx] = UINT32_MAX;
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->nextMer()) {
123 uint32 pos = MS->thePositionInSequence();
124 uint32 cnt = E->count(MS->theFMer()) + E->count(MS->theRMer());
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 (uint32 xx=pos; xx<pos+merSize; xx++)
135 numcount[xx]++;
136
137 if (outputStats)
138 for (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 (uint32 x=0; x < S->sequenceLength(); x++)
153 if (numcount[x] == 0)
154 mincount[x] = 0;
155
156 // Report the single kmer count?
157
158 if (outputCount) {
159 for (uint32 x=0; x < S->sequenceLength(); x++)
160 fprintf(stdout, "%u\t%u\t%u\n", Sid, x, totcount[x]);
161 }
162
163 // Report the depth?
164
165 if (outputDepth) {
166 for (uint32 x=0; x < S->sequenceLength(); x++)
167 fprintf(stdout, "%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 (uint32 x=0; x < S->sequenceLength(); x++)
174 fprintf(stdout, "%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 }