Dear all,

Could you help he please to modify my loop in bash to multiple alignment my FASTQ files?

I have a many fastq files R1 and R2 and I would like to align all those read in bash loop - lets say I have `X1_R1_001.fastq` + `X1_R2_001-fastq` and `Y1_R1_001.fastq` + `Y1_R2_001.fastq`. Sample X1 is pair-end read R1 + R2 and so on.

```
#!/bin/bash
for i in *fastq;
do tophat2 -o ${i%.fastq}tpout -G path/to/my/reference.gtf -p 8 path/to/my/bowtie_index ${i}R1_001.fastq ${i}R2_001.fastq --rg-id X1 --rg-sample X1 --rg-library rna-seq --rg-platform Illumina
done;
```

And I would like to also change my `--rg-id` tag with name of my fastq files (in this example `X1` in first loop and `X2` in second loop). Output folder should have the name of the sample too.

Please do you have any idea how to modify my bash loop?

Thank you so much for any ideas!

Paul
So, this is what I came up with, based on our discussion:

```
for i in $(ls *.fastq | rev | cut -c 13- | rev | uniq)
do
tophat2 -o ${i%.fastq}tpout  -G path/to/my/reference.gtf -p 8 path/to/my/bowtie_index ${i}_R1_001.fastq ${i}_R2_001.fastq --rg-id ${i} --rg-sample ${i} --rg-library rna-seq --rg-platform Illumina
done
```

Hope this helps!

---

OK, this answer is gotten a little popular, and my shell knowledge has improved so I'm going to enhance this with some fail-safes to address commonly observed challenges.

First off, the `rev | cut -c 13-` is just asking for trouble. Let's substitute that with a `sed` so the command makes a little more sense. Here, I'm trying to remove all `_R[12]_001.fastq`.

`ls *.fastq | sed -r 's/_R[12]_001[.]fastq//' | uniq ` looks better. I've also edited the variable name from `i` so it makes a little more sense. And I've added some formatting changes plus quotes to avoid shell problems.

Here's the updated code:

```
for prefix in $(ls *R1*.fastq | sed -r 's/_R1_001[.]fastq//' | uniq)
do
tophat2 -o "${prefix%.fastq}tpout" \
    -G path/to/my/reference.gtf -p 8 \
    path/to/my/bowtie_index \
    "${prefix}_R1_001.fastq" "${prefix}_R2_001.fastq" \
    --rg-id "${prefix}" --rg-sample "${prefix}" --rg-library rna-seq --rg-platform Illumina
done
```

Note (21-Sep-2022): This will only work for one-pair-per-sample FASTQs. Multi-lane FASTQs will need to be concatenated so each sample has just one pair. The command can be modified easily for compressed FASTQ files.