Debian Codegraph @

meryl/test/exhaustive.C

raw
1 #include "bio++.H"
2 #include "libmeryl.H"
3 #include "kmerlite.H"
4
5 // This tests that all the mers in an input fasta file are counted
6 // properly. It does not test that the meryl output contains exactly
7 // those mers, just that those mers are there.
8 //
9 // If you can fit into one batch, then it _will_ verift that the
10 // meryl output is exactly correct.
11 //
12 // Reads a meryl-format kmer count in chunks. Each chunk is stored
13 // in a searchable structure (we should be using, say, an extended
14 // existDB, but we're using a balanced binary tree). The entire
15 // source fasta file is then streamed against the kmer chunk,
16 // decrementing the count for each mer. When the whole file is
17 // streamed, any kmers with positive count are reported.
18
19
20 // NB: My hacked kazlib returns a pointer to whatever we give it.
21 // Since we gave it a pointer to an object, it gives us back a
22 // pointer to "a pointer to an object". Hence, this ugliness.
23 //
24 int
25 kMerLiteSort(void const *a, void const *b) {
26 kMerLite const *A = *((kMerLite * const *)a);
27 kMerLite const *B = *((kMerLite * const *)b);
28
29 if (*A < *B) return(-1);
30 if (*A > *B) return(1);
31 return(0);
32 }
33
34
35
36 int
37 main(int argc, char **argv) {
38
39 char *merylCount = 0L;
40 char *fastaName = 0L;
41
42 int arg=1;
43 while (arg < argc) {
44
45 if (strcmp(argv[arg], "-m") == 0) {
46 merylCount = argv[++arg];
47 } else if (strcmp(argv[arg], "-f") == 0) {
48 fastaName = argv[++arg];
49 } else {
50 fprintf(stderr, "unknown option '%s'\n", argv[arg]);
51 }
52 arg++;
53 }
54
55 if ((merylCount == 0L) || (fastaName == 0L)) {
56 fprintf(stderr, "usage: %s -m <meryl-name-prefix> -f <fasta-file>\n", argv[0]);
57 exit(1);
58 }
59
60
61 // Open the count files
62 //
63 merylStreamReader *MSR = new merylStreamReader(merylCount);
64
65 fprintf(stderr, "Mers are "uint32FMT" bases.\n", MSR->merSize());
66 fprintf(stderr, "There are "uint64FMT" unique (copy = 1) mers.\n", MSR->numberOfUniqueMers());
67 fprintf(stderr, "There are "uint64FMT" distinct mers.\n", MSR->numberOfDistinctMers());
68 fprintf(stderr, "There are "uint64FMT" mers total.\n", MSR->numberOfTotalMers());
69
70 // Guess how many mers we can fit into 512MB, then report how many chunks we need to do.
71
72 uint32 merSize = MSR->merSize();
73 uint64 memoryLimit = 700 * 1024 * 1024;
74 uint64 perMer = sizeof(kMerLite) + sizeof(dnode_t);
75 uint64 mersPerBatch = memoryLimit / perMer;
76 uint32 numBatches = MSR->numberOfDistinctMers() / mersPerBatch;
77 uint32 batch = 0;
78
79 dnode_t *nodes = new dnode_t [mersPerBatch];
80 kMerLite *mers = new kMerLite [mersPerBatch];
81
82 if (MSR->numberOfDistinctMers() % mersPerBatch)
83 numBatches++;
84
85 fprintf(stderr, "perMer: "uint64FMT" bytes ("uint64FMT" for kMerLite, "uint64FMT" for dnode_t.\n",
86 perMer, (uint64)sizeof(kMerLite), (uint64)sizeof(dnode_t));
87 fprintf(stderr, "We can fit "uint64FMT" mers into "uint64FMT"MB.\n", mersPerBatch, memoryLimit >> 20);
88 fprintf(stderr, "So we need "uint32FMT" batches to verify the count.\n", numBatches);
89
90 while (MSR->validMer()) {
91 uint64 mersRemain = mersPerBatch;
92 dict_t *merDict = dict_create(mersPerBatch, kMerLiteSort);
93
94 batch++;
95
96 // STEP 1: Insert mersPerBatch into the merDict
97 //
98 fprintf(stderr, "STEP 1 BATCH "uint32FMTW(2)": Insert into merDict\n", batch);
99 while (MSR->nextMer() && mersRemain) {
100 mersRemain--;
101
102 mers[mersRemain] = MSR->theFMer();
103
104 // initialize the node with the value, then insert the node
105 // into the tree using the key
106
107 int32 val = (int32)MSR->theCount();
108 dnode_init(&nodes[mersRemain], (void *)val);
109 dict_insert(merDict, &nodes[mersRemain], &mers[mersRemain]);
110 }
111
112 // STEP 2: Stream the original file, decrementing the count
113 //
114 fprintf(stderr, "STEP 2 BATCH "uint32FMTW(2)": Stream fasta\n", batch);
115 seqStream *CS = new seqStream(fastaName, true);
116 merStream *MS = new merStream(new kMerBuilder(merSize), CS);
117
118 kMerLite mer;
119 dnode_t *nod;
120
121 while (MS->nextMer()) {
122 mer = MS->theFMer();
123
124 nod = dict_lookup(merDict, &mer);
125
126 if (nod != 0L) {
127 int32 val = (int32)dnode_get(nod);
128 val--;
129 dnode_put(nod, (void *)val);
130 } else {
131 // Unless the whole meryl file fit into our merDict, we cannot warn if
132 // we don't find mers.
133 //
134 if (numBatches == 1) {
135 char str[1024];
136 fprintf(stderr, "Didn't find node for mer '%s'\n", mer.merToString(merSize, str));
137 }
138 }
139 }
140
141 delete MS;
142 delete CS;
143
144 // STEP 3: Check every node in the tree to make sure that the counts
145 // are exactly zero.
146 //
147 fprintf(stderr, "STEP 3 BATCH "uint32FMTW(2)": Check\n", batch);
148 nod = dict_first(merDict);
149 while (nod) {
150 int32 val = (int32)dnode_get(nod);
151 kMerLite const *nodmer = (kMerLite const *)dnode_getkey(nod);
152
153 if (val != 0) {
154 char str[1024];
155 fprintf(stderr, "Got count "int32FMT" for mer '%s'\n",
156 val,
157 nodmer->merToString(merSize, str));
158 }
159
160 nod = dict_next(merDict, nod);
161 }
162
163
164 // STEP 4: Destroy the dictionary.
165 //
166 fprintf(stderr, "STEP 4 BATCH "uint32FMTW(2)": Destroy\n", batch);
167 while ((nod = dict_first(merDict)))
168 dict_delete(merDict, nod);
169 dict_destroy(merDict);
170 }
171 }