make HTSLIB=/path/to/htslib
Created
September 22, 2022 19:33
-
-
Save lindenb/a8a827b20e7004b51a1d51a5ba4f82e0 to your computer and use it in GitHub Desktop.
biostar9539326.c proper way to alter bgzf-compressed fastq files using htslib, multithreaded
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
/* | |
The MIT License (MIT) | |
Copyright (c) 2022 Pierre Lindenbaum PhD. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <assert.h> | |
#include <getopt.h> | |
#include <stdint.h> | |
#include "signal.h" | |
#include <zlib.h> | |
#include "htslib/kseq.h" | |
#include "htslib/kstring.h" | |
KSEQ_INIT(gzFile, gzread) | |
static void usage(FILE* out) { | |
fprintf(out,". Pierre Lindenbaum 2022.\n"); | |
fprintf(out,"Usage:\n"); | |
fprintf(out," biostar9539326 -C (compression-level 0-9) -o out.fq.gz <fastq|stdin> \n"); | |
fprintf(out,"\n"); | |
} | |
int main(int argc,char** argv) { | |
int opt; | |
char* filename_out = NULL; | |
int level=5; | |
gzFile fp1; | |
gzFile out; | |
kseq_t* ks1; | |
char mode[5]; | |
while ((opt = getopt(argc, argv, "ho:C:")) != -1) { | |
switch (opt) { | |
case 'h': | |
usage(stdout); | |
return 0; | |
case 'C': level=atoi(optarg); | |
break; | |
case 'o': | |
filename_out = optarg; | |
break; | |
case '?': | |
fprintf(stderr,"unknown option '%c'.\n",(char)optopt); | |
return EXIT_FAILURE; | |
default: /* '?' */ | |
fprintf(stderr,"unknown option\n"); | |
return EXIT_FAILURE; | |
} | |
} | |
if(level<0 || level>9) { | |
fprintf(stderr,"Bad compression level: %d.\n",level ); | |
usage(stderr); | |
return EXIT_FAILURE; | |
} | |
if(!(argc==optind || optind+1==argc)) | |
{ | |
fprintf(stderr,"Illegal number of arguments.\n"); | |
usage(stderr); | |
return EXIT_FAILURE; | |
} | |
fp1 = (optind==argc?gzdopen(fileno(stdin), "r"):gzopen(argv[optind], "r")); | |
if(fp1==NULL) { | |
fprintf(stderr,"Cannot open %s.(%s)\n",(optind==argc?"<STDIN>":argv[optind]),strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
ks1 = kseq_init(fp1); | |
if(ks1==NULL) { | |
fprintf(stderr,"Cannot initialize reader for \"%s\".\n",(optind==argc?"<STDIN>":argv[optind])); return EXIT_FAILURE; | |
} | |
sprintf(mode,"wb%d",level); | |
out = (filename_out==NULL?gzdopen(fileno(stdout),mode):gzopen(filename_out,mode)); | |
if( out==NULL ) { | |
fprintf(stderr,"Cannot open %s for writing.(%s)\n",(filename_out==NULL?"-":filename_out),strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
#define KSWRITE(X) gzwrite(out,(void*)(X.s),X.l) | |
while (kseq_read(ks1) >= 0) { | |
kputs("HELLO WORLD",&(ks1->name)); | |
gzputc(out,'@'); | |
KSWRITE(ks1->name); | |
gzputc(out,'\n'); | |
KSWRITE(ks1->seq); | |
gzputc(out,'\n'); | |
gzputc(out,'+'); | |
gzputc(out,'\n'); | |
KSWRITE(ks1->qual); | |
if(gzputc(out,'\n')==-1) { | |
fprintf(stderr,"I/O error\n"); | |
return EXIT_FAILURE; | |
} | |
} | |
kseq_destroy(ks1); | |
gzclose(fp1); | |
gzclose(out); | |
return EXIT_SUCCESS; | |
} |
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
CC?=gcc | |
ifeq ($(HTSLIB),) | |
$(error undefined $$HTSLIB) | |
endif | |
CFLAGS= -Wall -O3 -I$(HTSLIB) | |
LDFLAGS= -L$(HTSLIB) -lz -lhts | |
biostar9539326 : biostar9539326.c | |
$(CC) -o $@ $(CFLAGS) $< $(LDFLAGS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment