User Tools

Site Tools


handy_custom_functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
handy_custom_functions [2021/05/12 14:19] 168.91.18.151handy_custom_functions [2023/07/25 12:08] (current) 134.190.232.186
Line 11: Line 11:
 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'' 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''
  
-===Selecting or removing sequences from a FASTA file===+ 
 +===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''
  
 <code> <code>
-fish out a sequence from a fasta file +format NCBI headers to something readable 
-function grabseq +function reformat_ncbi_headers 
-    fasta=$3 +    newfile=${1%.fasta}.hdfmt.fasta 
-    to_grab=$+    cp $$newfile 
-    case "$1" in +    sed -i -r -e '/^>/ s/ >.*//' -e 's/>([^ ]*).*\[(.*)\]/>\2_i_\1/' -e 's/ /_/g$newfile 
-        -s) seqtk subseq $fasta <(grep    "$to_grab" $fasta | sed -e 's/>//' -e 's/ .*//');; +    sed -i -r -e '/^>/ s/gi.*ref\|(.*)\|/\1/' $newfile
-        -l) seqtk subseq $fasta <(grep -f "$to_grab" $fasta | sed -e 's/>//' -e 's/ .*//');; +
-        *) echo -e "grabseq -s <seqname> <fasta> to grab a single sequence\ngrab_seq -l <seqlist> <fasta> to grab a list of sequences" +
-    esac+
 } }
 </code> </code>
  
-This is essentially a wrapper for the [[https://github.com/lh3/seqtk|seqtk]] tool.+===Replacing work names with final names for publication===
  
-<code> +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 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.
-# select any sequence that has <pattern> in the header +
-# useful if you want to find single sequence +
-$ grabseq -s <pattern> <FASTA> > <NEWFASTA> +
- +
-# select a particular set of sequences that have <pattern> in their header and are in the <pattern_list> file +
-# useful if you want to multiple sequences +
-$ grabseq -l <pattern_list> <FASTA> > <NEWFASTA> +
-</code>+
  
 <code> <code>
-remove a particular entry from a fasta file +replace taxanames in trees, fasta, etc 
-function rmseq +function replace_names 
-    fasta=$3 +    input=$1 
-    to_rmv=$2 +    mappingfile=$2 
-    case "$1" in +    cp $input $input.nms 
-        -s) seqtk subseq $fasta <( grep ">" $fasta grep -E -v "$to_rmv" | sed "s/>//" );+    cat $mappingfile while read SEARCH REPLACEdo 
-        -l) seqtk subseq $fasta <( grep ">" $fasta | grep -v -f "$to_rmv" | sed "s/>//" );; +        sed --"s/$SEARCH/$REPLACE/" $1.nms 
-        *) echo -e "rmseq -s <seqname> <fasta> to remove a single sequence\nremove_seq -l <seqlist> <fasta> to remove a list of sequences" +    done
-    esac+
 } }
 </code> </code>
  
-This function does the opposite of grabseq. It will remove particular sequences from a FASTA file.+===Some other functions===
  
 <code> <code>
-remove a particular sequence that has <pattern> in the header +fasta to phylip 
-rmseq -s <pattern> <FASTA> > <NEWFASTA>+# depends on trimal 
 +function fa2phy { 
 +    trimal -in $-out ${1%.*}.phylip -phylip 
 +}
  
-remove a particular set of sequences that have <pattern> in the header and are in the <pattern_list> file +reverse complement function 
-rmseq -l <pattern_list> <FASTA> > <NEWFASTA>+function revcomp { 
 +    tr "[ATGCatgcNn]" "[TACGtacgNn]" | rev 
 +
 + 
 +# sum up all numbers in a list 
 +function total { 
 +    tr '\n' '+' $1 | sed "s/\+$/\n/" | bc 
 +}
 </code> </code>
handy_custom_functions.1620839968.txt.gz · Last modified: by 168.91.18.151