      SUBROUTINE DRWPLN(DGX,DGY,XWIDTH,YHIGHT,INCHN,RESNAM,SEQNO,RMS,   DRWP001
     &    COORD,ANAME,NATOM)                                            DRWP002
C-----------------------------------------------------------------------DRWP003
C                                                                       DRWP004
C  SUBROUTINE DRWPLN  -  Draw distorted planar groups                   DRWP005
C                                                                       DRWP006
C----------------------------------------------------------------------+DRWP007
                                                                        DRWP008
      INCLUDE 'bplot.inc'                                               DRWP009
                                                                        DRWP010
CHECK v.3.2-->                                                          DRWP011
      INTEGER       NATOM                                               DRWP012
CHECK v.3.2<--                                                          DRWP013
      CHARACTER*1   INCHN                                               DRWP014
      CHARACTER*3   ANAME(NATOM),RESNAM                                 DRWP015
      CHARACTER*5   NUMBER, SEQNO                                       DRWP016
      CHARACTER*10  RESID                                               DRWP017
CHECK v.3.2-->                                                          DRWP018
C      INTEGER       ICOORD, IATOM, I1, I2, JATOM, NATOM                DRWP019
      INTEGER       ICOORD, IATOM, I1, I2, JATOM                        DRWP020
CHECK v.3.2<--                                                          DRWP021
      REAL          BLUE, CALC, COORD(3,NATOM), COV2, DGX, DGY, DIST2,  DRWP022
     &              DX, GREEN, RED, RMS, SCALE, XOFF, XSTART, XWIDTH,   DRWP023
     &              X1, X2, YHIGHT, YOFF, YSTART, Y1, Y2                DRWP024
                                                                        DRWP025
      PARAMETER    (COV2 = 1.7 * 1.7)                                   DRWP026
                                                                        DRWP027
C---- Initialise variables                                              DRWP028
      CALL PSLWID(0.1)                                                  DRWP029
      SCALE = XWIDTH / DGLENP                                           DRWP030
      XOFF = 0.33 * XWIDTH                                              DRWP031
      YOFF = 0.35 * YHIGHT                                              DRWP032
      XSTART = DGX + (XWIDTH / 2.0)                                     DRWP033
      YSTART = DGY + 0.65 * YHIGHT                                      DRWP034
                                                                        DRWP035
C---- Draw a box around the region                                      DRWP036
C      CALL PSHADE(1.0)                                                 DRWP037
C      CALL PSBBOX(DGX,DGY,DGX,DGY+YHIGHT,DGX+XWIDTH,DGY+YHIGHT,        DRWP038
C     -    DGX+XWIDTH,DGY)                                              DRWP039
                                                                        DRWP040
C---- Print the residue ID                                              DRWP041
      RESID = INCHN // ' ' // RESNAM // SEQNO                           DRWP042
      IF (INCHN.EQ.' ') THEN                                            DRWP043
          I1 = 3                                                        DRWP044
      ELSE                                                              DRWP045
          I1 = 1                                                        DRWP046
      ENDIF                                                             DRWP047
      IF (RESID(9:10).EQ.'  ') THEN                                     DRWP048
          I2 = 8                                                        DRWP049
      ELSE IF (RESID(10:10).EQ.' ') THEN                                DRWP050
          I2 = 9                                                        DRWP051
      ELSE                                                              DRWP052
          I2 = 10                                                       DRWP053
      ENDIF                                                             DRWP054
      CALL PSCTXT(DGX + XWIDTH / 2.0,DGY + 0.1 * YHIGHT,10.0,           DRWP055
     &    RESID(I1:I2))                                                 DRWP056
                                                                        DRWP057
C---- Plot the RMS distance of the atoms from the best-fit plane        DRWP058
      WRITE(NUMBER,110) RMS                                             DRWP059
 110  FORMAT(F5.3)                                                      DRWP060
      IF (INCOLR(PLTYPE)) THEN                                          DRWP061
          RED = RGB(1,COLPLT(PLTYPE,3))                                 DRWP062
          GREEN = RGB(2,COLPLT(PLTYPE,3))                               DRWP063
          BLUE = RGB(3,COLPLT(PLTYPE,3))                                DRWP064
          CALL PSCOLB(RED,GREEN,BLUE)                                   DRWP065
      ENDIF                                                             DRWP066
      CALL PSCTXT(XSTART,DGY + 0.2 * YHIGHT,8.0,NUMBER)                 DRWP067
                                                                        DRWP068
