How Can I Convert Bam To Sam?
4
33
Entering edit mode
14.4 years ago
Biomed 5.0k

Any suggestions?

bam next-gen-sequencing sam • 180k views
ADD COMMENT
1
Entering edit mode

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

ADD REPLY
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

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 REPLY
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
62
Entering edit mode
14.4 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
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
10
Entering edit mode
12.1 years ago
A. Domingues ★ 2.7k

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
13.3 years ago
Stephen 2.8k

Galaxy also does this.

ADD COMMENT
0
Entering edit mode
3.3 years 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: 2364 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