c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program infomake  --  build 3D-1D scoring table from PDB  ##
c     ##                                                            ##
c     ################################################################
c
c
      program infomake
      implicit none
      include 'sizes.i'
      include 'aminos.i'
      include 'iounit.i'
      integer maxpdb,maxresid,maxenvir
      parameter (maxpdb=50)
      parameter (maxresid=1000)
      parameter (maxenvir=18)
      integer i,j,k,m,ilist,ipdb,lname(maxpdb)
      integer length,freeunit,trimtext
      integer npdb,nseq,nsub,nsum,atype,ctype
      integer factor,increment
      integer namino(maxamino),nclass(maxenvir)
      integer nres(maxpdb),nmut(maxpdb)
      integer seqtyp(maxamino,maxresid,maxpdb)
      integer table(maxamino,maxenvir)
      real*8 area,bury(maxresid,maxpdb)
      real*8 fraction,fpolar(maxresid,maxpdb)
      real*8 pamino(maxamino),pclass(maxamino)
      real*8 epcut,pbcut,p12cut,b12cut,b23cut
      real*8 total,pcell,info(maxamino,maxenvir)
      real*8 noise,constant
      character*1 answer,letter
      character*1 seq(maxresid,maxpdb)
      character*3 seq3(maxresid)
      character*3 struct,fold(maxresid,maxpdb)
      character*3 class(maxenvir)
      character*60 listfile,pdbfile
      character*80 record,name(maxpdb)
      logical exist,build
      data class  / 'B1a','B1b','B1 ','B2a','B2b','B2 ',
     &              'B3a','B3b','B3 ','P1a','P1b','P1 ',
     &              'P2a','P2b','P2 ','Ea ','Eb ','E  ' /
c
c
c     open the listing of Protein Data Bank summaries
c
      call initial
      listfile = 'sleuth.dat'
   10 continue
      inquire (file=listfile,exist=exist)
      if (exist) then
         ilist = freeunit ()
         open (unit=ilist,file=listfile,status='old')
      else
         write (iout,20)
   20    format (/,' Enter File with List of SLEUTH Summaries :  ',$)
         read (input,30)  listfile
   30    format (a60)
         goto 10
      end if
c
c     read in the summary of each protein structure
c
      npdb = 0
      dowhile (.true.)
         read (ilist,40,end=290)  pdbfile
   40    format (a80)
         length = trimtext(pdbfile)
         if (length .eq. 0) then
            exist = .false.
         else
            inquire (file=pdbfile(1:length),exist=exist)
         end if
         if (exist) then
            ipdb = freeunit ()
            open (unit=ipdb,file=pdbfile(1:length),status='old')
            npdb = npdb + 1
            name(npdb) = pdbfile
            lname(npdb) = length
c
c     get the number of amino acids in the protein
c
   50       continue
            read (ipdb,60)  record
   60       format (a80)
            if (record(1:23) .eq. ' Protein Data Bank File') then
               read (ipdb,70)  nseq
   70          format (/,32x,i12)
            else
               goto 50
            end if
c
c     get the amino acid sequence of the protein
c
   80       continue
            read (ipdb,90)  record
   90       format (a80)
            if (record(1:20) .eq. ' Amino Acid Sequence') then
               read (ipdb,100)
  100          format (//)
               do i = 1, ((nseq-1)/10)+1
                  read (ipdb,110)  (seq3(j),j=10*i-9,min(10*i,nseq))
  110             format (11x,9(a3,2x),a3)
               end do
            else
               goto 80
            end if
c
c     translate 3-letter to 1-letter amino acid code
c
            do i = 1, nseq
               do j = 1, maxamino
                  seqtyp(j,i,npdb) = 0
               end do
            end do
            do i = 1, nseq
               seq(i,npdb) = ' '
               do j = 1, maxamino
                  if (seq3(i) .eq. amino(j)) then
                     seq(i,npdb) = amino1(j)
                     seqtyp(1,i,npdb) = j
                     goto 120
                  end if
               end do
  120          continue
            end do
c
c     get the Kabsch-Sander secondary structure assignments
c
  130       continue
            read (ipdb,140)  record
  140       format (a80)
            if (record(1:24) .eq. ' Kabsch-Sander Secondary') then
               read (ipdb,150)
  150          format (//)
               do i = 1, ((nseq-1)/10)+1
                  read (ipdb,160)  (fold(j,npdb),j=10*i-9,
     &                                 min(10*i,nseq))
  160             format (11x,9(a3,2x),a3)
               end do
            else
               goto 130
            end if
c
c     get the buried surface area and fractional polar area
c
  170       continue
            read (ipdb,180)  record
  180       format (a80)
            if (record(1:20) .eq. ' Buried Surface Area') then
               read (ipdb,190)
  190          format (//)
               do i = 1, nseq
                  read (ipdb,200)  bury(i,npdb),fpolar(i,npdb)
  200             format (26x,f6.1,20x,f6.1)
               end do
            else
               goto 170
            end if
c
c     get the allowed mutation information for this structure
c
            nsub = 0
  210       continue
            read (ipdb,220,end=260)  record
  220       format (a80)
            if (record(1:22) .eq. ' Allowed Substitutions') then
               read (ipdb,230)
  230          format ()
               dowhile (.true.)
                  read (ipdb,240,err=250,end=260)  i,letter
  240             format (i6,7x,a1)
                  do j = 1, maxamino
                     if (letter .eq. amino1(j)) then
                        do k = 1, maxamino
                           if (seqtyp(k,i,npdb) .eq. 0) then
                              nsub = nsub + 1
                              seqtyp(k,i,npdb) = j
                              goto 250
                           end if
                        end do
                     end if
                  end do
  250             continue
               end do
            else
               goto 210
            end if
  260       continue
c
c     finished with this PDB abstract file, so close it
c
            nres(npdb) = nseq
            nmut(npdb) = nsub
            if (npdb .eq. 1) then
               write (iout,270)
  270          format ()
            end if
            write (iout,280)  pdbfile(1:20),nseq,nsub
  280       format (' Reading :',5x,a20,2x,i8,' Residues',i8,
     &                 ' Mutations')
            close (unit=ipdb)
         end if
      end do
c
c     all the PDB abstract and mutation files have been processed
c
  290 continue
      nsum = 0
      do i = 1, npdb
         nsum = nsum + nres(i) + nmut(i)
      end do
      write (iout,300)  npdb,nsum
  300 format (/,' Processed',i6,' Structures with',i10,
     &           ' Residues/Mutations')
      close (unit=ilist)
c
c     count the total number of each standard amino acid type
c
      do i = 1, maxamino
         namino(i) = 0
      end do
      do i = 1, npdb
         do j = 1, nres(i)
            do k = 1, maxamino
               m = seqtyp(k,j,i)
               if (m .eq. 0)  goto 310
               namino(m) = namino(m) + 1
            end do
  310       continue
         end do
      end do
      nsum = 0
      do i = 1, 20
c     do i = 1, maxamino
         nsum = nsum + namino(i)
      end do
      do i = 1, maxamino
         pamino(i) = dble(namino(i)) / dble(nsum)
      end do
      write (iout,320)
  320 format (/,' Number of Residues of each Amino Acid Type :',/)
      do i = 1, maxamino
         write (iout,330)  amino(i),namino(i),100.0d0*pamino(i)
  330    format (10x,a3,5x,i6,5x,f6.2,' %')
      end do
c
c     get the buried surface area and fraction polar area cutoffs
c
  340 continue
      write (iout,350)
  350 format (/,' Enter the Buried Surface Area Cutoff Values :  ',$)
      read (input,360)  epcut,pbcut
  360 format (2f10.0)
      write (iout,370)
  370 format (/,' Enter a Partially Buried Fraction Polar Cutoff :  ',$)
      read (input,380)  p12cut
  380 format (f10.0)
      write (iout,390)
  390 format (/,' Enter the Fully Buried Fraction Polar Cutoffs :  ',$)
      read (input,400)  b12cut,b23cut
  400 format (2f10.0)
c
c     make a table of the number of each residue in each environment
c
      do i = 1, maxenvir
         do j = 1, maxamino
            table(j,i) = 0
         end do
      end do
      do i = 1, npdb
         do j = 1, nres(i)
            struct = fold(j,i)
            call upcase (struct)
            area = bury(j,i)
            fraction = fpolar(j,i)
            if (area .lt. epcut) then
               if (struct .eq. ' H ') then
                  ctype = 16
               else if (struct .eq. ' E ') then
                  ctype = 17
               else
                  ctype = 18
               end if
            else if (area .lt. pbcut) then
               if (fraction .lt. p12cut) then
                  if (struct .eq. ' H ') then
                     ctype = 10
                  else if (struct .eq. ' E ') then
                     ctype = 11
                  else
                     ctype = 12
                  end if
               else
                  if (struct .eq. ' H ') then
                     ctype = 13
                  else if (struct .eq. ' E ') then
                     ctype = 14
                  else
                     ctype = 15
                  end if
               end if
            else
               if (fraction .lt. b12cut) then
                  if (struct .eq. ' H ') then
                     ctype = 1
                  else if (struct .eq. ' E ') then
                     ctype = 2
                  else
                     ctype = 3
                  end if
               else if (fraction .lt. b23cut) then
                  if (struct .eq. ' H ') then
                     ctype = 4
                  else if (struct .eq. ' E ') then
                     ctype = 5
                  else
                     ctype = 6
                  end if
               else
                  if (struct .eq. ' H ') then
                     ctype = 7
                  else if (struct .eq. ' E ') then
                     ctype = 8
                  else
                     ctype = 9
                  end if
               end if
            end if
            do k = 1, maxamino
               atype = seqtyp(k,j,i)
               if (atype .eq. 0)  goto 410
               table(atype,ctype) = table(atype,ctype) + 1
            end do
  410       continue
         end do
      end do
c
c     count the total number of residues in each environment class
c
      nsum = 0
      do i = 1, maxenvir
         nclass(i) = 0
         do j = 1, 20
            nclass(i) = nclass(i) + table(j,i)
         end do
         nsum = nsum + nclass(i)
      end do
      do i = 1, maxenvir
         pclass(i) = dble(nclass(i)) / dble(nsum)
      end do
      write (iout,420)
  420 format (/,' Number of Residues in each Environmental Class :',/)
      do i = 1, maxenvir
         write (iout,430)  class(i),nclass(i),100.0d0*pclass(i)
  430    format (10x,a3,5x,i6,5x,f6.2,' %')
      end do
c
c     add an amount of "noise" to each frequency table value
c
      factor = 100
      noise = 0.0d0
      write (iout,440)
  440 format (/,' Enter the Fraction of Noise to be Added [0.0] :  ',$)
      read (input,450)  noise
  450 format (f10.0)
      constant = noise * dble(factor) * dble(nsum)
      do i = 1, maxenvir
         do j = 1, 20
            increment = nint(constant*pamino(j)*pclass(i))
            table(j,i) = factor*table(j,i) + increment
         end do
      end do
      nsum = 0
      do i = 1, maxenvir
         nclass(i) = 0
         do j = 1, 20
            nclass(i) = nclass(i) + table(j,i)
         end do
         nsum = nsum + nclass(i)
      end do
      do i = 1, maxenvir
         pclass(i) = dble(nclass(i)) / dble(nsum)
      end do
      do i = 1, 20
         namino(i) = 0
      end do
      do i = 1, maxenvir
         do j = 1, 20
            namino(j) = namino(j) + table(j,i)
         end do
      end do
      do i = 1, 20
         pamino(i) = dble(namino(i)) / dble(nsum)
      end do
c
c     write out the full residue occurance table values
c
      write (iout,460)
  460 format (/,' Frequency of Amino Acid-Environment Combinations :')
      write (iout,470)  (amino1(i),i=1,20)
  470 format (/,'Class',3x,19(a1,4x),a1,/)
      do i = 1, maxenvir
         write (iout,480)  class(i),(nint(table(j,i)
     &                                     /dble(factor)),j=1,20)
  480    format (a3,2x,20i5)
      end do
c
c     if any class is very rare, then don't build table
c
      build = .true.
      do i = 1, maxenvir
         if (pclass(i) .lt. 0.005d0)  build = .false.
      end do
c
c     build the scoring table of information values
c
      total = 0.0d0
      if (build) then
         do i = 1, maxenvir
            do j = 1, 20
               pcell = dble(max(table(j,i),factor)) / dble(nclass(i))
               info(j,i) = log(pcell/pamino(j))
               total = total + dble(table(j,i))/factor * info(j,i)
            end do
         end do
c
c     write out the full residue information table values
c
         write (iout,490)
  490    format (/,' 3D-to-1D Alignment Information Scoring Table :')
         write (iout,500)  epcut,pbcut,p12cut,b12cut,b23cut,noise
  500    format (/,'EPcut:',f6.1,3x,'PBcut:',f6.1,3x,'P12cut:',f6.1,3x,
     &              'B12cut:',f6.1,3x,'B13cut:',f6.1,3x,'Noise:',f6.2)
         write (iout,510)  (amino1(i),i=1,20)
  510    format (/,'Class',3x,19(a1,4x),a1,/)
         do i = 1, maxenvir
            write (iout,520)  class(i),(nint(100.0d0*info(j,i)),j=1,20)
  520       format (a3,2x,20i5)
         end do
         write (iout,530)  total
  530    format (/,' Total Information Value of Scoring Table :',f12.2)
      end if
c
c     decide whether to repeat the information table calculation
c
  540 continue
      write (iout,550)
  550 format (/,' Repeat for Different Cutoff Values',
     &           ' (Yes or [No]) :  ',$)
      read (input,560)  answer
  560 format (a1)
      call upcase (answer)
      if (answer .eq. 'Y')  goto 340
      end
