-
Notifications
You must be signed in to change notification settings - Fork 25
/
mWigWriter.c
327 lines (286 loc) · 9.44 KB
/
mWigWriter.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
// Copyright [1999-2017] EMBL-European Bioinformatics Institute
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdint.h>
// Local header
#include "multiplexer.h"
//////////////////////////////////////////////////////
// Tee operator
//////////////////////////////////////////////////////
#define BLOCK_LENGTH 10000
#define MAX_OUT_BLOCKS 2
typedef struct BlockData_st {
char * chroms[BLOCK_LENGTH];
int starts[BLOCK_LENGTH];
int finishes[BLOCK_LENGTH];
double * values;
int count;
int width;
bool bedGraph;
struct BlockData_st * next;
} BlockData;
typedef struct TeeMultiplexerData_st {
FILE * infile;
FILE * outfile;
Multiplexer * in;
BlockData * dataBlocks;
BlockData * lastBlock;
int count;
pthread_t threadID;
pthread_mutex_t continue_mutex;
pthread_cond_t continue_cond;
bool done;
bool bedGraph;
} TeeMultiplexerData;
static void printBlock(FILE * infile, FILE * outfile, BlockData * block) {
int i, j;
bool pointByPoint = false;
bool makeHeader=false;
char ** chromPtr = block->chroms;
int * startPtr = block->starts;
int * finishPtr = block->finishes;
double * valuePtr = block->values;
char * lastChrom = NULL;
int lastFinish = -1;
char buffer[5000];
for (i = 0; i < block->count; i++) {
// Change mode
if (!block->bedGraph && *finishPtr - *startPtr < 2 && !pointByPoint) {
pointByPoint = true;
makeHeader = true;
} else if (*finishPtr - *startPtr > 5 && pointByPoint) {
pointByPoint = false;
}
if (pointByPoint) {
if (makeHeader || (pointByPoint && (lastChrom != *chromPtr || *startPtr > lastFinish)))
fprintf(outfile, "fixedStep chrom=%s start=%i step=1\n", *chromPtr, *startPtr);
makeHeader = false;
for (j = 0; j < *finishPtr - *startPtr; j++) {
int k;
double * ptr = valuePtr;
for (k = 0; k < block->width; k++)
fprintf(outfile, "\t%lf", *(ptr++));
fprintf(outfile, "\n");
}
valuePtr += block->width;
} else if (!infile) {
// Careful bedgraph lines are 0 based
fprintf(outfile, "%s\t%i\t%i", *chromPtr, *startPtr-1, *finishPtr-1);
int k;
for (k = 0; k < block->width; k++)
fprintf(outfile, "\t%lf", *(valuePtr++));
fprintf(outfile, "\n");
} else {
// Read next line in infile
if (!fgets(buffer, 5000, infile)) {
fprintf(stderr, "Could not paste data to file lines, inconsistent number of lines.\n");
exit(1);
}
// Skip empty lines and metadata lines:
while (! (strlen(buffer) && strncmp(buffer, "track", 5) && strncmp(buffer, "browser", 7))) {
if (!fgets(buffer, 5000, infile)) {
fprintf(stderr, "Could not paste data to file lines, inconsistent number of lines.\n");
exit(1);
}
}
// Strip end of line symbols
int k;
for (k = strlen(buffer)-1; k >= 0; k--) {
if (buffer[k] == '\n' || buffer[k] == '\r')
buffer[k] = '\0';
else
break;
}
// Print out
fprintf(outfile, "%s", buffer);
for (k = 0; k < block->width; k++)
fprintf(outfile, "\t%lf", *(valuePtr++));
fprintf(outfile, "\n");
}
lastChrom = *chromPtr;
lastFinish = *finishPtr;
chromPtr++;
startPtr++;
finishPtr++;
}
}
static bool goToNextBlock(TeeMultiplexerData * data) {
BlockData * ptr = data->dataBlocks;
static int i = 0;
i++;
pthread_mutex_lock(&data->continue_mutex);
// Received kill signal
if (data->count < 0)
return true;
// Check that there is work left
data->count--;
if (data->count == 0 && !data->done)
pthread_cond_wait(&data->continue_cond, &data->continue_mutex);
pthread_cond_signal(&data->continue_cond);
pthread_mutex_unlock(&data->continue_mutex);
// Step forward
data->dataBlocks = data->dataBlocks->next;
free(ptr->values);
free(ptr);
return false;
}
static void * printToFile(void * args) {
TeeMultiplexerData * data = (TeeMultiplexerData *) args;
// Wait for first block to arrive
pthread_mutex_lock(&data->continue_mutex);
if (data->count == 0 && !data->done)
pthread_cond_wait(&data->continue_cond, &data->continue_mutex);
pthread_mutex_unlock(&data->continue_mutex);
if (data->count < 0)
return NULL;
while(data->dataBlocks) {
printBlock(data->infile, data->outfile, data->dataBlocks);
if (goToNextBlock(data))
return NULL;
}
return NULL;
}
static void TeeMultiplexerPop(Multiplexer * multi) {
TeeMultiplexerData * data = (TeeMultiplexerData *) multi->data;
Multiplexer * in = data->in;
if (!data->in->done) {
multi->chrom = in->chrom;
multi->start = in->start;
multi->finish = in->finish;
// No need to copy values, pointer points to the source multipliexers values' array
//multi->value = in->value;
if (data->threadID) {
int index = data->lastBlock->count;
data->lastBlock->chroms[index] = in->chrom;
data->lastBlock->starts[index] = in->start;
data->lastBlock->finishes[index] = in->finish;
int i;
double * ptr = data->lastBlock->values + (index * multi->count);
for (i = 0; i < multi->count; i++)
*(ptr++) = in->values[i];
if (++data->lastBlock->count >= BLOCK_LENGTH) {
// Communications
pthread_mutex_lock(&data->continue_mutex);
data->count++;
pthread_cond_signal(&data->continue_cond);
if (data->count > MAX_OUT_BLOCKS)
pthread_cond_wait(&data->continue_cond, &data->continue_mutex);
pthread_mutex_unlock(&data->continue_mutex);
data->lastBlock->next = (BlockData*) calloc(1, sizeof(BlockData));
data->lastBlock->next->values = (double*) calloc(BLOCK_LENGTH * multi->count, sizeof(double));
data->lastBlock->next->width = in->count;
data->lastBlock = data->lastBlock->next;
data->lastBlock->bedGraph = data->bedGraph;
}
}
popMultiplexer(in);
} else if (data->threadID) {
pthread_mutex_lock(&data->continue_mutex);
data->count++;
data->done = true;
pthread_cond_signal(&data->continue_cond);
pthread_mutex_unlock(&data->continue_mutex);
multi->done = true;
pthread_join(data->threadID, NULL);
}
}
static void launchWriter(TeeMultiplexerData * data, int width) {
// Initialize variables
data->count = 0;
data->done = false;
pthread_cond_init(&data->continue_cond, NULL);
pthread_mutex_init(&data->continue_mutex, NULL);
data->dataBlocks = data->lastBlock = (BlockData*) calloc(1, sizeof(BlockData));
data->dataBlocks->values = (double*) calloc(BLOCK_LENGTH * width, sizeof(double));
data->dataBlocks->width = width;
data->lastBlock->bedGraph = data->bedGraph;
// Launch pthread
int err = pthread_create(&data->threadID, NULL, &printToFile, data);
if (err) {
fprintf(stderr, "Could not create new thread %i\n", err);
exit(1);
}
}
static void killWriter(TeeMultiplexerData * data) {
BlockData * block;
if (!data->threadID)
return;
// Set trap
pthread_mutex_lock(&data->continue_mutex);
data->count = -1;
pthread_cond_signal(&data->continue_cond);
pthread_mutex_unlock(&data->continue_mutex);
// Wait for the catch
pthread_join(data->threadID, NULL);
// Clear variables
pthread_cond_destroy(&data->continue_cond);
pthread_mutex_destroy(&data->continue_mutex);
while (data->dataBlocks) {
block = data->dataBlocks;
data->dataBlocks = block->next;
free(block->values);
free(block);
}
data->dataBlocks = NULL;
data->lastBlock = NULL;
}
static void TeeMultiplexerSeek(Multiplexer * multi, const char * chrom, int start, int finish) {
TeeMultiplexerData * data = (TeeMultiplexerData *) multi->data;
killWriter(data);
fflush(data->outfile);
seekMultiplexer(data->in, chrom, start, finish);
multi->done = false;
launchWriter(data, multi->count);
popMultiplexer(multi);
}
Multiplexer * TeeMultiplexer(Multiplexer * in, FILE * outfile, bool bedGraph, bool holdFire) {
TeeMultiplexerData * data = (TeeMultiplexerData *) calloc(1, sizeof(TeeMultiplexerData));
data->in = in;
data->outfile = outfile;
data->bedGraph = bedGraph;
// Hold fire means that you wait for the first seek before doing any writing
if (!holdFire)
launchWriter(data, in->count);
Multiplexer * res = newCoreMultiplexer(data, in->count, &TeeMultiplexerPop, &TeeMultiplexerSeek);
res->values = in->values;
res->inplay = in->inplay;
res->default_values = in->default_values;
popMultiplexer(res);
return res;
}
void toStdoutMultiplexer(Multiplexer * in, bool bedGraph, bool holdFire) {
runMultiplexer(TeeMultiplexer(in, stdout, bedGraph, holdFire));
}
//////////////////////////////////////////////////////////
// Paste Iterator
//////////////////////////////////////////////////////////
Multiplexer * PasteMultiplexer(Multiplexer * in, FILE * infile, FILE * outfile, bool holdFire) {
TeeMultiplexerData * data = (TeeMultiplexerData *) calloc(1, sizeof(TeeMultiplexerData));
data->in = in;
data->infile = infile;
data->bedGraph = true;
data->outfile = outfile;
// Hold fire means that you wait for the first seek before doing any writing
if (!holdFire)
launchWriter(data, in->count);
Multiplexer * res = newCoreMultiplexer(data, in->count, &TeeMultiplexerPop, &TeeMultiplexerSeek);
res->values = in->values;
res->default_values = in->default_values;
res->inplay = in->inplay;
popMultiplexer(res);
return res;
}