Created
January 31, 2014 16:42
-
-
Save holtgrewe/8735877 to your computer and use it in GitHub Desktop.
Shell script that takes a reference and a SAM file (both should be small) converts sam to sorted BAM and displays ist using `samtools tview`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
FIX=$DIR/fix_sam.py | |
# Global variables for reference REF and SAM alignment. | |
REF= | |
SAM= | |
# Whether or not verbose mode is enabled. | |
VERBOSE= | |
# Whether or not to keep the files. | |
KEEP= | |
# Temporary directory created. | |
TMPDIR=$(mktemp -d) | |
# Remove temporary directory at exit/crashes. | |
function cleanup { | |
test -z "$KEEP" || return | |
test -z "$VERBOSE" || echo "rm -rf ${TMPDIR}" >&2 | |
rm -rf ${TMPDIR} | |
} | |
trap cleanup EXIT | |
# Parse parameters. | |
while getopts "r:s:vk" opt; do | |
case $opt in | |
r) | |
REF=$OPTARG | |
;; | |
s) | |
SAM=$OPTARG | |
;; | |
v) | |
VERBOSE=1 | |
;; | |
k) | |
KEEP=1 | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
# Check parameters. | |
if [[ -z "$REF" ]]; then | |
echo "Parameter -r required" >&2 | |
exit 1 | |
fi | |
if [[ -z "$SAM" ]]; then | |
echo "Parameter -s required" >&2 | |
exit 1 | |
fi | |
# Copy reference into temp dir. | |
test -z "$VERBOSE" || echo "cp $REF $TMPDIR"/ref.fa >&2 | |
cp $REF $TMPDIR/ref.fa | |
# Strip leading 1I from CIGAR strings. | |
test -z "$VERBOSE" || echo "$FIX <$SAM >$TMPDIR/aln.sam" | |
$FIX <$SAM >$TMPDIR/aln.sam | |
# Convert SAM file to BAM file and sort it. | |
test -z "$VERBOSE" || echo "samtools view -Sb $TMPDIR/aln.sam > $TMPDIR/aln.bam" >&2 | |
samtools view -Sb $TMPDIR/aln.sam > $TMPDIR/aln.bam |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also see https://gist.github.com/holtgrewe/8735840.