/
remove.c
203 lines (194 loc) · 5.67 KB
/
remove.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
/* bwtool_remove - code removing specified parts of a bigWig */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jkweb/common.h>
#include <jkweb/linefile.h>
#include <jkweb/hash.h>
#include <jkweb/rangeTree.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"
#define NANUM sqrt(-1)
void usage_remove()
/* Explain usage and exit. */
{
errAbort(
"bwtool - Data operations on bigWig files\n"
"usage:\n"
" bwtool remove <operator> <value|mask.bed> input.bw[:chr:start-end] output.bw\n"
"where:\n"
" operator is one of the following: \"less\", \"more\", or \"mask\". In the case\n"
" of \"mask\", the next parameter is a bed file containing the regions to remove.\n"
" If the operator is not \"mask\", it is a typical binary operator for a thresholding\n"
" operation using the value parameter.\n"
"options:\n"
" -inverse remove the data NOT specified in the operation\n"
);
}
static struct hash *load_range_tree( char *range_file)
/* load the masking ranges into the tree*/
{
struct hash *rt_hash = hashNew(0);
struct bed *beds = bedLoadNAll(range_file, 3);
struct bed *bed;
for (bed = beds; bed != NULL; bed = bed->next)
{
boolean new_chrom = FALSE;
struct rbTree *chrom_range = hashFindVal(rt_hash, bed->chrom);
if (!chrom_range)
{
new_chrom = TRUE;
chrom_range = rangeTreeNew();
}
rangeTreeAdd(chrom_range, bed->chromStart, bed->chromEnd);
if (new_chrom)
hashAdd(rt_hash, bed->chrom, chrom_range);
}
bedFreeList(&beds);
return rt_hash;
}
static void bwtool_remove_thresh(struct metaBig *mb, enum bw_op_type op, char *val_s,
char *outputfile, enum wigOutType wot, unsigned decimals,
boolean condense, boolean wig_only)
/* deal with the thresholding type of removal. */
{
char wigfile[512];
safef(wigfile, sizeof(wigfile), "%s.tmp.wig", outputfile);
FILE *out = mustOpen(wigfile, "w");
double val = (double)((float)sqlDouble(val_s));
struct bed *section;
const double na = NANUM;
for (section = mb->sections; section != NULL; section = section->next)
{
struct perBaseWig *pbwList = perBaseWigLoadContinue(mb, section->chrom, section->chromStart, section->chromEnd);
struct perBaseWig *pbw;
for (pbw = pbwList; pbw != NULL; pbw = pbw->next)
{
int size = pbw->len;
int i;
for (i = 0; i < size; i++)
{
switch (op)
{
case less:
{
if (pbw->data[i] < val)
pbw->data[i] = na;
break;
}
case less_equal:
{
if (pbw->data[i] <= val)
pbw->data[i] = na;
break;
}
case more:
{
if (pbw->data[i] > val)
pbw->data[i] = na;
break;
}
case more_equal:
{
if (pbw->data[i] >= val)
pbw->data[i] = na;
break;
}
case equal:
{
if (pbw->data[i] == val)
pbw->data[i] = na;
break;
}
case not_equal:
{
if (pbw->data[i] != val)
pbw->data[i] = na;
break;
}
case invalid:
case mask:
default:
{
break;
}
}
}
}
perBaseWigOutputNASkip(pbwList, out, wot, decimals, NULL, FALSE, condense);
perBaseWigFreeList(&pbwList);
}
carefulClose(&out);
if (wig_only)
rename(wigfile, outputfile);
else
{
writeBw(wigfile, outputfile, mb->chromSizeHash);
remove(wigfile);
}
}
static void bwtool_remove_mask(struct metaBig *mb, char *mask_file, char *outputfile, enum wigOutType wot,
unsigned decimals, boolean condense, boolean wig_only, boolean inverse)
/* masking */
{
char wigfile[512];
safef(wigfile, sizeof(wigfile), "%s.tmp.wig", outputfile);
FILE *out = mustOpen(wigfile, "w");
struct hash *rt_hash = load_range_tree(mask_file);
struct bed *section;
const double na = NANUM;
for (section = mb->sections; section != NULL; section = section->next)
{
struct perBaseWig *pbwList = perBaseWigLoadContinue(mb, section->chrom, section->chromStart, section->chromEnd);
struct perBaseWig *pbw;
struct rbTree *chrom_tree = (struct rbTree *)hashFindVal(rt_hash, section->chrom);
if (chrom_tree && pbwList)
{
for (pbw = pbwList; pbw != NULL; pbw = pbw->next)
{
int size = pbw->chromEnd - pbw->chromStart;
int i;
for (i = 0; i < size; i++)
{
/* algorithmically, probably not the greatest solution to do a tree lookup at ever base */
/* maybe get back to this another day */
boolean does_overlap = rangeTreeOverlaps(chrom_tree, pbw->chromStart + i, pbw->chromStart + i + 1);
if ((does_overlap && !inverse) || (!does_overlap && inverse))
pbw->data[i] = na;
}
}
perBaseWigOutputNASkip(pbwList, out, wot, decimals, NULL, FALSE, condense);
perBaseWigFreeList(&pbwList);
}
}
carefulClose(&out);
if (wig_only)
rename(wigfile, outputfile);
else
{
writeBw(wigfile, outputfile, mb->chromSizeHash);
remove(wigfile);
}
hashFree(&rt_hash);
}
void bwtool_remove(struct hash *options, char *favorites, char *regions, unsigned decimals, enum wigOutType wot,
boolean condense, boolean wig_only, char *thresh_type, char *val_or_file, char *bigfile, char *tmp_dir,
char *outputfile)
/* bwtool_remove - main for removal program */
{
boolean inverse = (hashFindVal(options, "inverse") != NULL) ? TRUE : FALSE;
enum bw_op_type op= get_bw_op_type(thresh_type, inverse);
if (op == invalid)
usage_remove();
struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, regions);
if (op == mask)
bwtool_remove_mask(mb, val_or_file, outputfile, wot, decimals, condense, wig_only, inverse);
else
bwtool_remove_thresh(mb, op, val_or_file, outputfile, wot, decimals, condense, wig_only);
metaBigClose(&mb);
}