c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ###############################################################
c     ##                                                           ##
c     ##  subroutine polarea  --  Eisenberg fractional polar area  ##
c     ##                                                           ##
c     ###############################################################
c
c
c     "polarea" computes the fraction of the side chain surface
c     area covered by either solvent or polar protein atoms
c
c     literature reference:
c
c     D. Eisenberg, M. Wesson and M. Yamashita, "Interpretation of
c     Protein Folding and Binding with Atomic Solvation Parameters",
c     Chemica Scripta, 29A, 217-221 (1989)
c
c     J. U. Bowie, R. Luthy and D. Eisenberg, "A Method to Identify
c     Protein Sequences That Fold into a Known Three-Dimensional
c     Structure", Science, 253, 164-170 (1991)
c
c
      subroutine polarea (nres,resatm,radius,schain,fpolar)
      implicit none
      include 'sizes.i'
      include 'math.i'
      include 'pdb.i'
      include 'sequen.i'
      integer maxdots
      parameter (maxdots=1000)
      integer i,j,k,m,n,ndots
      integer nres,narea,npolar
      integer resatm(2,maxres)
      integer nclose,close(maxatm)
      integer nexpose,nphilic
      real*8 xdiff,ydiff,zdiff
      real*8 dist2,near,near2
      real*8 xdot,ydot,zdot,dots(3,maxdots)
      real*8 area,polar,fpolar(maxres)
      real*8 rad,rad2,radius(maxatm)
      character*1 symbol
      logical exposed,philic
      logical atmpolar(maxatm)
      logical schain(maxatm)
c
c
c     locate a set of equally spaced unit vectors on a sphere
c
      ndots = maxdots
      call genun (ndots,dots)
c
c     make a list of the polar atoms for use below
c
      do i = 1, npdb
         symbol = atmnam(i)(2:2)
         if (symbol.eq.'O' .or. symbol.eq.'N' .or. symbol.eq.'A') then
            atmpolar(i) = .true.
         else
            atmpolar(i) = .false.
         end if
      end do
c
c     find Eisenberg's fractional polar area for each residue
c
      do i = 1, nres
         area = 0.0d0
         polar = 0.0d0
         fpolar(i) = 0.0d0
         do k = resatm(1,i), resatm(2,i)
            narea = 0
            nexpose = 0
            nphilic = 0
c
c     make a list of atoms that can overlap the current atom
c
            if (schain(k) .and. radius(k).ne.0.0d0) then
               nclose = 0
               do j = 1, npdb
                  if (j.ne.k .and. radius(j).ne.0.0d0) then
                     rad2 = (radius(k) + radius(j))**2
                     xdiff = xpdb(j) - xpdb(k)
                     ydiff = ypdb(j) - ypdb(k)
                     zdiff = zpdb(j) - zpdb(k)
                     dist2 = xdiff**2 + ydiff**2 + zdiff**2
                     if (dist2 .le. rad2) then
                        nclose = nclose + 1
                        close(nclose) = j
                     end if
                  end if
               end do
c
c     get coordinates for the equally spaced points on sphere
c
               do j = 1, ndots
                  xdot = xpdb(k) + radius(k)*dots(1,j)
                  ydot = ypdb(k) + radius(k)*dots(2,j)
                  zdot = zpdb(k) + radius(k)*dots(3,j)
c
c     check for point covered by the its own side chain atoms
c
                  do m = resatm(1,i), resatm(2,i)
                     rad = radius(m)
                     rad2 = rad**2
                     if (m .eq. k)  goto 10
                     if (rad .eq. 0.0d0)  goto 10
                     if (.not. schain(m))  goto 10
                     xdiff = xpdb(m) - xdot
                     ydiff = ypdb(m) - ydot
                     zdiff = zpdb(m) - zdot
                     dist2 = xdiff**2 + ydiff**2 + zdiff**2
                     if (dist2 .lt. rad2)  goto 30
   10                continue
                  end do
c
c     check for exposed area and area buried by hydrophilic atoms
c
                  exposed = .true.
                  philic = .false.
                  near = 1000.0d0
                  near2 = near**2
                  do m = 1, nclose
                     n = close(m)
                     rad = radius(n)
                     rad2 = rad**2
                     if (rad .ne. 0.0d0) then
                        xdiff = xpdb(n) - xdot
                        if (xdiff .gt. rad)  goto 20
                        ydiff = ypdb(n) - ydot
                        if (ydiff .gt. rad)  goto 20
                        zdiff = zpdb(n) - zdot
                        if (zdiff .gt. rad)  goto 20
                        dist2 = xdiff**2 + ydiff**2 + zdiff**2
                        if (dist2.lt.near2 .and. dist2.lt.rad2) then
                           exposed = .false.
                           near2 = dist2
                           near = sqrt(near2)
                           if (atmpolar(n)) then
                               philic = .true.
                           else
                               philic = .false.
                           end if
                        end if
   20                   continue
                     end if
                  end do
                  narea = narea + 1
                  if (exposed)  nexpose = nexpose + 1
                  if (philic)  nphilic = nphilic + 1
   30             continue
               end do
            end if
c
c     sum to give total area and polar area for the current atom
c
            area = area + 4.0d0*pi*radius(k)**2
     &                         * (dble(narea)/dble(ndots))
            npolar = nexpose + nphilic
            polar = polar + 4.0d0*pi*radius(k)**2
     &                         * (dble(npolar)/dble(ndots))
         end do
c
c     compute the fractional polar area for the whole residue
c
         if (area .gt. 0.0d0)  fpolar(i) = 100.0d0 * polar / area
         if (fpolar(i) .gt. 100.0d0)  fpolar(i) = 100.0d0
      end do
      return
      end
c
c
c     #################################################################
c     ##                                                             ##
c     ##  subroutine genun  --  get set of unit vectors over sphere  ##
c     ##                                                             ##
c     #################################################################
c
c
c     "genun" finds the coordinates of a set of at most "n" equally
c     spaced unit vectors on the surface of a sphere
c
c
      subroutine genun (ndots,dots)
      implicit none
      include 'math.i'
      integer i,j,ndots
      integer nequat,nvert,nu,nhor
      real*8 fi,fj,x,y,z,xy
      real*8 dots(3,ndots)
c
c
c     get coordinates for equally spaced points on a sphere
c
      nequat = int(sqrt(pi*dble(ndots)))
      nvert = nequat / 2
      if (nvert .lt. 1)  nvert = 1
      nu = 0
      do i = 0, nvert
         fi = (pi * dble(i)) / dble(nvert)
         z = cos(fi)
         xy = sin(fi)
         nhor = int(dble(nequat) * xy)
         if (nhor .lt. 1)  nhor = 1
         do j = 0, nhor-1
            fj = (2.0d0 * pi * dble(j)) / dble(nhor)
            x = cos(fj) * xy
            y = sin(fj) * xy
            if (nu .ge. ndots)  goto 10
            nu = nu + 1
            dots(1,nu) = x
            dots(2,nu) = y
            dots(3,nu) = z
         end do
      end do
   10 continue
      ndots = nu
      return
      end
