This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to subtract a portion of a bam file from the large bam file of the same sample?

The question might seem confusing, but I really have this problem.

I have an aligned.bam file for a sample. I want to separate the bam file: 1) above a certain coverage 2) and bam file not in that coverage range.

I have selected the aligned reads above a certain coverage using: samtools coverage > creation bed file > reads in this bed region i.e selected.bam. Now, I want to get the portion of the bam file that is not in the selected.bam. So, basically its a complement of the selected.bam

Step01: Aligned.bam > calculate coverage (bed file) > select region above certain coverage (selected.bed) Step 02: Aligned.bam selected.bed (filter) > selected.bam

Step 03: How to get unselected.bam?

Note: I cannot use complement of the bed file because an aligned read may span the bed boundary. Using the complement of the bed file will cause some of the reads to be selected in both the categories.

bam alignment samtools merge bedtools

1 answer

use -U in the samtools view command. That way you generate both files at the same time. :

-U FILE
  

Write alignments that are not selected by the various filter options to FILE. When this option is used, all alignments (or all alignments intersecting the regions specified) are written to either the output file or this file, but never both.

Log in to answer this question.