#!/usr/bin/perl
use strict;
#use Env;
BEGIN{ @INC = ( '/bmlfast/bml_casp15/tools/casp14/DeepRank_db_tools/tools/SCRATCH-1D_1.1/perl_lib/', @INC ); }
use Env;

##########################################################################################
#                                                                                        #
#  Project     :  SCRATCH-1D                                                             #
#  Release     :  1.1                                                                    #
#                                                                                        #
#  File        :  install.pl                                                             #
#  Description :  SCRATCH-1D installation script                                         #
#                                                                                        #
#  Author(s)   :  Christophe Magnan (cmagnan@ics.uci.edu)                                #
#  Copyright   :  Institute for Genomics and Bioinformatics                              #
#                 University of California, Irvine                                       #
#                                                                                        #
#  Modified    :  2015/07/02                                                             #
#                                                                                        #
##########################################################################################

# Releases included in this package
my $blast_release="blast-2.2.26";
my $brnn1d_release="1D-BRNN_3.3";
my $profilpro_release="PROFILpro_1.1";
my $homolpro_release="HOMOLpro_1.1";
my $sspro_release="SSpro_5.2";
my $sspro8_release="SSpro8_5.2";
my $accpro_release="ACCpro_5.2";
my $accpro20_release="ACCpro20_5.2";

