-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenodsp.c
2271 lines (1873 loc) · 58.2 KB
/
genodsp.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// genodsp.c-- (GENOme Digital Signal Processing) read a list of genomic
// intervals, with values, and perform base-granularity digital
// signal processing operations on the waveforms.
#include <stdlib.h>
#define true 1
#define false 0
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <float.h>
#include "utilities.h"
// program revision vitals (not the best way to do this!))
char* programName = "genodsp";
#define programVersionMajor "0"
#define programVersionMinor "0"
#define programVersionSubMinor "10"
#define programRevisionDate "20220616"
// dsp operators
#define globals_owner // (make this the owner of the global variables)
#include "genodsp_interface.h"
#include "sum.h"
#include "clump.h"
#include "percentile.h"
#include "add.h"
#include "multiply.h"
#include "mask.h"
#include "logical.h"
#include "minmax.h"
#include "morphology.h"
#include "map.h"
#include "opio.h"
#include "variables.h"
//----------
//
// global data and types--
//
//----------
// command line options
dspop* pipeline = NULL;
dspop* tailOp = NULL;
int valColumn = 4-1;
int noOutputValues = false;
int valPrecision = 0;
int collapseRuns = true;
int showUncovered = uncovered_hide; // (one of uncovered_show, etc.)
int clipToLength = false;
int originOne = false;
int inhibitOutput = false;
int dbgInput = false;
int dbgPipe = false;
int dbgGlobals = false;
// linked lists for scratch vectors
typedef struct svspec // vector of values
{
struct svspec* next; // next spec in a linked list
int inUse; // true => someone is using this vector
valtype* vector; // vector of values
} svspec;
typedef struct svispec // vector of integers
{
struct svispec* next; // next spec in a linked list
int inUse; // true => someone is using this vector
s32* vector; // vector of values
} svispec;
// linked lists for named global variables
typedef struct namedglobal
{
struct namedglobal* next; // next variable in a linked list
char* name; // variable's name (allocated within this block)
valtype v; // variable's value
} namedglobal;
// miscellany
#define min_of(a,b) ((a <= b)? a: b)
//----------
//
// prototypes--
//
//----------
int main (int argc, char** argv);
// private functions
static void parse_options (int _argc, char** _argv);
static int process_operator_options (int _argc, char** _argv);
static void read_chromosome_lengths (char* filename);
static int add_chromosome_spec (char* name,
u32 chromStart, u32 chromLength);
static void sort_chromosomes_by_length (void);
static void init_scratch_vectors (u32 scratchLength);
static void free_scratch_vectors (void);
static void init_named_globals (void);
static void free_named_globals (void);
// dsp operations table
dspinfo dspTable[] =
{dspinforecord("sum" , op_window_sum) ,
dspinfoalias ("window_sum") ,
dspinforecord("slidingsum" , op_sliding_sum) ,
dspinfoalias ("sliding_sum") ,
dspinforecord("smooth" , op_smooth) ,
dspinforecord("cumulativesum" , op_cumulative_sum) ,
dspinfoalias ("cumulative") ,
dspinfoalias ("integrate") ,
dspinforecord("clump" , op_clump) ,
dspinforecord("anticlump" , op_skimp) ,
dspinfoalias ("anti_clump") ,
dspinfoalias ("skimp") ,
dspinforecord("percentile" , op_percentile) ,
dspinforecord("add" , op_add) ,
dspinforecord("subtract" , op_subtract) ,
dspinforecord("addconst" , op_add_constant) ,
dspinfoalias ("add_const") ,
dspinforecord("invert" , op_invert) ,
dspinforecord("multiply" , op_multiply) ,
dspinforecord("divide" , op_divide) ,
dspinforecord("abs" , op_absolute_value) ,
dspinforecord("mask" , op_mask) ,
dspinforecord("masknot" , op_mask_not) ,
dspinfoalias ("mask_not") ,
dspinforecord("clip" , op_clip) ,
dspinforecord("erase" , op_erase) ,
dspinforecord("binarize" , op_binarize) ,
dspinforecord("or" , op_or) ,
dspinforecord("and" , op_and) ,
dspinforecord("maxover" , op_max_in_interval),
dspinfoalias ("max_over") ,
dspinforecord("minover" , op_min_in_interval),
dspinfoalias ("min_over") ,
dspinforecord("localmin" , op_local_minima) ,
dspinfoalias ("local_min") ,
dspinforecord("localmax" , op_local_maxima) ,
dspinfoalias ("local_max") ,
dspinforecord("bestmin", op_best_local_min) ,
dspinfoalias ("best_min") ,
dspinfoalias ("bestlocalmin") ,
dspinfoalias ("best_local_min") ,
dspinforecord("bestmax", op_best_local_max) ,
dspinfoalias ("best_max") ,
dspinfoalias ("bestlocalmax") ,
dspinfoalias ("best_local_max") ,
dspinforecord("minwith", op_min_with) ,
dspinfoalias ("min_with") ,
dspinforecord("maxwith", op_max_with) ,
dspinfoalias ("max_with") ,
dspinforecord("close" , op_close) ,
dspinforecord("open" , op_open) ,
dspinforecord("dilate" , op_dilate) ,
dspinforecord("erode" , op_erode) ,
dspinforecord("map" , op_map) ,
dspinforecord("input" , op_input) ,
dspinforecord("output" , op_output) ,
dspinforecord("variables" , op_show_variables) };
#define dspTableLen (sizeof(dspTable)/sizeof(dspinfo))
//----------
//
// option parsing--
//
//----------
#define specialPipeChar '='
static opfunc_usage chastiseUsage = NULL;
static char* chastiseUsageName = NULL;
static void usage (char* message);
static void usage_operations (void);
void chastise (const char* format, ...)
{
va_list args;
va_start (args, format);
if (format != NULL)
vfprintf (stderr, format, args);
va_end (args);
if (chastiseUsage != NULL)
{
(*chastiseUsage) (chastiseUsageName, stderr, " ");
exit (EXIT_FAILURE);
}
usage (NULL);
}
static void usage (char* message)
{
if (message != NULL) fprintf (stderr, "%s\n", message);
fprintf (stderr, "usage: [cat <file>] | %s --chromosomes=<filename> [options] [operations]\n", programName);
fprintf (stderr, "\n");
// 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (stderr, " --chromosomes=<filename> (required) read chromosome names and lengths from\n");
fprintf (stderr, " a file\n");
fprintf (stderr, " --value=<col> input intervals contain a value in the specified\n");
fprintf (stderr, " column; by default we assume this is in column 4\n");
fprintf (stderr, " --novalue input intervals have no value (value given is 1)\n");
fprintf (stderr, " --nooutputvalue don't write value with output intervals\n");
fprintf (stderr, " --precision=<number> number of digits to round output values to\n");
fprintf (stderr, " (by default, output is rounded to integers)\n");
fprintf (stderr, " --nocollapse in output, don't collapse runs of identical values\n");
fprintf (stderr, " to intervals\n");
fprintf (stderr, " --uncovered:hide don't output intervals that have no coverage\n");
fprintf (stderr, " (this is the default)\n");
fprintf (stderr, " --uncovered:show in output, include intervals that have no coverage\n");
fprintf (stderr, " --uncovered:NA in output, mark uncovered intervals as NA\n");
fprintf (stderr, " --cliptochromosome clip interals to chromosome length\n");
fprintf (stderr, " (default is to report such intervals as errors)\n");
fprintf (stderr, " --origin=one input/output intervals are origin-one, closed\n");
fprintf (stderr, " --origin=zero input/output intervals are origin-zero, half-open\n");
fprintf (stderr, " (this is the default)\n");
fprintf (stderr, " --nooutput don't output the resulting intervals/values\n");
fprintf (stderr, " (by default these are written to stdout)\n");
fprintf (stderr, " --window=<length> (W=) size of window\n");
fprintf (stderr, " (for operators that have a window size)\n");
fprintf (stderr, " --help[=<operator>] get detail about a particular operator\n");
fprintf (stderr, " ? list available operators with brief descriptions\n");
fprintf (stderr, " ?<operator> same as --help=<operator>\n");
fprintf (stderr, " --report=comments copy comments from the input to stderr. Comments\n");
fprintf (stderr, " are lines beginning with a \"#\". This can be\n");
fprintf (stderr, " helpful in tracking progress during a long run.\n");
fprintf (stderr, " --progress=input:<n> report processing of every nth input line\n");
fprintf (stderr, " --progress=operations report each operation as it begins\n");
fprintf (stderr, " --version report the program version and quit\n");
fprintf (stderr, "\n");
fprintf (stderr, "Note that if input intervals overlap, their values are summed.\n");
fprintf (stderr, "\n");
fprintf (stderr, "Input is usually piped in on stdin. However, if the first operator is \"input\"\n");
fprintf (stderr, "stdin is ignored.\n");
fprintf (stderr, "\n");
fprintf (stderr, "For a list of available operations, do \"genodsp ?\".\n");
fprintf (stderr, "For more detailed descriptions of the operations, do \"genodsp --help\".\n");
exit (EXIT_FAILURE);
}
static void usage_operations (void)
{
dspinfo* opInfo;
u32 dspIx;
fprintf (stderr, "Operations (general form is %c <operator> [arguments]):\n",
specialPipeChar);
for (dspIx=0 ; dspIx<dspTableLen ; dspIx++)
{
opInfo = &dspTable[dspIx];
if (opInfo->funcShort == NULL) continue;
(*opInfo->funcShort) (opInfo->name, 12, stderr, " ");
}
exit (EXIT_FAILURE);
}
static void parse_options (int _argc, char** _argv)
{
int argc;
char** argv;
char* arg, *argVal, *argScan, *argScan2;
char* chromsFilename = NULL;
u32 chromStart = 0;
u32 chromLength;
dspinfo* opInfo, *realOpInfo;
u32 dspIx;
int argsConsumed;
int tempInt;
// skip program name
//programName = _argv[0];
argv = _argv+1; argc = _argc - 1;
//////////
// scan arguments
//////////
if (argc == 0)
chastise (NULL);
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// operation
if (arg[0] == specialPipeChar)
{
if (dbgPipe)
fprintf (stderr, "pipe char in arg[%d]\n", (int) (argv-_argv));
if ((argc == 1) && (arg[0] == specialPipeChar) && (arg[1] == 0))
chastise ("%c at end of command line, with no operation\n",
specialPipeChar);
argsConsumed = process_operator_options (argc, argv);
argv += argsConsumed - 1;
argc -= argsConsumed - 1;
goto next_arg;
}
// --chromosomes=<filename>
if ((strcmp_prefix (arg, "--chromosomes=") == 0)
|| (strcmp_prefix (arg, "--chroms=") == 0))
{
chromsFilename = argVal;
goto next_arg;
}
// --value=<col>
if ((strcmp (arg, "--novalue") == 0)
|| (strcmp (arg, "--novalues") == 0)
|| (strcmp (arg, "--value=none") == 0))
{
valColumn = -1;
set_named_global ("valColumn", (valtype) valColumn);
goto next_arg;
}
if (strcmp_prefix (arg, "--value=") == 0)
{
valColumn = string_to_int (argVal) - 1;
if (valColumn == -1)
chastise ("value column can't be 0 (\"%s\")\n", arg);
if (valColumn < 0)
chastise ("value column can't be negative (\"%s\")\n", arg);
if (valColumn < 3)
chastise ("value column can't be 1, 2 or 3 (\"%s\")\n", arg);
set_named_global ("valColumn", (valtype) valColumn);
goto next_arg;
}
if ((strcmp (arg, "--nooutputvalue") == 0)
|| (strcmp (arg, "--nooutputvalues") == 0))
{
noOutputValues = true;
set_named_global ("noOutputValues", (valtype) noOutputValues);
goto next_arg;
}
// --precision=<col>
if (strcmp_prefix (arg, "--precision=") == 0)
{
valPrecision = string_to_int (argVal);
if (valPrecision < 0)
chastise ("precision can't be negative (\"%s\")\n", arg);
set_named_global ("valPrecision", (valtype) valPrecision);
goto next_arg;
}
// --nocollapse
if (strcmp (arg, "--nocollapse") == 0)
{
collapseRuns = false;
set_named_global ("collapseRuns", (valtype) collapseRuns);
goto next_arg;
}
// --uncovered:hide
if ((strcmp (arg, "--uncovered:hide") == 0)
|| (strcmp (arg, "--hide:uncovered") == 0))
{
showUncovered = uncovered_hide;
set_named_global ("showUncovered", (valtype) showUncovered);
goto next_arg;
}
// --uncovered:show
if ((strcmp (arg, "--uncovered:show") == 0)
|| (strcmp (arg, "--show:uncovered") == 0))
{
showUncovered = uncovered_show;
set_named_global ("showUncovered", (valtype) showUncovered);
goto next_arg;
}
// --uncovered:NA
if ((strcmp (arg, "--uncovered:NA") == 0)
|| (strcmp (arg, "--uncovered:mark") == 0)
|| (strcmp (arg, "--mark:uncovered") == 0)
|| (strcmp (arg, "--markgaps") == 0))
{
showUncovered = uncovered_NA;
set_named_global ("showUncovered", (valtype) showUncovered);
goto next_arg;
}
// --cliptochromosome
if ((strcmp (arg, "--cliptochromosome") == 0)
|| (strcmp (arg, "--cliptochrom") == 0)
|| (strcmp (arg, "--cliptolength") == 0)
|| (strcmp (arg, "--clip") == 0))
{
clipToLength = true;
goto next_arg;
}
// --origin=one, --origin=zero
if ((strcmp (arg, "--origin=one") == 0)
|| (strcmp (arg, "--origin=1") == 0))
{
originOne = true;
set_named_global ("originOne", (valtype) originOne);
goto next_arg;
}
if ((strcmp (arg, "--origin=zero") == 0)
|| (strcmp (arg, "--origin=0") == 0))
{
originOne = false;
set_named_global ("originOne", (valtype) originOne);
goto next_arg;
}
// --nooutput
if (strcmp (arg, "--nooutput") == 0)
{ inhibitOutput = true; goto next_arg; }
// --window=<length> or W=<length>
if ((strcmp_prefix (arg, "--window=") == 0)
|| (strcmp_prefix (arg, "W=") == 0)
|| (strcmp_prefix (arg, "--W=") == 0))
{
tempInt = string_to_unitized_int (argVal, /*thousands*/ true);
if (tempInt == 0)
chastise ("window size can't be zero (\"%s\")\n", arg);
if (tempInt < 0)
chastise ("window size can't be negative (\"%s\")\n", arg);
set_named_global ("windowSize", (valtype) tempInt);
goto next_arg;
}
// --help and --help=<operator> (and variations starting with '?')
if (strcmp (arg, "?") == 0)
{
usage_operations ();
exit (EXIT_SUCCESS);
}
if (strcmp_prefix (arg, "?") == 0)
{ argVal = arg+1; goto help_for_one; }
if ((strcmp_prefix (arg, "--help=") == 0)
|| (strcmp_prefix (arg, "?=") == 0))
{
if (strcmp (argVal, "*") == 0)
goto help_for_all_operators;
help_for_one:
opInfo = realOpInfo = NULL;
for (dspIx=0 ; dspIx<dspTableLen ; dspIx++)
{
if (dspTable[dspIx].funcShort != NULL) realOpInfo = &dspTable[dspIx];
if (strcmp (argVal,dspTable[dspIx].name) != 0) continue;
opInfo = realOpInfo;
break;
}
if (opInfo == NULL) goto help_for_unknown;
fprintf (stderr, "=== %s ===\n", opInfo->name);
(*opInfo->funcUsage) (opInfo->name, stderr, " ");
exit (EXIT_SUCCESS);
}
if (strcmp (arg, "--help") == 0)
{
help_for_all_operators:
for (dspIx=0 ; dspIx<dspTableLen ; dspIx++)
{
opInfo = &dspTable[dspIx];
if (opInfo->funcShort == NULL) continue;
fprintf (stderr, "=== %s ===\n", opInfo->name);
(*opInfo->funcUsage) (opInfo->name, stderr, " ");
}
exit (EXIT_SUCCESS);
}
// --report=comments
if ((strcmp (arg, "--report=comments") == 0)
|| (strcmp (arg, "--report:comments") == 0))
{ reportComments = true; goto next_arg; }
// --report=progress=input:<n>
if ((strcmp_prefix (arg, "--progress=input:") == 0)
|| (strcmp_prefix (arg, "--progress:input=") == 0)
|| (strcmp_prefix (arg, "--progress:input:") == 0))
{
if (strcmp_prefix (argVal, "input:") == 0)
argVal = strchr(arg,':') + 1;
reportInputProgress = string_to_unitized_int (argVal, /*thousands*/ true);
goto next_arg;
}
// --progress
if ((strcmp (arg, "--progress=operations") == 0)
|| (strcmp (arg, "--progress:operations") == 0)
|| (strcmp (arg, "--debug=operations") == 0)) // backward compatibility
{ trackOperations = true; goto next_arg; }
// --version
if (strcmp (arg, "--version") == 0)
{
fprintf (stderr, "%s (version %s.%s.%s released %s)\n",
programName,
programVersionMajor, programVersionMinor, programVersionSubMinor, programRevisionDate);
exit (EXIT_SUCCESS);
}
// --debug arguments
if (strcmp (arg, "--debug=input") == 0)
{ dbgInput = true; goto next_arg; }
if (strcmp (arg, "--debug=pipe") == 0)
{ dbgPipe = true; goto next_arg; }
if (strcmp (arg, "--debug=globals") == 0)
{ dbgGlobals = true; goto next_arg; }
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("Can't understand \"%s\"\n", arg);
// (undocumented) <chromosome>:<length> or <chromosome>:<start>:<end>
// .. in this case <start> and <end> are origin-zero half-open,
// .. regardless of any user setting
argScan = strchr(arg,':');
if (argScan == NULL) goto no_length;
argScan2 = strchr(argScan+1,':');
if (argScan2 == NULL)
{
chromStart = 0;
*(argScan++) = 0;
chromLength = string_to_u32 (argScan);
}
else
{
*(argScan++) = 0;
*(argScan2++) = 0;
chromStart = string_to_u32 (argScan);
chromLength = string_to_u32 (argScan2) - chromStart;
}
if (!add_chromosome_spec (arg, chromStart, chromLength))
chastise ("can't specify %s more than once\n", arg);
next_arg:
argv++; argc--;
continue;
}
//////////
// read chromosome lengths
//////////
if (chromsFilename != NULL)
read_chromosome_lengths (chromsFilename);
//////////
// sanity checks
//////////
// make sure we got at least one chromosome-of-interest
if (chromsOfInterest == NULL)
chastise ("gotta give me some chromosome names\n");
return;
//////////
// failure exits
//////////
help_for_unknown:
fprintf (stderr, "\"%s\" is not a known operation\n",
argVal);
exit(EXIT_FAILURE);
no_length:
fprintf (stderr, "\"%s\" contains no chromosome length\n"
"(expected \"chromosome:length\" or \"chromosome:start:end\")\n",
arg);
exit (EXIT_FAILURE);
}
// process_operator_options--
static int process_operator_options (int _argc, char** _argv)
{
int argc = _argc;
char** argv = _argv;
char* arg;
int argsConsumed, dspArgC, argIx;
char* dspName;
dspinfo* opInfo, *realOpInfo;
u32 dspIx;
dspop* op;
argv = _argv; argc = _argc;
// process the operator name
arg = argv[0];
argsConsumed = 0;
dspArgC = 0;
if ((arg[0] == specialPipeChar) && (arg[1] == 0))
{
argv++;
argc--; // (this will never go to zero)
argsConsumed++;
dspName = argv[0];
}
else
dspName = skip_whitespace (arg+1);
if (dbgPipe)
fprintf (stderr, " dspName=\"%s\"\n", dspName);
opInfo = realOpInfo = NULL;
for (dspIx=0 ; dspIx<dspTableLen ; dspIx++)
{
if (dspTable[dspIx].funcShort != NULL) realOpInfo = &dspTable[dspIx];
if (strcmp (dspName,dspTable[dspIx].name) != 0) continue;
opInfo = realOpInfo;
break;
}
if (opInfo == NULL)
chastise ("\"%s\" is not a known operation\n", dspName);
argv++; argc--; argsConsumed++; // skip operator name
// scan ahead to locate the terminating arg (next arg that starts with the
// internal pipe character, or end of arg list)
for (argIx=0 ; argIx<argc ; argIx++)
{
arg = argv[argIx];
if (arg[0] == specialPipeChar) break;
argsConsumed++;
}
dspArgC = argIx;
if (dbgPipe)
{
fprintf (stderr, " args:");
for (argIx=0 ; argIx<argc ; argIx++)
fprintf (stderr, " %s", argv[argIx]);
fprintf (stderr, "\n");
}
// tell operator to parse its arguments
chastiseUsage = opInfo->funcUsage;
chastiseUsageName = opInfo->name;
op = (*opInfo->funcParse) (opInfo->name, dspArgC, argv);
chastiseUsage = NULL;
chastiseUsageName = NULL;
op->name = copy_string (opInfo->name);
op->funcApply = opInfo->funcApply;
op->funcFree = opInfo->funcFree;
// op->atRandom must be set by the parse function
// add operation item to the pipeline
if (tailOp == NULL) pipeline = op;
else tailOp->next = op;
op->next = NULL;
tailOp = op;
if (dbgPipe)
fprintf (stderr, " argsConsumed=%d\n", argsConsumed);
return argsConsumed;
}
// read_chromosome_lengths--
static void read_chromosome_lengths (char* filename)
{
FILE* f;
char lineBuffer[1001];
u32 lineNumber = 0;
int missingEol = false;
int lineLen;
char* scan, *mark, *field;
char* chrom;
u32 chromLength;
f = fopen (filename, "rt");
if (f == NULL) goto cant_open_file;
lineNumber = 0;
while (true)
{
if (fgets (lineBuffer, sizeof(lineBuffer), f) == NULL)
break;
lineNumber++;
// check for lines getting split by fgets (the final line in the file
// might not have a newline, but no internal lines can be that way)
if (missingEol) goto missing_eol;
lineLen = strlen(lineBuffer);
if (lineLen != 0)
missingEol = (lineBuffer[lineLen-1] != '\n');
// parse the line
scan = skip_whitespace(lineBuffer);
if (*scan == 0) continue; // empty line
if (*scan == '#') continue; // comment line
chrom = scan = lineBuffer;
if (*scan == 0) goto no_chrom;
mark = skip_darkspace(scan);
scan = skip_whitespace(mark);
if (*mark != 0) *mark = 0;
if (*scan == 0) goto no_length;
field = scan;
mark = skip_darkspace(scan);
scan = skip_whitespace(mark);
if (*mark != 0) *mark = 0;
chromLength = string_to_u32 (field);
if (!add_chromosome_spec (chrom, 0, chromLength))
goto same_chromosome;
}
// success
fclose (f);
return;
//////////
// failure exits
//////////
cant_open_file:
fprintf (stderr, "can't open \"%s\" for reading\n", filename);
exit (EXIT_FAILURE);
missing_eol:
fprintf (stderr, "problem at line %u, line is longer than internal buffer\n",
lineNumber-1);
exit (EXIT_FAILURE);
no_chrom:
fprintf (stderr, "problem at line %u, line contains no chromosome or begins with whitespace\n",
lineNumber);
exit (EXIT_FAILURE);
no_length:
fprintf (stderr, "problem at line %u, line contains no chromosome length\n",
lineNumber);
exit (EXIT_FAILURE);
same_chromosome:
fprintf (stderr, "problem at line %u, chromosome \"%s\" appears more than once\n",
lineNumber, chrom);
exit (EXIT_FAILURE);
}
//----------
//
// main program--
//
//----------
int main
(int argc,
char** argv)
{
char* chrom;
u32 vLen;
valtype* v;
spec* chromSpec;
dspop* firstOp, *stopOp, *op, *nextOp;
u32 maxLength;
u32 ix, chromIx;
opfunc_free funcFree;
init_named_globals ();
set_named_global ("valColumn", (valtype) valColumn);
set_named_global ("valPrecision", (valtype) valPrecision);
set_named_global ("collapseRuns", (valtype) collapseRuns);
set_named_global ("showUncovered", (valtype) showUncovered);
set_named_global ("originOne", (valtype) originOne);
parse_options (argc, argv);
//////////
// allocate vectors
//////////
sort_chromosomes_by_length ();
// determine the length of any scratch vectors; every scratch vector will
// be as long as the longest chromosome
maxLength = 0;
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
if (chromSpec->length == 0) goto no_length_specified;
if (chromSpec->length > maxLength) maxLength = chromSpec->length;
}
init_scratch_vectors (maxLength);
// allocate chromosome value vectors
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
if (trackOperations)
tracking_report ("allocate(%s / %s bytes)\n",
chromSpec->chrom, ucommatize(chromSpec->length));
chromSpec->valVector = (valtype*) calloc (chromSpec->length, sizeof(valtype));
if (chromSpec->valVector == NULL) goto cant_allocate_val;
v = chromSpec->valVector;
for (ix=0 ; ix<chromSpec->length ; ix++)
v[ix] = 0.0;
}
if (trackOperations)
tracking_report ("allocate(--done--)\n");
//////////
// process intervals
//////////
// read intervals and values; note that if the first operator is an
// input operator, we avoid this step (since the first operator will
// overwrite whatever intervals we might have read)
op = pipeline;
if ((op == NULL) || (strcmp (op->name, "input") != 0))
read_intervals (stdin, valColumn, originOne, ri_overlapSum, /*clear*/ false, 0.0);
// perform operations; when possible, we perform a series of operations on
// one chromosome before moving onto the next; it is expected that this
// will have some improvement in cache performance, at least for chromosomes
// smaller than the cache
firstOp = pipeline;
while (firstOp != NULL)
{
for (stopOp=firstOp ; stopOp!=NULL ; stopOp=stopOp->next)
{ if (stopOp->atRandom) break; }
if (stopOp != firstOp)
{
// run several operations serially on each chromosome
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
for (op=firstOp ; op!=stopOp ; op=op->next)
{
chrom = chromSpec->chrom;
vLen = chromSpec->length;
v = chromSpec->valVector;
if (trackOperations)
fprintf (stderr, "%s(%s)\n", op->name, chrom);
(*op->funcApply) (op, chrom, vLen, v);
}
}
}
if (stopOp == NULL)
firstOp = NULL;
else
{
// run one operation on all chromosomes 'simultaneously'
op = stopOp;
if (trackOperations)
tracking_report ("%s(*)\n", op->name);
(*op->funcApply) (op, "*", maxLength, NULL);
firstOp = stopOp->next;
}
}
// report intervals
if (!inhibitOutput)
report_intervals (stdout, valPrecision, noOutputValues,
collapseRuns, showUncovered, originOne);
//////////
// success
//////////
// deallocate
free_scratch_vectors ();
free_named_globals ();
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
if (chromSpec->chrom != NULL) free (chromSpec->chrom);
if (chromSpec->valVector != NULL) free (chromSpec->valVector);
free (chromSpec);
}
chromsOfInterest = NULL;
free (chromsSorted);
chromsSorted = NULL;
for (op=pipeline ; op!=NULL ; op=nextOp)
{
nextOp = op->next;
if (op->name != NULL) { free (op->name); op->name = NULL; }
funcFree = op->funcFree;
funcFree (op);
}
// proclaim success
return EXIT_SUCCESS;
//////////
// failure exits
//////////
no_length_specified:
fprintf (stderr, "no length was specified for %s\n",
chromSpec->chrom);
return EXIT_FAILURE;
cant_allocate_val:
fprintf (stderr, "failed to allocate %d-base vector for %s, %d bytes per base\n",
chromSpec->length, chromSpec->chrom, (int) sizeof(valtype));
return EXIT_FAILURE;
}
//----------
//
// add_chromosome_spec--
// Add a chromosome to our list of chromosome specifications.
//
//----------
//
// Arguments:
// char* name: The name of the chromsome (e.g. "chr1").