#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Cwd 'abs_path';

my $libdir    = "";
my $structure = "true";
my $function  = "true";
my $sequence  = "true";


GetOptions('libdir:s' => \$libdir, 'B:s' => \$function, 'P:s' => \$structure, 'N:s' => \$sequence);


my $fname=""; # in case user provides a file name
($libdir, $fname)=&get_absolute_path($libdir);


if(!$libdir)
{
    print "\nPlease provide a valid path for saving the I-TASSER library\n\n";
    print "Usage:\n";
    print "./download_lib.pl -libdir libdir -P true -B true -N true\n";    
    printf "====================\n";
    printf "Mandatory arguments:\n";
    printf "====================\n";
    printf "-libdir template_library_directory (full path for saving the library files, such as /home/yangji/ITLIB)\n\n";

    printf "===================\n";
    printf "Optional arguments:\n";
    printf "===================\n";
    printf "-P  [true or false], whether to download PDB template files (default: true)\n";
    printf "    You can set it as false if you already have modeled structure and just need function predictions\n";
    printf "-B  [true or false], whether to download BioLiP function library files (default: true)\n";
    printf "    You can set it as false if you do not need function predictions\n";
    printf "-N  [true or false], whether to download the non-redundant sequence database nr (default: true)\n";
    printf "    You can set it as false if you want to use your own nr database. (In this case, you will have to go to 
    the I-TASSER library directory, and make a soft link with the command: ln -s location_of_your_nr nr)\n\n";

    &get_library_size();
    exit();
}

&get_library_size();




printf "\nYour settings for the I-TASSER library are:\n\n";
printf "-libdir   = $libdir\n";
printf "-P        = $structure\n";
printf "-B        = $function\n";
printf "-N        = $sequence\n\n";

my @folders=qw(
	       PDB
	       BPOCKET
	       PSSM
	       CNT
	       DEP
	       dotProfiles
	       Enzyme
	       GO
	       receptor
	       ligand
	       map
	       MTX
	       nr
	       SIG
	       stride
	       summary
	       );


if(!-d "$libdir")
{
    system("mkdir -p $libdir") == 0 or die "System call failed: $!";
}

if(!-d "$libdir")
{
    print "Failed to create $libdir. Please check problem of your system\n";
    exit;
}

chdir $libdir;
foreach my $f(@folders)
{
    system("mkdir -p $f") == 0 or die "System call failed: $!";
}


##NR
if(lc($sequence) eq "true")
{
    my $nfiles=`ls $libdir/nr/ |wc -l`;
    chomp($nfiles);

    if($nfiles<30) #avoid re-download
    {
	print "Downloading NR database...\n";
	#system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/cgi-bin/download_ftp.cgi?ID=nr.tar.gz") == 0 or die "System call failed: $!";  
	system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/nr.tar.gz") == 0 or die "System call failed: $!";  
	system("tar -xzvf nr.tar.gz")== 0 or die "System call failed: $!";
	system("rm -f nr.tar.gz") == 0 or die "System call failed: $!";
	system("rm -f log") == 0 or die "System call failed: $!";
    }
    else
    {
	print "NR database is already present. Skipping nr download ..\n";  
    }
}

print "Download PDB structure files...\n";
system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/PDB.tar.bz2") == 0 or die "System call failed: $!";
system("tar -xvf PDB.tar.bz2>log") == 0 or die "System call failed: $!";
system("rm -f PDB.tar.bz2") == 0 or die "System call failed: $!";
system("rm -f log") == 0 or die "System call failed: $!";

if(lc($structure) eq "true")
{
    print "Downloading template profiles...\n";
    chdir $libdir;
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/MTX.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf MTX.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f MTX.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/DEP.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf DEP.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f DEP.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/SIG.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf SIG.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f SIG.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/CNT.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf CNT.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f CNT.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/dotProfiles.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf dotProfiles.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f dotProfiles.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/stride.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf stride.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f stride.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/summary1.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf summary1.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f summary1.tar.bz2") == 0 or die "System call failed: $!";
    
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/summary2.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf summary2.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f summary2.tar.bz2") == 0 or die "System call failed: $!";

    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/library/Bfactor.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf Bfactor.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f Bfactor.tar.bz2") == 0 or die "System call failed: $!";
    system("rm -f log") == 0 or die "System call failed: $!";
}

