c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ##############################################################
c     ##                                                          ##
c     ##  program abstract  --  create a summary from a PDB file  ##
c     ##                                                          ##
c     ##############################################################
c
c
c     "abstract" takes as input a Protein Data Bank file and
c     creates a summary of the protein structure containing the
c     number of residues, amino acid sequence, dihedral angles,
c     secondary structure, alpha carbon coordinates, and exposed
c     surface area for use with the SLEUTH searching program
c
c
      program abstract
      implicit none
      include 'sizes.i'
      include 'aminos.i'
      include 'atoms.i'
      include 'files.i'
      include 'iounit.i'
      include 'pdb.i'
      include 'phipsi.i'
      include 'sequen.i'
      include 'titles.i'
      integer i,j,k,m,idat,nres
      integer freeunit,trimtext
      integer nline,label,itemp
      integer first,last,length
      integer start,stop
      integer nchi(maxamino)
      integer ni(maxres),cai(maxres)
      integer ci(maxres),oi(maxres)
      integer cbi(maxres),cgi(maxres)
      integer cdi(maxres),cei(maxres)
      integer czi(maxres)
      integer resatm(2,maxres)
      real*8 dihedral,radius(maxatm)
      real*8 bury(maxres),fbury(maxres)
      real*8 fpolar(maxres)
      character*1 letter,cis
      character*3 resname
      character*3 fold(maxres)
      character*8 surftyp
      character*60 pdbnam,datafile,tempname
      logical caonly,schain(maxatm)
      data nchi   / 0,0,1,2,2,1,1,1,2,2,2,2,2,2,2,3,3,3,4,4,3,0,2,0 /
c
c
c     get and read the Protein Data Bank file
c
      call initial
      call getpdb
c
c     remove any directory prefix from the filename
c
      first = 1
      do i = 1, leng
         letter = filename(i:i)
         if (letter .eq. '/')  first = i + 1
         if (letter .eq. ']')  first = i + 1
         if (letter .eq. ':')  first = i + 1
      end do
      length = leng - first + 1
      pdbnam(1:length) = filename(first:leng)
c
c     open an output file for the summary data
c
      datafile = pdbnam(1:length)//'.dat'
      call version (datafile,'new')
      idat = freeunit ()
      open (unit=idat,file=datafile,status='new')
c
c     copy the PDB coordinates into the main coordinate area
c
      n = npdb
      do i = 1, npdb
         x(i) = xpdb(i)
         y(i) = ypdb(i)
         z(i) = zpdb(i)
      end do
c
c     set a pointer to the first and last atom of each residue
c
      nres = 0
      k = 0
      do i = 1, npdb
         if (resnum(i) .ne. k) then
            k = resnum(i)
            if (nres .ne. 0)  resatm(2,nres) = i - 1
            nres = nres + 1
            resatm(1,nres) = i
         end if
      end do
      resatm(2,nres) = npdb
c
c     get the one-letter sequence and code for each residue
c
      nseq = nres
      do i = 1, nres
         start = resatm(1,i)
         resname = resnam(start)
         seq(i) = 'X'
         seqtyp(i) = maxamino
         do k = 1, maxamino
            if (resname .eq. amino(k)) then
               seq(i) = amino1(k)
               seqtyp(i) = k
               goto 10
            end if
         end do
   10    continue
      end do
c
c     use only a single repeat from a series of identical repeats;
c     repeats must be at least 7 residues long to be considered
c
      do k = 100, 2, -1
         if (mod(nseq,k) .eq. 0) then
            m = nseq / k
            if (m .lt. 7)  goto 30
            do i = 1, m
               do j = 1, k-1
                  if (seq(i) .ne. seq(i+j*m))  goto 30
               end do
            end do
            write (iout,20)  m
   20       format (/,' Warning, Found Sequence Repeat of Length',i4,
     &                 ' Residues; Continuing')
c
c     activate following lines to use only single sequence repeat
c
c           nseq = nseq / k
c           nres = nseq
c           npdb = resatm(2,nres)
c           n = npdb
            goto 40
         end if
   30    continue
      end do
   40 continue
c
c     print out the file name and number of residues
c
      write (idat,50)  pdbnam(1:length)
   50 format (/,' Protein Data Bank File :',15x,a<length>)
      write (idat,60)  title(1:ltitle)
   60 format (/,' Title :',5x,a<ltitle>)
      write (idat,70)  nres
   70 format (/,' Number of Amino Acid Residues :',i12)
c
c     quit work on this structure if there are no residues
c
      if (nres .eq. 0) then
         write (iout,80)  pdbnam(1:length)
   80    format (/,' Warning, Protein Data Bank file "',a<length>,
     &              '" contains No Residues')
         goto 230
      end if
