Created
December 23, 2014 04:22
-
-
Save lh3/74ad583c2d7bb16fb1f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bzlib.h> | |
#include <zlib.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct zfile_s { | |
int is_bz; | |
gzFile fpz; | |
FILE *fp; // for bzip2 | |
BZFILE *fpbz; | |
} *zfile_t; | |
static inline ssize_t zfile_read(zfile_t fp, void *buf, size_t len) | |
{ | |
if (fp->is_bz) { | |
int err; | |
return BZ2_bzRead(&err, fp->fpbz, buf, len); | |
} else return gzread(fp->fpz, buf, len); | |
} | |
zfile_t zfile_open(const char *fn, int is_bz) | |
{ | |
zfile_t fp; | |
fp = calloc(1, sizeof(struct zfile_s)); | |
if (is_bz) { | |
int err; | |
fp->is_bz = 1; | |
fp->fp = fopen(fn, "rb"); | |
fp->fpbz = BZ2_bzReadOpen(&err, fp->fp, 0, 0, 0, 0); | |
} else fp->fpz = fn? gzopen(fn, "r") : gzdopen(fileno(stdin), "r"); | |
return fp; | |
} | |
void zfile_close(zfile_t fp) | |
{ | |
if (fp->is_bz) { | |
int err; | |
BZ2_bzReadClose(&err, fp->fpbz); | |
fclose(fp->fp); | |
} else gzclose(fp->fpz); | |
free(fp); | |
} | |
#include "kseq.h" | |
KSEQ_INIT(zfile_t, zfile_read) | |
#include <unistd.h> | |
int main(int argc, char *argv[]) | |
{ | |
zfile_t fp; | |
kseq_t *seq; | |
int c, l, is_bz = 0; | |
while ((c = getopt(argc, argv, "b")) >= 0) | |
if (c == 'b') is_bz = 1; | |
if (argc == optind) { | |
fprintf(stderr, "Usage: %s [-b] <in.fasta>\n", argv[0]); | |
return 1; | |
} | |
fp = zfile_open(argv[optind], is_bz); | |
seq = kseq_init(fp); | |
while ((l = kseq_read(seq)) >= 0) { | |
printf("name: %s\n", seq->name.s); | |
if (seq->comment.l) printf("comment: %s\n", seq->comment.s); | |
printf("seq: %s\n", seq->seq.s); | |
if (seq->qual.l) printf("qual: %s\n", seq->qual.s); | |
} | |
printf("return value: %d\n", l); | |
kseq_destroy(seq); | |
zfile_close(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment