c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1991  by  Jay William Ponder  ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program sleuth  --  PDB sequence and substructure search  ##
c     ##                                                            ##
c     ################################################################
c
c
      program sleuth
      implicit none
      include 'sizes.i'
      include 'aminos.i'
      include 'iounit.i'
      integer maxpdb,maxresid,maxlist,maxfind
      parameter (maxpdb=2000)
      parameter (maxresid=2000)
      parameter (maxlist=500000)
      parameter (maxfind=40)
      integer i,j,k,m,ica,jca,last,line
      integer isave,ilist,ipdb,ires,ldir,site
      integer freeunit,trimtext
      integer npdb,nseq,nsum,nfind,nval,nmatch
      integer start(maxpdb),stop(maxpdb)
      integer nres(maxpdb),seqtyp(maxlist)
      integer ndist,idist(2,maxfind)
      integer length,lname(maxpdb),nchi(maxamino)
      real*8 phi(maxlist),psi(maxlist)
      real*8 omega(maxlist),chi(4,maxlist)
      real*8 phi0(maxfind),phi1(maxfind)
      real*8 psi0(maxfind),psi1(maxfind)
      real*8 philo,phihi,phival,psilo,psihi,psival
      real*8 xca(maxlist),yca(maxlist),zca(maxlist)
      real*8 bury(maxlist)
      real*8 dist0(maxfind),dist1(maxfind)
      real*8 dist,distlo,disthi
      character*1 seq(maxlist),letter
      character*1 find(maxfind),struct(maxfind),surface(maxfind)
      character*3 seq3(maxresid),fold(maxresid)
      character*3 fold1(maxlist),fold2(maxlist)
      character*20 pdbname,define(maxfind)
      character*40 string
      character*60 listfile,pdbfile,datadir
      character*80 record,name(maxpdb)
      logical exist,done
      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     open the listing of Protein Data Bank abstract 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     set a default directory to be checked for abstract files
c
      datadir = '/user/ponder/sleuth/data/'
      ldir = trimtext (datadir)
c
c     read in the abstract summary of each protein structure
c
      npdb = 0
      last = 0
      dowhile (.true.)
         read (ilist,40,end=340)  pdbname
   40    format (a20)
         pdbfile = pdbname
         length = trimtext (pdbfile)
         if (length .eq. 0) then
            exist = .false.
         else
            inquire (file=pdbfile(1:length),exist=exist)
            if (.not. exist) then
               pdbfile = datadir(1:ldir)//pdbfile(1:length)
               length = length + ldir
               inquire (file=pdbfile(1:length),exist=exist)
            end if
         end if
         if (exist) then
            ipdb = freeunit ()
            open (unit=ipdb,file=pdbfile(1:length),status='old')
            npdb = npdb + 1
            name(npdb) = pdbname
            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:21) .eq. ' Number of Amino Acid') then
               string = record(33:44)
               read (string,*)  nseq
               start(npdb) = last + 1
               last = last + nseq
               stop(npdb) = last
            else
               goto 50
            end if
c
c     get the amino acid sequence of the protein
c
   70       continue
            read (ipdb,80)  record
   80       format (a80)
            if (record(1:20) .eq. ' Amino Acid Sequence') then
               read (ipdb,90)
   90          format (//)
               do i = 1, ((nseq-1)/10)+1
                  read (ipdb,100)  (seq3(j),j=10*i-9,min(10*i,nseq))
  100             format (11x,9(a3,2x),a3)
               end do
            else
               goto 70
            end if
c
c     translate 3-letter to 1-letter amino acid code
c
            do i = start(npdb), stop(npdb)
               ires = i - start(npdb) + 1
               seq(i) = ' '
               do j = 1, maxamino
                  if (seq3(ires) .eq. amino(j)) then
                     seq(i) = amino1(j)
                     seqtyp(i) = j
                     goto 110
                  end if
               end do
  110          continue
            end do
c
c     get the phi, psi, omega and chi angle values
c
  120       continue
            read (ipdb,130)  record
  130       format (a80)
            if (record(22:36) .eq. 'Dihedral Angles') then
               read (ipdb,140)
  140          format (//)
               do i = start(npdb), stop(npdb)
                  k = nchi(seqtyp(i))
                  read (ipdb,150)  phi(i),psi(i),omega(i),
     &                             (chi(j,i),j=1,k)
  150             format (19x,7f8.1)
               end do
            else
               goto 120
            end if
c
c     get the alpha carbon atom positions
c
  160       continue
            read (ipdb,170)  record
  170       format (a80)
            if (record(1:23) .eq. ' Alpha Carbon Cartesian') then
               read (ipdb,180)
  180          format (//)
               do i = start(npdb), stop(npdb)
                  read (ipdb,190)  xca(i),yca(i),zca(i)
  190             format (19x,3f12.3)
               end do
            else
               goto 160
            end if
c
c     get the Richards-Kundrot secondary structure assignments
c
  200       continue
            read (ipdb,210)  record
  210       format (a80)
            if (record(1:27) .eq. ' Richards-Kundrot Secondary') then
               read (ipdb,220)
  220          format (//)
               do i = 1, ((nseq-1)/10)+1
                  read (ipdb,230)  (fold(j),j=10*i-9,min(10*i,nseq))
  230             format (11x,9(a3,2x),a3)
               end do
            else
               goto 200
            end if
            do i = start(npdb), stop(npdb)
               ires = i - start(npdb) + 1
               fold1(i) = fold(ires)
            end do
c
c     get the Kabsch-Sander secondary structure assignments
c
  240       continue
            read (ipdb,250)  record
  250       format (a80)
            if (record(1:24) .eq. ' Kabsch-Sander Secondary') then
               read (ipdb,260)
  260          format (//)
               do i = 1, ((nseq-1)/10)+1
                  read (ipdb,270)  (fold(j),j=10*i-9,min(10*i,nseq))
  270             format (11x,9(a3,2x),a3)
               end do
            else
               goto 240
            end if
            do i = start(npdb), stop(npdb)
               ires = i - start(npdb) + 1
               call upcase (fold(ires))
               fold2(i) = '   '
               if (fold(ires) .eq. ' H ')  fold2(i) = ' H '
               if (fold(ires) .eq. ' E ')  fold2(i) = ' S '
               if (fold(ires) .eq. ' T ')  fold2(i) = ' T '
               if (fold(ires) .eq. ' G ')  fold2(i) = ' 3 '
               if (fold(ires) .eq. ' B ')  fold2(i) = ' C '
               if (fold(ires) .eq. ' I ')  fold2(i) = ' C '
               if (fold(ires) .eq. ' S ')  fold2(i) = ' C '
            end do
c
c     get the percentage of surface area buried
c
  280       continue
            read (ipdb,290)  record
  290       format (a80)
            if (record(1:20) .eq. ' Buried Surface Area') then
               read (ipdb,300)
  300          format (//)
               do i = start(npdb), stop(npdb)
                  read (ipdb,310)  bury(i)
  310             format (39x,f6.1)
               end do
            else
               goto 280
            end if
c
c     finished with this PDB file, so close it
c
            nres(npdb) = nseq
            if (npdb .eq. 1) then
               write (iout,320)
  320          format ()
            end if
            write (iout,330)  pdbname(1:20),nseq
  330       format (' Reading :',5x,a20,i10,' Residues')
            close (unit=ipdb)
         end if
      end do
c
c     all the PDB files have been processed
c
  340 continue
      nsum = 0
      do i = 1, npdb
         nsum = nsum + nres(i)
      end do
      write (iout,350)  npdb,nsum
  350 format (/,' Processed',i6,' PDB Files containing',i8,' Residues')
      close (unit=ilist)
c
c     locate the amino acid sequence to be searched for
c
      done = .false.
      dowhile (.not. done)
         write (iout,360)
  360    format (/,' Enter Amino Acid Search Sequence :   ',$)
         read (input,370)  string
  370    format (a40)
         call upcase (string)
         nfind = 0
         do i = 1, 40
            if (string(i:i) .ne. ' ') then
               nfind = nfind + 1
               find(nfind) = string(i:i)
            end if
         end do
         if (nfind .eq. 0)  done = .true.
c
c     get amino acids for user-defined types
c
         if (.not. done) then
            do i = 1, nfind
               if (find(i) .eq. '*') then
                  write (iout,380)  i
  380             format (' Amino Acids for Position',i4,' :       ',$)
                  read (input,390)  define(i)
  390             format (a20)
                  call upcase (define(i))
               end if
            end do
c
c     get any residue structural type preferences
c
            do i = 1, nfind
               struct(i) = ' '
            end do
            write (iout,400)
  400       format (' Enter Residue Structural Types :     ',$)
            read (input,410)  record
  410       format (a80)
            length = trimtext (record)
            do i = 1, length
               struct(i) = record(i:i)
            end do
c
c     get any residue accessible surface preferences
c
            do i = 1, nfind
               surface(i) = ' '
            end do
            write (iout,420)
  420       format (' Enter Residue Accessibility Types :  ',$)
            read (input,430)  record
  430       format (a80)
            length = trimtext (record)
            do i = 1, length
               surface(i) = record(i:i)
            end do
c
c     get any phi-psi dihedral angle constraints
c
            do i = 1, nfind
               phi0(i) = -180.0d0
               phi1(i) = 180.0d0
               psi0(i) = -180.0d0
               psi1(i) = 180.0d0
            end do
  440       continue
            write (iout,450)
  450       format (' Enter a PHI-PSI Angle Constraint :   ',$)
            read (input,460)  record
  460       format (a80)
            line = 0
            read (record,*,err=470,end=470)  line,philo,phihi,
     &                                       psilo,psihi
  470       continue
            if (line .ne. 0) then
               phi0(line) = philo
               phi1(line) = phihi
               psi0(line) = psilo
               psi1(line) = psihi
               goto 440
            end if
c
c     get any alpha carbon distance constraints
c
            ndist = 0
  480       continue
            write (iout,490)
  490       format (' Enter Alpha Carbon Distance Constraint :  ',$)
            read (input,500)  record
  500       format (a80)
            ica = 0
            read (record,*,err=510,end=510)  ica,jca,distlo,disthi
  510       continue
            if (ica .ne. 0) then
               ndist = ndist + 1
               idist(1,ndist) = ica
               idist(2,ndist) = jca
               dist0(ndist) = distlo * distlo
               dist1(ndist) = disthi * disthi
               goto 480
            end if
c
c     open a file to contain a summary of current search matches
c
            isave = freeunit ()
            inquire (file='sleuth.out',exist=exist)
            if (exist) then
               open (unit=isave,file='sleuth.out',status='old')
               close (unit=isave,status='delete')
            end if
            open (unit=isave,file='sleuth.out',status='new')
c
c     find all occurances of the specified search data
c
            nmatch = 0
            do i = 1, npdb
               do j = start(i), stop(i)-nfind+1
                  do k = 1, nfind
                     site = j + k - 1
c
c     check for match with amino acid pattern
c
                     letter = seq(site)
                     if (find(k) .eq. 'X') then
                        continue
                     else if (find(k) .eq. '+') then
                        if (letter.ne.'R' .and. letter.ne.'K') then
                           goto 600
                        end if
                     else if (find(k) .eq. '-') then
                        if (letter.ne.'D' .and. letter.ne.'E') then
                           goto 600
                        end if
                     else if (find(k) .eq. '*') then
                        do m = 1, 20
                           if (letter .eq. define(k)(m:m)) then
                              goto 520
                           end if
                        end do
                        goto 600
  520                   continue
                     else if (find(k) .ne. letter) then
                        goto 600
                     end if
c
c     check for match with residue structural type preference
c
                     if (struct(k) .eq. ' ') then
                        continue
                     else if (index('PXHTSC3',struct(k)) .ne. 0) then
                        letter = struct(k)
                        if (index(fold1(site),letter).eq.0 .and.
     &                      index(fold2(site),letter).eq.0)  goto 600
                     else if (index('pxhtsc',struct(k)) .ne. 0) then
                        letter = struct(k)
                        call upcase (letter)
                        if (index(fold1(site),letter).ne.0 .or.
     &                      index(fold2(site),letter).ne.0)  goto 600
                     end if
c
c     check for match with residue accessible surface preference
c
                     if (surface(k) .eq. ' ') then
                        continue
                     else if (surface(k) .eq. 'B') then
                        if (bury(site) .lt. 80.0d0)  goto 600
                     else if (surface(k) .eq. 'b') then
                        if (bury(site) .ge. 80.0d0)  goto 600
                     else if (surface(k) .eq. 'E') then
                        if (bury(site) .gt. 20.0d0)  goto 600
                     else if (surface(k) .eq. 'e') then
                        if (bury(site) .le. 20.0d0)  goto 600
                     end if
c
c     check for a chain break within the proposed match
c
                     if (index(fold1(site),'X') .ne. 0) then
                        if (k .ne. 1) then
                           if (index(fold1(site-1),'X') .ne. 0) then
                              goto 600
                           end if
                        end if
                     end if
c
c     retrieve the phi-psi values for this residue
c
                     phival = phi(site)
                     psival = psi(site)
c
c     check for match with phi-psi angle ranges
c
                     if (phi0(k) .lt. phi1(k)) then
                        if (phival .lt. phi0(k) .or.
     &                      phival .gt. phi1(k))  goto 600
                     else
                        if (phival .lt. phi0(k) .and.
     &                      phival .gt. phi1(k))  goto 600
                     end if
                     if (psi0(k) .lt. psi1(k)) then
                        if (psival .lt. psi0(k) .or.
     &                      psival .gt. psi1(k))  goto 600
                     else
                        if (psival .lt. psi0(k) .and.
     &                      psival .gt. psi1(k))  goto 600
                     end if
                  end do
c
c     check for match with alpha carbon pair distances
c
                  do k = 1, ndist
                     ica = idist(1,k) + j - start(i)
                     jca = idist(2,k) + j - start(i)
                     dist = (xca(ica)-xca(jca))**2 +
     &                      (yca(ica)-yca(jca))**2 +
     &                      (zca(ica)-zca(jca))**2
                     if (dist.lt.dist0(k) .or.
     &                   dist.gt.dist1(k))  goto 600
                  end do
c
c     print info about the match which has been found
c
                  nmatch = nmatch + 1
                  ires = j - start(i) + 1
                  write (iout,530)  nmatch,name(i)(1:12),
     &                              ires,ires+nfind-1
  530             format (/,' Pattern Match:',i7,9x,'File: ',a12,5x,
     &                       'Residue: ',i5,' to',i5,/)
                  do k = j, j+nfind-1
                     nval = nchi(seqtyp(k))
                     if (nval .eq. 0) then
                        write (iout,540)  seq(k),phi(k),psi(k),
     &                                    omega(k),fold1(k),
     &                                    fold2(k),bury(k)
  540                   format (4x,a1,2x,3f7.1,30x,a3,2x,a3,f7.1)
                     else if (nval .eq. 1) then
                        write (iout,550)  seq(k),phi(k),psi(k),
     &                                    omega(k),chi(1,k),
     &                                    fold1(k),fold2(k),bury(k)
  550                   format (4x,a1,2x,4f7.1,23x,a3,2x,a3,f7.1)
                     else if (nval .eq. 2) then
                        write (iout,560)  seq(k),phi(k),psi(k),
     &                                    omega(k),(chi(m,k),m=1,2),
     &                                    fold1(k),fold2(k),bury(k)
  560                   format (4x,a1,2x,5f7.1,16x,a3,2x,a3,f7.1)
                     else if (nval .eq. 3) then
                        write (iout,570)  seq(k),phi(k),psi(k),
     &                                    omega(k),(chi(m,k),m=1,3),
     &                                    fold1(k),fold2(k),bury(k)
  570                   format (4x,a1,2x,6f7.1,9x,a3,2x,a3,f7.1)
                     else if (nval .eq. 4) then
                        write (iout,580)  seq(k),phi(k),psi(k),
     &                                    omega(k),(chi(m,k),m=1,4),
     &                                    fold1(k),fold2(k),bury(k)
  580                   format (4x,a1,2x,7f7.1,2x,a3,2x,a3,f7.1)
                     end if
                  end do
                  write (isave,590)  nmatch,(seq(k),k=j,j+nfind-1),
     &                               ires,ires+nfind-1,name(i)(1:4)
  590             format (6x,i5,5x,<nfind>a1,10x,2i5,5x,a4)
c
c     jump to here when a match has failed for any reason
c
  600             continue
               end do
            end do
            write (iout,610)  nmatch
  610       format (/,' Total Number of Pattern Matches for this',
     &                 ' Search :',i7)
            close (unit=isave)
         end if
      end do
      end
