      subroutine Mksang(mcaaat, atomin, scaaat, scangs, scatin,         MKSA001
     &                  seqcod, aacod3, hetcod, seqlen)                 MKSA002
c                                                                       MKSA003
c     calculate chi angles from atom data. check for that alternates    MKSA004
c     are closest to c alpha                                            MKSA005
c                                                                       MKSA006
      include 'sstruc.par'                                              MKSA007
CHECK v.3.2-->                                                          MKSA008
      save natsdc, extprc                                               MKSA009
CHECK v.3.2<--                                                          MKSA010
      real      mcaaat(ncoord,mnchna,maxres),                           MKSA011
     &          scaaat(ncoord,sdchna,maxres),                           MKSA012
     &          scangs(nsdang,maxres)                                   MKSA013
      character aacod3*(abetsz*3), hetcod*63                            MKSA014
      logical   atomin(mnchna,maxres), scatin(sdchna,maxres)            MKSA015
      integer   seqcod(maxres), seqlen                                  MKSA016
                                                                        MKSA017
      integer   natsdc(sideaa), nxang, i, j, nxchi, nxres, nchi, aacod, MKSA018
     &          extprc(sideaa), swatm, extatm, chipl, nswap, wkswap     MKSA019
      logical   athere(maxchi+3), xney                                  MKSA020
      real      relats(ncoord,maxchi+3), distmn, distal, ang1, ang2     MKSA021
      character outerr*80, wkres*3                                      MKSA022
      real      atdist, dihed                                           MKSA023
      data natsdc / 0, 0, 1, 2, 3, 2, 0, 2, 2, 0, 4, 2, 3,              MKSA024
     &              2, 0, 1, 3, 4, 1, 1, 0, 1, 2, 0, 2, 0, 0, 3, 4,     MKSA025
     &              0, 2, 1, 1, 1, 2, 0, 4, 0, 2, 0, 2, 2, 1, 3, 1,     MKSA026
     &              2, 2, 3, 0, 2/                                      MKSA027
      data extprc / 0, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 2, 0,              MKSA028
     &              0, 0, 0, 0, 0, 0, 3, 0, 2, 0, 0, 4, 0, 0, 0, 0,     MKSA029
     &              0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 2, 4, 0, 0, 2,     MKSA030
     &                    0, 4, 0, 0, 4/                                MKSA031
