]> git.donarmstrong.com Git - fastq-tools.git/blob - src/fastq-qualadj.c
b14e8c9097264a5dea22afcd85f2ffe6eab1760a
[fastq-tools.git] / src / fastq-qualadj.c
1
2 /*
3  * This file is part of fastq-tools.
4  *
5  * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
6  *
7  * fastq-qualadj:
8  * Adjust quality scores by a given offset.
9  *
10  */
11
12
13 #include "common.h"
14 #include "parse.h"
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <getopt.h>
19 #include <zlib.h>
20
21
22 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
23 #  include <fcntl.h>
24 #  include <io.h>
25 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
26 #else
27 #  define SET_BINARY_MODE(file)
28 #endif
29
30
31 static const char* prog_name = "fastq-grep";
32
33 void print_help()
34 {
35     fprintf(stdout, 
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"
39 "Options:\n"
40 "  -h, --help              print this message\n"
41 "  -V, --version           output version information and exit\n"
42     );
43 }
44
45 void fastq_qualadj(FILE* fin, FILE* fout, int offset)
46 {
47     fastq_t* fqf = fastq_open(fin);
48     seq_t* seq = fastq_alloc_seq();
49     size_t i;
50     int c;
51
52     while (fastq_next(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;
57         }
58
59         fastq_print(fout, seq);
60     }
61
62     fastq_free_seq(seq);
63     fastq_close(fqf);
64 }
65
66
67 int main(int argc, char* argv[])
68 {
69     SET_BINARY_MODE(stdin);
70     SET_BINARY_MODE(stdout);
71
72     char offset = 0;
73
74     static struct option long_options[] =
75         { 
76           {"help",         no_argument, NULL, 'h'},
77           {"version",      no_argument, NULL, 'V'},
78           {0, 0, 0, 0}
79         };
80
81     int opt;
82     int opt_idx;
83
84     while (1) {
85         opt = getopt_long(argc, argv, "hV", long_options, &opt_idx);
86
87         if (opt == -1) break;
88
89         switch(opt) {
90             case 'h':
91                 print_help();
92                 return 0;
93
94             case 'V':
95                 print_version(stdout, prog_name);
96                 return 0;
97
98             case '?':
99                 return 1;
100
101             default:
102                 abort();
103         }
104     }
105
106     if (optind >= argc) {
107         fprintf(stderr, "An offset must be specified.\n");
108         return 1;
109     }
110
111     offset = atoi(argv[optind++]);
112
113
114     FILE* fin;
115
116     if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
117         fastq_qualadj(stdin, stdout, offset);
118     }
119     else {
120         for (; optind < argc; optind++) {
121             fin = fopen(argv[optind], "rb");
122             if (fin == NULL) {
123                 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
124                 continue;
125             }
126
127             fastq_qualadj(fin, stdout, offset);
128
129             fclose(fin);
130         }
131     }
132
133     return 0;
134 }
135
136