3 * This file is part of fastq-tools.
5 * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
8 * Adjust quality scores by a given offset.
22 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
25 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
27 # define SET_BINARY_MODE(file)
31 static const char* prog_name = "fastq-grep";
36 "fastq-qualadj [OPTION]... OFFSET [FILE]...\n"
37 "The given offset is added to each and every quality score, where\n"
38 "the offset may be negative.\n"
40 " -h, --help print this message\n"
41 " -V, --version output version information and exit\n"
45 void fastq_qualadj(FILE* fin, FILE* fout, int offset)
47 fastq_t* fqf = fastq_create(fin);
48 seq_t* seq = seq_create();
52 while (fastq_read(fqf, seq)) {
53 for (i = 0; i < seq->qual.n; ++i) {
54 c = (int) seq->qual.s[i] + offset;
55 c = c < 0 ? 0 : (c > 126 ? 126: c);
56 seq->qual.s[i] = (char) c;
59 fastq_print(fout, seq);
67 int main(int argc, char* argv[])
69 SET_BINARY_MODE(stdin);
70 SET_BINARY_MODE(stdout);
74 static struct option long_options[] =
76 {"help", no_argument, NULL, 'h'},
77 {"version", no_argument, NULL, 'V'},
88 opt = getopt_long(argc, argv, "hV0:1::2::3::4::5::6::7::8::9::", long_options, &opt_idx);
94 /* this is a bit of a hack to prevent getopt from choking on
95 * negative numbers. */
107 if (optarg) tmplen += strlen(optarg);
108 tmp = malloc(tmplen + 1);
110 if (optarg) snprintf(tmp, tmplen + 1, "-%c%s", (char) opt, optarg);
111 else snprintf(tmp, tmplen + 1, "-%c", (char) opt);
123 print_version(stdout, prog_name);
134 if (offset == 0 && optind >= argc) {
135 fprintf(stderr, "An offset must be specified.\n");
138 else if (offset == 0) offset = atoi(argv[optind++]);
142 if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
143 fastq_qualadj(stdin, stdout, offset);
146 for (; optind < argc; optind++) {
147 fin = fopen(argv[optind], "rb");
149 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
153 fastq_qualadj(fin, stdout, offset);