I have a large number of .fastq.gz files of different lane and reads. I have to merge them each reads group files into single .fastq.gz files.


1st type

2nd type

3rd type

4th type

so now i need to merge all the corresponding files into single file.


I tried ""cat'" but its not possible to do for large files. Could any one help me to run in script parallel in perl or python or shell. or any linux command which can be used for large file.

Thanks all.

  • 2
    $\begingroup$ Why is cat not possible for large files, please tell more about the size and number of files and/or the error you get when running the cat command. $\endgroup$ – benn May 16 '19 at 10:32
  • $\begingroup$ the folder size 40gb.. there are >200 files for each type.. so I couldn't do it manually thats why I am looking for scripts or any efficient command $\endgroup$ – Nitha May 16 '19 at 10:44
  • 1
    $\begingroup$ Did you try cat NA24694_GCCAAT_L001_R1_???.fastq.gz > EA00694_GCCAAT_L001_R1.fastq.gz? $\endgroup$ – benn May 16 '19 at 10:47
  • $\begingroup$ You need to dump your data on an external harddrive with sufficient. If you run out of harddrive it will throw an exception, or destroy the data. You are requesting a solution in higher order programming, I don't think its needed, just carefully crafted key words to build the file up in stages rather than all at once. $\endgroup$ – Michael May 16 '19 at 12:29

All you need is cat. You won't find any better tool for a simple job like this. Just run:

cat NA24694_GCCAAT_L001_R1*fastq.gz > EA00694_GCCAAT_L001_R1.fastq.gz
cat NA24694_GCCAAT_L001_R2*fastq.gz > EA00694_GCCAAT_L001_R2.fastq.gz
cat NA24694_GCCAAT_L001_R3*fastq.gz > EA00694_GCCAAT_L002_R1.fastq.gz
cat NA24694_GCCAAT_L001_R4*fastq.gz > EA00694_GCCAAT_L002_R2.fastq.gz

That should work just fine. You say you tried cat and it "didn't work" but since you don't tell us how it failed, I can't really help. The only issues I can think of is that either there are too many files (as in several hundred thousand, whatever the value returned by getconf ARG_MAX on your system which is 2097152 on mine) or, more likely, you are running out of disk space.

If it's a disk space issue, you might be able to get around it by adding each file and then deleting it:

for i in 1 2 3 4; do 
    for file in NA24694_GCCAAT_L001_R${i}_*fastq.gz; do
        cat "$file" >> EA00694_GCCAAT_L001_R${i}.fastq.gz && rm "$file"

IMPORTANT: the command above is destructive. It will delete each file after it has been added to the new one. If everything works fine that's not a problem, but if I've made a mistake or your file names are slightly different you might lose your data. So I strongly urge you to simply run the cat commands in the first section on a machine with enough disk space to deal with them.

| improve this answer | |
  • $\begingroup$ I was using cat command manually by adding to file names to it I had these types of files EA13698_AGTAAT_L001_R1_001.fastq.gz EA13698_AGTAAT_L001_R2_032.fastq.gz EA13698_AGTAAT_L002_R1_003.fastq.gz EA13698_AGTAAT_L002_R2_037.fastq.gz I had these the four types of files, in each group it had more than 50 files so I couldnt keep on adding names to cat command. eg "cat EA13698_AGTAAT_L001_R1_001.fastq.gz EA13698_AGTAAT_L001_R1_002.fastq.gz .... > EA13698_AGTAAT_L001_R1.fastq.gz. manually doing this it takes time so I was searching for script which will do looping. $\endgroup$ – Nitha May 17 '19 at 4:32
  • $\begingroup$ No need to loop, just do them all at once with globbing. 50 files won't be too long and you won't have to type them out. $\endgroup$ – Devon Ryan May 17 '19 at 6:42
  • $\begingroup$ IMO the way I would do it is with ">>" (append) rather than brute forcing the concatenation with ">" (over-ride) ... which is what the loop is about. I like the shell loop. $\endgroup$ – Michael May 17 '19 at 10:22
  • $\begingroup$ As said by @terdon, I had used * to group the types of file in 'cat'.. it worked. Thanks all. $\endgroup$ – Nitha May 17 '19 at 10:38

We sequence DNA on daily basis. Like terdon said, you can use cat to group all the FastQ of all your sample whether they are bgzipped or not like so:

root@slurm1: ~  # ls

You could do:

# initiate am empty array

# loop to add the unique samples to an array
for i in *.fastq.gz;
    if [[ "$sampleName" =~ "${listSamples[@]}" ]]; then
        sampleName=$(echo $i | cut -d'_' -f1)

# loop in the newly created array of samples and group them by reads number
for i in "${listSamples[@]}"
    cat "$i"_*_R1_* > "$i"_1.fastq.gz
    cat "$i"_*_R2_* > "$i"_2.fastq.gz

However, I highly suggest you to compress your files as .bz2 to save storage as much as possible. You can achieve this using the multithreaded tool lbzip2. Assuming that you have 10 CPUs available, you can do:

for i in "${listSamples[@]}"
    bzcat "$i"_*_R1_* | lbzip2 -n 10 --best > "$i"_1.fastq.bz2
    bzcat "$i"_*_R2_* | lbzip2 -n 10 --best > "$i"_2.fastq.bz2

Hope this helps !

| improve this answer | |
  • $\begingroup$ I like the bz2 storage suggestion $\endgroup$ – Michael May 20 '19 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.