      SUBROUTINE DGPLOT(BTYPE,IPAGE,NDONEA,NDONEL,NDONEP)               DGPL001
C-----------------------------------------------------------------------DGPL002
C                                                                       DGPL003
C  SUBROUTINE DGPLOT  -  Plot this set of distorted geometries          DGPL004
C                                                                       DGPL005
C----------------------------------------------------------------------+DGPL006
                                                                        DGPL007
      SAVE                                                              DGPL008
                                                                        DGPL009
      INCLUDE 'bplot.inc'                                               DGPL010
                                                                        DGPL011
      INTEGER       NAMINO                                              DGPL012
      PARAMETER    (NAMINO = 20)                                        DGPL013
                                                                        DGPL014
CHECK v.3.2-->                                                          DGPL015
C      CHARACTER*1   BTYPE, INCHN                                       DGPL016
C      CHARACTER*3   ANAME(NPLATM)                                      DGPL017
C      CHARACTER*3   LNAME(NAMINO), RESNAM, RNAME(NAMINO)               DGPL018
C      CHARACTER*5   SEQNO                                              DGPL019
      CHARACTER*1   BTYPE, INCHN, OTHCHN                                DGPL020
      CHARACTER*3   ANAME(NPLATM), LNAME(NAMINO), OTHNAM, RESNAM,       DGPL021
     &              RNAME(NAMINO)                                       DGPL022
      CHARACTER*5   OTHSEQ, SEQNO                                       DGPL023
CHECK v.3.2<--                                                          DGPL024
      CHARACTER*8   CONTD                                               DGPL025
CHECK v.3.1-->                                                          DGPL026
      CHARACTER*30  PLDESC                                              DGPL027
CHECK v.3.1<--                                                          DGPL028
      CHARACTER*120 IREC                                                DGPL029
      INTEGER       IACROS, IAMINO, IMAIN, IPAGE, LINE, NATOM, NDGACR,  DGPL030
     &              NDONE, NDONEA, NDONEL, NDONEP, NLEFT, RLEN          DGPL031
CHECK v.3.2-->                                                          DGPL032
C      LOGICAL       ENDFIL, FOUND, PRHEAD                              DGPL033
      LOGICAL       ENDFIL, FIRST, FOUND, LAST, PRHEAD                  DGPL034
CHECK v.3.2<--                                                          DGPL035
      REAL          COORD(3,NPLATM), DGLIM, DGX, DGY, DIFF, EVALUE,     DGPL036
     &              VALUE, XWIDTH, YHIGHT, YPOS, YTEST                  DGPL037
                                                                        DGPL038
      DATA LNAME  / 'Ala', 'Arg', 'Asn', 'Asp', 'Cys', 'Gln', 'Glu',    DGPL039
     &              'Gly', 'His', 'Ile', 'Leu', 'Lys', 'Met', 'Phe',    DGPL040
     &              'Pro', 'Ser', 'Thr', 'Trp', 'Tyr', 'Val' /          DGPL041
                                                                        DGPL042
      DATA RNAME  / 'ALA', 'ARG', 'ASN', 'ASP', 'CYS', 'GLN', 'GLU',    DGPL043
     &              'GLY', 'HIS', 'ILE', 'LEU', 'LYS', 'MET', 'PHE',    DGPL044
     &              'PRO', 'SER', 'THR', 'TRP', 'TYR', 'VAL' /          DGPL045
                                                                        DGPL046
CHECK v.3.1-->                                                          DGPL047
      DATA PLDESC / 'Distorted geometry' /                              DGPL048
CHECK v.3.1<--                                                          DGPL049
                                                                        DGPL050
CHECK v.3.2-->                                                          DGPL051
      DATA FIRST  / .TRUE. /                                            DGPL052
CHECK v.3.2<--                                                          DGPL053
                                                                        DGPL054
                                                                        DGPL055
