This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Samtools sam to bam error

Hi there!

I am trying to convert my SAM file (7.5G) to BAM format using this line of code:

samtools view --threads 4 -buS ${sample}.sam -o ${sample}.bam

My first issue is the memory requirement - it is taking over 180G to start running and secondly when it does run I get the the following error

samtools view: error reading file "*l.sam"\
samtools view: error closing "*.sam": -5

Can anyone suggest what the error means and how to correct it please!

samtools

Which version of samtools are you using? It is odd that you need 180G of RAM. There is no need to use any flags since samtools should autodetermine the options from file extensions.

$ samtools --version
samtools 1.15.1
Using htslib 1.15.1
Copyright (C) 2022 Genome Research Ltd.

I am using v1.14 - I tried conda update samtools but it is not updating to v1.15.1

That version is recent enough. What happens if you simply do samtools view --threads 4 ${sample}.sam -o ${sample}.bam?? Any specific reason to choose uncompressed BAM in your original command?

I still get the following error when I change it to your suggested code

samtools view: error reading file "{sample}.sam" samtools view: error closing "{sample}.sam": -5

Is ${SAMPLE} variable properly expanding to a file that exists? Error indicates that samtools is having a problem reading the file.

Yes, I checked the SAM file exist. The line of code does start to generate a bam file, however, it only gets to ~ 200Mb before throwing the error.

Please show the full script so how the variable gets defined. It makes no sense this uses excessive memory, the command essentially needs none so the error is somewhere in the script.

Here's the code

for sample in *.fq  
do  
echo $sample >> alignment.log  
bowtie -p 16 --chunkmbs 200 -a --best --strata -v 0 --norc -m 20 -x $index ${sample}  -S ${sample::-3}.sam --un ${sample::-3}_unaligned.fq --max ${sample::-3}_overM.fq 2>> alignment.log  
samtools view  ${sample::-3}.sam -o ${sample::-3}.bam                           
samtools flagstat ${sample::-3}.bam  
samtools view -b -F 4 ${sample::-3}.bam | samtools sort -o ${sample::-3}.sort.bam                   
samtools index ${sample::-3}l.sort.bam

Did you eventually figure it out? I am facing a similar issue. Thanks

0 answers

No answers yet.

Log in to answer this question.