You might find that in your daily computer work, there are some commands that are long and you execute quite often. Sometimes it may also be a series of commands that you execute in a row that you do quite often. It may be worth it to write a custom function that wraps these lines of code into a short, so that you do not have to type all of these commands anymore.
We have already discussed one such function in a previous section:
# easier viewing of tab separated files
function coltab {
column -t -s $'\t' $1 | less -S
}
I will discuss here some more custom functions that I found are very useful in my daily workflow. To add these functions to your system, simply add them to your .bashrc
Reformatting FASTA files downloaded from NCBI
For most of my analyses, the header format of NCBI FASTA files is very annoying. This function will convert the annoying format into >SpeciesName_i_AccessionNumber
# format NCBI headers to something readable
function reformat_ncbi_headers {
newfile=${1%.fasta}.hdfmt.fasta
cp $1 $newfile
sed -i -r -e '/^>/ s/ >.*//' -e 's/>([^ ]*).*\[(.*)\]/>\2_i_\1/' -e 's/ /_/g' $newfile
sed -i -r -e '/^>/ s/gi.*ref\|(.*)\|/\1/' $newfile
}
Replacing work names with final names for publication
In my experience I do my analyses with new genomes / transcriptomes etc I work with 'worknames'. For example 'bin125' or 'L4', or 'BBO'. That is fine while you do your analyses, but in the end when you want to publish your work you'll want to have proper names. This function takes a mapping file that contains the short / worknames in the first column and the final names in the second column, and replaces the worknames with the final names in tree files, FASTA files, you name it.
# replace taxanames in trees, fasta, etc
function replace_names {
input=$1
mappingfile=$2
cp $input $input.nms
cat $mappingfile | while read SEARCH REPLACE; do
sed -i -r "s/$SEARCH/$REPLACE/" $1.nms
done
}
Some other functions
# fasta to phylip
# depends on trimal
function fa2phy {
trimal -in $1 -out ${1%.*}.phylip -phylip
}
# reverse complement function
function revcomp {
tr "[ATGCatgcNn]" "[TACGtacgNn]" | rev
}
# sum up all numbers in a list
function total {
tr '\n' '+' $1 | sed "s/\+$/\n/" | bc
}
