c
c
c     ################################################################
c     ##                                                            ##
c     ##  subroutine secstr  --  Kabsch-Sander secondary structure  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "secstr" computes an extended Kabsch-Sander secondary structure
c     assignment for in input Brookhaven PDB file as a simple string
c     of one-letter codes:
c
c          B      residue in isolated beta-bridge
c          E      extended strand, participates in beta-ladder
c          G      3-helix (3/10 helix)
c          H      4-helix (alpha-helix)
c          I      5-helix (pi-helix)
c          S      bend
c          T      hydrogen-bonded turn
c          e      extension of beta-strand
c          g      extension of 3/10 helix
c          h      extension of alpha-helix
c
c     literature references :
c
c     W. Kabsch and C. Sander, "Dictionary of Protein Secondary
c     Structure: Pattern Recognition of Hydrogen-Bonded and
c     Geometrical Features, Biopolymers, 22, 2577-2637 (1983)
c
c     R.A. Laskowski, M.W. MacArthur, D.S. Moss and J.M. Thornton,
c     PROCHECK: a Program to Check the Stereochemical Quality of
c     Protein Structures, J. Appl. Cryst., 26, 283-291 (1993)
c
c
      subroutine secstr (pdbname,nres,x,y,z,cai,fold)
      implicit none
      include 'iounit.i'
      integer i,j,k
      integer nres,cai(*)
      real*8 x(*),y(*),z(*)
      real*8 dx,dy,dz,d2
      character*3 fold(*)
      character*60 pdbname
      logical done
c
c     variables local the current routine
c
      include 'sstruc.i'
      integer seqcod(maxrsd),seqlen,hbond(maxbnd,maxrsd),
     &        chnsz(maxchn),chnct,bridpt(2,maxrsd),hedlen,
     &        dspart(maxrsd),chnend(maxchn)
      integer nmcatm(maxrsd),nscatm(maxrsd)
      real    mcbval(maxrsd),scbval(maxrsd)
      real    bvalue(maxrsd)
      real    mcaaat(ncoord,mnchna,maxrsd), hbonde(maxbnd,maxrsd),
     &        scaaat(ncoord,sdchna,maxrsd), mcangs(nmnang,maxrsd),
     &        scangs(nsdang,maxrsd), cadsts(11,maxrsd),
     &        dsdsts(3,maxrsd)
      character aacod1*(abetsz), aacod3*(3*abetsz), atname*(4*mnchna-4),
     &          ssbond(nstruc,maxrsd)*1, sssumm(maxrsd)*1,
     &          summpl(maxrsd)*1, seqbcd(maxrsd)*6,
     &          autsum(maxrsd)*1, ssord*21, brksym(maxrsd)*1,
     &          headln*132, altcod(maxrsd)*1, aastd(maxrsd)*1,
     &          atpres(3,maxrsd)*1, thmprs(maxrsd)*1, hetcod*63,
     &          outsty*1, dsdef(maxrsd)*1, dudcod*105, modcod*(nmod*3),
     &          stdmod*(nmod+1), nstdmd*(nmod+1), modpl*(nmod),
     &          batnam*(4*mnchna+4)
      character*(fnamln) finam, brknam, outnam, outdir
      logical atomin(mnchna,maxrsd), scatin(sdchna,maxrsd), caonly,
     &        readok, astruc
      data aacod1(1:29)  /'ABCDEFGHIXKLMNXPQRSTXVWXYZX--'/
      data aacod3( 1:39) /'ALAASXCYSASPGLUPHEGLYHISILEXXXLYSLEUMET'/,
     &     aacod3(40:78) /'ASNXXXPROGLNARGSERTHRXXXVALTRPXXXTYRGLX'/,
     &     aacod3(79:87) /'UNKPCAINI'/
      data hetcod(1:39)  /'AIBPHLSECALMMPRFRDPYRLYMGLMPPHPGLOLETPH'/,
     &     hetcod(40:63) /'ABANLEB2VB2IB1FBNOB2AB2F'/
      data modcod(1:39)  /'ACEANIBZOCBZCLTFORNH2PHORHATFATOS NHMYR'/,
     &     modcod(40:45) /'BOCMSU'/
      data dudcod( 1:39) /'  A  C  G  T  U1MA5MCOMC1MG2MGM2G7MGOMG'/,
     &     dudcod(40:78) /' YG  I +UH2U5MUPSU +C +AGALAGLGCUASGMAN'/
     &     dudcod(79:105) /'GLCCEGG4SAGSNAGGLSNGSNAMEXC'/
      data stdmod /'ABCDEFGHIJKLMNZY'/
      data nstdmd /'abcdefghijklmnzy'/
      data modpl  /'+0++++0++++0+++'/
      data ssord /'HEheBbGgIiJKLMNOPTtS '/
      data atname /' N   CA  C   O  '/
      data batnam /' N   CA  B       O1  O2 '/
      data atomin /allatm * .false./, mcaaat /allcrd * 0/
c
c
c     initialize some stuff and set the PDB filename
c
      brknam = pdbname
      do i = 1, maxrsd
         mcbval(i) = 0.0
         scbval(i) = 0.0
         nmcatm(i) = 0
         nscatm(i) = 0
      end do
      open (unit=brkfi,file=brknam,status='old')
c
c     read in the Brookhaven PDB file
c
      call rdbrk (mcaaat,atname,atomin,aacod3,hetcod,seqcod,
     &            seqbcd,scaaat,scatin,altcod,aastd,atpres,
     &            thmprs,caonly,astruc,headln,hedlen,readok,
     &            dudcod,modcod,stdmod,nstdmd,modpl,seqlen,
     &            batnam,bvalue,mcbval,scbval,nmcatm,nscatm)
c
c     look for breaks across the peptide bonds
c
      call chnbrk (mcaaat,atomin,seqbcd,brksym,chnsz,chnct,
     &             chnend,caonly,seqlen)
c
c     create hydrogen atoms on the main chain nitrogens
c
      call creath (mcaaat,atomin,chnsz,chnct,seqlen)
c
c     determine all hydrogen bonds
c
      call mkhbnd (mcaaat,atomin,hbond,hbonde,seqcod,chnend,seqlen)
c
c     find main chain torsion angles
c
      call mkangl (mcaaat,mcangs,atomin,chnsz,chnct,caonly,
     &             seqlen,scaaat,scatin)
c
c     determine turns, bridges and sheets
c
      call mktb (hbond,ssbond,mcangs,bridpt,chnend,seqlen)
c
c     determine bend residues
c
      call mkbend (mcangs,ssbond,seqlen)
c
c     make Kabsch and Sander type secondary structure summary
c
      call mksumm (ssbond,sssumm,summpl,seqlen)
c
c     set the secondary structure that is returned to SLEUTH,
c     make sure corresponding alpha carbon coordinates match
c
      if (nres .gt. seqlen) then
          write (iout,10)
   10     format (/,' SECSTR  --  Severe Failure in Reading',
     &               ' the PDB File')
         stop
      end if
      j = 0
      do i = 1, nres
         done = .false.
         k = cai(i)
         if (x(k)+y(k)+z(k) .eq. 0.0d0) then
            fold(i) = '   '
            done = .true.
         end if
         dowhile (.not. done)
            j = j + 1
            dx = mcaaat(1,calph,j) - x(k)
            dy = mcaaat(2,calph,j) - y(k)
            dz = mcaaat(3,calph,j) - z(k)
            d2 = dx*dx + dy*dy + dz*dz
            if (d2 .lt. 0.01d0) then
               done = .true.
               fold(i) = ' '//summpl(j)//' '
            else if (j .ge. seqlen) then
               write (iout,20)  i
   20          format (/,' SECSTR  --  Failure to Match Alpha Carbon',
     &                    ' Coordinates at Residue',i5)
               stop
            end if
         end do
      end do
      return
      end
c
c
c     ###############################################################
c     ##                                                           ##
c     ##  subroutine rdbrk  --  read a Brookhaven PDB file format  ##
c     ##                                                           ##
c     ###############################################################
c
c
      subroutine rdbrk (mcaaat,atname,atomin,aacod3,hetcod,seqcod,
     &                  seqbcd,scaaat,scatin,altcod,aastd,atpres,
     &                  thmprs,caonly,astruc,headln,hedlen,readok,
     &                  dudcod,modcod,stdmod,nstdmd,modpl,seqlen,
     &			batnam,bvalue,mcbval,scbval,nmcatm,nscatm)
      include 'sstruc.i'
      integer seqlen,seqcod(maxrsd),hedlen
      integer aaconv
      integer nmcatm(maxrsd),nscatm(maxrsd)
      real bvalue(maxrsd)
      real mcbval(maxrsd),scbval(maxrsd)
      real mcaaat(ncoord,mnchna,maxrsd),scaaat(ncoord,sdchna,maxrsd)
      character aacod3*(abetsz*3),atname*(mnchna*4-4),seqbcd(maxrsd)*6
      character headln*132,altcod(maxrsd)*1,aastd(maxrsd)*1
      character atpres(3,maxrsd)*1,thmprs(maxrsd)*1,hetcod*63
      character modcod*(nmod*3),stdmod*(nmod+1),nstdmd*(nmod+1)
      character modpl*(nmod),dudcod*105,batnam*(mnchna*4+4)
      logical atomin(mnchna,maxrsd),scatin(sdchna,maxrsd)
      logical caonly,astruc,readok
