#!/usr/bin/perl -w
# Paulino Gomez-Puertas 2001.
# BcolorPDB:    It introduces values from a "b-value" file into 
#               the b-factor colum of a PDB file to be 
#               represented as color code by rasmol 
#               "temperature" option.
#
# example of "matrix" file (two columns: 1st-> residue number
#                                        2nd-> new "b-value"):
#
#    124  7.9
#    125  8.2
#    131  3.4
#    132  3.7
#    
#
# see help at the end of this script listing.
#

### MAIN PROGRAM #######

&GETPARAMETERS;
&OPENFILES;
&CREATEMATRIZPAREJAS;
&MAKESUBSTITUTIONS;
&PRINTOUTPUTFILE;
print STDERR "\n\n ** \"When using Rasmol, colour the atoms by temperature\"**  \n\n";

#### END OF MAIN PROGRAM #####

### SUB GETPARAMETERS ####
sub GETPARAMETERS {
  if (@ARGV == 0) {
  &SALIDAERROR;
  }
  for ($i=0;$i<@ARGV;$i++) {
     $aa[$i]=$ARGV[$i];
     chomp $aa[$i];
     if (@ARGV != 2) {
        if ($aa[$i] eq "-h") {
        &MUESTRAHELP;
        }
        &SALIDAERROR;
     }
  }
  $pdbfile = $aa[0];
  $matrixfile = $aa[1];
}
#### END OF SUB GETPARAMETERS ####

### SUB OPENFILES ####
sub OPENFILES {

  open (ORIGFILE, "$pdbfile") || die "\n Sorry, I couldn't open \"$pdbfile\".\n\n";
  while (<ORIGFILE>) {
	push (@original, $_); #It creates @original from the pdb file lines
  }
  close ORIGFILE;


  open (MATRIXFILE, "$matrixfile") || die "\n Sorry, I couldn't open \"$matrixfile\".\n\n";
  while (<MATRIXFILE>) {
	push (@parejas, $_); #It creates @parejas from the matrix file lines
  }
  close MATRIXFILE;

}
#### END OF SUB OPENFILES ####


### SUB CREATEMATRIZPAREJAS ####
sub CREATEMATRIZPAREJAS {
    $xx=0;
    for ($i=0;$i<@parejas;$i++) { # it creates an array of all the matrix 
	                            # residue-value pairs
        @separa = split (' ', $parejas[$i]);
        if (@separa > 1) {
            
	    $g = length ($separa[0]);
	    if ($g<4) {
	       $h = 4 - $g;
	       $j = ' ' x $h;
	       substr ($separa[0],0,0) = $j; # it makes all residue numbers having a length of 4 characters
	    }   
	    $matriz_parejas[$xx][0] = $separa[0];

            $d =  length ($separa[1]);
	    if ($d<6) {
  	      $e = 6 - $d;
	      $f = ' ' x $e;
	      substr ($separa[1],0,0) = $f; # it makes all b-values having a length of 6 characters
	    }
	    $matriz_parejas[$xx][1] = $separa[1];

            $xx = $xx + 1;
	}
    }
}
#### END OF SUB CREATEMATRIZPAREJAS ####


### SUB MAKESUBSTITUTIONS #####
sub MAKESUBSTITUTIONS {

  for ($i=0;$i<@original;$i++) {
    $c = length ($original[$i]);
    $comprueba = substr($original[$i],0,6);
    if (($comprueba eq "ATOM  ") or ($comprueba eq "HETATM")) { #no ATOM/HETATM lines remain unchanged
       if ($c<=66) {
         $b = 67 - $c;
	 $a = ' ' x $b;
	 substr ($original[$i], ($c - 1), 0) = $a; # it adds spaces to columns 60-65 (b-value) if necessary
       }
       substr ($original[$i], 60, 6) = "      "; # b-value of all ATOM/HETATMs is previously changed to blank

       
       ##### program core: changing b-values

       $numresidue = substr($original[$i],22,4); # getting the residue number
       for ($k=0;$k<@matriz_parejas;$k++) {
          if ($numresidue eq $matriz_parejas[$k][0]) { # if the residue is listed in "matrix" file
             substr ($original[$i], 60, 6) = $matriz_parejas[$k][1]; # b-value of matching residues is changed to correspondent value
	  }
	}
	                
       ##### end of program core

    }
  }
}
#### END OF SUB MAKESUBSTITUTIONS #####


### SUB PRINTOUTPUTFILE #####
sub PRINTOUTPUTFILE {
  for ($i=0;$i<@original;$i++) {
  print "$original[$i]";
  }
}
#### END OF SUB PRINTOUTPUTFILE #####

#### SUB SALIDA_ERROR ####
sub SALIDAERROR {
   print STDERR "\n Usage: \"BcolorPDB  pdb_orig_file  b-values_file \> pdb_dest_file\" \n\n";
   print STDERR "  Help: \"BcolorPDB -h\" \n";
   die "\n";
}
#### END OF SUB SALIDA_ERROR ####

#### SUB MUESTRAHELP #####
sub MUESTRAHELP {

 print STDERR "\n
 \"BcolorPDB\":
 
 This script adds new values to the b-value (\"temperature\") column 
 of a PDB structure file. 
 It allows representation of residues coloured according to conservation, 
 accessibility, protein interaction site prediction values, etc.  
   
 Usage: \"BcolorPDB  pdb_orig_file  b-values_file \> pdb_dest_file\" 
   
 pdb_orig_file: PDB structure to be \"coloured\" 
 pdb_dest_file: name of destination file 
 b-values_file: file containing the \"new\" b-values
  
 The structure of the \"b-values_file\" must be:
  
    124     7.9
    125     8
    131     3.4
    132     1
     .       .
     .       .
  
 where the 1st column indicates the residue number 
 and the 2nd one contains the associated new \"b-values\".";  
    
 die "\n\n *PGP 2001*\n\n";

}
#### END OF SUB MUESTRAHELP #####

################################
#### end of script listing #####
################################
