This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
forked from slowkoni/rfmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputline.cpp
258 lines (219 loc) · 7.08 KB
/
inputline.cpp
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
/* RFMIX v2.XX - Local Ancestry and Admixture Analysis
Bustamante Lab - Stanford School of Medicine
(c) 2016 Mark Hamilton Wright
This program is licensed for academic research use only
unless otherwise stated. Contact [email protected] for
commercial licensing options.
Academic and research users should cite Brian Maples'
paper describing RFMIX in any publication using RFMIX
results. Citation is printed when the program is started. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "kmacros.h"
#include "inputline.h"
#include "rfmix.h"
#define INPUTLINE_CHUNK (8192)
FILE *Inputline::open_gzip_read(char *fname) {
int fds[2];
int child_pid;
FILE *f;
if (pipe(fds) != 0) {
fprintf(stderr,"Can't create pipe to read output from gzip -dc (%s)\n", strerror(errno));
exit(-1);
}
child_pid = fork();
if (child_pid == 0) {
/* New child process executes this code, which just dups the write end of the pipe to
standard output, so that gzip which writes to stdout will really be outputing to
the pipe for the parent to read */
close(0);
close(1);
close(fds[0]);
dup2(fds[1], 1);
close(fds[1]);
execlp("gzip","gzip","-dc",fname, NULL);
/* If gzip is successfully started, execlp() never returns and these lines
are never reached. Only if execlp() fails will the process continue here */
fprintf(stderr,"Can't execute gzip to decompress input file %s (%s)\n", fname, strerror(errno));
exit(-1);
} else {
/* parent process. If the child_pid is -1, then fork() failed, which pretty much
never happens. */
if (child_pid == -1) {
fprintf(stderr,"Can't fork() a child process to start gzip for decompression (%s)\n",
strerror(errno));
exit(-1);
}
/* Close the write end of the pipe because we are only going to read. Then open a normal
stdio FILE stream from the pipe read end file descriptor. We set the child_pid field
of the object so that the destructor knows to call wait() to reap the child process.
Otherwise, it is a zombie hanging around */
close(fds[1]);
f = fdopen(fds[0], "r");
if (f == NULL) {
fprintf(stderr,"Can't open file handle from file descriptor to read gzip output (%s)\n",
strerror(errno));
exit(-1);
}
this->child_pid = child_pid;
}
return f;
}
FILE *Inputline::open_bcftools_read(char *fname, char *chm) {
int fds[2];
int child_pid;
FILE *f;
if (pipe(fds) != 0) {
fprintf(stderr,"Can't create pipe to read output from bcftools (%s)\n", strerror(errno));
exit(-1);
}
child_pid = fork();
if (child_pid == 0) {
/* New child process executes this code, which just dups the write end of the pipe to
standard output, so that bcftools which writes to stdout will really be outputing to
the pipe for the parent to read */
close(0);
close(1);
close(fds[0]);
dup2(fds[1], 1);
close(fds[1]);
if (chm != NULL) {
execlp("bcftools","bcftools","view","--regions", chm, fname, NULL);
} else {
execlp("bcftools","bcftools","view", fname, NULL);
}
/* If bcftools is successfully started, execlp() never returns and these lines
are never reached. Only if execlp() fails will the process continue here */
fprintf(stderr,"Can't execute bcftools to read BCF file %s (%s) - is bcftools installed?\n",
fname, strerror(errno));
exit(-1);
} else {
/* parent process. If the child_pid is -1, then fork() failed, which pretty much
never happens. */
if (child_pid == -1) {
fprintf(stderr,"Can't fork() a child process to start bcftools (%s)\n",
strerror(errno));
exit(-1);
}
/* Close the write end of the pipe because we are only going to read. Then open a normal
stdio FILE stream from the pipe read end file descriptor. We set the child_pid field
of the object so that the destructor knows to call wait() to reap the child process.
Otherwise, it is a zombie hanging around */
close(fds[1]);
f = fdopen(fds[0], "r");
if (f == NULL) {
fprintf(stderr,"Can't open file handle from file descriptor to read bcftools output (%s)\n",
strerror(errno));
exit(-1);
}
this->child_pid = child_pid;
}
return f;
}
Inputline::Inputline(char *fname, char *chm) {
/* use the filename extension to determine if we need a helper program to read
the file. Functions above take care of starting the relevant program and
piping its output back to a FILE handle we can read like any other file */
int l = strlen(fname);
if ((l > 7 && strcmp(".bcf.gz", fname + l - 7) == 0) ||
(l > 4 && strcmp(".bcf", fname +l - 4) == 0)) {
f = open_bcftools_read(fname, chm);
}
else if (l > 3 && strcmp(".gz", fname + l - 3) == 0) {
f = open_gzip_read(fname);
}
else {
f = fopen(fname, "r");
child_pid = 0; // There is no helper program in a child process if we are reading the file directly
}
if (f == NULL) {
fprintf(stderr,"\nCan't open input file %s (%s)\n\n", fname, strerror(errno));
exit(-1);
}
this->fname = strdup(fname);
MA(input_buf, INPUTLINE_CHUNK, char);
MA(mod_buf, INPUTLINE_CHUNK, char);
alloc_length = INPUTLINE_CHUNK;
line_stored = 0;
line_no = 0;
pthread_mutex_init(&lock, NULL);
}
Inputline::Inputline(char *fname) {
Inputline(fname, NULL);
}
char *Inputline::nextline(int return_copy) {
int l, input_l;
char *rval;
pthread_mutex_lock(&lock);
if (line_stored) {
line_stored = 0;
if (return_copy)
rval = strdup(input_buf);
else {
strcpy(mod_buf, input_buf);
rval = mod_buf;
}
pthread_mutex_unlock(&lock);
return rval;
}
l = 0;
input_l = 0;
input_buf[0] = 0;
line_no++;
for(;;) {
if (alloc_length - l < INPUTLINE_CHUNK) {
alloc_length += INPUTLINE_CHUNK;
RA(input_buf, alloc_length, char);
RA(mod_buf, alloc_length, char);
}
if (fgets(input_buf + l, INPUTLINE_CHUNK, f) == NULL) break;
input_l = strlen(input_buf + l);
if (input_l < INPUTLINE_CHUNK - 1) break;
l += input_l;
}
if (input_l == 0) {
rval = NULL;
} else {
if (return_copy)
rval = strdup(input_buf);
else {
strcpy(mod_buf, input_buf);
rval = mod_buf;
}
}
pthread_mutex_unlock(&lock);
return rval;
}
char *Inputline::nextline(void) {
return nextline(INPUTLINE_NOCOPY);
}
void Inputline::pushback() {
/* it probably does not make sense for inputline_pushback() to be used in
a multithreaded context */
pthread_mutex_lock(&lock);
line_stored = 1;
pthread_mutex_unlock(&lock);
}
Inputline::~Inputline() {
if (pthread_mutex_destroy(&lock) == EBUSY)
fprintf(stderr,"Warning: Call to inputline_free() with object locked\n");
fclose(f);
f = NULL;
free(fname);
if (child_pid) {
kill(child_pid, SIGINT);
waitpid(child_pid, NULL, 0);
}
free(input_buf);
input_buf = NULL;
free(mod_buf);
mod_buf = NULL;
alloc_length = 0;
line_stored = 0;
}