Batch File For Loop Help
2
1
Entering edit mode
3.2 years ago
cthangav ▴ 110

Hello I'm trying to run bowtie for a several files using a loop so I don't have to repeat the command over and over. The loop looks something like this:

mm10="GenomeFolder/Bowtie2Index/genome"

for (( i = 76; i <= 79; i++ ))
  do
  bowtie2  -x $mm10  -1 SRR51363$i_1.fastq.gz  -2 SRR51363$i_2.fastq.gz \
done

The problem is that I can't use the $i variable in between SRR51363 and _1.fastq.gz, It causes an error. Any adivce on the right syntax? I tried to google this but it's hard to describe this problem I'm having. Thank you!

syntax • 973 views
ADD COMMENT
2
Entering edit mode
3.2 years ago

Put curly braces around variable names, and quotes around words:

mm10="GenomeFolder/Bowtie2Index/genome"

for (( i = 76; i <= 79; i++ ))
do
  bowtie2 -x "${mm10}" -1 "SRR51363${i}_1.fastq.gz" -2 "SRR51363${i}_2.fastq.gz"
done

Curly braces in this context prevent variable names from being expanded incorrectly. Quotes allow files and paths to contain spaces and special characters. While the latter may not be an issue here for this specific example, it is a good habit to get into.

ADD COMMENT
0
Entering edit mode
3.2 years ago

with bash:

$ seq 76 1 79 | while read line; do echo SRR51363$line\_1.fastq.gz  SRR51363$line\_2.fastq.gz; done

SRR5136376_1.fastq.gz SRR5136376_2.fastq.gz
SRR5136377_1.fastq.gz SRR5136377_2.fastq.gz
SRR5136378_1.fastq.gz SRR5136378_2.fastq.gz
SRR5136379_1.fastq.gz SRR5136379_2.fastq.gz

with parallel:

$ parallel --dry-run bowtie -x mm10 -1 SRR51363{}_1.fastq.gz -2 SRR51363{}_2.fastq.gz ::: {76..79}

bowtie -x mm10 -1 SRR5136376_1.fastq.gz -2 SRR5136376_2.fastq.gz
bowtie -x mm10 -1 SRR5136377_1.fastq.gz -2 SRR5136377_2.fastq.gz
bowtie -x mm10 -1 SRR5136378_1.fastq.gz -2 SRR5136378_2.fastq.gz
bowtie -x mm10 -1 SRR5136379_1.fastq.gz -2 SRR5136379_2.fastq.gz
ADD COMMENT

Login before adding your answer.

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