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

Hi, how can I make a loop for sam to bam then delete sam file in the same loop

samtobam loop

Are you sure you can't pipe your output straight to something that will convert it to bam? For instance, samtools sort will work on a sam, so you can pipe your sam output straight into it, and never actually make a sam file.

1 answer

As others have suggested, do you have a process that generates SAM output that can be piped directly to samtools?

# sam output generated, yet sam file never exists
bowtie alignment_index -c GGATCC --sam | samtools view -b -o output.bam -

What language/method would you like to loop with? What have you tried? A bash example:

#!/bin/bash

sam_file="my_filename.sam"
alignment_index="path_to_your_index"

for f in $sam_file
do
  bowtie $alignment_index -c GGATCC --sam > $f
  samtools view $f -b -o output.bam
  rm $f
done

Log in to answer this question.