C---- Initialise variables according to the type of plot                DGPL056
      IF (BTYPE.EQ.'L') THEN                                            DGPL057
          DGLIM = DGLIML                                                DGPL058
          HEADIN = 'Main-chain bond lengths'                            DGPL059
          HLEN = 23                                                     DGPL060
          NDGACR = NDGACL                                               DGPL061
          XWIDTH = DGWIDX / NDGACL                                      DGPL062
          YHIGHT = XWIDTH / DGASPL                                      DGPL063
          NLEFT = TODO(1)                                               DGPL064
      ELSE IF (BTYPE.EQ.'A') THEN                                       DGPL065
          DGLIM = DGLIMA                                                DGPL066
          HEADIN = 'Main-chain bond angles'                             DGPL067
          HLEN = 22                                                     DGPL068
          NDGACR = NDGACA                                               DGPL069
          XWIDTH = DGWIDX / NDGACA                                      DGPL070
          YHIGHT = XWIDTH / DGASPA                                      DGPL071
          NLEFT = TODO(2)                                               DGPL072
      ELSE                                                              DGPL073
          HEADIN = 'Planar groups'                                      DGPL074
          HLEN = 13                                                     DGPL075
          NDGACR = NDGACP                                               DGPL076
          XWIDTH = DGWIDX / NDGACP                                      DGPL077
          YHIGHT = XWIDTH / DGASPP                                      DGPL078
          NLEFT = TODO(3)                                               DGPL079
      ENDIF                                                             DGPL080
CHECK v.3.2-->                                                          DGPL081
      LAST = .FALSE.                                                    DGPL082
CHECK v.3.2<--                                                          DGPL083
                                                                        DGPL084
C---- If plotting distorted planes, then open planes file               DGPL085
      IF (BTYPE.EQ.'P') THEN                                            DGPL086
          OPEN(UNIT=3, FILE=FILPLN, STATUS='OLD', FORM='FORMATTED',     DGPL087
     &        ACCESS='SEQUENTIAL',                                      DGPL088
CVAX     -        READONLY,                                             DGPL089
     &        ERR=900)                                                  DGPL090
          LINE = 0                                                      DGPL091
                                                                        DGPL092
C---- Otherwise rewind the bond lengths/angles files, and skip the      DGPL093
C     header records                                                    DGPL094
      ELSE                                                              DGPL095
          REWIND(2)                                                     DGPL096
          LINE = 0                                                      DGPL097
          DO 100, IMAIN = 1, NALP                                       DGPL098
              READ(2,*,END=902)                                         DGPL099
              LINE = LINE + 1                                           DGPL100
 100      CONTINUE                                                      DGPL101
      ENDIF                                                             DGPL102
                                                                        DGPL103
C---- Initialise variables                                              DGPL104
      CONTD = ' '                                                       DGPL105
      DGX = 0.0                                                         DGPL106
      ENDFIL = .FALSE.                                                  DGPL107
      FOUND = .FALSE.                                                   DGPL108
      IACROS = NDGACR                                                   DGPL109
      NDONE = 0                                                         DGPL110
      PRHEAD = .TRUE.                                                   DGPL111
                                                                        DGPL112
C---- Loop until end of appropriate file reached                        DGPL113
 200  CONTINUE                                                          DGPL114
                                                                        DGPL115
C----     If plotting planar groups get the next group to be plotted    DGPL116
          IF (BTYPE.EQ.'P') THEN                                        DGPL117
              CALL REAPLN(LINE,VALUE,IMAIN,INCHN,RESNAM,SEQNO,FOUND,    DGPL118
     &            COORD,ANAME,NATOM,ENDFIL)                             DGPL119
              IF (IFAIL) GO TO 999                                      DGPL120
                                                                        DGPL121
C----     Otherwise, get the next bond length/angle to be plotted       DGPL122
          ELSE                                                          DGPL123
              CALL REALAN(BTYPE,LINE,DGLIM,VALUE,EVALUE,IMAIN,INCHN,    DGPL124
CHECK v.3.2-->                                                          DGPL125
C     -            RESNAM,SEQNO,ENDFIL)                                 DGPL126
     &            RESNAM,SEQNO,ENDFIL,OTHCHN,OTHNAM,OTHSEQ)             DGPL127
CHECK v.3.2<--                                                          DGPL128
              IF (IFAIL) GO TO 999                                      DGPL129
              DIFF = ABS(VALUE - EVALUE)                                DGPL130
              IF (.NOT.ENDFIL) THEN                                     DGPL131
                  FOUND = .TRUE.                                        DGPL132
              ELSE                                                      DGPL133
                  FOUND = .FALSE.                                       DGPL134
              ENDIF                                                     DGPL135
          ENDIF                                                         DGPL136
                                                                        DGPL137
C----     Skip this one if it doesn't belong to the required chain      DGPL138
          IF (CHAIN.NE.' ' .AND. INCHN.NE.CHAIN) FOUND = .FALSE.        DGPL139
                                                                        DGPL140
C----     If have a valid value, then plot this distorted bond/angle    DGPL141
          IF (FOUND) THEN                                               DGPL142
                                                                        DGPL143
C----         Increment x- and y-values for next box                    DGPL144
              IACROS = IACROS + 1                                       DGPL145
                                                                        DGPL146
C----         If at end of line, start new line                         DGPL147
              IF (IACROS.GT.NDGACR) THEN                                DGPL148
                                                                        DGPL149
C----             Calculate y-position of bottom of box                 DGPL150
                  YTEST = DGY - YHIGHT                                  DGPL151
                                                                        DGPL152
C----             If printing a heading, add the size of that           DGPL153
                  IF (PRHEAD) YTEST = YTEST - DGTEX * DGWIDY            DGPL154
                                                                        DGPL155
C----             If this is the last row of the current set of plots,  DGPL156
C                 then allow for size of explanatory note at bottom, tooDGPL157
                  IF (NLEFT.LE.NDGACR) THEN                             DGPL158
                      YTEST = YTEST - DGNOTE * DGWIDY                   DGPL159
                  ENDIF                                                 DGPL160
                                                                        DGPL161
C----             If this will fall off the bottom of the page, then    DGPL162
C                 start a new one                                       DGPL163
                  IF (YTEST.LT.DGMRGY) THEN                             DGPL164
                                                                        DGPL165
C----                 If this is not the first page, then finish off    DGPL166
C                     previous page                                     DGPL167
                      IF (IPAGE.GT.0) THEN                              DGPL168
CHECK v.3.2-->                                                          DGPL169
C                          CALL DISEND                                  DGPL170
                          CALL DISEND(LAST)                             DGPL171
CHECK v.3.2<--                                                          DGPL172
                      ENDIF                                             DGPL173
                                                                        DGPL174
C----                 Open new PostScript file and set flag to print    DGPL175
C                     heading                                           DGPL176
CHECK v.3.1-->                                                          DGPL177
C                      CALL DISHED(IPAGE)                               DGPL178
                      CALL DISHED(IPAGE,PLDESC,FIRST)                   DGPL179
                      FIRST = .FALSE.                                   DGPL180
                      PLDESC = ' '                                      DGPL181
CHECK v.3.1<--                                                          DGPL182
                      DGY = DGMRGY + DGWIDY                             DGPL183
                      PRHEAD = .TRUE.                                   DGPL184
                  ENDIF                                                 DGPL185
                                                                        DGPL186
C----             Print the heading if required                         DGPL187
                  IF (PRHEAD) THEN                                      DGPL188
                      DGX = DGMRGX                                      DGPL189
                      DGY = DGY - DGTEX * DGWIDY                        DGPL190
                      YPOS = DGY + DGTEX * DGWIDY * DGHDOF              DGPL191
                      CALL PSTEXT(DGX,YPOS,DGHDSZ,HEADIN(1:HLEN) //     DGPL192
     &                    CONTD)                                        DGPL193
                      CONTD = ' (contd)'                                DGPL194
                      PRHEAD = .FALSE.                                  DGPL195
                  ENDIF                                                 DGPL196
                                                                        DGPL197
C----             Prepare for new row of boxes                          DGPL198
                  DGY = DGY - YHIGHT                                    DGPL199
                  IACROS = 1                                            DGPL200
              ENDIF                                                     DGPL201
                                                                        DGPL202
C----         Calculate graph position and draw axes                    DGPL203
              NDONE = NDONE + 1                                         DGPL204
              NLEFT = NLEFT - 1                                         DGPL205
              DGX = DGMRGX + (IACROS - 1) * XWIDTH                      DGPL206
              CALL PSLWID(0.1)                                          DGPL207
                                                                        DGPL208
C----         Change residue name to caps and lower case                DGPL209
              DO 300, IAMINO = 1, NAMINO                                DGPL210
                  IF (RESNAM.EQ.RNAME(IAMINO)) RESNAM = LNAME(IAMINO)   DGPL211
CHECK v.3.2-->                                                          DGPL212
                  IF (OTHNAM.EQ.RNAME(IAMINO)) OTHNAM = LNAME(IAMINO)   DGPL213
CHECK v.3.2<--                                                          DGPL214
 300          CONTINUE                                                  DGPL215
                                                                        DGPL216
