#!/usr/bin/env perl
use strict ;
#
# opens validate.dat_old and validate.dat_new
# generates sum over each residue and compares old/new
# returns a com list of residues with higher sum
#

my $file_old = "validate_starting.dat" ;
my $file_new = "validate_modified.dat" ;

open ( FILE_OLD, "< $file_old" || die "$! $file_old" ) ;
open ( FILE_NEW, "< $file_new" || die "$! $file_new" ) ;

my @file_old = <FILE_OLD> ;
my @file_new = <FILE_NEW> ; 

close ( FILE_OLD ) ;
close ( FILE_NEW ) ;


my ( $validate_starting_data, $validate_starting_index ) = extract_keys_sum ( @file_old ) ;
my ( $validate_modified_data, $validate_modified_index ) = extract_keys_sum ( @file_new ) ;


# @my_seq = ( ["energy", "atom", "res_name", "res_num", "seg_name"], [...] )
# %inverse_my_seq = ( "atom + res_name + res_num + seg_name" => $nres );
# $validate_modifed_index{$key} returns $nres

# #ref is val_... points to array, array is hash -> @array = ( [hash], [hash],...)
# #and hash
# #    print "@$ref[0]{'atom'}\n"; bad!
#foreach my $elem (@ref){
#    print "$elem\n";
#}



compare ();
sub compare
{
    my @key_sum = ();
    my @m_data  = @$validate_modified_data;   #dereference
    my %m_index = %$validate_modified_index;
    my @s_data  = @$validate_starting_data;
    my %s_index = %$validate_starting_index;

    foreach my $key ( keys %m_index )   # if %m_index has more keys than s_index the array value returns an undefined value
	                                # m_index is always <= s_index, the other way around it might not work correctly
    {
#	print "$key: --------------------", $m_data[$m_index{$key}]{"energy"}, "\t", $s_data[$s_index{$key}]{"energy"}, "\n";

	# comment for debuging
	print $m_data[$m_index{$key}]{"res_name"}, $m_data[$m_index{$key}]{"res_num"}, "  starting -> ", $s_data[$s_index{$key}]{"energy"}, "  ", $m_data[$m_index{$key}]{"energy"}, " <- modified\n" ;
	# end debugging

	if ( $m_data[$m_index{$key}]{"energy"} < 
	     $s_data[$s_index{$key}]{"energy"} ){
	    
	    my $atom = $m_data[$m_index{$key}]{"atom"} ;
	    push ( @key_sum, "set coor select by residue atom numb $atom end restore\n" );
	}
    }

    
# writes new coordinates to file
    my $file_com = "val.com" ;
    open ( FILE_COM, "> $file_com" || die "$! $file_com" ) ;

    foreach (@key_sum)
    {
	print FILE_COM $_ ;
    }

    close ( FILE_COM ) ;

}




#     my @file_old = @{$_[0]} ;
#     my @file_new = @{$_[1]} ;

#     my @comp = () ;
#     my @com = () ;

# #same as in res_anal add atom id for unambiguity (to sum)


#     for ( my $count = 0; $count < @file_new; $count++ )
#     {
# 	$comp[$count][0] = $file_new[$count][0] ;
# #	print $file_new[$count][0] . "\t";
# 	my @m =  grep ($_ eq $file_new[$count][0], @file_old);
	
# 	my $l;
# 	foreach $l (@m) {
# #	    print $l . "\n";
# 	}


# #	if ( $file_old[$count][1] <= $file_new[$count][1] )
# # 	if ( $file_old[$count][0] eq $file_new[$count][0] )

# # 	{
# # 	    $comp[$count][1] = $file_old[$count][1] ;
# # 	}
# # 	else
# # 	{
# # 	    $comp[$count][1] = $file_new[$count][1] ;
# # 	    push @com, join(' ', "set coor select seq ", $file_old[$count][2] , " end restore\n")
# # 	}
#     }

# # writes new coordinates to file
#     my $file_com = "val.com" ;
#     open ( FILE_COM, "> $file_com" || die "$! $file_com" ) ;

#     foreach (@com)
#     {
# 	print FILE_COM $_ ;
#     }

#     close ( FILE_COM ) ;

# # # print @sum content
# # my $count = 0;
# # for ( $count = 0; $count < @sum_old ; $count++ )
# # {
# #     print $s


sub extract_keys_sum
{
    my @file =  @_ ;
    my $line;
    my $nres = 0;
    my @words;
    my @my_seq = ();
    my %inverse_my_seq = () ;
    my $key_new ;
    my $key_old = "%^&#"; 
    $nres = -1 ;

    foreach $line ( @file )
    {
	chomp ( $line ) ;
	@words = split(" ", $line) ;
#	print "$words[11]\n";
	$key_new = join( " ", $words[11], $words[12], $words[13] ) ;

	if ( $words[10] eq "C" or $words[10] eq "CA" or $words[10] eq "N" or $words[10] eq "O" ) { 
	  next; }

	if ( $key_new ne $key_old )
	{
	    $key_old = $key_new ;
	    $nres++ ;
	    $my_seq[$nres]{"energy"} =  0 ;
	    $my_seq[$nres]{"atom"} = $words[9] ;    # 
	    $my_seq[$nres]{"atom_name"} = $words[10] ;
	    $my_seq[$nres]{"res_name"} = $words[11] ;    # MET, GLY,
	    $my_seq[$nres]{"res_num"} = $words[12] ;     # 1, 2, 
	    $my_seq[$nres]{"seg_name"} = $words[13] ;    # MOL1,
	    my $key = join ( " ", $words[9], $key_old ) ;
	    $inverse_my_seq{$key} = $nres ;
	    

#	    print "$key\n";
	    
	}
	if ( $words[12] == 15){
	#print for degub
	print $key_new, " energy: ", $words[7], "\n";
	#end debug
	}

	$my_seq[$nres]{"energy"} =  $my_seq[$nres]{"energy"} + $words[7];        # energy

#	print $my_seq[$nres]{"atom_name"}, "\t", $my_seq[$nres]{"energy"}, "\n";
	# @my_seq = ( ["energy", "atom", "res_name", "res_num", "seg_name"], [...] )
	# %inverse_my_seq = ( "atom + res_name + res_num + seg_name" => $nres );
    }

    print "\n";
    
    return ( \@my_seq, \%inverse_my_seq ) ;
}


# # residue is unabigously defined by start/end atom number
# # this part determines residue atoms and saves them by atom number

#     my @residues = ();
#     my $numb = 0;        # residue count
#     my $count = 0;

#     for ($count = 0; $count < @my_seq ; $count++ )
#     {
# 	if ( $my_seq[$count]{"res_name"} ne $residues[$numb]{"res_name"}
#  	     or $my_seq[$count]{"res_num"} ne $residues[$numb]{"res_num"}
# 	     or $my_seq[$count]{"seg_name"} ne $residues[$numb]{"seg_name"} )
# 	{
# 	    $numb++ ;
	    
# 	    $residues[$numb]{"res_name"} = $my_seq[$count]{"res_name"} ;
# 	    $residues[$numb]{"res_num"} = $my_seq[$count]{"res_num"} ;
# 	    $residues[$numb]{"seg_name"} = $my_seq[$count]{"seg_name"} ;
# 	    $residues[$numb]{"atom_start"} = $my_seq[$count]{"atom_num"} ;    # starting atom number of residue
	    
# #	    print $numb . "\t" . $residues[$numb]{"res_name"} . "\t" . $residues[$numb]{"atom_start"} . "\n";
# 	}
#     }
#     shift @residues;	# remove first elem, becase we start writing at $numb = 1


# #
# # sum over each residue
# #
    
#     my @sum = () ;
#     my $s = 0 ;
#     my $count = -1 ;
#     my $value ;
#     $line = 0;
#     my $line2 = "%Y&*" ;    # some value
#     my $count2 = 0 ;

# ###

#     for ($nres = 0; $nres < @my_seq; $nres++)
#     {
# 	$line = join ('', $my_seq[$nres]{"res_name"}, $my_seq[$nres]{"res_num"}, $my_seq[$nres]{"seg_name"}) ;
# 	$value = $my_seq[$count]{"energy"};
	
# 	if ( $line ne $line2 )
# 	{

# 	    $s = 0 ;
# 	    $count++ ;
# 	    $line2 = $line ;

# 	    $sum[$count][0] = join ('', $residues[$count]{"atom_start"}, $residues[$count]{"res_name"},
# 				       $residues[$count]{"res_num"}, $residues[$count]{"seg_name"}) ;   #residue identifier
# 	}
	
# 	$s = $s + $value ;
# 	$sum[$count][1] = $s ;

# #	print $line . "\t" . $value . "\t" . $sum[$count][1] . "\n";	
# #	$sum[$line] = $s ;
# #	$sum[$count-1] = $s ;
# #	print $line, "\t", $line2, "\t", $value, "\t", $s, "\t", $count, "\n" ;
	
# 	$count2++;
#     }
    
# #    for ($count = 0; $count < @sum; $count++){
# #	print $sum[$count][0] . "-" ."\n";
# #    }
# #    print "\n";
    
# #    print $count = @sum ;
# #    print "\n" ;
    
#     return ( @sum ) ;
# }
