This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Error converting sam to bam

Hi I am getting a message while converting sam to bam. I used this comand for mapping:

bwa mem -M -R '@RG\tID:sample1' -t 2 ref.fa sample1_R1.fastq
sample1_R2.fastq > sample1.sam

Now I tried to convert sam to bam, using:

samtools view -hSb  PI200492.sam > PI200492.bam

and I got an Error

[W::sam_read1] parse error at line 1
[main_samview] truncated file

It looks the aligment worked!

Could someone help me please?

Thanks

genome

Can you show the content of sam file? you can use head PI200492.sam

I got this using

head -n 20 PI200492.sam

[M::bwa_idx_load_from_disk] read 0 ALT contigs [M::process] read 144932 sequences (20000145 bp)... [M::process] read 144502 sequences (20000294 bp)... [M::mem_pestat] # candidate unique pairs for (FF, FR, RF, RR): (1, 37520, 8, 0) [M::mem_pestat] skip orientation FF as there are not enough pairs [M::mem_pestat] analyzing insert size distribution for orientation FR... [M::mem_pestat] (25, 50, 75) percentile: (253, 289, 327) [M::mem_pestat] low and high boundaries for computing mean and std.dev: (105, 475) [M::mem_pestat] mean and std.dev: (290.07, 56.84) [M::mem_pestat] low and high boundaries for proper pairs: (31, 549) [M::mem_pestat] skip orientation RF as there are not enough pairs [M::mem_pestat] skip orientation RR as there are not enough pairs [M::mem_process_seqs] Processed 144932 reads in
79.266 CPU sec, 39.456 real sec @SQ     SN:Chr01        LN:56831624 @SQ     SN:Chr02        LN:48577505 @SQ     SN:Chr03        LN:45779781 @SQ     SN:Chr04        LN:52389146 @SQ     SN:Chr05       LN:42234498 @SQ     SN:Chr06        LN:51416486 @SQ     SN:Chr07       LN:44630646

are you sure you've written:

bwa mem -M -R '@RG\tID:sample1' -t 2 ref.fa sample1_R1.fastq sample1_R2.fastq  > sample1.sam

???

Yes I am sure it was this command.

Yes I am sure it was this command.

no, it was not. You have used 'nohup'

Did you use exactly that command or was it embedded into any nohup-like redirection command?

In fact, I used

nohup  bwa mem -M -R '@RG\tID:sample1' -t 2 ref.fa sample1_R1.fastq sample1_R2.fastq  > sample1.sam &

But I need to use "nohup" because the server here always disconnect for inactivity.

Do you think it can be the problem?

I'd recommend avoiding nohup. Append & to the command or interrupt using Ctrl-Z and then use bg to send the job to background and then follow up with disown <jobid> so SIGHUPs don't bother the process. Retain the job ID to monitor the job and check your file on completion.

I am sorry, I did't get it. Could you please explain it better? Thanks

When you run a command, you can run it either in the foreground or the background. The default is foreground, but you can run it in the background by using an & at the end of the command.

#this is foreground processing
wc -l largefile.fastq
#this is background processing
wc -l largefile.fastq &

But how can you move a running process from the foreground to the background? You interrupt it using Ctrl-Z and then use the bg command to send it to the background. The process will then run in the background.

Even a background process is linked to you, so if you logout or your connection terminates, the job will be killed. You can use nohup to overcome that, but nohup is weird - I stopped using it when it messed with my redirections. You can also use a screen. I use a custom screen script that writes a log to a custom file and has a name attached to it, all done through a custom screenrc file created on the file - but I digress.

A nifty trick is to use disown to remove your ownership of the process. That way, even if the terminal disconnects, your process keeps running as it is not attached to your terminal. But yeah, I was super sleepy when I wrote that answer and screen is a MUCH better way to do it.

You can use GNU screen to be independent from an active terminal session.

Simply open a new screen by screen -S <name>. It can be detached with Ctrl-A Ctrl-D and reattached with screen -r <name>.

I see. Thank you. It will help a lot. But it works just in one of the servers I've been working.

I think this should just work without the -S and -h, but I'm not sure which samtools version you are using.

samtools view -b  PI200492.sam > PI200492.bam

Hi

I already tried this command and I got the same error. The samtools version is 1.5.

1 answer

use

bwa mem -M -R '@RG\tID:sample1' -t 2 ref.fa sample1_R1.fastq sample1_R2.fastq |  samtools view -hSb   -o PI200492.bam -

nohup would combine stderr and stdout.

I will try it and I will let you know.

Thank you very much for helping me.

Log in to answer this question.