| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <math.h> |
| 5 |
|
| 6 |
#include "libmeryl.H" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#if 0 |
| 12 |
void |
| 13 |
heatMap() { |
| 14 |
speedCounter *C = new speedCounter(" Examining: %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1ffffff, false); |
| 15 |
|
| 16 |
#define MAXA 150 |
| 17 |
#define MAXB 150 |
| 18 |
|
| 19 |
double heatraw[MAXA][MAXB]; |
| 20 |
double heatsca[MAXA][MAXB]; |
| 21 |
|
| 22 |
for (uint32 i=0; i<MAXA; i++) |
| 23 |
for (uint32 j=0; j<MAXB; j++) |
| 24 |
heatraw[i][j] = heatsca[i][j] = 0; |
| 25 |
|
| 26 |
A->nextMer(); |
| 27 |
B->nextMer(); |
| 28 |
|
| 29 |
while ((A->validMer()) || |
| 30 |
(B->validMer())) { |
| 31 |
kMer &a = A->theFMer(); |
| 32 |
kMer &b = B->theFMer(); |
| 33 |
|
| 34 |
uint32 ac = A->theCount(); |
| 35 |
uint32 bc = B->theCount(); |
| 36 |
|
| 37 |
if (ac >= MAXA) |
| 38 |
ac = MAXA-1; |
| 39 |
|
| 40 |
if (bc >= MAXB) |
| 41 |
bc = MAXB-1; |
| 42 |
|
| 43 |
if (A->validMer() == false) { |
| 44 |
ac = 0; |
| 45 |
heatraw[ac][bc]++; |
| 46 |
B->nextMer(); |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
if (B->validMer() == false) { |
| 51 |
bc = 0; |
| 52 |
heatraw[ac][bc]++; |
| 53 |
A->nextMer(); |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
if (a == b) { |
| 58 |
heatraw[ac][bc]++; |
| 59 |
A->nextMer(); |
| 60 |
B->nextMer(); |
| 61 |
|
| 62 |
} else if (a < b) { |
| 63 |
heatraw[ac][0]++; |
| 64 |
A->nextMer(); |
| 65 |
|
| 66 |
} else { |
| 67 |
heatraw[0][bc]++; |
| 68 |
B->nextMer(); |
| 69 |
} |
| 70 |
|
| 71 |
C->tick(); |
| 72 |
} |
| 73 |
|
| 74 |
delete C; |
| 75 |
delete A; |
| 76 |
delete B; |
| 77 |
|
| 78 |
// Scale each row to be between 0 and 1 |
| 79 |
|
| 80 |
#if 0 |
| 81 |
for (uint32 j=0; j<MAXB; j++) { |
| 82 |
double mina = heatraw[0][j]; |
| 83 |
double maxa = heatraw[0][j]; |
| 84 |
|
| 85 |
for (uint32 ii=0; ii<MAXA; ii++) { |
| 86 |
if (maxa < heatraw[ii][j]) |
| 87 |
maxa = heatraw[ii][j]; |
| 88 |
if (heatraw[ii][j] < mina) |
| 89 |
mina = heatraw[ii][j]; |
| 90 |
} |
| 91 |
|
| 92 |
for (uint32 i=0; i<MAXA; i++) |
| 93 |
heatsca[i][j] = (heatraw[i][j] - mina) / (maxa - mina); |
| 94 |
} |
| 95 |
#endif |
| 96 |
|
| 97 |
|
| 98 |
for (uint32 i=0; i<MAXA; i++) |
| 99 |
for (uint32 j=0; j<MAXB; j++) |
| 100 |
fprintf(stdout, uint32FMT"\t"uint32FMT"\t%f\n", i, j, log(heatraw[i][j])); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
int |
| 109 |
main(int argc, char **argv) { |
| 110 |
merylStreamReader *T = 0L; |
| 111 |
merylStreamReader *S = 0L; |
| 112 |
char *outputPrefix = NULL; |
| 113 |
char *plotTitle = NULL; |
| 114 |
int arg = 1; |
| 115 |
int err = 0; |
| 116 |
|
| 117 |
while (arg < argc) { |
| 118 |
if (strcmp(argv[arg], "-truth") == 0) { |
| 119 |
T = new merylStreamReader(argv[++arg]); |
| 120 |
|
| 121 |
} else if (strcmp(argv[arg], "-sample") == 0) { |
| 122 |
S = new merylStreamReader(argv[++arg]); |
| 123 |
|
| 124 |
} else if (strcmp(argv[arg], "-output") == 0) { |
| 125 |
outputPrefix = argv[++arg]; |
| 126 |
|
| 127 |
} else if (strcmp(argv[arg], "-title") == 0) { |
| 128 |
plotTitle = argv[++arg]; |
| 129 |
|
| 130 |
} else { |
| 131 |
fprintf(stderr, "unknown option '%s'\n", argv[arg]); |
| 132 |
err++; |
| 133 |
} |
| 134 |
|
| 135 |
arg++; |
| 136 |
} |
| 137 |
|
| 138 |
if ((T == 0L) || (S == 0L) || (outputPrefix == 0L) || (plotTitle == 0L) || (err)) { |
| 139 |
fprintf(stderr, "usage: %s [opts]\n", argv[0]); |
| 140 |
fprintf(stderr, " -truth k-mers from reference\n"); |
| 141 |
fprintf(stderr, " -sample k-mers from sample\n"); |
| 142 |
fprintf(stderr, " -output output prefix\n"); |
| 143 |
fprintf(stderr, " -title plot label\n"); |
| 144 |
exit(1); |
| 145 |
} |
| 146 |
|
| 147 |
uint32 kmerSize = T->merSize(); |
| 148 |
|
| 149 |
#define HMAX 64 * 1024 |
| 150 |
|
| 151 |
uint32 *Htrue = new uint32 [HMAX]; |
| 152 |
uint32 *Hnoise = new uint32 [HMAX]; |
| 153 |
|
| 154 |
for (uint32 i=0; i<HMAX; i++) |
| 155 |
Htrue[i] = Hnoise[i] = 0; |
| 156 |
|
| 157 |
T->nextMer(); |
| 158 |
S->nextMer(); |
| 159 |
|
| 160 |
while ((T->validMer()) || |
| 161 |
(S->validMer())) { |
| 162 |
kMer &t = T->theFMer(); |
| 163 |
kMer &s = S->theFMer(); |
| 164 |
|
| 165 |
uint32 tc = T->theCount(); |
| 166 |
uint32 sc = S->theCount(); |
| 167 |
|
| 168 |
if (tc >= HMAX) tc = HMAX-1; |
| 169 |
if (sc >= HMAX) sc = HMAX-1; |
| 170 |
|
| 171 |
// If we're out of truth kmers, the sample is noise. |
| 172 |
if (T->validMer() == false) { |
| 173 |
Hnoise[sc]++; |
| 174 |
S->nextMer(); |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// If we're out of sample kmers, do nothing but go to the next truth kmer. |
| 179 |
if (S->validMer() == false) { |
| 180 |
T->nextMer(); |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
// If the kmers are equal, this is a true kmer |
| 185 |
if (t == s) { |
| 186 |
Htrue[sc]++; |
| 187 |
T->nextMer(); |
| 188 |
S->nextMer(); |
| 189 |
} |
| 190 |
|
| 191 |
// If the truth kmer is the lesser, get the next truth. |
| 192 |
else if (t < s) { |
| 193 |
T->nextMer(); |
| 194 |
} |
| 195 |
|
| 196 |
// Else the sample kmer is smaller, add it to the noise pile, and get the next. |
| 197 |
else { |
| 198 |
Hnoise[sc]++; |
| 199 |
S->nextMer(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
delete T; |
| 204 |
delete S; |
| 205 |
|
| 206 |
char outputName[FILENAME_MAX]; |
| 207 |
|
| 208 |
sprintf(outputName, "%s.gp", outputPrefix); |
| 209 |
FILE *outputGP = fopen(outputName, "w"); |
| 210 |
|
| 211 |
sprintf(outputName, "%s.dat", outputPrefix); |
| 212 |
FILE *outputDAT = fopen(outputName, "w"); |
| 213 |
|
| 214 |
fprintf(outputGP, "set terminal png\n"); |
| 215 |
fprintf(outputGP, "set output \"%s.png\"\n", outputPrefix); |
| 216 |
fprintf(outputGP, "set title \"%s true/false %d-mers\"\n", plotTitle, kmerSize); |
| 217 |
fprintf(outputGP, "set xlabel \"k-mer count\"\n"); |
| 218 |
fprintf(outputGP, "set ylabel \"number of kmers\"\n"); |
| 219 |
fprintf(outputGP, "plot [0:100] [0:1000000] \"%s.dat\" using 1:2 with lines title \"true\", \"%s.dat\" using 1:3 with lines title \"false\"\n", |
| 220 |
outputPrefix, outputPrefix); |
| 221 |
|
| 222 |
fclose(outputGP); |
| 223 |
|
| 224 |
for (uint32 i=0; i<HMAX; i++) |
| 225 |
fprintf(outputDAT, uint32FMT"\t"uint32FMT"\t"uint32FMT"\n", i, Htrue[i], Hnoise[i]); |
| 226 |
|
| 227 |
fclose(outputDAT); |
| 228 |
|
| 229 |
sprintf(outputName, "gnuplot < %s.gp", outputPrefix); |
| 230 |
system(outputName); |
| 231 |
|
| 232 |
exit(0); |
| 233 |
} |