This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to remove split/broken reads from bam file

Is there any flag to remove broken (ONT reads split and map to different locations) reads from bam file.

Note: The ONT reads mapped with gap should not remove.

split samtools graphmap nanopore minimap2

1 answer

As Pierre Lindenbaum said in the comment, samtools view -F 2048 will remove all those reads that have supplementary alignment. Be sure that the alignment program you use specifies those (like bwa mem does, for example).

If you also want to remove reads that have secondary alignments, there is no flag to do so but you can work around it with awk. Combining it with the one above:

samtools view -h -F 2048 file.bam | awk 'substr($1, 0, 1)=="@" || $0 !~ /ZS:/' | samtools view -h -b > filtered_file.bam

Be careful: ZS: is a tag of HISAT2. If you're using another program, check which tag is used for secondary alignments. I think it is AS: in Bowtie2, and I don't know what it is in bwa. Usually, it is specified in the manual.

Log in to answer this question.