Created
October 10, 2018 11:49
-
-
Save jrjhealey/0f6442c9fcc38f267d03b510e36f9f2f to your computer and use it in GitHub Desktop.
Entirely uncessarily complex script to colour DNA sequences at arbitrary positions.. because...reasons? Even the help is colourful!
This file contains hidden or 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 | |
# Set colours: | |
df=$(tput sgr0) | |
tr=$(tput setaf 1) | |
tg=$(tput setaf 2) | |
ty=$(tput setaf 3) | |
tb=$(tput setaf 4) | |
usage(){ | |
cat << EOF >&2 | |
Arbitrary colouring of ATGC sequences by providing indexed colour vectors in (pretty much) | |
pure bash. Colours are fixed for the moment, but can easily be extended or altered by | |
changing/adding more of the numbers passed to tput in the first couple of lines of the | |
scripts source code (you'd also want to rename the arguments too etc, probably). | |
Usage: | |
$ bash seqcol.sh -r "1,2,3" -g "4,5,6" -y "7,8,9" -b "10,11,12" -s ATGCATCGATCGGCTAGCTA | |
Yeilds: | |
${tr}A${df}${tr}T${df}${tr}G${df}${tg}C${df}${tg}A${df}${tg}T${df}${ty}C${df}${ty}G${df}${ty}A${df}${tb}T${df}${tb}C${df}${tb}G${df}GCTAGCTA | |
Some more advanced usages: | |
$ bash seqcol.sh -r "\$(seq -s, 1 10)" -y "\$(seq -s, 11 20)" -s ATGCATCGATCGGCTAGCTA | |
Yields: | |
${tr}ATGCATCGAT${ty}CGGCTAGCTA${df} | |
$ bash seqcol.sh -r "\$(seq -s, 1 2 20)" -y "\$(seq -s, 2 2 21)" -s ATGCATCGATCGGCTAGCTA | |
Yeilds: | |
${tr}A${ty}T${tr}G${ty}C${tr}A${ty}T${tr}C${ty}G${tr}A${ty}T${tr}C${ty}G${tr}G${ty}C${tr}T${ty}A${tr}G${ty}C${tr}T${ty}A${df} | |
A thoroughly unecessary use of my time, inspired by this question: https://www.biostars.org/p/342247 | |
EOF | |
} | |
for arg in "$@"; do | |
shift | |
case "$arg" in | |
"--help") set -- "$@" "-h" ;; | |
"--sequence") set -- "$@" "-s" ;; | |
"--red") set -- "$@" "-r" ;; | |
"--green") set -- "$@" "-g" ;; | |
"--yellow") set -- "$@" "-y" ;; | |
"--blue") set -- "$@" "-b" ;; | |
*) set -- "$@" "$arg" ;; | |
esac | |
done | |
while getopts "hr:g:y:b:s:" OPTION ; do | |
case $OPTION in | |
s) sequence=$OPTARG ;; | |
r) red=$OPTARG ;; | |
g) green=$OPTARG ;; | |
y) yellow=$OPTARG ;; | |
b) blue=$OPTARG ;; | |
h) usage ; exit 0 ;; | |
esac | |
done | |
in_array() { | |
# Check if element ($2) is in array ($1) | |
ARRAY=$2 | |
for e in ${ARRAY[*]} ; do | |
if [[ "$e" == "$1" ]] ; then | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
red(){ | |
printf "${tr}$1${df}" | |
} | |
green(){ | |
printf "${tg}$1${df}" | |
} | |
yellow(){ | |
printf "${ty}$1${df}" | |
} | |
blue(){ | |
printf "${tb}$1${df}" | |
} | |
string=$(echo "$sequence" | tr '[:lower:]' '[:upper:]') | |
IFS=',' read -r -a Rarray <<< "$red" | |
IFS=',' read -r -a Garray <<< "$green" | |
IFS=',' read -r -a Yarray <<< "$yellow" | |
IFS=',' read -r -a Barray <<< "$blue" | |
for i in $(seq 1 "${#string}") ; do | |
if in_array "$i" "${Rarray[*]}" ; then | |
red "${string:i-1:1}" | |
elif in_array "$i" "${Garray[*]}" ; then | |
green "${string:i-1:1}" | |
elif in_array "$i" "${Yarray[*]}" ; then | |
yellow "${string:i-1:1}" | |
elif in_array "$i" "${Barray[*]}" ; then | |
blue "${string:i-1:1}" | |
else | |
printf "${string:i-1:1}" | |
fi | |
done | |
printf "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment