#!/usr/bin/perl
use strict;
use warnings;

use Digest::MD5 qw(md5 md5_hex md5_base64);
use MIME::Parser;
use MIME::Entity;
use MIME::Body;
use Getopt::Long;

use lib qw(Core);
use lib qw(Classes);

use LocalConfiguration;

use StatusManager;
use TargetsManager;
use Logger;
use PredictionsManager;
use GroupsManager;
use SplitByParent;

my $SCRIPTNAME = 'copy_4eval.pl';

#my $logger = new Logger();

my $targets_manager = new TargetsManager();
my $status_manager = new StatusManager();
my $predictions_manager = new PredictionsManager();
my $groups_manager = new GroupsManager();


my @group_list = $status_manager->info_all_group_list();
my @target_list = $status_manager->info_public_target_list_all();


# create hash of codes of server groups
# it is used for filtering human groups for server targets
my %server_codes;
foreach my $group (@group_list){
	if($groups_manager->is_server( $group->{ID})){
		$server_codes{int($group->{CODE})} = 1;
#		print int($group->{CODE})."\n";
	}
}

my $datestring = localtime();
print "-----$datestring\n"; 

# copy predictions for targets expired for human predictions
my $new_dir;
for(my $t_count = 0; $t_count < scalar(@target_list); $t_count++) {
    if($targets_manager->is_expired($target_list[$t_count]->{ID}, "human") && !($targets_manager->is_canceled($target_list[$t_count]->{ID}) == 1) ) {
        my $target = $target_list[$t_count]->{NAME};
	print "$target\n"; 
        # check if target is in predictions/closed_targets.list: if not, add it to the list
        my $isAppended = `grep \'$target\' predictions/closed_targets.list -c`;
        my $isAppendedH = `grep \'$target\' predictions/QS_closed_targets.list -c`;

        if($isAppended == 0){
                # append expired target to predictions/closed_targets_server.list
                system(sprintf("echo %s >> predictions/closed_targets.list",$target));
        }
        if($isAppendedH == 0 and $target =~ /^H/){
                # append expired target to predictions/closed_targets_server.list
                system(sprintf("echo %s >> predictions/QS_closed_targets.list",$target));
        }

	#check if destination dir exists
        if ($target =~ /^H/) { #Copy Heteromers to a different dir
		$new_dir = sprintf("%s/%s",$LOCAL_CONFIG->{DATA_QS_MODELS_DIR},$target);		
	} else {
		$new_dir = sprintf("%s/%s",$LOCAL_CONFIG->{DATA_MODELS_DIR},$target);		 }

	if(!(-e $new_dir)){
	  system(sprintf("mkdir %s",$new_dir));
          system(sprintf("chgrp users %s", $new_dir));
          system(sprintf("chmod 775 %s", $new_dir));
	
	  #TODO if target was canceled for humans only  and/or target is server only then select predictions from server groups only
	  my %info = $targets_manager->info($target_list[$t_count]->{ID});
#	  print $target_list[$t_count]->{NAME}." ".$info{IS_SERVER_ONLY}."\n";
	  if($info{IS_SERVER_ONLY} == 1 || $targets_manager->is_canceled($target_list[$t_count]->{ID}) == 2){
		#create list of codes servers
		my $LS_EXP = sprintf("/bin/ls -1 %s/%s/%sTS*_?", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target, $target) ;
		open IN, " $LS_EXP |";
		while(defined(my $f = <IN>)){
		   chomp $f;
		   $f = substr($f, rindex($f, "/") + 1);
		   my $code;
		   if($f =~ m/TS([0-9]{3})_([1-5])/){
			$code = int($1);
		   }
		   if(defined($code) && $code ne ''){
			if(defined $server_codes{$code}){
				# remove HETATM record
				my $command = sprintf("grep  -v HETATM  %s/%s/$f >  %s/%s/$f ", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target, $LOCAL_CONFIG->{DATA_MODELS_DIR},$target);
				system ("$command");
			}
		   }
		}
		close(IN);
	  }else{
	  # copy TS models
		my $REG_EXPR = sprintf("%sTS*_?",$target);# regexpr for TS predictions
		my $dirreg = $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}."/".$target;
		# remove HETATM records
		system(sprintf("for f in  %s/%s; do grep -v HETATM \$f > %s/\${f##*/} ; done", $dirreg, $REG_EXPR, $new_dir));

  		my $targeto = $target."o";
		my $diro = $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}."/".$targeto;
		if (-e $diro) {		
        	    my $isAppendedo = `grep \'$targeto\' predictions/QS_closed_targets.list -c`;
           	    if($isAppendedo == 0){
                # append expired target to predictions/closed_targets_server.list
	               	system(sprintf("echo %s >> predictions/QS_closed_targets.list",$targeto));
        	    }	
		    my $diro_out = $LOCAL_CONFIG->{DATA_QS_MODELS_DIR}."/".$targeto;
		    if (!(-e $diro_out)) {		
  		       mkdir $diro_out or print "Error creating directory $diro_out\n";
		       my $REG_EXPR2 = sprintf("%sTS*_?o",$target);# regexpr for homomultimeric predictions
		       system(sprintf("for f in  %s/%s; do grep -v HETATM \$f > %s/\${f##*/} ; done", $diro, $REG_EXPR2, $diro_out));
		       system(sprintf("for f in %s/* ; do chgrp users \$f ;  chmod 664 \$f ; done", $diro_out));
		    }
		}		
	  }

	  # change permissions in dir
	system(sprintf("for f in %s/* ; do chgrp users \$f ;  chmod 664 \$f ; done", $new_dir));
	} # end check dir existence

	#copy RR models
	$new_dir = sprintf("%s/%s",$LOCAL_CONFIG->{DATA_RR_MODELS_DIR},$target);
	my $REG_EXPR = sprintf("%sRR*_?",$target);# regexpr for TS predictions
        if(!(-e $new_dir) && $target =~ m/^[TR][0-9]{4}/){
                system(sprintf("mkdir %s",$new_dir));
                system(sprintf("chgrp users %s", $new_dir));
                system(sprintf("chmod 775 %s", $new_dir));
          my %info = $targets_manager->info($target_list[$t_count]->{ID});
#	  print $target." ".$info{IS_SERVER_ONLY}."\n";
          if($info{IS_SERVER_ONLY} == 1 || $targets_manager->is_canceled($target_list[$t_count]->{ID}) == 2){
                #create list of codes servers
                my $LS_EXP = sprintf("/bin/ls -1 %s/%s/%sRR*_?", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target, $target) ;
                open IN, " $LS_EXP |";
                while(<IN>){
                   chomp $_;
                   my $code;
                   if($_ =~ m/RR([0-9]{3})_([1-5])/){
                        $code = int($1);
                   }
                   if(defined($code) && $code ne ''){
                        if(defined $server_codes{$code}){
                                my $command = sprintf("cp -p $_ %s/%s", $LOCAL_CONFIG->{DATA_RR_MODELS_DIR},$target);
                                system ("$command ");
                        }
                   }
                }
                close(IN);

          }else{
                system(sprintf("for f in  %s/%s/%s; do cp -p \$f %s/%s ; done", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR},$target,$REG_EXPR,$LOCAL_CONFIG->{DATA_RR_MODELS_DIR},$target));
	  }
        }
  }
}
# make links for models in /local/CASP13/MODELS_PDB
system("/local/Projects/Perl/casp13/src/scripts/linkModels.pl");

