sam.gz files to bam using samtools
2
0
Entering edit mode
22 months ago

Hello bioinformaticians,

I wish to convert compressed aligned.sam.gz files to aligned.bam files using samtools. Is it possible?

I tried this command

samtools sort -@ 8 -o aligned.bam aligned.sam.gz

Suggestions are welcome

RNA-Seq samtools • 2.2k views
ADD COMMENT
0
Entering edit mode

Hi! Does the command samtools sort work even when the provided compressed SAM file has a name ending with “.bam” (as asked here: https://github.com/samtools/samtools/issues/2007)? In general, how does samtools sort infer the input format? Thank you very much!

ADD REPLY
2
Entering edit mode
22 months ago
Martin ▴ 60

Try the following in bash:

zcat aligned.sam.gz | samtools view -o aligned.bam

To sort, do the following:

zcat aligned.sam.gz | samtools sort -o aligned.bam

The -@ 8 (multi-threading) is optional

ADD COMMENT
0
Entering edit mode

gzip -cd is more generic that zcat as macOS zcat behaves differently than Linux zcat.

ADD REPLY
1
Entering edit mode
21 months ago
jkbonfield ★ 1.3k

You command is perfectly fine, assuming you actually meant "sort", given your request was "convert". If you really just meant convert then use "view" instead.

You said you tried the command, but didn't give us anything else to go on. I assume it didn't work, or you wouldn't be here, but maybe an error message would help get sensible replies? Also which version of samtools are you using? Please use the latest versions where possible.

Note there may be a difference in performance between gzipped SAM and bgzfed SAM. They're both gz format, but bgzf is random access so the decode can be parellelised in addition to the encode side.

Finally, this sort of implies you're running an aligner | gzip > sam.gz and then want to sort and convert to bam. If so, why? It's better to just pipe the output of the aligner directly into the next part of the pipeline (eg markdup if doing that, or skip that and samtools sort). Samtools can read from stdin and it'll auto-detect the file format too.

ADD COMMENT

Login before adding your answer.

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