Converting Sam To Bam
2
1
Entering edit mode
12.2 years ago
Haluk ▴ 190

Hi,

I have set of sam files and I would like to convert them into bam files with the following script. But it didn't create bam files? What I have made wrong?

for file in `ls ~/wgs/sam`; 
do 
   ~/bin/samtools-0.1.18/samtools view -bS ~/wgs/sam/$file \
   > ~/wgs/bam/$FILE.bam; 
done

Thanks

bam sam • 31k views
ADD COMMENT
1
Entering edit mode

Çould you comment on whether any files are created at all? If yes, what type of files are they?

ADD REPLY
0
Entering edit mode

Actually, I had a typo in my script. Here is the right one. Sorry about that.

for file in `ls ~/wgs/sam`
do
  ~/bin/samtools-0.1.18/samtools view -bS ~/wgs/sam/$file > ~/wgs/bam/$file.bam
done
ADD REPLY
0
Entering edit mode

You use $file to define the SAM file, yet $FILE for the BAM. s/$FILE/$file/ and you should be good.

ADD REPLY
0
Entering edit mode

As a result, I had bam files for each sam files.

ADD REPLY
0
Entering edit mode

And that's what you wanted, right

ADD REPLY
0
Entering edit mode

yes, Andreas, problem solved.

ADD REPLY
3
Entering edit mode
12.2 years ago
Pascal ★ 1.5k

Haluk, you should use the option -o FILE output file name [stdout] instead of redirecting the stdout to the bam file.

ADD COMMENT
1
Entering edit mode

that doesn't affect whether it's BAM output or not.

ADD REPLY
0
Entering edit mode

I think this was in reference to the possibility of stderr also directing to the output file when ">" instead of "-o" is used. Found something about it here: https://sourceforge.net/p/bio-bwa/mailman/message/32092344/

Not sure if it's still a problem though!

ADD REPLY
1
Entering edit mode
8.9 years ago
Jautis ▴ 530

Not sure what you did wrong, but I use samtools view -bT reference.fa test.sam > test.bam and it works pretty well

ADD COMMENT

Login before adding your answer.

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