-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfqextract.c
33 lines (30 loc) · 900 Bytes
/
fqextract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "khash.h" // khash.h from samtools/bwa/seqtk/klib
KHASH_SET_INIT_STR(s)
// Heng Li's code, taken from https://www.biostars.org/p/10353/
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
char buf[BUF_SIZE];
FILE *fp;
int lineno = 0, flag = 0, ret;
khash_t(s) *h;
if (argc == 1) {
fprintf(stderr, "Usage: cat in.fq | fqextract <name.lst>\n");
return 1;
}
h = kh_init(s);
fp = fopen(argv[1], "rb"); // FIXME: check fp
while (fgets(buf, BUF_SIZE, fp))
kh_put(s, h, strdup(buf), &ret); // FIXME: check ret
fclose(fp);
while (fgets(buf, BUF_SIZE, stdin)) {
if (++lineno%4 == 1)
flag = (kh_get(s, h, buf + 1) != kh_end(h));
if (flag) fputs(buf, stdout);
}
kh_destroy(s, h); // FIXME: free keys before destroy
return 0;
}