# Retrieving installation path
my $install_dir=`pwd`; chomp($install_dir);
my $locerr="\nThis script must run from its own directory.\n";
if((! -d "$install_dir/bin")||(! -d "$install_dir/doc")){ print "$locerr\n"; exit 1; }
if((! -d "$install_dir/env")||(! -d "$install_dir/lib")){ print "$locerr\n"; exit 1; }
if((! -d "$install_dir/pkg")||(! -d "$install_dir/tmp")){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/check_project_profile.pl"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/SCRATCH-1D_predictions.pl"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/SCRATCH-1D_predictions.sh"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/install_template1.sh"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/install_template2.sh"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/install_template3.sh"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$blast_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$brnn1d_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$profilpro_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$homolpro_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$sspro_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$sspro8_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$accpro_release"){ print "$locerr\n"; exit 1; }
if(! -d "$install_dir/pkg/$accpro20_release"){ print "$locerr\n"; exit 1; }
print "\nInstalling SCRATCH-1D release 1.1...\nPath: $install_dir\n";

# Checking Blast Installation
my @d=split("-",$blast_release); print "Checking blast (release $d[1]) installation...\n";
my $blast_install_dir="$install_dir/pkg/$blast_release";
if(! -f "$blast_install_dir/bin/blastall"){ print "blastall binary not found\n"; exit 1; }
if(! -f "$blast_install_dir/bin/blastpgp"){ print "blastpgp binary not found\n"; exit 1; }

# Installing 1D-BRNN Software
@d=split("_",$brnn1d_release); print "Installing 1D-BRNN (release $d[1])...\n";
my $brnn1d_install_dir="$install_dir/pkg/$brnn1d_release";
if(! -f "$brnn1d_install_dir/src/makefile"){ print "1D-BRNN makefile not found\n"; exit 1; }
`cd $brnn1d_install_dir/src; #chmod 777 *; make; make clean; #chmod 777 ../bin/*`;
if(! -f "$brnn1d_install_dir/bin/predict_multi"){ print "1D-BRNN installation failed\n";
print "Please install a recent gcc compiler.\n"; exit 1; }

# Installing PROFILpro Software
@d=split("_",$profilpro_release);
print "Installing PROFILpro (release $d[1])...\n";
my $profilpro_install_dir="$install_dir/pkg/$profilpro_release";
if(! -f "$profilpro_install_dir/install.pl"){
print "PROFILpro installer not found\n"; exit 1; }
`cd $profilpro_install_dir; ./install.pl $install_dir/pkg $blast_install_dir`;
if(! -f "$profilpro_install_dir/bin/generate_profiles.sh"){
print "PROFILpro installation failed\n"; exit 1; }
if(! -f "$profilpro_install_dir/env/PROFILpro.sh"){
print "PROFILpro installation failed\n"; exit 1; }

# Installing HOMOLpro Software
@d=split("_",$homolpro_release);
print "Installing HOMOLpro (release $d[1])...\n";
my $homolpro_install_dir="$install_dir/pkg/$homolpro_release";
if(! -f "$homolpro_install_dir/install.pl"){
print "HOMOLpro installer not found\n"; exit 1; }
`cd $homolpro_install_dir; ./install.pl $install_dir/pkg $blast_install_dir`;
if(! -f "$homolpro_install_dir/bin/add_homology_predictions.sh"){
print "HOMOLpro installation failed\n"; exit 1; }
if(! -f "$homolpro_install_dir/env/HOMOLpro.sh"){
print "HOMOLpro installation failed\n"; exit 1; }

# Installing SSpro Software
my $dependencies="$profilpro_install_dir $homolpro_install_dir $brnn1d_install_dir";
@d=split("_",$sspro_release); print "Installing SSpro (release $d[1])...\n";
my $sspro_install_dir="$install_dir/pkg/$sspro_release";
if(! -f "$sspro_install_dir/install.pl"){ print "SSpro installer not found\n"; exit 1; }
`cd $sspro_install_dir; ./install.pl $dependencies`; my $sspro_error="SSpro installation failed";
if(! -f "$sspro_install_dir/env/SSpro.sh"){ print "$sspro_error\n"; exit 1; }
if(! -f "$sspro_install_dir/bin/sequence_to_ss.sh"){ print "$sspro_error\n"; exit 1; }
if(! -f "$sspro_install_dir/bin/sequence_to_ss_ab.sh"){ print "$sspro_error\n"; exit 1; }
if(! -f "$sspro_install_dir/bin/profiles_to_ss.sh"){ print "$sspro_error\n"; exit 1; }
if(! -f "$sspro_install_dir/bin/profiles_to_ss_ab.sh"){ print "$sspro_error\n"; exit 1; }
if(! -f "$sspro_install_dir/data/models.txt"){ print "$sspro_error\n"; exit 1; }

# Installing SSpro8 Software
@d=split("_",$sspro8_release); print "Installing SSpro8 (release $d[1])...\n";
my $sspro8_install_dir="$install_dir/pkg/$sspro8_release";
if(! -f "$sspro8_install_dir/install.pl"){ print "SSpro8 installer not found\n"; exit 1; }
`cd $sspro8_install_dir; ./install.pl $dependencies`; my $sspro8_error="SSpro8 installation failed";
if(! -f "$sspro8_install_dir/env/SSpro8.sh"){ print "$sspro8_error\n"; exit 1; }
if(! -f "$sspro8_install_dir/bin/sequence_to_ss8.sh"){ print "$sspro8_error\n"; exit 1; }
if(! -f "$sspro8_install_dir/bin/sequence_to_ss8_ab.sh"){ print "$sspro8_error\n"; exit 1; }
if(! -f "$sspro8_install_dir/bin/profiles_to_ss8.sh"){ print "$sspro8_error\n"; exit 1; }
if(! -f "$sspro8_install_dir/bin/profiles_to_ss8_ab.sh"){ print "$sspro8_error\n"; exit 1; }
if(! -f "$sspro8_install_dir/data/models.txt"){ print "$sspro8_error\n"; exit 1; }

# Installing ACCpro Software
@d=split("_",$accpro_release); print "Installing ACCpro (release $d[1])...\n";
my $accpro_install_dir="$install_dir/pkg/$accpro_release";
if(! -f "$accpro_install_dir/install.pl"){ print "ACCpro installer not found\n"; exit 1; }
`cd $accpro_install_dir; ./install.pl $dependencies`; my $accpro_error="ACCpro installation failed";
if(! -f "$accpro_install_dir/env/ACCpro.sh"){ print "$accpro_error\n"; exit 1; }
if(! -f "$accpro_install_dir/bin/sequence_to_acc.sh"){ print "$accpro_error\n"; exit 1; }
if(! -f "$accpro_install_dir/bin/sequence_to_acc_ab.sh"){ print "$accpro_error\n"; exit 1; }
if(! -f "$accpro_install_dir/bin/profiles_to_acc.sh"){ print "$accpro_error\n"; exit 1; }
if(! -f "$accpro_install_dir/bin/profiles_to_acc_ab.sh"){ print "$accpro_error\n"; exit 1; }
if(! -f "$accpro_install_dir/data/models.txt"){ print "$accpro_error\n"; exit 1; }

# Installing ACCpro20 Software
@d=split("_",$accpro20_release); print "Installing ACCpro20 (release $d[1])...\n";
my $accpro20_install_dir="$install_dir/pkg/$accpro20_release";
if(! -f "$accpro20_install_dir/install.pl"){ print "ACCpro20 installer not found\n"; exit 1; }
`cd $accpro20_install_dir; ./install.pl $dependencies`;
my $accpro20_error="ACCpro20 installation failed";
if(! -f "$accpro20_install_dir/env/ACCpro20.sh"){ print "$accpro20_error\n"; exit 1; }
if(! -f "$accpro20_install_dir/bin/sequence_to_acc20.sh"){ print "$accpro20_error\n"; exit 1; }
if(! -f "$accpro20_install_dir/bin/sequence_to_acc20_ab.sh"){ print "$accpro20_error\n"; exit 1; }
if(! -f "$accpro20_install_dir/bin/profiles_to_acc20.sh"){ print "$accpro20_error\n"; exit 1; }
if(! -f "$accpro20_install_dir/bin/profiles_to_acc20_ab.sh"){ print "$accpro20_error\n"; exit 1; }
if(! -f "$accpro20_install_dir/data/models.txt"){ print "$accpro20_error\n"; exit 1; }

# Preparing SCRATCH-1D bash profile
print "Installing SCRATCH-1D bash profile...\n"; open(IN,"$install_dir/lib/install_template1.sh");
open(OUT,">$install_dir/env/SCRATCH-1D.sh"); while(my $l=<IN>){ chomp($l);
if($l eq "<INSTALL_DIR>"){ print OUT "export SCRATCH1D_ROOT_DIR=\"$install_dir\"\n"; }
elsif($l eq "<PROFILpro_DIR>"){ print OUT "export PROFILpro_INSTALL_DIR=\"$profilpro_install_dir\"\n"; }
elsif($l eq "<SSpro_DIR>"){ print OUT "export SSpro_INSTALL_DIR=\"$sspro_install_dir\"\n"; }
elsif($l eq "<SSpro8_DIR>"){ print OUT "export SSpro8_INSTALL_DIR=\"$sspro8_install_dir\"\n"; }
elsif($l eq "<ACCpro_DIR>"){ print OUT "export ACCpro_INSTALL_DIR=\"$accpro_install_dir\"\n"; }
elsif($l eq "<ACCpro20_DIR>"){ print OUT "export ACCpro20_INSTALL_DIR=\"$accpro20_install_dir\"\n"; }
elsif($l eq "<HOMOLpro_DIR>"){ print OUT "export HOMOLpro_INSTALL_DIR=\"$homolpro_install_dir\"\n"; }
else{ print OUT "$l\n"; } } close(IN); close(OUT); #`chmod 777 $install_dir/env/SCRATCH-1D.sh`;

# Preparing SCRATCH-1D launchers
print "Installing SCRATCH-1D bin scripts...\n"; open(IN,"$install_dir/lib/install_template2.sh");
open(OUT,">$install_dir/bin/run_SCRATCH-1D_predictors.sh"); while(my $l=<IN>){ chomp($l);
if($l eq "<INSTALL_DIR>"){ print OUT "install_dir=\"$install_dir\"\n"; } else{ print OUT "$l\n"; } }
close(IN); close(OUT); #`chmod 777 $install_dir/bin/run_SCRATCH-1D_predictors.sh`;
open(OUT,">$install_dir/bin/get_abinitio_predictions.sh");
open(IN,"$install_dir/lib/install_template3.sh"); while(my $l=<IN>){ chomp($l);
if($l eq "<INSTALL_DIR>"){ print OUT "install_dir=\"$install_dir\"\n"; } else{ print OUT "$l\n"; } }
close(IN); close(OUT); #`chmod 777 $install_dir/bin/get_abinitio_predictions.sh`;

# Print message to test installation
print "\nSCRATCH-1D successfully installed...\n\nTo test the installation:\n\n";
print "Change directory to the 'doc' sub-folder\n\n   cd doc\n\n";
print "Run SCRATCH-1D on the provided test datasets:\n\n";
print "   ../bin/run_SCRATCH-1D_predictors.sh test.fasta test.out 4\n\n";
print "The output files 'test.out.*' should be identical to 'test.hom.*'\n\n";

