      subroutine mksang (mcaaat, atomin, scaaat, scangs, scatin, seqcod,MKSA001
     &                   aacod3, hetcod, seqlen, nscats, chstop, chstrt,MKSA002
     &                   seqbcd, mcser,  scser,  mcocc,  mcthrm, scocc, MKSA003
CHECK v.3.2-->                                                          MKSA004
C     +                   scthrm, scname, altnam, naltmc, naltsc, altmciMKSA005
     &                   scthrm, scname, naltmc, naltsc, altmci,        MKSA006
CHECK v.3.2<--                                                          MKSA007
     &                   altsci, altmcr, altscr, altmca, altsca, lab,   MKSA008
     &                   extdat, extres, nline,  tphnam, olenam, pphnam,MKSA009
     &                   atname, mcangs, nrec,   mcalt,  scalt,  altmcc,MKSA010
     &                   altscc, pyrnam, totswp, altdat, altlin, altres,MKSA011
CHECK v.2.1.3-->                                                        MKSA012
C     +                   batnam)                                       MKSA013
CHECK v.3.2-->                                                          MKSA014
C     +                   batnam, NATALT, NATZER, NHEALT, NHEZER)       MKSA015
     &                   batnam, NATALT, NATZER, NHEALT, NHEZER, NMR)   MKSA016
CHECK v.3.2<--                                                          MKSA017
CHECK v.2.1.3<--                                                        MKSA018
c     calculate chi angles from atom data. check for that alternates    MKSA019
c     are closest to c alpha                                            MKSA020
c                                                                       MKSA021
      include 'brkcln.par'                                              MKSA022
c                                                                       MKSA023
      real      mcaaat(ncoord,mnchna,maxres),                           MKSA024
     &          scaaat(ncoord,sdchna,maxres), scangs(nsdang,maxres),    MKSA025
     &          mcocc(mnchna,maxres), mcthrm(mnchna,maxres),            MKSA026
     &          scocc(sdchna,maxres), scthrm(sdchna,maxres),            MKSA027
     &          altmcr(2,altmax), altscr(2,altmax),                     MKSA028
     &          altmca(3,altmax), altsca(3,altmax),                     MKSA029
     &          mcangs(nmnang,maxres)                                   MKSA030
c                                                                       MKSA031
      character aacod3*(abetsz*3), hetcod*63, seqbcd(maxres)*6,         MKSA032
CHECK v.3.2-->                                                          MKSA033
C     +          scname(sideaa)*44, altnam(3)*44, lab(maxres)*1,        MKSA034
     &          scname(sideaa)*44, lab(maxres)*1,                       MKSA035
CHECK v.3.2<--                                                          MKSA036
     &          extdat(maxlin)*66, tphnam*((4*mnchna)-4),               MKSA037
     &          olenam*((4*mnchna)-4), pphnam*((4*mnchna)-4),           MKSA038
     &          atname*((4*mnchna)-4), chstop(maxres)*1,                MKSA039
     &          chstrt(maxres)*1, mcalt(mnchna,maxres)*1,               MKSA040
     &          scalt(sdchna,maxres)*1, altmcc(maxres)*1,               MKSA041
     &          altscc(maxres)*1, pyrnam*((4*mnchna)-4),                MKSA042
     &          altdat(maxalt)*66, batnam*((4*mnchna)-4)                MKSA043
c                                                                       MKSA044
      logical   atomin(mnchna,maxres), scatin(sdchna,maxres)            MKSA045
c                                                                       MKSA046
      integer   seqcod(maxres), seqlen, mcser(mnchna,maxres),           MKSA047
     &          scser(sdchna,maxres), naltmc, naltsc,                   MKSA048
     &          altmci(3,altmax), altsci(3,altmax),                     MKSA049
     &          extres(maxlin), nline, nrec, altres(maxalt),            MKSA050
     &          altlin, natsdc(sideaa), nxang, i, j, nxchi, nxres, nchi,MKSA051
     &          aacod, extprc(sideaa), swatm, extatm, chipl, nswap,     MKSA052
     &          wkswap, nold(sdchna,maxres), nscats(sideaa), k,         MKSA053
     &          change(maxres), n, ii, istart, nathet,                  MKSA054
     &          totswp(sideaa), astart                                  MKSA055
c                                                                       MKSA056
c     integer   altref                                                  MKSA057
c                                                                       MKSA058
CHECK v.3.2-->                                                          MKSA059
C      logical   athere(maxchi+3), alt, xney                            MKSA060
      logical   athere(maxchi+3), NMR, xney                             MKSA061
CHECK v.3.2<--                                                          MKSA062
c                                                                       MKSA063
      real      relats(ncoord,maxchi+3), distmn, distal, ang1, ang2,    MKSA064
     &          distg1, distg2                                          MKSA065
c                                                                       MKSA066
      character outerr*80, wkres*3, scdata*44, ld*1                     MKSA067
c     character altps1, altps2                                          MKSA068
      character cbet*4, scatom*4, scold*4, mcatom*4                     MKSA069
      character label*6                                                 MKSA070