c
c     local variables
c
      integer aapln
      integer trimtext,resfnd
      character allspc*16
      parameter (aapln = 13)
      parameter (allspc = '                ')
      real acoord(ncoord),oldocc,occup,therm,oldthm
      logical altset,xney
      integer atcnt,attype,i,aacnt,wkpl
      integer clslen,cmplen,soulen,sctype,altpos(sideaa),respl
      integer scatct,thmcnt,aacode,nscats(sideaa),hedend,sqrsct
      character aain(maxrsd)*3,atmnam*4,resnam*3,altloc*1
      character brkrec*80,oldchn*1,seqnum*6
      character innum*6,keywd*(keylen),oldatm*4,setloc*1
      character class*40,compnd*50,source*50,brcode*4,cbet*4
      character scname(sideaa)*16,altnam(3)*16,contch*1,incode*4
      character wkstr*50,inseqr*150,setatm*4
      data scname(1)  /allspc/, scname(2) /allspc/
      data scname(3)  /' SG             '/
      data scname(4)  /' CG  OD1 OD2    '/
      data scname(5)  /' CG  CD  OE1 OE2'/
      data scname(6)  /' CG  CD1 CD2    '/, scname(7) /allspc/
      data scname(8)  /' CG  ND1 CD2    '/
      data scname(9)  /' CG1 CD1        '/, scname(10) /allspc/
      data scname(11) /' CG  CD  CE  NZ '/
      data scname(12) /' CG  CD1 CD2    '/
      data scname(13) /' CG  SD  CE     '/
      data scname(14) /' CG  OD1 ND2    '/, scname(15) /allspc/
      data scname(16) /' CG             '/
      data scname(17) /' CG  CD  OE1 NE2'/
      data scname(18) /' CG  CD  NE  CZ '/
      data scname(19) /' OG             '/
      data scname(20) /' OG1 CG2        '/, scname(21) /allspc/
      data scname(22) /' CG1 CG2        '/
      data scname(23) /' CG  CD1 CD2    '/, scname(24) /allspc/
      data scname(25) /' CG  CD1 CD2    '/
      data scname(26) /allspc/, scname(27) /allspc/
      data scname(28) /' CG  CD  OE     '/
      data scname(29) /' CG  CD  CE  NZ '/
      data scname(30) /' CB1 CB2        '/
      data scname(31) /' CG  CD1 CD2    '/
      data scname(32) /'SEG  OD1 OD2    '/
      data scname(33) /' CM             '/
      data scname(34) /' SG             '/
      data scname(35) /' CG  CD1 CD2    '/
      data scname(36) /'                '/
      data scname(37) /' CG  CD  CE  NZ '/
      data scname(38) /' CM             '/
      data scname(39) /' CG  CD1 CD2    '/
      data scname(40) /'                '/
      data scname(41) /' CG  CD1 CD2    '/
      data scname(42) /' CG  CD1 CD2    '/
      data scname(43) /' CG             '/
      data scname(44) /' CG  CD  CE     '/
      data scname(45) /' CG1 CG2        '/
      data scname(46) /' CG1 CD1        '/
      data scname(47) /' CG  CD1 CD2    '/
      data scname(48) /' CG  CD  CE     '/
      data scname(49) /allspc/, scname(50) /' CG  CD1 CD2    '/
      data altpos / 7*0, 1, 5*0, 2, 2*0, 3, 12*0, 13*0, 8*0/
      data altnam(1)  /' CG  AD1 AD2    '/
      data altnam(2)  /' CG  AD1 AD2    '/
      data altnam(3)  /' CG  CD  AE1 AE2'/
      data nscats / 1, 4, 2, 4, 5, 7, 0, 6, 4, -1, 5, 4, 4, 4, -1,
     &              3, 5, 7, 2, 3, -1, 3, 10, -1, 8, 5, -1, 4, 12,
     &              2, 7, 4, 2, 2, 7, 8, 6, 1, 10, 3, 5, 9, 2,  4,
     &              3, 4, 7, 4, 1, 7/
      data cbet /' CB '/
      save scname,altnam,altpos,nscats,cbet
c
c
c     routine to read the brookhaven data file and extract the sequence
c     and the atom names and coordinates. Checking is also done for
c     residue compatibility between sequence and atom. C-alpha only
c     files are flagged. The sequence residues are expected together,
c     then the secondary structure records (sheets together), and then
c     the atom records which are expected in residue order.
c
      seqlen = 0
      hedlen = 0
      inseqr = space
      sqrsct = 0
      brcode = '    '
      astruc = .false.
   10 continue
      read (brkfi,'(A)',end=270)  brkrec
      keywd = brkrec(1:keylen)
      if (keywd .eq. seqkey)  goto 20
      if (keywd .eq. hedkey)  goto 50
      if (keywd .eq. cmpkey)  goto 70
      if (keywd .eq. soukey)  goto 90
      if (keywd .eq. atmkey)  goto 110
      if (keywd .eq. htakey)  goto 110
c
c     the following if clause was present in the original code, and
c     effectively aborts if a MODEL keyword (ie, NMR model structure)
c     is found; commented to cause NMR structures to be processed
c     as single long "chain", JWP, December 1994
c
c     if (keywd .eq. modkey) then
c        readok = .false.
c        seqlen = 0
c        go to 270
c     end if
      if (keywd .eq. hlxkey .or. keywd .eq. shtkey .or.
     &    keywd .eq. trnkey .or. keywd .eq. dsfkey) then
         if (.not. astruc) then
c           open (unit=authfi,status='scratch')
            open (unit=authfi,status='old')
            astruc = .true.
         end if
         write (authfi,'(a)')  brkrec
      end if
      goto 10
   20 continue
      read (brkrec,30)  (aain(i),i=1,aapln)
   30 format (18x,13(x,a3))
      do 40, i = 1, aapln
         if (aain(i) .ne. '   ') then
            if (resfnd(inseqr,aain(i)) .eq. 0) then
               sqrsct = sqrsct + 1
               inseqr((sqrsct-1)*3+1:sqrsct*3) = aain(i)
            end if
         end if
   40 continue
      goto 10
c
c     set up header records
c
   50 continue
      read (brkrec,60)  contch,wkstr,incode
   60 format (9x,a1,a40,12x,a4)
      if (contch .eq. space) then
         brcode = incode
         class = wkstr
         clslen = trimtext (class)
      end if
      goto 10
   70 continue
      read (brkrec,80)  contch,wkstr
   80 format (9x,a1,a50)
      if (contch .eq. space) then
         compnd = wkstr
         cmplen = trimtext (compnd)
      end if
      goto 10
   90 continue
      read (brkrec,100)  contch,wkstr
  100 format (9x,a1,a50)
      if (contch .eq. space) then
         source = wkstr
         soulen = trimtext (source)
      end if
      goto 10
c
c     set up header if any exists
c
  110 continue
      headln(1:6) = brcode // '  '
      hedlen = 6
      if (clslen .gt. 0) then
         headln(7:8+clslen) = class(1:clslen)//'  '
         hedlen = hedlen + clslen + 2
      end if
      if (cmplen .gt. 0) then
         hedend = hedlen + cmplen + 2
         if (hedend .gt. 132)  hedend = 132
         headln(hedlen+1:hedend) = compnd(1:cmplen)//'  '
         hedlen = hedend
      end if
      if (soulen .gt. 0) then
         hedend = hedlen + soulen + 2
         if (hedend .gt. 132)  hedend = 132
         headln(hedlen+1:hedend)  = source(1:soulen)
         hedlen = hedend
      end if
      if (hedlen .gt. 132)  hedlen = 132
c
c     look for next atom records to process
c
  120 continue
      if (brkrec(1:keylen) .ne. atmkey .and.
     &    brkrec(1:keylen) .ne. htakey) then
         read (brkfi,'(A)',end=270)  brkrec
         goto 120
      end if
c
c     have first atom record for an aa
c     for each aa get the seqnum and resnam and check against the
c     read in sequence; if ok initialise to no atoms present and
c     get the coords
c
      aacnt = 0
      do 130 i = 1, maxrsd
         aastd(i) = space
  130 continue
      read (brkrec,140)  oldchn
  140 format (21x,a1)
  150 continue
      keywd = brkrec(1:keylen)
      aacode = 0
      altset = .false.
      setatm = '    '
      read (brkrec,160)  oldatm,setloc,resnam,seqnum,oldocc,oldthm
  160 format (12x,a4,a1,a3,1x,a6,27x,2f6.2)
      if (sqrsct.ne.0 .and. resfnd(inseqr,resnam).eq.0)  goto 240
      if (resfnd(dudcod,resnam) .ne. 0)  goto 240
      respl = resfnd(modcod,resnam)
      if (respl .ne. 0) then
         respl = (respl-1)/3 + 1
         if (modpl(respl:respl) .eq. '+') then
            aastd(aacnt+1) = stdmod(respl:respl)
         else
            if (aastd(aacnt) .eq. 'S') then
               aastd(aacnt) = stdmod(respl:respl)
            else
               if (aastd(aacnt) .eq. 'O') then
                  aastd(aacnt) = nstdmd(respl:respl)
               else
                  if (llt(aastd(aacnt),'a')) then
                     aastd(aacnt) = stdmod(nmod+1:nmod+1)
                  else
                     aastd(aacnt) = nstdmd(nmod+1:nmod+1)
                  end if
               end if
            end if
         end if
         goto 240
      end if
      aacode = aaconv(resnam,aacod3,hetcod)
      if (aacode .le. 0)  goto 240
      aacnt = aacnt + 1
      if (aastd(aacnt) .eq. space) then
         aastd(aacnt) = 'S'
         if (aacode .gt. stdaa)  aastd(aacnt) = 'O'
      else
         if (aacode .gt. stdaa) then
            wkpl = index(stdmod,aastd(aacnt))
            aastd(aacnt) = nstdmd(wkpl:wkpl)
         end if
      end if
      seqcod(aacnt) = aacode
      seqbcd(aacnt) = seqnum
      atcnt = 0
      scatct = 0
      thmcnt = 0
      thmprs(aacnt) = space
      do i = 1, mnchna
         atomin(i,aacnt) = .false.
      end do
      do i = 1, sdchna
         scatin(i,aacnt) = .false.
      end do
      do i = 1, 3
         atpres(i,aacnt) = space
      end do
