polish: add check function

This commit is contained in:
kuoi 2024-07-03 17:14:33 +10:00
parent e8e40e4327
commit 01729e0726

206
RGBEPP.sh
View file

@ -19,9 +19,33 @@ PathSortdiamond=/home/guoyi/Downloads/PhD/wes/sortdiamond
HELP=false HELP=false
ARG_C='scaffolds'
#ARG_F='all'
ARG_M=16
ARG_T=8
### Get some arrays ### Get some arrays
ARGS=$(getopt -o c:,f:,g:,h,l:,m:,r:,t: --long contigs:,genes:,functions:,help,list:,memory:,reference:,threads: -n 'RGBEPP.sh' -- "$@") show_help(){
# echo -e "\t\t\t\t\t\t\t\033[0;31mR\033[0m\033[0;92mG\033[0m\033[0;94mB\033[0m \033[0;33mE\033[0m\033[0;94mP\033[0m\033[0;33mP\033[0m\n\t\t\t\t\tReference Genome based Exon Phylogeny Pipeline\n \
echo -e "\t\t\t\t\t\t\t\033[0;47;31mR\033[0m\033[0;47;92mG\033[0m\033[0;47;94mB\033[0m\033[0;47m \033[0m\033[0;47;33mE\033[0m\033[0;47;94mP\033[0m\033[0;47;33mP\033[0m\n\t\t\t\t\tReference Genome based Exon Phylogeny Pipeline\n \
Version: $pkgver\n \
License: GPL-3.0-only\n \
Author: Guoyi Zhang\n \
-c\t--contigs\tcontings type: scaffolds or contigs\n \
-g\t--genes\t\tgene file path\n \
-f\t--functions\tfunctions type (optional): all clean \n \
\t \t\tassembly fasta map pre split merge align\n \
-h\t--help\t\tshow this information\n \
-l\t--list\t\tlist file path\n \
-m\t--memory\tmemory settings (optional, default 16 GB)\n \
-r\t--reference\treference genome path\n \
-t\t--threads\tthreads setting (optional, default 8 threads)\n \
for example: bash $0 -c scaffolds -f all -l list -g genes \ \n \
\t -r Reference.exons.aa.fas \n"
}
ARGS=$(getopt -o c:,f:,g:,h,l:,m:,r:,t: --long contigs:,genes:,functions:,help,list:,memory:,reference:,threads:,macse:,sortdiamond:,splitfasta: -n 'RGBEPP.sh' -- "$@")
if [ $? != 0 ]; then if [ $? != 0 ]; then
echo "Failed to parse options." >&2 echo "Failed to parse options." >&2
exit 1 exit 1
@ -32,7 +56,7 @@ while true; do
case "$1" in case "$1" in
-c|--contigs) -c|--contigs)
case "$2" in case "$2" in
"") ARG_C='scaffolds'; shift 2 ;; "") shift 2 ;;
*) ARG_C=$2; shift 2 ;; *) ARG_C=$2; shift 2 ;;
esac ;; esac ;;
-g|--genes) -g|--genes)
@ -42,26 +66,12 @@ while true; do
esac ;; esac ;;
-f|--functions) -f|--functions)
case "$2" in case "$2" in
"") ARG_F='all'; shift 2 ;; "") shift 2 ;;
*) ARG_F=$2; shift 2 ;; *) ARG_F=$2; shift 2 ;;
esac ;; esac ;;
-h|--help) -h|--help)
# echo -e "\t\t\t\t\t\t\t\033[0;31mR\033[0m\033[0;92mG\033[0m\033[0;94mB\033[0m \033[0;33mE\033[0m\033[0;94mP\033[0m\033[0;33mP\033[0m\n\t\t\t\t\tReference Genome based Exon Phylogeny Pipeline\n \ show_help
echo -e "\t\t\t\t\t\t\t\033[0;47;31mR\033[0m\033[0;47;92mG\033[0m\033[0;47;94mB\033[0m\033[0;47m \033[0m\033[0;47;33mE\033[0m\033[0;47;94mP\033[0m\033[0;47;33mP\033[0m\n\t\t\t\t\tReference Genome based Exon Phylogeny Pipeline\n \ HELP=true;
Version: $pkgver\n \
License: GPL-3.0-only\n \
Author: Guoyi Zhang\n \
-c\t--contigs\tcontings type: scaffolds or contigs\n \
-g\t--genes\t\tgene file path\n \
-f\t--functions\tfunctions type (optional): all clean \n \
\t \t\tassembly fasta map pre split merge align\n \
-h\t--help\t\thelp: show this information\n \
-l\t--list\t\tlist file path\n \
-m\t--memory\tmemory settings (optional, default 16 GB)\n \
-r\t--reference\treference genome path\n \
-t\t--threads\tthreads setting (optional, default 8 threads)\n \
for example: bash $0 -c scaffolds -f all -l list -g genes -r Reference.exons.aa.fas \n"
HELP=true
shift ;; shift ;;
-l|--list) -l|--list)
case "$2" in case "$2" in
@ -70,7 +80,7 @@ while true; do
esac ;; esac ;;
-m|--memory) -m|--memory)
case "$2" in case "$2" in
"") ARG_M=16; shift 2 ;; "") shift 2 ;;
*) ARG_M=$2; shift 2 ;; *) ARG_M=$2; shift 2 ;;
esac ;; esac ;;
-r|--reference) -r|--reference)
@ -80,26 +90,92 @@ while true; do
esac ;; esac ;;
-t|--threads) -t|--threads)
case "$2" in case "$2" in
"") ARG_T=8; shift 2 ;; "") shift 2 ;;
*) ARG_T=$2; shift 2 ;; *) ARG_T=$2; shift 2 ;;
esac ;; esac ;;
--) shift; break ;; --macse)
*) echo "Internal error!"; exit 1 ;; case "$2" in
"") shift 2 ;;
*) PathMacse=$2; shift 2 ;;
esac ;;
--sortdiamond)
case "$2" in
"") shift 2 ;;
*) PathSortdiamond=$2; shift 2 ;;
esac ;;
--splitfasta)
case "$2" in
"") shift 2 ;;
*) PathSplitfsata=$2; shift 2 ;;
esac ;;
--)
show_help
HELP=true
shift; break ;;
*) echo "Unknown option: $1"
exit 1
;;
esac esac
done done
### Get and check some arguments ### Get and check some arguments
if [ "$HELP" = false ]; then check_var() {
if [ -z "$ARG_L" ]; then local var_name="$1"
echo "List argument can't be empty" local var_value="${!var_name}" # get value
exit 1
fi
readarray -t full_names < "$ARG_L" if [ -z "$var_value" ]; then
readarray -t genes < "$ARG_G" echo "Error: $var_name is not set or is empty"
length_fn=${#full_names[@]} exit 1
length_gn=${#genes[@]} else
echo "$var_name is set to: $var_value"
case "$var_name" in
"ARG_G")
readarray -t genes < "$var_value"
length_gn=${#genes[@]}
;;
"ARG_L")
readarray -t full_names < "$var_value"
length_fn=${#full_names[@]}
;;
esac
fi
}
check_path(){
local path_name="$1"
local path_value="${!path_name}" # get value
# expand ~
path_value=$(eval echo "$path_value")
if [ -e "$path_value" ]; then
echo "$path_name exists at: $path_value"
else
echo "Error: $path_name does not exist at: $path_value"
exit 1
fi
}
check_command() {
local cmd_name="$1"
if command -v "$cmd_name" >/dev/null 2>&1; then
echo "$cmd_name command exists."
else
echo "Error: $cmd_name command not found."
exit 1
fi
}
if [ "$HELP" = false ]; then
check_command "cp"
check_command "cd"
check_command "mv"
check_command "find"
check_command "mkdir"
fi fi
### Quality control && Trimming ### Quality control && Trimming
@ -109,6 +185,15 @@ if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "clean" ]; then
## Prepare ## Prepare
mkdir -p $DirQcTrim mkdir -p $DirQcTrim
check_var "ARG_L"
check_command "fastp"
readarray -t full_names < "$ARG_L"
length_fn=${#full_names[@]}
readarray -t genes < "$ARG_G"
length_gn=${#genes[@]}
## Quality control and trimming using fastp ## Quality control and trimming using fastp
for (( i=0; i<$length_fn; i++ )); do for (( i=0; i<$length_fn; i++ )); do
fastp -i $DirRaw/${full_names[$i]}_R1.fastq.gz -I $DirRaw/${full_names[$i]}_R2.fastq.gz -j $DirQcTrim/${full_names[$i]}.json -h $DirQcTrim/${full_names[$i]}.html -o $DirQcTrim/${full_names[$i]}_R1.fastq.gz -O $DirQcTrim/${full_names[$i]}_R2.fastq.gz -w $ARG_T fastp -i $DirRaw/${full_names[$i]}_R1.fastq.gz -I $DirRaw/${full_names[$i]}_R2.fastq.gz -j $DirQcTrim/${full_names[$i]}.json -h $DirQcTrim/${full_names[$i]}.html -o $DirQcTrim/${full_names[$i]}_R1.fastq.gz -O $DirQcTrim/${full_names[$i]}_R2.fastq.gz -w $ARG_T
@ -123,6 +208,9 @@ if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "assembly" ]; then
## Prepare ## Prepare
mkdir -p $DirAssembly mkdir -p $DirAssembly
check_var "ARG_L"
check_command "spades.py"
## De novo assembly using spades ## De novo assembly using spades
for (( i=0; i<$length_fn; i++ )); do for (( i=0; i<$length_fn; i++ )); do
mkdir -p $DirAssembly/${full_names[$i]} mkdir -p $DirAssembly/${full_names[$i]}
@ -137,10 +225,8 @@ fi
if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "fasta" ]; then if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "fasta" ]; then
## Check if the contigs type is specified ## Check if the contigs type is specified
if [ -z "$ARG_C" ] ; then check_var "ARG_C"
echo "Argument of contig type missing." check_var "ARG_L"
exit 1
fi
## Prepare ## Prepare
mkdir -p $DirFasta mkdir -p $DirFasta
@ -159,10 +245,9 @@ fi
if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "map" ]; then if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "map" ]; then
## Check if the reference or contigs type is specified ## Check if the reference or contigs type is specified
if [ -z "$ARG_R" ] || [ -z "$ARG_C" ] ; then check_var "ARG_C"
echo "Argument of reference or contig type missing." check_var "ARG_R"
exit 1 check_command "diamond"
fi
## Prepare ## Prepare
mkdir -p $DirMap mkdir -p $DirMap
@ -205,7 +290,12 @@ if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "map" ]; then
fi fi
if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "pre" ]; then if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "pre" ]; then
mkdir -p $DirPre mkdir -p $DirPre
check_var "ARG_L"
checkPath "PathSortdiamond"
for (( i=0; i<$length_fn; i++ )); do for (( i=0; i<$length_fn; i++ )); do
$PathSortdiamond $DirMap/${full_names[$i]}.m8 $DirPre/${full_names[$i]}.fasta $PathSortdiamond $DirMap/${full_names[$i]}.m8 $DirPre/${full_names[$i]}.fasta
done done
@ -215,6 +305,10 @@ fi
if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "split" ]; then if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "split" ]; then
mkdir -p $DirSplit mkdir -p $DirSplit
cd $DirPre cd $DirPre
check_var "ARG_L"
checkPath "PathSplitfasta"
for (( i=0; i<$length_fn; i++ )); do for (( i=0; i<$length_fn; i++ )); do
$PathSplitfsata ${full_names[$i]}.fasta $PathSplitfsata ${full_names[$i]}.fasta
done done
@ -225,10 +319,7 @@ fi
if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "merge" ]; then if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "merge" ]; then
## Check if the genes is specified ## Check if the genes is specified
if [ -z "$ARG_G" ] ; then check_var "ARG_G"
echo "Argument of genes list missing."
exit 1
fi
mkdir -p $DirMerge mkdir -p $DirMerge
cd $DirSplit cd $DirSplit
@ -246,34 +337,17 @@ fi
if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "align" ]; then if [ "$ARG_F" = "all" ] || [ "$ARG_F" = "align" ]; then
## Check if the genes is specified ## Check if the genes is specified
if [ -z "$ARG_G" ] ; then check_var "ARG_G"
echo "Argument of genes list missing." check_command "java"
exit 1 check_command "parallel"
fi checkPath "PathMacse"
# current_thread=0 # current_thread=0
mkdir -p $DirAlign mkdir -p $DirAlign
mkdir -p $DirAlign/AA && mkdir -p $DirAlign/NT mkdir -p $DirAlign/AA && mkdir -p $DirAlign/NT
cd $DirMerge cd $DirMerge
align_by_macse() { parallel -j $ARG_T java -jar $PathMacse -prog alignSequences -seq {}.fasta -out_AA ../$DirAlign/AA/{}.fasta -out_NT ../$DirAlign/NT/{}.fasta ::: "${genes[@]}"
java -jar $PathMacse -prog alignSequences -seq $1.fasta -out_AA ../$DirAlign/AA/$1.fasta -out_NT ../$DirAlign/NT/$1.fasta &
}
export -f align_by_macse
parallel -j $ARG_T align_by_macse ::: "${genes[@]}"
# for (( i=0; i<$length_gn; i++ ))
# do
# java -jar $PathMacse -prog alignSequences -seq ${genes[$i]}.fasta -out_AA ../$DirAlign/AA/${genes[$i]}.fasta -out_NT ../$DirAlign/NT/${genes[$i]}.fasta &
# ((current_thread++))
# if [ $current_thread -eq $ARG_T ]; then
# wait
# current_thread=0
# fi
# done
cd - cd -