#! /usr/bin/awk -f
#
#   Calculate RMSD of atoms with the same name in two PDB files
#
#	The PDB feild:
#           |<--- here -->|
#ATOM      1  N   ALA A 327      40.574  34.523  43.012  1.00 34.04
#
#	is used to determine if two atoms are a "pair"
#
BEGIN {
if(! atom) atom = "CA"
maxXYZ = maxdB = 0
max_atom_XYZ = max_atom_dB = 0
maxXYZ_ID = maxdB_ID = "-"
max_atom_XYZ_ID = max_atom_dB_ID = "-"
}

/^ATOM/{
    # read in values (watching for duplications)
    ID = substr($0,12,15)
    ++count[ID]

    if(count[ID] == 1)
    {
	# not initialized yet
	X[ID] = substr($0, 31, 8)+0
	Y[ID] = substr($0, 39, 8)+0
	Z[ID] = substr($0, 47, 8)+0
	B[ID] = substr($0, 61, 6)+0
    }
    
    if(count[ID] == 2)
    {
	++pairs
    
	# seen this before, subtract values
	dX     = X[ID] - substr($0, 31, 8)
	dY     = Y[ID] - substr($0, 39, 8)
	dZ     = Z[ID] - substr($0, 47, 8)
	dB[ID] = B[ID] - substr($0, 61, 6)
	
	# get drift (and add up squares of drifts)
	sqrD   = dX*dX + dY*dY + dZ*dZ
	dXYZ[ID] = sqrt(sqrD)

	# remember maximum shifts
	if(dXYZ[ID] > maxXYZ) {maxXYZ  = dXYZ[ID]; maxXYZ_ID = ID }
	if(dB[ID]*dB[ID] > maxdB*maxdB) {maxdB = dB[ID]; maxdB_ID = ID }

	# maintain mean-square sums	
	sumXYZ += sqrD
	sumB   += dB[ID]*dB[ID]

	# separate stats for special atom type
	split(ID,word)
	if(word[1] == atom)
	{
	    ++atom_pairs

	    # maintain separate mean-square sums
	    sum_atom_XYZ += sqrD
	    sum_atom_B   += dB[ID]*dB[ID]
	    
	    # remember maximum drifts too
	    if(dXYZ[ID] > max_atom_XYZ) {max_atom_XYZ  = dXYZ[ID]; max_atom_XYZ_ID = ID }
	    if(dB[ID]*dB[ID] > max_atom_dB*max_atom_dB) {max_atom_dB = dB[ID]; max_atom_dB_ID = ID }	    
	}
	# debug output
	if(debug)
	{
	    printf("%s moved %8.4f (XYZ) %6.2f (B) at %s\n", ID, dXYZ[ID], dB[ID], "cen_x " substr($0,31,24))
	}	
    }

    if(count[ID] > 2)
    {
	print "WARNING: " ID " appeared more than twice! "
    }
}


END{
    
    if(pairs+0 == 0) 
    {
	print "no atom pairs found"
	exit
    }
    rmsXYZ = sqrt(sumXYZ/pairs)
    rmsB = sqrt(sumB/pairs)
    if(atom_pairs+0 != 0)
    {
	rms_atom_XYZ = sqrt(sum_atom_XYZ/atom_pairs)
	rms_atom_B = sqrt(sum_atom_B/atom_pairs)
    }
    

    if(! xlog) 
    {
	print pairs " atom pairs found"
	print "RMSD("atom" )= " rms_atom_XYZ " ("atom_pairs, atom " pairs)"
	print "RMSD(all)= " rmsXYZ " ("pairs" atom pairs)"
	print "RMSD(Bfac)= " rmsB

	print "MAXD(all)= " maxXYZ "\tfor " maxXYZ_ID
	print "MAXD(Bfac)= " maxdB "\tfor " maxdB_ID
	
	# final check for orphan atoms 
	for(id in count)
	{
	    if(count[id]<2) print "WARNING: " id " only found once"
	}
    }
    else
    {
	printf "%10.8f %10.8f %10.5f %10.8f %8.2f\n", rms_atom_XYZ, rmsXYZ, rmsB, maxXYZ, maxdB
    }
}