c
c     process coordinates find atom type and if main chain set in coords
c
  200 continue
      read (brkrec,210)  atmnam,altloc,(acoord(i),i=1,3),occup,therm
  210 format (12x,a4,a1,13x,3f8.3,2f6.2)
      if (atmnam .eq. ' OXT' .or. atmnam .eq. ' NXT' .or.
     &    atmnam(2:2) .eq. 'H' .or. atmnam(2:2) .eq. 'D')  goto 240
      if (aacode.lt.44) then
         attype = index(atname,atmnam)
      else
         if (aacode.gt.44 .and. aacode.le.50) then
            attype = index(batnam,atmnam)
         end if
      end if
      if (altloc .ne. space) then
         if (setatm .eq. '    ') then
            setatm = atmnam
            setloc = altloc
            oldocc = occup
            oldthm = therm
         end if
         if (.not. altset) then
            if (setatm .eq. atmnam) then
               if (occup .gt. oldocc) then
                  setloc = altloc
                  oldocc = occup
                  if (xney(oldthm,0.0))  thmcnt = thmcnt - 1
                  oldthm = therm
                  if (attype .ne. 0) then
                     atcnt = atcnt - 1
                  else
                     scatct = scatct - 1
                  end if
               end if
            else
               altset = .true.
            end if
         end if
      end if
      if (xney(therm,0.0) .and. (altloc.eq.setloc .or.
     &             altloc.eq.space))  thmcnt = thmcnt + 1
      if (attype.ne.0 .and. (altloc.eq.setloc .or.
     &             altloc.eq.space)) then
         attype = attype/4 + 1
         if (attype .le. 4) then
            mcbval(aacnt) = mcbval(aacnt) + therm
            nmcatm(aacnt) = nmcatm(aacnt) + 1
            do i = 1, ncoord
               mcaaat(i,attype,aacnt) = acoord(i)
            end do
            atomin(attype,aacnt) = .true.
         end if
         if (attype .ne. calph)  caonly = .false.
         atcnt = atcnt + 1
      end if
      if (attype.eq.0 .and. (altloc.eq.setloc.or.altloc.eq.space)) then
         scbval(aacnt) = scbval(aacnt) + therm
         nscatm(aacnt) = nscatm(aacnt) + 1
         scatct = scatct + 1
         if (aacode .le. sideaa) then
            if (atmnam .eq. cbet) then
               sctype = 1
            else
               sctype = index(scname(aacode),atmnam)
               if (sctype.eq.0 .and. altpos(aacode).ne.0)
     &            sctype = index(altnam(altpos(aacode)),atmnam)
               if (sctype .ne. 0)  sctype = sctype/4 + 2
            end if
            if (sctype .ne. 0) then
               scatin(sctype,aacnt) = .true.
               if (atmnam(3:4).eq.'G ' .OR. atmnam(3:4).eq.'G1') then
                  bvalue(aacnt) = therm
               end if
               do i = 1, ncoord
                  scaaat(i,sctype,aacnt) = acoord(i)
               end do
            end if
         end if
      end if
c
c     search for the next atom record and treat as appropriate
c     process coords only if same aa
c     if new aa start aa process again after setting the count fields
c
  240 continue
      read (brkfi,'(a)',end=250)  brkrec
      if (brkrec(1:keylen) .ne. endkey) then
         if (brkrec(1:keylen) .eq. atmkey .or.
     &       brkrec(1:keylen) .eq. htakey) then
            read (brkrec,'(21x,a6)')  innum
            if (innum.eq.seqnum .and. keywd.eq.brkrec(1:keylen)) then
               if (aacode .le. 0)  goto 240
               goto 200
            else
               if (aacode .gt. 0)
     &            call cntset (atpres,thmprs,altcod,nscats,aacode,
     &                         aacnt,atcnt,scatct,thmcnt,setloc)
               If (aacnt .ge. maxrsd) then
                  goto 260
               else
                  if (aacode .gt. 0)  aastd(aacnt+1) = space
                  goto 150
               end if
            end if
         end if
         goto 240
      end if
  250 continue
      if (aacode .gt. 0)
     &   call cntset (atpres,thmprs,altcod,nscats,aacode,
     &                aacnt,atcnt,scatct,thmcnt,setloc)
  260 continue
      seqlen = aacnt
  270 continue
      if (seqlen .eq. 0)  readok = .false.
      close (unit=brkfi,status='delete')
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine cntset  ##
c     ##                     ##
c     #########################
c
c
c     "cntset" performs end of amino acid flag setting
c
c
      subroutine cntset (atpres,thmprs,altcod,nscats,aacode,
     &                   aacnt,atcnt,scatct,thmcnt,setloc)
      implicit none
      include 'sstruc.i'
      integer aacode,aacnt,atcnt,scatct,thmcnt
      integer totats,nscats(sideaa)
      character*1 atpres(3,maxrsd),thmprs(maxrsd),altcod(maxrsd)
      character*1 setloc,atmchk
c
c
      altcod(aacnt) = setloc
      atpres(1,aacnt) = space
      atpres(2,aacnt) = space
      atpres(3,aacnt) = space
      thmprs(aacnt) = space
      totats = atcnt + scatct
      if (aacode .le. sideaa) then
         if (nscats(aacode) .ge. 0) then
            atpres(1,aacnt) = atmchk(mnchna+nscats(aacode)-1,totats)
            atpres(2,aacnt) = atmchk(mnchna-1,atcnt)
            atpres(3,aacnt) = atmchk(nscats(aacode),scatct)
            thmprs(aacnt) = atmchk(mnchna+nscats(aacode)-1,thmcnt)
         end if
      end if
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function atmchk  ##
c     ##                   ##
c     #######################
c
c
c     "atmchk" sets presence fields to all, none or some as appropriate
c
c
      function atmchk (total,count)
      implicit none
      include 'sstruc.i'
      integer total,count
      character*1 atmchk
c
c
      if (total .eq. 0) then
         atmchk = space
      else
         if (count .eq. 0) then
            atmchk = 'N'
         else
            if (count .ge. total) then
               atmchk = 'A'
            else
               atmchk = 'S'
            end if
         end if
      end if
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function aaconv  ##
c     ##                   ##
c     #######################
c
c
c     "aaconv" finds the numerical position of an amino acid code
c
c
      function aaconv (aacode,aacod3,hetcod)
      implicit none
      include 'sstruc.i'
      integer aaconv,aaval,resfnd
      character*3 aacode
      character aacod3*(3*abetsz)
      character*63 hetcod*63
      character*3 lastcd
      data lastcd / '   ' /
      save lastcd
c
c
      aaval = resfnd(aacod3,aacode)
      if (aaval .eq. 0) then
         aaval = resfnd(hetcod,aacode)
         if (aaval .eq. 0) then
            aaval = unkcod
            if (aacode.ne.lastcd) then
               lastcd = aacode
            endif
         else
            aaval = abetsz + aaval/3 + 1
         end if
      else
         aaval = aaval/3 + 1
      end if
      aaconv = aaval
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function resfnd  ##
c     ##                   ##
c     #######################
c
c
c     "resfnd" locates a query in source on 3 character boundaries
c
c
      function resfnd (soustr,questr)
      implicit none
      integer resfnd,respl,nxpl
      character*3 questr
      character*(*) soustr
c
c
      respl = index(soustr,questr)
   10 continue
      If (mod(respl,3).ne.1 .and. respl.ne.0) then
         nxpl = index(soustr(respl+1:),questr)
         if (nxpl .ne. 0) then
            respl = respl + nxpl
         else
            respl = nxpl
         end if
         goto 10
      end if
      resfnd = respl
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine chnbrk  ##
c     ##                     ##
c     #########################
c
c
c     "chnbrk" searchs through looking for distant peptide bonds;
c     if any over the limit are found or if a C or N atom does not
c     exist then a chain break is deemed to occur
c
c
      subroutine chnbrk (mcaaat,atomin,seqbcd,brksym,chnsz,
     &                   chnct,chnend,caonly,seqlen)
      implicit none
      include 'sstruc.i'
      integer nxres,ninchn,chnct,seqlen
      integer chnsz(maxchn),chnend(maxchn)
      real atdist
      real mcaaat(ncoord,mnchna,maxrsd)
      character*1 brksym(maxrsd)
      character*6 seqbcd(maxrsd)
      logical atomin(mnchna,maxrsd),caonly
c
c
      do nxres = 1, seqlen
         brksym(nxres) = space
      end do
      ninchn = 1
      chnct = 1
      do nxres = 2, seqlen
         if (caonly) then
            if (atdist(mcaaat(1,calph,nxres-1),mcaaat(1,calph,nxres))
     &                          .gt. cadbnd) then
               call chnset (brksym,chnsz,chnct,chnend,
     &                      ninchn,seqbcd,nxres,seqlen)
            else
               ninchn = ninchn + 1
            end if
         else
            if (atomin(carb,nxres-1) .and. atomin(nmain,nxres)) then
               if (atdist(mcaaat(1,carb,nxres-1),mcaaat(1,nmain,nxres))
     &                        .gt. pepbnd) then
                  call chnset (brksym,chnsz,chnct,chnend,
     &                         ninchn,seqbcd,nxres,seqlen)
               else
                  ninchn = ninchn + 1
               end if
            else
               call chnset (brksym,chnsz,chnct,chnend,
     &                      ninchn,seqbcd,nxres,seqlen)
            end if
         end if
      end do
      call chnset (brksym,chnsz,chnct,chnend,
     &             ninchn,seqbcd,seqlen+1,seqlen)
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine chnset  ##
c     ##                     ##
c     #########################
c
c
      subroutine chnset (brksym,chnsz,chnct,chnend,
     &                   ninchn,seqbcd,nxres,seqlen)
      implicit none
      include 'sstruc.i'
      integer chnct,ninchn,seqlen,nxres
      integer chnsz(maxchn),chnend(maxchn)
      character*1 brksym(maxrsd)
      character*6 seqbcd(maxrsd)
c
c
      chnsz(chnct) = ninchn
      if (chnct .eq. 1) then
         chnend(chnct) = ninchn
      else
         chnend(chnct) = chnend(chnct-1) + ninchn
      end if
      ninchn = 1
      if (nxres .le. seqlen) then
         brksym(nxres-1) = brkch
         brksym(nxres) = brkch
         chnct = chnct + 1
      end if
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine creath  ##
c     ##                     ##
c     #########################
c
c
c     "createh" is a procedure to create hydrogen atoms; these are
c     deemed to be one angstrom from the nitrogen atom and angled
c     so N-H is parallel to C=O before. By taking the vector from
c     O to C the N-H is generated on the other side of C-N to the O
c
c
      subroutine creath (mcaaat,atomin,chnsz,chnct,seqlen)
      implicit none
      include 'sstruc.i'
      integer i,nxres,chnst,nxchn
      integer chnsz(maxchn),chnct,seqlen
      real atvec(ncoord),colen
      real mcaaat(ncoord,mnchna,maxrsd)
      logical atomin(mnchna,maxrsd)