c
c     print out the amino acid sequence of the protein
c
      write (idat,90)
   90 format (/,' Amino Acid Sequence :',
     &        //,12x,'1',4x,'2',4x,'3',4x,'4',4x,'5',
     &            4x,'6',4x,'7',4x,'8',4x,'9',4x,'10',/)
      nline = (nseq-1)/10 + 1
      do i = 1, nline
         label = 10 * (i-1)
         first = label + 1
         last = min(label+10,nseq)
         write (idat,100)  label,(amino(seqtyp(k)),k=first,last)
  100    format (i6,3x,10(2x,a3))
      end do
c
c     locate the backbone and sidechain atoms for each residue
c
      do i = 1, nres
         start = resatm(1,i)
         stop = resatm(2,i)
         call findatm (' N  ',start,stop,ni(i))
         call findatm (' CA ',start,stop,cai(i))
         call findatm (' C  ',start,stop,ci(i))
         call findatm (' O  ',start,stop,oi(i))
         call findatm (' CB ',start,stop,cbi(i))
         call findatm (' CG ',start,stop,cgi(i))
         if (cgi(i) .eq. 0)  call findatm (' CG1',start,stop,cgi(i))
         if (cgi(i) .eq. 0)  call findatm (' OG ',start,stop,cgi(i))
         if (cgi(i) .eq. 0)  call findatm (' OG1',start,stop,cgi(i))
         if (cgi(i) .eq. 0)  call findatm (' SG ',start,stop,cgi(i))
         call findatm (' CD ',start,stop,cdi(i))
         if (cdi(i) .eq. 0)  call findatm (' CD1',start,stop,cdi(i))
         if (cdi(i) .eq. 0)  call findatm (' OD1',start,stop,cdi(i))
         if (cdi(i) .eq. 0)  call findatm (' ND1',start,stop,cdi(i))
         if (cdi(i) .eq. 0)  call findatm (' SD ',start,stop,cdi(i))
         call findatm (' CE ',start,stop,cei(i))
         if (cei(i) .eq. 0)  call findatm (' NE ',start,stop,cei(i))
         if (cei(i) .eq. 0)  call findatm (' OE ',start,stop,cei(i))
         if (cei(i) .eq. 0)  call findatm (' OE1',start,stop,cei(i))
         call findatm (' CZ ',start,stop,czi(i))
         if (czi(i) .eq. 0)  call findatm (' NZ ',start,stop,czi(i))
      end do
c
c     test for a structure with only alpha-carbon backbone atoms
c
      caonly = .true.
      do i = 1, nres
         if (max(ni(i),ci(i)) .gt. 0) then
            caonly = .false.
            goto 110
         end if
      end do
  110 continue
c
c     compute the backbone dihedral angles for each residue
c
      phi(1) = 0.0d0
      psi(1) = dihedral(ni(1),cai(1),ci(1),ni(2))
      omega(1) = dihedral(cai(1),ci(1),ni(2),cai(2))
      do i = 2, nres-1
         phi(i) = dihedral(ci(i-1),ni(i),cai(i),ci(i))
         psi(i) = dihedral(ni(i),cai(i),ci(i),ni(i+1))
         omega(i) = dihedral(cai(i),ci(i),ni(i+1),cai(i+1))
      end do
      phi(nres) = dihedral(ci(nres-1),ni(nres),cai(nres),ci(nres))
      psi(nres) = dihedral(ni(nres),cai(nres),ci(nres),oi(nres))
      omega(nres) = 0.0d0
c
c     compute the sidechain dihedral angles for each residue
c
      do i = 1, nres
         chi(1,i) = dihedral(ni(i),cai(i),cbi(i),cgi(i))
         chi(2,i) = dihedral(cai(i),cbi(i),cgi(i),cdi(i))
         chi(3,i) = dihedral(cbi(i),cgi(i),cdi(i),cei(i))
         chi(4,i) = dihedral(cgi(i),cdi(i),cei(i),czi(i))
      end do
c
c     print out the dihedral angle values for each residue
c
      write (idat,120)
  120 format (/,' Main and Side Chain Dihedral Angles :',
     &        //,' Residue',5x,'Type',7x,'Phi     Psi   Omega',
     &            '   Chi 1   Chi 2   Chi 3   Chi 4',/)
      do i = 1, nres
         write (idat,130)  i,amino(seqtyp(i)),phi(i),psi(i),omega(i),
     &                     (chi(k,i),k=1,nchi(seqtyp(i)))
  130    format (i6,7x,a3,3x,7f8.1)
      end do
c
c     print out the alpha carbon coordinates for each residue
c
      write (idat,140)
  140 format (/' Alpha Carbon Cartesian Coordinates :',
     &        //,' Residue',5x,'Type',11x,'X',11x,'Y',11x,'Z',/)
      do i = 1, nres
         start = resatm(1,i)
         stop = resatm(2,i)
         k = cai(i)
         if (k .eq. 0) then
            n = n + 1
            cai(i) = n
            k = n
            x(n) = 0.0d0
            y(n) = 0.0d0
            z(n) = 0.0d0
            npdb = n
            xpdb(npdb) = 0.0d0
            ypdb(npdb) = 0.0d0
            zpdb(npdb) = 0.0d0
            atmnam(npdb) = ' X  '
            resnam(npdb) = amino(seqtyp(i))
            resnum(npdb) = i
         end if
         write (idat,150)  i,amino(seqtyp(i)),x(k),y(k),z(k)
  150    format (i6,7x,a3,3x,3f12.3)
      end do
c
c     get Richards-Kundrot secondary structure assignments
c
      call second (nres,cai,fold)
c
c     check for additional cis peptides based on omega angle
c
      cis = 'P'
      do i = 1, nres-1
         if (abs(omega(i)) .lt. 20.0d0) then
            if (fold(i)(2:2).eq.cis .or. fold(i)(3:3).eq.cis) then
               continue
            else if (fold(i)(2:2) .eq. '/') then
               fold(i)(2:2) = cis
            else
               fold(i)(1:1) = fold(i)(2:2)
               fold(i)(2:2) = '/'
               fold(i)(3:3) = cis
            end if
         end if
      end do
      if (caonly) then
         do i = 1, nres
            fold(i) = '   '
         end do
      end if
c
c     print Richards-Kundrot secondary structure of each residue
c
      write (idat,160)
  160 format (/,' Richards-Kundrot Secondary Structure Assignment :',
     &        //,12x,'1',4x,'2',4x,'3',4x,'4',4x,'5',
     &            4x,'6',4x,'7',4x,'8',4x,'9',3x,'10',/)
      nline = (nres-1)/10 + 1
      do i = 1, nline
         label = 10 * (i-1)
         first = label + 1
         last = min(label+10,nres)
         write (idat,170)  label,(fold(k),k=first,last)
  170    format (i6,3x,10(2x,a3))
      end do
c
c     get Kabsch-Sander secondary structure assignments
c
      itemp = freeunit ()
      tempname = pdbnam(1:length)//'.tmp'
      open (unit=itemp,file=tempname,status='new')
      call prtpdb (itemp)
      close (unit=itemp)
      call secstr (tempname,nres,x,y,z,cai,fold)
c
c     print Kabsch-Sander secondary structure of each residue
c
      write (idat,180)
  180 format (/,' Kabsch-Sander Secondary Structure Assignment :',
     &        //,12x,'1',4x,'2',4x,'3',4x,'4',4x,'5',
     &            4x,'6',4x,'7',4x,'8',4x,'9',3x,'10',/)
      nline = (nres-1)/10 + 1
      do i = 1, nline
         label = 10 * (i-1)
         first = label + 1
         last = min(label+10,nres)
         write (idat,190)  label,(fold(k),k=first,last)
  190    format (i6,3x,10(2x,a3))
      end do
c
c     compute some surface accessibility measures for each residue
c
      surftyp = 'RICHARDS'
c     surftyp = 'CHOTHIA '
      if (caonly) then
         do i = 1, nres
            bury(i) = 0.0d0
            fbury(i) = 0.0d0
            fpolar(i) = 0.0d0
         end do
      else
         call setradii (surftyp,radius,schain)
         call burial (surftyp,nres,resatm,radius,schain,bury,fbury)
         call polarea (nres,resatm,radius,schain,fpolar)
      end if
c
c     print out the surface accessibility values of each residue
c
      write (idat,200)
  200 format (/' Buried Surface Area Percentages :',
     &        //,' Residue',5x,'Type',6x,'Buried Area',
     &           4x,'% Buried',4x,'% Polar Cover'/)
      do i = 1, nres
         write (idat,210)  i,amino(seqtyp(i)),bury(i),fbury(i),fpolar(i)
  210    format (i6,7x,a3,10x,f6.1,7x,f6.1,7x,f6.1)
      end do
c
c     print a final message and close the output file
c
      length = trimtext(datafile)
      write (iout,220)  datafile(1:length)
  220 format (/,' PDB Summary Information written to :  ',a<length>)
  230 continue
      close (unit=idat)
      end
c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1993  by  Jay William Ponder  ##
c     ###################################################
c
c     ############################################################
c     ##                                                        ##
c     ##  subroutine findatm  --  locate PDB atom in a residue  ##
c     ##                                                        ##
c     ############################################################
c
c
c     "findatm" locates a specific PDB atom name type within a
c     range of atoms from the PDB file, returns zero if the name
c     type was not found
c
c
      subroutine findatm (name,start,stop,ipdb)
      implicit none
      include 'sizes.i'
      include 'pdb.i'
      integer i,ipdb,start,stop
      character*4 name
c
c
c     search for the specified atom within the residue
c
      ipdb = 0
      do i = start, stop
         if (atmnam(i) .eq. name) then
            ipdb = i
            goto 10
         end if
      end do
   10 continue
      return
      end
