#!/usr/bin/perl
# AUTHOR: Cameron Mura (07/2001). 
# LAST MODIFIED: 05/21/2004
# USAGE: 
# 	map_conservation_to_B.pl file1.pdb file2_sites.txt
# 
# DESCRIPTION:
# This Perl script takes 2 input files: argv[0] ("file1.pdb") is the PDB file 
# and argv[1] ("file2_sites.txt") is a file tabulating conserved residues in 
# the format:
#
# "res#,ss(super strong)|s(trong)|m(edium)|w(eak)" ...
#
# where "res#" is the residue # and "superstrong", "strong", "medium", 
# or "weak" specify how strongly that site is conserved.
# 
# The output is a PDB file with all of the B-factors flattened to 20.00,
# except for the "superstrong", "strong", "medium", or "weak" sites, which
# are assigned B-values of 90.00, 70.00, 40.00, or 33.00, respectively. 
# NOTE: make sure all occupancies are 1.00 for any atoms with B-factors you
# want changed.
#
# This is useful for programs like GRASP, which can color a surface by the B-factor 
# field of PDB file.  Or Robert Campbell's color_b.py module for PyMOL.

$pdb_in = $ARGV[0];
$site_file = $ARGV[1];
$bfac = "";
@cons = "";
$site_line = "";
 
open (PDB, $pdb_in) || die "Cannot open file \"$pdb_in\"\n";

while (<PDB>)
	{
 	 $line = $_;	
	 chomp ($line); 
	 $resnum = ""; $bfac = "";
	 if ($line =~ /(^(ATOM|HET)\s+\d+\s+[\w\*]+\s+\w\w\w [A-Z]\s+)(\d+)(.+)(1\.00\s+)(\d+\.\d+)(.*)/) 
	   {
	    $resnum = $3; $bfac = "20.00";
		  $counter = 0;
		  open (SITES, $site_file) || die "Cannot open file \"$site_file\"\n";
		  while (<SITES>)
		  {if ($counter == 0) 

		     {
		 
	  	      $site_line = $_; chomp ($site_line); @cons = "";
	  	      @cons = split(/,/, $site_line);
  		      if ($cons[0] == $resnum and $cons[1] eq "ss" )   	{$bfac = "95.00"; $resnum++; last;}
		      if ($cons[0] == $resnum and $cons[1] eq "s" ) 	{$bfac = "65.00"; $resnum++; last;} 
	  	      if ($cons[0] == $resnum and $cons[1] eq "m" ) 	{$bfac = "45.00"; $resnum++; last;}
                      if ($cons[0] == $resnum and $cons[1] eq "w" )   	{$bfac = "30.00"; $resnum++; last;} 
 
		      next;  
		     }

	          }
		close (SITES);

	    if ($bfac eq "") { $bfac = "20.00";}
	    $shit = $5 . $bfac;
   	    print "$1$3$4$shit$7\n";
	   }
	}