CHECK v.2.1-->                                                          MKSA071
      character altlab*6                                                MKSA072
CHECK v.2.1<--                                                          MKSA073
c                                                                       MKSA074
      real      atdist, dihed                                           MKSA075
CHECK v.2.0.1-->                                                        MKSA076
      real      dihang                                                  MKSA077
CHECK v.2.0.1<--                                                        MKSA078
c                                                                       MKSA079
                                                                        MKSA080
CHECK v.2.1.3-->                                                        MKSA081
      INTEGER       NATALT, NATZER, NHEALT, NHEZER                      MKSA082
CHECK v.2.1.3<--                                                        MKSA083
                                                                        MKSA084
      save extprc, natsdc                                               MKSA085
c                                                                       MKSA086
      data natsdc / 2*0, 1, 2, 3, 2, 0, 2*2, 0, 4, 2, 3, 2, 0, 1, 3, 5, MKSA087
     &              2*1, 0, 1, 2, 0, 2, 2*0, 3, 4, 0, 2, 3*1, 2, 0, 4,  MKSA088
     &              0, 2, 0, 2*2, 1, 3, 1, 2*2, 3, 0, 2 /               MKSA089
c                                                                       MKSA090
      data extprc / 3*0, 3*4, 2*0, 5, 2*0, 2, 5*0, 4, 0, 3, 0, 2, 2*0,  MKSA091
     &              4, 5*0, 4, 3*0, 4, 3*0, 4, 0, 2, 4, 2*0, 2, 5, 4,   MKSA092
     &              2*0, 4 /                                            MKSA093
c                                                                       MKSA094
      data cbet / ' CB ' /                                              MKSA095
c                                                                       MKSA096
 6000 format (a6, i5, 1x, a4, a1, a3, 1x, a6, 3x, 3f8.3, 2f6.2)         MKSA097
CHECK v.3.0.1-->                                                        MKSA098
C 6001 format (a6, i5, 1x, a4, a1, a3, 1x, a6, 3x, 3f8.3, 2f6.2, 1x, a2)MKSA099
C 6002 format (a6, i5, 1x, a4, a1, a3, 1x, a6, 3x, 3f8.3, 2f6.2, 1x, i1)MKSA100
CHECK v.3.0.1<--                                                        MKSA101
 6003 format (a6, i5, 1x, a4, a1, a3, 1x, a6, 3x, 3f8.3, 2f6.2, 1x, a1, MKSA102
     & a1, a4, a1)                                                      MKSA103
CHECK v.3.0.1-->                                                        MKSA104
C 6004 format (a6, i5, 1x, a4, a1, a3, 1x, a6, 3x, 3f8.3, 2f6.2, 1x, a1,MKSA105
C     + a1)                                                             MKSA106
CHECK v.3.0.1<--                                                        MKSA107
 6005 format (a66)                                                      MKSA108
WINNT - proper MASTER record writes mumber of atoms in col 51-55        MKSA109
c 6006 format ('MASTER',i6)                                             MKSA110
 6006 format ('MASTER',44x,i6)                                          MKSA111
WINNT - end                                                             MKSA112
 6007 format ('END')                                                    MKSA113
 6008 format (a6, i5, 1x, a4, a1, a3, 1x, a6, 3x, 3f8.3, 2f6.2, 1x, a1, MKSA114
     & a1, 4x, a1)                                                      MKSA115
 6010 format (' distg1 is', 2f10.2)                                     MKSA116
 6020 format (' distg2 is', 2f10.2)                                     MKSA117
CHECK v.2.0.1-->                                                        MKSA118
C 6030 format (' ILE residue still wrong!!')                            MKSA119
CHECK v.2.1.3-->                                                        MKSA120
C 6030 format (' ILE residue still wrong!!',1x,i4)                      MKSA121
CHECK v.2.1.4-->                                                        MKSA122
 6030 format (' **** ILE residue has wrong chirality at CB',1x,i4)      MKSA123
CHECK v.2.1.4<--                                                        MKSA124
CHECK v.2.1.3<--                                                        MKSA125
CHECK v.2.0.1<--                                                        MKSA126
CHECK v.2.1.3-->                                                        MKSA127
C 6040 format (' side chain atoms swapped for residues:')               MKSA128
 6040 format (' * Side chain atoms swapped for residues:')              MKSA129
CHECK v.3.4-->                                                          MKSA130
 6042 format (' * Side chain atoms should be swapped for the following',MKSA131
     &    ' residues:',/,                                               MKSA132
     &    ' * As this is an NMR ensemble the swap has not been made')   MKSA133
CHECK v.3.4<--                                                          MKSA134
CHECK v.2.1.3<--                                                        MKSA135
CHECK v.3.2-->                                                          MKSA136
C 6050 format (a3,1x,i4,2x)                                             MKSA137
 6050 format (a3,1x,a6,1x)                                              MKSA138
CHECK v.3.2<--                                                          MKSA139
 6060 format (1x,'*',1X, a)                                             MKSA140