c
c
      chnst = 0
      do nxchn = 1, chnct
         do nxres = chnst+2, chnst+chnsz(nxchn)
            if (atomin(nmain,nxres) .and. atomin(carb,nxres-1)
     &                .and. atomin(oxyg,nxres-1)) then
               colen = 0.0
               do i = 1, ncoord
                  atvec(i) = mcaaat(i,carb,nxres-1)
     &                         - mcaaat(i,oxyg,nxres-1)
                  colen = colen + atvec(i)**2
               end do
               colen = sqrt(colen)
               do i = 1, ncoord
                  atvec(i) = atvec(i) / colen
               end do
               do i = 1, ncoord
                  mcaaat(i,nhyd,nxres) = mcaaat(i,nmain,nxres)+atvec(i)
               end do
               atomin(nhyd,nxres) = .true.
            else
               atomin(nhyd,nxres) = .false.
            end if
         end do
         chnst = chnst + chnsz(nxchn)
      end do
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine mkhbnd  ##
c     ##                     ##
c     #########################
c
c
c     "mkhbnd" discovers the possible hydrogen bonds between two
c     amino acids; each amino acid is allowed 4 bonds, 2 from the
c     C=O and 2 from the N-H; should it be that 3 or more bonds are
c     possible the most favourable are kept; a hydrogen bond is
c     deemed to exist if the Kabsch and Sander energy formula gives
c     a value less than the cutoff; the angle and distance approach
c     is implicitly built in to this method; the formula corresponds
c     to a maximum distance of 5.2 A for in line atoms and a maximum
c     angular deviation of 63 degrees; a check on the maximum distance
c     is used to abandon unnecessary calculations; the formula is:
c    
c            E = q1*q2*(1/d(ON)+1/d(CH)-1/d(OH)-1/d(CN))*f
c
c     residues must be separated by at least one residue to bond, and
c     proline cannot be a donor of a hydrogen bond
c
c
      subroutine mkhbnd (mcaaat,atomin,hbond,hbonde,
     &                   seqcod,chnend,seqlen)
      implicit none
      include 'sstruc.i'
      integer seqlen
      integer ksf,mndist
      real mxdist,maxeng,ksq1,ksq2,maxcad,englim
      parameter (mxdist=5.2)
      parameter (maxeng=-0.5)
      parameter (mndist=0.5)
      parameter (englim=-9.9)
      parameter (ksq1=0.42)
      parameter (ksq2=0.2)
      parameter (ksf=332)
      parameter (maxcad=8.0)
      integer i,nxres,othres,nbonds,fstchn,othchn
      integer hbond(maxbnd,maxrsd),seqcod(maxrsd),chnend(maxchn)
      real atdist,don,doh,dch,dcn,energy,cadist
      real mcaaat(ncoord,mnchna,maxrsd),hbonde(maxbnd,maxrsd)
      logical nhpres,xeqy,atomin(mnchna,maxrsd)
c
c
      do nxres = 1, seqlen
         do i = 1,maxbnd
            hbond(i,nxres) = 0
            hbonde(i,nxres) = 0.0
         end do
      end do
      nbonds = 0
      fstchn = 1
      do nxres = 1, seqlen
         if (nxres .gt. chnend(fstchn))  fstchn = fstchn + 1
         nhpres = .false.
         if (atomin(nmain,nxres) .and. atomin(nhyd,nxres)
     &       .and. seqcod(nxres).ne.procod)  nhpres = .true.
         if (nhpres) then
            othchn = 1
            do othres = 1, seqlen
               if (othres .gt. chnend(othchn))  othchn = othchn + 1
               if ((iabs(nxres-othres).eq.1 .and. fstchn.ne.othchn)
     &               .or. iabs(nxres-othres).ge.2) then
                  if (atomin(calph,nxres).and.atomin(calph,othres)) then
                     cadist = atdist (mcaaat(1,calph,othres),
     &                                mcaaat(1,calph,nxres))
                     if (cadist .lt. maxcad) then
                        if (atomin(carb,othres) .and.
     &                      atomin(oxyg,othres)) then
                           don = atdist (mcaaat(1,oxyg,othres),
     &                                   mcaaat(1,nmain,nxres))
                           doh = atdist (mcaaat(1,oxyg,othres),
     &                                   mcaaat(1,nhyd,nxres))
                           dch = atdist (mcaaat(1,carb,othres),
     &                                   mcaaat(1,nhyd,nxres))
                           dcn = atdist (mcaaat(1,carb,othres),
     &                                   mcaaat(1,nmain,nxres))
                           if (xeqy(don,0.0) .or. xeqy(doh,0.0) .or.
     &                         xeqy(dch,0.0) .or. xeqy(dcn,0.0)) then
                           else
                              energy = ksq1 * ksq2 * ksf *
     &                                 (1.0/don+1.0/dch-1.0/doh-1.0/dcn)
                              if (energy .lt. englim) then
                                 energy = englim
                              end if
                              if (energy .lt. maxeng)
     &                           call engset (hbonde,hbond,energy,
     &                                        nxres,othres,nbonds)
                           end if
                        end if
                     end if
                  end if
               end if
            end do
         end if
      end do
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine engset  ##
c     ##                     ##
c     #########################
c
c
c     "engset" finds a place to put this H-bond, discarding
c     third bonds if necessary
c
c
      subroutine engset (hbonde,hbond,energy,donres,accres,bndct)
      implicit none
      include 'sstruc.i'
      integer donres,accres,bndct
      integer engpl,acpl,donpl,rejptr
      integer hbond(maxbnd,maxrsd)
      real hbonde(maxbnd,maxrsd),energy
c
c
      donpl = engpl(energy,hbonde(nst,donres))
      acpl = engpl(energy,hbonde(cst,accres))
      if (acpl.ne.0 .and. donpl.ne.0) then
         if (hbond(nst+1,donres) .ne. 0) then
            rejptr = hbond(nst+1,donres)
            call rejbnd (donres,hbond(cst,rejptr),hbonde(cst,rejptr))
            bndct = bndct - 1
         end if
         if (hbond(cst+1,accres) .ne. 0) then
            rejptr = hbond(cst+1,accres)
            call rejbnd (accres,hbond(nst,rejptr),hbonde(nst,rejptr))
            bndct = bndct - 1
         end if
         if (acpl .eq. 1) then
            hbond(cst+1,accres) = hbond(cst,accres)
            hbonde(cst+1,accres) = hbonde(cst,accres)
         end if
         if (donpl .eq. 1) then
            hbond(nst+1,donres) = hbond(nst,donres)
            hbonde(nst+1,donres) = hbonde(nst,donres)
         end if
         hbond(cst+acpl-1,accres) = donres
         hbonde(cst+acpl-1,accres) = energy
         hbond(nst+donpl-1,donres) = accres
         hbonde(nst+donpl-1,donres) = energy
         bndct = bndct + 1
      end if
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine rejbnd  ##
c     ##                     ##
c     #########################
c
c
      subroutine rejbnd (rejptr,bond,bonde)
      implicit none
      include 'sstruc.i'
      integer rejpl,rejptr,bond(2)
      real bonde(2)
c
c
      rejpl = 1
      if (bond(1) .ne. rejptr)  rejpl = rejpl + 1
      if (rejpl .eq. 1) then
         bond(1) = bond(2)
         bonde(1) = bonde(2)
      end if
      bond(2) = 0
      bonde(2) = 0.0
      return
      end
c
c
c     ######################
c     ##                  ##
c     ##  function engpl  ##
c     ##                  ##
c     ######################
c
c
      function engpl (energy,bonde)
      implicit none
      include 'sstruc.i'
      integer engpl
      real energy,bonde(2)
c
c
      engpl = 0
      If (energy .lt. bonde(1)) then
         engpl = 1
      else if (energy .lt. bonde(2)) then
         engpl = 2
      end if
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function atdist  ##
c     ##                   ##
c     #######################
c
c
c     "atdist" determines the distance between two atoms
c
c
      function atdist (coorda,coordb)
      implicit none
      include 'sstruc.i'
      integer i
      real atdist,dist
      real coorda(ncoord),coordb(ncoord)
c
c
      dist = 0.0
      do i = 1, ncoord
         dist = dist + (coorda(i)-coordb(i))**2
      end do
      atdist = sqrt(dist)
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function aplace  ##
c     ##                   ##
c     #######################
c
c
c     "aplace" finds a free place in the bond table,
c     or else returns with 0
c
c
      function aplace (bondpl)
      implicit none
      include 'sstruc.i'
      integer i,wkpl
      integer aplace,bondpl(2)
c
c
      do i = 1, 2
         If (bondpl(i) .eq. 0) then
            wkpl = i
            goto 10
         end if
      end do
      wkpl = 0
   10 continue
      aplace = wkpl
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine mkangl  ##
c     ##                     ##
c     #########################
c
c
      Subroutine MkAngl(McAAat, McAngs, Atomin, chnsz, chnct, caonly,
     &                  Seqlen, scaaat, scatin)
c
c     Procedure to calculate the main chain dihedral angles
c     if all the atoms are present.
c
      include 'sstruc.i'
      Real    McAAat(ncoord,mnchna,maxrsd), McAngs(nmnang,maxrsd)
      Logical Atomin(mnchna,maxrsd), caonly
      Integer Seqlen, chnsz(maxchn), chnct
      Real    Dihed
      real      scaaat(ncoord,sdchna,maxrsd)
      logical   scatin(sdchna,maxrsd)
c
c     local variables
c
      Integer angrng(nmnang,3), angatm(nmnang,4), angoff(nmnang,4),
     &        angchn(nmnang), I, j, Nxres, Nxset, chnst, nxchn, stang,
     &        finang
      data ((angrng(I,j),j=1,3), i=1,nmnang)
     &      /2, 0, phi, 1, -1, psi, 1, -1, omega, 2, -2, chiral,
     &       1, 0, IMPLD, 3, -2, kappa, 2, 0, tco /
      data ((angatm(I,j),j=1,4), i=1,nmnang)
     &     /carb, nmain, calph, carb, nmain, calph, carb, nmain,
     &      calph, carb, nmain, calph, calph, calph, calph, calph,
     &      CALPH, NMAIN, CARB, MCBETA,
     &      calph, calph, calph, calph, carb, oxyg, carb, oxyg /
      data ((angoff(I,j),j=1,4), i=1,nmnang)
     &     /-1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, -1, 0, 1, 2,
     &       0, 0, 0, 0,-2, 0, 0, 2, 0, 0, -1, -1 /
      data angchn / 2, 2, 2, 4, 1, 5, 2 /
      SAVE angrng, angatm, angoff, angchn
