Debian Codegraph @

libkmer/driver-posDB.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "bio++.H"
7 #include "existDB.H"
8 #include "positionDB.H"
9
10 #include "seqCache.H"
11 #include "seqStream.H"
12 #include "merStream.H"
13
14 // Driver for the positionDB creation. Reads a sequence.fasta, builds
15 // a positionDB for the mers in the file, and then writes the internal
16 // structures to disk.
17 //
18 // The positionDB constructor is smart enough to read either a pre-built
19 // image or a regular multi-fasta file.
20
21
22 #define MERSIZE 20
23
24
25 int
26 test1(char *filename) {
27 merStream *T = new merStream(new kMerBuilder(MERSIZE), new seqStream(filename), true, true);
28 positionDB *M = new positionDB(T, MERSIZE, 0, 0L, 0L, 0L, 0, 0, 0, 0, true);
29 uint64 *posn = new uint64 [1024];
30 uint64 posnMax = 1024;
31 uint64 posnLen = uint64ZERO;
32 uint64 count = uint64ZERO;
33 uint32 missing = uint32ZERO;
34 uint32 failed = uint32ZERO;
35 char str[33];
36
37 T->rewind();
38
39 while (T->nextMer()) {
40 if (M->getExact(T->theFMer(),
41 posn,
42 posnMax,
43 posnLen,
44 count)) {
45
46 missing = uint32ZERO;
47 for (uint32 i=0; i<posnLen; i++)
48 if (posn[i] == T->thePositionInStream())
49 missing++;
50
51 if (missing != 1) {
52 failed++;
53
54 fprintf(stdout, "%s @ "uint64FMT"/"uint64FMT": Found "uint64FMT" table entries, and "uint32FMT" matching positions (",
55 T->theFMer().merToString(str), T->theSequenceNumber(), T->thePositionInStream(), posnLen, missing);
56
57 for (uint32 i=0; i<posnLen; i++) {
58 fprintf(stdout, uint64FMT, posn[i]);
59 if (i < posnLen - 1)
60 fprintf(stdout, " ");
61 else
62 fprintf(stdout, ")\n");
63 }
64 }
65 } else {
66 failed++;
67
68 fprintf(stdout, "Found no matches for mer=%s at pos="uint64FMT"\n",
69 T->theFMer().merToString(str), T->thePositionInStream());
70 }
71 }
72
73 delete M;
74 delete T;
75
76 return(failed != 0);
77 }
78
79
80
81 int
82 test2(char *filename, char *query) {
83 merStream *T = new merStream(new kMerBuilder(MERSIZE), new seqStream(filename), true, true);
84 positionDB *M = new positionDB(T, MERSIZE, 0, 0L, 0L, 0L, 0, 0, 0, 0, true);
85 uint64 *posn = new uint64 [1024];
86 uint64 posnMax = 1024;
87 uint64 posnLen = uint64ZERO;
88 uint64 count = uint64ZERO;
89 char str[33];
90
91 delete T;
92
93 T = new merStream(new kMerBuilder(MERSIZE), new seqStream(query), true, true);
94
95 while (T->nextMer()) {
96 if (M->getExact(T->theFMer(),
97 posn,
98 posnMax,
99 posnLen,
100 count)) {
101 fprintf(stdout, "Got a F match for mer=%s at "uint64FMT"/"uint64FMT" (in mers), numMatches="uint64FMT"\n",
102 T->theFMer().merToString(str), T->theSequenceNumber(), T->thePositionInStream(), posnLen);
103 }
104
105 if (M->getExact(T->theRMer(),
106 posn,
107 posnMax,
108 posnLen,
109 count)) {
110 fprintf(stdout, "Got a R match for mer=%s at "uint64FMT"/"uint64FMT" (in mers), numMatches="uint64FMT"\n",
111 T->theRMer().merToString(str), T->theSequenceNumber(), T->thePositionInStream(), posnLen);
112 }
113 }
114
115 delete M;
116 delete T;
117
118 return(0);
119 }
120
121
122
123 // Builds a positionDB possibly using a subset of the file.
124 //
125 // Subset on entire sequences:
126 // -use x-y,a,b
127 //
128 // Subset on a range of mers, in this case, use only the 1000th
129 // through 1999th (inclusive) mer:
130 // -merbegin 1000 -merend 2000
131 //
132 // Or do both, use the first 1000 mers from the 3rd sequence:
133 // -use 3 -merbegin 0 -merend 1000
134
135
136
137 int
138 main(int argc, char **argv) {
139 uint32 mersize = 20;
140 uint32 merskip = 0;
141
142 char *maskF = 0L;
143 char *onlyF = 0L;
144
145 uint64 merBegin = ~uint64ZERO;
146 uint64 merEnd = ~uint64ZERO;
147
148 char *sequenceFile = 0L;
149 char *outputFile = 0L;
150
151 if (argc < 3) {
152 fprintf(stderr, "usage: %s [args]\n", argv[0]);
153 fprintf(stderr, " -mersize k The size of the mers, default=20.\n");
154 fprintf(stderr, " -merskip k The skip between mers, default=0\n");
155 fprintf(stderr, " -use a-b,c Specify which sequences to use, default=all\n");
156 fprintf(stderr, " -merbegin b Build on a subset of the mers, starting at mer #b, default=all mers\n");
157 fprintf(stderr, " -merend e Build on a subset of the mers, ending at mer #e, default=all mers\n");
158 fprintf(stderr, " -sequence s.fasta Input sequences.\n");
159 fprintf(stderr, " -output p.posDB Output filename.\n");
160 fprintf(stderr, "\n");
161 fprintf(stderr, " To dump information about an image:\n");
162 fprintf(stderr, " -dump datafile\n");
163 fprintf(stderr, "\n");
164 fprintf(stderr, " To run sanity tests:\n");
165 fprintf(stderr, " -buildonly [build opts] sequence.fasta\n");
166 fprintf(stderr, " -- just builds a table and exits\n");
167 fprintf(stderr, " -existence [build opts] sequence.fasta\n");
168 fprintf(stderr, " -- builds (or reads) a table reports if any mers\n");
169 fprintf(stderr, " in sequence.fasta cannot be found\n");
170 fprintf(stderr, " -extra [build opts] sequence.fasta\n");
171 fprintf(stderr, " -- builds (or reads) a table reports if any mers\n");
172 fprintf(stderr, " NOT in sequence.fasta are be found\n");
173 fprintf(stderr, " -test1 sequence.fasta\n");
174 fprintf(stderr, " -- Tests if each and every mer is found in the\n");
175 fprintf(stderr, " positionDB. Reports if it doesn't find a mer\n");
176 fprintf(stderr, " at the correct position. Doesn't report if table\n");
177 fprintf(stderr, " has too much stuff.\n");
178 fprintf(stderr, " -test2 db.fasta sequence.fasta\n");
179 fprintf(stderr, " -- Builds a positionDB from db.fasta, then searches\n");
180 fprintf(stderr, " the table for each mer in sequence.fasta. Reports\n");
181 fprintf(stderr, " all mers it finds.\n");
182 fprintf(stderr, " -- This is a silly test and you shouldn't do it.\n");
183 exit(1);
184 }
185
186 int arg = 1;
187 while (arg < argc) {
188 if (strcmp(argv[arg], "-mersize") == 0) {
189 mersize = strtouint32(argv[++arg], 0L);
190
191 } else if (strcmp(argv[arg], "-merskip") == 0) {
192 merskip = strtouint32(argv[++arg], 0L);
193
194 } else if (strcmp(argv[arg], "-mask") == 0) {
195 maskF = argv[++arg];
196 } else if (strcmp(argv[arg], "-only") == 0) {
197 onlyF = argv[++arg];
198
199 } else if (strcmp(argv[arg], "-merbegin") == 0) {
200 merBegin = strtouint64(argv[++arg], 0L);
201
202 } else if (strcmp(argv[arg], "-merend") == 0) {
203 merEnd = strtouint64(argv[++arg], 0L);
204
205 } else if (strcmp(argv[arg], "-sequence") == 0) {
206 sequenceFile = argv[++arg];
207
208 } else if (strcmp(argv[arg], "-output") == 0) {
209 outputFile = argv[++arg];
210
211 } else if (strcmp(argv[arg], "-dump") == 0) {
212 positionDB *e = new positionDB(argv[++arg], 0, 0, 0, false);
213 e->printState(stdout);
214 delete e;
215 exit(0);
216 } else if (strcmp(argv[arg], "-test1") == 0) {
217 exit(test1(argv[arg+1]));
218 } else if (strcmp(argv[arg], "-test2") == 0) {
219 exit(test2(argv[arg+1], argv[arg+2]));
220 } else {
221 fprintf(stderr, "ERROR: unknown arg '%s'\n", argv[arg]);
222 exit(1);
223 }
224
225 arg++;
226 }
227
228 // Exit quickly if the output file exists.
229 //
230 if (fileExists(outputFile)) {
231 fprintf(stderr, "Output file '%s' exists already!\n", outputFile);
232 exit(0);
233 }
234
235
236 merStream *MS = new merStream(new kMerBuilder(MERSIZE),
237 new seqStream(sequenceFile),
238 true, true);
239
240 // Approximate the number of mers in the sequences.
241 //
242 uint64 numMers = MS->approximateNumberOfMers();
243
244 // Reset the limits.
245 //
246 // XXX: If the user somehow knows how many mers are in the input
247 // file, and specifies an end between there and the amount of
248 // sequence, we'll pointlessly still make a merStreamFile, even
249 // though we shouldn't.
250 //
251 if (merBegin == ~uint64ZERO) merBegin = 0;
252 if (merEnd == ~uint64ZERO) merEnd = numMers;
253
254 if (merBegin >= merEnd) {
255 fprintf(stderr, "ERROR: merbegin="uint64FMT" and merend="uint64FMT" are incompatible.\n",
256 merBegin, merEnd);
257 exit(1);
258 }
259
260 if ((merBegin > 0) || (merEnd < numMers))
261 MS->setBaseRange(merBegin, merEnd);
262
263 existDB *maskDB = 0L;
264 if (maskF) {
265 fprintf(stderr, "Building maskDB from '%s'\n", maskF);
266 maskDB = new existDB(maskF, mersize, existDBnoFlags, 0, ~uint32ZERO);
267 }
268
269 existDB *onlyDB = 0L;
270 if (onlyF) {
271 fprintf(stderr, "Building onlyDB from '%s'\n", onlyF);
272 onlyDB = new existDB(onlyF, mersize, existDBnoFlags, 0, ~uint32ZERO);
273 }
274
275 fprintf(stderr, "Building table with merSize "uint32FMT", merSkip "uint32FMT"\n", mersize, merskip);
276
277 positionDB *positions = new positionDB(MS, mersize, merskip, maskDB, onlyDB, 0L, 0, 0, 0, 0, true);
278
279 fprintf(stderr, "Dumping positions table to '%s'\n", outputFile);
280
281 positions->saveState(outputFile);
282
283 delete MS;
284 delete positions;
285
286 exit(0);
287 }