| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "meryl.H" |
| 7 | #include "libmeryl.H" |
| 8 | |
| 9 | |
| 10 | void |
| 11 | binaryOperations(merylArgs *args) { |
| 12 | |
| 13 | if (args->mergeFilesLen != 2) { |
| 14 | fprintf(stderr, "ERROR - must have exactly two files!\n"); |
| 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_SUB) && |
| 22 | (args->personality != PERSONALITY_ABS) && |
| 23 | (args->personality != PERSONALITY_DIVIDE)) { |
| 24 | fprintf(stderr, "ERROR - only personalities sub and abs\n"); |
| 25 | fprintf(stderr, "ERROR - are supported in binaryOperations().\n"); |
| 26 | fprintf(stderr, "ERROR - this is a coding error, not a user error.\n"); |
| 27 | exit(1); |
| 28 | } |
| 29 | |
| 30 | // Open the input files, read in the first mer |
| 31 | // |
| 32 | merylStreamReader *A = new merylStreamReader(args->mergeFiles[0]); |
| 33 | merylStreamReader *B = new merylStreamReader(args->mergeFiles[1]); |
| 34 | |
| 35 | A->nextMer(); |
| 36 | B->nextMer(); |
| 37 | |
| 38 | // Make sure that the mersizes agree, and pick a prefix size for |
| 39 | // the output |
| 40 | // |
| 41 | if (A->merSize() != B->merSize()) { |
| 42 | fprintf(stderr, "ERROR - mersizes are different!\n"); |
| 43 | fprintf(stderr, "ERROR - mersize of '%s' is "uint32FMT"\n", args->mergeFiles[0], A->merSize()); |
| 44 | fprintf(stderr, "ERROR - mersize of '%s' is "uint32FMT"\n", args->mergeFiles[1], B->merSize()); |
| 45 | exit(1); |
| 46 | } |
| 47 | |
| 48 | // Open the output file, using the larger of the two prefix sizes |
| 49 | // |
| 50 | merylStreamWriter *W = new merylStreamWriter(args->outputFile, |
| 51 | A->merSize(), |
| 52 | A->merCompression(), |
| 53 | (A->prefixSize() > B->prefixSize()) ? A->prefixSize() : B->prefixSize(), |
| 54 | A->hasPositions()); |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | // SUB - report A - B |
| 60 | // ABS - report the absolute difference between the two files |
| 61 | // |
| 62 | // These two operations are very similar (SUB was derived from ABS), so |
| 63 | // any bug found in one is probably in the other. |
| 64 | // |
| 65 | kMer Amer; |
| 66 | uint32 Acnt = uint32ZERO; |
| 67 | kMer Bmer; |
| 68 | uint32 Bcnt = uint32ZERO; |
| 69 | |
| 70 | switch (args->personality) { |
| 71 | case PERSONALITY_SUB: |
| 72 | while (A->validMer() || B->validMer()) { |
| 73 | Amer = A->theFMer(); |
| 74 | Acnt = A->theCount(); |
| 75 | Bmer = B->theFMer(); |
| 76 | Bcnt = B->theCount(); |
| 77 | |
| 78 | // If the A stream is all out of mers, set Amer to be the |
| 79 | // same as Bmer, and set Acnt to zero. Similar for B. |
| 80 | // |
| 81 | if (!A->validMer()) { |
| 82 | Amer = Bmer; |
| 83 | Acnt = uint32ZERO; |
| 84 | } |
| 85 | if (!B->validMer()) { |
| 86 | Bmer = Amer; |
| 87 | Bcnt = uint32ZERO; |
| 88 | } |
| 89 | |
| 90 | //fprintf(stderr, "sub A="uint64HEX" B="uint64HEX"\n", Amer, Bmer); |
| 91 | |
| 92 | if (Amer == Bmer) { |
| 93 | W->addMer(Amer, (Acnt > Bcnt) ? Acnt - Bcnt : 0); |
| 94 | A->nextMer(); |
| 95 | B->nextMer(); |
| 96 | } else if (Amer < Bmer) { |
| 97 | W->addMer(Amer, Acnt); |
| 98 | A->nextMer(); |
| 99 | } else { |
| 100 | B->nextMer(); |
| 101 | } |
| 102 | } |
| 103 | break; |
| 104 | case PERSONALITY_ABS: |
| 105 | while (A->validMer() || B->validMer()) { |
| 106 | Amer = A->theFMer(); |
| 107 | Acnt = A->theCount(); |
| 108 | Bmer = B->theFMer(); |
| 109 | Bcnt = B->theCount(); |
| 110 | |
| 111 | // If the A stream is all out of mers, set Amer to be the |
| 112 | // same as Bmer, and set Acnt to zero. Similar for B. |
| 113 | // |
| 114 | if (!A->validMer()) { |
| 115 | Amer = Bmer; |
| 116 | Acnt = uint32ZERO; |
| 117 | } |
| 118 | if (!B->validMer()) { |
| 119 | Bmer = Amer; |
| 120 | Bcnt = uint32ZERO; |
| 121 | } |
| 122 | |
| 123 | if (Amer == Bmer) { |
| 124 | W->addMer(Amer, (Acnt > Bcnt) ? Acnt - Bcnt : Bcnt - Acnt); |
| 125 | A->nextMer(); |
| 126 | B->nextMer(); |
| 127 | } else if (Amer < Bmer) { |
| 128 | W->addMer(Amer, Acnt); |
| 129 | A->nextMer(); |
| 130 | } else { |
| 131 | W->addMer(Bmer, Bcnt); |
| 132 | B->nextMer(); |
| 133 | } |
| 134 | } |
| 135 | break; |
| 136 | case PERSONALITY_DIVIDE: |
| 137 | while (A->validMer() || B->validMer()) { |
| 138 | Amer = A->theFMer(); |
| 139 | Acnt = A->theCount(); |
| 140 | Bmer = B->theFMer(); |
| 141 | Bcnt = B->theCount(); |
| 142 | |
| 143 | // If the A stream is all out of mers, set Amer to be the |
| 144 | // same as Bmer, and set Acnt to zero. Similar for B. |
| 145 | // |
| 146 | if (!A->validMer()) { |
| 147 | Amer = Bmer; |
| 148 | Acnt = uint32ZERO; |
| 149 | } |
| 150 | if (!B->validMer()) { |
| 151 | Bmer = Amer; |
| 152 | Bcnt = uint32ZERO; |
| 153 | } |
| 154 | |
| 155 | if (Amer == Bmer) { |
| 156 | if ((Acnt > 0) && (Bcnt > 0)) { |
| 157 | double d = 1000.0 * (double)Acnt / (double)Bcnt; |
| 158 | if (d > 4096.0 * 1024.0 * 1024.0) |
| 159 | d = 4096.0 * 1024.0 * 1024.0; |
| 160 | W->addMer(Amer, (uint32)floor(d)); |
| 161 | } |
| 162 | A->nextMer(); |
| 163 | B->nextMer(); |
| 164 | } else if (Amer < Bmer) { |
| 165 | A->nextMer(); |
| 166 | } else { |
| 167 | B->nextMer(); |
| 168 | } |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | delete A; |
| 174 | delete B; |
| 175 | delete W; |
| 176 | } |