if(lc($function) eq "true")
{
    print "Downloading BioLiP function library files...\n";
    chdir "$libdir/BPOCKET";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/BPOCKET/BSITES.bsr") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/BPOCKET/BSITES.center") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/BPOCKET/BSITES.fasta") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/BPOCKET/pocket.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf pocket.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f pocket.tar.bz2") == 0 or die "System call failed: $!";
    system("rm -f log") == 0 or die "System call failed: $!";
    
    chdir "$libdir/PSSM";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/PSSM/BSITES.bsr") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/PSSM/BSITES.fpt") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/PSSM/BSITES.fasta") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/PSSM/PSSM.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf PSSM.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f PSSM.tar.bz2") == 0 or die "System call failed: $!";
    system("rm -f log") == 0 or die "System call failed: $!";

    chdir "$libdir";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/COFACTOR/map.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf map.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f map.tar.bz2") == 0 or die "System call failed: $!";  
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/COFACTOR/receptor.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf receptor.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f receptor.tar.bz2") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/COFACTOR/ligand.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf ligand.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f ligand.tar.bz2") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/COFACTOR/Enzyme.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf Enzyme.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f Enzyme.tar.bz2") == 0 or die "System call failed: $!";
    system("wget -o log -c http://zhanglab.dcmb.med.umich.edu/BioLiP/download/COACH_lib/COFACTOR/GO.tar.bz2") == 0 or die "System call failed: $!";
    system("tar -xvf GO.tar.bz2 >log") == 0 or die "System call failed: $!";
    system("rm -f GO.tar.bz2") == 0 or die "System call failed: $!";  
    system("rm -f log") == 0 or die "System call failed: $!";
}
    
print "All libraries were downloaded\n";
print "Please note that if you want to predict Gene Ontology terms, you will need to download and install the GO::Parser manually.\n";
print "You can follow this link: http://zhanglab.dcmb.med.umich.edu/I-TASSER/addition/\n";



sub get_library_size
{ 
    my $total=0;
    my $total1=0;

    system("rm -f index.html") == 0 or die "System call failed: $!";
    system("wget -o log http://zhanglab.dcmb.med.umich.edu/library/") == 0 or die "System call failed: $!";
    my $rst=`cat index.html`;

    my @files=(
	       "PDB",
	       "MTX",
	       "DEP",
	       "SIG",
	       "CNT",
	       "dotProfiles",       	       
	       "Bfactor",	       
	       );


    my %size=(
	      "nr"=>7.3,
	      "PDB"=>4.4, 
	      "MTX"=>1.8, 
	      "DEP"=>2.8, 
	      "SIG"=>1.9, 
	      "CNT"=>0.87, 
	      "dotProfiles"=>0.85, 
	      "Bfactor"=>0.95, 
	      "TM-SITE"=>1.3, 
	      "S-SITE"=>2.7, 
	      "COFACTOR"=>16.1, 
	      "summary"=>2.6, 	      
	      );



    my @keys = keys %size;
    foreach my $k (@keys)
    {
	$total1 += $size{$k};
    }


    printf "\nThe I-TASSER library files to be downloaded include:\n";
    printf "--------------------------------------------\n";
    printf "%-15s     Size          Size\n", "Name";
    printf "%-15s (compressed) (decompressed)\n", "";

    printf "--------------------------------------------\n";
    printf "%-15s %-5.2f GB       %5.2f GB\n", "nr", 4198/1024, $size{"nr"}; 
    $total +=4198; 

    foreach my $f(@files)
    {
	if($rst =~ /$f\.tar\.bz2.+\s+\(~(\d+)MB\)/)
	{
	    printf "%-15s %-5.2f GB       %5.2f GB\n", $f, $1/1024, $size{$f};
	    $total += $1;
	}
    }

    printf "%-15s %-5.2f GB       %5.2f GB\n", "summary", 226/1024, $size{"summary"};
    $total +=226;
    printf "%-15s %-5.2f GB       %5.2f GB\n", "TM-SITE", 1263/1024,$size{"TM-SITE"};
    $total +=1263;
    printf "%-15s %-5.2f GB       %5.2f GB\n", "S-SITE", 417/1024,$size{"S-SITE"};
    $total +=417;
    printf "%-15s %-5.2f GB       %5.2f GB\n", "COFACTOR", 2670/1024, $size{"COFACTOR"};
    $total +=2670;

    printf "%-15s %-5.2f GB       %5.2f GB\n", "others", 1, 6.5;

    $total=11;
    $total1=50;
    printf "\n%-15s~%-5.2f GB      ~%5.2f GB\n", "TOTAL", $total, $total1;
    printf "--------------------------------------------\n";

    system("rm -f index.html log") == 0 or die "System call failed: $!";
}


sub get_absolute_path
{
    my ($path)=@_;
    my $dir = "";
    my $fname= "";
    if($path ne "")
    {
	`mkdir -p $path` if(!-e $path && !-d $path);
	my $apath = abs_path($path); # for '..'	
	if (defined($apath))
	{
	    $dir = $apath; 
	    if(-e $dir && !-d $dir) #it is not an existing directory, should be a file
	    {
		$dir =~ s/(.*)\/.*$/$1/;
		$fname=$apath;
		$fname =~ s/^.*[\/\\]//;
	    }	
	}
    }
    return ($dir, $fname);
}
