Debian Codegraph @

trie/trie.C

raw
1 #include "util++.H"
2 #include "bio++.H"
3
4 //#define ALPHALEN 4
5 #define ALPHALEN 20
6
7 // NOTE that our list of letters is not alphabetic. The DNA letters
8 // are first, then the rest of the protein letters.
9 //
10 const char *trieAlpha = "acgtdefhiklmnpqrsvwy";
11 uint32 trieAlphaMap[256] = {0};
12
13 class trieNode {
14 public:
15 trieNode(void) {
16 for (uint32 i=0; i<ALPHALEN; i++)
17 next[i] = ~uint32ZERO;
18 numseq = 0;
19 seqptr = ~uint32ZERO;
20 };
21
22 uint32 next[ALPHALEN]; // next node for A, C, G, T input, ~uint32ZERO if no next
23 uint32 numseq; // number of seqs we have in here
24 uint32 seqptr; // pointer to seqs
25 };
26
27
28 class trieSeqPtr {
29 public:
30 trieSeqPtr(void) {
31 seqiid = ~uint32ZERO;
32 nodeiid = ~uint32ZERO;
33 defline = 0L;
34 reversed = false;
35 };
36
37 uint32 seqiid;
38 uint32 nodeiid;
39 char *defline;
40 bool reversed;
41 };
42
43
44
45
46 int
47 trieSeqPtrCompare(const void *a, const void *b) {
48 const trieSeqPtr *A = (const trieSeqPtr *)a;
49 const trieSeqPtr *B = (const trieSeqPtr *)b;
50
51 if (A->nodeiid < B->nodeiid)
52 return(-1);
53 if (A->nodeiid > B->nodeiid)
54 return(1);
55 return(0);
56 }
57
58
59 uint32
60 addSequence(trieNode *nodes, uint32 &nodesLen,
61 trieSeqPtr *seqptr, uint32 &seqptrLen,
62 seqInCore *S,
63 bool isReverse) {
64 char *s = 0L;
65 uint32 n = 0;
66
67 if (S->sequenceLength() < 12)
68 return(0);
69
70 for (s = S->sequence(); *s; s++)
71 if (trieAlphaMap[*s] == 0)
72 return(0);
73
74 for (s = S->sequence(); *s; s++) {
75 uint32 v = trieAlphaMap[*s] - 1;
76
77 // add a new pointer if needed
78 if (nodes[n].next[v] == ~uint32ZERO)
79 nodes[n].next[v] = nodesLen++;
80
81 // Go there
82 n = nodes[n].next[v];
83 }
84
85 // add this sequence to node i -- after all sequences have been
86 // added, we'll sort this list and build pointers.
87
88 seqptr[seqptrLen].seqiid = S->getIID();
89 seqptr[seqptrLen].nodeiid = n;
90 seqptr[seqptrLen].defline = strdup(S->header());
91 seqptr[seqptrLen].reversed = isReverse;
92 seqptrLen++;
93
94 return(1);
95 }
96
97
98
99 int
100 main(int argc, char **argv) {
101 char *queries = 0L;
102 char *genome = 0L;
103 FILE *logfile = 0L;
104
105 int arg=1;
106 int err=0;
107 while (arg < argc) {
108 if (strcmp(argv[arg], "-q") == 0) {
109 queries = argv[++arg];
110 } else if (strcmp(argv[arg], "-g") == 0) {
111 genome = argv[++arg];
112 } else if (strcmp(argv[arg], "-l") == 0) {
113 errno = 0;
114 logfile = fopen(argv[++arg], "w");
115 if (errno)
116 fprintf(stderr, "Failed to open logfile '%s': %s\n", argv[arg], strerror(errno)), exit(1);
117 } else {
118 err++;
119 }
120 arg++;
121 }
122
123 if (queries == 0L)
124 err = 1;
125 if (genome == 0L)
126 err = 1;
127
128 if (err) {
129 fprintf(stderr, "usage: %s -q queries.fasta -g genome.fasta\n", argv[0]);
130 fprintf(stderr, " -q queries.fasta -- the input with the short stuff\n");
131 fprintf(stderr, " -q queries.fasta -- the input with the short stuff\n");
132 exit(1);
133 }
134
135 for (uint32 i=0; i<ALPHALEN; i++) {
136 trieAlphaMap[trieAlpha[i]] = i+1;
137 trieAlphaMap[trieAlpha[i] + 'A' - 'a'] = i+1;
138 }
139
140
141 // Build the trie from the queries. We just allocate a large
142 // number of nodes so we don't need to deal with reallocation.
143 //
144 // 32M * 28 = 896M.
145 //
146 uint32 nodesLen = 1;
147 uint32 nodesMax = 32 * 1024 * 1024;
148 trieNode *nodes = new trieNode [nodesMax];
149
150 seqCache *F = new seqCache(queries);
151 seqInCore *S = 0L;
152
153 uint32 seqptrLen = 0;
154 uint32 seqptrMax = 2 * 1024 * 1024;
155 trieSeqPtr *seqptr = new trieSeqPtr [seqptrMax];
156
157 // Number of matches per IID, not seqptr (which has two entries for
158 // each iid, one forward, one reverse).
159 //
160 uint32 *nummatches = new uint32 [seqptrMax];
161 for (uint32 i=0; i<seqptrMax; i++)
162 nummatches[i] = 0;
163
164 while ((S = F->getSequenceInCore()) != 0L) {
165 uint32 success = 0;
166
167 success += addSequence(nodes, nodesLen, seqptr, seqptrLen, S, false);
168
169 #if ALPHALEN == 4
170 reverseComplementSequence(S->sequence(), S->sequenceLength());
171 success += addSequence(nodes, nodesLen, seqptr, seqptrLen, S, true);
172 #else
173 success++;
174 #endif
175
176 if (success != 2)
177 if (logfile)
178 fprintf(logfile, "Failed to add sequence '%s' ('%s').\n", S->header(), S->sequence());
179
180 if (nodesLen >= nodesMax)
181 fprintf(stderr, "ERROR: out of node space.\n"), exit(1);
182 if (seqptrLen >= seqptrMax)
183 fprintf(stderr, "ERROR: out of seqptr space.\n"), exit(1);
184
185 delete S;
186 }
187
188 delete F;
189
190 fprintf(stderr, "Used "uint32FMT" trie nodes. \n", nodesLen);
191
192 // Fix up sequence pointers - we could probably do this inplace
193 // with some trickery, but why?
194
195 qsort(seqptr, seqptrLen, sizeof(trieSeqPtr), trieSeqPtrCompare);
196
197 // Now sorted by node iid, so run through both arrays and set
198 // pointers. We point to the first thing found, and remember
199 // the number of things found.
200
201 for (uint32 i=0; i<seqptrLen; i++) {
202 uint32 ni = seqptr[i].nodeiid;
203
204 if (nodes[ni].seqptr == ~uint32ZERO)
205 nodes[ni].seqptr = i;
206
207 nodes[ni].numseq++;
208 }
209
210 //
211
212 F = new seqCache(genome);
213 S = 0L;
214 while ((S = F->getSequenceInCore()) != 0L) {
215 char *s = S->sequence();
216 uint32 siid = S->getIID();
217 uint32 spos = 0;
218
219 uint32 n[256] = {0}; // Pointer into the trie
220 uint32 d[256] = {0}; // Depth this pointer is at (== sequence length)
221 uint32 nLen = 0;
222
223 //fprintf(stderr, "WORKING ON '%s'\n", S->header());
224
225 while (*s) {
226 if (trieAlphaMap[*s] == 0) {
227
228 // Not a valid symbol, all node pointers are killed, no exact matches
229 // possible!
230 nLen = 0;
231
232 } else {
233
234 // Valid symbol. Advance all pointers, print out any
235 // matches, kill any pointers, and then finally add a new
236 // one.
237
238 uint32 v = trieAlphaMap[*s] - 1;
239 uint32 ni;
240 uint32 nj;
241
242 // Advance pointers.
243 //
244 for (ni=0; ni<nLen; ni++) {
245 n[ni] = nodes[n[ni]].next[v];
246 d[ni]++;
247 }
248
249 // Kill any thing that is now dead - copy nj into ni
250 //
251 for (ni=0, nj=0; nj<nLen; nj++)
252 if (n[nj] != ~uint32ZERO) {
253 if ((ni != nj)) {
254 n[ni] = n[nj];
255 d[ni] = d[nj];
256 }
257 ni++;
258 }
259 nLen = ni;
260
261 // Print any matches
262 //
263 for (ni=0; ni<nLen; ni++) {
264 if (nodes[n[ni]].numseq > 0) {
265 for (nj=0; nj<nodes[n[ni]].numseq; nj++) {
266 uint32 p = nodes[n[ni]].seqptr + nj;
267
268 nummatches[seqptr[p].seqiid]++;
269 if (nummatches[seqptr[p].seqiid] == 1000) {
270 if (logfile)
271 fprintf(logfile, "sequence "uint32FMT" '%s' has too many matches, not reporting any more.\n",
272 seqptr[p].seqiid,
273 seqptr[p].defline);
274 } else if (nummatches[seqptr[p].seqiid] < 1000) {
275 fprintf(stdout, "sim4begin\n");
276 fprintf(stdout, uint32FMT"["uint32FMT"-0-0] "uint32FMT"[0-0] <"uint32FMT"-0-100-%s-unknown>\n",
277 seqptr[p].seqiid,
278 d[ni] + 1,
279 siid,
280 d[ni] + 1,
281 seqptr[p].reversed ? "complement" : "forward");
282 fprintf(stdout, "edef=%s\n", seqptr[p].defline);
283 fprintf(stdout, "ddef=%s\n", S->header());
284 fprintf(stdout, "1-"uint32FMT" ("uint32FMT"-"uint32FMT") <"uint32FMT"-0-100>\n",
285 d[ni] + 1,
286 spos - d[ni] + 1,
287 spos + 1,
288 d[ni] + 1);
289 fprintf(stdout, "sim4end\n");
290 }
291 }
292 }
293 }
294
295 // Add a new pointer for the just seen letter
296 //
297 if (nodes[0].next[v] != ~uint32ZERO) {
298 d[nLen] = 0;
299 n[nLen++] = nodes[0].next[v];
300 }
301 }
302
303 s++;
304 spos++;
305 }
306
307 delete S;
308 }
309
310 // We should print out the total number of matches for each
311 // sequence.... Report those with matches first.
312 //
313 if (logfile) {
314 for (uint32 i=0; i<seqptrLen; i++)
315 if ((seqptr[i].reversed == false) && (nummatches[seqptr[i].seqiid] > 0))
316 fprintf(logfile, "sequence "uint32FMT" '%s' has "uint32FMT" matches.\n",
317 seqptr[i].seqiid,
318 seqptr[i].defline,
319 nummatches[seqptr[i].seqiid]);
320
321 for (uint32 i=0; i<seqptrLen; i++)
322 if ((seqptr[i].reversed == false) && (nummatches[seqptr[i].seqiid] == 0))
323 fprintf(logfile, "sequence "uint32FMT" '%s' has no matches.\n",
324 seqptr[i].seqiid,
325 seqptr[i].defline,
326 nummatches[seqptr[i].seqiid]);
327 }
328
329 delete F;
330 }