C----         Draw appropriate bond/angle/plane                         DGPL217
              IF (BTYPE.EQ.'L') THEN                                    DGPL218
                  CALL DRWLIN(DGX,DGY,XWIDTH,YHIGHT,IMAIN,INCHN,RESNAM, DGPL219
CHECK v.3.2-->                                                          DGPL220
C     -                SEQNO,EVALUE,VALUE)                              DGPL221
     &                SEQNO,EVALUE,VALUE,OTHCHN,OTHNAM,OTHSEQ)          DGPL222
CHECK v.3.2<--                                                          DGPL223
                                                                        DGPL224
              ELSE IF (BTYPE.EQ.'A') THEN                               DGPL225
                  CALL DRWANG(DGX,DGY,XWIDTH,YHIGHT,IMAIN,INCHN,RESNAM, DGPL226
CHECK v.3.2-->                                                          DGPL227
C     -                SEQNO,EVALUE,VALUE)                              DGPL228
     &                SEQNO,EVALUE,VALUE,OTHCHN,OTHNAM,OTHSEQ)          DGPL229
CHECK v.3.2<--                                                          DGPL230
              ELSE                                                      DGPL231
                  CALL DRWPLN(DGX,DGY,XWIDTH,YHIGHT,INCHN,RESNAM,SEQNO, DGPL232
     &                VALUE,COORD,ANAME,NATOM)                          DGPL233
              ENDIF                                                     DGPL234
          ENDIF                                                         DGPL235
      IF (.NOT.ENDFIL) GO TO 200                                        DGPL236
                                                                        DGPL237
C---- Store how many distortions plotted, and print explanatory text, ifDGPL238
C     necessary                                                         DGPL239
      IF (NDONE.GT.0) THEN                                              DGPL240
          IF (BTYPE.EQ.'L') THEN                                        DGPL241
              NDONEL = NDONE                                            DGPL242
              WRITE(IREC,420) DGLIML                                    DGPL243
 420          FORMAT('Bonds differing by > ',F4.2,'A from small-mole',  DGPL244
     &            'cule values. Values shown: "ideal", difference, a',  DGPL245
     &            'ctual')                                              DGPL246
              RLEN = 96                                                 DGPL247
          ELSE IF (BTYPE.EQ.'A') THEN                                   DGPL248
              NDONEA = NDONE                                            DGPL249
              WRITE(IREC,440) DGLIMA                                    DGPL250
 440          FORMAT('Bond angles differing by >',F5.1,' degrees fro',  DGPL251
     &            'm small-molec values. Values shown: "ideal", actu',  DGPL252
     &            'al, diff.')                                          DGPL253
              RLEN = 101                                                DGPL254
          ELSE                                                          DGPL255
              NDONEP = NDONE                                            DGPL256
              WRITE(IREC,460) DGLIMR, DGLIMP                            DGPL257
 460          FORMAT('Sidechains with RMS dist. from planarity > ',     DGPL258
     &            F4.2,'A for rings, or > ',F4.2,'A otherwise. Value',  DGPL259
     &            ' shown is RMS dist.')                                DGPL260
              RLEN = 106                                                DGPL261
          ENDIF                                                         DGPL262
                                                                        DGPL263
C----     Write the basis on which the plotted bonds have been selected DGPL264
          DGX = DGMRGX                                                  DGPL265
          DGY = DGY - DGNOTE * DGWIDY                                   DGPL266
          CALL PSTEXT(DGX,DGY,10.0,IREC(1:RLEN))                        DGPL267
      ENDIF                                                             DGPL268
                                                                        DGPL269
      GO TO 999                                                         DGPL270
                                                                        DGPL271
                                                                        DGPL272
C---- Fatal errors                                                      DGPL273
 900  CONTINUE                                                          DGPL274
      PRINT*, '*** ERROR. Unable to open .pln file'                     DGPL275
      GO TO 990                                                         DGPL276
                                                                        DGPL277
 902  CONTINUE                                                          DGPL278
      PRINT*, '*** ERROR. Premature EOF in .lan file at line:',         DGPL279
     &    LINE + 1, '   [2]'                                            DGPL280
      GO TO 990                                                         DGPL281
                                                                        DGPL282
990   CONTINUE                                                          DGPL283
      IFAIL = .TRUE.                                                    DGPL284
                                                                        DGPL285
999   CONTINUE                                                          DGPL286
      RETURN                                                            DGPL287
      END                                                               DGPL288
