#!/usr/bin/perl

use strict;
use warnings;

###########################################################
#
# The script corrects eavluation of models with several parents:
# add the original (cleaned) prediction to /data/CASP13/MODELS
# run 
#
###########################################################

my $list_file = "/data/CASP13/MODELS/parent_models.txt";

#my @plugins = qw/lga_sda lga_sia mammoth DaliLite LVR/;
my @plugins = qw//;

my @domains = qw/D1 D2 D3 D4 D5 D6 D12 D13 D14 D15 D16 D23 D24 D25 D26 D34 D35 D36 D45 D46 D56/;
#my @domains = qw/D1 D2 D12/;

my $exlude_targets = "T0654 ";
#my $include_targets = "T0649 T0651 T0653 T0654";
my $include_targets = "T0713";

my @models = ();
my @targets = ();
my @models_1 = (); # original predictions (not splitted into parents)
my %hash1;
my %hash2;

open FILE, "< $list_file";
while(defined(my $line = <FILE>)){
  chomp $line;
  if ( $line =~ m/(^T[0-9]{4})(TS[0-9]{3}_[1-9])_?/ ) {
	my $target = $1;
	my $model_1 = $1.$2;# original 
	if($exlude_targets =~ m/$target/){
#	if($include_targets !~ m/$target/){
	   next;
	}
	push (@models, $line);
	$hash1{$target} = 1;
	$hash2{$model_1} = 1;
  }
}
close(FILE);

@targets = sort keys %hash1;
@models_1 = sort keys %hash2;

# copy 
=head
foreach my $model_1 (@models_1){
#   print $model_1."\n";
   if ($model_1 =~ m/^(T[0-9]{4})(TS[0-9]{3}_[1-9])$/){

	my $target = $1;
	my $src_file = sprintf("/local/CASP13/predictions/cleaned/%s/%s",$target,$model_1);
	my $dest_file = sprintf("/local/CASP13/MODELS/%s/%s", $target, $model_1);
#	print ("cp -p $src_file $dest_file\n");
#	system("cp -p $src_file $dest_file");
#	print ("chown casp:casp $dest_file\n");
#	system("chown casp:casp $dest_file");
#	system("rm -f $dest_file"); # remove the predictions with maltiple parants as whole prediction
   } else {
	print "ALARM!:$model_1\n";
   }
}
=cut
exit;

# clean files with mvsm results 
# delete predictions T0649TS302_1 if exist T0649TS302_1_1
=head THIS PART OF CODE HAS BEEN NEVER USED: IT ISN'T EVEN COMPLETED
foreach my $target (@targets){

	if(-d "/data/CASP13/RESULTS/MVSM/DATA/$target"){
	   my $mvsm_dir = "/data/CASP13/RESULTS/MVSM/DATA/$target";
	   opendir DIR, $mvsm_dir;
	   my @files;
	   while(my $file = readdir(DIR)){ 
		if($file =~ m/^\./){next;}
		push @files, $file;
	   }
	   closedir(DIR);
	   foreach my $file (@files){
	     system("cp $mvsm_dir/$file $mvsm_dir/tmp");
	     foreach my $model_1 (@models_1){
		if($model_1 !~ m/$target/){ next;}
		system("grep '$model_1 ' -v $mvsm_dir/tmp > $mvsm_dir/tmp2");
		sleep 1;
		system("mv $mvsm_dir/tmp2 $mvsm_dir/tmp");
	     }
	     
	   }
	}  
	foreach my $domain (@domains){

	}

}
=cut

=head
foreach my $target (@targets){
   print "$target\n";
}

exit;

my %hash_caca;

foreach my $model (@models){
   my $target = substr($model,0,5);
	if(!defined($hash_caca{$target})){
	    system ("./rsync_upload.sh $target caca ");
	    print ("./rsync_upload.sh $target caca \n");
	    $hash_caca{$target} = 1;
	}
   foreach my $domain (@domains){
	if(!defined($hash_caca{"$target-$domain"}) && $domain =~ m/D[1-6]$/ && -e "/local/CASP13/TARGETS/$target-$domain.pdb"){
                    print ("./rsync_upload.sh $target-$domain caca \n");
                    system ("./rsync_upload.sh $target-$domain caca ");
                    $hash_caca{"$target-$domain"} = 1;
	}  
   }
   foreach my $plugin (@plugins){
	if (-e "/local/CASP13/TARGETS/$target.pdb" && -e "/local/CASP13/MODELS/$target/$model"){
	     system("./rsync_upload.sh $target $plugin $model ");
	     print ("./rsync_upload.sh $target $plugin $model \n");
	}
	if($target eq 'T0678' || $target eq 'T0744'){next;} # these targets should be run  from admin web-site
	foreach my $domain (@domains){
	    if(-e "/local/CASP13/TARGETS/$target-$domain.pdb" && -e "/local/CASP13/MODELS/$target-$domain/$model-$domain"){
		if($domain =~ m/D[1-6]{2}/ && $plugin ne "lga_sda") {next;}
		system("./rsync_upload.sh $target-$domain $plugin $model-$domain ");
		print ("./rsync_upload.sh $target-$domain $plugin $model-$domain \n");
	    }
	}
   }
}

exit;
=cut


=head
foreach my $target (@targets){
    printf ("find /data/CASP13/RESULTS/CACA/ -name $target |xargs --no-run-if-empty echo  \n");
   system(sprintf ("find /data/CASP13/RESULTS/CACA/ -name $target* |xargs --no-run-if-empty echo  "));
#    print ("./rsync_upload.sh $target caca \n");
#    system ("./rsync_upload.sh $target caca ");
}

exit;
=cut
