forked from dpryan79/Misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_homopolymer.c
317 lines (287 loc) · 9.14 KB
/
filter_homopolymer.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAXREAD 1024
typedef struct {
char *name1; //Name of left read, max length of maxname1
char *sequence1; //Sequence of the left read, max length of maxread1
char *qual1; //Quality score of the left read, max length of maxread1
char *name2; //As above, but for the left read
char *sequence2;
char *qual2;
int maxname1;
int maxname2;
int maxread1;
int maxread2;
int homopolymer; //Has the read been homopolymer filtered
} seq_read;
int read_init(seq_read *read) {
int rv = 1;
read->name1 = calloc(sizeof(char), MAXREAD);
read->name2 = calloc(sizeof(char), MAXREAD);
read->sequence1 = calloc(sizeof(char), MAXREAD);
read->sequence2 = calloc(sizeof(char), MAXREAD);
read->qual1 = calloc(sizeof(char), MAXREAD);
read->qual2 = calloc(sizeof(char), MAXREAD);
read->maxname1 = MAXREAD;
read->maxname2 = MAXREAD;
read->maxread1 = MAXREAD;
read->maxread2 = MAXREAD;
if(read->name1 == NULL) rv=0;
if(read->name2 == NULL) rv=0;
if(read->sequence1 == NULL) rv=0;
if(read->sequence2 == NULL) rv=0;
if(read->qual1 == NULL) rv=0;
if(read->qual2 == NULL) rv=0;
return rv;
}
//Free up the memory occupied by a read
void read_destroy(seq_read *read) {
free(read->name1);
free(read->name2);
free(read->sequence1);
free(read->sequence2);
free(read->qual1);
free(read->qual2);
free(read);
}
void filterHomopolymer(seq_read *read, int maxL) {
int i = 0, L = 0;
int l = strlen(read->sequence1);
char c = NULL;
read->homopolymer = 0;
if(l > 0) {
for(i=0; i<l; i++) {
if(read->sequence1[i] == c) L++;
if(L > maxL) {
read->homopolymer = 1;
return;
}
if(read->sequence1[i] !=c) {
L = 1;
c = read->sequence1[i];
}
}
}
//read #2
l = strlen(read->sequence2);
read->homopolymer = 0;
if(l > 0) {
for(i=0; i<l; i++) {
if(read->sequence2[i] == c) L++;
if(L > maxL) {
read->homopolymer = 1;
return;
}
if(read->sequence2[i] !=c) {
L = 1;
c = read->sequence2[i];
}
}
}
}
void usage(char *prog) {
printf("%s [options] [-1 fastq_1.gz -2 fastq_2.gz | fastq.gz]\n", prog);
printf("\n\
Input files are gzipped fastq files. If reads are paired-end rather than\n\
single-ended, -1 and -2 must be used to denote the left and right reads\n\
(usually ending in _1.fq.gz and _2.fq.gz or similar, respectively)\n\
\n\
Output files will be gzipped and named by removing the .fq.gz or \n\
.fastq.gz extension and replacing it with .filtered.fq.gz\n\
\n\
--homopolymer The maximum allowed length for a homopolymer repeat. If a\n\
stretch greater than this length is present then the read/pair\n\
will be filtered. The default is 7.\n\
\n");
}
FILE* determine_name(char *fname, int orphaned) {
char *basename = malloc(sizeof(char)*MAXREAD);
char *p = basename;
char *oname;
char *cmd = malloc(sizeof(char)*MAXREAD);
FILE *output;
//This is unlikely to ever occur
if(strlen(fname) > MAXREAD-1) {
basename = realloc(basename, sizeof(char) * (strlen(fname) + 20));
printf("determine_name\n");
if(p!=basename) free(p);
}
basename = strcpy(basename, fname);
p = strrchr(basename, '.');
if(p != NULL) {
if(strcmp(p, ".gz") == 0 || strcmp(p, ".bz2") == 0 || strcmp(p, ".bz") == 0 || strcmp(p, ".GZ") == 0) {
*p = '\0';
p = strrchr(basename, '.');
if(p != NULL) {
if(strcmp(p, ".fastq") == 0 || strcmp(p, ".fq") == 0) *p = '\0';
}
}
}
//construct the output file name
oname = malloc(sizeof(char)*(strlen(basename) + strlen(".filtered.fq.gz") + 1));
sprintf(oname, "%s.filtered.fq.gz", basename);
//Finally, open the file for writing
sprintf(cmd, "gzip > %s", oname);
output = popen(cmd, "w");
free(cmd);
free(basename);
free(oname);
return output;
}
void write_trimmed(FILE *f, seq_read *read, int lr) {
if(lr == 0) {
fprintf(f, "%s\n", read->name1);
fprintf(f, "%s\n", read->sequence1);
fprintf(f, "+\n");
fprintf(f, "%s\n", read->qual1);
} else {
fprintf(f, "%s\n", read->name2);
fprintf(f, "%s\n", read->sequence2);
fprintf(f, "+\n");
fprintf(f, "%s\n", read->qual2);
}
}
int main(int argc, char *argv[]) {
int i;
char *file1 = NULL, *file2 = NULL;
char *line = malloc(MAXREAD*sizeof(char));
int maxline = MAXREAD; //This is the buffer size that gzgets will read into
//it can be increased internally
int total_reads = 0, maxL = 7;
int total_filtered = 0;
int keep = 0;
FILE *f1, *f2, *of1, *of2;
seq_read *read = malloc(sizeof(seq_read));
char *p = line, *p2;
char *cmd = malloc(sizeof(char)*MAXREAD);
//Read in the input
for(i=1; i<argc; i++) {
if(strcmp(argv[i], "-h") == 0) {
usage(argv[0]);
return(1);
}
if(strcmp(argv[i], "--homopolymer") == 0) {
i++;
maxL= atoi(argv[i]);
} else {
if(strncmp(argv[i], "-", 1) == 0) {
//Got an unmatched option
usage(argv[0]);
return(1);
}
if(file1 == NULL) {
file1 = argv[i];
} else {
printf("To make things simpler, you must specify paired-end reads with -1 and -2.\n");
usage(argv[0]);
return(1);
}
}
}
if(file1 == NULL) {
usage(argv[0]);
return 1;
}
//Open the files
p2 = strrchr(file1, '.');
if(strcmp(p2, ".gz") == 0 || strcmp(p2, ".GZ") == 0) {
sprintf(cmd, "zcat %s", file1);
} else if(strcmp(p2, ".bz2") == 0 || strcmp(p2, ".bz") == 0) {
sprintf(cmd, "bzcat %s", file1);
} else {
sprintf(cmd, "cat %s", file1);
}
f1 = popen(cmd, "r");
of1 = determine_name(file1, 0);
if(file2 != NULL) {
if(strcmp(p2, ".gz") == 0 || strcmp(p2, ".GZ") == 0) {
sprintf(cmd, "zcat %s", file2);
} else if(strcmp(p2, ".bz2") == 0 || strcmp(p2, ".bz") == 0) {
sprintf(cmd, "bzcat %s", file2);
} else {
sprintf(cmd, "cat %s", file2);
}
f2 = popen(cmd, "r");
of2 = determine_name(file2, 0);
}
free(cmd);
/******************************************************************
/
/ Everything Below here can be put in a thread function with
/ multiple mutexes
/
******************************************************************/
//Initialize the read
read_init(read);
//Parse each read
while(1) {
//Lock a mutex
//Read1 Name
line = fgets(line, maxline, f1);
if(line == NULL) break;
line[strlen(line)-1] = '\0';
read->name1 = strcpy(read->name1, line);
//Read1 sequence
line = fgets(line, maxline, f1);
line[strlen(line)-1] = '\0';
read->sequence1 = strcpy(read->sequence1, line);
//+
line = fgets(line, maxline, f1);
line[strlen(line)-1] = '\0';
//Read1 Quality
line = fgets(line, maxline, f1);
line[strlen(line)-1] = '\0';
read->qual1 = strcpy(read->qual1, line);
//Read2
if(file2 != NULL) {
//Read1 Name
line = fgets(line, maxline, f2);
line[strlen(line)-1] = '\0';
read->name2 = strcpy(read->name2, line);
//Read1 sequence
line = fgets(line, maxline, f2);
line[strlen(line)-1] = '\0';
read->sequence2 = strcpy(read->sequence2, line);
//+
line = fgets(line, maxline, f2);
line[strlen(line)-1] = '\0';
//Read1 Quality
line = fgets(line, maxline, f2);
line[strlen(line)-1] = '\0';
read->qual2 = strcpy(read->qual2, line);
}
read->homopolymer = 0;
filterHomopolymer(read, maxL);
if(read->homopolymer == 1) {
total_filtered++;
} else {
write_trimmed(of1, read, 0);
if(file2 != NULL) {
write_trimmed(of2, read, 1);
}
}
total_reads++;
//Give some output
if(total_reads % 1000000 == 0) printf("%i reads processed\n", total_reads);
}
/******************************************************************
/
/ This marks the end of the threading function
/
******************************************************************/
//Write some diagnostic output
printf("There were %i reads processed.\n", total_reads);
printf("\t%i\thomopolymer filtered\n", total_filtered);
//Close things up
read_destroy(read);
pclose(f1);
pclose(of1);
if(file2 != NULL) {
pclose(f2);
pclose(of2);
}
free(line);
free(p);
return 0;
}