3 * This file is part of fastq-tools.
5 * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
8 * Smith-Waterman alignments against sequences within a fastq file.
21 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
24 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
26 # define SET_BINARY_MODE(file)
30 static const char* prog_name = "fastq-match";
36 "fastq-match [OPTION]... QUERY [FILE]...\n"
37 "Perform Smith-Waterman local alignment of a query sequence\n"
38 "against each sequence in a fastq file.\n\n"
40 " -h, --help print this message\n"
41 " -V, --version output version information and exit\n"
46 void fastq_match(FILE* fin, FILE* fout,
48 unsigned char* query, int n)
52 fastq_t* fqf = fastq_open(fin);
53 seq_t* seq = fastq_alloc_seq();
55 while (fastq_next(fqf, seq)) {
56 fprintf(fout, "%s\t", seq->seq.s);
58 fastq_sw_conv_seq((unsigned char*)seq->seq.s, seq->seq.n);
59 score = fastq_sw(sw, (unsigned char*)seq->seq.s, seq->seq.n);
61 fprintf(fout, "%d\n", score);
70 int main(int argc, char* argv[])
72 SET_BINARY_MODE(stdin);
73 SET_BINARY_MODE(stdout);
85 static struct option long_options[] =
87 {"help", no_argument, NULL, 'h'},
88 {"version", no_argument, NULL, 'V'},
94 opt = getopt_long(argc, argv, "hV", long_options, &opt_idx);
100 if (long_options[opt_idx].flag != 0) break;
110 print_version(stdout, prog_name);
122 if (optind >= argc) {
123 fprintf(stderr, "A query sequence must be specified.\n");
127 query = (unsigned char*)argv[optind++];
128 query_len = strlen((char*)query);
129 fastq_sw_conv_seq(query, query_len);
131 sw = fastq_alloc_sw(query, query_len);
133 if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
134 fastq_match(stdin, stdout, sw, query, query_len);
137 for (; optind < argc; optind++) {
138 fin = fopen(argv[optind], "rb");
140 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
144 fastq_match(fin, stdout, sw, query, query_len);