#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
my %commands = () ;

if ( @ARGV <= 0 ) { push ( @{$commands{'help'}}, ' ' ) ; } 
else { (%commands) = main_config::get_commands ( @ARGV ) ; }

my $conf_file ;
if ( -f "main_config.data" )
{    $conf_file = "main_config.data" ; }
else
{    $conf_file = join ('', $ENV{"MAIN_CONF"}, "main_config.data" ) ; }

my %my_main = main_config::read_main_config_file ( $conf_file ) ; 

#####################################################################

if ( !exists( $my_main{"SUPERIMP_FILE_INP"} ) ) { push ( @{$my_main{"SUPERIMP_FILE_INP"}}, "" ) } ; 
if ( !exists( $my_main{"SUPERIMP_SOURCE"} ) ) { push ( @{$my_main{"SUPERIMP_SOURCE"}}, "SELECTION" ) } ; 
if ( !exists( $my_main{"SUPERIMP_SELECTION"} ) ) { push ( @{$my_main{"SUPERIMP_SELECTION"}}, "" ) } ; 
if ( !exists( $my_main{"SUPERIMP_ATOMS"} ) ) { push ( @{$my_main{"SUPERIMP_ATOMS"}}, "CA" ) } ; 
if ( !exists( $my_main{"SUPERIMP_APPLIED_SELECTION"} ) ) { @{$my_main{"SUPERIMP_APPLIED_SELECTION"}} = ( "" ) ; } ; 
if ( !exists( $my_main{"SUPERIMP_TARGET"} ) ) { push ( @{$my_main{"SUPERIMP_TARGET"}}, "" ) } ; 
if ( !exists( $my_main{"SUPERIMP_WAY"} ) ) { push ( @{$my_main{"SUPERIMP_WAY"}}, "DIST" ) } ; 
if ( !exists( $my_main{"SUPERIMP_MATRIX"} ) ) { push ( @{$my_main{"SUPERIMP_MATRIX"}}, "superimpose.com" ) } ; 
if ( !exists( $my_main{"SUPERIMP_TASK"} ) ) { push ( @{$my_main{"SUPERIMP_TASK"}}, "FIT" ) } ; 

######################################################

my $MACRO_FILE = "superimpose.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    print ">$keyword< >", join ( " ", @{$commands{$keyword}} ), "<\n" ; 
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 't' || substr($keyword, 0,8) eq "target" ) { goto TARGET }
#    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "file" ) { goto FILE }
    if ( $keyword eq 'n' || substr($keyword, 0,5) eq "atoms" ) { goto ATOMS }
    if ( $keyword eq 'm' || substr($keyword, 0,6) eq "matrix" ) { goto MATRIX }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "source" ) { goto SOURCE }
    if ( $keyword eq 'a' || substr($keyword, 0,7) eq "applied" ) { goto APPLIED }
    if ( $keyword eq 'l' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION }
    if ( $keyword eq 'w' || substr($keyword, 0,3) eq "way" ) { goto WAY }
    if ( $keyword eq 'k' || substr($keyword, 0,4) eq "task" ) { goto TASK }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

HELP:
    print << "END_OF_TEXT" ;
  -h|--help)  prints this message with available options and current status 
  -o|--output)      set file name of the created macro [$MACRO_FILE]
  -s|--source)      source [SELECTION/PAIRS/FILE] [@{$my_main{"SUPERIMP_SOURCE"}}]
  -w|--way)         way of superimposition [PAIR/DIST/SEQ_ID/3D_CE/FatCat] [@{$my_main{"SUPERIMP_WAY"}}]
  -l|--selection)   from /source/ selection [@{$my_main{"SUPERIMP_SELECTION"}}]
  -n|--atoms)       atom names [@{$my_main{"SUPERIMP_ATOMS"}}]
  -a|--applied)     applied to selection /when empty, equals to from/ [@{$my_main{"SUPERIMP_APPLIED_SELECTION"}}]
  -t|--target)      to /target/ selection [@{$my_main{"SUPERIMP_TARGET"}}]
  -k|--task)        task [FIT/SHOW_RMS] [@{$my_main{"SUPERIMP_TASK"}}]
  -m|--matrix)      rotation matrix file [@{$my_main{"SUPERIMP_MATRIX"}}]
     --doit)        create the macro 