C---- If necessary, flip entire residue upside down                     DRWP069
      IF (COORD(2,1).GT.0.0) THEN                                       DRWP070
          DO 300, IATOM = 1, NATOM                                      DRWP071
              COORD(2,IATOM) = - COORD(2,IATOM)                         DRWP072
 300      CONTINUE                                                      DRWP073
      ENDIF                                                             DRWP074
                                                                        DRWP075
C---- Loop through all atom-pairs in the planar group                   DRWP076
      DO 800, IATOM = 1, NATOM                                          DRWP077
          DO 600, JATOM = IATOM + 1, NATOM                              DRWP078
                                                                        DRWP079
C----         Calculate the distance between this atom-pair             DRWP080
              DIST2 = 0.0                                               DRWP081
              DO 500, ICOORD = 1, 3                                     DRWP082
                  CALC = COORD(ICOORD,IATOM) - COORD(ICOORD,JATOM)      DRWP083
                  DIST2 = DIST2 + CALC * CALC                           DRWP084
 500          CONTINUE                                                  DRWP085
                                                                        DRWP086
C----         If this is less than the covalent bonding distance, join  DRWP087
C             the two atoms by a bond                                   DRWP088
              IF (DIST2.LE.COV2) THEN                                   DRWP089
                                                                        DRWP090
C----             Plot face-on projection                               DRWP091
                  X1 = XSTART + COORD(3,IATOM) * SCALE                  DRWP092
                  X2 = XSTART + COORD(3,JATOM) * SCALE                  DRWP093
                  Y1 = YSTART + COORD(2,IATOM) * SCALE                  DRWP094
                  Y2 = YSTART + COORD(2,JATOM) * SCALE                  DRWP095
                  CALL PSLWID(1.0)                                      DRWP096
                  CALL PSLINE(X1,Y1,X2,Y2)                              DRWP097
                                                                        DRWP098
C----             Plot projection shown on right-hand side              DRWP099
                  X1 = XSTART + XOFF + COORD(1,IATOM) * SCALE           DRWP100
                  X2 = XSTART + XOFF + COORD(1,JATOM) * SCALE           DRWP101
                  Y1 = YSTART + COORD(2,IATOM) * SCALE                  DRWP102
                  Y2 = YSTART + COORD(2,JATOM) * SCALE                  DRWP103
                  CALL PSLWID(0.3)                                      DRWP104
                  CALL PSLINE(X1,Y1,X2,Y2)                              DRWP105
                                                                        DRWP106
C----             Plot projection shown at bottom                       DRWP107
                  X1 = XSTART + COORD(3,IATOM) * SCALE                  DRWP108
                  X2 = XSTART + COORD(3,JATOM) * SCALE                  DRWP109
                  Y1 = YSTART - YOFF + COORD(1,IATOM) * SCALE           DRWP110
                  Y2 = YSTART - YOFF + COORD(1,JATOM) * SCALE           DRWP111
                  CALL PSLWID(0.3)                                      DRWP112
                  CALL PSLINE(X1,Y1,X2,Y2)                              DRWP113
              ENDIF                                                     DRWP114
 600      CONTINUE                                                      DRWP115
 800  CONTINUE                                                          DRWP116
                                                                        DRWP117
C---- Label the first atom                                              DRWP118
      DX = 0.03 * XWIDTH                                                DRWP119
      X1 = XSTART + COORD(3,1) * SCALE + DX                             DRWP120
      Y1 = YSTART + COORD(2,1) * SCALE                                  DRWP121
      CALL PSTEXT(X1,Y1,6.0,ANAME(1))                                   DRWP122
      IF (INCOLR(PLTYPE)) CALL PSCOLB(0.0,0.0,0.0)                      DRWP123
                                                                        DRWP124
      RETURN                                                            DRWP125
      END                                                               DRWP126
