简体   繁体   中英

bash script - loop through files and add to different variables

I have files with names like:

0195_R1.fastq
0195_R2.fastq
0196_R1.fastq
0196_R2.fastq
0197_R1.fastq
0197_R2.fastq

and so on.

I need to run a software for each pair of files (the R1 and R2 are correspondent to each other) like:

bowtie2 -x index_files -1 0195_R1.fastq -2 0195_R2.fastq -S 0195_output.sam

With multiple pairs I'd have to run multiple times. So I tried to do a bash script using a for loop but I've had no success. Also, I don't know how to rename the output sequentially.

I've tried the following:

for R1 in $FQDIR/*_R1.fastq; do
for R2 in $FQDIR/*_R2.fastq; do

    bowtie2 -x index_files -1 $R1 -2 $R2 -S $N_output.sam

done
done

What should I do?

If you loop over all the R1 and R2 files, you'll run bowtie for all possible pairs of data files. If I understand correctly, that's not what you want - you only want to process the corresponding pairs.

To do that, loop over R1 files only, and try to find the corresponding R2 file for each:

#!/bin/bash
fqdir=...
for r1 in "$fqdir"/*_R1.fastq; do
    r2=${r1%_R1.fastq}_R2.fastq
    if [[ -f $r2 ]] ; then
        bowtie2 -x index_files -1 "$r1" -2 "$r2" -S "$N"_output.sam
    else
        echo "$r2 not found" >&2
    fi
done

I'm not sure what $N stands for. Maybe you can use $r1 instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM