#!/usr/bin/perl

use strict;
use warnings;


###########################################################
#
# The script runs AssemblyAlign.jar
#
###########################################################

my $RECALCULATE = 0; # recalculate results if 1

my $CASP = "CASP13";
#my $MODELS_DIR  = "/local/$CASP/predictions/cleaned/";
#my $MODELS_DIR  = "/local/$CASP/predictions/cleaned/HETEROoligo_ALL_subunits_reqrd/";
my $MODELS_DIR  = "/local/$CASP/predictions/cleaned/HETEROoligo_1chain_enough/";
#my $MODELS_DIR  = "/local/CASP13/predictions/cleaned/CAPRI37/split/";

my $TARGETS_DIR = "/local/$CASP/Targets_oligo/";
#my $RESULTS_DIR = "/local/$CASP/RESULTS/AA/"; 
#my $RESULTS_DIR = "/local/$CASP/RESULTS/AA/HETEROoligo_ALL_subunits_reqrd/"; 
my $RESULTS_DIR = "/local/$CASP/RESULTS/AA/HETEROoligo_1chain_enough/";
#my $RESULTS_DIR = "/local/$CASP/RESULTS/AA/CAPRI37/split/";
my $TMP_DIR = "/tmp/AA_.$$/";


my $program = "java -jar /home/bohdan/SoftWare/AssemblyProperties/aa.jar ";

my @TARGETS = ();

if ( defined($ARGV[0]) ){
   push @TARGETS, $ARGV[0];
} else {
   @TARGETS = &getTargets();
}


foreach my $target (@TARGETS) {
  #print $target."\n"; next;  
  if (! -f "$TARGETS_DIR/$target.pdb" ){
	next;
  }
  my @models = &getModels($target);
  if (! -d "$MODELS_DIR/$target" ) {
     warn("Directory $MODELS_DIR/$target doesn't exist");
     next;
  } 
  #print "$target\n"; next;
  my $resFile = sprintf("%s/%s.aa", $RESULTS_DIR, $target);
  if (-f "$resFile"){
     if ($RECALCULATE == 0) {
	next;
     } else {	
	system("rm -rf $resFile");
     }
  }
  print $target."\n";  # next;
  system("mkdir -p $TMP_DIR/$target");
  foreach my $model (@models) {
    #print "$model\n";
    my $model_path = sprintf("%s/%s/%s", $MODELS_DIR, $target, $model);
#:TODO correct the paramters of the program
    my $command = sprintf("%s -t %s/%s.pdb -q %s/%s/%s -o %s/%s/tmp.%s.aa  2>/dev/null ", $program, $TARGETS_DIR, $target, $MODELS_DIR, $target, $model, $TMP_DIR, $target, $model);
    if (system("$command") != 0){
	print STDERR "PROBLEM: $model\n";
    }
  }
  &gatherResults($target);
  sleep(1);
}
system("rm -rf $TMP_DIR");

#----------------------------
# SUBROUTINES
#----------------------------

sub getModels {
    my $t = shift;
    my @models = ();
    if (! -d "$MODELS_DIR/$t") {return @models;}
    if (opendir D, "$MODELS_DIR/$t"){
	while(defined(my $m = readdir(D))){
		unless ($m =~ m/^T/){next;}
		push @models, $m;
	}
        closedir D; 
        @models = sort @models;
    }
    return @models;
}

# getTargets
# the method parses the models dir
sub getTargets {
    my @targets;
    if (! -d "$MODELS_DIR") {return @targets;}
    if (opendir D, "$MODELS_DIR"){
        while(defined(my $t = readdir(D))){
                unless ($t =~ m/^T[0-9Racspx]\S+o$/ || $t =~ m/^T[0-9Racspx]\S+-T[0-9Racspx]\S+$/){next;}
		unless (-d "$MODELS_DIR/$t" ){next;}
                push @targets, $t;
        }
        closedir D;
        @targets = sort @targets;
    }
    return @targets;
}

sub usage {
   my $text = shift;
   print "\nUSAGE: $0 <TARGET>\n\n";
   if (defined($text)){
	warn($text);
   }
   exit;
}

sub gatherResults {
    my $target = shift;
    system("mkdir -p $RESULTS_DIR");
    my $resFile = sprintf("%s/%s.aa", $RESULTS_DIR, $target);
    open R, "> $resFile";
    my $header = 0;
    opendir D, "$TMP_DIR/$target/";
    my @files = sort readdir(D); 
    while (my $f = shift @files) {
	if ( $f =~ m/tmp\.(\S+)\.aa/ ) {
		my $model = $1;
		open F, "< $TMP_DIR/$target/$f";
		my $l = <F>; chomp $l;
		if ($header == 0) {
			print R "$l\n";
			$header = 1;
		}
		$l = <F>; chomp $l;
		$l =~ s/\S+\///g;
		if ($l =~ m/^$/){next;}
		print R "$l\n";
		close F;
	}
    }
    closedir D;
    close R;
}


=head
sub uploadResultFile{
    my ($fullPathResFile) = shift;
    my $command = sprintf("rsync -az -e ssh $fullPathResFile  $SERVER:/$fullPathResFile ");
    system("$command");
#    print("$command\n");

}


sub rsyncModels{
    my ($fullPathModelDir) = @_;
    my $command = sprintf("rsync -az -e ssh $SERVER:/%s/ /%s/ --delete ", $fullPathModelDir, $fullPathModelDir);
    system("$command");
#    print("$command\n");
}

sub rsyncTargets{
    my ($fullPathTargetDir) = @_;
    my $command = sprintf("rsync -az -e ssh $SERVER:/%s/ /%s/ --delete", $fullPathTargetDir, $fullPathTargetDir);
    system("$command");
    $command = sprintf("rsync -az -e ssh $SERVER:/%s/templates/ /%s/templates/ --delete", $fullPathTargetDir, $fullPathTargetDir);
    system("$command");
    #print("$command\n");
}


sub parseTargetsDir {
    opendir D, $TARGETS_DIR;
    while(defined(my $f = readdir(D))){
        next unless $f =~ m/^(T|R).*.pdb$/;
        # skip targets -D1 that have been copied from -D0
        if (($f =~ m/-D1\.pdb/) && (-l "$TARGETS_DIR/$f")) {next;}
        # skip combined domains -D12  
        if ($f =~ m/-D[1-9]{2}/) {next;}
        # skip test domain
        if ($f =~ m/-D[7-9]{1}/) {next;} # skip test domains
        $f =~ s/\.pdb$//;
        if (&checkIfResultsExist($f)){
                push @TARGETS, $f;
        }
    }
    closedir D;
}

sub checkIfResultsExist {
    my $target = shift;
    my $targetFile = sprintf("%s/%s.pdb", $TARGETS_DIR, $target);
    my $resultDir = sprintf("%s/%s/", $RESULTS_DIR, $target);
    if (! -e $resultDir) {
        return 1;
    }
    # check if target file has been modified AFTER result dir has been created
    if ((stat($targetFile))[9] > (stat($resultDir))[9]){
        return 1;
    }
    return 0;
}
=cut