CHECK v.3.2-->                                                          MKSA141
C 6070 format (1x,'#',1X, a)                                            MKSA142
CHECK v.3.2<--                                                          MKSA143
 6100 format (' WARNING: Number of atoms/hetatms in new file differs',  MKSA144
     & ' from old file - ',/,                                           MKSA145
     & 10X, ' new number = ',I5,/,                                      MKSA146
     & 10X, ' old number = ',I5)                                        MKSA147
 6120 format (' Number of alternate mainchain atoms is ', i5)           MKSA148
 6130 format (' Number of alternate sidechain atoms is ', i5)           MKSA149
c                                                                       MKSA150
      astart = 1                                                        MKSA151
      istart = 1                                                        MKSA152
      nswap  = 0                                                        MKSA153
      outerr = space                                                    MKSA154
      nathet = 0                                                        MKSA155
      do 10 i = 1, sdchna                                               MKSA156
      do 9 j = 1, maxres                                                MKSA157
      nold(i,j) = 0                                                     MKSA158
    9 continue                                                          MKSA159
c     write out any extra data before residue 1                         MKSA160
   10 continue                                                          MKSA161
      if (nline .gt. 0) then                                            MKSA162
         do 50 ii = 1, nline                                            MKSA163
         if (extres(ii) .eq. 0) then                                    MKSA164
            write (outfi, 6005) extdat(ii)                              MKSA165
            if (extdat(ii)(1:keylen) .eq. atmkey .or.                   MKSA166
     &          extdat(ii)(1:keylen) .eq. htakey) nathet = nathet + 1   MKSA167
         else                                                           MKSA168
            if (extres(ii) .gt. 0) then                                 MKSA169
               istart = ii                                              MKSA170
               goto 51                                                  MKSA171
            end if                                                      MKSA172
         end if                                                         MKSA173
   50    continue                                                       MKSA174
      end if                                                            MKSA175
   51 continue                                                          MKSA176
      if (altlin .gt. 0) then                                           MKSA177
         do 60 ii = 1, altlin                                           MKSA178
         if (altres(ii) .eq. 0) then                                    MKSA179
            write (altfi, 6005) altdat(ii)                              MKSA180
            if (altdat(ii)(1:keylen) .eq. atmkey .or.                   MKSA181
     &          altdat(ii)(1:keylen) .eq. htakey) nathet = nathet + 1   MKSA182
         else                                                           MKSA183
            if (altres(ii) .gt. 0) then                                 MKSA184
               astart = ii                                              MKSA185
               goto 61                                                  MKSA186
            end if                                                      MKSA187
         end if                                                         MKSA188
   60    continue                                                       MKSA189
      end if                                                            MKSA190
   61 continue                                                          MKSA191
      do 5000 nxres = 1, seqlen                                         MKSA192
      if (seqcod(nxres) .le. abetsz) then                               MKSA193
         wkres = aacod3((seqcod(nxres)*3)-2:seqcod(nxres)*3)            MKSA194
      else                                                              MKSA195
         wkres = hetcod(((seqcod(nxres)-abetsz)*3)-2:                   MKSA196
     &                   (seqcod(nxres)-abetsz)*3)                      MKSA197
      end if                                                            MKSA198
      aacod = seqcod(nxres)                                             MKSA199
      nchi = 0                                                          MKSA200
      nchi = natsdc(aacod)                                              MKSA201
      do 100 nxang = nchi+1, nsdang                                     MKSA202
      scangs(nxang,nxres) = null                                        MKSA203
  100 continue                                                          MKSA204
      if (nchi .gt. 0) then                                             MKSA205
         athere(1) = atomin(nmain,nxres)                                MKSA206
         athere(2) = atomin(calph,nxres)                                MKSA207
         do 200 i = 1, ncoord                                           MKSA208
         relats(i,1) = mcaaat(i,nmain,nxres)                            MKSA209
         relats(i,2) = mcaaat(i,calph,nxres)                            MKSA210
  200    continue                                                       MKSA211
         extatm = 0                                                     MKSA212
         if (extprc(aacod) .gt. 1) extatm = 1                           MKSA213
         do 400 i = 1, nchi+1+extatm                                    MKSA214
         athere(2+i) = scatin(i,nxres)                                  MKSA215
         do 300 j = 1, ncoord                                           MKSA216
         relats(j,2+i) = scaaat(j,i,nxres)                              MKSA217
  300    continue                                                       MKSA218
  400    continue                                                       MKSA219
c        ILE CASE                                                       MKSA220
         swatm = 0                                                      MKSA221
         if (extprc(aacod) .eq. 5) then                                 MKSA222
            if (scatin(nchi,nxres) .and. scatin(nchi+2,nxres)) then     MKSA223
               distg1 = atdist(scaaat(1,nchi,nxres),                    MKSA224
     &                         scaaat(1,nchi+2,nxres))                  MKSA225
               if (distg1 .gt. ccdst) then                              MKSA226
