      Subroutine MkTB(Hbond, SSbond, mcangs, bridpt, chnend, seqlen)    MKTB001
c                                                                       MKTB002
c     routine to establish the turns and bridges                        MKTB003
c                                                                       MKTB004
      INCLUDE 'sstruc.par'                                              MKTB005
                                                                        MKTB006
CHECK v.3.2-->                                                          MKTB007
      SAVE turnch, turnsz, turnpl, bsofst, bsrng, pbrdg, stchap, stchpa MKTB008
CHECK v.3.2<--                                                          MKTB009
                                                                        MKTB010
      Integer   Hbond(maxbnd,maxres), seqlen, bridpt(2,maxres),         MKTB011
     &          chnend(maxchn)                                          MKTB012
      Real      Mcangs(nmnang,maxres)                                   MKTB013
      Character SSbond(nstruc,maxres)*1                                 MKTB014
      Integer   strdpl                                                  MKTB015
                                                                        MKTB016
      Integer   nrules, parlel                                          MKTB017
      Parameter (nrules = 3, parlel = 1)                                MKTB018
      Integer   nxres, nxbond, nxturn, turnsz(nturns), turnpl(nturns),  MKTB019
     &          bsofst(nrules,4), i, j, nxrule, bndi, bndj, bndptr,     MKTB020
     &          brpl, brdgpt, bsrng(nrules,2), pbrdg(nrules), brpla,    MKTB021
     &          brdg(4,maxres), brptr, brdir, newptr, nxptr, nxpl,      MKTB022
     &          exrule(2,nrules), brptpl, nwptpl, nxstrn,               MKTB023
     &          lststd, stsht, finsht, nxstr, chcode, stchpl, thstpl,   MKTB024
     &          nxstpl, nxblg, nxbrdg, strpl, brdpt, nxbrpl,            MKTB025
     &          strcd(2,maxres), bstval, shtcod(maxres), fstchn         MKTB026
                                                                        MKTB027
      Character turnch(nturns)*1, wkch*1, strdch*1, brdch*1,            MKTB028
     &          stchap*(nstchs), stchpa*(nstchs),                       MKTB029
     &          nstchp*(nstchs*2+10)                                    MKTB030
      logical   xney                                                    MKTB031
c                                                                       MKTB032
c     for turn calculations                                             MKTB033
c                                                                       MKTB034
      data turnch / '3', '4', '5'/                                      MKTB035
      data turnsz / 3, 4, 5/                                            MKTB036
      data turnpl / thrtnh, alphah, pih/                                MKTB037
c                                                                       MKTB038
c     for bridge calculations                                           MKTB039
c                                                                       MKTB040
      data ((bsofst(i,j),j = 1,4),i = 1,nrules)                         MKTB041
     &     / -1, 0, 0, 1, 0, 0, 0, 0, -1, -1, -2, 1/                    MKTB042
      data ((bsrng(i,j),j = 1,2), i = 1,nrules)                         MKTB043
     &     / 2, -1, 1, 0, 2, -1/                                        MKTB044
      data pbrdg / 1, -1, -1/                                           MKTB045
      data exrule /-1, 1, 1, 1, -1, -1/                                 MKTB046
c                                                                       MKTB047
c     for strand and bridge labelling                                   MKTB048
c                                                                       MKTB049
      data stchap /'ABCDEFGHIJKLMNOPQRSTUVWXYZ'/                        MKTB050
      data stchpa /'abcdefghijklmnopqrstuvwxyz'/                        MKTB051
      data nstchp(1:26) /'ABCDEFGHIJKLMNOPQRSTUVWXYZ'/,                 MKTB052
     &     nstchp(27:52) /'abcdefghijklmnopqrstuvwxyz'/,                MKTB053
     &     nstchp(53:62) /'1234567890'/                                 MKTB054
                                                                        MKTB055
CHECK v.3.2-->                                                          MKTB056
C      SAVE turnch, turnsz, turnpl, bsofst, bsrng, pbrdg, stchap, stchpaMKTB057
CHECK v.3.2<--                                                          MKTB058
                                                                        MKTB059
      do 100, nxres = 1, seqlen                                         MKTB060
      do 100, i = 1, nstruc                                             MKTB061
      ssbond(i,nxres) = space                                           MKTB062
  100 continue                                                          MKTB063
                                                                        MKTB064
      fstchn = 1                                                        MKTB065
      do 1000, nxres = 1, seqlen                                        MKTB066
      if (nxres .gt. chnend(fstchn)) fstchn = fstchn + 1                MKTB067
      do 800, nxbond = 1, 2                                             MKTB068
      do 600, nxturn = 1, nturns                                        MKTB069
      if (Hbond(nxbond,nxres) .ne. 0) then                              MKTB070
         If (Hbond(nxbond,nxres) - nxres .eq. turnsz(nxturn) .and.      MKTB071
     &      Hbond(nxbond,nxres) .le. chnend(fstchn)) then               MKTB072
            If (SSbond(turnpl(nxturn),nxres) .eq. bndend) then          MKTB073
               SSbond(turnpl(nxturn),nxres) = bndbth                    MKTB074
            else                                                        MKTB075
               SSbond(turnpl(nxturn),nxres) = bndbeg                    MKTB076
            end if                                                      MKTB077
            SSbond(turnpl(nxturn),Hbond(nxbond,nxres)) = bndend         MKTB078
            do 400, i = nxres + 1, nxres + turnsz(nxturn) - 1           MKTB079
            If (SSbond(turnpl(nxturn),i) .eq. space)                    MKTB080
     &         SSbond(turnpl(nxturn),i) = turnch(nxturn)                MKTB081
  400       continue                                                    MKTB082
            do 500 i = nxres, nxres + turnsz(nxturn)                    MKTB083
            SSbond(turn,i) = trnch                                      MKTB084
  500       continue                                                    MKTB085
         end if                                                         MKTB086
      end if                                                            MKTB087
  600 continue                                                          MKTB088
  800 continue                                                          MKTB089
 1000 continue                                                          MKTB090
                                                                        MKTB091
      do 2000, nxres = 1,seqlen                                         MKTB092
      do 2000, nxbond = 1, 4                                            MKTB093
      brdg(nxbond,nxres) = 0                                            MKTB094
 2000 continue                                                          MKTB095
                                                                        MKTB096
      do 4000, nxrule = 1, nrules                                       MKTB097
      do 3000, Nxres = bsrng(nxrule,1), seqlen + bsrng(nxrule,2)        MKTB098
      do 2400, bndi = 1, 2                                              MKTB099
      bndptr = hbond(bndi,nxres+bsofst(nxrule,1))                       MKTB100
      if (nxrule .le. parlel .or. bndptr .gt. nxres) then               MKTB101
         brdgpt = bndptr + bsofst(nxrule,2)                             MKTB102
         If (bndptr .gt. - bsofst(nxrule,3) .and.                       MKTB103
     &      iabs(nxres - brdgpt) .gt. bdgsep)  then                     MKTB104
            do 2200, bndj = 1, 2                                        MKTB105
            If (hbond(bndj,bndptr+bsofst(nxrule,3)) .eq.                MKTB106
     &         nxres+bsofst(nxrule,4)) then                             MKTB107
               brpl = 1                                                 MKTB108
               If (brdg(brpl,nxres) .ne. 0) brpl = 2                    MKTB109
               Brdg(brpl,nxres) = pbrdg(nxrule) * brdgpt                MKTB110
               brdg(brpl+2,nxres) = exrule(1,nxrule)                    MKTB111
               brpla = 1                                                MKTB112
               If (brdg(brpla,brdgpt) .ne. 0) brpla = 2                 MKTB113
               Brdg(brpla,brdgpt) = pbrdg(nxrule) * nxres               MKTB114
               brdg(brpla+2,brdgpt) = exrule(2,nxrule)                  MKTB115
            end if                                                      MKTB116
 2200       continue                                                    MKTB117
         end if                                                         MKTB118
      end if                                                            MKTB119
 2400 continue                                                          MKTB120
 3000 continue                                                          MKTB121
 4000 continue                                                          MKTB122
                                                                        MKTB123
      do 4500, nxres = 1, seqlen                                        MKTB124
      if (xney(mcangs(chiral,nxres), null)) then                        MKTB125
         ssbond(chisgn,nxres) = '+'                                     MKTB126
         if (mcangs(chiral,nxres) .lt. 0.0) ssbond(chisgn,nxres) = '-'  MKTB127
      end if                                                            MKTB128
 4500 continue                                                          MKTB129
                                                                        MKTB130
