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

my $TOPO_HOME = $ENV{"MAIN"} . "top/" ;
my $PURY_HOME = $ENV{"PURY"};
my $PURY_EXE = $ENV{"PURY_EXE"};
my $local_run = $PURY_HOME."server/serverTopo.pl";
#my $local_run = $PURY_HOME."server/prepare_PURY_topology.pl";
my $http_run = $PURY_HOME."server/prepare_topology.pl";

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 ) ; 
my %new_main = () ;

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

#print ${$my_main{"MY_COMMANDS"}}[0], "\n" ;
if ( !exists($my_main{"MY_COMMANDS"} ) ) { @{$my_main{"MY_COMMANDS"}} = ("?MAIN_UTILS:my_commands.com" ) };
if ( !exists($my_main{"DISPLAY_SIZE"} ) ) { push ( @{$my_main{"DISPLAY_SIZE"}}, "" ) };
if ( !exists($my_main{"PDB_CRYST"} ) ) { push ( @{$my_main{"PDB_CRYST"}}, "USE" ) };
if ( !exists($my_main{"IMAGE_HELIX"} ) ) { @{$my_main{"IMAGE_HELIX"}} = ( "" ) ; }
if ( !exists($my_main{"IMAGE_BETA"} ) )  { @{$my_main{"IMAGE_BETA"}} = ( "" ) ; }
if ( !exists($my_main{"DEPP_ENV"} ) ) 
     { push ( @{$my_main{"DEPP_ENV"}}, "NEW" ) };

my $MACRO_FILE = ".main" ;
my @PDB_FILE = () ;
my @CIF_FILE = () ;
my $INPUT_FILE = "read.com" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my $DEPP_PAGES = $my_main{"DEPP_ENV"}[0] ;
my $DISPLAY = join ( " ", @{$my_main{"DISPLAY_SIZE"}} ) ;
my $GROUPS_DEF = "SEGMENTS" ;
my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
#    print " $keyword \n" ;
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help"   ) { goto HELP }
    if ( $keyword eq 'c' || substr($keyword, 0,7) eq "command" ) { goto MY_COMMANDS }
    if ( $keyword eq 'i' || substr($keyword, 0,5) eq "input" ) { goto INPUT }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'g' || substr($keyword, 0,6) eq "groups" ) { goto GROUPS }
    if ( $keyword eq 'f' || substr($keyword, 0,3) eq "cif" ) { goto CIF }
    if ( $keyword eq 'p' || substr($keyword, 0,3) eq "pdb" ) { goto PDB }
    if ( $keyword eq 'd' || substr($keyword, 0,4) eq "depp" ) { goto DEPP }
    if ( $keyword eq 's' || substr($keyword, 0,7) eq "display" ) { goto DISPLAY }
    if ( $keyword eq 'x' || substr($keyword, 0,7) eq "crystal" ) { goto CRYSTAL }
    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
  -c|--command)  define setup macro for my tweaks MY COMMANDS [@{$my_main{"MY_COMMANDS"}}]
  -i|--input)    set file name of the data input macro [$INPUT_FILE]
  -g|--groups)   define NCS groups by [NONE/CHAINS/SEGMENTS] [$GROUPS_DEF]
  -o|--output)   set file name of the created macro [$MACRO_FILE] 
  -p|--pdb)      extract information from a PDB file [@PDB_FILE]
  -f|--cif)      extract information from a CIF file [@CIF_FILE]
  -x|--crystal)  PDB CRYSTAL record use [USE/IGNORE] [$my_main{"PDB_CRYST"}[0]]
  -d|--depp)     depp pages [NEW/OLD] menu [$DEPP_PAGES]
  -s|--display)  display position and sizes [0 0 1280 1024] menu [$DISPLAY]
     --doit)     create the macro
END_OF_TEXT
    exit ;
 OUTPUT:  $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
 INPUT:  $INPUT_FILE = shift ( @{$commands{$keyword}} ) ; next ;
 MY_COMMANDS: @{$my_main{"MY_COMMANDS"}} = shift ( @{$commands{$keyword}} ) ; next ;
 GROUPS:  $GROUPS_DEF = shift (@{$commands{$keyword}} ) ; next ;
 PDB:  @PDB_FILE = @{$commands{$keyword}} ; next ;
 CIF:  @CIF_FILE = @{$commands{$keyword}} ; next ;
 DEPP:  @{$my_main{"DEPP_ENV"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
 DISPLAY:  @{$my_main{"DISPLAY_SIZE"}} = ( join(" ", @{$commands{$keyword}} )) ; next ;
 CRYSTAL:  @{$my_main{"PDB_CRYST"}} = ( join(" ", @{$commands{$keyword}} )) ; next ;

}
# print " proc error $PROC_ERROR \n" ;
#print @{$commands{"MY_COMMANDS"}}, " doit  $doit\n" ;

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

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

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 
#print "write segm 76 {", join(" ", @{$my_main{"SG_LIST"}}), "} \n" ;


###  . ${MAIN_CONF}main_config.data.save.sh

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

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

$DEPP_PAGES = $my_main{"DEPP_ENV"}[0] ;
$DISPLAY = $my_main{"DISPLAY_SIZE"}[0] ;


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 "create_auto_start.pl> FILE OPEN ERROR: >$MACRO_FILE< \n" ; 

if ( $DEPP_PAGES eq "NEW" ) 
{    print MACRO "<MAIN_CMDS_NEW:load_depp_page\n" ;}
else
{    print MACRO "<MAIN_CMDS:load_depp_page\n" ;}

if ( $DISPLAY ne "" && $DISPLAY ne " " )
{
    print MACRO "set display {", $DISPLAY, "} \n" ;
}

print MACRO "image init \n" ;
print MACRO "< $INPUT_FILE\n" ;

 if (length( $my_main{"MY_COMMANDS"}[0]) != 0 ) 
{
    my $x_cmds = join(" ", @{$my_main{"MY_COMMANDS"}} ) ;
print MACRO << "END_OF_TEXT" ;
< $x_cmds 
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;
ima  forward
menu page 9 current
return
END_OF_TEXT

close(MACRO) ;

if ( @PDB_FILE le 0 && @CIF_FILE le 0 ) { exit ; }
#######################################

if ( @CIF_FILE gt 0 )
{
    my $my_file = pop ( @CIF_FILE ) ;
    my @cmds = () ;

    push ( @cmds, "read file $my_file atom mmcif \n" ) ;
    push ( @cmds, "write over file SAVE_FILE.PDB atom pdb \n" ) ;
    push ( @cmds, "write over file cell.dat cell \n" ) ;
    push ( @cmds, "quit \n" ) ;
    open ( MACRO,  "> tmp.sh" ) || die "create_auto_start.pl> FILE OPEN ERROR: >tmp.sh< \n" ; 
    print MACRO ( @cmds ) ;
    print (@cmds ) ;
    system ( "mainps_2016.exe_LINUX_GFORT-64 --nostartup < tmp.sh" ) ;
#    system ( "rm tmp.sh" ) ;
    @PDB_FILE = ("SAVE_FILE.PDB") ;
}

my $sub_line ;

my @MY_TOPO_FILES ;
my $FILE ;
if ( !exists($my_main{"FILE_TOPO"}) )
{
    push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_19_csd.main" ) );
    push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_ion.main" ) );
    push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_sme.main" ) );
    push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_dna_rna.main" ) );
}

foreach $FILE ( @{$my_main{"FILE_TOPO"}} )
{
    print "topo loop: $FILE \n";
    if ( uc($FILE) eq "ENGH-HUBER" )
    { 
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_19_csd.main" ) );
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_ion.main" ) );
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_sme.main" ) );
    }
#	if ( uc($FILE) eq "PURY" )
#	{ print MACRO "< ?MAIN_UTILS:get_top_par_PURY\n"  ; }
    if ( uc($FILE) eq "PURY_ALLH" )
    { 
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_allh_pury.main" ) );
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_ion.main" ) );
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_sme.main" ) );
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_dna_rna.main" ) );
    }
#	    print MACRO "< ?MAIN_UTILS:get_top_par_PURY_ALLH\n"  ; }
    if ( uc($FILE) eq "SUGAR" )
    { 
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_sugar.main" ) );
#	    print MACRO "< ?MAIN_UTILS:get_top_par_sugar.com\n"  ; 
    }
    if ( uc($FILE) eq "RCSB_NUC" )
    { 
	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/top_dna_rna.main" ) );
#	push( @MY_TOPO_FILES, join ('', $ENV{"MAIN"}, "top/par_dna_rna.main" ) ) ;
    }
}


my $LIST = join (" ", @PDB_FILE ) ;
if ( @PDB_FILE > 1 ) 
{ system ( "cat $LIST  > SAVE_FILE.PDB" ) ; }
else
{ system ( "cp -p $LIST  SAVE_FILE.PDB" ) ; }

my $PDB = "SAVE_FILE.PDB" ;
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, $ref_helix_list, $ref_beta_list ) = 
    main_config::read_pdb_file ( $PDB ) ;

my @my_sel_list ; my $i ;
my @my_atom_list = @{$ref_atom_list} ;
my @my_seq_list = @{$ref_seq_list} ;

@{$my_main{"IMAGE_HELIX"}} = @{$ref_helix_list} ;
@{$my_main{"IMAGE_BETA"}} = @{$ref_beta_list} ;

my @my_ctab = () ;
my $PDB_CT = "SAVE_FILE.CTAB" ;
if ( -f $PDB_CT ) 
{
    my $ref_ctab = main_config::read_ctab_file ( $PDB_CT );
    my @my_ctab = @{$ref_ctab } ;
}
#print " lenghth: ", $#my_atom_list, " - ", $#my_seq_list, " - ", scalar(@my_seq_list), " - ",  $my_seq_list[$#my_seq_list]{"atom_end"},  "\n" ;

# for ($i=0; $i <= $#my_atom_list; $i++ ) { $my_sel_list[$i] = 1 ; }
# print "my_sel_list: ", $#my_sel_list, "\n" ;
#     main_config::write_pdb_file ( "test.pdb", $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, \@my_sel_list  ) ;
# exit ;

if ( exists ($my_main{"FILE_MY_TOPO"} ) && length ($my_main{"FILE_MY_TOPO"}[0] ) > 0  )
{
    my $FILE ; 
    foreach $FILE ( @{$my_main{"FILE_MY_TOPO"}} )
    { 	if ( lc($FILE) =~ "top" ) { push( @MY_TOPO_FILES, $FILE ) ; }     }
}

my $path = './' ;
opendir( ALLFILES, $path );
my @all_files = readdir( ALLFILES );
closedir( ALLFILES );

foreach $FILE ( sort( @all_files ) )
{     
    if ( $FILE =~ "top" &&  $FILE !~ ".com" ) 
    { 
	push( @MY_TOPO_FILES, $FILE ) ; 
    } 
}

my %file_hash ;
my @list_files = () ;

push ( @list_files, $TOPO_HOME . "top_99_csd.main" ) ;
push ( @list_files, $TOPO_HOME . "top_prot_adds.main" ) ;
push ( @list_files, $TOPO_HOME . "top_sme.main" ) ;
push ( @list_files, $TOPO_HOME . "top_ion.main" ) ;

print join("\n", @list_files ), "\n", ;
foreach $FILE ( @MY_TOPO_FILES )
{
    if ( !exists($file_hash{$FILE} ) )
    {
	if ( $FILE !~ ".com" ) 
	{
	    $file_hash{$FILE} = "DONE" ;
	    push ( @list_files, $FILE ) ;
	    print "my topo files: ", $FILE, "\n" ;
	}
    }
}

my ( $ref_resi_list, $ref_class_list, $ref_topo_lib ) = main_config::read_topology_files ( @list_files ) ;
my %my_topo_resi = %{$ref_resi_list} ;
my @my_class_list = @{$ref_class_list} ;
my %my_topo_lib = %{$ref_topo_lib} ;
my %my_missing_resid ;


#foreach $sub_line (@my_class_list) { print $sub_line, "\n" } ;  
#foreach $sub_line ( sort keys %my_topo_resi) { print $sub_line, "\n" } ;  
#foreach $sub_line ($ref_segm_list) { push(@my_segm_list, @$sub_line) ;  print " TEST segm{", $sub_line, "}\n" ; }

my @my_segm_list = @{$ref_segm_list} ;
my @my_resid_list = @{$ref_resid_list} ;
my @my_sequ_list = @{$ref_seq_list} ;
my @unit_cell = @{$ref_unit_cell} ;
if ( $my_main{"PDB_CRYST"}[0] eq "USE" && scalar ( @{$ref_space_group} ) > 0 )
{ @{$my_main{"FILE_SYMM"}} = @{$ref_space_group} ; }


print " AUTO_START> segm list: ", @my_segm_list , "\n" ;
#print " AUTO_START> unit cell: ", @unit_cell , "\n" ;

my $nn = 0 ;
foreach  $sub_line ( sort ( @my_resid_list ) ) 
{ 
    my $chr4 = $sub_line;
    $sub_line =~ s/ //g ;
    if ( !exists($my_topo_resi{$sub_line} ) ) 
    { 
	$nn++ ; print " missing resid {", $sub_line, "} {", "}\n" ; 
    	$my_missing_resid{$chr4} = "FOUND" ; 
    }
#    else
#    {  print " existin resid {", $sub_line, "} {", "}\n" ;}
}

#$nn = 0 ;
if ( $nn > 0 ) 
{ 
    my $rsds;
    foreach $rsds (keys %my_missing_resid) 
    {
	my @my_sel_list ; my $i ;
	for ($i=0; $i <= $#my_atom_list; $i++ ) { $my_sel_list[$i] = 0 ; }
	print " searching for missin residus:>$rsds<<\n";
	
	my $IRS ;
	my $IATOM_OLD = 0 ;
	for ( $IRS=0; $IRS <= $#my_sequ_list ; $IRS++ )
	{
	    
	    if ( $rsds ne $my_sequ_list[$IRS]{"res_name"} ) { next ; }
	    
	    my $IATOM ;
	    if ( $IRS == 0 ) { $IATOM_OLD =  0 ; } 
	    else { $IATOM_OLD =  $my_sequ_list[$IRS-1]{"atom_end"}+1 ; }
	    print $IRS, " iatom:",  $my_sequ_list[$IRS]{"atom_end"}, "\n" ;
	    for ( $IATOM = $IATOM_OLD; $IATOM <= $my_sequ_list[$IRS]{"atom_end"}; $IATOM++ )
	    { $my_sel_list[$IATOM] = 1 ; }
	    
	    
	    my $chr4 = $rsds;
	    $chr4 =~ s/ //g ;
	    my $file_out = $chr4."_prepare.pdb";
	    
	    main_config::write_pdb_file ( $file_out, $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, \@my_sel_list ) ;
	    
	    if (-e $PURY_EXE) 
	    {
		print "\tfetching topology locally - RESIDUE = $chr4\n - by $local_run \n";
#		system ("$local_run > log.perlfile");
#	     system ("$local_run -f $file_out -r $chr4 > log.perlfile");
		my $program = "$local_run -f $file_out -r $chr4 > log.pury_auto_start_LOCAL" ;
		print " echo $program\n" ;
		system ( $program );
#		system ("$local_run -f $file_out -r $chr4 ");
	    } else {
		print "\tfetching topology from http://pury.ijs.si - RESIDUE = $chr4\n";
		system ("$http_run $file_out $chr4 > pury_auto_start_WWW.log");
	    }
#	    clear_log_files ($chr4);
	    last ;
	}
    }
    
}

#foreach  $sub_line ( sort ( @my_segm_list ) ) { print " segment {", $sub_line, "}\n" ; }
#for  ( $sub_line=0; $sub_line < @my_sequ_list ; $sub_line++) 
#{ 
#    print $sub_line, " seq {", $my_sequ_list[$sub_line]{"seq_id"},  "} res {", $my_sequ_list[$sub_line]{"res_name"}, "}seg {", $my_sequ_list[$sub_line]{"seg_name"},"}\n" ; 
#}


$sub_line = join (" ", @my_segm_list ) ;

#@{$my_main{"SG_LIST"}} = () ;
#foreach  $sub_line ( sort ( @my_segm_list ) ) { print "SUB SEGM $sub_line \n", push ( @{$my_main{"SG_LIST"}}, $sub_line) ; }



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

#print " select by chain :::\n" ;
#print " create_auto_start> before if $GROUPS_DEF segm{", join(" ", @my_segm_list), "}\n" ; 
if ( $GROUPS_DEF eq "CHAINS" ) 
{
    $ref_segm_list = &find_NCS_by_chain (\@my_segm_list, \@my_sequ_list ) ;
    @my_segm_list = @{$ref_segm_list} ;
    for ($i=0; $i <= $#my_atom_list; $i++ ) { $my_sel_list[$i] = 1 ; }
    print "my_sel_list: ", $#my_sel_list, " ", $sub_line, "\n" ;
    main_config::write_pdb_file ( "SAVE_FILE.PDB", $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, \@my_sel_list  ) ;
} 
elsif ( $GROUPS_DEF eq "SEGMENTS" ) 
{
    if ( $#my_segm_list > 0 )
    {
	$ref_segm_list = &find_NCS_by_segm (\@my_segm_list, \@my_sequ_list ) ;
	@my_segm_list = @{$ref_segm_list} ;
    }
}

# my $N_GROUPS = $my_main{"SG_NGROUP"}[0] ; my $igr ;
# for ($igr = 1; $igr <= $N_GROUPS ; $igr++ ) 
# { 
#     my $GROUP= join ( '', "SG_GROUP" , $igr ) ;
#     my $STRATEGY = join ( '', "SG_GROUP" , $igr, "_STRATEGY" ) ;
#     print " $igr, $GROUP, ", $my_main{$STRATEGY}[0], " :: ", join(" ", @{$my_main{$GROUP}} ), "\n" ;
# }


&select_by_segment (@my_segm_list ) ;
$sub_line = join (" ", @{$ref_segm_list} ) ;

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

#@{$my_main{"SG_LIST"}} = split(" ", $sub_line) ;


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

if (  $my_main{"PDB_CRYST"}[0] eq "USE" && scalar ( @unit_cell ) > 0 ) 
{ 
my $FILE = $my_main{"FILE_CELL"}[0] ;
print "creating \"$FILE\"\n" ;
open ( TXT,  "> $FILE" ) || print "create_auto_start.pl> FILE OPEN ERROR: >$FILE< \n" ;
printf TXT "%9.3f %9.3f %9.3f %8.2f %8.2f %8.2f\n", 
    @unit_cell ;
close ( TXT ) ;
$my_main{"PDB_CRYST"}[0] = "IGNORE";
}
#exit ;
my $macro_file ;
#$macro_file  = join ('', $ENV{"MAIN_CONF"}, "create_main_config.pl", " -m ",  $sub_line) ; 
#              system ( "$macro_file" ) ; 
#exit ;
$macro_file  = join ('', $ENV{"MAIN_CONF"}, "create_read.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
# # $macro_file = join ('', $ENV{"MAIN_CONF"}, "create_re_image.pl" ) ; 
# #               system ( "$macro_file --style WEIGHTS --doit" ) ; 
# #               system ( "cp $PDB_FILE SAVE_FILE.PDB" ) ;
main_config::create_re_image ( %my_main ) ;
#print "re image segm auto:", @{$my_main{"SG_LIST"}}, " | ", $sub_line, "\n" ;

exit ;

#my $PDB_FILE = "SAVE_FILE.PDB" ;
#main_config::write_pdb_file ( $PDB_FILE, $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  ) ;



sub clear_log_files {

    my $r = shift;
    my @files = qw (bond.log libcheck.run organic.run charge.tmp data.raw log.backup
log.perlfile);
    my @tmps = qw (.par _new.tmp .pdb _prepare.pdb _prepare.tmp);
    
    foreach (@files) {
	unlink ($_);
    }
    foreach (@tmps) {
	my $file = $r.$_;
	unlink ($file);
    }
    
}

 sub select_by_segment
{
    my @my_segm_list = @_ ;
    my $word ; my $key ;
    my %selections = () ;

    foreach $key (keys %my_main ) 
    { 
	if ( $key =~ "IMAGE_HASH" ) 
	{ 
#	    print " delete $key value = {", @{$my_main{$key}}, "}\n" ;
	    delete $my_main{$key} ; 
	} 
    }


    foreach $word ( @my_segm_list ) 
    {
	my $sel_id = join( "_", "seg", $word ) ;
	my $sel = join( " ", "segm name", $word ) ;
#    print "segment list ", $word, "\n" ;
	$selections{$sel_id}{"NAME"} = "seg" ;
	$selections{$sel_id}{"SELECTION"} = $sel ;
	$selections{$sel_id}{"ONOF"} = "ON" ;
	$selections{$sel_id}{"ATOM"} = "NONE" ;
	$selections{$sel_id}{"BOND"} = "LINE" ;
	$selections{$sel_id}{"RIBBON"} = "NONE" ;
	$selections{$sel_id}{"SURFACE"} = "NONE" ;
	$selections{$sel_id}{"STYLE"} = "WEIGHT" ;
    }
    my $name ;
    foreach $key (keys %selections ) 
    {
	foreach $name (keys %{$selections{$key}} ) 
	{
	    my $word = join( "_", "IMAGE_HASH", $key, $name ) ;
	    @{$my_main{$word}} = ( $selections{$key}{$name} ) ;
#	    print " image selection: $key $name value = {", $selections{$key}{$name}, "}\n" ;
	}
    }
    @{$my_main{"IMAGE_HYDRO"}} = ( "OFF" ) ; 
}

 sub find_NCS_by_chain
{
    my ( $ref_segm_list, $ref_sequ_list ) = @_ ;
    my $word ; my $key ;
    my %selections = () ;
    my @my_segm_list = @{$ref_segm_list} ;
    my @my_sequ_list = @{$ref_sequ_list} ;

my @std_amino_dna_rna = ("ALB ", "ALA ", "ARG ", "CYS ", "ASP ", "ASN ", "GLY ", 
 "GLN ", "GLU ", "HIS ", "ILE ", "LEU ", "LYS ", "MET ", "PHE ", "PRO ",
 "SER ", "TRP ", "THR ", "TYR ", "VAL ", 
 "C   ", "T   ", "A   ", "G   ", "U   ", "BGM ");

    my $chain_old ; 
    $chain_old = join ("|", $my_sequ_list[0]{"chain_id"},  $my_sequ_list[0]{"seg_name"} ); 
    my $chain ; my $ii ;
    my @chain_list ;

    my @chain_ids ;
    push( @chain_ids, $chain_old ) ;
    for ($ii = 0; $ii < scalar( @my_sequ_list ) ; $ii++ )
    {
	$chain = join ("|", $my_sequ_list[$ii]{"chain_id"},  $my_sequ_list[$ii]{"seg_name"} ); 
#	print "chain ids: $chain - $chain_old \n" ;

	if ( $chain ne $chain_old ) 
	{
#	    print " loop: |$chain|$chain_old|$ii\n" ;
	    push( @chain_list, $ii-1 ) ;
	    push( @chain_ids, $chain ) ;
	    $chain_old = $chain ;
	}
    }
    if ( scalar (@chain_ids) == 1 ) { push( @chain_list, 0 ) ; }
    push( @chain_list, $ii-1 ) ;
    push( @chain_ids, $chain ) ;

    my $n_chains = scalar( @chain_ids )-1 ;
    my $i_chain; my $j_chain ; my $jj ; my $N_II ; my $N_JJ ; my $FL_STD ;
    my @CHAIN_CTAB = () ;
    for ($i_chain = 0; $i_chain < $n_chains ; $i_chain++ ) { push( @{$CHAIN_CTAB[$i_chain]}, $i_chain ) ; }
    for ($i_chain = 0; $i_chain < $n_chains-1 ; $i_chain++ )
    {
#	print $i_chain, " chain start: ", $chain_ids[$i_chain], " - ", $chain_list[$i_chain], " \n" ;
	for ($j_chain = $i_chain+1; $j_chain < $n_chains ; $j_chain++ )
	{
#	    print $j_chain, " chain J: ", $chain_ids[$j_chain], " - ", $chain_list[$j_chain], " \n" ;

	    $N_II = 0 ; $N_JJ = 0 ;
	    if ( $i_chain < 1 ) { $ii = 0 ; }
	    else
	    { $ii = $chain_list[$i_chain-1]+1 ;}

#	    if ( $my_sequ_list[$ii]{"res_name"} eq "HOH " || $my_sequ_list[$jj]{"res_name"} eq "HOH " ) { next ; }
	    $FL_STD = 0 ; my $kk ;
	    for ($kk=$ii; $kk <= $chain_list[$i_chain] ; $kk++ )
	    {
		for ($jj=0; $jj < scalar(@std_amino_dna_rna) ; $jj++ )
		{
#		    print  "$kk $jj <", $my_sequ_list[$kk]{"res_name"}, "> = <",  $std_amino_dna_rna[$jj], "> $FL_STD\n" ;
		    if ( $my_sequ_list[$kk]{"res_name"} eq $std_amino_dna_rna[$jj] ) { $FL_STD++ ; last ;}
		}
	    }
#	    print  "-----> $i_chain chain == $FL_STD - before \n" ;
	    if ( $FL_STD == 0 ) { next ; }
#	    print  "-----> $i_chain chain == $FL_STD - after \n" ;
	    $jj = $chain_list[$j_chain-1]+1 ;
	    my $N_MATCH = 0 ;
	    while ( $ii <= $chain_list[$i_chain] && $jj <= $chain_list[$j_chain] )
	    {
		if ( $my_sequ_list[$ii]{"res_name"} eq $my_sequ_list[$jj]{"res_name"} )
		{
		    $N_MATCH++ ;
#		    print $ii, "->", join ("|", $my_sequ_list[$ii]{"res_name"}, $my_sequ_list[$ii]{"chain_id"}, $my_sequ_list[$ii]{"seq_id"}, $my_sequ_list[$ii]{"seg_name"} ), 
#		    $jj, "++", join ("|", $my_sequ_list[$jj]{"res_name"}, $my_sequ_list[$jj]{"seq_id"}, $my_sequ_list[$jj]{"chain_id"}, $my_sequ_list[$jj]{"seg_name"} ), 
#		    "\n " ;
		    $ii++ ; $jj++ ; $N_II++ ; $N_JJ++ ;
		}
		else
		{
		    $jj++ ; $N_JJ++ ;
		}
	    }
#	    print "I-I MATCHING $i_chain - $j_chain -->> $N_MATCH ============ $N_II $N_JJ \n" ;
	    
	    $N_II = 0 ; $N_JJ = 0 ;
	    if ( $i_chain < 1 ) { $ii = 0 ; }
	    else
	    { $ii = $chain_list[$i_chain-1]+1 ;}
	    
	    $jj = $chain_list[$j_chain-1]+1 ;

	    my $NJ_MATCH = 0 ; my $nj_count = 0 ;
	    while ( $ii <= $chain_list[$i_chain] && $jj <= $chain_list[$j_chain] )
	    {
		$nj_count++ ;
		if ( $my_sequ_list[$ii]{"res_name"} eq $my_sequ_list[$jj]{"res_name"} )
		{
		    $NJ_MATCH++ ;
#		    print $ii, "->", join ("|", $my_sequ_list[$ii]{"res_name"}, $my_sequ_list[$ii]{"chain_id"}, $my_sequ_list[$ii]{"seq_id"}, $my_sequ_list[$ii]{"seg_name"} ), 
#		    $jj, "++", join ("|", $my_sequ_list[$jj]{"res_name"}, $my_sequ_list[$jj]{"seq_id"}, $my_sequ_list[$jj]{"chain_id"}, $my_sequ_list[$jj]{"seg_name"} ), 
#		    "\n " ;
		    $ii++ ; $jj++ ; $N_II++ ; $N_JJ++ ;
		}
		else
		{
		    $ii++ ; $N_II++ ;
		}
	    }
#	    print "JJJ MATCHING $i_chain - $j_chain -->> $NJ_MATCH = $N_II $N_JJ \n" ;
	    if ( $NJ_MATCH == $N_MATCH && $NJ_MATCH == $N_JJ && $N_JJ >= $nj_count ) 
	    { 
#		print " find_NCS_by_chain> BINGO: ==========>>>>> $i_chain - $j_chain\n" ; 
		@CHAIN_CTAB = main_config::make_ctab_link ( $i_chain, $j_chain, @CHAIN_CTAB ) ;
	    }

	}
    }
    my $N_GROUPS = 0 ; 
    my @GROUP = () ;
    my @fl_done ;
    for ($i_chain = 0; $i_chain < $n_chains ; $i_chain++ ) 
    { 
	if ( exists($GROUP[$i_chain]) ) { next ; }
	$N_GROUPS++ ;
	

	$fl_done[$i_chain] = "N" ;
	for ($j_chain = 0; $j_chain < scalar(@{$CHAIN_CTAB[$i_chain]}) ; $j_chain++ )
	{
	    $GROUP[${$CHAIN_CTAB[$i_chain]}[$j_chain]]  = $N_GROUPS ;
	    $fl_done[${$CHAIN_CTAB[$i_chain]}[$j_chain]] = "Y" ; 
	}
	if ( scalar(@{$CHAIN_CTAB[$i_chain]}) > 1 ) { $fl_done[$i_chain] = "Y" ; }
    }

#print chain CTAB

    for ($i_chain = 0; $i_chain < $n_chains ; $i_chain++ ) 
    { 
	print "NCS_by_chain>", $i_chain, " > ", $GROUP[$i_chain],  " ", $fl_done[$i_chain],  " ", 
	join( " ", @{$CHAIN_CTAB[$i_chain]} ) , "\n" ;
    }

    my @group_list = () ;

    for ($i_chain = 0; $i_chain < $n_chains ; $i_chain++ ) 
    { push ( @{$group_list[ $GROUP[$i_chain]]},  $i_chain ) ; }


    foreach $key (keys %my_main ) 
    { 
	if ( $key =~ "GROUP" ) 
	{ 
	    print " NCS_by_chain> delete $key value = {", @{$my_main{$key}}, "}\n" ;
	    delete $my_main{$key} ; 
	} 
    }
    my @segs = ("A".."Z") ;
    my @seg_name ;
    my $igr ;
    for ($ii = 1; $ii <= $N_GROUPS ; $ii++ ) 
    { 
	if ( scalar ( @{$group_list[$ii]}) > 1 ) 
	{
	    for ( $igr = 0; $igr < scalar ( @{$group_list[$ii]}) ; $igr++ ) 
	    { 
#		print $ii, " > groups ", join( "_",  $segs[$ii-1], $igr+1 ) , "||\n" ;
		$seg_name[@{$group_list[$ii]}[$igr]] =  join( "_",  $segs[$ii-1], $igr+1 ) ;
	    }
	}
	else
	{
#	    print $ii, " > groups ", $segs[$ii-1] , "|\n" ;
	    $seg_name[@{$group_list[$ii]}[0]] =  $segs[$ii-1] ;
	}
    }
#    print join (", ", @seg_name ), "\n" ;

    for ($i_chain = 0; $i_chain < $n_chains ; $i_chain++ ) 
    { 
	$N_II = 0 ; 
	if ( $i_chain < 1 ) { $ii = 0 ; }
	else
	{ $ii = $chain_list[$i_chain-1]+1 ;}

	my $kk ; 
#	my $chr = sprintf "%4s", $seg_name[$i_chain] ;
	my $chr =  substr( join("", $seg_name[$i_chain], "    " ), 0,4);
	for ($kk=$ii; $kk <= $chain_list[$i_chain] ; $kk++ )
	{
	    $my_sequ_list[$kk]{"seg_name"} = $chr ;
	}
    }
    @{$my_main{"SG_LIST"}} = @seg_name ;
    ${$my_main{"SG_NMOL"}}[0] = scalar (@seg_name ) ;
#    print "sgs: ", ${$my_main{"SG_NMOL"}}[0], " | ", join(" ", @{$my_main{"SG_LIST"}} ), "\n" ;

    for ($igr = 1; $igr <= $N_GROUPS ; $igr++ ) 
    { 
	my $GROUP= join ( '', "SG_GROUP" , $igr ) ;
	my $STRATEGY = join ( '', "SG_GROUP" , $igr, "_STRATEGY" ) ;
	$my_main{$STRATEGY}[0] = "EACH" ;
	@{$my_main{$GROUP}} = () ;
	foreach  my $kk ( @{$group_list[$igr]} )
	{ push ( @{$my_main{$GROUP}}, $seg_name[$kk] ) ; }
	print $igr, "  grp: ", scalar(@{$my_main{$GROUP}}), " | ", join(" ", @{$my_main{$GROUP}} ), "\n" ;
  }
    $my_main{"SG_NGROUP"}[0] = $N_GROUPS ;
    $my_main{"SG_NCRYST"}[0] = 1 ;
    $my_main{"SG_CRYST1"} = () ; 
    $my_main{"SG_CRYST1"}[0] = join ( ' ', @{$my_main{"SG_LIST"}} ) ;
 
    return ( \@seg_name ) ;
#     {
# 	my $chain ;
# 	my @my_chain_list = @{$my_segm_list{$word}} ;
# 	foreach $chain ( @my_chain_list ) 
# 	{
# 	    my $sel_id = join( "_", "seg", $word ) ;
# 	    my $sel = join( " ", "segm name", $word, ".and chain name", $chain ) ;
# #    print "segment list ", $word, "\n" ;
# 	    $selections{$sel_id}{"NAME"} = "seg" ;
# 	    $selections{$sel_id}{"SELECTION"} = $sel ;
# 	    $selections{$sel_id}{"ONOF"} = "ON" ;
# 	    $selections{$sel_id}{"ATOM"} = "NONE" ;
# 	    $selections{$sel_id}{"BOND"} = "LINE" ;
# 	    $selections{$sel_id}{"RIBBON"} = "NONE" ;
# 	    $selections{$sel_id}{"SURFACE"} = "NONE" ;
# 	    $selections{$sel_id}{"STYLE"} = "WEIGHT" ;
# 	}
#     }
#     my $name ;
#     foreach $key (keys %selections ) 
#     {
# 	foreach $name (keys %{$selections{$key}} ) 
# 	{
# 	    my $word = join( "_", "IMAGE_HASH", $key, $name ) ;
# 	    @{$my_main{$word}} = ( $selections{$key}{$name} ) ;
# #	    print " image selection: $key $name value = {", $selections{$key}{$name}, "}\n" ;
# 	}
#     }
#     @{$my_main{"IMAGE_HYDRO"}} = ( "OFF" ) ; 
}


 sub find_NCS_by_segm
{
    my ( $ref_segm_list, $ref_sequ_list ) = @_ ;
    my $word ; my $key ;
    my %selections = () ;
    my @my_segm_list = @{$ref_segm_list} ;
    my @my_sequ_list = @{$ref_sequ_list} ;

    my @std_amino_dna_rna = ("ALB ", "ALA ", "ARG ", "CYS ", "ASP ", "ASN ", "GLY ", 
			     "GLN ", "GLU ", "HIS ", "ILE ", "LEU ", "LYS ", "MET ", "PHE ", "PRO ",
			     "SER ", "TRP ", "THR ", "TYR ", "VAL ", 
			     "C   ", "T   ", "A   ", "G   ", "U   ", 
                             "DA  ", "DC  ", "DG  ", "DT  ", "BGM ");

    print " create_auto_start> find_ncs_by_segm> top segm{", join(" ", @my_segm_list), "}\n" ; 
#    print " create_auto_start> find_ncs_by_segm> top my_main{", join(" ", @{$my_main{"SG_LIST"}}), "}\n" ; 
    my $seg_old ; 
    $seg_old = $my_sequ_list[0]{"seg_name"}; 
    print "seg old:", $seg_old , "\n" ;
#    if ( $seg_old = "" ) { return ; }
    my $segment ; my $ii ;
    my @segm_list ;

    my @segm_ids ;
    push( @segm_ids, $seg_old ) ;
    for ($ii = 0; $ii <= $#my_sequ_list ; $ii++ )
    {
	$segment = $my_sequ_list[$ii]{"seg_name"} ; 
#	print "seg ids: $segment - $seg_old \n" ;

	if ( $segment ne $seg_old ) 
	{
#	    print " loop: |$seg_old|$segment| $ii\n" ;
	    push( @segm_list, $ii-1 ) ;
	    push( @segm_ids, $segment ) ;
	    $seg_old = $segment ;
	}
    }
    if ( $#segm_ids == 1 ) { push( @segm_list, 0 ) ; }
    push( @segm_list, $ii-1 ) ;
    push( @segm_ids, $segment ) ;

    print $#segm_ids, " ", $#segm_list, " ::> ", join("-", @segm_ids ), "<\n";
	    print join(" ", @segm_list ), "\n";


    my $n_segments = $#segm_ids ;
    my $i_chain; my $j_chain ; my $jj ; my $N_II ; my $N_JJ ; my $FL_STD ;
    my @SEGM_CTAB = () ;
    my @fl_std_chain = () ;
    for ($i_chain = 0; $i_chain <= $n_segments ; $i_chain++ ) { push( @{$SEGM_CTAB[$i_chain]}, $i_chain ) ; }
    for ($i_chain = 0; $i_chain <= $n_segments ; $i_chain++ )
#    for ($i_chain = 0; $i_chain <= 1 ; $i_chain++ )
    {
#	print $i_chain, " chain start: ", $segm_ids[$i_chain], " - ", $segm_list[$i_chain], " \n" ;

	if ( $i_chain < 1 ) { $ii = 0 ; }
	else
	{ $ii = $segm_list[$i_chain-1]+1 ;}
	$FL_STD = 0 ; my $kk ; 
	for ($kk=$ii; $kk <= $segm_list[$i_chain] ; $kk++ )
	{
#	    print  "$kk $ii <", $my_sequ_list[$kk]{"res_name"}, "|c:|", $my_sequ_list[$kk]{"chain_id"}, "|q:|", $my_sequ_list[$kk]{"seq_id"}, "|s:|", $my_sequ_list[$kk]{"seg_name"}, "> =", $FL_STD,"\n" ;
	    for ($jj=0; $jj < scalar(@std_amino_dna_rna) ; $jj++ )
	    {
#		    print  "$kk $jj <", $my_sequ_list[$kk]{"res_name"}, "> = <",  $std_amino_dna_rna[$jj], "> $FL_STD\n" ;
		if ( $my_sequ_list[$kk]{"res_name"} eq $std_amino_dna_rna[$jj] ) { $FL_STD++ ; last ;}
	    }
	}
	$fl_std_chain[$i_chain] = $FL_STD ;

#	print  "-----> $i_chain chain =FL STD: $FL_STD \n" ;
#	print  "-----> $i_chain I-chain == $FL_STD - after> ",  $segm_list[$i_chain]-$ii, "\n" ;
#	for ($j_chain = $i_chain+1; $j_chain <= $n_segments ; $j_chain++ )
    }
    for ($i_chain = 0; $i_chain <= $n_segments-1 ; $i_chain++ )
    {
	if ( $fl_std_chain[$i_chain] <= 3 ) { next ; }
	for ($j_chain = $i_chain+1; $j_chain <= $n_segments ; $j_chain++ )
	{
	if ( $fl_std_chain[$j_chain] <= 3 ) { next ; }
#	    print $j_chain, " chain J: ", $segm_ids[$j_chain], " - ", $segm_list[$j_chain], " \n" ;

#	    if ( $my_sequ_list[$ii]{"res_name"} eq "HOH " || $my_sequ_list[$jj]{"res_name"} eq "HOH " ) { next ; }
	    if ( $j_chain < 1 ) { $ii = 0 ; }
	    else
	    { $ii = $segm_list[$j_chain-1]+1 ;}

	    $N_II = 0 ; $N_JJ = 0 ;
	    if ( $i_chain == 0 ) { $ii = 0 ; }
	    else
	    { $ii = $segm_list[$i_chain-1]+1 ;}
	    $jj = $segm_list[$j_chain-1]+1 ;
#	    print " ===========================  matching from: $ii $jj \n" ;
	    my $N_MATCH = 0 ;
	    while ( $ii <= $segm_list[$i_chain] && $jj <= $segm_list[$j_chain] )
	    {
		if ( $my_sequ_list[$ii]{"res_name"} eq "HOH " ) { $ii++ ; next ; }
		if ( $my_sequ_list[$jj]{"res_name"} eq "HOH " ) { $jj++ ; next ; }
		if ( $my_sequ_list[$ii]{"res_name"} eq $my_sequ_list[$jj]{"res_name"} )
		{
		    $N_MATCH++ ;
#		    print $ii, "->", join ("|", $my_sequ_list[$ii]{"res_name"}, $my_sequ_list[$ii]{"chain_id"}, $my_sequ_list[$ii]{"seq_id"}, $my_sequ_list[$ii]{"seg_name"} ), 
#		    $jj, "++", join ("|", $my_sequ_list[$jj]{"res_name"}, $my_sequ_list[$jj]{"seq_id"}, $my_sequ_list[$jj]{"chain_id"}, $my_sequ_list[$jj]{"seg_name"} ), 
#		    "\n " ;
		    $ii++ ; $jj++ ; $N_II++ ; $N_JJ++ ;
		}
		else
		{
#		    print $ii, " ELSE ->", join ("|", $my_sequ_list[$ii]{"res_name"}, $my_sequ_list[$ii]{"chain_id"}, $my_sequ_list[$ii]{"seq_id"}, $my_sequ_list[$ii]{"seg_name"} ), 
#		    $jj, "++", join ("|", $my_sequ_list[$jj]{"res_name"}, $my_sequ_list[$jj]{"seq_id"}, $my_sequ_list[$jj]{"chain_id"}, $my_sequ_list[$jj]{"seg_name"} ), 
#		    "\n " ;
		    $jj++ ; $N_JJ++ ;
		}
	    }
#	    print "I-I MATCHING $i_chain - $j_chain -->> $N_MATCH ============ $N_II $N_JJ \n" ;
	    
	    $N_II = 0 ; $N_JJ = 0 ;
	    
	    $jj = $segm_list[$j_chain-1]+1 ;
	    if ( $i_chain == 0 ) { $ii = 0 ; }
	    else
	    { $ii = $segm_list[$i_chain-1]+1 ;}

	    my $NJ_MATCH = 0 ; my $nj_count = 0 ;
	    while ( $ii <= $segm_list[$i_chain] && $jj <= $segm_list[$j_chain] )
	    {
		$nj_count++ ;
		if ( $my_sequ_list[$ii]{"res_name"} eq $my_sequ_list[$jj]{"res_name"} )
		{
		    $NJ_MATCH++ ;
#		    print $ii, "->", join ("|", $my_sequ_list[$ii]{"res_name"}, $my_sequ_list[$ii]{"chain_id"}, $my_sequ_list[$ii]{"seq_id"}, $my_sequ_list[$ii]{"seg_name"} ), 
#		    $jj, "++", join ("|", $my_sequ_list[$jj]{"res_name"}, $my_sequ_list[$jj]{"seq_id"}, $my_sequ_list[$jj]{"chain_id"}, $my_sequ_list[$jj]{"seg_name"} ), 
#		    "\n " ;
		    $ii++ ; $jj++ ; $N_II++ ; $N_JJ++ ;
		}
		else
		{
		    $ii++ ; $N_II++ ;
		}
	    }
#	    print "JJJ MATCHING $i_chain - $j_chain -->> $NJ_MATCH = $N_II $N_JJ \n" ;
	    if ( $NJ_MATCH == $N_MATCH && $NJ_MATCH == $N_JJ && $N_JJ >= $nj_count && $N_JJ > 3 ) 
	    { 
#		print " find_NCS_by_segm> BINGO: ==========>>> ", 
#		$segm_ids[$i_chain], " - ", $segm_ids[$j_chain], " n_jj: $i_chain $j_chain ::", $N_JJ, "\n" ; 
##		$my_segm_list[$i_chain], " - ", $my_segm_list[$j_chain], " n_jj: $i_chain $j_chain ::", $N_JJ, "\n" ; 
		@SEGM_CTAB = main_config::make_ctab_link ( $i_chain, $j_chain, @SEGM_CTAB ) ;
	    }

	}
    }
#    print " create_auto_start> find_ncs_by_segm> after matching{", join(" ", @my_segm_list), "}\n" ; 
#    exit ;
    my $N_GROUPS = 0 ; 
    my @GROUP = () ;
    my %segment_group = () ;
    for ($i_chain = 0; $i_chain <= $n_segments ; $i_chain++ ) 
    { 
	$segment = $segm_ids[$i_chain] ;
	
	if ( exists($segment_group{$segment}) ) {  next ; }
#	print "ctab:", join (" ", @{$SEGM_CTAB[$i_chain]} ), "\n" ;
	$N_GROUPS++ ;
#	print "$N_GROUPS = NEW GROUP>", $segment, "\n" ;
	$segment_group{$segment}  = $N_GROUPS ; 


	foreach $j_chain (@{$SEGM_CTAB[$i_chain]}) 
	{

	    $segment_group{$segm_ids[$j_chain]}  = $N_GROUPS ; 
#	    print "$N_GROUPS = ADDS GROUP>", $segm_ids[$j_chain], "\n" ;
	}
    }

#    print "chain CTAB\n" ;
#
#    for ($i_chain = 0; $i_chain < $n_segments ; $i_chain++ ) 
#    { 
#	print $i_chain, " > ", $segment_group{$segm_ids[$i_chain]},  " :: ", 
#	join( " ", @{$SEGM_CTAB[$i_chain]} ) , "\n" ;
#    }
##
#    my @group_list = () ;
#
#    for ($i_chain = 0; $i_chain < $n_segments ; $i_chain++ ) 
#    { push ( @{$group_list[ $segment_group{$segm_ids[$i_chain]}]},  $i_chain ) ; }


    foreach $key (keys %my_main ) 
    { 
	if ( $key =~ "GROUP" ) 
	{ 
#	    print " delete $key value = {", @{$my_main{$key}}, "}\n" ;
	    delete $my_main{$key} ; 
	} 
    }

    my @seg_name ;
    my $igr ;
    my $GROUP;
    @seg_name =  @my_segm_list ;
    my $nn = 0 ;
    foreach $segment ( @seg_name )
    {
	if ( !exists($segment_group{$segment} ) ) { $nn++ ; }
	print " $nn  segm group $segment \n" ;
    }
#    print " nn: $nn  groups: $N_GROUPS $#seg_name \n" ;
    if ( $nn == 0 ) 
    {
	for ($igr = 1; $igr <= $N_GROUPS ; $igr++ ) 
	{ 
	    my $GROUP= join ( '', "SG_GROUP" , $igr ) ;
	    my $STRATEGY = join ( '', "SG_GROUP" , $igr, "_STRATEGY" ) ;
	    $my_main{$STRATEGY}[0] = "EACH" ;
	    @{$my_main{$GROUP}} = () ;
	}

	foreach $segment ( @seg_name )
	{
	    $igr = $segment_group{$segment} ;
	    $GROUP= join ( '', "SG_GROUP" , $igr ) ;
	    push ( @{$my_main{$GROUP}}, $segment ) ; 
#	    print "$igr  $GROUP  grp: ", $#{$my_main{$GROUP}}, " | ", join(" ", @{$my_main{$GROUP}} ), "\n" ;
	}
    }
    else
    {
	$N_GROUPS = $#seg_name ;
	for ($igr = 1; $igr <= $N_GROUPS ; $igr++ ) 
	{ 
	    my $GROUP= join ( '', "SG_GROUP" , $igr ) ;
	    my $STRATEGY = join ( '', "SG_GROUP" , $igr, "_STRATEGY" ) ;
	    $my_main{$STRATEGY}[0] = "EACH" ;
	    @{$my_main{$GROUP}} = () ;
	}
	$igr = 0 ;
	foreach $segment ( sort ( @seg_name ) )
	{
	    $igr++ ;
	    my $GROUP= join ( '', "SG_GROUP" , $igr ) ;
	    push ( @{$my_main{$GROUP}}, $segment ) ; 
# 	print $igr, $GROUP, "  grp: ", scalar(@{$my_main{$GROUP}}), " | ", join(" ", @{$my_main{$GROUP}} ), "\n" ;
	}
    }

    @{$my_main{"SG_LIST"}} = @seg_name ;
    ${$my_main{"SG_NMOL"}}[0] = scalar (@seg_name ) ;
    $my_main{"SG_NGROUP"}[0] = $N_GROUPS ;
    $my_main{"SG_NCRYST"}[0] = 1 ;
    $my_main{"SG_CRYST1"} = () ; 
    $my_main{"SG_CRYST1"}[0] = join ( ' ', @{$my_main{"SG_LIST"}} ) ;
    
    return ( \@seg_name ) ;
#     {
# 	my $chain ;
# 	my @my_segm_list = @{$my_segm_list{$word}} ;
# 	foreach $chain ( @my_segm_list ) 
# 	{
# 	    my $sel_id = join( "_", "seg", $word ) ;
# 	    my $sel = join( " ", "segm name", $word, ".and chain name", $chain ) ;
# #    print "segment list ", $word, "\n" ;
# 	    $selections{$sel_id}{"NAME"} = "seg" ;
# 	    $selections{$sel_id}{"SELECTION"} = $sel ;
# 	    $selections{$sel_id}{"ONOF"} = "ON" ;
# 	    $selections{$sel_id}{"ATOM"} = "NONE" ;
# 	    $selections{$sel_id}{"BOND"} = "LINE" ;
# 	    $selections{$sel_id}{"RIBBON"} = "NONE" ;
# 	    $selections{$sel_id}{"SURFACE"} = "NONE" ;
# 	    $selections{$sel_id}{"STYLE"} = "WEIGHT" ;
# 	}
#     }
#     my $name ;
#     foreach $key (keys %selections ) 
#     {
# 	foreach $name (keys %{$selections{$key}} ) 
# 	{
# 	    my $word = join( "_", "IMAGE_HASH", $key, $name ) ;
# 	    @{$my_main{$word}} = ( $selections{$key}{$name} ) ;
# #	    print " image selection: $key $name value = {", $selections{$key}{$name}, "}\n" ;
# 	}
#     }
#     @{$my_main{"IMAGE_HYDRO"}} = ( "OFF" ) ; 
}



