#!/usr/bin/perl

use strict;
use warnings;

use lib qw/Classes Core/;

use ResultsManager;
use TargetsManager;

use LocalConfiguration;

my $target = $ARGV[0];

my $outFile = "/local/CASP13/tmp_Grishin/$target.Lisa.csv";

my $DEBUG = 0; 

my @ALLGROUPS = qw/
006 008 011 014 022 023 024 026 032 034 038 040 041
042 044 049 050 054 056 063 064 065 067 073 076 077 080
092 097 106 110 111 116 117 118 120 128 132 133 144
145 153 155 156 157 160 162 169 171 173 184 186 192
193 197 203 204 206 210 212 216 228 230 235 237 241
251 258 260 263 268 269 276 277 279 281 282 290 296
300 301 310 311 317 322 326 328 333 335 336 338 340
342 345 346 347 349 357 358 360 361 362 364 368 381
385 386 391 401 403 404 410 414 417 419 420 425 428
430 433 434 436 437 438 439 442 445 448 452 454 457
460 465 466 476 479 482 483 490 492 493 495 499
/;

my %SERVERS = (
'008'=>1, '011'=>1, '022'=>1, '038'=>1, '041'=>1, '050'=>1, '073'=>1, '110'=>1,
'117'=>1, '133'=>1, '145'=>1, '156'=>1, '160'=>1, '171'=>1, '184'=>1, '193'=>1, 
'206'=>1, '210'=>1, '212'=>1, '216'=>1, '228'=>1, '237'=>1, '251'=>1, '263'=>1, 
'268'=>1, '277'=>1, '279'=>1, '300'=>1, '335'=>1, '345'=>1, '346'=>1, '349'=>1, 
'381'=>1, '410'=>1, '414'=>1, '420'=>1, '436'=>1, '448'=>1, '452'=>1, '454'=>1, 
'466'=>1, '479'=>1, '492'=>1, '499'=>1
);


my @TCGROUPS = qw/
032 038 040 041 042
044 064 065 080 155
157 162 169 186 219
276 287 300 310 329
342 345 357 361 420
428 476 479 490 /;


my %LIST_MODELS;

# parse full list models 

my $listModelsFile;
if ( $target =~ m/^T[acspx]/){
   $listModelsFile = "/local/Projects/Perl/casp13/src/scripts/TC.fullListModels.Lisa.csv";
} else {
   $listModelsFile = "/local/Projects/Perl/casp13/src/scripts/fullListModels.Lisa.csv";
}

open F, "< $listModelsFile";
while(defined(my $l = <F>)){
    chomp $l;
    $LIST_MODELS{$l} = '0';
}
close F;


my @GROUPS;

my $targetManager = new TargetsManager();
my $target_name;
my $domain; 
if ($target =~ m/^(T[acspx0-9][0-9]{3})-D([1-9]$)/){
   $target_name = $1;
   $domain = $2;
} else {
   $target_name = $target;
}




my $targ_id = $targetManager->get_id_by_name($target_name);
my %info = $targetManager->info($targ_id);
if ($target_name =~ m/^T[acspx]/){
	push @GROUPS, @TCGROUPS;
# remove check for server targets: i.e. generate all groups (including human ones)  for both kinds of targets
#} elsif ($info{IS_SERVER_ONLY} ){
#	push @GROUPS, sort keys %SERVERS;
} else {
	push @GROUPS, @ALLGROUPS;
}

my $resultsManager = new ResultsManager();
my $params = {
	field=>'gr.code,pr.model',
	order=>'ASC',
	target=>$target,
   };

my %hashDone; 

my @results = $resultsManager->get_results($params);

open OUT, "> $outFile";

printf OUT "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", 
	'#', 'Model', 'GR#', 'submitted', 'GDT_TS', 'NP_P', 'RMS_CA', 'RMS_ALL',
	'AL0_P', 'AL4_P', 'ALI_P', 'LGA_S', 'RMSD[L]',  'Zscore[D]', 'RMSD[D]', 'Al.Res.[D]', 'MolPrb', 'LDDT', 
	'SphGr', 'CAD_AA', 'RPF', 'CODM', 'DFM', 'Handed.', 'SOV', 'CE', 
	'QCS', 'CONTS', 'TM', 'TM_ALIGN', 'DALI_RAW', 'DALI_ALIGN', 'MAMMOTH_ln_e', 'MAMMOTH_ALIGN', 
	'LGA_ALIGN', 'CONTSINTRA';
	;