CHECK v.2.1.3-->                                                        MKSA227
C                  write (logfi, 6010) distg1, ccdst                    MKSA228
                  write (*, 6010) distg1, ccdst                         MKSA229
CHECK v.2.1.3<--                                                        MKSA230
CHECK v.1.0-->                                                          MKSA231
C                  write (*,     6010) distg1, ccdst                    MKSA232
CHECK v.1.0<--                                                          MKSA233
                  if (scatin(nchi+1,nxres)) then                        MKSA234
                     distg2 = atdist(scaaat(1,nchi+1,nxres),            MKSA235
     &                               scaaat(1,nchi+2,nxres))            MKSA236
                     if (distg2 .lt. ccdst) then                        MKSA237
CHECK v.2.1.3-->                                                        MKSA238
C                        write (logfi, 6020) distg2, ccdst              MKSA239
                        write (logfi, 6020) distg2, ccdst               MKSA240
CHECK v.2.1.3<--                                                        MKSA241
CHECK v.1.0-->                                                          MKSA242
C                        write (*,     6020) distg2, ccdst              MKSA243
CHECK v.1.0<--                                                          MKSA244
                        swatm = 1                                       MKSA245
CHECK v.3.2-->                                                          MKSA246
C                        call swap (aacod, nxres, scaaat, scocc, scthrm,MKSA247
C     +                             Scatin, nold, totswp)               MKSA248
                        call swap (aacod, nxres, scaaat, scocc, scthrm, MKSA249
CHECK v.3.4-->                                                          MKSA250
C     +                             nold, totswp)                       MKSA251
     &                             nold, totswp,NMR)                    MKSA252
CHECK v.3.4<--                                                          MKSA253
CHECK v.3.2<--                                                          MKSA254
                        change(nxres) = 1                               MKSA255
                     end if                                             MKSA256
                  end if                                                MKSA257
               end if                                                   MKSA258
            end if                                                      MKSA259
CHECK v.2.0.1-->                                                        MKSA260
            dihang=dihed(1,scaaat(1,1,nxres),scaaat(1,2,nxres),         MKSA261
     &          scaaat(1,3,nxres),mcaaat(1,calph,nxres))                MKSA262
CHECK v.2.1.3-->                                                        MKSA263
C            if (dihang.lt.0) write(logfi,6030) nxres                   MKSA264
CHECK v.2.1.3<--                                                        MKSA265
            if (dihang.lt.0) write(*,6030) nxres                        MKSA266
CHECK v.2.0.1<--                                                        MKSA267
         end if                                                         MKSA268
         if (extprc(aacod) .eq. 1) then                                 MKSA269
            if (atomin(calph,nxres) .and. scatin(nchi+1,nxres) .and.    MKSA270
     &                                    scatin(nchi+2,nxres)) then    MKSA271
               distmn = atdist(mcaaat(1,calph,nxres),                   MKSA272
     &                         scaaat(1,nchi+1,nxres))                  MKSA273
               distal = atdist(mcaaat(1,calph,nxres),                   MKSA274
     &                         scaaat(1,nchi+2,nxres))                  MKSA275
               if (distal .lt. distmn) then                             MKSA276
                  swatm = 1                                             MKSA277
CHECK v.3.2-->                                                          MKSA278
C                  call swap (aacod, nxres, scaaat, scocc, scthrm,      MKSA279
C     +                       scatin, nold, totswp)                     MKSA280
                  call swap (aacod, nxres, scaaat, scocc, scthrm,       MKSA281
CHECK v.3.4-->                                                          MKSA282
C     +                       nold, totswp)                             MKSA283
     &                       nold, totswp,NMR)                          MKSA284
