This is a test version of Biostars. For the public version, visit https://www.biostars.org.
calculating read depth via a for in loop over many fastq files ?

I have been trying to get the depth of several fastq files using for in loop approach: I have used this code to print out name of each fastq then the depth of that file: what I used was:

  Sample=(*.fastq)
for f in *.fastq
do
Depth=$(grep -c @ $f)
echo "$Sample:$Depth" >> Depth_before_trimm
done

but what it produces is the file name of the first fastq and keep repeating it again instead of the names of other files.

Could you tell what might be causing this ?

linux r bash

you cannot get a "DEPTH" from a fastq file. the depth is associated to BAM/SAM/CRAM/VCF files.

2 answers

What you need is count bases, not reads. See this post

grep -c @ $f

are you trying to count the number of lines in the fastqs ? do not ever try to use '@' because '@' can also be present in the quality string.

Here is a loop

rm -f output.txt
for F in *.fastq
do
   echo -n "$F" >> output.txt
   cat "$F" | paste - - - - | wc -l  >> output.txt
done

I really appreciate this, but I am trying to make it with even a simple loop, the one I provided: could you please let me know what wrong I have done with that loop ?

I know it is trivial, but I want to create a variable with all names of the files, and then pass it to the echo within the loop.

Log in to answer this question.