foreach my $res (@results){
  my $key;
  if ($res->{PARENT} >= 2){
	$key = sprintf("%03d_%d_%d",$res->{CODE}, $res->{MODEL}, $res->{PARENT});
  } else {
	$key = sprintf("%03d_%d",$res->{CODE}, $res->{MODEL});
	$res->{MODEL_NAME} =~ s/(_.)_1/$1/; # treat first parent as a model: i.e. _2_1 == _2
  }
# printf OUT "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
  $LIST_MODELS{$key} = sprintf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
#	$res->{INDEX},
	$res->{MODEL_NAME},
	$res->{CODE}.($res->{GROUP_TYPE} > 0 ? 's' : ''),
	's',
	$res->{GDT_TS_4},
	$res->{NP_P_4},
	$res->{ACE_RMS_CA},
	$res->{ACE_RMS_ALL},
	$res->{AL0},
	$res->{AL4},
	$res->{AL4XP},
	$res->{LGA_S_5},
	$res->{RMSD_5},
	($res->{DALI_ZSCORE} > -999 ? $res->{DALI_ZSCORE}: 'N/A'),
	($res->{DALI_RMSD} > 0.0? $res->{DALI_RMSD}: 'N/A'),
	($res->{DALI_RMSD} > 0.0? $res->{DALI_ALIGN_RESIDUES}: 'N/A'),
	($res->{MP_SCORE} < 6.0 ? $res->{MP_SCORE}: 'N/A'),
	($res->{LDDT}> -1.0? $res->{LDDT}: 'N/A' ), 
	($res->{SG_LVR} > -1.0 ? $res->{SG_LVR} : 'N/A'),
	($res->{CAD_AA} > -1.0 ? $res->{CAD_AA} : 'N/A'),
	($res->{RPF} > -1.0 ? $res->{RPF} : 'N/A'),
	($res->{CODM} > -1.0 ? $res->{CODM} : 'N/A'),
	($res->{DFM} > -1.0 ? $res->{DFM} : 'N/A'),
	($res->{HANDEDNESS} > -1.0 ? $res->{HANDEDNESS} : 'N/A'),
	($res->{SOV} > -1.0 ? $res->{SOV} : 'N/A'),
	($res->{CE} > -1.0 ? $res->{CE} : 'N/A'),
	($res->{QCS} > -1.0 ? $res->{QCS} : 'N/A'),
	($res->{CONTS} > -1.0 ? $res->{CONTS} : 'N/A'),
	($res->{TM_SCORE} > -1.0 ? $res->{TM_SCORE} : 'N/A'),
	($res->{TM_N_ALIGN} > -1.0 ? $res->{TM_N_ALIGN} : 'N/A'),
	($res->{DALI_RAW} > -1.0 ? $res->{DALI_RAW}: 'N/A'),
	($res->{DALI_RAW} > -1.0 ? $res->{DALI_N_ALIGN}: 'N/A'),
	$res->{LN_E_M},
	$res->{MAMMOTH_P_ALIGN},
	$res->{EQV0P},
	$res->{CONTSINTRA};
#	$hashDone{sprintf("%03d_%d",$res->{CODE}, $res->{MODEL})} = 1; 	
}

my $index = 1; 
foreach my $key (sort keys %LIST_MODELS){
	if ($LIST_MODELS{$key} eq '0'){
		my $code = substr($key, 0, 3);
		printf OUT "%s\t%s\t%s\tns\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n",
                $index,
                sprintf ("%sTS%s%s", $target_name, $key, (defined($domain) ? '-D'.$domain : '')),
                sprintf ("%03d%s", $code, (defined($SERVERS{sprintf("%03d",$code)}) ? 's' : ''))
                ;
	} else {
		printf OUT "%s\t%s", $index, $LIST_MODELS{$key};
	}
	$index++;
}

=head
my $index = scalar(@results) + 1;

foreach my $code (@GROUPS){
	foreach my $m (qw/1 2 3 4 5/){
		my $key = sprintf("%03d_%d", $code, $m);
		if (exists $hashDone{$key}){next;}
		printf OUT "%s\t%s\t%s\tns\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n",
		$index,
		sprintf	("%sTS%03d_%d%s", $target_name, $code, $m, (defined($domain) ? '-D'.$domain : '')),
		sprintf ("%03d%s", $code, (defined($SERVERS{sprintf("%03d",$code)}) ? 's' : ''))
		;
		$index++;
;
	}
}
=cut

close OUT;

system("chgrp casp $outFile");
system("chmod 664 $outFile");
