Source code

Download source archive ↓

upstream-dependencies/seqtk/seqtk.c

1/* The MIT License
2
3   Copyright (c) 2018-     Dana-Farber Cancer Institute
4                 2008-2018 Broad Institute
5
6   Permission is hereby granted, free of charge, to any person obtaining
7   a copy of this software and associated documentation files (the
8   "Software"), to deal in the Software without restriction, including
9   without limitation the rights to use, copy, modify, merge, publish,
10   distribute, sublicense, and/or sell copies of the Software, and to
11   permit persons to whom the Software is furnished to do so, subject to
12   the following conditions:
13
14   The above copyright notice and this permission notice shall be
15   included in all copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   SOFTWARE.
25*/
26
27#include <stdio.h>
28#include <ctype.h>
29#include <stdlib.h>
30#include <stdint.h>
31#include <inttypes.h>
32#include <zlib.h>
33#include <string.h>
34#include <unistd.h>
35#include <limits.h>
36#include <assert.h>
37#include <math.h>
38
39#include "kseq.h"
40KSEQ_INIT(gzFile, gzread)
41
42typedef struct {
43	int n, m;
44	uint64_t *a; // of size m * 2, (beg, end) pair
45	int8_t *rev;
46} reglist_t;
47
48#include "khash.h"
49KHASH_MAP_INIT_STR(reg, reglist_t)
50KHASH_SET_INIT_INT64(64)
51
52typedef kh_reg_t reghash_t;
53
54reghash_t *stk_reg_read_alt(const char *fn)
55{
56	reghash_t *h = kh_init(reg);
57	gzFile fp;
58	kstream_t *ks;
59	int dret;
60	kstring_t str = {0,0,0};
61
62	fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
63	if (fp == 0) return 0;
64	ks = ks_init(fp);
65	while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
66		int64_t beg = -1, end = -1;
67		int i, c, rev = 0;
68		char *p, *q;
69		reglist_t *r;
70		khint_t k;
71		for (i = 0, p = q = str.s;; ++p) {
72			if (*p == '\t' || *p == '\0') {
73				c = *p;
74				*p = 0;
75				if (i == 1) {
76					if (isdigit(*q)) beg = strtol(q, &q, 10);
77					if (q != p) beg = -1;
78				} else if (i == 2) {
79					if (isdigit(*q)) end = strtol(q, &q, 10);
80					if (q != p) end = -1;
81				} else if (i == 5) {
82					if (*q == '+') rev = 1;
83					else if (*q == '-') rev = -1;
84				}
85				++i, q = p + 1;
86				if (c == 0) break;
87			}
88		}
89		if (i == 0) continue;
90		k = kh_get(reg, h, str.s);
91		if (k == kh_end(h)) {
92			int ret;
93			char *s = strdup(str.s);
94			k = kh_put(reg, h, s, &ret);
95			memset(&kh_val(h, k), 0, sizeof(reglist_t));
96		}
97		r = &kh_val(h, k);
98		if (end < 0 && beg > 0) end = beg, beg = beg - 1; // if there is only one column
99		if (beg < 0) beg = 0, end = INT64_MAX;
100		if (r->n == r->m) {
101			r->m = r->m? r->m<<1 : 4;
102			r->a = (uint64_t*)realloc(r->a, r->m * 16);
103			r->rev = (int8_t*)realloc(r->rev, r->m);
104		}
105		r->a[r->n<<1] = (uint64_t)beg;
106		r->a[r->n<<1|1] = (uint64_t)end;
107		r->rev[r->n++] = rev;
108	}
109	ks_destroy(ks);
110	gzclose(fp);
111	free(str.s);
112	return h;
113}
114
115reghash_t *stk_reg_read(const char *fn)
116{
117	reghash_t *h = kh_init(reg);
118	gzFile fp;
119	kstream_t *ks;
120	int dret;
121	kstring_t *str;
122	// read the list
123	fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
124	if (fp == 0) return 0;
125	ks = ks_init(fp);
126	str = calloc(1, sizeof(kstring_t));
127	while (ks_getuntil(ks, 0, str, &dret) >= 0) {
128		int64_t beg = -1, end = -1;
129		reglist_t *p;
130		khint_t k = kh_get(reg, h, str->s);
131		if (k == kh_end(h)) {
132			int ret;
133			char *s = strdup(str->s);
134			k = kh_put(reg, h, s, &ret);
135			memset(&kh_val(h, k), 0, sizeof(reglist_t));
136		}
137		p = &kh_val(h, k);
138		if (dret != '\n') {
139			if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
140				beg = atol(str->s);
141				if (dret != '\n') {
142					if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
143						end = atol(str->s);
144						if (end < 0) end = -1;
145					}
146				}
147			}
148		}
149		// skip the rest of the line
150		if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
151		if (end < 0 && beg > 0) end = beg, beg = beg - 1; // if there is only one column
152		if (beg < 0) beg = 0, end = INT64_MAX;
153		if (p->n == p->m) {
154			p->m = p->m? p->m<<1 : 4;
155			p->a = realloc(p->a, p->m * 16);
156		}
157		p->a[p->n<<1] = (uint64_t)beg;
158		p->a[p->n<<1|1] = (uint64_t)end;
159		p->n++;
160	}
161	ks_destroy(ks);
162	gzclose(fp);
163	free(str->s); free(str);
164	return h;
165}
166
167void stk_reg_destroy(reghash_t *h)
168{
169	khint_t k;
170	if (h == 0) return;
171	for (k = 0; k < kh_end(h); ++k) {
172		if (kh_exist(h, k)) {
173			free(kh_val(h, k).a);
174			free(kh_val(h, k).rev);
175			free((char*)kh_key(h, k));
176		}
177	}
178	kh_destroy(reg, h);
179}
180
181/* constant table */
182
183unsigned char seq_nt16_table[256] = {
184	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
185	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
186	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15 /*'-'*/,15,15,
187	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
188	15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
189	15,15, 5, 6,  8,15, 7, 9,  0,10,15,15, 15,15,15,15,
190	15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
191	15,15, 5, 6,  8,15, 7, 9,  0,10,15,15, 15,15,15,15,
192	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
193	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
194	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
195	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
196	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
197	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
198	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
199	15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15
200};
201
202unsigned char seq_nt6_table[256] = {
203    0, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
204    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
205    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
206    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
207    5, 1, 5, 2,  5, 5, 5, 3,  5, 5, 5, 5,  5, 5, 5, 5,
208    5, 5, 5, 5,  4, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
209    5, 1, 5, 2,  5, 5, 5, 3,  5, 5, 5, 5,  5, 5, 5, 5,
210    5, 5, 5, 5,  4, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
211
212    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
213    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
214    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
215    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
216    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
217    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
218    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,
219    5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5,  5, 5, 5, 5
220};
221
222char *seq_nt16_rev_table = "XACMGRSVTWYHKDBN";
223unsigned char seq_nt16to4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
224unsigned char seq_nt16comp_table[] = { 0, 8, 4, 12, 2, 10, 9, 14, 1, 6, 5, 13, 3, 11, 7, 15 };
225int bitcnt_table[] = { 4, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
226char comp_tab[] = {
227	  0,   1,	2,	 3,	  4,   5,	6,	 7,	  8,   9,  10,	11,	 12,  13,  14,	15,
228	 16,  17,  18,	19,	 20,  21,  22,	23,	 24,  25,  26,	27,	 28,  29,  30,	31,
229	 32,  33,  34,	35,	 36,  37,  38,	39,	 40,  41,  42,	43,	 44,  45,  46,	47,
230	 48,  49,  50,	51,	 52,  53,  54,	55,	 56,  57,  58,	59,	 60,  61,  62,	63,
231	 64, 'T', 'V', 'G', 'H', 'E', 'F', 'C', 'D', 'I', 'J', 'M', 'L', 'K', 'N', 'O',
232	'P', 'Q', 'Y', 'S', 'A', 'A', 'B', 'W', 'X', 'R', 'Z',	91,	 92,  93,  94,	95,
233	 64, 't', 'v', 'g', 'h', 'e', 'f', 'c', 'd', 'i', 'j', 'm', 'l', 'k', 'n', 'o',
234	'p', 'q', 'y', 's', 'a', 'a', 'b', 'w', 'x', 'r', 'z', 123, 124, 125, 126, 127
235};
236
237static void stk_printstr(FILE *fp, const kstring_t *s, unsigned line_len)
238{
239	if (line_len != UINT_MAX && line_len != 0) {
240		int64_t i, rest = s->l;
241		for (i = 0; i < s->l; i += line_len, rest -= line_len) {
242			fputc('\n', fp);
243			if (rest > line_len) fwrite(s->s + i, 1, line_len, fp);
244			else fwrite(s->s + i, 1, rest, fp);
245		}
246		fputc('\n', fp);
247	} else {
248		fputc('\n', fp);
249		fputs(s->s, fp);
250		fputc('\n', fp);
251	}
252}
253
254static inline void stk_printseq_renamed(FILE *fp, const kseq_t *s, int line_len, const char *prefix, int64_t n)
255{
256	fputc(s->qual.l? '@' : '>', fp);
257	if (n >= 0) {
258		if (prefix) fputs(prefix, fp);
259		fprintf(fp, "%lld", (long long)n);
260	} else fputs(s->name.s, fp);
261	if (s->comment.l) {
262		fputc(' ', fp); fputs(s->comment.s, fp);
263	}
264	stk_printstr(fp, &s->seq, line_len);
265	if (s->qual.l) {
266		fputc('+', fp);
267		stk_printstr(fp, &s->qual, line_len);
268	}
269}
270
271static inline void stk_printseq_suffix(FILE *fp, const kseq_t *s, int line_len, const char *suffix)
272{
273	fputc(s->qual.l? '@' : '>', fp);
274	fputs(s->name.s, fp);
275	if (suffix) fputs(suffix, fp);
276	if (s->comment.l) {
277		fputc(' ', fp); fputs(s->comment.s, fp);
278	}
279	stk_printstr(fp, &s->seq, line_len);
280	if (s->qual.l) {
281		fputc('+', fp);
282		stk_printstr(fp, &s->qual, line_len);
283	}
284}
285
286static inline void stk_printseq(FILE *fp, const kseq_t *s, int line_len)
287{
288	stk_printseq_renamed(fp, s, line_len, 0, -1);
289}
290
291/* 
292   64-bit Mersenne Twister pseudorandom number generator. Adapted from:
293
294     http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
295
296   which was written by Takuji Nishimura and Makoto Matsumoto and released
297   under the 3-clause BSD license.
298*/
299
300typedef uint64_t krint64_t;
301
302struct _krand_t;
303typedef struct _krand_t krand_t;
304
305#define KR_NN 312
306#define KR_MM 156
307#define KR_UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
308#define KR_LM 0x7FFFFFFFULL /* Least significant 31 bits */
309
310struct _krand_t {
311	int mti;
312	krint64_t mt[KR_NN];
313};
314
315static void kr_srand0(krint64_t seed, krand_t *kr)
316{
317	kr->mt[0] = seed;
318	for (kr->mti = 1; kr->mti < KR_NN; ++kr->mti) 
319		kr->mt[kr->mti] = 6364136223846793005ULL * (kr->mt[kr->mti - 1] ^ (kr->mt[kr->mti - 1] >> 62)) + kr->mti;
320}
321
322krand_t *kr_srand(krint64_t seed)
323{
324	krand_t *kr;
325	kr = malloc(sizeof(krand_t));
326	kr_srand0(seed, kr);
327	return kr;
328}
329
330krint64_t kr_rand(krand_t *kr)
331{
332	krint64_t x;
333	static const krint64_t mag01[2] = { 0, 0xB5026F5AA96619E9ULL };
334	if (kr->mti >= KR_NN) {
335		int i;
336		if (kr->mti == KR_NN + 1) kr_srand0(5489ULL, kr);
337		for (i = 0; i < KR_NN - KR_MM; ++i) {
338			x = (kr->mt[i] & KR_UM) | (kr->mt[i+1] & KR_LM);
339			kr->mt[i] = kr->mt[i + KR_MM] ^ (x>>1) ^ mag01[(int)(x&1)];
340		}
341		for (; i < KR_NN - 1; ++i) {
342			x = (kr->mt[i] & KR_UM) | (kr->mt[i+1] & KR_LM);
343			kr->mt[i] = kr->mt[i + (KR_MM - KR_NN)] ^ (x>>1) ^ mag01[(int)(x&1)];
344		}
345		x = (kr->mt[KR_NN - 1] & KR_UM) | (kr->mt[0] & KR_LM);
346		kr->mt[KR_NN - 1] = kr->mt[KR_MM - 1] ^ (x>>1) ^ mag01[(int)(x&1)];
347		kr->mti = 0;
348	}
349	x = kr->mt[kr->mti++];
350	x ^= (x >> 29) & 0x5555555555555555ULL;
351	x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
352	x ^= (x << 37) & 0xFFF7EEE000000000ULL;
353	x ^= (x >> 43);
354	return x;
355}
356
357#define kr_drand(_kr) ((kr_rand(_kr) >> 11) * (1.0/9007199254740992.0))
358
359
360/* quality based trimming with Mott's algorithm */
361int stk_trimfq(int argc, char *argv[])
362{ // FIXME: when a record with zero length will always be treated as a fasta record
363	gzFile fp;
364	kseq_t *seq;
365	double param = 0.05, q_int2real[128];
366	int c, min_len = 30, left = 0, right = 0, fixed_len = -1;
367	int64_t i;
368	while ((c = getopt(argc, argv, "l:q:b:e:L:")) >= 0) {
369		switch (c) {
370			case 'q': param = atof(optarg); break;
371			case 'l': min_len = atoi(optarg); break;
372			case 'b': left = atoi(optarg); break;
373			case 'e': right = atoi(optarg); break;
374			case 'L': fixed_len = atoi(optarg); break;
375		}
376	}
377	if (optind == argc) {
378		fprintf(stderr, "\n");
379		fprintf(stderr, "Usage:   seqtk trimfq [options] <in.fq>\n\n");
380		fprintf(stderr, "Options: -q FLOAT    error rate threshold (disabled by -b/-e) [%.2f]\n", param);
381		fprintf(stderr, "         -l INT      maximally trim down to INT bp (disabled by -b/-e) [%d]\n", min_len);
382		fprintf(stderr, "         -b INT      trim INT bp from left (non-zero to disable -q/-l) [0]\n");
383		fprintf(stderr, "         -e INT      trim INT bp from right (non-zero to disable -q/-l) [0]\n");
384		fprintf(stderr, "         -L INT      retain at most INT bp from the 5'-end (non-zero to disable -q/-l) [0]\n");
385		fprintf(stderr, "         -Q          force FASTQ output\n");
386		fprintf(stderr, "\n");
387		return 1;
388	}
389	fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
390	if (fp == 0) {
391		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
392		return 1;
393	}
394	seq = kseq_init(fp);
395	for (i = 0; i < 128; ++i)
396		q_int2real[i] = pow(10., -(i - 33) / 10.);
397	while (kseq_read(seq) >= 0) {
398		int64_t beg, tmp, end;
399		double s, max;
400		if (left || right || fixed_len > 0) {
401			beg = left; end = seq->seq.l - right;
402			if (beg >= end) beg = end = 0;
403			if (fixed_len > 0 && end - beg > fixed_len) end = beg + fixed_len;
404		} else if (seq->qual.l > min_len) {
405			for (i = 0, beg = tmp = 0, end = seq->qual.l, s = max = 0.; i < seq->qual.l; ++i) {
406				int q = seq->qual.s[i];
407				if (q < 36) q = 36;
408				if (q > 127) q = 127;
409				s += param - q_int2real[q];
410				if (s > max) max = s, beg = tmp, end = i + 1;
411				if (s < 0) s = 0, tmp = i + 1;
412			}
413
414			/* max never set; all low qual, just give first min_len bp */
415			if (max == 0.) beg = 0, end = min_len;
416
417			if (end - beg < min_len) { // window-based 
418				int is, imax;
419				for (i = 0, is = 0; i < min_len; ++i)
420					is += seq->qual.s[i] - 33;
421				for (imax = is, beg = 0; i < seq->qual.l; ++i) {
422					is += (int)seq->qual.s[i] - seq->qual.s[i - min_len];
423					if (imax < is) imax = is, beg = i - min_len + 1;
424				}
425				end = beg + min_len;
426			}
427		} else beg = 0, end = seq->seq.l;
428		putchar(seq->is_fastq? '@' : '>'); fputs(seq->name.s, stdout); 
429		if (seq->comment.l) {
430			putchar(' '); puts(seq->comment.s);
431		} else putchar('\n');
432		fwrite(seq->seq.s + beg, 1, end - beg, stdout); putchar('\n');
433		if (seq->is_fastq) {
434			puts("+");
435			fwrite(seq->qual.s + beg, 1, end - beg, stdout); putchar('\n');
436		}
437	}
438	kseq_destroy(seq);
439	gzclose(fp);
440	return 0;
441}
442
443/* composition */
444int stk_comp(int argc, char *argv[])
445{
446	gzFile fp;
447	kseq_t *seq;
448	int64_t l;
449	int c, upper_only = 0;
450	reghash_t *h = 0;
451	reglist_t dummy;
452
453	while ((c = getopt(argc, argv, "ur:")) >= 0) {
454		switch (c) {
455			case 'u': upper_only = 1; break;
456			case 'r': h = stk_reg_read(optarg); break;
457		}
458	}
459	if (argc == optind && isatty(fileno(stdin))) {
460		fprintf(stderr, "Usage:  seqtk comp [-u] [-r in.bed] <in.fa>\n\n");
461		fprintf(stderr, "Output format: chr, length, #A, #C, #G, #T, #2, #3, #4, #CpG, #tv, #ts, #CpG-ts\n");
462		return 1;
463	}
464	fp = optind < argc && strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
465	if (fp == 0) {
466		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
467		return 1;
468	}
469	seq = kseq_init(fp);
470	dummy.n = dummy.m = 1; dummy.a = calloc(1, 16);
471	while ((l = kseq_read(seq)) >= 0) {
472		int64_t i;
473		int k;
474		reglist_t *p = 0;
475		if (h) {
476			khint_t k = kh_get(reg, h, seq->name.s);
477			if (k != kh_end(h)) p = &kh_val(h, k);
478		} else {
479			p = &dummy;
480			dummy.a[1] = l;
481		}
482		for (k = 0; p && k < p->n; ++k) {
483			int64_t beg = p->a[k<<1], end = p->a[k<<1|1], cnt[11];
484			int la, lb, na, nb, nc;
485			if (beg > 0) la = seq->seq.s[beg-1], lb = seq_nt16_table[la];
486			else la = 'a', lb = -1;
487			na = seq->seq.s[beg]; nb = seq_nt16_table[na]; nc = bitcnt_table[nb];
488			memset(cnt, 0, 11 * sizeof(int64_t));
489			for (i = beg; i < end; ++i) {
490				int is_CpG = 0, a, b, c;
491				a = na; b = nb; c = nc;
492				na = seq->seq.s[i+1]; nb = seq_nt16_table[na]; nc = bitcnt_table[nb];
493				if (b == 2 || b == 10) { // C or Y
494					if (nb == 4 || nb == 5) is_CpG = 1;
495				} else if (b == 4 || b == 5) { // G or R
496					if (lb == 2 || lb == 10) is_CpG = 1;
497				}
498				if (upper_only == 0 || isupper(a)) {
499					if (c > 1) ++cnt[c+2];
500					if (c == 1) ++cnt[seq_nt16to4_table[b]];
501					if (b == 10 || b == 5) ++cnt[9];
502					else if (c == 2) {
503						++cnt[8];
504					}
505					if (is_CpG) {
506						++cnt[7];
507						if (b == 10 || b == 5) ++cnt[10];
508					}
509				}
510				la = a; lb = b;
511			}
512			if (h) printf("%s\t%ld\t%ld", seq->name.s, beg, end);
513			else printf("%s\t%ld", seq->name.s, l);
514			for (i = 0; i < 11; ++i) printf("\t%ld", cnt[i]);
515			putchar('\n');
516		}
517		fflush(stdout);
518	}
519	free(dummy.a);
520	kseq_destroy(seq);
521	gzclose(fp);
522	return 0;
523}
524
525int stk_randbase(int argc, char *argv[])
526{
527	gzFile fp;
528	kseq_t *seq;
529	int64_t l;
530	if (argc == 1) {
531		fprintf(stderr, "Usage: seqtk randbase <in.fa>\n");
532		return 1;
533	}
534	fp = (strcmp(argv[1], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[1], "r");
535	if (fp == 0) {
536		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
537		return 1;
538	}
539	seq = kseq_init(fp);
540	while ((l = kseq_read(seq)) >= 0) {
541		int64_t i;
542		printf(">%s", seq->name.s);
543		for (i = 0; i < l; ++i) {
544			int c, b, a, j, k, m;
545			b = seq->seq.s[i];
546			c = seq_nt16_table[b];
547			a = bitcnt_table[c];
548			if (a == 2) {
549				m = (drand48() < 0.5);
550				for (j = k = 0; j < 4; ++j) {
551					if ((1<<j & c) == 0) continue;
552					if (k == m) break;
553					++k;
554				}
555				seq->seq.s[i] = islower(b)? "acgt"[j] : "ACGT"[j];
556			}
557			if (i%60 == 0) putchar('\n');
558			putchar(seq->seq.s[i]);
559		}
560		putchar('\n');
561	}
562	kseq_destroy(seq);
563	gzclose(fp);
564	return 0;
565}
566
567int stk_hety(int argc, char *argv[])
568{
569	gzFile fp;
570	kseq_t *seq;
571	int64_t l;
572	int c, win_size = 50000, n_start = 5, win_step, is_lower_mask = 0;
573	char *buf;
574	uint64_t cnt[3];
575	if (argc == 1) {
576		fprintf(stderr, "\n");
577		fprintf(stderr, "Usage:   seqtk hety [options] <in.fa>\n\n");
578		fprintf(stderr, "Options: -w INT   window size [%d]\n", win_size);
579		fprintf(stderr, "         -t INT   # start positions in a window [%d]\n", n_start);
580		fprintf(stderr, "         -m       treat lowercases as masked\n");
581		fprintf(stderr, "\n");
582		return 1;
583	}
584	while ((c = getopt(argc, argv, "w:t:m")) >= 0) {
585		switch (c) {
586			case 'w': win_size = atoi(optarg); break;
587			case 't': n_start = atoi(optarg); break;
588			case 'm': is_lower_mask = 1; break;
589		}
590	}
591	fp = (strcmp(argv[optind], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind], "r");
592	if (fp == 0) {
593		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
594		return 1;
595	}
596	seq = kseq_init(fp);
597	win_step = win_size / n_start;
598	buf = calloc(win_size, 1);
599	while ((l = kseq_read(seq)) >= 0) {
600		int64_t x, i, y, z, next = 0;
601		cnt[0] = cnt[1] = cnt[2] = 0;
602		for (i = 0; i <= l; ++i) {
603			if ((i >= win_size && i % win_step == 0) || i == l) {
604				if (i == l && l >= win_size) {
605					for (y = l - win_size; y < next; ++y) --cnt[(int)buf[y % win_size]];
606				}
607				if (cnt[1] + cnt[2] > 0)
608					printf("%s\t%ld\t%ld\t%.2lf\t%ld\t%ld\n", seq->name.s, next, i,
609						   (double)cnt[2] / (cnt[1] + cnt[2]) * win_size, cnt[1] + cnt[2], cnt[2]);
610				next = i;
611			}
612			if (i < l) {
613				y = i % win_size;
614				c = seq->seq.s[i];
615				if (is_lower_mask && islower(c)) c = 'N';
616				c = seq_nt16_table[c];
617				x = bitcnt_table[c];
618				if (i >= win_size) --cnt[(int)buf[y]];
619				buf[y] = z = x > 2? 0 : x == 2? 2 : 1;
620				++cnt[z];
621			}
622		}
623	}
624	free(buf);
625	kseq_destroy(seq);
626	gzclose(fp);
627	return 0;
628}
629
630int stk_gap(int argc, char *argv[])
631{
632	gzFile fp;
633	kseq_t *seq;
634	int64_t len;
635	int c, min_size = 50;
636	if (argc == 1) {
637		fprintf(stderr, "Usage: seqtk gap [-l %d] <in.fa>\n", min_size);
638		return 1;
639	}
640	while ((c = getopt(argc, argv, "l:")) >= 0) {
641		switch (c) {
642			case 'l': min_size = atoi(optarg); break;
643		}
644	}
645	fp = (strcmp(argv[optind], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind], "r");
646	if (fp == 0) {
647		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
648		return 1;
649	}
650	seq = kseq_init(fp);
651	while ((len = kseq_read(seq)) >= 0) {
652		int64_t i, l;
653		for (i = l = 0; i <= len; ++i) {
654			c = i < len? seq_nt6_table[(uint8_t)seq->seq.s[i]] : 5;
655			if (i == len || (c >= 1 && c <= 4)) {
656				if (l > 0 && l >= min_size)
657					printf("%s\t%ld\t%ld\n", seq->name.s, i - l, i);
658				l = 0;
659			} else ++l;
660		}
661	}
662	kseq_destroy(seq);
663	gzclose(fp);
664	return 0;
665}
666
667/* subseq */
668
669int stk_subseq(int argc, char *argv[])
670{
671	khash_t(reg) *h = kh_init(reg);
672	gzFile fp;
673	kseq_t *seq;
674	int64_t l, i, j, seq_max = 0;
675	int c, is_tab = 0, line = 0, do_strand = 0;
676	char *seq_buf = 0;
677	khint_t k;
678	while ((c = getopt(argc, argv, "tl:s")) >= 0) {
679		switch (c) {
680			case 't': is_tab = 1; break;
681			case 's': do_strand = 1; break;
682			case 'l': line = atoi(optarg); break;
683		}
684	}
685	if (optind + 2 > argc) {
686		fprintf(stderr, "Usage:   seqtk subseq [options] <in.fa> <in.bed>|<name.list>\n");
687		fprintf(stderr, "Options:\n");
688		fprintf(stderr, "  -t       TAB delimited output\n");
689		fprintf(stderr, "  -s       strand aware\n");
690		fprintf(stderr, "  -l INT   sequence line length [%d]\n", line);
691		fprintf(stderr, "Note: Use 'samtools faidx' if only a few regions are intended.\n");
692		return 1;
693	}
694	if (do_strand) h = stk_reg_read_alt(argv[optind+1]);
695	else h = stk_reg_read(argv[optind+1]);
696	if (h == 0) {
697		fprintf(stderr, "[E::%s] failed to read the list of regions in file '%s'\n", __func__, argv[optind+1]);
698		return 1;
699	}
700	// subseq
701	fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
702	if (fp == 0) {
703		fprintf(stderr, "[E::%s] failed to open the input file/stream\n", __func__);
704		return 1;
705	}
706	seq = kseq_init(fp);
707	while ((l = kseq_read(seq)) >= 0) {
708		reglist_t *p;
709		k = kh_get(reg, h, seq->name.s);
710		if (k == kh_end(h)) continue;
711		p = &kh_val(h, k);
712		for (i = 0; i < p->n; ++i) {
713			int64_t beg = p->a[i<<1], end = p->a[i<<1|1];
714			if (beg >= seq->seq.l) {
715				fprintf(stderr, "[subseq] %s: %ld >= %ld\n", seq->name.s, beg, seq->seq.l);
716				continue;
717			}
718			if (end > seq->seq.l) end = seq->seq.l;
719			if (is_tab == 0) {
720				printf("%c%s", seq->qual.l == seq->seq.l? '@' : '>', seq->name.s);
721				if (beg > 0 || (int64_t)p->a[i<<1|1] != INT64_MAX) {
722					if (end == INT64_MAX) {
723						if (beg) printf(":%ld", beg+1);
724					} else printf(":%ld-%ld", beg+1, end);
725				}
726				if (seq->comment.l) printf(" %s", seq->comment.s);
727			} else printf("%s\t%ld\t", seq->name.s, beg + 1);
728			if (end > seq->seq.l) end = seq->seq.l;
729			if (do_strand && p->rev) { // TODO: the strand mode only works with FASTA
730				if (end - beg >= seq_max) {
731					seq_max = end - beg;
732					kroundup64(seq_max);
733					seq_buf = (char*)realloc(seq_buf, seq_max);
734				}
735				if (p->rev[i] < 0)
736					for (j = end - 1; j >= beg; --j)
737						seq_buf[end - 1 - j] = (uint8_t)seq->seq.s[j] >= 128? 'N' : comp_tab[(uint8_t)seq->seq.s[j]];
738				else memcpy(seq_buf, &seq->seq.s[beg], end - beg);
739				putchar('\n');
740				fwrite(seq_buf, 1, end - beg, stdout);
741				putchar('\n');
742			} else {
743				for (j = 0; j < end - beg; ++j) {
744					if (is_tab == 0 && (j == 0 || (line > 0 && j % line == 0))) putchar('\n');
745					putchar(seq->seq.s[j + beg]);
746				}
747				putchar('\n');
748				if (seq->qual.l != seq->seq.l || is_tab) continue;
749				printf("+");
750				for (j = 0; j < end - beg; ++j) {
751					if (j == 0 || (line > 0 && j % line == 0)) putchar('\n');
752					putchar(seq->qual.s[j + beg]);
753				}
754				putchar('\n');
755			}
756		}
757	}
758	// free
759	free(seq_buf);
760	kseq_destroy(seq);
761	gzclose(fp);
762	stk_reg_destroy(h);
763	return 0;
764}
765
766/* mergefa */
767int stk_mergefa(int argc, char *argv[])
768{
769	gzFile fp[2];
770	kseq_t *seq[2];
771	int i, c, is_intersect = 0, is_haploid = 0, qual = 0, is_mask = 0, is_randhet = 0;
772	int64_t l;
773	uint64_t cnt[5];
774	while ((c = getopt(argc, argv, "himrq:")) >= 0) {
775		switch (c) {
776			case 'i': is_intersect = 1; break;
777			case 'h': is_haploid = 1; break;
778			case 'm': is_mask = 1; break;
779			case 'r': is_randhet = 1; break;
780			case 'q': qual = atoi(optarg); break;
781		}
782	}
783	if (is_mask && is_intersect) {
784		fprintf(stderr, "[%s] `-i' and `-h' cannot be applied at the same time.\n", __func__);
785		return 1;
786	}
787	if (optind + 2 > argc) {
788		fprintf(stderr, "\nUsage: seqtk mergefa [options] <in1.fa> <in2.fa>\n\n");
789		fprintf(stderr, "Options: -q INT   quality threshold [0]\n");
790		fprintf(stderr, "         -i       take intersection\n");
791		fprintf(stderr, "         -m       convert to lowercase when one of the input base is N\n");
792		fprintf(stderr, "         -r       pick a random allele from het\n");
793		fprintf(stderr, "         -h       suppress hets in the input\n\n");
794		return 1;
795	}
796	for (i = 0; i < 2; ++i) {
797		fp[i] = strcmp(argv[optind+i], "-")? gzopen(argv[optind+i], "r") : gzdopen(fileno(stdin), "r");
798		seq[i] = kseq_init(fp[i]);
799	}
800	if (fp[0] == 0 || fp[1] == 0) {
801		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
802		return 1;
803	}
804	cnt[0] = cnt[1] = cnt[2] = cnt[3] = cnt[4] = 0;
805	srand48(11);
806	while (kseq_read(seq[0]) >= 0) {
807		int64_t min_l;
808		int c[2], b[2], is_upper;
809		kseq_read(seq[1]);
810		if (strcmp(seq[0]->name.s, seq[1]->name.s))
811			fprintf(stderr, "[%s] Different sequence names: %s != %s\n", __func__, seq[0]->name.s, seq[1]->name.s);
812		if (seq[0]->seq.l != seq[1]->seq.l)
813			fprintf(stderr, "[%s] Unequal sequence length: %ld != %ld\n", __func__, seq[0]->seq.l, seq[1]->seq.l);
814		min_l = seq[0]->seq.l < seq[1]->seq.l? seq[0]->seq.l : seq[1]->seq.l;
815		printf(">%s", seq[0]->name.s);
816		for (l = 0; l < min_l; ++l) {
817			c[0] = seq[0]->seq.s[l]; c[1] = seq[1]->seq.s[l];
818			if (seq[0]->qual.l && seq[0]->qual.s[l] - 33 < qual) c[0] = tolower(c[0]);
819			if (seq[1]->qual.l && seq[1]->qual.s[l] - 33 < qual) c[1] = tolower(c[1]);
820			if (is_intersect) is_upper = (isupper(c[0]) || isupper(c[1]))? 1 : 0;
821			else if (is_mask) is_upper = (isupper(c[0]) || isupper(c[1]))? 1 : 0;
822			else is_upper = (isupper(c[0]) && isupper(c[1]))? 1 : 0;
823			c[0] = seq_nt16_table[c[0]]; c[1] = seq_nt16_table[c[1]];
824			if (c[0] == 0) c[0] = 15;
825			if (c[1] == 0) c[1] = 15;
826			b[0] = bitcnt_table[c[0]];
827			b[1] = bitcnt_table[c[1]];
828			if (is_upper) {
829				if (b[0] == 1 && b[1] == 1) {
830					if (c[0] == c[1]) ++cnt[0];
831					else ++cnt[1];
832				} else if (b[0] == 1 && b[1] == 2) ++cnt[2];
833				else if (b[0] == 2 && b[1] == 1) ++cnt[3];
834				else if (b[0] == 2 && b[1] == 2) ++cnt[4];
835			}
836			if (is_haploid && (b[0] > 1 || b[1] > 1)) is_upper = 0;
837			if (is_intersect) {
838				c[0] = c[0] & c[1];
839				if (c[0] == 0) is_upper = 0; // FIXME: is this a bug - c[0] cannot be 0!
840			} else if (is_mask) {
841				if (c[0] == 15 || c[1] == 15) is_upper = 0;
842				c[0] &= c[1];
843				if (c[0] == 0) is_upper = 0;
844			} else if (is_randhet) {
845				if (b[0] == 1 && b[1] == 1) { // two homs
846					c[0] |= c[1];
847				} else if (((b[0] == 1 && b[1] == 2) || (b[0] == 2 && b[1] == 1)) && (c[0]&c[1])) { // one hom, one het
848					c[0] = (lrand48()&1)? (c[0] & c[1]) : (c[0] | c[1]);
849				} else if (b[0] == 2 && b[1] == 2 && c[0] == c[1]) { // double hets
850					if (lrand48()&1) {
851						if (lrand48()&1) {
852							for (i = 8; i >= 1; i >>= 1) // pick the "larger" allele
853								if (c[0]&i) c[0] &= i;
854						} else {
855							for (i = 1; i <= 8; i <<= 1) // pick the "smaller" allele
856								if (c[0]&i) c[0] &= i;
857						}
858					} // else set as het
859				} else is_upper = 0;
860			} else c[0] |= c[1];
861			c[0] = seq_nt16_rev_table[c[0]];
862			if (!is_upper) c[0] = tolower(c[0]);
863			if (l%60 == 0) putchar('\n');
864			putchar(c[0]);
865		}
866		putchar('\n');
867	}
868	fprintf(stderr, "[%s] (same,diff,hom-het,het-hom,het-het)=(%ld,%ld,%ld,%ld,%ld)\n", __func__, (long)cnt[0], (long)cnt[1], (long)cnt[2], (long)cnt[3], (long)cnt[4]);
869	return 0;
870}
871
872int stk_famask(int argc, char *argv[])
873{
874	gzFile fp[2];
875	kseq_t *seq[2];
876	int i, c;
877	int64_t l;
878	while ((c = getopt(argc, argv, "")) >= 0);
879	if (argc - optind < 2) {
880		fprintf(stderr, "Usage: seqtk famask <src.fa> <mask.fa>\n");
881		return 1;
882	}
883	for (i = 0; i < 2; ++i) {
884		fp[i] = strcmp(argv[optind+i], "-")? gzopen(argv[optind+i], "r") : gzdopen(fileno(stdin), "r");
885		seq[i] = kseq_init(fp[i]);
886	}
887	if (fp[0] == 0 || fp[1] == 0) {
888		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
889		return 1;
890	}
891	while (kseq_read(seq[0]) >= 0) {
892		int64_t min_l;
893		int c[2];
894		kseq_read(seq[1]);
895		if (strcmp(seq[0]->name.s, seq[1]->name.s))
896			fprintf(stderr, "[%s] Different sequence names: %s != %s\n", __func__, seq[0]->name.s, seq[1]->name.s);
897		if (seq[0]->seq.l != seq[1]->seq.l)
898			fprintf(stderr, "[%s] Unequal sequence length: %ld != %ld\n", __func__, seq[0]->seq.l, seq[1]->seq.l);
899		min_l = seq[0]->seq.l < seq[1]->seq.l? seq[0]->seq.l : seq[1]->seq.l;
900		printf(">%s", seq[0]->name.s);
901		for (l = 0; l < min_l; ++l) {
902			c[0] = seq[0]->seq.s[l]; c[1] = seq[1]->seq.s[l];
903			if (c[1] == 'x') c[0] = tolower(c[0]);
904			else if (c[1] != 'X') c[0] = c[1];
905			if (l%60 == 0) putchar('\n');
906			putchar(c[0]);
907		}
908		putchar('\n');
909	}
910	return 0;
911}
912
913int stk_mutfa(int argc, char *argv[])
914{
915	khash_t(reg) *h = kh_init(reg);
916	gzFile fp;
917	kseq_t *seq;
918	kstream_t *ks;
919	int64_t l, i;
920	int dret;
921	kstring_t *str;
922	khint_t k;
923	if (argc < 3) {
924		fprintf(stderr, "Usage: seqtk mutfa <in.fa> <in.snp>\n\n");
925		fprintf(stderr, "Note: <in.snp> contains at least four columns per line which are:\n");
926		fprintf(stderr, "      'chr  1-based-pos  any  base-changed-to'.\n");
927		return 1;
928	}
929	// read the list
930	str = calloc(1, sizeof(kstring_t));
931	fp = strcmp(argv[2], "-")? gzopen(argv[2], "r") : gzdopen(fileno(stdin), "r");
932	if (fp == 0) {
933		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
934		return 1;
935	}
936	ks = ks_init(fp);
937	while (ks_getuntil(ks, 0, str, &dret) >= 0) {
938		char *s = strdup(str->s);
939		int64_t beg = 0;
940		int ret;
941		reglist_t *p;
942		k = kh_get(reg, h, s);
943		if (k == kh_end(h)) {
944			k = kh_put(reg, h, s, &ret);
945			memset(&kh_val(h, k), 0, sizeof(reglist_t));
946		}
947		p = &kh_val(h, k);
948		if (ks_getuntil(ks, 0, str, &dret) > 0) beg = atol(str->s) - 1; // 2nd col
949		ks_getuntil(ks, 0, str, &dret); // 3rd col
950		ks_getuntil(ks, 0, str, &dret); // 4th col
951		// skip the rest of the line
952		if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
953		if (isalpha(str->s[0]) && str->l == 1) {
954			if (p->n == p->m) {
955				p->m = p->m? p->m<<1 : 4;
956				p->a = realloc(p->a, p->m * 16);
957			}
958			p->a[p->n<<1] = (uint64_t)beg;
959			p->a[p->n<<1|1] = (uint64_t)str->s[0];
960			p->n++;
961		}
962	}
963	ks_destroy(ks);
964	gzclose(fp);
965	free(str->s); free(str);
966	// mutfa
967	fp = strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
968	if (fp == 0) {
969		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
970		return 1;
971	}
972	seq = kseq_init(fp);
973	while ((l = kseq_read(seq)) >= 0) {
974		reglist_t *p;
975		k = kh_get(reg, h, seq->name.s);
976		if (k != kh_end(h)) {
977			p = &kh_val(h, k);
978			for (i = 0; i < p->n; ++i) {
979				int64_t beg = p->a[i<<1];
980				if (beg < seq->seq.l)
981					seq->seq.s[beg] = (int)p->a[i<<1|1];
982			}
983		}
984		printf(">%s", seq->name.s);
985		for (i = 0; i < l; ++i) {
986			if (i%60 == 0) putchar('\n');
987			putchar(seq->seq.s[i]);
988		}
989		putchar('\n');
990	}
991	// free
992	kseq_destroy(seq);
993	gzclose(fp);
994	for (k = 0; k < kh_end(h); ++k) {
995		if (kh_exist(h, k)) {
996			free(kh_val(h, k).a);
997			free((char*)kh_key(h, k));
998		}
999	}
1000	kh_destroy(reg, h);
1001	return 0;
1002}
1003
1004int stk_listhet(int argc, char *argv[])
1005{
1006	gzFile fp;
1007	kseq_t *seq;
1008	int64_t l, i;
1009	if (argc == 1) {
1010		fprintf(stderr, "Usage: seqtk listhet <in.fa>\n");
1011		return 1;
1012	}
1013	fp = (strcmp(argv[1], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[1], "r");
1014	if (fp == 0) {
1015		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1016		return 1;
1017	}
1018	seq = kseq_init(fp);
1019	while ((l = kseq_read(seq)) >= 0) {
1020		for (i = 0; i < l; ++i) {
1021			int b = seq->seq.s[i];
1022			if (bitcnt_table[seq_nt16_table[b]] == 2)
1023				printf("%s\t%ld\t%c\n", seq->name.s, i+1, b);
1024		}
1025	}
1026	kseq_destroy(seq);
1027	gzclose(fp);
1028	return 0;
1029}
1030
1031int stk_split(int argc, char *argv[])
1032{
1033	gzFile fp;
1034	kseq_t *seq;
1035	int64_t l;
1036	int c, i, n = 10, len = 0;
1037	char *prefix, *fn;
1038	FILE **out;
1039	while ((c = getopt(argc, argv, "n:l:")) >= 0) {
1040		if (c == 'n') n = atoi(optarg);
1041		else if (c == 'l') len = atoi(optarg);
1042	}
1043	if (argc == optind) {
1044		fprintf(stderr, "Usage: seqtk split [options] <prefix> <in.fa>\n");
1045		fprintf(stderr, "Options:\n");
1046		fprintf(stderr, "  -n INT    number of files [%d]\n", n);
1047		fprintf(stderr, "  -l INT    line length [%d]\n", len);
1048		return 1;
1049	}
1050	prefix = argv[optind];
1051	fp = (strcmp(argv[optind+1], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind+1], "r");
1052	if (fp == 0) {
1053		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1054		return 1;
1055	}
1056	out = (FILE**)malloc(sizeof(FILE*) * n);
1057	fn = (char*)malloc(strlen(prefix) + 10);
1058	for (i = 0; i < n; ++i) {
1059		sprintf(fn, "%s.%.5d.fa", prefix, i + 1);
1060		out[i] = fopen(fn, "w+");
1061		if (out[i] == 0) {
1062			fprintf(stderr, "ERROR: failed to create file %s\n", fn);
1063			exit(1);
1064		}
1065	}
1066	free(fn);
1067	seq = kseq_init(fp);
1068	i = 0;
1069	while ((l = kseq_read(seq)) >= 0) {
1070		stk_printseq(out[i % n], seq, len);
1071		++i;
1072	}
1073	for (i = 0; i < n; ++i) fclose(out[i]);
1074	free(out);
1075	kseq_destroy(seq);
1076	gzclose(fp);
1077	return 0;
1078}
1079
1080/* cutN */
1081static int cutN_min_N_tract = 1000;
1082static int cutN_nonN_penalty = 10;
1083
1084static int64_t find_next_cut(const kseq_t *ks, int64_t k, int64_t *begin, int64_t *end)
1085{
1086	int64_t i, b, e;
1087	while (k < ks->seq.l) {
1088		if (seq_nt16_table[(int)ks->seq.s[k]] == 15) {
1089			int64_t score, max;
1090			score = 0; e = max = -1;
1091			for (i = k; i < ks->seq.l && score >= 0; ++i) { /* forward */
1092				if (seq_nt16_table[(int)ks->seq.s[i]] == 15) ++score;
1093				else score -= cutN_nonN_penalty;
1094				if (score > max) max = score, e = i;
1095			}
1096			score = 0; b = max = -1;
1097			for (i = e; i >= 0 && score >= 0; --i) { /* backward */
1098				if (seq_nt16_table[(int)ks->seq.s[i]] == 15) ++score;
1099				else score -= cutN_nonN_penalty;
1100				if (score > max) max = score, b = i;
1101			}
1102			if (e + 1 - b >= cutN_min_N_tract) {
1103				*begin = b;
1104				*end = e + 1;
1105				return *end;
1106			}
1107			k = e + 1;
1108		} else ++k;
1109	}
1110	return -1;
1111}
1112static void print_seq(FILE *fpout, const kseq_t *ks, int64_t begin, int64_t end)
1113{
1114	int64_t i;
1115	if (begin >= end) return; // FIXME: why may this happen? Understand it!
1116	fprintf(fpout, "%c%s:%ld-%ld", ks->qual.l? '@' : '>', ks->name.s, begin+1, end);
1117	for (i = begin; i < end && i < ks->seq.l; ++i) {
1118		if ((i - begin)%60 == 0) fputc('\n', fpout);
1119		fputc(ks->seq.s[i], fpout);
1120	}
1121	fputc('\n', fpout);
1122	if (ks->qual.l == 0) return;
1123	fputs("+\n", fpout);
1124	for (i = begin; i < end && i < ks->qual.l; ++i) {
1125		if ((i - begin)%60 == 0) fputc('\n', fpout);
1126		fputc(ks->qual.s[i], fpout);
1127	}
1128	fputc('\n', fpout);
1129}
1130int stk_cutN(int argc, char *argv[])
1131{
1132	int64_t l;
1133	int c, gap_only = 0;
1134	gzFile fp;
1135	kseq_t *ks;
1136	while ((c = getopt(argc, argv, "n:p:g")) >= 0) {
1137		switch (c) {
1138			case 'n': cutN_min_N_tract = atoi(optarg); break;
1139			case 'p': cutN_nonN_penalty = atoi(optarg); break;
1140			case 'g': gap_only = 1; break;
1141			default: return 1;
1142		}
1143	}
1144	if (argc == optind) {
1145		fprintf(stderr, "\n");
1146		fprintf(stderr, "Usage:   seqtk cutN [options] <in.fa>\n\n");
1147		fprintf(stderr, "Options: -n INT    min size of N tract [%d]\n", cutN_min_N_tract);
1148		fprintf(stderr, "         -p INT    penalty for a non-N [%d]\n", cutN_nonN_penalty);
1149		fprintf(stderr, "         -g        print gaps only, no sequence\n\n");
1150		return 1;
1151	}
1152	fp = (strcmp(argv[optind], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind], "r");
1153	if (fp == 0) {
1154		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1155		return 1;
1156	}
1157	ks = kseq_init(fp);
1158	while ((l = kseq_read(ks)) >= 0) {
1159		int64_t k = 0, begin = 0, end = 0;
1160		while (find_next_cut(ks, k, &begin, &end) >= 0) {
1161			if (begin != 0) {
1162				if (gap_only) printf("%s\t%ld\t%ld\n", ks->name.s, begin, end);
1163				else print_seq(stdout, ks, k, begin);
1164			}
1165			k = end;
1166		}
1167		if (!gap_only) print_seq(stdout, ks, k, l);
1168	}
1169	kseq_destroy(ks);
1170	gzclose(fp);
1171	return 0;
1172}
1173
1174int stk_hrun(int argc, char *argv[])
1175{
1176	gzFile fp;
1177	kseq_t *ks;
1178	int min_len = 7, c = 0;
1179	int64_t l = 0, beg = 0, i;
1180	if (argc == optind) {
1181		fprintf(stderr, "Usage: seqtk hrun <in.fa> [minLen=%d]\n", min_len);
1182		return 1;
1183	}
1184	if (argc == optind + 2) min_len = atoi(argv[optind+1]);
1185	fp = (strcmp(argv[optind], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind], "r");
1186	if (fp == 0) {
1187		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1188		return 1;
1189	}
1190	ks = kseq_init(fp);
1191	while (kseq_read(ks) >= 0) {
1192		c = ks->seq.s[0]; l = 1; beg = 0;
1193		for (i = 1; i < ks->seq.l; ++i) {
1194			if (ks->seq.s[i] != c) {
1195				if (l >= min_len) printf("%s\t%ld\t%ld\t%c\n", ks->name.s, beg, beg + l, c);
1196				c = ks->seq.s[i]; l = 1; beg = i;
1197			} else ++l;
1198		}
1199	}
1200	if (l >= min_len) printf("%s\t%ld\t%ld\t%c\n", ks->name.s, beg, beg + l, c);
1201	kseq_destroy(ks);
1202	gzclose(fp);
1203	return 0;
1204}
1205
1206/* sample */
1207
1208static void cpy_kstr(kstring_t *dst, const kstring_t *src)
1209{
1210	if (src->l == 0) return;
1211	if (src->l + 1 > dst->m) {
1212		dst->m = src->l + 1;
1213		kroundup64(dst->m);
1214		dst->s = realloc(dst->s, dst->m);
1215	}
1216	dst->l = src->l;
1217	memcpy(dst->s, src->s, src->l + 1);
1218}
1219
1220static void cpy_kseq(kseq_t *dst, const kseq_t *src)
1221{
1222	cpy_kstr(&dst->name, &src->name);
1223	cpy_kstr(&dst->seq,  &src->seq);
1224	cpy_kstr(&dst->qual, &src->qual);
1225	cpy_kstr(&dst->comment, &src->comment);
1226}
1227
1228int stk_sample(int argc, char *argv[])
1229{
1230	int c, twopass = 0;
1231	uint64_t i, num = 0, n_seqs = 0;
1232	double frac = 0.;
1233	gzFile fp;
1234	kseq_t *seq;
1235	krand_t *kr = 0;
1236
1237	while ((c = getopt(argc, argv, "2s:")) >= 0)
1238		if (c == 's') kr = kr_srand(atol(optarg));
1239		else if (c == '2') twopass = 1;
1240
1241	if (optind + 2 > argc) {
1242		fprintf(stderr, "\n");
1243		fprintf(stderr, "Usage:   seqtk sample [-2] [-s seed=11] <in.fa> <frac>|<number>\n\n");
1244		fprintf(stderr, "Options: -s INT       RNG seed [11]\n");
1245		fprintf(stderr, "         -2           2-pass mode: twice as slow but with much reduced memory\n\n");
1246		return 1;
1247	}
1248	frac = atof(argv[optind+1]);
1249	if (frac >= 1.0) num = (uint64_t)(frac + .499), frac = 0.;
1250	else if (twopass) {
1251		fprintf(stderr, "[W::%s] when sampling a fraction, option -2 is ignored.", __func__);
1252		twopass = 0;
1253	}
1254	if (kr == 0) kr = kr_srand(11);
1255
1256	if (!twopass) { // the streaming version
1257		kseq_t *buf = 0;
1258		if (num > 0) buf = calloc(num, sizeof(kseq_t));
1259		if (num > 0 && buf == NULL) {
1260			fprintf(stderr, "[E::%s] Could not allocate enough memory for %" PRIu64 " sequences. Exiting...\n", __func__, num);
1261			free(kr);
1262			return 1;
1263		}
1264
1265		fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
1266		if (fp == 0) {
1267			fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1268			return 1;
1269		}
1270		seq = kseq_init(fp);
1271		n_seqs = 0;
1272		while (kseq_read(seq) >= 0) {
1273			double r = kr_drand(kr);
1274			++n_seqs;
1275			if (num) {
1276				uint64_t y = n_seqs - 1 < num? n_seqs - 1 : (uint64_t)(r * n_seqs);
1277				if (y < num) cpy_kseq(&buf[y], seq);
1278			} else if (r < frac) stk_printseq(stdout, seq, UINT_MAX);
1279		}
1280		for (i = 0; i < num; ++i) {
1281			kseq_t *p = &buf[i];
1282			if (p->seq.l) stk_printseq(stdout, p, UINT_MAX);
1283			free(p->seq.s); free(p->qual.s); free(p->name.s);
1284		}
1285		if (buf != NULL) free(buf);
1286	} else {
1287		uint64_t *buf;
1288		khash_t(64) *hash;
1289		int absent;
1290
1291		if (strcmp(argv[optind], "-") == 0) {
1292			fprintf(stderr, "[E::%s] in the 2-pass mode, the input cannot be STDIN.\n", __func__);
1293			free(kr);
1294			return 1;
1295		}
1296
1297		// 1st pass
1298		buf = malloc(num * 8);
1299		for (i = 0; i < num; ++i) buf[i] = UINT64_MAX;
1300		fp = gzopen(argv[optind], "r");
1301		if (fp == 0) {
1302			fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1303			return 1;
1304		}
1305		seq = kseq_init(fp);
1306		n_seqs = 0;
1307		while (kseq_read(seq) >= 0) {
1308			double r = kr_drand(kr);
1309			uint64_t y;
1310			++n_seqs;
1311			y = n_seqs - 1 < num? n_seqs - 1 : (uint64_t)(r * n_seqs);
1312			if (y < num) buf[y] = n_seqs;
1313		}
1314		kseq_destroy(seq);
1315		gzclose(fp);
1316		hash = kh_init(64);
1317		for (i = 0; i < num; ++i) kh_put(64, hash, buf[i], &absent);
1318		free(buf);
1319		// 2nd pass
1320		fp = gzopen(argv[optind], "r");
1321		seq = kseq_init(fp);
1322		n_seqs = 0;
1323		while (kseq_read(seq) >= 0)
1324			if (kh_get(64, hash, ++n_seqs) != kh_end(hash))
1325				stk_printseq(stdout, seq, UINT_MAX);
1326		kh_destroy(64, hash);
1327	}
1328
1329	kseq_destroy(seq);
1330	gzclose(fp);
1331	free(kr);
1332	return 0;
1333}
1334
1335/* seq */
1336
1337void stk_mask(kseq_t *seq, const khash_t(reg) *h, int is_complement, int mask_chr)
1338{
1339	int64_t i, j;
1340	khiter_t k;
1341	k = kh_get(reg, h, seq->name.s);
1342	if (k == kh_end(h)) { // not found in the hash table
1343		if (is_complement) {
1344			if (mask_chr) {
1345				for (j = 0; j < seq->seq.l; ++j)
1346					seq->seq.s[j] = mask_chr;
1347			} else {
1348				for (j = 0; j < seq->seq.l; ++j)
1349					seq->seq.s[j] = tolower(seq->seq.s[j]);
1350			}
1351		}
1352	} else {
1353		reglist_t *p = &kh_val(h, k);
1354		if (!is_complement) {
1355			for (i = 0; i < p->n; ++i) {
1356				int64_t beg = p->a[i<<1], end = p->a[i<<1|1];
1357				if (beg >= seq->seq.l) continue;
1358				if (end > seq->seq.l) end = seq->seq.l;
1359				if (!mask_chr) for (j = beg; j < end; ++j) seq->seq.s[j] = tolower(seq->seq.s[j]);
1360				else for (j = beg; j < end; ++j) seq->seq.s[j] = mask_chr;
1361			}
1362		} else {
1363			int8_t *mask = calloc(seq->seq.l, 1);
1364			for (i = 0; i < p->n; ++i) {
1365				int64_t beg = p->a[i<<1], end = p->a[i<<1|1];
1366				if (end >= seq->seq.l) end = seq->seq.l;
1367				for (j = beg; j < end; ++j) mask[j] = 1;
1368			}
1369			if (mask_chr) {
1370				for (j = 0; j < seq->seq.l; ++j)
1371					if (mask[j] == 0) seq->seq.s[j] = mask_chr;
1372			} else {
1373				for (j = 0; j < seq->seq.l; ++j)
1374					if (mask[j] == 0) seq->seq.s[j] = tolower(seq->seq.s[j]);
1375			}
1376			free(mask);
1377		}
1378	}
1379}
1380
1381int stk_seq(int argc, char *argv[])
1382{
1383	gzFile fp;
1384	kseq_t *seq;
1385	int c, qual_thres = 0, flag = 0, qual_shift = 33, mask_chr = 0, min_len = 0, max_q = 255, fake_qual = -1;
1386	unsigned line_len = 0;
1387	int64_t i, n_seqs = 0;
1388	double frac = 1.;
1389	khash_t(reg) *h = 0;
1390	krand_t *kr = 0;
1391
1392	while ((c = getopt(argc, argv, "N12q:l:Q:aACrn:s:f:M:L:cVUX:SF:xR")) >= 0) {
1393		switch (c) {
1394			case 'a':
1395			case 'A': flag |= 1; break;
1396			case 'C': flag |= 2; break;
1397			case 'r': flag |= 4; break;
1398			case 'c': flag |= 8; break;
1399			case '1': flag |= 16; break;
1400			case '2': flag |= 32; break;
1401			case 'V': flag |= 64; break;
1402			case 'N': flag |= 128; break;
1403			case 'U': flag |= 256; break;
1404			case 'S': flag |= 512; break;
1405			case 'x': flag |= 1024; break;
1406			case 'R': flag |= 2048; break;
1407			case 'M': h = stk_reg_read(optarg); break;
1408			case 'n': mask_chr = *optarg; break;
1409			case 'Q': qual_shift = atoi(optarg); break;
1410			case 'q': qual_thres = atoi(optarg); break;
1411			case 'X': max_q = atoi(optarg); break;
1412			case 'l': line_len = atoi(optarg); break;
1413			case 'L': min_len = atoi(optarg); break;
1414			case 's': kr = kr_srand(atol(optarg)); break;
1415			case 'f': frac = atof(optarg); break;
1416			case 'F': fake_qual = *optarg; break;
1417		}
1418	}
1419	if (kr == 0) kr = kr_srand(11);
1420	if (argc == optind && isatty(fileno(stdin))) {
1421		fprintf(stderr, "\n");
1422		fprintf(stderr, "Usage:   seqtk seq [options] <in.fq>|<in.fa>\n\n");
1423		fprintf(stderr, "Options: -q INT    mask bases with quality lower than INT [0]\n");
1424		fprintf(stderr, "         -X INT    mask bases with quality higher than INT [255]\n");
1425		fprintf(stderr, "         -n CHAR   masked bases converted to CHAR; 0 for lowercase [0]\n");
1426		fprintf(stderr, "         -l INT    number of residues per line; 0 for 2^32-1 [%d]\n", line_len);
1427		fprintf(stderr, "         -Q INT    quality shift: ASCII-INT gives base quality [%d]\n", qual_shift);
1428		fprintf(stderr, "         -s INT    random seed (effective with -f) [11]\n");
1429		fprintf(stderr, "         -f FLOAT  sample FLOAT fraction of sequences [1]\n");
1430		fprintf(stderr, "         -M FILE   mask regions in BED or name list FILE [null]\n");
1431		fprintf(stderr, "         -L INT    drop sequences with length shorter than INT [0]\n");
1432		fprintf(stderr, "         -F CHAR   fake FASTQ quality []\n");
1433		fprintf(stderr, "         -c        mask complement region (effective with -M)\n");
1434		fprintf(stderr, "         -r        reverse complement\n");
1435		fprintf(stderr, "         -R        output both forward and reverse complement\n");
1436		fprintf(stderr, "         -A        force FASTA output (discard quality)\n");
1437		fprintf(stderr, "         -C        drop comments at the header lines\n");
1438		fprintf(stderr, "         -N        drop sequences containing ambiguous bases\n");
1439		fprintf(stderr, "         -1        output the 2n-1 reads only\n");
1440		fprintf(stderr, "         -2        output the 2n reads only\n");
1441		fprintf(stderr, "         -V        shift quality by '(-Q) - 33'\n");
1442		fprintf(stderr, "         -U        convert all bases to uppercases\n");
1443		fprintf(stderr, "         -x        convert all lowercases to -n\n");
1444		fprintf(stderr, "         -S        strip of white spaces in sequences\n");
1445		fprintf(stderr, "\n");
1446		free(kr);
1447		return 1;
1448	}
1449	if (line_len == 0) line_len = UINT_MAX;
1450	fp = optind < argc && strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
1451	if (fp == 0) {
1452		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1453		return 1;
1454	}
1455	seq = kseq_init(fp);
1456	qual_thres += qual_shift;
1457	while (kseq_read(seq) >= 0) {
1458		++n_seqs;
1459		if (seq->seq.l < min_len) continue; // NB: length filter before taking random
1460		if (frac < 1. && kr_drand(kr) >= frac) continue;
1461		if (flag & 48) { // then choose odd/even reads only
1462			if ((flag&16) && (n_seqs&1) == 0) continue;
1463			if ((flag&32) && (n_seqs&1) == 1) continue;
1464		}
1465		if (flag & 512) { // option -S: squeeze out white spaces
1466			int64_t k;
1467			if (seq->qual.l) {
1468				for (i = k = 0; i < seq->seq.l; ++i)
1469					if (!isspace(seq->seq.s[i]))
1470						seq->qual.s[k++] = seq->qual.s[i];
1471				seq->qual.l = k;
1472			}
1473			for (i = k = 0; i < seq->seq.l; ++i)
1474				if (!isspace(seq->seq.s[i]))
1475					seq->seq.s[k++] = seq->seq.s[i];
1476			seq->seq.l = k;
1477		}
1478		if (seq->qual.l && qual_thres > qual_shift) {
1479			if (mask_chr) {
1480				for (i = 0; i < seq->seq.l; ++i)
1481					if (seq->qual.s[i] < qual_thres || seq->qual.s[i] > max_q)
1482						seq->seq.s[i] = mask_chr;
1483			} else {
1484				for (i = 0; i < seq->seq.l; ++i)
1485					if (seq->qual.s[i] < qual_thres || seq->qual.s[i] > max_q)
1486						seq->seq.s[i] = tolower(seq->seq.s[i]);
1487			}
1488		}
1489		if (flag & 256) // option -U: convert to uppercases
1490			for (i = 0; i < seq->seq.l; ++i)
1491				seq->seq.s[i] = toupper(seq->seq.s[i]);
1492		else if ((flag & 1024) && mask_chr > 0)
1493			for (i = 0; i < seq->seq.l; ++i)
1494				seq->seq.s[i] = islower(seq->seq.s[i])? mask_chr : seq->seq.s[i];
1495		if (flag & 1) seq->qual.l = 0; // option -a: fastq -> fasta
1496		else if (fake_qual >= 33 && fake_qual <= 127) {
1497			if (seq->qual.m < seq->seq.m) {
1498				seq->qual.m = seq->seq.m;
1499				seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m);
1500			}
1501			seq->qual.l = seq->seq.l;
1502			memset(seq->qual.s, fake_qual, seq->qual.l);
1503			seq->qual.s[seq->qual.l] = 0;
1504		}
1505		if (flag & 2) seq->comment.l = 0; // option -C: drop fasta/q comments
1506		if (h) stk_mask(seq, h, flag&8, mask_chr); // masking
1507		if (flag & (4|2048)) { // option -r or -R: reverse complement
1508			int c0, c1;
1509			if (flag & 2048)
1510				stk_printseq_suffix(stdout, seq, line_len, "+");
1511			for (i = 0; i < seq->seq.l>>1; ++i) { // reverse complement sequence
1512				c0 = comp_tab[(int)seq->seq.s[i]];
1513				c1 = comp_tab[(int)seq->seq.s[seq->seq.l - 1 - i]];
1514				seq->seq.s[i] = c1;
1515				seq->seq.s[seq->seq.l - 1 - i] = c0;
1516			}
1517			if (seq->seq.l & 1) // complement the remaining base
1518				seq->seq.s[seq->seq.l>>1] = comp_tab[(int)seq->seq.s[seq->seq.l>>1]];
1519			if (seq->qual.l) {
1520				for (i = 0; i < seq->seq.l>>1; ++i) // reverse quality
1521					c0 = seq->qual.s[i], seq->qual.s[i] = seq->qual.s[seq->qual.l - 1 - i], seq->qual.s[seq->qual.l - 1 - i] = c0;
1522			}
1523		}
1524		if ((flag & 64) && seq->qual.l && qual_shift != 33)
1525			for (i = 0; i < seq->qual.l; ++i)
1526				seq->qual.s[i] -= qual_shift - 33;
1527		if (flag & 128) { // option -N: drop sequences containing ambiguous bases - Note: this is the last step!
1528			for (i = 0; i < seq->seq.l; ++i)
1529				if (seq_nt16to4_table[seq_nt16_table[(int)seq->seq.s[i]]] > 3) break;
1530			if (i < seq->seq.l) continue;
1531		}
1532		if (flag & 2048)
1533			stk_printseq_suffix(stdout, seq, line_len, "-");
1534		else
1535			stk_printseq(stdout, seq, line_len);
1536	}
1537	kseq_destroy(seq);
1538	gzclose(fp);
1539	stk_reg_destroy(h);
1540	free(kr);
1541	return 0;
1542}
1543
1544int stk_gc(int argc, char *argv[])
1545{
1546	int c, is_at = 0, min_l = 20;
1547	double frac = 0.6f, xdropoff = 10.0f, q;
1548	gzFile fp;
1549	kseq_t *seq;
1550
1551	while ((c = getopt(argc, argv, "wx:f:l:")) >= 0) {
1552		if (c == 'x') xdropoff = atof(optarg);
1553		else if (c == 'w') is_at = 1;
1554		else if (c == 'f') frac = atof(optarg);
1555		else if (c == 'l') min_l = atoi(optarg);
1556	}
1557	if (optind + 1 > argc) {
1558		fprintf(stderr, "Usage: seqtk gc [options] <in.fa>\n");
1559		fprintf(stderr, "Options:\n");
1560		fprintf(stderr, "  -w         identify high-AT regions\n");
1561		fprintf(stderr, "  -f FLOAT   min GC fraction (or AT fraction for -w) [%.2f]\n", frac);
1562		fprintf(stderr, "  -l INT     min region length to output [%d]\n", min_l);
1563		fprintf(stderr, "  -x FLOAT   X-dropoff [%.1f]\n", xdropoff);
1564		return 1;
1565	}
1566	q = (1.0f - frac) / frac;
1567
1568	fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
1569	if (fp == 0) {
1570		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1571		return 1;
1572	}
1573	seq = kseq_init(fp);
1574	while (kseq_read(seq) >= 0) {
1575		int64_t i, start = 0, max_i = 0, n_hits = 0, start_hits = 0, max_hits = 0;
1576		double sc = 0., max = 0.;
1577		for (i = 0; i < seq->seq.l; ++i) {
1578			int hit;
1579			c = seq_nt16_table[(int)seq->seq.s[i]];
1580			if (is_at) hit = (c == 1 || c == 8 || c == 9);
1581			else hit = (c == 2 || c == 4 || c == 6);
1582			n_hits += hit;
1583			if (hit) {
1584				if (sc == 0) start = i, start_hits = n_hits;
1585				sc += q;
1586				if (sc > max) max = sc, max_i = i, max_hits = n_hits;
1587			} else if (sc > 0) {
1588				sc += -1.0f;
1589				if (sc < 0 || max - sc > xdropoff) {
1590					if (max_i + 1 - start >= min_l)
1591						printf("%s\t%ld\t%ld\t%ld\n", seq->name.s, start, max_i + 1, max_hits - start_hits + 1);
1592					sc = max = 0;
1593					i = max_i;
1594				}
1595			}
1596		}
1597		if (max > 0. && max_i + 1 - start >= min_l)
1598			printf("%s\t%ld\t%ld\t%ld\n", seq->name.s, start, max_i + 1, max_hits - start_hits + 1);
1599	}
1600	kseq_destroy(seq);
1601	gzclose(fp);
1602	return 0;
1603}
1604
1605int stk_mergepe(int argc, char *argv[])
1606{
1607	gzFile fp1, fp2;
1608	kseq_t *seq[2];
1609
1610	if (argc < 3) {
1611		fprintf(stderr, "Usage: seqtk mergepe <in1.fq> <in2.fq>\n");
1612		return 1;
1613	}
1614	fp1 = strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
1615	fp2 = strcmp(argv[2], "-")? gzopen(argv[2], "r") : gzdopen(fileno(stdin), "r");
1616	if (fp1 == 0 || fp2 == 0) {
1617		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1618		return 1;
1619	}
1620	seq[0] = kseq_init(fp1);
1621	seq[1] = kseq_init(fp2);
1622	while (kseq_read(seq[0]) >= 0) {
1623		if (kseq_read(seq[1]) < 0) {
1624			fprintf(stderr, "[W::%s] the 2nd file has fewer records.\n", __func__);
1625			break;
1626		}
1627		stk_printseq(stdout, seq[0], 0);
1628		stk_printseq(stdout, seq[1], 0);
1629	}
1630	if (kseq_read(seq[1]) >= 0)
1631		fprintf(stderr, "[W::%s] the 1st file has fewer records.\n", __func__);
1632	kseq_destroy(seq[0]); gzclose(fp1);
1633	kseq_destroy(seq[1]); gzclose(fp2);
1634	return 0;
1635}
1636
1637int stk_dropse(int argc, char *argv[])
1638{
1639	gzFile fp;
1640	kseq_t *seq, last;
1641
1642	if (argc == 1 && isatty(fileno(stdin))) {
1643		fprintf(stderr, "Usage: seqtk dropse <in.fq>\n");
1644		return 1;
1645	}
1646	fp = argc > 1 && strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
1647	if (fp == 0) {
1648		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1649		return 1;
1650	}
1651	seq = kseq_init(fp);
1652
1653	memset(&last, 0, sizeof(kseq_t));
1654	while (kseq_read(seq) >= 0) {
1655		if (last.name.l) {
1656			kstring_t *p = &last.name, *q = &seq->name;
1657			int is_diff;
1658			if (p->l == q->l) {
1659				int l = (p->l > 2 && p->s[p->l-2] == '/' && q->s[q->l-2] == '/' && isdigit(p->s[p->l-1]) && isdigit(q->s[q->l-1]))? p->l - 2 : p->l;
1660				is_diff = strncmp(p->s, q->s, l);
1661			} else is_diff = 1;
1662			if (!is_diff) {
1663				stk_printseq(stdout, &last, 0);
1664				stk_printseq(stdout, seq, 0);
1665				last.name.l = 0;
1666			} else cpy_kseq(&last, seq);
1667		} else cpy_kseq(&last, seq);
1668	}
1669
1670	kseq_destroy(seq);
1671	gzclose(fp);
1672	// free last!
1673	return 0;
1674}
1675
1676static inline int kputc(int c, kstring_t *s)
1677{
1678	if (s->l + 1 >= s->m) {
1679		char *tmp;
1680		s->m = s->l + 2;
1681		kroundup64(s->m);
1682		if ((tmp = (char*)realloc(s->s, s->m)))
1683			s->s = tmp;
1684		else
1685			return EOF;
1686	}
1687	s->s[s->l++] = c;
1688	s->s[s->l] = 0;
1689	return c;
1690}
1691
1692int stk_hpc(int argc, char *argv[])
1693{
1694	gzFile fp;
1695	kseq_t *seq;
1696	kstring_t str = {0,0,0};
1697
1698	if (argc == 1 && isatty(fileno(stdin))) {
1699		fprintf(stderr, "Usage: seqtk hpc <in.fq>\n");
1700		return 1;
1701	}
1702	fp = argc > 1 && strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
1703	if (fp == 0) {
1704		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1705		return 1;
1706	}
1707	seq = kseq_init(fp);
1708
1709	while (kseq_read(seq) >= 0) {
1710		int64_t i, last;
1711		str.l = 0;
1712		if (seq->seq.l == 0) continue;
1713		for (i = 1, last = 0; i <= seq->seq.l; ++i) {
1714			if (i == seq->seq.l || seq->seq.s[last] != seq->seq.s[i]) {
1715				kputc(seq->seq.s[last], &str);
1716				last = i;
1717			}
1718		}
1719		putchar('>'); puts(seq->name.s);
1720		puts(str.s);
1721	}
1722
1723	kseq_destroy(seq);
1724	gzclose(fp);
1725	free(str.s);
1726	return 0;
1727}
1728
1729int stk_rename(int argc, char *argv[])
1730{
1731	gzFile fp;
1732	kseq_t *seq, last;
1733	char *prefix = 0;
1734	uint64_t n = 1;
1735
1736	if (argc == 1 && isatty(fileno(stdin))) {
1737		fprintf(stderr, "Usage: seqtk rename <in.fq> [prefix]\n");
1738		return 1;
1739	}
1740	fp = argc > 1 && strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
1741	if (fp == 0) {
1742		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1743		return 1;
1744	}
1745	seq = kseq_init(fp);
1746	if (argc > 2) prefix = argv[2];
1747
1748	memset(&last, 0, sizeof(kseq_t));
1749	while (kseq_read(seq) >= 0) {
1750		if (last.name.l) {
1751			kstring_t *p = &last.name, *q = &seq->name;
1752			int is_diff;
1753			if (p->l == q->l) {
1754				int l = (p->l > 2 && p->s[p->l-2] == '/' && q->s[q->l-2] == '/' && isdigit(p->s[p->l-1]) && isdigit(q->s[q->l-1]))? p->l - 2 : p->l;
1755				is_diff = strncmp(p->s, q->s, l);
1756			} else is_diff = 1;
1757			if (!is_diff) {
1758				stk_printseq_renamed(stdout, &last, 0, prefix, n);
1759				stk_printseq_renamed(stdout, seq,   0, prefix, n);
1760				last.name.l = 0;
1761				++n;
1762			} else {
1763				stk_printseq_renamed(stdout, &last, 0, prefix, n);
1764				++n;
1765				cpy_kseq(&last, seq);
1766			}
1767		} else cpy_kseq(&last, seq);
1768	}
1769	if (last.name.l) stk_printseq_renamed(stdout, &last, 0, prefix, n);
1770
1771	kseq_destroy(seq);
1772	gzclose(fp);
1773	// free last!
1774	return 0;
1775}
1776
1777int stk_kfreq(int argc, char *argv[])
1778{
1779	gzFile fp;
1780	kseq_t *ks;
1781	int64_t i;
1782	int kmer, l, mask;
1783	char *nei;
1784
1785	if (argc < 2) {
1786		fprintf(stderr, "Usage: seqtk kfreq <kmer> <in.fa>\n");
1787		return 1;
1788	}
1789
1790	// get the k-mer
1791	l = strlen(argv[1]);
1792	for (i = kmer = 0; i < l; ++i) {
1793		int c = seq_nt6_table[(int)argv[1][i]];
1794		assert(c >= 1 && c <= 4);
1795		kmer = kmer << 2 | (c - 1);
1796	}
1797	mask = (1<<2*l) - 1;
1798
1799	// get the neighbors
1800	nei = calloc(1, 1<<2*l);
1801	for (i = 0; i < l; ++i) {
1802		int j, x;
1803		x = kmer & ~(3 << 2*i);
1804		for (j = 0; j < 4; ++j) 
1805			nei[x|j<<2*i] = 1;
1806	}
1807
1808	fp = argc == 2 || strcmp(argv[2], "-") == 0? gzdopen(fileno(stdin), "r") : gzopen(argv[2], "r");
1809	if (fp == 0) {
1810		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1811		return 1;
1812	}
1813	ks = kseq_init(fp);
1814	while (kseq_read(ks) >= 0) {
1815		int k, x[2], which;
1816		int64_t cnt[2], cnt_nei[2];
1817		x[0] = x[1] = k = 0;
1818		cnt[0] = cnt[1] = cnt_nei[0] = cnt_nei[1] = 0;
1819		for (i = 0; i < ks->seq.l; ++i) {
1820			int c = seq_nt6_table[(int)ks->seq.s[i]];
1821			if (c >= 1 && c <= 4) {
1822				x[0] = (x[0] << 2 | (c - 1)) & mask;
1823				x[1] = (x[1] >> 2 | (4 - c) << 2*(l-1));
1824				if (k < l) ++k;
1825				if (k == l) {
1826					if (x[0] == kmer) ++cnt[0];
1827					else if (x[1] == kmer) ++cnt[1];
1828					if (nei[x[0]]) ++cnt_nei[0];
1829					else if (nei[x[1]]) ++cnt_nei[1];
1830				}
1831			} else k = 0;
1832		}
1833		which = cnt_nei[0] > cnt_nei[1]? 0 : 1;
1834		printf("%s\t%ld\t%c\t%ld\t%ld\n", ks->name.s, ks->seq.l, "+-"[which], cnt_nei[which], cnt[which]);
1835	}
1836	kseq_destroy(ks);
1837	gzclose(fp);
1838	return 0;
1839}
1840
1841/* fqchk */
1842
1843typedef struct {
1844	int64_t q[94], b[5];
1845} posstat_t;
1846
1847static void fqc_aux(posstat_t *p, int64_t pos, int64_t allq[94], double perr[94], int qthres)
1848{
1849	int k;
1850	int64_t sum = 0, qsum = 0, sum_low = 0;
1851	double psum = 0;
1852	if (pos <= 0) printf("ALL");
1853	else printf("%ld", pos);
1854	for (k = 0; k <= 4; ++k) sum += p->b[k];
1855	printf("\t%lld", (long long)sum);
1856	for (k = 0; k <= 4; ++k)
1857		printf("\t%.1f", 100. * p->b[k] / sum);
1858	for (k = 0; k <= 93; ++k) {
1859		qsum += p->q[k] * k, psum += p->q[k] * perr[k];
1860		if (k < qthres) sum_low += p->q[k];
1861	}
1862	printf("\t%.1f\t%.1f", (double)qsum/sum, -4.343*log((psum+1e-6)/(sum+1e-6)));
1863	if (qthres <= 0) {
1864		for (k = 0; k <= 93; ++k)
1865			if (allq[k] > 0) printf("\t%.2f", 100. * p->q[k] / sum);
1866	} else printf("\t%.1f\t%.1f", 100. * sum_low / sum, 100. * (sum - sum_low) / sum);
1867	putchar('\n');
1868}
1869
1870int stk_fqchk(int argc, char *argv[])
1871{
1872	gzFile fp;
1873	kseq_t *seq;
1874	int c, k, offset = 33, n_diffQ = 0, qthres = 20;
1875	int64_t i, tot_len = 0, n = 0, max_len = 0, min_len = INT64_MAX, max_alloc = 0;
1876	double perr[94];
1877	posstat_t all, *pos = 0;
1878
1879	while ((c = getopt(argc, argv, "q:")) >= 0)
1880		if (c == 'q') qthres = atoi(optarg);
1881
1882	if (optind == argc) {
1883		fprintf(stderr, "Usage: seqtk fqchk [-q %d] <in.fq>\n", qthres);
1884		fprintf(stderr, "Note: use -q0 to get the distribution of all quality values\n");
1885		return 1;
1886	}
1887	fp = (strcmp(argv[optind], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind], "r");
1888	if (fp == 0) {
1889		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1890		return 1;
1891	}
1892	seq = kseq_init(fp);
1893	for (k = 0; k <= 93; ++k)
1894		perr[k] = pow(10., -.1 * k);
1895	perr[0] = perr[1] = perr[2] = perr[3] = .5;
1896	while (kseq_read(seq) >= 0) {
1897		if (seq->qual.l == 0) continue;
1898		++n;
1899		tot_len += seq->seq.l;
1900		min_len = min_len < seq->seq.l? min_len : seq->seq.l;
1901		max_len = max_len > seq->seq.l? max_len : seq->seq.l;
1902		if (max_len > max_alloc) {
1903			int old_max = max_alloc;
1904			max_alloc = max_len;
1905			kroundup64(max_alloc);
1906			pos = realloc(pos, max_alloc * sizeof(posstat_t));
1907			memset(&pos[old_max], 0, (max_alloc - old_max) * sizeof(posstat_t));
1908		}
1909		for (i = 0; i < seq->qual.l; ++i) {
1910			int q = seq->qual.s[i] - offset;
1911			int b = seq_nt6_table[(int)seq->seq.s[i]];
1912			b = b? b - 1 : 4;
1913			q = q < 93? q : 93;
1914			++pos[i].q[q];
1915			++pos[i].b[b];
1916		}
1917	}
1918	kseq_destroy(seq);
1919	gzclose(fp);
1920
1921	memset(&all, 0, sizeof(posstat_t));
1922	for (i = 0; i < max_len; ++i) {
1923		for (k = 0; k <= 93; ++k)
1924			all.q[k] += pos[i].q[k];
1925		for (k = 0; k <= 4; ++k)
1926			all.b[k] += pos[i].b[k];
1927	}
1928	for (k = n_diffQ = 0; k <= 93; ++k)
1929		if (all.q[k]) ++n_diffQ;
1930	printf("min_len: %ld; max_len: %ld; avg_len: %.2f; %d distinct quality values\n", min_len, max_len, (double)tot_len/n, n_diffQ);
1931	printf("POS\t#bases\t%%A\t%%C\t%%G\t%%T\t%%N\tavgQ\terrQ");
1932	if (qthres <= 0) {
1933		for (k = 0; k <= 93; ++k)
1934			if (all.q[k] > 0) printf("\t%%Q%d", k);
1935	} else printf("\t%%low\t%%high");
1936	putchar('\n');
1937	fqc_aux(&all, 0, all.q, perr, qthres);
1938	for (i = 0; i < max_len; ++i)
1939		fqc_aux(&pos[i], i + 1, all.q, perr, qthres);
1940	free(pos);
1941	return 0;
1942}
1943
1944int stk_size(int argc, char *argv[])
1945{
1946	gzFile fp;
1947	kseq_t *seq;
1948	uint64_t n = 0, l = 0;
1949	if (argc == 1 && isatty(fileno(stdin))) {
1950		fprintf(stderr, "Usage: seqtk size <in.fq>\n");
1951		return 1;
1952	}
1953	fp = argc > 1 && strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
1954	if (fp == 0) {
1955		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
1956		return 1;
1957	}
1958	seq = kseq_init(fp);
1959	while (kseq_read(seq) >= 0) {
1960		++n;
1961		l += seq->seq.l;
1962	}
1963	kseq_destroy(seq);
1964	gzclose(fp);
1965	printf("%lld\t%lld\n", (long long)n, (long long)l);
1966	return 0;
1967}
1968
1969int stk_telo(int argc, char *argv[])
1970{
1971	gzFile fp;
1972	kseq_t *seq;
1973	char *telo_seq = "CCCTAA";
1974	int c, i, j, len, absent, show_profile = 0, penalty = 1, max_drop = 2000, min_score = 300;
1975	uint64_t x, mask, sum_input = 0, sum_telo = 0;
1976	khash_t(64) *h;
1977
1978	while ((c = getopt(argc, argv, "m:p:d:s:P")) >= 0) {
1979		if (c == 'm') telo_seq = optarg;
1980		else if (c == 'p') penalty = atoi(optarg);
1981		else if (c == 'd') max_drop = atoi(optarg);
1982		else if (c == 's') min_score = atoi(optarg);
1983		else if (c == 'P') show_profile = 1;
1984	}
1985	if (penalty < 0) penalty = -penalty;
1986	if (argc == optind && isatty(fileno(stdin))) {
1987		fprintf(stderr, "Usage: seqtk telo [options] <in.fq>\n");
1988		fprintf(stderr, "Options:\n");
1989		fprintf(stderr, "  -m STR     motif [%s]\n", telo_seq);
1990		fprintf(stderr, "  -p INT     penalty [%d]\n", penalty);
1991		fprintf(stderr, "  -d INT     max drop [%d]\n", max_drop);
1992		fprintf(stderr, "  -s INT     min score [%d]\n", min_score);
1993		fprintf(stderr, "  -P         print scoring\n");
1994		return 1;
1995	}
1996
1997	len = strlen(telo_seq);
1998	mask = (1ULL<<2*len) - 1;
1999
2000	h = kh_init(64); // hash table for all roations of the telomere motif
2001	kh_resize(64, h, len * 2);
2002	for (i = 0; i < len; ++i) {
2003		for (j = 0, x = 0; j < len; ++j) {
2004			int c = seq_nt6_table[(uint8_t)telo_seq[(i + j) % len]];
2005			assert(c >= 1 && c <= 4);
2006			x = x<<2 | (c-1);
2007		}
2008		kh_put(64, h, x, &absent);
2009	}
2010
2011	fp = argc > 1 && strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
2012	if (fp == 0) {
2013		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
2014		return 1;
2015	}
2016	seq = kseq_init(fp);
2017	while (kseq_read(seq) >= 0) {
2018		ssize_t i, l, max_i = -1, st = 0;
2019		int64_t score, max;
2020		sum_input += seq->seq.l;
2021		score = max = 0, max_i = -1;
2022		for (i = 0, l = 0, x = 0; i < seq->seq.l; ++i) { // 5'-end, check CCCTAA
2023			int hit = 0, c = seq_nt6_table[(uint8_t)seq->seq.s[i]];
2024			if (c >= 1 && c <= 4) { // not N
2025				x = (x<<2 | (c-1)) & mask;
2026				if (++l >= len && kh_get(64, h, x) != kh_end(h)) // x is at least 6bp long and is a telomere motif
2027					hit = 1;
2028			} else l = 0, x = 0; // N, ambiguous base
2029			if (show_profile) printf("P\t%s\t%d\t%d\t%d\n", seq->name.s, i, score, max);
2030			if (i >= len) score += hit? 1 : -penalty;
2031			if (score > max) max = score, max_i = i;
2032			else if (max - score > max_drop) break;
2033		}
2034		if (max >= min_score) {
2035			if (!show_profile) printf("%s\t0\t%ld\t%ld\n", seq->name.s, max_i + 1, seq->seq.l);
2036			sum_telo += max_i + 1;
2037			st = max_i + 1;
2038		}
2039		score = max = 0, max_i = -1;
2040		for (i = seq->seq.l - 1, l = 0, x = 0; i >= st; --i) { // 3'-end; similar to the for loop above
2041			int hit = 0, c = seq_nt6_table[(uint8_t)seq->seq.s[i]];
2042			if (c >= 1 && c <= 4) {
2043				x = (x<<2 | (4-c)) & mask;
2044				if (++l >= len && kh_get(64, h, x) != kh_end(h))
2045					hit = 1;
2046			} else l = 0, x = 0;
2047			if (show_profile) printf("Q\t%s\t%d\t%d\t%d\n", seq->name.s, seq->seq.l - i, score, max);
2048			if (seq->seq.l - i >= len) score += hit? 1 : -penalty;
2049			if (score > max) max = score, max_i = i;
2050			else if (max - score > max_drop) break;
2051		}
2052		if (max >= min_score) {
2053			if (!show_profile) printf("%s\t%ld\t%ld\t%ld\n", seq->name.s, max_i, seq->seq.l, seq->seq.l);
2054			sum_telo += seq->seq.l - max_i;
2055		}
2056	}
2057	kh_destroy(64, h);
2058	kseq_destroy(seq);
2059	gzclose(fp);
2060	fprintf(stderr, "%ld\t%ld\n", (long)sum_telo, (long)sum_input);
2061	return 0;
2062}
2063
2064/* main function */
2065static int usage()
2066{
2067	fprintf(stderr, "\n");
2068	fprintf(stderr, "Usage:   seqtk <command> <arguments>\n");
2069	fprintf(stderr, "Version: 1.5-r133\n\n");
2070	fprintf(stderr, "Command: seq       common transformation of FASTA/Q\n");
2071	fprintf(stderr, "         size      report the number sequences and bases\n");
2072	fprintf(stderr, "         comp      get the nucleotide composition of FASTA/Q\n");
2073	fprintf(stderr, "         sample    subsample sequences\n");
2074	fprintf(stderr, "         subseq    extract subsequences from FASTA/Q\n");
2075	fprintf(stderr, "         fqchk     fastq QC (base/quality summary)\n");
2076	fprintf(stderr, "         mergepe   interleave two PE FASTA/Q files\n");
2077	fprintf(stderr, "         split     split one file into multiple smaller files\n");
2078	fprintf(stderr, "         trimfq    trim FASTQ using the Phred algorithm\n\n");
2079	fprintf(stderr, "         hety      regional heterozygosity\n");
2080	fprintf(stderr, "         gc        identify high- or low-GC regions\n");
2081	fprintf(stderr, "         mutfa     point mutate FASTA at specified positions\n");
2082	fprintf(stderr, "         mergefa   merge two FASTA/Q files\n");
2083	fprintf(stderr, "         famask    apply a X-coded FASTA to a source FASTA\n");
2084	fprintf(stderr, "         dropse    drop unpaired from interleaved PE FASTA/Q\n");
2085	fprintf(stderr, "         rename    rename sequence names\n");
2086	fprintf(stderr, "         randbase  choose a random base from hets\n");
2087	fprintf(stderr, "         cutN      cut sequence at long N\n");
2088	fprintf(stderr, "         gap       get the gap locations\n");
2089	fprintf(stderr, "         listhet   extract the position of each het\n");
2090	fprintf(stderr, "         hpc       homopolyer-compressed sequence\n");
2091	fprintf(stderr, "         telo      identify telomere repeats in asm or long reads\n");
2092	fprintf(stderr, "\n");
2093	return 1;
2094}
2095
2096int main(int argc, char *argv[])
2097{
2098	if (argc == 1) return usage();
2099	if (strcmp(argv[1], "comp") == 0) return stk_comp(argc-1, argv+1);
2100	else if (strcmp(argv[1], "fqchk") == 0) return stk_fqchk(argc-1, argv+1);
2101	else if (strcmp(argv[1], "hety") == 0) return stk_hety(argc-1, argv+1);
2102	else if (strcmp(argv[1], "gc") == 0) return stk_gc(argc-1, argv+1);
2103	else if (strcmp(argv[1], "subseq") == 0) return stk_subseq(argc-1, argv+1);
2104	else if (strcmp(argv[1], "mutfa") == 0) return stk_mutfa(argc-1, argv+1);
2105	else if (strcmp(argv[1], "mergefa") == 0) return stk_mergefa(argc-1, argv+1);
2106	else if (strcmp(argv[1], "mergepe") == 0) return stk_mergepe(argc-1, argv+1);
2107	else if (strcmp(argv[1], "dropse") == 0) return stk_dropse(argc-1, argv+1);
2108	else if (strcmp(argv[1], "randbase") == 0) return stk_randbase(argc-1, argv+1);
2109	else if (strcmp(argv[1], "cutN") == 0) return stk_cutN(argc-1, argv+1);
2110	else if (strcmp(argv[1], "gap") == 0) return stk_gap(argc-1, argv+1);
2111	else if (strcmp(argv[1], "listhet") == 0) return stk_listhet(argc-1, argv+1);
2112	else if (strcmp(argv[1], "famask") == 0) return stk_famask(argc-1, argv+1);
2113	else if (strcmp(argv[1], "trimfq") == 0) return stk_trimfq(argc-1, argv+1);
2114	else if (strcmp(argv[1], "hrun") == 0) return stk_hrun(argc-1, argv+1);
2115	else if (strcmp(argv[1], "sample") == 0) return stk_sample(argc-1, argv+1);
2116	else if (strcmp(argv[1], "seq") == 0) return stk_seq(argc-1, argv+1);
2117	else if (strcmp(argv[1], "kfreq") == 0) return stk_kfreq(argc-1, argv+1);
2118	else if (strcmp(argv[1], "rename") == 0) return stk_rename(argc-1, argv+1);
2119	else if (strcmp(argv[1], "split") == 0) return stk_split(argc-1, argv+1);
2120	else if (strcmp(argv[1], "hpc") == 0) return stk_hpc(argc-1, argv+1);
2121	else if (strcmp(argv[1], "size") == 0) return stk_size(argc-1, argv+1);
2122	else if (strcmp(argv[1], "telo") == 0) return stk_telo(argc-1, argv+1);
2123	else {
2124		fprintf(stderr, "[main] unrecognized command '%s'. Abort!\n", argv[1]);
2125		return 1;
2126	}
2127}