How Can I Convert Bam To Sam?
6
32
Entering edit mode
12.7 years ago
Biomed 4.8k

Any suggestions?

next-gen sequencing bam sam conversion • 158k views
ADD COMMENT
61
Entering edit mode
12.7 years ago

Use samtools from the command line:

samtools view -h -o out.sam in.bam
ADD COMMENT
0
Entering edit mode

it does not work for me! why?!

ADD REPLY
3
Entering edit mode

oh I understand guys. before this command, we should install samtools in the bash. http://www.htslib.org/download/

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
10
Entering edit mode
10.4 years ago
A. Domingues ★ 2.6k

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
ADD COMMENT
3
Entering edit mode
11.7 years ago
Stephen 2.8k

Galaxy also does this.

ADD COMMENT
1
Entering edit mode
11.7 years ago
Fred Wang ▴ 10

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

ADD COMMENT
11
Entering edit mode

"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.

ADD REPLY
0
Entering edit mode
12.1 years ago
Manish • 0

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?

ADD COMMENT
10
Entering edit mode

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

ADD REPLY
0
Entering edit mode
20 months ago
Tian ▴ 50

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

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

Login before adding your answer.

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