-
Notifications
You must be signed in to change notification settings - Fork 1
/
minmax.c
2294 lines (1764 loc) · 58.4 KB
/
minmax.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
// minmax.c-- genodsp operators dealing with minima and maxima
#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"
#include "genodsp_interface.h"
#include "minmax.h"
// miscellany
#define min_of(a,b) ((a <= b)? a: b)
#define noIndex ((u32) -1)
//----------
// [[-- a dsp operation function group, operating on the whole genome --]]
//
// See genodsp_interface.h, "headers for dsp operator function groups" for
// function descriptions and argument details.
//
//----------
//
// op_min_in_interval--
// Find the minimum value(s) in each of a set of intervals.
//
// The intervals are replaced by infinity everywhere except at their single
// trough position. Ties are broken by the position nearest the center of the
// interval. Any bases outside the intervals are also replaced by zeros.
//
//----------
// private dspop subtype
typedef struct dspop_minover
{
dspop common; // common elements shared with all operators
char* filename;
int originOne;
valtype infinityVal;
int debug;
} dspop_minover;
// op_min_in_interval_short--
void op_min_in_interval_short (char* name, int nameWidth, FILE* f, char* indent)
{
int nameFill = nameWidth-2 - strlen(name);
if (indent == NULL) indent = "";
if (nameFill > 0) fprintf (f, "%s%s:%*s", indent, name, nameFill+1, " ");
else fprintf (f, "%s%s: ", indent, name);
fprintf (f, "find the minimum value in each of a set of intervals\n");
}
// op_min_in_interval_usage--
void op_min_in_interval_usage (char* name, FILE* f, char* indent)
{
if (indent == NULL) indent = "";
// 3456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (f, "%sFind the minimum value in each of a set of intervals. The intervals are\n", indent);
fprintf (f, "%sreplaced by infinity everywhere except at their single trough position. Ties\n", indent);
fprintf (f, "%sare broken by the position nearest the center of the interval. Any bases\n", indent);
fprintf (f, "%soutside the intervals are also replaced by infinity.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%susage: %s <filename> [options]\n", indent, name);
fprintf (f, "%s --origin=one input intervals are origin-one, closed\n", indent);
fprintf (f, "%s --origin=zero input intervals are origin-zero, half-open\n", indent);
fprintf (f, "%s --infinity=<value> value to fill non-minima\n", indent);
fprintf (f, "%s (default is +inf)\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sThe input intervals are required to be sorted along each chromosome, and\n", indent);
fprintf (f, "%snon-overlapping)\n", indent);
}
// op_min_in_interval_parse--
dspop* op_min_in_interval_parse (char* name, int _argc, char** _argv)
{
dspop_minover* op;
int argc = _argc;
char** argv = _argv;
char* arg, *argVal;
valtype tempVal;
// allocate and initialize our control record
op = (dspop_minover*) malloc (sizeof(dspop_minover));
if (op == NULL) goto cant_allocate;
op->common.atRandom = true;
op->filename = NULL;
op->originOne = (int) get_named_global ("originOne", false);
op->infinityVal = valtypeMax;
op->debug = false;
// parse arguments
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// --origin=one, --origin=zero
if ((strcmp (arg, "--origin=one") == 0)
|| (strcmp (arg, "--origin=1") == 0))
{ op->originOne = true; goto next_arg; }
if ((strcmp (arg, "--origin=zero") == 0)
|| (strcmp (arg, "--origin=0") == 0))
{ op->originOne = false; goto next_arg; }
// --infinity=<value>
if (strcmp_prefix (arg, "--infinity=") == 0)
{
tempVal = string_to_valtype (argVal);
op->infinityVal = tempVal;
goto next_arg;
}
// --debug arguments
if (strcmp (arg, "--debug") == 0)
{ op->debug = true; goto next_arg; }
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
// <filename>
if (op->filename == NULL)
{
op->filename = copy_string (arg);
goto next_arg;
}
// unknown argument
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
next_arg:
argv++; argc--;
continue;
}
if (op->filename == NULL) goto filename_missing;
return (dspop*) op;
cant_allocate:
fprintf (stderr, "[%s] failed to allocate control record (%d bytes)\n",
name, (int) sizeof(dspop_minover));
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
filename_missing:
fprintf (stderr, "[%s] no filename was provided\n",
name);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
}
// op_min_in_interval_free--
// op_min_in_interval_free--
void op_min_in_interval_free (dspop* _op)
{
dspop_minover* op = (dspop_minover*) _op;
if (op->filename != NULL) free (op->filename);
free (op);
}
// op_min_in_interval_apply--
void op_min_in_interval_apply
(arg_dont_complain(dspop* _op),
arg_dont_complain(char* vName),
arg_dont_complain(u32 vLen),
arg_dont_complain(valtype* _v))
{
dspop_minover* op = (dspop_minover*) _op;
char* filename = op->filename;
valtype infinityVal = op->infinityVal;
FILE* f;
char lineBuffer[1001];
char prevChrom[1001];
valtype* v = NULL;
char* chrom;
spec* chromSpec;
u32 start, end, o, prevEnd, adjStart, adjEnd;
valtype minVal, val;
u32 ix, chromIx, minIx, inset, maxInset;
int ok;
f = fopen (filename, "rt");
if (f == NULL) goto cant_open_file;
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
chromSpec->flag = false;
}
// read intervals and values and find the minima
if (op->originOne) o = 1;
else o = 0;
prevChrom[0] = 0;
chromSpec = NULL;
prevEnd = 0;
v = NULL;
while (true)
{
ok = read_interval (f, lineBuffer, sizeof(lineBuffer), -1,
&chrom, &start, &end, &val);
if (!ok) break;
if (val == 0.0) continue; // treat zero as a missing interval
// if this interval is on a different chromosome, finish the previous
// chromosome and then switch to this one
if (strcmp (chrom, prevChrom) != 0)
{
// clear any gap at the end of the previous chromosome
if (v != NULL)
{
if ((op->debug) && (prevEnd < chromSpec->length))
fprintf (stderr, "zeroing %s %u..%u\n", prevChrom, prevEnd, chromSpec->length-1);
for (ix=prevEnd ; ix<chromSpec->length ; ix++)
v[ix] = infinityVal;
}
v = NULL;
chromSpec = find_chromosome_spec (chrom);
if (chromSpec != NULL)
{
v = chromSpec->valVector;
prevEnd = 0;
if (chromSpec->flag) goto chrom_not_together;
}
safe_strncpy (prevChrom, chrom, sizeof(prevChrom)-1);
}
// if the current chromosome is not of any interest, ignore this
// interval
if (chromSpec == NULL) continue;
// if this is the first interval on this chromosome, 'mark' the
// chromosome
if (!chromSpec->flag)
{
if (trackOperations) fprintf (stderr, "%s(%s)\n", op->common.name, chrom);
chromSpec->flag = true;
}
// validate the interval, and (if necessary) shift it onto the vector
start -= o;
adjStart = start;
adjEnd = end;
if (chromSpec->start == 0)
{
// if only length has been specified, we *reject* intervals beyond
// the end
if (end > chromSpec->length) goto chrom_too_short;
}
else
{
// if start and end have been specified, we *ignore* intervals, or
// portions of intervals, beyond the end
if (end <= chromSpec->start) continue;
adjEnd = end - chromSpec->start;
if (start <= chromSpec->start) adjStart = 0;
else adjStart = start - chromSpec->start;
if (adjStart >= chromSpec->length) continue;
if (adjEnd >= chromSpec->length) adjEnd = chromSpec->length;
}
// make sure intervals are in sorted order along the chromosome
if (adjStart < prevEnd)
goto intervals_out_of_order;
// clear any gap before this interval
if ((op->debug) && (prevEnd < adjStart))
fprintf (stderr, "zeroing %s %u..%u\n", chrom, prevEnd, adjStart-1);
for (ix=prevEnd ; ix<adjStart ; ix++)
v[ix] = infinityVal;
// find the minimum over this interval
minVal = v[adjStart];
minIx = adjStart;
maxInset = 0;
for (ix=adjStart+1 ; ix<adjEnd ; ix++)
{
if (v[ix] > minVal) continue;
if (v[ix] < minVal)
{
minVal = v[ix];
minIx = ix;
maxInset = min_of (minIx-adjStart,adjEnd-minIx);
continue;
}
// we have a tie, which min is closest to center?
inset = min_of (ix-adjStart,adjEnd-ix);
if (inset > maxInset)
{
minIx = ix;
maxInset = inset;
}
}
if (op->debug)
fprintf (stderr, "minimum over %s %u..%u is " valtypeFmt " at %u\n",
chrom, adjStart, adjEnd-1, minVal, minIx);
// clear the interval, except for the min
for (ix=adjStart ; ix<adjEnd ; ix++)
{ if (ix != minIx) v[ix] = infinityVal; }
prevEnd = adjEnd;
}
// clear any gap at the end of the last chromosome observed
if (v != NULL)
{
if ((op->debug) && (prevEnd < chromSpec->length))
fprintf (stderr, "zeroing %s %u..%u\n", prevChrom, prevEnd, chromSpec->length-1);
for (ix=prevEnd ; ix<chromSpec->length ; ix++)
v[ix] = infinityVal;
}
// clear any chromosomes that weren't observed in the incoming set
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
if (chromSpec->flag) continue;
chrom = chromSpec->chrom;
v = chromSpec->valVector;
start = 0;
end = chromSpec->length;
if (trackOperations)
fprintf (stderr, "%s(%s,absent)\n", op->common.name, chrom);
if (op->debug)
fprintf (stderr, "zeroing (all of) %s %u..%u\n",
chrom, chromSpec->start+start, chromSpec->start+end-1);
for (ix=start ; ix<end ; ix++)
v[ix] = infinityVal;
}
// success
fclose (f);
return;
//////////
// failure exits
//////////
cant_open_file:
fprintf (stderr, "[%s] can't open \"%s\" for reading\n",
op->common.name, filename);
exit (EXIT_FAILURE);
chrom_too_short:
fprintf (stderr, "[%s] in \"%s\", %s %d %d is beyond the end of the chromosome (L=%d)\n",
op->common.name, filename, chrom, start, end, chromSpec->length);
exit (EXIT_FAILURE);
chrom_not_together:
fprintf (stderr, "[%s] in \"%s\", not all intervals on %s are together (%d..%d begins new group)\n",
op->common.name, filename, chrom, start, end);
exit (EXIT_FAILURE);
intervals_out_of_order:
fprintf (stderr, "[%s] in \"%s\", intervals on %s are not sorted (%d..%d after %d)\n",
op->common.name, filename, chrom, start, end, chromSpec->start+prevEnd);
exit (EXIT_FAILURE);
}
//----------
// [[-- a dsp operation function group, operating on the whole genome --]]
//
// See genodsp_interface.h, "headers for dsp operator function groups" for
// function descriptions and argument details.
//
//----------
//
// op_max_in_interval--
// Find the maximum value(s) in each of a set of intervals.
//
// The intervals are replaced by zeros everywhere except at their single peak
// position. Ties are broken by the position nearest the center of the
// interval. Any bases outside the intervals are also replaced by zeros.
//
//----------
// private dspop subtype
typedef struct dspop_maxover
{
dspop common; // common elements shared with all operators
char* filename;
int originOne;
valtype zeroVal;
int debug;
} dspop_maxover;
// op_max_in_interval_short--
void op_max_in_interval_short (char* name, int nameWidth, FILE* f, char* indent)
{
int nameFill = nameWidth-2 - strlen(name);
if (indent == NULL) indent = "";
if (nameFill > 0) fprintf (f, "%s%s:%*s", indent, name, nameFill+1, " ");
else fprintf (f, "%s%s: ", indent, name);
fprintf (f, "find the maximum value in each of a set of intervals\n");
}
// op_max_in_interval_usage--
void op_max_in_interval_usage (char* name, FILE* f, char* indent)
{
if (indent == NULL) indent = "";
// 3456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (f, "%sFind the maximum value in each of a set of intervals. The intervals are\n", indent);
fprintf (f, "%sreplaced by zeros everywhere except at their single peak position. Ties are\n", indent);
fprintf (f, "%sbroken by the position nearest the center of the interval. Any bases\n", indent);
fprintf (f, "%soutside the intervals are also replaced by zeros.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%susage: %s <filename> [options]\n", indent, name);
fprintf (f, "%s --origin=one input intervals are origin-one, closed\n", indent);
fprintf (f, "%s --origin=zero input intervals are origin-zero, half-open\n", indent);
fprintf (f, "%s --zero=<value> (Z=) zero value to fill non-maxima\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sThe input intervals are required to be sorted along each chromosome, and\n", indent);
fprintf (f, "%snon-overlapping)\n", indent);
}
// op_max_in_interval_parse--
dspop* op_max_in_interval_parse (char* name, int _argc, char** _argv)
{
dspop_maxover* op;
int argc = _argc;
char** argv = _argv;
char* arg, *argVal;
valtype tempVal;
// allocate and initialize our control record
op = (dspop_maxover*) malloc (sizeof(dspop_maxover));
if (op == NULL) goto cant_allocate;
op->common.atRandom = true;
op->filename = NULL;
op->originOne = (int) get_named_global ("originOne", false);
op->zeroVal = 0.0;
op->debug = false;
// parse arguments
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// --origin=one, --origin=zero
if ((strcmp (arg, "--origin=one") == 0)
|| (strcmp (arg, "--origin=1") == 0))
{ op->originOne = true; goto next_arg; }
if ((strcmp (arg, "--origin=zero") == 0)
|| (strcmp (arg, "--origin=0") == 0))
{ op->originOne = false; goto next_arg; }
// --zero=<value> or Z=<value>
if ((strcmp_prefix (arg, "--zero=") == 0)
|| (strcmp_prefix (arg, "Z=") == 0)
|| (strcmp_prefix (arg, "--Z=") == 0))
{
tempVal = string_to_valtype (argVal);
op->zeroVal = tempVal;
goto next_arg;
}
// --debug arguments
if (strcmp (arg, "--debug") == 0)
{ op->debug = true; goto next_arg; }
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
// <filename>
if (op->filename == NULL)
{
op->filename = copy_string (arg);
goto next_arg;
}
// unknown argument
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
next_arg:
argv++; argc--;
continue;
}
if (op->filename == NULL) goto filename_missing;
return (dspop*) op;
cant_allocate:
fprintf (stderr, "[%s] failed to allocate control record (%d bytes)\n",
name, (int) sizeof(dspop_maxover));
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
filename_missing:
fprintf (stderr, "[%s] no filename was provided\n",
name);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
}
// op_max_in_interval_free--
// op_max_in_interval_free--
void op_max_in_interval_free (dspop* _op)
{
dspop_maxover* op = (dspop_maxover*) _op;
if (op->filename != NULL) free (op->filename);
free (op);
}
// op_max_in_interval_apply--
void op_max_in_interval_apply
(arg_dont_complain(dspop* _op),
arg_dont_complain(char* vName),
arg_dont_complain(u32 vLen),
arg_dont_complain(valtype* _v))
{
dspop_maxover* op = (dspop_maxover*) _op;
char* filename = op->filename;
valtype zeroVal = op->zeroVal;
FILE* f;
char lineBuffer[1001];
char prevChrom[1001];
valtype* v = NULL;
char* chrom;
spec* chromSpec;
u32 start, end, o, prevEnd, adjStart, adjEnd;
valtype maxVal, val;
u32 ix, chromIx, maxIx, inset, maxInset;
int ok;
f = fopen (filename, "rt");
if (f == NULL) goto cant_open_file;
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
chromSpec->flag = false;
}
// read intervals and values and find the maxima
if (op->originOne) o = 1;
else o = 0;
prevChrom[0] = 0;
chromSpec = NULL;
prevEnd = 0;
v = NULL;
while (true)
{
ok = read_interval (f, lineBuffer, sizeof(lineBuffer), -1,
&chrom, &start, &end, &val);
if (!ok) break;
if (val == 0.0) continue; // treat zero as a missing interval
// if this interval is on a different chromosome, finish the previous
// chromosome and then switch to this one
if (strcmp (chrom, prevChrom) != 0)
{
// clear any gap at the end of the previous chromosome
if (v != NULL)
{
if ((op->debug) && (prevEnd < chromSpec->length))
fprintf (stderr, "zeroing %s %u..%u\n", prevChrom, prevEnd, chromSpec->length-1);
for (ix=prevEnd ; ix<chromSpec->length ; ix++)
v[ix] = zeroVal;
}
v = NULL;
chromSpec = find_chromosome_spec (chrom);
if (chromSpec != NULL)
{
v = chromSpec->valVector;
prevEnd = 0;
if (chromSpec->flag) goto chrom_not_together;
}
safe_strncpy (prevChrom, chrom, sizeof(prevChrom)-1);
}
// if the current chromosome is not of any interest, ignore this
// interval
if (chromSpec == NULL) continue;
// if this is the first interval on this chromosome, 'mark' the
// chromosome
if (!chromSpec->flag)
{
if (trackOperations) fprintf (stderr, "%s(%s)\n", op->common.name, chrom);
chromSpec->flag = true;
}
// validate the interval, and (if necessary) shift it onto the vector
start -= o;
adjStart = start;
adjEnd = end;
if (chromSpec->start == 0)
{
// if only length has been specified, we *reject* intervals beyond
// the end
if (end > chromSpec->length) goto chrom_too_short;
}
else
{
// if start and end have been specified, we *ignore* intervals, or
// portions of intervals, beyond the end
if (end <= chromSpec->start) continue;
adjEnd = end - chromSpec->start;
if (start <= chromSpec->start) adjStart = 0;
else adjStart = start - chromSpec->start;
if (adjStart >= chromSpec->length) continue;
if (adjEnd >= chromSpec->length) adjEnd = chromSpec->length;
}
// make sure intervals are in sorted order along the chromosome
if (adjStart < prevEnd)
goto intervals_out_of_order;
// clear any gap before this interval
if ((op->debug) && (prevEnd < adjStart))
fprintf (stderr, "zeroing %s %u..%u\n", chrom, prevEnd, adjStart-1);
for (ix=prevEnd ; ix<adjStart ; ix++)
v[ix] = zeroVal;
// find the maximum over this interval
maxVal = v[adjStart];
maxIx = adjStart;
maxInset = 0;
for (ix=adjStart+1 ; ix<adjEnd ; ix++)
{
if (v[ix] < maxVal) continue;
if (v[ix] > maxVal)
{
maxVal = v[ix];
maxIx = ix;
maxInset = min_of (maxIx-adjStart,adjEnd-maxIx);
continue;
}
// we have a tie, which max is closest to center?
inset = min_of (ix-adjStart,adjEnd-ix);
if (inset > maxInset)
{
maxIx = ix;
maxInset = inset;
}
}
if (op->debug)
fprintf (stderr, "maximum over %s %u..%u is " valtypeFmt " at %u\n",
chrom, adjStart, adjEnd-1, maxVal, maxIx);
// clear the interval, except for the max
for (ix=adjStart ; ix<adjEnd ; ix++)
{ if (ix != maxIx) v[ix] = zeroVal; }
prevEnd = adjEnd;
}
// clear any gap at the end of the last chromosome observed
if (v != NULL)
{
if ((op->debug) && (prevEnd < chromSpec->length))
fprintf (stderr, "zeroing %s %u..%u\n", prevChrom, prevEnd, chromSpec->length-1);
for (ix=prevEnd ; ix<chromSpec->length ; ix++)
v[ix] = zeroVal;
}
// clear any chromosomes that weren't observed in the incoming set
for (chromIx=0 ; chromsSorted[chromIx]!=NULL ; chromIx++)
{
chromSpec = chromsSorted[chromIx];
if (chromSpec->flag) continue;
chrom = chromSpec->chrom;
v = chromSpec->valVector;
start = 0;
end = chromSpec->length;
if (trackOperations)
fprintf (stderr, "%s(%s,absent)\n", op->common.name, chrom);
if (op->debug)
fprintf (stderr, "zeroing (all of) %s %u..%u\n",
chrom, chromSpec->start+start, chromSpec->start+end-1);
for (ix=start ; ix<end ; ix++)
v[ix] = zeroVal;
}
// success
fclose (f);
return;
//////////
// failure exits
//////////
cant_open_file:
fprintf (stderr, "[%s] can't open \"%s\" for reading\n",
op->common.name, filename);
exit (EXIT_FAILURE);
chrom_too_short:
fprintf (stderr, "[%s] in \"%s\", %s %d %d is beyond the end of the chromosome (L=%d)\n",
op->common.name, filename, chrom, start, end, chromSpec->length);
exit (EXIT_FAILURE);
chrom_not_together:
fprintf (stderr, "[%s] in \"%s\", not all intervals on %s are together (%d..%d begins new group)\n",
op->common.name, filename, chrom, start, end);
exit (EXIT_FAILURE);
intervals_out_of_order:
fprintf (stderr, "[%s] in \"%s\", intervals on %s are not sorted (%d..%d after %d)\n",
op->common.name, filename, chrom, start, end, chromSpec->start+prevEnd);
exit (EXIT_FAILURE);
}
//----------
// [[-- a dsp operation function group, operating on a single chromosome --]]
//
// See genodsp_interface.h, "headers for dsp operator function groups" for
// function descriptions and argument details.
//
//----------
//
// op_local_minima--
// Apply a trough-finder (more of a local minima finder). Any entry equal to
// to the minimum (in the window centered at that entry) is kept. All entries
// that aren't the minimum are cleared.
//
//----------
// private dspop subtype
typedef struct dspop_localmin
{
dspop common; // common elements shared with all operators
u32 neighborhood;
valtype infinityVal;
} dspop_localmin;
// op_local_minima_short--
void op_local_minima_short (char* name, int nameWidth, FILE* f, char* indent)
{
int nameFill = nameWidth-2 - strlen(name);
if (indent == NULL) indent = "";
if (nameFill > 0) fprintf (f, "%s%s:%*s", indent, name, nameFill+1, " ");
else fprintf (f, "%s%s: ", indent, name);
fprintf (f, "find local minima\n");
}
// op_local_minima_usage--
void op_local_minima_usage (char* name, FILE* f, char* indent)
{
if (indent == NULL) indent = "";
// 3456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (f, "%sIdentify local minima. Any entry *not* equal to the minimum over the\n", indent);
fprintf (f, "%sneighborhood centered at that entry is set to infinity. Input values beyond\n", indent);
fprintf (f, "%sthe ends of the vector are not considered.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%susage: %s [options]\n", indent, name);
fprintf (f, "%s --neighborhood=<length> (N=) size of neighborhood\n", indent);
fprintf (f, "%s (default is 3)\n", indent);
fprintf (f, "%s (if this is not odd, it will be increased by 1)\n", indent);
fprintf (f, "%s --infinity=<value> value to fill non-minima\n", indent);
fprintf (f, "%s (default is +inf)\n", indent);
}
// op_local_minima_parse--
dspop* op_local_minima_parse (char* name, int _argc, char** _argv)
{
dspop_localmin* op;
int argc = _argc;
char** argv = _argv;
char* arg, *argVal;
int tempInt;
valtype tempVal;
// allocate and initialize our control record
op = (dspop_localmin*) malloc (sizeof(dspop_localmin));
if (op == NULL) goto cant_allocate;
op->common.atRandom = false;
op->neighborhood = 3;
op->infinityVal = valtypeMax;
// parse arguments
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// --neighborhood=<length> or N=<length>
if ((strcmp_prefix (arg, "--neighborhood=") == 0)
|| (strcmp_prefix (arg, "N=") == 0)
|| (strcmp_prefix (arg, "--N=") == 0))
{
tempInt = string_to_unitized_int (argVal, /*thousands*/ true);
if (tempInt == 0)
chastise ("[%s] neighborhood can't be zero (\"%s\")\n", name, arg);
if (tempInt < 0)
chastise ("[%s] neighborhood can't be negative (\"%s\")\n", name, arg);
if (tempInt < 3)
{
fprintf (stderr, "[%s] WARNING: raising neighborhood from %d to %d\n",
name, tempInt, 3);
tempInt = 3;
}
if ((tempInt & 1) == 0)
{
fprintf (stderr, "[%s] WARNING: raising neighborhood from %d to %d\n",
name, tempInt, tempInt+1);
tempInt++;
}
op->neighborhood = (u32) tempInt;
goto next_arg;
}
// --infinity=<value>
if (strcmp_prefix (arg, "--infinity=") == 0)
{
tempVal = string_to_valtype (argVal);
op->infinityVal = tempVal;
goto next_arg;
}
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
// unknown argument
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
next_arg:
argv++; argc--;
continue;
}
return (dspop*) op;
cant_allocate:
fprintf (stderr, "[%s] failed to allocate control record (%d bytes)\n",
name, (int) sizeof(dspop_localmin));
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
}
// op_local_minima_free--
void op_local_minima_free (dspop* op)
{
free (op);
}
// op_local_minima_apply--
void op_local_minima_apply
(arg_dont_complain(dspop* _op),
arg_dont_complain(char* vName),
arg_dont_complain(u32 vLen),
arg_dont_complain(valtype* v))
{
dspop_localmin* op = (dspop_localmin*) _op;
u32 neighborhood = op->neighborhood;
valtype infinityVal = op->infinityVal;
valtype* s = get_scratch_vector();
valtype val;
u32 hOff, ix, wIx, wStart, wEnd;
// find the local minima
hOff = (neighborhood - 1) / 2;
for (ix=0 ; ix<vLen ; ix++)
{
if (ix < hOff) wStart = 0;