From bcdcb70445c167236311be383233e8dff4c9d54c Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Tue, 22 Nov 2011 12:40:21 -0800 Subject: [PATCH] Added a program to adjust quality scores by a fixed offset. --- README.md | 1 + doc/fastq-qualadj.1 | 28 +++++++++ src/Makefile.am | 4 +- src/fastq-grep.c | 2 +- src/fastq-qualadj.c | 136 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 doc/fastq-qualadj.1 create mode 100644 src/fastq-qualadj.c diff --git a/README.md b/README.md index 75a28d3..c906677 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ information. * *fastq-uniq* : count duplicate reads +* *fastq-qualadj* : adjust quality scores by a fixed offset install diff --git a/doc/fastq-qualadj.1 b/doc/fastq-qualadj.1 new file mode 100644 index 0000000..8945d37 --- /dev/null +++ b/doc/fastq-qualadj.1 @@ -0,0 +1,28 @@ +.TH FASTQ-GREP 1 + +.SH NAME +fastq-qualadj - adjust quality scores by a fixed offset + +.SH SYNOPSIS +.B fastq-grep [OPTION]... OFFSET [FILE]... + +.SH DESCRIPTION +The given offset is added to each and every quality score, where the offset may +be negative. This helps in converting from one scale to another. For example, to +convert reads in Phred+64 (Illumina 1.3) scale to the Phred+33 scale, on offset +of -31 can be specified. + +One ore more FILEs may be specified, otherwise input is read from standard input. +Input files may be gziped. + +.SH OPTIONS +.TP +\fB\-h\fR, \fB\-\-help\fR +Output a help message and exit. +.TP +\fB\-V\fR, \fB\-\-version\fR +Output version information and exit. + +.SH AUTHOR +Written by Daniel C. Jones + diff --git a/src/Makefile.am b/src/Makefile.am index 7bc48d0..84707f2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ -bin_PROGRAMS = fastq-grep fastq-kmers fastq-match fastq-uniq fastq-qual fastq-sample +bin_PROGRAMS = fastq-grep fastq-kmers fastq-match fastq-uniq fastq-qual fastq-sample fastq-qualadj fastq_common_src=common.h common.c fastq_parse_src=parse.h parse.c @@ -25,3 +25,5 @@ fastq_qual_LDADD = -lz fastq_sample_SOURCES = fastq-sample.c $(fastq_common_src) $(fastq_parse_src) $(fastq_rng_src) fastq_sample_LDADD = -lz +fastq_qualadj_SOURCES = fastq-qualadj.c $(fastq_common_src) $(fastq_parse_src) +fastq_qualadj_LDADD = -lz diff --git a/src/fastq-grep.c b/src/fastq-grep.c index 7c9e3bf..994ab58 100644 --- a/src/fastq-grep.c +++ b/src/fastq-grep.c @@ -120,7 +120,7 @@ int main(int argc, char* argv[]) while (1) { opt = getopt_long(argc, argv, "ivchV", long_options, &opt_idx); - if( opt == -1 ) break; + if (opt == -1) break; switch (opt) { case 0: diff --git a/src/fastq-qualadj.c b/src/fastq-qualadj.c new file mode 100644 index 0000000..b14e8c9 --- /dev/null +++ b/src/fastq-qualadj.c @@ -0,0 +1,136 @@ + +/* + * This file is part of fastq-tools. + * + * Copyright (c) 2011 by Daniel C. Jones + * + * fastq-qualadj: + * Adjust quality scores by a given offset. + * + */ + + +#include "common.h" +#include "parse.h" +#include +#include +#include +#include +#include + + +#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) +# include +# include +# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +#else +# define SET_BINARY_MODE(file) +#endif + + +static const char* prog_name = "fastq-grep"; + +void print_help() +{ + fprintf(stdout, +"fastq-qualadj [OPTION]... OFFSET [FILE]...\n" +"The given offset is added to each and every quality score, where\n" +"the offset may be negative.\n" +"Options:\n" +" -h, --help print this message\n" +" -V, --version output version information and exit\n" + ); +} + +void fastq_qualadj(FILE* fin, FILE* fout, int offset) +{ + fastq_t* fqf = fastq_open(fin); + seq_t* seq = fastq_alloc_seq(); + size_t i; + int c; + + while (fastq_next(fqf, seq)) { + for (i = 0; i < seq->qual.n; ++i) { + c = (int) seq->qual.s[i] - offset; + c = c < 0 ? 0 : (c > 126 ? 126: c); + seq->qual.s[i] = (char) c; + } + + fastq_print(fout, seq); + } + + fastq_free_seq(seq); + fastq_close(fqf); +} + + +int main(int argc, char* argv[]) +{ + SET_BINARY_MODE(stdin); + SET_BINARY_MODE(stdout); + + char offset = 0; + + static struct option long_options[] = + { + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'V'}, + {0, 0, 0, 0} + }; + + int opt; + int opt_idx; + + while (1) { + opt = getopt_long(argc, argv, "hV", long_options, &opt_idx); + + if (opt == -1) break; + + switch(opt) { + case 'h': + print_help(); + return 0; + + case 'V': + print_version(stdout, prog_name); + return 0; + + case '?': + return 1; + + default: + abort(); + } + } + + if (optind >= argc) { + fprintf(stderr, "An offset must be specified.\n"); + return 1; + } + + offset = atoi(argv[optind++]); + + + FILE* fin; + + if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) { + fastq_qualadj(stdin, stdout, offset); + } + else { + for (; optind < argc; optind++) { + fin = fopen(argv[optind], "rb"); + if (fin == NULL) { + fprintf(stderr, "No such file '%s'.\n", argv[optind]); + continue; + } + + fastq_qualadj(fin, stdout, offset); + + fclose(fin); + } + } + + return 0; +} + + -- 2.39.2