| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "meryl.H" |
| 6 | #include "libmeryl.H" |
| 7 | |
| 8 | |
| 9 | |
| 10 | void |
| 11 | multipleOperations(merylArgs *args) { |
| 12 | |
| 13 | if (args->mergeFilesLen < 2) { |
| 14 | fprintf(stderr, "ERROR - must have at least two databases (you gave "uint32FMT")!\n", args->mergeFilesLen); |
| 15 | exit(1); |
| 16 | } |
| 17 | if (args->outputFile == 0L) { |
| 18 | fprintf(stderr, "ERROR - no output file specified.\n"); |
| 19 | exit(1); |
| 20 | } |
| 21 | if ((args->personality != PERSONALITY_MERGE) && |
| 22 | (args->personality != PERSONALITY_MIN) && |
| 23 | (args->personality != PERSONALITY_MINEXIST) && |
| 24 | (args->personality != PERSONALITY_MAX) && |
| 25 | (args->personality != PERSONALITY_MAXEXIST) && |
| 26 | (args->personality != PERSONALITY_ADD) && |
| 27 | (args->personality != PERSONALITY_AND) && |
| 28 | (args->personality != PERSONALITY_NAND) && |
| 29 | (args->personality != PERSONALITY_OR) && |
| 30 | (args->personality != PERSONALITY_XOR)) { |
| 31 | fprintf(stderr, "ERROR - only personalities min, minexist, max, maxexist, add, and, nand, or, xor\n"); |
| 32 | fprintf(stderr, "ERROR - are supported in multipleOperations(). (%d)\n", args->personality); |
| 33 | fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 34 | exit(1); |
| 35 | } |
| 36 | |
| 37 | merylStreamReader **R = new merylStreamReader* [args->mergeFilesLen]; |
| 38 | merylStreamWriter *W = 0L; |
| 39 | |
| 40 | // Open the input files, read in the first mer |
| 41 | // |
| 42 | for (uint32 i=0; i<args->mergeFilesLen; i++) { |
| 43 | R[i] = new merylStreamReader(args->mergeFiles[i]); |
| 44 | R[i]->nextMer(); |
| 45 | } |
| 46 | |
| 47 | // Verify that the mersizes are all the same |
| 48 | // |
| 49 | bool fail = false; |
| 50 | uint32 merSize = R[0]->merSize(); |
| 51 | uint32 merComp = R[0]->merCompression(); |
| 52 | |
| 53 | for (uint32 i=0; i<args->mergeFilesLen; i++) { |
| 54 | fail |= (merSize != R[i]->merSize()); |
| 55 | fail |= (merComp != R[i]->merCompression()); |
| 56 | } |
| 57 | |
| 58 | if (fail) |
| 59 | fprintf(stderr, "ERROR: mer sizes (or compression level) differ.\n"), exit(1); |
| 60 | |
| 61 | // Open the output file, using the largest prefix size found in the |
| 62 | // input/mask files. |
| 63 | // |
| 64 | uint32 prefixSize = 0; |
| 65 | for (uint32 i=0; i<args->mergeFilesLen; i++) |
| 66 | if (prefixSize < R[i]->prefixSize()) |
| 67 | prefixSize = R[i]->prefixSize(); |
| 68 | |
| 69 | W = new merylStreamWriter(args->outputFile, merSize, merComp, prefixSize, args->positionsEnabled); |
| 70 | |
| 71 | // We will find the smallest mer in any file, and count the number of times |
| 72 | // it is present in the input files. |
| 73 | |
| 74 | bool moreInput = true; |
| 75 | |
| 76 | kMer currentMer; // The current mer we're operating on |
| 77 | uint32 currentCount = uint32ZERO; // The count (operation dependent) of this mer |
| 78 | uint32 currentTimes = uint32ZERO; // Number of files it's in |
| 79 | |
| 80 | uint32 currentPositionsMax = 0; |
| 81 | uint32 *currentPositions = 0L; |
| 82 | |
| 83 | kMer thisMer; // The mer we just read |
| 84 | uint32 thisFile = ~uint32ZERO; // The file we read it from |
| 85 | uint32 thisCount = uint32ZERO; // The count of the mer we just read |
| 86 | |
| 87 | speedCounter *C = new speedCounter(" %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1fffff, args->beVerbose); |
| 88 | |
| 89 | currentMer.setMerSize(merSize); |
| 90 | thisMer.setMerSize(merSize); |
| 91 | |
| 92 | while (moreInput) { |
| 93 | |
| 94 | // Find the smallest mer present in any input file. |
| 95 | // |
| 96 | moreInput = false; |
| 97 | thisMer.clear(); |
| 98 | thisFile = ~uint32ZERO; |
| 99 | thisCount = uint32ZERO; |
| 100 | |
| 101 | // Load thisMer with the first valid mer |
| 102 | for (uint32 i=0; i<args->mergeFilesLen && !moreInput; i++) |
| 103 | if (R[i]->validMer()) { |
| 104 | moreInput = true; |
| 105 | thisCount = R[i]->theCount(); |
| 106 | thisFile = i; |
| 107 | thisMer = R[i]->theFMer(); |
| 108 | } |
| 109 | |
| 110 | // Now find the smallest one |
| 111 | if (moreInput) { |
| 112 | for (uint32 i=thisFile+1; i<args->mergeFilesLen; i++) |
| 113 | if ((R[i]->validMer()) && (R[i]->theFMer()) < thisMer) { |
| 114 | moreInput = true; |
| 115 | thisCount = R[i]->theCount(); |
| 116 | thisFile = i; |
| 117 | thisMer = R[i]->theFMer(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // If we've hit a different mer, write out the last one |
| 122 | if ((moreInput == false) || (thisMer != currentMer)) { |
| 123 | switch (args->personality) { |
| 124 | case PERSONALITY_MIN: |
| 125 | case PERSONALITY_MAX: |
| 126 | if (currentTimes == args->mergeFilesLen) |
| 127 | W->addMer(currentMer, currentCount); |
| 128 | break; |
| 129 | case PERSONALITY_MERGE: |
| 130 | case PERSONALITY_MINEXIST: |
| 131 | case PERSONALITY_MAXEXIST: |
| 132 | case PERSONALITY_ADD: |
| 133 | W->addMer(currentMer, currentCount, currentPositions); |
| 134 | break; |
| 135 | case PERSONALITY_AND: |
| 136 | if (currentTimes == args->mergeFilesLen) |
| 137 | W->addMer(currentMer, currentCount); |
| 138 | break; |
| 139 | case PERSONALITY_NAND: |
| 140 | if (currentTimes != args->mergeFilesLen) |
| 141 | W->addMer(currentMer, currentCount); |
| 142 | break; |
| 143 | case PERSONALITY_OR: |
| 144 | W->addMer(currentMer, currentCount); |
| 145 | break; |
| 146 | case PERSONALITY_XOR: |
| 147 | if ((currentTimes % 2) == 1) |
| 148 | W->addMer(currentMer, currentCount); |
| 149 | break; |
| 150 | default: |
| 151 | fprintf(stderr, "ERROR - invalid personality in multipleOperations::write\n"); |
| 152 | fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 153 | exit(1); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | currentMer = thisMer; |
| 158 | |
| 159 | currentCount = uint32ZERO; |
| 160 | currentTimes = uint32ZERO; |
| 161 | |
| 162 | C->tick(); |
| 163 | } |
| 164 | |
| 165 | // All done? Exit. |
| 166 | if (moreInput == false) |
| 167 | continue; |
| 168 | |
| 169 | // Perform the operation |
| 170 | switch (args->personality) { |
| 171 | case PERSONALITY_MERGE: |
| 172 | if (R[thisFile]->thePositions()) { |
| 173 | if (currentPositionsMax == 0) { |
| 174 | currentPositionsMax = 1048576; |
| 175 | currentPositions = new uint32 [currentPositionsMax]; |
| 176 | } |
| 177 | |
| 178 | if (currentPositionsMax < currentCount + thisCount) { |
| 179 | while (currentPositionsMax < currentCount + thisCount) |
| 180 | currentPositionsMax *= 2; |
| 181 | |
| 182 | uint32 *t = new uint32 [currentPositionsMax]; |
| 183 | memcpy(t, currentPositions, sizeof(uint32) * currentCount); |
| 184 | delete [] currentPositions; |
| 185 | currentPositions = t; |
| 186 | } |
| 187 | |
| 188 | if (thisCount < 16) { |
| 189 | uint32 *p = R[thisFile]->thePositions(); |
| 190 | for (uint32 i=0; i<thisCount; i++) |
| 191 | currentPositions[currentCount + i] = p[i]; |
| 192 | } else { |
| 193 | memcpy(currentPositions + currentCount, R[thisFile]->thePositions(), sizeof(uint32) * thisCount); |
| 194 | } |
| 195 | } |
| 196 | // Otherwise, we're the same as ADD. |
| 197 | currentCount += thisCount; |
| 198 | break; |
| 199 | case PERSONALITY_MIN: |
| 200 | case PERSONALITY_MINEXIST: |
| 201 | if (currentTimes == 0) { |
| 202 | currentCount = thisCount; |
| 203 | } else { |
| 204 | if (currentCount > thisCount) |
| 205 | currentCount = thisCount; |
| 206 | } |
| 207 | break; |
| 208 | case PERSONALITY_MAX: |
| 209 | case PERSONALITY_MAXEXIST: |
| 210 | if (currentCount < thisCount) |
| 211 | currentCount = thisCount; |
| 212 | break; |
| 213 | case PERSONALITY_ADD: |
| 214 | currentCount += thisCount; |
| 215 | break; |
| 216 | case PERSONALITY_AND: |
| 217 | case PERSONALITY_NAND: |
| 218 | case PERSONALITY_OR: |
| 219 | case PERSONALITY_XOR: |
| 220 | currentCount = 1; |
| 221 | break; |
| 222 | default: |
| 223 | fprintf(stderr, "ERROR - invalid personality in multipleOperations::operate\n"); |
| 224 | fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 225 | exit(1); |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | currentTimes++; |
| 230 | |
| 231 | // Move the file we just read from to the next mer |
| 232 | R[thisFile]->nextMer(); |
| 233 | } |
| 234 | |
| 235 | for (uint32 i=0; i<args->mergeFilesLen; i++) |
| 236 | delete R[i]; |
| 237 | delete R; |
| 238 | delete W; |
| 239 | delete C; |
| 240 | } |