-
Notifications
You must be signed in to change notification settings - Fork 24
/
args_assembler.c
370 lines (323 loc) · 9.98 KB
/
args_assembler.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2011-2013 Andre Masella
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include<ctype.h>
#include<errno.h>
#include<ltdl.h>
#include<stdlib.h>
#include<string.h>
#include "pandaseq.h"
static bool set_algorithm(
PandaAssembler assembler,
char flag,
char *argument) {
char *extra;
size_t it;
(void) flag;
if (argument == NULL)
return true;
extra = strchr(argument, LT_PATHSEP_CHAR);
if (extra != NULL) {
*extra = '\0';
extra++;
}
for (it = 0; it < panda_algorithms_length; it++) {
if (strcmp(argument, panda_algorithms[it]->name) == 0) {
PandaAlgorithm algo;
if (panda_algorithms[it]->create == NULL) {
fprintf(stderr, "It seems that no one wrote the code to use this algorithm properly.\n");
free(argument);
return false;
}
algo = panda_algorithms[it]->create(extra);
if (algo == NULL) {
fprintf(stderr, "Unable to initialise requested algorithm.\n");
free(argument);
return false;
}
panda_assembler_set_algorithm(assembler, algo);
panda_algorithm_unref(algo);
free(argument);
return true;
}
}
fprintf(stderr, "Unknown algorithm: %s\n", argument);
free(argument);
return false;
}
const panda_tweak_assembler panda_stdargs_algorithm = { 'A', "algorithm:parameters", "Select the algorithm to use for overlap selection and scoring.", set_algorithm, false };
static bool set_primers_after(
PandaAssembler assembler,
char flag,
char *argument) {
(void) flag;
(void) argument;
panda_assembler_set_primers_after(assembler, true);
return true;
}
const panda_tweak_assembler panda_stdargs_primers_after = { 'a', NULL, "Strip the primers after assembly, rather than before.", set_primers_after, false };
static bool set_primer_penalty(
PandaAssembler assembler,
char flag,
char *argument) {
double threshold = 0;
(void) flag;
if (argument != NULL) {
errno = 0;
threshold = strtod(argument, NULL);
if (errno != 0 || threshold < 0) {
fprintf(stderr, "Bad threshold: %s. It should be between 0 and 1.\n", argument);
free(argument);
return false;
}
free(argument);
}
panda_assembler_set_primer_penalty(assembler, threshold);
return true;
}
const panda_tweak_assembler panda_stdargs_primer_penalty = { 'D', "threshold", "Penalise primers if the further they are from the start of the sequence.", set_primer_penalty, false };
bool no_n_check(
PandaLogProxy logger,
const panda_result_seq *sequence,
void *user_data) {
(void) logger;
(void) user_data;
return sequence->degenerates == 0;
}
static bool add_module(
PandaAssembler assembler,
char flag,
char *argument) {
PandaModule module;
(void) flag;
if (argument == NULL) {
return true;
}
module = panda_module_load(panda_assembler_get_logger(assembler), argument);
free(argument);
if (module == NULL) {
return false;
}
panda_assembler_add_module(assembler, module);
panda_module_unref(module);
return true;
}
const panda_tweak_assembler panda_stdargs_module = { 'C', "filter", "Load a pluggable filter module.", add_module, true };
static bool set_degenerates(
PandaAssembler assembler,
char flag,
char *argument) {
PandaModule m = panda_module_new("DEGENERATE", no_n_check, NULL, NULL, NULL);
(void) flag;
panda_assembler_add_module(assembler, m);
panda_module_unref(m);
if (argument != NULL) {
free(argument);
}
return true;
}
const panda_tweak_assembler panda_stdargs_degenerates = { 'N', NULL, "Eliminate all sequences with unknown nucleotides in the output.", set_degenerates, false };
static bool set_threshold(
PandaAssembler assembler,
char flag,
char *argument) {
double threshold = 0.6;
(void) flag;
if (argument != NULL) {
errno = 0;
threshold = strtod(argument, NULL);
if (errno != 0 || threshold < 0 || threshold > 1) {
fprintf(stderr, "Bad threshold: %s. It should be between 0 and 1.\n", argument);
free(argument);
return false;
}
free(argument);
}
panda_assembler_set_threshold(assembler, threshold);
return true;
}
const panda_tweak_assembler panda_stdargs_threshold = { 't', "threshold", "The minimum probability that a sequence must have to assemble and, if used, match a primer.", set_threshold, false };
static bool set_primer(
PandaAssembler assembler,
char *argument,
char *direction,
void (*set_trim) (PandaAssembler,
size_t),
void (*set_func) (PandaAssembler,
panda_nt *,
size_t),
panda_nt (*parse) (char)) {
if (argument != NULL) {
char *endptr;
size_t offset;
errno = 0;
offset = strtol(argument, &endptr, 10);
if (*endptr != '\0') {
panda_nt buffer[PANDA_MAX_LEN];
size_t it;
for (it = 0; it < strlen(argument); it++) {
if ((buffer[it] = parse(argument[it])) == '\0') {
fprintf(stderr, "ERR\tBADNT\t%cPRIMER\n", (int) toupper(direction[0]));
free(argument);
return false;
}
}
set_func(assembler, buffer, strlen(argument));
} else if (errno != 0 || offset < 1 || offset > PANDA_MAX_LEN) {
fprintf(stderr, "Bad %s primer length.\n", direction);
free(argument);
return false;
} else {
set_trim(assembler, offset);
}
free(argument);
}
return true;
}
static bool set_primer_group(
PandaAssembler assembler,
char flag,
char *argument) {
if (flag == 'p') {
return set_primer(assembler, argument, "forward", panda_assembler_set_forward_trim, panda_assembler_set_forward_primer, panda_nt_from_ascii);
} else if (flag == 'q') {
return set_primer(assembler, argument, "reverse", panda_assembler_set_reverse_trim, panda_assembler_set_reverse_primer, panda_nt_from_ascii_complement);
}
if (argument != NULL) {
free(argument);
}
return false;
}
const panda_tweak_assembler panda_stdargs_forward_primer = { 'p', "primer", "Forward primer sequence or number of bases to be removed.", set_primer_group, false };
const panda_tweak_assembler panda_stdargs_reverse_primer = { 'q', "primer", "Reverse primer sequence or number of bases to be removed.", set_primer_group, false };
bool short_check(
PandaLogProxy logger,
const panda_result_seq *sequence,
void *user_data) {
(void) logger;
return sequence->sequence_length >= (size_t) user_data;
}
static bool set_short_check(
PandaAssembler assembler,
char flag,
char *argument) {
long int minlen;
PandaModule m;
(void) flag;
if (argument == NULL) {
return true;
}
errno = 0;
minlen = (size_t) strtol(argument, NULL, 10);
if (errno != 0 || minlen < 0 || (size_t) minlen > 2 * PANDA_MAX_LEN) {
fprintf(stderr, "Bad minimum length.\n");
free(argument);
return false;
}
m = panda_module_new("SHORT", short_check, NULL, (void *) (size_t) minlen, NULL);
panda_assembler_add_module(assembler, m);
panda_module_unref(m);
free(argument);
return true;
}
const panda_tweak_assembler panda_stdargs_min_len = { 'l', "length", "Minimum length for a sequence.", set_short_check, false };
bool long_check(
PandaLogProxy logger,
const panda_result_seq *sequence,
void *user_data) {
size_t length = (size_t) user_data;
(void) logger;
return sequence->sequence_length <= length;
}
static bool set_long_check(
PandaAssembler assembler,
char flag,
char *argument) {
size_t maxlen;
PandaModule m;
(void) flag;
if (argument == NULL) {
return true;
}
errno = 0;
maxlen = (size_t) strtol(argument, NULL, 10);
if (errno != 0 || maxlen < 1 || maxlen > 2 * PANDA_MAX_LEN) {
fprintf(stderr, "Bad maximum length.\n");
free(argument);
return false;
}
m = panda_module_new("LONG", long_check, NULL, (void *) maxlen, NULL);
panda_assembler_add_module(assembler, m);
panda_module_unref(m);
free(argument);
return true;
}
const panda_tweak_assembler panda_stdargs_max_len = { 'L', "length", "Maximum length for a sequence.", set_long_check, false };
static bool set_minimum_overlap(
PandaAssembler assembler,
char flag,
char *argument) {
int min_overlap;
(void) flag;
if (argument == NULL) {
return true;
}
errno = 0;
min_overlap = strtol(argument, NULL, 10);
if (errno != 0 || min_overlap < 1 || (size_t) min_overlap > 2 * PANDA_MAX_LEN) {
fprintf(stderr, "Bad overlap length.\n");
free(argument);
return false;
}
panda_assembler_set_minimum_overlap(assembler, min_overlap);
free(argument);
return true;
}
const panda_tweak_assembler panda_stdargs_min_overlap = { 'o', "length", "Minumum overlap region length for a sequence.", set_minimum_overlap, false };
static bool set_maxiumum_overlap(
PandaAssembler assembler,
char flag,
char *argument) {
int max_overlap;
(void) flag;
if (argument == NULL) {
return true;
}
errno = 0;
max_overlap = strtol(argument, NULL, 10);
if (errno != 0 || max_overlap < 0 || (size_t) max_overlap > 2 * PANDA_MAX_LEN) {
fprintf(stderr, "Bad overlap length.\n");
free(argument);
return false;
}
panda_assembler_set_maximum_overlap(assembler, max_overlap);
free(argument);
return true;
}
const panda_tweak_assembler panda_stdargs_max_overlap = { 'O', "length", "Maximum overlap region length for a sequence. (0 to use read length.)", set_maxiumum_overlap, false };
const panda_tweak_assembler *const panda_stdargs[] = {
&panda_stdargs_algorithm,
&panda_stdargs_module,
&panda_stdargs_primer_penalty,
&panda_stdargs_max_len,
&panda_stdargs_degenerates,
&panda_stdargs_max_overlap,
&panda_stdargs_primers_after,
&panda_stdargs_min_len,
&panda_stdargs_min_overlap,
&panda_stdargs_forward_primer,
&panda_stdargs_reverse_primer,
&panda_stdargs_threshold,
};
const size_t panda_stdargs_length = sizeof(panda_stdargs) / sizeof(panda_tweak_assembler *);