Question: How to combine two .sam files?
0
gravatar for luzglongoria
9 months ago by
luzglongoria20
luzglongoria20 wrote:

Hi there,

I just wonder if I can combine two .sam files. I am not sure if the ‘cat’ command would work in this case. I would like the final file to be a .sam file as well.

Thank you so much.

rna-seq alignment • 2.2k views
ADD COMMENTlink modified 9 months ago by h.mon26k • written 9 months ago by luzglongoria20
1

samtools merge or picard https://broadinstitute.github.io/picard/command-line-overview.html#MergeSamFiles

ADD REPLYlink written 9 months ago by Pierre Lindenbaum121k

Why work on sam files and not bam?

ADD REPLYlink written 9 months ago by WouterDeCoster40k

Or maybe even combine fastq files with cat if you have them?

ADD REPLYlink written 9 months ago by grant.hovhannisyan1.6k
1

It can be more efficient to run alignments in parallel and merge afterwards.

ADD REPLYlink written 9 months ago by WouterDeCoster40k

I have converted SAM files into BAM files

samtools view -bS -o /PATH/file1.sam > file1.bam
samtools view -bS -o /PATH/file2.sam > file2.bam

Now I wan to combine these 2 files with this command:

samtools merge out.bam file1.bam file2.bam

But I got this error message

[W::bam_merge_core2] No @HD tag found.
[W::sam_read1] parse error at line 2

It's something wrong with my BAM files?

ADD REPLYlink written 9 months ago by luzglongoria20
1

No @HD tag found. It's something wrong with my BAM files?

your sam file are probably missing a sam header.... What is the output of

head -n1 file.sam
ADD REPLYlink written 9 months ago by Pierre Lindenbaum121k
  1. Make sure you have a recent version of samtools installed
  2. samtools view file1.sam -o file1.bam
ADD REPLYlink modified 9 months ago • written 9 months ago by WouterDeCoster40k
3
gravatar for ATpoint
9 months ago by
ATpoint19k
Germany
ATpoint19k wrote:

Your command is wrong. Samtools works like this:

## Option 1: use -o to write the output to a file
samtools view -b -o out.bam in.sam

## Option 2: write STDOUT to file:
samtools view -b in.sam > out.bam

You combined both commands, resulting in a corrupted file. I am not even sure if your input files are still OK. I would realign, completely avoiding SAM files using a pipeline:

alignment (...) | samtools view -b -o out.bam -

This will directly output a BAM file from your alignment. There is no need for SAM files. They only take up space. Alternatively, directly pipe the alignment into samtools sort:

alignment (...) | samtools sort -o sorted.bam -

From there, do the merge:

samtools merge out.bam file1.bam file2.bam

Make sure you have the current version of SAMtools installed.

ADD COMMENTlink modified 9 months ago • written 9 months ago by ATpoint19k
2
gravatar for h.mon
9 months ago by
h.mon26k
Brazil
h.mon26k wrote:

Just cat will produce a corrupt sam file. You have two options wiht samtools:

  • samtools cat - work on for bam and cram files, and the sequence dictionary of the files being concatenated need to be identical

  • samtools merge - work for sam, bam and cram, takes as input a sorted files, and outputs a sorted file

ADD COMMENTlink modified 9 months ago • written 9 months ago by h.mon26k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1137 users visited in the last hour