This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to decompress several fastq.gz files into one single file?

How to decompress several fastq.gz files and concatenate them into one single .fastq file? There are two kinds of file in the same folder, some ending in ...R2_001.fastq.gz , and others ending in R1_001.fastq.gz. The work is to decompress and concatenate files ending in ...R2_001.fastq.gz into one R2_001.fastq file, and do the same for ...R2_001.fastq.gz files. Really appreciate any help. Thanks -madza

next-gen

Didn't know this was a prob. Thanks for letting me know. -madza

What if we wanted that name of the file is also the name of the output file?

Please use ADD COMMENT/ADD REPLY when responding to existing posts to keep threads logically organized. SUBMIT ANSWERS is used only for submitting new answers for original question.

4 answers

Hi Madza

Another approach could be

For Compressed output files:

cat *R1*.fastq.gz >> R1.fastq.gz
cat *R2*.fastq.gz >> R2.fastq.gz

For Uncompressed output files:

zcat *R1*.fastq.gz >> R1.fastq
zcat *R2*.fastq.gz >> R2.fastq

Thanks, Persistent LABS

Hi Madza,

Can you give it a try?

For file ending in ...R1_001.fastq.gz

find -name '*R1_001.fastq.gz' | xargs gunzip -c  >> all_R1_001.fastq

similarly or file ending in ...R2_001.fastq.gz

find -name '*R2_001.fastq.gz' | xargs gunzip -c  >> all_R2_001.fastq

Let me know if it worked for you.

Actually cat is enough.

cat 1.fq.gz 2.fq.gz 3.fq.gz ... > all.fq.gz

or

cat *.fq.gz > all.fq.gz

OP asked for uncompressed fastq at the end, so you'll have to add in a gunzip -c ;)

It will be a pain to type (of course with tab completion) all the file names specially when there are say 100 such files and that too for R1 and R2 separately.

That's why I suggested to use a wildcard

Using GNU Parallel:

parallel -j1 'zcat *{}_001.fastq.gz > {}_001.fastq' ::: R1 R2

If you disk system is fast, you can increase -j1.

Gnu parallel is amazing. I find this more intuitive and easier syntax to remember:

find . -name '*.fastq' | parallel -j 1 'zcat {} > {.}.combined.fastq'

But your example will not decompress and append foo_R1_001.fastq.gz and bar_R1_001.fastq.gz into R1_001.fastq

Yes, you are right about that. My code should be altered to capture either R1 or R2 and write output accordingly, and this should then be ran twice slightly modified. Therefore your example is definitely better.

What if we wanted that name of the file is also the name of the output file?

Log in to answer this question.