Skip to content

Instantly share code, notes, and snippets.

@mtw
Created October 26, 2013 22:05
Show Gist options
  • Save mtw/7175143 to your computer and use it in GitHub Desktop.
Save mtw/7175143 to your computer and use it in GitHub Desktop.
Extract properly-paired reads and their mates (ie flags 99/147/163/83) from paired-end BAM files
#!/bin/bash
samtools=`which samtools`
bn=$(basename $1 .bam)
echo processing $bn
$samtools view -h -b -f99 $1 > $bn.flag99.bam
$samtools view -h -b -f147 $1 > $bn.flag147.bam
$samtools view -h -b -f163 $1 > $bn.flag163.bam
$samtools view -h -b -f83 $1 > $bn.flag83.bam
echo merging tmp BAM files
$samtools merge $bn.uns.pp.bam $bn.flag99.bam $bn.flag147.bam $bn.flag163.bam $bn.flag83.bam
echo sorting final BAM file
$samtools sort $bn.uns.pp.bam $bn.pp
echo cleaning up
rm $bn.flag99.bam
rm $bn.flag147.bam
rm $bn.flag163.bam
rm $bn.flag83.bam
rm $bn.uns.pp.bam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment