#!/bin/bash
#------------
#PanRV installation Bash Script
#Kanwal Naz
#January 02,2017
#ASAB, NUST

echo "----During installation, system may require paths. In case of any

difficulty during installation and path setting consult the Trouble Shooting section
Poor ineternet connection may download some files partially that may cause problem in propoer installation.
In this case manual download is recommended -----"

cd /home/$USER
mkdir PanRV
sudo chmod -R 777 /home/$USER/PanRV
sudo chown -R $USER /home/$USER/PanRV/*
echo "------------------------------------------------------------------------"
echo "----------------------STEP 1:Basic Installations------------------------"
echo "------------------------------------------------------------------------"
sudo apt-get update
sudo apt-get install unrar-free 
sudo apt-get install unrar
sudo apt-get install gcc build-essential 
sudo apt-get install lib32stdc++6
echo "STEP 2: Press Enter to install Perl & bioperl"
read xyz
sudo apt-get install perl
sudo apt-get install bioperl
echo "STEP 3: Press Enter to install blast"
read xyz
sudo apt-get install blast2 
sudo apt-get install ncbi-blast+ 
echo "STEP 4: Press Enter to install java"
read xyz
sudo apt-get remove --auto-remove openjdk*
sudo apt install default-jre
echo "STEP 5: Press Enter to install Docker"
read xyz
#sudo apt-get remove docker docker-engine docker.io
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
docker --version
sudo chmod -R 777 /usr/bin/docker
sudo chown -R $USER /usr/bin/docker
echo "----------------------Basic Installations Done------------------------"
echo "------------------------------------------------------------"
echo "------------------------------------------------------------"
echo "-------STEP 2:Pangenome Analysis Tool Installatoin-------------"
echo "------------------------------------------------------------"
echo "STEP 5: Press Enter to Roary"
read xyz
sudo apt-get install bedtools cd-hit ncbi-blast+ mcl parallel cpanminus prank mafft fasttree
sudo apt-get install roary
echo "---------- Pangenome Analysis Tool Installed Successfully-------------"

cd /home/$USER/PanRV
mkdir Results
cd Results
mkdir log
cd log
touch PanRV.log
cd ..
cd ..
sudo chmod -R 777 /home/$USER/PanRV/Results
sudo chown -R $USER /home/$USER/PanRV/Results/*

echo "------------------------------------------------------------------------"
echo "STEP 3: Press Enter to Set Path variables"
read xyz
echo "***************** | Setting up Path Variables | **********************"
echo "export $HOME:/usr/bin:/usr/local/bin" >> ~/.bashrc 
echo "export PATH=$PATH:/usr/bin:/usr/local/bin" >> ~/.bashrc 
echo "export BLASTDIR=/usr/bin" >> ~/.bashrc 
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64" >> ~/.bashrc 
echo "export LD_RUN_PATH=/usr/local/lib64" >> ~/.bashrc
echo "export PSORT_HMMTOP=/home/$USER/PanRV/hmmtop_2.1" >> ~/.bashrc 
echo "export HMMTOP_ARCH=/home/$USER/PanRV/hmmtop_2.1/hmmtop.arch" >> ~/.bashrc 
echo "export HMMTOP_PSV=/home/$USER/PanRV/hmmtop_2.1/hmmtop.psv" >> ~/.bashrc 
#echo "export PSORT_ROOT=/usr/local/psort" >> ~/.bashrc 
echo "export PATH=$PATH:$BLASTDIR:$PSORT_HMMTOP:$HMMTOP_ARCH:$HMMTOP_PSV" >> ~/.bashrc 
echo "source ./bashrc" >> ~/.bashrc 

echo "------------------------------------------------------------------------"
echo "STEP 4: Press Enter to install HMMTOP"
read xyz
wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/hmmtop_2.1.tar/download" -O hmmtop2.1.tar
read xyz
tar -zxvf hmmtop2.1.tar 
cd hmmtop_2.1
sudo chmod -R 777 /home/$USER/PanRV/hmmtop_2.1
sudo chown -R $USER /home/$USER/PanRV/hmmtop_2.1/*
cc hmmtop.c -lm -o hmmtop 
cd .. 
rm hmmtop2.1.tar  
echo "***************** | Download and Install HMMTOP| **********************"

echo "STEP 5: Press Enter to complete PSortb installation"
read xyz
sudo docker pull brinkmanlab/psortb_commandline:1.0.2
wget --no-check-certificate "https://raw.githubusercontent.com/brinkmanlab/psortb_commandline_docker/master/psortb"
chmod +x psortb
sudo sed -i -e 's/sudo//g' psortb
sudo sed -i -e 's/-ti//g' psortb
echo "***************** | PSORTB Installed | **********************"

echo "------------------------------------------------------------------------"
echo "STEP 6: Press Enter to Install OSDDLinux"
read xyz
echo "***************** |OSDLinux installation Start| **********************"
cd /
sudo mkdir /gpsr 
useradd -d /gpsr $USER
cd /home/$USER/PanRV
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1CYCwl1uX6w2Rm-uoQZsTRChuxtI4D4rl' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1CYCwl1uX6w2Rm-uoQZsTRChuxtI4D4rl" -O base.tar.gz && rm -rf /tmp/cookies.txt
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1IqftUGCyApUjAAtFfbqAcVoh5hlu2AwU' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1IqftUGCyApUjAAtFfbqAcVoh5hlu2AwU" -O data.tar.gz && rm -rf /tmp/cookies.txt
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1HfvIX7FNDi4lzXY-B4fu2gQT8Pn2Vk0R' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1HfvIX7FNDi4lzXY-B4fu2gQT8Pn2Vk0R" -O models.tar.gz && rm -rf /tmp/cookies.txt
sudo tar -zxvf base.tar.gz -C /gpsr/
sudo tar -zxvf data.tar.gz -C /gpsr/base
sudo tar -zxvf models.tar.gz -C /gpsr/base
sudo chown -R $USER /gpsr/*
sudo chgrp -R $USER /gpsr/*
sudo crontab /gpsr/cronjobs
sudo cat /gpsr/gpsr_env.sh >> ~/.bashrc
sudo cp /usr/bin/perl /gpsr/local/bin/
wget --no-check-certificate "https://docs.google.com/uc?export=download&id=0B4QOadCkpLvxQ1RsTjQ2QnB3ODg" -O Modifications.rar 
unrar x Modifications.rar 
sudo chmod -R 777 /home/$USER/PanRV/Modifications 
sudo chown -R $USER /home/$USER/PanRV/Modifications/*
sudo cp Modifications/abcpred/abcpred.pl /gpsr/standalone/abcpred/
sudo cp Modifications/propred/propred.pl /gpsr/standalone/propred/
sudo cp Modifications/propred1/propred1.pl /gpsr/standalone/propred1/
echo "***************** | OSDDLinux Installed | **********************"

echo "------------------------------------------------------------------------"
echo "STEP 7: Press Enter to download databases"
read xyz
echo "***************** |Database downloading Start| **********************"
cd /home/$USER/PanRV/
sudo mkdir DBs
cd DBs
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/ANNOT.rar/download" -O ANNOT.rar
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/CARD.rar/download" -O CARD.rar 
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/COG.rar/download" -O COG.rar 
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/DEG.rar/download" -O DEG.rar 
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/HSDB.rar/download" -O HSDB.rar 
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/GFDB.rar/download" -O GFDB.rar 
sudo wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRVPrerequisits/DBs/VIRF.rar/download" -O VIRF.rar  
sudo unrar x ANNOT.rar
sudo unrar x CARD.rar
sudo unrar x COG.rar
sudo unrar x DEG.rar
sudo unrar x HSDB.rar
sudo unrar x GFDB.rar
sudo unrar x VIRF.rar
sudo chmod -R 777 /home/$USER/PanRV/DBs
sudo chown -R $USER /home/$USER/PanRV/DBs/*
echo "***************** | Databases downloaded successfully | **********************"
cd ..
echo "------------------------------------------------------------------------"
echo "STEP 8: Press Enter to download PanRV executable"
read xyz
echo "***************** |PanRV executable downloading Start| **********************"
wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/PanRV.jar/download" -O PanRV.jar
sudo chmod -R 777 /home/$USER/PanRV/PanRV.jar
sudo chown -R $USER /home/$USER/PanRV/PanRV.jar
echo "***************** | PanRV executable downloaded successfully | **********************"

echo "------------------------------------------------------------------------"
echo "STEP 9: Press Enter to download Configuration files"
read xyz
echo "***************** |Configuration files downloading Start| **********************"
wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/Configs.rar/download" -O Configs.rar
unrar x Configs.rar
sudo chmod -R 777 /home/$USER/PanRV/Configs
sudo chown -R $USER /home/$USER/PanRV/Configs/*
echo "***************** | PanRV executable downloaded successfully | **********************"

echo "------------------------------------------------------------------------"
echo "STEP 10: Press Enter to download Library files"
read xyz
echo "***************** |Library files downloading Start| **********************"
wget --no-check-certificate "https://sourceforge.net/projects/panrv2/files/lib.rar/download" -O lib.rar
unrar x lib.rar
sudo chmod -R 777 /home/$USER/PanRV/lib
sudo chown -R $USER /home/$USER/PanRV/lib/*
echo "***************** | PanRV Installed successfully | **********************"
sudo chmod -R 777 /home/$USER/PanRV/
sudo chown -R $USER /home/$USER/PanRV/*
echo "------------------------------------------------------------------------"
echo "------------------------------------------------------------------------"
echo "------------------------------------------------------------------------"
echo "---------------------Post Installation Steps-----------------------------"

echo "navigate to PanRV/Configs directory and make changes in files
----- 


(config. properties & log4j.properties) by replacing “USER” with your system’s user name.

--Recheck all paths added in .Bashr

--Check owner & executable permissions of following directories

1. /home/USER/PanRV

2. /usr/local/psort

3. /gpsr


Runing PanRV:
First run following 2 commands each time system is restarted
sudo chmod -R 777 /var/run/docker.sock
sudo chown -R $USER /var/run/docker.sock

and the to run program run

java -jar PanRV.jar"



