// gcc -g -O3 -Wall fastq_count_nomal.c -o fastq_count_nomal -lz
#include
#include
#include
#include
#include
#include
#include
#include
//#include
struct globalArgs_t {
const char *infile; /* -i option */
int verbosity; /* -v option */
} globalArgs;
void load_file(gzFile fp,unsigned long *Line_num);
static inline int readNextNode(gzFile fq,char *buf[4]) ;
void display_usage(char * argv[]);
static inline long long usec(void);
static gzFile open_input_stream(const char *filename);
long long usec(void){
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}
void display_usage(char * argv[]){
char *buffer=(char* )malloc(10240*sizeof(char));
const char* usage=
"\nCopyright (c) 2020\n" \
"Contact: XiongXu \n" \
"Discription:\n This program is used for cutting the reads to get the specified cycles.\n" \
"Usage: %s [-i Infile] [-o OUTFILE] [-s start] [-e end] [-h] \n" \
"Example1:\n %s -i /share/work1/staff/xuxiong/test/13C37198_L7_I012.R1.clean.fastq.gz -s 0 -e 100 -o out \n" \
"Example2:\n zcat /share/work1/staff/xuxiong/test/13C37198_L7_I012.R1.clean.fastq.gz |%s -e 100 -o sss \n" \
"Example3:\n zcat /share/work1/staff/xuxiong/test/13C37198_L7_I012.R1.clean.fastq.gz |%s -e 50 |less -S \n" \
"\n" \
" [-i Infile] = Infile.default is stdin [required]\n" \
" [-v ] = verbose mod [option]\n" \
" [-h] This helpful help screen. [option]\n" \
"\n";
sprintf(buffer,usage,argv[0],argv[0],argv[0],argv[0]);
fprintf(stderr,"%s",buffer);
free(buffer);
exit(1);
}
gzFile open_input_stream(const char *filename){
int fd ;
if (strncmp(filename,"-", 1)==0 || !strcmp(filename,"")) {
fd = STDIN_FILENO;
} else {
#if defined(__APPLE__) && defined(__MACH__) || defined(unix) || defined(linux)
fd = open(filename, O_CREAT | O_RDONLY, 0666 );
#else
fd = _open(filename, _O_CREAT|_O_BINARY | _O_RDONLY , 0666 );
#endif
if (fd==-1) fprintf(stderr, "Failed to create input file (%s)", filename);
}
gzFile in = gzdopen(fd,"rb");
return in;
}
int readNextNode(gzFile fq,char *buf[4]) {
int i=1;
buf[0]=gzgets(fq,buf[0],1024*sizeof(char));
if (!gzeof(fq)) {
buf[1]=gzgets(fq,buf[1],1024*sizeof(char));
buf[2]=gzgets(fq,buf[2],1024*sizeof(char));
buf[3]=gzgets(fq,buf[3],1024*sizeof(char));
if (globalArgs.verbosity) fprintf(stdout,"%s%s%s%s",buf[0],buf[1],buf[2],buf[3]);
} else {
return 0;
}
return i;
}
void load_file(gzFile fp,unsigned long *Line_num) {
long long begin,end;
begin=usec();
char *buf[4];
int i=0;
for(i=0;i<4;++i) {
buf[i] = (char *)calloc(1024,sizeof(char));
}
while (1) {
int status=readNextNode(fp,buf);
if (!status) break;
(*Line_num)++;
}
end=usec();
fprintf(stderr,"Total_reads: %lu\nFinished in %.3f s\n",*Line_num,(double)(end-begin)/CLOCKS_PER_SEC);
for(i=0;i<4;++i) {
free(buf[i]);
}
}
int main(int argc, char *argv[])
{
int opt = 0;
globalArgs.infile="-";
globalArgs.verbosity=0;
const char *optString = "i:vh?";
if (argc<2){
display_usage(argv);
exit(1);
}
opt = getopt( argc, argv, optString );
while( opt != -1 ) {
switch( opt ) {
case 'i':
globalArgs.infile = optarg;
break;
case 'v':
globalArgs.verbosity++;
break;
case '?': /* fall-through is intentional */
case 'h':
display_usage(argv);
break;
default:
fprintf(stderr,"error parameter!\n");
/* You won't actually get here. */
break;
}
opt = getopt( argc, argv, optString );
}
gzFile in=open_input_stream(globalArgs.infile);
unsigned long reads_num=0;
load_file(in,&reads_num);
gzclose(in);
return 0;
}