=head
# copy predictions for targets expired for server predictions
for(my $t_count = 0; $t_count < scalar(@target_list); $t_count++) {
    if($targets_manager->is_expired($target_list[$t_count]->{ID}, "server") && !($targets_manager->is_canceled($target_list[$t_count]->{ID}) == 1) ) {
	
	# check if target is in predictions/closed_targets_server.list: if not, add it to the list
        my $isAppended = `grep \'$target_list[$t_count]->{NAME}\' predictions/closed_targets_server.list -c`;
        if($isAppended == 0){
                # append expired target to predictions/closed_targets_server.list
                system(sprintf("echo %s >> predictions/closed_targets_server.list",$target_list[$t_count]->{NAME}));
        }

	
	if($target_list[$t_count]->{NAME} =~ m/^[TR][0-9]{4}/){ # check if target is regular R0123
		# len_target - length of target sequence
		my $len_target = $targets_manager->info_per_target($target_list[$t_count]->{ID},1)->{NUMBER_OF_AA};

		# get all server predictions for the target
		my @predictions = $predictions_manager->tarballs_predictions($target_list[$t_count]->{NAME}, "server");
		my $old_dir = sprintf("%s/%s", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target_list[$t_count]->{NAME});
		my $new_dir = sprintf("%s/%s", $LOCAL_CONFIG->{DATA_SERV_MODELS_DIR}, $target_list[$t_count]->{NAME});
		if(!(-e $new_dir)){
			system(sprintf("mkdir %s",$new_dir));
			system(sprintf("chgrp users %s", $new_dir));
			system(sprintf("chmod 775 %s", $new_dir));

			for(my $p_count = 0; $p_count < scalar(@predictions); $p_count++) {

				
				my $old_file = sprintf("%s/%s%s%s_%s", $old_dir, $predictions[$p_count]->{TARGET}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{GROUP_CODE}, $predictions[$p_count]->{MODEL});
				# check of file existance
				if(! -f $old_file){
					warn "file $old_file doesn't exist but the corresponding prediction is in database. Check directory ./prediction/junk/";
				} else{
				# check length requirement: the model has to cover at least 75% of the target's sequence
				my $len_model = `grep \'ATOM\' $old_file | grep -c \' CA\'`; chomp $len_model;
				if($len_model > 0.75 * $len_target){

				my $new_file = sprintf("%s/%s%s%s_%s", $new_dir, $predictions[$p_count]->{TARGET}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{GROUP_CODE}, $predictions[$p_count]->{MODEL});
				system("ln $old_file $new_file");
				}

				}
			}
		}

	}
    }
}
=cut
