5

My directory structure is given below

x:\Project_2012-158A\Sample_4041

SampleSheet.csv
4041_CGTACG_L002_R1_001.fastq
4041_CGTACG_L002_R2_001.fastq
4041_CGTACG_L006_R2_001.fastq
4041_CGTACG_L006_R1_001.fastq

x:\Project_2012-158A\Sample_4027

SampleSheet.csv
4027_TAGCTT_L002_R2_001.fastq
4027_TAGCTT_L006_R1_001.fastq
4027_TAGCTT_L002_R1_001.fastq
4027_TAGCTT_L006_R2_001.fastq

x:\Project_2012-158A\Sample_D425

SampleSheet.csv
D425_ACTGAT_L008_R2_001.fastq
D425_ACTGAT_L008_R1_001.fastq
D425_ACTGAT_L004_R2_001.fastq
D425_ACTGAT_L004_R1_001.fastq

I want to concatenate the files with "R1" and "R2" separately for every sample. I know

cat file1.fastq file2.fastq > concatenation.fastq 

will give concatenate but how do I do that for all the sudirectories with single script?

2
  • 3
    Backslashes and drive letters ... are you on Windows? Unless you are asking about something like cygwin, you are probably on the wrong site.
    – jw013
    Commented Nov 14, 2012 at 18:09
  • No. This directory structure I created using windows. I want to perform my tasks on Linux.
    – Jana
    Commented Nov 14, 2012 at 18:43

2 Answers 2

6
cd /path/to/Project_2012-158A &&
for dir in Sample*/; do
    for r in R1 R2; do
        outfile=${dir%/}_${r}.fastq
        glob=*_${r}_*.fastq
        cat "$dir"/$glob > "$dir/$outfile" &&
        rm -f "$dir"/$glob
    done
done
10
  • Thanks @jw013. I'm new here. I wish I could vote you up.
    – Jana
    Commented Nov 14, 2012 at 21:02
  • 1
    Note that except with zsh, that would create empty files for Sample directories that don't have R1 or R2 files. Commented Nov 14, 2012 at 21:02
  • @StephaneChazelas You are right. If the OP wants, it'd be trivial to check for and remove the empty files. I also considered adding lines to remove the original files but eventually decided not to unless the OP specifically asks for it. For now, I just gave a simplest example.
    – jw013
    Commented Nov 14, 2012 at 21:05
  • @jw013 Is there anyways I could save the file names with SampleXXX_R1.fastq and SampleXXX_R2.fastq for every samples? and also delete the original files?
    – Jana
    Commented Nov 15, 2012 at 22:13
  • @jw013 I'm getting this error Sample_1008//Sample_1008//Sample_1008/_R1.fastq: No such file or directory concatinate_final.sh: line 6: Sample_1008//Sample_1008//Sample_1008/_R2.fastq: No such file or directory concatinate_final.sh: line 6: Sample_1009//Sample_1009//Sample_1009/_R1.fastq: No such file or directory concatinate_final.sh: line 6: Sample_1009//Sample_1009//Sample_1009/_R2.fastq: No such file or directory
    – Jana
    Commented Nov 16, 2012 at 0:34
4

Like this:

cat ./*/*R2* > result

* - match everything

Be aware that this will get every occurrence of R2 in the filename.

2
  • Thanks @week, but this concatenate all files with R2 into one file. I don't want that. I wan't the files to be concatenated under "every" sub directory. ex: under 'Sample_4041' directory all files with R2 should be concatenated and the same applies to other sub directories as well. At the end I'll have only two files under each sample sub directory
    – Jana
    Commented Nov 14, 2012 at 19:11
  • 2
    @Jana add that to your question - it's an important part of the requirement.
    – jw013
    Commented Nov 14, 2012 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .