Skip to content

Instantly share code, notes, and snippets.

@RonaldFindling
Last active January 1, 2016 17:20
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 RonaldFindling/5b3868f8da3b10130b38 to your computer and use it in GitHub Desktop.
Save RonaldFindling/5b3868f8da3b10130b38 to your computer and use it in GitHub Desktop.
FIlter bam-file by chromosome and range
#!/bin/bash
##############################
#set the following variables
##############################
#use the bamfile without .bam
myfile="alignment"
#the chromosome to filter for
mychrom="chr1"
# reads that start inbetween $from and $to get selected
from="43500000"
to= "43600000"
#how many reads should be in the resultfile at max
readnumber="50"
##############################
#script starts here
##############################
samfile="${myfile}.sam"
bamfile="${myfile}.bam"
samchromfile="${myfile}_${mychrom}".sam
samchromtargetfile="${myfile}_${mychrom}_from_${from}_to_${to}.sam"
bamchromtargetfile="${myfile}_${mychrom}_from_${from}_to_${to}.bam"
sortedbamchromtargetfile="${myfile}_${mychrom}_from_${from}_to_${to}_sorted.bam"
tmpfile="tmp.txt"
##convert initial bam to sam
samtools view -h "${bamfile}" > "${samfile}"
#filter by chromosome
cat ${samfile} | awk -v mychrom=${mychrom} 'BEGIN{FS="\t";} {if($3==mychrom) print $0}' > ${samchromfile}
#filter by given target (start and end)
cat ${samchromfile} | awk -v from=${from} -v to=${to} '{FS="\t"} $4>=from && $4<=to' from=$from to=$to > ${samchromtargetfile}
#take the first x lines
head -n ${readnumber} ${samchromtargetfile} > ${tmpfile} && mv ${tmpfile} ${samchromtargetfile}
#push the original header to the beginning of the new file
head -n 1000 ${samfile} | grep "^@" | cat - ${samchromtargetfile} > ${tmpfile} && mv ${tmpfile} ${samchromtargetfile}
#convert sam to bam
samtools view -bS ${samchromtargetfile} > ${bamchromtargetfile}
tmpfilename=`echo "${sortedbamchromtargetfile}" | sed "s/.bam$//"`
# create sorted bam
samtools sort "${bamchromtargetfile}" "${tmpfilename}"
tmpfilename=`echo "${sortedbamchromtargetfile}" | sed "s/.bam$//"`
#create bai
samtools index "${sortedbamchromtargetfile}" "${tmpfilename}.bai"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment