This is a test version of Biostars. For the public version, visit https://www.biostars.org.
problem in conversion of sam to bam file

hi, I used Bwa tool for mapping to get .sam file in output.Now I used samtools view to convert this .sam file into .bam file but it is not running.It gets exit within a second. Can anyone suugest why this is happening?

The command I am using is

samtools view -bS newoutputERX676467.sam > newoutputERX676467.bam

the head of the output is

head  newoutputERX676467.bam
[W::sam_read1] parse error at line 1
[main_samview] truncated file.
software error next-gen alignment

what is the output of

head newoutputERX676467.sam

?

head newoutputERX676467.sam 
[M::bwa_idx_load_from_disk] read 0 ALT contigs
@SQ SN:NC_000001.11 LN:248956422
@SQ SN:NT_187361.1  LN:175055
@SQ SN:NT_187362.1  LN:32032
@SQ SN:NT_187363.1  LN:127682
@SQ SN:NT_187364.1  LN:66860
@SQ SN:NT_187365.1  LN:40176
@SQ SN:NT_187366.1  LN:42210
@SQ SN:NT_187367.1  LN:176043
@SQ SN:NT_187368.1  LN:40745

I added markup to your post for increased readability. You can do this by selecting the text and clicking the 101010 button. When you compose or edit a post that button is in your toolbar, see image below:

101010 Button

Do you use nohup? Stderr and stdout seem to be mixed. I would always do bwa mem (options) in.fq | samtools view -o out.bam

Likely not the source of your problem, but please use the new samtools syntax (with a recent version of samtools):

samtools view newoutputERX676467.sam -o newoutputERX676467.bam

Samtools recognizes file extensions.

i tried with this also,but still it is not working.

The problem is that you aligned with nohup. Don't do that. You'll have to repeat your alignment.

Agreed. As I told you already, your SAM file is corrputed because STDERR messages are in it. Start over with the alignment and use any of the suggested solutions, or simply your script without nohup.

You are just missing to include the header in your samtools view command:

samtools view -bSh newoutputERX676467.sam > newoutputERX676467.bam

(It's the old samtools syntax, you may want to update it as Wouter suggested)

I moved your answer to the comment section as it would not help given the error message of OP, indicating that stderr and stdout are mixed in the same file.

2 answers

I recommend against nohup as it mixes stdout and stderr which can be problematic, corrupting the output. If you want to capture stderr in a file, better wrap your code into a function like

function Alignment {

  FASTQ=$1
  bwa (...) (code...) | samtools view -o ${1%.fq}.bam

}

and then run it like Alignment In.fq 2> stderr.log Wrapping in a function is useful because as soon as your code gets longer you can simply catch all stderr in a file with a single 2> and do not run into trouble with mixing stderr/stdout. Also consider using GNU screen.

head newoutputERX676467.sam 
[M::bwa_idx_load_from_disk] read 0 ALT contigs
@SQ SN:NC_000001.11 LN:248956422
@SQ SN:NT_187361.1  LN:175055
@SQ SN:NT_187362.1  LN:32032
@SQ SN:NT_187363.1  LN:127682
@SQ SN:NT_187364.1  LN:66860
@SQ SN:NT_187365.1  LN:40176
@SQ SN:NT_187366.1  LN:42210
@SQ SN:NT_187367.1  LN:176043
@SQ SN:NT_187368.1  LN:40745

you're using bwa the wrong way. You're mixing the standard error and the standard outpout in the same stream

your command should be something like

bwa mem ref.fa f1.fq f2.fq > out.sam

or

bwa mem ref.fa f1.fq f2.fq > out.sam 2> messages.txt

I used this command only,which u have mentioned above.

Log in to answer this question.