c
      DO 10, NXRES = 1, SEQLEN
         ATOMIN(MCBETA,NXRES) = SCATIN(CBETA,NXRES)
         MCAAAT(1,MCBETA,NXRES) = SCAAAT(1,CBETA,NXRES)
         MCAAAT(2,MCBETA,NXRES) = SCAAAT(2,CBETA,NXRES)
         MCAAAT(3,MCBETA,NXRES) = SCAAAT(3,CBETA,NXRES)
   10 CONTINUE
      stang = 1
      finang = nmnang
      if (caonly) then
         stang = chiral
         finang = kappa
      end if
      do 70, Nxset = stang, finang
         chnst = 0
         do 60, nxchn = 1, chnct
            if (chnsz(nxchn) .lt. angchn(nxset)) then
               do 20, nxres = chnst + 1, chnst + chnsz(nxchn)
                  McAngs(angrng(nxset,3),Nxres) = Null
   20          continue
            else
               do 30, Nxres = chnst + angrng(nxset,1),
     &                    chnst + chnsz(nxchn) + angrng(nxset,2)
                  If (atomin(angatm(nxset,1), nxres+angoff(nxset,1)) .an
     &d.      atomin(angatm(nxset,2), nxres+angoff(nxset,2)) .and.
     &      atomin(angatm(nxset,3), nxres+angoff(nxset,3)) .and.
     &      atomin(angatm(nxset,4), nxres+angoff(nxset,4))) then
                     McAngs(angrng(nxset,3),Nxres) = Dihed(nxset,
     &          Mcaaat(1,angatm(nxset,1), nxres+angoff(nxset,1)),
     &          Mcaaat(1,angatm(nxset,2), nxres+angoff(nxset,2)),
     &          Mcaaat(1,angatm(nxset,3), nxres+angoff(nxset,3)),
     &          Mcaaat(1,angatm(nxset,4), nxres+angoff(nxset,4)))
                  else
                     McAngs(angrng(nxset,3),Nxres) = Null
                  end if
   30          continue
               do 40, nxres = chnst + 1, chnst + angrng(nxset,1) - 1
                  McAngs(angrng(nxset,3),nxres) = Null
   40          continue
               do 50, nxres = chnst + chnsz(nxchn) + angrng(nxset,2) + 1
     &,                    chnst + chnsz(nxchn)
                  McAngs(angrng(nxset,3),nxres) = Null
   50          continue
            end if
            chnst = chnst + chnsz(nxchn)
   60    continue
   70 continue
      return
      end
c
c
c     ######################
c     ##                  ##
c     ##  function dihed  ##
c     ##                  ##
c     ######################
c
c
      Real Function Dihed(angnum, AtomA, AtomB, AtomC, AtomD)
c
c     routine to calculate the dihedral angle between the 4 atoms
c     given
c
      include 'sstruc.i'
      Real    Atoma(ncoord), Atomb(ncoord), Atomc(ncoord), atomd(ncoord)
      integer angnum
      Real    Atdist
      Logical xeqy
      Real    dihatm(ncoord,dihdat), codist(ncoord,dihdat-1),
     &        atmdst(dihdat-1), dotprd(dihdat-1,dihdat-1),
     &        ang, cosang, detant
      Integer i, j, k
      do 10, I = 1, ncoord
         Dihatm(i,1) = atoma(i)
         Dihatm(i,2) = atomb(i)
         Dihatm(i,3) = atomc(i)
         Dihatm(i,4) = atomd(i)
   10 continue
      do 30, i = 1, dihdat-1
         atmdst(i) = atdist(Dihatm(1,i),Dihatm(1,I+1))
         do 20, j = 1,ncoord
            codist(j,i) = dihatm(j,I+1) - dihatm(j,i)
   20    continue
   30 continue
      do 40, i = 1, dihdat-1
         do 40, j = 1, dihdat-1
            dotprd(i,j) = 0.0
   40 continue
      do 50, i = 1, dihdat - 2
         do 50, j = i+1, dihdat - 1
            do 50, k = 1, ncoord
               dotprd(i,j) = dotprd(i,j) + codist(k,i) * codist(k,j)
   50 continue
      do 60, i = 1, dihdat-2
         do 60, j = i+1, dihdat - 1
            if (xeqy(atmdst(i), 0.0) .or. xeqy(atmdst(j), 0.0)) then
               dotprd(i,j) = 1.0
            else
               dotprd(i,j) = dotprd(i,j) / atmdst(i) / atmdst(j)
            end if
   60 continue
      if (angnum .gt. ndihed .and. angnum .le. nmnang) then
         cosang =  dotprd(1,3)
      else
         if (xeqy(dotprd(1,2), 1.0) .or. xeqy(dotprd(2,3), 1.0)) then
            cosang = 1.0
         else
            cosang = (dotprd(1,2) * dotprd(2,3) - dotprd(1,3)) /
     &               (sqrt(1.0-dotprd(1,2)**2) *
     &                sqrt(1.0-dotprd(2,3)**2))
         end if
      end if
      if (abs(cosang) .gt. 1.0) cosang = anint(cosang/abs(cosang))
      ang = acos(cosang) * radian
      if (angnum .le. ndihed .or. angnum .gt. nmnang) then
         detant = codist(1,1) * codist(2,2) * codist(3,3) -
     &            codist(1,1) * codist(2,3) * codist(3,2) +
     &            codist(1,2) * codist(2,3) * codist(3,1) -
     &            codist(1,2) * codist(2,1) * codist(3,3) +
     &            codist(1,3) * codist(2,1) * codist(3,2) -
     &            codist(1,3) * codist(2,2) * codist(3,1)
         If (detant .lt. 0.0) ang = -ang
      end if
      dihed = ang
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  subroutine mktb  ##
c     ##                   ##
c     #######################
c
c
      Subroutine MkTB(Hbond, SSbond, mcangs, bridpt, chnend, seqlen)
c
c     routine to establish the turns and bridges
c
      include 'sstruc.i'
      Integer   Hbond(maxbnd,maxrsd), seqlen, bridpt(2,maxrsd),
     &          chnend(maxchn)
      Real      Mcangs(nmnang,maxrsd)
      Character SSbond(nstruc,maxrsd)*1
      Integer   strdpl
      Integer   nrules, parlel
      Parameter (nrules = 3, parlel = 1)
      Integer   nxres, nxbond, nxturn, turnsz(nturns), turnpl(nturns),
     &          bsofst(nrules,4), i, j, nxrule, bndi, bndj, bndptr,
     &          brpl, brdgpt, bsrng(nrules,2), pbrdg(nrules), brpla,
     &          brdg(4,maxrsd), brptr, brdir, newptr, nxptr, nxpl,
     &          exrule(2,nrules), brptpl, nwptpl, nxstrn,
     &          lststd, stsht, finsht, nxstr, chcode, stchpl, thstpl,
     &          nxstpl, nxblg, nxbrdg, strpl, brdpt, nxbrpl,
     &          strcd(2,maxrsd), bstval, shtcod(maxrsd), fstchn
      Character turnch(nturns)*1, wkch*1, strdch*1, brdch*1,
     &          stchap*(nstchs), stchpa*(nstchs),
     &	        nstchp*(nstchs*2+10)
      logical   xney
c
c     for turn calculations
c
      data turnch / '3', '4', '5'/
      data turnsz / 3, 4, 5/
      data turnpl / thrtnh, alphah, pih/
c
c     for bridge calculations
c
      data ((bsofst(i,j),j = 1,4),i = 1,nrules)
     &     / -1, 0, 0, 1, 0, 0, 0, 0, -1, -1, -2, 1/
      data ((bsrng(i,j),j = 1,2), i = 1,nrules)
     &     / 2, -1, 1, 0, 2, -1/
      data pbrdg / 1, -1, -1/
      data exrule /-1, 1, 1, 1, -1, -1/
