Thursday, April 10, 2014

Merge multiple BAM files

If you have multiple lanes in the sequencing experiment after mapping to the genome using bowtie you will end up with multiple SAM files, as bowtie will allow only one fastq file as input. To continue with the pipeline you will need to merge SAM files with samtools. First convert them to BAM:

samtools view -Sb file.sam > file.bam

Then:
samtools merge output.bam file1.bam file2.bam file3.bam

2 comments:

  1. find ./ -name '*.bam' | {
    read firstbam
    samtools view -h "$firstbam"
    while read bam; do
    samtools view "$bam"
    done
    } | samtools view -ubS - | samtools sort -o merged.bam -
    samtools index merged.bam
    ls -l merged.bam merged.bam.bai

    ReplyDelete
  2. what is the difference between samtools merge and cat *fastq.gz >> combined-file.fq.gz ?
    Do you actually need to use samtools?

    ReplyDelete