Thanks gorge. I will try samtools to count reads. Actually, I counted
fastq file and sam file by C. The fastq file line * 1/4 would be the
fastq reads count, and the sam file line *1/2 would be the mapped
reads count right?
I have never done this kind of analysis. However, I have been using bwa for a year now. Although I know about samtools, I often use python or C++ to analyze my SAM files directly. It's just faster to count everything I want in one pass rather than running samtools many times.
If you use bwa then everything you need should be in the SAM file. There should be a line for all your reads both mapped and unmapped. You don't need the FASTQ file to figure out how many reads you have total or which ones are mapped.
It makes a lot of sense to just use the samtool commands that I suggested, just to take out the unknown factor of whether your C code is correct.
Note that to count which reads are mapped, you have to look at the second column of the SAM file, the number there should NOT have the third bit set. In C, this would be something like:
if (!(flag&4)) mapped_count++;
You should ignore all lines that start with '@' and count the rest. Then your mapped reads are
If you are not doing something like this then your counts might not be right.