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)
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"
47 void fastq_match(FILE* fin, FILE* fout,
49 unsigned char* query, int n)
53 fastq_t* fqf = fastq_open(fin);
54 seq_t* seq = fastq_alloc_seq();
56 while (fastq_next(fqf, seq)) {
57 fprintf(fout, "%s\t", seq->seq.s);
59 fastq_sw_conv_seq((unsigned char*)seq->seq.s, seq->seq.n);
60 score = fastq_sw(sw, (unsigned char*)seq->seq.s, seq->seq.n);
62 fprintf(fout, "%d\n", score);
71 int main(int argc, char* argv[])
73 SET_BINARY_MODE(stdin);
74 SET_BINARY_MODE(stdout);
88 static struct option long_options[] =
90 {"help", no_argument, &help_flag, 1},
91 {"gap-init", required_argument, NULL, 0},
92 {"gap-extend", required_argument, NULL, 0},
98 opt = getopt_long(argc, argv, "h", long_options, &opt_idx);
100 if (opt == -1) break;
104 if (long_options[opt_idx].flag != 0) break;
126 if (optind >= argc) {
127 fprintf(stderr, "A query sequence must be specified.\n");
131 query = (unsigned char*)argv[optind++];
132 query_len = strlen((char*)query);
133 fastq_sw_conv_seq(query, query_len);
135 sw = fastq_alloc_sw(query, query_len);
137 if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
138 fastq_match(stdin, stdout, sw, query, query_len);
141 for (; optind < argc; optind++) {
142 fin = fopen(argv[optind], "rb");
144 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
148 fastq_match(fin, stdout, sw, query, query_len);