How to break script if Bowtie2 errors?
1
1
Entering edit mode
4.6 years ago
dfajar2 ▴ 140

Hi, I'll try to explain my problem the best I can. I have a script that runs multiple bowtie2 alignments. The problem is that it continues running the next alignment even if the current one fails to run.

script.sh

#!/bin/bash

bowtie2 --threads 32 -x sp_idx -U reads1.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 1_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads2.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 2_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads3.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 3_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads4.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 4_sorted.bam

What I want is that if there's an error while running Bowtie2, the script stops running and not keep running to the end. What's happening is that if line 2 fails, the script continues with the alignment of sample 3 and then 4. So why doesn't it stop running if line 2 is errors out?

Thanks!

bowtie2 • 1.2k views
ADD COMMENT
5
Entering edit mode
4.6 years ago
Ram 43k

That's how shell scripts operate. If you expect that your shell script should stop as soon as an error (exit code != 0) is encountered, use set -eo pipefail right at the top after the interpreter (!#/bin/bash) line.

#!/bin/bash
set -eo pipefail

bowtie2 --threads 32 -x sp_idx -U reads1.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 1_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads2.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 2_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads3.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 3_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads4.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 4_sorted.bam

More reading:

ADD COMMENT
0
Entering edit mode

Perfect, thanks!!!!

I tried set -e in the past and that didn't work. :-)

ADD REPLY
1
Entering edit mode

Probably because one of the commands that are part of a pipe failed, but the rightmost command in that pipe succeeded so the overall command chain took on that 0 exit code instead of the non-zero code of the failing command.

ADD REPLY
1
Entering edit mode

Well, I learned something new today. Thx

ADD REPLY
0
Entering edit mode

That's how it always goes with the shell. Just when we think we know something, it surprises us.

ADD REPLY

Login before adding your answer.

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