#!/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";
	}
}

# copy predictions for targets expired for human predictions
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) ) {
	print $target_list[$t_count]->{NAME}."\n"; 
        # check if target is in predictions/closed_targets.list: if not, add it to the list
        my $isAppended = `grep \'$target_list[$t_count]->{NAME}\' predictions/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_list[$t_count]->{NAME}));
        }

	#check if destination dir exists
	my $new_dir = sprintf("%s/%s",$LOCAL_CONFIG->{DATA_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));
	
	  #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_list[$t_count]->{NAME}, $target_list[$t_count]->{NAME}) ;
		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}){
				# 
				# my $command = sprintf("cp -p $_ %s/%s", $LOCAL_CONFIG->{DATA_MODELS_DIR},$target_list[$t_count]->{NAME});
				# remove HETATM record
				my $command = sprintf("grep  -v HETATM  %s/%s/$f >  %s/%s/$f ", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target_list[$t_count]->{NAME}, $LOCAL_CONFIG->{DATA_MODELS_DIR},$target_list[$t_count]->{NAME});
				system ("$command");
			}
		   }
		}
		close(IN);

	  }else{
	     # copy TS and AL models
	     my $REG_EXPR = sprintf("%sTS*_?",$target_list[$t_count]->{NAME});# regexpr for TS predictions
#             my $REG_EXPR2 = sprintf("%sTS*_?_?",$target_list[$t_count]->{NAME});# regexpr for TS predictions and for additional models
#	     my $REG_EXPR3 = sprintf("%sAL*_?*",$target_list[$t_count]->{NAME});# regexpr for AL predictions  WE DON"T HAVE AL PREDICITONS IN CASPCOMMONS
#		system(sprintf("for f in  %s/%s/%s; do cp -p \$f %s/%s ; done", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR},$target_list[$t_count]->{NAME},$REG_EXPR,$LOCAL_CONFIG->{DATA_MODELS_DIR},$target_list[$t_count]->{NAME}));

# remove HETATM records
		 system(sprintf("for f in  %s/%s/%s; do grep -v HETATM \$f > %s/%s/\${f##*/} ; done", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR},$target_list[$t_count]->{NAME},$REG_EXPR,$LOCAL_CONFIG->{DATA_MODELS_DIR},$target_list[$t_count]->{NAME}));

###		system(sprintf("for f in  %s/%s/%s; do cp -p \$f %s/%s ; done", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR},$target_list[$t_count]->{NAME},$REG_EXPR2,$LOCAL_CONFIG->{DATA_MODELS_DIR},$target_list[$t_count]->{NAME}));
###                system(sprintf("for f in  %s/%s/%s; do cp -p \$f %s/%s ; done", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR},$target_list[$t_count]->{NAME},$REG_EXPR3,$LOCAL_CONFIG->{DATA_MODELS_DIR},$target_list[$t_count]->{NAME}));
	  }
  
          # split by parent
          my $split_manager = new SplitByParent();
          opendir(DIR, $new_dir) or die $!;
          while(my $file = readdir(DIR)){
             next unless ($file =~ m/^[TR][Rracspx0-9][0-9]{3}TS[0-9]{3}_[1-5]$/);
             $split_manager->process("$new_dir/$file");
          }
          close(DIR);

	  # finally 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_list[$t_count]->{NAME});
	my $REG_EXPR = sprintf("%sRR*_?",$target_list[$t_count]->{NAME});# regexpr for TS predictions
        if(!(-e $new_dir) && $target_list[$t_count]->{NAME} =~ 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_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/%sRR*_?", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target_list[$t_count]->{NAME}, $target_list[$t_count]->{NAME}) ;
                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_list[$t_count]->{NAME});
                                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_list[$t_count]->{NAME},$REG_EXPR,$LOCAL_CONFIG->{DATA_RR_MODELS_DIR},$target_list[$t_count]->{NAME}));
	  }
        }
  }
}
# make links for models in /local/CASPCOMMONS/MODELS_PDB
system("/local/Projects/Perl/caspcommons/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 casp %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
