This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools crashes when sorting files

Hi,

I am processing ChIP sequencing data. I mapped FASTQ files using Bowtie2 then I tried to convert sam files to bam files, then sort and index the files using samtools. When I try to sort the bam files, gibberish letters show up and crash in the middle. I have total four files but I got the same error in all four files (all files were downloaded from publicly available data). I used samtools 1.2 this time(server in my university installed samtools1.2), I also used samtools 1.12 on the laptop computer, the same error occurred.

The code I used was:

for i in *.sam
do
  samtools view -b -S $i -o $i.bam
  samtools sort $i.bam -o $i.sorted.bam
  samtools index $i.sorted.bam
done

I also tried:

for i in *.sam
do
  samtools sort $i -o $i.sorted.bam
  samtools index $i.sorted.bam
done

but the same result.

These codes worked in the past, and I could not figure out where the problem is.

samtools

for i in *.sam

you should learn about make and about workflow managers (nextflow, snakemake....)

, gibberish letters show up and crash in the middle.

is there any space in the filenames ? protect the filenames with quotes:

set -x
for i in *.sam
do
echo "RUNNING $i" 1>&2
samtools sort -T "$i.tmp"  -O BAM -o "$i.sorted.bam" "$i"
samtools index "$i.sorted.bam"
done

Thank you for your reply.

I got error message saying

  • can't parse output format "BAM"
  • "fail to open file output.bam" <- I got this message whenever I ran "samtools sort -o output.bam input.sam" (I don't know I need to put "input.sam" right after "sort")

What version of samtools are you using?

1.2 according to original post. So older.

yomogy : Latest version is 1.16. It may feel odd but 1.2 is not the latest. It goes 1.1, 1.2,1.3 and so on.

add an echo in front of each samtools command, then send us the resulting output

PS. if you have only four samples, just run them manually :-) or add the commands to a text file manually

then run

bash myfile.txt

see if they crashes that way as well

1 answer

can't parse output format "BAM"

remove -O BAM . Your samtools is too old.

Thank you very much, it works!

Log in to answer this question.