CHECK v.3.4<--                                                          MKSA285
CHECK v.3.2<--                                                          MKSA286
                  change(nxres) = 1                                     MKSA287
                  athere(nchi+3) = scatin(nchi+2,nxres)                 MKSA288
                  do 500 i = 1, ncoord                                  MKSA289
                  relats(i,nchi+3) = scaaat(i,nchi+2,nxres)             MKSA290
  500             continue                                              MKSA291
               end if                                                   MKSA292
            end if                                                      MKSA293
         end if                                                         MKSA294
         do 1000 nxchi = 1, nchi                                        MKSA295
         if (athere(nxchi)   .and. athere(nxchi+1) .and.                MKSA296
     &       athere(nxchi+2) .and. athere(nxchi+3)) then                MKSA297
            scangs(nxchi,nxres) = dihed(nmnang+nxchi,                   MKSA298
     &                                  relats(1,nxchi),                MKSA299
     &                                  relats(1,nxchi+1),              MKSA300
     &                                  relats(1,nxchi+2),              MKSA301
     &                                  relats(1,nxchi+3))              MKSA302
         else                                                           MKSA303
            scangs(nxchi,nxres) = null                                  MKSA304
         end if                                                         MKSA305
 1000    continue                                                       MKSA306
         if (extprc(aacod) .gt. 1 .and. xney(scangs(nchi,nxres),null))  MKSA307
     &       then                                                       MKSA308
            if (athere(nchi)   .and. athere(nchi+1) .and.               MKSA309
     &          athere(nchi+2) .and. athere(nchi+3+extatm)) then        MKSA310
               scangs(nchi+1,nxres) = dihed(nmnang+nchi+extatm,         MKSA311
     &                                      relats(1,nchi),             MKSA312
     &                                      relats(1,nchi+1),           MKSA313
     &                                      relats(1,nchi+2),           MKSA314
     &                                      relats(1,nchi+3+extatm))    MKSA315
            else                                                        MKSA316
               scangs(nchi+1,nxres) = null                              MKSA317
            end if                                                      MKSA318
            if (xney(scangs(nchi+1,nxres),null)) then                   MKSA319
               if (extprc(aacod) .eq. 2 .or.                            MKSA320
     &             extprc(aacod) .eq. 3 .or.                            MKSA321
     &             extprc(aacod) .eq. 5) then                           MKSA322
                  ang1 = scangs(nchi,nxres)                             MKSA323
                  ang2 = scangs(nchi+1,nxres)                           MKSA324
                  if (ang1 .lt. 0.0) ang1 = 360.0 + ang1                MKSA325
                  if (ang2 .lt. 0.0) ang2 = 360.0 + ang2                MKSA326
                  chipl = nchi                                          MKSA327
                  if (abs(ang1-ang2) .gt. 180.0) then                   MKSA328
                     if (ang2 .gt. ang1 .and. extprc(aacod) .eq. 2)     MKSA329
     &                  chipl = nchi + 1                                MKSA330
                     if (ang1 .gt. ang2 .and. extprc(aacod) .eq. 3)     MKSA331
     &                  chipl = nchi + 1                                MKSA332
                  else                                                  MKSA333
                     if (ang2 .lt. ang1 .and. extprc(aacod) .eq. 2)     MKSA334
     &                  chipl = nchi + 1                                MKSA335
                     if (ang1 .lt. ang2 .and. extprc(aacod) .eq. 3)     MKSA336
     &                  chipl = nchi + 1                                MKSA337
                  end if                                                MKSA338
                  if (chipl .ne. nchi) then                             MKSA339
CHECK v.2.1.3-->                                                        MKSA340
C                     if (extprc(aacod) .eq. 5) write (logfi, 6030)     MKSA341
                     if (extprc(aacod) .eq. 5) write (*, 6030)          MKSA342
CHECK v.2.1.3<--                                                        MKSA343
CHECK v.1.0-->                                                          MKSA344
C                     if (extprc(aacod) .eq. 5) write (*,     6030)     MKSA345
CHECK v.1.0<--                                                          MKSA346
                     swatm = 1                                          MKSA347
CHECK v.3.2-->                                                          MKSA348
C                     call swap (aacod, nxres, scaaat, scocc, scthrm,   MKSA349
C     +                          scatin, nold, totswp)                  MKSA350
                     call swap (aacod, nxres, scaaat, scocc, scthrm,    MKSA351
CHECK v.3.4-->                                                          MKSA352
C     +                          nold, totswp)                          MKSA353
     &                          nold, totswp,NMR)                       MKSA354
CHECK v.3.4<--                                                          MKSA355
CHECK v.3.2<--                                                          MKSA356
                     change(nxres) = 1                                  MKSA357
                     scangs(nchi,nxres) = scangs(nchi+1,nxres)          MKSA358
                  end if                                                MKSA359
               end if                                                   MKSA360
               if (extprc(aacod) .eq. 4) then                           MKSA361
                  if (abs(scangs(nchi+1,nxres)) .lt.                    MKSA362
     &                abs(scangs(nchi,nxres))) then                     MKSA363
                     swatm = 1                                          MKSA364
CHECK v.3.2-->                                                          MKSA365
C                     call swap (aacod, nxres, scaaat, scocc, scthrm,   MKSA366
C     +                          scatin, nold, totswp)                  MKSA367
                     call swap (aacod, nxres, scaaat, scocc, scthrm,    MKSA368
CHECK v.3.4-->                                                          MKSA369
C     +                          nold, totswp)                          MKSA370
     &                          nold, totswp,NMR)                       MKSA371
CHECK v.3.4<--                                                          MKSA372
CHECK v.3.2<--                                                          MKSA373
                     change(nxres) = 1                                  MKSA374
                     scangs(nchi,nxres) = scangs(nchi+1,nxres)          MKSA375
                  end if                                                MKSA376
               end if                                                   MKSA377
               scangs(nchi+1,nxres) = null                              MKSA378
            end if                                                      MKSA379
         end if                                                         MKSA380
         if (swatm .eq. 1) then                                         MKSA381
CHECK v.2.1.3-->                                                        MKSA382
C            if (nswap .eq. 0) write (logfi, 6040)                      MKSA383
CHECK v.3.4-->                                                          MKSA384
C            if (nswap .eq. 0) write (*, 6040)                          MKSA385
            if (nswap .eq. 0) THEN                                      MKSA386
                IF (.NOT.NMR) THEN                                      MKSA387
                    write (*, 6040)                                     MKSA388
                ELSE                                                    MKSA389
                    write (*, 6042)                                     MKSA390
                ENDIF                                                   MKSA391
            endif                                                       MKSA392
CHECK v.3.4<--                                                          MKSA393
CHECK v.2.1.3<--                                                        MKSA394
CHECK v.1.0-->                                                          MKSA395
C            if (nswap .eq. 0) write (*,     6040)                      MKSA396
CHECK v.1.0<--                                                          MKSA397
CHECK v.3.2-->                                                          MKSA398
C            wkswap = mod(nswap,8) + 1                                  MKSA399
C            write (outerr(((wkswap-1)*10)+1:wkswap*10), 6050) wkres,   MKSA400
C     +                                                        nxres    MKSA401
C            if (wkswap .eq. 8) then                                    MKSA402
            wkswap = mod(nswap,7) + 1                                   MKSA403
            write (outerr(((wkswap-1)*11)+1:wkswap*11), 6050) wkres,    MKSA404
     &          seqbcd(nxres)                                           MKSA405
            if (wkswap .eq. 7) then                                     MKSA406
CHECK v.3.2<--                                                          MKSA407
CHECK v.2.1.3-->                                                        MKSA408
C               write (logfi, 6060) outerr                              MKSA409
               write (*, 6060) outerr                                   MKSA410
CHECK v.2.1.3<--                                                        MKSA411
CHECK v.1.0-->                                                          MKSA412
C               write (*,     6060) outerr                              MKSA413
CHECK v.1.0<--                                                          MKSA414
               outerr = space                                           MKSA415
            end if                                                      MKSA416
            nswap = nswap + 1                                           MKSA417
         end if                                                         MKSA418
      end if                                                            MKSA419
      if (lab(nxres) .eq. 'A') then                                     MKSA420
         label = 'ATOM  '                                               MKSA421
      else                                                              MKSA422
         label = 'HETATM'                                               MKSA423
      end if                                                            MKSA424
c     write data out to new brookhaven file                             MKSA425
      do 3000 j = 1, 5                                                  MKSA426
      if (aacod .gt. 44 .and. aacod .le. 50) then                       MKSA427
         mcatom = batnam((4*j)-3:4*j)                                   MKSA428
      else if (aacod .eq. 42) then                                      MKSA429
         mcatom = tphnam((4*j)-3:4*j)                                   MKSA430
      else if (aacod .eq. 41) then                                      MKSA431
         mcatom = olenam((4*j)-3:4*j)                                   MKSA432
      else if (aacod .eq. 39) then                                      MKSA433
         mcatom = pphnam((4*j)-3:4*j)                                   MKSA434
      else if (aacod .eq. 36) then                                      MKSA435
         mcatom = pyrnam((4*j)-3:4*j)                                   MKSA436
      else                                                              MKSA437
         mcatom = atname((4*j)-3:4*j)                                   MKSA438
      end if                                                            MKSA439
      if ((mcangs(1,nxres) .lt. 23.0 .or.                               MKSA440
     &     mcangs(1,nxres) .gt. 45.0) .and.                             MKSA441
     &      xney(mcangs(1,nxres),null)) then                            MKSA442
         ld = '*'                                                       MKSA443
      else                                                              MKSA444
         ld = ' '                                                       MKSA445
      end if                                                            MKSA446
      if (atomin(j,nxres)) then                                         MKSA447
CHECK v.2.1-->                                                          MKSA448
         altlab = label                                                 MKSA449
CHECK v.3.2-->                                                          MKSA450
C         if (mcocc(j,nxres).eq.0.0) then                               MKSA451
         if (.NOT.NMR .AND. mcocc(j,nxres).eq.0.0) then                 MKSA452
CHECK v.3.2<--                                                          MKSA453
             altlab = label(1:2) // 'ZERO'                              MKSA454
CHECK v.2.1.3-->                                                        MKSA455
             IF (label(1:2).EQ.'AT') THEN                               MKSA456
                 NATZER = NATZER + 1                                    MKSA457
             ELSE                                                       MKSA458
                 NHEZER = NHEZER + 1                                    MKSA459
             ENDIF                                                      MKSA460
CHECK v.2.1.3<--                                                        MKSA461
         endif                                                          MKSA462
C         write (outfi, 6008) label, mcser(j,nxres), mcatom,            MKSA463
         write (outfi, 6008) altlab, mcser(j,nxres), mcatom,            MKSA464
CHECK v.2.1<--                                                          MKSA465
     &                       mcalt(j,nxres), wkres, seqbcd(nxres),      MKSA466
     &                      (mcaaat(i,j,nxres), i=1,ncoord),            MKSA467
     &                       mcocc(j,nxres), mcthrm(j,nxres),           MKSA468
     &                       chstop(nxres), chstrt(nxres), ld           MKSA469
         nathet = nathet + 1                                            MKSA470
      end if                                                            MKSA471
      if (naltmc .gt. 0 .and. mcalt(j,nxres) .ne. ' ') then             MKSA472
         do 2900 i = 1, naltmc                                          MKSA473
         if (altmci(2,i) .eq. j .and. altmci(1,i) .eq. nxres) then      MKSA474
            write (altfi, 6000) label, altmci(3,i), mcatom, altmcc(i),  MKSA475
     &                          wkres, seqbcd(nxres),                   MKSA476
     &                         (altmca(k,i), k=1,3), altmcr(1,i),       MKSA477
     &                          altmcr(2,i)                             MKSA478
                                                                        MKSA479
