 function [names,corrd,pdb4] = pdb2dyana(pdbFileName)
%
%  $BEGIN
%  $HEADER pdb2dyana
%
%  This is a MATLAB script file 
%  for converting a general PDB
%  file into the correct orientation
%  to use in a DYANA library
%
%  $HEADER REFERENCE
%  An AMBER/DYANA/MOLMOL Phosphorylated 
%  Amino Acid Library Set and Incorporation 
%  into NMR Sturcture Calculations
%
%  $HEADER ASSUMPTIONS AND LIMITATIONS:
%  This script assumes that the nomenclature is 
%  consistent with AMBER for N, CA, and C.
%  In addition the script assumes that the 
%  backbone nitrogen is the first atom(1).
%
%  $HEADER PROGRAMMER
%  John W. Craft, Jr.
%  University of Houston, (2005)
%
%  $HEADER VERSION
%  1.0.0
%
%  Example
%  pdb2dyana("file_name")
%
%  See Also pdb2amber
%  
%  $END
%

[names,corrd]=pdbReadFile(pdbFileName);

message = {'Original PDB data'}
pdb1 = corrd
pdb2 = pdbTranslate(pdb1);

i_n = atomfind(names,'N');
i_ca = atomfind(names,'CA');
i_c = atomfind(names,'C');

% compute the first transformation
% which is to rotate N->CA onto the x axis

x = [1 0 0];
n = pdb1(i_n,:);
ca = pdb1(i_ca,:);
bond_n_ca = ca - n;
bond_n_ca_u = bond_n_ca ./ norm(bond_n_ca);

dot1 = x * bond_n_ca_u';
theta1 = acos(dot1);
cross1 = cross(x,bond_n_ca_u);
cross1n = cross1 ./ norm(cross1);
rot_angle = theta1;
rot_axis1 = cross1n;

q1 = rotquat(theta1,rot_axis1);
tranmat1 = quatmat(q1);
pdb3 = pdbTransform(tranmat1,pdb2);

% compute the second transformation
% which is to rotate C' onto the
% x-y plane

y = [0 1 0];
ca3 = pdb3(i_ca,:)
c3 = pdb3(i_c,:)
bond_c3_ca3 = c3 - ca3
bond_c3_ca3_u = bond_c3_ca3 ./ norm(bond_c3_ca3);
bond_c3_ca3_u
norm(bond_c3_ca3_u)
dot2 = y * bond_c3_ca3_u';
theta2 = atan(bond_c3_ca3(3)/bond_c3_ca3(2))
theta3 = theta2
if bond_c3_ca3(2) < 0
   theta3 = -sign(theta2)*( pi - abs(theta2) )
end
rot_axis2 = x;

q2 = rotquat(theta3,rot_axis2);
tranmat2 = quatmat(q2);

message = {'New x,y,z for DYANA'}
pdb4 = pdbTransform(tranmat2,pdb3)


% independent check

q3 = quatmult(q1,q2);
tranmat3 = quatmat(q3);
pdb44 = pdbTransform(tranmat3,pdb2);

