Skip to content

Instantly share code, notes, and snippets.

@victorlin
Last active February 21, 2017 19:12
Show Gist options
  • Save victorlin/fb509a6600fffcd5b7b5563688685e59 to your computer and use it in GitHub Desktop.
Save victorlin/fb509a6600fffcd5b7b5563688685e59 to your computer and use it in GitHub Desktop.
batch run bedtools bam2bed on accepted_hits.bam in subdirectories

bam2bed_all.sh

find

  • man find
  • This command is used to search for files and/or subdirectories in a directory.
  • In this script, find is used to search for immediate subdirectories matching the regular expression R*.dm6. The output of find is a list of each match. These subdirectories are passed through a pipe into a push/pop loop.

pushd and popd

  • Wikipedia
  • These commands are used to traverse directories using a stack structure.
  • In this script, they are used to cd into each subdirectory (via pushd) and return to the base directory (via popd). They are also modified to run quietly (no output).

other notes

#!/usr/bin/env bash
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
fn_bam="accepted_hits.bam"
fn_bed="accepted_hits.bed"
bed_dir="path/to/bam2bed"
mkdir -p bed_dir
find . -mindepth 1 -maxdepth 1 -type d -name "R*.dm6" | while read -r dir
do
pushd "$dir"
basedir=${PWD##*/}
fp_bed=${bed_dir}/${basedir}_${fn_bed}
echo "Creating ${fp_bed}"
bedtools bamtobed -i ${fn_bam} > ${fp_bed}
popd
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment