For loop in bash script
0
0
Entering edit mode
17 months ago
Jinsoul • 0

Hi all,

I'm expecting to get 17 different paired-end fastq files (34 in total), so I want to make a bash script to just run my code through all the fastq files in a directory at once. How can I change the name of the input and output files each time the script runs though each file? So when it moves to the file_002, all names have file_002 at the beginning instead of file_001 and so on. And also, when merging the R1 and R2 reads how can I make that it only merges the corresponding files with a loop?

for file in directory_name
do
 pear -f file_001_R1.fastq.gz -r file_001_R2.fastq.gz -o file_001.fastq
 cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o file_001_barcode.fastq file_001.fastq
 cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o file_001_UMI.fastq file_001.fastq
 seqkit concat file_001_barcode.fastq file_001_UMI.fastq > file_001_concatenation.fastq
done

Thank you

bash • 702 views
ADD COMMENT
0
Entering edit mode

You could try something like this:

i=1

for file in *; do
    pear -f file_00$i_R1.fastq.gz -r file_00$i_R2.fastq.gz -o file_00$i.fastq
    cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o file_00$i_barcode.fastq file_001.fastq
    cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o file_001_UMI.fastq file_00$i.fastq
    seqkit concat file_00$i_barcode.fastq file_00$i_UMI.fastq > file_00$i_concatenation.fastq

    let i++

done
ADD REPLY
1
Entering edit mode

This is a fairly clunky approach.

It would be better to use bash parameter expansion. Some examples of this are shown here: How to run BWA or the other aligner for paired .fastq in a bash loop and pipeline?

ADD REPLY

Login before adding your answer.

Traffic: 2595 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6