For loop with 2 variables
1
1
Entering edit mode
3.8 years ago
quentin54520 ▴ 120

I have a large number of files. It is paired end sequencing. My files are of type _1.fastq.gz and _2.fastq.gz.

In the fastp command I must enter the 2 files corresponding to the same samples in _1 and _2 in the form fastp -i file_a_1.fastq.gz -I file_a_2.fastq.gz.

How could I do this for all my files without having to manually write the command with each file? When there is only one variable I do a loop for i in $ (ls Fastq_dir / *. fastq.gz) do ...

But with 2 variables?

bash fastp • 3.4k views
ADD COMMENT
0
Entering edit mode

Using parallel, try: parallel --dry-run fastp -i {} {=s/_1/_2/=} ::: *_1.fastq.gz or in bash loop try one of do echo $i ${i/_1/_2} or do echo $i ${i%_*}_2.fastq.gz.

ADD REPLY
1
Entering edit mode
3.8 years ago
Sam ★ 4.8k

Assume all your data follow the same format:

for i in `ls *_1.fastq.gz`;
do
    second=`echo ${i} | sed 's/_1/_2/g'`
    # Do what ever with first fastq be ${i} and second be ${second}
done
ADD COMMENT
0
Entering edit mode

Thanks a lot!

I have only one issue with that : my files is like this :

Sample_A_1_1.fastq.gz
Sample_A_1_2.fastq.gz
Sample_A_2_1.fastq.gz
Sample_A_2_2.fastq.gz
..

So it could replace Sample_A_1_1.fastq.gz by Sample_A_2_2.fastq.gz?

Instead of sed 's/_1/_2/g' maybe it could work with sed 's/_1.fastq/_2.fastq/g'

ADD REPLY
2
Entering edit mode

Use bash parameter expansion.

do_whatever ${i} ${i/_1.fastq.gz/_2.fastq.gz}

Or if you want to stick to sed,

second=$(echo $i | sed 's/_1.fastq.gz/_2.fastq.gz/')
ADD REPLY

Login before adding your answer.

Traffic: 2457 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