forked from dzerbino/WiggleTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandParser.c
581 lines (501 loc) · 16.5 KB
/
commandParser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright 2013 EMBL-EBI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <string.h>
// Local header
#include "multiplexer.h"
void printHelp() {
puts("WiggleTools");
puts("");
puts("Copyright EMBL-EBI, 2013.");
puts("Development contact: Daniel Zerbino [email protected]");
puts("");
puts("This library parses wiggle files and executes various operations on them streaming through lazy evaluators.");
puts("");
puts("Inputs:");
puts("\tThe program takes in Wig, BigWig, BedGraph, Bed, BigBed and Bam files, which are distinguished thanks to their suffix (.wig, .bw, .bg, .bed, .bb, .bam respectively).");
puts("\tNote that wiggletools assumes that every bam file has an index .bai file next to it.");
puts("");
puts("Outputs:");
puts("\tThe program outputs a wiggle file in stdout unless the output is squashed");
puts("");
puts("Command line:");
puts("\twiggletools --help");
puts("\twiggletools program");
puts("");
puts("Program grammar:");
puts("\tprogram = (iterator) | do (iterator) | (statistic) | (extraction)");
puts("\tstatistic = AUC (output) (iterator) | mean (output) (iterator) | variance (output) (iterator) | pearson (output) (iterator) (iterator) | isZero (iterator)");
puts("\toutput = filename | -");
puts("\textraction = profile (output) (int) (iterator) (iterator) | profiles (output) (int) (iterator) (iterator)");
puts("\t\t| apply_paste (out_filename) (statistic) (bed_file) (iterator)");
puts("\titerator = (filename) | (unary_operator) (iterator) | (binary_operator) (iterator) (iterator) | (reducer) (multiplex) | (setComparison) (multiplex) (multiplex)");
puts("\tunary_operator = unit | write (output) | write_bg (ouput) | smooth (int) | exp | ln | log (float) | pow (float) | offset (float) | scale (float) | gt (float)");
puts("\tbinary_operator = diff | ratio | apply (statistic)");
puts("\treducer = cat | sum | product | mean | var | stddev | entropy | CV | median | min | max");
puts("\titerator_list = (iterator) : | (iterator) (iterator_list)");
puts("\tmultiplex = (iterator_list) | map (unary_operator) (multiplex)");
puts("\tsetComparison = ttest | wilcoxon");
puts("\tfilename = *.wig | *.bw | *.bed | *.bb | *.bg | *.bam");
}
static char * nextToken(int argc, char ** argv) {
static int count;
static char ** ptr;
static int index = 0;
if (argv) {
ptr = argv;
count = argc;
}
if (index == count)
return NULL;
else
return ptr[index++];
}
static char * needNextToken() {
char * token = nextToken(0,0);
if (token) {
return token;
} else {
fprintf(stderr, "wiggletools: Unexpected end of command line\n");
exit(1);
}
}
static WiggleIterator * readIteratorToken(char * token);
static WiggleIterator * readIterator() {
return readIteratorToken(needNextToken());
}
static WiggleIterator ** readFileList(int * count, char * firstToken) {
size_t buffer_size = 8;
char * token;
int i =0;
WiggleIterator ** iters = (WiggleIterator **) calloc(buffer_size, sizeof(WiggleIterator*));
for (token = firstToken; token != NULL && strcmp(token, ":"); token = nextToken(0,0)) {
if (i == buffer_size) {
buffer_size *= 2;
iters = (WiggleIterator **) realloc(iters, buffer_size * sizeof(WiggleIterator*));
}
iters[i++] = readIteratorToken(token);
}
*count = i;
return iters;
}
static WiggleIterator ** readIteratorList(int * count);
static WiggleIterator ** readMappedIteratorList(int * count) {
char * token = needNextToken();
WiggleIterator ** iters;
int i;
if (strcmp(token, "unit") == 0) {
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = UnitWiggleIterator(iters[i]);
} else if (strcmp(token, "smooth") == 0) {
int width = atoi(needNextToken());
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = SmoothWiggleIterator(iters[i], width);
} else if (strcmp(token, "exp") == 0) {
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = NaturalExpWiggleIterator(iters[i]);
} else if (strcmp(token, "ln") == 0) {
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = NaturalLogWiggleIterator(iters[i]);
} else if (strcmp(token, "log") == 0) {
double base = atof(needNextToken());
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = LogWiggleIterator(iters[i], base);
} else if (strcmp(token, "pow") == 0) {
double base = atof(needNextToken());
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = PowerWiggleIterator(iters[i], base);
} else if (strcmp(token, "scale") == 0) {
double scalar = atof(needNextToken());
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = ScaleWiggleIterator(iters[i], scalar);
} else if (strcmp(token, "offset") == 0) {
double scalar = atof(needNextToken());
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = ShiftWiggleIterator(iters[i], scalar);
} else if (strcmp(token, "gt") == 0) {
double scalar = atof(needNextToken());
iters = readIteratorList(count);
for (i = 0; i < *count; i++)
iters[i] = HighPassFilterWiggleIterator(iters[i], scalar);
} else {
fprintf(stderr, "Unary function unkown: %s\n", token);
exit(1);
}
return iters;
}
static WiggleIterator ** readIteratorList(int * count) {
char * token = needNextToken();
if (strcmp(token, "map"))
return readFileList(count, token);
else
return readMappedIteratorList(count);
}
static Multiplexer * readMultiplexer() {
int count = 0;
WiggleIterator ** iters = readIteratorList(&count);
return newMultiplexer(iters, count);
}
static FILE * readOutputFilename() {
char * filename = needNextToken();
if (strcmp(filename, "-")) {
FILE * file = fopen(filename, "w");
if (!file) {
fprintf(stderr, "Could not open output file %s.\n", filename);
exit(1);
}
return file;
} else
return stdout;
}
static WiggleIterator * readTee() {
FILE * file = readOutputFilename();
return TeeWiggleIterator(readIterator(), file, false);
}
static WiggleIterator * readBGTee() {
FILE * file = readOutputFilename();
return TeeWiggleIterator(readIterator(), file, true);
}
static WiggleIterator * readLastIteratorToken(char * token) {
WiggleIterator * iter = readIteratorToken(token);
char * remainder = nextToken(0,0);
if (remainder) {
fprintf(stderr, "Trailing tokens: the last tokens in your command were not read, check your syntax:\n...");
while (remainder) {
fprintf(stderr, " %s", remainder);
remainder = nextToken(0,0);
}
fprintf(stderr, "\n");
exit(1);
}
return iter;
}
static WiggleIterator * readLastIterator() {
return readLastIteratorToken(needNextToken());
}
static WiggleIterator * readBTee() {
char * filename = needNextToken();
FILE * file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Could not open %s.\n", filename);
exit(1);
}
return BinaryTeeWiggleIterator(readIterator(), file, false);
}
static WiggleIterator * readSmooth() {
int width = atoi(needNextToken());
return SmoothWiggleIterator(readIterator(), width);
}
static WiggleIterator * readPow() {
double base = atof(needNextToken());
return PowerWiggleIterator(readIterator(), base);
}
static WiggleIterator * readGt() {
double cutoff = atof(needNextToken());
return HighPassFilterWiggleIterator(readIterator(), cutoff);
}
static WiggleIterator * readScale() {
double scalar = atof(needNextToken());
return ScaleWiggleIterator(readIterator(), scalar);
}
static WiggleIterator * readShift() {
double scalar = atof(needNextToken());
return ShiftWiggleIterator(readIterator(), scalar);
}
static WiggleIterator * readExp() {
return NaturalExpWiggleIterator(readIterator());
}
static WiggleIterator * readSum() {
return SumReduction(readMultiplexer());
}
static char ** getListOfFilenames(int * count, char * first) {
int length = 1000;
char ** filenames = calloc(sizeof(char*), length);
char * token;
if (first) {
filenames[0] = first;
(*count)++;
}
for (token = needNextToken(); token != NULL && strcmp(token, ":"); token = nextToken(0,0)) {
filenames[*count] = token;
if (++(*count) == length) {
length += 1000;
filenames = realloc(filenames, sizeof(char *) * length);
}
}
return filenames;
}
static WiggleIterator * readCat() {
int count = 0;
char ** filenames = getListOfFilenames(&count, NULL);
return CatWiggleIterator(filenames, count);
}
static WiggleIterator * readProduct() {
return ProductReduction(readMultiplexer());
}
static WiggleIterator * readMin() {
return MinReduction(readMultiplexer());
}
static WiggleIterator * readMax() {
return MaxReduction(readMultiplexer());
}
static WiggleIterator * readMean() {
return MeanReduction(readMultiplexer());
}
static WiggleIterator * readVariance() {
return VarianceReduction(readMultiplexer());
}
static WiggleIterator * readStdDev() {
return StdDevReduction(readMultiplexer());
}
static WiggleIterator * readEntropy() {
return EntropyReduction(readMultiplexer());
}
static WiggleIterator * readCV() {
return CVReduction(readMultiplexer());
}
static WiggleIterator * readMedian() {
return MedianReduction(readMultiplexer());
}
static WiggleIterator * readUnit() {
return UnitWiggleIterator(readIterator());
}
static WiggleIterator * readDifference() {
WiggleIterator ** iters = calloc(2, sizeof(WiggleIterator *));
iters[0] = readIterator();
iters[1] = ScaleWiggleIterator(readIterator(), -1);
return SumReduction(newMultiplexer(iters, 2));
}
static WiggleIterator * readRatio() {
WiggleIterator ** iters = calloc(2, sizeof(WiggleIterator *));
iters[0] = readIterator();
iters[1] = PowerWiggleIterator(readIterator(), -1);
return ProductReduction(newMultiplexer(iters, 2));
}
static WiggleIterator * readSeek() {
char * chrom = needNextToken();
int start = atoi(needNextToken());
int finish = atoi(needNextToken());
WiggleIterator * iter = readIterator();
seek(iter, chrom, start, finish);
return iter;
}
static WiggleIterator * readNaturalLog() {
return NaturalLogWiggleIterator(readIterator());
}
static WiggleIterator * readLog() {
double base = atof(needNextToken());
return LogWiggleIterator(readIterator(), base);
}
static WiggleIterator * readTTest() {
Multiplexer ** multis = calloc(2, sizeof(Multiplexer *));
multis[0] = readMultiplexer();
multis[1] = readMultiplexer();
return TTestReduction(newMultiset(multis, 2));
}
static WiggleIterator * readMWUTest() {
Multiplexer ** multis = calloc(2, sizeof(Multiplexer *));
multis[0] = readMultiplexer();
multis[1] = readMultiplexer();
return MWUReduction(newMultiset(multis, 2));
}
static WiggleIterator * readApply() {
char * operation = needNextToken();
double (*function)(WiggleIterator *);
if (strcmp(operation, "AUC") == 0)
function = &AUC;
else if (strcmp(operation, "mean") == 0)
function = &mean;
else if (strcmp(operation, "variance") == 0)
function = &variance;
else {
fprintf(stderr, "Name of function to be applied unrecognized: %s\n", operation);
exit(1);
}
WiggleIterator * regions = readIterator();
WiggleIterator * data = readIterator();
return ApplyWiggleIterator(regions, function, data);
}
static WiggleIterator * readIteratorToken(char * token) {
if (strcmp(token, "cat") == 0)
return readCat();
if (strcmp(token, "scale") == 0)
return readScale();
if (strcmp(token, "offset") == 0)
return readShift();
if (strcmp(token, "unit") == 0)
return readUnit();
if (strcmp(token, "sum") == 0)
return readSum();
if (strcmp(token, "mult") == 0)
return readProduct();
if (strcmp(token, "diff") == 0)
return readDifference();
if (strcmp(token, "ratio") == 0)
return readRatio();
if (strcmp(token, "apply") == 0)
return readApply();
if (strcmp(token, "mean") == 0)
return readMean();
if (strcmp(token, "var") == 0)
return readVariance();
if (strcmp(token, "stddev") == 0)
return readStdDev();
if (strcmp(token, "entropy") == 0)
return readEntropy();
if (strcmp(token, "CV") == 0)
return readCV();
if (strcmp(token, "median") == 0)
return readMedian();
if (strcmp(token, "min") == 0)
return readMin();
if (strcmp(token, "max") == 0)
return readMax();
if (strcmp(token, "seek") == 0)
return readSeek();
if (strcmp(token, "btee") == 0)
return readBTee();
if (strcmp(token, "write") == 0)
return readTee();
if (strcmp(token, "write_bg") == 0)
return readBGTee();
if (strcmp(token, "smooth") == 0)
return readSmooth();
if (strcmp(token, "exp") == 0)
return readExp();
if (strcmp(token, "ln") == 0)
return readNaturalLog();
if (strcmp(token, "log") == 0)
return readLog();
if (strcmp(token, "pow") == 0)
return readPow();
if (strcmp(token, "gt") == 0)
return readGt();
if (strcmp(token, "ttest") == 0)
return readTTest();
if (strcmp(token, "wilcoxon") == 0)
return readMWUTest();
return SmartReader(token);
}
static void readProfile() {
FILE * file = readOutputFilename();
int width = atoi(needNextToken());
WiggleIterator * regions = readIterator();
WiggleIterator * wig = readLastIterator();
WiggleIterator * profiles = ProfileWiggleIterator(regions, width, wig);
double * profile = calloc(width, sizeof(double));
for (; !profiles->done; pop(profiles))
addProfile(profile, (double *) profiles->valuePtr, width);
int i;
for (i = 0; i < width; i++)
fprintf(file, "%i\t%lf\n", i, profile[i]);
free(profile);
fclose(file);
}
static void fprintfProfile(FILE * file, double * profile, int width) {
int i;
fprintf(file, "%f", profile[0]);
for (i = 1; i < width; i++)
fprintf(file, "\t%f", profile[i]);
fprintf(file, "\n");
}
static void readProfiles() {
FILE * file = readOutputFilename();
int width = atoi(needNextToken());
WiggleIterator * regions = readIterator();
WiggleIterator * wig = readLastIterator();
WiggleIterator * profiles;
for (profiles = ProfileWiggleIterator(regions, width, wig); !profiles->done; pop(profiles)) {
fprintf(file, "%s\t%i\t%i\t", profiles->chrom, profiles->start, profiles->finish);
fprintfProfile(file, (double *) profiles->valuePtr, width);
}
fclose(file);
}
static void readAUC() {
FILE * file = readOutputFilename();
fprintf(file, "%lf\n", AUC(readLastIterator()));
fclose(file);
}
static void readMeanIntegrated() {
FILE * file = readOutputFilename();
fprintf(file, "%lf\n", mean(readLastIterator()));
fclose(file);
}
static void readVarianceIntegrated() {
FILE * file = readOutputFilename();
fprintf(file, "%lf\n", variance(readLastIterator()));
fclose(file);
}
static void readPearson() {
FILE * file = readOutputFilename();
WiggleIterator * iter1 = readIterator();
WiggleIterator * iter2 = readLastIterator();
fprintf(file, "%lf\n", pearsonCorrelation(iter1, iter2));
fclose(file);
}
static WiggleIterator * readApplyPaste() {
FILE * outfile = readOutputFilename();
char * operation = needNextToken();
char * infilename = needNextToken();
FILE * infile = fopen(infilename, "r");
if (!infile) {
fprintf(stderr, "Could not open %s.\n", infilename);
exit(1);
}
double (*function)(WiggleIterator *);
if (strcmp(operation, "AUC") == 0)
function = &AUC;
else if (strcmp(operation, "mean") == 0)
function = &mean;
else if (strcmp(operation, "variance") == 0)
function = &variance;
else {
fprintf(stderr, "Name of function to be applied unrecognized: %s\n", operation);
exit(1);
}
return PasteWiggleIterator(ApplyWiggleIterator(SmartReader(infilename), function, readLastIterator()), infile, outfile);
}
void rollYourOwn(int argc, char ** argv) {
char * token = nextToken(argc, argv);
if (strcmp(token, "AUC") == 0)
readAUC();
else if (strcmp(token, "mean") == 0)
readMeanIntegrated();
else if (strcmp(token, "variance") == 0)
readVarianceIntegrated();
else if (strcmp(token, "pearson") == 0)
readPearson();
else if (strncmp(token, "write", 5) == 0)
runWiggleIterator(readLastIteratorToken(token));
else if (strcmp(token, "do") == 0)
runWiggleIterator(readLastIterator());
else if (strcmp(token, "isZero") == 0)
isZero(readLastIterator());
else if (strcmp(token, "apply_paste") == 0)
runWiggleIterator(readApplyPaste());
else if (strcmp(token, "profile") == 0)
readProfile();
else if (strcmp(token, "profiles") == 0)
readProfiles();
else
toStdout(readLastIteratorToken(token), false);
}