 function [name,crd] = readPDBfile(pdbFileName)
%
%  $BEGIN
%  $HEADER readPDBfile
%
%  This script reads a Protein Data Bank PDB
%  file and stores the atom names, atom 
%  (x, y, z) positions
%
%  $HEADER REFERENCE
% 
%
%  $HEADER PROGRAMMER
%  John W. Craft, Jr.
%  University of Houston, (2005)
%
%  $HEADER VERSION
%  1.0.0
%
%  Example
%  [name,crd] = pdbRead(pdbFileName)
%
%  See Also pdbWriteFile coordWriteFile
%  
%  $END
%

fid = fopen(pdbFileName); % Open coordinate file

buffer = fgetl(fid); % Get the first line
word = words(buffer); % word-break the line to words separated by space

% ~ is a logical not. Read next line if ~ TER and ~ ATOM
while (and(~strcmp(word{1},'TER'),~strcmp(word{1},'ATOM')))
   nextBuffer = fgetl(fid); 
   word = words(nextBuffer);
end

%special protein data bank cases are handled below
if or(strcmp(word{5},'R') ,strcmp(word{5},'A'))
   x=7; y=8; z=9;
else
   x=6; y=7; z=8;
end

% Loop over the list of ATOMS
i =1;
while strcmp(word{1},'ATOM');

   % Delete the line below is no need for infinite loop protect
   if isequal(i,2000) break; end; % limit molecule length to 200

   % Get the data
   coords(i,:) = [str2double(word{x}),str2double(word{y}),str2double(word{z})];
   atomname(i) = word(3);

   buffer = fgetl(fid); % Get the next line
   word = words(buffer);
   if strcmp(word{1},'END') break; end;
   if strcmp(word{1},'TER') break; end;

   i = i+1; 

end
fclose(fid);

crd = coords(1:i,:);
name = atomname';

