]> git.donarmstrong.com Git - fastq-tools.git/blob - src/fastq-match.c
Much simpler faster code for parsing fastq files.
[fastq-tools.git] / src / fastq-match.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-match :
8  * Smith-Waterman alignments against sequences within a fastq file.
9  *
10  */
11
12
13 #include "common.h"
14 #include "parse.h"
15 #include "sw.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include <zlib.h>
19 #include <getopt.h>
20
21 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
22 #  include <fcntl.h>
23 #  include <io.h>
24 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
25 #else
26 #  define SET_BINARY_MODE(file)
27 #endif
28
29
30 static const char* prog_name = "fastq-match";
31
32
33 void print_help()
34 {
35     fprintf(stdout, 
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"
39 "Options:\n"
40 "  -h, --help              print this message\n"
41 "  -V, --version           output version information and exit\n"
42     );
43 }
44
45
46 void fastq_match(FILE* fin, FILE* fout, sw_t* sw)
47 {
48     int score;
49
50     fastq_t* fqf = fastq_create(fin);
51     seq_t* seq = seq_create();
52
53     while (fastq_read(fqf, seq)) {
54         fprintf(fout, "%s\t", seq->seq.s);
55
56         fastq_sw_conv_seq((unsigned char*)seq->seq.s, seq->seq.n);
57         score = fastq_sw(sw, (unsigned char*)seq->seq.s, seq->seq.n);
58
59         fprintf(fout, "%d\n", score);
60     }
61
62     seq_free(seq);
63     fastq_free(fqf);
64 }
65
66
67
68 int main(int argc, char* argv[])
69 {
70     SET_BINARY_MODE(stdin);
71     SET_BINARY_MODE(stdout);
72
73     unsigned char* query;
74     int query_len;
75
76     sw_t* sw;
77
78     FILE*  fin;
79
80     int opt;
81     int opt_idx;
82
83     static struct option long_options[] =
84         { 
85           {"help",       no_argument,       NULL, 'h'},
86           {"version",    no_argument,       NULL, 'V'},
87           {0, 0, 0, 0}
88         };
89
90
91     while (1) {
92         opt = getopt_long(argc, argv, "hV", long_options, &opt_idx);
93
94         if (opt == -1) break;
95
96         switch (opt) {
97             case 0:
98                 if (long_options[opt_idx].flag != 0) break;
99                 if (optarg) {
100                 }
101                 break;
102
103             case 'h':
104                 print_help();
105                 return 0;
106
107             case 'V':
108                 print_version(stdout, prog_name);
109                 return 0;
110
111             case '?':
112                 return 1;
113
114             default:
115                 abort();
116         }
117     }
118
119
120     if (optind >= argc) {
121         fprintf(stderr, "A query sequence must be specified.\n");
122         return 1;
123     }
124
125     query = (unsigned char*)argv[optind++];
126     query_len = strlen((char*)query);
127     fastq_sw_conv_seq(query, query_len);
128
129     sw = fastq_alloc_sw(query, query_len);
130
131     if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
132         fastq_match(stdin, stdout, sw);
133     }
134     else {
135         for (; optind < argc; optind++) {
136             fin = fopen(argv[optind], "rb");
137             if (fin == NULL) {
138                 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
139                 continue;
140             }
141
142             fastq_match(fin, stdout, sw);
143         }
144     }
145
146     fastq_free_sw(sw);
147
148     return 0;
149 }
150
151
152