This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How Can I Convert Bam To Sam?

Any suggestions?

bam next-gen-sequencing sam

Hi,

When I am converting Sam to Bam and back to Sam.I am loosing quality score of the Sam file. i.e. Input sam and the reversed sam does not match. Is this expected?

Hi manish. You should open a new question for this. When you put your question in as an answer, people are unlikely to respond.

when converting from sam to bam the default method compresses the resulting bam file. Compression could result in data loss.

"Compression could result in data loss." This is only correct if a lossy compression algorithm is used. BAM natively employs zlib (~LZ77) compression, which is a _lossless_ compression algorithm. Thus, no data is lost.

4 answers

Use samtools from the command line:

samtools view -h -o out.sam in.bam

You can use this:

To convert BAM to SAM you can use:

$ samtools view -h file.bam > file.sam 

You should include -h option to include the header in the SAM output.

And to convert SAM to BAM:

$ samtools view -b file_copy.sam > file_copy.bam

-b is the option to output BAM.

Where is the command line? I'm sorry I don't know what I am doing and I just downloaded samtools but I cannot figure out where to input the $ samtools view -h file.bam > file.sam code, as I need to turn a file that is bam to a sam file for my mother.

Have you tried Googling "where is the command line"? Also, the fact that you need to convert a file for your mother is irrelevant here, so why mention it?

When you do find the command line, ensure you don't enter the $ at the beginning; that's just the symbol for the command line prompt and is not meant to be part of the command.

If you want to do it in batch:

# convert BAM to SAM
for file in ./*.bam
do
    echo $file 
    samtools view -h $file > ${file/.bam/.sam}
done

Galaxy also does this.

With parallel, remember to cite the paper, it's remarkable work.

parallel --plus 'samtools view -h {} -o {...}.sam' ::: *.bam

Log in to answer this question.