Debian Codegraph @

meryl/args.C

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5
6 #include "bio++.H"
7 #include "meryl.H"
8
9
10 // Some string handling utilities.
11 //
12 bool
13 writeString(const char *str, FILE *F) {
14 errno = 0;
15
16 uint32 len = 0;
17 if (str) {
18 len = (uint32)strlen(str) + 1;
19 fwrite(&len, sizeof(uint32), 1, F);
20 fwrite( str, sizeof(char), len, F);
21 } else {
22 fwrite(&len, sizeof(uint32), 1, F);
23 }
24
25 if (errno) {
26 fprintf(stderr, "writeString()-- Failed to write string of length "uint32FMT": %s\n", len, strerror(errno));
27 fprintf(stderr, "writeString()-- First 80 bytes of string is:\n");
28 fprintf(stderr, "%80.80s\n", str);
29 return(false);
30 }
31
32 return(true);
33 }
34
35 char*
36 readString(FILE *F) {
37 errno = 0;
38
39 uint32 len = 0;
40 fread(&len, sizeof(uint32), 1, F);
41 if (errno) {
42 fprintf(stderr, "readString()-- Failed to read string: %s\n", strerror(errno));
43 exit(1);
44 }
45
46 char *str = 0L;
47
48 if (len > 0) {
49 str = new char [len];
50 fread(str, sizeof(char), len, F);
51 if (errno) {
52 fprintf(stderr, "readString()-- Failed to read string: %s\n", strerror(errno));
53 exit(1);
54 }
55 }
56
57 return(str);
58 }
59
60 char*
61 duplString(char *str) {
62 char *dupstr = 0L;
63 if (str) {
64 uint32 len = (uint32)strlen(str);
65 dupstr = new char [len+1];
66 strcpy(dupstr, str);
67 }
68 return(dupstr);
69 }
70
71
72
73 void
74 merylArgs::usage(void) {
75 fprintf(stderr, "usage: %s [personality] [global options] [options]\n", execName);
76 fprintf(stderr, "\n");
77 fprintf(stderr, "where personality is:\n");
78 fprintf(stderr, " -P -- compute parameters\n");
79 fprintf(stderr, " -B -- build table\n");
80 fprintf(stderr, " -S -- scan table\n");
81 fprintf(stderr, " -M -- \"math\" operations\n");
82 fprintf(stderr, " -D -- dump table\n");
83 fprintf(stderr, "\n");
84 fprintf(stderr, "-P: Given a sequence file (-s) or an upper limit on the\n");
85 fprintf(stderr, " number of mers in the file (-n), compute the table size\n");
86 fprintf(stderr, " (-t in build) to minimize the memory usage.\n");
87 fprintf(stderr, " -m # (size of a mer; required)\n");
88 fprintf(stderr, " -c # (homopolymer compression; optional)\n");
89 fprintf(stderr, " -p (enable positions)\n");
90 fprintf(stderr, " -s seq.fasta (seq.fasta is scanned to determine the number of mers)\n");
91 fprintf(stderr, " -n # (compute params assuming file with this many mers in it)\n");
92 fprintf(stderr, "\n");
93 fprintf(stderr, " Only one of -s, -n need to be specified. If both are given\n");
94 fprintf(stderr, " -s takes priority.\n");
95 fprintf(stderr, "\n");
96 fprintf(stderr, "\n");
97 fprintf(stderr, "-B: Given a sequence file (-s) and lots of parameters, compute\n");
98 fprintf(stderr, " the mer-count tables. By default, both strands are processed.\n");
99 fprintf(stderr, " -f (only build for the forward strand)\n");
100 fprintf(stderr, " -r (only build for the reverse strand)\n");
101 fprintf(stderr, " -C (use canonical mers, assumes both strands)\n");
102 fprintf(stderr, " -L # (DON'T save mers that occur less than # times)\n");
103 fprintf(stderr, " -U # (DON'T save mers that occur more than # times)\n");
104 fprintf(stderr, " -m # (size of a mer; required)\n");
105 fprintf(stderr, " -c # (homopolymer compression; optional)\n");
106 fprintf(stderr, " -p (enable positions)\n");
107 fprintf(stderr, " -s seq.fasta (sequence to build the table for)\n");
108 fprintf(stderr, " -o tblprefix (output table prefix)\n");
109 fprintf(stderr, " -v (entertain the user)\n");
110 fprintf(stderr, "\n");
111 fprintf(stderr, " By default, the computation is done as one large sequential process.\n");
112 fprintf(stderr, " Multi-threaded operation is possible, at additional memory expense, as\n");
113 fprintf(stderr, " is segmented operation, at additional I/O expense.\n");
114 fprintf(stderr, "\n");
115 fprintf(stderr, " Threaded operation: Split the counting in to n almost-equally sized\n");
116 fprintf(stderr, " pieces. This uses an extra h MB (from -P) per thread.\n");
117 fprintf(stderr, " -threads n (use n threads to build)\n");
118 fprintf(stderr, "\n");
119 fprintf(stderr, " Segmented, sequential operation: Split the counting into pieces that\n");
120 fprintf(stderr, " will fit into no more than m MB of memory, or into n equal sized pieces.\n");
121 fprintf(stderr, " Each piece is computed sequentially, and the results are merged at the end.\n");
122 fprintf(stderr, " Only one of -memory and -segments is needed.\n");
123 fprintf(stderr, " -memory mMB (use at most m MB of memory per segment)\n");
124 fprintf(stderr, " -segments n (use n segments)\n");
125 fprintf(stderr, "\n");
126 fprintf(stderr, " Segmented, batched operation: Same as sequential, except this allows\n");
127 fprintf(stderr, " each segment to be manually executed in parallel.\n");
128 fprintf(stderr, " Only one of -memory and -segments is needed.\n");
129 fprintf(stderr, " -memory mMB (use at most m MB of memory per segment)\n");
130 fprintf(stderr, " -segments n (use n segments)\n");
131 fprintf(stderr, " -configbatch (create the batches)\n");
132 fprintf(stderr, " -countbatch n (run batch number n)\n");
133 fprintf(stderr, " -mergebatch (merge the batches)\n");
134 fprintf(stderr, " Initialize the compute with -configbatch, which needs all the build options.\n");
135 fprintf(stderr, " Execute all -countbatch jobs, then -mergebatch to complete.\n");
136 fprintf(stderr, " meryl -configbatch -B [options] -o file\n");
137 fprintf(stderr, " meryl -countbatch 0 -o file\n");
138 fprintf(stderr, " meryl -countbatch 1 -o file\n");
139 fprintf(stderr, " ...\n");
140 fprintf(stderr, " meryl -countbatch N -o file\n");
141 fprintf(stderr, " meryl -mergebatch N -o file\n");
142 fprintf(stderr, " Batched mode can run on the grid.\n");
143 fprintf(stderr, " -sge jobname unique job name for this execution. Meryl will submit\n");
144 fprintf(stderr, " jobs with name mpjobname, ncjobname, nmjobname, for\n");
145 fprintf(stderr, " phases prepare, count and merge.\n");
146 fprintf(stderr, " -sgebuild \"options\" any additional options to sge, e.g.,\n");
147 fprintf(stderr, " -sgemerge \"options\" \"-p -153 -pe thread 2 -A merylaccount\"\n");
148 fprintf(stderr, " N.B. - -N will be ignored\n");
149 fprintf(stderr, " N.B. - be sure to quote the options\n");
150 fprintf(stderr, "\n");
151 fprintf(stderr, "-M: Given a list of tables, perform a math, logical or threshold operation.\n");
152 fprintf(stderr, " Unless specified, all operations take any number of databases.\n");
153 fprintf(stderr, "\n");
154 fprintf(stderr, " Math operations are:\n");
155 fprintf(stderr, " min count is the minimum count for all databases. If the mer\n");
156 fprintf(stderr, " does NOT exist in all databases, the mer has a zero count, and\n");
157 fprintf(stderr, " is NOT in the output.\n");
158 fprintf(stderr, " minexist count is the minimum count for all databases that contain the mer\n");
159 fprintf(stderr, " max count is the maximum count for all databases\n");
160 fprintf(stderr, " add count is sum of the counts for all databases\n");
161 fprintf(stderr, " sub count is the first minus the second (binary only)\n");
162 fprintf(stderr, " abs count is the absolute value of the first minus the second (binary only)\n");
163 fprintf(stderr, "\n");
164 fprintf(stderr, " Logical operations are:\n");
165 fprintf(stderr, " and outputs mer iff it exists in all databases\n");
166 fprintf(stderr, " nand outputs mer iff it exists in at least one, but not all, databases\n");
167 fprintf(stderr, " or outputs mer iff it exists in at least one database\n");
168 fprintf(stderr, " xor outputs mer iff it exists in an odd number of databases\n");
169 fprintf(stderr, "\n");
170 fprintf(stderr, " Threshold operations are:\n");
171 fprintf(stderr, " lessthan x outputs mer iff it has count < x\n");
172 fprintf(stderr, " lessthanorequal x outputs mer iff it has count <= x\n");
173 fprintf(stderr, " greaterthan x outputs mer iff it has count > x\n");
174 fprintf(stderr, " greaterthanorequal x outputs mer iff it has count >= x\n");
175 fprintf(stderr, " equal x outputs mer iff it has count == x\n");
176 fprintf(stderr, " Threshold operations work on exactly one database.\n");
177 fprintf(stderr, "\n");
178 fprintf(stderr, " -s tblprefix (use tblprefix as a database)\n");
179 fprintf(stderr, " -o tblprefix (create this output)\n");
180 fprintf(stderr, " -v (entertain the user)\n");
181 fprintf(stderr, "\n");
182 fprintf(stderr, " NOTE: Multiple tables are specified with multiple -s switches; e.g.:\n");
183 fprintf(stderr, " %s -M add -s 1 -s 2 -s 3 -s 4 -o all\n", execName);
184 fprintf(stderr, " NOTE: It is NOT possible to specify more than one operation:\n");
185 fprintf(stderr, " %s -M add -s 1 -s 2 -sub -s 3\n", execName);
186 fprintf(stderr, " will NOT work.\n");
187 fprintf(stderr, "\n");
188 fprintf(stderr, "\n");
189 fprintf(stderr, "-D: Dump the table (not all of these work).\n");
190 fprintf(stderr, "\n");
191 fprintf(stderr, " -Dd Dump a histogram of the distance between the same mers.\n");
192 fprintf(stderr, " -Dt Dump mers >= a threshold. Use -n to specify the threshold.\n");
193 fprintf(stderr, " -Dc Count the number of mers, distinct mers and unique mers.\n");
194 fprintf(stderr, " -Dh Dump (to stdout) a histogram of mer counts.\n");
195 fprintf(stderr, " -s Read the count table from here (leave off the .mcdat or .mcidx).\n");
196 fprintf(stderr, "\n");
197 fprintf(stderr, "\n");
198 }
199
200
201
202 void
203 merylArgs::clear(void) {
204
205 execName = 0L;
206 options = 0L;
207
208 beVerbose = false;
209 doForward = true;
210 doReverse = false;
211 doCanonical = false;
212
213 inputFile = 0L;
214 outputFile = 0L;
215 queryFile = 0L;
216
217 merSize = 20;
218 merComp = 0;
219 positionsEnabled = false;
220
221 numMersEstimated = 0;
222 numMersActual = 0;
223
224 numBasesActual = 0;
225
226 mersPerBatch = 0;
227 basesPerBatch = 0;
228
229 numBuckets = 0;
230 numBuckets_log2 = 0;
231 merDataWidth = 0;
232 merDataMask = uint64ZERO;
233 bucketPointerWidth = 0;
234
235 numThreads = 0;
236 memoryLimit = 0;
237 segmentLimit = 0;
238 configBatch = false;
239 countBatch = false;
240 mergeBatch = false;
241 batchNumber = 0;
242
243 sgeJobName = 0L;
244 sgeBuildOpt = 0L;
245 sgeMergeOpt = 0L;
246 isOnGrid = false;
247
248 lowCount = 0;
249 highCount = ~lowCount;
250 desiredCount = 0;
251
252 outputCount = 0;
253 outputAll = 0;
254 outputPosition = 0;
255
256 mergeFilesMax = 0;
257 mergeFilesLen = 0;
258 mergeFiles = 0L;
259
260 personality = 0;
261 }
262
263
264
265
266 merylArgs::merylArgs(int argc, char **argv) {
267
268 clear();
269
270 execName = duplString(argv[0]);
271
272 if (argc == 1) {
273 usage();
274 exit(1);
275 }
276
277 // Count how many '-s' switches there are, then allocate space
278 // for them in mergeFiles. We also sum the length of all options,
279 // so we can copy them into an 'options' string used when we
280 // resubmit to the grid.
281 //
282 uint32 optionsLen = 0;
283 for (int arg=1; arg < argc; arg++) {
284 optionsLen += strlen(argv[arg]) + 1;
285 if (strcmp(argv[arg], "-s") == 0)
286 mergeFilesMax++;
287 }
288
289 mergeFiles = new char * [mergeFilesMax];
290 options = new char [2 * optionsLen + 1];
291 options[0] = 0;
292
293 bool fail = false;
294
295 char *optptr = options;
296
297 for (int arg=1; arg < argc; arg++) {
298 if (arg > 1)
299 *optptr++ = ' ';
300
301 // Arg! If the arg has spaces or other stuff that the shell
302 // needs escaped we need to escape them again. So, we copy byte
303 // by byte and insert escapes at the right points.
304
305 for (char *op=argv[arg]; *op; op++, optptr++) {
306 if (isspace(*op) || !isalnum(*op))
307 if ((*op != '-') && (*op != '_') && (*op != '.') && (*op != '/'))
308 *optptr++ = '\\';
309 *optptr = *op;
310 }
311
312 //strcat(options, argv[arg]);
313 }
314
315
316 // Parse the options
317 //
318 for (int arg=1; arg < argc; arg++) {
319 if (strncmp(argv[arg], "-V", 2) == 0) {
320 fprintf(stdout, "meryl the Mighty Mer Counter version (no version)\n");
321 exit(0);
322 } else if (strcmp(argv[arg], "-m") == 0) {
323 arg++;
324 merSize = strtouint32(argv[arg], 0L);
325 } else if (strcmp(argv[arg], "-c") == 0) {
326 arg++;
327 merComp = strtouint32(argv[arg], 0L);
328 } else if (strcmp(argv[arg], "-p") == 0) {
329 positionsEnabled = true;
330 } else if (strcmp(argv[arg], "-s") == 0) {
331 arg++;
332 delete [] inputFile;
333 inputFile = duplString(argv[arg]);
334 mergeFiles[mergeFilesLen++] = duplString(argv[arg]);
335 } else if (strcmp(argv[arg], "-n") == 0) {
336 arg++;
337 numMersEstimated = strtouint64(argv[arg], 0L);
338 } else if (strcmp(argv[arg], "-f") == 0) {
339 doForward = true;
340 doReverse = false;
341 doCanonical = false;
342 } else if (strcmp(argv[arg], "-r") == 0) {
343 doForward = false;
344 doReverse = true;
345 doCanonical = false;
346 } else if (strcmp(argv[arg], "-C") == 0) {
347 doForward = false;
348 doReverse = false;
349 doCanonical = true;
350 } else if (strcmp(argv[arg], "-L") == 0) {
351 arg++;
352 lowCount = strtouint32(argv[arg], 0L);
353 } else if (strcmp(argv[arg], "-U") == 0) {
354 arg++;
355 highCount = strtouint32(argv[arg], 0L);
356 } else if (strcmp(argv[arg], "-o") == 0) {
357 arg++;
358 delete [] outputFile;
359 outputFile = duplString(argv[arg]);
360 } else if (strcmp(argv[arg], "-v") == 0) {
361 beVerbose = true;
362
363 } else if (strcmp(argv[arg], "-P") == 0) {
364 personality = 'P';
365 } else if (strcmp(argv[arg], "-B") == 0) {
366 personality = 'B';
367 } else if (strcmp(argv[arg], "-S") == 0) {
368 personality = 'S';
369 } else if (strcmp(argv[arg], "-M") == 0) {
370 arg++;
371 if (strcmp(argv[arg], "merge") == 0) {
372 personality = PERSONALITY_MERGE;
373 } else if (strcmp(argv[arg], "min") == 0) {
374 personality = PERSONALITY_MIN;
375 } else if (strcmp(argv[arg], "minexist") == 0) {
376 personality = PERSONALITY_MINEXIST;
377 } else if (strcmp(argv[arg], "max") == 0) {
378 personality = PERSONALITY_MAX;
379 } else if (strcmp(argv[arg], "maxexist") == 0) {
380 personality = PERSONALITY_MAXEXIST;
381 } else if (strcmp(argv[arg], "add") == 0) {
382 personality = PERSONALITY_ADD;
383 } else if (strcmp(argv[arg], "sub") == 0) {
384 personality = PERSONALITY_SUB;
385 } else if (strcmp(argv[arg], "abs") == 0) {
386 personality = PERSONALITY_ABS;
387 } else if (strcmp(argv[arg], "divide") == 0) {
388 personality = PERSONALITY_DIVIDE;
389 } else if (strcmp(argv[arg], "and") == 0) {
390 personality = PERSONALITY_AND;
391 } else if (strcmp(argv[arg], "nand") == 0) {
392 personality = PERSONALITY_NAND;
393 } else if (strcmp(argv[arg], "or") == 0) {
394 personality = PERSONALITY_OR;
395 } else if (strcmp(argv[arg], "xor") == 0) {
396 personality = PERSONALITY_XOR;
397 } else if (strcmp(argv[arg], "lessthan") == 0) {
398 personality = PERSONALITY_LEQ;
399 arg++;
400 desiredCount = strtouint32(argv[arg], 0L) - 1;
401 } else if (strcmp(argv[arg], "lessthanorequal") == 0) {
402 personality = PERSONALITY_LEQ;
403 arg++;
404 desiredCount = strtouint32(argv[arg], 0L);
405 } else if (strcmp(argv[arg], "greaterthan") == 0) {
406 personality = PERSONALITY_GEQ;
407 arg++;
408 desiredCount = strtouint32(argv[arg], 0L) + 1;
409 } else if (strcmp(argv[arg], "greaterthanorequal") == 0) {
410 personality = PERSONALITY_GEQ;
411 arg++;
412 desiredCount = strtouint32(argv[arg], 0L);
413 } else if (strcmp(argv[arg], "equal") == 0) {
414 personality = PERSONALITY_EQ;
415 arg++;
416 desiredCount = strtouint32(argv[arg], 0L);
417 } else {
418 fprintf(stderr, "ERROR: unknown math personality %s\n", argv[arg]);
419 exit(1);
420 }
421 } else if (strcmp(argv[arg], "-Dd") == 0) {
422 personality = 'd';
423 } else if (strcmp(argv[arg], "-Dt") == 0) {
424 personality = 't';
425 } else if (strcmp(argv[arg], "-Dp") == 0) {
426 personality = 'p';
427 } else if (strcmp(argv[arg], "-Dc") == 0) {
428 personality = 'c';
429 } else if (strcmp(argv[arg], "-Dh") == 0) {
430 personality = 'h';
431 } else if (strcmp(argv[arg], "-memory") == 0) {
432 arg++;
433 memoryLimit = strtouint64(argv[arg], 0L) * 1024 * 1024;
434 } else if (strcmp(argv[arg], "-segments") == 0) {
435 arg++;
436 segmentLimit = strtouint64(argv[arg], 0L);
437 } else if (strcmp(argv[arg], "-threads") == 0) {
438 arg++;
439 numThreads = strtouint32(argv[arg], 0L);
440 } else if (strcmp(argv[arg], "-configbatch") == 0) {
441 personality = 'B';
442 configBatch = true;
443 countBatch = false;
444 mergeBatch = false;
445 batchNumber = uint32ZERO;
446 } else if (strcmp(argv[arg], "-countbatch") == 0) {
447 arg++;
448 personality = 'B';
449 configBatch = false;
450 countBatch = true;
451 mergeBatch = false;
452 batchNumber = strtouint32(argv[arg], 0L);
453 } else if (strcmp(argv[arg], "-mergebatch") == 0) {
454 personality = 'B';
455 configBatch = false;
456 countBatch = false;
457 mergeBatch = true;
458 batchNumber = uint32ZERO;
459 } else if (strcmp(argv[arg], "-sge") == 0) {
460 sgeJobName = argv[++arg];
461 } else if (strcmp(argv[arg], "-sgebuild") == 0) {
462 sgeBuildOpt = argv[++arg];
463 } else if (strcmp(argv[arg], "-sgemerge") == 0) {
464 sgeMergeOpt = argv[++arg];
465 } else if (strcmp(argv[arg], "-forcebuild") == 0) {
466 isOnGrid = true;
467 } else {
468 fprintf(stderr, "Unknown option '%s'.\n", argv[arg]);
469 fail = true;
470 }
471 }
472
473 // Using threads is only useful if we are not a batch.
474 //
475 if ((numThreads > 0) && (configBatch || countBatch || mergeBatch)) {
476 if (configBatch)
477 fprintf(stderr, "WARNING: -threads has no effect with -configbatch, disabled.\n");
478 if (countBatch)
479 fprintf(stderr, "WARNING: -threads has no effect with -countbatch, disabled.\n");
480 if (mergeBatch)
481 fprintf(stderr, "WARNING: -threads has no effect with -mergebatch, disabled.\n");
482 numThreads = 0;
483 }
484
485 // SGE is not useful unless we are in batch mode.
486 //
487 if (sgeJobName && !configBatch && !countBatch && !mergeBatch) {
488 fprintf(stderr, "ERROR: -sge not useful unless in batch mode (replace -B with -configbatch)\n");
489 exit(1);
490 }
491
492 if (fail)
493 exit(1);
494 }
495
496
497
498 merylArgs::merylArgs(const char *prefix) {
499
500 clear();
501
502 char *filename = new char [strlen(prefix) + 17];
503 sprintf(filename, "%s.merylArgs", prefix);
504
505 errno = 0;
506 FILE *F = fopen(filename, "rb");
507 if (errno) {
508 fprintf(stderr, "merylArgs::readConfig()-- Failed to open '%s': %s\n", filename, strerror(errno));
509 exit(1);
510 }
511
512 char magic[17] = {0};
513 fread(magic, sizeof(char), 16, F);
514 if (strncmp(magic, "merylBatcherv02", 16) != 0) {
515 fprintf(stderr, "merylArgs::readConfig()-- '%s' doesn't appear to be a merylArgs file.\n", filename);
516 exit(1);
517 }
518
519 // Load the config, then reset the pointers.
520
521 fread(this, sizeof(merylArgs), 1, F);
522
523 execName = readString(F);
524 options = 0L;
525 inputFile = readString(F);
526 outputFile = readString(F);
527 queryFile = 0L;
528 sgeJobName = readString(F);
529 sgeBuildOpt = readString(F);
530 sgeMergeOpt = readString(F);
531
532 mergeFiles = new char* [mergeFilesLen];
533
534 for (uint32 i=0; i<mergeFilesLen; i++)
535 mergeFiles[i] = readString(F);
536
537 fclose(F);
538
539 delete [] filename;
540 }
541
542
543
544 merylArgs::~merylArgs() {
545 delete [] execName;
546 delete [] options;
547 delete [] inputFile;
548 delete [] outputFile;
549
550 for (uint32 i=0; i<mergeFilesLen; i++)
551 delete [] mergeFiles[i];
552
553 delete [] mergeFiles;
554 }
555
556
557
558 bool
559 merylArgs::writeConfig(void) {
560 char *filename;
561
562 filename = new char [strlen(outputFile) + 17];
563 sprintf(filename, "%s.merylArgs", outputFile);
564
565 errno = 0;
566 FILE *F = fopen(filename, "wb");
567 if (errno) {
568 fprintf(stderr, "merylArgs::writeConfig()-- Failed to open '%s': %s\n", filename, strerror(errno));
569 exit(1);
570 }
571
572 fwrite("merylBatcherv02", sizeof(char), 16, F);
573
574 fwrite(this, sizeof(merylArgs), 1, F);
575
576 writeString(execName, F);
577 writeString(inputFile, F);
578 writeString(outputFile, F);
579 writeString(sgeJobName, F);
580 writeString(sgeBuildOpt, F);
581 writeString(sgeMergeOpt, F);
582
583 for (uint32 i=0; i<mergeFilesLen; i++)
584 writeString(mergeFiles[i], F);
585
586 fclose(F);
587
588 return(true);
589 }