3 * This file is part of fastq-tools.
5 * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
8 * Sample reads with or without replacement from 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)
29 static const char* prog_name = "fastq-sample";
31 static int replacement_flag;
37 "fastq-sample [OPTION]... [FILE [FILE2]]\n"
38 "Sample random reads from a FASTQ file."
40 " -n N the number of reads to sample (default: 10000)\n"
41 " -p N the proportion of the total reads to sample\n"
42 " -o, --output=PREFIX output file prefix\n"
43 " -r, --with-replacement sample with relpacement\n"
44 " -h, --help print this message\n"
45 " -V, --version output version information and exit\n"
50 /* count the number of entries in a fastq file */
51 unsigned long count_entries(fastq_t* fqf)
53 seq_t* seq = fastq_alloc_seq();
55 while (fastq_next(fqf, seq)) ++n;
62 /* compare two unsigned integers (for qsort) */
63 int cmpul( const void* a, const void* b ) {
64 if( *(unsigned long*) a < *(unsigned long*) b ) return -1;
65 else if( *(unsigned long*) a > *(unsigned long*) b ) return 1;
70 /* randomly shuffle an array of unsigned longs */
71 void shuffle(rng_t* rng, unsigned long* xs, unsigned long n)
73 unsigned long i, j, k;
74 for (i = n - 1; i > 0; --i) {
75 j = fastq_rng_uniform_int(rng, i + 1);
76 k = xs[j]; xs[j] = xs[i]; xs[i] = k; // swap
81 unsigned long* index_with_replacement(rng_t* rng, unsigned long n, unsigned long k)
83 unsigned long* xs = malloc_or_die(k * sizeof(unsigned long));
85 for (i = 0; i < k; ++i) xs[i] = fastq_rng_uniform_int(rng, n);
90 unsigned long* index_without_replacement(rng_t* rng, unsigned long n)
92 unsigned long* xs = malloc_or_die(n * sizeof(unsigned long));
94 for (i = 0; i < n; ++i) xs[i] = i;
100 void fastq_sample(const char* prefix, FILE* file1, FILE* file2, unsigned long k, double p)
103 * The basic idea is this:
105 * 1. Count the number of lines in the file, n.
107 * 2a. If sampling with replacement, generate k random integers in [0, n-1].
109 * 2b. If sampling without replacement, generate a list of integers 0..(n-1),
110 * shuffle with fisher-yates, then consider the first k.
112 * 3. Sort the integer list.
114 * 3. Read through the file again, when the number at the front of the integer
115 * list matches the index of the fastq etry, print the entry, and pop the
122 fastq_t* f1 = fastq_open(file1);
123 fastq_t* f2 = file2 == NULL ? NULL : fastq_open(file2);
125 n = count_entries(f1);
127 n2 = count_entries(f2);
129 fprintf(stderr, "Input files have differing numbers of entries (%lu != %lu).\n", n, n2);
135 if (f2 != NULL) fastq_rewind(f2);
138 k = (unsigned long) (p * (double) n);
139 if (!replacement_flag && k > n) k = n;
142 rng_t* rng = fastq_rng_alloc();
145 if (replacement_flag) xs = index_with_replacement(rng, n, k);
146 else xs = index_without_replacement(rng, n);
148 qsort(xs, k, sizeof(unsigned long), cmpul);
158 output_len = strlen(prefix) + 7;
159 output_name = malloc_or_die((output_len + 1) * sizeof(char));
161 snprintf(output_name, output_len, "%s.fastq", prefix);
162 fout1 = fopen(output_name, "wb");
164 fprintf(stderr, "Cannot open file %s for writing.\n", output_name);
173 output_len = strlen(prefix) + 9;
174 output_name = malloc_or_die((output_len + 1) * sizeof(char));
176 snprintf(output_name, output_len, "%s.1.fastq", prefix);
177 fout1 = fopen(output_name, "wb");
179 fprintf(stderr, "Cannot open file %s for writing.\n", output_name);
183 snprintf(output_name, output_len, "%s.2.fastq", prefix);
184 fout2 = fopen(output_name, "wb");
186 fprintf(stderr, "Cannot open file %s for writing.\n", output_name);
195 unsigned long i = 0; // read number
196 unsigned long j = 0; // index into xs
199 seq_t* seq1 = fastq_alloc_seq();
200 seq_t* seq2 = fastq_alloc_seq();
202 while (j < k && fastq_next(f1, seq1)) {
204 ret = fastq_next(f2, seq2);
206 fputs("Input files have differing numbers of entries.\n", stderr);
211 while (j < k && xs[j] == i) {
212 fastq_print(fout1, seq1);
213 if (f2 != NULL) fastq_print(fout2, seq2);
220 fastq_free_seq(seq1);
221 fastq_free_seq(seq2);
223 if (f2 != NULL) fastq_close(f2);
226 if (fout2 != NULL) fclose(fout2);
233 int main(int argc, char* argv[])
235 SET_BINARY_MODE(stdin);
236 SET_BINARY_MODE(stdout);
241 const char* prefix = NULL;
242 unsigned long k = 10000; // number of reads to sample
243 double p = -1; // proportion of reads to sample
246 static struct option long_options[] =
248 {"with-replacement", no_argument, NULL, 'r'},
249 {"output", no_argument, NULL, 'o'},
250 {"help", no_argument, NULL, 'h'},
251 {"version", no_argument, NULL, 'V'},
255 replacement_flag = 0;
258 opt = getopt_long(argc, argv, "n:p:o:rhV", long_options, &opt_idx);
260 if( opt == -1 ) break;
264 if (long_options[opt_idx].flag != 0) break;
270 k = strtoul(optarg, NULL, 10);
276 fputs("Sample proportion ('-p') is less than zero.\n", stderr);
282 replacement_flag = 1;
294 print_version(stdout, prog_name);
308 char* prefix_alloc = NULL;
310 if (optind >= argc) {
314 if (strcmp(argv[optind], "-") == 0) {
316 if (prefix == NULL) prefix = "stdin.sample";
319 file1 = fopen(argv[optind], "rb");
321 fprintf(stderr, "Cannot open '%s' for reading.\n", argv[optind]);
325 if (prefix == NULL) {
326 /* guess at a reasonable output refix by trimming the
327 * trailing file extension, if any. */
331 tmp = strrchr(argv[optind], '/');
332 if (tmp != NULL) argv[optind] = tmp + 1;
334 /* exclude file suffixes */
335 tmp = strchr(argv[optind], '.');
336 if (tmp == NULL) prefix = argv[optind];
338 prefix_alloc = malloc_or_die((tmp - argv[optind] + 1) * sizeof(char));
339 memcpy(prefix_alloc, argv[optind], (tmp - argv[optind]) * sizeof(char));
340 prefix_alloc[tmp - argv[optind]] = '\0';
341 prefix = prefix_alloc;
348 if (strcmp(argv[optind], "-") == 0) {
349 if (file1 == stdin) {
350 fprintf(stderr, "Both input files cannot be standard input.\n");
356 file2 = fopen(argv[optind], "rb");
358 fprintf(stderr, "Cannot open '%s' for reading.\n", argv[optind]);
365 fastq_sample(prefix, file1, file2, k, p);