CHECK v.3.0.1-->                                                        MKTB131
C 5000 continue                                                         MKTB132
CHECK v.3.0.1<--                                                        MKTB133
      do 5100, nxres = 1, seqlen                                        MKTB134
      shtcod(nxres) = 0                                                 MKTB135
      strcd(1,nxres) = 0                                                MKTB136
      strcd(2,nxres) = 0                                                MKTB137
      bridpt(1,nxres) = 0                                               MKTB138
      bridpt(2,nxres) = 0                                               MKTB139
 5100 continue                                                          MKTB140
      nxstrn = 0                                                        MKTB141
                                                                        MKTB142
      do 7000, nxres = 1, seqlen                                        MKTB143
      do 6900, brpl = 1, 2                                              MKTB144
      If (brdg(brpl,nxres) .ne. 0 .and.                                 MKTB145
     &   iabs(brdg(brpl,nxres)) .gt. nxres) then                        MKTB146
         brptr = iabs(brdg(brpl,nxres))                                 MKTB147
         brdir = brdg(brpl,nxres) / brptr                               MKTB148
         brptpl = 2                                                     MKTB149
         if (iabs(brdg(1,brptr)) .eq. nxres) brptpl = 1                 MKTB150
         do 5600, nxptr = nxres + 1, min(seqlen,nxres+blgszl)           MKTB151
         do 5500, nxpl = 1,2                                            MKTB152
         If (brdg(nxpl,nxptr) * brdir .gt. 0) then                      MKTB153
            newptr = iabs(brdg(nxpl,nxptr))                             MKTB154
CHECK v.2.1.3-->                                                        MKTB155
            IF (iabs(newptr-brptr).NE.0) THEN                           MKTB156
CHECK v.2.1.3-->                                                        MKTB157
            if ((newptr-brptr)/iabs(newptr-brptr) .eq. brdir) then      MKTB158
               if ((nxptr - nxres .gt. blgszs .and.                     MKTB159
     &            iabs(newptr - brptr) .le. blgszs) .OR.                MKTB160
     &            (nxptr - nxres .le. blgszs .and.                      MKTB161
     &            iabs(newptr - brptr) .le. blgszl)) then               MKTB162
                  nwptpl = 2                                            MKTB163
                  if (iabs(brdg(1,newptr)) .eq. nxptr) nwptpl = 1       MKTB164
                  do 5300, i = nxres, nxptr                             MKTB165
                  if (ssbond(sheet,i) .eq. space)                       MKTB166
     &               ssbond(sheet,i) = shtsym                           MKTB167
 5300             continue                                              MKTB168
                  if (brdg(brpl+2,nxres) .lt. 0)                        MKTB169
     &               ssbond(sheet,nxres) = shtsma                       MKTB170
                  if (brdg(nxpl+2,nxptr) .lt. 0)                        MKTB171
     &               ssbond(sheet,nxptr) = shtsma                       MKTB172
                  do 5400, i = brptr, newptr, brdir                     MKTB173
                  if (ssbond(sheet,i) .eq. space)                       MKTB174
     &               ssbond(sheet,i) = shtsym                           MKTB175
 5400             continue                                              MKTB176
                  if (brdg(brptpl+2,brptr) .lt. 0)                      MKTB177
     &               ssbond(sheet,brptr) = shtsma                       MKTB178
                  if (brdg(nwptpl+2,newptr) .lt. 0)                     MKTB179
     &               ssbond(sheet,newptr) = shtsma                      MKTB180
                  if (strcd(brpl,nxres) .eq. 0) then                    MKTB181
                     nxstrn = nxstrn + 1                                MKTB182
                     strcd(brpl,nxres) = nxstrn * brdir                 MKTB183
                     strcd(brptpl,brptr) = nxstrn * brdir               MKTB184
                  end if                                                MKTB185
                  strcd(nxpl,nxptr) = strcd(brpl,nxres)                 MKTB186
                  strcd(nwptpl,newptr) = strcd(brptpl,brptr)            MKTB187
                  go to 5700                                            MKTB188
               end if                                                   MKTB189
            end if                                                      MKTB190
CHECK v.2.1.3-->                                                        MKTB191
            ENDIF                                                       MKTB192
CHECK v.2.1.3-->                                                        MKTB193
         end if                                                         MKTB194
 5500    continue                                                       MKTB195
 5600    continue                                                       MKTB196
 5700    continue                                                       MKTB197
         wkch = pbch                                                    MKTB198
         if (brdir .lt. 0) wkch = apbch                                 MKTB199
         ssbond(bridge,nxres) = wkch                                    MKTB200
         ssbond(bridge,brptr) = wkch                                    MKTB201
      end if                                                            MKTB202
 6900 continue                                                          MKTB203
 7000 continue                                                          MKTB204
                                                                        MKTB205
      nxres = 1                                                         MKTB206
 7500 continue                                                          MKTB207
      if (ssbond(sheet,nxres) .eq. space) then                          MKTB208
         nxres = nxres + 1                                              MKTB209
         if (nxres .gt. seqlen) goto 10000                              MKTB210
         goto 7500                                                      MKTB211
      end if                                                            MKTB212
                                                                        MKTB213
      stsht = nxres                                                     MKTB214
      nxres = stsht + 1                                                 MKTB215
 7700 continue                                                          MKTB216
      if (ssbond(sheet,nxres) .ne. space) then                          MKTB217
         nxres = nxres + 1                                              MKTB218
         if (nxres .gt. seqlen) goto 7800                               MKTB219
         goto 7700                                                      MKTB220
      end if                                                            MKTB221
 7800 continue                                                          MKTB222
      finsht = nxres - 1                                                MKTB223
      lststd = 0                                                        MKTB224
                                                                        MKTB225
 8000 continue                                                          MKTB226
      call whstrd(strcd, stsht, finsht, nxstr, nxpl, lststd, bstval)    MKTB227
      if (bstval .ne. 0) then                                           MKTB228
         lststd = abs(strcd(nxpl,nxstr))                                MKTB229
         chcode = mod(lststd,nstchs)                                    MKTB230
         if (chcode .eq. 0) chcode = nstchs                             MKTB231
         if (strcd(nxpl,nxstr) .lt. 0) then                             MKTB232
            strdch = stchap(chcode:chcode)                              MKTB233
         else                                                           MKTB234
            strdch = stchpa(chcode:chcode)                              MKTB235
         end if                                                         MKTB236
         stchpl = bridg1                                                MKTB237
         if (ssbond(bridg1,nxstr) .ne. space) stchpl = bridg2           MKTB238
         if (stchpl .ne. bridg2) then                                   MKTB239
            thstpl = nxstr                                              MKTB240
 8200       continue                                                    MKTB241
            nxstpl = strdpl(thstpl,finsht,strcd(nxpl,nxstr),strcd)      MKTB242
            if (nxstpl .le. 0) goto 8300                                MKTB243
            if (ssbond(bridg1,nxstpl) .ne. space) stchpl = bridg2       MKTB244
            thstpl = nxstpl                                             MKTB245
            if (stchpl .ne. bridg2) goto 8200                           MKTB246
         end if                                                         MKTB247
 8300    continue                                                       MKTB248
         ssbond(stchpl,nxstr) = strdch                                  MKTB249
         bridpt(stchpl-bridg1+1,nxstr) = abs(brdg(nxpl,nxstr))          MKTB250
         thstpl = nxstr                                                 MKTB251
 8500    continue                                                       MKTB252
         nxstpl = strdpl(thstpl,finsht,strcd(nxpl,nxstr),strcd)         MKTB253
         if (nxstpl .le. 0) goto 8700                                   MKTB254
         do 8600, nxblg = thstpl + 1, nxstpl - 1                        MKTB255
         ssbond(stchpl,nxblg) = bulgch                                  MKTB256
 8600    continue                                                       MKTB257
         ssbond(stchpl,nxstpl) = strdch                                 MKTB258
         strpl = 1                                                      MKTB259
         if (strcd(1,nxstpl) .ne. strcd(nxpl,nxstr)) strpl = 2          MKTB260
         bridpt(stchpl-bridg1+1,nxstpl) = abs(brdg(strpl,nxstpl))       MKTB261
         thstpl = nxstpl                                                MKTB262
         go to 8500                                                     MKTB263
 8700    continue                                                       MKTB264
         go to 8000                                                     MKTB265
      end if                                                            MKTB266
      nxres = finsht + 1                                                MKTB267
      if (nxres .le. seqlen) goto 7500                                  MKTB268
10000 continue                                                          MKTB269
      If (lststd .gt. nstchs)                                           MKTB270
     &   Write(errfi,10100) lststd                                      MKTB271
10100 format('there are ',I3,' strands. Labels have restarted')         MKTB272
                                                                        MKTB273
      Call shtlab(ssbond, bridpt, shtcod, seqlen)                       MKTB274
      do 10500 nxres = 1, seqlen                                        MKTB275
      if (shtcod(nxres) .ne. 0) then                                    MKTB276
c        chcode = mod(shtcod(nxres),nstchs)                             MKTB277
         chcode = shtcod(nxres)                                         MKTB278
c        if (chcode .eq. 0) chcode = nstchs                             MKTB279
         ssbond(sheet1,nxres) = nstchp(chcode:chcode)                   MKTB280
      end if                                                            MKTB281
10500 continue                                                          MKTB282
                                                                        MKTB283
      nxbrdg = 0                                                        MKTB284
      do 11000, nxres = 1, seqlen                                       MKTB285
      do 10900, nxpl = 1, 2                                             MKTB286
      If (brdg(nxpl,nxres) .ne. 0 .and. strcd(nxpl,nxres) .eq. 0        MKTB287
     &   .and. abs(brdg(nxpl,nxres)) .gt. nxres) then                   MKTB288
         nxbrdg = nxbrdg + 1                                            MKTB289
         brdpt = abs(brdg(nxpl,nxres))                                  MKTB290
         chcode = mod(nxbrdg,nstchs)                                    MKTB291
         if (chcode .eq. 0) chcode = nstchs                             MKTB292
         if (brdg(nxpl,nxres) .lt. 0) then                              MKTB293
            brdch = stchap(chcode:chcode)                               MKTB294
         else                                                           MKTB295
            brdch = stchpa(chcode:chcode)                               MKTB296
         end if                                                         MKTB297
         nxbrpl = bridg1                                                MKTB298
         if (ssbond(bridg1,nxres) .ne. space) nxbrpl = bridg2           MKTB299
         ssbond(nxbrpl,nxres) = brdch                                   MKTB300
         bridpt(nxbrpl-bridg1+1,nxres) = brdpt                          MKTB301
         nxbrpl = bridg1                                                MKTB302
         if (ssbond(bridg1,brdpt) .ne. space) nxbrpl = bridg2           MKTB303
         ssbond(nxbrpl,brdpt) = brdch                                   MKTB304
         bridpt(nxbrpl-bridg1+1,brdpt) = nxres                          MKTB305
      end if                                                            MKTB306
10900 continue                                                          MKTB307
11000 continue                                                          MKTB308
      If (nxbrdg .gt. nstchs)                                           MKTB309
     &  Write(errfi,11100) nxbrdg                                       MKTB310
11100 format('there are ',I3,' bridges. Labels have restarted')         MKTB311
                                                                        MKTB312
      END                                                               MKTB313
