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