-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbwtool.c
More file actions
319 lines (307 loc) · 10.1 KB
/
bwtool.c
File metadata and controls
319 lines (307 loc) · 10.1 KB
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
/* bwtool - a bigWig manipulation tool */
#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"
#define NANUM sqrt(-1)
void usage()
/* Explain usage and exit. */
{
errAbort(
PACKAGE_STRING " - Data operations on bigWig files\n"
"usage:\n"
" bwtool command [additional command parameters]\n"
"commands:\n"
" aggregate (or \"agg\") produce plot data as an average of values around\n"
" the regions specified in a bed file\n"
" chromgraph roughly convert to the chromgraph format, suitable for UCSC's\n"
" Genome Graphs page\n"
" distribution (or \"dist\") produce plot data as the frequency of values seen\n"
" in the bigWig\n"
" extract (or \"ex\") extract data in some other ways than paste, matrix, or\n"
" window with a given bed, preserving strand directionality.\n"
" fill fill in regions of genome where no data exists with a value\n"
" find find regions of bigWig with given properties\n"
" lift project data from one genome assembly to another using a\n"
" liftOver file (can be lossy)\n"
" matrix extract same-sized sections from bigWig to examine as a matrix\n"
" paste output data from multiple bigWigs and align them one per column\n"
" in tab-delimited output meant to feed into computations\n"
/* #ifdef HAVE_LIBGSL */
/* " random print out random data or random regions from the bigWig file\n" */
/* #endif */
" remove remove data equal to or thresholded on a given value\n"
" or remove data using ranges specified in a bed file\n"
" roll compute rolling means, etc\n"
" sax run symbolic aggregate approximation (SAX) algorithm on data\n"
" shift move data on the chromosome\n"
/* " split make a set of files describing evenly-sized regions of the bigWig,\n" */
/* " each of which may be used separately on a cluster and combined later\n" */
" summary provide some summary stats for each region in a bed file\n"
" window print out tiling windows of data in comma-separated lists\n\n"
"general options:\n"
" -wigtype=<bg|fix|var> output bedGraph, fixedStep, or variableStep wig\n"
" -wig-only for bigWig-creating programs, make a wig instead\n"
" -regions=bed use specific regions\n"
" -condense condense output, particularly bedGraphs\n"
" -decimals=n output specified number of decimals (default 2)\n"
" -fill=val some programs allow filling missing parts of the bigWig\n"
" with a specified value prior to using data.\n"
" -pseudo=val add a pseudo-count at every value\n"
" -o=output.txt where normally standard output is written, write to a\n"
" file instead.\n"
" -tmp-dir=dir by default, bigWig caching is done in /tmp/udcCache/*.\n"
" Override this by setting dir to the desired path.\n"
);
}
enum bw_op_type get_bw_op_type(char *thresh_type, boolean inverse)
/* speed things up inside loop. we don't want a string comparison for every datapoint */
/* to know what kind of operation to perform. */
{
if (thresh_type)
{
if (sameWord(thresh_type, "less"))
{
if (inverse)
return more_equal;
return less;
}
else if (sameWord(thresh_type, "less-equal"))
{
if (inverse)
return more;
return less_equal;
}
else if (sameWord(thresh_type, "more"))
{
if (inverse)
return less_equal;
return more;
}
else if (sameWord(thresh_type, "more-equal"))
{
if (inverse)
return less;
return more_equal;
}
else if (sameWord(thresh_type, "equal"))
{
if (inverse)
return not_equal;
return equal;
}
else if (sameWord(thresh_type, "not-equal"))
{
if (inverse)
return equal;
return not_equal;
}
else if (sameWord(thresh_type, "mask"))
return mask;
}
return invalid;
}
int main(int argc, char *argv[])
/* Process command line. */
{
/* Display help if no arguments are given. */
if (argc == 1)
{
usage();
}
bool version_cmd = sameString(argv[1], "--version") || sameString(argv[1], "-V");//argv containing '-' will be removed by the next function
struct hash *options = optionParseIntoHashExceptNumbers(&argc, argv, FALSE);
/* common options */
char *tmp_dir = (char *)hashOptionalVal(options, "tmp-dir", NULL);
char *regions = (char *)hashOptionalVal(options, "regions", NULL);
char *output_file = (char *)hashOptionalVal(options, "o", NULL);
char *favorites = (char *)hashOptionalVal(options, "favorites", NULL);
unsigned decimals = sqlUnsigned((char *)hashOptionalVal(options, "decimals", "2"));
enum wigOutType wot = get_wig_out_type((char *)hashOptionalVal(options, "wigtype", "fix"));
boolean condense = (hashFindVal(options, "condense") != NULL) ? TRUE : FALSE;
boolean wig_only = (hashFindVal(options, "wig-only") != NULL) ? TRUE : FALSE;
char *fill_s = hashFindVal(options, "fill");
double na = NANUM;
double fill = na;
if (fill_s)
fill = sqlDouble(hashFindVal(options, "fill"));
if (argc == 1 && !version_cmd) {
usage();
}
if (version_cmd)
{
printf(PACKAGE_STRING "\nCopyright (C) 2015 Centre for Genomic Regulation (CRG) in Barcelona.\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n\n"
"Written by Andy Pohl.\n");
}
else if (sameString(argv[1], "remove"))
{
if (argc != 6)
usage_remove();
else
bwtool_remove(options, favorites, regions, decimals, wot, condense, wig_only, argv[2], argv[3], argv[4], tmp_dir, argv[5]);
}
else if (sameString(argv[1], "fill"))
{
if (argc != 5)
usage_fill();
else
bwtool_fill(options, favorites, regions, decimals, wot, condense, argv[2], argv[3], tmp_dir, argv[4]);
}
else if (sameString(argv[1], "shift"))
{
if (argc != 5)
usage_shift();
else
bwtool_shift(options, favorites, regions, decimals, wot, condense, argv[2], argv[3], tmp_dir, argv[4]);
}
else if (sameString(argv[1], "find"))
{
if (argc < 5)
usage_find();
else if (sameString(argv[2], "local-extrema"))
{
if (argc != 5)
usage_find();
else
bwtool_find_extrema(options, favorites, regions, decimals, fill, argv[3], tmp_dir, argv[4]);
}
else if (sameString(argv[2], "less") || sameString(argv[2], "less-equal") || sameString(argv[2], "more") ||
sameString(argv[2], "more-equal") || sameString(argv[2], "equal") || sameString(argv[2], "not"))
{
if (argc != 6)
usage_find();
else
bwtool_find_thresh(options, favorites, regions, fill, argv[2], argv[3], argv[4], tmp_dir, argv[5]);
}
else if (sameString(argv[2], "maxima"))
{
if (argc != 6)
usage_find();
else
bwtool_find_max(options, favorites, argv[3], fill, argv[4], tmp_dir, argv[5]);
}
}
else if (sameString(argv[1], "matrix"))
{
if (argc != 6)
usage_matrix();
else
bwtool_matrix(options, favorites, argv[3], decimals, fill, argv[2], argv[4], tmp_dir, argv[5]);
}
else if (sameString(argv[1], "distribution") || sameString(argv[1], "dist"))
{
if (argc != 4)
usage_distrib();
else
bwtool_distrib(options, favorites, regions, decimals, argv[2], tmp_dir, argv[3]);
}
/* #ifdef HAVE_LIBGSL */
/* else if (sameString(argv[1], "random")) */
/* { */
/* if (argc != 6) */
/* usage_random(); */
/* else */
/* bwtool_random(options, favorites, regions, decimals, fill, argv[2], argv[3], argv[4], argv[5]); */
/* } */
/* #endif */
else if (sameString(argv[1], "aggregate") || sameString(argv[1], "agg"))
{
decimals = sqlUnsigned((char *)hashOptionalVal(options, "decimals", "6"));
if (argc != 6)
usage_aggregate();
else
bwtool_aggregate(options, regions, decimals, fill, argv[2], argv[3], argv[4], tmp_dir, argv[5]);
}
else if (sameString(argv[1], "chromgraph") || sameString(argv[1], "cg"))
{
if (argc != 4)
usage_chromgraph();
else
bwtool_chromgraph(options, favorites, regions, decimals, fill, argv[2], tmp_dir, argv[3]);
}
else if (sameString(argv[1], "paste"))
{
if (hashFindVal(options, "wigtype") == NULL)
wot = bedGraphOut;
if (argc < 3)
usage_paste();
else
{
struct slName *list = NULL;
int i;
for (i = 2; i < argc; i++)
{
struct slName *name = slNameNew(argv[i]);
slAddHead(&list, name);
}
slReverse(&list);
bwtool_paste(options, favorites, regions, decimals, fill, wot, &list, tmp_dir, output_file);
}
}
else if (sameString(argv[1], "lift"))
{
if (argc != 5)
usage_lift();
else
bwtool_lift(options, favorites, regions, decimals, wot, argv[2], tmp_dir, argv[3], argv[4]);
}
else if (sameString(argv[1], "roll"))
{
if (argc != 6)
usage_roll();
else
bwtool_roll(options, favorites, regions, decimals, fill, wot, argv[2], argv[3], argv[4], tmp_dir, argv[5]);
}
else if (sameString(argv[1], "summary"))
{
if (argc != 5)
usage_summary();
else
bwtool_summary(options, favorites, regions, decimals, fill, argv[2], argv[3], tmp_dir, argv[4]);
}
else if (sameString(argv[1], "sax"))
{
if (argc != 5)
usage_sax();
else
bwtool_sax(options, favorites, regions, decimals, argv[2], argv[3], tmp_dir, argv[4]);
}
else if (sameString(argv[1], "split"))
{
if (argc != 5)
usage_split();
else
bwtool_split(options, regions, argv[2], argv[3], tmp_dir, argv[4]);
}
else if (sameString(argv[1], "window") || sameString(argv[1], "win"))
{
if (argc != 4)
usage_window();
else
bwtool_window(options, favorites, regions, decimals, fill, argv[2], argv[3], tmp_dir, output_file);
}
else if (sameString(argv[1], "extract") || sameString(argv[1], "ex"))
{
if (argc != 6)
usage_extract();
else
bwtool_extract(options, argv[3], decimals, fill, argv[2], argv[4], tmp_dir, argv[5]);
}
else
{
usage();
}
hashFree(&options);
return 0;
}