-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_average.cc
240 lines (209 loc) · 7.56 KB
/
window_average.cc
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
/*
Input is a file containing <chromosome>, <position>, <depth> fields
Options: -w <window_size> -e <every>
Outputs to stdout a window-averaged depth, taking, for the current row,
the window defined by p in (p - w/2, p + w/2)
*/
#include <getopt.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
// #include <cmath>
#include <vector>
#include "samutil/file_utils.h"
#include "histo.h"
struct ll
{
int64_t pos;
size_t depth;
ll * next;
ll(size_t _p, size_t _d, ll * _n) : pos(_p), depth(_d), next(_n) { }
};
int main(int argc, char *argv[])
{
size_t window_size = 0, every = 1000, max_mem = 4e9, bins_per_unit = 1, nunits = 100;
const char *label = "";
const char *window_averaged_outfile = "";
char c;
while ((c = getopt(argc, argv, "w:e:l:m:b:n:a:")) >= 0)
{
switch(c)
{
case 'w': window_size = static_cast<size_t>(atof(optarg)); break;
case 'e': every = static_cast<size_t>(atof(optarg)); break;
case 'l': label = optarg; break;
case 'm': max_mem = static_cast<size_t>(atof(optarg)); break;
case 'b': bins_per_unit = static_cast<size_t>(atof(optarg)); break;
case 'n': nunits = static_cast<size_t>(atof(optarg)); break;
case 'a': window_averaged_outfile = optarg; break;
}
}
size_t nbins = bins_per_unit * nunits;
char const* infile = argv[optind];
FILE * in_fh = fopen(infile, "r");
if (in_fh == NULL)
{
fprintf(stderr, "Error: Couldn't open input file %s\n", infile);
exit(10);
}
char const* histfile = argv[optind + 1];
FILE *hist_fh = fopen(histfile, "w");
if (! hist_fh)
{
fprintf(stderr, "Error: Couldn't histogram output file %s\n", histfile);
exit(10);
}
FILE *win_fh = fopen(window_averaged_outfile, "w");
char chrom[64], prev_chrom[64] = "";
size_t depth, pileup_depth;
int64_t pileup_pos = -1, window_pos = 1, half_window_size = window_size / 2;
float running_sum = 0.0, current_count = 0.0;
ll *head = NULL, *tail = NULL, *elem = NULL, *mid = NULL;
size_t max_pileup_line_size = 1e7;
size_t bytes_read, bytes_wanted = max_mem;
char *chunk = new char[bytes_wanted + max_pileup_line_size + 1];
size_t fread_nsec;
char *last_fragment;
size_t *hist = (size_t *)calloc(nbins, sizeof(size_t));
size_t dbin;
bool new_contig = true;
//size_t line = 0;
while (! feof(in_fh))
{
bytes_read = FileUtils::read_until_newline(chunk, bytes_wanted, max_pileup_line_size, in_fh, &fread_nsec);
std::vector<char *> lines_vec = FileUtils::find_complete_lines_nullify(chunk, &last_fragment);
size_t nlines = lines_vec.size();
char **lines = lines_vec.data();
char **line;
for (line = lines; line != lines + nlines; )
{
if (pileup_pos < window_pos)
{
// we've gone past the pileup, need to load a new line.
// this should happen whenever
sscanf(*line++, "%s\t%zi\t%*c\t%zu", chrom, &pileup_pos, &pileup_depth);
new_contig = strcmp(chrom, prev_chrom);
}
else { new_contig = false; }
if (new_contig) { window_pos = 1; }
depth = pileup_pos == window_pos ? pileup_depth : 0;
int64_t window_min_pos = window_pos - window_size;
if (new_contig)
{
// starting a new contig. flush remaining windows
while (head != NULL && head->next != NULL)
{
running_sum -= head->depth;
current_count--;
elem = head;
head = head->next;
while (mid != tail && mid->pos < head->pos + half_window_size)
{
mid = mid->next;
}
if ((tail->pos - head->pos) >= half_window_size)
{
if (win_fh && (mid->pos % every == 0))
{
fprintf(win_fh, "%s\t%s\t%zu\t%f\n",
label,
prev_chrom, mid->pos,
running_sum / window_size);
}
dbin = bin(running_sum / window_size, bins_per_unit);
// tally histogram
if (dbin < nbins)
{
hist[dbin]++;
}
}
delete elem;
}
if (strcmp(prev_chrom, "") != 0)
{
print_hist(hist_fh, label, prev_chrom, hist, bins_per_unit, nbins);
}
// now start the new window
head = new ll(window_pos, depth, NULL);
tail = head;
mid = head;
current_count = 0;
running_sum = 0;
}
else
{
// moving the window as usual. append
tail->next = new ll(window_pos, depth, NULL);
running_sum += tail->depth;
current_count++;
tail = tail->next;
// update mid until it is is within appropriate range of tail
while (mid->pos < tail->pos - half_window_size)
{
mid = mid->next;
}
// update head until it is within appropriate range of tail
while (head->pos < window_min_pos)
{
running_sum -= head->depth;
current_count--;
elem = head;
head = head->next;
delete elem;
}
}
// output windows, update histogram
if ((tail->pos - head->pos) >= half_window_size)
{
if (win_fh && (mid->pos % every == 0))
{
fprintf(win_fh, "%s\t%s\t%zu\t%f\n",
label,
prev_chrom, mid->pos,
running_sum / window_size);
}
dbin = bin(running_sum / window_size, bins_per_unit);
if (dbin < nbins)
{
hist[dbin]++;
}
}
strcpy(prev_chrom, chrom);
++window_pos;
}
}
fclose(in_fh);
// now we still have a non-empty linked list. Proceed to print
// out the remaining shrinking windows
while (head->next != NULL)
{
running_sum -= head->depth;
current_count--;
elem = head;
head = head->next;
while (mid != tail && mid->pos < head->pos + half_window_size)
{
mid = mid->next;
}
if ((tail->pos - head->pos) >= half_window_size)
{
if (win_fh && (mid->pos % every == 0))
{
fprintf(win_fh, "%s\t%s\t%zu\t%f\n",
label,
prev_chrom, mid->pos,
running_sum / window_size);
}
dbin = bin(running_sum / window_size, bins_per_unit);
if (dbin < nbins)
{
hist[dbin]++;
}
}
delete elem;
}
print_hist(hist_fh, label, prev_chrom, hist, bins_per_unit, nbins);
free(hist);
fclose(hist_fh);
if (win_fh) { fclose(win_fh); };
}