Convert SAM to BAM
2
2
Entering edit mode
10.3 years ago

Hello!

Can't convert SAM to BAM.

My SAM-file has a header:

@SQ    SN:2L    AS:FlyBase r5    LN:23011544    SP:Drosophila melanogaster
@SQ    SN:2LHet    AS:FlyBase r5    LN:368872    SP:Drosophila melanogaster
@SQ    SN:2R    AS:FlyBase r5    LN:21146708    SP:Drosophila melanogaster
@SQ    SN:2RHet    AS:FlyBase r5    LN:3288761    SP:Drosophila melanogaster
@SQ    SN:3L    AS:FlyBase r5    LN:24543557    SP:Drosophila melanogaster
@SQ    SN:3LHet    AS:FlyBase r5    LN:2555491    SP:Drosophila melanogaster
@SQ    SN:3R    AS:FlyBase r5    LN:27905053    SP:Drosophila melanogaster
@SQ    SN:3RHet    AS:FlyBase r5    LN:2517507    SP:Drosophila melanogaster
@SQ    SN:4    AS:FlyBase r5    LN:1351857    SP:Drosophila melanogaster
@SQ    SN:X    AS:FlyBase r5    LN:22422827    SP:Drosophila melanogaster
@SQ    SN:XHet    AS:FlyBase r5    LN:204112    SP:Drosophila melanogaster
@SQ    SN:YHet    AS:FlyBase r5    LN:347038    SP:Drosophila melanogaster
@SQ    SN:M    AS:FlyBase r5    LN:19517    SP:Drosophila melanogaster
@SQ    SN:U    AS:FlyBase r5    LN:10049159    SP:Drosophila melanogaster
@SQ    SN:Uextra    AS:FlyBase r5    LN:29004788    SP:Drosophila melanogaster
HWI-EAS146:8:1:3:289#0    16    Uextra    11516293    255    36M    *    0    0    NGGAGNCAAATGCCTCGTCATCTAATTAGTGACGCG    aaa^_aa\^TG\\LYLY`_`aBBBBBBBBBBBBBBB
HWI-EAS146:8:1:3:289#0    16    Uextra    10227800    255    36M    *    0    0    NGGAGNCAAATGCCTCGTCATCTAATTAGTGACGCG    aaa^_aa\^TG\\LYLY`_`aBBBBBBBBBBBBBBB
HWI-EAS146:8:1:3:289#0    16    Uextra    20746385    255    36M    *    0    0    NGGAGNCAAATGCCTCGTCATCTAATTAGTGACGCG    aaa^_aa\^TG\\LYLY`_`aBBBBBBBBBBBBBBB

I write a command:

samtools view -bS file.sam > file.bam

No error is displayed, program stops working successfully but created BAM file can't be opened (The error is: type of archive is not supported).

sam RNA-Seq bam • 71k views
ADD COMMENT
1
Entering edit mode

BAM file can't be opened : by which software ?

ADD REPLY
0
Entering edit mode

Usually I can see content of bam file just with gedit

ADD REPLY
6
Entering edit mode

Usually I can see content of bam file just with gedit

god kills a kitten every time you're doing this :-)

ADD REPLY
0
Entering edit mode

Thank you for the help) The problem was in running samtools from root (doesn't allow to write files).

ADD REPLY
2
Entering edit mode

You may be talking about sam file. Try running: samtools view -H file.bam. This command will show the header of the bam file.

ADD REPLY
0
Entering edit mode

Thank you, I saw that the bam file is ok.

But I can't convert it to sorted bam file (and I thought that the mistake was at step of sam-to-bam converting).

I wrote a command: samtools sort file.bam file_sorted. the mistake is:

[bam_sort_core] merging from 4 files...
open: No such file or directory
[bam_merge_core] fail to open file 2024-sorted.0000.bam
ADD REPLY
1
Entering edit mode

As Pierre indicated take some time to go through the samtools manual because most of these are basic stuff. About your sort error: samtools splits the big bam file for sort purpose. It seems that you may have deleted those intermediate files and when samtools tried to merge them it didn't find them. Don't delete any intermediate files until the sort process is done.

ADD REPLY
0
Entering edit mode

Don't ever delete anything while any program is working)

Thank you for your help. The error was that I run program while being in root and it doesn't allow to write files. Too little experience with Linux..

ADD REPLY
1
Entering edit mode

Hi,

I'd suggest mentioning that you're a "linux beginner" in your questions. That would help us tweak our geek levels suitably :-)

ADD REPLY
3
Entering edit mode
10.3 years ago
Ian 6.1k

The command line should be:

samtools view -bS -o file.bam file.sam

without the '>' redirect

samtools view -bS file.sam > file.bam

This is fine, I had just never done run it without the -o flag before.

ADD COMMENT
0
Entering edit mode

Why? The OPs command like works as well, doesn't it?

ADD REPLY
0
Entering edit mode

You are right!

ADD REPLY
0
Entering edit mode
10.3 years ago
Caddymob ★ 1.0k

Try samtools import.

~> samtools import
Usage: bamtk import <in.ref_list> <in.sam> <out.bam>
ADD COMMENT
1
Entering edit mode

I think that command might be deprecated.

ADD REPLY

Login before adding your answer.

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