Skip to content

Instantly share code, notes, and snippets.

@ipstone
Forked from mtw/extract_pp.sh
Created August 21, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipstone/4e35ddf39c231681b2a188d73107e71d to your computer and use it in GitHub Desktop.
Save ipstone/4e35ddf39c231681b2a188d73107e71d 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