END_OF_TEXT
#  -f|--file)        from file [@{$my_main{"SUPERIMP_FILE_INP"}}]

    exit ;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
FILE: @{$my_main{"SUPERIMP_FILE_INP"}} = @{$commands{$keyword}} ; next ;
SELECTION: @{$my_main{"SUPERIMP_SELECTION"}} = @{$commands{$keyword}} ; next ;
ATOMS: @{$my_main{"SUPERIMP_ATOMS"}} = @{$commands{$keyword}} ; next ;
APPLIED: @{$my_main{"SUPERIMP_APPLIED_SELECTION"}} = @{$commands{$keyword}} ; next ;
SOURCE: @{$my_main{"SUPERIMP_SOURCE"}} = @{$commands{$keyword}} ; next ;
TARGET: @{$my_main{"SUPERIMP_TARGET"}} = @{$commands{$keyword}} ; next ;
MATRIX: @{$my_main{"SUPERIMP_MATRIX"}} = shift ( @{$commands{$keyword}} ) ; next ;
TASK: @{$my_main{"SUPERIMP_TASK"}} = shift ( @{$commands{$keyword}} ) ; next ;
WAY: @{$my_main{"SUPERIMP_WAY"}} = shift ( @{$commands{$keyword}} ) ; next ;
}

if ( $PROC_ERROR eq "TRUE" ) { exit ; }

#######################################

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

if ( $doit eq "FALSE" )  { exit ; }

######################################

my $ATOMS = join( " ",  @{$my_main{"SUPERIMP_ATOMS"}} ) ;
my $CUT_OFF = 1.8 ;
my $FILE_INP = join( " ",  @{$my_main{"SUPERIMP_FILE_INP"}} ) ;
my $SELECTION_FROM = join( " ",  @{$my_main{"SUPERIMP_SELECTION"}} ) ;
my $SELECTION_APPLIED = join( " ",  @{$my_main{"SUPERIMP_APPLIED_SELECTION"}} ) ;
if ( $SELECTION_APPLIED eq "" || $SELECTION_APPLIED eq " " ) 
{ 
    print " selection applied >", $SELECTION_APPLIED, "<\n" ;
    @{$my_main{"SUPERIMP_APPLIED_SELECTION"}} =  @{$my_main{"SUPERIMP_SELECTION"}} ; 
    $SELECTION_APPLIED = join( " ",  @{$my_main{"SUPERIMP_APPLIED_SELECTION"}} ) ;
}
my $SELECTION_TARGET = join( " ",  @{$my_main{"SUPERIMP_TARGET"}} ) ;
my $MATRIX_FILE = $my_main{"SUPERIMP_MATRIX"}[0] ;

my $SUPER_CE = $ENV{ 'CE_SUPERIMPOSE'}  ;

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

#echo
#echo " generating \"./${MACRO_FILE}\" macro "
#echo " used for RMS fit based superpositions of molecules within each group"
print MACRO << "END_OF_TEXT" ;
key ca sele atom name CA end
key out sele .n all end
END_OF_TEXT


if ( $my_main{"SUPERIMP_WAY"}[0] eq "3D_CE" ||  $my_main{"SUPERIMP_WAY"}[0] eq "FatCat" ) 
{
    if ( $my_main{"SUPERIMP_SOURCE"}[0] eq "FILE" ) 
    {
	system ("cp $FILE_INP tmp2.pdb" ) ;
	print MACRO << "END_OF_TEXT" ;
key old sele all end
read file $FILE_INP coor pdb
set vari MY_SEGMS char segm sele .n old end
< ?MAIN_UTILS:make_bonds_from_topo.com MY_SEGMS
calc bond sele .n old .a .n by bond all end

write over file tmp1.pdb sele $SELECTION_TARGET .a atom name $ATOMS end coor pdb
write over file tmp2.pdb sele .n old .a atom name $ATOMS end coor pdb

END_OF_TEXT
    }
    else
    {
    print MACRO << "END_OF_TEXT" ;
write over file tmp1.pdb sele $SELECTION_TARGET .a atom name $ATOMS end coor pdb
write over file tmp2.pdb sele $SELECTION_FROM .a atom name $ATOMS end coor pdb
END_OF_TEXT
    }

    if ( $my_main{"SUPERIMP_WAY"}[0] eq "3D_CE" ) 
    {
    print MACRO << "END_OF_TEXT" ;
shell "bash $SUPER_CE/runCE.sh -file1 tmp1.pdb -file2 tmp2.pdb -printCE > CE.log"
shell "CE_matrix_extract.pl -i CE.log -m  $MATRIX_FILE"
END_OF_TEXT
    }
    elsif ( $my_main{"SUPERIMP_WAY"}[0] eq "FatCat" ) 
    {
	print MACRO << "END_OF_TEXT" ;

shell "bash $SUPER_CE/runFatcat.sh -file1 tmp1.pdb -file2 tmp2.pdb -printCE > FatCat.log"
shell "CE_matrix_extract.pl -i FatCat.log -m  $MATRIX_FILE"

END_OF_TEXT
    }

    print MACRO << "END_OF_TEXT" ;
set coor save now
< $MATRIX_FILE
END_OF_TEXT


if ( $my_main{"SUPERIMP_TASK"}[0] eq "FIT" ) 
{
    if ( $my_main{"SUPERIMP_SOURCE"}[0] eq "FILE" ) 
    {
	print MACRO << "END_OF_TEXT" ;
rota atom sele .n old end matrix 3
tran atom sele .n old end vector XTRAN YTRAN ZTRAN
END_OF_TEXT
    }
    else
    {
    print MACRO << "END_OF_TEXT" ;
rota atom sele $SELECTION_APPLIED end matrix 3
tran atom sele $SELECTION_APPLIED end vector XTRAN YTRAN ZTRAN

END_OF_TEXT
    }
}
}


if ( $my_main{"SUPERIMP_WAY"}[0] eq "PAIR" ) 
{
     if ( $my_main{"SUPERIMP_SOURCE"}[0] eq "SELECTION" ) 
     {
	 print MACRO << "END_OF_TEXT" ;
make pair init sele $SELECTION_FROM .a atom name $ATOMS end \\
               sele $SELECTION_TARGET .a atom name $ATOMS end 

END_OF_TEXT
     }
     print MACRO << "END_OF_TEXT" ;
rms coor all  eigen pair
save over file $MATRIX_FILE rms
set coor save now
set vari MAT_ROT global = 3
< $MATRIX_FILE
rota atom sele $SELECTION_APPLIED end matrix 3
tran atom sele $SELECTION_APPLIED end vector XTRAN YTRAN ZTRAN

END_OF_TEXT
}

if ( $my_main{"SUPERIMP_WAY"}[0] eq "DIST" ) 
{
    print MACRO << "END_OF_TEXT" ;
calc pair ( init sele $SELECTION_FROM .a atom name $ATOMS end \\
      sele $SELECTION_TARGET .a atom name $ATOMS end range 0. $CUT_OFF )

save over file $MATRIX_FILE rms

set coor save now
set vari MAT_ROT global = 3
< $MATRIX_FILE
rota atom sele $SELECTION_APPLIED end matrix MAT_ROT
tran atom sele $SELECTION_APPLIED end vector XTRAN YTRAN ZTRAN

END_OF_TEXT
}

if ( $my_main{"SUPERIMP_WAY"}[0] eq "SEQ_ID" ) 
{
    print MACRO << "END_OF_TEXT" ;
make pair init seq name_mat \\
    sele $SELECTION_FROM .a atom name $ATOMS end \\ 
    sele $SELECTION_TARGET .a atom name $ATOMS end 
rms coor all  eigen pair
save over file $MATRIX_FILE rms
set coor save now
set vari MAT_ROT global = 3
< $MATRIX_FILE
rota atom sele $SELECTION_APPLIED end matrix 3
tran atom sele $SELECTION_APPLIED end vector XTRAN YTRAN ZTRAN


END_OF_TEXT
}

print MACRO "image over false\n" ;
print MACRO "return\n" ;

close ( MACRO ) ;
exit ;
