-
Notifications
You must be signed in to change notification settings - Fork 22
/
find.c
331 lines (320 loc) · 9.47 KB
/
find.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
/* bwtool_find - find parts of the data to */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jkweb/common.h>
#include <jkweb/linefile.h>
#include <jkweb/hash.h>
#include <jkweb/options.h>
#include <jkweb/sqlNum.h>
#include <jkweb/basicBed.h>
#include <jkweb/bigWig.h>
#include <beato/bigs.h>
#include "bwtool.h"
#include "bwtool_shared.h"
#include <jkweb/rangeTree.h>
#include <beato/extrema.h>
#include <float.h>
void usage_find()
/* Explain usage and exit. */
{
errAbort(
"bwtool find - find parts of genome that satisfy given constraints\n"
" in the bigWig data.\n"
"usage:\n"
" bwtool find <operator> [operator options] input.bw[:chr:start-end] output.bed\n"
"operators:\n"
" local-extrema output is 6-field bedGraph with +/- indicating if the extrema\n"
" is a local minimum or maximum.\n"
" -min-sep=d to avoid local clusters of extrema, define a minimum distance\n"
" they must be separated by (d)\n"
" -maxima only find local maxima\n"
" -minima only find local minima\n"
" less|less-equal|more|more-equal|equal|not <number>\n"
" maxima <regions.bed> find the highest value in each region given in a bed and output\n"
" the same bed as bed12.\n"
" -with-max output the maximum value just after the bed12.\n"
" -median-base if there are multiple tied maxima, output the median base location\n"
" instead of all of them.\n"
);
}
static enum ex_removal get_removal(struct hash *options)
/* convert -minima and -maxima options to enum */
{
boolean maxima = (hashFindVal(options, "maxima") != NULL) ? TRUE : FALSE;
boolean minima = (hashFindVal(options, "minima") != NULL) ? TRUE : FALSE;
if (maxima && minima)
errAbort("cannot specify both -maxima and -minima");
if (maxima)
return remove_min;
if (minima)
return remove_max;
return no_removal;
}
void bwtool_find_extrema(struct hash *options, char *favorites, char *regions, unsigned decimals, double fill, char *bigfile, char *tmp_dir, char *outputfile)
/* find local extrema */
{
unsigned min_sep = sqlUnsigned((char *)hashOptionalVal(options, "min-sep", "0"));
char *other_bigfile = (char *)hashOptionalVal(options, "against", NULL);
enum ex_removal rem = get_removal(options);
struct metaBig *main_big = metaBigOpen_check(bigfile, tmp_dir, regions);
struct metaBig *other_big = NULL;
struct extrema *main_list;
struct extrema *other_list = NULL;
struct extrema *ex;
unsigned shift = 0;
FILE *out;
if (other_bigfile)
{
char *num;
if (rem == no_removal)
errAbort("must specify either -maxima or -minima with -against");
if (!strchr(other_bigfile, ','))
errAbort("must specify shift limit in -against option");
num = chopPrefixAt(other_bigfile, ',');
shift = sqlUnsigned(num);
other_big = metaBigOpen_check(other_bigfile, tmp_dir, regions);
}
if (!main_big || (!other_big && other_bigfile))
errAbort("could not open bigWig file");
main_list = extrema_find(main_big, min_sep, rem);
slReverse(&main_list);
if (other_bigfile)
{
other_list = extrema_find(other_big, min_sep, rem);
extrema_find_shifts(main_list, other_list, shift);
}
metaBigClose(&main_big);
if (other_big)
metaBigClose(&other_big);
out = mustOpen(outputfile, "w");
if (other_bigfile)
for (ex = main_list; ex != NULL; ex = ex->next)
fprintf(out, "%s\t%d\t%d\t%d\t1000\t%c\n", ex->chrom, ex->chromStart, ex->chromStart+1, (int)ex->val, ex->min_or_max);
else
{
slSort(&main_list, extrema_bed_cmp);
for (ex = main_list; ex != NULL; ex = ex->next)
fprintf(out, "%s\t%d\t%d\t%0.*f\t1000\t%c\n", ex->chrom, ex->chromStart, ex->chromStart+1, decimals, ex->val, ex->min_or_max);
}
carefulClose(&out);
extrema_free_list(&main_list);
}
static boolean fit_thresh(double val, double thresh, enum bw_op_type op)
/* just a big switch depending on the operator */
{
boolean ret = FALSE;
switch (op)
{
case less:
{
if (val < thresh)
ret = TRUE;
break;
}
case less_equal:
{
if (val <= thresh)
ret = TRUE;
break;
}
case more:
{
if (val > thresh)
ret = TRUE;
break;
}
case more_equal:
{
if (val >= thresh)
ret = TRUE;
break;
}
case equal:
{
if (val == thresh)
ret = TRUE;
break;
}
case not_equal:
{
if (val != thresh)
ret = TRUE;
break;
}
case invalid:
default:
{
ret = FALSE;
}}
return ret;
}
void bwtool_find_thresh(struct hash *options, char *favorites, char *regions, double fill,
char *thresh_type, char *thresh_s, char *bigfile, char *tmp_dir, char *outputfile)
/* the other kind of finding, based on thresholding. */
{
boolean inverse = (hashFindVal(options, "inverse") != NULL) ? TRUE : FALSE;
enum bw_op_type op= get_bw_op_type(thresh_type, inverse);
struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, regions);
double thresh = sqlDouble(thresh_s);
FILE *out = mustOpen(outputfile, "w");
struct bed out_bed;
struct bed *section;
for (section = mb->sections; section != NULL; section = section->next)
{
struct perBaseWig *pbwList = perBaseWigLoadContinue(mb, section->chrom, section->chromStart,
section->chromEnd);
struct perBaseWig *pbw;
int i, len;
if (pbwList)
{
out_bed.chrom = pbwList->chrom;
for (pbw = pbwList; pbw != NULL; pbw = pbw->next)
{
i = 0;
len = pbw->chromEnd - pbw->chromStart;
out_bed.chromStart = out_bed.chromEnd = 0;
while (i < len)
{
while ((i < len) && (!fit_thresh(pbw->data[i], thresh, op)))
i++;
out_bed.chromStart = i + pbw->chromStart;
while ((i < len) && (fit_thresh(pbw->data[i], thresh, op)))
i++;
out_bed.chromEnd = i + pbw->chromStart;
if (out_bed.chromEnd > out_bed.chromStart)
bedTabOutN(&out_bed, 3, out);
}
}
perBaseWigFree(&pbwList);
}
}
metaBigClose(&mb);
carefulClose(&out);
}
static struct bed *bed12FromBed6(struct bed6 **pList)
/* Destroy bed6 list in favor of a typical bed */
{
struct bed *list = NULL;
struct bed6 *top;
while ((top = slPopHead(pList)) != NULL)
{
struct bed *uno;
AllocVar(uno);
uno->chrom = cloneString(top->chrom);
uno->chromStart = top->chromStart;
uno->chromEnd = top->chromEnd;
uno->name = cloneString(top->name);
uno->score = top->score;
uno->strand[0] = top->strand[0];
uno->strand[1] = '\0';
uno->thickStart = uno->chromStart;
uno->thickEnd = uno->chromEnd;
slAddHead(&list, uno);
bed6Free(&top);
}
freez(pList);
slReverse(&list);
return list;
}
static int median_base_calc(struct slInt **pList)
/* in a list of ints, find the median. straight-forward enough. */
{
struct slInt *list;
if (!pList || !*pList)
return -1;
int size = slCount(*pList);
list = *pList;
if (size == 1)
return list->val;
int i = 0;
struct slInt *cur;
slSort(pList, slIntCmp);
list = *pList;
if (size % 2 == 0)
{
for (cur = list, i = 0; ((cur != NULL) && (i < (size/2)-1)); cur = cur->next, i++)
;
return (cur->val + cur->next->val)/2;
}
for (cur = list, i = 0; ((cur != NULL) && (i < (size/2))); cur = cur->next, i++)
;
return cur->val;
}
void bwtool_find_max(struct hash *options, char *favorites, char *regions, double fill,
char *bigfile, char *tmp_dir, char *outputfile)
/* find max points in a range */
{
boolean med_base = (hashFindVal(options, "median-base") != NULL) ? TRUE : FALSE;
boolean with_max = (hashFindVal(options, "with-max") != NULL) ? TRUE : FALSE;
struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, NULL);
FILE *out = mustOpen(outputfile, "w");
struct bed6 *sections6 = readBed6Soft(regions);
struct bed *sections = bed12FromBed6(§ions6);
struct bed *section;
for (section = sections; section != NULL; section = section->next)
{
struct perBaseWig *pbwList = perBaseWigLoadContinue(mb, section->chrom, section->chromStart,
section->chromEnd);
struct perBaseWig *pbw;
struct slInt *ii;
int i, size;
double max = -DBL_MAX;
struct slInt *list = NULL;
for (pbw = pbwList; pbw != NULL; pbw = pbw->next)
{
int pbw_off = pbw->chromStart - section->chromStart;
for (i = 0; i < pbw->len; i++)
{
if (pbw->data[i] > max)
{
slFreeList(&list);
struct slInt *new_int = slIntNew(i + pbw_off);
slAddHead(&list, new_int);
max = pbw->data[i];
}
else if (pbw->data[i] == max)
{
struct slInt *new_int = slIntNew(i + pbw_off);
slAddHead(&list, new_int);
}
}
}
slReverse(&list);
if (list)
{
size = slCount(list);
if (med_base)
{
section->blockCount = 1;
AllocArray(section->blockSizes, sizeof(int));
AllocArray(section->chromStarts, sizeof(int));
section->blockSizes[0] = 1;
section->chromStarts[0] = median_base_calc(&list);
}
else
{
section->blockCount = size;
AllocArray(section->blockSizes, sizeof(int) * size);
AllocArray(section->chromStarts, sizeof(int) * size);
for (i = 0, ii = list; (i < size) && (ii != NULL); i++, ii = ii->next)
{
section->blockSizes[i] = 1;
section->chromStarts[i] = ii->val;
}
}
if (!with_max)
bedTabOutN(section, 12, out);
else
{
bedOutputN(section, 12, out, '\t', '\t');
fprintf(out, "%f\n", max);
}
slFreeList(&list);
}
perBaseWigFree(&pbwList);
}
metaBigClose(&mb);
bedFreeList(§ions);
carefulClose(&out);
}