#!/usr/bin/perl
# Simple Approach for Ranking Structure Determining Residues
open (ARCH, "100_estructuras.pdb") or die "Cannot open file \n";

$Model = 1;

while ($linea = <ARCH>) 
{
	if ($linea =~ "^MODEL")
		{
		$Num_res = 0;
		$Comparar_num_res = 0;
		$Continuidad_conteo = 0;
		do	{
				shift (@baseline_molecule); 
				$vaciar_push = @baseline_molecule;
			} until  ($vaciar_push == 0);
		do
			{
				shift (@baseline_VM_molecule); 
				$vaciar_push = @baseline_VM_molecule;
			} until  ($vaciar_push == 0);
		do	{
				shift (@baseline_residue); 
				$vaciar_push = @baseline_residue;
			} until  ($vaciar_push == 0);	
		do	{
				shift (@baseline_VM_residue); 
				$vaciar_push = @baseline_VM_residue;
			} until  ($vaciar_push == 0);
		do	{
				shift (@Num_res); 
				$vaciar_push = @Num_res;
			} until  ($vaciar_push == 0);
		$Archsal = "Model_$Model.pl";
		open (ARCHSAL, ">$Archsal")||die "Cannot open file \n";
		print ARCHSAL "#Model_$Model\n";
		$baseline_molecule = "molecule = (";
		@baseline_molecule = split ('', $baseline_molecule);
		unshift (@baseline_molecule, "@");
		$baseline_PM_molecule = "PM_molecule = (";
		@baseline_PM_molecule = split ('', $baseline_PM_molecule);
		unshift (@baseline_PM_molecule, "@");
		$baseline_VM_molecule = "VM_molecule = (";
		@baseline_VM_molecule = split ('', $baseline_VM_molecule);
		unshift (@baseline_VM_molecule, "@");
		}
	
	if ($linea =~ "^TER")
		{
		$Continuidad_conteo = $Num_res;
		}
	
	if ($linea =~ "^ENDMDL")
		{
		pop @baseline_residue;
		push (@baseline_residue, ");\n");
		$Temporal_3 = join ('',@baseline_residue);
		print ARCHSAL "$Temporal_3";
		pop @baseline_PM_residue;
		push (@baseline_PM_residue, ");\n");
		$Temporal_33 = join ('',@baseline_PM_residue);
		print ARCHSAL "$Temporal_33";
		push (@baseline_VM_residue, ";\n");
		$Temporal_33 = join ('',@baseline_VM_residue);
		print ARCHSAL "$Temporal_33";
		$n=1;
		do	{
			print ARCHSAL '$count_atm_res_[',($n-1),"] = ",($count_atm_res_[$n]-1), ";\n" ;
			$n++;
			} until ($n> $Num_res);
		$n= 0;
		do	{
			$count_atm_res_[$n] = 0;
			$n++;
			} until ($n> $Num_res);
		pop @baseline_molecule;
		push (@baseline_molecule, ");\n");
		$Temporal_4 = join ('',@baseline_molecule);
		print ARCHSAL "$Temporal_4";
		pop @baseline_PM_molecule;
		push (@baseline_PM_molecule, ");\n");
		$Temporal_44 = join ('',@baseline_PM_molecule);
		print ARCHSAL "$Temporal_44";
		pop @baseline_VM_molecule;
		push (@baseline_VM_molecule, ");\n");
		$Temporal_44 = join ('',@baseline_VM_molecule);
		print ARCHSAL "$Temporal_44";
		print ARCHSAL '$',"Model = $Model;\n";
		$matrix_salida = "matrix_center_mass_$Model.csv";
		@matrix_salida = split ('', $matrix_salida);
		unshift (@matrix_salida, '>');
		unshift (@matrix_salida, '"');
		unshift (@matrix_salida, ',');
		unshift (@matrix_salida, "'");
		unshift (@matrix_salida, '"');
		unshift (@matrix_salida, "'");
		unshift (@matrix_salida, ',');
		unshift (@matrix_salida, '"');
		push (@matrix_salida, '"');
		push (@matrix_salida, ',');
		push (@matrix_salida, "'");
		push (@matrix_salida, '"');
		push (@matrix_salida, "'");
		push (@matrix_salida, ',');
		push (@matrix_salida, '"');
		$matrix_salida = join ('',@matrix_salida);
		print ARCHSAL '$Num_res = ',($Num_res-1),";\n";
		print ARCHSAL "\n";
		print ARCHSAL "open (ARCHSAL, ",'"',">center_mass_model_$Model.pl",'"',")||die ",'"',"Cannot open file ",'\\',"n",'"',";\n";
		print ARCHSAL '$',"j= 0;\n";
		print ARCHSAL "	do\n";
		print ARCHSAL "	{\n";
		print ARCHSAL '		$',"SMX = 0; ",'$',"SMY = 0; ",'$',"SMZ = 0; ",'$',"SMT = 0;\n";
		print ARCHSAL '		$',"k= 0;\n";
		print ARCHSAL "		do\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"SMX= ",'$',"SMX+(",'$',"PM_molecule[",'$',"j][",'$',"k]*",'$',"molecule[",'$',"j][",'$',"k][0]);\n";
		print ARCHSAL '			$',"SMY= ",'$',"SMY+(",'$',"PM_molecule[",'$',"j][",'$',"k]*",'$',"molecule[",'$',"j][",'$',"k][1]);\n";
		print ARCHSAL '			$',"SMZ= ",'$',"SMZ+(",'$',"PM_molecule[",'$',"j][",'$',"k]*",'$',"molecule[",'$',"j][",'$',"k][2]);\n";
		print ARCHSAL '			$',"SMT= ",'$',"SMT+(",'$',"PM_molecule[",'$',"j][",'$',"k]);\n";
		print ARCHSAL '			$',"k++;\n";
		print ARCHSAL "		}	until  (",'$',"k> ",'$',"count_atm_res_[",'$',"j]);\n";
		print ARCHSAL '		$',"CMX = sprintf ",'"',"%",".4f",'", ','$',"SMX/",'$',"SMT;\n";
		print ARCHSAL '		$',"CMY = sprintf ",'"',"%",".4f",'", ','$',"SMY/",'$',"SMT;\n";
		print ARCHSAL '		$',"CMZ = sprintf ",'"',"%",".4f",'", ','$',"SMZ/",'$',"SMT;\n";
		print ARCHSAL '		$',"impresion = ",'$',"j+1;\n";
		print ARCHSAL '		$',"baseline_CM_residue = ",'"',"CM_residue_",'$',"impresion = (",'"',";\n";
		print ARCHSAL '		@',"baseline_CM_residue = split ('', ",'$',"baseline_CM_residue); # Transformo la linea en un arreglo\n";
		print ARCHSAL "		unshift (",'@',"baseline_CM_residue, ",'"@"',");\n";
		print ARCHSAL "		push (",'@',"baseline_CM_residue, ",'"$',"CMX, ",'$',"CMY, ",'$',"CMZ);",'\n")',";\n";
		print ARCHSAL '		$',"Temporal2 = join ('',",'@',"baseline_CM_residue);\n";
		print ARCHSAL "		print ARCHSAL ",'"$',"Temporal2",'"',";\n";
		print ARCHSAL "     #Darby NJ, Creighton TE. 1993. Protein structure. Oxford University Press, New York. p. 4 ISBN 019963310X\n";
		print ARCHSAL "		if (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"ALA",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.52;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"ARG",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.28;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"ASN",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.84;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"ASP",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.79;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"CYX",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.74;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"CYS",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.74;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"GLN",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.01;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"GLU",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.96;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"GLY",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.25;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"HIS",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.04;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"ILE",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.09;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"LEU",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.09;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"LYS",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.18;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"MET",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.09;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"PHE",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.18;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"PRO",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.78;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"SER",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '		$',"VM_molecule[",'$',"j][0] = 2.59;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"THR",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.81;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"TRP",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.39;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"TYR",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.23;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"VAL",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.93;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "     # Voss NR, Gerstein M. 2005. Calculation of standard atomic volumes for RNA and comparison with proteins: RNA is packed more tightly. Journal of moleculer Biology 346:477–492. DOI: 10.1016/j.jmb.2004.11.072\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"DG",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.27;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"DA",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.22;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"DC",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.02;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"DU",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.98;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"DT",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.20;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"G",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.27;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"A",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.22;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"C",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.02;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"U",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 2.98;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		elsif (",'$',"VM_molecule[",'$',"j][0] eq ",'"',"T",'"',")\n";
		print ARCHSAL "		{\n";
		print ARCHSAL '			$',"VM_molecule[",'$',"j][0] = 3.20;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		else\n ";
		print ARCHSAL "		{\n";
		print ARCHSAL ' 		$',"VM_molecule[",'$',"j][0] = 2.50;\n";
		print ARCHSAL "		}\n";
		print ARCHSAL "		print ARCHSAL ",'"\@',"VM_residue_",'$',"impresion = ",'$',"VM_molecule[",'$',"j][0];",'\\',"n",'";';	
		print ARCHSAL "		\n";
		print ARCHSAL '	$',"j++;\n";
		print ARCHSAL "	} until (",'$',"j>",'$',"Num_res);\n";
		print ARCHSAL "\n";
		print ARCHSAL "open (ARCHSAL, ",'"',">>center_mass_model_$Model.pl",'"',")||die ",'"',"Cannot open file ",'\\',"n",'"',";\n";
		print ARCHSAL "print ARCHSAL ",'"',"open (ARCHSAL, $matrix_salida);",'"',";\n";
		print ARCHSAL "close ARCHSAL;\n";
		$Model=++$Model;
		close ARCHSAL;
		}
	
	if ($linea =~ "^ATOM")
	{
		@linea_larga_atomo = split ('', $linea);
		$baseline_coordinate = "coordinate_";
		@baseline_coordinate = split ('', $baseline_coordinate);
		unshift (@baseline_coordinate, "@");
		do	{
				shift (@Num_res); 
				$vaciar_push = @Num_res;
			} until  ($vaciar_push == 0);
		$n=20;
		do	{
			$n=++$n;
			} until ($linea_larga_atomo[$n] =~ '\d');
		do 	{
			push (@Num_res, "$linea_larga_atomo[$n]");
			$n=++$n;
			} until ($linea_larga_atomo[$n] =~ '\s');
		$Num_res = join ('',@Num_res);
		$Num_res = $Num_res + $Continuidad_conteo;
		push (@baseline_coordinate, $Num_res);
		push (@baseline_coordinate, "_");
		$n=11;
		do	{
			$n=++$n;
			} until ($linea_larga_atomo[$n] =~ '\w');
		if ($linea_larga_atomo[$n] eq 'N')
			{
			$PM_atomo = 14;
			}
		elsif ($linea_larga_atomo[$n] eq 'C')
			{
			$PM_atomo = 12;
			}		
		elsif ($linea_larga_atomo[$n] eq 'H')
			{
			$PM_atomo = 1;
			}
		elsif ($linea_larga_atomo[$n] eq 'O')
			{
			$PM_atomo = 16;
			}
		elsif ($linea_larga_atomo[$n] eq 'S')
			{
			$PM_atomo = 32;
			}
		elsif ($linea_larga_atomo[$n] eq 'P')
			{
			$PM_atomo = 31;
			}
		elsif ($linea_larga_atomo[$n] eq 'ZN')
			{
			$PM_atomo = 65;
			}
		elsif ($linea_larga_atomo[$n] eq 'NA')
			{
			$PM_atomo = 23;
			}
		do 	{
			push (@baseline_coordinate, "$linea_larga_atomo[$n]");
			$n=++$n;
			} until ($linea_larga_atomo[$n] =~ '\W');
		@Temporal_1 = @baseline_coordinate;
		unshift (@Temporal_1, "\\");
		if ($Comparar_num_res == 0)
			{
			$baseline_residue = "residue_";
			@baseline_residue = split ('', $baseline_residue);
			push (@baseline_residue, $Num_res);
			@Temporal_2 = @baseline_residue;
			unshift (@Temporal_2, "\\@");
			push (@baseline_molecule, @Temporal_2);
			push (@baseline_molecule, ",");
			unshift (@baseline_residue, "@");
			push (@baseline_residue, " = (",@Temporal_1);	
			push (@baseline_residue, ",");
			$baseline_PM_residue = "PM_residue_";
			@baseline_PM_residue = split ('', $baseline_PM_residue);
			push (@baseline_PM_residue, $Num_res);
			@Temporal_22 = @baseline_PM_residue;
			unshift (@Temporal_22, "\\@");
			push (@baseline_PM_molecule, @Temporal_22);
			push (@baseline_PM_molecule, ",");
			unshift (@baseline_PM_residue, "@");
			push (@baseline_PM_residue, " = (",$PM_atomo);	
			push (@baseline_PM_residue, ",");
			$baseline_VM_residue = "VM_residue_";
			@baseline_VM_residue = split ('', $baseline_VM_residue);
			push (@baseline_VM_residue, $Num_res);
			@Temporal_22 = @baseline_VM_residue;
			unshift (@Temporal_22, '\\@');
			push (@baseline_VM_molecule, @Temporal_22);
			push (@baseline_VM_molecule, ",");
			unshift (@baseline_VM_residue, '@');
			push (@baseline_VM_residue, ' = "');	
			$n=14;
			do	{
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\w');
			do 	{
				push (@baseline_VM_residue, "$linea_larga_atomo[$n]");
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\W');
			push (@baseline_VM_residue, '"');
			$Comparar_num_res = $Num_res;
			$count_atm_res_[$Num_res]++;
		} 
		
		elsif ($Num_res == $Comparar_num_res)
		{
			$Temporal_3 = join ('',@Temporal_1);
			push (@baseline_residue, "$Temporal_3");
			push (@baseline_residue, ",");	
			push (@baseline_PM_residue, "$PM_atomo");
			push (@baseline_PM_residue, ",");
			$count_atm_res_[$Num_res]++;
		} else
		{	
			pop @baseline_residue;
			push (@baseline_residue, ");\n");
			$Temporal_3 = join ('',@baseline_residue);
			print ARCHSAL "$Temporal_3";	
			pop @baseline_PM_residue;
			push (@baseline_PM_residue, ");\n");
			$Temporal_33 = join ('',@baseline_PM_residue);
			print ARCHSAL "$Temporal_33";
			push (@baseline_VM_residue, ";\n");
			$Temporal_33 = join ('',@baseline_VM_residue);
			print ARCHSAL "$Temporal_33";
			$baseline_residue = "residue_";
			@baseline_residue = split ('', $baseline_residue);
			unshift (@baseline_residue, "@");
			push (@baseline_residue, $Num_res);
			@Temporal_2 = @baseline_residue;
			unshift (@Temporal_2, "\\");
			push (@baseline_molecule, @Temporal_2);
			push (@baseline_molecule, ",");
			push (@baseline_residue, " = (",@Temporal_1);	
			push (@baseline_residue, ",");
			$baseline_PM_residue = "PM_residue_";
			@baseline_PM_residue = split ('', $baseline_PM_residue);
			push (@baseline_PM_residue, $Num_res);
			@Temporal_22 = @baseline_PM_residue;
			unshift (@Temporal_22, "\\@");
			push (@baseline_PM_molecule, @Temporal_22);
			push (@baseline_PM_molecule, ",");
			unshift (@baseline_PM_residue, "@");
			push (@baseline_PM_residue, " = (",$PM_atomo);	
			push (@baseline_PM_residue, ",");
			$baseline_VM_residue = "VM_residue_";
			@baseline_VM_residue = split ('', $baseline_VM_residue);
			push (@baseline_VM_residue, $Num_res);
			@Temporal_22 = @baseline_VM_residue;
			unshift (@Temporal_22, '\\@');
			push (@baseline_VM_molecule, @Temporal_22);
			push (@baseline_VM_molecule, ",");
			unshift (@baseline_VM_residue, '@');
			push (@baseline_VM_residue, ' = "');	
			$n=14;
			do	{
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\w');
			do {
				push (@baseline_VM_residue, "$linea_larga_atomo[$n]");
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\W');
			push (@baseline_VM_residue, '"');	
			$count_atm_res_[$Num_res]++;
			$Comparar_num_res = $Num_res;
		}
			push (@baseline_coordinate, " = (");		
			$n=29;
			do	{
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\S');
			do	{
				push (@baseline_coordinate, "$linea_larga_atomo[$n]");
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\s');		
			push (@baseline_coordinate, ", ");
			$n=38;
			do	{
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\S');
			do 	{
				push (@baseline_coordinate, "$linea_larga_atomo[$n]");
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\s');
			push (@baseline_coordinate, ", ");
			$n=46;
			do	{
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\S');
			do	{
				push (@baseline_coordinate, "$linea_larga_atomo[$n]");
				$n=++$n;
				} until ($linea_larga_atomo[$n] =~ '\s');
			push (@baseline_coordinate, ");");
			$nombre_corto = join ('',@baseline_coordinate);
			print ARCHSAL ("$nombre_corto \n");		
	}
}
close ARCHSAL;
close ARCH;

@filesModel = glob "Model*.pl";
foreach $a (@filesModel){
system ($a);
}

$base2_CMmolecule = "molecule = (";
@base2_CMmolecule = split ('', $base2_CMmolecule);
unshift (@base2_CMmolecule, "@");

$base2_VMmolecule = "VM_molecule = (";
@base2_VMmolecule = split ('', $base2_VMmolecule);
unshift (@base2_VMmolecule, "@");

$n = 1;
do {
	$base2_CMresidue = "CM_residue_$n,";
	@base2_CMresidue = split ('', $base2_CMresidue);
	unshift (@base2_CMresidue, '\@');
	push @base2_CMmolecule, @base2_CMresidue;
	
	$base2_VMresidue = "VM_residue_$n,";
	@base2_VMresidue = split ('', $base2_VMresidue);
	unshift (@base2_VMresidue, '\@');
	push @base2_VMmolecule, @base2_VMresidue;
	
	$n++;
	} until ($n>65);

pop @base2_CMmolecule;
push(@base2_CMmolecule, ");\n");
$Temporal_55 = join('',@base2_CMmolecule);

pop @base2_VMmolecule;
push(@base2_VMmolecule, ");\n");
$Temporal_66 = join('',@base2_VMmolecule);

@filesCM = glob "center*.pl";
foreach $b (@filesCM){
	open (ARCHSAL, '>>', $b);
	print ARCHSAL "\n";
	print ARCHSAL "$Temporal_55"; print ARCHSAL "$Temporal_66"; 
	print ARCHSAL '$',"Num_res = ",$Num_res-1,";\n"; 
	print ARCHSAL "print ARCHSAL ",'"',"Residue1,Residue2,Distance,Normalized_Distance,Inverse_Distance",'\n";',"\n";
	print ARCHSAL '$',"i=0;\n"; 
	print ARCHSAL "while (",'$',"i < ",'$',"Num_res)\n";
	print ARCHSAL "	{\n";
    print ARCHSAL '	$',"distance_calculated = 0;\n";
	print ARCHSAL '	$',"distance_normalized = 0;\n";
	print ARCHSAL '	$',"distance_inverse = 0;\n";
	print ARCHSAL '	$',"k=",'$',"i+1;\n";
	print ARCHSAL "		 do\n";
	print ARCHSAL "		   {\n";
	print ARCHSAL '			$',"distance_calculated = sprintf ",'"%',".4f",'",'," sqrt ((",'$',"molecule[",'$',"i][0]-",'$',"molecule[",'$',"k][0])*(",'$',"molecule[",'$',"i][0]-",'$',"molecule[",'$',"k][0])+(",'$',"molecule[",'$',"i][1]-",'$',"molecule[",'$',"k][1])*(",'$',"molecule[",'$',"i][1]-",'$',"molecule[",'$',"k][1])+(",'$',"molecule[",'$',"i][2]-",'$',"molecule[",'$',"k][2])*(",'$',"molecule[",'$',"i][2]-",'$',"molecule[",'$',"k][2]));\n";	
	print ARCHSAL '			$',"distance_normalized = sprintf ",'"%',".4f",'", ','$',"distance_calculated/(",'$',"VM_molecule[",'$',"i][0]+",'$',"VM_molecule[",'$',"k][0]);\n";
	print ARCHSAL '			$',"distance_inverse = sprintf ",'"%',".4f",'",'," 1/",'$',"distance_normalized;\n";
	print ARCHSAL "			print ARCHSAL ",'$',"i+1,",'","',",(",'$',"k+1),",'",$',"distance_calculated",',$',"distance_normalized",',$',"distance_inverse",'\n"',";\n";
	print ARCHSAL '	$',"k++;\n";	
	print ARCHSAL "} until (",'$',"k>",'$',"Num_res);\n";	
	print ARCHSAL '$',"i++;\n";
	print ARCHSAL "}\n";
	close ARCHSAL;

system($b);

}

exit;
# Simple Approach for Ranking Structure Determining Residues