CHECK v.2.1-->                                                          MKSA480
               altlab = label(1:2) // 'ALT'                             MKSA481
               write (outfi, 6000) altlab, altsci(3,j), scatom,         MKSA482
     &                             altscc(j), wkres, seqbcd(nxres),     MKSA483
     &                            (altsca(k,j), k=1,3), altscr(1,j),    MKSA484
     &                             altscr(2,j)                          MKSA485
CHECK v.2.1<--                                                          MKSA486
                                                                        MKSA487
CHECK v.2.1.3-->                                                        MKSA488
             IF (label(1:2).EQ.'AT') THEN                               MKSA489
                 NATALT = NATALT + 1                                    MKSA490
             ELSE                                                       MKSA491
                 NHEALT = NHEALT + 1                                    MKSA492
             ENDIF                                                      MKSA493
CHECK v.2.1.3<--                                                        MKSA494
            nathet = nathet + 1                                         MKSA495
         end if                                                         MKSA496
 2900    continue                                                       MKSA497
      end if                                                            MKSA498
c     side chain atoms                                                  MKSA499
 3000 continue                                                          MKSA500
      scdata = scname(aacod)                                            MKSA501
      do 4000 i = 1, nscats(aacod)                                      MKSA502
      if (scatin(i,nxres)) then                                         MKSA503
         if (i .eq. 1) then                                             MKSA504
            scatom = cbet                                               MKSA505
         else                                                           MKSA506
            scatom = scdata((4*(i-2))+1:4*(i-1))                        MKSA507
c          determine the original atom name                             MKSA508
         end if                                                         MKSA509
         if (nold(i,nxres) .ne. 0) then                                 MKSA510
            n = nold(i,nxres)                                           MKSA511
            scold = scdata((4*(n-2))+1:4*(n-1))                         MKSA512
         else                                                           MKSA513
            scold = '    '                                              MKSA514
         end if                                                         MKSA515
CHECK v.2.1-->                                                          MKSA516
         altlab = label                                                 MKSA517
CHECK v.3.2-->                                                          MKSA518
C         if (scocc(i,nxres).eq.0.0) then                               MKSA519
         if (.NOT.NMR .AND. scocc(i,nxres).eq.0.0) then                 MKSA520
CHECK v.3.2<--                                                          MKSA521
             altlab = label(1:2) // 'ZERO'                              MKSA522
CHECK v.2.1.3-->                                                        MKSA523
             IF (label(1:2).EQ.'AT') THEN                               MKSA524
                 NATZER = NATZER + 1                                    MKSA525
             ELSE                                                       MKSA526
                 NHEZER = NHEZER + 1                                    MKSA527
             ENDIF                                                      MKSA528
CHECK v.2.1.3<--                                                        MKSA529
         endif                                                          MKSA530
C         write (outfi, 6003) label, scser(i,nxres), scatom,            MKSA531
         write (outfi, 6003) altlab, scser(i,nxres), scatom,            MKSA532
CHECK v.2.1<--                                                          MKSA533
     &                       scalt(i,nxres), wkres, seqbcd(nxres),      MKSA534
     &                      (scaaat(j,i,nxres), j=1,ncoord),            MKSA535
     &                       scocc(i,nxres), scthrm(i,nxres),           MKSA536
     &                       chstop(nxres), chstrt(nxres), scold, ld    MKSA537
         nathet = nathet + 1                                            MKSA538
         if (naltsc .gt. 0 .and. scalt(i,nxres) .ne. ' ') then          MKSA539
            do 3960 j = 1, naltsc                                       MKSA540
            if (altsci(1,j) .eq. nxres .and.                            MKSA541
     &          altsci(2,j) .eq. i) then                                MKSA542
               write (altfi, 6000) label, altsci(3,j), scatom,          MKSA543
     &                             altscc(j), wkres, seqbcd(nxres),     MKSA544
     &                            (altsca(k,j), k=1,3), altscr(1,j),    MKSA545
     &                             altscr(2,j)                          MKSA546
                                                                        MKSA547
CHECK v.2.1-->                                                          MKSA548
               altlab = label(1:2) // 'ALT'                             MKSA549
               write (outfi, 6000) altlab, altsci(3,j), scatom,         MKSA550
     &                             altscc(j), wkres, seqbcd(nxres),     MKSA551
     &                            (altsca(k,j), k=1,3), altscr(1,j),    MKSA552
     &                             altscr(2,j)                          MKSA553
CHECK v.2.1<--                                                          MKSA554
                                                                        MKSA555
