]> git.donarmstrong.com Git - fastq-tools.git/blob - src/fastq-match.c
reimplemented smith-waterman
[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 "fastq-common.h"
14 #include "fastq-parse.h"
15 #include "fastq-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 int help_flag;
31
32
33 void print_help()
34 {
35     fprintf( stderr, 
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     );
42 }
43
44
45
46
47 void fastq_match(FILE* fin, FILE* fout,
48                  sw_t* sw,
49                  unsigned char* query, int n)
50 {
51     int score;
52
53     fastq_t* fqf = fastq_open(fin);
54     seq_t* seq = fastq_alloc_seq();
55
56     while (fastq_next(fqf, seq)) {
57         fprintf(fout, "%s\t", seq->seq.s);
58
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);
61
62         fprintf(fout, "%d\n", score);
63     }
64
65     fastq_free_seq(seq);
66     fastq_close(fqf);
67 }
68
69
70
71 int main(int argc, char* argv[])
72 {
73     SET_BINARY_MODE(stdin);
74     SET_BINARY_MODE(stdout);
75
76     unsigned char* query;
77     int query_len;
78
79     sw_t* sw;
80
81     FILE*  fin;
82
83     help_flag = 0;
84
85     int opt;
86     int opt_idx;
87
88     static struct option long_options[] =
89         { 
90           {"help",       no_argument, &help_flag, 1},
91           {"gap-init",   required_argument, NULL, 0},
92           {"gap-extend", required_argument, NULL, 0},
93           {0, 0, 0, 0}
94         };
95
96
97     while (1) {
98         opt = getopt_long(argc, argv, "h", long_options, &opt_idx);
99
100         if( opt == -1 ) break;
101
102         switch (opt) {
103             case 0:
104                 if (long_options[opt_idx].flag != 0) break;
105                 if (optarg) {
106                 }
107                 break;
108
109             case 'h':
110                 help_flag = 1;
111                 break;
112
113             case '?':
114                 return 1;
115
116             default:
117                 abort();
118         }
119     }
120
121     if (help_flag) {
122         print_help();
123         return 0;
124     }
125
126     if (optind >= argc) {
127         fprintf(stderr, "A query sequence must be specified.\n");
128         return 1;
129     }
130
131     query = (unsigned char*)argv[optind++];
132     query_len = strlen((char*)query);
133     fastq_sw_conv_seq(query, query_len);
134
135     sw = fastq_alloc_sw(query, query_len);
136
137     if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
138         fastq_match(stdin, stdout, sw, query, query_len);
139     }
140     else {
141         for (; optind < argc; optind++) {
142             fin = fopen(argv[optind], "rb");
143             if (fin == NULL) {
144                 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
145                 continue;
146             }
147
148             fastq_match(fin, stdout, sw, query, query_len);
149         }
150     }
151
152     fastq_free_sw(sw);
153
154     return 0;
155 }
156
157
158