CHECK v.3.2-->                                                          MKSA032
C      save natsdc, extprc                                              MKSA033
CHECK v.3.2<--                                                          MKSA034
                                                                        MKSA035
      nswap = 0                                                         MKSA036
      outerr = space                                                    MKSA037
      do 5000 nxres = 1, seqlen                                         MKSA038
      aacod = seqcod(nxres)                                             MKSA039
      nchi = 0                                                          MKSA040
      nchi = natsdc(aacod)                                              MKSA041
      do 100, nxang = nchi + 1, nsdang                                  MKSA042
      scangs(nxang,nxres) = null                                        MKSA043
  100 continue                                                          MKSA044
      if (nchi .gt. 0) then                                             MKSA045
         athere(1) = atomin(nmain,nxres)                                MKSA046
         athere(2) = atomin(calph,nxres)                                MKSA047
         do 200, i = 1, ncoord                                          MKSA048
         relats(i,1) = mcaaat(i,nmain,nxres)                            MKSA049
         relats(i,2) = mcaaat(i,calph,nxres)                            MKSA050
  200    continue                                                       MKSA051
         extatm = 0                                                     MKSA052
         if (extprc(aacod) .gt. 1) extatm = 1                           MKSA053
         do 400 i = 1, nchi + 1 + extatm                                MKSA054
         athere(2+i) = scatin(i,nxres)                                  MKSA055
         do 300, j = 1, ncoord                                          MKSA056
         relats(j,2+i) = scaaat(j,i,nxres)                              MKSA057
  300    continue                                                       MKSA058
  400    continue                                                       MKSA059
         swatm = 0                                                      MKSA060
         if (extprc(aacod) .eq. 1) then                                 MKSA061
            if (atomin(calph,nxres) .and. scatin(nchi+1,nxres)          MKSA062
     &         .and. scatin(nchi+2,nxres)) then                         MKSA063
               distmn = atdist(mcaaat(1,calph,nxres),                   MKSA064
     &                  scaaat(1,nchi+1,nxres))                         MKSA065
               distal = atdist(mcaaat(1,calph,nxres),                   MKSA066
     &                  scaaat(1,nchi+2,nxres))                         MKSA067
               if (distal .lt. distmn) then                             MKSA068
                  swatm = 1                                             MKSA069
                  athere(nchi+3) = scatin(nchi+2,nxres)                 MKSA070
                  do 500, i = 1, ncoord                                 MKSA071
                  relats(i,nchi+3) = scaaat(i,nchi+2,nxres)             MKSA072
  500             continue                                              MKSA073
               end if                                                   MKSA074
            end if                                                      MKSA075
         end if                                                         MKSA076
         do 1000, nxchi = 1, nchi                                       MKSA077
         If (athere(nxchi) .and. athere(nxchi+1) .and.                  MKSA078
     &      athere(nxchi+2) .and. athere(nxchi+3)) then                 MKSA079
            scangs(nxchi,nxres) = dihed(nmnang+nxchi,                   MKSA080
     &            relats(1,nxchi), relats(1,nxchi+1),                   MKSA081
     &            relats(1,nxchi+2), relats(1,nxchi+3))                 MKSA082
         else                                                           MKSA083
            scangs(nxchi,nxres) = null                                  MKSA084
         end if                                                         MKSA085
 1000    continue                                                       MKSA086
         if (extprc(aacod) .gt. 1 .and.                                 MKSA087
     &      xney(scangs(nchi,nxres), null)) then                        MKSA088
            If (athere(nchi) .and. athere(nchi+1) .and.                 MKSA089
     &         athere(nchi+2) .and. athere(nchi+3+extatm)) then         MKSA090
               scangs(nchi+1,nxres) = dihed(nmnang+nchi+extatm,         MKSA091
     &            relats(1,nchi), relats(1,nchi+1),                     MKSA092
     &            relats(1,nchi+2), relats(1,nchi+3+extatm))            MKSA093
            else                                                        MKSA094
               scangs(nchi+1,nxres) = null                              MKSA095
            end if                                                      MKSA096
            if (xney(scangs(nchi+1,nxres), null)) then                  MKSA097
               If (extprc(aacod) .eq. 2 .or. extprc(aacod) .eq. 3) then MKSA098
                  ang1 = scangs(nchi,nxres)                             MKSA099
                  ang2 = scangs(nchi+1,nxres)                           MKSA100
                  if (ang1 .lt. 0.0) ang1 = 360.0 + ang1                MKSA101
                  if (ang2 .lt. 0.0) ang2 = 360.0 + ang2                MKSA102
                  chipl = nchi                                          MKSA103
                  if (abs(ang1 - ang2) .gt. 180.0) then                 MKSA104
                     if (ang2 .gt. ang1 .and. extprc(aacod) .eq. 2)     MKSA105
     &                                                chipl = nchi + 1  MKSA106
                     if (ang1 .gt. ang2 .and. extprc(aacod) .eq. 3)     MKSA107
     &                                                chipl = nchi + 1  MKSA108
                  else                                                  MKSA109
                     if (ang2 .lt. ang1 .and. extprc(aacod) .eq. 2)     MKSA110
     &                                                chipl = nchi + 1  MKSA111
                     if (ang1 .lt. ang2 .and. extprc(aacod) .eq. 3)     MKSA112
     &                                                chipl = nchi + 1  MKSA113
                  end if                                                MKSA114
                  if (chipl .ne. nchi) then                             MKSA115
                     swatm = 1                                          MKSA116
                     scangs(nchi,nxres) = scangs(nchi+1,nxres)          MKSA117
                  end if                                                MKSA118
               end if                                                   MKSA119
               if (extprc(aacod) .eq. 4) then                           MKSA120
                  if (abs(scangs(nchi+1,nxres)) .lt.                    MKSA121
     &                         abs(scangs(nchi,nxres))) then            MKSA122
                     swatm = 1                                          MKSA123
                     scangs(nchi,nxres) = scangs(nchi+1,nxres)          MKSA124
                  end if                                                MKSA125
               end if                                                   MKSA126
               scangs(nchi+1,nxres) = null                              MKSA127
            end if                                                      MKSA128
         end if                                                         MKSA129
         if (swatm .eq. 1) then                                         MKSA130
            if (nswap .eq. 0)                                           MKSA131
     &         write(errfi,'(A)') 'side chain atoms swapped for '       MKSA132
            wkswap = mod(nswap,8) + 1                                   MKSA133
            if (seqcod(nxres) .le. abetsz) then                         MKSA134
               wkres = aacod3(seqcod(nxres)*3-2:seqcod(nxres)*3)        MKSA135
            else                                                        MKSA136
               wkres = hetcod((seqcod(nxres)-abetsz)*3-2:               MKSA137
     &                       (seqcod(nxres)-abetsz)*3)                  MKSA138
            end if                                                      MKSA139
CHECK v.2.1.4-->                                                        MKSA140
C            write(outerr((wkswap-1)*10+1:wkswap*10),'(a3,x,i4,2x)')    MKSA141
            write(outerr((wkswap-1)*10+1:wkswap*10),'(a3,1x,i4,2x)')    MKSA142
CHECK v.2.1.4<--                                                        MKSA143
     &        wkres, nxres                                              MKSA144
            if (wkswap .eq. 8) then                                     MKSA145
               write(errfi,'(A)') outerr                                MKSA146
               outerr = space                                           MKSA147
            end if                                                      MKSA148
            nswap = nswap + 1                                           MKSA149
         end if                                                         MKSA150
      end if                                                            MKSA151
 5000 continue                                                          MKSA152
      if (mod(nswap,8) .ne. 0) write(errfi,'(A)') outerr                MKSA153
      END                                                               MKSA154