CHECK v.2.1.3-->                                                        MKSA556
               IF (label(1:2).EQ.'AT') THEN                             MKSA557
                   NATALT = NATALT + 1                                  MKSA558
               ELSE                                                     MKSA559
                   NHEALT = NHEALT + 1                                  MKSA560
               ENDIF                                                    MKSA561
CHECK v.2.1.3<--                                                        MKSA562
               nathet = nathet + 1                                      MKSA563
            end if                                                      MKSA564
 3960       continue                                                    MKSA565
         end if                                                         MKSA566
      end if                                                            MKSA567
c     now write out any extra data associated with this residue         MKSA568
 4000 continue                                                          MKSA569
      if (nline .gt. 0) then                                            MKSA570
         do 4500 ii = istart, nline                                     MKSA571
         if (extres(ii) .eq. nxres) then                                MKSA572
            write (outfi, 6005) extdat(ii)                              MKSA573
            if (extdat(ii)(1:keylen) .eq. atmkey .or.                   MKSA574
     &          extdat(ii)(1:keylen) .eq. htakey)                       MKSA575
     &         nathet = nathet + 1                                      MKSA576
         else                                                           MKSA577
            if (extres(ii) .gt. nxres) then                             MKSA578
               istart = ii                                              MKSA579
               goto 4510                                                MKSA580
            end if                                                      MKSA581
         end if                                                         MKSA582
 4500    continue                                                       MKSA583
      end if                                                            MKSA584
 4510 continue                                                          MKSA585
      if (altlin .gt. 0) then                                           MKSA586
         do 4600 ii = astart, altlin                                    MKSA587
         if (altres(ii) .eq. nxres) then                                MKSA588
            write (altfi, 6005) altdat(ii)                              MKSA589
            if (altdat(ii)(1:keylen) .eq. atmkey .or.                   MKSA590
     &          altdat(ii)(1:keylen) .eq. htakey)                       MKSA591
     &         nathet = nathet + 1                                      MKSA592
         else                                                           MKSA593
            if (altres(ii) .gt. nxres) then                             MKSA594
               astart = ii                                              MKSA595
               goto 4610                                                MKSA596
            end if                                                      MKSA597
         end if                                                         MKSA598
 4600    continue                                                       MKSA599
      end if                                                            MKSA600
 4610 continue                                                          MKSA601
 5000 continue                                                          MKSA602
      write (outfi, 6006) nathet                                        MKSA603
      if (nathet .ne. nrec) then                                        MKSA604
CHECK v.2.1.3-->                                                        MKSA605
C         write (logfi, 6100) nathet, nrec                              MKSA606
         write (*, 6100) nathet, nrec                                   MKSA607
CHECK v.2.1.3<--                                                        MKSA608
CHECK v.1.0-->                                                          MKSA609
C         write (*,     6100) nathet, nrec                              MKSA610
CHECK v.1.0<--                                                          MKSA611
      end if                                                            MKSA612
CHECK v.3.2-->                                                          MKSA613
C      if (alt) write (altfi, 6006)                                     MKSA614
CHECK v.3.2<--                                                          MKSA615
      write (outfi, 6007)                                               MKSA616
CHECK v.3.2-->                                                          MKSA617
C      if (mod(nswap,8) .ne. 0) then                                    MKSA618
      if (mod(nswap,7) .ne. 0) then                                     MKSA619
CHECK v.3.2<--                                                          MKSA620
CHECK v.2.1.3-->                                                        MKSA621
C         write (logfi, 6070) outerr                                    MKSA622
CHECK v.3.2-->                                                          MKSA623
C         write (*, 6070) outerr                                        MKSA624
         write (*, 6060) outerr                                         MKSA625
CHECK v.3.2<--                                                          MKSA626
CHECK v.2.1.3<--                                                        MKSA627
CHECK v.1.0-->                                                          MKSA628
C         write (*,     6070) outerr                                    MKSA629
CHECK v.1.0<--                                                          MKSA630
      end if                                                            MKSA631
      if (naltmc .gt. 0) then                                           MKSA632
CHECK v.2.1.3-->                                                        MKSA633
C         write (logfi, 6120) naltmc                                    MKSA634
         write (*, 6120) naltmc                                         MKSA635
CHECK v.2.1.3<--                                                        MKSA636
CHECK v.1.0-->                                                          MKSA637
C         write (*,     6120) naltmc                                    MKSA638
CHECK v.1.0<--                                                          MKSA639
      end if                                                            MKSA640
      if (naltsc .gt. 0) then                                           MKSA641
CHECK v.2.1.3-->                                                        MKSA642
C         write (logfi, 6130) naltsc                                    MKSA643
         write (*, 6130) naltsc                                         MKSA644
CHECK v.2.1.3<--                                                        MKSA645
CHECK v.1.0-->                                                          MKSA646
C         write (*,     6130) naltsc                                    MKSA647
CHECK v.1.0<--                                                          MKSA648
      end if                                                            MKSA649
c     call msgprt                                                       MKSA650
      end                                                               MKSA651
