Subset (reduce) bam to contain only variant containing reads
I have targeted sequencing alignments bam files. I want to "pull out" only read pairs in which the reads have a variant, i.e. do not perfectly match the reference. It doesn't matter if these are sequencing errors etc.
Any suggestions, simply using samtools would be great but I could find any bam flags for this, a pysam solution would also be great.
• 1,437 views
•
link
1 answer
I don't know why Pierre Lindenbaum answer doesn't show but he proposed this solution.
samtools view -h in.bam | grep -E '^@|NM\:i\:[^0]' > out.sam
NM:i:count = Number of differences (mismatches plus inserted and deleted bases) between the sequence and reference...
If it's missing you can add it using
samtools calmd "$bam" "$bam"
An alternate solution is to use bamtools like so.
bamtools filter -tag "NM:>0" -in in.bam -out out.bam
you can use >, >=, <, <=, ! in the expression
• 0 views
•
link
Log in to answer this question.
Some past threads to get you going:
How to extract reads with a known variant form a bam file
Extract PE Reads (with their mates) supporting variants in vcf file
Extracting Reads Containing A Specific Variant From A Bam File
Sam/Bam: Extract Only Alignment Reads That Contain A Specific Position
Extract reads with variant at given position