Skip to content

Instantly share code, notes, and snippets.

@corburn
Last active February 9, 2021 16:07
Show Gist options
  • Save corburn/81877ac131a07d9a6036 to your computer and use it in GitHub Desktop.
Save corburn/81877ac131a07d9a6036 to your computer and use it in GitHub Desktop.
Bash functions
# https://danielmiessler.com/blog/the-one-line-cli-bandwidth-test/
alias bt="wget http://cachefly.cachefly.net/100mb.test -O /dev/null"
# Watch nginx logs
alias access='tail -f /var/log/nginx/access.log'
alias error='tail -f /var/log/nginx/error.log'
# column is used here to display a Tab Separated Values file with vertically aligned columns
# less is used so the arrow keys can be used to scroll up/down left/right
tsv() {
column -t -s$'\t' ${1:missing filename argument} | less -S
}
#######################################
# Display .tsv files in a terminal as a table with the ability to scroll with arrow-keys.
# Globals:
# None
# Arguments:
# 1: Path to tsv file
# Returns:
# None
#######################################
tsv() {
column -s $'\t' -t $1 | less -S
}
#######################################
# split comma-separated string into an array
# Globals:
# None
# Arguments:
# ${1} - string to split
# ${2} - separator (default is ",")
# Returns:
# ${result} - result value on success
#######################################
split_string() {
local ifs=${IFS}
IFS="${2:-,}"
read -a result < <(echo "${1}")
IFS=${ifs}
return 0
}
#######################################
# Convert fasta to fasta by stripping quality information.
# Globals:
# None
# Arguments:
# 1: Path to fastq file
# Returns:
# None
#######################################
fastq_to_fasta() {
local fastq="${1-}"
# Strip path: s=${s##*/}
# Strip specific extension: ${s%.txt}
# Strip generic extension: ${s%.*}
# awk is removing the quality information leaving only the contig name and nucleotide sequence.
if [[ $fastq == *.gz ]]; then
gunzip -c $fastq | awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' > ${fastq%.*.gz}.fasta
elif [ $fastq = *.bz2 ]; then
bunzip2 -c $fastq | awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' > ${fastq%.*.bz2}.fasta
else
awk '{if(NR%4==1) {printf(">%s\n",substr($0,2));} else if(NR%4==2) print;}' $fastq > ${fastq%.*}.fasta
fi
}
#######################################
# Split fasta by contig naming each file based on the contig description
# Globals:
# None
# Arguments:
# 1: Path to fasta file
# Returns:
# None
#######################################
split_fasta() {
local input_fasta="${1-}"
# http://merenlin.com/2014/08/top-5-bash-commands-for-handling-data/
awk '/^>/ {OUT=substr($0,2) ".fa"}; OUT {print >OUT}' $input_fasta
}
# Recursively set file and directory permissions.Source: http://superuser.com/questions/91935/how-to-chmod-755-all-directories-but-no-file-recursively
# To recursively give directories read & execute privileges:
recursive_grant_directory_permissions() {
local base_dir="${1-}"
local octal_permissions="${2:-755}"
find $base_dir -type d -exec chmod $octal_permissions {} +
}
Or, if there are many objects to process:
chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
Or, to reduce chmod spawning:
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644
A faster command, but doesn't have the desired result if, for example, the file was set with 777 permissions.
chmod -R u+rwX,go+rX,go-w /path
-R = recursively; u+rwX = Users can read, write and execute; go+rX = group and others can read and execute; go-w = group and others can't write.
The important thing to note here is that X acts differently to x - the man page says The execute/search bits if the file is a directory or any of the execute/search bits are set in the original (unmodified) mode. In other words, chmod u+X on a file won't set the execute bit; and g+X will only set it if it's already set for the user.
# Recursively extract tgz files
recursive_extract_tgz() {
local base_dir="${1:-./}"
find $base_dir -type f -name '*.tgz' -o -name '*.tar.gz' -execdir tar xzf {} \;
}
# Reverse complement DNA sequence
#echo "GATC" | rev | tr ATGC TACG
# Intialize zsh to use module load
#source ${MODULESHOME}/init/zsh
# HAK5 Microoptimization to reduce diskaccess/power consumption
# https://youtu.be/XloTXFgNL7s?t=669
tmpfs /tmp tmpfs defaults,noatime,nodirtime,size=512M,mode=1777 0 0
tmpfs /var/spool tmpfs defaults,noatime,nodirtime,size=512M,mode=1777 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nodirtime,size=512M,mode=1777 0 0
# CAUTION: logs lost on reboot
tmpfs /var/log tmpfs defaults,noatime,nodirtime,size=512M,mode=0755 0 0
#%Module1.0
#
# To use this template, fill in the module-whatis description
# and enable the prepend-path commands relevant to the package.
#
# For more configuration options see:
# http://modules.sourceforge.net/man/modulefile.html
proc ModulesHelp { } {
puts stderr "[ module-info name ]"
puts stderr "URL: https://github.com/USER/FOOBAR"
}
module-whatis "A modulefile template"
# moduleDir is the absolute path to directory containing this modulefile file.
# example: /packages/modulefiles/<package_name>/<package_version>
set moduleDir [ file dirname $ModulesCurrentModulefile ]
# packageDir is the absolute path to the package this module references.
# example: /packages/<package_name>/<package_version>
set packageDir [ file normalize ${modulesDir}/../../${moduleName} ]
#prepend-path PATH $packageDir
#prepend-path MANPATH $packageDir/share/man
#prepend-path CPATH $packageDir/include
#prepend-path LD_LIBRARY_PATH $packageDir/lib
#prepend-path PKG_CONFIG_PATH $packageDir/lib/pkgconfig
PACKAGE_TEMPLATE_VERSION?=dev
.PHONY: package_template
package_template: ${LOCAL}/package_template/${PACKAGE_TEMPLATE_VERSION}
${SRC}/package_template:
git clone https://github.com/USER/REPO.git $@
${LOCAL}/package_tempalte/dev: ${SRC}/package_template
cd $< \
&& git pull \
&& ./configure --prefix=$@ \
&& make \
&& make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment