#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
use Tk;

my $Win_background = "grey" ;
my $Win_foreground = "black" ;
my $max_length = 20 ;

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{"IMAGE_COLOR_START"} ) ) 
{ 
    if ( $my_main{"SG_NMOL"}[0] > 8 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 144 ) }
    elsif( $my_main{"SG_NMOL"}[0] > 4 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 150 ) ; }
    elsif( $my_main{"SG_NMOL"}[0] > 2 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 152 ) ; }
    else
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 160 ); }
}
if ( !exists($my_main{"IMAGE_COLOR_INCR"} ) ) 
{ 
    if ( $my_main{"SG_NMOL"}[0] > 8 ) 
    { push ( @{$my_main{"IMAGE_COLOR_INCR"}}, 5 ) }
    else
    { push ( @{$my_main{"IMAGE_COLOR_INCR"}}, 5 ) ; }
}

my @all_selections ;
my @selection_list ;
my @window_list ;
######################################################

my $PROC_ERROR = "FALSE" ;
my $MACRO_FILE ="re_image.cmds" ;

# # my $keyword ; 
# # foreach $keyword ( sort keys (%commands) ) 
# # {
# #     if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
# #     if ( $keyword eq 'm' || substr($keyword, 0,5) eq "macro" ) { goto MACRO }
# #     print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;
# # 
# #   HELP:
# #     print << "END_OF_TEXT" ;
# #     -h|-help|--help) prints this message with available options and current status 
# #     -m|--macro)      configure the perl script [$MACRO_FILE]
# # END_OF_TEXT
# # #    exit ;
# # # MACRO: 
# # #    $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; 
# # next ;
# # }
# # # if ( $PROC_ERROR eq "TRUE" ) { exit ; }
# # #######################################


my $PDB_FILE  = ${$my_main{"FILE_MODEL"}}[0] ;

my ( $ref_segm_list, $ref_resid_list, $ref_seq_list, $ref_unit_cell, 
     $ref_space_group, $ref_atom_list, $ref_xyz_list, $ref_b_list, 
     $ref_w_list, $ref_atom_el_list ) = 
    main_config::read_pdb_file ( $PDB_FILE ) ;

my @my_segm_list = @{$ref_segm_list} ;
my @my_resid_list = @{$ref_resid_list} ;
my @my_seq = @{$ref_seq_list} ;

my %image_cmds ;

$image_cmds{"ATOM"} = ["NONE", "BALL", "NAME", "CLASS", "CROSS"] ;
$image_cmds{"BOND"} = ["NONE", "LINE", "STICK"] ;
$image_cmds{"RIBBON"} = ["NONE", "LINE", "STICK"] ;
$image_cmds{"STYLE"} = ["WEIGHT", "TYPES", "N2C", "SECONDARY", "RESIDUE", "AUTO" ] ;

my %selections = () ;

my $word ;
foreach $word ( @my_segm_list ) 
{
    my $sel_id = join( "_", "seg_ID", $word ) ;
    my $sel = join( " ", "segm name", $word ) ;
    print "segment list ", $word, "\n" ;
    $selections{$sel_id}{"SELECTION"} = $sel ;
    $selections{$sel_id}{"ATOM"} = "NONE" ;
    $selections{$sel_id}{"BOND"} = "LINE" ;
    $selections{$sel_id}{"RIBBON"} = "NONE" ;
    $selections{$sel_id}{"STYLE"} = "WEIGHT" ;
}


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

my $mw = MainWindow->new;
$mw->title("Tk: $MACRO_FILE");
$mw->packPropagate( 0 ) ;
$mw->geometry( "800x300+800+0" ) ;

my $f = $mw->Frame->pack(-side => 'bottom');
$f->Button(-text => "Quit", 
           -command => sub { exit; })->pack(-side => 'left');
$f->Button(-text => "doit",
           -command => \&create_macro )->pack(-side => 'bottom');
#my $t = $mw->Scrolled("Text", -width => $max_length + 30, 
#                   -wrap => 'none')->pack(-expand => 10, -fill => 'both');

#
#    molecular model data
#
#$frame_file = $mw->Scrolled("Text", asldjfk;l
#my $frame_model = $mw->Scrolled("Text", -width => $max_length + 30, -height => 8, -background => $Win_background, -foreground => $Win_foreground,
my $frame_model = $mw->Scrolled("Text", -width => $max_length + 30, -height => 8, -background => $Win_background, -foreground => $Win_foreground,
                    -wrap => 'none')->pack(-expand => 'x', -fill => 'both', -side => 'top');
$frame_model->configure(-state => 'normal');
$frame_model->Frame( -label =>  " --- MOLECULAR MODEL DATA --- ", -relief => 
		     'flat', -borderwidth => 2)->pack(-side => 'top');
#my $frame_model = $mw ;
$frame_model->insert('end', "\n");
$frame_model->insert('end', "\n");
$frame_model->insert('end', "\n");

generate_selection_frame ( $frame_model ) ;



MainLoop;


sub generate_selection_frame
{
    my ( $frame_segment ) = @_ ;

    my $text ; my $word ; my $I ; my $key ;

    my $w ; my $wl ;
    @all_selections = () ;
    foreach $word ( keys %selections ) { push( @all_selections, $word ) ; }

    if ( $all_selections[@all_selections-1] ne "" ) { push( @all_selections, "" ) ; }
    for ($I=0; $I < @all_selections ; $I++ ) 
    {
	$key = $all_selections[$I] ;
	if ( $key ne "" ) 
	{
	    $selection_list[$I]{"NAME"} = $selections{$key}{"NAME"} ;
	    $selection_list[$I]{"SELECTION"} = $selections{$key}{"SELECTION"} ;
	    $selection_list[$I]{"ATOM"} = $selections{$key}{"ATOM"} ;
	    $selection_list[$I]{"BOND"} = $selections{$key}{"BOND"} ;
	}
	else
	{
	    $selection_list[$I]{"NAME"} = "" ; 
	    $selection_list[$I]{"SELECTION"} = "" ; 
	    $selection_list[$I]{"ATOM"} = "" ;
	    $selection_list[$I]{"BOND"} = "" ;
	}
#	print $key, " kind: ", $window_list[$I]{"GROUP"}, " label item \n" ;
	
	$w = $frame_segment->Entry(-textvariable => \$all_selections[$I], -justify => 'left', -relief => 'groove', 
				   -background => $Win_background, -foreground => $Win_foreground, 
				   -width => $max_length , -borderwidth => 4 )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_NAME"} = $w ;
	$w->bind("<Return>", [\&hit_select_return,  $I, $frame_segment ] ) ;
	$frame_segment->windowCreate('end', -window => $w);


	$w = $frame_segment->Entry(-textvariable => \$selection_list[$I]{"SELECTION"}, -justify => 'left', -relief => 'groove', 
				   -background => $Win_background, -foreground => $Win_foreground, 
				   -width => $max_length + 20 , -borderwidth => 4 )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_SEL"} = $w ;
	$frame_segment->windowCreate('end', -window => $w);


#	widgets_segm{$key}{"SEG_ID"} = $w ;
#	print " SEGM list group", $window_list[$I]{"GROUP"}, " x ", join(" - ", @{$image_cmds{"ATOM"}} ), "\n" ;
	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground,
				-textvariable => \$selection_list[$I]{"ATOM"}, 
				-variable => \$selection_list[$I]{"ATOM"}, 
					 -options => [ @{$image_cmds{"ATOM"}} ]
	    )->pack();
	$window_list[$I]{"WINDOW_ATOM"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"BOND"}, 
		       -variable => \$selection_list[$I]{"BOND"}, 
		       -options => [ @{$image_cmds{"BOND"}} ]
 	    );
	$window_list[$I]{"WINDOW_BOND"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"RIBBON"}, 
		       -variable => \$selection_list[$I]{"RIBBON"}, 
		       -options => [ @{$image_cmds{"RIBBON"}} ]
 	    );
	$window_list[$I]{"WINDOW_RIBBON"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"STYLE"}, 
		       -variable => \$selection_list[$I]{"STYLE"}, 
		       -options => [ @{$image_cmds{"STYLE"}} ]
 	    );
	$window_list[$I]{"WINDOW_STYLE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


	$frame_segment->insert('end', "\n");
    }

}

sub hit_select_return
{
    my ($e, $II, $frame_segment ) = @_ ;
#    my (%z) = @_ ;
    my $w = $e->get ;
    my $wl ;
    print " Hit_return> entry {", $w, " number: ", $II, "\n" ;
    $all_selections[$II] = uc($w) ;
#    $all_selections[$II] = $II ;
#    $window_list[$I]{"GROUP"} = $II ;
     my @new_list ;
 
    my $k ; my $nn = @all_selections ; my $j=0 ;
    for ($k = 0; $k < $nn ; $k++ )
    {
#	print " in hit $k all_selections{", $all_selections[$k], 	 "}" ;
	if ( length($all_selections[$k]) > 0 ) 
	    
#	{ push ( @new_list, $all_selections[$k] ) ; print "PUSH print {", $all_selections[$k], "}\n" ; }
	{ 
	    $all_selections[$j] = $all_selections[$k]; 
	    $window_list[$j]{"WINDOW_NAME"} = $window_list[$k]{"WINDOW_NAME"} ;
	    $window_list[$j]{"WINDOW_SEL"} = $window_list[$k]{"WINDOW_SEL"} ;
	    $window_list[$j]{"WINDOW_BOND"} = $window_list[$k]{"WINDOW_BOND"} ;
	    $window_list[$j]{"WINDOW_ATOM"} = $window_list[$k]{"WINDOW_ATOM"} ;
	    $window_list[$j]{"WINDOW_RIBBON"} = $window_list[$k]{"WINDOW_RIBBON"} ;
	    $window_list[$j]{"WINDOW_STYLE"} = $window_list[$k]{"WINDOW_STYLE"} ;
	    $selection_list[$j]{"NAME"} = $selection_list[$k]{"NAME"} ;
	    $selection_list[$j]{"SELECTION"} = $selection_list[$k]{"SELECTION"} ;
	    $selection_list[$j]{"ATOM"} = $selection_list[$k]{"ATOM"} ;
	    $selection_list[$j]{"BOND"} = $selection_list[$k]{"BOND"} ;
	    $selection_list[$j]{"RIBBON"} = $selection_list[$k]{"RIBBON"} ;
	    $selection_list[$j]{"STYLE"} = $selection_list[$k]{"STYLE"} ;
#	    print "PUSH $j from $k {", $all_selections[$k], "}\n" ; 
	    $j++ ; 
	}
	else
	{
#	    print " NO PUSH $j from $k {", $all_selections[$k], "}\n" ; 

	}
    }
#    push(@all_selections, "" ) ; 
    $all_selections[$j] = "" ; $j++ ;
    if ( $j > $nn ) 
    {

	print " $j >> $nn \n" ;
	my $I = $j-1 ;
	my $key = $all_selections[$I] ;
	$wl = $frame_segment->Entry(-textvariable => \$all_selections[$I], -justify => 'left', -relief => 'groove', 
				   -background => $Win_background, -foreground => $Win_foreground, 
				   -width => $max_length )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_NAME"} = $wl ;
	$wl->bind("<Return>", [\&hit_select_return,  $I, $frame_segment ] ) ;
	$frame_segment->windowCreate('end', -window => $wl);

	$w = $frame_segment->Entry(-textvariable => \$selection_list[$I]{"SELECTION"}, -justify => 'left', -relief => 'groove', 
				   -background => $Win_background, -foreground => $Win_foreground, 
				   -width => $max_length + 20 , -borderwidth => 4 )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_SEL"} = $w ;
	$frame_segment->windowCreate('end', -window => $w);

	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground,
				-textvariable => \$selection_list[$I]{"ATOM"}, 
				-variable => \$selection_list[$I]{"ATOM"}, 
				-options => [ @{$image_cmds{"ATOM"}} ]
	    )->pack();
	$window_list[$I]{"WINDOW_ATOM"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"BOND"}, 
		       -variable => \$selection_list[$I]{"BOND"}, 
		       -options => [ @{$image_cmds{"BOND"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_BOND"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"RIBBON"}, 
		       -variable => \$selection_list[$I]{"RIBBON"}, 
		       -options => [ @{$image_cmds{"RIBBON"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_RIBBON"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"STYLE"}, 
		       -variable => \$selection_list[$I]{"STYLE"}, 
		       -options => [ @{$image_cmds{"STYLE"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_STYLE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


	$frame_segment->insert('end', "\n");

	if ( $II != $nn) { $window_list[$II+1]{"WINDOW_NAME"}->focus() ; }
 
    }
    elsif ( $j < $nn )
    {

	print " $j << $nn \n" ;

	$w = $window_list[$nn-1]{"WINDOW_STYLE"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_RIBBON"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_BOND"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_ATOM"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_SEL"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_NAME"} ; $w->destroy() if Tk::Exists($w) ;

	my $start_line = join (".", $nn+1, "7" ) ;
	$frame_segment->delete($start_line, 'end');
	$frame_segment->insert('end', "\n");
 	pop ( @all_selections ) ; pop ( @all_selections ) ; 
	push ( @all_selections, "" ) ;

	my $I = @all_selections - 1 ;
	print "I $I \n" ;
	    $selection_list[$I]{"NAME"} = "" ; 
	    $selection_list[$I]{"SELECTION"} = "" ; 
	    $selection_list[$I]{"ATOM"} = "NONE" ;
	    $selection_list[$I]{"BOND"} = "LINE" ;
	    $selection_list[$I]{"RIBBON"} = "NONE" ;
	    $selection_list[$I]{"STYLE"} = "AUTO" ;
	my $key = $all_selections[$I] ;
	$wl = $frame_segment->Entry(-textvariable => \$all_selections[$I], -justify => 'left', -relief => 'groove', 
				   -background => $Win_background, -foreground => $Win_foreground, 
				   -width => $max_length )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_NAME"} = $wl ;
	$wl->bind("<Return>", [\&hit_select_return,  $I, $frame_segment ] ) ;
	$frame_segment->windowCreate('end', -window => $wl);

	$w = $frame_segment->Entry(-textvariable => \$selection_list[$I]{"SELECTION"}, -justify => 'left', -relief => 'groove', 
				   -background => $Win_background, -foreground => $Win_foreground, 
				   -width => $max_length + 20 , -borderwidth => 4 )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_SEL"} = $w ;
	$frame_segment->windowCreate('end', -window => $w);

	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground,
				-textvariable => \$selection_list[$I]{"ATOM"}, 
				-variable => \$selection_list[$I]{"ATOM"}, 
				-options => [ @{$image_cmds{"ATOM"}} ]
	    )->pack();

	$window_list[$I]{"WINDOW_ATOM"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"BOND"}, 
		       -variable => \$selection_list[$I]{"BOND"}, 
		       -options => [ @{$image_cmds{"BOND"}} ]
 	    );
	$window_list[$I]{"WINDOW_BOND"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"RIBBON"}, 
		       -variable => \$selection_list[$I]{"RIBBON"}, 
		       -options => [ @{$image_cmds{"RIBBON"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_RIBBON"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => 7, -background => $Win_background, -foreground => $Win_foreground  ,
		       -textvariable => \$selection_list[$I]{"STYLE"}, 
		       -variable => \$selection_list[$I]{"STYLE"}, 
		       -options => [ @{$image_cmds{"STYLE"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_STYLE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


	$frame_segment->insert('end', "\n");

	$window_list[$I]{"WINDOW_NAME"}->focus() ; 



#	print " new ", scalar(@all_selections)," \n" ;
    }
    else
    {
#	print " ELSE $j == $nn \n" ;
	if ( $II+1 != $nn) { $window_list[$II+1]{"WINDOW_NAME"}->focus() ; }
    }

#    print " Hit_return> array {", @_, "\n" ;
}



sub create_macro
{
    my $I ; my $key ;
    my %sl_keys ;
    for ($I=0; $I < @all_selections ; $I++ ) 
    {
	$key = $all_selections[$I] ;
	if ( $key ne "" ) 
	{
	    $selections{$key}{"NAME"} = $selection_list[$I]{"NAME"} ;
	    $selections{$key}{"SELECTION"} = $selection_list[$I]{"SELECTION"} ;
	    $selections{$key}{"ATOM"} = $selection_list[$I]{"ATOM"} ;
	    $selections{$key}{"BOND"} = $selection_list[$I]{"BOND"} ;
	    $selections{$key}{"RIBBON"} = $selection_list[$I]{"RIBBON"} ;
	    $selections{$key}{"STYLE"} = $selection_list[$I]{"STYLE"} ;
	    $sl_keys{$key} = "DONE" ;
	}
    }

    foreach $key (keys %selections ) 
    {
	if ( !exists($sl_keys{$key} ) ) 
	{
	    delete $selections{$key} ;
	}
    }


my $ICOLOR = ${$my_main{"IMAGE_COLOR_START"}}[0] ;
my $ICOLOR_INCREM = ${$my_main{"IMAGE_COLOR_INCR"}}[0];
my $sel_id ;
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" ;

print MACRO << "END_OF_TEXT" ;
subroutine char SEGMENTS
  image erase
delete atom sele segm name #* end
key image sele .not all end

END_OF_TEXT

foreach $sel_id ( keys %selections )
{

#    print "sel ID>", 
#    $sel_id, " atom:", $selections{$sel_id}{"ATOM"}, 
#    " bond: ", $selections{$sel_id}{"BOND"}, "<\n" ;

    if ( $sel_id eq "" ) { next ; }
    print MACRO 
	"\n image group name ", $sel_id, "\n" ;
    print MACRO 
	"key display select ", $selections{$sel_id}{"SELECTION"}, " end\n" ;
    print MACRO 
	"set color \n",
	"sele display end = $ICOLOR\n" ;


# style = ATOM WEIGHTS
if ( $selections{$sel_id}{"STYLE"} eq "AUTO" )
{
    print MACRO
	" sele display end = $ICOLOR\n" ;
}

# style = ATOM WEIGHTS
if ( $selections{$sel_id}{"STYLE"} eq "WEIGHTS" )
{
    print MACRO
	" sele display .a weigh 0.02 1.1 end = $ICOLOR\n",
	" sele display .a \\ \n", 
	"     atom name H* .a by bond weigh 0.02 1.1 end = $ICOLOR\n",
	" sele display .a .n weigh 0.02 1.1 end = 200\n",
	" sele display .a \\ \n", 
	"     atom name H* .a .n by bond weigh 0.02 1.1 end = 200\n" ;
}

# style = ATOM TYPES
if ( $selections{$sel_id}{"STYLE"} eq "TYPES" )
{
    print MACRO << "END_OF_TEXT" ;
sele display end default
sele display .a atom name C* end = $ICOLOR
END_OF_TEXT
}

# style  N2C

if ( $selections{$sel_id}{"STYLE"} eq "N2C" )
{
    print MACRO 
	" sele display end residue 128 255 6 \n" ;
}

# style  N2C

if ( $selections{$sel_id}{"STYLE"} eq "SECONDARY" )
{
    print MACRO << "END_OF_TEXT" ;
 sele display .a sec alpha end col 110
 sele display .a sec beta end col 220
END_OF_TEXT
}



if ( $selections{$sel_id}{"STYLE"} eq "RESIDUE" )
{
    print MACRO << "END_OF_TEXT" ;

sele display  .a resi name GLY ILE VAL LEU ALA PRO MET end = 32
sele display  .a resi name ASP GLU end = 110
sele display  .a resi name LYS ARG end = 240
sele display  .a resi name TYR PHE TRP HIS* end = 200
sele display  .a resi name SER THR ASN GLN end = 220
sele display  .a resi name CYS end = 160
END_OF_TEXT
}

print MACRO 
    "exit\n\n" ;


# CHAINS

if ( $selections{$sel_id}{"RIBBON"} eq "STICK" )
{
    print MACRO << "END_OF_TEXT" ;

key xx_active sele active end
key active sele display end
 <?MAIN_CMDS:image_color_set.cmds
 <?MAIN_CMDS_NEW:image_do_rib_stick.cmds 
key active sele xx_active end
key xx_active drop

END_OF_TEXT
}
elsif ( $selections{$sel_id}{"RIBBON"} eq "LINE" )
{
    print MACRO << "END_OF_TEXT" ;

key xx_active sele active end
key active sele display end
 <?MAIN_CMDS:image_color_set.cmds
 <?MAIN_CMDS_NEW:image_do_rib_line.cmds 
key active sele xx_active end
key xx_active drop

END_OF_TEXT
}

# ATOMS

if ( $selections{$sel_id}{"ATOM"} eq "BALL" )
{
    print MACRO << "END_OF_TEXT" ;
image sele display end set atom ball

END_OF_TEXT
}
elsif ( @{$my_main{"IMAGE_ATOM_STYLE"}}[0] eq "NAME" )
{
    print MACRO << "END_OF_TEXT" ;

image sele display end set atom name

END_OF_TEXT
}
elsif ( $selections{$sel_id}{"ATOM"} eq "CLASS" )
{
    print MACRO << "END_OF_TEXT" ;
image sele display end set atom class

END_OF_TEXT
}
elsif ( $selections{$sel_id}{"ATOM"} eq "CROSS" )
{
    print MACRO << "END_OF_TEXT" ;
image sele display end set atom cross

END_OF_TEXT
}

# BONDS
if ( $selections{$sel_id}{"BOND"} eq "LINE" )
{ 
    print MACRO << "END_OF_TEXT" ;
image sele display end set bond line
image sele display .a .n by bond display end set atom cros
image sele display .a .n by bond all .a .n atom name O* end set atom ball
END_OF_TEXT
}
elsif ( $selections{$sel_id}{"BOND"} eq "STICK" )
{ 
    print MACRO << "END_OF_TEXT" ;
image sele display end set bond stick
image sele display .a .n by bond display end set atom cros
image sele display .a .n by bond all .a .n atom name O* end set atom ball

END_OF_TEXT
} 

$ICOLOR = $ICOLOR + $ICOLOR_INCREM ;

}
print MACRO << "END_OF_TEXT" ;

  image group name MISCELLANEOUS
!  image sele display .a .not by bond all end atom cros
  image sele segm name WORK_SEGM end col 85 hbond
  image sele t_anchor end col 200 atom cros
  image sele t_anchor .o by pair t_anchor end col 200 pair 

END_OF_TEXT

print MACRO << "END_OF_TEXT" ;

key display drop
return

END_OF_TEXT

close(MACRO) ;
#exit;
}
