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


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

use DomainsManager;

my $d_m = new DomainsManager();
my $target = "T0957s2";
#$d_m->split_targets_to_domains($target);

# split models to domains
$d_m->split_models_to_domains($target);


exit;
# DON"T RUN
=head

# CREATE TARBALLS OF TRIMMED PREDICTIONS
my $one_time = 0;
sleep 1;
for (my $i = 0; $i < scalar(@domains); $i++) {
        if ($domains[$i]->{TARGET_NAME} eq $ARGV[0]) {
	    if ($one_time == 0){
		system(sprintf("find %s -name \'%s-D*\' -type f | xargs --no-run-if-empty /bin/rm -f  \n",$tarballPredictionsDir,$domains[$i]->{TARGET_NAME}));
		$one_time = 1;
	    }
	    if ($domains[$i]->{DOMAIN_INDEX} =~ m/^[1-9]$/){
		&createPredictionsTarball($domains[$i]->{TARGET_NAME},$domains[$i]->{DOMAIN_INDEX});
	    }
	}
}

# CREATE TARBALL OF TRIMMED TARGETS
sleep 1;
&createTargetsTarball();

# make links of all models in /local/CASP13/MODELS_PDB
system("/local/Projects/Perl/casp13/src/scripts/linkModels.pl");

exit(0);


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

# sub returns string of ranges of all domains
# the string is used in name of tarball
sub toStringRangeDomains {
    my ($inDir,$target) = @_;
    my @domainsFiles ;
    open (INDIR, " /bin/ls -1 $inDir/$target-D?.pdb | ");
    while(defined(my $file = <INDIR>)){
        chomp $file;
        $file = substr($file, rindex($file,'/') + 1);
        push @domainsFiles, $file;
    }
    close(INDIR);
    my $result = "";
    foreach my $file (@domainsFiles){
        my $index;
        if($file =~ m/^\S+-D([1-9]{1})/){
                $index = $1;
                $result .= "D$index"."_";
        } else {
                return;
        }
        open (FIRST, "grep \"^ATOM\" $inDir/$file | head -n 1 | ");
        if (defined(my $line = <FIRST>)) {
           my $start = substr($line,22,4);
           $start =~ s/^\s+//;
           $start =~ s/\s+$//;
           $result .= "$start-";
        }
        close(FIRST);
        open (LAST, "grep \"^ATOM\" $inDir/$file | tail -n 1 | ");
        if (defined(my $line = <LAST>)) {
          my $end = substr($line,22,4);
          $end =~ s/^\s+//;
          $end =~ s/\s+$//;
          $result .= "$end.";
        }
        close(LAST);
    }
    return $result;
}


sub toStringRangeDomainsFromPDBFiles {
    my ($inDir,$target) = @_;
    my @domainsFiles ;
    open (INDIR, " /bin/ls -1 $inDir/$target-D?.pdb | ");
    while(defined(my $file = <INDIR>)){
        chomp $file;
        $file = substr($file, rindex($file,'/') + 1);
        push @domainsFiles, $file;
    }
    close(INDIR);
    my $result = "";
    foreach my $file (@domainsFiles){
        my $index;
        if($file =~ m/^\S+-D([1-9]{1})/){
                $index = $1;
                $result .= "D$index"."_";
        } else {
                return;
        }
        $result .= &getRangeFromPDBFile("$inDir/$file");
    }
    return $result;
}


sub getRangeFromPDBFile {
    my ($pdbFile) = @_;
    my $result = "";
    my $start = '';
    my $end = '';
    my $current = '';
    open (CAs, "grep \"^ATOM\" $pdbFile | grep \"CA\" | ");
    while (defined(my $line = <CAs>)){
        $current = substr($line,22,4);
        $current =~ s/^\s+//;
        $current =~ s/\s+$//;
        if ($start eq '') {
            $start = $current;
            $result .= $start.'-';
        }
        if ($end eq ''){
           $end = $current;
        }
        if ( ($current - $end) > 20 ) { # if gap in numeration of amino acids is larger than 20 
           $result .= $end."_";
           $start = '';
           $end = '';
        } else {
           $end = $current;
        }
    }
    if ( ($end - $start) > 20){
        $result .= $end."_";
    }
    if ($result =~ m/-$/) {
        $result =~ s/_(\d+)-$/_/g;
    }
    close(CAs);
    return $result;
}



sub createPredictionsTarball {
    my ($target, $index) = @_;
    my $dir_dom1 = sprintf("%s/%s-D%s", $LOCAL_CONFIG->{DATA_MODELS_DIR}, $target, $index);
    if (! -d $dir_dom1) {
        return;
    }
    my $tar_name = sprintf("%s/%s-D%s.tar.gz", $tarballPredictionsDir, $target, $index);
    if (-e $tar_name ) {
        system(sprintf("/bin/rm -f %s",$tar_name));
    }
    system(sprintf("cd %s ; tar -czhf $tar_name %s-D%s ", $LOCAL_CONFIG->{DATA_MODELS_DIR} ,$target, $index));
    system("chgrp casp $tar_name");
    system("chmod 664 $tar_name");
#    printf("cd %s ; tar -czhf $tar_name %s-D%s ", $LOCAL_CONFIG->{DATA_MODELS_DIR} ,$target, $index);

}


sub createTargetsTarball {
    if (-e $tarballTargetsName){
        system("/bin/rm -f $tarballTargetsName");
    }
    system(sprintf("cd %s; tar -czhf %s *-D?.pdb", $LOCAL_CONFIG->{DATA_TARGETS_DIR}, $tarballTargetsName));
    system("chgrp casp $tarballTargetsName");
    system("chmod 664 $tarballTargetsName");
}
=cut
