3 * This file is part of fastq-tools.
5 * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
8 * Collect quality score statistics.
20 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
23 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
25 # define SET_BINARY_MODE(file)
28 static const char* prog_name = "fastq-grep";
33 "fastq-qual [OPTION]... [FILE]...\n"
34 "Output a tab-delimnated table such that row i, column j, given \n"
35 "the number of times that quality score i occured in read position j\n\n."
37 " -h, --help print this message\n"
38 " -V, --version output version information and exit\n");
43 void tally_quals(FILE* fin, unsigned int** xs, size_t* n)
45 seq_t* seq = fastq_alloc_seq();
46 fastq_t* fqf = fastq_open(fin);
50 while (fastq_next(fqf, seq)) {
51 if (seq->qual.n > *n) {
52 *xs = realloc_or_die(*xs, 255 * seq->qual.n * sizeof(unsigned int));
53 memset(*xs + *n, 0, 255 * (seq->qual.n - *n) * sizeof(unsigned int));
58 for (i = 0; i < seq->qual.n; ++i) {
59 (*xs)[i * 255 + (int) seq->qual.s[i]] += 1;
70 void print_table(FILE* fout, unsigned int* xs, size_t n)
74 for (j = 0; j < 255; ++j) {
75 fprintf(fout, "%u", xs[j]);
76 for (i = 1; i < n; ++i) {
77 fprintf(fout, "\t%u", xs[i * 255 + j]);
85 int main(int argc, char* argv[])
87 SET_BINARY_MODE(stdin);
88 SET_BINARY_MODE(stdout);
94 static struct option long_options[] =
96 {"help", no_argument, 0, 'h'},
97 {"version", no_argument, 0, 'V'},
102 opt = getopt_long(argc, argv, "hV", long_options, &opt_idx);
104 if( opt == -1 ) break;
112 print_version(stdout, prog_name);
125 unsigned int* xs = NULL;
127 if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
128 tally_quals(stdin, &xs, &n);
131 for (; optind < argc; optind++) {
132 fin = fopen(argv[optind], "rb");
134 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
138 tally_quals(fin, &xs, &n);
142 print_table(stdout, xs, n);