Skip to content

Commit f317817

Browse files
author
Mengyao Zhao
committed
catch weight matrix reading errors
1 parent 70a12c1 commit f317817

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CC = gcc
22
CXX = g++
3-
CFLAGS += -Wall -pipe -O2 #-g -fsanitize=address
3+
#CFLAGS += -Wall -pipe -O2
4+
CFLAGS += -Wall -pipe -g -fsanitize=address # for debug
45
CXXFLAGS := $(CFLAGS)
56
LOBJS = ssw.o
67
LCPPOBJS = ssw_cpp.o

src/main.c

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* main.c
22
* Created by Mengyao Zhao on 06/23/11.
33
* Version 1.2.2
4-
* Last revision by Mengyao Zhao on 2022-Apr-15.
4+
* Last revision by Mengyao Zhao on 2022-May-21.
55
*/
66

77
#include <stdlib.h>
@@ -197,11 +197,9 @@ int main (int argc, char * const argv[]) {
197197
gzFile read_fp, ref_fp;
198198
kseq_t *read_seq, *ref_seq;
199199
int32_t l, m, k, match = 2, mismatch = 2, gap_open = 3, gap_extension = 1, path = 0, reverse = 0, n = 5, sam = 0, protein = 0, header = 0, s1 = 67108864, s2 = 128, filter = 0;
200-
char mat_name[16];
201-
mat_name[0] = '\0';
202200
int8_t *mata, *ref_num, *num, *num_rc = 0;
203201
const int8_t* mat;
204-
char* read_rc = 0;
202+
char* read_rc = 0, *mat_name = 0;
205203

206204
static const int8_t mat50[] = {
207205
// A R N D C Q E G H I L K M F P S T W Y V B Z X *
@@ -257,22 +255,6 @@ int main (int argc, char * const argv[]) {
257255

258256
int8_t* table = nt_table;
259257

260-
// Parse command line.
261-
while ((l = getopt(argc, argv, "m:x:o:e:a:f:pcrsh")) >= 0) {
262-
switch (l) {
263-
case 'm': match = atoi(optarg); break;
264-
case 'x': mismatch = atoi(optarg); break;
265-
case 'o': gap_open = atoi(optarg); break;
266-
case 'e': gap_extension = atoi(optarg); break;
267-
case 'a': strcpy(mat_name, optarg); break;
268-
case 'f': filter = atoi(optarg); break;
269-
case 'p': protein = 1; break;
270-
case 'c': path = 1; break;
271-
case 'r': reverse = 1; break;
272-
case 's': sam = 1; break;
273-
case 'h': header = 1; break;
274-
}
275-
}
276258
if (optind + 2 > argc) {
277259
fprintf(stderr, "\n");
278260
fprintf(stderr, "Usage: ssw_test [options] ... <target.fasta> <query.fasta>(or <query.fastq>)\n");
@@ -291,6 +273,28 @@ int main (int argc, char * const argv[]) {
291273
return 1;
292274
}
293275

276+
// Parse command line.
277+
while ((l = getopt(argc, argv, "m:x:o:e:a:f:pcrsh")) >= 0) {
278+
switch (l) {
279+
case 'm': match = atoi(optarg); break;
280+
case 'x': mismatch = atoi(optarg); break;
281+
case 'o': gap_open = atoi(optarg); break;
282+
case 'e': gap_extension = atoi(optarg); break;
283+
284+
case 'a':
285+
mat_name = (char*)malloc(strlen(optarg) + 1);
286+
strcpy(mat_name, optarg);
287+
break;
288+
289+
case 'f': filter = atoi(optarg); break;
290+
case 'p': protein = 1; break;
291+
case 'c': path = 1; break;
292+
case 'r': reverse = 1; break;
293+
case 's': sam = 1; break;
294+
case 'h': header = 1; break;
295+
}
296+
}
297+
294298
// initialize scoring matrix for genome sequences
295299
mata = (int8_t*)calloc(25, sizeof(int8_t));
296300
for (l = k = 0; LIKELY(l < 4); ++l) {
@@ -300,14 +304,21 @@ int main (int argc, char * const argv[]) {
300304
for (m = 0; LIKELY(m < 5); ++m) mata[k++] = 0;
301305
mat = mata;
302306

303-
if (protein == 1 && (! strcmp(mat_name, "\0"))) {
307+
if (protein == 1 && mat_name == 0) {
304308
n = 24;
305309
table = aa_table;
306310
mat = mat50;
307-
} else if (strcmp(mat_name, "\0")) {
311+
} else if (mat_name != 0) {
308312

309313
// Parse score matrix.
310314
FILE *f_mat = fopen(mat_name, "r");
315+
free(mat_name);
316+
if (f_mat == NULL) {
317+
fprintf(stderr, "Failed to open the weight matrix file.\n");
318+
free(mata);
319+
return 1;
320+
}
321+
311322
char line[128];
312323
mata = (int8_t*)realloc(mata, 1024 * sizeof(int8_t));
313324
k = 0;

0 commit comments

Comments
 (0)