c
c     for strand and bridge labelling
c
      data stchap /'ABCDEFGHIJKLMNOPQRSTUVWXYZ'/
      data stchpa /'abcdefghijklmnopqrstuvwxyz'/
      data nstchp(1:26) /'ABCDEFGHIJKLMNOPQRSTUVWXYZ'/,
     &     nstchp(27:52) /'abcdefghijklmnopqrstuvwxyz'/,
     &     nstchp(53:62) /'1234567890'/
      SAVE turnch, turnsz, turnpl, bsofst, bsrng, pbrdg, stchap, stchpa
      do 10, nxres = 1, seqlen
         do 10, i = 1, nstruc
            ssbond(i,nxres) = space
   10 continue
      fstchn = 1
      do 60, nxres = 1, seqlen
         if (nxres .gt. chnend(fstchn)) fstchn = fstchn + 1
         do 50, nxbond = 1, 2
            do 40, nxturn = 1, nturns
               if (Hbond(nxbond,nxres) .ne. 0) then
                  If (Hbond(nxbond,nxres) - nxres .eq. turnsz(nxturn) .a
     &nd.      Hbond(nxbond,nxres) .le. chnend(fstchn)) then
                     If (SSbond(turnpl(nxturn),nxres) .eq. bndend) then
                        SSbond(turnpl(nxturn),nxres) = bndbth
                     else
                        SSbond(turnpl(nxturn),nxres) = bndbeg
                     end if
                     SSbond(turnpl(nxturn),Hbond(nxbond,nxres)) = bndend
                     do 20, i = nxres + 1, nxres + turnsz(nxturn) - 1
                        If (SSbond(turnpl(nxturn),i) .eq. space)
     &         SSbond(turnpl(nxturn),i) = turnch(nxturn)
   20                continue
                     do 30 i = nxres, nxres + turnsz(nxturn)
                        SSbond(turn,i) = trnch
   30                continue
                  end if
               end if
   40       continue
   50    continue
   60 continue
      do 70, nxres = 1,seqlen
         do 70, nxbond = 1, 4
            brdg(nxbond,nxres) = 0
   70 continue
      do 110, nxrule = 1, nrules
         do 100, Nxres = bsrng(nxrule,1), seqlen + bsrng(nxrule,2)
            do 90, bndi = 1, 2
               bndptr = hbond(bndi,nxres+bsofst(nxrule,1))
               if (nxrule .le. parlel .or. bndptr .gt. nxres) then
                  brdgpt = bndptr + bsofst(nxrule,2)
                  If (bndptr .gt. - bsofst(nxrule,3) .and.
     &      iabs(nxres - brdgpt) .gt. bdgsep)  then
                     do 80, bndj = 1, 2
                        If (hbond(bndj,bndptr+bsofst(nxrule,3)) .eq.
     &         nxres+bsofst(nxrule,4)) then
                           brpl = 1
                           If (brdg(brpl,nxres) .ne. 0) brpl = 2
                           Brdg(brpl,nxres) = pbrdg(nxrule) * brdgpt
                           brdg(brpl+2,nxres) = exrule(1,nxrule)
                           brpla = 1
                           If (brdg(brpla,brdgpt) .ne. 0) brpla = 2
                           Brdg(brpla,brdgpt) = pbrdg(nxrule) * nxres
                           brdg(brpla+2,brdgpt) = exrule(2,nxrule)
                        end if
   80                continue
                  end if
               end if
   90       continue
  100    continue
  110 continue
      do 120, nxres = 1, seqlen
         if (xney(mcangs(chiral,nxres), null)) then
            ssbond(chisgn,nxres) = '+'
            if (mcangs(chiral,nxres) .lt. 0.0) ssbond(chisgn,nxres) = '-
     &'
         end if
  120 continue
      do 130, nxres = 1, seqlen
         shtcod(nxres) = 0
         strcd(1,nxres) = 0
         strcd(2,nxres) = 0
         bridpt(1,nxres) = 0
         bridpt(2,nxres) = 0
  130 continue
      nxstrn = 0
      do 200, nxres = 1, seqlen
         do 190, brpl = 1, 2
            If (brdg(brpl,nxres) .ne. 0 .and.
     &   iabs(brdg(brpl,nxres)) .gt. nxres) then
               brptr = iabs(brdg(brpl,nxres))
               brdir = brdg(brpl,nxres) / brptr
               brptpl = 2
               if (iabs(brdg(1,brptr)) .eq. nxres) brptpl = 1
               do 170, nxptr = nxres + 1, min(seqlen,nxres+blgszl)
                  do 160, nxpl = 1,2
                     If (brdg(nxpl,nxptr) * brdir .gt. 0) then
                        newptr = iabs(brdg(nxpl,nxptr))
                        IF (iabs(newptr-brptr).NE.0) THEN
                           if ((newptr-brptr)/iabs(newptr-brptr) .eq. br
     &dir) then
                              if ((nxptr - nxres .gt. blgszs .and.
     &            iabs(newptr - brptr) .le. blgszs) .OR.
     &            (nxptr - nxres .le. blgszs .and.
     &            iabs(newptr - brptr) .le. blgszl)) then
                                 nwptpl = 2
                                 if (iabs(brdg(1,newptr)) .eq. nxptr) nw
     &ptpl = 1
                                 do 140, i = nxres, nxptr
                                    if (ssbond(sheet,i) .eq. space)
     &               ssbond(sheet,i) = shtsym
  140                            continue
                                 if (brdg(brpl+2,nxres) .lt. 0)
     &               ssbond(sheet,nxres) = shtsma
                                 if (brdg(nxpl+2,nxptr) .lt. 0)
     &               ssbond(sheet,nxptr) = shtsma
                                 do 150, i = brptr, newptr, brdir
                                    if (ssbond(sheet,i) .eq. space)
     &               ssbond(sheet,i) = shtsym
  150                            continue
                                 if (brdg(brptpl+2,brptr) .lt. 0)
     &               ssbond(sheet,brptr) = shtsma
                                 if (brdg(nwptpl+2,newptr) .lt. 0)
     &               ssbond(sheet,newptr) = shtsma
                                 if (strcd(brpl,nxres) .eq. 0) then
                                    nxstrn = nxstrn + 1
                                    strcd(brpl,nxres) = nxstrn * brdir
                                    strcd(brptpl,brptr) = nxstrn * brdir
                                 end if
                                 strcd(nxpl,nxptr) = strcd(brpl,nxres)
                                 strcd(nwptpl,newptr) = strcd(brptpl,brp
     &tr)
                                 go to 180
                              end if
                           end if
                        ENDIF
                     end if
  160             continue
  170          continue
  180          continue
               wkch = pbch
               if (brdir .lt. 0) wkch = apbch
               ssbond(bridge,nxres) = wkch
               ssbond(bridge,brptr) = wkch
            end if
  190    continue
  200 continue
      nxres = 1
  210 continue
      if (ssbond(sheet,nxres) .eq. space) then
         nxres = nxres + 1
         if (nxres .gt. seqlen) goto 300
         goto 210
      end if
      stsht = nxres
      nxres = stsht + 1
  220 continue
      if (ssbond(sheet,nxres) .ne. space) then
         nxres = nxres + 1
         if (nxres .gt. seqlen) goto 230
         goto 220
      end if
  230 continue
      finsht = nxres - 1
      lststd = 0
  240 continue
      call whstrd(strcd, stsht, finsht, nxstr, nxpl, lststd, bstval)
      if (bstval .ne. 0) then
         lststd = abs(strcd(nxpl,nxstr))
         chcode = mod(lststd,nstchs)
         if (chcode .eq. 0) chcode = nstchs
         if (strcd(nxpl,nxstr) .lt. 0) then
            strdch = stchap(chcode:chcode)
         else
            strdch = stchpa(chcode:chcode)
         end if
         stchpl = bridg1
         if (ssbond(bridg1,nxstr) .ne. space) stchpl = bridg2
         if (stchpl .ne. bridg2) then
            thstpl = nxstr
  250       continue
            nxstpl = strdpl(thstpl,finsht,strcd(nxpl,nxstr),strcd)
            if (nxstpl .le. 0) goto 260
            if (ssbond(bridg1,nxstpl) .ne. space) stchpl = bridg2
            thstpl = nxstpl
            if (stchpl .ne. bridg2) goto 250
         end if
  260    continue
         ssbond(stchpl,nxstr) = strdch
         bridpt(stchpl-bridg1+1,nxstr) = abs(brdg(nxpl,nxstr))
         thstpl = nxstr
  270    continue
         nxstpl = strdpl(thstpl,finsht,strcd(nxpl,nxstr),strcd)
         if (nxstpl .le. 0) goto 290
         do 280, nxblg = thstpl + 1, nxstpl - 1
            ssbond(stchpl,nxblg) = bulgch
  280    continue
         ssbond(stchpl,nxstpl) = strdch
         strpl = 1
         if (strcd(1,nxstpl) .ne. strcd(nxpl,nxstr)) strpl = 2
         bridpt(stchpl-bridg1+1,nxstpl) = abs(brdg(strpl,nxstpl))
         thstpl = nxstpl
         go to 270
  290    continue
         go to 240
      end if
      nxres = finsht + 1
      if (nxres .le. seqlen) goto 210
  300 continue
      Call shtlab(ssbond, bridpt, shtcod, seqlen)
      do 310 nxres = 1, seqlen
         if (shtcod(nxres) .ne. 0) then
            chcode = shtcod(nxres)
            ssbond(sheet1,nxres) = nstchp(chcode:chcode)
         end if
  310 continue
      nxbrdg = 0
      do 330, nxres = 1, seqlen
         do 320, nxpl = 1, 2
            If (brdg(nxpl,nxres) .ne. 0 .and. strcd(nxpl,nxres) .eq. 0
     &   .and. abs(brdg(nxpl,nxres)) .gt. nxres) then
               nxbrdg = nxbrdg + 1
               brdpt = abs(brdg(nxpl,nxres))
               chcode = mod(nxbrdg,nstchs)
               if (chcode .eq. 0) chcode = nstchs
               if (brdg(nxpl,nxres) .lt. 0) then
                  brdch = stchap(chcode:chcode)
               else
                  brdch = stchpa(chcode:chcode)
               end if
               nxbrpl = bridg1
               if (ssbond(bridg1,nxres) .ne. space) nxbrpl = bridg2
               ssbond(nxbrpl,nxres) = brdch
               bridpt(nxbrpl-bridg1+1,nxres) = brdpt
               nxbrpl = bridg1
               if (ssbond(bridg1,brdpt) .ne. space) nxbrpl = bridg2
               ssbond(nxbrpl,brdpt) = brdch
               bridpt(nxbrpl-bridg1+1,brdpt) = nxres
            end if
  320    continue
  330 continue
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function strdpl  ##
c     ##                   ##
c     #######################
c
c
c     "strdpl" finds next occurrence of the code, if any, in array
c
c
      function strdpl (stpl,finpl,thcode,strand)
      implicit none
      include 'sstruc.i'
      integer strdpl,nxpl
      integer stpl,finpl,thcode
      integer strand(2,maxrsd)
c
c
      do nxpl = stpl+1, finpl
         if (strand(1,nxpl) .eq. thcode)  goto 10
         if (strand(2,nxpl) .eq. thcode)  goto 10
      end do
      nxpl = 0
   10 continue
      strdpl = nxpl
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine whstrd  ##
c     ##                     ##
c     #########################
c
c
c     "whtstd" finds the starting position of the lowest numbered
c     strand that is greater than the last
c
c
      subroutine whstrd (strcd,stsht,finsht,stres,stpl,lststd,bstval)
      implicit none
      include 'sstruc.i'
      integer nxstr,nxpl
      integer stsht,finsht,stres,stpl,lststd,bstval
      integer strcd(2,maxrsd)
c
c
      bstval = 0
      do nxstr = stsht, finsht
         do nxpl = 1, 2
            if (abs(strcd(nxpl,nxstr)) .gt. lststd) then
               if (bstval .eq. 0) then
                  bstval = abs(strcd(nxpl,nxstr))
                  stpl = nxpl
                  stres = nxstr
               else
                  if (abs(strcd(nxpl,nxstr)) .lt. bstval) then
                     bstval = abs(strcd(nxpl,nxstr))
                     stpl = nxpl
                     stres = nxstr
                  end if
               end if
            end if
         end do
      end do
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine shtlab  ##
c     ##                     ##
c     #########################
c
c
      Subroutine Shtlab(ssbond, bridpt, shtcod, seqlen)
c
c     label the sheets using numbers which are turned into
c     alphabetic symbols later
c
      include 'sstruc.i'
      character ssbond(nstruc,maxrsd)*1
      integer   bridpt(2,maxrsd), shtcod(maxrsd), seqlen
      integer   nxres, stsht, finsht, shtnum, nxisht, nxpl
      logical   found
      do 10, nxres = 1, seqlen
         shtcod(maxrsd) = 0
   10 continue
      shtnum = 0
      nxres = 1
   20 continue
      call fdshus(nxres, ssbond, shtcod, stsht, finsht, seqlen)
      if (stsht .ne. 0) then
         shtnum = shtnum + 1
         do 40, nxisht = stsht, finsht
            shtcod(nxisht) = shtnum
            do 30, nxpl = 1,2
               If (bridpt(nxpl,nxisht) .ne. 0) then
                  if (shtcod(bridpt(nxpl,nxisht)) .eq. 0)
     &         call setsht(bridpt(nxpl,nxisht), shtnum, shtcod,
     &                     ssbond, seqlen)
               end if
   30       continue
   40    continue
   50    continue
         call fdshps(finsht+1, shtnum, ssbond, shtcod, bridpt, found,
     &               seqlen)
         if (found) go to 50
         nxres = finsht + 1
         if (nxres .le. seqlen) go to 20
      end if
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine fdshus  ##
c     ##                     ##
c     #########################
c
c
      Subroutine fdshus(stpt, ssbond, shtcod, stsht, finsht, seqlen)
c
c     find the first unset sheet starting at stpt
c
      include 'sstruc.i'
      integer   stpt, stsht, finsht, shtcod(maxrsd), seqlen
      character ssbond(nstruc,maxrsd)*1
      integer   nxres
      stsht = 0
      nxres = stpt
   10 continue
      if (ssbond(sheet,nxres) .eq. space .or. shtcod(nxres) .ne. 0) then
         nxres = nxres + 1
         if (nxres .gt. seqlen) go to 30
         go to 10
      end if
      stsht = nxres
   20 continue
      nxres = nxres + 1
      if (nxres .le. seqlen) then
         if (ssbond(sheet,nxres) .ne. space) goto 20
      end if
      finsht = nxres - 1
   30 continue
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine fdshps  ##
c     ##                     ##
c     #########################
c
c
      subroutine fdshps(stpt, shtnum, ssbond, shtcod, bridpt, found,
     &                  seqlen)
c
c     find the next partially set sheet, if any (neg shtnum), set it
c     to fully set and then set its partners to partly set if not
c     already set
c
      include 'sstruc.i'
      integer   stpt, shtnum, shtcod(maxrsd), bridpt(2,maxrsd), seqlen
      character ssbond(nstruc,maxrsd)
      logical   found
      integer   nxres, stsht, finsht, nxpl
      found = .false.
      nxres = stpt
   10 continue
      if (shtcod(nxres) .ne. - shtnum) then
         nxres = nxres + 1
         if (nxres .gt. seqlen) goto 50
         goto 10
      end if
      stsht = nxres
      found = .true.
   20 continue
      if (shtcod(nxres) .eq. -shtnum) then
         nxres = nxres + 1
         if (nxres .le. seqlen) goto 20
      end if
      finsht = nxres - 1
      do 40, nxres = stsht, finsht
         shtcod(nxres) = shtnum
         do 30, nxpl = 1,2
            if (bridpt(nxpl,nxres) .ne. 0) then
               if (shtcod(bridpt(nxpl,nxres)) .eq. 0)
     &      call setsht(bridpt(nxpl,nxres), shtnum, shtcod, ssbond,
     &                  seqlen)
            end if
   30    continue
   40 continue
   50 continue
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine setsht  ##
c     ##                     ##
c     #########################
c
c
      Subroutine Setsht(refpl, shtnum, shtcod, ssbond, seqlen)
c
c     finds the limits of the super sheet containing refpl and
c     sets this to partially set
c
      include 'sstruc.i'
      integer   refpl, shtnum, shtcod(maxrsd), seqlen
      character ssbond(nstruc,maxrsd)*1
      integer   nxres, stsht, finsht
      nxres = refpl
   10 continue
      if (ssbond(sheet,nxres) .ne. space) then
         nxres = nxres + 1
         if (nxres .le. seqlen) go to 10
      end if
      finsht = nxres - 1
      nxres = refpl
   20 continue
      if (ssbond(sheet,nxres) .ne. space) then
         nxres = nxres - 1
         if (nxres .ge. 1) go to 20
      end if
      stsht = nxres + 1
      do 30, nxres = stsht, finsht
         shtcod(nxres) = -shtnum
   30 continue
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine mkbend  ##
c     ##                     ##
c     #########################
c
c
      Subroutine MkBend(mcangs, ssbond, seqlen)
c
c     routine to determine which main chain residues are bent at over
c     the requisite angle
c
      include 'sstruc.i'
      Real      mcangs(nmnang,maxrsd)
      Character Ssbond(nstruc,maxrsd)*1
      Integer   seqlen
c
      real      bendsz
      parameter (bendsz = 70.0)
      Integer   nxres
      logical   xney
      do 10, nxres = 1, seqlen
         if (mcangs(kappa,nxres) .gt. bendsz .and.
     &   xney(mcangs(kappa,nxres), null)) ssbond(bend,nxres) = bendch
   10 continue
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine mksumm  ##
c     ##                     ##
c     #########################
c
c
      Subroutine MkSumm(ssbond, sssumm, summpl, seqlen)
c
c     generate a structure summary using the priority rules
c
      include 'sstruc.i'
      character*1 ssbond(nstruc,maxrsd), sssumm(maxrsd), summpl(maxrsd)
      integer     seqlen
      Integer     nxres
      character   stsym(nstruc)*1, altsym(nstruc)*1
      data stsym /'H', ' ', 'E', 'B', ' ', ' ', 'G', 'I', 'T', 'S', '+'/
      data altsym/'h', ' ', 'e', 'b', ' ', ' ', 'g', 'i', 't', 's', '+'/
      SAVE stsym, altsym
      do 10, nxres = 1, seqlen
         SSsumm(nxres) = space
         summpl(nxres) = space
   10 continue
      call hlxset(ssbond, sssumm, summpl, alphah, ahsz, stsym(alphah),
     &            altsym(alphah), seqlen)
      do 20, nxres = 1, seqlen
         If (ssbond(sheet,nxres) .ne. space) then
            if (sssumm(nxres) .eq. space) sssumm(nxres) = stsym(sheet)
            if (summpl(nxres) .eq. space .or. summpl(nxres) .eq.
     &      altsym(alphah) .or. summpl(nxres) .eq. altsym(sheet))
     &      summpl(nxres) = stsym(sheet)
            if (ssbond(sheet,nxres) .eq. shtsma) then
               if (summpl(nxres-1) .eq. space .or. summpl(nxres-1) .eq.
     &         stsym(bridge)) summpl(nxres-1) = altsym(sheet)
               if (summpl(nxres+1) .eq. space .or. summpl(nxres-1) .eq.
     &         stsym(bridge)) summpl(nxres+1) = altsym(sheet)
            end if
         end if
         If (ssbond(bridge,nxres) .ne. space) then
            if (sssumm(nxres) .eq. space) sssumm(nxres) = stsym(bridge)
            if (summpl(nxres) .eq. space) summpl(nxres) = stsym(bridge)
         end if
   20 continue
      call hlxset(ssbond, sssumm, summpl, thrtnh, thrtsz, stsym(thrtnh),
     &            altsym(thrtnh), seqlen)
      call hlxset(ssbond, sssumm, summpl, pih, pihsz, stsym(pih),
     &            altsym(pih), seqlen)
      call hlxres(sssumm, stsym(thrtnh), altsym(thrtnh), stsym(turn),
     &            altsym(turn), thrtsz, seqlen)
      call hlxres(summpl, stsym(thrtnh), altsym(thrtnh), stsym(turn),
     &            altsym(turn), thrtsz, seqlen)
      call hlxres(sssumm, stsym(pih), altsym(pih), stsym(turn),
     &            altsym(turn), pihsz, seqlen)
      call hlxres(summpl, stsym(pih), altsym(pih), stsym(turn),
     &            altsym(turn), pihsz, seqlen)
      call turnst(ssbond, sssumm, summpl, stsym(turn), altsym(turn),
     &            seqlen)
      call bendst(ssbond, sssumm, summpl, seqlen)
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine hlxset  ##
c     ##                     ##
c     #########################
c
c
      Subroutine Hlxset(ssbond, sssumm, summpl, hlxpos, hlxsz, hlxch,
     &                  altch,  seqlen)
c
c     go through the bond tables and note any helices into the
c     summary line
c
      include 'sstruc.i'
      character*1 ssbond(nstruc,maxrsd), sssumm(maxrsd), hlxch, altch,
     &            summpl(maxrsd)
      integer     hlxpos, hlxsz, seqlen
      integer     nxres, nxsym
      do 20, nxres = 2, seqlen - hlxsz
         if (ssbond(hlxpos,nxres) .eq. bndbeg .or.
     &   ssbond(hlxpos,nxres) .eq. bndbth) then
            if (ssbond(hlxpos,nxres-1) .eq. bndbeg .or.
     &      ssbond(hlxpos,nxres-1) .eq. bndbth) then
               do 10, nxsym = 0, hlxsz - 1
                  if (sssumm(nxres+nxsym) .eq. space)
     &         sssumm(nxres+nxsym) = hlxch
                  if (summpl(nxres+nxsym) .eq. space .OR.
     &         summpl(nxres+nxsym) .eq. altch)
     &         summpl(nxres+nxsym) = hlxch
   10          continue
               if (summpl(nxres-1) .eq. space) summpl(nxres-1) = altch
               if (summpl(nxres+hlxsz) .eq. space)
     &             summpl(nxres+hlxsz) = altch
            end if
         end if
   20 continue
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine hlxres  ##
c     ##                     ##
c     #########################
c
c
      Subroutine hlxres(summ, hlxch, altch, turnch, alturn, hlxsz,
     &                  seqlen)
c
c     to remove helix character runs of less than minimal helix size
c
      include 'sstruc.i'
      character*1 summ(maxrsd), hlxch, turnch, altch, alturn
      integer     hlxsz, seqlen
      integer     nxres, stpos, nxsym
      stpos = 0
      do 20, nxres = 1, seqlen
         if (summ(nxres) .eq. hlxch .or. summ(nxres) .eq. altch) then
            If (stpos .eq. 0) stpos = nxres
         else
            if (stpos .ne. 0) then
               if (nxres - stpos .lt. hlxsz) then
                  do 10, nxsym = stpos, nxres - 1
                     if (summ(nxsym) .eq. hlxch) then
                        summ(nxsym) = turnch
                     else
                        summ(nxsym) = alturn
                     end if
   10             continue
               end if
               stpos = 0
            end if
         end if
   20 continue
      if (stpos .ne. 0) then
         if (seqlen - stpos + 1 .lt. hlxsz) then
            do 30, nxsym = stpos, seqlen
               if (summ(nxres) .eq. hlxch) then
                  summ(nxsym) = turnch
               else
                  summ(nxsym) = alturn
               end if
   30       continue
         end if
      end if
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine turnst  ##
c     ##                     ##
c     #########################
c
c
      Subroutine turnst(ssbond, sssumm, summpl, turnch, alturn, seqlen)
c
c     search for solitary turn type bonds and set in the symbol
c
      include 'sstruc.i'
      character*1 ssbond(nstruc,maxrsd), sssumm(maxrsd), turnch,
     &            summpl(maxrsd), alturn
      integer     seqlen
      integer     nxres, nxsym, nxhlx, hlxpri(nhelix), hlxsz(nhelix)
      hlxpri(1) = alphah
      hlxpri(2) = thrtnh
      hlxpri(3) = pih
      hlxsz(1) = ahsz
      hlxsz(2) = thrtsz
      hlxsz(3) = pihsz
      do 40, nxhlx = 1, nhelix
         If (ssbond(hlxpri(nxhlx),1) .eq. bndbeg .and.
     &   ssbond(hlxpri(nxhlx),2) .ne. bndbeg) then
            do 10, nxsym = 2, hlxsz(nxhlx)
               If (sssumm(nxsym) .eq. space) sssumm(nxsym) = turnch
               If (summpl(nxsym) .eq. space .or. summpl(nxsym) .eq.
     &      alturn) summpl(nxsym) = turnch
   10       continue
            If (summpl(1) .eq. space) summpl(1) = alturn
            If (summpl(hlxsz(nxhlx)+1) .eq. space)
     &      summpl(hlxsz(nxhlx)+1) = alturn
         end if
         do 30, nxres = 2, seqlen - hlxsz(nxhlx)
            If ((ssbond(hlxpri(nxhlx),nxres) .eq. bndbeg .or.
     &   ssbond(hlxpri(nxhlx),nxres) .eq. bndbth) .AND.
     &   (ssbond(hlxpri(nxhlx),nxres-1) .ne. bndbeg .and.
     &   ssbond(hlxpri(nxhlx),nxres-1) .ne. bndbth) .AND.
     &   (ssbond(hlxpri(nxhlx),nxres+1) .ne. bndbeg .and.
     &   ssbond(hlxpri(nxhlx),nxres+1) .ne. bndbth)) then
               do 20, nxsym = nxres + 1, Nxres + hlxsz(nxhlx) - 1
                  If (sssumm(nxsym) .eq. space) sssumm(nxsym) = turnch
                  If (summpl(nxsym) .eq. space .or. summpl(nxsym) .eq.
     &      alturn) summpl(nxsym) = turnch
   20          continue
               If (summpl(nxres) .eq. space) summpl(nxres) = alturn
               If (summpl(nxres+hlxsz(nxhlx)) .eq. space)
     &      summpl(nxres+hlxsz(nxhlx)) = alturn
            end if
   30    continue
   40 continue
      return
      end
c
c
c     #########################
c     ##                     ##
c     ##  subroutine bendst  ##
c     ##                     ##
c     #########################
c
c
c     "bendst" enters bend symbols into the summary line
c
c
      subroutine bendst (ssbond,sssumm,summpl,seqlen)
      implicit none
      include 'sstruc.i'
      integer seqlen,nxres
      character*1 ssbond(nstruc,maxrsd)
      character*1 sssumm(maxrsd),summpl(maxrsd)
c
c
      do nxres = 1, seqlen
         if (ssbond(bend,nxres) .ne. space) then
            if (sssumm(nxres) .eq. space)
     &         sssumm(nxres) = ssbond(bend,nxres)
            if (summpl(nxres) .eq. space)
     &         summpl(nxres) = ssbond(bend,nxres)
         end if
      end do
      return
      end
c
c
c     #######################
c     ##                   ##
c     ##  function cystyp  ##
c     ##                   ##
c     #######################
c
c
c     "cyctyp" checks that we have a potential disulfide former
c
c
      function cystyp (seqcod)
      implicit none
      include 'sstruc.i'
      integer  seqcod
      logical cystyp
c
c
      If (seqcod .eq. cyscod .or. seqcod .eq. mprcod) then
         cystyp = .true.
      else
         cystyp = .false.
      end if
      return
      end
c
c
c     ########################
c     ##                    ##
c     ##  subroutine dsset  ##
c     ##                    ##
c     ########################
c
c
      Subroutine dsset(part1,  part2,  dsdsts, scangs, scaaat, scatin,
     &                 atomin, mcaaat, dsdef,  dspart, typefl, seqlen)
c
c     calculate all the features for a disulphide bridge. that is 3 chi
c     angles, calpha, cbeta, and ss distances. set in the partner and
c     definition fields. Check for conflict with author defined records
c     or previously defined bridges
c
      include 'sstruc.i'
      integer   part1, part2, dspart(maxrsd), seqlen
      real      dsdsts(3,maxrsd), scangs(nsdang,maxrsd),
     &          scaaat(ncoord,sdchna,maxrsd),
     &          mcaaat(ncoord,mnchna,maxrsd)
      logical   scatin(sdchna,maxrsd), atomin(mnchna,maxrsd)
      character dsdef(maxrsd)*1, typefl*1
      real      atdist, dihed
      character*1 botdef, autdef
      real        mxdis
      parameter  (botdef = 'B', autdef = 'A', mxdis = 9.9)
      integer     nxdis
      If ((dspart(part1) .ne. 0 .and. dspart(part1) .ne. part2) .or.
     &   (dspart(part1) .eq. 0 .and. dspart(part2) .ne. 0)) then
      else
         if (dsdef(part1) .eq. autdef) then
            dsdef(part1) = botdef
            dsdef(part2) = botdef
         else
            dspart(part1) = part2
            dspart(part2) = part1
            dsdef(part1) = typefl
            dsdef(part2) = typefl
            if (atomin(calph,part1) .and. scatin(cbeta,part1) .and.
     &         scatin(sgamma,part1) .and. scatin(sgamma,part2))
     &         scangs(2,part1) = dihed(nmnang+2,
     &         mcaaat(1,calph,part1), scaaat(1,cbeta,part1),
     &         scaaat(1,sgamma,part1), scaaat(1,sgamma,part2))
            if (scatin(cbeta,part1) .and. scatin(sgamma,part1) .and.
     &         scatin(sgamma,part2) .and. scatin(cbeta,part2)) then
               scangs(3,part1) = dihed(nmnang+3,
     &         scaaat(1,cbeta,part1), scaaat(1,sgamma,part1),
     &         scaaat(1,sgamma,part2), scaaat(1,cbeta,part2))
               scangs(3,part2) = scangs(3,part1)
            end if
            if (atomin(calph,part2) .and. scatin(cbeta,part2) .and.
     &         scatin(sgamma,part2) .and. scatin(sgamma,part1))
     &         scangs(2,part2) = dihed(nmnang+2,
     &         mcaaat(1,calph,part2), scaaat(1,cbeta,part2),
     &         scaaat(1,sgamma,part2), scaaat(1,sgamma,part1))
            do 10, nxdis = 1, 3
               dsdsts(nxdis,part1) = -1.0
   10       continue
            If (atomin(calph,part1) .and. atomin(calph,part2))
     &         dsdsts(1,part1) = atdist(mcaaat(1,calph,part1),
     &         mcaaat(1,calph,part2))
            If (scatin(cbeta,part1) .and. atomin(cbeta,part2))
     &         dsdsts(2,part1) = atdist(scaaat(1,cbeta,part1),
     &         scaaat(1,cbeta,part2))
            If (scatin(sgamma,part1) .and. scatin(sgamma,part2))
     &         dsdsts(3,part1) = atdist(scaaat(1,sgamma,part1),
     &         scaaat(1,sgamma,part2))
            do 20, nxdis = 1, 3
               if (dsdsts(nxdis,part1) .gt. mxdis) then
                  dsdsts(nxdis,part1) = mxdis
               end if
   20       continue
            do 30, nxdis = 1,3
               dsdsts(nxdis,part2) = dsdsts(nxdis,part1)
   30       continue
         end if
      end if
      return
      end
c
c
c     ######################
c     ##                  ##
c     ##  function respl  ##
c     ##                  ##
c     ######################
c
c
c     "respl" searches through the residue identifiers to find
c     the position of the relevant code; codes will normally be
c     in order so the search starts from the last position
c
c
      function respl (seqbcd,theres,lstpos,seqlen)
c
c
      implicit none
      include 'sstruc.i'
      integer lstpos,seqlen
      integer respl,nxres
      character*6 theres,seqbcd(maxrsd)
c
c
      do nxres = lstpos+1, seqlen
         if (seqbcd(nxres) .eq. theres)  goto 10
      end do
      do nxres = 1, lstpos
         if (seqbcd(nxres) .eq. theres)  goto 10
      end do
      nxres = -1
   10 continue
      respl = nxres
      return
      end
c
c
c     #####################
c     ##                 ##
c     ##  function xeqy  ##
c     ##                 ##
c     #####################
c
c
      function xeqy (x,y)
      implicit none
      real acurcy
      parameter (acurcy=0.0001)
      real x,y
      logical xeqy
c
c
      xeqy = (abs(x-y) .lt. acurcy)
      return
      end
c
c
c     #####################
c     ##                 ##
c     ##  function xney  ##
c     ##                 ##
c     #####################
c
c
      function xney (x,y)
      implicit none
      real acurcy
      parameter (acurcy=0.0001)
      real x,y
      logical xney
c
c
      xney = (abs(x-y) .gt. acurcy)
      return
      end
