2015-10-07 11:32:17 -03:00
|
|
|
#
|
|
|
|
# Bash file to be sourced at the begining of each bash script
|
|
|
|
# for setting up basic variables and functions
|
|
|
|
#
|
|
|
|
|
2015-11-08 19:33:00 +01:00
|
|
|
export AwkCmd="gawk"
|
|
|
|
|
2021-11-03 13:19:01 +01:00
|
|
|
# setup the LC_ALL environment variable (for Linux mostly)
|
|
|
|
# so the GNU tools (like sort) will work properly
|
|
|
|
|
|
|
|
export LANG=C
|
|
|
|
export LC_ALL=C
|
|
|
|
|
|
|
|
|
2015-10-07 11:32:17 -03:00
|
|
|
########################
|
|
|
|
#
|
|
|
|
# General usage functions
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
function getAbsolutePath {
|
|
|
|
[[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } ||
|
|
|
|
{ cd "$(dirname "$1")"; echo "$(pwd -P)/$(basename "$1")"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
# Manage temp directory
|
|
|
|
|
|
|
|
function pushTmpDir {
|
2015-11-09 15:27:32 +01:00
|
|
|
TMP_DIR=$(mktemp -d -t "$1_proc_$$_XXXXXX")
|
2015-10-07 11:32:17 -03:00
|
|
|
pushd $TMP_DIR >& /dev/null
|
2015-10-11 10:39:59 -03:00
|
|
|
TMP_DIR_STACK="$TMP_DIR $TMP_DIR_STACK"
|
|
|
|
logdebug "Pushing temp directory $TMP_DIR"
|
|
|
|
logdebug "Stack : ${TMP_DIR_STACK}"
|
2015-10-07 11:32:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
function popTmpDir {
|
2015-11-08 19:33:00 +01:00
|
|
|
TMP_DIR=$(echo $TMP_DIR_STACK | $AwkCmd '{print $1}')
|
|
|
|
TMP_DIR_STACK=$(echo $TMP_DIR_STACK | $AwkCmd '{$1="";print $0}')
|
2015-10-07 11:32:17 -03:00
|
|
|
popd >& /dev/null
|
|
|
|
rm -rf $TMP_DIR >& /dev/null
|
2015-10-11 10:39:59 -03:00
|
|
|
logdebug "Poping temp directory $TMP_DIR"
|
|
|
|
logdebug "Stack : ${TMP_DIR_STACK}"
|
2015-10-07 11:32:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Logging functions
|
|
|
|
|
|
|
|
|
|
|
|
function errcho {
|
|
|
|
>&2 echo $*
|
|
|
|
}
|
|
|
|
|
|
|
|
function openLogFile {
|
2015-10-11 10:39:59 -03:00
|
|
|
ORG_LOGFILE=$1
|
|
|
|
export ORG_LOGFILE
|
|
|
|
touch ${ORG_LOGFILE}
|
2015-10-07 11:32:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function loginfo {
|
|
|
|
errcho `date +'%Y-%m-%d %H:%M:%S'` "[OA INFO ] $$ -- $1"
|
2015-10-11 10:39:59 -03:00
|
|
|
if [[ ! -z ${ORG_LOGFILE} ]]; then
|
|
|
|
echo `date +'%Y-%m-%d %H:%M:%S'` "[OA INFO ] $$ -- $1" >> ${ORG_LOGFILE}
|
2015-10-07 11:32:17 -03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function logerror {
|
|
|
|
errcho `date +'%Y-%m-%d %H:%M:%S'` "[OA ERROR ] $$ -- $1"
|
2015-10-11 10:39:59 -03:00
|
|
|
if [[ ! -z ${ORG_LOGFILE} ]]; then
|
|
|
|
echo `date +'%Y-%m-%d %H:%M:%S'` "[OA ERROR ] $$ -- $1" >> ${ORG_LOGFILE}
|
2015-10-07 11:32:17 -03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function logwarning {
|
|
|
|
errcho `date +'%Y-%m-%d %H:%M:%S'` "[OA WARNING] $$ -- $1"
|
2015-10-11 10:39:59 -03:00
|
|
|
if [[ ! -z ${ORG_LOGFILE} ]]; then
|
|
|
|
echo `date +'%Y-%m-%d %H:%M:%S'` "[OA WARNING] $$ -- $1" >> ${ORG_LOGFILE}
|
2015-10-07 11:32:17 -03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-11 10:39:59 -03:00
|
|
|
function logdebug {
|
|
|
|
if [[ ! -z ${ORG_DEBUG} ]]; then
|
|
|
|
errcho `date +'%Y-%m-%d %H:%M:%S'` "[OA DEBUG ] $$ -- $1"
|
|
|
|
if [[ ! -z ${ORG_LOGFILE} ]]; then
|
|
|
|
echo `date +'%Y-%m-%d %H:%M:%S'` "[OA DEBUG ] $$ -- $1" >> ${ORG_LOGFILE}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-07 11:32:17 -03:00
|
|
|
# Sequence related functions
|
|
|
|
|
2015-10-08 15:38:27 -03:00
|
|
|
# Counts how many sequences are stored in a fasta file
|
|
|
|
# - $1 : The fasta file to count
|
2015-10-07 11:32:17 -03:00
|
|
|
function fastaCount {
|
|
|
|
grep '^>' $1 | wc -l
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:38:27 -03:00
|
|
|
|
|
|
|
# compute the sequence length from a fasta sequence
|
|
|
|
# - $1 : The fasta file to cut
|
|
|
|
function seqlength {
|
|
|
|
cat $1 | \
|
|
|
|
wc |\
|
2015-11-08 19:33:00 +01:00
|
|
|
$AwkCmd -v t="`head -1 $1 | wc -c`" '{print $3 - t - $1 + 1}'
|
2015-10-08 15:38:27 -03:00
|
|
|
}
|
|
|
|
|
2021-11-03 13:19:01 +01:00
|
|
|
function readfirstfastaseq {
|
|
|
|
awk '(/^>/ && first) {on=0}
|
|
|
|
(on) {seq=seq $1}
|
|
|
|
(/^>/ && ! first) { first = 1
|
|
|
|
on = 1
|
|
|
|
}
|
|
|
|
END {print seq}
|
|
|
|
' $*
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:38:27 -03:00
|
|
|
# extract a subseq from a fasta sequence
|
|
|
|
# - $1 : The fasta file to cut
|
|
|
|
# - $2 : First position of the subsequence (first position is numered 1),
|
|
|
|
# - $3 : End of the subsequence (included in the subsequence)
|
|
|
|
function cutseq {
|
2015-11-08 19:33:00 +01:00
|
|
|
$AwkCmd -v from=$2 -v end=$3 'function printfasta(seq) { \
|
2015-10-08 15:38:27 -03:00
|
|
|
seqlen=length(seq); \
|
|
|
|
for (i=1; i <= seqlen; i+=60) \
|
|
|
|
print substr(seq,i,60); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/^>/ {print $0} \
|
|
|
|
! /^>/ {seq=seq$0} \
|
|
|
|
END {printfasta(substr(seq,from,end-from+1))}' $1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Joins a set of sequences stored in a fasta file into
|
|
|
|
# a single sequence
|
|
|
|
# - $1 : The fasta file containing the sequences to join
|
|
|
|
function joinfasta {
|
2015-11-08 19:33:00 +01:00
|
|
|
$AwkCmd '(NR==1 && /^>/) {print $0} \
|
2016-04-25 11:15:14 +02:00
|
|
|
! /^>/ {print $0}' "${1}" | \
|
2015-10-08 15:38:27 -03:00
|
|
|
formatfasta
|
|
|
|
}
|
|
|
|
|
2016-04-25 11:15:14 +02:00
|
|
|
function fasta1line {
|
|
|
|
$AwkCmd '(/^>/ && seq !="") {print seq} \
|
|
|
|
/^>/ {print $0;seq=""} \
|
|
|
|
!/^>/ {seq=seq $0} \
|
|
|
|
END {print seq}' "${1}"
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:38:27 -03:00
|
|
|
function formatfasta {
|
2016-10-05 08:01:30 -03:00
|
|
|
$AwkCmd 'function printfasta(seq) { \
|
2015-10-08 15:38:27 -03:00
|
|
|
seqlen=length(seq); \
|
|
|
|
for (i=1; i <= seqlen; i+=60) \
|
|
|
|
print substr(seq,i,60); \
|
|
|
|
} \
|
2016-10-05 08:01:30 -03:00
|
|
|
(seq && /^>/) { printfasta(seq); \
|
|
|
|
seq=""} \
|
2015-10-08 15:38:27 -03:00
|
|
|
/^>/ { print $0 } \
|
|
|
|
! /^>/ { seq=seq $0 } \
|
2016-04-25 11:15:14 +02:00
|
|
|
END { printfasta(seq)}' "${1}"
|
2015-10-08 15:38:27 -03:00
|
|
|
}
|
|
|
|
|
2021-11-03 13:19:01 +01:00
|
|
|
# Reverse complement a DNA string
|
|
|
|
# - $1 : The DNA string to reverse complement
|
|
|
|
function reversecomp {
|
|
|
|
echo $1 \
|
|
|
|
| tr 'Aa' '@!' | tr 'Tt' 'Aa' | tr '@!' 'Tt' \
|
|
|
|
| tr 'Cc' '@!' | tr 'Gg' 'Cc' | tr '@!' 'Gc' \
|
|
|
|
| tr 'Mm' '@!' | tr 'Kk' 'Mm' | tr '@!' 'Kk' \
|
|
|
|
| tr 'Rr' '@!' | tr 'Yy' 'Rr' | tr '@!' 'Yy' \
|
|
|
|
| tr 'Ww' '@!' | tr 'Ss' 'Ww' | tr '@!' 'Ss' \
|
|
|
|
| tr 'Bb' '@!' | tr 'Vv' 'Bb' | tr '@!' 'Vv' \
|
|
|
|
| tr 'Dd' '@!' | tr 'Hh' 'Dd' | tr '@!' 'Hh' \
|
|
|
|
| rev
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process management related function
|
|
|
|
#
|
|
|
|
|
|
|
|
function timeoutcmd() {
|
|
|
|
local seconde=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
$* &
|
|
|
|
|
|
|
|
local mainpid=$!
|
|
|
|
sleep $seconde &
|
|
|
|
local sleeppid=$!
|
|
|
|
|
|
|
|
local nproc=$(ps $mainpid $sleeppid | tail -n +2 | wc -l)
|
|
|
|
|
|
|
|
while (( nproc > 1 )) ; do
|
|
|
|
sleep 1
|
|
|
|
nproc=$(ps $mainpid $sleeppid | tail -n +2 | wc -l)
|
|
|
|
done
|
|
|
|
|
|
|
|
local timealive=$(ps $sleeppid | tail -n +2 | wc -l)
|
|
|
|
|
|
|
|
if (( timealive > 0 )) ; then
|
|
|
|
kill -9 $sleeppid
|
|
|
|
else
|
|
|
|
if (( $(ps $mainpid | tail -n +2 | wc -l) > 0 )) ; then
|
|
|
|
kill -9 $mainpid
|
|
|
|
logwarning "Timeout after ${seconde}s on command : $*"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:38:27 -03:00
|
|
|
|
2015-10-07 11:32:17 -03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
########################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################
|
|
|
|
#
|
|
|
|
# Local variable definitions
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2015-11-08 14:28:57 +01:00
|
|
|
# The absolute path to the ORG.Annot home directory
|
2015-10-07 11:32:17 -03:00
|
|
|
ORG_HOME=`getAbsolutePath $(dirname ${BASH_SOURCE[0]})/..`
|
|
|
|
|
2015-11-08 14:28:57 +01:00
|
|
|
ORG_PORTNAME=`${ORG_HOME}/config/guess_port` # The architecture running the ORG.Annot instance
|
|
|
|
|
|
|
|
BIN_DIR="${ORG_HOME}/ports/${ORG_PORTNAME}/bin" # Directory containing binaries for this port
|
|
|
|
|
|
|
|
SCRIPT_DIR="${ORG_HOME}/scripts" # Directory containing scripts utilities
|
2015-10-07 11:32:17 -03:00
|
|
|
|
|
|
|
PROG_DIR="$(getAbsolutePath $(dirname $0))" # Directory containing the main script file
|
|
|
|
|
2015-11-08 14:28:57 +01:00
|
|
|
LIB_DIR="$(getAbsolutePath ${PROG_DIR}/../lib)" # Directory containing the main script libraries
|
2015-10-07 11:32:17 -03:00
|
|
|
|
|
|
|
CALL_DIR="$(getAbsolutePath $(pwd))" # Directory from where the main script is called
|
|
|
|
|
|
|
|
|
2015-11-08 14:28:57 +01:00
|
|
|
DATA_DIR="${ORG_HOME}/data" # Directory containing reference data for the annotation
|
|
|
|
|
|
|
|
IR_DATA_DIR="${DATA_DIR}/ir" # Directory containing data related to
|
|
|
|
# IRs detection
|
|
|
|
|
|
|
|
TRNA_DATA_DIR="${DATA_DIR}/trna" # Directory containing data related to
|
|
|
|
# tRNAs detection
|
|
|
|
|
2016-04-25 11:15:14 +02:00
|
|
|
CDS_DATA_DIR="${DATA_DIR}/cds" # Directory containing data related to
|
2015-11-08 14:28:57 +01:00
|
|
|
# CDSs detection
|
2015-10-12 07:15:08 -03:00
|
|
|
|
2015-11-08 14:28:57 +01:00
|
|
|
RRNA_DATA_DIR="${DATA_DIR}/rrna" # Directory containing data related to
|
|
|
|
# rRNAs detection
|
2015-10-10 19:12:02 -03:00
|
|
|
|
2016-05-02 10:56:40 +02:00
|
|
|
NUCRRNA_DATA_DIR="${DATA_DIR}/nucrrna" # Directory containing data related to
|
|
|
|
# rRNAs detection
|
|
|
|
|
|
|
|
ITS_DATA_DIR="${DATA_DIR}/its" # Directory containing data related to
|
|
|
|
# rRNAs detection
|
|
|
|
|
2015-10-07 11:32:17 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
########################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################
|
|
|
|
#
|
|
|
|
# Altering the environment
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
# We alter the path to include the bin dir corresponding to the port
|
2015-11-08 14:28:57 +01:00
|
|
|
PATH="${SCRIPT_DIR}:${BIN_DIR}:${PATH}"
|
2015-10-07 11:32:17 -03:00
|
|
|
export PATH
|
|
|
|
|
|
|
|
# Force to basic international setting for a correction behaviour of AWK on mac with float
|
|
|
|
export LANG=C
|
|
|
|
export LC_ALL=C
|
|
|
|
|