#!/bin/bash # script to admin and import data to SelenoDB. # Type "./selenodb_admin -h" for details # Sergi Castellano, scaste@imim.es set +o verbose set +o xtrace ### GLOBAL VARIABLES ### LIB=lib DB=selenodb INIT=data/init META=data/meta GENE=data/genes POST=data/post LOG=logs LOGF=$(date "+20%y-%m-%d")_selenodb.log LOGG=genes_imported.log LOGGG=$(date "+20%y-%m-%d")_genes_imported.log ### FUNCTIONS ### source $LIB/help.sh source $LIB/import.sh source $LIB/admin.sh ### SHOW IF WRONG COMMAND LINE ### usage="USAGE $0 -a [create drop dump undump] -i [init meta gene] -d [dir]" ### PICK UP COMMAND LINE OPTIONS ### while getopts ":a:i:d:h" opt; do case $opt in a) admin=$OPTARG;; i) import=$OPTARG;; d) dir=$OPTARG;; h) help;; \?) echo $usage # to catch invalid options exit 1;; esac done shift $(($OPTIND -1)) ######### # ADMIN # ######### ##################### # - DUMP DATABASE - # ##################### case $admin in dump) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF # A shorter but less clear syntax is: # mysql -e 'USE selenodb' 2>/dev/null && dump && fdone || ferror;; if mysql -e "USE $DB" 2>/dev/null; then if dump; then fdone else ferror; exit 1 # Dump did not work fi else echo -e "### DUMP $DB DATABASE ###\n" | log_tee echo -e "DATABASE does NOT exist. No DUMP is possible!\n" | log_tee # Database does not exist exit 1 fi;; ############## # - UNDUMP - # ############## undump) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF # A shorter but less clear syntax is: # mysql -e 'USE selenodb' 2>/dev/null && fdone && dump && fdone && undump && fdone && dump || ferror if mysql -e "USE $DB" 2>/dev/null; then if dump; then fdone if undump; then fdone if dump; then fdone else ferror # Dump did not work fi else ferror; exit 1 # Undump did not work fi else ferror; exit 1 # Dump did not work fi else echo -e "### UNDUMP $DB DATABASE ###\n" | log_tee echo -e "DATABASE does NOT exist. CREATE it first!\n" | log_tee # Database does not exist exit 1 fi;; ##################### # - DROP DATABASE - # ##################### drop) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF if mysql -e "USE $DB" 2>/dev/null; then if dump; then fdone if drop; then fdone else ferror; exit 1 # Drop did not work fi else ferror; exit 1 # Dump did not work fi else echo -e "### DROP $DB DATABASE ###\n" | log_tee echo -e "DATABASE does NOT exist. No DROP is possible!\n" | log_tee # Database does not exist exit 1 fi;; ####################### # - CREATE DATABASE - # ####################### create) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF if mysql -e "USE $DB" 2>/dev/null; then echo -e "### CREATE $DB DATABASE ###\n" | log_tee echo -e "DATABASE exist. DROP it first!\n" | log_tee exit 1 else if create; then fdone if load; then fdone if dump; then fdone else ferror; exit 1 # Dump did not work fi else ferror; exit 1 # Load schema did not work fi else ferror; exit 1 # Create did not work fi fi;; # ANY OTHER OPTION # *) echo $usage;; esac ######################### # - INITIALIZE TABLES - # ######################### # Imports tables one by one in the right order (there are dependencies!) # Stops if error and notify erroneous table # No need to rollback imported tables # Those tables (files) are tagged as ".imported" # Retryes again from the unsuccessful table case $import in init) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF echo -e "### INITIALIZE DATABASE $DB ###\n" | log_tee if mysql -e "USE $DB" 2>/dev/null; then # Check DB exists for name in $INIT/*/*/*; do mv $name ${name%.imported}; done # prepare init files if import "$INIT/*/*/*"; then # parameter is a list of files echo -e "DATABASE INITIALIZATION DONE!\n" | log_tee else echo -e "DATABASE INITIALIZATION ERROR!\n" | log_tee exit 1 fi else echo -e "DATABASE does NOT exist: No INITIALIZATION data import is possible\n" | log_tee exit 1 fi;; #################################### # - GENE INDEPENDENT (META) DATA - # #################################### # Imports tables one by one in the right order (there are dependencies!) # Stops if error and notify erroneous table # No need to rollback imported tables # Those tables (files) are tagged as ".imported" # Retryes again from the unsuccessful table meta) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF echo -e "### IMPORT META DATA ###\n" | log_tee if mysql -e "USE $DB" 2>/dev/null; then # Check DB exists for name in $META/*/*/*; do mv $name ${name%.imported}; done # prepare meta files if import "$META/*/*/*"; then # parameter is a list of files echo -e "META DATA IMPORT DONE!\n" | log_tee else echo -e "META DATA IMPORT ERROR!\n" | log_tee exit 1 fi else echo -e "DATABASE does NOT exist: No META data IMPORT is possible\n" | log_tee exit 1 fi;; #################################### # - GENE INDEPENDENT (POST) DATA - # #################################### # Imports tables one by one in the right order (there are dependencies!) # Stops if error and notify erroneous table # No need to rollback imported tables # Those tables (files) are tagged as ".imported" # Retryes again from the unsuccessful table post) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF echo -e "### IMPORT POST DATA ###\n" | log_tee if mysql -e "USE $DB" 2>/dev/null; then # Check DB exists for name in $POST/*/*/*; do mv $name ${name%.imported}; done # prepare meta files if import "$POST/*/*/*"; then # parameter is a list of files echo -e "POST DATA IMPORT DONE!\n" | log_tee else echo -e "POST DATA IMPORT ERROR!\n" | log_tee exit 1 fi else echo -e "DATABASE does NOT exist: No META data IMPORT is possible\n" | log_tee exit 1 fi;; ########################### # - GENE DEPENDENT DATA - # ########################### # Imports tables one by one in the right order (there are dependencies!) # Stops if error and notify erroneous table # Usually, no need to rollback imported tables # Those tables (files) are tagged as ".imported" # Retryes again from the unsuccessful table gene) echo -e "###########################" >> $LOG/$LOGF echo -e "### $(date "+%H:%M:%S %m-%d-20%y") ###" >> $LOG/$LOGF echo -e "###########################\n" >> $LOG/$LOGF echo -e "### IMPORT GENE ANNOTATION DATA ###\n" | log_tee echo -e "Gene: $dir\n" | log_tee if mysql -e "USE $DB" 2>/dev/null; then # Check DB exists if dump; then for name in $GENE/$dir/*/*/*.imported; do mv $name ${name%.imported}; done # prepare gene files rm -f $GENE/$dir/*/*/*.tmp # remove tmp files of a previous annotation if import "$GENE/$dir/*/*/*"; then # parameter is a list of files if import "$GENE/$dir/*/*/*.tmp"; then # Second round if [ -e $GENE/$dir/*/*/*.tmp ]; then if import "$GENE/$dir/*/*/*.tmp"; then # Third round: import protein_release_ext_ref.tmp echo -e "GENE ANNOTATION DATA IMPORT DONE!\n" | log_tee echo -e "$(date "+%m-%d-20%y"): $dir\n" >> $LOG/$LOGG if dump; then fdone else ferror fi else echo -e "GENE ANNOTATION DATA IMPORT ERROR!\n" | log_tee exit 1 fi else echo -e "GENE ANNOTATION DATA IMPORT DONE!\n" | log_tee echo -e "$(date "+%m-%d-20%y"): $dir\n" >> $LOG/$LOGG if dump; then fdone else ferror fi fi else echo -e "GENE ANNOTATION DATA IMPORT ERROR!\n" | log_tee exit 1 fi else echo -e "GENE ANNOTATION DATA IMPORT ERROR!\n" | log_tee exit 1 fi else ferror; exit 1 fi else echo -e "DATABASE does NOT exist: No GENE data IMPORT is possible\n" | log_tee exit 1 fi;; # ANY OTHER OPTION # *) echo $usage;; esac # end gene exit 0 # succesful end of program